2 * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ogg_afh_common.c Functions common to ogg/vorbis and ogg/speex. */
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
);
41 ret
= ogg_stream_packetout(stream
, &packet
);
45 return -E_STREAM_PACKETOUT
;
46 ret
= ci
->packet_callback(&packet
, i
+ 1, afhi
,
50 if (ret
== 0) /* header complete */
59 static int process_ogg_packets(ogg_sync_state
*oss
, struct afh_info
*afhi
,
60 struct ogg_afh_callback_info
*ci
)
63 ogg_stream_state stream
;
67 if (ogg_sync_pageout(oss
, &page
) != 1)
68 return -E_SYNC_PAGEOUT
;
70 ret
= ogg_page_serialno(&page
);
71 ogg_stream_init(&stream
, ret
);
73 ret
= -E_STREAM_PAGEIN
;
74 if (ogg_stream_pagein(&stream
, &page
) < 0)
77 ret
= -E_STREAM_PACKETOUT
;
78 if (ogg_stream_packetout(&stream
, &packet
) != 1)
80 ret
= ci
->packet_callback(&packet
, 0, afhi
, ci
->private_data
);
83 ret
= process_packets_2_and_3(oss
, &stream
, afhi
, ci
);
86 afhi
->header_offset
= 0;
87 afhi
->header_len
= oss
->returned
;
90 ogg_stream_clear(&stream
);
94 static void set_chunk_tv(int num_frames
, int num_chunks
, int frequency
,
95 struct timeval
*result
)
97 uint64_t x
= (uint64_t)num_frames
* 1000 * 1000
98 / frequency
/ num_chunks
;
100 result
->tv_sec
= x
/ 1000 / 1000;
101 result
->tv_usec
= x
% (1000 * 1000);
102 PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks
,
107 * Pass first three ogg packets to callback and build the chunk table.
109 * This function extracts the first three ogg packets of the audio data
110 * given by \a map and \a numbytes and passes each packet to the callback
113 * If the packet callback indicates success, the chunk table is built. Chunk
114 * zero contains the first three ogg packets while all other chunks consist of
115 * exactly one ogg page.
117 * \param map Audio file data.
118 * \param numbytes The length of \a map.
119 * \param afhi Passed to the packet callback, contains chunk table.
120 * \param ci The callback structure.
124 int ogg_get_file_info(char *map
, size_t numbytes
, struct afh_info
*afhi
,
125 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
, len
);
139 memcpy(buf
, map
, len
);
141 if (ogg_sync_wrote(&oss
, len
) < 0)
143 ret
= process_ogg_packets(&oss
, afhi
, ci
);
148 /* count ogg pages and get duration of the file */
149 for (i
= 0; ogg_sync_pageseek(&oss
, &op
) > 0; i
++)
150 num_frames
= ogg_page_granulepos(&op
);
151 PARA_INFO_LOG("%d pages, %llu frames\n", i
, num_frames
);
155 afhi
->seconds_total
= num_frames
/ afhi
->frequency
;
156 /* use roughly one page per chunk */
157 frames_per_chunk
= num_frames
/ i
;
158 PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
159 afhi
->seconds_total
, frames_per_chunk
);
161 afhi
->chunk_table
= para_malloc(ct_size
* sizeof(uint32_t));
162 afhi
->chunk_table
[0] = 0;
163 afhi
->chunk_table
[1] = afhi
->header_len
;
164 oss
.returned
= afhi
->header_len
;
166 for (j
= 1; ogg_sync_pageseek(&oss
, &op
) > 0; /* nothing */) {
167 int granule
= ogg_page_granulepos(&op
);
169 while (granule
> j
* frames_per_chunk
) {
173 afhi
->chunk_table
= para_realloc(
175 ct_size
* sizeof(uint32_t));
177 afhi
->chunk_table
[j
] = oss
.returned
;
180 afhi
->chunks_total
= j
;
181 set_chunk_tv(num_frames
, j
, afhi
->frequency
, &afhi
->chunk_tv
);
184 ogg_sync_clear(&oss
);