1 /*
2 * MPEG Audio decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * MPEG Audio decoder
25 */
26
37
38 /*
39 * TODO:
40 * - test lsf / mpeg25 extensively.
41 */
42
45
46 #define BACKSTEP_SIZE 512
48 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
49
50 /* layer 3 "granule" */
63 int region_size[3];
/* number of huffman codes in each region */
69
74 /* next header (used in free format parsing) */
83 int adu_mode;
///< 0 for standard mp3, 1 for adu formatted mp3
91
93
96
97 /* vlc structure for decoding layer 3 huffman tables */
100 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
101 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
104 0, 128, 128, 128, 130, 128, 154, 166,
105 142, 204, 190, 170, 542, 460, 662, 414
106 };
110 /* computed from band_size_long */
113 /* intensity stereo coef table */
117
121
124 };
125
126 /* lower 2 bits: modulo 3, higher bits: shift */
128 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
130 /* mult table for layer 2 group quantization */
131
132 #define SCALE_GEN(v) \
133 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
134
139 };
140
141 /**
142 * Convert region offsets to region sizes and truncate
143 * size to big_values.
144 */
146 {
147 int i, k, j = 0;
149 for (i = 0; i < 3; i++) {
152 j = k;
153 }
154 }
155
157 {
159 if (s->sample_rate_index != 8)
161 else
163 } else {
164 if (s->sample_rate_index <= 2)
166 else if (s->sample_rate_index != 8)
168 else
170 }
172 }
173
175 int ra1, int ra2)
176 {
177 int l;
179 /* should not overflow */
180 l =
FFMIN(ra1 + ra2 + 2, 22);
182 }
183
185 {
188 if(s->sample_rate_index == 8)
190 /* if switched mode, we handle the 36 first samples as
191 long blocks. For 8000Hz, we handle the 72 first
192 exponents as long blocks */
193 if (s->sample_rate_index <= 2)
195 else
197
199 } else {
202 }
203 } else {
206 }
207 }
208
209 /* layer 1 unscaling */
210 /* n = number of bits of the mantissa minus 1 */
212 {
215
217 mod = shift & 3;
218 shift >>= 2;
221 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
222 return (int)((val + (1LL << (shift - 1))) >> shift);
223 }
224
226 {
228
230 mod = shift & 3;
231 shift >>= 2;
232
234 /* NOTE: at this point, 0 <= shift <= 21 */
235 if (shift > 0)
236 val = (val + (1 << (shift - 1))) >> shift;
238 }
239
240 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
242 {
244 int e;
245
248 e -= exponent >> 2;
249 #ifdef DEBUG
250 if(e < 1)
252 #endif
253 if (e > 31)
254 return 0;
255 m = (m + (1 << (e - 1))) >> e;
256
258 }
259
261 {
262 int i, j, k;
264
265 /* scale factors table for layer 1/2 */
266 for (i = 0; i < 64; i++) {
268 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
269 shift = i / 3;
270 mod = i % 3;
272 }
273
274 /* scale factor multiply for layer 1 */
275 for (i = 0; i < 15; i++) {
277 n = i + 2;
278 norm = ((INT64_C(1) <<
n) *
FRAC_ONE) / ((1 <<
n) - 1);
286 }
287
289
290 /* huffman decode tables */
291 offset = 0;
292 for (i = 1; i < 16; i++) {
295 uint8_t tmp_bits [512] = { 0 };
296 uint16_t tmp_codes[512] = { 0 };
297
299
300 j = 0;
301 for (x = 0; x < xsize; x++) {
302 for (y = 0; y < xsize; y++) {
303 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->
bits [j ];
304 tmp_codes[(x << 5) | y | ((x&&
y)<<4)]= h->
codes[j++];
305 }
306 }
307
308 /* XXX: fail test */
312 tmp_bits, 1, 1, tmp_codes, 2, 2,
315 }
317
318 offset = 0;
319 for (i = 0; i < 2; i++) {
322 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
326 }
328
329 for (i = 0; i < 9; i++) {
330 k = 0;
331 for (j = 0; j < 22; j++) {
334 }
336 }
337
338 /* compute n ^ (4/3) and store it in mantissa/exp format */
339
341
342 for (i = 0; i < 4; i++) {
345 int val1, val2, val3, steps;
348 val1 = val % steps;
349 val /= steps;
350 val2 = val % steps;
351 val3 = val / steps;
353 }
354 }
355 }
356
357
358 for (i = 0; i < 7; i++) {
359 float f;
361 if (i != 6) {
362 f = tan((
double)i *
M_PI / 12.0);
363 v =
FIXR(f / (1.0 + f));
364 } else {
366 }
369 }
370 /* invalid values */
371 for (i = 7; i < 16; i++)
373
374 for (i = 0; i < 16; i++) {
375 double f;
376 int e, k;
377
378 for (j = 0; j < 2; j++) {
379 e = -(j + 1) * ((i + 1) >> 1);
381 k = i & 1;
387 }
388 }
389
390 for (i = 0; i < 8; i++) {
391 float ci, cs, ca;
393 cs = 1.0 / sqrt(1.0 + ci * ci);
394 ca = cs * ci;
395 #if !USE_FLOATS
400 #else
405 #endif
406 }
407 }
408
409 #if USE_FLOATS
411 {
414
415 return 0;
416 }
417 #endif
418
420 {
421 static int initialized_tables = 0;
423
424 if (!initialized_tables) {
426 initialized_tables = 1;
427 }
428
430
431 #if USE_FLOATS
435 #endif
436
438
442 else
445
448
449 return 0;
450 }
451
452 #define C3 FIXHR(0.86602540378443864676/2)
453 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
454 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
455 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
456
457 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
458 cases. */
460 {
462
463 in0 = in[0*3];
464 in1 = in[1*3] + in[0*3];
465 in2 = in[2*3] + in[1*3];
466 in3 = in[3*3] + in[2*3];
467 in4 = in[4*3] + in[3*3];
468 in5 = in[5*3] + in[4*3];
469 in5 += in3;
470 in3 += in1;
471
474
475 t1 = in0 - in4;
477
478 out[ 7] =
480 out[ 1] =
482
484 in4 = in0 + in2;
485 in5 += 2*in1;
487 out[ 8] =
488 out[ 9] = in4 + in1;
489 out[ 2] =
490 out[ 3] = in4 - in1;
491
492 in0 -= in2;
494 out[ 0] =
495 out[ 5] = in0 - in5;
496 out[ 6] =
497 out[11] = in0 + in5;
498 }
499
500 /* return the number of decoded frames */
502 {
503 int bound, i,
v,
n, ch, j, mant;
506
508 bound = (s->mode_ext + 1) * 4;
509 else
511
512 /* allocation bits */
513 for (i = 0; i < bound; i++) {
514 for (ch = 0; ch < s->nb_channels; ch++) {
516 }
517 }
518 for (i = bound; i <
SBLIMIT; i++)
520
521 /* scale factors */
522 for (i = 0; i < bound; i++) {
523 for (ch = 0; ch < s->nb_channels; ch++) {
524 if (allocation[ch][i])
526 }
527 }
528 for (i = bound; i <
SBLIMIT; i++) {
529 if (allocation[0][i]) {
532 }
533 }
534
535 /* compute samples */
536 for (j = 0; j < 12; j++) {
537 for (i = 0; i < bound; i++) {
538 for (ch = 0; ch < s->nb_channels; ch++) {
539 n = allocation[ch][i];
540 if (n) {
542 v =
l1_unscale(n, mant, scale_factors[ch][i]);
543 } else {
544 v = 0;
545 }
547 }
548 }
549 for (i = bound; i <
SBLIMIT; i++) {
550 n = allocation[0][i];
551 if (n) {
557 } else {
560 }
561 }
562 }
563 return 12;
564 }
565
567 {
568 int sblimit; /* number of used subbands */
570 int table, bit_alloc_bits, i, j, ch, bound,
v;
574 int scale, qindex,
bits, steps, k, l,
m,
b;
575
576 /* select decoding table */
578 s->sample_rate, s->lsf);
581
583 bound = (s->mode_ext + 1) * 4;
584 else
585 bound = sblimit;
586
587 av_dlog(s->
avctx,
"bound=%d sblimit=%d\n", bound, sblimit);
588
589 /* sanity check */
590 if (bound > sblimit)
591 bound = sblimit;
592
593 /* parse bit allocation */
594 j = 0;
595 for (i = 0; i < bound; i++) {
596 bit_alloc_bits = alloc_table[j];
597 for (ch = 0; ch < s->nb_channels; ch++)
598 bit_alloc[ch][i] =
get_bits(&s->
gb, bit_alloc_bits);
599 j += 1 << bit_alloc_bits;
600 }
601 for (i = bound; i < sblimit; i++) {
602 bit_alloc_bits = alloc_table[j];
606 j += 1 << bit_alloc_bits;
607 }
608
609 /* scale codes */
610 for (i = 0; i < sblimit; i++) {
611 for (ch = 0; ch < s->nb_channels; ch++) {
612 if (bit_alloc[ch][i])
614 }
615 }
616
617 /* scale factors */
618 for (i = 0; i < sblimit; i++) {
619 for (ch = 0; ch < s->nb_channels; ch++) {
620 if (bit_alloc[ch][i]) {
621 sf = scale_factors[ch][i];
622 switch (scale_code[ch][i]) {
623 default:
624 case 0:
628 break;
629 case 2:
631 sf[1] = sf[0];
632 sf[2] = sf[0];
633 break;
634 case 1:
637 sf[1] = sf[0];
638 break;
639 case 3:
642 sf[1] = sf[2];
643 break;
644 }
645 }
646 }
647 }
648
649 /* samples */
650 for (k = 0; k < 3; k++) {
651 for (l = 0; l < 12; l += 3) {
652 j = 0;
653 for (i = 0; i < bound; i++) {
654 bit_alloc_bits = alloc_table[j];
655 for (ch = 0; ch < s->nb_channels; ch++) {
656 b = bit_alloc[ch][i];
657 if (b) {
658 scale = scale_factors[ch][i][k];
659 qindex = alloc_table[j+
b];
661 if (bits < 0) {
662 int v2;
663 /* 3 values at the same time */
667
674 } else {
675 for (m = 0; m < 3; m++) {
679 }
680 }
681 } else {
685 }
686 }
687 /* next subband in alloc table */
688 j += 1 << bit_alloc_bits;
689 }
690 /* XXX: find a way to avoid this duplication of code */
691 for (i = bound; i < sblimit; i++) {
692 bit_alloc_bits = alloc_table[j];
693 b = bit_alloc[0][i];
694 if (b) {
695 int mant, scale0, scale1;
696 scale0 = scale_factors[0][i][k];
697 scale1 = scale_factors[1][i][k];
698 qindex = alloc_table[j+
b];
700 if (bits < 0) {
701 /* 3 values at the same time */
704 mant = v % steps;
705 v = v / steps;
710 mant = v % steps;
711 v = v / steps;
720 } else {
721 for (m = 0; m < 3; m++) {
727 }
728 }
729 } else {
736 }
737 /* next subband in alloc table */
738 j += 1 << bit_alloc_bits;
739 }
740 /* fill remaining samples to zero */
741 for (i = sblimit; i <
SBLIMIT; i++) {
742 for (ch = 0; ch < s->nb_channels; ch++) {
746 }
747 }
748 }
749 }
750 return 3 * 12;
751 }
752
753 #define SPLIT(dst,sf,n) \
754 if (n == 3) { \
755 int m = (sf * 171) >> 9; \
756 dst = sf - 3 * m; \
757 sf = m; \
758 } else if (n == 4) { \
759 dst = sf & 3; \
760 sf >>= 2; \
761 } else if (n == 5) { \
762 int m = (sf * 205) >> 10; \
763 dst = sf - 5 * m; \
764 sf = m; \
765 } else if (n == 6) { \
766 int m = (sf * 171) >> 10; \
767 dst = sf - 6 * m; \
768 sf = m; \
769 } else { \
770 dst = 0; \
771 }
772
774 int n3)
775 {
776 SPLIT(slen[3], sf, n3)
777 SPLIT(slen[2], sf, n2)
778 SPLIT(slen[1], sf, n1)
779 slen[0] = sf;
780 }
781
783 int16_t *exponents)
784 {
786 int len, i, j, k, l,
v0,
shift, gain, gains[3];
787 int16_t *exp_ptr;
788
789 exp_ptr = exponents;
792
796 v0 = gain - ((g->
scale_factors[i] + pretab[i]) << shift) + 400;
797 len = bstab[i];
798 for (j = len; j > 0; j--)
799 *exp_ptr++ = v0;
800 }
801
809 len = bstab[i];
810 for (l = 0; l < 3; l++) {
812 for (j = len; j > 0; j--)
813 *exp_ptr++ = v0;
814 }
815 }
816 }
817 }
818
819 /* handle n = 0 too */
821 {
823 }
824
825
827 int *end_pos2)
828 {
834 *end_pos2 =
837 }
838 }
839
840 /* Following is a optimized code for
841 INTFLOAT v = *src
842 if(get_bits1(&s->gb))
843 v = -v;
844 *dst = v;
845 */
846 #if USE_FLOATS
847 #define READ_FLIP_SIGN(dst,src) \
848 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
849 AV_WN32A(dst, v);
850 #else
851 #define READ_FLIP_SIGN(dst,src) \
852 v = -get_bits1(&s->gb); \
853 *(dst) = (*(src) ^ v) - v;
854 #endif
855
857 int16_t *exponents, int end_pos2)
858 {
859 int s_index;
860 int i;
861 int last_pos, bits_left;
864
865 /* low frequencies (called big values) */
866 s_index = 0;
867 for (i = 0; i < 3; i++) {
868 int j, k, l, linbits;
870 if (j == 0)
871 continue;
872 /* select vlc table */
876 vlc = &huff_vlc[l];
877
878 if (!l) {
880 s_index += 2 * j;
881 continue;
882 }
883
884 /* read huffcode and compute each couple */
885 for (; j > 0; j--) {
889
890 if (pos >= end_pos){
892 if (pos >= end_pos)
893 break;
894 }
896
897 if (!y) {
900 s_index += 2;
901 continue;
902 }
903
904 exponent= exponents[s_index];
905
908 if (y & 16) {
909 x = y >> 5;
910 y = y & 0x0f;
911 if (x < 15) {
913 } else {
919 }
920 if (y < 15) {
922 } else {
928 }
929 } else {
930 x = y >> 5;
931 y = y & 0x0f;
933 if (x < 15) {
935 } else {
941 }
943 }
944 s_index += 2;
945 }
946 }
947
948 /* high frequencies */
950 last_pos = 0;
951 while (s_index <= 572) {
952 int pos, code;
954 if (pos >= end_pos) {
955 if (pos > end_pos2 && last_pos) {
956 /* some encoders generate an incorrect size for this
957 part. We must go back into the data */
958 s_index -= 4;
960 av_log(s->
avctx,
AV_LOG_INFO,
"overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
962 s_index=0;
963 break;
964 }
966 if (pos >= end_pos)
967 break;
968 }
969 last_pos = pos;
970
977 while (code) {
978 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
980 int pos = s_index + idxtab[code];
981 code ^= 8 >> idxtab[code];
983 }
984 s_index += 4;
985 }
986 /* skip extension bits */
990 s_index=0;
993 s_index = 0;
994 }
997
1000
1001 return 0;
1002 }
1003
1004 /* Reorder short blocks from bitstream order to interleaved order. It
1005 would be faster to do it in parsing, but the code would be far more
1006 complicated */
1008 {
1012
1014 return;
1015
1017 if (s->sample_rate_index != 8)
1019 else
1021 } else {
1023 }
1024
1027 ptr1 = ptr;
1028 dst = tmp;
1029 for (j = len; j > 0; j--) {
1030 *dst++ = ptr[0*
len];
1031 *dst++ = ptr[1*
len];
1032 *dst++ = ptr[2*
len];
1033 ptr++;
1034 }
1036 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1037 }
1038 }
1039
1040 #define ISQRT2 FIXR(0.70710678118654752440)
1041
1043 {
1044 int i, j, k, l;
1045 int sf_max, sf,
len, non_zero_found;
1046 INTFLOAT (*is_tab)[16], *tab0, *
tab1, tmp0, tmp1, v1, v2;
1047 int non_zero_found_short[3];
1048
1049 /* intensity stereo */
1051 if (!s->lsf) {
1053 sf_max = 7;
1054 } else {
1056 sf_max = 16;
1057 }
1058
1061
1062 non_zero_found_short[0] = 0;
1063 non_zero_found_short[1] = 0;
1064 non_zero_found_short[2] = 0;
1067 /* for last band, use previous scale factor */
1068 if (i != 11)
1069 k -= 3;
1071 for (l = 2; l >= 0; l--) {
1074 if (!non_zero_found_short[l]) {
1075 /* test if non zero band. if so, stop doing i-stereo */
1076 for (j = 0; j <
len; j++) {
1077 if (tab1[j] != 0) {
1078 non_zero_found_short[l] = 1;
1079 goto found1;
1080 }
1081 }
1083 if (sf >= sf_max)
1084 goto found1;
1085
1086 v1 = is_tab[0][sf];
1087 v2 = is_tab[1][sf];
1088 for (j = 0; j <
len; j++) {
1089 tmp0 = tab0[j];
1092 }
1093 } else {
1094 found1:
1096 /* lower part of the spectrum : do ms stereo
1097 if enabled */
1098 for (j = 0; j <
len; j++) {
1099 tmp0 = tab0[j];
1100 tmp1 = tab1[j];
1103 }
1104 }
1105 }
1106 }
1107 }
1108
1109 non_zero_found = non_zero_found_short[0] |
1110 non_zero_found_short[1] |
1111 non_zero_found_short[2];
1112
1113 for (i = g1->
long_end - 1;i >= 0;i--) {
1117 /* test if non zero band. if so, stop doing i-stereo */
1118 if (!non_zero_found) {
1119 for (j = 0; j <
len; j++) {
1120 if (tab1[j] != 0) {
1121 non_zero_found = 1;
1122 goto found2;
1123 }
1124 }
1125 /* for last band, use previous scale factor */
1126 k = (i == 21) ? 20 : i;
1128 if (sf >= sf_max)
1129 goto found2;
1130 v1 = is_tab[0][sf];
1131 v2 = is_tab[1][sf];
1132 for (j = 0; j <
len; j++) {
1133 tmp0 = tab0[j];
1136 }
1137 } else {
1138 found2:
1140 /* lower part of the spectrum : do ms stereo
1141 if enabled */
1142 for (j = 0; j <
len; j++) {
1143 tmp0 = tab0[j];
1144 tmp1 = tab1[j];
1147 }
1148 }
1149 }
1150 }
1152 /* ms stereo ONLY */
1153 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1154 global gain */
1155 #if USE_FLOATS
1157 #else
1160 for (i = 0; i < 576; i++) {
1161 tmp0 = tab0[i];
1162 tmp1 = tab1[i];
1163 tab0[i] = tmp0 + tmp1;
1164 tab1[i] = tmp0 - tmp1;
1165 }
1166 #endif
1167 }
1168 }
1169
1170 #if USE_FLOATS
1171 #if HAVE_MIPSFPU
1173 #endif /* HAVE_MIPSFPU */
1174 #else
1175 #if HAVE_MIPSDSPR1
1177 #endif /* HAVE_MIPSDSPR1 */
1178 #endif /* USE_FLOATS */
1179
1180 #ifndef compute_antialias
1181 #if USE_FLOATS
1182 #define AA(j) do { \
1183 float tmp0 = ptr[-1-j]; \
1184 float tmp1 = ptr[ j]; \
1185 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1186 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1187 } while (0)
1188 #else
1189 #define AA(j) do { \
1190 int tmp0 = ptr[-1-j]; \
1191 int tmp1 = ptr[ j]; \
1192 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1193 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1194 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1195 } while (0)
1196 #endif
1197
1199 {
1202
1203 /* we antialias only "long" bands */
1206 return;
1207 /* XXX: check this for 8000Hz case */
1208 n = 1;
1209 } else {
1211 }
1212
1214 for (i = n; i > 0; i--) {
1223
1224 ptr += 18;
1225 }
1226 }
1227 #endif /* compute_antialias */
1228
1231 {
1234 int i, j, mdct_long_end, sblimit;
1235
1236 /* find last non zero block */
1239 while (ptr >= ptr1) {
1241 ptr -= 6;
1243 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1244 break;
1245 }
1246 sblimit = ((ptr - g->
sb_hybrid) / 18) + 1;
1247
1249 /* XXX: check for 8000 Hz */
1251 mdct_long_end = 2;
1252 else
1253 mdct_long_end = 0;
1254 } else {
1255 mdct_long_end = sblimit;
1256 }
1257
1261
1262 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1263 ptr = g->
sb_hybrid + 18 * mdct_long_end;
1264
1265 for (j = mdct_long_end; j < sblimit; j++) {
1266 /* select frequency inversion */
1267 win =
RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1268 out_ptr = sb_samples + j;
1269
1270 for (i = 0; i < 6; i++) {
1271 *out_ptr = buf[4*i];
1273 }
1275 for (i = 0; i < 6; i++) {
1276 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1277 buf[4*(i + 6*2)] =
MULH3(out2[i + 6], win[i + 6], 1);
1279 }
1281 for (i = 0; i < 6; i++) {
1282 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1283 buf[4*(i + 6*0)] =
MULH3(out2[i + 6], win[i + 6], 1);
1285 }
1287 for (i = 0; i < 6; i++) {
1288 buf[4*(i + 6*0)] =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1289 buf[4*(i + 6*1)] =
MULH3(out2[i + 6], win[i + 6], 1);
1290 buf[4*(i + 6*2)] = 0;
1291 }
1292 ptr += 18;
1293 buf += (j&3) != 3 ? 1 : (4*18-3);
1294 }
1295 /* zero bands */
1296 for (j = sblimit; j <
SBLIMIT; j++) {
1297 /* overlap */
1298 out_ptr = sb_samples + j;
1299 for (i = 0; i < 18; i++) {
1300 *out_ptr = buf[4*i];
1301 buf[4*i] = 0;
1303 }
1304 buf += (j&3) != 3 ? 1 : (4*18-3);
1305 }
1306 }
1307
1308 /* main layer3 decoding function */
1310 {
1311 int nb_granules, main_data_begin;
1312 int gr, ch, blocksplit_flag, i, j, k,
n, bits_pos;
1314 int16_t exponents[576]; //FIXME try INTFLOAT
1315
1316 /* read side info */
1317 if (s->lsf) {
1320 nb_granules = 1;
1321 } else {
1323 if (s->nb_channels == 2)
1325 else
1327 nb_granules = 2;
1328 for (ch = 0; ch < s->nb_channels; ch++) {
1329 s->
granules[ch][0].
scfsi = 0;
/* all scale factors are transmitted */
1331 }
1332 }
1333
1334 for (gr = 0; gr < nb_granules; gr++) {
1335 for (ch = 0; ch < s->nb_channels; ch++) {
1343 }
1344
1346 /* if MS stereo only is selected, we precompute the
1347 1/sqrt(2) renormalization factor */
1351 if (s->lsf)
1353 else
1356 if (blocksplit_flag) {
1361 }
1363 for (i = 0; i < 2; i++)
1365 for (i = 0; i < 3; i++)
1368 } else {
1369 int region_address1, region_address2;
1372 for (i = 0; i < 3; i++)
1374 /* compute huffman coded region sizes */
1378 region_address1, region_address2);
1380 }
1383
1385 if (!s->lsf)
1391 }
1392 }
1393
1395 int skip;
1399 /* now we get bits from the main_data_begin offset */
1402
1406 #if !UNCHECKED_BITSTREAM_READER
1408 #endif
1410 for (gr = 0; gr < nb_granules && (s->
last_buf_size >> 3) < main_data_begin; gr++) {
1411 for (ch = 0; ch < s->nb_channels; ch++) {
1416 }
1417 }
1423 } else {
1425 }
1426 } else {
1427 gr = 0;
1428 }
1429
1430 for (; gr < nb_granules; gr++) {
1431 for (ch = 0; ch < s->nb_channels; ch++) {
1434
1435 if (!s->lsf) {
1437 int slen, slen1, slen2;
1438
1439 /* MPEG1 scale factors */
1442 av_dlog(s->
avctx,
"slen1=%d slen2=%d\n", slen1, slen2);
1445 j = 0;
1446 if (slen1) {
1447 for (i = 0; i <
n; i++)
1449 } else {
1450 for (i = 0; i <
n; i++)
1452 }
1453 if (slen2) {
1454 for (i = 0; i < 18; i++)
1456 for (i = 0; i < 3; i++)
1458 } else {
1459 for (i = 0; i < 21; i++)
1461 }
1462 } else {
1464 j = 0;
1465 for (k = 0; k < 4; k++) {
1466 n = k == 0 ? 6 : 5;
1467 if ((g->
scfsi & (0x8 >> k)) == 0) {
1468 slen = (k < 2) ? slen1 : slen2;
1469 if (slen) {
1470 for (i = 0; i <
n; i++)
1472 } else {
1473 for (i = 0; i <
n; i++)
1475 }
1476 } else {
1477 /* simply copy from last granule */
1478 for (i = 0; i <
n; i++) {
1480 j++;
1481 }
1482 }
1483 }
1485 }
1486 } else {
1487 int tindex, tindex2, slen[4], sl, sf;
1488
1489 /* LSF scale factors */
1492 else
1493 tindex = 0;
1494
1497 /* intensity stereo case */
1498 sf >>= 1;
1499 if (sf < 180) {
1501 tindex2 = 3;
1502 } else if (sf < 244) {
1504 tindex2 = 4;
1505 } else {
1507 tindex2 = 5;
1508 }
1509 } else {
1510 /* normal case */
1511 if (sf < 400) {
1513 tindex2 = 0;
1514 } else if (sf < 500) {
1516 tindex2 = 1;
1517 } else {
1519 tindex2 = 2;
1521 }
1522 }
1523
1524 j = 0;
1525 for (k = 0; k < 4; k++) {
1527 sl = slen[k];
1528 if (sl) {
1529 for (i = 0; i <
n; i++)
1531 } else {
1532 for (i = 0; i <
n; i++)
1534 }
1535 }
1536 /* XXX: should compute exact size */
1537 for (; j < 40; j++)
1539 }
1540
1542
1543 /* read Huffman coded residue */
1545 } /* ch */
1546
1549
1550 for (ch = 0; ch < s->nb_channels; ch++) {
1552
1556 }
1557 } /* gr */
1560 return nb_granules * 18;
1561 }
1562
1565 {
1566 int i, nb_frames, ch,
ret;
1568
1570
1571 /* skip error protection field */
1572 if (s->error_protection)
1574
1575 switch(s->layer) {
1576 case 1:
1579 break;
1580 case 2:
1583 break;
1584 case 3:
1586 default:
1588
1596 } else
1600 }
1601
1605
1607 if (i < 0)
1610 }
1611 av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1614 }
1615
1616 if(nb_frames < 0)
1617 return nb_frames;
1618
1619 /* get output buffer */
1620 if (!samples) {
1626 }
1627
1628 /* apply the synthesis filter */
1629 for (ch = 0; ch < s->nb_channels; ch++) {
1630 int sample_stride;
1632 samples_ptr = samples[ch];
1633 sample_stride = 1;
1634 } else {
1635 samples_ptr = samples[0] + ch;
1636 sample_stride = s->nb_channels;
1637 }
1638 for (i = 0; i < nb_frames; i++) {
1641 RENAME(ff_mpa_synth_window),
1644 samples_ptr += 32 * sample_stride;
1645 }
1646 }
1647
1648 return nb_frames * 32 *
sizeof(
OUT_INT) * s->nb_channels;
1649 }
1650
1653 {
1655 int buf_size = avpkt->
size;
1659
1660 while(buf_size && !*buf){
1661 buf++;
1662 buf_size--;
1663 }
1664
1667
1669 if (header>>8 ==
AV_RB32(
"TAG")>>8) {
1671 return buf_size;
1672 }
1676 }
1677
1679 /* free format: prepare to compute frame size */
1680 s->frame_size = -1;
1682 }
1683 /* update codec info */
1688
1689 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1692 } else if (s->frame_size < buf_size) {
1694 buf_size= s->frame_size;
1695 }
1696
1698
1700 if (ret >= 0) {
1702 *got_frame_ptr = 1;
1704 //FIXME maybe move the other codec info stuff from above here too
1705 } else {
1707 /* Only return an error if the bad frame makes up the whole packet or
1708 * the error is related to buffer management.
1709 * If there is more data in the packet, just consume the bad frame
1710 * instead of returning an error, which would discard the whole
1711 * packet. */
1712 *got_frame_ptr = 0;
1715 }
1716 s->frame_size = 0;
1717 return buf_size;
1718 }
1719
1721 {
1726 }
1727
1729 {
1731 }
1732
1733 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1735 int *got_frame_ptr,
AVPacket *avpkt)
1736 {
1738 int buf_size = avpkt->
size;
1743
1744 len = buf_size;
1745
1746 // Discard too short frames
1750 }
1751
1752
1755
1756 // Get header and restore sync word
1757 header =
AV_RB32(buf) | 0xffe00000;
1758
1762 }
1763
1765 /* update codec info */
1771
1772 s->frame_size =
len;
1773
1775
1777 if (ret < 0) {
1780 }
1781
1782 *got_frame_ptr = 1;
1783
1784 return buf_size;
1785 }
1786 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1787
1788 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1789
1790 /**
1791 * Context for MP3On4 decoder
1792 */
1793 typedef struct MP3On4DecodeContext {
1794 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1795 int syncword; ///< syncword patch
1796 const uint8_t *coff;
///< channel offsets in output buffer
1797 MPADecodeContext *mp3decctx[5];
///< MPADecodeContext for every decoder instance
1798 } MP3On4DecodeContext;
1799
1801
1802 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1803
1804 /* number of mp3 decoder instances */
1805 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1806
1807 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1808 static const uint8_t chan_offset[8][5] = {
1809 { 0 },
1810 { 0 }, // C
1811 { 0 }, // FLR
1812 { 2, 0 }, // C FLR
1813 { 2, 0, 3 }, // C FLR BS
1814 { 2, 0, 3 }, // C FLR BLRS
1815 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1816 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1817 };
1818
1819 /* mp3on4 channel layouts */
1820 static const int16_t chan_layout[8] = {
1821 0,
1829 };
1830
1832 {
1833 MP3On4DecodeContext *s = avctx->
priv_data;
1834 int i;
1835
1836 for (i = 0; i < s->frames; i++)
1838
1839 return 0;
1840 }
1841
1842
1844 {
1845 MP3On4DecodeContext *s = avctx->
priv_data;
1847 int i;
1848
1852 }
1853
1859 }
1864
1866 s->syncword = 0xffe00000;
1867 else
1868 s->syncword = 0xfff00000;
1869
1870 /* Init the first mp3 decoder in standard way, so that all tables get builded
1871 * We replace avctx->priv_data with the context of the first decoder so that
1872 * decode_init() does not have to be changed.
1873 * Other decoders will be initialized here copying data from the first context
1874 */
1875 // Allocate zeroed memory for the first decoder context
1877 if (!s->mp3decctx[0])
1878 goto alloc_fail;
1879 // Put decoder context in place to make init_decode() happy
1882 // Restore mp3on4 context pointer
1884 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1885
1886 /* Create a separate codec/context for each frame (first is already ok).
1887 * Each frame is 1 or 2 channels - up to 5 frames allowed
1888 */
1889 for (i = 1; i < s->frames; i++) {
1891 if (!s->mp3decctx[i])
1892 goto alloc_fail;
1893 s->mp3decctx[i]->adu_mode = 1;
1894 s->mp3decctx[i]->avctx = avctx;
1895 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1896 }
1897
1898 return 0;
1899 alloc_fail:
1900 decode_close_mp3on4(avctx);
1902 }
1903
1904
1906 {
1907 int i;
1908 MP3On4DecodeContext *s = avctx->
priv_data;
1909
1910 for (i = 0; i < s->frames; i++)
1912 }
1913
1914
1915 static int decode_frame_mp3on4(
AVCodecContext *avctx,
void *data,
1916 int *got_frame_ptr,
AVPacket *avpkt)
1917 {
1920 int buf_size = avpkt->
size;
1921 MP3On4DecodeContext *s = avctx->
priv_data;
1923 int fsize, len = buf_size, out_size = 0;
1928
1929 /* get output buffer */
1934
1935 // Discard too short frames
1938
1940
1941 ch = 0;
1942 for (fr = 0; fr < s->frames; fr++) {
1945 m = s->mp3decctx[fr];
1947
1951 }
1952 header = (
AV_RB32(buf) & 0x000fffff) | s->syncword;
// patch header
1953
1957 }
1958
1960
1961 if (ch + m->nb_channels > avctx->
channels ||
1962 s->coff[fr] + m->nb_channels > avctx->
channels) {
1964 "channel count\n");
1966 }
1967 ch += m->nb_channels;
1968
1969 outptr[0] = out_samples[s->coff[fr]];
1970 if (m->nb_channels > 1)
1971 outptr[1] = out_samples[s->coff[fr] + 1];
1972
1976 if (m->nb_channels > 1)
1979 }
1980
1982 buf += fsize;
1983 len -= fsize;
1984
1986 }
1990 }
1991
1992 /* update codec info */
1993 avctx->
sample_rate = s->mp3decctx[0]->sample_rate;
1994
1996 *got_frame_ptr = 1;
1997
1998 return buf_size;
1999 }
2000 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */