ogg_afh_common: Fix signedness issue.
[paraslash.git] / ogg_afh_common.c
1 /* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file ogg_afh_common.c Functions common to all ogg/ codecs. */
4
5 #include <ogg/ogg.h>
6 #include <regex.h>
7
8 #include "para.h"
9 #include "afh.h"
10 #include "error.h"
11 #include "string.h"
12 #include "ogg_afh_common.h"
13 #include "fd.h"
14
15 /* Taken from decoder_example.c of libvorbis-1.2.3. */
16 static int process_packets_2_and_3(ogg_sync_state *oss,
17 ogg_stream_state *stream, struct afh_info *afhi,
18 struct oac_callback_info *ci)
19 {
20 ogg_page page;
21 ogg_packet packet;
22 int i = 0;
23
24 while (i < 2) {
25 while (i < 2) {
26 int ret = ogg_sync_pageout(oss, &page);
27 if (ret == 0)
28 break; /* Need more data */
29 if (ret != 1)
30 continue;
31 /*
32 * We can ignore any errors here as they'll also become
33 * apparent at packetout.
34 */
35 ogg_stream_pagein(stream, &page);
36 PARA_INFO_LOG("ogg page serial: %d\n",
37 ogg_page_serialno(&page));
38 while (i < 2) {
39 ret = ogg_stream_packetout(stream, &packet);
40 if (ret == 0)
41 break;
42 if (ret < 0)
43 return -E_STREAM_PACKETOUT;
44 ret = ci->packet_callback(&packet, i + 1,
45 ogg_page_serialno(&page), afhi,
46 ci->private_data);
47 if (ret < 0)
48 return ret;
49 if (ret == 0) /* header complete */
50 return 1;
51 i++;
52 }
53 }
54 }
55 return 1;
56 }
57
58 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
59 struct oac_callback_info *ci)
60 {
61 ogg_packet packet;
62 ogg_stream_state stream;
63 ogg_page page;
64 int ret;
65
66 if (ogg_sync_pageout(oss, &page) != 1)
67 return -E_SYNC_PAGEOUT;
68
69 ret = ogg_page_serialno(&page);
70 ogg_stream_init(&stream, ret);
71
72 ret = -E_STREAM_PAGEIN;
73 if (ogg_stream_pagein(&stream, &page) < 0)
74 goto out;
75
76 ret = -E_STREAM_PACKETOUT;
77 if (ogg_stream_packetout(&stream, &packet) != 1)
78 goto out;
79 ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
80 afhi, ci->private_data);
81 if (ret < 0)
82 goto out;
83 ret = process_packets_2_and_3(oss, &stream, afhi, ci);
84 if (ret < 0)
85 goto out;
86 ret = 1;
87 out:
88 ogg_stream_clear(&stream);
89 return ret;
90 }
91
92 static void set_chunk_tv(int frames_per_chunk, int frequency,
93 struct timeval *result)
94 {
95 uint64_t x = (uint64_t)frames_per_chunk * 1000 * 1000 / frequency;
96
97 result->tv_sec = x / 1000 / 1000;
98 result->tv_usec = x % (1000 * 1000);
99 PARA_INFO_LOG("%d frames per chunk, chunk time: %lums\n",
100 frames_per_chunk, tv2ms(result));
101 }
102
103 /**
104 * Pass first three ogg packets to callback and build the chunk table.
105 *
106 * This function extracts the first three ogg packets of the audio data
107 * given by \a map and \a numbytes and passes each packet to the callback
108 * defined by \a ci.
109 *
110 * If the packet callback indicates success and \a afhi is not \p NULL, the
111 * chunk table is built. Chunk zero contains the first three ogg packets while
112 * all other chunks consist of exactly one ogg page.
113 *
114 * \param map Audio file data.
115 * \param numbytes The length of \a map.
116 * \param afhi Passed to the packet callback, contains chunk table.
117 * \param ci The callback structure.
118 *
119 * \return Standard.
120 */
121 int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
122 struct oac_callback_info *ci)
123 {
124 ogg_sync_state oss;
125 ogg_page op;
126 char *buf;
127 int ret, i, j, frames_per_chunk, ct_size, prev_pageno = 0;
128 long long unsigned granule_skip = 0, num_frames = 0;
129 int64_t granule = 0, prev_granule = 0;
130
131 ogg_sync_init(&oss);
132 ret = -E_OGG_SYNC;
133 buf = ogg_sync_buffer(&oss, numbytes);
134 if (!buf)
135 goto out;
136 memcpy(buf, map, numbytes);
137 ret = -E_OGG_SYNC;
138 if (ogg_sync_wrote(&oss, numbytes) < 0)
139 goto out;
140 ret = process_ogg_packets(&oss, afhi, ci);
141 if (ret < 0)
142 goto out;
143 if (!afhi)
144 goto out;
145 afhi->header_len = oss.returned;
146 oss.returned = 0;
147 oss.fill = numbytes;
148 /* count ogg pages and get duration of the file */
149 for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++) {
150 int this_pageno = ogg_page_pageno(&op);
151
152 granule = ogg_page_granulepos(&op);
153 if (i > 0 && this_pageno != prev_pageno + 1) /* hole */
154 granule_skip += granule - prev_granule;
155 prev_pageno = this_pageno;
156 prev_granule = granule;
157 }
158 num_frames = granule - granule_skip;
159 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
160 ret = -E_OGG_EMPTY;
161 if (i == 0)
162 goto out;
163 afhi->seconds_total = num_frames / afhi->frequency;
164 /* use roughly one page per chunk */
165 frames_per_chunk = num_frames / i;
166 PARA_INFO_LOG("%" PRIu32 " seconds, %d frames/chunk\n",
167 afhi->seconds_total, frames_per_chunk);
168 ct_size = 250;
169 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
170 afhi->chunk_table[0] = 0;
171 afhi->chunk_table[1] = afhi->header_len;
172 oss.returned = afhi->header_len;
173 oss.fill = numbytes;
174 for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
175 granule = ogg_page_granulepos(&op);
176
177 while (granule >= (j + 1) * frames_per_chunk) {
178 j++;
179 if (j >= ct_size) {
180 ct_size *= 2;
181 afhi->chunk_table = para_realloc(
182 afhi->chunk_table,
183 ct_size * sizeof(uint32_t));
184 }
185 afhi->chunk_table[j] = oss.returned;
186 }
187 }
188 afhi->chunks_total = j;
189 set_max_chunk_size(afhi);
190 set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
191 ret = 0;
192 out:
193 ogg_sync_clear(&oss);
194 return ret;
195 }
196
197 static int write_ogg_page(int fd, const ogg_page *op)
198 {
199 int ret;
200
201 PARA_DEBUG_LOG("header/body: %li/%li\n", op->header_len, op->body_len);
202 ret = xwrite(fd, (const char *)op->header, op->header_len);
203 if (ret < 0)
204 return ret;
205 return xwrite(fd, (const char *)op->body, op->body_len);
206 }
207
208 /**
209 * Change meta tags of ogg files.
210 *
211 * \param map The (read-only) memory map of the input file.
212 * \param map_sz The size of the input file in bytes.
213 * \param fd The output file descriptor.
214 * \param meta_packet Codec-specific packet containing modified tags.
215 * \param meta_sz Size of the metadata packet.
216 *
217 * This function writes a new ogg file content using file descriptor \a fd,
218 * which must correspond to a file which has been opened for writing. The
219 * second packet is supposed to contain the metadata, and is replaced by \a
220 * meta_packet. This output file has to be closed by the caller.
221 *
222 * \return Standard.
223 */
224 int oac_rewrite_tags(const char *map, size_t map_sz, int fd,
225 char *meta_packet, size_t meta_sz)
226 {
227 ogg_sync_state oss_in, oss_out;
228 ogg_stream_state stream_in, stream_out, *si = NULL, *so = NULL;
229 ogg_packet packet;
230 ogg_page op;
231 char *buf;
232 int serial, ret;
233 long len = map_sz;
234
235 ogg_sync_init(&oss_in);
236 ogg_sync_init(&oss_out);
237
238 ret = -E_OGG_SYNC;
239 buf = ogg_sync_buffer(&oss_in, len);
240 if (!buf)
241 goto out;
242 memcpy(buf, map, len);
243 ret = -E_OGG_SYNC;
244 if (ogg_sync_wrote(&oss_in, len) < 0)
245 goto out;
246 if (ogg_sync_pageout(&oss_in, &op) != 1)
247 goto out;
248 ret = ogg_page_serialno(&op);
249 serial = ret;
250
251 si = &stream_in;
252 ogg_stream_init(si, serial);
253 /* Packet #0 goes to an own page */
254 ret = -E_STREAM_PAGEIN;
255 if (ogg_stream_pagein(si, &op) < 0)
256 goto out;
257 ret = -E_STREAM_PACKETOUT;
258 if (ogg_stream_packetout(si, &packet) != 1)
259 goto out;
260 ret = -E_STREAM_PACKETIN;
261 so = &stream_out;
262 ogg_stream_init(so, serial);
263 if (ogg_stream_packetin(so, &packet) != 0)
264 goto out;
265 ret = ogg_stream_flush(so, &op);
266 assert(ret != 0);
267 /* packets have been flushed into the page. */
268 ret = write_ogg_page(fd, &op);
269 if (ret < 0)
270 goto out;
271 /*
272 * For all supported ogg/xxx audio formats the meta data packet is
273 * packet #1. Write out our modified version of this packet.
274 */
275 packet.packetno = 1;
276 packet.b_o_s = packet.e_o_s = 0;
277 packet.packet = (typeof(packet.packet))meta_packet;
278 packet.bytes = meta_sz;
279 ret = -E_STREAM_PACKETIN;
280 if (ogg_stream_packetin(so, &packet) != 0)
281 goto out;
282 /* Copy ogg packets, ignoring the meta data packet. */
283 for (;;) {
284 ret = ogg_stream_packetout(si, &packet);
285 if (ret == -1)
286 break;
287 if (ret != 1) {
288 ret = -E_STREAM_PAGEOUT;
289 if (ogg_sync_pageout(&oss_in, &op) < 0)
290 goto out;
291 ret = -E_STREAM_PAGEIN;
292 if (ogg_stream_pagein(si, &op))
293 goto out;
294 continue;
295 }
296 PARA_DEBUG_LOG("packet: bytes: %d, granule: %d, packetno: %d\n",
297 (int)packet.bytes, (int)packet.granulepos,
298 (int)packet.packetno);
299 /* ignore meta data packet which we replaced */
300 if (packet.packetno == 1)
301 continue;
302 ret = -E_STREAM_PACKETIN;
303 if (ogg_stream_packetin(so, &packet) != 0)
304 goto out;
305 /* only create a new ogg page if granulepos is valid */
306 if (packet.granulepos == -1)
307 continue;
308 /* force remaining packets into a page */
309 for (;;) {
310 #ifdef HAVE_OGG_STREAM_FLUSH_FILL
311 ret = ogg_stream_flush_fill(so, &op, INT_MAX);
312 #else
313 ret = ogg_stream_flush(so, &op);
314 #endif
315 if (ret <= 0)
316 break;
317 PARA_DEBUG_LOG("writing page (%li bytes)\n",
318 op.header_len + op.body_len);
319 ret = write_ogg_page(fd, &op);
320 if (ret < 0)
321 goto out;
322 }
323 }
324 if (ogg_stream_flush(so, &op)) {
325 /* write remaining data */
326 ret = write_ogg_page(fd, &op);
327 if (ret < 0)
328 goto out;
329 }
330 ret = 1;
331 out:
332 ogg_sync_clear(&oss_in);
333 ogg_sync_clear(&oss_out);
334 if (si)
335 ogg_stream_clear(si);
336 if (so)
337 ogg_stream_clear(so);
338 return ret;
339 }
340
341 /* Structure for providing custom headers for streaming. */
342 struct oac_custom_header {
343 char *buf;
344 size_t len;
345 ogg_stream_state oss;
346 };
347
348 /**
349 * Allocate and return a custom header structure.
350 *
351 * For some audio codecs which employ the ogg container format, the server side
352 * wants to replace the meta tags at the beginning of the file because they are
353 * not needed for streaming and can be arbitrary large. The structure returned
354 * by this function is typically used as the ->private field of struct \ref
355 * oac_callback_info for \ref oac_get_file_info(). This allows the audio format
356 * handler to set up a custom header which is identical to the original header,
357 * but with the meta data part replaced by fixed length dummy contents.
358 *
359 * \return The returned memory must be initialized with the serial number of
360 * the ogg stream before ogg packets can be submitted to it. This is not done
361 * here because the header structure is allocated before \ref
362 * oac_get_file_info() is called, and the serial number is not known at this
363 * point.
364 *
365 * \sa \ref oac_custom_header_init().
366 */
367 struct oac_custom_header *oac_custom_header_new(void)
368 {
369 return para_calloc(sizeof(struct oac_custom_header));
370 }
371
372 /**
373 * Set the serial number of an allocated header structure.
374 *
375 * \param serial Passed to the callback function.
376 * \param h As returned from \ref oac_custom_header_new().
377 *
378 * This function must be called before any packets are submitted.
379 */
380 void oac_custom_header_init(int serial, struct oac_custom_header *h)
381 {
382 ogg_stream_init(&h->oss, serial);
383 }
384
385 /**
386 * Submit an ogg packet to a custom header structure.
387 *
388 * \param op The packet to append.
389 * \param h Must be initialized.
390 *
391 * The packet may be the one which was passed to the callback, or a completely
392 * different one, like a dummy metadata packet.
393 *
394 * \return Standard.
395 */
396 int oac_custom_header_append(ogg_packet *op, struct oac_custom_header *h)
397 {
398 return ogg_stream_packetin(&h->oss, op) < 0? -E_OGG_PACKET_IN : 1;
399 }
400
401 /**
402 * Force remaining packets into an ogg page.
403 *
404 * \param h Should contain submitted but not yet flushed packets.
405 *
406 * This is called after the first packet has been submitted with \ref
407 * oac_custom_header_append() to make sure the first ogg page contains only
408 * this packet. Also when header processing is complete, the callbacks call
409 * this to force the previously submitted packets into a page.
410 */
411 void oac_custom_header_flush(struct oac_custom_header *h)
412 {
413 ogg_page og;
414
415 while (ogg_stream_flush(&h->oss, &og)) {
416 size_t len = og.header_len + og.body_len;
417 h->buf = para_realloc(h->buf, h->len + len);
418 memcpy(h->buf + h->len, og.header, og.header_len);
419 memcpy(h->buf + h->len + og.header_len, og.body, og.body_len);
420 h->len += len;
421 }
422 }
423
424 /**
425 * Return the custom header buffer and deallocate resources.
426 *
427 * This is called after the ogg packets which comprise the header have been
428 * submitted and flushed.
429 *
430 * \param buf Result pointer.
431 * \param h Must not be used any more after the call.
432 *
433 * \return The size of the header. This is the sum of the sizes of all ogg
434 * pages that have been flushed out.
435 */
436 size_t oac_custom_header_get(char **buf, struct oac_custom_header *h)
437 {
438 size_t ret = h->len;
439
440 *buf = h->buf;
441 ogg_stream_clear(&h->oss);
442 free(h);
443 return ret;
444 }