2 * Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ogg_afh_common.c Functions common to all ogg/ codecs. */
16 #include "ogg_afh_common.h"
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)
30 int ret = ogg_sync_pageout(oss, &page);
32 break; /* Need more data */
36 * We can ignore any errors here as they'll also become
37 * apparent at packetout.
39 ogg_stream_pagein(stream, &page);
40 PARA_INFO_LOG("ogg page serial: %d\n",
41 ogg_page_serialno(&page));
43 ret = ogg_stream_packetout(stream, &packet);
47 return -E_STREAM_PACKETOUT;
48 ret = ci->packet_callback(&packet, i + 1,
49 ogg_page_serialno(&page), afhi,
53 if (ret == 0) /* header complete */
62 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
63 struct ogg_afh_callback_info *ci)
66 ogg_stream_state stream;
70 if (ogg_sync_pageout(oss, &page) != 1)
71 return -E_SYNC_PAGEOUT;
73 ret = ogg_page_serialno(&page);
74 ogg_stream_init(&stream, ret);
76 ret = -E_STREAM_PAGEIN;
77 if (ogg_stream_pagein(&stream, &page) < 0)
80 ret = -E_STREAM_PACKETOUT;
81 if (ogg_stream_packetout(&stream, &packet) != 1)
83 ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
84 afhi, ci->private_data);
87 ret = process_packets_2_and_3(oss, &stream, afhi, ci);
92 ogg_stream_clear(&stream);
96 static void set_chunk_tv(int frames_per_chunk, int frequency,
97 struct timeval *result)
99 uint64_t x = (uint64_t)frames_per_chunk * 1000 * 1000 / frequency;
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));
108 * Pass first three ogg packets to callback and build the chunk table.
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
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.
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.
125 int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
126 struct ogg_afh_callback_info *ci)
131 int ret, i, j, frames_per_chunk, ct_size;
132 long long unsigned num_frames = 0;
136 buf = ogg_sync_buffer(&oss, numbytes);
139 memcpy(buf, map, numbytes);
141 if (ogg_sync_wrote(&oss, numbytes) < 0)
143 ret = process_ogg_packets(&oss, afhi, ci);
148 afhi->header_len = oss.returned;
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);
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);
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;
169 for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
170 int granule = ogg_page_granulepos(&op);
172 while (granule >= (j + 1) * frames_per_chunk) {
176 afhi->chunk_table = para_realloc(
178 ct_size * sizeof(uint32_t));
180 afhi->chunk_table[j] = oss.returned;
183 afhi->chunks_total = j;
184 set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
187 ogg_sync_clear(&oss);
191 static int write_ogg_page(int fd, const ogg_page *op)
195 PARA_DEBUG_LOG("header/body: %li/%li\n", op->header_len, op->body_len);
196 ret = xwrite(fd, (const char *)op->header, op->header_len);
199 return xwrite(fd, (const char *)op->body, op->body_len);
203 * Change meta tags of ogg files.
205 * \param map The (read-only) memory map of the input file.
206 * \param map_sz The size of the input file in bytes.
207 * \param fd The output file descriptor.
208 * \param meta_packet Codec-specific packet containing modified tags.
209 * \param meta_sz Size of the metadata packet.
211 * This function writes a new ogg file content using file descriptor \a fd,
212 * which must correspond to a file which has been opened for writing. The
213 * second packet is supposed to contain the metadata, and is replaced by \a
214 * meta_packet. This output file has to be closed by the caller.
218 int ogg_rewrite_tags(const char *map, size_t map_sz, int fd,
219 char *meta_packet, size_t meta_sz)
221 ogg_sync_state oss_in, oss_out;
222 ogg_stream_state stream_in, stream_out, *si = NULL, *so = NULL;
229 ogg_sync_init(&oss_in);
230 ogg_sync_init(&oss_out);
233 buf = ogg_sync_buffer(&oss_in, len);
236 memcpy(buf, map, len);
238 if (ogg_sync_wrote(&oss_in, len) < 0)
240 if (ogg_sync_pageout(&oss_in, &op) != 1)
242 ret = ogg_page_serialno(&op);
246 ogg_stream_init(si, serial);
247 /* Packet #0 goes to an own page */
248 ret = -E_STREAM_PAGEIN;
249 if (ogg_stream_pagein(si, &op) < 0)
251 ret = -E_STREAM_PACKETOUT;
252 if (ogg_stream_packetout(si, &packet) != 1)
254 ret = -E_STREAM_PACKETIN;
256 ogg_stream_init(so, serial);
257 if (ogg_stream_packetin(so, &packet) != 0)
259 ret = ogg_stream_flush(so, &op);
261 /* packets have been flushed into the page. */
262 ret = write_ogg_page(fd, &op);
266 * For all supported ogg/xxx audio formats the meta data packet is
267 * packet #1. Write out our modified version of this packet.
270 packet.b_o_s = packet.e_o_s = 0;
271 packet.packet = (typeof(packet.packet))meta_packet;
272 packet.bytes = meta_sz;
273 ret = -E_STREAM_PACKETIN;
274 if (ogg_stream_packetin(so, &packet) != 0)
276 /* Copy ogg packets, ignoring the meta data packet. */
278 ret = ogg_stream_packetout(si, &packet);
282 ret = -E_STREAM_PAGEOUT;
283 if (ogg_sync_pageout(&oss_in, &op) < 0)
285 ret = -E_STREAM_PAGEIN;
286 if (ogg_stream_pagein(si, &op))
290 PARA_DEBUG_LOG("packet: bytes: %d, granule: %d, packetno: %d\n",
291 (int)packet.bytes, (int)packet.granulepos,
292 (int)packet.packetno);
293 /* ignore meta data packet which we replaced */
294 if (packet.packetno == 1)
296 ret = -E_STREAM_PACKETIN;
297 if (ogg_stream_packetin(so, &packet) != 0)
299 /* only create a new ogg page if granulepos is valid */
300 if (packet.granulepos == -1)
302 /* force remaining packets into a page */
304 #ifdef HAVE_OGG_STREAM_FLUSH_FILL
305 ret = ogg_stream_flush_fill(so, &op, INT_MAX);
307 ret = ogg_stream_flush(so, &op);
311 PARA_DEBUG_LOG("writing page (%li bytes)\n",
312 op.header_len + op.body_len);
313 ret = write_ogg_page(fd, &op);
318 if (ogg_stream_flush(so, &op)) {
319 /* write remaining data */
320 ret = write_ogg_page(fd, &op);
326 ogg_sync_clear(&oss_in);
327 ogg_sync_clear(&oss_out);
329 ogg_stream_clear(si);
331 ogg_stream_clear(so);