summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/libmad/frame.c
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/libmad/frame.c') (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/libmad/frame.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/core/multimedia/opieplayer/libmad/frame.c b/core/multimedia/opieplayer/libmad/frame.c
index bf15e7f..3aacb56 100644
--- a/core/multimedia/opieplayer/libmad/frame.c
+++ b/core/multimedia/opieplayer/libmad/frame.c
@@ -1,51 +1,51 @@
1/* 1/*
2 * libmad - MPEG audio decoder library 2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2001 Robert Leslie 3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software 16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * 18 *
19 * $Id$ 19 * $Id$
20 */ 20 */
21 21
22# ifdef HAVE_CONFIG_H 22# ifdef HAVE_CONFIG_H
23# include "libmad_config.h" 23# include "libmad_config.h"
24# endif 24# endif
25 25
26# include "libmad_global.h" 26# include "libmad_global.h"
27 27
28# include <stdlib.h> 28# include <stdlib.h>
29 29
30# include "bit.h" 30# include "bit.h"
31# include "stream.h" 31# include "stream.h"
32# include "frame.h" 32# include "frame.h"
33# include "timer.h" 33# include "timer.h"
34# include "layer12.h" 34# include "layer12.h"
35# include "layer3.h" 35# include "layer3.h"
36 36
37static 37static
38unsigned long const bitrate_table[5][15] = { 38unsigned long const bitrate_table[5][15] = {
39 /* MPEG-1 */ 39 /* MPEG-1 */
40 { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */ 40 { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */
41 256000, 288000, 320000, 352000, 384000, 416000, 448000 }, 41 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
42 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */ 42 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */
43 128000, 160000, 192000, 224000, 256000, 320000, 384000 }, 43 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
44 { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */ 44 { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */
45 112000, 128000, 160000, 192000, 224000, 256000, 320000 }, 45 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
46 46
47 /* MPEG-2 LSF */ 47 /* MPEG-2 LSF */
48 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */ 48 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */
49 128000, 144000, 160000, 176000, 192000, 224000, 256000 }, 49 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
50 { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */ 50 { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */
51 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */ 51 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */
@@ -165,260 +165,264 @@ int decode_header(struct mad_header *header, struct mad_stream *stream)
165 165
166 if (header->flags & MAD_FLAG_LSF_EXT) 166 if (header->flags & MAD_FLAG_LSF_EXT)
167 header->bitrate = bitrate_table[3 + (header->layer >> 1)][index]; 167 header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
168 else 168 else
169 header->bitrate = bitrate_table[header->layer - 1][index]; 169 header->bitrate = bitrate_table[header->layer - 1][index];
170 170
171 /* sampling_frequency */ 171 /* sampling_frequency */
172 index = mad_bit_read(&stream->ptr, 2); 172 index = mad_bit_read(&stream->ptr, 2);
173 173
174 if (index == 3) { 174 if (index == 3) {
175 stream->error = MAD_ERROR_BADSAMPLERATE; 175 stream->error = MAD_ERROR_BADSAMPLERATE;
176 return -1; 176 return -1;
177 } 177 }
178 178
179 header->samplerate = samplerate_table[index]; 179 header->samplerate = samplerate_table[index];
180 180
181 if (header->flags & MAD_FLAG_LSF_EXT) { 181 if (header->flags & MAD_FLAG_LSF_EXT) {
182 header->samplerate /= 2; 182 header->samplerate /= 2;
183 183
184 if (header->flags & MAD_FLAG_MPEG_2_5_EXT) 184 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
185 header->samplerate /= 2; 185 header->samplerate /= 2;
186 } 186 }
187 187
188 /* padding_bit */ 188 /* padding_bit */
189 if (mad_bit_read(&stream->ptr, 1)) 189 if (mad_bit_read(&stream->ptr, 1))
190 header->flags |= MAD_FLAG_PADDING; 190 header->flags |= MAD_FLAG_PADDING;
191 191
192 /* private_bit */ 192 /* private_bit */
193 if (mad_bit_read(&stream->ptr, 1)) 193 if (mad_bit_read(&stream->ptr, 1))
194 header->private_bits |= MAD_PRIVATE_HEADER; 194 header->private_bits |= MAD_PRIVATE_HEADER;
195 195
196 /* mode */ 196 /* mode */
197 header->mode = 3 - mad_bit_read(&stream->ptr, 2); 197 header->mode = 3 - mad_bit_read(&stream->ptr, 2);
198 198
199 /* mode_extension */ 199 /* mode_extension */
200 header->mode_extension = mad_bit_read(&stream->ptr, 2); 200 header->mode_extension = mad_bit_read(&stream->ptr, 2);
201 201
202 /* copyright */ 202 /* copyright */
203 if (mad_bit_read(&stream->ptr, 1)) 203 if (mad_bit_read(&stream->ptr, 1))
204 header->flags |= MAD_FLAG_COPYRIGHT; 204 header->flags |= MAD_FLAG_COPYRIGHT;
205 205
206 /* original/copy */ 206 /* original/copy */
207 if (mad_bit_read(&stream->ptr, 1)) 207 if (mad_bit_read(&stream->ptr, 1))
208 header->flags |= MAD_FLAG_ORIGINAL; 208 header->flags |= MAD_FLAG_ORIGINAL;
209 209
210 /* emphasis */ 210 /* emphasis */
211 header->emphasis = mad_bit_read(&stream->ptr, 2); 211 header->emphasis = mad_bit_read(&stream->ptr, 2);
212 212
213 if (header->emphasis == 2) { 213# if defined(OPT_STRICT)
214 /*
215 * ISO/IEC 11172-3 says this is a reserved emphasis value, but
216 * streams exist which use it anyway. Since the value is not important
217 * to the decoder proper, we allow it unless OPT_STRICT is defined.
218 */
219 if (header->emphasis == MAD_EMPHASIS_RESERVED) {
214 stream->error = MAD_ERROR_BADEMPHASIS; 220 stream->error = MAD_ERROR_BADEMPHASIS;
215 return -1; 221 return -1;
216 } 222 }
223# endif
217 224
218 /* error_check() */ 225 /* error_check() */
219 226
220 /* crc_check */ 227 /* crc_check */
221 if (header->flags & MAD_FLAG_PROTECTION) 228 if (header->flags & MAD_FLAG_PROTECTION)
222 header->crc_target = mad_bit_read(&stream->ptr, 16); 229 header->crc_target = mad_bit_read(&stream->ptr, 16);
223 230
224 return 0; 231 return 0;
225} 232}
226 233
227/* 234/*
228 * NAME:free_bitrate() 235 * NAME:free_bitrate()
229 * DESCRIPTION:attempt to discover the bitstream's free bitrate 236 * DESCRIPTION:attempt to discover the bitstream's free bitrate
230 */ 237 */
231static 238static
232int free_bitrate(struct mad_stream *stream, struct mad_header const *header) 239int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
233{ 240{
234 struct mad_bitptr keep_ptr; 241 struct mad_bitptr keep_ptr;
235 unsigned long rate = 0; 242 unsigned long rate = 0;
236 unsigned int pad_slot, slots_per_frame; 243 unsigned int pad_slot, slots_per_frame;
237 unsigned char const *ptr = 0; 244 unsigned char const *ptr = 0;
238 245
239 keep_ptr = stream->ptr; 246 keep_ptr = stream->ptr;
240 247
241 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; 248 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
242 slots_per_frame = (header->layer == MAD_LAYER_III && 249 slots_per_frame = (header->layer == MAD_LAYER_III &&
243 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; 250 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
244 251
245 while (mad_stream_sync(stream) == 0) { 252 while (mad_stream_sync(stream) == 0) {
246 struct mad_stream peek_stream; 253 struct mad_stream peek_stream;
247 struct mad_header peek_header; 254 struct mad_header peek_header;
248 255
249 peek_stream = *stream; 256 peek_stream = *stream;
250 peek_header = *header; 257 peek_header = *header;
251 258
252 if (decode_header(&peek_header, &peek_stream) == 0 && 259 if (decode_header(&peek_header, &peek_stream) == 0 &&
253 peek_header.layer == header->layer && 260 peek_header.layer == header->layer &&
254 peek_header.samplerate == header->samplerate) { 261 peek_header.samplerate == header->samplerate) {
255 unsigned int N; 262 unsigned int N;
256 263
257 ptr = mad_bit_nextbyte(&stream->ptr); 264 ptr = mad_bit_nextbyte(&stream->ptr);
258 265
259 N = ptr - stream->this_frame; 266 N = ptr - stream->this_frame;
260 267
261 if (header->layer == MAD_LAYER_I) { 268 if (header->layer == MAD_LAYER_I) {
262 rate = (unsigned long) header->samplerate * 269 rate = (unsigned long) header->samplerate *
263 (N - 4 * pad_slot + 4) / 48 / 1000; 270 (N - 4 * pad_slot + 4) / 48 / 1000;
264 } 271 }
265 else { 272 else {
266 rate = (unsigned long) header->samplerate * 273 rate = (unsigned long) header->samplerate *
267 (N - pad_slot + 1) / slots_per_frame / 1000; 274 (N - pad_slot + 1) / slots_per_frame / 1000;
268 } 275 }
269 276
270 if (rate >= 8) 277 if (rate >= 8)
271 break; 278 break;
272 } 279 }
273 280
274 mad_bit_skip(&stream->ptr, 8); 281 mad_bit_skip(&stream->ptr, 8);
275 } 282 }
276 283
277 stream->ptr = keep_ptr; 284 stream->ptr = keep_ptr;
278 285
279 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) { 286 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
280 stream->error = MAD_ERROR_LOSTSYNC; 287 stream->error = MAD_ERROR_LOSTSYNC;
281 return -1; 288 return -1;
282 } 289 }
283 290
284 stream->freerate = rate * 1000; 291 stream->freerate = rate * 1000;
285 292
286# if 0 && defined(DEBUG)
287 fprintf(stderr, "free bitrate == %lu\n", stream->freerate);
288# endif
289
290 return 0; 293 return 0;
291} 294}
292 295
293/* 296/*
294 * NAME:header->decode() 297 * NAME:header->decode()
295 * DESCRIPTION:read the next frame header from the stream 298 * DESCRIPTION:read the next frame header from the stream
296 */ 299 */
297int mad_header_decode(struct mad_header *header, struct mad_stream *stream) 300int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
298{ 301{
299 register unsigned char const *ptr, *end; 302 register unsigned char const *ptr, *end;
300 unsigned int pad_slot, N; 303 unsigned int pad_slot, N;
301 304
302 ptr = stream->next_frame; 305 ptr = stream->next_frame;
303 end = stream->bufend; 306 end = stream->bufend;
304 307
305 if (ptr == 0) { 308 if (ptr == 0) {
306 stream->error = MAD_ERROR_BUFPTR; 309 stream->error = MAD_ERROR_BUFPTR;
307 goto fail; 310 goto fail;
308 } 311 }
309 312
310 /* stream skip */ 313 /* stream skip */
311 if (stream->skiplen) { 314 if (stream->skiplen) {
312 if (!stream->sync) 315 if (!stream->sync)
313 ptr = stream->this_frame; 316 ptr = stream->this_frame;
314 317
315 if (end - ptr < stream->skiplen) { 318 if (end - ptr < stream->skiplen) {
316 stream->skiplen -= end - ptr; 319 stream->skiplen -= end - ptr;
317 stream->next_frame = end; 320 stream->next_frame = end;
318 321
319 stream->error = MAD_ERROR_BUFLEN; 322 stream->error = MAD_ERROR_BUFLEN;
320 goto fail; 323 goto fail;
321 } 324 }
322 325
323 ptr += stream->skiplen; 326 ptr += stream->skiplen;
324 stream->skiplen = 0; 327 stream->skiplen = 0;
325 328
326 stream->sync = 1; 329 stream->sync = 1;
327 } 330 }
328 331
329 sync: 332 sync:
330 /* synchronize */ 333 /* synchronize */
331 if (stream->sync) { 334 if (stream->sync) {
332 if (end - ptr < MAD_BUFFER_GUARD) { 335 if (end - ptr < MAD_BUFFER_GUARD) {
333 stream->next_frame = ptr; 336 stream->next_frame = ptr;
334 337
335 stream->error = MAD_ERROR_BUFLEN; 338 stream->error = MAD_ERROR_BUFLEN;
336 goto fail; 339 goto fail;
337 } 340 }
338 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { 341 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
339 /* mark point where frame sync word was expected */ 342 /* mark point where frame sync word was expected */
340 stream->this_frame = ptr; 343 stream->this_frame = ptr;
341 stream->next_frame = ptr + 1; 344 stream->next_frame = ptr + 1;
342 345
343 stream->error = MAD_ERROR_LOSTSYNC; 346 stream->error = MAD_ERROR_LOSTSYNC;
344 goto fail; 347 goto fail;
345 } 348 }
346 } 349 }
347 else { 350 else {
348 mad_bit_init(&stream->ptr, ptr); 351 mad_bit_init(&stream->ptr, ptr);
349 352
350 if (mad_stream_sync(stream) == -1) { 353 if (mad_stream_sync(stream) == -1) {
351 if (end - stream->next_frame >= MAD_BUFFER_GUARD) 354 if (end - stream->next_frame >= MAD_BUFFER_GUARD)
352 stream->next_frame = end - MAD_BUFFER_GUARD; 355 stream->next_frame = end - MAD_BUFFER_GUARD;
353 356
354 stream->error = MAD_ERROR_BUFLEN; 357 stream->error = MAD_ERROR_BUFLEN;
355 goto fail; 358 goto fail;
356 } 359 }
357 360
358 ptr = mad_bit_nextbyte(&stream->ptr); 361 ptr = mad_bit_nextbyte(&stream->ptr);
359 } 362 }
360 363
361 /* begin processing */ 364 /* begin processing */
362 stream->this_frame = ptr; 365 stream->this_frame = ptr;
363 stream->next_frame = ptr + 1; /* possibly bogus sync word */ 366 stream->next_frame = ptr + 1; /* possibly bogus sync word */
364 367
365 mad_bit_init(&stream->ptr, stream->this_frame); 368 mad_bit_init(&stream->ptr, stream->this_frame);
366 369
367 if (decode_header(header, stream) == -1) 370 if (decode_header(header, stream) == -1)
368 goto fail; 371 goto fail;
369 372
370 /* calculate frame duration */ 373 /* calculate frame duration */
371 mad_timer_set(&header->duration, 0, 374 mad_timer_set(&header->duration, 0,
372 32 * MAD_NSBSAMPLES(header), header->samplerate); 375 32 * MAD_NSBSAMPLES(header), header->samplerate);
373 376
374 /* calculate free bit rate */ 377 /* calculate free bit rate */
375 if (header->bitrate == 0) { 378 if (header->bitrate == 0) {
376 if ((stream->freerate == 0 || !stream->sync) && 379 if ((stream->freerate == 0 || !stream->sync ||
380 (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
377 free_bitrate(stream, header) == -1) 381 free_bitrate(stream, header) == -1)
378 goto fail; 382 goto fail;
379 383
380 header->bitrate = stream->freerate; 384 header->bitrate = stream->freerate;
381 header->flags |= MAD_FLAG_FREEFORMAT; 385 header->flags |= MAD_FLAG_FREEFORMAT;
382 } 386 }
383 387
384 /* calculate beginning of next frame */ 388 /* calculate beginning of next frame */
385 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; 389 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
386 390
387 if (header->layer == MAD_LAYER_I) 391 if (header->layer == MAD_LAYER_I)
388 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4; 392 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
389 else { 393 else {
390 unsigned int slots_per_frame; 394 unsigned int slots_per_frame;
391 395
392 slots_per_frame = (header->layer == MAD_LAYER_III && 396 slots_per_frame = (header->layer == MAD_LAYER_III &&
393 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; 397 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
394 398
395 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot; 399 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
396 } 400 }
397 401
398 /* verify there is enough data left in buffer to decode this frame */ 402 /* verify there is enough data left in buffer to decode this frame */
399 if (N + MAD_BUFFER_GUARD > end - stream->this_frame) { 403 if (N + MAD_BUFFER_GUARD > end - stream->this_frame) {
400 stream->next_frame = stream->this_frame; 404 stream->next_frame = stream->this_frame;
401 405
402 stream->error = MAD_ERROR_BUFLEN; 406 stream->error = MAD_ERROR_BUFLEN;
403 goto fail; 407 goto fail;
404 } 408 }
405 409
406 stream->next_frame = stream->this_frame + N; 410 stream->next_frame = stream->this_frame + N;
407 411
408 if (!stream->sync) { 412 if (!stream->sync) {
409 /* check that a valid frame header follows this frame */ 413 /* check that a valid frame header follows this frame */
410 414
411 ptr = stream->next_frame; 415 ptr = stream->next_frame;
412 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { 416 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
413 ptr = stream->next_frame = stream->this_frame + 1; 417 ptr = stream->next_frame = stream->this_frame + 1;
414 goto sync; 418 goto sync;
415 } 419 }
416 420
417 stream->sync = 1; 421 stream->sync = 1;
418 } 422 }
419 423
420 header->flags |= MAD_FLAG_INCOMPLETE; 424 header->flags |= MAD_FLAG_INCOMPLETE;
421 425
422 return 0; 426 return 0;
423 427
424 fail: 428 fail: