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