3
3
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4
4
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
6
* This file is part of FFmpeg.
8
* FFmpeg is free software; you can redistribute it and/or
7
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
8
* Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net>
10
* This file is part of Libav.
12
* Libav is free software; you can redistribute it and/or
9
13
* modify it under the terms of the GNU Lesser General Public
10
14
* License as published by the Free Software Foundation; either
11
15
* version 2.1 of the License, or (at your option) any later version.
13
* FFmpeg is distributed in the hope that it will be useful,
17
* Libav is distributed in the hope that it will be useful,
14
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
20
* Lesser General Public License for more details.
18
22
* You should have received a copy of the GNU Lesser General Public
19
* License along with FFmpeg; if not, write to the Free Software
23
* License along with Libav; if not, write to the Free Software
20
24
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
414
408
skip_bits(gb, 14); // coreCoderDelay
415
409
extension_flag = get_bits1(gb);
417
if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
418
ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
411
if (m4ac->object_type == AOT_AAC_SCALABLE ||
412
m4ac->object_type == AOT_ER_AAC_SCALABLE)
419
413
skip_bits(gb, 3); // layerNr
421
415
memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
422
416
if (channel_config == 0) {
423
417
skip_bits(gb, 4); // element_instance_tag
424
if ((ret = decode_pce(ac, new_che_pos, gb)))
418
if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
427
if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
421
if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
430
if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
424
if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
433
427
if (extension_flag) {
434
switch (ac->m4ac.object_type) {
428
switch (m4ac->object_type) {
435
429
case AOT_ER_BSAC:
436
430
skip_bits(gb, 5); // numOfSubFrame
437
431
skip_bits(gb, 11); // layer_length
455
449
* Decode audio specific configuration; reference: table 1.13.
451
* @param ac pointer to AACContext, may be null
452
* @param avctx pointer to AVCCodecContext, used for logging
453
* @param m4ac pointer to MPEG4AudioConfig, used for parsing
457
454
* @param data pointer to AVCodecContext extradata
458
455
* @param data_size size of AVCCodecContext extradata
460
* @return Returns error status. 0 - OK, !0 - error
457
* @return Returns error status or number of consumed bits. <0 - error
462
static int decode_audio_specific_config(AACContext *ac, void *data,
459
static int decode_audio_specific_config(AACContext *ac,
460
AVCodecContext *avctx,
461
MPEG4AudioConfig *m4ac,
462
const uint8_t *data, int data_size)
465
464
GetBitContext gb;
468
467
init_get_bits(&gb, data, data_size * 8);
470
if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
469
if ((i = ff_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
472
if (ac->m4ac.sampling_index > 12) {
473
av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
471
if (m4ac->sampling_index > 12) {
472
av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
476
if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1)
475
if (m4ac->sbr == 1 && m4ac->ps == -1)
479
478
skip_bits_long(&gb, i);
481
switch (ac->m4ac.object_type) {
480
switch (m4ac->object_type) {
482
481
case AOT_AAC_MAIN:
484
if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
484
if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
488
av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
489
ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
488
av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
489
m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
493
return get_bits_count(&gb);
1728
1708
start += w * 128;
1731
for (m = 0; m < size; m++, start += inc)
1732
for (i = 1; i <= FFMIN(m, order); i++)
1733
coef[start] -= coef[start - i * inc] * lpc[i - 1];
1712
for (m = 0; m < size; m++, start += inc)
1713
for (i = 1; i <= FFMIN(m, order); i++)
1714
coef[start] -= coef[start - i * inc] * lpc[i - 1];
1717
for (m = 0; m < size; m++, start += inc) {
1718
tmp[0] = coef[start];
1719
for (i = 1; i <= FFMIN(m, order); i++)
1720
coef[start] += tmp[i] * lpc[i - 1];
1721
for (i = order; i > 0; i--)
1722
tmp[i] = tmp[i - 1];
1730
* Apply windowing and MDCT to obtain the spectral
1731
* coefficient from the predicted sample by LTP.
1733
static void windowing_and_mdct_ltp(AACContext *ac, float *out,
1734
float *in, IndividualChannelStream *ics)
1736
const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1737
const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
1738
const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1739
const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
1741
if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
1742
ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
1744
memset(in, 0, 448 * sizeof(float));
1745
ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
1746
memcpy(in + 576, in + 576, 448 * sizeof(float));
1748
if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
1749
ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
1751
memcpy(in + 1024, in + 1024, 448 * sizeof(float));
1752
ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
1753
memset(in + 1024 + 576, 0, 448 * sizeof(float));
1755
ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
1759
* Apply the long term prediction
1761
static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
1763
const LongTermPrediction *ltp = &sce->ics.ltp;
1764
const uint16_t *offsets = sce->ics.swb_offset;
1767
if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1768
float *predTime = sce->ret;
1769
float *predFreq = ac->buf_mdct;
1770
int16_t num_samples = 2048;
1772
if (ltp->lag < 1024)
1773
num_samples = ltp->lag + 1024;
1774
for (i = 0; i < num_samples; i++)
1775
predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
1776
memset(&predTime[i], 0, (2048 - i) * sizeof(float));
1778
windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
1780
if (sce->tns.present)
1781
apply_tns(predFreq, &sce->tns, &sce->ics, 0);
1783
for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
1785
for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
1786
sce->coeffs[i] += predFreq[i];
1791
* Update the LTP buffer for next frame
1793
static void update_ltp(AACContext *ac, SingleChannelElement *sce)
1795
IndividualChannelStream *ics = &sce->ics;
1796
float *saved = sce->saved;
1797
float *saved_ltp = sce->coeffs;
1798
const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1799
const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
1802
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1803
memcpy(saved_ltp, saved, 512 * sizeof(float));
1804
memset(saved_ltp + 576, 0, 448 * sizeof(float));
1805
ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
1806
for (i = 0; i < 64; i++)
1807
saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
1808
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
1809
memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
1810
memset(saved_ltp + 576, 0, 448 * sizeof(float));
1811
ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
1812
for (i = 0; i < 64; i++)
1813
saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
1814
} else { // LONG_STOP or ONLY_LONG
1815
ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
1816
for (i = 0; i < 512; i++)
1817
saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
1820
memcpy(sce->ltp_state, &sce->ltp_state[1024], 1024 * sizeof(int16_t));
1821
ac->fmt_conv.float_to_int16(&(sce->ltp_state[1024]), sce->ret, 1024);
1822
ac->fmt_conv.float_to_int16(&(sce->ltp_state[2048]), saved_ltp, 1024);
1739
1826
* Conduct IMDCT and windowing.
1741
static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
1828
static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
1743
1830
IndividualChannelStream *ics = &sce->ics;
1744
1831
float *in = sce->coeffs;
1771
1854
if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
1772
1855
(ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
1773
ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, bias, 512);
1856
ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
1775
for (i = 0; i < 448; i++)
1776
out[i] = saved[i] + bias;
1858
memcpy( out, saved, 448 * sizeof(float));
1778
1860
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1779
ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, bias, 64);
1780
ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, bias, 64);
1781
ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, bias, 64);
1782
ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, bias, 64);
1783
ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, bias, 64);
1861
ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
1862
ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
1863
ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
1864
ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
1865
ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
1784
1866
memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
1786
ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, bias, 64);
1787
for (i = 576; i < 1024; i++)
1788
out[i] = buf[i-512] + bias;
1868
ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
1869
memcpy( out + 576, buf + 64, 448 * sizeof(float));
1792
1873
// buffer update
1793
1874
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1794
for (i = 0; i < 64; i++)
1795
saved[i] = temp[64 + i] - bias;
1796
ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
1797
ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
1798
ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
1875
memcpy( saved, temp + 64, 64 * sizeof(float));
1876
ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
1877
ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
1878
ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
1799
1879
memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
1800
1880
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
1801
1881
memcpy( saved, buf + 512, 448 * sizeof(float));
2014
2096
switch (elem_type) {
2017
err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
2099
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2021
err = decode_cpe(ac, &gb, che);
2103
err = decode_cpe(ac, gb, che);
2025
err = decode_cce(ac, &gb, che);
2107
err = decode_cce(ac, gb, che);
2029
err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
2111
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2033
err = skip_data_stream_element(ac, &gb);
2115
err = skip_data_stream_element(ac, gb);
2036
2118
case TYPE_PCE: {
2037
2119
enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
2038
2120
memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
2039
if ((err = decode_pce(ac, new_che_pos, &gb)))
2121
if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
2041
2123
if (ac->output_configured > OC_TRIAL_PCE)
2042
2124
av_log(avctx, AV_LOG_ERROR,
2123
2223
ff_mdct_end(&ac->mdct);
2124
2224
ff_mdct_end(&ac->mdct_small);
2128
AVCodec aac_decoder = {
2225
ff_mdct_end(&ac->mdct_ltp);
2230
#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
2232
struct LATMContext {
2233
AACContext aac_ctx; ///< containing AACContext
2234
int initialized; ///< initilized after a valid extradata was seen
2237
int audio_mux_version_A; ///< LATM syntax version
2238
int frame_length_type; ///< 0/1 variable/fixed frame length
2239
int frame_length; ///< frame length for fixed frame length
2242
static inline uint32_t latm_get_value(GetBitContext *b)
2244
int length = get_bits(b, 2);
2246
return get_bits_long(b, (length+1)*8);
2249
static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
2252
AVCodecContext *avctx = latmctx->aac_ctx.avctx;
2253
MPEG4AudioConfig m4ac;
2254
int config_start_bit = get_bits_count(gb);
2255
int bits_consumed, esize;
2257
if (config_start_bit % 8) {
2258
av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
2259
"config not byte aligned.\n", 1);
2260
return AVERROR_INVALIDDATA;
2263
decode_audio_specific_config(NULL, avctx, &m4ac,
2264
gb->buffer + (config_start_bit / 8),
2265
get_bits_left(gb) / 8);
2267
if (bits_consumed < 0)
2268
return AVERROR_INVALIDDATA;
2270
esize = (bits_consumed+7) / 8;
2272
if (avctx->extradata_size <= esize) {
2273
av_free(avctx->extradata);
2274
avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
2275
if (!avctx->extradata)
2276
return AVERROR(ENOMEM);
2279
avctx->extradata_size = esize;
2280
memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
2281
memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2283
skip_bits_long(gb, bits_consumed);
2286
return bits_consumed;
2289
static int read_stream_mux_config(struct LATMContext *latmctx,
2292
int ret, audio_mux_version = get_bits(gb, 1);
2294
latmctx->audio_mux_version_A = 0;
2295
if (audio_mux_version)
2296
latmctx->audio_mux_version_A = get_bits(gb, 1);
2298
if (!latmctx->audio_mux_version_A) {
2300
if (audio_mux_version)
2301
latm_get_value(gb); // taraFullness
2303
skip_bits(gb, 1); // allStreamSameTimeFraming
2304
skip_bits(gb, 6); // numSubFrames
2306
if (get_bits(gb, 4)) { // numPrograms
2307
av_log_missing_feature(latmctx->aac_ctx.avctx,
2308
"multiple programs are not supported\n", 1);
2309
return AVERROR_PATCHWELCOME;
2312
// for each program (which there is only on in DVB)
2314
// for each layer (which there is only on in DVB)
2315
if (get_bits(gb, 3)) { // numLayer
2316
av_log_missing_feature(latmctx->aac_ctx.avctx,
2317
"multiple layers are not supported\n", 1);
2318
return AVERROR_PATCHWELCOME;
2321
// for all but first stream: use_same_config = get_bits(gb, 1);
2322
if (!audio_mux_version) {
2323
if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
2326
int ascLen = latm_get_value(gb);
2327
if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
2330
skip_bits_long(gb, ascLen);
2333
latmctx->frame_length_type = get_bits(gb, 3);
2334
switch (latmctx->frame_length_type) {
2336
skip_bits(gb, 8); // latmBufferFullness
2339
latmctx->frame_length = get_bits(gb, 9);
2344
skip_bits(gb, 6); // CELP frame length table index
2348
skip_bits(gb, 1); // HVXC frame length table index
2352
if (get_bits(gb, 1)) { // other data
2353
if (audio_mux_version) {
2354
latm_get_value(gb); // other_data_bits
2358
esc = get_bits(gb, 1);
2364
if (get_bits(gb, 1)) // crc present
2365
skip_bits(gb, 8); // config_crc
2371
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
2375
if (ctx->frame_length_type == 0) {
2376
int mux_slot_length = 0;
2378
tmp = get_bits(gb, 8);
2379
mux_slot_length += tmp;
2380
} while (tmp == 255);
2381
return mux_slot_length;
2382
} else if (ctx->frame_length_type == 1) {
2383
return ctx->frame_length;
2384
} else if (ctx->frame_length_type == 3 ||
2385
ctx->frame_length_type == 5 ||
2386
ctx->frame_length_type == 7) {
2387
skip_bits(gb, 2); // mux_slot_length_coded
2392
static int read_audio_mux_element(struct LATMContext *latmctx,
2396
uint8_t use_same_mux = get_bits(gb, 1);
2397
if (!use_same_mux) {
2398
if ((err = read_stream_mux_config(latmctx, gb)) < 0)
2400
} else if (!latmctx->aac_ctx.avctx->extradata) {
2401
av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
2402
"no decoder config found\n");
2403
return AVERROR(EAGAIN);
2405
if (latmctx->audio_mux_version_A == 0) {
2406
int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
2407
if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
2408
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
2409
return AVERROR_INVALIDDATA;
2410
} else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
2411
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2412
"frame length mismatch %d << %d\n",
2413
mux_slot_length_bytes * 8, get_bits_left(gb));
2414
return AVERROR_INVALIDDATA;
2421
static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
2424
struct LATMContext *latmctx = avctx->priv_data;
2428
if (avpkt->size == 0)
2431
init_get_bits(&gb, avpkt->data, avpkt->size * 8);
2433
// check for LOAS sync word
2434
if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
2435
return AVERROR_INVALIDDATA;
2437
muxlength = get_bits(&gb, 13) + 3;
2438
// not enough data, the parser should have sorted this
2439
if (muxlength > avpkt->size)
2440
return AVERROR_INVALIDDATA;
2442
if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
2445
if (!latmctx->initialized) {
2446
if (!avctx->extradata) {
2450
if ((err = aac_decode_init(avctx)) < 0)
2452
latmctx->initialized = 1;
2456
if (show_bits(&gb, 12) == 0xfff) {
2457
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2458
"ADTS header detected, probably as result of configuration "
2460
return AVERROR_INVALIDDATA;
2463
if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0)
2469
av_cold static int latm_decode_init(AVCodecContext *avctx)
2471
struct LATMContext *latmctx = avctx->priv_data;
2474
ret = aac_decode_init(avctx);
2476
if (avctx->extradata_size > 0) {
2477
latmctx->initialized = !ret;
2479
latmctx->initialized = 0;
2486
AVCodec ff_aac_decoder = {
2130
2488
AVMEDIA_TYPE_AUDIO,