00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #include "avcodec.h"
00084 #include "internal.h"
00085 #include "get_bits.h"
00086 #include "dsputil.h"
00087 #include "fft.h"
00088 #include "lpc.h"
00089
00090 #include "aac.h"
00091 #include "aactab.h"
00092 #include "aacdectab.h"
00093 #include "cbrt_tablegen.h"
00094 #include "sbr.h"
00095 #include "aacsbr.h"
00096 #include "mpeg4audio.h"
00097 #include "aacadtsdec.h"
00098
00099 #include <assert.h>
00100 #include <errno.h>
00101 #include <math.h>
00102 #include <string.h>
00103
00104 #if ARCH_ARM
00105 # include "arm/aac.h"
00106 #endif
00107
00108 union float754 {
00109 float f;
00110 uint32_t i;
00111 };
00112
00113 static VLC vlc_scalefactors;
00114 static VLC vlc_spectral[11];
00115
00116 static const char overread_err[] = "Input buffer exhausted before END element found\n";
00117
00118 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
00119 {
00120
00121 if (!ac->m4ac.chan_config) {
00122 return ac->tag_che_map[type][elem_id];
00123 }
00124
00125 switch (ac->m4ac.chan_config) {
00126 case 7:
00127 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00128 ac->tags_mapped++;
00129 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00130 }
00131 case 6:
00132
00133
00134
00135 if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00136 ac->tags_mapped++;
00137 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00138 }
00139 case 5:
00140 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00141 ac->tags_mapped++;
00142 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00143 }
00144 case 4:
00145 if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
00146 ac->tags_mapped++;
00147 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00148 }
00149 case 3:
00150 case 2:
00151 if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
00152 ac->tags_mapped++;
00153 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00154 } else if (ac->m4ac.chan_config == 2) {
00155 return NULL;
00156 }
00157 case 1:
00158 if (!ac->tags_mapped && type == TYPE_SCE) {
00159 ac->tags_mapped++;
00160 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00161 }
00162 default:
00163 return NULL;
00164 }
00165 }
00166
00179 static av_cold int che_configure(AACContext *ac,
00180 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00181 int type, int id,
00182 int *channels)
00183 {
00184 if (che_pos[type][id]) {
00185 if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
00186 return AVERROR(ENOMEM);
00187 ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
00188 if (type != TYPE_CCE) {
00189 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
00190 if (type == TYPE_CPE ||
00191 (type == TYPE_SCE && ac->m4ac.ps == 1)) {
00192 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
00193 }
00194 }
00195 } else {
00196 if (ac->che[type][id])
00197 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
00198 av_freep(&ac->che[type][id]);
00199 }
00200 return 0;
00201 }
00202
00211 static av_cold int output_configure(AACContext *ac,
00212 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00213 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00214 int channel_config, enum OCStatus oc_type)
00215 {
00216 AVCodecContext *avctx = ac->avctx;
00217 int i, type, channels = 0, ret;
00218
00219 if (new_che_pos != che_pos)
00220 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00221
00222 if (channel_config) {
00223 for (i = 0; i < tags_per_config[channel_config]; i++) {
00224 if ((ret = che_configure(ac, che_pos,
00225 aac_channel_layout_map[channel_config - 1][i][0],
00226 aac_channel_layout_map[channel_config - 1][i][1],
00227 &channels)))
00228 return ret;
00229 }
00230
00231 memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00232
00233 avctx->channel_layout = aac_channel_layout[channel_config - 1];
00234 } else {
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 for (i = 0; i < MAX_ELEM_ID; i++) {
00245 for (type = 0; type < 4; type++) {
00246 if ((ret = che_configure(ac, che_pos, type, i, &channels)))
00247 return ret;
00248 }
00249 }
00250
00251 memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00252
00253 avctx->channel_layout = 0;
00254 }
00255
00256 avctx->channels = channels;
00257
00258 ac->output_configured = oc_type;
00259
00260 return 0;
00261 }
00262
00270 static void decode_channel_map(enum ChannelPosition *cpe_map,
00271 enum ChannelPosition *sce_map,
00272 enum ChannelPosition type,
00273 GetBitContext *gb, int n)
00274 {
00275 while (n--) {
00276 enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map;
00277 map[get_bits(gb, 4)] = type;
00278 }
00279 }
00280
00288 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
00289 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00290 GetBitContext *gb)
00291 {
00292 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00293 int comment_len;
00294
00295 skip_bits(gb, 2);
00296
00297 sampling_index = get_bits(gb, 4);
00298 if (m4ac->sampling_index != sampling_index)
00299 av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
00300
00301 num_front = get_bits(gb, 4);
00302 num_side = get_bits(gb, 4);
00303 num_back = get_bits(gb, 4);
00304 num_lfe = get_bits(gb, 2);
00305 num_assoc_data = get_bits(gb, 3);
00306 num_cc = get_bits(gb, 4);
00307
00308 if (get_bits1(gb))
00309 skip_bits(gb, 4);
00310 if (get_bits1(gb))
00311 skip_bits(gb, 4);
00312
00313 if (get_bits1(gb))
00314 skip_bits(gb, 3);
00315
00316 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00317 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
00318 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
00319 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
00320
00321 skip_bits_long(gb, 4 * num_assoc_data);
00322
00323 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
00324
00325 align_get_bits(gb);
00326
00327
00328 comment_len = get_bits(gb, 8) * 8;
00329 if (get_bits_left(gb) < comment_len) {
00330 av_log(avctx, AV_LOG_ERROR, overread_err);
00331 return -1;
00332 }
00333 skip_bits_long(gb, comment_len);
00334 return 0;
00335 }
00336
00345 static av_cold int set_default_channel_config(AVCodecContext *avctx,
00346 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00347 int channel_config)
00348 {
00349 if (channel_config < 1 || channel_config > 7) {
00350 av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00351 channel_config);
00352 return -1;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 if (channel_config != 2)
00367 new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT;
00368 if (channel_config > 1)
00369 new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT;
00370 if (channel_config == 4)
00371 new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;
00372 if (channel_config > 4)
00373 new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00374 = AAC_CHANNEL_BACK;
00375 if (channel_config > 5)
00376 new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;
00377 if (channel_config == 7)
00378 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT;
00379
00380 return 0;
00381 }
00382
00391 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
00392 GetBitContext *gb,
00393 MPEG4AudioConfig *m4ac,
00394 int channel_config)
00395 {
00396 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00397 int extension_flag, ret;
00398
00399 if (get_bits1(gb)) {
00400 av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
00401 return -1;
00402 }
00403
00404 if (get_bits1(gb))
00405 skip_bits(gb, 14);
00406 extension_flag = get_bits1(gb);
00407
00408 if (m4ac->object_type == AOT_AAC_SCALABLE ||
00409 m4ac->object_type == AOT_ER_AAC_SCALABLE)
00410 skip_bits(gb, 3);
00411
00412 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00413 if (channel_config == 0) {
00414 skip_bits(gb, 4);
00415 if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
00416 return ret;
00417 } else {
00418 if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
00419 return ret;
00420 }
00421 if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
00422 return ret;
00423
00424 if (extension_flag) {
00425 switch (m4ac->object_type) {
00426 case AOT_ER_BSAC:
00427 skip_bits(gb, 5);
00428 skip_bits(gb, 11);
00429 break;
00430 case AOT_ER_AAC_LC:
00431 case AOT_ER_AAC_LTP:
00432 case AOT_ER_AAC_SCALABLE:
00433 case AOT_ER_AAC_LD:
00434 skip_bits(gb, 3);
00435
00436
00437
00438 break;
00439 }
00440 skip_bits1(gb);
00441 }
00442 return 0;
00443 }
00444
00456 static int decode_audio_specific_config(AACContext *ac,
00457 AVCodecContext *avctx,
00458 MPEG4AudioConfig *m4ac,
00459 const uint8_t *data, int data_size)
00460 {
00461 GetBitContext gb;
00462 int i;
00463
00464 init_get_bits(&gb, data, data_size * 8);
00465
00466 if ((i = ff_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
00467 return -1;
00468 if (m4ac->sampling_index > 12) {
00469 av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
00470 return -1;
00471 }
00472 if (m4ac->sbr == 1 && m4ac->ps == -1)
00473 m4ac->ps = 1;
00474
00475 skip_bits_long(&gb, i);
00476
00477 switch (m4ac->object_type) {
00478 case AOT_AAC_MAIN:
00479 case AOT_AAC_LC:
00480 if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
00481 return -1;
00482 break;
00483 default:
00484 av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00485 m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
00486 return -1;
00487 }
00488
00489 return get_bits_count(&gb);
00490 }
00491
00499 static av_always_inline int lcg_random(int previous_val)
00500 {
00501 return previous_val * 1664525 + 1013904223;
00502 }
00503
00504 static av_always_inline void reset_predict_state(PredictorState *ps)
00505 {
00506 ps->r0 = 0.0f;
00507 ps->r1 = 0.0f;
00508 ps->cor0 = 0.0f;
00509 ps->cor1 = 0.0f;
00510 ps->var0 = 1.0f;
00511 ps->var1 = 1.0f;
00512 }
00513
00514 static void reset_all_predictors(PredictorState *ps)
00515 {
00516 int i;
00517 for (i = 0; i < MAX_PREDICTORS; i++)
00518 reset_predict_state(&ps[i]);
00519 }
00520
00521 static void reset_predictor_group(PredictorState *ps, int group_num)
00522 {
00523 int i;
00524 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
00525 reset_predict_state(&ps[i]);
00526 }
00527
00528 #define AAC_INIT_VLC_STATIC(num, size) \
00529 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
00530 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
00531 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
00532 size);
00533
00534 static av_cold int aac_decode_init(AVCodecContext *avctx)
00535 {
00536 AACContext *ac = avctx->priv_data;
00537
00538 ac->avctx = avctx;
00539 ac->m4ac.sample_rate = avctx->sample_rate;
00540
00541 if (avctx->extradata_size > 0) {
00542 if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
00543 avctx->extradata,
00544 avctx->extradata_size) < 0)
00545 return -1;
00546 }
00547
00548 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00549
00550 AAC_INIT_VLC_STATIC( 0, 304);
00551 AAC_INIT_VLC_STATIC( 1, 270);
00552 AAC_INIT_VLC_STATIC( 2, 550);
00553 AAC_INIT_VLC_STATIC( 3, 300);
00554 AAC_INIT_VLC_STATIC( 4, 328);
00555 AAC_INIT_VLC_STATIC( 5, 294);
00556 AAC_INIT_VLC_STATIC( 6, 306);
00557 AAC_INIT_VLC_STATIC( 7, 268);
00558 AAC_INIT_VLC_STATIC( 8, 510);
00559 AAC_INIT_VLC_STATIC( 9, 366);
00560 AAC_INIT_VLC_STATIC(10, 462);
00561
00562 ff_aac_sbr_init();
00563
00564 dsputil_init(&ac->dsp, avctx);
00565
00566 ac->random_state = 0x1f2e3d4c;
00567
00568
00569
00570
00571
00572 if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
00573 ac->add_bias = 385.0f;
00574 ac->sf_scale = 1. / (-1024. * 32768.);
00575 ac->sf_offset = 0;
00576 } else {
00577 ac->add_bias = 0.0f;
00578 ac->sf_scale = 1. / -1024.;
00579 ac->sf_offset = 60;
00580 }
00581
00582 ff_aac_tableinit();
00583
00584 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00585 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00586 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00587 352);
00588
00589 ff_mdct_init(&ac->mdct, 11, 1, 1.0);
00590 ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
00591
00592 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00593 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00594 ff_init_ff_sine_windows(10);
00595 ff_init_ff_sine_windows( 7);
00596
00597 cbrt_tableinit();
00598
00599 return 0;
00600 }
00601
00605 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
00606 {
00607 int byte_align = get_bits1(gb);
00608 int count = get_bits(gb, 8);
00609 if (count == 255)
00610 count += get_bits(gb, 8);
00611 if (byte_align)
00612 align_get_bits(gb);
00613
00614 if (get_bits_left(gb) < 8 * count) {
00615 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00616 return -1;
00617 }
00618 skip_bits_long(gb, 8 * count);
00619 return 0;
00620 }
00621
00622 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
00623 GetBitContext *gb)
00624 {
00625 int sfb;
00626 if (get_bits1(gb)) {
00627 ics->predictor_reset_group = get_bits(gb, 5);
00628 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00629 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00630 return -1;
00631 }
00632 }
00633 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00634 ics->prediction_used[sfb] = get_bits1(gb);
00635 }
00636 return 0;
00637 }
00638
00644 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
00645 GetBitContext *gb, int common_window)
00646 {
00647 if (get_bits1(gb)) {
00648 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
00649 memset(ics, 0, sizeof(IndividualChannelStream));
00650 return -1;
00651 }
00652 ics->window_sequence[1] = ics->window_sequence[0];
00653 ics->window_sequence[0] = get_bits(gb, 2);
00654 ics->use_kb_window[1] = ics->use_kb_window[0];
00655 ics->use_kb_window[0] = get_bits1(gb);
00656 ics->num_window_groups = 1;
00657 ics->group_len[0] = 1;
00658 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00659 int i;
00660 ics->max_sfb = get_bits(gb, 4);
00661 for (i = 0; i < 7; i++) {
00662 if (get_bits1(gb)) {
00663 ics->group_len[ics->num_window_groups - 1]++;
00664 } else {
00665 ics->num_window_groups++;
00666 ics->group_len[ics->num_window_groups - 1] = 1;
00667 }
00668 }
00669 ics->num_windows = 8;
00670 ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
00671 ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
00672 ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
00673 ics->predictor_present = 0;
00674 } else {
00675 ics->max_sfb = get_bits(gb, 6);
00676 ics->num_windows = 1;
00677 ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
00678 ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00679 ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
00680 ics->predictor_present = get_bits1(gb);
00681 ics->predictor_reset_group = 0;
00682 if (ics->predictor_present) {
00683 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00684 if (decode_prediction(ac, ics, gb)) {
00685 memset(ics, 0, sizeof(IndividualChannelStream));
00686 return -1;
00687 }
00688 } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00689 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00690 memset(ics, 0, sizeof(IndividualChannelStream));
00691 return -1;
00692 } else {
00693 av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
00694 memset(ics, 0, sizeof(IndividualChannelStream));
00695 return -1;
00696 }
00697 }
00698 }
00699
00700 if (ics->max_sfb > ics->num_swb) {
00701 av_log(ac->avctx, AV_LOG_ERROR,
00702 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00703 ics->max_sfb, ics->num_swb);
00704 memset(ics, 0, sizeof(IndividualChannelStream));
00705 return -1;
00706 }
00707
00708 return 0;
00709 }
00710
00719 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
00720 int band_type_run_end[120], GetBitContext *gb,
00721 IndividualChannelStream *ics)
00722 {
00723 int g, idx = 0;
00724 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00725 for (g = 0; g < ics->num_window_groups; g++) {
00726 int k = 0;
00727 while (k < ics->max_sfb) {
00728 uint8_t sect_end = k;
00729 int sect_len_incr;
00730 int sect_band_type = get_bits(gb, 4);
00731 if (sect_band_type == 12) {
00732 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
00733 return -1;
00734 }
00735 while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
00736 sect_end += sect_len_incr;
00737 sect_end += sect_len_incr;
00738 if (get_bits_left(gb) < 0) {
00739 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00740 return -1;
00741 }
00742 if (sect_end > ics->max_sfb) {
00743 av_log(ac->avctx, AV_LOG_ERROR,
00744 "Number of bands (%d) exceeds limit (%d).\n",
00745 sect_end, ics->max_sfb);
00746 return -1;
00747 }
00748 for (; k < sect_end; k++) {
00749 band_type [idx] = sect_band_type;
00750 band_type_run_end[idx++] = sect_end;
00751 }
00752 }
00753 }
00754 return 0;
00755 }
00756
00767 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
00768 unsigned int global_gain,
00769 IndividualChannelStream *ics,
00770 enum BandType band_type[120],
00771 int band_type_run_end[120])
00772 {
00773 const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
00774 int g, i, idx = 0;
00775 int offset[3] = { global_gain, global_gain - 90, 100 };
00776 int noise_flag = 1;
00777 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00778 for (g = 0; g < ics->num_window_groups; g++) {
00779 for (i = 0; i < ics->max_sfb;) {
00780 int run_end = band_type_run_end[idx];
00781 if (band_type[idx] == ZERO_BT) {
00782 for (; i < run_end; i++, idx++)
00783 sf[idx] = 0.;
00784 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00785 for (; i < run_end; i++, idx++) {
00786 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00787 if (offset[2] > 255U) {
00788 av_log(ac->avctx, AV_LOG_ERROR,
00789 "%s (%d) out of range.\n", sf_str[2], offset[2]);
00790 return -1;
00791 }
00792 sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
00793 }
00794 } else if (band_type[idx] == NOISE_BT) {
00795 for (; i < run_end; i++, idx++) {
00796 if (noise_flag-- > 0)
00797 offset[1] += get_bits(gb, 9) - 256;
00798 else
00799 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00800 if (offset[1] > 255U) {
00801 av_log(ac->avctx, AV_LOG_ERROR,
00802 "%s (%d) out of range.\n", sf_str[1], offset[1]);
00803 return -1;
00804 }
00805 sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
00806 }
00807 } else {
00808 for (; i < run_end; i++, idx++) {
00809 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00810 if (offset[0] > 255U) {
00811 av_log(ac->avctx, AV_LOG_ERROR,
00812 "%s (%d) out of range.\n", sf_str[0], offset[0]);
00813 return -1;
00814 }
00815 sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
00816 }
00817 }
00818 }
00819 }
00820 return 0;
00821 }
00822
00826 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
00827 const uint16_t *swb_offset, int num_swb)
00828 {
00829 int i, pulse_swb;
00830 pulse->num_pulse = get_bits(gb, 2) + 1;
00831 pulse_swb = get_bits(gb, 6);
00832 if (pulse_swb >= num_swb)
00833 return -1;
00834 pulse->pos[0] = swb_offset[pulse_swb];
00835 pulse->pos[0] += get_bits(gb, 5);
00836 if (pulse->pos[0] > 1023)
00837 return -1;
00838 pulse->amp[0] = get_bits(gb, 4);
00839 for (i = 1; i < pulse->num_pulse; i++) {
00840 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
00841 if (pulse->pos[i] > 1023)
00842 return -1;
00843 pulse->amp[i] = get_bits(gb, 4);
00844 }
00845 return 0;
00846 }
00847
00853 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
00854 GetBitContext *gb, const IndividualChannelStream *ics)
00855 {
00856 int w, filt, i, coef_len, coef_res, coef_compress;
00857 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00858 const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00859 for (w = 0; w < ics->num_windows; w++) {
00860 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00861 coef_res = get_bits1(gb);
00862
00863 for (filt = 0; filt < tns->n_filt[w]; filt++) {
00864 int tmp2_idx;
00865 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
00866
00867 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
00868 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
00869 tns->order[w][filt], tns_max_order);
00870 tns->order[w][filt] = 0;
00871 return -1;
00872 }
00873 if (tns->order[w][filt]) {
00874 tns->direction[w][filt] = get_bits1(gb);
00875 coef_compress = get_bits1(gb);
00876 coef_len = coef_res + 3 - coef_compress;
00877 tmp2_idx = 2 * coef_compress + coef_res;
00878
00879 for (i = 0; i < tns->order[w][filt]; i++)
00880 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00881 }
00882 }
00883 }
00884 }
00885 return 0;
00886 }
00887
00895 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
00896 int ms_present)
00897 {
00898 int idx;
00899 if (ms_present == 1) {
00900 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00901 cpe->ms_mask[idx] = get_bits1(gb);
00902 } else if (ms_present == 2) {
00903 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00904 }
00905 }
00906
00907 #ifndef VMUL2
00908 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
00909 const float *scale)
00910 {
00911 float s = *scale;
00912 *dst++ = v[idx & 15] * s;
00913 *dst++ = v[idx>>4 & 15] * s;
00914 return dst;
00915 }
00916 #endif
00917
00918 #ifndef VMUL4
00919 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
00920 const float *scale)
00921 {
00922 float s = *scale;
00923 *dst++ = v[idx & 3] * s;
00924 *dst++ = v[idx>>2 & 3] * s;
00925 *dst++ = v[idx>>4 & 3] * s;
00926 *dst++ = v[idx>>6 & 3] * s;
00927 return dst;
00928 }
00929 #endif
00930
00931 #ifndef VMUL2S
00932 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
00933 unsigned sign, const float *scale)
00934 {
00935 union float754 s0, s1;
00936
00937 s0.f = s1.f = *scale;
00938 s0.i ^= sign >> 1 << 31;
00939 s1.i ^= sign << 31;
00940
00941 *dst++ = v[idx & 15] * s0.f;
00942 *dst++ = v[idx>>4 & 15] * s1.f;
00943
00944 return dst;
00945 }
00946 #endif
00947
00948 #ifndef VMUL4S
00949 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
00950 unsigned sign, const float *scale)
00951 {
00952 unsigned nz = idx >> 12;
00953 union float754 s = { .f = *scale };
00954 union float754 t;
00955
00956 t.i = s.i ^ (sign & 1<<31);
00957 *dst++ = v[idx & 3] * t.f;
00958
00959 sign <<= nz & 1; nz >>= 1;
00960 t.i = s.i ^ (sign & 1<<31);
00961 *dst++ = v[idx>>2 & 3] * t.f;
00962
00963 sign <<= nz & 1; nz >>= 1;
00964 t.i = s.i ^ (sign & 1<<31);
00965 *dst++ = v[idx>>4 & 3] * t.f;
00966
00967 sign <<= nz & 1; nz >>= 1;
00968 t.i = s.i ^ (sign & 1<<31);
00969 *dst++ = v[idx>>6 & 3] * t.f;
00970
00971 return dst;
00972 }
00973 #endif
00974
00987 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
00988 GetBitContext *gb, const float sf[120],
00989 int pulse_present, const Pulse *pulse,
00990 const IndividualChannelStream *ics,
00991 enum BandType band_type[120])
00992 {
00993 int i, k, g, idx = 0;
00994 const int c = 1024 / ics->num_windows;
00995 const uint16_t *offsets = ics->swb_offset;
00996 float *coef_base = coef;
00997
00998 for (g = 0; g < ics->num_windows; g++)
00999 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
01000
01001 for (g = 0; g < ics->num_window_groups; g++) {
01002 unsigned g_len = ics->group_len[g];
01003
01004 for (i = 0; i < ics->max_sfb; i++, idx++) {
01005 const unsigned cbt_m1 = band_type[idx] - 1;
01006 float *cfo = coef + offsets[i];
01007 int off_len = offsets[i + 1] - offsets[i];
01008 int group;
01009
01010 if (cbt_m1 >= INTENSITY_BT2 - 1) {
01011 for (group = 0; group < g_len; group++, cfo+=128) {
01012 memset(cfo, 0, off_len * sizeof(float));
01013 }
01014 } else if (cbt_m1 == NOISE_BT - 1) {
01015 for (group = 0; group < g_len; group++, cfo+=128) {
01016 float scale;
01017 float band_energy;
01018
01019 for (k = 0; k < off_len; k++) {
01020 ac->random_state = lcg_random(ac->random_state);
01021 cfo[k] = ac->random_state;
01022 }
01023
01024 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
01025 scale = sf[idx] / sqrtf(band_energy);
01026 ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
01027 }
01028 } else {
01029 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
01030 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
01031 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
01032 OPEN_READER(re, gb);
01033
01034 switch (cbt_m1 >> 1) {
01035 case 0:
01036 for (group = 0; group < g_len; group++, cfo+=128) {
01037 float *cf = cfo;
01038 int len = off_len;
01039
01040 do {
01041 int code;
01042 unsigned cb_idx;
01043
01044 UPDATE_CACHE(re, gb);
01045 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01046 cb_idx = cb_vector_idx[code];
01047 cf = VMUL4(cf, vq, cb_idx, sf + idx);
01048 } while (len -= 4);
01049 }
01050 break;
01051
01052 case 1:
01053 for (group = 0; group < g_len; group++, cfo+=128) {
01054 float *cf = cfo;
01055 int len = off_len;
01056
01057 do {
01058 int code;
01059 unsigned nnz;
01060 unsigned cb_idx;
01061 uint32_t bits;
01062
01063 UPDATE_CACHE(re, gb);
01064 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01065 #if MIN_CACHE_BITS < 20
01066 UPDATE_CACHE(re, gb);
01067 #endif
01068 cb_idx = cb_vector_idx[code];
01069 nnz = cb_idx >> 8 & 15;
01070 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01071 LAST_SKIP_BITS(re, gb, nnz);
01072 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
01073 } while (len -= 4);
01074 }
01075 break;
01076
01077 case 2:
01078 for (group = 0; group < g_len; group++, cfo+=128) {
01079 float *cf = cfo;
01080 int len = off_len;
01081
01082 do {
01083 int code;
01084 unsigned cb_idx;
01085
01086 UPDATE_CACHE(re, gb);
01087 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01088 cb_idx = cb_vector_idx[code];
01089 cf = VMUL2(cf, vq, cb_idx, sf + idx);
01090 } while (len -= 2);
01091 }
01092 break;
01093
01094 case 3:
01095 case 4:
01096 for (group = 0; group < g_len; group++, cfo+=128) {
01097 float *cf = cfo;
01098 int len = off_len;
01099
01100 do {
01101 int code;
01102 unsigned nnz;
01103 unsigned cb_idx;
01104 unsigned sign;
01105
01106 UPDATE_CACHE(re, gb);
01107 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01108 cb_idx = cb_vector_idx[code];
01109 nnz = cb_idx >> 8 & 15;
01110 sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
01111 LAST_SKIP_BITS(re, gb, nnz);
01112 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
01113 } while (len -= 2);
01114 }
01115 break;
01116
01117 default:
01118 for (group = 0; group < g_len; group++, cfo+=128) {
01119 float *cf = cfo;
01120 uint32_t *icf = (uint32_t *) cf;
01121 int len = off_len;
01122
01123 do {
01124 int code;
01125 unsigned nzt, nnz;
01126 unsigned cb_idx;
01127 uint32_t bits;
01128 int j;
01129
01130 UPDATE_CACHE(re, gb);
01131 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01132
01133 if (!code) {
01134 *icf++ = 0;
01135 *icf++ = 0;
01136 continue;
01137 }
01138
01139 cb_idx = cb_vector_idx[code];
01140 nnz = cb_idx >> 12;
01141 nzt = cb_idx >> 8;
01142 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01143 LAST_SKIP_BITS(re, gb, nnz);
01144
01145 for (j = 0; j < 2; j++) {
01146 if (nzt & 1<<j) {
01147 uint32_t b;
01148 int n;
01149
01150
01151 UPDATE_CACHE(re, gb);
01152 b = GET_CACHE(re, gb);
01153 b = 31 - av_log2(~b);
01154
01155 if (b > 8) {
01156 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
01157 return -1;
01158 }
01159
01160 #if MIN_CACHE_BITS < 21
01161 LAST_SKIP_BITS(re, gb, b + 1);
01162 UPDATE_CACHE(re, gb);
01163 #else
01164 SKIP_BITS(re, gb, b + 1);
01165 #endif
01166 b += 4;
01167 n = (1 << b) + SHOW_UBITS(re, gb, b);
01168 LAST_SKIP_BITS(re, gb, b);
01169 *icf++ = cbrt_tab[n] | (bits & 1<<31);
01170 bits <<= 1;
01171 } else {
01172 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
01173 *icf++ = (bits & 1<<31) | v;
01174 bits <<= !!v;
01175 }
01176 cb_idx >>= 4;
01177 }
01178 } while (len -= 2);
01179
01180 ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
01181 }
01182 }
01183
01184 CLOSE_READER(re, gb);
01185 }
01186 }
01187 coef += g_len << 7;
01188 }
01189
01190 if (pulse_present) {
01191 idx = 0;
01192 for (i = 0; i < pulse->num_pulse; i++) {
01193 float co = coef_base[ pulse->pos[i] ];
01194 while (offsets[idx + 1] <= pulse->pos[i])
01195 idx++;
01196 if (band_type[idx] != NOISE_BT && sf[idx]) {
01197 float ico = -pulse->amp[i];
01198 if (co) {
01199 co /= sf[idx];
01200 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
01201 }
01202 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
01203 }
01204 }
01205 }
01206 return 0;
01207 }
01208
01209 static av_always_inline float flt16_round(float pf)
01210 {
01211 union float754 tmp;
01212 tmp.f = pf;
01213 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
01214 return tmp.f;
01215 }
01216
01217 static av_always_inline float flt16_even(float pf)
01218 {
01219 union float754 tmp;
01220 tmp.f = pf;
01221 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
01222 return tmp.f;
01223 }
01224
01225 static av_always_inline float flt16_trunc(float pf)
01226 {
01227 union float754 pun;
01228 pun.f = pf;
01229 pun.i &= 0xFFFF0000U;
01230 return pun.f;
01231 }
01232
01233 static av_always_inline void predict(PredictorState *ps, float *coef,
01234 float sf_scale, float inv_sf_scale,
01235 int output_enable)
01236 {
01237 const float a = 0.953125;
01238 const float alpha = 0.90625;
01239 float e0, e1;
01240 float pv;
01241 float k1, k2;
01242 float r0 = ps->r0, r1 = ps->r1;
01243 float cor0 = ps->cor0, cor1 = ps->cor1;
01244 float var0 = ps->var0, var1 = ps->var1;
01245
01246 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
01247 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
01248
01249 pv = flt16_round(k1 * r0 + k2 * r1);
01250 if (output_enable)
01251 *coef += pv * sf_scale;
01252
01253 e0 = *coef * inv_sf_scale;
01254 e1 = e0 - k1 * r0;
01255
01256 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
01257 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
01258 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
01259 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
01260
01261 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
01262 ps->r0 = flt16_trunc(a * e0);
01263 }
01264
01268 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
01269 {
01270 int sfb, k;
01271 float sf_scale = ac->sf_scale, inv_sf_scale = 1 / ac->sf_scale;
01272
01273 if (!sce->ics.predictor_initialized) {
01274 reset_all_predictors(sce->predictor_state);
01275 sce->ics.predictor_initialized = 1;
01276 }
01277
01278 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01279 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
01280 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
01281 predict(&sce->predictor_state[k], &sce->coeffs[k],
01282 sf_scale, inv_sf_scale,
01283 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
01284 }
01285 }
01286 if (sce->ics.predictor_reset_group)
01287 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
01288 } else
01289 reset_all_predictors(sce->predictor_state);
01290 }
01291
01300 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
01301 GetBitContext *gb, int common_window, int scale_flag)
01302 {
01303 Pulse pulse;
01304 TemporalNoiseShaping *tns = &sce->tns;
01305 IndividualChannelStream *ics = &sce->ics;
01306 float *out = sce->coeffs;
01307 int global_gain, pulse_present = 0;
01308
01309
01310
01311
01312 pulse.num_pulse = 0;
01313
01314 global_gain = get_bits(gb, 8);
01315
01316 if (!common_window && !scale_flag) {
01317 if (decode_ics_info(ac, ics, gb, 0) < 0)
01318 return -1;
01319 }
01320
01321 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01322 return -1;
01323 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01324 return -1;
01325
01326 pulse_present = 0;
01327 if (!scale_flag) {
01328 if ((pulse_present = get_bits1(gb))) {
01329 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01330 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01331 return -1;
01332 }
01333 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01334 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01335 return -1;
01336 }
01337 }
01338 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01339 return -1;
01340 if (get_bits1(gb)) {
01341 av_log_missing_feature(ac->avctx, "SSR", 1);
01342 return -1;
01343 }
01344 }
01345
01346 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01347 return -1;
01348
01349 if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01350 apply_prediction(ac, sce);
01351
01352 return 0;
01353 }
01354
01358 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
01359 {
01360 const IndividualChannelStream *ics = &cpe->ch[0].ics;
01361 float *ch0 = cpe->ch[0].coeffs;
01362 float *ch1 = cpe->ch[1].coeffs;
01363 int g, i, group, idx = 0;
01364 const uint16_t *offsets = ics->swb_offset;
01365 for (g = 0; g < ics->num_window_groups; g++) {
01366 for (i = 0; i < ics->max_sfb; i++, idx++) {
01367 if (cpe->ms_mask[idx] &&
01368 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01369 for (group = 0; group < ics->group_len[g]; group++) {
01370 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
01371 ch1 + group * 128 + offsets[i],
01372 offsets[i+1] - offsets[i]);
01373 }
01374 }
01375 }
01376 ch0 += ics->group_len[g] * 128;
01377 ch1 += ics->group_len[g] * 128;
01378 }
01379 }
01380
01388 static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
01389 {
01390 const IndividualChannelStream *ics = &cpe->ch[1].ics;
01391 SingleChannelElement *sce1 = &cpe->ch[1];
01392 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01393 const uint16_t *offsets = ics->swb_offset;
01394 int g, group, i, k, idx = 0;
01395 int c;
01396 float scale;
01397 for (g = 0; g < ics->num_window_groups; g++) {
01398 for (i = 0; i < ics->max_sfb;) {
01399 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01400 const int bt_run_end = sce1->band_type_run_end[idx];
01401 for (; i < bt_run_end; i++, idx++) {
01402 c = -1 + 2 * (sce1->band_type[idx] - 14);
01403 if (ms_present)
01404 c *= 1 - 2 * cpe->ms_mask[idx];
01405 scale = c * sce1->sf[idx];
01406 for (group = 0; group < ics->group_len[g]; group++)
01407 for (k = offsets[i]; k < offsets[i + 1]; k++)
01408 coef1[group * 128 + k] = scale * coef0[group * 128 + k];
01409 }
01410 } else {
01411 int bt_run_end = sce1->band_type_run_end[idx];
01412 idx += bt_run_end - i;
01413 i = bt_run_end;
01414 }
01415 }
01416 coef0 += ics->group_len[g] * 128;
01417 coef1 += ics->group_len[g] * 128;
01418 }
01419 }
01420
01426 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
01427 {
01428 int i, ret, common_window, ms_present = 0;
01429
01430 common_window = get_bits1(gb);
01431 if (common_window) {
01432 if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
01433 return -1;
01434 i = cpe->ch[1].ics.use_kb_window[0];
01435 cpe->ch[1].ics = cpe->ch[0].ics;
01436 cpe->ch[1].ics.use_kb_window[1] = i;
01437 ms_present = get_bits(gb, 2);
01438 if (ms_present == 3) {
01439 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01440 return -1;
01441 } else if (ms_present)
01442 decode_mid_side_stereo(cpe, gb, ms_present);
01443 }
01444 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01445 return ret;
01446 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01447 return ret;
01448
01449 if (common_window) {
01450 if (ms_present)
01451 apply_mid_side_stereo(ac, cpe);
01452 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01453 apply_prediction(ac, &cpe->ch[0]);
01454 apply_prediction(ac, &cpe->ch[1]);
01455 }
01456 }
01457
01458 apply_intensity_stereo(cpe, ms_present);
01459 return 0;
01460 }
01461
01462 static const float cce_scale[] = {
01463 1.09050773266525765921,
01464 1.18920711500272106672,
01465 M_SQRT2,
01466 2,
01467 };
01468
01474 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
01475 {
01476 int num_gain = 0;
01477 int c, g, sfb, ret;
01478 int sign;
01479 float scale;
01480 SingleChannelElement *sce = &che->ch[0];
01481 ChannelCoupling *coup = &che->coup;
01482
01483 coup->coupling_point = 2 * get_bits1(gb);
01484 coup->num_coupled = get_bits(gb, 3);
01485 for (c = 0; c <= coup->num_coupled; c++) {
01486 num_gain++;
01487 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01488 coup->id_select[c] = get_bits(gb, 4);
01489 if (coup->type[c] == TYPE_CPE) {
01490 coup->ch_select[c] = get_bits(gb, 2);
01491 if (coup->ch_select[c] == 3)
01492 num_gain++;
01493 } else
01494 coup->ch_select[c] = 2;
01495 }
01496 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
01497
01498 sign = get_bits(gb, 1);
01499 scale = cce_scale[get_bits(gb, 2)];
01500
01501 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01502 return ret;
01503
01504 for (c = 0; c < num_gain; c++) {
01505 int idx = 0;
01506 int cge = 1;
01507 int gain = 0;
01508 float gain_cache = 1.;
01509 if (c) {
01510 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01511 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01512 gain_cache = powf(scale, -gain);
01513 }
01514 if (coup->coupling_point == AFTER_IMDCT) {
01515 coup->gain[c][0] = gain_cache;
01516 } else {
01517 for (g = 0; g < sce->ics.num_window_groups; g++) {
01518 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01519 if (sce->band_type[idx] != ZERO_BT) {
01520 if (!cge) {
01521 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01522 if (t) {
01523 int s = 1;
01524 t = gain += t;
01525 if (sign) {
01526 s -= 2 * (t & 0x1);
01527 t >>= 1;
01528 }
01529 gain_cache = powf(scale, -t) * s;
01530 }
01531 }
01532 coup->gain[c][idx] = gain_cache;
01533 }
01534 }
01535 }
01536 }
01537 }
01538 return 0;
01539 }
01540
01546 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
01547 GetBitContext *gb)
01548 {
01549 int i;
01550 int num_excl_chan = 0;
01551
01552 do {
01553 for (i = 0; i < 7; i++)
01554 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01555 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01556
01557 return num_excl_chan / 7;
01558 }
01559
01567 static int decode_dynamic_range(DynamicRangeControl *che_drc,
01568 GetBitContext *gb, int cnt)
01569 {
01570 int n = 1;
01571 int drc_num_bands = 1;
01572 int i;
01573
01574
01575 if (get_bits1(gb)) {
01576 che_drc->pce_instance_tag = get_bits(gb, 4);
01577 skip_bits(gb, 4);
01578 n++;
01579 }
01580
01581
01582 if (get_bits1(gb)) {
01583 n += decode_drc_channel_exclusions(che_drc, gb);
01584 }
01585
01586
01587 if (get_bits1(gb)) {
01588 che_drc->band_incr = get_bits(gb, 4);
01589 che_drc->interpolation_scheme = get_bits(gb, 4);
01590 n++;
01591 drc_num_bands += che_drc->band_incr;
01592 for (i = 0; i < drc_num_bands; i++) {
01593 che_drc->band_top[i] = get_bits(gb, 8);
01594 n++;
01595 }
01596 }
01597
01598
01599 if (get_bits1(gb)) {
01600 che_drc->prog_ref_level = get_bits(gb, 7);
01601 skip_bits1(gb);
01602 n++;
01603 }
01604
01605 for (i = 0; i < drc_num_bands; i++) {
01606 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01607 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01608 n++;
01609 }
01610
01611 return n;
01612 }
01613
01621 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
01622 ChannelElement *che, enum RawDataBlockType elem_type)
01623 {
01624 int crc_flag = 0;
01625 int res = cnt;
01626 switch (get_bits(gb, 4)) {
01627 case EXT_SBR_DATA_CRC:
01628 crc_flag++;
01629 case EXT_SBR_DATA:
01630 if (!che) {
01631 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
01632 return res;
01633 } else if (!ac->m4ac.sbr) {
01634 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
01635 skip_bits_long(gb, 8 * cnt - 4);
01636 return res;
01637 } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
01638 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
01639 skip_bits_long(gb, 8 * cnt - 4);
01640 return res;
01641 } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
01642 ac->m4ac.sbr = 1;
01643 ac->m4ac.ps = 1;
01644 output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
01645 } else {
01646 ac->m4ac.sbr = 1;
01647 }
01648 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
01649 break;
01650 case EXT_DYNAMIC_RANGE:
01651 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01652 break;
01653 case EXT_FILL:
01654 case EXT_FILL_DATA:
01655 case EXT_DATA_ELEMENT:
01656 default:
01657 skip_bits_long(gb, 8 * cnt - 4);
01658 break;
01659 };
01660 return res;
01661 }
01662
01669 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
01670 IndividualChannelStream *ics, int decode)
01671 {
01672 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
01673 int w, filt, m, i;
01674 int bottom, top, order, start, end, size, inc;
01675 float lpc[TNS_MAX_ORDER];
01676
01677 for (w = 0; w < ics->num_windows; w++) {
01678 bottom = ics->num_swb;
01679 for (filt = 0; filt < tns->n_filt[w]; filt++) {
01680 top = bottom;
01681 bottom = FFMAX(0, top - tns->length[w][filt]);
01682 order = tns->order[w][filt];
01683 if (order == 0)
01684 continue;
01685
01686
01687 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01688
01689 start = ics->swb_offset[FFMIN(bottom, mmm)];
01690 end = ics->swb_offset[FFMIN( top, mmm)];
01691 if ((size = end - start) <= 0)
01692 continue;
01693 if (tns->direction[w][filt]) {
01694 inc = -1;
01695 start = end - 1;
01696 } else {
01697 inc = 1;
01698 }
01699 start += w * 128;
01700
01701
01702 for (m = 0; m < size; m++, start += inc)
01703 for (i = 1; i <= FFMIN(m, order); i++)
01704 coef[start] -= coef[start - i * inc] * lpc[i - 1];
01705 }
01706 }
01707 }
01708
01712 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
01713 {
01714 IndividualChannelStream *ics = &sce->ics;
01715 float *in = sce->coeffs;
01716 float *out = sce->ret;
01717 float *saved = sce->saved;
01718 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01719 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01720 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01721 float *buf = ac->buf_mdct;
01722 float *temp = ac->temp;
01723 int i;
01724
01725
01726 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01727 for (i = 0; i < 1024; i += 128)
01728 ff_imdct_half(&ac->mdct_small, buf + i, in + i);
01729 } else
01730 ff_imdct_half(&ac->mdct, buf, in);
01731
01732
01733
01734
01735
01736
01737
01738 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01739 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01740 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, bias, 512);
01741 } else {
01742 for (i = 0; i < 448; i++)
01743 out[i] = saved[i] + bias;
01744
01745 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01746 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, bias, 64);
01747 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, bias, 64);
01748 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, bias, 64);
01749 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, bias, 64);
01750 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, bias, 64);
01751 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
01752 } else {
01753 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, bias, 64);
01754 for (i = 576; i < 1024; i++)
01755 out[i] = buf[i-512] + bias;
01756 }
01757 }
01758
01759
01760 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01761 for (i = 0; i < 64; i++)
01762 saved[i] = temp[64 + i] - bias;
01763 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
01764 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
01765 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
01766 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01767 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01768 memcpy( saved, buf + 512, 448 * sizeof(float));
01769 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01770 } else {
01771 memcpy( saved, buf + 512, 512 * sizeof(float));
01772 }
01773 }
01774
01780 static void apply_dependent_coupling(AACContext *ac,
01781 SingleChannelElement *target,
01782 ChannelElement *cce, int index)
01783 {
01784 IndividualChannelStream *ics = &cce->ch[0].ics;
01785 const uint16_t *offsets = ics->swb_offset;
01786 float *dest = target->coeffs;
01787 const float *src = cce->ch[0].coeffs;
01788 int g, i, group, k, idx = 0;
01789 if (ac->m4ac.object_type == AOT_AAC_LTP) {
01790 av_log(ac->avctx, AV_LOG_ERROR,
01791 "Dependent coupling is not supported together with LTP\n");
01792 return;
01793 }
01794 for (g = 0; g < ics->num_window_groups; g++) {
01795 for (i = 0; i < ics->max_sfb; i++, idx++) {
01796 if (cce->ch[0].band_type[idx] != ZERO_BT) {
01797 const float gain = cce->coup.gain[index][idx];
01798 for (group = 0; group < ics->group_len[g]; group++) {
01799 for (k = offsets[i]; k < offsets[i + 1]; k++) {
01800
01801 dest[group * 128 + k] += gain * src[group * 128 + k];
01802 }
01803 }
01804 }
01805 }
01806 dest += ics->group_len[g] * 128;
01807 src += ics->group_len[g] * 128;
01808 }
01809 }
01810
01816 static void apply_independent_coupling(AACContext *ac,
01817 SingleChannelElement *target,
01818 ChannelElement *cce, int index)
01819 {
01820 int i;
01821 const float gain = cce->coup.gain[index][0];
01822 const float bias = ac->add_bias;
01823 const float *src = cce->ch[0].ret;
01824 float *dest = target->ret;
01825 const int len = 1024 << (ac->m4ac.sbr == 1);
01826
01827 for (i = 0; i < len; i++)
01828 dest[i] += gain * (src[i] - bias);
01829 }
01830
01836 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
01837 enum RawDataBlockType type, int elem_id,
01838 enum CouplingPoint coupling_point,
01839 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
01840 {
01841 int i, c;
01842
01843 for (i = 0; i < MAX_ELEM_ID; i++) {
01844 ChannelElement *cce = ac->che[TYPE_CCE][i];
01845 int index = 0;
01846
01847 if (cce && cce->coup.coupling_point == coupling_point) {
01848 ChannelCoupling *coup = &cce->coup;
01849
01850 for (c = 0; c <= coup->num_coupled; c++) {
01851 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
01852 if (coup->ch_select[c] != 1) {
01853 apply_coupling_method(ac, &cc->ch[0], cce, index);
01854 if (coup->ch_select[c] != 0)
01855 index++;
01856 }
01857 if (coup->ch_select[c] != 2)
01858 apply_coupling_method(ac, &cc->ch[1], cce, index++);
01859 } else
01860 index += 1 + (coup->ch_select[c] == 3);
01861 }
01862 }
01863 }
01864 }
01865
01869 static void spectral_to_sample(AACContext *ac)
01870 {
01871 int i, type;
01872 float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
01873 for (type = 3; type >= 0; type--) {
01874 for (i = 0; i < MAX_ELEM_ID; i++) {
01875 ChannelElement *che = ac->che[type][i];
01876 if (che) {
01877 if (type <= TYPE_CPE)
01878 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
01879 if (che->ch[0].tns.present)
01880 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
01881 if (che->ch[1].tns.present)
01882 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
01883 if (type <= TYPE_CPE)
01884 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
01885 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
01886 imdct_and_windowing(ac, &che->ch[0], imdct_bias);
01887 if (type == TYPE_CPE) {
01888 imdct_and_windowing(ac, &che->ch[1], imdct_bias);
01889 }
01890 if (ac->m4ac.sbr > 0) {
01891 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
01892 }
01893 }
01894 if (type <= TYPE_CCE)
01895 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
01896 }
01897 }
01898 }
01899 }
01900
01901 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
01902 {
01903 int size;
01904 AACADTSHeaderInfo hdr_info;
01905
01906 size = ff_aac_parse_header(gb, &hdr_info);
01907 if (size > 0) {
01908 if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
01909 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
01910 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
01911 ac->m4ac.chan_config = hdr_info.chan_config;
01912 if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
01913 return -7;
01914 if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
01915 return -7;
01916 } else if (ac->output_configured != OC_LOCKED) {
01917 ac->output_configured = OC_NONE;
01918 }
01919 if (ac->output_configured != OC_LOCKED) {
01920 ac->m4ac.sbr = -1;
01921 ac->m4ac.ps = -1;
01922 }
01923 ac->m4ac.sample_rate = hdr_info.sample_rate;
01924 ac->m4ac.sampling_index = hdr_info.sampling_index;
01925 ac->m4ac.object_type = hdr_info.object_type;
01926 if (!ac->avctx->sample_rate)
01927 ac->avctx->sample_rate = hdr_info.sample_rate;
01928 if (hdr_info.num_aac_frames == 1) {
01929 if (!hdr_info.crc_absent)
01930 skip_bits(gb, 16);
01931 } else {
01932 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
01933 return -1;
01934 }
01935 }
01936 return size;
01937 }
01938
01939 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
01940 int *data_size, GetBitContext *gb)
01941 {
01942 AACContext *ac = avctx->priv_data;
01943 ChannelElement *che = NULL, *che_prev = NULL;
01944 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
01945 int err, elem_id, data_size_tmp;
01946 int samples = 0, multiplier;
01947
01948 if (show_bits(gb, 12) == 0xfff) {
01949 if (parse_adts_frame_header(ac, gb) < 0) {
01950 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
01951 return -1;
01952 }
01953 if (ac->m4ac.sampling_index > 12) {
01954 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
01955 return -1;
01956 }
01957 }
01958
01959 ac->tags_mapped = 0;
01960
01961 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
01962 elem_id = get_bits(gb, 4);
01963
01964 if (elem_type < TYPE_DSE) {
01965 if (!(che=get_che(ac, elem_type, elem_id))) {
01966 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
01967 elem_type, elem_id);
01968 return -1;
01969 }
01970 samples = 1024;
01971 }
01972
01973 switch (elem_type) {
01974
01975 case TYPE_SCE:
01976 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
01977 break;
01978
01979 case TYPE_CPE:
01980 err = decode_cpe(ac, gb, che);
01981 break;
01982
01983 case TYPE_CCE:
01984 err = decode_cce(ac, gb, che);
01985 break;
01986
01987 case TYPE_LFE:
01988 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
01989 break;
01990
01991 case TYPE_DSE:
01992 err = skip_data_stream_element(ac, gb);
01993 break;
01994
01995 case TYPE_PCE: {
01996 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
01997 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
01998 if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
01999 break;
02000 if (ac->output_configured > OC_TRIAL_PCE)
02001 av_log(avctx, AV_LOG_ERROR,
02002 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
02003 else
02004 err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
02005 break;
02006 }
02007
02008 case TYPE_FIL:
02009 if (elem_id == 15)
02010 elem_id += get_bits(gb, 8) - 1;
02011 if (get_bits_left(gb) < 8 * elem_id) {
02012 av_log(avctx, AV_LOG_ERROR, overread_err);
02013 return -1;
02014 }
02015 while (elem_id > 0)
02016 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
02017 err = 0;
02018 break;
02019
02020 default:
02021 err = -1;
02022 break;
02023 }
02024
02025 che_prev = che;
02026 elem_type_prev = elem_type;
02027
02028 if (err)
02029 return err;
02030
02031 if (get_bits_left(gb) < 3) {
02032 av_log(avctx, AV_LOG_ERROR, overread_err);
02033 return -1;
02034 }
02035 }
02036
02037 spectral_to_sample(ac);
02038
02039 multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
02040 samples <<= multiplier;
02041 if (ac->output_configured < OC_LOCKED) {
02042 avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
02043 avctx->frame_size = samples;
02044 }
02045
02046 data_size_tmp = samples * avctx->channels * sizeof(int16_t);
02047 if (*data_size < data_size_tmp) {
02048 av_log(avctx, AV_LOG_ERROR,
02049 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
02050 *data_size, data_size_tmp);
02051 return -1;
02052 }
02053 *data_size = data_size_tmp;
02054
02055 if (samples)
02056 ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
02057
02058 if (ac->output_configured)
02059 ac->output_configured = OC_LOCKED;
02060
02061 return 0;
02062 }
02063
02064 static int aac_decode_frame(AVCodecContext *avctx, void *data,
02065 int *data_size, AVPacket *avpkt)
02066 {
02067 const uint8_t *buf = avpkt->data;
02068 int buf_size = avpkt->size;
02069 GetBitContext gb;
02070 int buf_consumed;
02071 int buf_offset;
02072 int err;
02073
02074 init_get_bits(&gb, buf, buf_size * 8);
02075
02076 if ((err = aac_decode_frame_int(avctx, data, data_size, &gb)) < 0)
02077 return err;
02078
02079 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
02080 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
02081 if (buf[buf_offset])
02082 break;
02083
02084 return buf_size > buf_offset ? buf_consumed : buf_size;
02085 }
02086
02087 static av_cold int aac_decode_close(AVCodecContext *avctx)
02088 {
02089 AACContext *ac = avctx->priv_data;
02090 int i, type;
02091
02092 for (i = 0; i < MAX_ELEM_ID; i++) {
02093 for (type = 0; type < 4; type++) {
02094 if (ac->che[type][i])
02095 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
02096 av_freep(&ac->che[type][i]);
02097 }
02098 }
02099
02100 ff_mdct_end(&ac->mdct);
02101 ff_mdct_end(&ac->mdct_small);
02102 return 0;
02103 }
02104
02105
02106 #define LOAS_SYNC_WORD 0x2b7
02107
02108 struct LATMContext {
02109 AACContext aac_ctx;
02110 int initialized;
02111
02112
02113 int audio_mux_version_A;
02114 int frame_length_type;
02115 int frame_length;
02116 };
02117
02118 static inline uint32_t latm_get_value(GetBitContext *b)
02119 {
02120 int length = get_bits(b, 2);
02121
02122 return get_bits_long(b, (length+1)*8);
02123 }
02124
02125 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
02126 GetBitContext *gb)
02127 {
02128 AVCodecContext *avctx = latmctx->aac_ctx.avctx;
02129 MPEG4AudioConfig m4ac;
02130 int config_start_bit = get_bits_count(gb);
02131 int bits_consumed, esize;
02132
02133 if (config_start_bit % 8) {
02134 av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
02135 "config not byte aligned.\n", 1);
02136 return AVERROR_INVALIDDATA;
02137 } else {
02138 bits_consumed =
02139 decode_audio_specific_config(NULL, avctx, &m4ac,
02140 gb->buffer + (config_start_bit / 8),
02141 get_bits_left(gb) / 8);
02142
02143 if (bits_consumed < 0)
02144 return AVERROR_INVALIDDATA;
02145
02146 esize = (bits_consumed+7) / 8;
02147
02148 if (avctx->extradata_size <= esize) {
02149 av_free(avctx->extradata);
02150 avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
02151 if (!avctx->extradata)
02152 return AVERROR(ENOMEM);
02153 }
02154
02155 avctx->extradata_size = esize;
02156 memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
02157 memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
02158
02159 skip_bits_long(gb, bits_consumed);
02160 }
02161
02162 return bits_consumed;
02163 }
02164
02165 static int read_stream_mux_config(struct LATMContext *latmctx,
02166 GetBitContext *gb)
02167 {
02168 int ret, audio_mux_version = get_bits(gb, 1);
02169
02170 latmctx->audio_mux_version_A = 0;
02171 if (audio_mux_version)
02172 latmctx->audio_mux_version_A = get_bits(gb, 1);
02173
02174 if (!latmctx->audio_mux_version_A) {
02175
02176 if (audio_mux_version)
02177 latm_get_value(gb);
02178
02179 skip_bits(gb, 1);
02180 skip_bits(gb, 6);
02181
02182 if (get_bits(gb, 4)) {
02183 av_log_missing_feature(latmctx->aac_ctx.avctx,
02184 "multiple programs are not supported\n", 1);
02185 return AVERROR_PATCHWELCOME;
02186 }
02187
02188
02189
02190
02191 if (get_bits(gb, 3)) {
02192 av_log_missing_feature(latmctx->aac_ctx.avctx,
02193 "multiple layers are not supported\n", 1);
02194 return AVERROR_PATCHWELCOME;
02195 }
02196
02197
02198 if (!audio_mux_version) {
02199 if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
02200 return ret;
02201 } else {
02202 int ascLen = latm_get_value(gb);
02203 if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
02204 return ret;
02205 ascLen -= ret;
02206 skip_bits_long(gb, ascLen);
02207 }
02208
02209 latmctx->frame_length_type = get_bits(gb, 3);
02210 switch (latmctx->frame_length_type) {
02211 case 0:
02212 skip_bits(gb, 8);
02213 break;
02214 case 1:
02215 latmctx->frame_length = get_bits(gb, 9);
02216 break;
02217 case 3:
02218 case 4:
02219 case 5:
02220 skip_bits(gb, 6);
02221 break;
02222 case 6:
02223 case 7:
02224 skip_bits(gb, 1);
02225 break;
02226 }
02227
02228 if (get_bits(gb, 1)) {
02229 if (audio_mux_version) {
02230 latm_get_value(gb);
02231 } else {
02232 int esc;
02233 do {
02234 esc = get_bits(gb, 1);
02235 skip_bits(gb, 8);
02236 } while (esc);
02237 }
02238 }
02239
02240 if (get_bits(gb, 1))
02241 skip_bits(gb, 8);
02242 }
02243
02244 return 0;
02245 }
02246
02247 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
02248 {
02249 uint8_t tmp;
02250
02251 if (ctx->frame_length_type == 0) {
02252 int mux_slot_length = 0;
02253 do {
02254 tmp = get_bits(gb, 8);
02255 mux_slot_length += tmp;
02256 } while (tmp == 255);
02257 return mux_slot_length;
02258 } else if (ctx->frame_length_type == 1) {
02259 return ctx->frame_length;
02260 } else if (ctx->frame_length_type == 3 ||
02261 ctx->frame_length_type == 5 ||
02262 ctx->frame_length_type == 7) {
02263 skip_bits(gb, 2);
02264 }
02265 return 0;
02266 }
02267
02268 static int read_audio_mux_element(struct LATMContext *latmctx,
02269 GetBitContext *gb)
02270 {
02271 int err;
02272 uint8_t use_same_mux = get_bits(gb, 1);
02273 if (!use_same_mux) {
02274 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
02275 return err;
02276 } else if (!latmctx->aac_ctx.avctx->extradata) {
02277 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
02278 "no decoder config found\n");
02279 return AVERROR(EAGAIN);
02280 }
02281 if (latmctx->audio_mux_version_A == 0) {
02282 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
02283 if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
02284 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
02285 return AVERROR_INVALIDDATA;
02286 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
02287 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
02288 "frame length mismatch %d << %d\n",
02289 mux_slot_length_bytes * 8, get_bits_left(gb));
02290 return AVERROR_INVALIDDATA;
02291 }
02292 }
02293 return 0;
02294 }
02295
02296
02297 static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
02298 AVPacket *avpkt)
02299 {
02300 struct LATMContext *latmctx = avctx->priv_data;
02301 int muxlength, err;
02302 GetBitContext gb;
02303
02304 if (avpkt->size == 0)
02305 return 0;
02306
02307 init_get_bits(&gb, avpkt->data, avpkt->size * 8);
02308
02309
02310 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
02311 return AVERROR_INVALIDDATA;
02312
02313 muxlength = get_bits(&gb, 13) + 3;
02314
02315 if (muxlength > avpkt->size)
02316 return AVERROR_INVALIDDATA;
02317
02318 if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
02319 return err;
02320
02321 if (!latmctx->initialized) {
02322 if (!avctx->extradata) {
02323 *out_size = 0;
02324 return avpkt->size;
02325 } else {
02326 if ((err = aac_decode_init(avctx)) < 0)
02327 return err;
02328 latmctx->initialized = 1;
02329 }
02330 }
02331
02332 if (show_bits(&gb, 12) == 0xfff) {
02333 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
02334 "ADTS header detected, probably as result of configuration "
02335 "misparsing\n");
02336 return AVERROR_INVALIDDATA;
02337 }
02338
02339 if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0)
02340 return err;
02341
02342 return muxlength;
02343 }
02344
02345 av_cold static int latm_decode_init(AVCodecContext *avctx)
02346 {
02347 struct LATMContext *latmctx = avctx->priv_data;
02348 int ret;
02349
02350 ret = aac_decode_init(avctx);
02351
02352 if (avctx->extradata_size > 0) {
02353 latmctx->initialized = !ret;
02354 } else {
02355 latmctx->initialized = 0;
02356 }
02357
02358 return ret;
02359 }
02360
02361
02362 AVCodec aac_decoder = {
02363 "aac",
02364 AVMEDIA_TYPE_AUDIO,
02365 CODEC_ID_AAC,
02366 sizeof(AACContext),
02367 aac_decode_init,
02368 NULL,
02369 aac_decode_close,
02370 aac_decode_frame,
02371 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
02372 .sample_fmts = (const enum AVSampleFormat[]) {
02373 AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE
02374 },
02375 .channel_layouts = aac_channel_layout,
02376 };
02377
02378
02379
02380
02381
02382
02383 AVCodec aac_latm_decoder = {
02384 .name = "aac_latm",
02385 .type = CODEC_TYPE_AUDIO,
02386 .id = CODEC_ID_AAC_LATM,
02387 .priv_data_size = sizeof(struct LATMContext),
02388 .init = latm_decode_init,
02389 .close = aac_decode_close,
02390 .decode = latm_decode_frame,
02391 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
02392 .sample_fmts = (const enum AVSampleFormat[]) {
02393 AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE
02394 },
02395 .channel_layouts = aac_channel_layout,
02396 };