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 #include "libavutil/intfloat.h"
00102
00103 #include <assert.h>
00104 #include <errno.h>
00105 #include <math.h>
00106 #include <string.h>
00107
00108 #if ARCH_ARM
00109 # include "arm/aac.h"
00110 #endif
00111
00112 static VLC vlc_scalefactors;
00113 static VLC vlc_spectral[11];
00114
00115 static const char overread_err[] = "Input buffer exhausted before END element found\n";
00116
00117 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
00118 {
00119
00120 if (!ac->m4ac.chan_config) {
00121 return ac->tag_che_map[type][elem_id];
00122 }
00123
00124 switch (ac->m4ac.chan_config) {
00125 case 7:
00126 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00127 ac->tags_mapped++;
00128 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00129 }
00130 case 6:
00131
00132
00133
00134 if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00135 ac->tags_mapped++;
00136 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00137 }
00138 case 5:
00139 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00140 ac->tags_mapped++;
00141 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00142 }
00143 case 4:
00144 if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
00145 ac->tags_mapped++;
00146 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00147 }
00148 case 3:
00149 case 2:
00150 if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
00151 ac->tags_mapped++;
00152 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00153 } else if (ac->m4ac.chan_config == 2) {
00154 return NULL;
00155 }
00156 case 1:
00157 if (!ac->tags_mapped && type == TYPE_SCE) {
00158 ac->tags_mapped++;
00159 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00160 }
00161 default:
00162 return NULL;
00163 }
00164 }
00165
00166 static int count_channels(enum ChannelPosition che_pos[4][MAX_ELEM_ID])
00167 {
00168 int i, type, sum = 0;
00169 for (i = 0; i < MAX_ELEM_ID; i++) {
00170 for (type = 0; type < 4; type++) {
00171 sum += (1 + (type == TYPE_CPE)) *
00172 (che_pos[type][i] != AAC_CHANNEL_OFF &&
00173 che_pos[type][i] != AAC_CHANNEL_CC);
00174 }
00175 }
00176 return sum;
00177 }
00178
00191 static av_cold int che_configure(AACContext *ac,
00192 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00193 int type, int id, int *channels)
00194 {
00195 if (che_pos[type][id]) {
00196 if (!ac->che[type][id]) {
00197 if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
00198 return AVERROR(ENOMEM);
00199 ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
00200 }
00201 if (type != TYPE_CCE) {
00202 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
00203 if (type == TYPE_CPE ||
00204 (type == TYPE_SCE && ac->m4ac.ps == 1)) {
00205 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
00206 }
00207 }
00208 } else {
00209 if (ac->che[type][id])
00210 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
00211 av_freep(&ac->che[type][id]);
00212 }
00213 return 0;
00214 }
00215
00224 static av_cold int output_configure(AACContext *ac,
00225 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00226 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00227 int channel_config, enum OCStatus oc_type)
00228 {
00229 AVCodecContext *avctx = ac->avctx;
00230 int i, type, channels = 0, ret;
00231
00232 if (new_che_pos != che_pos)
00233 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00234
00235 if (channel_config) {
00236 for (i = 0; i < tags_per_config[channel_config]; i++) {
00237 if ((ret = che_configure(ac, che_pos,
00238 aac_channel_layout_map[channel_config - 1][i][0],
00239 aac_channel_layout_map[channel_config - 1][i][1],
00240 &channels)))
00241 return ret;
00242 }
00243
00244 memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00245
00246 avctx->channel_layout = aac_channel_layout[channel_config - 1];
00247 } else {
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 for (i = 0; i < MAX_ELEM_ID; i++) {
00258 for (type = 0; type < 4; type++) {
00259 if ((ret = che_configure(ac, che_pos, type, i, &channels)))
00260 return ret;
00261 }
00262 }
00263
00264 memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00265
00266 avctx->channel_layout = 0;
00267 }
00268
00269 avctx->channels = channels;
00270
00271 ac->output_configured = oc_type;
00272
00273 return 0;
00274 }
00275
00283 static void decode_channel_map(enum ChannelPosition *cpe_map,
00284 enum ChannelPosition *sce_map,
00285 enum ChannelPosition type,
00286 GetBitContext *gb, int n)
00287 {
00288 while (n--) {
00289 enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map;
00290 map[get_bits(gb, 4)] = type;
00291 }
00292 }
00293
00301 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
00302 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00303 GetBitContext *gb)
00304 {
00305 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00306 int comment_len;
00307
00308 skip_bits(gb, 2);
00309
00310 sampling_index = get_bits(gb, 4);
00311 if (m4ac->sampling_index != sampling_index)
00312 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");
00313
00314 num_front = get_bits(gb, 4);
00315 num_side = get_bits(gb, 4);
00316 num_back = get_bits(gb, 4);
00317 num_lfe = get_bits(gb, 2);
00318 num_assoc_data = get_bits(gb, 3);
00319 num_cc = get_bits(gb, 4);
00320
00321 if (get_bits1(gb))
00322 skip_bits(gb, 4);
00323 if (get_bits1(gb))
00324 skip_bits(gb, 4);
00325
00326 if (get_bits1(gb))
00327 skip_bits(gb, 3);
00328
00329 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00330 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
00331 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
00332 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
00333
00334 skip_bits_long(gb, 4 * num_assoc_data);
00335
00336 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
00337
00338 align_get_bits(gb);
00339
00340
00341 comment_len = get_bits(gb, 8) * 8;
00342 if (get_bits_left(gb) < comment_len) {
00343 av_log(avctx, AV_LOG_ERROR, overread_err);
00344 return -1;
00345 }
00346 skip_bits_long(gb, comment_len);
00347 return 0;
00348 }
00349
00358 static av_cold int set_default_channel_config(AVCodecContext *avctx,
00359 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00360 int channel_config)
00361 {
00362 if (channel_config < 1 || channel_config > 7) {
00363 av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00364 channel_config);
00365 return -1;
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 if (channel_config != 2)
00380 new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT;
00381 if (channel_config > 1)
00382 new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT;
00383 if (channel_config == 4)
00384 new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;
00385 if (channel_config > 4)
00386 new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00387 = AAC_CHANNEL_BACK;
00388 if (channel_config > 5)
00389 new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;
00390 if (channel_config == 7)
00391 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT;
00392
00393 return 0;
00394 }
00395
00404 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
00405 GetBitContext *gb,
00406 MPEG4AudioConfig *m4ac,
00407 int channel_config)
00408 {
00409 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00410 int extension_flag, ret;
00411
00412 if (get_bits1(gb)) {
00413 av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
00414 return -1;
00415 }
00416
00417 if (get_bits1(gb))
00418 skip_bits(gb, 14);
00419 extension_flag = get_bits1(gb);
00420
00421 if (m4ac->object_type == AOT_AAC_SCALABLE ||
00422 m4ac->object_type == AOT_ER_AAC_SCALABLE)
00423 skip_bits(gb, 3);
00424
00425 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00426 if (channel_config == 0) {
00427 skip_bits(gb, 4);
00428 if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
00429 return ret;
00430 } else {
00431 if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
00432 return ret;
00433 }
00434
00435 if (count_channels(new_che_pos) > 1) {
00436 m4ac->ps = 0;
00437 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
00438 m4ac->ps = 1;
00439
00440 if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
00441 return ret;
00442
00443 if (extension_flag) {
00444 switch (m4ac->object_type) {
00445 case AOT_ER_BSAC:
00446 skip_bits(gb, 5);
00447 skip_bits(gb, 11);
00448 break;
00449 case AOT_ER_AAC_LC:
00450 case AOT_ER_AAC_LTP:
00451 case AOT_ER_AAC_SCALABLE:
00452 case AOT_ER_AAC_LD:
00453 skip_bits(gb, 3);
00454
00455
00456
00457 break;
00458 }
00459 skip_bits1(gb);
00460 }
00461 return 0;
00462 }
00463
00476 static int decode_audio_specific_config(AACContext *ac,
00477 AVCodecContext *avctx,
00478 MPEG4AudioConfig *m4ac,
00479 const uint8_t *data, int bit_size,
00480 int sync_extension)
00481 {
00482 GetBitContext gb;
00483 int i;
00484
00485 av_dlog(avctx, "extradata size %d\n", avctx->extradata_size);
00486 for (i = 0; i < avctx->extradata_size; i++)
00487 av_dlog(avctx, "%02x ", avctx->extradata[i]);
00488 av_dlog(avctx, "\n");
00489
00490 init_get_bits(&gb, data, bit_size);
00491
00492 if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
00493 return -1;
00494 if (m4ac->sampling_index > 12) {
00495 av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
00496 return -1;
00497 }
00498
00499 skip_bits_long(&gb, i);
00500
00501 switch (m4ac->object_type) {
00502 case AOT_AAC_MAIN:
00503 case AOT_AAC_LC:
00504 case AOT_AAC_LTP:
00505 if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
00506 return -1;
00507 break;
00508 default:
00509 av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00510 m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
00511 return -1;
00512 }
00513
00514 av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
00515 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
00516 m4ac->sample_rate, m4ac->sbr, m4ac->ps);
00517
00518 return get_bits_count(&gb);
00519 }
00520
00528 static av_always_inline int lcg_random(int previous_val)
00529 {
00530 return previous_val * 1664525 + 1013904223;
00531 }
00532
00533 static av_always_inline void reset_predict_state(PredictorState *ps)
00534 {
00535 ps->r0 = 0.0f;
00536 ps->r1 = 0.0f;
00537 ps->cor0 = 0.0f;
00538 ps->cor1 = 0.0f;
00539 ps->var0 = 1.0f;
00540 ps->var1 = 1.0f;
00541 }
00542
00543 static void reset_all_predictors(PredictorState *ps)
00544 {
00545 int i;
00546 for (i = 0; i < MAX_PREDICTORS; i++)
00547 reset_predict_state(&ps[i]);
00548 }
00549
00550 static int sample_rate_idx (int rate)
00551 {
00552 if (92017 <= rate) return 0;
00553 else if (75132 <= rate) return 1;
00554 else if (55426 <= rate) return 2;
00555 else if (46009 <= rate) return 3;
00556 else if (37566 <= rate) return 4;
00557 else if (27713 <= rate) return 5;
00558 else if (23004 <= rate) return 6;
00559 else if (18783 <= rate) return 7;
00560 else if (13856 <= rate) return 8;
00561 else if (11502 <= rate) return 9;
00562 else if (9391 <= rate) return 10;
00563 else return 11;
00564 }
00565
00566 static void reset_predictor_group(PredictorState *ps, int group_num)
00567 {
00568 int i;
00569 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
00570 reset_predict_state(&ps[i]);
00571 }
00572
00573 #define AAC_INIT_VLC_STATIC(num, size) \
00574 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
00575 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
00576 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
00577 size);
00578
00579 static av_cold int aac_decode_init(AVCodecContext *avctx)
00580 {
00581 AACContext *ac = avctx->priv_data;
00582 float output_scale_factor;
00583
00584 ac->avctx = avctx;
00585 ac->m4ac.sample_rate = avctx->sample_rate;
00586
00587 if (avctx->extradata_size > 0) {
00588 if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
00589 avctx->extradata,
00590 avctx->extradata_size*8, 1) < 0)
00591 return -1;
00592 } else {
00593 int sr, i;
00594 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00595
00596 sr = sample_rate_idx(avctx->sample_rate);
00597 ac->m4ac.sampling_index = sr;
00598 ac->m4ac.channels = avctx->channels;
00599 ac->m4ac.sbr = -1;
00600 ac->m4ac.ps = -1;
00601
00602 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
00603 if (ff_mpeg4audio_channels[i] == avctx->channels)
00604 break;
00605 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
00606 i = 0;
00607 }
00608 ac->m4ac.chan_config = i;
00609
00610 if (ac->m4ac.chan_config) {
00611 int ret = set_default_channel_config(avctx, new_che_pos, ac->m4ac.chan_config);
00612 if (!ret)
00613 output_configure(ac, ac->che_pos, new_che_pos, ac->m4ac.chan_config, OC_GLOBAL_HDR);
00614 else if (avctx->err_recognition & AV_EF_EXPLODE)
00615 return AVERROR_INVALIDDATA;
00616 }
00617 }
00618
00619 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
00620 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00621 output_scale_factor = 1.0 / 32768.0;
00622 } else {
00623 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00624 output_scale_factor = 1.0;
00625 }
00626
00627 AAC_INIT_VLC_STATIC( 0, 304);
00628 AAC_INIT_VLC_STATIC( 1, 270);
00629 AAC_INIT_VLC_STATIC( 2, 550);
00630 AAC_INIT_VLC_STATIC( 3, 300);
00631 AAC_INIT_VLC_STATIC( 4, 328);
00632 AAC_INIT_VLC_STATIC( 5, 294);
00633 AAC_INIT_VLC_STATIC( 6, 306);
00634 AAC_INIT_VLC_STATIC( 7, 268);
00635 AAC_INIT_VLC_STATIC( 8, 510);
00636 AAC_INIT_VLC_STATIC( 9, 366);
00637 AAC_INIT_VLC_STATIC(10, 462);
00638
00639 ff_aac_sbr_init();
00640
00641 dsputil_init(&ac->dsp, avctx);
00642 ff_fmt_convert_init(&ac->fmt_conv, avctx);
00643
00644 ac->random_state = 0x1f2e3d4c;
00645
00646 ff_aac_tableinit();
00647
00648 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00649 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00650 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00651 352);
00652
00653 ff_mdct_init(&ac->mdct, 11, 1, output_scale_factor/1024.0);
00654 ff_mdct_init(&ac->mdct_small, 8, 1, output_scale_factor/128.0);
00655 ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0/output_scale_factor);
00656
00657 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00658 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00659 ff_init_ff_sine_windows(10);
00660 ff_init_ff_sine_windows( 7);
00661
00662 cbrt_tableinit();
00663
00664 avcodec_get_frame_defaults(&ac->frame);
00665 avctx->coded_frame = &ac->frame;
00666
00667 return 0;
00668 }
00669
00673 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
00674 {
00675 int byte_align = get_bits1(gb);
00676 int count = get_bits(gb, 8);
00677 if (count == 255)
00678 count += get_bits(gb, 8);
00679 if (byte_align)
00680 align_get_bits(gb);
00681
00682 if (get_bits_left(gb) < 8 * count) {
00683 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00684 return -1;
00685 }
00686 skip_bits_long(gb, 8 * count);
00687 return 0;
00688 }
00689
00690 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
00691 GetBitContext *gb)
00692 {
00693 int sfb;
00694 if (get_bits1(gb)) {
00695 ics->predictor_reset_group = get_bits(gb, 5);
00696 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00697 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00698 return -1;
00699 }
00700 }
00701 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00702 ics->prediction_used[sfb] = get_bits1(gb);
00703 }
00704 return 0;
00705 }
00706
00710 static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
00711 GetBitContext *gb, uint8_t max_sfb)
00712 {
00713 int sfb;
00714
00715 ltp->lag = get_bits(gb, 11);
00716 ltp->coef = ltp_coef[get_bits(gb, 3)];
00717 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
00718 ltp->used[sfb] = get_bits1(gb);
00719 }
00720
00724 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
00725 GetBitContext *gb)
00726 {
00727 if (get_bits1(gb)) {
00728 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
00729 return AVERROR_INVALIDDATA;
00730 }
00731 ics->window_sequence[1] = ics->window_sequence[0];
00732 ics->window_sequence[0] = get_bits(gb, 2);
00733 ics->use_kb_window[1] = ics->use_kb_window[0];
00734 ics->use_kb_window[0] = get_bits1(gb);
00735 ics->num_window_groups = 1;
00736 ics->group_len[0] = 1;
00737 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00738 int i;
00739 ics->max_sfb = get_bits(gb, 4);
00740 for (i = 0; i < 7; i++) {
00741 if (get_bits1(gb)) {
00742 ics->group_len[ics->num_window_groups - 1]++;
00743 } else {
00744 ics->num_window_groups++;
00745 ics->group_len[ics->num_window_groups - 1] = 1;
00746 }
00747 }
00748 ics->num_windows = 8;
00749 ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
00750 ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
00751 ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
00752 ics->predictor_present = 0;
00753 } else {
00754 ics->max_sfb = get_bits(gb, 6);
00755 ics->num_windows = 1;
00756 ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
00757 ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00758 ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
00759 ics->predictor_present = get_bits1(gb);
00760 ics->predictor_reset_group = 0;
00761 if (ics->predictor_present) {
00762 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00763 if (decode_prediction(ac, ics, gb)) {
00764 return AVERROR_INVALIDDATA;
00765 }
00766 } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00767 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00768 return AVERROR_INVALIDDATA;
00769 } else {
00770 if ((ics->ltp.present = get_bits(gb, 1)))
00771 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
00772 }
00773 }
00774 }
00775
00776 if (ics->max_sfb > ics->num_swb) {
00777 av_log(ac->avctx, AV_LOG_ERROR,
00778 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00779 ics->max_sfb, ics->num_swb);
00780 return AVERROR_INVALIDDATA;
00781 }
00782
00783 return 0;
00784 }
00785
00794 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
00795 int band_type_run_end[120], GetBitContext *gb,
00796 IndividualChannelStream *ics)
00797 {
00798 int g, idx = 0;
00799 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00800 for (g = 0; g < ics->num_window_groups; g++) {
00801 int k = 0;
00802 while (k < ics->max_sfb) {
00803 uint8_t sect_end = k;
00804 int sect_len_incr;
00805 int sect_band_type = get_bits(gb, 4);
00806 if (sect_band_type == 12) {
00807 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
00808 return -1;
00809 }
00810 do {
00811 sect_len_incr = get_bits(gb, bits);
00812 sect_end += sect_len_incr;
00813 if (get_bits_left(gb) < 0) {
00814 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00815 return -1;
00816 }
00817 if (sect_end > ics->max_sfb) {
00818 av_log(ac->avctx, AV_LOG_ERROR,
00819 "Number of bands (%d) exceeds limit (%d).\n",
00820 sect_end, ics->max_sfb);
00821 return -1;
00822 }
00823 } while (sect_len_incr == (1 << bits) - 1);
00824 for (; k < sect_end; k++) {
00825 band_type [idx] = sect_band_type;
00826 band_type_run_end[idx++] = sect_end;
00827 }
00828 }
00829 }
00830 return 0;
00831 }
00832
00843 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
00844 unsigned int global_gain,
00845 IndividualChannelStream *ics,
00846 enum BandType band_type[120],
00847 int band_type_run_end[120])
00848 {
00849 int g, i, idx = 0;
00850 int offset[3] = { global_gain, global_gain - 90, 0 };
00851 int clipped_offset;
00852 int noise_flag = 1;
00853 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00854 for (g = 0; g < ics->num_window_groups; g++) {
00855 for (i = 0; i < ics->max_sfb;) {
00856 int run_end = band_type_run_end[idx];
00857 if (band_type[idx] == ZERO_BT) {
00858 for (; i < run_end; i++, idx++)
00859 sf[idx] = 0.;
00860 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00861 for (; i < run_end; i++, idx++) {
00862 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00863 clipped_offset = av_clip(offset[2], -155, 100);
00864 if (offset[2] != clipped_offset) {
00865 av_log_ask_for_sample(ac->avctx, "Intensity stereo "
00866 "position clipped (%d -> %d).\nIf you heard an "
00867 "audible artifact, there may be a bug in the "
00868 "decoder. ", offset[2], clipped_offset);
00869 }
00870 sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
00871 }
00872 } else if (band_type[idx] == NOISE_BT) {
00873 for (; i < run_end; i++, idx++) {
00874 if (noise_flag-- > 0)
00875 offset[1] += get_bits(gb, 9) - 256;
00876 else
00877 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00878 clipped_offset = av_clip(offset[1], -100, 155);
00879 if (offset[1] != clipped_offset) {
00880 av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
00881 "(%d -> %d).\nIf you heard an audible "
00882 "artifact, there may be a bug in the decoder. ",
00883 offset[1], clipped_offset);
00884 }
00885 sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
00886 }
00887 } else {
00888 for (; i < run_end; i++, idx++) {
00889 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00890 if (offset[0] > 255U) {
00891 av_log(ac->avctx, AV_LOG_ERROR,
00892 "%s (%d) out of range.\n", sf_str[0], offset[0]);
00893 return -1;
00894 }
00895 sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
00896 }
00897 }
00898 }
00899 }
00900 return 0;
00901 }
00902
00906 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
00907 const uint16_t *swb_offset, int num_swb)
00908 {
00909 int i, pulse_swb;
00910 pulse->num_pulse = get_bits(gb, 2) + 1;
00911 pulse_swb = get_bits(gb, 6);
00912 if (pulse_swb >= num_swb)
00913 return -1;
00914 pulse->pos[0] = swb_offset[pulse_swb];
00915 pulse->pos[0] += get_bits(gb, 5);
00916 if (pulse->pos[0] > 1023)
00917 return -1;
00918 pulse->amp[0] = get_bits(gb, 4);
00919 for (i = 1; i < pulse->num_pulse; i++) {
00920 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
00921 if (pulse->pos[i] > 1023)
00922 return -1;
00923 pulse->amp[i] = get_bits(gb, 4);
00924 }
00925 return 0;
00926 }
00927
00933 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
00934 GetBitContext *gb, const IndividualChannelStream *ics)
00935 {
00936 int w, filt, i, coef_len, coef_res, coef_compress;
00937 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00938 const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00939 for (w = 0; w < ics->num_windows; w++) {
00940 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00941 coef_res = get_bits1(gb);
00942
00943 for (filt = 0; filt < tns->n_filt[w]; filt++) {
00944 int tmp2_idx;
00945 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
00946
00947 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
00948 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
00949 tns->order[w][filt], tns_max_order);
00950 tns->order[w][filt] = 0;
00951 return -1;
00952 }
00953 if (tns->order[w][filt]) {
00954 tns->direction[w][filt] = get_bits1(gb);
00955 coef_compress = get_bits1(gb);
00956 coef_len = coef_res + 3 - coef_compress;
00957 tmp2_idx = 2 * coef_compress + coef_res;
00958
00959 for (i = 0; i < tns->order[w][filt]; i++)
00960 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00961 }
00962 }
00963 }
00964 }
00965 return 0;
00966 }
00967
00975 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
00976 int ms_present)
00977 {
00978 int idx;
00979 if (ms_present == 1) {
00980 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00981 cpe->ms_mask[idx] = get_bits1(gb);
00982 } else if (ms_present == 2) {
00983 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00984 }
00985 }
00986
00987 #ifndef VMUL2
00988 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
00989 const float *scale)
00990 {
00991 float s = *scale;
00992 *dst++ = v[idx & 15] * s;
00993 *dst++ = v[idx>>4 & 15] * s;
00994 return dst;
00995 }
00996 #endif
00997
00998 #ifndef VMUL4
00999 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
01000 const float *scale)
01001 {
01002 float s = *scale;
01003 *dst++ = v[idx & 3] * s;
01004 *dst++ = v[idx>>2 & 3] * s;
01005 *dst++ = v[idx>>4 & 3] * s;
01006 *dst++ = v[idx>>6 & 3] * s;
01007 return dst;
01008 }
01009 #endif
01010
01011 #ifndef VMUL2S
01012 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
01013 unsigned sign, const float *scale)
01014 {
01015 union av_intfloat32 s0, s1;
01016
01017 s0.f = s1.f = *scale;
01018 s0.i ^= sign >> 1 << 31;
01019 s1.i ^= sign << 31;
01020
01021 *dst++ = v[idx & 15] * s0.f;
01022 *dst++ = v[idx>>4 & 15] * s1.f;
01023
01024 return dst;
01025 }
01026 #endif
01027
01028 #ifndef VMUL4S
01029 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
01030 unsigned sign, const float *scale)
01031 {
01032 unsigned nz = idx >> 12;
01033 union av_intfloat32 s = { .f = *scale };
01034 union av_intfloat32 t;
01035
01036 t.i = s.i ^ (sign & 1U<<31);
01037 *dst++ = v[idx & 3] * t.f;
01038
01039 sign <<= nz & 1; nz >>= 1;
01040 t.i = s.i ^ (sign & 1U<<31);
01041 *dst++ = v[idx>>2 & 3] * t.f;
01042
01043 sign <<= nz & 1; nz >>= 1;
01044 t.i = s.i ^ (sign & 1U<<31);
01045 *dst++ = v[idx>>4 & 3] * t.f;
01046
01047 sign <<= nz & 1; nz >>= 1;
01048 t.i = s.i ^ (sign & 1U<<31);
01049 *dst++ = v[idx>>6 & 3] * t.f;
01050
01051 return dst;
01052 }
01053 #endif
01054
01067 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
01068 GetBitContext *gb, const float sf[120],
01069 int pulse_present, const Pulse *pulse,
01070 const IndividualChannelStream *ics,
01071 enum BandType band_type[120])
01072 {
01073 int i, k, g, idx = 0;
01074 const int c = 1024 / ics->num_windows;
01075 const uint16_t *offsets = ics->swb_offset;
01076 float *coef_base = coef;
01077
01078 for (g = 0; g < ics->num_windows; g++)
01079 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
01080
01081 for (g = 0; g < ics->num_window_groups; g++) {
01082 unsigned g_len = ics->group_len[g];
01083
01084 for (i = 0; i < ics->max_sfb; i++, idx++) {
01085 const unsigned cbt_m1 = band_type[idx] - 1;
01086 float *cfo = coef + offsets[i];
01087 int off_len = offsets[i + 1] - offsets[i];
01088 int group;
01089
01090 if (cbt_m1 >= INTENSITY_BT2 - 1) {
01091 for (group = 0; group < g_len; group++, cfo+=128) {
01092 memset(cfo, 0, off_len * sizeof(float));
01093 }
01094 } else if (cbt_m1 == NOISE_BT - 1) {
01095 for (group = 0; group < g_len; group++, cfo+=128) {
01096 float scale;
01097 float band_energy;
01098
01099 for (k = 0; k < off_len; k++) {
01100 ac->random_state = lcg_random(ac->random_state);
01101 cfo[k] = ac->random_state;
01102 }
01103
01104 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
01105 scale = sf[idx] / sqrtf(band_energy);
01106 ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
01107 }
01108 } else {
01109 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
01110 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
01111 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
01112 OPEN_READER(re, gb);
01113
01114 switch (cbt_m1 >> 1) {
01115 case 0:
01116 for (group = 0; group < g_len; group++, cfo+=128) {
01117 float *cf = cfo;
01118 int len = off_len;
01119
01120 do {
01121 int code;
01122 unsigned cb_idx;
01123
01124 UPDATE_CACHE(re, gb);
01125 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01126 cb_idx = cb_vector_idx[code];
01127 cf = VMUL4(cf, vq, cb_idx, sf + idx);
01128 } while (len -= 4);
01129 }
01130 break;
01131
01132 case 1:
01133 for (group = 0; group < g_len; group++, cfo+=128) {
01134 float *cf = cfo;
01135 int len = off_len;
01136
01137 do {
01138 int code;
01139 unsigned nnz;
01140 unsigned cb_idx;
01141 uint32_t bits;
01142
01143 UPDATE_CACHE(re, gb);
01144 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01145 cb_idx = cb_vector_idx[code];
01146 nnz = cb_idx >> 8 & 15;
01147 bits = nnz ? GET_CACHE(re, gb) : 0;
01148 LAST_SKIP_BITS(re, gb, nnz);
01149 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
01150 } while (len -= 4);
01151 }
01152 break;
01153
01154 case 2:
01155 for (group = 0; group < g_len; group++, cfo+=128) {
01156 float *cf = cfo;
01157 int len = off_len;
01158
01159 do {
01160 int code;
01161 unsigned cb_idx;
01162
01163 UPDATE_CACHE(re, gb);
01164 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01165 cb_idx = cb_vector_idx[code];
01166 cf = VMUL2(cf, vq, cb_idx, sf + idx);
01167 } while (len -= 2);
01168 }
01169 break;
01170
01171 case 3:
01172 case 4:
01173 for (group = 0; group < g_len; group++, cfo+=128) {
01174 float *cf = cfo;
01175 int len = off_len;
01176
01177 do {
01178 int code;
01179 unsigned nnz;
01180 unsigned cb_idx;
01181 unsigned sign;
01182
01183 UPDATE_CACHE(re, gb);
01184 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01185 cb_idx = cb_vector_idx[code];
01186 nnz = cb_idx >> 8 & 15;
01187 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
01188 LAST_SKIP_BITS(re, gb, nnz);
01189 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
01190 } while (len -= 2);
01191 }
01192 break;
01193
01194 default:
01195 for (group = 0; group < g_len; group++, cfo+=128) {
01196 float *cf = cfo;
01197 uint32_t *icf = (uint32_t *) cf;
01198 int len = off_len;
01199
01200 do {
01201 int code;
01202 unsigned nzt, nnz;
01203 unsigned cb_idx;
01204 uint32_t bits;
01205 int j;
01206
01207 UPDATE_CACHE(re, gb);
01208 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01209
01210 if (!code) {
01211 *icf++ = 0;
01212 *icf++ = 0;
01213 continue;
01214 }
01215
01216 cb_idx = cb_vector_idx[code];
01217 nnz = cb_idx >> 12;
01218 nzt = cb_idx >> 8;
01219 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01220 LAST_SKIP_BITS(re, gb, nnz);
01221
01222 for (j = 0; j < 2; j++) {
01223 if (nzt & 1<<j) {
01224 uint32_t b;
01225 int n;
01226
01227
01228 UPDATE_CACHE(re, gb);
01229 b = GET_CACHE(re, gb);
01230 b = 31 - av_log2(~b);
01231
01232 if (b > 8) {
01233 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
01234 return -1;
01235 }
01236
01237 SKIP_BITS(re, gb, b + 1);
01238 b += 4;
01239 n = (1 << b) + SHOW_UBITS(re, gb, b);
01240 LAST_SKIP_BITS(re, gb, b);
01241 *icf++ = cbrt_tab[n] | (bits & 1U<<31);
01242 bits <<= 1;
01243 } else {
01244 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
01245 *icf++ = (bits & 1U<<31) | v;
01246 bits <<= !!v;
01247 }
01248 cb_idx >>= 4;
01249 }
01250 } while (len -= 2);
01251
01252 ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
01253 }
01254 }
01255
01256 CLOSE_READER(re, gb);
01257 }
01258 }
01259 coef += g_len << 7;
01260 }
01261
01262 if (pulse_present) {
01263 idx = 0;
01264 for (i = 0; i < pulse->num_pulse; i++) {
01265 float co = coef_base[ pulse->pos[i] ];
01266 while (offsets[idx + 1] <= pulse->pos[i])
01267 idx++;
01268 if (band_type[idx] != NOISE_BT && sf[idx]) {
01269 float ico = -pulse->amp[i];
01270 if (co) {
01271 co /= sf[idx];
01272 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
01273 }
01274 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
01275 }
01276 }
01277 }
01278 return 0;
01279 }
01280
01281 static av_always_inline float flt16_round(float pf)
01282 {
01283 union av_intfloat32 tmp;
01284 tmp.f = pf;
01285 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
01286 return tmp.f;
01287 }
01288
01289 static av_always_inline float flt16_even(float pf)
01290 {
01291 union av_intfloat32 tmp;
01292 tmp.f = pf;
01293 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
01294 return tmp.f;
01295 }
01296
01297 static av_always_inline float flt16_trunc(float pf)
01298 {
01299 union av_intfloat32 pun;
01300 pun.f = pf;
01301 pun.i &= 0xFFFF0000U;
01302 return pun.f;
01303 }
01304
01305 static av_always_inline void predict(PredictorState *ps, float *coef,
01306 int output_enable)
01307 {
01308 const float a = 0.953125;
01309 const float alpha = 0.90625;
01310 float e0, e1;
01311 float pv;
01312 float k1, k2;
01313 float r0 = ps->r0, r1 = ps->r1;
01314 float cor0 = ps->cor0, cor1 = ps->cor1;
01315 float var0 = ps->var0, var1 = ps->var1;
01316
01317 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
01318 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
01319
01320 pv = flt16_round(k1 * r0 + k2 * r1);
01321 if (output_enable)
01322 *coef += pv;
01323
01324 e0 = *coef;
01325 e1 = e0 - k1 * r0;
01326
01327 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
01328 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
01329 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
01330 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
01331
01332 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
01333 ps->r0 = flt16_trunc(a * e0);
01334 }
01335
01339 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
01340 {
01341 int sfb, k;
01342
01343 if (!sce->ics.predictor_initialized) {
01344 reset_all_predictors(sce->predictor_state);
01345 sce->ics.predictor_initialized = 1;
01346 }
01347
01348 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01349 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
01350 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
01351 predict(&sce->predictor_state[k], &sce->coeffs[k],
01352 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
01353 }
01354 }
01355 if (sce->ics.predictor_reset_group)
01356 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
01357 } else
01358 reset_all_predictors(sce->predictor_state);
01359 }
01360
01369 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
01370 GetBitContext *gb, int common_window, int scale_flag)
01371 {
01372 Pulse pulse;
01373 TemporalNoiseShaping *tns = &sce->tns;
01374 IndividualChannelStream *ics = &sce->ics;
01375 float *out = sce->coeffs;
01376 int global_gain, pulse_present = 0;
01377
01378
01379
01380
01381 pulse.num_pulse = 0;
01382
01383 global_gain = get_bits(gb, 8);
01384
01385 if (!common_window && !scale_flag) {
01386 if (decode_ics_info(ac, ics, gb) < 0)
01387 return AVERROR_INVALIDDATA;
01388 }
01389
01390 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01391 return -1;
01392 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01393 return -1;
01394
01395 pulse_present = 0;
01396 if (!scale_flag) {
01397 if ((pulse_present = get_bits1(gb))) {
01398 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01399 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01400 return -1;
01401 }
01402 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01403 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01404 return -1;
01405 }
01406 }
01407 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01408 return -1;
01409 if (get_bits1(gb)) {
01410 av_log_missing_feature(ac->avctx, "SSR", 1);
01411 return -1;
01412 }
01413 }
01414
01415 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01416 return -1;
01417
01418 if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01419 apply_prediction(ac, sce);
01420
01421 return 0;
01422 }
01423
01427 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
01428 {
01429 const IndividualChannelStream *ics = &cpe->ch[0].ics;
01430 float *ch0 = cpe->ch[0].coeffs;
01431 float *ch1 = cpe->ch[1].coeffs;
01432 int g, i, group, idx = 0;
01433 const uint16_t *offsets = ics->swb_offset;
01434 for (g = 0; g < ics->num_window_groups; g++) {
01435 for (i = 0; i < ics->max_sfb; i++, idx++) {
01436 if (cpe->ms_mask[idx] &&
01437 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01438 for (group = 0; group < ics->group_len[g]; group++) {
01439 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
01440 ch1 + group * 128 + offsets[i],
01441 offsets[i+1] - offsets[i]);
01442 }
01443 }
01444 }
01445 ch0 += ics->group_len[g] * 128;
01446 ch1 += ics->group_len[g] * 128;
01447 }
01448 }
01449
01457 static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
01458 {
01459 const IndividualChannelStream *ics = &cpe->ch[1].ics;
01460 SingleChannelElement *sce1 = &cpe->ch[1];
01461 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01462 const uint16_t *offsets = ics->swb_offset;
01463 int g, group, i, idx = 0;
01464 int c;
01465 float scale;
01466 for (g = 0; g < ics->num_window_groups; g++) {
01467 for (i = 0; i < ics->max_sfb;) {
01468 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01469 const int bt_run_end = sce1->band_type_run_end[idx];
01470 for (; i < bt_run_end; i++, idx++) {
01471 c = -1 + 2 * (sce1->band_type[idx] - 14);
01472 if (ms_present)
01473 c *= 1 - 2 * cpe->ms_mask[idx];
01474 scale = c * sce1->sf[idx];
01475 for (group = 0; group < ics->group_len[g]; group++)
01476 ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
01477 coef0 + group * 128 + offsets[i],
01478 scale,
01479 offsets[i + 1] - offsets[i]);
01480 }
01481 } else {
01482 int bt_run_end = sce1->band_type_run_end[idx];
01483 idx += bt_run_end - i;
01484 i = bt_run_end;
01485 }
01486 }
01487 coef0 += ics->group_len[g] * 128;
01488 coef1 += ics->group_len[g] * 128;
01489 }
01490 }
01491
01497 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
01498 {
01499 int i, ret, common_window, ms_present = 0;
01500
01501 common_window = get_bits1(gb);
01502 if (common_window) {
01503 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
01504 return AVERROR_INVALIDDATA;
01505 i = cpe->ch[1].ics.use_kb_window[0];
01506 cpe->ch[1].ics = cpe->ch[0].ics;
01507 cpe->ch[1].ics.use_kb_window[1] = i;
01508 if (cpe->ch[1].ics.predictor_present && (ac->m4ac.object_type != AOT_AAC_MAIN))
01509 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
01510 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
01511 ms_present = get_bits(gb, 2);
01512 if (ms_present == 3) {
01513 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01514 return -1;
01515 } else if (ms_present)
01516 decode_mid_side_stereo(cpe, gb, ms_present);
01517 }
01518 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01519 return ret;
01520 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01521 return ret;
01522
01523 if (common_window) {
01524 if (ms_present)
01525 apply_mid_side_stereo(ac, cpe);
01526 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01527 apply_prediction(ac, &cpe->ch[0]);
01528 apply_prediction(ac, &cpe->ch[1]);
01529 }
01530 }
01531
01532 apply_intensity_stereo(ac, cpe, ms_present);
01533 return 0;
01534 }
01535
01536 static const float cce_scale[] = {
01537 1.09050773266525765921,
01538 1.18920711500272106672,
01539 M_SQRT2,
01540 2,
01541 };
01542
01548 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
01549 {
01550 int num_gain = 0;
01551 int c, g, sfb, ret;
01552 int sign;
01553 float scale;
01554 SingleChannelElement *sce = &che->ch[0];
01555 ChannelCoupling *coup = &che->coup;
01556
01557 coup->coupling_point = 2 * get_bits1(gb);
01558 coup->num_coupled = get_bits(gb, 3);
01559 for (c = 0; c <= coup->num_coupled; c++) {
01560 num_gain++;
01561 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01562 coup->id_select[c] = get_bits(gb, 4);
01563 if (coup->type[c] == TYPE_CPE) {
01564 coup->ch_select[c] = get_bits(gb, 2);
01565 if (coup->ch_select[c] == 3)
01566 num_gain++;
01567 } else
01568 coup->ch_select[c] = 2;
01569 }
01570 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
01571
01572 sign = get_bits(gb, 1);
01573 scale = cce_scale[get_bits(gb, 2)];
01574
01575 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01576 return ret;
01577
01578 for (c = 0; c < num_gain; c++) {
01579 int idx = 0;
01580 int cge = 1;
01581 int gain = 0;
01582 float gain_cache = 1.;
01583 if (c) {
01584 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01585 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01586 gain_cache = powf(scale, -gain);
01587 }
01588 if (coup->coupling_point == AFTER_IMDCT) {
01589 coup->gain[c][0] = gain_cache;
01590 } else {
01591 for (g = 0; g < sce->ics.num_window_groups; g++) {
01592 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01593 if (sce->band_type[idx] != ZERO_BT) {
01594 if (!cge) {
01595 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01596 if (t) {
01597 int s = 1;
01598 t = gain += t;
01599 if (sign) {
01600 s -= 2 * (t & 0x1);
01601 t >>= 1;
01602 }
01603 gain_cache = powf(scale, -t) * s;
01604 }
01605 }
01606 coup->gain[c][idx] = gain_cache;
01607 }
01608 }
01609 }
01610 }
01611 }
01612 return 0;
01613 }
01614
01620 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
01621 GetBitContext *gb)
01622 {
01623 int i;
01624 int num_excl_chan = 0;
01625
01626 do {
01627 for (i = 0; i < 7; i++)
01628 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01629 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01630
01631 return num_excl_chan / 7;
01632 }
01633
01641 static int decode_dynamic_range(DynamicRangeControl *che_drc,
01642 GetBitContext *gb, int cnt)
01643 {
01644 int n = 1;
01645 int drc_num_bands = 1;
01646 int i;
01647
01648
01649 if (get_bits1(gb)) {
01650 che_drc->pce_instance_tag = get_bits(gb, 4);
01651 skip_bits(gb, 4);
01652 n++;
01653 }
01654
01655
01656 if (get_bits1(gb)) {
01657 n += decode_drc_channel_exclusions(che_drc, gb);
01658 }
01659
01660
01661 if (get_bits1(gb)) {
01662 che_drc->band_incr = get_bits(gb, 4);
01663 che_drc->interpolation_scheme = get_bits(gb, 4);
01664 n++;
01665 drc_num_bands += che_drc->band_incr;
01666 for (i = 0; i < drc_num_bands; i++) {
01667 che_drc->band_top[i] = get_bits(gb, 8);
01668 n++;
01669 }
01670 }
01671
01672
01673 if (get_bits1(gb)) {
01674 che_drc->prog_ref_level = get_bits(gb, 7);
01675 skip_bits1(gb);
01676 n++;
01677 }
01678
01679 for (i = 0; i < drc_num_bands; i++) {
01680 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01681 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01682 n++;
01683 }
01684
01685 return n;
01686 }
01687
01695 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
01696 ChannelElement *che, enum RawDataBlockType elem_type)
01697 {
01698 int crc_flag = 0;
01699 int res = cnt;
01700 switch (get_bits(gb, 4)) {
01701 case EXT_SBR_DATA_CRC:
01702 crc_flag++;
01703 case EXT_SBR_DATA:
01704 if (!che) {
01705 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
01706 return res;
01707 } else if (!ac->m4ac.sbr) {
01708 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
01709 skip_bits_long(gb, 8 * cnt - 4);
01710 return res;
01711 } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
01712 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
01713 skip_bits_long(gb, 8 * cnt - 4);
01714 return res;
01715 } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
01716 ac->m4ac.sbr = 1;
01717 ac->m4ac.ps = 1;
01718 output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
01719 } else {
01720 ac->m4ac.sbr = 1;
01721 }
01722 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
01723 break;
01724 case EXT_DYNAMIC_RANGE:
01725 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01726 break;
01727 case EXT_FILL:
01728 case EXT_FILL_DATA:
01729 case EXT_DATA_ELEMENT:
01730 default:
01731 skip_bits_long(gb, 8 * cnt - 4);
01732 break;
01733 };
01734 return res;
01735 }
01736
01743 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
01744 IndividualChannelStream *ics, int decode)
01745 {
01746 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
01747 int w, filt, m, i;
01748 int bottom, top, order, start, end, size, inc;
01749 float lpc[TNS_MAX_ORDER];
01750 float tmp[TNS_MAX_ORDER + 1];
01751
01752 for (w = 0; w < ics->num_windows; w++) {
01753 bottom = ics->num_swb;
01754 for (filt = 0; filt < tns->n_filt[w]; filt++) {
01755 top = bottom;
01756 bottom = FFMAX(0, top - tns->length[w][filt]);
01757 order = tns->order[w][filt];
01758 if (order == 0)
01759 continue;
01760
01761
01762 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01763
01764 start = ics->swb_offset[FFMIN(bottom, mmm)];
01765 end = ics->swb_offset[FFMIN( top, mmm)];
01766 if ((size = end - start) <= 0)
01767 continue;
01768 if (tns->direction[w][filt]) {
01769 inc = -1;
01770 start = end - 1;
01771 } else {
01772 inc = 1;
01773 }
01774 start += w * 128;
01775
01776 if (decode) {
01777
01778 for (m = 0; m < size; m++, start += inc)
01779 for (i = 1; i <= FFMIN(m, order); i++)
01780 coef[start] -= coef[start - i * inc] * lpc[i - 1];
01781 } else {
01782
01783 for (m = 0; m < size; m++, start += inc) {
01784 tmp[0] = coef[start];
01785 for (i = 1; i <= FFMIN(m, order); i++)
01786 coef[start] += tmp[i] * lpc[i - 1];
01787 for (i = order; i > 0; i--)
01788 tmp[i] = tmp[i - 1];
01789 }
01790 }
01791 }
01792 }
01793 }
01794
01799 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
01800 float *in, IndividualChannelStream *ics)
01801 {
01802 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01803 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01804 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01805 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01806
01807 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
01808 ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
01809 } else {
01810 memset(in, 0, 448 * sizeof(float));
01811 ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
01812 }
01813 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
01814 ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
01815 } else {
01816 ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
01817 memset(in + 1024 + 576, 0, 448 * sizeof(float));
01818 }
01819 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
01820 }
01821
01825 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
01826 {
01827 const LongTermPrediction *ltp = &sce->ics.ltp;
01828 const uint16_t *offsets = sce->ics.swb_offset;
01829 int i, sfb;
01830
01831 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01832 float *predTime = sce->ret;
01833 float *predFreq = ac->buf_mdct;
01834 int16_t num_samples = 2048;
01835
01836 if (ltp->lag < 1024)
01837 num_samples = ltp->lag + 1024;
01838 for (i = 0; i < num_samples; i++)
01839 predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
01840 memset(&predTime[i], 0, (2048 - i) * sizeof(float));
01841
01842 windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
01843
01844 if (sce->tns.present)
01845 apply_tns(predFreq, &sce->tns, &sce->ics, 0);
01846
01847 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
01848 if (ltp->used[sfb])
01849 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
01850 sce->coeffs[i] += predFreq[i];
01851 }
01852 }
01853
01857 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
01858 {
01859 IndividualChannelStream *ics = &sce->ics;
01860 float *saved = sce->saved;
01861 float *saved_ltp = sce->coeffs;
01862 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01863 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01864 int i;
01865
01866 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01867 memcpy(saved_ltp, saved, 512 * sizeof(float));
01868 memset(saved_ltp + 576, 0, 448 * sizeof(float));
01869 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
01870 for (i = 0; i < 64; i++)
01871 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
01872 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01873 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
01874 memset(saved_ltp + 576, 0, 448 * sizeof(float));
01875 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
01876 for (i = 0; i < 64; i++)
01877 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
01878 } else {
01879 ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
01880 for (i = 0; i < 512; i++)
01881 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
01882 }
01883
01884 memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
01885 memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
01886 memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
01887 }
01888
01892 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
01893 {
01894 IndividualChannelStream *ics = &sce->ics;
01895 float *in = sce->coeffs;
01896 float *out = sce->ret;
01897 float *saved = sce->saved;
01898 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01899 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01900 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01901 float *buf = ac->buf_mdct;
01902 float *temp = ac->temp;
01903 int i;
01904
01905
01906 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01907 for (i = 0; i < 1024; i += 128)
01908 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
01909 } else
01910 ac->mdct.imdct_half(&ac->mdct, buf, in);
01911
01912
01913
01914
01915
01916
01917
01918 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01919 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01920 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
01921 } else {
01922 memcpy( out, saved, 448 * sizeof(float));
01923
01924 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01925 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
01926 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
01927 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
01928 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
01929 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
01930 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
01931 } else {
01932 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
01933 memcpy( out + 576, buf + 64, 448 * sizeof(float));
01934 }
01935 }
01936
01937
01938 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01939 memcpy( saved, temp + 64, 64 * sizeof(float));
01940 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
01941 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
01942 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
01943 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01944 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01945 memcpy( saved, buf + 512, 448 * sizeof(float));
01946 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01947 } else {
01948 memcpy( saved, buf + 512, 512 * sizeof(float));
01949 }
01950 }
01951
01957 static void apply_dependent_coupling(AACContext *ac,
01958 SingleChannelElement *target,
01959 ChannelElement *cce, int index)
01960 {
01961 IndividualChannelStream *ics = &cce->ch[0].ics;
01962 const uint16_t *offsets = ics->swb_offset;
01963 float *dest = target->coeffs;
01964 const float *src = cce->ch[0].coeffs;
01965 int g, i, group, k, idx = 0;
01966 if (ac->m4ac.object_type == AOT_AAC_LTP) {
01967 av_log(ac->avctx, AV_LOG_ERROR,
01968 "Dependent coupling is not supported together with LTP\n");
01969 return;
01970 }
01971 for (g = 0; g < ics->num_window_groups; g++) {
01972 for (i = 0; i < ics->max_sfb; i++, idx++) {
01973 if (cce->ch[0].band_type[idx] != ZERO_BT) {
01974 const float gain = cce->coup.gain[index][idx];
01975 for (group = 0; group < ics->group_len[g]; group++) {
01976 for (k = offsets[i]; k < offsets[i + 1]; k++) {
01977
01978 dest[group * 128 + k] += gain * src[group * 128 + k];
01979 }
01980 }
01981 }
01982 }
01983 dest += ics->group_len[g] * 128;
01984 src += ics->group_len[g] * 128;
01985 }
01986 }
01987
01993 static void apply_independent_coupling(AACContext *ac,
01994 SingleChannelElement *target,
01995 ChannelElement *cce, int index)
01996 {
01997 int i;
01998 const float gain = cce->coup.gain[index][0];
01999 const float *src = cce->ch[0].ret;
02000 float *dest = target->ret;
02001 const int len = 1024 << (ac->m4ac.sbr == 1);
02002
02003 for (i = 0; i < len; i++)
02004 dest[i] += gain * src[i];
02005 }
02006
02012 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
02013 enum RawDataBlockType type, int elem_id,
02014 enum CouplingPoint coupling_point,
02015 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
02016 {
02017 int i, c;
02018
02019 for (i = 0; i < MAX_ELEM_ID; i++) {
02020 ChannelElement *cce = ac->che[TYPE_CCE][i];
02021 int index = 0;
02022
02023 if (cce && cce->coup.coupling_point == coupling_point) {
02024 ChannelCoupling *coup = &cce->coup;
02025
02026 for (c = 0; c <= coup->num_coupled; c++) {
02027 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
02028 if (coup->ch_select[c] != 1) {
02029 apply_coupling_method(ac, &cc->ch[0], cce, index);
02030 if (coup->ch_select[c] != 0)
02031 index++;
02032 }
02033 if (coup->ch_select[c] != 2)
02034 apply_coupling_method(ac, &cc->ch[1], cce, index++);
02035 } else
02036 index += 1 + (coup->ch_select[c] == 3);
02037 }
02038 }
02039 }
02040 }
02041
02045 static void spectral_to_sample(AACContext *ac)
02046 {
02047 int i, type;
02048 for (type = 3; type >= 0; type--) {
02049 for (i = 0; i < MAX_ELEM_ID; i++) {
02050 ChannelElement *che = ac->che[type][i];
02051 if (che) {
02052 if (type <= TYPE_CPE)
02053 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
02054 if (ac->m4ac.object_type == AOT_AAC_LTP) {
02055 if (che->ch[0].ics.predictor_present) {
02056 if (che->ch[0].ics.ltp.present)
02057 apply_ltp(ac, &che->ch[0]);
02058 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
02059 apply_ltp(ac, &che->ch[1]);
02060 }
02061 }
02062 if (che->ch[0].tns.present)
02063 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
02064 if (che->ch[1].tns.present)
02065 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
02066 if (type <= TYPE_CPE)
02067 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
02068 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
02069 imdct_and_windowing(ac, &che->ch[0]);
02070 if (ac->m4ac.object_type == AOT_AAC_LTP)
02071 update_ltp(ac, &che->ch[0]);
02072 if (type == TYPE_CPE) {
02073 imdct_and_windowing(ac, &che->ch[1]);
02074 if (ac->m4ac.object_type == AOT_AAC_LTP)
02075 update_ltp(ac, &che->ch[1]);
02076 }
02077 if (ac->m4ac.sbr > 0) {
02078 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
02079 }
02080 }
02081 if (type <= TYPE_CCE)
02082 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
02083 }
02084 }
02085 }
02086 }
02087
02088 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
02089 {
02090 int size;
02091 AACADTSHeaderInfo hdr_info;
02092
02093 size = avpriv_aac_parse_header(gb, &hdr_info);
02094 if (size > 0) {
02095 if (hdr_info.chan_config) {
02096 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
02097 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
02098 ac->m4ac.chan_config = hdr_info.chan_config;
02099 if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
02100 return -7;
02101 if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config,
02102 FFMAX(ac->output_configured, OC_TRIAL_FRAME)))
02103 return -7;
02104 } else if (ac->output_configured != OC_LOCKED) {
02105 ac->m4ac.chan_config = 0;
02106 ac->output_configured = OC_NONE;
02107 }
02108 if (ac->output_configured != OC_LOCKED) {
02109 ac->m4ac.sbr = -1;
02110 ac->m4ac.ps = -1;
02111 ac->m4ac.sample_rate = hdr_info.sample_rate;
02112 ac->m4ac.sampling_index = hdr_info.sampling_index;
02113 ac->m4ac.object_type = hdr_info.object_type;
02114 }
02115 if (!ac->avctx->sample_rate)
02116 ac->avctx->sample_rate = hdr_info.sample_rate;
02117 if (hdr_info.num_aac_frames == 1) {
02118 if (!hdr_info.crc_absent)
02119 skip_bits(gb, 16);
02120 } else {
02121 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
02122 return -1;
02123 }
02124 }
02125 return size;
02126 }
02127
02128 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
02129 int *got_frame_ptr, GetBitContext *gb)
02130 {
02131 AACContext *ac = avctx->priv_data;
02132 ChannelElement *che = NULL, *che_prev = NULL;
02133 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
02134 int err, elem_id;
02135 int samples = 0, multiplier, audio_found = 0;
02136
02137 if (show_bits(gb, 12) == 0xfff) {
02138 if (parse_adts_frame_header(ac, gb) < 0) {
02139 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
02140 return -1;
02141 }
02142 if (ac->m4ac.sampling_index > 12) {
02143 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
02144 return -1;
02145 }
02146 }
02147
02148 ac->tags_mapped = 0;
02149
02150 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
02151 elem_id = get_bits(gb, 4);
02152
02153 if (elem_type < TYPE_DSE) {
02154 if (!(che=get_che(ac, elem_type, elem_id))) {
02155 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
02156 elem_type, elem_id);
02157 return -1;
02158 }
02159 samples = 1024;
02160 }
02161
02162 switch (elem_type) {
02163
02164 case TYPE_SCE:
02165 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
02166 audio_found = 1;
02167 break;
02168
02169 case TYPE_CPE:
02170 err = decode_cpe(ac, gb, che);
02171 audio_found = 1;
02172 break;
02173
02174 case TYPE_CCE:
02175 err = decode_cce(ac, gb, che);
02176 break;
02177
02178 case TYPE_LFE:
02179 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
02180 audio_found = 1;
02181 break;
02182
02183 case TYPE_DSE:
02184 err = skip_data_stream_element(ac, gb);
02185 break;
02186
02187 case TYPE_PCE: {
02188 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
02189 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
02190 if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
02191 break;
02192 if (ac->output_configured > OC_TRIAL_PCE)
02193 av_log(avctx, AV_LOG_ERROR,
02194 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
02195 else
02196 err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
02197 break;
02198 }
02199
02200 case TYPE_FIL:
02201 if (elem_id == 15)
02202 elem_id += get_bits(gb, 8) - 1;
02203 if (get_bits_left(gb) < 8 * elem_id) {
02204 av_log(avctx, AV_LOG_ERROR, overread_err);
02205 return -1;
02206 }
02207 while (elem_id > 0)
02208 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
02209 err = 0;
02210 break;
02211
02212 default:
02213 err = -1;
02214 break;
02215 }
02216
02217 che_prev = che;
02218 elem_type_prev = elem_type;
02219
02220 if (err)
02221 return err;
02222
02223 if (get_bits_left(gb) < 3) {
02224 av_log(avctx, AV_LOG_ERROR, overread_err);
02225 return -1;
02226 }
02227 }
02228
02229 spectral_to_sample(ac);
02230
02231 multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
02232 samples <<= multiplier;
02233 if (ac->output_configured < OC_LOCKED) {
02234 avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
02235 avctx->frame_size = samples;
02236 }
02237
02238 if (samples) {
02239
02240 ac->frame.nb_samples = samples;
02241 if ((err = avctx->get_buffer(avctx, &ac->frame)) < 0) {
02242 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
02243 return err;
02244 }
02245
02246 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
02247 ac->fmt_conv.float_interleave((float *)ac->frame.data[0],
02248 (const float **)ac->output_data,
02249 samples, avctx->channels);
02250 else
02251 ac->fmt_conv.float_to_int16_interleave((int16_t *)ac->frame.data[0],
02252 (const float **)ac->output_data,
02253 samples, avctx->channels);
02254
02255 *(AVFrame *)data = ac->frame;
02256 }
02257 *got_frame_ptr = !!samples;
02258
02259 if (ac->output_configured && audio_found)
02260 ac->output_configured = OC_LOCKED;
02261
02262 return 0;
02263 }
02264
02265 static int aac_decode_frame(AVCodecContext *avctx, void *data,
02266 int *got_frame_ptr, AVPacket *avpkt)
02267 {
02268 AACContext *ac = avctx->priv_data;
02269 const uint8_t *buf = avpkt->data;
02270 int buf_size = avpkt->size;
02271 GetBitContext gb;
02272 int buf_consumed;
02273 int buf_offset;
02274 int err;
02275 int new_extradata_size;
02276 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
02277 AV_PKT_DATA_NEW_EXTRADATA,
02278 &new_extradata_size);
02279
02280 if (new_extradata) {
02281 av_free(avctx->extradata);
02282 avctx->extradata = av_mallocz(new_extradata_size +
02283 FF_INPUT_BUFFER_PADDING_SIZE);
02284 if (!avctx->extradata)
02285 return AVERROR(ENOMEM);
02286 avctx->extradata_size = new_extradata_size;
02287 memcpy(avctx->extradata, new_extradata, new_extradata_size);
02288 if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
02289 avctx->extradata,
02290 avctx->extradata_size*8, 1) < 0)
02291 return AVERROR_INVALIDDATA;
02292 }
02293
02294 init_get_bits(&gb, buf, buf_size * 8);
02295
02296 if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb)) < 0)
02297 return err;
02298
02299 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
02300 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
02301 if (buf[buf_offset])
02302 break;
02303
02304 return buf_size > buf_offset ? buf_consumed : buf_size;
02305 }
02306
02307 static av_cold int aac_decode_close(AVCodecContext *avctx)
02308 {
02309 AACContext *ac = avctx->priv_data;
02310 int i, type;
02311
02312 for (i = 0; i < MAX_ELEM_ID; i++) {
02313 for (type = 0; type < 4; type++) {
02314 if (ac->che[type][i])
02315 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
02316 av_freep(&ac->che[type][i]);
02317 }
02318 }
02319
02320 ff_mdct_end(&ac->mdct);
02321 ff_mdct_end(&ac->mdct_small);
02322 ff_mdct_end(&ac->mdct_ltp);
02323 return 0;
02324 }
02325
02326
02327 #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
02328
02329 struct LATMContext {
02330 AACContext aac_ctx;
02331 int initialized;
02332
02333
02334 int audio_mux_version_A;
02335 int frame_length_type;
02336 int frame_length;
02337 };
02338
02339 static inline uint32_t latm_get_value(GetBitContext *b)
02340 {
02341 int length = get_bits(b, 2);
02342
02343 return get_bits_long(b, (length+1)*8);
02344 }
02345
02346 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
02347 GetBitContext *gb, int asclen)
02348 {
02349 AACContext *ac = &latmctx->aac_ctx;
02350 AVCodecContext *avctx = ac->avctx;
02351 MPEG4AudioConfig m4ac = {0};
02352 int config_start_bit = get_bits_count(gb);
02353 int sync_extension = 0;
02354 int bits_consumed, esize;
02355
02356 if (asclen) {
02357 sync_extension = 1;
02358 asclen = FFMIN(asclen, get_bits_left(gb));
02359 } else
02360 asclen = get_bits_left(gb);
02361
02362 if (config_start_bit % 8) {
02363 av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
02364 "config not byte aligned.\n", 1);
02365 return AVERROR_INVALIDDATA;
02366 }
02367 if (asclen <= 0)
02368 return AVERROR_INVALIDDATA;
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 };