| | 1 | /* |
| | 2 | * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz> |
| | 3 | * |
| | 4 | * This file is part of FFmpeg. |
| | 5 | * |
| | 6 | * FFmpeg is free software; you can redistribute it and/or |
| | 7 | * modify it under the terms of the GNU Lesser General Public |
| | 8 | * License as published by the Free Software Foundation; either |
| | 9 | * version 2.1 of the License, or (at your option) any later version. |
| | 10 | * |
| | 11 | * FFmpeg is distributed in the hope that it will be useful, |
| | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 14 | * Lesser General Public License for more details. |
| | 15 | * |
| | 16 | * You should have received a copy of the GNU Lesser General Public |
| | 17 | * License along with FFmpeg; if not, write to the Free Software |
| | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| | 19 | */ |
| | 20 | |
| | 21 | /** |
| | 22 | * @file latmaac.c |
| | 23 | * LATM wrapped AAC decoder |
| | 24 | */ |
| | 25 | |
| | 26 | #include <stdio.h> |
| | 27 | #include <stdlib.h> |
| | 28 | #include <string.h> |
| | 29 | #include <math.h> |
| | 30 | #include <sys/types.h> |
| | 31 | |
| | 32 | #include "parser.h" |
| | 33 | #include "get_bits.h" |
| | 34 | #include "put_bits.h" |
| | 35 | #include "mpeg4audio.h" |
| | 36 | #include "neaacdec.h" |
| | 37 | |
| | 38 | /* |
| | 39 | Note: This decoder filter is intended to decode LATM streams transferred |
| | 40 | in MPEG transport streams which only contain one program. |
| | 41 | To do a more complex LATM demuxing a separate LATM demuxer should be used. |
| | 42 | */ |
| | 43 | |
| | 44 | #define SYNC_LATM 0x2b7 // 11 bits |
| | 45 | #define MAX_SIZE 8*1024 |
| | 46 | |
| | 47 | typedef struct AACDecoder |
| | 48 | { |
| | 49 | faacDecHandle aac_decoder; |
| | 50 | uint8_t initialized; |
| | 51 | |
| | 52 | // parser data |
| | 53 | uint8_t audio_mux_version_A; |
| | 54 | uint8_t frameLengthType; |
| | 55 | uint8_t extra[64]; // should be way enough |
| | 56 | int extrasize; |
| | 57 | } AACDecoder; |
| | 58 | |
| | 59 | static inline int64_t latm_get_value(GetBitContext *b) |
| | 60 | { |
| | 61 | uint8_t bytesForValue = get_bits(b, 2); |
| | 62 | int64_t value = 0; |
| | 63 | int i; |
| | 64 | for (i=0; i<=bytesForValue; i++) { |
| | 65 | value <<= 8; |
| | 66 | value |= get_bits(b, 8); |
| | 67 | } |
| | 68 | return value; |
| | 69 | } |
| | 70 | |
| | 71 | static void readGASpecificConfig(int audioObjectType, GetBitContext *b, PutBitContext *o) |
| | 72 | { |
| | 73 | int framelen_flag; |
| | 74 | int dependsOnCoder; |
| | 75 | int ext_flag; |
| | 76 | |
| | 77 | framelen_flag = get_bits(b, 1); |
| | 78 | put_bits(o, 1, framelen_flag); |
| | 79 | dependsOnCoder = get_bits(b, 1); |
| | 80 | put_bits(o, 1, dependsOnCoder); |
| | 81 | if (dependsOnCoder) { |
| | 82 | int delay = get_bits(b, 14); |
| | 83 | put_bits(o, 14, delay); |
| | 84 | } |
| | 85 | ext_flag = get_bits(b, 1); |
| | 86 | put_bits(o, 1, ext_flag); |
| | 87 | |
| | 88 | if (audioObjectType == 6 || audioObjectType == 20) { |
| | 89 | int layerNr = get_bits(b, 3); |
| | 90 | put_bits(o, 3, layerNr); |
| | 91 | } |
| | 92 | if (ext_flag) { |
| | 93 | if (audioObjectType == 22) { |
| | 94 | skip_bits(b, 5); // numOfSubFrame |
| | 95 | skip_bits(b, 11); // layer_length |
| | 96 | |
| | 97 | put_bits(o, 16, 0); |
| | 98 | } |
| | 99 | if (audioObjectType == 17 || |
| | 100 | audioObjectType == 19 || |
| | 101 | audioObjectType == 20 || |
| | 102 | audioObjectType == 23) { |
| | 103 | |
| | 104 | skip_bits(b, 3); // stuff |
| | 105 | put_bits(o, 3, 0); |
| | 106 | } |
| | 107 | |
| | 108 | skip_bits(b, 1); // extflag3 |
| | 109 | put_bits(o, 1, 0); |
| | 110 | } |
| | 111 | } |
| | 112 | |
| | 113 | static int readAudioSpecificConfig(struct AACDecoder *decoder, GetBitContext *b) |
| | 114 | { |
| | 115 | PutBitContext o; |
| | 116 | int ret = 0; |
| | 117 | int audioObjectType; |
| | 118 | int samplingFrequencyIndex; |
| | 119 | int channelConfiguration; |
| | 120 | |
| | 121 | init_put_bits(&o, decoder->extra, sizeof(decoder->extra)); |
| | 122 | |
| | 123 | audioObjectType = get_bits(b, 5); |
| | 124 | put_bits(&o, 5, audioObjectType); |
| | 125 | if (audioObjectType == 31) { |
| | 126 | uint8_t extended = get_bits(b, 6); |
| | 127 | put_bits(&o, 6, extended); |
| | 128 | audioObjectType = 32 + extended; |
| | 129 | } |
| | 130 | |
| | 131 | samplingFrequencyIndex = get_bits(b, 4); |
| | 132 | put_bits(&o, 4, samplingFrequencyIndex); |
| | 133 | if (samplingFrequencyIndex == 0x0f) { |
| | 134 | uint32_t f = get_bits_long(b, 24); |
| | 135 | put_bits(&o, 24, f); |
| | 136 | } |
| | 137 | channelConfiguration = get_bits(b, 4); |
| | 138 | put_bits(&o, 4, channelConfiguration); |
| | 139 | |
| | 140 | if (audioObjectType == 1 || audioObjectType == 2 || audioObjectType == 3 |
| | 141 | || audioObjectType == 4 || audioObjectType == 6 || audioObjectType == 7) { |
| | 142 | readGASpecificConfig(audioObjectType, b, &o); |
| | 143 | } else if (audioObjectType == 5) { |
| | 144 | int sbr_present = 1; |
| | 145 | samplingFrequencyIndex = get_bits(b, 4); |
| | 146 | if (samplingFrequencyIndex == 0x0f) { |
| | 147 | uint32_t f = get_bits_long(b, 24); |
| | 148 | put_bits(&o, 24, f); |
| | 149 | } |
| | 150 | audioObjectType = get_bits(b, 5); |
| | 151 | put_bits(&o, 5, audioObjectType); |
| | 152 | } else if (audioObjectType >= 17) { |
| | 153 | int epConfig; |
| | 154 | readGASpecificConfig(audioObjectType, b, &o); |
| | 155 | epConfig = get_bits(b, 2); |
| | 156 | put_bits(&o, 2, epConfig); |
| | 157 | } |
| | 158 | |
| | 159 | // count the extradata |
| | 160 | ret = put_bits_count(&o); |
| | 161 | decoder->extrasize = (ret + 7) / 8; |
| | 162 | |
| | 163 | flush_put_bits(&o); |
| | 164 | return ret; |
| | 165 | } |
| | 166 | |
| | 167 | static void readStreamMuxConfig(struct AACDecoder *parser, GetBitContext *b) |
| | 168 | { |
| | 169 | int audio_mux_version = get_bits(b, 1); |
| | 170 | parser->audio_mux_version_A = 0; |
| | 171 | if (audio_mux_version == 1) { // audioMuxVersion |
| | 172 | parser->audio_mux_version_A = get_bits(b, 1); |
| | 173 | } |
| | 174 | |
| | 175 | if (parser->audio_mux_version_A == 0) { |
| | 176 | int frame_length_type; |
| | 177 | |
| | 178 | if (audio_mux_version == 1) { |
| | 179 | // taraFullness |
| | 180 | latm_get_value(b); |
| | 181 | } |
| | 182 | get_bits(b, 1); // allStreamSameTimeFraming = 1 |
| | 183 | get_bits(b, 6); // numSubFrames = 0 |
| | 184 | get_bits(b, 4); // numPrograms = 0 |
| | 185 | |
| | 186 | // for each program (which there is only on in DVB) |
| | 187 | get_bits(b, 3); // numLayer = 0 |
| | 188 | |
| | 189 | // for each layer (which there is only on in DVB) |
| | 190 | if (audio_mux_version == 0) { |
| | 191 | readAudioSpecificConfig(parser, b); |
| | 192 | } else { |
| | 193 | int ascLen = latm_get_value(b); |
| | 194 | ascLen -= readAudioSpecificConfig(parser, b); |
| | 195 | |
| | 196 | // skip left over bits |
| | 197 | while (ascLen > 16) { |
| | 198 | skip_bits(b, 16); |
| | 199 | ascLen -= 16; |
| | 200 | } |
| | 201 | skip_bits(b, ascLen); |
| | 202 | } |
| | 203 | |
| | 204 | // these are not needed... perhaps |
| | 205 | frame_length_type = get_bits(b, 3); |
| | 206 | parser->frameLengthType = frame_length_type; |
| | 207 | if (frame_length_type == 0) { |
| | 208 | get_bits(b, 8); |
| | 209 | } else if (frame_length_type == 1) { |
| | 210 | get_bits(b, 9); |
| | 211 | } else if (frame_length_type == 3 || frame_length_type == 4 || frame_length_type == 5) { |
| | 212 | // celp_table_index |
| | 213 | get_bits(b, 6); |
| | 214 | } else if (frame_length_type == 6 || frame_length_type == 7) { |
| | 215 | // hvxc_table_index |
| | 216 | get_bits(b, 1); |
| | 217 | } |
| | 218 | |
| | 219 | // other data |
| | 220 | if (get_bits(b, 1)) { |
| | 221 | // other data present |
| | 222 | if (audio_mux_version == 1) { |
| | 223 | // other_data_bits |
| | 224 | latm_get_value(b); |
| | 225 | } else { |
| | 226 | int esc, tmp; |
| | 227 | // other data bits |
| | 228 | int64_t other_data_bits = 0; |
| | 229 | do { |
| | 230 | esc = get_bits(b, 1); |
| | 231 | tmp = get_bits(b, 8); |
| | 232 | other_data_bits = other_data_bits << 8 | tmp; |
| | 233 | } while (esc); |
| | 234 | } |
| | 235 | } |
| | 236 | |
| | 237 | // CRC if necessary |
| | 238 | if (get_bits(b, 1)) { |
| | 239 | // config_crc |
| | 240 | get_bits(b, 8); |
| | 241 | } |
| | 242 | } else { |
| | 243 | // TBD |
| | 244 | } |
| | 245 | } |
| | 246 | |
| | 247 | static int readPayloadLengthInfo(struct AACDecoder *parser, GetBitContext *b) |
| | 248 | { |
| | 249 | if (parser->frameLengthType == 0) { |
| | 250 | uint8_t tmp; |
| | 251 | int muxSlotLengthBytes = 0; |
| | 252 | do { |
| | 253 | tmp = get_bits(b, 8); |
| | 254 | muxSlotLengthBytes += tmp; |
| | 255 | } while (tmp == 255); |
| | 256 | return muxSlotLengthBytes; |
| | 257 | } else { |
| | 258 | if (parser->frameLengthType == 3 || |
| | 259 | parser->frameLengthType == 5 || |
| | 260 | parser->frameLengthType == 7) { |
| | 261 | get_bits(b, 2); |
| | 262 | } |
| | 263 | return 0; |
| | 264 | } |
| | 265 | } |
| | 266 | |
| | 267 | static void readAudioMuxElement(struct AACDecoder *parser, GetBitContext *b, uint8_t *payload, int *payloadsize) |
| | 268 | { |
| | 269 | uint8_t use_same_mux = get_bits(b, 1); |
| | 270 | if (!use_same_mux) { |
| | 271 | readStreamMuxConfig(parser, b); |
| | 272 | } |
| | 273 | if (parser->audio_mux_version_A == 0) { |
| | 274 | int j; |
| | 275 | int muxSlotLengthBytes = readPayloadLengthInfo(parser, b); |
| | 276 | muxSlotLengthBytes = FFMIN(muxSlotLengthBytes, *payloadsize); |
| | 277 | for (j=0; j<muxSlotLengthBytes; j++) { |
| | 278 | *payload++ = get_bits(b, 8); |
| | 279 | } |
| | 280 | *payloadsize = muxSlotLengthBytes; |
| | 281 | } |
| | 282 | } |
| | 283 | |
| | 284 | static int readAudioSyncStream(struct AACDecoder *parser, GetBitContext *b, int size, uint8_t *payload, int *payloadsize) |
| | 285 | { |
| | 286 | int muxlength; |
| | 287 | |
| | 288 | if (get_bits(b, 11) != SYNC_LATM) return -1; // not LATM |
| | 289 | |
| | 290 | muxlength = get_bits(b, 13); |
| | 291 | if (muxlength+3 > size) return -1; // not enough data, the parser should have sorted this |
| | 292 | |
| | 293 | readAudioMuxElement(parser, b, payload, payloadsize); |
| | 294 | |
| | 295 | return 0; |
| | 296 | } |
| | 297 | |
| | 298 | static void channel_setup(AVCodecContext *avctx) |
| | 299 | { |
| | 300 | AACDecoder *decoder = avctx->priv_data; |
| | 301 | |
| | 302 | if (avctx->request_channels > 0 && avctx->request_channels == 2 && |
| | 303 | avctx->request_channels < avctx->channels) { |
| | 304 | NeAACDecConfigurationPtr faac_cfg; |
| | 305 | avctx->channels = 2; |
| | 306 | faac_cfg = NeAACDecGetCurrentConfiguration(decoder->aac_decoder); |
| | 307 | faac_cfg->downMatrix = 1; |
| | 308 | faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate; |
| | 309 | NeAACDecSetConfiguration(decoder->aac_decoder, faac_cfg); |
| | 310 | } |
| | 311 | } |
| | 312 | |
| | 313 | static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size, AVPacket *avpkt) |
| | 314 | { |
| | 315 | AACDecoder *decoder = avctx->priv_data; |
| | 316 | uint8_t tempbuf[MAX_SIZE]; |
| | 317 | int bufsize = MAX_SIZE; |
| | 318 | int max_size = *out_size; |
| | 319 | NeAACDecFrameInfo info; |
| | 320 | GetBitContext b; |
| | 321 | |
| | 322 | if(avpkt->size == 0) |
| | 323 | return 0; |
| | 324 | |
| | 325 | init_get_bits(&b, avpkt->data, avpkt->size * 8); |
| | 326 | if (readAudioSyncStream(decoder, &b, avpkt->size, tempbuf, &bufsize)) { |
| | 327 | return -1; |
| | 328 | } |
| | 329 | |
| | 330 | if (!decoder->initialized) { |
| | 331 | // we are going to initialize from decoder specific info when available |
| | 332 | if (decoder->extrasize > 0) { |
| | 333 | uint32_t sample_rate; |
| | 334 | uint8_t channels; |
| | 335 | if (NeAACDecInit2(decoder->aac_decoder, decoder->extra, decoder->extrasize, &sample_rate, &channels)) { |
| | 336 | return -1; |
| | 337 | } |
| | 338 | avctx->sample_rate = sample_rate; |
| | 339 | avctx->channels = channels; |
| | 340 | channel_setup(avctx); |
| | 341 | decoder->initialized = 1; |
| | 342 | } else { |
| | 343 | *out_size = 0; |
| | 344 | return avpkt->size; |
| | 345 | } |
| | 346 | } |
| | 347 | |
| | 348 | NeAACDecDecode2(decoder->aac_decoder, &info, tempbuf, bufsize, &out, max_size); |
| | 349 | if (info.error > 0) { |
| | 350 | av_log(avctx, AV_LOG_ERROR, "libfaad: frame decoding failed: %s\n", |
| | 351 | NeAACDecGetErrorMessage(info.error)); |
| | 352 | return -1; |
| | 353 | } |
| | 354 | if (!avctx->frame_size) |
| | 355 | avctx->frame_size = info.samples/avctx->channels; |
| | 356 | |
| | 357 | if (out_size) |
| | 358 | *out_size = info.samples * sizeof(short); |
| | 359 | |
| | 360 | return avpkt->size; |
| | 361 | } |
| | 362 | |
| | 363 | static int latm_decode_init(AVCodecContext *avctx) |
| | 364 | { |
| | 365 | AACDecoder *decoder = avctx->priv_data; |
| | 366 | NeAACDecConfigurationPtr faac_cfg; |
| | 367 | |
| | 368 | avctx->bit_rate = 0; |
| | 369 | avctx->sample_fmt = SAMPLE_FMT_S16; |
| | 370 | |
| | 371 | decoder->aac_decoder = NeAACDecOpen(); |
| | 372 | if (!decoder->aac_decoder) { |
| | 373 | return -1; |
| | 374 | } |
| | 375 | |
| | 376 | faac_cfg = NeAACDecGetCurrentConfiguration(decoder->aac_decoder); |
| | 377 | if (faac_cfg) { |
| | 378 | faac_cfg->outputFormat = FAAD_FMT_16BIT; |
| | 379 | faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate; |
| | 380 | faac_cfg->defObjectType = LC; |
| | 381 | NeAACDecSetConfiguration(decoder->aac_decoder, faac_cfg); |
| | 382 | } |
| | 383 | |
| | 384 | decoder->initialized = 0; |
| | 385 | return 0; |
| | 386 | } |
| | 387 | |
| | 388 | static int latm_decode_end(AVCodecContext *avctx) |
| | 389 | { |
| | 390 | AACDecoder *decoder = avctx->priv_data; |
| | 391 | NeAACDecClose(decoder->aac_decoder); |
| | 392 | return 0; |
| | 393 | } |
| | 394 | |
| | 395 | AVCodec libfaad_latm_decoder = { |
| | 396 | .name = "libfaad_latm", |
| | 397 | .type = CODEC_TYPE_AUDIO, |
| | 398 | .id = CODEC_ID_AAC_LATM, |
| | 399 | .priv_data_size = sizeof (AACDecoder), |
| | 400 | .init = latm_decode_init, |
| | 401 | .close = latm_decode_end, |
| | 402 | .decode = latm_decode_frame, |
| | 403 | .long_name = NULL_IF_CONFIG_SMALL("libfaad AAC LATM (Advanced Audio Codec)"), |
| | 404 | }; |