]> git.tuebingen.mpg.de Git - paraslash.git/blob - ogg_afh_common.c
Revert "Create three ogg pages when skipping vorbis comments."
[paraslash.git] / ogg_afh_common.c
1 /*
2  * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ogg_afh_common.c Functions common to ogg/vorbis and ogg/speex. */
8
9 #include <ogg/ogg.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "string.h"
16 #include "ogg_afh_common.h"
17
18
19 /* Taken from decoder_example.c of libvorbis-1.2.3. */
20 static int process_packets_2_and_3(ogg_sync_state *oss,
21                 ogg_stream_state *stream, struct afh_info *afhi,
22                 struct ogg_afh_callback_info *ci)
23 {
24         ogg_page page;
25         ogg_packet packet;
26         int i = 0;
27
28         while (i < 2) {
29                 while (i < 2) {
30                         int ret = ogg_sync_pageout(oss, &page);
31                         if (ret == 0)
32                                 break; /* Need more data */
33                         if (ret != 1)
34                                 continue;
35                         /*
36                          * We can ignore any errors here as they'll also become
37                          * apparent at packetout.
38                          */
39                         ogg_stream_pagein(stream, &page);
40                         PARA_INFO_LOG("ogg page serial: %d\n",
41                                 ogg_page_serialno(&page));
42                         while (i < 2) {
43                                 ret = ogg_stream_packetout(stream, &packet);
44                                 if (ret == 0)
45                                         break;
46                                 if (ret < 0)
47                                         return -E_STREAM_PACKETOUT;
48                                 ret = ci->packet_callback(&packet, i + 1,
49                                         ogg_page_serialno(&page), afhi,
50                                         ci->private_data);
51                                 if (ret < 0)
52                                         return ret;
53                                 if (ret == 0) /* header complete */
54                                         return 1;
55                                 i++;
56                         }
57                 }
58         }
59         return 1;
60 }
61
62 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
63                 struct ogg_afh_callback_info *ci)
64 {
65         ogg_packet packet;
66         ogg_stream_state stream;
67         ogg_page page;
68         int ret;
69
70         if (ogg_sync_pageout(oss, &page) != 1)
71                 return -E_SYNC_PAGEOUT;
72
73         ret = ogg_page_serialno(&page);
74         ogg_stream_init(&stream, ret);
75
76         ret = -E_STREAM_PAGEIN;
77         if (ogg_stream_pagein(&stream, &page) < 0)
78                 goto out;
79
80         ret = -E_STREAM_PACKETOUT;
81         if (ogg_stream_packetout(&stream, &packet) != 1)
82                 goto out;
83         ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
84                 afhi, ci->private_data);
85         if (ret < 0)
86                 goto out;
87         ret = process_packets_2_and_3(oss, &stream, afhi, ci);
88         if (ret < 0)
89                 goto out;
90         ret = 1;
91 out:
92         ogg_stream_clear(&stream);
93         return ret;
94 }
95
96 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
97                 struct timeval *result)
98 {
99         uint64_t x = (uint64_t)num_frames * 1000 * 1000
100                 / frequency / num_chunks;
101
102         result->tv_sec = x / 1000 / 1000;
103         result->tv_usec = x % (1000 * 1000);
104         PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
105                 tv2ms(result));
106 }
107
108 /**
109  * Pass first three ogg packets to callback and build the chunk table.
110  *
111  * This function extracts the first three ogg packets of the audio data
112  * given by \a map and \a numbytes and passes each packet to the callback
113  * defined by \a ci.
114  *
115  * If the packet callback indicates success and \a afhi is not \p NULL, the
116  * chunk table is built. Chunk zero contains the first three ogg packets while
117  * all other chunks consist of exactly one ogg page.
118  *
119  * \param map Audio file data.
120  * \param numbytes The length of \a map.
121  * \param afhi Passed to the packet callback, contains chunk table.
122  * \param ci The callback structure.
123  *
124  * \return Standard.
125  */
126 int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
127                 struct ogg_afh_callback_info *ci)
128 {
129         ogg_sync_state oss;
130         ogg_page op;
131         long len = numbytes;
132         char *buf;
133         int ret, i, j, frames_per_chunk, ct_size;
134         long long unsigned num_frames = 0;
135
136         ogg_sync_init(&oss);
137         ret = -E_OGG_SYNC;
138         buf = ogg_sync_buffer(&oss, len);
139         if (!buf)
140                 goto out;
141         memcpy(buf, map, len);
142         ret = -E_OGG_SYNC;
143         if (ogg_sync_wrote(&oss, len) < 0)
144                 goto out;
145         ret = process_ogg_packets(&oss, afhi, ci);
146         if (ret < 0)
147                 goto out;
148         if (!afhi)
149                 goto out;
150         afhi->header_len = oss.returned;
151         oss.returned = 0;
152         oss.fill = numbytes;
153         /* count ogg packages and get duration of the file */
154         for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
155                 num_frames = ogg_page_granulepos(&op);
156         PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
157         ret = -E_OGG_EMPTY;
158         if (i == 0)
159                 goto out;
160         afhi->seconds_total = num_frames / afhi->frequency;
161         /* use roughly one page per chunk */
162         frames_per_chunk = num_frames / i;
163         PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
164                 afhi->seconds_total, frames_per_chunk);
165         ct_size = 250;
166         afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
167         afhi->chunk_table[0] = 0;
168         afhi->chunk_table[1] = afhi->header_len;
169         oss.returned = afhi->header_len;
170         oss.fill = numbytes;
171         for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
172                 int granule = ogg_page_granulepos(&op);
173
174                 while (granule > j * frames_per_chunk) {
175                         j++;
176                         if (j >= ct_size) {
177                                 ct_size *= 2;
178                                 afhi->chunk_table = para_realloc(
179                                         afhi->chunk_table,
180                                         ct_size * sizeof(uint32_t));
181                         }
182                         afhi->chunk_table[j] = oss.returned;
183                 }
184         }
185         afhi->chunks_total = j;
186         set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
187         ret = 0;
188 out:
189         ogg_sync_clear(&oss);
190         return ret;
191 }