| | 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 "bitstream.h" |
| | 34 | #include "mpeg4audio.h" |
| | 35 | #include "neaacdec.h" |
| | 36 | |
| | 37 | #define min(a,b) ((a)<(b) ? (a) : (b)) |
| | 38 | |
| | 39 | |
| | 40 | /* |
| | 41 | Note: This decoder filter is intended to decode LATM streams transferred |
| | 42 | in MPEG transport streams which are only supposed to contain one program. |
| | 43 | To do a more complex LATM demuxing a separate LATM demuxer should be used. |
| | 44 | */ |
| | 45 | |
| | 46 | #define AAC_NONE 0 // mode not detected (or indicated in mediatype) |
| | 47 | #define AAC_LATM 1 // LATM packets (ISO/IEC 14496-3 1.7.3 Multiplex layer) |
| | 48 | |
| | 49 | #define SYNC_LATM 0x2b7 // 11 bits |
| | 50 | |
| | 51 | #define MAX_SIZE 8*1024 |
| | 52 | |
| | 53 | typedef struct AACConfig |
| | 54 | { |
| | 55 | uint8_t extra[64]; // should be way enough |
| | 56 | int extrasize; |
| | 57 | |
| | 58 | int audioObjectType; |
| | 59 | int samplingFrequencyIndex; |
| | 60 | int samplingFrequency; |
| | 61 | int channelConfiguration; |
| | 62 | int channels; |
| | 63 | } AACConfig; |
| | 64 | |
| | 65 | typedef struct AACParser |
| | 66 | { |
| | 67 | AACConfig config; |
| | 68 | uint8_t frameLengthType; |
| | 69 | uint16_t muxSlotLengthBytes; |
| | 70 | |
| | 71 | uint8_t audio_mux_version; |
| | 72 | uint8_t audio_mux_version_A; |
| | 73 | int taraFullness; |
| | 74 | uint8_t config_crc; |
| | 75 | int64_t other_data_bits; |
| | 76 | |
| | 77 | int mode; |
| | 78 | int offset; // byte offset in "buf" buffer |
| | 79 | uint8_t buf[MAX_SIZE]; // allocated buffer |
| | 80 | int count; // number of bytes written in buffer |
| | 81 | } AACParser; |
| | 82 | |
| | 83 | typedef struct AACDecoder |
| | 84 | { |
| | 85 | AACParser *parser; |
| | 86 | faacDecHandle aac_decoder; |
| | 87 | int open; |
| | 88 | uint32_t in_samplerate; |
| | 89 | uint8_t in_channels; |
| | 90 | } AACDecoder; |
| | 91 | |
| | 92 | typedef struct { |
| | 93 | AACDecoder* decoder; |
| | 94 | } FAACContext; |
| | 95 | |
| | 96 | static inline int64_t latm_get_value(GetBitContext *b) |
| | 97 | { |
| | 98 | uint8_t bytesForValue = get_bits(b, 2); |
| | 99 | int64_t value = 0; |
| | 100 | int i; |
| | 101 | for (i=0; i<=bytesForValue; i++) { |
| | 102 | value <<= 8; |
| | 103 | value |= get_bits(b, 8); |
| | 104 | } |
| | 105 | return value; |
| | 106 | } |
| | 107 | |
| | 108 | static void readGASpecificConfig(struct AACConfig *cfg, GetBitContext *b, PutBitContext *o) |
| | 109 | { |
| | 110 | int framelen_flag = get_bits(b, 1); |
| | 111 | put_bits(o, 1, framelen_flag); |
| | 112 | int dependsOnCoder = get_bits(b, 1); |
| | 113 | put_bits(o, 1, dependsOnCoder); |
| | 114 | int ext_flag; |
| | 115 | int delay; |
| | 116 | int layerNr; |
| | 117 | |
| | 118 | if (dependsOnCoder) { |
| | 119 | delay = get_bits(b, 14); |
| | 120 | put_bits(o, 14, delay); |
| | 121 | } |
| | 122 | ext_flag = get_bits(b, 1); |
| | 123 | put_bits(o, 1, ext_flag); |
| | 124 | if (!cfg->channelConfiguration) { |
| | 125 | // program config element |
| | 126 | // TODO: |
| | 127 | } |
| | 128 | |
| | 129 | if (cfg->audioObjectType == 6 || cfg->audioObjectType == 20) { |
| | 130 | layerNr = get_bits(b, 3); |
| | 131 | put_bits(o, 3, layerNr); |
| | 132 | } |
| | 133 | if (ext_flag) { |
| | 134 | if (cfg->audioObjectType == 22) { |
| | 135 | skip_bits(b, 5); // numOfSubFrame |
| | 136 | skip_bits(b, 11); // layer_length |
| | 137 | |
| | 138 | put_bits(o, 16, 0); |
| | 139 | } |
| | 140 | if (cfg->audioObjectType == 17 || |
| | 141 | cfg->audioObjectType == 19 || |
| | 142 | cfg->audioObjectType == 20 || |
| | 143 | cfg->audioObjectType == 23) { |
| | 144 | |
| | 145 | skip_bits(b, 3); // stuff |
| | 146 | put_bits(o, 3, 0); |
| | 147 | } |
| | 148 | |
| | 149 | skip_bits(b, 1); // extflag3 |
| | 150 | put_bits(o, 1, 0); |
| | 151 | } |
| | 152 | } |
| | 153 | |
| | 154 | static int readAudioSpecificConfig(struct AACConfig *cfg, GetBitContext *b) |
| | 155 | { |
| | 156 | PutBitContext o; |
| | 157 | init_put_bits(&o, cfg->extra, sizeof(cfg->extra)); |
| | 158 | |
| | 159 | // returns the number of bits read |
| | 160 | int ret = 0; |
| | 161 | int sbr_present = -1; |
| | 162 | |
| | 163 | // object |
| | 164 | cfg->audioObjectType = get_bits(b, 5); |
| | 165 | put_bits(&o, 5, cfg->audioObjectType); |
| | 166 | if (cfg->audioObjectType == 31) { |
| | 167 | uint8_t n = get_bits(b, 6); |
| | 168 | put_bits(&o, 6, n); |
| | 169 | cfg->audioObjectType = 32 + n; |
| | 170 | } |
| | 171 | |
| | 172 | cfg->samplingFrequencyIndex = get_bits(b, 4); |
| | 173 | cfg->samplingFrequency = ff_mpeg4audio_sample_rates[cfg->samplingFrequencyIndex]; |
| | 174 | put_bits(&o, 4, cfg->samplingFrequencyIndex); |
| | 175 | if (cfg->samplingFrequencyIndex == 0x0f) { |
| | 176 | uint32_t f = get_bits_long(b, 24); |
| | 177 | put_bits(&o, 24, f); |
| | 178 | cfg->samplingFrequency = f; |
| | 179 | } |
| | 180 | cfg->channelConfiguration = get_bits(b, 4); |
| | 181 | put_bits(&o, 4, cfg->channelConfiguration); |
| | 182 | cfg->channels = ff_mpeg4audio_channels[cfg->channelConfiguration]; |
| | 183 | |
| | 184 | if (cfg->audioObjectType == 5) { |
| | 185 | sbr_present = 1; |
| | 186 | |
| | 187 | // TODO: parsing !!!!!!!!!!!!!!!! |
| | 188 | } |
| | 189 | |
| | 190 | switch (cfg->audioObjectType) { |
| | 191 | case 1: |
| | 192 | case 2: |
| | 193 | case 3: |
| | 194 | case 4: |
| | 195 | case 6: |
| | 196 | case 7: |
| | 197 | case 17: |
| | 198 | case 19: |
| | 199 | case 20: |
| | 200 | case 21: |
| | 201 | case 22: |
| | 202 | case 23: |
| | 203 | readGASpecificConfig(cfg, b, &o); |
| | 204 | break; |
| | 205 | } |
| | 206 | |
| | 207 | if (sbr_present == -1) { |
| | 208 | if (cfg->samplingFrequency <= 24000) { |
| | 209 | cfg->samplingFrequency *= 2; |
| | 210 | } |
| | 211 | } |
| | 212 | |
| | 213 | // count the extradata |
| | 214 | ret = put_bits_count(&o); |
| | 215 | align_put_bits(&o); |
| | 216 | flush_put_bits(&o); |
| | 217 | cfg->extrasize = (ret + 7) >> 3; |
| | 218 | return ret; |
| | 219 | } |
| | 220 | |
| | 221 | static void readStreamMuxConfig(struct AACParser *parser, GetBitContext *b) |
| | 222 | { |
| | 223 | parser->audio_mux_version_A = 0; |
| | 224 | parser->audio_mux_version = get_bits(b, 1); |
| | 225 | if (parser->audio_mux_version == 1) { // audioMuxVersion |
| | 226 | parser->audio_mux_version_A = get_bits(b, 1); |
| | 227 | } |
| | 228 | |
| | 229 | if (parser->audio_mux_version_A == 0) { |
| | 230 | if (parser->audio_mux_version == 1) { |
| | 231 | parser->taraFullness = latm_get_value(b); |
| | 232 | } |
| | 233 | get_bits(b, 1); // allStreamSameTimeFraming = 1 |
| | 234 | get_bits(b, 6); // numSubFrames = 0 |
| | 235 | get_bits(b, 4); // numPrograms = 0 |
| | 236 | |
| | 237 | // for each program |
| | 238 | get_bits(b, 3); // numLayer = 0 |
| | 239 | |
| | 240 | // for each layer |
| | 241 | if (parser->audio_mux_version == 0) { |
| | 242 | // audio specific config. |
| | 243 | readAudioSpecificConfig(&parser->config, b); |
| | 244 | } else { |
| | 245 | int ascLen = latm_get_value(b); |
| | 246 | ascLen -= readAudioSpecificConfig(&parser->config, b); |
| | 247 | |
| | 248 | // fill bits |
| | 249 | while (ascLen > 16) { |
| | 250 | skip_bits(b, 16); |
| | 251 | ascLen -= 16; |
| | 252 | } |
| | 253 | skip_bits(b, ascLen); |
| | 254 | } |
| | 255 | |
| | 256 | // these are not needed... perhaps |
| | 257 | int frame_length_type = get_bits(b, 3); |
| | 258 | parser->frameLengthType = frame_length_type; |
| | 259 | if (frame_length_type == 0) { |
| | 260 | get_bits(b, 8); |
| | 261 | } else if (frame_length_type == 1) { |
| | 262 | get_bits(b, 9); |
| | 263 | } else if (frame_length_type == 3 || |
| | 264 | frame_length_type == 4 || |
| | 265 | frame_length_type == 5) { |
| | 266 | int celp_table_index = get_bits(b, 6); |
| | 267 | } else if (frame_length_type == 6 || |
| | 268 | frame_length_type == 7) { |
| | 269 | int hvxc_table_index = get_bits(b, 1); |
| | 270 | } |
| | 271 | |
| | 272 | // other data |
| | 273 | parser->other_data_bits = 0; |
| | 274 | if (get_bits(b, 1)) { |
| | 275 | // other data present |
| | 276 | if (parser->audio_mux_version == 1) { |
| | 277 | parser->other_data_bits = latm_get_value(b); |
| | 278 | } else { |
| | 279 | // other data not present |
| | 280 | parser->other_data_bits = 0; |
| | 281 | int esc, tmp; |
| | 282 | do { |
| | 283 | parser->other_data_bits <<= 8; |
| | 284 | esc = get_bits(b, 1); |
| | 285 | tmp = get_bits(b, 8); |
| | 286 | parser->other_data_bits |= tmp; |
| | 287 | } while (esc); |
| | 288 | } |
| | 289 | } |
| | 290 | |
| | 291 | // CRC |
| | 292 | if (get_bits(b, 1)) { |
| | 293 | parser->config_crc = get_bits(b, 8); |
| | 294 | } |
| | 295 | } else { |
| | 296 | // tbd |
| | 297 | } |
| | 298 | } |
| | 299 | |
| | 300 | static void readPayloadLengthInfo(struct AACParser *parser, GetBitContext *b) |
| | 301 | { |
| | 302 | uint8_t tmp; |
| | 303 | if (parser->frameLengthType == 0) { |
| | 304 | parser->muxSlotLengthBytes = 0; |
| | 305 | do { |
| | 306 | tmp = get_bits(b, 8); |
| | 307 | parser->muxSlotLengthBytes += tmp; |
| | 308 | } while (tmp == 255); |
| | 309 | } else { |
| | 310 | if (parser->frameLengthType == 5 || |
| | 311 | parser->frameLengthType == 7 || |
| | 312 | parser->frameLengthType == 3) { |
| | 313 | get_bits(b, 2); |
| | 314 | } |
| | 315 | } |
| | 316 | } |
| | 317 | |
| | 318 | static void readAudioMuxElement(struct AACParser *parser, GetBitContext *b, uint8_t *payload, int *payloadsize) |
| | 319 | { |
| | 320 | uint8_t use_same_mux = get_bits(b, 1); |
| | 321 | if (!use_same_mux) { |
| | 322 | readStreamMuxConfig(parser, b); |
| | 323 | } |
| | 324 | |
| | 325 | if (parser->audio_mux_version_A == 0) { |
| | 326 | int j; |
| | 327 | |
| | 328 | readPayloadLengthInfo(parser, b); |
| | 329 | |
| | 330 | // copy data |
| | 331 | for (j=0; j<parser->muxSlotLengthBytes; j++) { |
| | 332 | *payload++ = get_bits(b, 8); |
| | 333 | } |
| | 334 | *payloadsize = parser->muxSlotLengthBytes; |
| | 335 | |
| | 336 | // ignore otherdata |
| | 337 | } else { |
| | 338 | // TBD |
| | 339 | } |
| | 340 | } |
| | 341 | |
| | 342 | static int readAudioSyncStream(struct AACParser *parser, GetBitContext *b, int size, uint8_t *payload, int *payloadsize) |
| | 343 | { |
| | 344 | // ISO/IEC 14496-3 Table 1.28 - Syntax of AudioMuxElement() |
| | 345 | if (get_bits(b, 11) != 0x2b7) return -1; // not LATM |
| | 346 | int muxlength = get_bits(b, 13); |
| | 347 | |
| | 348 | if (3+muxlength > size) return 0; // not enough data |
| | 349 | |
| | 350 | readAudioMuxElement(parser, b, payload, payloadsize); |
| | 351 | |
| | 352 | // we don't parse anything else here... |
| | 353 | return (3+muxlength); |
| | 354 | } |
| | 355 | |
| | 356 | |
| | 357 | static void flush_buf(struct AACParser *parser, int offset) { |
| | 358 | int bytes_to_flush = min(parser->count, offset); |
| | 359 | int left = (parser->count - bytes_to_flush); |
| | 360 | |
| | 361 | if (bytes_to_flush > 0) { |
| | 362 | if (left > 0) { |
| | 363 | memcpy(parser->buf, parser->buf+bytes_to_flush, left); |
| | 364 | parser->count = left; |
| | 365 | } else { |
| | 366 | parser->count = 0; |
| | 367 | } |
| | 368 | } |
| | 369 | } |
| | 370 | |
| | 371 | static struct AACParser *latm_create_parser() |
| | 372 | { |
| | 373 | struct AACParser *parser = (struct AACParser *)av_malloc(sizeof(struct AACParser)); |
| | 374 | memset(parser, 0, sizeof(struct AACParser)); |
| | 375 | return parser; |
| | 376 | } |
| | 377 | |
| | 378 | static void latm_destroy_parser(struct AACParser *parser) |
| | 379 | { |
| | 380 | av_free(parser); |
| | 381 | } |
| | 382 | |
| | 383 | static void latm_flush(struct AACParser *parser) |
| | 384 | { |
| | 385 | parser->offset = 0; |
| | 386 | parser->count = 0; |
| | 387 | } |
| | 388 | |
| | 389 | static void latm_write_data(struct AACParser *parser, uint8_t *data, int len) |
| | 390 | { |
| | 391 | // buffer overflow check... just ignore the data before |
| | 392 | if (parser->count + len > MAX_SIZE) { |
| | 393 | flush_buf(parser, parser->offset); |
| | 394 | parser->offset = 0; |
| | 395 | if (parser->count + len > MAX_SIZE) { |
| | 396 | int to_flush = (parser->count+len) - MAX_SIZE; |
| | 397 | flush_buf(parser, to_flush); |
| | 398 | } |
| | 399 | } |
| | 400 | |
| | 401 | // append data |
| | 402 | memcpy(parser->buf+parser->count, data, len); |
| | 403 | parser->count += len; |
| | 404 | } |
| | 405 | |
| | 406 | static int latm_parse_packet(struct AACParser *parser, uint8_t *data, int maxsize) |
| | 407 | { |
| | 408 | /* |
| | 409 | Return value is either number of bytes parsed or |
| | 410 | -1 when failed. |
| | 411 | 0 = need more data. |
| | 412 | */ |
| | 413 | |
| | 414 | uint8_t *start = parser->buf + parser->offset; |
| | 415 | int bytes = parser->count - parser->offset; |
| | 416 | GetBitContext b; |
| | 417 | init_get_bits(&b, start, bytes * 8); |
| | 418 | |
| | 419 | if (parser->mode == AAC_LATM) { |
| | 420 | int outsize = 0; |
| | 421 | int ret = readAudioSyncStream(parser, &b, bytes, data, &outsize); |
| | 422 | |
| | 423 | if (ret < 0) return -1; |
| | 424 | if (ret == 0) return 0; |
| | 425 | |
| | 426 | // update the offset |
| | 427 | parser->offset += ret; |
| | 428 | return outsize; |
| | 429 | } |
| | 430 | |
| | 431 | // check for syncwords |
| | 432 | while (bytes > 2) { |
| | 433 | if (show_bits(&b, 11) == SYNC_LATM) { |
| | 434 | // we must parse config first... |
| | 435 | int outsize = 0; |
| | 436 | |
| | 437 | // check if there is a complete packet available... |
| | 438 | int ret = readAudioSyncStream(parser, &b, bytes, data, &outsize); |
| | 439 | if (ret < 0) return -1; |
| | 440 | if (ret == 0) return 0; |
| | 441 | parser->offset += ret; |
| | 442 | |
| | 443 | parser->mode = AAC_LATM; |
| | 444 | return outsize; |
| | 445 | } |
| | 446 | skip_bits(&b, 8); |
| | 447 | parser->offset++; |
| | 448 | bytes--; |
| | 449 | } |
| | 450 | return 0; |
| | 451 | } |
| | 452 | |
| | 453 | static void aac_filter_close(AACDecoder *decoder) |
| | 454 | { |
| | 455 | if (decoder->aac_decoder) { |
| | 456 | NeAACDecClose(decoder->aac_decoder); |
| | 457 | decoder->aac_decoder = NULL; |
| | 458 | } |
| | 459 | decoder->open = 0; |
| | 460 | } |
| | 461 | |
| | 462 | static int aac_decoder_open(AACDecoder *decoder) |
| | 463 | { |
| | 464 | if (decoder->aac_decoder) return 0; |
| | 465 | |
| | 466 | decoder->aac_decoder = NeAACDecOpen(); |
| | 467 | if (!decoder->aac_decoder) return -1; |
| | 468 | |
| | 469 | // are we going to initialize from decoder specific info ? |
| | 470 | if (decoder->parser->config.extrasize > 0) { |
| | 471 | char ret = NeAACDecInit2(decoder->aac_decoder, (unsigned char*)decoder->parser->config.extra, decoder->parser->config.extrasize, &decoder->in_samplerate, &decoder->in_channels); |
| | 472 | if (ret < 0) { |
| | 473 | aac_filter_close(decoder); // gone wrong ? |
| | 474 | return -1; |
| | 475 | } |
| | 476 | decoder->open = 1; |
| | 477 | } else { |
| | 478 | // we'll open the decoder later... |
| | 479 | decoder->open = 0; |
| | 480 | } |
| | 481 | return 0; |
| | 482 | } |
| | 483 | |
| | 484 | AACDecoder *aac_filter_create() |
| | 485 | { |
| | 486 | AACDecoder *decoder = (AACDecoder *)av_malloc(sizeof(AACDecoder)); |
| | 487 | decoder->parser = latm_create_parser(); |
| | 488 | decoder->aac_decoder = NULL; |
| | 489 | decoder->open = 0; |
| | 490 | return (void *)decoder; |
| | 491 | } |
| | 492 | |
| | 493 | void aac_filter_destroy(AACDecoder *decoder) |
| | 494 | { |
| | 495 | aac_filter_close(decoder); |
| | 496 | latm_destroy_parser(decoder->parser); |
| | 497 | av_free(decoder); |
| | 498 | } |
| | 499 | |
| | 500 | int aac_filter_receive(AACDecoder *decoder, void *out, int *out_size, uint8_t *data, int size) |
| | 501 | { |
| | 502 | uint8_t tempbuf[32*1024]; |
| | 503 | int ret; |
| | 504 | int consumed = size; |
| | 505 | int decoded; |
| | 506 | int max_size = *out_size; |
| | 507 | |
| | 508 | *out_size = 0; |
| | 509 | |
| | 510 | //------------------------------------------------------------------------- |
| | 511 | // Multiplex Parsing |
| | 512 | //------------------------------------------------------------------------- |
| | 513 | |
| | 514 | latm_write_data(decoder->parser, data, size); |
| | 515 | |
| | 516 | do { |
| | 517 | ret = latm_parse_packet(decoder->parser, tempbuf, sizeof(tempbuf)); |
| | 518 | if (ret < 0) { |
| | 519 | latm_flush(decoder->parser); |
| | 520 | return consumed; |
| | 521 | } |
| | 522 | if (ret == 0) return consumed; |
| | 523 | |
| | 524 | data = tempbuf; |
| | 525 | size = ret; |
| | 526 | |
| | 527 | //------------------------------------------------------------------------- |
| | 528 | // Initialize decoder (if necessary) |
| | 529 | //------------------------------------------------------------------------- |
| | 530 | if (!decoder->open) { |
| | 531 | aac_filter_close(decoder); |
| | 532 | if (decoder->parser->mode == AAC_LATM) { |
| | 533 | ret = aac_decoder_open(decoder); |
| | 534 | if (ret < 0) return consumed; |
| | 535 | } |
| | 536 | |
| | 537 | if(!decoder->open) return consumed; |
| | 538 | } |
| | 539 | |
| | 540 | //------------------------------------------------------------------------- |
| | 541 | // Decode samples |
| | 542 | //------------------------------------------------------------------------- |
| | 543 | NeAACDecFrameInfo info; |
| | 544 | void *buf = NeAACDecDecode(decoder->aac_decoder, &info, data, size); |
| | 545 | |
| | 546 | if (buf) { |
| | 547 | decoder->in_samplerate = info.samplerate; |
| | 548 | decoder->in_channels = info.channels; |
| | 549 | |
| | 550 | //--------------------------------------------------------------------- |
| | 551 | // Deliver decoded samples |
| | 552 | //--------------------------------------------------------------------- |
| | 553 | |
| | 554 | // kram dekoduje 16-bit. my vypustame 16-bit. takze by to malo byt okej |
| | 555 | decoded = info.samples * sizeof(short); |
| | 556 | |
| | 557 | // napraskame tam sample |
| | 558 | *out_size += decoded; |
| | 559 | if(*out_size > max_size) { |
| | 560 | av_log(NULL, AV_LOG_ERROR, "overflow!\n"); |
| | 561 | } else { |
| | 562 | memcpy(out, buf, decoded); |
| | 563 | out = (unsigned char *)out + decoded; |
| | 564 | } |
| | 565 | } else { |
| | 566 | // need more data |
| | 567 | break; |
| | 568 | } |
| | 569 | |
| | 570 | } while (1); // decode all packets |
| | 571 | return consumed; |
| | 572 | } |
| | 573 | |
| | 574 | void aac_filter_getinfo(AACDecoder *decoder, int *sample_rate, int *channels) |
| | 575 | { |
| | 576 | if(!decoder->open) return; |
| | 577 | *sample_rate = decoder->in_samplerate; |
| | 578 | *channels = decoder->in_channels; |
| | 579 | } |
| | 580 | |
| | 581 | static int faac_decode_init(AVCodecContext *avctx) |
| | 582 | { |
| | 583 | FAACContext *s = avctx->priv_data; |
| | 584 | avctx->frame_size = 360; |
| | 585 | avctx->sample_rate = 48000; |
| | 586 | avctx->channels = 2; |
| | 587 | avctx->bit_rate = 8192 * 8 * avctx->sample_rate / avctx->frame_size; |
| | 588 | s->decoder = aac_filter_create(); |
| | 589 | return 0; |
| | 590 | } |
| | 591 | |
| | 592 | static int faac_decode_frame(AVCodecContext *avctx, |
| | 593 | void *data, int *data_size, |
| | 594 | uint8_t *buf, int buf_size) |
| | 595 | { |
| | 596 | FAACContext *s = avctx->priv_data; |
| | 597 | int ret; |
| | 598 | |
| | 599 | if (s->decoder == NULL) faac_decode_init(avctx); |
| | 600 | ret = aac_filter_receive(s->decoder, data, data_size, buf, buf_size); |
| | 601 | aac_filter_getinfo(s->decoder, &(avctx->sample_rate), &(avctx->channels)); |
| | 602 | return ret; |
| | 603 | } |
| | 604 | |
| | 605 | static int faac_decode_end(AVCodecContext *avctx) |
| | 606 | { |
| | 607 | FAACContext *s = avctx->priv_data; |
| | 608 | if(s->decoder != NULL) { |
| | 609 | aac_filter_destroy(s->decoder); |
| | 610 | } |
| | 611 | return 0; |
| | 612 | } |
| | 613 | |
| | 614 | |
| | 615 | |
| | 616 | #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits) |
| | 617 | #define LATM_MASK 0xFFE000 // top 11 bits |
| | 618 | #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits |
| | 619 | |
| | 620 | typedef struct LATMParseContext{ |
| | 621 | ParseContext pc; |
| | 622 | int count; |
| | 623 | } LATMParseContext; |
| | 624 | |
| | 625 | /** |
| | 626 | * finds the end of the current frame in the bitstream. |
| | 627 | * @return the position of the first byte of the next frame, or -1 |
| | 628 | */ |
| | 629 | static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf, |
| | 630 | int buf_size) { |
| | 631 | LATMParseContext *s = s1->priv_data; |
| | 632 | ParseContext *pc = &s->pc; |
| | 633 | int pic_found, i; |
| | 634 | uint32_t state; |
| | 635 | |
| | 636 | pic_found = pc->frame_start_found; |
| | 637 | state = pc->state; |
| | 638 | |
| | 639 | i = 0; |
| | 640 | if(!pic_found){ |
| | 641 | for(i=0; i<buf_size; i++){ |
| | 642 | state = (state<<8) | buf[i]; |
| | 643 | if((state & LATM_MASK) == LATM_HEADER){ |
| | 644 | i++; |
| | 645 | s->count = - i; |
| | 646 | pic_found=1; |
| | 647 | break; |
| | 648 | } |
| | 649 | } |
| | 650 | } |
| | 651 | |
| | 652 | if(pic_found){ |
| | 653 | /* EOF considered as end of frame */ |
| | 654 | if (buf_size == 0) |
| | 655 | return 0; |
| | 656 | if((state & LATM_SIZE_MASK) - s->count <= buf_size) { |
| | 657 | pc->frame_start_found = 0; |
| | 658 | pc->state = -1; |
| | 659 | return (state & LATM_SIZE_MASK) - s->count; |
| | 660 | } |
| | 661 | } |
| | 662 | |
| | 663 | s->count += buf_size; |
| | 664 | pc->frame_start_found = pic_found; |
| | 665 | pc->state = state; |
| | 666 | return END_NOT_FOUND; |
| | 667 | } |
| | 668 | |
| | 669 | static int latm_parse(AVCodecParserContext *s1, |
| | 670 | AVCodecContext *avctx, |
| | 671 | const uint8_t **poutbuf, int *poutbuf_size, |
| | 672 | const uint8_t *buf, int buf_size) |
| | 673 | { |
| | 674 | LATMParseContext *s = s1->priv_data; |
| | 675 | ParseContext *pc = &s->pc; |
| | 676 | int next; |
| | 677 | |
| | 678 | if(s1->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
| | 679 | next = buf_size; |
| | 680 | }else{ |
| | 681 | next = latm_find_frame_end(s1, buf, buf_size); |
| | 682 | |
| | 683 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
| | 684 | *poutbuf = NULL; |
| | 685 | *poutbuf_size = 0; |
| | 686 | return buf_size; |
| | 687 | } |
| | 688 | } |
| | 689 | *poutbuf = buf; |
| | 690 | *poutbuf_size = buf_size; |
| | 691 | return next; |
| | 692 | } |
| | 693 | |
| | 694 | static int latm_split(AVCodecContext *avctx, |
| | 695 | const uint8_t *buf, int buf_size) |
| | 696 | { |
| | 697 | int i; |
| | 698 | uint32_t state= -1; |
| | 699 | |
| | 700 | for(i=0; i<buf_size; i++){ |
| | 701 | state= (state<<8) | buf[i]; |
| | 702 | if((state & LATM_MASK) == LATM_HEADER) |
| | 703 | return i-2; |
| | 704 | } |
| | 705 | return 0; |
| | 706 | } |
| | 707 | |
| | 708 | AVCodec libfaad2_decoder = { |
| | 709 | .name = "AAC/LATM", |
| | 710 | .type = CODEC_TYPE_AUDIO, |
| | 711 | .id = CODEC_ID_AAC_LATM, |
| | 712 | .priv_data_size = sizeof (FAACContext), |
| | 713 | .init = faac_decode_init, |
| | 714 | .close = faac_decode_end, |
| | 715 | .decode = faac_decode_frame, |
| | 716 | .long_name = "AAC over LATM", |
| | 717 | }; |
| | 718 | |
| | 719 | AVCodecParser latm_parser = { |
| | 720 | { CODEC_ID_AAC_LATM }, |
| | 721 | sizeof(LATMParseContext), |
| | 722 | NULL, |
| | 723 | latm_parse, |
| | 724 | ff_parse_close, |
| | 725 | latm_split, |
| | 726 | }; |