| | 1 | /* |
| | 2 | * AAC LATM decoder |
| | 3 | * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz> |
| | 4 | * Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net> |
| | 5 | * |
| | 6 | * This file is part of FFmpeg. |
| | 7 | * |
| | 8 | * FFmpeg is free software; you can redistribute it and/or |
| | 9 | * modify it under the terms of the GNU Lesser General Public |
| | 10 | * License as published by the Free Software Foundation; either |
| | 11 | * version 2.1 of the License, or (at your option) any later version. |
| | 12 | * |
| | 13 | * FFmpeg is distributed in the hope that it will be useful, |
| | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 16 | * Lesser General Public License for more details. |
| | 17 | * |
| | 18 | * You should have received a copy of the GNU Lesser General Public |
| | 19 | * License along with FFmpeg; if not, write to the Free Software |
| | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| | 21 | */ |
| | 22 | |
| | 23 | /** |
| | 24 | * @file |
| | 25 | * AAC LATM decoder |
| | 26 | * @author Paul Kendall <paul@kcbbs.gen.nz> |
| | 27 | * @author Janne Grunau <janne-ffmpeg@jannau.net> |
| | 28 | */ |
| | 29 | |
| | 30 | /* |
| | 31 | Note: This decoder filter is intended to decode LATM streams transferred |
| | 32 | in MPEG transport streams which only contain one program. |
| | 33 | To do a more complex LATM demuxing a separate LATM demuxer should be used. |
| | 34 | */ |
| | 35 | |
| | 36 | #include "get_bits.h" |
| | 37 | #include "dsputil.h" |
| | 38 | |
| | 39 | #include "aac.h" |
| | 40 | #include "aacdectab.h" |
| | 41 | #include "mpeg4audio.h" |
| | 42 | |
| | 43 | #include <assert.h> |
| | 44 | |
| | 45 | #define LOAS_SYNC_WORD 0x2b7 // 11 bits |
| | 46 | #define MAX_SIZE 8*1024 |
| | 47 | |
| | 48 | struct LATMContext |
| | 49 | { |
| | 50 | AACContext aac_ctx; |
| | 51 | AVCodec *aac_codec; |
| | 52 | uint8_t initialized; |
| | 53 | |
| | 54 | // parser data |
| | 55 | uint8_t audio_mux_version_A; |
| | 56 | uint8_t same_time_framing; |
| | 57 | uint8_t frame_length_type; |
| | 58 | uint32_t frame_length; |
| | 59 | }; |
| | 60 | |
| | 61 | static inline int64_t latm_get_value(GetBitContext *b) |
| | 62 | { |
| | 63 | uint8_t bytesForValue = get_bits(b, 2); |
| | 64 | int64_t value = 0; |
| | 65 | int i; |
| | 66 | for (i=0; i<=bytesForValue; i++) { |
| | 67 | value <<= 8; |
| | 68 | value |= get_bits(b, 8); |
| | 69 | } |
| | 70 | return value; |
| | 71 | } |
| | 72 | |
| | 73 | // copied from libavcodec/mpeg4audio.c |
| | 74 | static av_always_inline unsigned int copy_bits(PutBitContext *pb, |
| | 75 | GetBitContext *gb, int bits) |
| | 76 | { |
| | 77 | unsigned int el = get_bits(gb, bits); |
| | 78 | put_bits(pb, bits, el); |
| | 79 | return el; |
| | 80 | } |
| | 81 | |
| | 82 | static void latm_read_ga_specific_config(int audio_object_type, |
| | 83 | MPEG4AudioConfig *c, |
| | 84 | GetBitContext *gb, PutBitContext *pb) |
| | 85 | { |
| | 86 | int ext_flag; |
| | 87 | |
| | 88 | copy_bits(pb, gb, 1); // framelen_flag |
| | 89 | if (copy_bits(pb, gb, 1)) // depends_on_coder |
| | 90 | copy_bits(pb, gb, 14); // delay |
| | 91 | ext_flag = copy_bits(pb, gb, 1); |
| | 92 | |
| | 93 | if (!c->chan_config) |
| | 94 | ff_copy_pce_data(pb, gb); // program_config_element |
| | 95 | |
| | 96 | if (audio_object_type == AOT_AAC_SCALABLE || |
| | 97 | audio_object_type == AOT_ER_AAC_SCALABLE) |
| | 98 | copy_bits(pb, gb, 3); // layer number |
| | 99 | |
| | 100 | if (!ext_flag) |
| | 101 | return; |
| | 102 | |
| | 103 | if (audio_object_type == AOT_ER_BSAC) { |
| | 104 | copy_bits(pb, gb, 5); // numOfSubFrame |
| | 105 | copy_bits(pb, gb, 11); // layer_length |
| | 106 | } else if (audio_object_type == AOT_ER_AAC_LC || |
| | 107 | audio_object_type == AOT_ER_AAC_LTP || |
| | 108 | audio_object_type == AOT_ER_AAC_SCALABLE || |
| | 109 | audio_object_type == AOT_ER_AAC_LD) |
| | 110 | copy_bits(pb, gb, 3); // stuff |
| | 111 | copy_bits(pb, gb, 1); // extflag3 |
| | 112 | } |
| | 113 | |
| | 114 | static int latm_read_audio_specific_config(GetBitContext *gb, |
| | 115 | PutBitContext *pb) |
| | 116 | { |
| | 117 | int num_bits=0; |
| | 118 | int audio_object_type; |
| | 119 | |
| | 120 | MPEG4AudioConfig b, *c; |
| | 121 | c = &b; |
| | 122 | |
| | 123 | c->sbr = -1; |
| | 124 | |
| | 125 | audio_object_type = copy_bits(pb, gb, 5); |
| | 126 | if (audio_object_type == AOT_ESCAPE) { |
| | 127 | audio_object_type = AOT_ESCAPE + copy_bits(pb, gb, 6) + 1; |
| | 128 | } |
| | 129 | c->object_type = audio_object_type; |
| | 130 | |
| | 131 | c->sampling_index = copy_bits(pb, gb, 4); |
| | 132 | c->sample_rate = ff_mpeg4audio_sample_rates[c->sampling_index]; |
| | 133 | if (c->sampling_index == 0x0f) { |
| | 134 | c->sample_rate = copy_bits(pb, gb, 24); |
| | 135 | } |
| | 136 | c->chan_config = copy_bits(pb, gb, 4); |
| | 137 | |
| | 138 | if (c->chan_config < FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) |
| | 139 | c->channels = ff_mpeg4audio_channels[c->chan_config]; |
| | 140 | |
| | 141 | if (audio_object_type == AOT_AAC_MAIN || |
| | 142 | audio_object_type == AOT_AAC_LC || |
| | 143 | audio_object_type == AOT_AAC_SSR || |
| | 144 | audio_object_type == AOT_AAC_LTP || |
| | 145 | audio_object_type == AOT_AAC_SCALABLE || |
| | 146 | audio_object_type == AOT_TWINVQ) { |
| | 147 | latm_read_ga_specific_config(audio_object_type, c, gb, pb); |
| | 148 | } else if (audio_object_type == AOT_SBR) { |
| | 149 | c->sbr = 1; |
| | 150 | c->ext_sampling_index = copy_bits(pb, gb, 4); |
| | 151 | c->ext_sample_rate = ff_mpeg4audio_sample_rates[c->ext_sampling_index]; |
| | 152 | if (c->ext_sampling_index == 0x0f) { |
| | 153 | c->ext_sample_rate = copy_bits(pb, gb, 24); |
| | 154 | } |
| | 155 | c->object_type = copy_bits(pb, gb, 5); |
| | 156 | } else if (audio_object_type >= AOT_ER_AAC_LC) { |
| | 157 | latm_read_ga_specific_config(audio_object_type, c, gb, pb); |
| | 158 | copy_bits(pb, gb, 2); // epConfig |
| | 159 | } |
| | 160 | |
| | 161 | if (c->sbr == -1 && c->sample_rate <= 24000) |
| | 162 | c->sample_rate *= 2; |
| | 163 | |
| | 164 | // count the extradata |
| | 165 | num_bits = put_bits_count(pb); |
| | 166 | |
| | 167 | flush_put_bits(pb); |
| | 168 | return num_bits; |
| | 169 | } |
| | 170 | |
| | 171 | static int latm_decode_audio_specific_config(struct LATMContext *latmctx, |
| | 172 | GetBitContext *gb) |
| | 173 | { |
| | 174 | PutBitContext pb; |
| | 175 | int32_t esize, bits_consumed; |
| | 176 | uint8_t extradata[32+FF_INPUT_BUFFER_PADDING_SIZE]; |
| | 177 | AVCodecContext *avctx = latmctx->aac_ctx.avctx; |
| | 178 | |
| | 179 | init_put_bits(&pb, extradata, 32 * 8); |
| | 180 | |
| | 181 | bits_consumed = latm_read_audio_specific_config(gb, &pb); |
| | 182 | |
| | 183 | if (bits_consumed < 0) |
| | 184 | return AVERROR_INVALIDDATA; |
| | 185 | |
| | 186 | esize = (bits_consumed+7) / 8; |
| | 187 | |
| | 188 | if (avctx->extradata_size != esize) { |
| | 189 | av_free(avctx->extradata); |
| | 190 | avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE); |
| | 191 | if (!avctx->extradata) |
| | 192 | return AVERROR(ENOMEM); |
| | 193 | |
| | 194 | avctx->extradata_size = esize; |
| | 195 | memcpy(avctx->extradata, extradata, esize); |
| | 196 | memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
| | 197 | } |
| | 198 | |
| | 199 | return bits_consumed; |
| | 200 | } |
| | 201 | |
| | 202 | static int read_stream_mux_config(struct LATMContext *latmctx, |
| | 203 | GetBitContext *gb) |
| | 204 | { |
| | 205 | int ret, audio_mux_version = get_bits(gb, 1); |
| | 206 | |
| | 207 | latmctx->audio_mux_version_A = 0; |
| | 208 | if (audio_mux_version) |
| | 209 | latmctx->audio_mux_version_A = get_bits(gb, 1); |
| | 210 | |
| | 211 | if (!latmctx->audio_mux_version_A) { |
| | 212 | |
| | 213 | if (audio_mux_version) |
| | 214 | latm_get_value(gb); // taraFullness |
| | 215 | |
| | 216 | skip_bits(gb, 1); // allStreamSameTimeFraming |
| | 217 | skip_bits(gb, 6); // numSubFrames |
| | 218 | // numPrograms |
| | 219 | if (get_bits(gb, 4)) { // numPrograms |
| | 220 | av_log_missing_feature(latmctx->aac_ctx.avctx, |
| | 221 | "multiple programs are not supported\n", 1); |
| | 222 | return AVERROR_PATCHWELCOME; |
| | 223 | } |
| | 224 | |
| | 225 | // for each program (which there is only on in DVB) |
| | 226 | |
| | 227 | // for each layer (which there is only on in DVB) |
| | 228 | if (get_bits(gb, 3)) { // numLayer |
| | 229 | av_log_missing_feature(latmctx->aac_ctx.avctx, |
| | 230 | "multiple layers are not supported\n", 1); |
| | 231 | return AVERROR_PATCHWELCOME; |
| | 232 | } |
| | 233 | |
| | 234 | // for all but first stream: use_same_config = get_bits(gb, 1); |
| | 235 | if (!audio_mux_version) { |
| | 236 | ret = latm_decode_audio_specific_config(latmctx, gb); |
| | 237 | if (ret < 0) |
| | 238 | return ret; |
| | 239 | } else { |
| | 240 | int ascLen = latm_get_value(gb); |
| | 241 | ret = latm_decode_audio_specific_config(latmctx, gb); |
| | 242 | if (ret < 0) |
| | 243 | return ret; |
| | 244 | ascLen -= ret; |
| | 245 | skip_bits_long(gb, ascLen); |
| | 246 | } |
| | 247 | |
| | 248 | latmctx->frame_length_type = get_bits(gb, 3); |
| | 249 | switch (latmctx->frame_length_type) { |
| | 250 | case 0: |
| | 251 | skip_bits(gb, 8); // latmBufferFullness |
| | 252 | break; |
| | 253 | case 1: |
| | 254 | latmctx->frame_length = get_bits(gb, 9); |
| | 255 | break; |
| | 256 | case 3: |
| | 257 | case 4: |
| | 258 | case 5: |
| | 259 | skip_bits(gb, 6); // CELP frame length table index |
| | 260 | break; |
| | 261 | case 6: |
| | 262 | case 7: |
| | 263 | skip_bits(gb, 1); // HVXC frame length table index |
| | 264 | break; |
| | 265 | } |
| | 266 | |
| | 267 | if (get_bits(gb, 1)) { // other data |
| | 268 | if (audio_mux_version) { |
| | 269 | latm_get_value(gb); // other_data_bits |
| | 270 | } else { |
| | 271 | int esc; |
| | 272 | do { |
| | 273 | esc = get_bits(gb, 1); |
| | 274 | skip_bits(gb, 8); |
| | 275 | } while (esc); |
| | 276 | } |
| | 277 | } |
| | 278 | |
| | 279 | if (get_bits(gb, 1)) // crc present |
| | 280 | skip_bits(gb, 8); // config_crc |
| | 281 | } |
| | 282 | |
| | 283 | return 0; |
| | 284 | } |
| | 285 | |
| | 286 | static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb) |
| | 287 | { |
| | 288 | uint8_t tmp; |
| | 289 | |
| | 290 | if (ctx->frame_length_type == 0) { |
| | 291 | int mux_slot_length = 0; |
| | 292 | do { |
| | 293 | tmp = get_bits(gb, 8); |
| | 294 | mux_slot_length += tmp; |
| | 295 | } while (tmp == 255); |
| | 296 | return mux_slot_length; |
| | 297 | } else if (ctx->frame_length_type == 1) { |
| | 298 | return ctx->frame_length; |
| | 299 | } else if (ctx->frame_length_type == 3 || |
| | 300 | ctx->frame_length_type == 5 || |
| | 301 | ctx->frame_length_type == 7) { |
| | 302 | skip_bits(gb, 2); // mux_slot_length_coded |
| | 303 | } |
| | 304 | return 0; |
| | 305 | } |
| | 306 | |
| | 307 | static int read_audio_mux_element(struct LATMContext *latmctx, |
| | 308 | GetBitContext *b, |
| | 309 | uint8_t *payload, int *payloadsize) |
| | 310 | { |
| | 311 | uint8_t use_same_mux = get_bits(b, 1); |
| | 312 | if (!use_same_mux) { |
| | 313 | read_stream_mux_config(latmctx, b); |
| | 314 | } else if (!latmctx->aac_ctx.avctx->extradata) { |
| | 315 | av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG, |
| | 316 | "no decoder config found\n"); |
| | 317 | return AVERROR(EAGAIN); |
| | 318 | } |
| | 319 | if (latmctx->audio_mux_version_A == 0) { |
| | 320 | int j; |
| | 321 | int mux_slot_length_bytes = read_payload_length_info(latmctx, b); |
| | 322 | mux_slot_length_bytes = FFMIN(mux_slot_length_bytes, *payloadsize); |
| | 323 | for (j=0; j<mux_slot_length_bytes; j++) { |
| | 324 | *payload++ = get_bits(b, 8); |
| | 325 | } |
| | 326 | *payloadsize = mux_slot_length_bytes; |
| | 327 | } |
| | 328 | return 0; |
| | 329 | } |
| | 330 | |
| | 331 | static int read_audio_sync_stream(struct LATMContext *latmctx, |
| | 332 | GetBitContext *gb, int size, |
| | 333 | uint8_t *payload, int *payloadsize) |
| | 334 | { |
| | 335 | int muxlength; |
| | 336 | |
| | 337 | if (get_bits(gb, 11) != LOAS_SYNC_WORD) |
| | 338 | return AVERROR_INVALIDDATA; |
| | 339 | |
| | 340 | muxlength = get_bits(gb, 13); |
| | 341 | // not enough data, the parser should have sorted this |
| | 342 | if (muxlength+3 > size) |
| | 343 | return AVERROR_INVALIDDATA; |
| | 344 | |
| | 345 | read_audio_mux_element(latmctx, gb, payload, payloadsize); |
| | 346 | |
| | 347 | return 0; |
| | 348 | } |
| | 349 | |
| | 350 | |
| | 351 | static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size, |
| | 352 | AVPacket *avpkt) |
| | 353 | { |
| | 354 | struct LATMContext *latmctx = avctx->priv_data; |
| | 355 | uint8_t *tmp, tmpbuf[MAX_SIZE]; |
| | 356 | int ret, bufsize = MAX_SIZE; |
| | 357 | GetBitContext gb; |
| | 358 | |
| | 359 | if(avpkt->size == 0) |
| | 360 | return 0; |
| | 361 | |
| | 362 | init_get_bits(&gb, avpkt->data, avpkt->size * 8); |
| | 363 | |
| | 364 | ret = read_audio_sync_stream(latmctx, &gb, avpkt->size, tmpbuf, &bufsize); |
| | 365 | if (ret < 0) |
| | 366 | return ret; |
| | 367 | |
| | 368 | if (!latmctx->initialized) { |
| | 369 | if (!avctx->extradata) { |
| | 370 | *out_size = 0; |
| | 371 | return avpkt->size; |
| | 372 | } else { |
| | 373 | assert(latmctx->aac_codec->init); |
| | 374 | ret = latmctx->aac_codec->init(avctx); |
| | 375 | if (ret < 0) |
| | 376 | return ret; |
| | 377 | latmctx->initialized = 1; |
| | 378 | } |
| | 379 | } |
| | 380 | |
| | 381 | tmp = avpkt->data; |
| | 382 | avpkt->data = tmpbuf; |
| | 383 | avpkt->size = bufsize; |
| | 384 | |
| | 385 | assert(latmctx->aac_codec->decode); |
| | 386 | ret = latmctx->aac_codec->decode(avctx, out, out_size, avpkt); |
| | 387 | avpkt->data = tmp; |
| | 388 | return ret; |
| | 389 | } |
| | 390 | |
| | 391 | static int latm_decode_init(AVCodecContext *avctx) |
| | 392 | { |
| | 393 | struct LATMContext *latmctx = avctx->priv_data; |
| | 394 | int ret; |
| | 395 | |
| | 396 | latmctx->aac_codec = avcodec_find_decoder_by_name("aac"); |
| | 397 | if (!latmctx->aac_codec) { |
| | 398 | av_log(avctx, AV_LOG_ERROR, "AAC decoder is required by AAC LATM decoder.\n"); |
| | 399 | return AVERROR(ENOSYS); |
| | 400 | } |
| | 401 | |
| | 402 | assert(latmctx->aac_codec->init); |
| | 403 | ret = latmctx->aac_codec->init(avctx); |
| | 404 | |
| | 405 | if (avctx->extradata_size > 0) |
| | 406 | latmctx->initialized = !ret; |
| | 407 | else |
| | 408 | latmctx->initialized = 0; |
| | 409 | |
| | 410 | return ret; |
| | 411 | } |
| | 412 | |
| | 413 | static int latm_decode_close(AVCodecContext *avctx) |
| | 414 | { |
| | 415 | struct LATMContext *latmctx = avctx->priv_data; |
| | 416 | assert(latmctx->aac_codec->close); |
| | 417 | return latmctx->aac_codec->close(avctx); |
| | 418 | } |
| | 419 | |
| | 420 | AVCodec aac_latm_decoder = { |
| | 421 | .name = "aac_latm", |
| | 422 | .type = CODEC_TYPE_AUDIO, |
| | 423 | .id = CODEC_ID_AAC_LATM, |
| | 424 | .priv_data_size = sizeof(struct LATMContext), |
| | 425 | .init = latm_decode_init, |
| | 426 | .close = latm_decode_close, |
| | 427 | .decode = latm_decode_frame, |
| | 428 | .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM " |
| | 429 | "syntax)"), |
| | 430 | .sample_fmts = (const enum SampleFormat[]) { |
| | 431 | SAMPLE_FMT_S16,SAMPLE_FMT_NONE |
| | 432 | }, |
| | 433 | .channel_layouts = aac_channel_layout, |
| | 434 | }; |