Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
54 | 322 | static int latm_decode_audio_specific_config(struct LATMContext *latmctx, | |
55 | GetBitContext *gb, int asclen) | ||
56 | { | ||
57 | 322 | AACDecContext *ac = &latmctx->aac_ctx; | |
58 | 322 | AVCodecContext *avctx = ac->avctx; | |
59 | 322 | MPEG4AudioConfig m4ac = { 0 }; | |
60 | GetBitContext gbc; | ||
61 | 322 | int config_start_bit = get_bits_count(gb); | |
62 | 322 | int sync_extension = 0; | |
63 | int bits_consumed, esize, i; | ||
64 | |||
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 322 times.
|
322 | 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 |
1/2✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
|
322 | } else if (asclen == 0) { |
71 | 322 | gbc = *gb; | |
72 | } else { | ||
73 | ✗ | return AVERROR_INVALIDDATA; | |
74 | } | ||
75 | |||
76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | if (get_bits_left(gb) <= 0) |
77 | ✗ | return AVERROR_INVALIDDATA; | |
78 | |||
79 | 322 | bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &m4ac, | |
80 | &gbc, config_start_bit, | ||
81 | sync_extension); | ||
82 | |||
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 322 times.
|
322 | if (bits_consumed < config_start_bit) |
84 | ✗ | return AVERROR_INVALIDDATA; | |
85 | 322 | bits_consumed -= config_start_bit; | |
86 | |||
87 |
1/2✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
|
322 | if (asclen == 0) |
88 | 322 | asclen = bits_consumed; | |
89 | |||
90 |
2/2✓ Branch 0 taken 316 times.
✓ Branch 1 taken 6 times.
|
322 | if (!latmctx->initialized || |
91 |
1/2✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
|
316 | ac->oc[1].m4ac.sample_rate != m4ac.sample_rate || |
92 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 315 times.
|
316 | ac->oc[1].m4ac.chan_config != m4ac.chan_config) { |
93 | |||
94 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (latmctx->initialized) { |
95 | 1 | 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 | 6 | av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n"); | |
98 | } | ||
99 | 7 | latmctx->initialized = 0; | |
100 | |||
101 | 7 | esize = (asclen + 7) / 8; | |
102 | |||
103 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | if (avctx->extradata_size < esize) { |
104 | 6 | av_free(avctx->extradata); | |
105 | 6 | avctx->extradata = av_malloc(esize + AV_INPUT_BUFFER_PADDING_SIZE); | |
106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!avctx->extradata) |
107 | ✗ | return AVERROR(ENOMEM); | |
108 | } | ||
109 | |||
110 | 7 | avctx->extradata_size = esize; | |
111 | 7 | gbc = *gb; | |
112 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
|
28 | for (i = 0; i < esize; i++) { |
113 | 21 | avctx->extradata[i] = get_bits(&gbc, 8); | |
114 | } | ||
115 | 7 | memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
116 | } | ||
117 | 322 | skip_bits_long(gb, asclen); | |
118 | |||
119 | 322 | return 0; | |
120 | } | ||
121 | |||
122 | 322 | static int read_stream_mux_config(struct LATMContext *latmctx, | |
123 | GetBitContext *gb) | ||
124 | { | ||
125 | 322 | int ret, audio_mux_version = get_bits(gb, 1); | |
126 | |||
127 | 322 | latmctx->audio_mux_version_A = 0; | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 322 times.
|
322 | if (audio_mux_version) |
129 | ✗ | latmctx->audio_mux_version_A = get_bits(gb, 1); | |
130 | |||
131 |
1/2✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
|
322 | if (!latmctx->audio_mux_version_A) { |
132 | |||
133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 322 times.
|
322 | if (audio_mux_version) |
134 | ✗ | latm_get_value(gb); // taraFullness | |
135 | |||
136 | 322 | skip_bits(gb, 1); // allStreamSameTimeFraming | |
137 | 322 | skip_bits(gb, 6); // numSubFrames | |
138 | // numPrograms | ||
139 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | 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 |
1/2✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
|
322 | if (!audio_mux_version) { |
154 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | 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 | 322 | latmctx->frame_length_type = get_bits(gb, 3); | |
163 |
1/5✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
322 | switch (latmctx->frame_length_type) { |
164 | 322 | case 0: | |
165 | 322 | skip_bits(gb, 8); // latmBufferFullness | |
166 | 322 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | if (get_bits(gb, 1)) // crc present |
196 | ✗ | skip_bits(gb, 8); // config_crc | |
197 | } | ||
198 | |||
199 | 322 | return 0; | |
200 | } | ||
201 | |||
202 | 515 | static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb) | |
203 | { | ||
204 | uint8_t tmp; | ||
205 | |||
206 |
1/2✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
|
515 | if (ctx->frame_length_type == 0) { |
207 | 515 | int mux_slot_length = 0; | |
208 | do { | ||
209 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1504 times.
|
1504 | if (get_bits_left(gb) < 8) |
210 | ✗ | return AVERROR_INVALIDDATA; | |
211 | 1504 | tmp = get_bits(gb, 8); | |
212 | 1504 | mux_slot_length += tmp; | |
213 |
2/2✓ Branch 0 taken 989 times.
✓ Branch 1 taken 515 times.
|
1504 | } while (tmp == 255); |
214 | 515 | 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 | 521 | static int read_audio_mux_element(struct LATMContext *latmctx, | |
226 | GetBitContext *gb) | ||
227 | { | ||
228 | int err; | ||
229 | 521 | uint8_t use_same_mux = get_bits(gb, 1); | |
230 |
2/2✓ Branch 0 taken 322 times.
✓ Branch 1 taken 199 times.
|
521 | if (!use_same_mux) { |
231 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 322 times.
|
322 | if ((err = read_stream_mux_config(latmctx, gb)) < 0) |
232 | ✗ | return err; | |
233 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 193 times.
|
199 | } else if (!latmctx->aac_ctx.avctx->extradata) { |
234 | 6 | av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG, | |
235 | "no decoder config found\n"); | ||
236 | 6 | return 1; | |
237 | } | ||
238 |
1/2✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
|
515 | if (latmctx->audio_mux_version_A == 0) { |
239 | 515 | int mux_slot_length_bytes = read_payload_length_info(latmctx, gb); | |
240 |
2/4✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 515 times.
|
515 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 515 times.
|
515 | } 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 | 515 | return 0; | |
251 | } | ||
252 | |||
253 | |||
254 | 523 | static int latm_decode_frame(AVCodecContext *avctx, AVFrame *out, | |
255 | int *got_frame_ptr, AVPacket *avpkt) | ||
256 | { | ||
257 | 523 | struct LATMContext *latmctx = avctx->priv_data; | |
258 | int muxlength, err; | ||
259 | GetBitContext gb; | ||
260 | |||
261 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 523 times.
|
523 | if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0) |
262 | ✗ | return err; | |
263 | |||
264 | // check for LOAS sync word | ||
265 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 523 times.
|
523 | if (get_bits(&gb, 11) != LOAS_SYNC_WORD) |
266 | ✗ | return AVERROR_INVALIDDATA; | |
267 | |||
268 | 523 | muxlength = get_bits(&gb, 13) + 3; | |
269 | // not enough data, the parser should have sorted this out | ||
270 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 521 times.
|
523 | if (muxlength > avpkt->size) |
271 | 2 | return AVERROR_INVALIDDATA; | |
272 | |||
273 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 515 times.
|
521 | if ((err = read_audio_mux_element(latmctx, &gb))) |
274 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | return (err < 0) ? err : avpkt->size; |
275 | |||
276 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 508 times.
|
515 | if (!latmctx->initialized) { |
277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!avctx->extradata) { |
278 | ✗ | *got_frame_ptr = 0; | |
279 | ✗ | return avpkt->size; | |
280 | } else { | ||
281 | 7 | push_output_configuration(&latmctx->aac_ctx); | |
282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if ((err = decode_audio_specific_config( |
283 | &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac, | ||
284 | 7 | avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) { | |
285 | ✗ | pop_output_configuration(&latmctx->aac_ctx); | |
286 | ✗ | return err; | |
287 | } | ||
288 | 7 | latmctx->initialized = 1; | |
289 | } | ||
290 | } | ||
291 | |||
292 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 515 times.
|
515 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 515 times.
|
515 | 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 | 515 | default: | |
307 | 515 | err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt); | |
308 | } | ||
309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 515 times.
|
515 | if (err < 0) |
310 | ✗ | return err; | |
311 | |||
312 | 515 | return muxlength; | |
313 | } | ||
314 | |||
315 | 11 | static av_cold int latm_decode_init(AVCodecContext *avctx) | |
316 | { | ||
317 | 11 | struct LATMContext *latmctx = avctx->priv_data; | |
318 | 11 | int ret = aac_decode_init(avctx); | |
319 | |||
320 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
|
11 | if (avctx->extradata_size > 0) |
321 | 5 | latmctx->initialized = !ret; | |
322 | |||
323 | 11 | 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 | */ | ||
331 | const FFCodec ff_aac_latm_decoder = { | ||
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), | ||
337 | .init = latm_decode_init, | ||
338 | .close = decode_close, | ||
339 | FF_CODEC_DECODE_CB(latm_decode_frame), | ||
340 | .p.sample_fmts = (const enum AVSampleFormat[]) { | ||
341 | AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE | ||
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 */ | ||
351 |