1 /* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file ogg_afh_common.c Functions common to all ogg/ codecs. */
12 #include "ogg_afh_common.h"
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)
26 int ret = ogg_sync_pageout(oss, &page);
28 break; /* Need more data */
32 * We can ignore any errors here as they'll also become
33 * apparent at packetout.
35 ogg_stream_pagein(stream, &page);
36 PARA_INFO_LOG("ogg page serial: %d\n",
37 ogg_page_serialno(&page));
39 ret = ogg_stream_packetout(stream, &packet);
43 return -E_STREAM_PACKETOUT;
44 ret = ci->packet_callback(&packet, i + 1,
45 ogg_page_serialno(&page), afhi,
49 if (ret == 0) /* header complete */
58 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
59 struct oac_callback_info *ci)
62 ogg_stream_state stream;
66 if (ogg_sync_pageout(oss, &page) != 1)
67 return -E_SYNC_PAGEOUT;
69 ret = ogg_page_serialno(&page);
70 ogg_stream_init(&stream, ret);
72 ret = -E_STREAM_PAGEIN;
73 if (ogg_stream_pagein(&stream, &page) < 0)
76 ret = -E_STREAM_PACKETOUT;
77 if (ogg_stream_packetout(&stream, &packet) != 1)
79 ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
80 afhi, ci->private_data);
83 ret = process_packets_2_and_3(oss, &stream, afhi, ci);
88 ogg_stream_clear(&stream);
92 static void set_chunk_tv(int frames_per_chunk, int frequency,
93 struct timeval *result)
95 uint64_t x = (uint64_t)frames_per_chunk * 1000 * 1000 / frequency;
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));
104 * Pass first three ogg packets to callback and build the chunk table.
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
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.
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.
121 int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
122 struct oac_callback_info *ci)
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;
133 buf = ogg_sync_buffer(&oss, numbytes);
136 memcpy(buf, map, numbytes);
138 if (ogg_sync_wrote(&oss, numbytes) < 0)
140 ret = process_ogg_packets(&oss, afhi, ci);
145 afhi->header_len = oss.returned;
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 int64_t this_granule = ogg_page_granulepos(&op);
152 if (this_granule >= 0)
153 granule = this_granule;
154 if (i > 0 && this_pageno != prev_pageno + 1) /* hole */
155 granule_skip += granule - prev_granule;
156 prev_pageno = this_pageno;
157 prev_granule = granule;
159 num_frames = granule - granule_skip;
160 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
164 afhi->seconds_total = num_frames / afhi->frequency;
165 /* use roughly one page per chunk */
166 frames_per_chunk = num_frames / i;
167 PARA_INFO_LOG("%" PRIu32 " seconds, %d frames/chunk\n",
168 afhi->seconds_total, frames_per_chunk);
170 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
171 afhi->chunk_table[0] = 0;
172 afhi->chunk_table[1] = afhi->header_len;
173 oss.returned = afhi->header_len;
175 for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
176 granule = ogg_page_granulepos(&op);
178 while (granule >= (j + 1) * frames_per_chunk) {
182 afhi->chunk_table = para_realloc(
184 ct_size * sizeof(uint32_t));
186 afhi->chunk_table[j] = oss.returned;
189 afhi->chunks_total = j;
190 set_max_chunk_size(afhi);
191 set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
194 ogg_sync_clear(&oss);
198 static int write_ogg_page(int fd, const ogg_page *op)
202 PARA_DEBUG_LOG("header/body: %li/%li\n", op->header_len, op->body_len);
203 ret = xwrite(fd, (const char *)op->header, op->header_len);
206 return xwrite(fd, (const char *)op->body, op->body_len);
210 * Change meta tags of ogg files.
212 * \param map The (read-only) memory map of the input file.
213 * \param map_sz The size of the input file in bytes.
214 * \param fd The output file descriptor.
215 * \param meta_packet Codec-specific packet containing modified tags.
216 * \param meta_sz Size of the metadata packet.
218 * This function writes a new ogg file content using file descriptor \a fd,
219 * which must correspond to a file which has been opened for writing. The
220 * second packet is supposed to contain the metadata, and is replaced by \a
221 * meta_packet. This output file has to be closed by the caller.
225 int oac_rewrite_tags(const char *map, size_t map_sz, int fd,
226 char *meta_packet, size_t meta_sz)
228 ogg_sync_state oss_in, oss_out;
229 ogg_stream_state stream_in, stream_out, *si = NULL, *so = NULL;
236 ogg_sync_init(&oss_in);
237 ogg_sync_init(&oss_out);
240 buf = ogg_sync_buffer(&oss_in, len);
243 memcpy(buf, map, len);
245 if (ogg_sync_wrote(&oss_in, len) < 0)
247 if (ogg_sync_pageout(&oss_in, &op) != 1)
249 ret = ogg_page_serialno(&op);
253 ogg_stream_init(si, serial);
254 /* Packet #0 goes to an own page */
255 ret = -E_STREAM_PAGEIN;
256 if (ogg_stream_pagein(si, &op) < 0)
258 ret = -E_STREAM_PACKETOUT;
259 if (ogg_stream_packetout(si, &packet) != 1)
261 ret = -E_STREAM_PACKETIN;
263 ogg_stream_init(so, serial);
264 if (ogg_stream_packetin(so, &packet) != 0)
266 ret = ogg_stream_flush(so, &op);
268 /* packets have been flushed into the page. */
269 ret = write_ogg_page(fd, &op);
273 * For all supported ogg/xxx audio formats the meta data packet is
274 * packet #1. Write out our modified version of this packet.
277 packet.b_o_s = packet.e_o_s = 0;
278 packet.packet = (typeof(packet.packet))meta_packet;
279 packet.bytes = meta_sz;
280 ret = -E_STREAM_PACKETIN;
281 if (ogg_stream_packetin(so, &packet) != 0)
283 /* Copy ogg packets, ignoring the meta data packet. */
285 ret = ogg_stream_packetout(si, &packet);
289 ret = -E_STREAM_PAGEOUT;
290 if (ogg_sync_pageout(&oss_in, &op) < 0)
292 ret = -E_STREAM_PAGEIN;
293 if (ogg_stream_pagein(si, &op))
297 PARA_DEBUG_LOG("packet: bytes: %d, granule: %d, packetno: %d\n",
298 (int)packet.bytes, (int)packet.granulepos,
299 (int)packet.packetno);
300 /* ignore meta data packet which we replaced */
301 if (packet.packetno == 1)
303 ret = -E_STREAM_PACKETIN;
304 if (ogg_stream_packetin(so, &packet) != 0)
306 /* only create a new ogg page if granulepos is valid */
307 if (packet.granulepos == -1)
309 /* force remaining packets into a page */
311 #ifdef HAVE_OGG_STREAM_FLUSH_FILL
312 ret = ogg_stream_flush_fill(so, &op, INT_MAX);
314 ret = ogg_stream_flush(so, &op);
318 PARA_DEBUG_LOG("writing page (%li bytes)\n",
319 op.header_len + op.body_len);
320 ret = write_ogg_page(fd, &op);
325 if (ogg_stream_flush(so, &op)) {
326 /* write remaining data */
327 ret = write_ogg_page(fd, &op);
333 ogg_sync_clear(&oss_in);
334 ogg_sync_clear(&oss_out);
336 ogg_stream_clear(si);
338 ogg_stream_clear(so);
342 /* Structure for providing custom headers for streaming. */
343 struct oac_custom_header {
346 ogg_stream_state oss;
350 * Allocate and return a custom header structure.
352 * For some audio codecs which employ the ogg container format, the server side
353 * wants to replace the meta tags at the beginning of the file because they are
354 * not needed for streaming and can be arbitrary large. The structure returned
355 * by this function is typically used as the ->private field of struct \ref
356 * oac_callback_info for \ref oac_get_file_info(). This allows the audio format
357 * handler to set up a custom header which is identical to the original header,
358 * but with the meta data part replaced by fixed length dummy contents.
360 * \return The returned memory must be initialized with the serial number of
361 * the ogg stream before ogg packets can be submitted to it. This is not done
362 * here because the header structure is allocated before \ref
363 * oac_get_file_info() is called, and the serial number is not known at this
366 * \sa \ref oac_custom_header_init().
368 struct oac_custom_header *oac_custom_header_new(void)
370 return para_calloc(sizeof(struct oac_custom_header));
374 * Set the serial number of an allocated header structure.
376 * \param serial Passed to the callback function.
377 * \param h As returned from \ref oac_custom_header_new().
379 * This function must be called before any packets are submitted.
381 void oac_custom_header_init(int serial, struct oac_custom_header *h)
383 ogg_stream_init(&h->oss, serial);
387 * Submit an ogg packet to a custom header structure.
389 * \param op The packet to append.
390 * \param h Must be initialized.
392 * The packet may be the one which was passed to the callback, or a completely
393 * different one, like a dummy metadata packet.
397 int oac_custom_header_append(ogg_packet *op, struct oac_custom_header *h)
399 return ogg_stream_packetin(&h->oss, op) < 0? -E_OGG_PACKET_IN : 1;
403 * Force remaining packets into an ogg page.
405 * \param h Should contain submitted but not yet flushed packets.
407 * This is called after the first packet has been submitted with \ref
408 * oac_custom_header_append() to make sure the first ogg page contains only
409 * this packet. Also when header processing is complete, the callbacks call
410 * this to force the previously submitted packets into a page.
412 void oac_custom_header_flush(struct oac_custom_header *h)
416 while (ogg_stream_flush(&h->oss, &og)) {
417 size_t len = og.header_len + og.body_len;
418 h->buf = para_realloc(h->buf, h->len + len);
419 memcpy(h->buf + h->len, og.header, og.header_len);
420 memcpy(h->buf + h->len + og.header_len, og.body, og.body_len);
426 * Return the custom header buffer and deallocate resources.
428 * This is called after the ogg packets which comprise the header have been
429 * submitted and flushed.
431 * \param buf Result pointer.
432 * \param h Must not be used any more after the call.
434 * \return The size of the header. This is the sum of the sizes of all ogg
435 * pages that have been flushed out.
437 size_t oac_custom_header_get(char **buf, struct oac_custom_header *h)
442 ogg_stream_clear(&h->oss);