FFmpeg
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 
37 struct LATMContext {
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 
47 static 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 {
74  return AVERROR_INVALIDDATA;
75  }
76 
77  if (get_bits_left(gb) <= 0)
78  return AVERROR_INVALIDDATA;
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)
85  return AVERROR_INVALIDDATA;
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 
124 static 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");
143  return AVERROR_PATCHWELCOME;
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");
151  return AVERROR_PATCHWELCOME;
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 
227 static 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) {
236  av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
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)) {
246  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
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 {
284  if ((err = decode_audio_specific_config(
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) {
295  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
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,
342  .p.sample_fmts = (const enum AVSampleFormat[]) {
344  },
345  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
346  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
347  .p.ch_layouts = ff_aac_ch_layout,
348  .flush = flush,
349  .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
350 };
351 
352 #endif /* AVCODEC_AAC_AACDEC_LATM_H */
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: aacdec.c:1101
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
LATMContext::frame_length_type
int frame_length_type
0/1 variable/fixed frame length
Definition: aacdec_latm.h:43
pop_output_configuration
static void pop_output_configuration(AACDecContext *ac)
Restore the previous output configuration if and only if the current configuration is unlocked.
Definition: aacdec.c:443
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:55
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
decode_audio_specific_config
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:1075
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
aac_decode_frame_int
static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb, const AVPacket *avpkt)
Definition: aacdec.c:2379
AVPacket::data
uint8_t * data
Definition: packet.h:539
b
#define b
Definition: input.c:41
AOT_ER_AAC_LTP
@ AOT_ER_AAC_LTP
N Error Resilient Long Term Prediction.
Definition: mpeg4audio.h:90
FFCodec
Definition: codec_internal.h:127
ff_aac_profiles
const AVProfile ff_aac_profiles[]
Definition: profiles.c:27
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_aac_decode_init_float
int ff_aac_decode_init_float(AVCodecContext *avctx)
Definition: aacdec_float.c:164
MPEG4AudioConfig
Definition: mpeg4audio.h:29
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
ff_aac_latm_decoder
const FFCodec ff_aac_latm_decoder
Definition: aacdec_latm.h:333
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
GetBitContext
Definition: get_bits.h:108
LOAS_SYNC_WORD
#define LOAS_SYNC_WORD
11 bits LOAS sync word
Definition: aacdec_latm.h:35
AOT_ER_AAC_LC
@ AOT_ER_AAC_LC
N Error Resilient Low Complexity.
Definition: mpeg4audio.h:88
latm_decode_frame
static int latm_decode_frame(AVCodecContext *avctx, AVFrame *out, int *got_frame_ptr, AVPacket *avpkt)
Definition: aacdec_latm.h:256
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
AOT_ER_AAC_LD
@ AOT_ER_AAC_LD
N Error Resilient Low Delay.
Definition: mpeg4audio.h:94
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
LATMContext::frame_length
int frame_length
frame length for fixed frame length
Definition: aacdec_latm.h:44
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:109
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
ff_aac_ch_layout
const AVChannelLayout ff_aac_ch_layout[]
Definition: aacdec_tab.c:96
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:370
latm_decode_audio_specific_config
static int latm_decode_audio_specific_config(struct LATMContext *latmctx, GetBitContext *gb, int asclen)
Definition: aacdec_latm.h:54
LATMContext::initialized
int initialized
initialized after a valid extradata was seen
Definition: aacdec_latm.h:39
aac_decode_er_frame
static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb)
Definition: aacdec.c:2133
read_stream_mux_config
static int read_stream_mux_config(struct LATMContext *latmctx, GetBitContext *gb)
Definition: aacdec_latm.h:124
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
latm_decode_init
static av_cold int latm_decode_init(AVCodecContext *avctx)
Definition: aacdec_latm.h:317
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
OutputConfiguration
Definition: aacdec.h:367
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
read_payload_length_info
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
Definition: aacdec_latm.h:204
LATMContext
Definition: aacdec_latm.h:37
decode_audio_specific_config_gb
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:1000
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:371
push_output_configuration
static int push_output_configuration(AACDecContext *ac)
Save current output configuration if and only if it has been locked.
Definition: aacdec.c:427
AOT_ER_AAC_ELD
@ AOT_ER_AAC_ELD
N Error Resilient Enhanced Low Delay.
Definition: mpeg4audio.h:110
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MPEG4AudioConfig::chan_config
int chan_config
Definition: mpeg4audio.h:33
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AACDecContext::oc
OutputConfiguration oc[2]
Definition: aacdec.h:527
ret
ret
Definition: filter_design.txt:187
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
MPEG4AudioConfig::object_type
int object_type
Definition: mpeg4audio.h:30
LATMContext::audio_mux_version_A
int audio_mux_version_A
LATM syntax version.
Definition: aacdec_latm.h:42
AACDecContext
main AAC decoding context
Definition: aacdec.h:448
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AACDecContext::avctx
struct AVCodecContext * avctx
Definition: aacdec.h:450
latm_get_value
static uint32_t latm_get_value(GetBitContext *b)
Definition: aacdec_latm.h:47
OutputConfiguration::m4ac
MPEG4AudioConfig m4ac
Definition: aacdec.h:368
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:495
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
LATMContext::aac_ctx
AACDecContext aac_ctx
containing AACContext
Definition: aacdec_latm.h:38
read_audio_mux_element
static int read_audio_mux_element(struct LATMContext *latmctx, GetBitContext *gb)
Definition: aacdec_latm.h:227
MPEG4AudioConfig::sample_rate
int sample_rate
Definition: mpeg4audio.h:32