44 #define BACKSTEP_SIZE 512
46 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
91 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
92 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
93 # define FIXR(x) ((float)(x))
94 # define FIXHR(x) ((float)(x))
95 # define MULH3(x, y, s) ((s)*(y)*(x))
96 # define MULLx(x, y, s) ((y)*(x))
97 # define RENAME(a) a ## _float
98 # define OUT_FMT AV_SAMPLE_FMT_FLT
99 # define OUT_FMT_P AV_SAMPLE_FMT_FLTP
101 # define SHR(a,b) ((a)>>(b))
103 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
104 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
105 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
106 # define MULH3(x, y, s) MULH((s)*(x), y)
107 # define MULLx(x, y, s) MULL(x,y,s)
108 # define RENAME(a) a ## _fixed
109 # define OUT_FMT AV_SAMPLE_FMT_S16
110 # define OUT_FMT_P AV_SAMPLE_FMT_S16P
115 #define HEADER_SIZE 4
123 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
124 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
127 0, 128, 128, 128, 130, 128, 154, 166,
128 142, 204, 190, 170, 542, 460, 662, 414
155 #define SCALE_GEN(v) \
156 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
172 for (i = 0; i < 3; i++) {
182 if (s->sample_rate_index != 8)
187 if (s->sample_rate_index <= 2)
189 else if (s->sample_rate_index != 8)
202 l =
FFMIN(ra1 + ra2 + 2, 22);
213 if (s->sample_rate_index <= 2)
231 static inline int l1_unscale(
int n,
int mant,
int scale_factor)
242 return (
int)((val + (1LL << (shift - 1))) >> shift);
256 val = (val + (1 << (shift - 1))) >> shift;
272 m = (m + (1 << (e - 1))) >> e;
283 for (i = 0; i < 64; i++) {
292 for (i = 0; i < 15; i++) {
295 norm = ((INT64_C(1) << n) *
FRAC_ONE) / ((1 << n) - 1);
309 for (i = 1; i < 16; i++) {
312 uint8_t tmp_bits [512] = { 0 };
313 uint16_t tmp_codes[512] = { 0 };
318 for (x = 0; x < xsize; x++) {
319 for (y = 0; y < xsize; y++) {
320 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->
bits [j ];
321 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->
codes[j++];
329 tmp_bits, 1, 1, tmp_codes, 2, 2,
336 for (i = 0; i < 2; i++) {
339 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
346 for (i = 0; i < 9; i++) {
348 for (j = 0; j < 22; j++) {
359 for (i = 0; i < 4; i++) {
362 int val1, val2, val3, steps;
375 for (i = 0; i < 7; i++) {
379 f = tan((
double)i * M_PI / 12.0);
380 v =
FIXR(f / (1.0 + f));
388 for (i = 7; i < 16; i++)
391 for (i = 0; i < 16; i++) {
395 for (j = 0; j < 2; j++) {
396 e = -(j + 1) * ((i + 1) >> 1);
397 f = pow(2.0, e / 4.0);
407 for (i = 0; i < 8; i++) {
410 cs = 1.0 / sqrt(1.0 + ci * ci);
428 static int initialized_tables = 0;
431 if (!initialized_tables) {
433 initialized_tables = 1;
457 #define C3 FIXHR(0.86602540378443864676/2)
458 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
459 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
460 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
469 in1 = in[1*3] + in[0*3];
470 in2 = in[2*3] + in[1*3];
471 in3 = in[3*3] + in[2*3];
472 in4 = in[4*3] + in[3*3];
473 in5 = in[5*3] + in[4*3];
508 int bound, i, v, n, ch, j, mant;
513 bound = (s->mode_ext + 1) * 4;
518 for (i = 0; i < bound; i++) {
519 for (ch = 0; ch < s->nb_channels; ch++) {
523 for (i = bound; i <
SBLIMIT; i++)
527 for (i = 0; i < bound; i++) {
528 for (ch = 0; ch < s->nb_channels; ch++) {
529 if (allocation[ch][i])
533 for (i = bound; i <
SBLIMIT; i++) {
534 if (allocation[0][i]) {
541 for (j = 0; j < 12; j++) {
542 for (i = 0; i < bound; i++) {
543 for (ch = 0; ch < s->nb_channels; ch++) {
544 n = allocation[ch][i];
547 v =
l1_unscale(n, mant, scale_factors[ch][i]);
554 for (i = bound; i <
SBLIMIT; i++) {
555 n = allocation[0][i];
575 int table, bit_alloc_bits, i, j, ch, bound, v;
583 s->sample_rate, s->lsf);
588 bound = (s->mode_ext + 1) * 4;
592 av_dlog(s->
avctx,
"bound=%d sblimit=%d\n", bound, sblimit);
600 for (i = 0; i < bound; i++) {
601 bit_alloc_bits = alloc_table[j];
602 for (ch = 0; ch < s->nb_channels; ch++)
603 bit_alloc[ch][i] =
get_bits(&s->
gb, bit_alloc_bits);
604 j += 1 << bit_alloc_bits;
606 for (i = bound; i < sblimit; i++) {
607 bit_alloc_bits = alloc_table[j];
611 j += 1 << bit_alloc_bits;
615 for (i = 0; i < sblimit; i++) {
616 for (ch = 0; ch < s->nb_channels; ch++) {
617 if (bit_alloc[ch][i])
623 for (i = 0; i < sblimit; i++) {
624 for (ch = 0; ch < s->nb_channels; ch++) {
625 if (bit_alloc[ch][i]) {
626 sf = scale_factors[ch][i];
627 switch (scale_code[ch][i]) {
655 for (k = 0; k < 3; k++) {
656 for (l = 0; l < 12; l += 3) {
658 for (i = 0; i < bound; i++) {
659 bit_alloc_bits = alloc_table[j];
660 for (ch = 0; ch < s->nb_channels; ch++) {
661 b = bit_alloc[ch][i];
663 scale = scale_factors[ch][i][k];
664 qindex = alloc_table[j+
b];
680 for (m = 0; m < 3; m++) {
693 j += 1 << bit_alloc_bits;
696 for (i = bound; i < sblimit; i++) {
697 bit_alloc_bits = alloc_table[j];
700 int mant, scale0, scale1;
701 scale0 = scale_factors[0][i][k];
702 scale1 = scale_factors[1][i][k];
703 qindex = alloc_table[j+
b];
726 for (m = 0; m < 3; m++) {
743 j += 1 << bit_alloc_bits;
746 for (i = sblimit; i <
SBLIMIT; i++) {
747 for (ch = 0; ch < s->nb_channels; ch++) {
758 #define SPLIT(dst,sf,n) \
760 int m = (sf * 171) >> 9; \
763 } else if (n == 4) { \
766 } else if (n == 5) { \
767 int m = (sf * 205) >> 10; \
770 } else if (n == 6) { \
771 int m = (sf * 171) >> 10; \
781 SPLIT(slen[3], sf, n3)
782 SPLIT(slen[2], sf, n2)
783 SPLIT(slen[1], sf, n1)
791 int len, i, j, k, l,
v0, shift, gain, gains[3];
801 v0 = gain - ((g->
scale_factors[i] + pretab[i]) << shift) + 400;
803 for (j = len; j > 0; j--)
815 for (l = 0; l < 3; l++) {
817 for (j = len; j > 0; j--)
852 #define READ_FLIP_SIGN(dst,src) \
853 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
856 #define READ_FLIP_SIGN(dst,src) \
857 v = -get_bits1(&s->gb); \
858 *(dst) = (*(src) ^ v) - v;
862 int16_t *exponents,
int end_pos2)
866 int last_pos, bits_left;
872 for (i = 0; i < 3; i++) {
873 int j, k, l, linbits;
909 exponent= exponents[s_index];
956 while (s_index <= 572) {
959 if (pos >= end_pos) {
960 if (pos > end_pos2 && last_pos) {
965 av_log(s->
avctx,
AV_LOG_INFO,
"overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
983 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
985 int pos = s_index + idxtab[code];
986 code ^= 8 >> idxtab[code];
1022 if (s->sample_rate_index != 8)
1034 for (j = len; j > 0; j--) {
1035 *dst++ = ptr[0*
len];
1036 *dst++ = ptr[1*
len];
1037 *dst++ = ptr[2*
len];
1041 memcpy(ptr1, tmp, len * 3 *
sizeof(*ptr1));
1045 #define ISQRT2 FIXR(0.70710678118654752440)
1050 int sf_max, sf,
len, non_zero_found;
1051 INTFLOAT (*is_tab)[16], *tab0, *
tab1, tmp0, tmp1, v1, v2;
1052 int non_zero_found_short[3];
1067 non_zero_found_short[0] = 0;
1068 non_zero_found_short[1] = 0;
1069 non_zero_found_short[2] = 0;
1076 for (l = 2; l >= 0; l--) {
1079 if (!non_zero_found_short[l]) {
1081 for (j = 0; j <
len; j++) {
1083 non_zero_found_short[l] = 1;
1093 for (j = 0; j <
len; j++) {
1103 for (j = 0; j <
len; j++) {
1114 non_zero_found = non_zero_found_short[0] |
1115 non_zero_found_short[1] |
1116 non_zero_found_short[2];
1118 for (i = g1->
long_end - 1;i >= 0;i--) {
1123 if (!non_zero_found) {
1124 for (j = 0; j <
len; j++) {
1131 k = (i == 21) ? 20 : i;
1137 for (j = 0; j <
len; j++) {
1147 for (j = 0; j <
len; j++) {
1165 for (i = 0; i < 576; i++) {
1168 tab0[i] = tmp0 + tmp1;
1169 tab1[i] = tmp0 - tmp1;
1176 #define AA(j) do { \
1177 float tmp0 = ptr[-1-j]; \
1178 float tmp1 = ptr[ j]; \
1179 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1180 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1183 #define AA(j) do { \
1184 int tmp0 = ptr[-1-j]; \
1185 int tmp1 = ptr[ j]; \
1186 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1187 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1188 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1208 for (i = n; i > 0; i--) {
1225 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1227 int i, j, mdct_long_end, sblimit;
1232 while (ptr >= ptr1) {
1236 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1239 sblimit = ((ptr - g->
sb_hybrid) / 18) + 1;
1248 mdct_long_end = sblimit;
1255 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1256 ptr = g->
sb_hybrid + 18 * mdct_long_end;
1258 for (j = mdct_long_end; j < sblimit; j++) {
1260 win =
RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1261 out_ptr = sb_samples + j;
1263 for (i = 0; i < 6; i++) {
1264 *out_ptr = buf[4*i];
1268 for (i = 0; i < 6; i++) {
1269 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1270 buf[4*(i + 6*2)] =
MULH3(out2[i + 6], win[i + 6], 1);
1274 for (i = 0; i < 6; i++) {
1275 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1276 buf[4*(i + 6*0)] =
MULH3(out2[i + 6], win[i + 6], 1);
1280 for (i = 0; i < 6; i++) {
1281 buf[4*(i + 6*0)] =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1282 buf[4*(i + 6*1)] =
MULH3(out2[i + 6], win[i + 6], 1);
1283 buf[4*(i + 6*2)] = 0;
1286 buf += (j&3) != 3 ? 1 : (4*18-3);
1289 for (j = sblimit; j <
SBLIMIT; j++) {
1291 out_ptr = sb_samples + j;
1292 for (i = 0; i < 18; i++) {
1293 *out_ptr = buf[4*i];
1297 buf += (j&3) != 3 ? 1 : (4*18-3);
1304 int nb_granules, main_data_begin;
1305 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1307 int16_t exponents[576];
1316 if (s->nb_channels == 2)
1321 for (ch = 0; ch < s->nb_channels; ch++) {
1327 for (gr = 0; gr < nb_granules; gr++) {
1328 for (ch = 0; ch < s->nb_channels; ch++) {
1349 if (blocksplit_flag) {
1356 for (i = 0; i < 2; i++)
1358 for (i = 0; i < 3; i++)
1362 int region_address1, region_address2;
1365 for (i = 0; i < 3; i++)
1371 region_address1, region_address2);
1400 #if !UNCHECKED_BITSTREAM_READER
1401 s->
gb.size_in_bits_plus8 += extrasize * 8;
1404 for (gr = 0; gr < nb_granules && (s->
last_buf_size >> 3) < main_data_begin; gr++) {
1405 for (ch = 0; ch < s->nb_channels; ch++) {
1424 for (; gr < nb_granules; gr++) {
1425 for (ch = 0; ch < s->nb_channels; ch++) {
1431 int slen, slen1, slen2;
1436 av_dlog(s->
avctx,
"slen1=%d slen2=%d\n", slen1, slen2);
1441 for (i = 0; i < n; i++)
1444 for (i = 0; i < n; i++)
1448 for (i = 0; i < 18; i++)
1450 for (i = 0; i < 3; i++)
1453 for (i = 0; i < 21; i++)
1459 for (k = 0; k < 4; k++) {
1461 if ((g->
scfsi & (0x8 >> k)) == 0) {
1462 slen = (k < 2) ? slen1 : slen2;
1464 for (i = 0; i < n; i++)
1467 for (i = 0; i < n; i++)
1472 for (i = 0; i < n; i++) {
1481 int tindex, tindex2, slen[4], sl, sf;
1496 }
else if (sf < 244) {
1508 }
else if (sf < 500) {
1519 for (k = 0; k < 4; k++) {
1523 for (i = 0; i < n; i++)
1526 for (i = 0; i < n; i++)
1544 for (ch = 0; ch < s->nb_channels; ch++) {
1554 return nb_granules * 18;
1558 const uint8_t *buf,
int buf_size)
1560 int i, nb_frames, ch, ret;
1566 if (s->error_protection)
1608 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1624 for (ch = 0; ch < s->nb_channels; ch++) {
1627 samples_ptr = samples[ch];
1630 samples_ptr = samples[0] + ch;
1631 sample_stride = s->nb_channels;
1633 for (i = 0; i < nb_frames; i++) {
1636 RENAME(ff_mpa_synth_window),
1639 samples_ptr += 32 * sample_stride;
1643 return nb_frames * 32 *
sizeof(
OUT_INT) * s->nb_channels;
1650 int buf_size = avpkt->
size;
1675 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1678 }
else if (s->frame_size < buf_size) {
1679 buf_size= s->frame_size;
1714 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1716 int *got_frame_ptr,
AVPacket *avpkt)
1719 int buf_size = avpkt->
size;
1728 av_log(avctx, AV_LOG_ERROR,
"Packet is too small\n");
1737 header =
AV_RB32(buf) | 0xffe00000;
1740 av_log(avctx, AV_LOG_ERROR,
"Invalid frame header\n");
1751 s->frame_size =
len;
1755 av_log(avctx, AV_LOG_ERROR,
"Error while decoding MPEG audio frame.\n");
1766 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1771 typedef struct MP3On4DecodeContext {
1777 } MP3On4DecodeContext;
1784 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1787 static const uint8_t chan_offset[8][5] = {
1799 static const int16_t chan_layout[8] = {
1812 MP3On4DecodeContext *s = avctx->
priv_data;
1815 for (i = 0; i < s->frames; i++)
1824 MP3On4DecodeContext *s = avctx->
priv_data;
1829 av_log(avctx, AV_LOG_ERROR,
"Codec extradata missing or too short.\n");
1836 av_log(avctx, AV_LOG_ERROR,
"Invalid channel config number.\n");
1845 s->syncword = 0xffe00000;
1847 s->syncword = 0xfff00000;
1856 if (!s->mp3decctx[0])
1864 s->mp3decctx[0]->adu_mode = 1;
1869 for (i = 1; i < s->frames; i++) {
1871 if (!s->mp3decctx[i])
1873 s->mp3decctx[i]->adu_mode = 1;
1874 s->mp3decctx[i]->avctx = avctx;
1875 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1880 decode_close_mp3on4(avctx);
1888 MP3On4DecodeContext *s = avctx->
priv_data;
1890 for (i = 0; i < s->frames; i++)
1895 static int decode_frame_mp3on4(
AVCodecContext *avctx,
void *data,
1896 int *got_frame_ptr,
AVPacket *avpkt)
1899 int buf_size = avpkt->
size;
1900 MP3On4DecodeContext *s = avctx->
priv_data;
1902 int fsize, len = buf_size, out_size = 0;
1911 av_log(avctx, AV_LOG_ERROR,
"get_buffer() failed\n");
1914 out_samples = (
OUT_INT **)s->frame->extended_data;
1923 for (fr = 0; fr < s->frames; fr++) {
1926 m = s->mp3decctx[fr];
1930 av_log(avctx, AV_LOG_ERROR,
"Frame size smaller than header size\n");
1933 header = (
AV_RB32(buf) & 0x000fffff) | s->syncword;
1940 if (ch + m->nb_channels > avctx->
channels ||
1941 s->coff[fr] + m->nb_channels > avctx->
channels) {
1942 av_log(avctx, AV_LOG_ERROR,
"frame channel count exceeds codec "
1946 ch += m->nb_channels;
1948 outptr[0] = out_samples[s->coff[fr]];
1949 if (m->nb_channels > 1)
1950 outptr[1] = out_samples[s->coff[fr] + 1];
1963 avctx->
sample_rate = s->mp3decctx[0]->sample_rate;
1965 s->frame->nb_samples = out_size / (avctx->
channels *
sizeof(
OUT_INT));
1974 #if CONFIG_MP1_DECODER
1982 .capabilities = CODEC_CAP_DR1,
1990 #if CONFIG_MP2_DECODER
1998 .capabilities = CODEC_CAP_DR1,
2006 #if CONFIG_MP3_DECODER
2014 .capabilities = CODEC_CAP_DR1,
2022 #if CONFIG_MP3ADU_DECODER
2029 .
decode = decode_frame_adu,
2030 .capabilities = CODEC_CAP_DR1,
2038 #if CONFIG_MP3ON4_DECODER
2043 .priv_data_size =
sizeof(MP3On4DecodeContext),
2044 .
init = decode_init_mp3on4,
2045 .
close = decode_close_mp3on4,
2046 .
decode = decode_frame_mp3on4,
2047 .capabilities = CODEC_CAP_DR1,
2048 .
flush = flush_mp3on4,