FFmpeg
Loading...
Searching...
No Matches
aacdec_latm.h
Go to the documentation of this file.
1/*
2 * AAC decoder
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>
6 *
7 * AAC LATM decoder
8 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
10 *
11 * AAC decoder fixed-point implementation
12 * Copyright (c) 2013
13 * MIPS Technologies, Inc., California.
14 *
15 * This file is part of FFmpeg.
16 *
17 * FFmpeg is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * FFmpeg is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with FFmpeg; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32#ifndef AVCODEC_AAC_AACDEC_LATM_H
33#define AVCODEC_AAC_AACDEC_LATM_H
34
35#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
36
38 AACDecContext aac_ctx; ///< containing AACContext
39 int initialized; ///< initialized after a valid extradata was seen
40
41 // parser data
42 int audio_mux_version_A; ///< LATM syntax version
43 int frame_length_type; ///< 0/1 variable/fixed frame length
44 int frame_length; ///< frame length for fixed frame length
45};
46
47static inline uint32_t latm_get_value(GetBitContext *b)
48{
49 int length = get_bits(b, 2);
50
51 return get_bits_long(b, (length+1)*8);
52}
53
55 GetBitContext *gb, int asclen)
56{
57 AACDecContext *ac = &latmctx->aac_ctx;
58 AVCodecContext *avctx = ac->avctx;
59 OutputConfiguration oc = { 0 };
60 MPEG4AudioConfig *m4ac = &oc.m4ac;
61 GetBitContext gbc;
62 int config_start_bit = get_bits_count(gb);
63 int sync_extension = 0;
64 int bits_consumed, esize, i;
65
66 if (asclen > 0) {
67 sync_extension = 1;
68 asclen = FFMIN(asclen, get_bits_left(gb));
69 init_get_bits(&gbc, gb->buffer, config_start_bit + asclen);
70 skip_bits_long(&gbc, config_start_bit);
71 } else if (asclen == 0) {
72 gbc = *gb;
73 } else {
75 }
76
77 if (get_bits_left(gb) <= 0)
79
80 bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &oc,
81 &gbc, config_start_bit,
82 sync_extension);
83
84 if (bits_consumed < config_start_bit)
86 bits_consumed -= config_start_bit;
87
88 if (asclen == 0)
89 asclen = bits_consumed;
90
91 if (!latmctx->initialized ||
92 ac->oc[1].m4ac.sample_rate != m4ac->sample_rate ||
93 ac->oc[1].m4ac.chan_config != m4ac->chan_config) {
94
95 if (latmctx->initialized) {
96 av_log(avctx, AV_LOG_INFO, "audio config changed (sample_rate=%d, chan_config=%d)\n",
97 m4ac->sample_rate, m4ac->chan_config);
98 } else {
99 av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
100 }
101 latmctx->initialized = 0;
102
103 esize = (asclen + 7) / 8;
104
105 if (avctx->extradata_size < esize) {
106 av_free(avctx->extradata);
108 if (!avctx->extradata)
109 return AVERROR(ENOMEM);
110 }
111
112 avctx->extradata_size = esize;
113 gbc = *gb;
114 for (i = 0; i < esize; i++) {
115 avctx->extradata[i] = get_bits(&gbc, 8);
116 }
117 memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
118 }
119 skip_bits_long(gb, asclen);
120
121 return 0;
122}
123
124static int read_stream_mux_config(struct LATMContext *latmctx,
125 GetBitContext *gb)
126{
127 int ret, audio_mux_version = get_bits(gb, 1);
128
129 latmctx->audio_mux_version_A = 0;
130 if (audio_mux_version)
131 latmctx->audio_mux_version_A = get_bits(gb, 1);
132
133 if (!latmctx->audio_mux_version_A) {
134
135 if (audio_mux_version)
136 latm_get_value(gb); // taraFullness
137
138 skip_bits(gb, 1); // allStreamSameTimeFraming
139 skip_bits(gb, 6); // numSubFrames
140 // numPrograms
141 if (get_bits(gb, 4)) { // numPrograms
142 avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
144 }
145
146 // for each program (which there is only one in DVB)
147
148 // for each layer (which there is only one in DVB)
149 if (get_bits(gb, 3)) { // numLayer
150 avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
152 }
153
154 // for all but first stream: use_same_config = get_bits(gb, 1);
155 if (!audio_mux_version) {
156 if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
157 return ret;
158 } else {
159 int ascLen = latm_get_value(gb);
160 if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
161 return ret;
162 }
163
164 latmctx->frame_length_type = get_bits(gb, 3);
165 switch (latmctx->frame_length_type) {
166 case 0:
167 skip_bits(gb, 8); // latmBufferFullness
168 break;
169 case 1:
170 latmctx->frame_length = get_bits(gb, 9);
171 break;
172 case 3:
173 case 4:
174 case 5:
175 skip_bits(gb, 6); // CELP frame length table index
176 break;
177 case 6:
178 case 7:
179 skip_bits(gb, 1); // HVXC frame length table index
180 break;
181 }
182
183 if (get_bits(gb, 1)) { // other data
184 if (audio_mux_version) {
185 latm_get_value(gb); // other_data_bits
186 } else {
187 int esc;
188 do {
189 if (get_bits_left(gb) < 9)
190 return AVERROR_INVALIDDATA;
191 esc = get_bits(gb, 1);
192 skip_bits(gb, 8);
193 } while (esc);
194 }
195 }
196
197 if (get_bits(gb, 1)) // crc present
198 skip_bits(gb, 8); // config_crc
199 }
200
201 return 0;
202}
203
205{
206 uint8_t tmp;
207
208 if (ctx->frame_length_type == 0) {
209 int mux_slot_length = 0;
210 do {
211 if (get_bits_left(gb) < 8)
212 return AVERROR_INVALIDDATA;
213 tmp = get_bits(gb, 8);
214 mux_slot_length += tmp;
215 } while (tmp == 255);
216 return mux_slot_length;
217 } else if (ctx->frame_length_type == 1) {
218 return ctx->frame_length;
219 } else if (ctx->frame_length_type == 3 ||
220 ctx->frame_length_type == 5 ||
221 ctx->frame_length_type == 7) {
222 skip_bits(gb, 2); // mux_slot_length_coded
223 }
224 return 0;
225}
226
227static int read_audio_mux_element(struct LATMContext *latmctx,
228 GetBitContext *gb)
229{
230 int err;
231 uint8_t use_same_mux = get_bits(gb, 1);
232 if (!use_same_mux) {
233 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
234 return err;
235 } else if (!latmctx->aac_ctx.avctx->extradata) {
237 "no decoder config found\n");
238 return 1;
239 }
240 if (latmctx->audio_mux_version_A == 0) {
241 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
242 if (mux_slot_length_bytes < 0 || mux_slot_length_bytes * 8LL > get_bits_left(gb)) {
243 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
244 return AVERROR_INVALIDDATA;
245 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
247 "frame length mismatch %d << %d\n",
248 mux_slot_length_bytes * 8, get_bits_left(gb));
249 return AVERROR_INVALIDDATA;
250 }
251 }
252 return 0;
253}
254
255
257 int *got_frame_ptr, AVPacket *avpkt)
258{
259 struct LATMContext *latmctx = avctx->priv_data;
260 int muxlength, err;
261 GetBitContext gb;
262
263 if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
264 return err;
265
266 // check for LOAS sync word
267 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
268 return AVERROR_INVALIDDATA;
269
270 muxlength = get_bits(&gb, 13) + 3;
271 // not enough data, the parser should have sorted this out
272 if (muxlength > avpkt->size)
273 return AVERROR_INVALIDDATA;
274
275 if ((err = read_audio_mux_element(latmctx, &gb)))
276 return (err < 0) ? err : avpkt->size;
277
278 if (!latmctx->initialized) {
279 if (!avctx->extradata) {
280 *got_frame_ptr = 0;
281 return avpkt->size;
282 } else {
285 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1],
286 avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
288 return err;
289 }
290 latmctx->initialized = 1;
291 }
292 }
293
294 if (show_bits(&gb, 12) == 0xfff) {
296 "ADTS header detected, probably as result of configuration "
297 "misparsing\n");
298 return AVERROR_INVALIDDATA;
299 }
300
301 switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
302 case AOT_ER_AAC_LC:
303 case AOT_ER_AAC_LTP:
304 case AOT_ER_AAC_LD:
305 case AOT_ER_AAC_ELD:
306 err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
307 break;
308 default:
309 err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
310 }
311 if (err < 0)
312 return err;
313
314 return muxlength;
315}
316
318{
319 struct LATMContext *latmctx = avctx->priv_data;
320 int ret = ff_aac_decode_init_float(avctx);
321
322 if (avctx->extradata_size > 0)
323 latmctx->initialized = !ret;
324
325 return ret;
326}
327
328/*
329 Note: This decoder filter is intended to decode LATM streams transferred
330 in MPEG transport streams which only contain one program.
331 To do a more complex LATM demuxing a separate LATM demuxer should be used.
332*/
334 .p.name = "aac_latm",
335 CODEC_LONG_NAME("AAC LATM (Advanced Audio Coding LATM syntax)"),
336 .p.type = AVMEDIA_TYPE_AUDIO,
337 .p.id = AV_CODEC_ID_AAC_LATM,
338 .priv_data_size = sizeof(struct LATMContext),
340 .close = decode_close,
344 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
346 .flush = flush,
348};
349
350#endif /* AVCODEC_AAC_AACDEC_LATM_H */
int ff_aac_decode_init_float(AVCodecContext *avctx)
static int read_audio_mux_element(struct LATMContext *latmctx, GetBitContext *gb)
static av_cold int latm_decode_init(AVCodecContext *avctx)
const FFCodec ff_aac_latm_decoder
static uint32_t latm_get_value(GetBitContext *b)
Definition aacdec_latm.h:47
#define LOAS_SYNC_WORD
11 bits LOAS sync word
Definition aacdec_latm.h:35
static int latm_decode_frame(AVCodecContext *avctx, AVFrame *out, int *got_frame_ptr, AVPacket *avpkt)
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
static int read_stream_mux_config(struct LATMContext *latmctx, GetBitContext *gb)
static int latm_decode_audio_specific_config(struct LATMContext *latmctx, GetBitContext *gb, int asclen)
Definition aacdec_latm.h:54
const AVChannelLayout ff_aac_ch_layout[]
Definition aacdec_tab.c:96
static FILE * out
static AVFormatContext * ctx
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define CODEC_CH_LAYOUTS_ARRAY(array)
#define NULL
Definition coverity.c:32
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_AAC_LATM
Definition codec_id.h:502
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define b
Definition input.c:43
static int decode_audio_specific_config(AACDecContext *ac, AVCodecContext *avctx, OutputConfiguration *oc, const uint8_t *data, int64_t bit_size, int sync_extension)
Definition aacdec.c:1194
static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb)
Definition aacdec.c:2251
static int decode_audio_specific_config_gb(AACDecContext *ac, AVCodecContext *avctx, OutputConfiguration *oc, GetBitContext *gb, int get_bit_alignment, int sync_extension)
Decode audio specific configuration; reference: table 1.13.
Definition aacdec.c:1119
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1220
static int push_output_configuration(AACDecContext *ac)
Save current output configuration if and only if it has been locked.
Definition aacdec.c:454
static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb, const AVPacket *avpkt)
Definition aacdec.c:2505
static void pop_output_configuration(AACDecContext *ac)
Restore the previous output configuration if and only if the current configuration is unlocked.
Definition aacdec.c:470
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
@ AOT_ER_AAC_LD
N Error Resilient Low Delay.
Definition mpeg4audio.h:95
@ AOT_ER_AAC_ELD
N Error Resilient Enhanced Low Delay.
Definition mpeg4audio.h:111
@ AOT_ER_AAC_LC
N Error Resilient Low Complexity.
Definition mpeg4audio.h:89
@ AOT_ER_AAC_LTP
N Error Resilient Long Term Prediction.
Definition mpeg4audio.h:91
#define av_malloc(s)
Definition ops_static.c:52
const AVProfile ff_aac_profiles[]
Definition profiles.c:27
main AAC decoding context
Definition aacdec.h:500
struct AVCodecContext * avctx
Definition aacdec.h:502
OutputConfiguration oc[2]
Definition aacdec.h:586
main external API structure.
Definition avcodec.h:443
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
const uint8_t * buffer
Definition get_bits.h:110
int audio_mux_version_A
LATM syntax version.
Definition aacdec_latm.h:42
int frame_length_type
0/1 variable/fixed frame length
Definition aacdec_latm.h:43
int initialized
initialized after a valid extradata was seen
Definition aacdec_latm.h:39
AACDecContext aac_ctx
containing AACContext
Definition aacdec_latm.h:38
int frame_length
frame length for fixed frame length
Definition aacdec_latm.h:44
MPEG4AudioConfig m4ac
Definition aacdec.h:420
#define av_free(p)
#define avpriv_request_sample(...)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52