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