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