3 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5 * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
8 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
11 * This file is part of FFmpeg.
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 * @author Oded Shimon ( ods15 ods15 dyndns org )
32 * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
36 #define FFT_FIXED_32 0
39 #include "libavutil/float_dsp.h"
40 #include "libavutil/opt.h"
52 #include "aacdectab.h"
53 #include "adts_header.h"
54 #include "cbrt_data.h"
57 #include "mpeg4audio.h"
59 #include "libavutil/intfloat.h"
69 # include "mips/aacdec_mips.h"
72 static av_always_inline void reset_predict_state(PredictorState *ps)
83 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
87 *dst++ = v[idx & 15] * s;
88 *dst++ = v[idx>>4 & 15] * s;
94 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
98 *dst++ = v[idx & 3] * s;
99 *dst++ = v[idx>>2 & 3] * s;
100 *dst++ = v[idx>>4 & 3] * s;
101 *dst++ = v[idx>>6 & 3] * s;
107 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
108 unsigned sign, const float *scale)
110 union av_intfloat32 s0, s1;
112 s0.f = s1.f = *scale;
113 s0.i ^= sign >> 1 << 31;
116 *dst++ = v[idx & 15] * s0.f;
117 *dst++ = v[idx>>4 & 15] * s1.f;
124 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
125 unsigned sign, const float *scale)
127 unsigned nz = idx >> 12;
128 union av_intfloat32 s = { .f = *scale };
129 union av_intfloat32 t;
131 t.i = s.i ^ (sign & 1U<<31);
132 *dst++ = v[idx & 3] * t.f;
134 sign <<= nz & 1; nz >>= 1;
135 t.i = s.i ^ (sign & 1U<<31);
136 *dst++ = v[idx>>2 & 3] * t.f;
138 sign <<= nz & 1; nz >>= 1;
139 t.i = s.i ^ (sign & 1U<<31);
140 *dst++ = v[idx>>4 & 3] * t.f;
143 t.i = s.i ^ (sign & 1U<<31);
144 *dst++ = v[idx>>6 & 3] * t.f;
150 static av_always_inline float flt16_round(float pf)
152 union av_intfloat32 tmp;
154 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
158 static av_always_inline float flt16_even(float pf)
160 union av_intfloat32 tmp;
162 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
166 static av_always_inline float flt16_trunc(float pf)
168 union av_intfloat32 pun;
170 pun.i &= 0xFFFF0000U;
174 static av_always_inline void predict(PredictorState *ps, float *coef,
177 const float a = 0.953125; // 61.0 / 64
178 const float alpha = 0.90625; // 29.0 / 32
182 float r0 = ps->r0, r1 = ps->r1;
183 float cor0 = ps->cor0, cor1 = ps->cor1;
184 float var0 = ps->var0, var1 = ps->var1;
186 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
187 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
189 pv = flt16_round(k1 * r0 + k2 * r1);
196 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
197 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
198 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
199 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
201 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
202 ps->r0 = flt16_trunc(a * e0);
206 * Apply dependent channel coupling (applied before IMDCT).
208 * @param index index into coupling gain array
210 static void apply_dependent_coupling(AACContext *ac,
211 SingleChannelElement *target,
212 ChannelElement *cce, int index)
214 IndividualChannelStream *ics = &cce->ch[0].ics;
215 const uint16_t *offsets = ics->swb_offset;
216 float *dest = target->coeffs;
217 const float *src = cce->ch[0].coeffs;
218 int g, i, group, k, idx = 0;
219 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
220 av_log(ac->avctx, AV_LOG_ERROR,
221 "Dependent coupling is not supported together with LTP\n");
224 for (g = 0; g < ics->num_window_groups; g++) {
225 for (i = 0; i < ics->max_sfb; i++, idx++) {
226 if (cce->ch[0].band_type[idx] != ZERO_BT) {
227 const float gain = cce->coup.gain[index][idx];
228 for (group = 0; group < ics->group_len[g]; group++) {
229 for (k = offsets[i]; k < offsets[i + 1]; k++) {
231 dest[group * 128 + k] += gain * src[group * 128 + k];
236 dest += ics->group_len[g] * 128;
237 src += ics->group_len[g] * 128;
242 * Apply independent channel coupling (applied after IMDCT).
244 * @param index index into coupling gain array
246 static void apply_independent_coupling(AACContext *ac,
247 SingleChannelElement *target,
248 ChannelElement *cce, int index)
250 const float gain = cce->coup.gain[index][0];
251 const float *src = cce->ch[0].ret;
252 float *dest = target->ret;
253 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
255 ac->fdsp->vector_fmac_scalar(dest, src, gain, len);
258 #include "aacdec_template.c"
260 #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
263 AACContext aac_ctx; ///< containing AACContext
264 int initialized; ///< initialized after a valid extradata was seen
267 int audio_mux_version_A; ///< LATM syntax version
268 int frame_length_type; ///< 0/1 variable/fixed frame length
269 int frame_length; ///< frame length for fixed frame length
272 static inline uint32_t latm_get_value(GetBitContext *b)
274 int length = get_bits(b, 2);
276 return get_bits_long(b, (length+1)*8);
279 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
280 GetBitContext *gb, int asclen)
282 AACContext *ac = &latmctx->aac_ctx;
283 AVCodecContext *avctx = ac->avctx;
284 MPEG4AudioConfig m4ac = { 0 };
286 int config_start_bit = get_bits_count(gb);
287 int sync_extension = 0;
288 int bits_consumed, esize, i;
292 asclen = FFMIN(asclen, get_bits_left(gb));
293 init_get_bits(&gbc, gb->buffer, config_start_bit + asclen);
294 skip_bits_long(&gbc, config_start_bit);
295 } else if (asclen == 0) {
298 return AVERROR_INVALIDDATA;
301 if (get_bits_left(gb) <= 0)
302 return AVERROR_INVALIDDATA;
304 bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &m4ac,
305 &gbc, config_start_bit,
308 if (bits_consumed < config_start_bit)
309 return AVERROR_INVALIDDATA;
310 bits_consumed -= config_start_bit;
313 asclen = bits_consumed;
315 if (!latmctx->initialized ||
316 ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
317 ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
319 if (latmctx->initialized) {
320 av_log(avctx, AV_LOG_INFO, "audio config changed (sample_rate=%d, chan_config=%d)\n", m4ac.sample_rate, m4ac.chan_config);
322 av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
324 latmctx->initialized = 0;
326 esize = (asclen + 7) / 8;
328 if (avctx->extradata_size < esize) {
329 av_free(avctx->extradata);
330 avctx->extradata = av_malloc(esize + AV_INPUT_BUFFER_PADDING_SIZE);
331 if (!avctx->extradata)
332 return AVERROR(ENOMEM);
335 avctx->extradata_size = esize;
337 for (i = 0; i < esize; i++) {
338 avctx->extradata[i] = get_bits(&gbc, 8);
340 memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
342 skip_bits_long(gb, asclen);
347 static int read_stream_mux_config(struct LATMContext *latmctx,
350 int ret, audio_mux_version = get_bits(gb, 1);
352 latmctx->audio_mux_version_A = 0;
353 if (audio_mux_version)
354 latmctx->audio_mux_version_A = get_bits(gb, 1);
356 if (!latmctx->audio_mux_version_A) {
358 if (audio_mux_version)
359 latm_get_value(gb); // taraFullness
361 skip_bits(gb, 1); // allStreamSameTimeFraming
362 skip_bits(gb, 6); // numSubFrames
364 if (get_bits(gb, 4)) { // numPrograms
365 avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
366 return AVERROR_PATCHWELCOME;
369 // for each program (which there is only one in DVB)
371 // for each layer (which there is only one in DVB)
372 if (get_bits(gb, 3)) { // numLayer
373 avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
374 return AVERROR_PATCHWELCOME;
377 // for all but first stream: use_same_config = get_bits(gb, 1);
378 if (!audio_mux_version) {
379 if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
382 int ascLen = latm_get_value(gb);
383 if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
387 latmctx->frame_length_type = get_bits(gb, 3);
388 switch (latmctx->frame_length_type) {
390 skip_bits(gb, 8); // latmBufferFullness
393 latmctx->frame_length = get_bits(gb, 9);
398 skip_bits(gb, 6); // CELP frame length table index
402 skip_bits(gb, 1); // HVXC frame length table index
406 if (get_bits(gb, 1)) { // other data
407 if (audio_mux_version) {
408 latm_get_value(gb); // other_data_bits
412 if (get_bits_left(gb) < 9)
413 return AVERROR_INVALIDDATA;
414 esc = get_bits(gb, 1);
420 if (get_bits(gb, 1)) // crc present
421 skip_bits(gb, 8); // config_crc
427 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
431 if (ctx->frame_length_type == 0) {
432 int mux_slot_length = 0;
434 if (get_bits_left(gb) < 8)
435 return AVERROR_INVALIDDATA;
436 tmp = get_bits(gb, 8);
437 mux_slot_length += tmp;
438 } while (tmp == 255);
439 return mux_slot_length;
440 } else if (ctx->frame_length_type == 1) {
441 return ctx->frame_length;
442 } else if (ctx->frame_length_type == 3 ||
443 ctx->frame_length_type == 5 ||
444 ctx->frame_length_type == 7) {
445 skip_bits(gb, 2); // mux_slot_length_coded
450 static int read_audio_mux_element(struct LATMContext *latmctx,
454 uint8_t use_same_mux = get_bits(gb, 1);
456 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
458 } else if (!latmctx->aac_ctx.avctx->extradata) {
459 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
460 "no decoder config found\n");
463 if (latmctx->audio_mux_version_A == 0) {
464 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
465 if (mux_slot_length_bytes < 0 || mux_slot_length_bytes * 8LL > get_bits_left(gb)) {
466 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
467 return AVERROR_INVALIDDATA;
468 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
469 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
470 "frame length mismatch %d << %d\n",
471 mux_slot_length_bytes * 8, get_bits_left(gb));
472 return AVERROR_INVALIDDATA;
479 static int latm_decode_frame(AVCodecContext *avctx, void *out,
480 int *got_frame_ptr, AVPacket *avpkt)
482 struct LATMContext *latmctx = avctx->priv_data;
486 if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
489 // check for LOAS sync word
490 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
491 return AVERROR_INVALIDDATA;
493 muxlength = get_bits(&gb, 13) + 3;
494 // not enough data, the parser should have sorted this out
495 if (muxlength > avpkt->size)
496 return AVERROR_INVALIDDATA;
498 if ((err = read_audio_mux_element(latmctx, &gb)))
499 return (err < 0) ? err : avpkt->size;
501 if (!latmctx->initialized) {
502 if (!avctx->extradata) {
506 push_output_configuration(&latmctx->aac_ctx);
507 if ((err = decode_audio_specific_config(
508 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
509 avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
510 pop_output_configuration(&latmctx->aac_ctx);
513 latmctx->initialized = 1;
517 if (show_bits(&gb, 12) == 0xfff) {
518 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
519 "ADTS header detected, probably as result of configuration "
521 return AVERROR_INVALIDDATA;
524 switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
529 err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
532 err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
540 static av_cold int latm_decode_init(AVCodecContext *avctx)
542 struct LATMContext *latmctx = avctx->priv_data;
543 int ret = aac_decode_init(avctx);
545 if (avctx->extradata_size > 0)
546 latmctx->initialized = !ret;
551 AVCodec ff_aac_decoder = {
553 .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
554 .type = AVMEDIA_TYPE_AUDIO,
555 .id = AV_CODEC_ID_AAC,
556 .priv_data_size = sizeof(AACContext),
557 .init = aac_decode_init,
558 .close = aac_decode_close,
559 .decode = aac_decode_frame,
560 .sample_fmts = (const enum AVSampleFormat[]) {
561 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
563 .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
564 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
565 .channel_layouts = aac_channel_layout,
567 .priv_class = &aac_decoder_class,
568 .profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
572 Note: This decoder filter is intended to decode LATM streams transferred
573 in MPEG transport streams which only contain one program.
574 To do a more complex LATM demuxing a separate LATM demuxer should be used.
576 AVCodec ff_aac_latm_decoder = {
578 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
579 .type = AVMEDIA_TYPE_AUDIO,
580 .id = AV_CODEC_ID_AAC_LATM,
581 .priv_data_size = sizeof(struct LATMContext),
582 .init = latm_decode_init,
583 .close = aac_decode_close,
584 .decode = latm_decode_frame,
585 .sample_fmts = (const enum AVSampleFormat[]) {
586 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
588 .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
589 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
590 .channel_layouts = aac_channel_layout,
592 .profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),