91
94
# define MULH3(x, y, s) ((s)*(y)*(x))
92
95
# define MULLx(x, y, s) ((y)*(x))
93
96
# define RENAME(a) a ## _float
94
# define OUT_FMT AV_SAMPLE_FMT_FLT
97
# define OUT_FMT AV_SAMPLE_FMT_FLT
98
# define OUT_FMT_P AV_SAMPLE_FMT_FLTP
96
100
# define SHR(a,b) ((a)>>(b))
97
101
/* WARNING: only correct for positive numbers */
101
105
# define MULH3(x, y, s) MULH((s)*(x), y)
102
106
# define MULLx(x, y, s) MULL(x,y,s)
103
107
# define RENAME(a) a ## _fixed
104
# define OUT_FMT AV_SAMPLE_FMT_S16
108
# define OUT_FMT AV_SAMPLE_FMT_S16
109
# define OUT_FMT_P AV_SAMPLE_FMT_S16P
107
112
/****************/
173
178
static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
175
if (g->block_type == 2)
176
g->region_size[0] = (36 / 2);
180
if (g->block_type == 2) {
181
if (s->sample_rate_index != 8)
182
g->region_size[0] = (36 / 2);
184
g->region_size[0] = (72 / 2);
178
186
if (s->sample_rate_index <= 2)
179
187
g->region_size[0] = (36 / 2);
180
188
else if (s->sample_rate_index != 8)
199
207
if (g->block_type == 2) {
200
208
if (g->switch_point) {
201
209
/* if switched mode, we handle the 36 first samples as
202
long blocks. For 8000Hz, we handle the 48 first
203
exponents as long blocks (XXX: check this!) */
210
long blocks. For 8000Hz, we handle the 72 first
211
exponents as long blocks */
204
212
if (s->sample_rate_index <= 2)
206
else if (s->sample_rate_index != 8)
209
g->long_end = 4; /* 8000 Hz */
211
217
g->short_start = 3;
302
308
for (i = 1; i < 16; i++) {
303
309
const HuffTable *h = &mpa_huff_tables[i];
305
uint8_t tmp_bits [512];
306
uint16_t tmp_codes[512];
308
memset(tmp_bits , 0, sizeof(tmp_bits ));
309
memset(tmp_codes, 0, sizeof(tmp_codes));
311
uint8_t tmp_bits [512] = { 0 };
312
uint16_t tmp_codes[512] = { 0 };
311
314
xsize = h->xsize;
432
435
s->avctx = avctx;
434
437
ff_mpadsp_init(&s->mpadsp);
438
ff_dsputil_init(&s->dsp, avctx);
436
avctx->sample_fmt= OUT_FMT;
440
if (avctx->request_sample_fmt == OUT_FMT &&
441
avctx->codec_id != AV_CODEC_ID_MP3ON4)
442
avctx->sample_fmt = OUT_FMT;
444
avctx->sample_fmt = OUT_FMT_P;
437
445
s->err_recognition = avctx->err_recognition;
439
if (avctx->codec_id == CODEC_ID_MP3ADU)
447
if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
442
450
avcodec_get_frame_defaults(&s->frame);
884
892
int pos = get_bits_count(&s->gb);
886
894
if (pos >= end_pos){
887
// av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
888
895
switch_buffer(s, &pos, &end_pos, &end_pos2);
889
// av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
890
896
if (pos >= end_pos)
963
// av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
964
969
switch_buffer(s, &pos, &end_pos, &end_pos2);
965
// av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
966
970
if (pos >= end_pos)
986
990
/* skip extension bits */
987
991
bits_left = end_pos2 - get_bits_count(&s->gb);
988
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
989
992
if (bits_left < 0 && (s->err_recognition & AV_EF_BUFFER)) {
990
993
av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1383
1390
FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
1384
1391
assert((get_bits_count(&s->gb) & 7) == 0);
1385
1392
/* now we get bits from the main_data_begin offset */
1386
av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1387
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1393
av_dlog(s->avctx, "seekback:%d, lastbuf:%d\n",
1394
main_data_begin, s->last_buf_size);
1389
1396
memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1390
1397
s->in_gb = s->gb;
1398
1405
g = &s->granules[ch][gr];
1399
1406
s->last_buf_size += g->part2_3_length;
1400
1407
memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1408
compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1403
1411
skip = s->last_buf_size - 8 * main_data_begin;
1545
1553
return nb_granules * 18;
1548
static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1556
static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
1549
1557
const uint8_t *buf, int buf_size)
1551
1559
int i, nb_frames, ch, ret;
1604
1612
/* get output buffer */
1605
1613
if (!samples) {
1606
1614
s->frame.nb_samples = s->avctx->frame_size;
1607
if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1615
if ((ret = ff_get_buffer(s->avctx, &s->frame)) < 0) {
1608
1616
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1611
samples = (OUT_INT *)s->frame.data[0];
1619
samples = (OUT_INT **)s->frame.extended_data;
1614
1622
/* apply the synthesis filter */
1615
1623
for (ch = 0; ch < s->nb_channels; ch++) {
1616
samples_ptr = samples + ch;
1625
if (s->avctx->sample_fmt == OUT_FMT_P) {
1626
samples_ptr = samples[ch];
1629
samples_ptr = samples[0] + ch;
1630
sample_stride = s->nb_channels;
1617
1632
for (i = 0; i < nb_frames; i++) {
1618
RENAME(ff_mpa_synth_filter)(
1620
s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1621
RENAME(ff_mpa_synth_window), &s->dither_state,
1622
samples_ptr, s->nb_channels,
1623
s->sb_samples[ch][i]);
1624
samples_ptr += 32 * s->nb_channels;
1633
RENAME(ff_mpa_synth_filter)(&s->mpadsp, s->synth_buf[ch],
1634
&(s->synth_buf_offset[ch]),
1635
RENAME(ff_mpa_synth_window),
1636
&s->dither_state, samples_ptr,
1637
sample_stride, s->sb_samples[ch][i]);
1638
samples_ptr += 32 * sample_stride;
1656
1670
avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1657
1671
if (!avctx->bit_rate)
1658
1672
avctx->bit_rate = s->bit_rate;
1659
avctx->sub_id = s->layer;
1661
1674
if (s->frame_size <= 0 || s->frame_size > buf_size) {
1662
1675
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1663
1676
return AVERROR_INVALIDDATA;
1664
1677
} else if (s->frame_size < buf_size) {
1665
av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
1666
1678
buf_size= s->frame_size;
1687
1699
return buf_size;
1702
static void mp_flush(MPADecodeContext *ctx)
1704
memset(ctx->synth_buf, 0, sizeof(ctx->synth_buf));
1705
ctx->last_buf_size = 0;
1690
1708
static void flush(AVCodecContext *avctx)
1692
MPADecodeContext *s = avctx->priv_data;
1693
memset(s->synth_buf, 0, sizeof(s->synth_buf));
1694
s->last_buf_size = 0;
1710
mp_flush(avctx->priv_data);
1697
1713
#if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1730
1746
avctx->channels = s->nb_channels;
1731
1747
if (!avctx->bit_rate)
1732
1748
avctx->bit_rate = s->bit_rate;
1733
avctx->sub_id = s->layer;
1735
1750
s->frame_size = len;
1737
#if FF_API_PARSE_FRAME
1738
if (avctx->parse_only)
1739
out_size = buf_size;
1742
1752
ret = mp_decode_frame(s, NULL, buf, buf_size);
1744
1754
av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1904
1900
MPADecodeContext *m;
1905
1901
int fsize, len = buf_size, out_size = 0;
1906
1902
uint32_t header;
1907
OUT_INT *out_samples;
1908
OUT_INT *outptr, *bp;
1909
int fr, j, n, ch, ret;
1903
OUT_INT **out_samples;
1911
1907
/* get output buffer */
1912
1908
s->frame->nb_samples = MPA_FRAME_SIZE;
1913
if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1909
if ((ret = ff_get_buffer(avctx, s->frame)) < 0) {
1914
1910
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1917
out_samples = (OUT_INT *)s->frame->data[0];
1913
out_samples = (OUT_INT **)s->frame->extended_data;
1919
1915
// Discard too short frames
1920
1916
if (buf_size < HEADER_SIZE)
1921
1917
return AVERROR_INVALIDDATA;
1923
// If only one decoder interleave is not needed
1924
outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1926
1919
avctx->bit_rate = 0;
1960
if (s->frames > 1) {
1961
n = m->avctx->frame_size*m->nb_channels;
1962
/* interleave output data */
1963
bp = out_samples + s->coff[fr];
1964
if (m->nb_channels == 1) {
1965
for (j = 0; j < n; j++) {
1966
*bp = s->decoded_buf[j];
1967
bp += avctx->channels;
1970
for (j = 0; j < n; j++) {
1971
bp[0] = s->decoded_buf[j++];
1972
bp[1] = s->decoded_buf[j];
1973
bp += avctx->channels;
1977
1957
avctx->bit_rate += m->bit_rate;
1993
1973
AVCodec ff_mp1_decoder = {
1995
1975
.type = AVMEDIA_TYPE_AUDIO,
1976
.id = AV_CODEC_ID_MP1,
1997
1977
.priv_data_size = sizeof(MPADecodeContext),
1998
1978
.init = decode_init,
1999
1979
.decode = decode_frame,
2000
#if FF_API_PARSE_FRAME
2001
.capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2003
1980
.capabilities = CODEC_CAP_DR1,
2005
1981
.flush = flush,
2006
1982
.long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
1983
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1985
AV_SAMPLE_FMT_NONE },
2009
1988
#if CONFIG_MP2_DECODER
2010
1989
AVCodec ff_mp2_decoder = {
2012
1991
.type = AVMEDIA_TYPE_AUDIO,
1992
.id = AV_CODEC_ID_MP2,
2014
1993
.priv_data_size = sizeof(MPADecodeContext),
2015
1994
.init = decode_init,
2016
1995
.decode = decode_frame,
2017
#if FF_API_PARSE_FRAME
2018
.capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2020
1996
.capabilities = CODEC_CAP_DR1,
2022
1997
.flush = flush,
2023
1998
.long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
1999
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
2001
AV_SAMPLE_FMT_NONE },
2026
2004
#if CONFIG_MP3_DECODER
2027
2005
AVCodec ff_mp3_decoder = {
2029
2007
.type = AVMEDIA_TYPE_AUDIO,
2008
.id = AV_CODEC_ID_MP3,
2031
2009
.priv_data_size = sizeof(MPADecodeContext),
2032
2010
.init = decode_init,
2033
2011
.decode = decode_frame,
2034
#if FF_API_PARSE_FRAME
2035
.capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2037
2012
.capabilities = CODEC_CAP_DR1,
2039
2013
.flush = flush,
2040
2014
.long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2015
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
2017
AV_SAMPLE_FMT_NONE },
2043
2020
#if CONFIG_MP3ADU_DECODER
2044
2021
AVCodec ff_mp3adu_decoder = {
2045
2022
.name = "mp3adu",
2046
2023
.type = AVMEDIA_TYPE_AUDIO,
2047
.id = CODEC_ID_MP3ADU,
2024
.id = AV_CODEC_ID_MP3ADU,
2048
2025
.priv_data_size = sizeof(MPADecodeContext),
2049
2026
.init = decode_init,
2050
2027
.decode = decode_frame_adu,
2051
#if FF_API_PARSE_FRAME
2052
.capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2054
2028
.capabilities = CODEC_CAP_DR1,
2056
2029
.flush = flush,
2057
2030
.long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2031
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
2033
AV_SAMPLE_FMT_NONE },
2060
2036
#if CONFIG_MP3ON4_DECODER
2061
2037
AVCodec ff_mp3on4_decoder = {
2062
2038
.name = "mp3on4",
2063
2039
.type = AVMEDIA_TYPE_AUDIO,
2064
.id = CODEC_ID_MP3ON4,
2040
.id = AV_CODEC_ID_MP3ON4,
2065
2041
.priv_data_size = sizeof(MP3On4DecodeContext),
2066
2042
.init = decode_init_mp3on4,
2067
2043
.close = decode_close_mp3on4,