42 #define BACKSTEP_SIZE 512
44 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
88 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
89 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
90 # define FIXR(x) ((float)(x))
91 # define FIXHR(x) ((float)(x))
92 # define MULH3(x, y, s) ((s)*(y)*(x))
93 # define MULLx(x, y, s) ((y)*(x))
94 # define RENAME(a) a ## _float
95 # define OUT_FMT AV_SAMPLE_FMT_FLT
97 # define SHR(a,b) ((a)>>(b))
99 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
100 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
101 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
102 # define MULH3(x, y, s) MULH((s)*(x), y)
103 # define MULLx(x, y, s) MULL(x,y,s)
104 # define RENAME(a) a ## _fixed
105 # define OUT_FMT AV_SAMPLE_FMT_S16
110 #define HEADER_SIZE 4
118 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
119 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
122 0, 128, 128, 128, 130, 128, 154, 166,
123 142, 204, 190, 170, 542, 460, 662, 414
150 #define SCALE_GEN(v) \
151 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
167 for (i = 0; i < 3; i++) {
179 if (s->sample_rate_index <= 2)
181 else if (s->sample_rate_index != 8)
194 l =
FFMIN(ra1 + ra2 + 2, 22);
205 if (s->sample_rate_index <= 2)
207 else if (s->sample_rate_index != 8)
225 static inline int l1_unscale(
int n,
int mant,
int scale_factor)
236 return (
int)((val + (1LL << (shift - 1))) >> shift);
250 val = (val + (1 << (shift - 1))) >> shift;
266 m = (m + (1 << (e - 1))) >> e;
277 for (i = 0; i < 64; i++) {
286 for (i = 0; i < 15; i++) {
289 norm = ((INT64_C(1) << n) *
FRAC_ONE) / ((1 << n) - 1);
303 for (i = 1; i < 16; i++) {
306 uint8_t tmp_bits [512];
307 uint16_t tmp_codes[512];
309 memset(tmp_bits , 0,
sizeof(tmp_bits ));
310 memset(tmp_codes, 0,
sizeof(tmp_codes));
315 for (x = 0; x < xsize; x++) {
316 for (y = 0; y < xsize; y++) {
317 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->
bits [j ];
318 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->
codes[j++];
326 tmp_bits, 1, 1, tmp_codes, 2, 2,
333 for (i = 0; i < 2; i++) {
336 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
343 for (i = 0; i < 9; i++) {
345 for (j = 0; j < 22; j++) {
356 for (i = 0; i < 4; i++) {
359 int val1, val2, val3, steps;
372 for (i = 0; i < 7; i++) {
376 f = tan((
double)i *
M_PI / 12.0);
377 v =
FIXR(f / (1.0 + f));
385 for (i = 7; i < 16; i++)
388 for (i = 0; i < 16; i++) {
392 for (j = 0; j < 2; j++) {
393 e = -(j + 1) * ((i + 1) >> 1);
394 f = pow(2.0, e / 4.0);
404 for (i = 0; i < 8; i++) {
407 cs = 1.0 / sqrt(1.0 + ci * ci);
425 static int initialized_tables = 0;
428 if (!initialized_tables) {
430 initialized_tables = 1;
449 #define C3 FIXHR(0.86602540378443864676/2)
450 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
451 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
452 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
461 in1 = in[1*3] + in[0*3];
462 in2 = in[2*3] + in[1*3];
463 in3 = in[3*3] + in[2*3];
464 in4 = in[4*3] + in[3*3];
465 in5 = in[5*3] + in[4*3];
500 int bound, i,
v, n, ch, j, mant;
505 bound = (s->mode_ext + 1) * 4;
510 for (i = 0; i < bound; i++) {
511 for (ch = 0; ch < s->nb_channels; ch++) {
515 for (i = bound; i <
SBLIMIT; i++)
519 for (i = 0; i < bound; i++) {
520 for (ch = 0; ch < s->nb_channels; ch++) {
521 if (allocation[ch][i])
525 for (i = bound; i <
SBLIMIT; i++) {
526 if (allocation[0][i]) {
533 for (j = 0; j < 12; j++) {
534 for (i = 0; i < bound; i++) {
535 for (ch = 0; ch < s->nb_channels; ch++) {
536 n = allocation[ch][i];
539 v =
l1_unscale(n, mant, scale_factors[ch][i]);
546 for (i = bound; i <
SBLIMIT; i++) {
547 n = allocation[0][i];
567 int table, bit_alloc_bits, i, j, ch, bound,
v;
575 s->sample_rate, s->lsf);
580 bound = (s->mode_ext + 1) * 4;
584 av_dlog(s->
avctx,
"bound=%d sblimit=%d\n", bound, sblimit);
592 for (i = 0; i < bound; i++) {
593 bit_alloc_bits = alloc_table[j];
594 for (ch = 0; ch < s->nb_channels; ch++)
595 bit_alloc[ch][i] =
get_bits(&s->
gb, bit_alloc_bits);
596 j += 1 << bit_alloc_bits;
598 for (i = bound; i < sblimit; i++) {
599 bit_alloc_bits = alloc_table[j];
603 j += 1 << bit_alloc_bits;
607 for (i = 0; i < sblimit; i++) {
608 for (ch = 0; ch < s->nb_channels; ch++) {
609 if (bit_alloc[ch][i])
615 for (i = 0; i < sblimit; i++) {
616 for (ch = 0; ch < s->nb_channels; ch++) {
617 if (bit_alloc[ch][i]) {
618 sf = scale_factors[ch][i];
619 switch (scale_code[ch][i]) {
647 for (k = 0; k < 3; k++) {
648 for (l = 0; l < 12; l += 3) {
650 for (i = 0; i < bound; i++) {
651 bit_alloc_bits = alloc_table[j];
652 for (ch = 0; ch < s->nb_channels; ch++) {
653 b = bit_alloc[ch][i];
655 scale = scale_factors[ch][i][k];
656 qindex = alloc_table[j+
b];
672 for (m = 0; m < 3; m++) {
685 j += 1 << bit_alloc_bits;
688 for (i = bound; i < sblimit; i++) {
689 bit_alloc_bits = alloc_table[j];
692 int mant, scale0, scale1;
693 scale0 = scale_factors[0][i][k];
694 scale1 = scale_factors[1][i][k];
695 qindex = alloc_table[j+
b];
718 for (m = 0; m < 3; m++) {
735 j += 1 << bit_alloc_bits;
738 for (i = sblimit; i <
SBLIMIT; i++) {
739 for (ch = 0; ch < s->nb_channels; ch++) {
750 #define SPLIT(dst,sf,n) \
752 int m = (sf * 171) >> 9; \
755 } else if (n == 4) { \
758 } else if (n == 5) { \
759 int m = (sf * 205) >> 10; \
762 } else if (n == 6) { \
763 int m = (sf * 171) >> 10; \
773 SPLIT(slen[3], sf, n3)
774 SPLIT(slen[2], sf, n2)
775 SPLIT(slen[1], sf, n1)
782 const uint8_t *bstab, *pretab;
783 int len, i, j, k, l,
v0, shift, gain, gains[3];
793 v0 = gain - ((g->
scale_factors[i] + pretab[i]) << shift) + 400;
795 for (j = len; j > 0; j--)
807 for (l = 0; l < 3; l++) {
809 for (j = len; j > 0; j--)
844 #define READ_FLIP_SIGN(dst,src) \
845 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
848 #define READ_FLIP_SIGN(dst,src) \
849 v = -get_bits1(&s->gb); \
850 *(dst) = (*(src) ^ v) - v;
854 int16_t *exponents,
int end_pos2)
858 int last_pos, bits_left;
864 for (i = 0; i < 3; i++) {
865 int j, k, l, linbits;
903 exponent= exponents[s_index];
950 while (s_index <= 572) {
953 if (pos >= end_pos) {
954 if (pos > end_pos2 && last_pos) {
959 av_log(s->
avctx,
AV_LOG_INFO,
"overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
979 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
981 int pos = s_index + idxtab[code];
982 code ^= 8 >> idxtab[code];
1019 if (s->sample_rate_index != 8)
1031 for (j = len; j > 0; j--) {
1032 *dst++ = ptr[0*
len];
1033 *dst++ = ptr[1*
len];
1034 *dst++ = ptr[2*
len];
1038 memcpy(ptr1, tmp, len * 3 *
sizeof(*ptr1));
1042 #define ISQRT2 FIXR(0.70710678118654752440)
1047 int sf_max, sf,
len, non_zero_found;
1048 INTFLOAT (*is_tab)[16], *tab0, *
tab1, tmp0, tmp1, v1, v2;
1049 int non_zero_found_short[3];
1064 non_zero_found_short[0] = 0;
1065 non_zero_found_short[1] = 0;
1066 non_zero_found_short[2] = 0;
1073 for (l = 2; l >= 0; l--) {
1076 if (!non_zero_found_short[l]) {
1078 for (j = 0; j <
len; j++) {
1080 non_zero_found_short[l] = 1;
1090 for (j = 0; j <
len; j++) {
1100 for (j = 0; j <
len; j++) {
1111 non_zero_found = non_zero_found_short[0] |
1112 non_zero_found_short[1] |
1113 non_zero_found_short[2];
1115 for (i = g1->
long_end - 1;i >= 0;i--) {
1120 if (!non_zero_found) {
1121 for (j = 0; j <
len; j++) {
1128 k = (i == 21) ? 20 : i;
1134 for (j = 0; j <
len; j++) {
1144 for (j = 0; j <
len; j++) {
1159 for (i = 0; i < 576; i++) {
1162 tab0[i] = tmp0 + tmp1;
1163 tab1[i] = tmp0 - tmp1;
1169 #define AA(j) do { \
1170 float tmp0 = ptr[-1-j]; \
1171 float tmp1 = ptr[ j]; \
1172 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1173 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1176 #define AA(j) do { \
1177 int tmp0 = ptr[-1-j]; \
1178 int tmp1 = ptr[ j]; \
1179 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1180 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1181 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1201 for (i = n; i > 0; i--) {
1218 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1220 int i, j, mdct_long_end, sblimit;
1225 while (ptr >= ptr1) {
1229 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1232 sblimit = ((ptr - g->
sb_hybrid) / 18) + 1;
1241 mdct_long_end = sblimit;
1248 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1249 ptr = g->
sb_hybrid + 18 * mdct_long_end;
1251 for (j = mdct_long_end; j < sblimit; j++) {
1253 win =
RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1254 out_ptr = sb_samples + j;
1256 for (i = 0; i < 6; i++) {
1257 *out_ptr = buf[4*i];
1261 for (i = 0; i < 6; i++) {
1262 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1263 buf[4*(i + 6*2)] =
MULH3(out2[i + 6], win[i + 6], 1);
1267 for (i = 0; i < 6; i++) {
1268 *out_ptr =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1269 buf[4*(i + 6*0)] =
MULH3(out2[i + 6], win[i + 6], 1);
1273 for (i = 0; i < 6; i++) {
1274 buf[4*(i + 6*0)] =
MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1275 buf[4*(i + 6*1)] =
MULH3(out2[i + 6], win[i + 6], 1);
1276 buf[4*(i + 6*2)] = 0;
1279 buf += (j&3) != 3 ? 1 : (4*18-3);
1282 for (j = sblimit; j <
SBLIMIT; j++) {
1284 out_ptr = sb_samples + j;
1285 for (i = 0; i < 18; i++) {
1286 *out_ptr = buf[4*i];
1290 buf += (j&3) != 3 ? 1 : (4*18-3);
1297 int nb_granules, main_data_begin;
1298 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1300 int16_t exponents[576];
1309 if (s->nb_channels == 2)
1314 for (ch = 0; ch < s->nb_channels; ch++) {
1320 for (gr = 0; gr < nb_granules; gr++) {
1321 for (ch = 0; ch < s->nb_channels; ch++) {
1342 if (blocksplit_flag) {
1349 for (i = 0; i < 2; i++)
1351 for (i = 0; i < 3; i++)
1355 int region_address1, region_address2;
1358 for (i = 0; i < 3; i++)
1364 region_address1, region_address2);
1393 #if !UNCHECKED_BITSTREAM_READER
1394 s->
gb.size_in_bits_plus8 += extrasize * 8;
1397 for (gr = 0; gr < nb_granules && (s->
last_buf_size >> 3) < main_data_begin; gr++) {
1398 for (ch = 0; ch < s->nb_channels; ch++) {
1416 for (; gr < nb_granules; gr++) {
1417 for (ch = 0; ch < s->nb_channels; ch++) {
1423 int slen, slen1, slen2;
1428 av_dlog(s->
avctx,
"slen1=%d slen2=%d\n", slen1, slen2);
1433 for (i = 0; i < n; i++)
1436 for (i = 0; i < n; i++)
1440 for (i = 0; i < 18; i++)
1442 for (i = 0; i < 3; i++)
1445 for (i = 0; i < 21; i++)
1451 for (k = 0; k < 4; k++) {
1453 if ((g->
scfsi & (0x8 >> k)) == 0) {
1454 slen = (k < 2) ? slen1 : slen2;
1456 for (i = 0; i < n; i++)
1459 for (i = 0; i < n; i++)
1464 for (i = 0; i < n; i++) {
1473 int tindex, tindex2, slen[4], sl, sf;
1488 }
else if (sf < 244) {
1500 }
else if (sf < 500) {
1511 for (k = 0; k < 4; k++) {
1515 for (i = 0; i < n; i++)
1518 for (i = 0; i < n; i++)
1533 if (s->nb_channels == 2)
1536 for (ch = 0; ch < s->nb_channels; ch++) {
1546 return nb_granules * 18;
1550 const uint8_t *buf,
int buf_size)
1552 int i, nb_frames, ch, ret;
1558 if (s->error_protection)
1600 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1616 for (ch = 0; ch < s->nb_channels; ch++) {
1617 samples_ptr = samples + ch;
1618 for (i = 0; i < nb_frames; i++) {
1619 RENAME(ff_mpa_synth_filter)(
1623 samples_ptr, s->nb_channels,
1625 samples_ptr += 32 * s->nb_channels;
1629 return nb_frames * 32 *
sizeof(
OUT_INT) * s->nb_channels;
1635 const uint8_t *buf = avpkt->
data;
1636 int buf_size = avpkt->
size;
1660 avctx->
sub_id = s->layer;
1662 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1665 }
else if (s->frame_size < buf_size) {
1667 buf_size= s->frame_size;
1698 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1700 int *got_frame_ptr,
AVPacket *avpkt)
1702 const uint8_t *buf = avpkt->
data;
1703 int buf_size = avpkt->
size;
1706 int len, out_size, ret = 0;
1721 header =
AV_RB32(buf) | 0xffe00000;
1734 avctx->
sub_id = s->layer;
1736 s->frame_size =
len;
1738 #if FF_API_PARSE_FRAME
1739 if (avctx->parse_only)
1740 out_size = buf_size;
1756 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1761 typedef struct MP3On4DecodeContext {
1765 const uint8_t *coff;
1768 } MP3On4DecodeContext;
1775 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1778 static const uint8_t chan_offset[8][5] = {
1790 static const int16_t chan_layout[8] = {
1803 MP3On4DecodeContext *s = avctx->
priv_data;
1806 for (i = 0; i < s->frames; i++)
1817 MP3On4DecodeContext *s = avctx->
priv_data;
1838 s->syncword = 0xffe00000;
1840 s->syncword = 0xfff00000;
1849 if (!s->mp3decctx[0])
1857 s->mp3decctx[0]->adu_mode = 1;
1862 for (i = 1; i < s->frames; i++) {
1864 if (!s->mp3decctx[i])
1866 s->mp3decctx[i]->adu_mode = 1;
1867 s->mp3decctx[i]->avctx = avctx;
1868 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1872 if (s->frames > 1) {
1874 sizeof(*s->decoded_buf));
1875 if (!s->decoded_buf)
1881 decode_close_mp3on4(avctx);
1889 MP3On4DecodeContext *s = avctx->
priv_data;
1891 for (i = 0; i < s->frames; i++) {
1899 static int decode_frame_mp3on4(
AVCodecContext *avctx,
void *data,
1900 int *got_frame_ptr,
AVPacket *avpkt)
1902 const uint8_t *buf = avpkt->
data;
1903 int buf_size = avpkt->
size;
1904 MP3On4DecodeContext *s = avctx->
priv_data;
1906 int fsize, len = buf_size, out_size = 0;
1910 int fr, j, n, ch, ret;
1918 out_samples = (
OUT_INT *)s->frame->data[0];
1925 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1930 for (fr = 0; fr < s->frames; fr++) {
1933 m = s->mp3decctx[fr];
1940 header = (
AV_RB32(buf) & 0x000fffff) | s->syncword;
1947 if (ch + m->nb_channels > avctx->
channels ||
1948 s->coff[fr] + m->nb_channels > avctx->
channels) {
1953 ch += m->nb_channels;
1962 if (s->frames > 1) {
1965 bp = out_samples + s->coff[fr];
1966 if (m->nb_channels == 1) {
1967 for (j = 0; j < n; j++) {
1968 *bp = s->decoded_buf[j];
1972 for (j = 0; j < n; j++) {
1973 bp[0] = s->decoded_buf[j++];
1974 bp[1] = s->decoded_buf[j];
1983 avctx->
sample_rate = s->mp3decctx[0]->sample_rate;
1985 s->frame->nb_samples = out_size / (avctx->
channels *
sizeof(
OUT_INT));
1994 #if CONFIG_MP1_DECODER
2011 #if CONFIG_MP2_DECODER
2028 #if CONFIG_MP3_DECODER
2045 #if CONFIG_MP3ADU_DECODER
2052 .
decode = decode_frame_adu,
2062 #if CONFIG_MP3ON4_DECODER
2067 .priv_data_size =
sizeof(MP3On4DecodeContext),
2068 .
init = decode_init_mp3on4,
2069 .
close = decode_close_mp3on4,
2070 .
decode = decode_frame_mp3on4,
2072 .
flush = flush_mp3on4,