Diffstat (limited to 'core/multimedia/opieplayer/libmad/layer12.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmad/layer12.c | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/core/multimedia/opieplayer/libmad/layer12.c b/core/multimedia/opieplayer/libmad/layer12.c index d291174..512b6a5 100644 --- a/core/multimedia/opieplayer/libmad/layer12.c +++ b/core/multimedia/opieplayer/libmad/layer12.c | |||
@@ -1,69 +1,69 @@ | |||
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 | # ifdef HAVE_LIMITS_H | 28 | # ifdef HAVE_LIMITS_H |
29 | # include <limits.h> | 29 | # include <limits.h> |
30 | # else | 30 | # else |
31 | # define CHAR_BIT 8 | 31 | # define CHAR_BIT 8 |
32 | # endif | 32 | # endif |
33 | 33 | ||
34 | # include "fixed.h" | 34 | # include "fixed.h" |
35 | # include "bit.h" | 35 | # include "bit.h" |
36 | # include "stream.h" | 36 | # include "stream.h" |
37 | # include "frame.h" | 37 | # include "frame.h" |
38 | # include "layer12.h" | 38 | # include "layer12.h" |
39 | 39 | ||
40 | /* | 40 | /* |
41 | * scalefactor table | 41 | * scalefactor table |
42 | * used in both Layer I and Layer II decoding | 42 | * used in both Layer I and Layer II decoding |
43 | */ | 43 | */ |
44 | static | 44 | static |
45 | mad_fixed_t const sf_table[63] = { | 45 | mad_fixed_t const sf_table[64] = { |
46 | # include "sf_table.dat" | 46 | # include "sf_table.dat" |
47 | }; | 47 | }; |
48 | 48 | ||
49 | /* --- Layer I ------------------------------------------------------------- */ | 49 | /* --- Layer I ------------------------------------------------------------- */ |
50 | 50 | ||
51 | /* linear scaling table */ | 51 | /* linear scaling table */ |
52 | static | 52 | static |
53 | mad_fixed_t const linear_table[14] = { | 53 | mad_fixed_t const linear_table[14] = { |
54 | MAD_F(0x15555555), /* 2^2 / (2^2 - 1) == 1.33333333333333 */ | 54 | MAD_F(0x15555555), /* 2^2 / (2^2 - 1) == 1.33333333333333 */ |
55 | MAD_F(0x12492492), /* 2^3 / (2^3 - 1) == 1.14285714285714 */ | 55 | MAD_F(0x12492492), /* 2^3 / (2^3 - 1) == 1.14285714285714 */ |
56 | MAD_F(0x11111111), /* 2^4 / (2^4 - 1) == 1.06666666666667 */ | 56 | MAD_F(0x11111111), /* 2^4 / (2^4 - 1) == 1.06666666666667 */ |
57 | MAD_F(0x10842108), /* 2^5 / (2^5 - 1) == 1.03225806451613 */ | 57 | MAD_F(0x10842108), /* 2^5 / (2^5 - 1) == 1.03225806451613 */ |
58 | MAD_F(0x10410410), /* 2^6 / (2^6 - 1) == 1.01587301587302 */ | 58 | MAD_F(0x10410410), /* 2^6 / (2^6 - 1) == 1.01587301587302 */ |
59 | MAD_F(0x10204081), /* 2^7 / (2^7 - 1) == 1.00787401574803 */ | 59 | MAD_F(0x10204081), /* 2^7 / (2^7 - 1) == 1.00787401574803 */ |
60 | MAD_F(0x10101010), /* 2^8 / (2^8 - 1) == 1.00392156862745 */ | 60 | MAD_F(0x10101010), /* 2^8 / (2^8 - 1) == 1.00392156862745 */ |
61 | MAD_F(0x10080402), /* 2^9 / (2^9 - 1) == 1.00195694716243 */ | 61 | MAD_F(0x10080402), /* 2^9 / (2^9 - 1) == 1.00195694716243 */ |
62 | MAD_F(0x10040100), /* 2^10 / (2^10 - 1) == 1.00097751710655 */ | 62 | MAD_F(0x10040100), /* 2^10 / (2^10 - 1) == 1.00097751710655 */ |
63 | MAD_F(0x10020040), /* 2^11 / (2^11 - 1) == 1.00048851978505 */ | 63 | MAD_F(0x10020040), /* 2^11 / (2^11 - 1) == 1.00048851978505 */ |
64 | MAD_F(0x10010010), /* 2^12 / (2^12 - 1) == 1.00024420024420 */ | 64 | MAD_F(0x10010010), /* 2^12 / (2^12 - 1) == 1.00024420024420 */ |
65 | MAD_F(0x10008004), /* 2^13 / (2^13 - 1) == 1.00012208521548 */ | 65 | MAD_F(0x10008004), /* 2^13 / (2^13 - 1) == 1.00012208521548 */ |
66 | MAD_F(0x10004001), /* 2^14 / (2^14 - 1) == 1.00006103888177 */ | 66 | MAD_F(0x10004001), /* 2^14 / (2^14 - 1) == 1.00006103888177 */ |
67 | MAD_F(0x10002000) /* 2^15 / (2^15 - 1) == 1.00003051850948 */ | 67 | MAD_F(0x10002000) /* 2^15 / (2^15 - 1) == 1.00003051850948 */ |
68 | }; | 68 | }; |
69 | 69 | ||
@@ -142,52 +142,59 @@ int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame) | |||
142 | 142 | ||
143 | allocation[ch][sb] = nb ? nb + 1 : 0; | 143 | allocation[ch][sb] = nb ? nb + 1 : 0; |
144 | } | 144 | } |
145 | } | 145 | } |
146 | 146 | ||
147 | for (sb = bound; sb < 32; ++sb) { | 147 | for (sb = bound; sb < 32; ++sb) { |
148 | nb = mad_bit_read(&stream->ptr, 4); | 148 | nb = mad_bit_read(&stream->ptr, 4); |
149 | 149 | ||
150 | if (nb == 15) { | 150 | if (nb == 15) { |
151 | stream->error = MAD_ERROR_BADBITALLOC; | 151 | stream->error = MAD_ERROR_BADBITALLOC; |
152 | return -1; | 152 | return -1; |
153 | } | 153 | } |
154 | 154 | ||
155 | allocation[0][sb] = | 155 | allocation[0][sb] = |
156 | allocation[1][sb] = nb ? nb + 1 : 0; | 156 | allocation[1][sb] = nb ? nb + 1 : 0; |
157 | } | 157 | } |
158 | 158 | ||
159 | /* decode scalefactors */ | 159 | /* decode scalefactors */ |
160 | 160 | ||
161 | for (sb = 0; sb < 32; ++sb) { | 161 | for (sb = 0; sb < 32; ++sb) { |
162 | for (ch = 0; ch < nch; ++ch) { | 162 | for (ch = 0; ch < nch; ++ch) { |
163 | if (allocation[ch][sb]) { | 163 | if (allocation[ch][sb]) { |
164 | scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6); | 164 | scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6); |
165 | 165 | ||
166 | # if defined(OPT_STRICT) | ||
167 | /* | ||
168 | * Scalefactor index 63 does not appear in Table B.1 of | ||
169 | * ISO/IEC 11172-3. Nonetheless, other implementations accept it, | ||
170 | * so we only reject it if OPT_STRICT is defined. | ||
171 | */ | ||
166 | if (scalefactor[ch][sb] == 63) { | 172 | if (scalefactor[ch][sb] == 63) { |
167 | stream->error = MAD_ERROR_BADSCALEFACTOR; | 173 | stream->error = MAD_ERROR_BADSCALEFACTOR; |
168 | return -1; | 174 | return -1; |
169 | } | 175 | } |
176 | # endif | ||
170 | } | 177 | } |
171 | } | 178 | } |
172 | } | 179 | } |
173 | 180 | ||
174 | /* decode samples */ | 181 | /* decode samples */ |
175 | 182 | ||
176 | for (s = 0; s < 12; ++s) { | 183 | for (s = 0; s < 12; ++s) { |
177 | for (sb = 0; sb < bound; ++sb) { | 184 | for (sb = 0; sb < bound; ++sb) { |
178 | for (ch = 0; ch < nch; ++ch) { | 185 | for (ch = 0; ch < nch; ++ch) { |
179 | nb = allocation[ch][sb]; | 186 | nb = allocation[ch][sb]; |
180 | frame->sbsample[ch][s][sb] = nb ? | 187 | frame->sbsample[ch][s][sb] = nb ? |
181 | mad_f_mul(I_sample(&stream->ptr, nb), | 188 | mad_f_mul(I_sample(&stream->ptr, nb), |
182 | sf_table[scalefactor[ch][sb]]) : 0; | 189 | sf_table[scalefactor[ch][sb]]) : 0; |
183 | } | 190 | } |
184 | } | 191 | } |
185 | 192 | ||
186 | for (sb = bound; sb < 32; ++sb) { | 193 | for (sb = bound; sb < 32; ++sb) { |
187 | if ((nb = allocation[0][sb])) { | 194 | if ((nb = allocation[0][sb])) { |
188 | mad_fixed_t sample; | 195 | mad_fixed_t sample; |
189 | 196 | ||
190 | sample = I_sample(&stream->ptr, nb); | 197 | sample = I_sample(&stream->ptr, nb); |
191 | 198 | ||
192 | for (ch = 0; ch < nch; ++ch) { | 199 | for (ch = 0; ch < nch; ++ch) { |
193 | frame->sbsample[ch][s][sb] = | 200 | frame->sbsample[ch][s][sb] = |
@@ -313,62 +320,86 @@ void II_samples(struct mad_bitptr *ptr, | |||
313 | output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C); | 320 | output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C); |
314 | 321 | ||
315 | /* s' = factor * s'' */ | 322 | /* s' = factor * s'' */ |
316 | /* (to be performed by caller) */ | 323 | /* (to be performed by caller) */ |
317 | } | 324 | } |
318 | } | 325 | } |
319 | 326 | ||
320 | /* | 327 | /* |
321 | * NAME:layer->II() | 328 | * NAME:layer->II() |
322 | * DESCRIPTION:decode a single Layer II frame | 329 | * DESCRIPTION:decode a single Layer II frame |
323 | */ | 330 | */ |
324 | int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame) | 331 | int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame) |
325 | { | 332 | { |
326 | struct mad_header *header = &frame->header; | 333 | struct mad_header *header = &frame->header; |
327 | struct mad_bitptr start; | 334 | struct mad_bitptr start; |
328 | unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb; | 335 | unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb; |
329 | unsigned char const *offsets; | 336 | unsigned char const *offsets; |
330 | unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3]; | 337 | unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3]; |
331 | mad_fixed_t samples[3]; | 338 | mad_fixed_t samples[3]; |
332 | 339 | ||
333 | nch = MAD_NCHANNELS(header); | 340 | nch = MAD_NCHANNELS(header); |
334 | 341 | ||
335 | if (header->flags & MAD_FLAG_LSF_EXT) | 342 | if (header->flags & MAD_FLAG_LSF_EXT) |
336 | index = 4; | 343 | index = 4; |
344 | else if (header->flags & MAD_FLAG_FREEFORMAT) | ||
345 | goto freeformat; | ||
337 | else { | 346 | else { |
338 | switch (nch == 2 ? header->bitrate / 2 : header->bitrate) { | 347 | unsigned long bitrate_per_channel; |
339 | case 32000: | 348 | |
340 | case 48000: | 349 | bitrate_per_channel = header->bitrate; |
341 | index = (header->samplerate == 32000) ? 3 : 2; | 350 | if (nch == 2) { |
342 | break; | 351 | bitrate_per_channel /= 2; |
352 | |||
353 | # if defined(OPT_STRICT) | ||
354 | /* | ||
355 | * ISO/IEC 11172-3 allows only single channel mode for 32, 48, 56, and | ||
356 | * 80 kbps bitrates in Layer II, but some encoders ignore this | ||
357 | * restriction. We enforce it if OPT_STRICT is defined. | ||
358 | */ | ||
359 | if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) { | ||
360 | stream->error = MAD_ERROR_BADMODE; | ||
361 | return -1; | ||
362 | } | ||
363 | # endif | ||
364 | } | ||
365 | else { /* nch == 1 */ | ||
366 | if (bitrate_per_channel > 192000) { | ||
367 | /* | ||
368 | * ISO/IEC 11172-3 does not allow single channel mode for 224, 256, | ||
369 | * 320, or 384 kbps bitrates in Layer II. | ||
370 | */ | ||
371 | stream->error = MAD_ERROR_BADMODE; | ||
372 | return -1; | ||
373 | } | ||
374 | } | ||
343 | 375 | ||
344 | case 56000: | 376 | if (bitrate_per_channel <= 48000) |
345 | case 64000: | 377 | index = (header->samplerate == 32000) ? 3 : 2; |
346 | case 80000: | 378 | else if (bitrate_per_channel <= 80000) |
347 | index = 0; | 379 | index = 0; |
348 | break; | 380 | else { |
349 | 381 | freeformat: | |
350 | default: | ||
351 | index = (header->samplerate == 48000) ? 0 : 1; | 382 | index = (header->samplerate == 48000) ? 0 : 1; |
352 | } | 383 | } |
353 | } | 384 | } |
354 | 385 | ||
355 | sblimit = sbquant_table[index].sblimit; | 386 | sblimit = sbquant_table[index].sblimit; |
356 | offsets = sbquant_table[index].offsets; | 387 | offsets = sbquant_table[index].offsets; |
357 | 388 | ||
358 | bound = 32; | 389 | bound = 32; |
359 | if (header->mode == MAD_MODE_JOINT_STEREO) { | 390 | if (header->mode == MAD_MODE_JOINT_STEREO) { |
360 | header->flags |= MAD_FLAG_I_STEREO; | 391 | header->flags |= MAD_FLAG_I_STEREO; |
361 | bound = 4 + header->mode_extension * 4; | 392 | bound = 4 + header->mode_extension * 4; |
362 | } | 393 | } |
363 | 394 | ||
364 | if (bound > sblimit) | 395 | if (bound > sblimit) |
365 | bound = sblimit; | 396 | bound = sblimit; |
366 | 397 | ||
367 | start = stream->ptr; | 398 | start = stream->ptr; |
368 | 399 | ||
369 | /* decode bit allocations */ | 400 | /* decode bit allocations */ |
370 | 401 | ||
371 | for (sb = 0; sb < bound; ++sb) { | 402 | for (sb = 0; sb < bound; ++sb) { |
372 | nbal = bitalloc_table[offsets[sb]].nbal; | 403 | nbal = bitalloc_table[offsets[sb]].nbal; |
373 | 404 | ||
374 | for (ch = 0; ch < nch; ++ch) | 405 | for (ch = 0; ch < nch; ++ch) |
@@ -410,54 +441,61 @@ int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame) | |||
410 | for (sb = 0; sb < sblimit; ++sb) { | 441 | for (sb = 0; sb < sblimit; ++sb) { |
411 | for (ch = 0; ch < nch; ++ch) { | 442 | for (ch = 0; ch < nch; ++ch) { |
412 | if (allocation[ch][sb]) { | 443 | if (allocation[ch][sb]) { |
413 | scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6); | 444 | scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6); |
414 | 445 | ||
415 | switch (scfsi[ch][sb]) { | 446 | switch (scfsi[ch][sb]) { |
416 | case 2: | 447 | case 2: |
417 | scalefactor[ch][sb][2] = | 448 | scalefactor[ch][sb][2] = |
418 | scalefactor[ch][sb][1] = | 449 | scalefactor[ch][sb][1] = |
419 | scalefactor[ch][sb][0]; | 450 | scalefactor[ch][sb][0]; |
420 | break; | 451 | break; |
421 | 452 | ||
422 | case 0: | 453 | case 0: |
423 | scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6); | 454 | scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6); |
424 | /* fall through */ | 455 | /* fall through */ |
425 | 456 | ||
426 | case 1: | 457 | case 1: |
427 | case 3: | 458 | case 3: |
428 | scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6); | 459 | scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6); |
429 | } | 460 | } |
430 | 461 | ||
431 | if (scfsi[ch][sb] & 1) | 462 | if (scfsi[ch][sb] & 1) |
432 | scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1]; | 463 | scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1]; |
433 | 464 | ||
465 | # if defined(OPT_STRICT) | ||
466 | /* | ||
467 | * Scalefactor index 63 does not appear in Table B.1 of | ||
468 | * ISO/IEC 11172-3. Nonetheless, other implementations accept it, | ||
469 | * so we only reject it if OPT_STRICT is defined. | ||
470 | */ | ||
434 | if (scalefactor[ch][sb][0] == 63 || | 471 | if (scalefactor[ch][sb][0] == 63 || |
435 | scalefactor[ch][sb][1] == 63 || | 472 | scalefactor[ch][sb][1] == 63 || |
436 | scalefactor[ch][sb][2] == 63) { | 473 | scalefactor[ch][sb][2] == 63) { |
437 | stream->error = MAD_ERROR_BADSCALEFACTOR; | 474 | stream->error = MAD_ERROR_BADSCALEFACTOR; |
438 | return -1; | 475 | return -1; |
439 | } | 476 | } |
477 | # endif | ||
440 | } | 478 | } |
441 | } | 479 | } |
442 | } | 480 | } |
443 | 481 | ||
444 | /* decode samples */ | 482 | /* decode samples */ |
445 | 483 | ||
446 | for (gr = 0; gr < 12; ++gr) { | 484 | for (gr = 0; gr < 12; ++gr) { |
447 | for (sb = 0; sb < bound; ++sb) { | 485 | for (sb = 0; sb < bound; ++sb) { |
448 | for (ch = 0; ch < nch; ++ch) { | 486 | for (ch = 0; ch < nch; ++ch) { |
449 | if ((index = allocation[ch][sb])) { | 487 | if ((index = allocation[ch][sb])) { |
450 | index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1]; | 488 | index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1]; |
451 | 489 | ||
452 | II_samples(&stream->ptr, &qc_table[index], samples); | 490 | II_samples(&stream->ptr, &qc_table[index], samples); |
453 | 491 | ||
454 | for (s = 0; s < 3; ++s) { | 492 | for (s = 0; s < 3; ++s) { |
455 | frame->sbsample[ch][3 * gr + s][sb] = | 493 | frame->sbsample[ch][3 * gr + s][sb] = |
456 | mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]); | 494 | mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]); |
457 | } | 495 | } |
458 | } | 496 | } |
459 | else { | 497 | else { |
460 | for (s = 0; s < 3; ++s) | 498 | for (s = 0; s < 3; ++s) |
461 | frame->sbsample[ch][3 * gr + s][sb] = 0; | 499 | frame->sbsample[ch][3 * gr + s][sb] = 0; |
462 | } | 500 | } |
463 | } | 501 | } |