summaryrefslogtreecommitdiff
path: root/apps/codecs/libmad/layer12.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libmad/layer12.c')
-rw-r--r--apps/codecs/libmad/layer12.c218
1 files changed, 109 insertions, 109 deletions
diff --git a/apps/codecs/libmad/layer12.c b/apps/codecs/libmad/layer12.c
index d294266e69..e00bb64530 100644
--- a/apps/codecs/libmad/layer12.c
+++ b/apps/codecs/libmad/layer12.c
@@ -69,7 +69,7 @@ mad_fixed_t const linear_table[14] ICONST_ATTR = {
69}; 69};
70 70
71/* 71/*
72 * NAME: I_sample() 72 * NAME: I_sample()
73 * DESCRIPTION: decode one requantized Layer I sample from a bitstream 73 * DESCRIPTION: decode one requantized Layer I sample from a bitstream
74 */ 74 */
75static inline 75static inline
@@ -99,7 +99,7 @@ mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb)
99} 99}
100 100
101/* 101/*
102 * NAME: layer->I() 102 * NAME: layer->I()
103 * DESCRIPTION: decode a single Layer I frame 103 * DESCRIPTION: decode a single Layer I frame
104 */ 104 */
105int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame) 105int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
@@ -121,10 +121,10 @@ int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
121 if (header->flags & MAD_FLAG_PROTECTION) { 121 if (header->flags & MAD_FLAG_PROTECTION) {
122 header->crc_check = 122 header->crc_check =
123 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)), 123 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
124 header->crc_check); 124 header->crc_check);
125 125
126 if (header->crc_check != header->crc_target && 126 if (header->crc_check != header->crc_target &&
127 !(frame->options & MAD_OPTION_IGNORECRC)) { 127 !(frame->options & MAD_OPTION_IGNORECRC)) {
128 stream->error = MAD_ERROR_BADCRC; 128 stream->error = MAD_ERROR_BADCRC;
129 return -1; 129 return -1;
130 } 130 }
@@ -137,8 +137,8 @@ int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
137 nb = mad_bit_read(&stream->ptr, 4); 137 nb = mad_bit_read(&stream->ptr, 4);
138 138
139 if (nb == 15) { 139 if (nb == 15) {
140 stream->error = MAD_ERROR_BADBITALLOC; 140 stream->error = MAD_ERROR_BADBITALLOC;
141 return -1; 141 return -1;
142 } 142 }
143 143
144 allocation[ch][sb] = nb ? nb + 1 : 0; 144 allocation[ch][sb] = nb ? nb + 1 : 0;
@@ -162,18 +162,18 @@ int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
162 for (sb = 0; sb < 32; ++sb) { 162 for (sb = 0; sb < 32; ++sb) {
163 for (ch = 0; ch < nch; ++ch) { 163 for (ch = 0; ch < nch; ++ch) {
164 if (allocation[ch][sb]) { 164 if (allocation[ch][sb]) {
165 scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6); 165 scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
166 166
167# if defined(OPT_STRICT) 167# if defined(OPT_STRICT)
168 /* 168 /*
169 * Scalefactor index 63 does not appear in Table B.1 of 169 * Scalefactor index 63 does not appear in Table B.1 of
170 * ISO/IEC 11172-3. Nonetheless, other implementations accept it, 170 * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
171 * so we only reject it if OPT_STRICT is defined. 171 * so we only reject it if OPT_STRICT is defined.
172 */ 172 */
173 if (scalefactor[ch][sb] == 63) { 173 if (scalefactor[ch][sb] == 63) {
174 stream->error = MAD_ERROR_BADSCALEFACTOR; 174 stream->error = MAD_ERROR_BADSCALEFACTOR;
175 return -1; 175 return -1;
176 } 176 }
177# endif 177# endif
178 } 178 }
179 } 179 }
@@ -184,27 +184,27 @@ int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
184 for (s = 0; s < 12; ++s) { 184 for (s = 0; s < 12; ++s) {
185 for (sb = 0; sb < bound; ++sb) { 185 for (sb = 0; sb < bound; ++sb) {
186 for (ch = 0; ch < nch; ++ch) { 186 for (ch = 0; ch < nch; ++ch) {
187 nb = allocation[ch][sb]; 187 nb = allocation[ch][sb];
188 (*frame->sbsample)[ch][s][sb] = nb ? 188 (*frame->sbsample)[ch][s][sb] = nb ?
189 mad_f_mul(I_sample(&stream->ptr, nb), 189 mad_f_mul(I_sample(&stream->ptr, nb),
190 sf_table[scalefactor[ch][sb]]) : 0; 190 sf_table[scalefactor[ch][sb]]) : 0;
191 } 191 }
192 } 192 }
193 193
194 for (sb = bound; sb < 32; ++sb) { 194 for (sb = bound; sb < 32; ++sb) {
195 if ((nb = allocation[0][sb])) { 195 if ((nb = allocation[0][sb])) {
196 mad_fixed_t sample; 196 mad_fixed_t sample;
197 197
198 sample = I_sample(&stream->ptr, nb); 198 sample = I_sample(&stream->ptr, nb);
199 199
200 for (ch = 0; ch < nch; ++ch) { 200 for (ch = 0; ch < nch; ++ch) {
201 (*frame->sbsample)[ch][s][sb] = 201 (*frame->sbsample)[ch][s][sb] =
202 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]); 202 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]);
203 } 203 }
204 } 204 }
205 else { 205 else {
206 for (ch = 0; ch < nch; ++ch) 206 for (ch = 0; ch < nch; ++ch)
207 (*frame->sbsample)[ch][s][sb] = 0; 207 (*frame->sbsample)[ch][s][sb] = 0;
208 } 208 }
209 } 209 }
210 } 210 }
@@ -221,18 +221,18 @@ struct {
221 unsigned char const offsets[30]; 221 unsigned char const offsets[30];
222} const sbquant_table[5] = { 222} const sbquant_table[5] = {
223 /* ISO/IEC 11172-3 Table B.2a */ 223 /* ISO/IEC 11172-3 Table B.2a */
224 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */ 224 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */
225 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } }, 225 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } },
226 /* ISO/IEC 11172-3 Table B.2b */ 226 /* ISO/IEC 11172-3 Table B.2b */
227 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */ 227 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */
228 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } }, 228 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } },
229 /* ISO/IEC 11172-3 Table B.2c */ 229 /* ISO/IEC 11172-3 Table B.2c */
230 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */ 230 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */
231 /* ISO/IEC 11172-3 Table B.2d */ 231 /* ISO/IEC 11172-3 Table B.2d */
232 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */ 232 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */
233 /* ISO/IEC 13818-3 Table B.1 */ 233 /* ISO/IEC 13818-3 Table B.1 */
234 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */ 234 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */
235 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } } 235 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }
236}; 236};
237 237
238/* bit allocation table */ 238/* bit allocation table */
@@ -275,13 +275,13 @@ struct quantclass {
275}; 275};
276 276
277/* 277/*
278 * NAME: II_samples() 278 * NAME: II_samples()
279 * DESCRIPTION: decode three requantized Layer II samples from a bitstream 279 * DESCRIPTION: decode three requantized Layer II samples from a bitstream
280 */ 280 */
281static 281static
282void II_samples(struct mad_bitptr *ptr, 282void II_samples(struct mad_bitptr *ptr,
283 struct quantclass const *quantclass, 283 struct quantclass const *quantclass,
284 mad_fixed_t output[3]) 284 mad_fixed_t output[3])
285{ 285{
286 unsigned int nb, s, sample[3]; 286 unsigned int nb, s, sample[3];
287 287
@@ -326,7 +326,7 @@ void II_samples(struct mad_bitptr *ptr,
326} 326}
327 327
328/* 328/*
329 * NAME: layer->II() 329 * NAME: layer->II()
330 * DESCRIPTION: decode a single Layer II frame 330 * DESCRIPTION: decode a single Layer II frame
331 */ 331 */
332int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame) 332int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
@@ -358,19 +358,19 @@ int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
358 * restriction. We enforce it if OPT_STRICT is defined. 358 * restriction. We enforce it if OPT_STRICT is defined.
359 */ 359 */
360 if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) { 360 if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) {
361 stream->error = MAD_ERROR_BADMODE; 361 stream->error = MAD_ERROR_BADMODE;
362 return -1; 362 return -1;
363 } 363 }
364# endif 364# endif
365 } 365 }
366 else { /* nch == 1 */ 366 else { /* nch == 1 */
367 if (bitrate_per_channel > 192000) { 367 if (bitrate_per_channel > 192000) {
368 /* 368 /*
369 * ISO/IEC 11172-3 does not allow single channel mode for 224, 256, 369 * ISO/IEC 11172-3 does not allow single channel mode for 224, 256,
370 * 320, or 384 kbps bitrates in Layer II. 370 * 320, or 384 kbps bitrates in Layer II.
371 */ 371 */
372 stream->error = MAD_ERROR_BADMODE; 372 stream->error = MAD_ERROR_BADMODE;
373 return -1; 373 return -1;
374 } 374 }
375 } 375 }
376 376
@@ -419,7 +419,7 @@ int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
419 for (sb = 0; sb < sblimit; ++sb) { 419 for (sb = 0; sb < sblimit; ++sb) {
420 for (ch = 0; ch < nch; ++ch) { 420 for (ch = 0; ch < nch; ++ch) {
421 if (allocation[ch][sb]) 421 if (allocation[ch][sb])
422 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2); 422 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
423 } 423 }
424 } 424 }
425 425
@@ -428,10 +428,10 @@ int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
428 if (header->flags & MAD_FLAG_PROTECTION) { 428 if (header->flags & MAD_FLAG_PROTECTION) {
429 header->crc_check = 429 header->crc_check =
430 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr), 430 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr),
431 header->crc_check); 431 header->crc_check);
432 432
433 if (header->crc_check != header->crc_target && 433 if (header->crc_check != header->crc_target &&
434 !(frame->options & MAD_OPTION_IGNORECRC)) { 434 !(frame->options & MAD_OPTION_IGNORECRC)) {
435 stream->error = MAD_ERROR_BADCRC; 435 stream->error = MAD_ERROR_BADCRC;
436 return -1; 436 return -1;
437 } 437 }
@@ -442,39 +442,39 @@ int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
442 for (sb = 0; sb < sblimit; ++sb) { 442 for (sb = 0; sb < sblimit; ++sb) {
443 for (ch = 0; ch < nch; ++ch) { 443 for (ch = 0; ch < nch; ++ch) {
444 if (allocation[ch][sb]) { 444 if (allocation[ch][sb]) {
445 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6); 445 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
446
447 switch (scfsi[ch][sb]) {
448 case 2:
449 scalefactor[ch][sb][2] =
450 scalefactor[ch][sb][1] =
451 scalefactor[ch][sb][0];
452 break;
453
454 case 0:
455 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
456 /* fall through */
457
458 case 1:
459 case 3:
460 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
461 }
462 446
463 if (scfsi[ch][sb] & 1) 447 switch (scfsi[ch][sb]) {
464 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1]; 448 case 2:
449 scalefactor[ch][sb][2] =
450 scalefactor[ch][sb][1] =
451 scalefactor[ch][sb][0];
452 break;
453
454 case 0:
455 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
456 /* fall through */
457
458 case 1:
459 case 3:
460 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
461 }
462
463 if (scfsi[ch][sb] & 1)
464 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1];
465 465
466# if defined(OPT_STRICT) 466# if defined(OPT_STRICT)
467 /* 467 /*
468 * Scalefactor index 63 does not appear in Table B.1 of 468 * Scalefactor index 63 does not appear in Table B.1 of
469 * ISO/IEC 11172-3. Nonetheless, other implementations accept it, 469 * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
470 * so we only reject it if OPT_STRICT is defined. 470 * so we only reject it if OPT_STRICT is defined.
471 */ 471 */
472 if (scalefactor[ch][sb][0] == 63 || 472 if (scalefactor[ch][sb][0] == 63 ||
473 scalefactor[ch][sb][1] == 63 || 473 scalefactor[ch][sb][1] == 63 ||
474 scalefactor[ch][sb][2] == 63) { 474 scalefactor[ch][sb][2] == 63) {
475 stream->error = MAD_ERROR_BADSCALEFACTOR; 475 stream->error = MAD_ERROR_BADSCALEFACTOR;
476 return -1; 476 return -1;
477 } 477 }
478# endif 478# endif
479 } 479 }
480 } 480 }
@@ -485,50 +485,50 @@ int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
485 for (gr = 0; gr < 12; ++gr) { 485 for (gr = 0; gr < 12; ++gr) {
486 for (sb = 0; sb < bound; ++sb) { 486 for (sb = 0; sb < bound; ++sb) {
487 for (ch = 0; ch < nch; ++ch) { 487 for (ch = 0; ch < nch; ++ch) {
488 if ((index = allocation[ch][sb])) { 488 if ((index = allocation[ch][sb])) {
489 int off = bitalloc_table[offsets[sb]].offset; 489 int off = bitalloc_table[offsets[sb]].offset;
490 index = offset_table[off][index - 1]; 490 index = offset_table[off][index - 1];
491 491
492 II_samples(&stream->ptr, &qc_table[index], samples); 492 II_samples(&stream->ptr, &qc_table[index], samples);
493 493
494 for (s = 0; s < 3; ++s) { 494 for (s = 0; s < 3; ++s) {
495 (*frame->sbsample)[ch][3 * gr + s][sb] = 495 (*frame->sbsample)[ch][3 * gr + s][sb] =
496 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]); 496 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
497 } 497 }
498 } 498 }
499 else { 499 else {
500 for (s = 0; s < 3; ++s) 500 for (s = 0; s < 3; ++s)
501 (*frame->sbsample)[ch][3 * gr + s][sb] = 0; 501 (*frame->sbsample)[ch][3 * gr + s][sb] = 0;
502 } 502 }
503 } 503 }
504 } 504 }
505 505
506 for (sb = bound; sb < sblimit; ++sb) { 506 for (sb = bound; sb < sblimit; ++sb) {
507 if ((index = allocation[0][sb])) { 507 if ((index = allocation[0][sb])) {
508 int off = bitalloc_table[offsets[sb]].offset; 508 int off = bitalloc_table[offsets[sb]].offset;
509 index = offset_table[off][index - 1]; 509 index = offset_table[off][index - 1];
510 510
511 II_samples(&stream->ptr, &qc_table[index], samples); 511 II_samples(&stream->ptr, &qc_table[index], samples);
512 512
513 for (ch = 0; ch < nch; ++ch) { 513 for (ch = 0; ch < nch; ++ch) {
514 for (s = 0; s < 3; ++s) { 514 for (s = 0; s < 3; ++s) {
515 (*frame->sbsample)[ch][3 * gr + s][sb] = 515 (*frame->sbsample)[ch][3 * gr + s][sb] =
516 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]); 516 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
517 } 517 }
518 } 518 }
519 } 519 }
520 else { 520 else {
521 for (ch = 0; ch < nch; ++ch) { 521 for (ch = 0; ch < nch; ++ch) {
522 for (s = 0; s < 3; ++s) 522 for (s = 0; s < 3; ++s)
523 (*frame->sbsample)[ch][3 * gr + s][sb] = 0; 523 (*frame->sbsample)[ch][3 * gr + s][sb] = 0;
524 } 524 }
525 } 525 }
526 } 526 }
527 527
528 for (ch = 0; ch < nch; ++ch) { 528 for (ch = 0; ch < nch; ++ch) {
529 for (s = 0; s < 3; ++s) { 529 for (s = 0; s < 3; ++s) {
530 for (sb = sblimit; sb < 32; ++sb) 530 for (sb = sblimit; sb < 32; ++sb)
531 (*frame->sbsample)[ch][3 * gr + s][sb] = 0; 531 (*frame->sbsample)[ch][3 * gr + s][sb] = 0;
532 } 532 }
533 } 533 }
534 } 534 }