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  MPEG4AudioConfig m4ac = { 0 };
60  GetBitContext gbc;
61  int config_start_bit = get_bits_count(gb);
62  int sync_extension = 0;
63  int bits_consumed, esize, i;
64 
65  if (asclen > 0) {
66  sync_extension = 1;
67  asclen = FFMIN(asclen, get_bits_left(gb));
68  init_get_bits(&gbc, gb->buffer, config_start_bit + asclen);
69  skip_bits_long(&gbc, config_start_bit);
70  } else if (asclen == 0) {
71  gbc = *gb;
72  } else {
73  return AVERROR_INVALIDDATA;
74  }
75 
76  if (get_bits_left(gb) <= 0)
77  return AVERROR_INVALIDDATA;
78 
79  bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &m4ac,
80  &gbc, config_start_bit,
81  sync_extension);
82 
83  if (bits_consumed < config_start_bit)
84  return AVERROR_INVALIDDATA;
85  bits_consumed -= config_start_bit;
86 
87  if (asclen == 0)
88  asclen = bits_consumed;
89 
90  if (!latmctx->initialized ||
91  ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
92  ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
93 
94  if (latmctx->initialized) {
95  av_log(avctx, AV_LOG_INFO, "audio config changed (sample_rate=%d, chan_config=%d)\n", m4ac.sample_rate, m4ac.chan_config);
96  } else {
97  av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
98  }
99  latmctx->initialized = 0;
100 
101  esize = (asclen + 7) / 8;
102 
103  if (avctx->extradata_size < esize) {
104  av_free(avctx->extradata);
106  if (!avctx->extradata)
107  return AVERROR(ENOMEM);
108  }
109 
110  avctx->extradata_size = esize;
111  gbc = *gb;
112  for (i = 0; i < esize; i++) {
113  avctx->extradata[i] = get_bits(&gbc, 8);
114  }
115  memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
116  }
117  skip_bits_long(gb, asclen);
118 
119  return 0;
120 }
121 
122 static int read_stream_mux_config(struct LATMContext *latmctx,
123  GetBitContext *gb)
124 {
125  int ret, audio_mux_version = get_bits(gb, 1);
126 
127  latmctx->audio_mux_version_A = 0;
128  if (audio_mux_version)
129  latmctx->audio_mux_version_A = get_bits(gb, 1);
130 
131  if (!latmctx->audio_mux_version_A) {
132 
133  if (audio_mux_version)
134  latm_get_value(gb); // taraFullness
135 
136  skip_bits(gb, 1); // allStreamSameTimeFraming
137  skip_bits(gb, 6); // numSubFrames
138  // numPrograms
139  if (get_bits(gb, 4)) { // numPrograms
140  avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
141  return AVERROR_PATCHWELCOME;
142  }
143 
144  // for each program (which there is only one in DVB)
145 
146  // for each layer (which there is only one in DVB)
147  if (get_bits(gb, 3)) { // numLayer
148  avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
149  return AVERROR_PATCHWELCOME;
150  }
151 
152  // for all but first stream: use_same_config = get_bits(gb, 1);
153  if (!audio_mux_version) {
154  if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
155  return ret;
156  } else {
157  int ascLen = latm_get_value(gb);
158  if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
159  return ret;
160  }
161 
162  latmctx->frame_length_type = get_bits(gb, 3);
163  switch (latmctx->frame_length_type) {
164  case 0:
165  skip_bits(gb, 8); // latmBufferFullness
166  break;
167  case 1:
168  latmctx->frame_length = get_bits(gb, 9);
169  break;
170  case 3:
171  case 4:
172  case 5:
173  skip_bits(gb, 6); // CELP frame length table index
174  break;
175  case 6:
176  case 7:
177  skip_bits(gb, 1); // HVXC frame length table index
178  break;
179  }
180 
181  if (get_bits(gb, 1)) { // other data
182  if (audio_mux_version) {
183  latm_get_value(gb); // other_data_bits
184  } else {
185  int esc;
186  do {
187  if (get_bits_left(gb) < 9)
188  return AVERROR_INVALIDDATA;
189  esc = get_bits(gb, 1);
190  skip_bits(gb, 8);
191  } while (esc);
192  }
193  }
194 
195  if (get_bits(gb, 1)) // crc present
196  skip_bits(gb, 8); // config_crc
197  }
198 
199  return 0;
200 }
201 
203 {
204  uint8_t tmp;
205 
206  if (ctx->frame_length_type == 0) {
207  int mux_slot_length = 0;
208  do {
209  if (get_bits_left(gb) < 8)
210  return AVERROR_INVALIDDATA;
211  tmp = get_bits(gb, 8);
212  mux_slot_length += tmp;
213  } while (tmp == 255);
214  return mux_slot_length;
215  } else if (ctx->frame_length_type == 1) {
216  return ctx->frame_length;
217  } else if (ctx->frame_length_type == 3 ||
218  ctx->frame_length_type == 5 ||
219  ctx->frame_length_type == 7) {
220  skip_bits(gb, 2); // mux_slot_length_coded
221  }
222  return 0;
223 }
224 
225 static int read_audio_mux_element(struct LATMContext *latmctx,
226  GetBitContext *gb)
227 {
228  int err;
229  uint8_t use_same_mux = get_bits(gb, 1);
230  if (!use_same_mux) {
231  if ((err = read_stream_mux_config(latmctx, gb)) < 0)
232  return err;
233  } else if (!latmctx->aac_ctx.avctx->extradata) {
234  av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
235  "no decoder config found\n");
236  return 1;
237  }
238  if (latmctx->audio_mux_version_A == 0) {
239  int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
240  if (mux_slot_length_bytes < 0 || mux_slot_length_bytes * 8LL > get_bits_left(gb)) {
241  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
242  return AVERROR_INVALIDDATA;
243  } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
244  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
245  "frame length mismatch %d << %d\n",
246  mux_slot_length_bytes * 8, get_bits_left(gb));
247  return AVERROR_INVALIDDATA;
248  }
249  }
250  return 0;
251 }
252 
253 
255  int *got_frame_ptr, AVPacket *avpkt)
256 {
257  struct LATMContext *latmctx = avctx->priv_data;
258  int muxlength, err;
259  GetBitContext gb;
260 
261  if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
262  return err;
263 
264  // check for LOAS sync word
265  if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
266  return AVERROR_INVALIDDATA;
267 
268  muxlength = get_bits(&gb, 13) + 3;
269  // not enough data, the parser should have sorted this out
270  if (muxlength > avpkt->size)
271  return AVERROR_INVALIDDATA;
272 
273  if ((err = read_audio_mux_element(latmctx, &gb)))
274  return (err < 0) ? err : avpkt->size;
275 
276  if (!latmctx->initialized) {
277  if (!avctx->extradata) {
278  *got_frame_ptr = 0;
279  return avpkt->size;
280  } else {
282  if ((err = decode_audio_specific_config(
283  &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
284  avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
286  return err;
287  }
288  latmctx->initialized = 1;
289  }
290  }
291 
292  if (show_bits(&gb, 12) == 0xfff) {
293  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
294  "ADTS header detected, probably as result of configuration "
295  "misparsing\n");
296  return AVERROR_INVALIDDATA;
297  }
298 
299  switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
300  case AOT_ER_AAC_LC:
301  case AOT_ER_AAC_LTP:
302  case AOT_ER_AAC_LD:
303  case AOT_ER_AAC_ELD:
304  err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
305  break;
306  default:
307  err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
308  }
309  if (err < 0)
310  return err;
311 
312  return muxlength;
313 }
314 
316 {
317  struct LATMContext *latmctx = avctx->priv_data;
318  int ret = aac_decode_init(avctx);
319 
320  if (avctx->extradata_size > 0)
321  latmctx->initialized = !ret;
322 
323  return ret;
324 }
325 
326 /*
327  Note: This decoder filter is intended to decode LATM streams transferred
328  in MPEG transport streams which only contain one program.
329  To do a more complex LATM demuxing a separate LATM demuxer should be used.
330 */
332  .p.name = "aac_latm",
333  CODEC_LONG_NAME("AAC LATM (Advanced Audio Coding LATM syntax)"),
334  .p.type = AVMEDIA_TYPE_AUDIO,
335  .p.id = AV_CODEC_ID_AAC_LATM,
336  .priv_data_size = sizeof(struct LATMContext),
338  .close = decode_close,
340  .p.sample_fmts = (const enum AVSampleFormat[]) {
342  },
343  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
344  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
345  .p.ch_layouts = ff_aac_ch_layout,
346  .flush = flush,
347  .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
348 };
349 
350 #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:1120
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:452
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:42
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
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:374
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:2247
AVPacket::data
uint8_t * data
Definition: packet.h:524
b
#define b
Definition: input.c:41
AOT_ER_AAC_LTP
@ AOT_ER_AAC_LTP
N Error Resilient Long Term Prediction.
Definition: mpeg4audio.h:88
FFCodec
Definition: codec_internal.h:126
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
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:331
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
GetBitContext
Definition: get_bits.h:108
LOAS_SYNC_WORD
#define LOAS_SYNC_WORD
11 bits LOAS sync word
Definition: aacdec_latm.h:35
decode_audio_specific_config
static int decode_audio_specific_config(AACDecContext *ac, AVCodecContext *avctx, MPEG4AudioConfig *m4ac, const uint8_t *data, int64_t bit_size, int sync_extension)
Definition: aacdec.c:1072
AOT_ER_AAC_LC
@ AOT_ER_AAC_LC
N Error Resilient Low Complexity.
Definition: mpeg4audio.h:87
latm_decode_frame
static int latm_decode_frame(AVCodecContext *avctx, AVFrame *out, int *got_frame_ptr, AVPacket *avpkt)
Definition: aacdec_latm.h:254
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:524
AOT_ER_AAC_LD
@ AOT_ER_AAC_LD
N Error Resilient Low Delay.
Definition: mpeg4audio.h:92
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
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:201
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:271
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
decode_audio_specific_config_gb
static int decode_audio_specific_config_gb(AACDecContext *ac, AVCodecContext *avctx, MPEG4AudioConfig *m4ac, GetBitContext *gb, int get_bit_alignment, int sync_extension)
Decode audio specific configuration; reference: table 1.13.
Definition: aacdec.c:1005
ff_aac_ch_layout
const AVChannelLayout ff_aac_ch_layout[]
Definition: aacdec_tab.c:96
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:2175
read_stream_mux_config
static int read_stream_mux_config(struct LATMContext *latmctx, GetBitContext *gb)
Definition: aacdec_latm.h:122
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:315
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:525
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
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
read_payload_length_info
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
Definition: aacdec_latm.h:202
LATMContext
Definition: aacdec_latm.h:37
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:523
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:436
AOT_ER_AAC_ELD
@ AOT_ER_AAC_ELD
N Error Resilient Enhanced Low Delay.
Definition: mpeg4audio.h:108
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:328
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:253
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AACDecContext::avctx
struct AVCodecContext * avctx
Definition: aacdec.h:255
latm_get_value
static uint32_t latm_get_value(GetBitContext *b)
Definition: aacdec_latm.h:47
OutputConfiguration::m4ac
MPEG4AudioConfig m4ac
Definition: aacdec.h:180
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:472
AVPacket
This structure stores compressed data.
Definition: packet.h:501
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:489
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:225
MPEG4AudioConfig::sample_rate
int sample_rate
Definition: mpeg4audio.h:32
aac_decode_init
static av_cold int aac_decode_init(AVCodecContext *avctx)
Definition: aacdec.c:1255