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