Diffstat (limited to 'core/multimedia/opieplayer/libmad/frame.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmad/frame.c | 18 |
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,27 +1,27 @@ | |||
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 | ||
@@ -189,52 +189,59 @@ int decode_header(struct mad_header *header, struct mad_stream *stream) | |||
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 | */ |
231 | static | 238 | static |
232 | int free_bitrate(struct mad_stream *stream, struct mad_header const *header) | 239 | int 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 | ||
@@ -262,52 +269,48 @@ int free_bitrate(struct mad_stream *stream, struct mad_header const *header) | |||
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 | */ |
297 | int mad_header_decode(struct mad_header *header, struct mad_stream *stream) | 300 | int 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; |
@@ -352,49 +355,50 @@ int mad_header_decode(struct mad_header *header, struct mad_stream *stream) | |||
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; |