45 #define BACKSTEP_SIZE 512
47 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
92 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
93 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
94 # define FIXR(x) ((float)(x))
95 # define FIXHR(x) ((float)(x))
96 # define MULH3(x, y, s) ((s)*(y)*(x))
97 # define MULLx(x, y, s) ((y)*(x))
98 # define RENAME(a) a ## _float
99 # define OUT_FMT AV_SAMPLE_FMT_FLT
100 # define OUT_FMT_P AV_SAMPLE_FMT_FLTP
102 # define SHR(a,b) ((a)>>(b))
104 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
105 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
106 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
107 # define MULH3(x, y, s) MULH((s)*(x), y)
108 # define MULLx(x, y, s) MULL(x,y,s)
109 # define RENAME(a) a ## _fixed
110 # define OUT_FMT AV_SAMPLE_FMT_S16
111 # define OUT_FMT_P AV_SAMPLE_FMT_S16P
116 #define HEADER_SIZE 4
124 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
125 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
128 0, 128, 128, 128, 130, 128, 154, 166,
129 142, 204, 190, 170, 542, 460, 662, 414
156 #define SCALE_GEN(v) \
157 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
173 for (i = 0; i < 3; i++) {
183 if (s->sample_rate_index != 8)
188 if (s->sample_rate_index <= 2)
190 else if (s->sample_rate_index != 8)
203 l =
FFMIN(ra1 + ra2 + 2, 22);
211 if(s->sample_rate_index == 8)
216 if (s->sample_rate_index <= 2)
234 static inline int l1_unscale(
int n,
int mant,
int scale_factor)
245 return (
int)((val + (1LL << (shift - 1))) >> shift);
259 val = (val + (1 << (shift - 1))) >> shift;
278 m = (m + (1 << (e - 1))) >> e;
289 for (i = 0; i < 64; i++) {
298 for (i = 0; i < 15; i++) {
301 norm = ((INT64_C(1) << n) *
FRAC_ONE) / ((1 << n) - 1);
315 for (i = 1; i < 16; i++) {
318 uint8_t tmp_bits [512] = { 0 };
319 uint16_t tmp_codes[512] = { 0 };
324 for (x = 0; x < xsize; x++) {
325 for (y = 0; y < xsize; y++) {
326 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->
bits [j ];
327 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->
codes[j++];
335 tmp_bits, 1, 1, tmp_codes, 2, 2,
342 for (i = 0; i < 2; i++) {
345 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
352 for (i = 0; i < 9; i++) {
354 for (j = 0; j < 22; j++) {
365 for (i = 0; i < 4; i++) {
368 int val1, val2, val3, steps;
381 for (i = 0; i < 7; i++) {
385 f = tan((
double)i *
M_PI / 12.0);
386 v =
FIXR(f / (1.0 + f));
394 for (i = 7; i < 16; i++)
397 for (i = 0; i < 16; i++) {
401 for (j = 0; j < 2; j++) {
402 e = -(j + 1) * ((i + 1) >> 1);
413 for (i = 0; i < 8; i++) {
416 cs = 1.0 / sqrt(1.0 + ci * ci);
434 static int initialized_tables = 0;
437 if (!initialized_tables) {
439 initialized_tables = 1;
463 #define C3 FIXHR(0.86602540378443864676/2)
464 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
465 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
466 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
475 in1 = in[1*3] + in[0*3];
476 in2 = in[2*3] + in[1*3];
477 in3 = in[3*3] + in[2*3];
478 in4 = in[4*3] + in[3*3];
479 in5 = in[5*3] + in[4*3];
514 int bound, i, v, n, ch, j, mant;
519 bound = (s->mode_ext + 1) * 4;
524 for (i = 0; i < bound; i++) {
525 for (ch = 0; ch < s->nb_channels; ch++) {
529 for (i = bound; i <
SBLIMIT; i++)
533 for (i = 0; i < bound; i++) {
534 for (ch = 0; ch < s->nb_channels; ch++) {
535 if (allocation[ch][i])
539 for (i = bound; i <
SBLIMIT; i++) {
540 if (allocation[0][i]) {
547 for (j = 0; j < 12; j++) {
548 for (i = 0; i < bound; i++) {
549 for (ch = 0; ch < s->nb_channels; ch++) {
550 n = allocation[ch][i];
553 v =
l1_unscale(n, mant, scale_factors[ch][i]);
560 for (i = bound; i <
SBLIMIT; i++) {
561 n = allocation[0][i];
581 int table, bit_alloc_bits, i, j, ch, bound, v;
589 s->sample_rate, s->lsf);
594 bound = (s->mode_ext + 1) * 4;
598 av_dlog(s->
avctx,
"bound=%d sblimit=%d\n", bound, sblimit);
606 for (i = 0; i < bound; i++) {
607 bit_alloc_bits = alloc_table[j];
608 for (ch = 0; ch < s->nb_channels; ch++)
609 bit_alloc[ch][i] =
get_bits(&s->
gb, bit_alloc_bits);
610 j += 1 << bit_alloc_bits;
612 for (i = bound; i < sblimit; i++) {
613 bit_alloc_bits = alloc_table[j];
617 j += 1 << bit_alloc_bits;
621 for (i = 0; i < sblimit; i++) {
622 for (ch = 0; ch < s->nb_channels; ch++) {
623 if (bit_alloc[ch][i])
629 for (i = 0; i < sblimit; i++) {
630 for (ch = 0; ch < s->nb_channels; ch++) {
631 if (bit_alloc[ch][i]) {
632 sf = scale_factors[ch][i];
633 switch (scale_code[ch][i]) {
661 for (k = 0; k < 3; k++) {
662 for (l = 0; l < 12; l += 3) {
664 for (i = 0; i < bound; i++) {
665 bit_alloc_bits = alloc_table[j];
666 for (ch = 0; ch < s->nb_channels; ch++) {
667 b = bit_alloc[ch][i];
669 scale = scale_factors[ch][i][k];
670 qindex = alloc_table[j+
b];
686 for (m = 0; m < 3; m++) {
699 j += 1 << bit_alloc_bits;
702 for (i = bound; i < sblimit; i++) {
703 bit_alloc_bits = alloc_table[j];
706 int mant, scale0, scale1;
707 scale0 = scale_factors[0][i][k];
708 scale1 = scale_factors[1][i][k];
709 qindex = alloc_table[j+
b];
732 for (m = 0; m < 3; m++) {
749 j += 1 << bit_alloc_bits;
752 for (i = sblimit; i <
SBLIMIT; i++) {
753 for (ch = 0; ch < s->nb_channels; ch++) {
764 #define SPLIT(dst,sf,n) \
766 int m = (sf * 171) >> 9; \
769 } else if (n == 4) { \
772 } else if (n == 5) { \
773 int m = (sf * 205) >> 10; \
776 } else if (n == 6) { \
777 int m = (sf * 171) >> 10; \
787 SPLIT(slen[3], sf, n3)
788 SPLIT(slen[2], sf, n2)
789 SPLIT(slen[1], sf, n1)
797 int len, i, j, k, l,
v0,
shift, gain, gains[3];
807 v0 = gain - ((g->
scale_factors[i] + pretab[i]) << shift) + 400;
809 for (j = len; j > 0; j--)
821 for (l = 0; l < 3; l++) {
823 for (j = len; j > 0; j--)
858 #define READ_FLIP_SIGN(dst,src) \
859 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
862 #define READ_FLIP_SIGN(dst,src) \
863 v = -get_bits1(&s->gb); \
864 *(dst) = (*(src) ^ v) - v;
868 int16_t *exponents,
int end_pos2)
872 int last_pos, bits_left;
878 for (i = 0; i < 3; i++) {
879 int j, k, l, linbits;
915 exponent= exponents[s_index];
962 while (s_index <= 572) {
965 if (pos >= end_pos) {
966 if (pos > end_pos2 && last_pos) {
971 av_log(s->
avctx,
AV_LOG_INFO,
"overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
989 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
991 int pos = s_index + idxtab[code];
992 code ^= 8 >> idxtab[code];
1028 if (s->sample_rate_index != 8)
1040 for (j = len; j > 0; j--) {
1041 *dst++ = ptr[0*
len];
1042 *dst++ = ptr[1*
len];
1043 *dst++ = ptr[2*
len];
1047 memcpy(ptr1, tmp, len * 3 *
sizeof(*ptr1));
1051 #define ISQRT2 FIXR(0.70710678118654752440)
1056 int sf_max, sf,
len, non_zero_found;
1057 INTFLOAT (*is_tab)[16], *tab0, *
tab1, tmp0, tmp1, v1, v2;
1058 int non_zero_found_short[3];
1073 non_zero_found_short[0] = 0;
1074 non_zero_found_short[1] = 0;
1075 non_zero_found_short[2] = 0;
1082 for (l = 2; l >= 0; l--) {
1085 if (!non_zero_found_short[l]) {
1087 for (j = 0; j <
len; j++) {
1089 non_zero_found_short[l] = 1;
1099 for (j = 0; j <
len; j++) {
1109 for (j = 0; j <
len; j++) {
1120 non_zero_found = non_zero_found_short[0] |
1121 non_zero_found_short[1] |
1122 non_zero_found_short[2];
1124 for (i = g1->
long_end - 1;i >= 0;i--) {
1129 if (!non_zero_found) {
1130 for (j = 0; j <
len; j++) {
1137 k = (i == 21) ? 20 : i;
1143 for (j = 0; j <
len; j++) {
1153 for (j = 0; j <
len; j++) {
1171 for (i = 0; i < 576; i++) {
1174 tab0[i] = tmp0 + tmp1;
1175 tab1[i] = tmp0 - tmp1;
1191 #ifndef compute_antialias
1193 #define AA(j) do { \
1194 float tmp0 = ptr[-1-j]; \
1195 float tmp1 = ptr[ j]; \
1196 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1197 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1200 #define AA(j) do { \
1201 int tmp0 = ptr[-1-j]; \
1202 int tmp1 = ptr[ j]; \
1203 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1204 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1205 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1225 for (i = n; i > 0; i--) {
1243 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1245 int i, j, mdct_long_end, sblimit;
1250 while (ptr >= ptr1) {
1254 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1257 sblimit = ((ptr - g->
sb_hybrid) / 18) + 1;
1266 mdct_long_end = sblimit;
1273 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1274 ptr = g->
sb_hybrid + 18 * mdct_long_end;
1276 for (j = mdct_long_end; j < sblimit; j++) {
1278 win =
RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1279 out_ptr = sb_samples + j;
1281 for (i = 0; i < 6; i++) {
1282 *out_ptr = buf[4*i];
1286 for (i = 0; i < 6; i++) {
1287 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1288 buf[4*(i + 6*2)] =
MULH3(out2[i + 6], win[i + 6], 1);
1292 for (i = 0; i < 6; i++) {
1293 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1294 buf[4*(i + 6*0)] =
MULH3(out2[i + 6], win[i + 6], 1);
1298 for (i = 0; i < 6; i++) {
1299 buf[4*(i + 6*0)] =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1300 buf[4*(i + 6*1)] =
MULH3(out2[i + 6], win[i + 6], 1);
1301 buf[4*(i + 6*2)] = 0;
1304 buf += (j&3) != 3 ? 1 : (4*18-3);
1307 for (j = sblimit; j <
SBLIMIT; j++) {
1309 out_ptr = sb_samples + j;
1310 for (i = 0; i < 18; i++) {
1311 *out_ptr = buf[4*i];
1315 buf += (j&3) != 3 ? 1 : (4*18-3);
1322 int nb_granules, main_data_begin;
1323 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1325 int16_t exponents[576];
1334 if (s->nb_channels == 2)
1339 for (ch = 0; ch < s->nb_channels; ch++) {
1345 for (gr = 0; gr < nb_granules; gr++) {
1346 for (ch = 0; ch < s->nb_channels; ch++) {
1367 if (blocksplit_flag) {
1374 for (i = 0; i < 2; i++)
1376 for (i = 0; i < 3; i++)
1380 int region_address1, region_address2;
1383 for (i = 0; i < 3; i++)
1389 region_address1, region_address2);
1417 #if !UNCHECKED_BITSTREAM_READER
1421 for (gr = 0; gr < nb_granules && (s->
last_buf_size >> 3) < main_data_begin; gr++) {
1422 for (ch = 0; ch < s->nb_channels; ch++) {
1441 for (; gr < nb_granules; gr++) {
1442 for (ch = 0; ch < s->nb_channels; ch++) {
1448 int slen, slen1, slen2;
1453 av_dlog(s->
avctx,
"slen1=%d slen2=%d\n", slen1, slen2);
1458 for (i = 0; i < n; i++)
1461 for (i = 0; i < n; i++)
1465 for (i = 0; i < 18; i++)
1467 for (i = 0; i < 3; i++)
1470 for (i = 0; i < 21; i++)
1476 for (k = 0; k < 4; k++) {
1478 if ((g->
scfsi & (0x8 >> k)) == 0) {
1479 slen = (k < 2) ? slen1 : slen2;
1481 for (i = 0; i < n; i++)
1484 for (i = 0; i < n; i++)
1489 for (i = 0; i < n; i++) {
1498 int tindex, tindex2, slen[4], sl, sf;
1513 }
else if (sf < 244) {
1525 }
else if (sf < 500) {
1536 for (k = 0; k < 4; k++) {
1540 for (i = 0; i < n; i++)
1543 for (i = 0; i < n; i++)
1561 for (ch = 0; ch < s->nb_channels; ch++) {
1571 return nb_granules * 18;
1575 const uint8_t *buf,
int buf_size)
1577 int i, nb_frames, ch, ret;
1583 if (s->error_protection)
1622 av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1641 for (ch = 0; ch < s->nb_channels; ch++) {
1644 samples_ptr = samples[ch];
1647 samples_ptr = samples[0] + ch;
1648 sample_stride = s->nb_channels;
1650 for (i = 0; i < nb_frames; i++) {
1653 RENAME(ff_mpa_synth_window),
1656 samples_ptr += 32 * sample_stride;
1660 return nb_frames * 32 *
sizeof(
OUT_INT) * s->nb_channels;
1667 int buf_size = avpkt->
size;
1672 while(buf_size && !*buf){
1681 if (header>>8 ==
AV_RB32(
"TAG")>>8) {
1701 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1704 }
else if (s->frame_size < buf_size) {
1706 buf_size= s->frame_size;
1741 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1743 int *got_frame_ptr,
AVPacket *avpkt)
1746 int buf_size = avpkt->
size;
1765 header =
AV_RB32(buf) | 0xffe00000;
1779 s->frame_size =
len;
1794 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1799 typedef struct MP3On4DecodeContext {
1805 } MP3On4DecodeContext;
1812 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1815 static const uint8_t chan_offset[8][5] = {
1827 static const int16_t chan_layout[8] = {
1840 MP3On4DecodeContext *s = avctx->
priv_data;
1843 for (i = 0; i < s->frames; i++)
1852 MP3On4DecodeContext *s = avctx->
priv_data;
1873 s->syncword = 0xffe00000;
1875 s->syncword = 0xfff00000;
1884 if (!s->mp3decctx[0])
1892 s->mp3decctx[0]->adu_mode = 1;
1897 for (i = 1; i < s->frames; i++) {
1899 if (!s->mp3decctx[i])
1901 s->mp3decctx[i]->adu_mode = 1;
1902 s->mp3decctx[i]->avctx = avctx;
1903 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1908 decode_close_mp3on4(avctx);
1916 MP3On4DecodeContext *s = avctx->
priv_data;
1918 for (i = 0; i < s->frames; i++)
1923 static int decode_frame_mp3on4(
AVCodecContext *avctx,
void *data,
1924 int *got_frame_ptr,
AVPacket *avpkt)
1927 int buf_size = avpkt->
size;
1928 MP3On4DecodeContext *s = avctx->
priv_data;
1930 int fsize, len = buf_size, out_size = 0;
1942 out_samples = (
OUT_INT **)s->frame->extended_data;
1951 for (fr = 0; fr < s->frames; fr++) {
1954 m = s->mp3decctx[fr];
1961 header = (
AV_RB32(buf) & 0x000fffff) | s->syncword;
1968 if (ch + m->nb_channels > avctx->
channels || s->coff[fr] + m->nb_channels > avctx->
channels) {
1973 ch += m->nb_channels;
1975 outptr[0] = out_samples[s->coff[fr]];
1976 if (m->nb_channels > 1)
1977 outptr[1] = out_samples[s->coff[fr] + 1];
1990 avctx->
sample_rate = s->mp3decctx[0]->sample_rate;
1992 s->frame->nb_samples = out_size / (avctx->
channels *
sizeof(
OUT_INT));
2001 #if CONFIG_MP1_DECODER
2017 #if CONFIG_MP2_DECODER
2033 #if CONFIG_MP3_DECODER
2049 #if CONFIG_MP3ADU_DECODER
2056 .
decode = decode_frame_adu,
2065 #if CONFIG_MP3ON4_DECODER
2070 .priv_data_size =
sizeof(MP3On4DecodeContext),
2071 .
init = decode_init_mp3on4,
2072 .
close = decode_close_mp3on4,
2073 .
decode = decode_frame_mp3on4,
2075 .
flush = flush_mp3on4,