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)
132 int ret, i, j, frames_per_chunk, ct_size;
133 long long unsigned num_frames = 0;
137 buf = ogg_sync_buffer(&oss, len);
140 memcpy(buf, map, len);
142 if (ogg_sync_wrote(&oss, len) < 0)
144 ret = process_ogg_packets(&oss, afhi, ci);
149 afhi->header_len = oss.returned;
152 /* count ogg pages and get duration of the file */
153 for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
154 num_frames = ogg_page_granulepos(&op);
155 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
159 afhi->seconds_total = num_frames / afhi->frequency;
160 /* use roughly one page per chunk */
161 frames_per_chunk = num_frames / i;
162 PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
163 afhi->seconds_total, frames_per_chunk);
165 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
166 afhi->chunk_table[0] = 0;
167 afhi->chunk_table[1] = afhi->header_len;
168 oss.returned = afhi->header_len;
170 for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
171 int granule = ogg_page_granulepos(&op);
173 while (granule >= (j + 1) * frames_per_chunk) {
177 afhi->chunk_table = para_realloc(
179 ct_size * sizeof(uint32_t));
181 afhi->chunk_table[j] = oss.returned;
184 afhi->chunks_total = j;
185 set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
188 ogg_sync_clear(&oss);