• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/aacdec.c

Go to the documentation of this file.
00001 /*
00002  * AAC decoder
00003  * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
00004  * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00030 /*
00031  * supported tools
00032  *
00033  * Support?             Name
00034  * N (code in SoC repo) gain control
00035  * Y                    block switching
00036  * Y                    window shapes - standard
00037  * N                    window shapes - Low Delay
00038  * Y                    filterbank - standard
00039  * N (code in SoC repo) filterbank - Scalable Sample Rate
00040  * Y                    Temporal Noise Shaping
00041  * N (code in SoC repo) Long Term Prediction
00042  * Y                    intensity stereo
00043  * Y                    channel coupling
00044  * Y                    frequency domain prediction
00045  * Y                    Perceptual Noise Substitution
00046  * Y                    Mid/Side stereo
00047  * N                    Scalable Inverse AAC Quantization
00048  * N                    Frequency Selective Switch
00049  * N                    upsampling filter
00050  * Y                    quantization & coding - AAC
00051  * N                    quantization & coding - TwinVQ
00052  * N                    quantization & coding - BSAC
00053  * N                    AAC Error Resilience tools
00054  * N                    Error Resilience payload syntax
00055  * N                    Error Protection tool
00056  * N                    CELP
00057  * N                    Silence Compression
00058  * N                    HVXC
00059  * N                    HVXC 4kbits/s VR
00060  * N                    Structured Audio tools
00061  * N                    Structured Audio Sample Bank Format
00062  * N                    MIDI
00063  * N                    Harmonic and Individual Lines plus Noise
00064  * N                    Text-To-Speech Interface
00065  * Y                    Spectral Band Replication
00066  * Y (not in this code) Layer-1
00067  * Y (not in this code) Layer-2
00068  * Y (not in this code) Layer-3
00069  * N                    SinuSoidal Coding (Transient, Sinusoid, Noise)
00070  * Y                    Parametric Stereo
00071  * N                    Direct Stream Transfer
00072  *
00073  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
00074  *       - HE AAC v2 comprises LC AAC with Spectral Band Replication and
00075            Parametric Stereo.
00076  */
00077 
00078 
00079 #include "avcodec.h"
00080 #include "internal.h"
00081 #include "get_bits.h"
00082 #include "dsputil.h"
00083 #include "fft.h"
00084 #include "lpc.h"
00085 
00086 #include "aac.h"
00087 #include "aactab.h"
00088 #include "aacdectab.h"
00089 #include "cbrt_tablegen.h"
00090 #include "sbr.h"
00091 #include "aacsbr.h"
00092 #include "mpeg4audio.h"
00093 #include "aac_parser.h"
00094 
00095 #include <assert.h>
00096 #include <errno.h>
00097 #include <math.h>
00098 #include <string.h>
00099 
00100 #if ARCH_ARM
00101 #   include "arm/aac.h"
00102 #endif
00103 
00104 union float754 {
00105     float f;
00106     uint32_t i;
00107 };
00108 
00109 static VLC vlc_scalefactors;
00110 static VLC vlc_spectral[11];
00111 
00112 static const char overread_err[] = "Input buffer exhausted before END element found\n";
00113 
00114 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
00115 {
00116     /* Some buggy encoders appear to set all elem_ids to zero and rely on
00117     channels always occurring in the same order. This is expressly forbidden
00118     by the spec but we will try to work around it.
00119     */
00120     int err_printed = 0;
00121     while (ac->tags_seen_this_frame[type][elem_id] && elem_id < MAX_ELEM_ID) {
00122         if (ac->output_configured < OC_LOCKED && !err_printed) {
00123             av_log(ac->avctx, AV_LOG_WARNING, "Duplicate channel tag found, attempting to remap.\n");
00124             err_printed = 1;
00125         }
00126         elem_id++;
00127     }
00128     if (elem_id == MAX_ELEM_ID)
00129         return NULL;
00130     ac->tags_seen_this_frame[type][elem_id] = 1;
00131 
00132     if (ac->tag_che_map[type][elem_id]) {
00133         return ac->tag_che_map[type][elem_id];
00134     }
00135     if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
00136         return NULL;
00137     }
00138     switch (ac->m4ac.chan_config) {
00139     case 7:
00140         if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00141             ac->tags_mapped++;
00142             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00143         }
00144     case 6:
00145         /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
00146            instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
00147            encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
00148         if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00149             ac->tags_mapped++;
00150             return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00151         }
00152     case 5:
00153         if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00154             ac->tags_mapped++;
00155             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00156         }
00157     case 4:
00158         if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
00159             ac->tags_mapped++;
00160             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00161         }
00162     case 3:
00163     case 2:
00164         if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
00165             ac->tags_mapped++;
00166             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00167         } else if (ac->m4ac.chan_config == 2) {
00168             return NULL;
00169         }
00170     case 1:
00171         if (!ac->tags_mapped && type == TYPE_SCE) {
00172             ac->tags_mapped++;
00173             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00174         }
00175     default:
00176         return NULL;
00177     }
00178 }
00179 
00192 static av_cold int che_configure(AACContext *ac,
00193                          enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00194                          int type, int id,
00195                          int *channels)
00196 {
00197     if (che_pos[type][id]) {
00198         if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
00199             return AVERROR(ENOMEM);
00200         ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
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         ac->tags_mapped = 0;
00246 
00247         avctx->channel_layout = aac_channel_layout[channel_config - 1];
00248     } else {
00249         /* Allocate or free elements depending on if they are in the
00250          * current program configuration.
00251          *
00252          * Set up default 1:1 output mapping.
00253          *
00254          * For a 5.1 stream the output order will be:
00255          *    [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
00256          */
00257 
00258         for (i = 0; i < MAX_ELEM_ID; i++) {
00259             for (type = 0; type < 4; type++) {
00260                 if ((ret = che_configure(ac, che_pos, type, i, &channels)))
00261                     return ret;
00262             }
00263         }
00264 
00265         memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00266         ac->tags_mapped = 4 * MAX_ELEM_ID;
00267 
00268         avctx->channel_layout = 0;
00269     }
00270 
00271     avctx->channels = channels;
00272 
00273     ac->output_configured = oc_type;
00274 
00275     return 0;
00276 }
00277 
00285 static void decode_channel_map(enum ChannelPosition *cpe_map,
00286                                enum ChannelPosition *sce_map,
00287                                enum ChannelPosition type,
00288                                GetBitContext *gb, int n)
00289 {
00290     while (n--) {
00291         enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
00292         map[get_bits(gb, 4)] = type;
00293     }
00294 }
00295 
00303 static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00304                       GetBitContext *gb)
00305 {
00306     int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00307     int comment_len;
00308 
00309     skip_bits(gb, 2);  // object_type
00310 
00311     sampling_index = get_bits(gb, 4);
00312     if (ac->m4ac.sampling_index != sampling_index)
00313         av_log(ac->avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
00314 
00315     num_front       = get_bits(gb, 4);
00316     num_side        = get_bits(gb, 4);
00317     num_back        = get_bits(gb, 4);
00318     num_lfe         = get_bits(gb, 2);
00319     num_assoc_data  = get_bits(gb, 3);
00320     num_cc          = get_bits(gb, 4);
00321 
00322     if (get_bits1(gb))
00323         skip_bits(gb, 4); // mono_mixdown_tag
00324     if (get_bits1(gb))
00325         skip_bits(gb, 4); // stereo_mixdown_tag
00326 
00327     if (get_bits1(gb))
00328         skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
00329 
00330     decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00331     decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE,  gb, num_side );
00332     decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK,  gb, num_back );
00333     decode_channel_map(NULL,                  new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE,   gb, num_lfe  );
00334 
00335     skip_bits_long(gb, 4 * num_assoc_data);
00336 
00337     decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC,    gb, num_cc   );
00338 
00339     align_get_bits(gb);
00340 
00341     /* comment field, first byte is length */
00342     comment_len = get_bits(gb, 8) * 8;
00343     if (get_bits_left(gb) < comment_len) {
00344         av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00345         return -1;
00346     }
00347     skip_bits_long(gb, comment_len);
00348     return 0;
00349 }
00350 
00359 static av_cold int set_default_channel_config(AACContext *ac,
00360                                       enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00361                                       int channel_config)
00362 {
00363     if (channel_config < 1 || channel_config > 7) {
00364         av_log(ac->avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00365                channel_config);
00366         return -1;
00367     }
00368 
00369     /* default channel configurations:
00370      *
00371      * 1ch : front center (mono)
00372      * 2ch : L + R (stereo)
00373      * 3ch : front center + L + R
00374      * 4ch : front center + L + R + back center
00375      * 5ch : front center + L + R + back stereo
00376      * 6ch : front center + L + R + back stereo + LFE
00377      * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
00378      */
00379 
00380     if (channel_config != 2)
00381         new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
00382     if (channel_config > 1)
00383         new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
00384     if (channel_config == 4)
00385         new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;  // back center
00386     if (channel_config > 4)
00387         new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00388         = AAC_CHANNEL_BACK;  // back stereo
00389     if (channel_config > 5)
00390         new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;   // LFE
00391     if (channel_config == 7)
00392         new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
00393 
00394     return 0;
00395 }
00396 
00402 static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
00403                                      int channel_config)
00404 {
00405     enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00406     int extension_flag, ret;
00407 
00408     if (get_bits1(gb)) { // frameLengthFlag
00409         av_log_missing_feature(ac->avctx, "960/120 MDCT window is", 1);
00410         return -1;
00411     }
00412 
00413     if (get_bits1(gb))       // dependsOnCoreCoder
00414         skip_bits(gb, 14);   // coreCoderDelay
00415     extension_flag = get_bits1(gb);
00416 
00417     if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
00418         ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
00419         skip_bits(gb, 3);     // layerNr
00420 
00421     memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00422     if (channel_config == 0) {
00423         skip_bits(gb, 4);  // element_instance_tag
00424         if ((ret = decode_pce(ac, new_che_pos, gb)))
00425             return ret;
00426     } else {
00427         if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
00428             return ret;
00429     }
00430     if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
00431         return ret;
00432 
00433     if (extension_flag) {
00434         switch (ac->m4ac.object_type) {
00435         case AOT_ER_BSAC:
00436             skip_bits(gb, 5);    // numOfSubFrame
00437             skip_bits(gb, 11);   // layer_length
00438             break;
00439         case AOT_ER_AAC_LC:
00440         case AOT_ER_AAC_LTP:
00441         case AOT_ER_AAC_SCALABLE:
00442         case AOT_ER_AAC_LD:
00443             skip_bits(gb, 3);  /* aacSectionDataResilienceFlag
00444                                     * aacScalefactorDataResilienceFlag
00445                                     * aacSpectralDataResilienceFlag
00446                                     */
00447             break;
00448         }
00449         skip_bits1(gb);    // extensionFlag3 (TBD in version 3)
00450     }
00451     return 0;
00452 }
00453 
00462 static int decode_audio_specific_config(AACContext *ac, void *data,
00463                                         int data_size)
00464 {
00465     GetBitContext gb;
00466     int i;
00467 
00468     init_get_bits(&gb, data, data_size * 8);
00469 
00470     if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
00471         return -1;
00472     if (ac->m4ac.sampling_index > 12) {
00473         av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
00474         return -1;
00475     }
00476     if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1)
00477         ac->m4ac.ps = 1;
00478 
00479     skip_bits_long(&gb, i);
00480 
00481     switch (ac->m4ac.object_type) {
00482     case AOT_AAC_MAIN:
00483     case AOT_AAC_LC:
00484         if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
00485             return -1;
00486         break;
00487     default:
00488         av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00489                ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
00490         return -1;
00491     }
00492     return 0;
00493 }
00494 
00502 static av_always_inline int lcg_random(int previous_val)
00503 {
00504     return previous_val * 1664525 + 1013904223;
00505 }
00506 
00507 static av_always_inline void reset_predict_state(PredictorState *ps)
00508 {
00509     ps->r0   = 0.0f;
00510     ps->r1   = 0.0f;
00511     ps->cor0 = 0.0f;
00512     ps->cor1 = 0.0f;
00513     ps->var0 = 1.0f;
00514     ps->var1 = 1.0f;
00515 }
00516 
00517 static void reset_all_predictors(PredictorState *ps)
00518 {
00519     int i;
00520     for (i = 0; i < MAX_PREDICTORS; i++)
00521         reset_predict_state(&ps[i]);
00522 }
00523 
00524 static void reset_predictor_group(PredictorState *ps, int group_num)
00525 {
00526     int i;
00527     for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
00528         reset_predict_state(&ps[i]);
00529 }
00530 
00531 #define AAC_INIT_VLC_STATIC(num, size) \
00532     INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
00533          ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
00534         ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
00535         size);
00536 
00537 static av_cold int aac_decode_init(AVCodecContext *avctx)
00538 {
00539     AACContext *ac = avctx->priv_data;
00540 
00541     ac->avctx = avctx;
00542     ac->m4ac.sample_rate = avctx->sample_rate;
00543 
00544     if (avctx->extradata_size > 0) {
00545         if (decode_audio_specific_config(ac, avctx->extradata, avctx->extradata_size))
00546             return -1;
00547     }
00548 
00549     avctx->sample_fmt = SAMPLE_FMT_S16;
00550 
00551     AAC_INIT_VLC_STATIC( 0, 304);
00552     AAC_INIT_VLC_STATIC( 1, 270);
00553     AAC_INIT_VLC_STATIC( 2, 550);
00554     AAC_INIT_VLC_STATIC( 3, 300);
00555     AAC_INIT_VLC_STATIC( 4, 328);
00556     AAC_INIT_VLC_STATIC( 5, 294);
00557     AAC_INIT_VLC_STATIC( 6, 306);
00558     AAC_INIT_VLC_STATIC( 7, 268);
00559     AAC_INIT_VLC_STATIC( 8, 510);
00560     AAC_INIT_VLC_STATIC( 9, 366);
00561     AAC_INIT_VLC_STATIC(10, 462);
00562 
00563     ff_aac_sbr_init();
00564 
00565     dsputil_init(&ac->dsp, avctx);
00566 
00567     ac->random_state = 0x1f2e3d4c;
00568 
00569     // -1024 - Compensate wrong IMDCT method.
00570     // 32768 - Required to scale values to the correct range for the bias method
00571     //         for float to int16 conversion.
00572 
00573     if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
00574         ac->add_bias  = 385.0f;
00575         ac->sf_scale  = 1. / (-1024. * 32768.);
00576         ac->sf_offset = 0;
00577     } else {
00578         ac->add_bias  = 0.0f;
00579         ac->sf_scale  = 1. / -1024.;
00580         ac->sf_offset = 60;
00581     }
00582 
00583     ff_aac_tableinit();
00584 
00585     INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00586                     ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00587                     ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00588                     352);
00589 
00590     ff_mdct_init(&ac->mdct, 11, 1, 1.0);
00591     ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
00592     // window initialization
00593     ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00594     ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00595     ff_init_ff_sine_windows(10);
00596     ff_init_ff_sine_windows( 7);
00597 
00598     cbrt_tableinit();
00599 
00600     return 0;
00601 }
00602 
00606 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
00607 {
00608     int byte_align = get_bits1(gb);
00609     int count = get_bits(gb, 8);
00610     if (count == 255)
00611         count += get_bits(gb, 8);
00612     if (byte_align)
00613         align_get_bits(gb);
00614 
00615     if (get_bits_left(gb) < 8 * count) {
00616         av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00617         return -1;
00618     }
00619     skip_bits_long(gb, 8 * count);
00620     return 0;
00621 }
00622 
00623 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
00624                              GetBitContext *gb)
00625 {
00626     int sfb;
00627     if (get_bits1(gb)) {
00628         ics->predictor_reset_group = get_bits(gb, 5);
00629         if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00630             av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00631             return -1;
00632         }
00633     }
00634     for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00635         ics->prediction_used[sfb] = get_bits1(gb);
00636     }
00637     return 0;
00638 }
00639 
00645 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
00646                            GetBitContext *gb, int common_window)
00647 {
00648     if (get_bits1(gb)) {
00649         av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
00650         memset(ics, 0, sizeof(IndividualChannelStream));
00651         return -1;
00652     }
00653     ics->window_sequence[1] = ics->window_sequence[0];
00654     ics->window_sequence[0] = get_bits(gb, 2);
00655     ics->use_kb_window[1]   = ics->use_kb_window[0];
00656     ics->use_kb_window[0]   = get_bits1(gb);
00657     ics->num_window_groups  = 1;
00658     ics->group_len[0]       = 1;
00659     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00660         int i;
00661         ics->max_sfb = get_bits(gb, 4);
00662         for (i = 0; i < 7; i++) {
00663             if (get_bits1(gb)) {
00664                 ics->group_len[ics->num_window_groups - 1]++;
00665             } else {
00666                 ics->num_window_groups++;
00667                 ics->group_len[ics->num_window_groups - 1] = 1;
00668             }
00669         }
00670         ics->num_windows       = 8;
00671         ics->swb_offset        =    ff_swb_offset_128[ac->m4ac.sampling_index];
00672         ics->num_swb           =   ff_aac_num_swb_128[ac->m4ac.sampling_index];
00673         ics->tns_max_bands     = ff_tns_max_bands_128[ac->m4ac.sampling_index];
00674         ics->predictor_present = 0;
00675     } else {
00676         ics->max_sfb               = get_bits(gb, 6);
00677         ics->num_windows           = 1;
00678         ics->swb_offset            =    ff_swb_offset_1024[ac->m4ac.sampling_index];
00679         ics->num_swb               =   ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00680         ics->tns_max_bands         = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
00681         ics->predictor_present     = get_bits1(gb);
00682         ics->predictor_reset_group = 0;
00683         if (ics->predictor_present) {
00684             if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00685                 if (decode_prediction(ac, ics, gb)) {
00686                     memset(ics, 0, sizeof(IndividualChannelStream));
00687                     return -1;
00688                 }
00689             } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00690                 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00691                 memset(ics, 0, sizeof(IndividualChannelStream));
00692                 return -1;
00693             } else {
00694                 av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
00695                 memset(ics, 0, sizeof(IndividualChannelStream));
00696                 return -1;
00697             }
00698         }
00699     }
00700 
00701     if (ics->max_sfb > ics->num_swb) {
00702         av_log(ac->avctx, AV_LOG_ERROR,
00703                "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00704                ics->max_sfb, ics->num_swb);
00705         memset(ics, 0, sizeof(IndividualChannelStream));
00706         return -1;
00707     }
00708 
00709     return 0;
00710 }
00711 
00720 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
00721                              int band_type_run_end[120], GetBitContext *gb,
00722                              IndividualChannelStream *ics)
00723 {
00724     int g, idx = 0;
00725     const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00726     for (g = 0; g < ics->num_window_groups; g++) {
00727         int k = 0;
00728         while (k < ics->max_sfb) {
00729             uint8_t sect_end = k;
00730             int sect_len_incr;
00731             int sect_band_type = get_bits(gb, 4);
00732             if (sect_band_type == 12) {
00733                 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
00734                 return -1;
00735             }
00736             while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
00737                 sect_end += sect_len_incr;
00738             sect_end += sect_len_incr;
00739             if (get_bits_left(gb) < 0) {
00740                 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00741                 return -1;
00742             }
00743             if (sect_end > ics->max_sfb) {
00744                 av_log(ac->avctx, AV_LOG_ERROR,
00745                        "Number of bands (%d) exceeds limit (%d).\n",
00746                        sect_end, ics->max_sfb);
00747                 return -1;
00748             }
00749             for (; k < sect_end; k++) {
00750                 band_type        [idx]   = sect_band_type;
00751                 band_type_run_end[idx++] = sect_end;
00752             }
00753         }
00754     }
00755     return 0;
00756 }
00757 
00768 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
00769                                unsigned int global_gain,
00770                                IndividualChannelStream *ics,
00771                                enum BandType band_type[120],
00772                                int band_type_run_end[120])
00773 {
00774     const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
00775     int g, i, idx = 0;
00776     int offset[3] = { global_gain, global_gain - 90, 100 };
00777     int noise_flag = 1;
00778     static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00779     for (g = 0; g < ics->num_window_groups; g++) {
00780         for (i = 0; i < ics->max_sfb;) {
00781             int run_end = band_type_run_end[idx];
00782             if (band_type[idx] == ZERO_BT) {
00783                 for (; i < run_end; i++, idx++)
00784                     sf[idx] = 0.;
00785             } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00786                 for (; i < run_end; i++, idx++) {
00787                     offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00788                     if (offset[2] > 255U) {
00789                         av_log(ac->avctx, AV_LOG_ERROR,
00790                                "%s (%d) out of range.\n", sf_str[2], offset[2]);
00791                         return -1;
00792                     }
00793                     sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
00794                 }
00795             } else if (band_type[idx] == NOISE_BT) {
00796                 for (; i < run_end; i++, idx++) {
00797                     if (noise_flag-- > 0)
00798                         offset[1] += get_bits(gb, 9) - 256;
00799                     else
00800                         offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00801                     if (offset[1] > 255U) {
00802                         av_log(ac->avctx, AV_LOG_ERROR,
00803                                "%s (%d) out of range.\n", sf_str[1], offset[1]);
00804                         return -1;
00805                     }
00806                     sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
00807                 }
00808             } else {
00809                 for (; i < run_end; i++, idx++) {
00810                     offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00811                     if (offset[0] > 255U) {
00812                         av_log(ac->avctx, AV_LOG_ERROR,
00813                                "%s (%d) out of range.\n", sf_str[0], offset[0]);
00814                         return -1;
00815                     }
00816                     sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
00817                 }
00818             }
00819         }
00820     }
00821     return 0;
00822 }
00823 
00827 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
00828                          const uint16_t *swb_offset, int num_swb)
00829 {
00830     int i, pulse_swb;
00831     pulse->num_pulse = get_bits(gb, 2) + 1;
00832     pulse_swb        = get_bits(gb, 6);
00833     if (pulse_swb >= num_swb)
00834         return -1;
00835     pulse->pos[0]    = swb_offset[pulse_swb];
00836     pulse->pos[0]   += get_bits(gb, 5);
00837     if (pulse->pos[0] > 1023)
00838         return -1;
00839     pulse->amp[0]    = get_bits(gb, 4);
00840     for (i = 1; i < pulse->num_pulse; i++) {
00841         pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
00842         if (pulse->pos[i] > 1023)
00843             return -1;
00844         pulse->amp[i] = get_bits(gb, 4);
00845     }
00846     return 0;
00847 }
00848 
00854 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
00855                       GetBitContext *gb, const IndividualChannelStream *ics)
00856 {
00857     int w, filt, i, coef_len, coef_res, coef_compress;
00858     const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00859     const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00860     for (w = 0; w < ics->num_windows; w++) {
00861         if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00862             coef_res = get_bits1(gb);
00863 
00864             for (filt = 0; filt < tns->n_filt[w]; filt++) {
00865                 int tmp2_idx;
00866                 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
00867 
00868                 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
00869                     av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
00870                            tns->order[w][filt], tns_max_order);
00871                     tns->order[w][filt] = 0;
00872                     return -1;
00873                 }
00874                 if (tns->order[w][filt]) {
00875                     tns->direction[w][filt] = get_bits1(gb);
00876                     coef_compress = get_bits1(gb);
00877                     coef_len = coef_res + 3 - coef_compress;
00878                     tmp2_idx = 2 * coef_compress + coef_res;
00879 
00880                     for (i = 0; i < tns->order[w][filt]; i++)
00881                         tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00882                 }
00883             }
00884         }
00885     }
00886     return 0;
00887 }
00888 
00896 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
00897                                    int ms_present)
00898 {
00899     int idx;
00900     if (ms_present == 1) {
00901         for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00902             cpe->ms_mask[idx] = get_bits1(gb);
00903     } else if (ms_present == 2) {
00904         memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00905     }
00906 }
00907 
00908 #ifndef VMUL2
00909 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
00910                            const float *scale)
00911 {
00912     float s = *scale;
00913     *dst++ = v[idx    & 15] * s;
00914     *dst++ = v[idx>>4 & 15] * s;
00915     return dst;
00916 }
00917 #endif
00918 
00919 #ifndef VMUL4
00920 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
00921                            const float *scale)
00922 {
00923     float s = *scale;
00924     *dst++ = v[idx    & 3] * s;
00925     *dst++ = v[idx>>2 & 3] * s;
00926     *dst++ = v[idx>>4 & 3] * s;
00927     *dst++ = v[idx>>6 & 3] * s;
00928     return dst;
00929 }
00930 #endif
00931 
00932 #ifndef VMUL2S
00933 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
00934                             unsigned sign, const float *scale)
00935 {
00936     union float754 s0, s1;
00937 
00938     s0.f = s1.f = *scale;
00939     s0.i ^= sign >> 1 << 31;
00940     s1.i ^= sign      << 31;
00941 
00942     *dst++ = v[idx    & 15] * s0.f;
00943     *dst++ = v[idx>>4 & 15] * s1.f;
00944 
00945     return dst;
00946 }
00947 #endif
00948 
00949 #ifndef VMUL4S
00950 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
00951                             unsigned sign, const float *scale)
00952 {
00953     unsigned nz = idx >> 12;
00954     union float754 s = { .f = *scale };
00955     union float754 t;
00956 
00957     t.i = s.i ^ (sign & 1<<31);
00958     *dst++ = v[idx    & 3] * t.f;
00959 
00960     sign <<= nz & 1; nz >>= 1;
00961     t.i = s.i ^ (sign & 1<<31);
00962     *dst++ = v[idx>>2 & 3] * t.f;
00963 
00964     sign <<= nz & 1; nz >>= 1;
00965     t.i = s.i ^ (sign & 1<<31);
00966     *dst++ = v[idx>>4 & 3] * t.f;
00967 
00968     sign <<= nz & 1; nz >>= 1;
00969     t.i = s.i ^ (sign & 1<<31);
00970     *dst++ = v[idx>>6 & 3] * t.f;
00971 
00972     return dst;
00973 }
00974 #endif
00975 
00988 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
00989                                        GetBitContext *gb, const float sf[120],
00990                                        int pulse_present, const Pulse *pulse,
00991                                        const IndividualChannelStream *ics,
00992                                        enum BandType band_type[120])
00993 {
00994     int i, k, g, idx = 0;
00995     const int c = 1024 / ics->num_windows;
00996     const uint16_t *offsets = ics->swb_offset;
00997     float *coef_base = coef;
00998     int err_idx;
00999 
01000     for (g = 0; g < ics->num_windows; g++)
01001         memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
01002 
01003     for (g = 0; g < ics->num_window_groups; g++) {
01004         unsigned g_len = ics->group_len[g];
01005 
01006         for (i = 0; i < ics->max_sfb; i++, idx++) {
01007             const unsigned cbt_m1 = band_type[idx] - 1;
01008             float *cfo = coef + offsets[i];
01009             int off_len = offsets[i + 1] - offsets[i];
01010             int group;
01011 
01012             if (cbt_m1 >= INTENSITY_BT2 - 1) {
01013                 for (group = 0; group < g_len; group++, cfo+=128) {
01014                     memset(cfo, 0, off_len * sizeof(float));
01015                 }
01016             } else if (cbt_m1 == NOISE_BT - 1) {
01017                 for (group = 0; group < g_len; group++, cfo+=128) {
01018                     float scale;
01019                     float band_energy;
01020 
01021                     for (k = 0; k < off_len; k++) {
01022                         ac->random_state  = lcg_random(ac->random_state);
01023                         cfo[k] = ac->random_state;
01024                     }
01025 
01026                     band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
01027                     scale = sf[idx] / sqrtf(band_energy);
01028                     ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
01029                 }
01030             } else {
01031                 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
01032                 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
01033                 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
01034                 const int cb_size = ff_aac_spectral_sizes[cbt_m1];
01035                 OPEN_READER(re, gb);
01036 
01037                 switch (cbt_m1 >> 1) {
01038                 case 0:
01039                     for (group = 0; group < g_len; group++, cfo+=128) {
01040                         float *cf = cfo;
01041                         int len = off_len;
01042 
01043                         do {
01044                             int code;
01045                             unsigned cb_idx;
01046 
01047                             UPDATE_CACHE(re, gb);
01048                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
01049 
01050                             if (code >= cb_size) {
01051                                 err_idx = code;
01052                                 goto err_cb_overflow;
01053                             }
01054 
01055                             cb_idx = cb_vector_idx[code];
01056                             cf = VMUL4(cf, vq, cb_idx, sf + idx);
01057                         } while (len -= 4);
01058                     }
01059                     break;
01060 
01061                 case 1:
01062                     for (group = 0; group < g_len; group++, cfo+=128) {
01063                         float *cf = cfo;
01064                         int len = off_len;
01065 
01066                         do {
01067                             int code;
01068                             unsigned nnz;
01069                             unsigned cb_idx;
01070                             uint32_t bits;
01071 
01072                             UPDATE_CACHE(re, gb);
01073                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
01074 
01075                             if (code >= cb_size) {
01076                                 err_idx = code;
01077                                 goto err_cb_overflow;
01078                             }
01079 
01080 #if MIN_CACHE_BITS < 20
01081                             UPDATE_CACHE(re, gb);
01082 #endif
01083                             cb_idx = cb_vector_idx[code];
01084                             nnz = cb_idx >> 8 & 15;
01085                             bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01086                             LAST_SKIP_BITS(re, gb, nnz);
01087                             cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
01088                         } while (len -= 4);
01089                     }
01090                     break;
01091 
01092                 case 2:
01093                     for (group = 0; group < g_len; group++, cfo+=128) {
01094                         float *cf = cfo;
01095                         int len = off_len;
01096 
01097                         do {
01098                             int code;
01099                             unsigned cb_idx;
01100 
01101                             UPDATE_CACHE(re, gb);
01102                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
01103 
01104                             if (code >= cb_size) {
01105                                 err_idx = code;
01106                                 goto err_cb_overflow;
01107                             }
01108 
01109                             cb_idx = cb_vector_idx[code];
01110                             cf = VMUL2(cf, vq, cb_idx, sf + idx);
01111                         } while (len -= 2);
01112                     }
01113                     break;
01114 
01115                 case 3:
01116                 case 4:
01117                     for (group = 0; group < g_len; group++, cfo+=128) {
01118                         float *cf = cfo;
01119                         int len = off_len;
01120 
01121                         do {
01122                             int code;
01123                             unsigned nnz;
01124                             unsigned cb_idx;
01125                             unsigned sign;
01126 
01127                             UPDATE_CACHE(re, gb);
01128                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
01129 
01130                             if (code >= cb_size) {
01131                                 err_idx = code;
01132                                 goto err_cb_overflow;
01133                             }
01134 
01135                             cb_idx = cb_vector_idx[code];
01136                             nnz = cb_idx >> 8 & 15;
01137                             sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
01138                             LAST_SKIP_BITS(re, gb, nnz);
01139                             cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
01140                         } while (len -= 2);
01141                     }
01142                     break;
01143 
01144                 default:
01145                     for (group = 0; group < g_len; group++, cfo+=128) {
01146                         float *cf = cfo;
01147                         uint32_t *icf = (uint32_t *) cf;
01148                         int len = off_len;
01149 
01150                         do {
01151                             int code;
01152                             unsigned nzt, nnz;
01153                             unsigned cb_idx;
01154                             uint32_t bits;
01155                             int j;
01156 
01157                             UPDATE_CACHE(re, gb);
01158                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
01159 
01160                             if (!code) {
01161                                 *icf++ = 0;
01162                                 *icf++ = 0;
01163                                 continue;
01164                             }
01165 
01166                             if (code >= cb_size) {
01167                                 err_idx = code;
01168                                 goto err_cb_overflow;
01169                             }
01170 
01171                             cb_idx = cb_vector_idx[code];
01172                             nnz = cb_idx >> 12;
01173                             nzt = cb_idx >> 8;
01174                             bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01175                             LAST_SKIP_BITS(re, gb, nnz);
01176 
01177                             for (j = 0; j < 2; j++) {
01178                                 if (nzt & 1<<j) {
01179                                     uint32_t b;
01180                                     int n;
01181                                     /* The total length of escape_sequence must be < 22 bits according
01182                                        to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
01183                                     UPDATE_CACHE(re, gb);
01184                                     b = GET_CACHE(re, gb);
01185                                     b = 31 - av_log2(~b);
01186 
01187                                     if (b > 8) {
01188                                         av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
01189                                         return -1;
01190                                     }
01191 
01192 #if MIN_CACHE_BITS < 21
01193                                     LAST_SKIP_BITS(re, gb, b + 1);
01194                                     UPDATE_CACHE(re, gb);
01195 #else
01196                                     SKIP_BITS(re, gb, b + 1);
01197 #endif
01198                                     b += 4;
01199                                     n = (1 << b) + SHOW_UBITS(re, gb, b);
01200                                     LAST_SKIP_BITS(re, gb, b);
01201                                     *icf++ = cbrt_tab[n] | (bits & 1<<31);
01202                                     bits <<= 1;
01203                                 } else {
01204                                     unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
01205                                     *icf++ = (bits & 1<<31) | v;
01206                                     bits <<= !!v;
01207                                 }
01208                                 cb_idx >>= 4;
01209                             }
01210                         } while (len -= 2);
01211 
01212                         ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
01213                     }
01214                 }
01215 
01216                 CLOSE_READER(re, gb);
01217             }
01218         }
01219         coef += g_len << 7;
01220     }
01221 
01222     if (pulse_present) {
01223         idx = 0;
01224         for (i = 0; i < pulse->num_pulse; i++) {
01225             float co = coef_base[ pulse->pos[i] ];
01226             while (offsets[idx + 1] <= pulse->pos[i])
01227                 idx++;
01228             if (band_type[idx] != NOISE_BT && sf[idx]) {
01229                 float ico = -pulse->amp[i];
01230                 if (co) {
01231                     co /= sf[idx];
01232                     ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
01233                 }
01234                 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
01235             }
01236         }
01237     }
01238     return 0;
01239 
01240 err_cb_overflow:
01241     av_log(ac->avctx, AV_LOG_ERROR,
01242            "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
01243            band_type[idx], err_idx, ff_aac_spectral_sizes[band_type[idx]]);
01244     return -1;
01245 }
01246 
01247 static av_always_inline float flt16_round(float pf)
01248 {
01249     union float754 tmp;
01250     tmp.f = pf;
01251     tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
01252     return tmp.f;
01253 }
01254 
01255 static av_always_inline float flt16_even(float pf)
01256 {
01257     union float754 tmp;
01258     tmp.f = pf;
01259     tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
01260     return tmp.f;
01261 }
01262 
01263 static av_always_inline float flt16_trunc(float pf)
01264 {
01265     union float754 pun;
01266     pun.f = pf;
01267     pun.i &= 0xFFFF0000U;
01268     return pun.f;
01269 }
01270 
01271 static av_always_inline void predict(AACContext *ac, PredictorState *ps, float *coef,
01272                     int output_enable)
01273 {
01274     const float a     = 0.953125; // 61.0 / 64
01275     const float alpha = 0.90625;  // 29.0 / 32
01276     float e0, e1;
01277     float pv;
01278     float k1, k2;
01279 
01280     k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
01281     k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
01282 
01283     pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
01284     if (output_enable)
01285         *coef += pv * ac->sf_scale;
01286 
01287     e0 = *coef / ac->sf_scale;
01288     e1 = e0 - k1 * ps->r0;
01289 
01290     ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
01291     ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
01292     ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
01293     ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
01294 
01295     ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
01296     ps->r0 = flt16_trunc(a * e0);
01297 }
01298 
01302 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
01303 {
01304     int sfb, k;
01305 
01306     if (!sce->ics.predictor_initialized) {
01307         reset_all_predictors(sce->predictor_state);
01308         sce->ics.predictor_initialized = 1;
01309     }
01310 
01311     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01312         for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
01313             for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
01314                 predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
01315                         sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
01316             }
01317         }
01318         if (sce->ics.predictor_reset_group)
01319             reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
01320     } else
01321         reset_all_predictors(sce->predictor_state);
01322 }
01323 
01332 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
01333                       GetBitContext *gb, int common_window, int scale_flag)
01334 {
01335     Pulse pulse;
01336     TemporalNoiseShaping    *tns = &sce->tns;
01337     IndividualChannelStream *ics = &sce->ics;
01338     float *out = sce->coeffs;
01339     int global_gain, pulse_present = 0;
01340 
01341     /* This assignment is to silence a GCC warning about the variable being used
01342      * uninitialized when in fact it always is.
01343      */
01344     pulse.num_pulse = 0;
01345 
01346     global_gain = get_bits(gb, 8);
01347 
01348     if (!common_window && !scale_flag) {
01349         if (decode_ics_info(ac, ics, gb, 0) < 0)
01350             return -1;
01351     }
01352 
01353     if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01354         return -1;
01355     if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01356         return -1;
01357 
01358     pulse_present = 0;
01359     if (!scale_flag) {
01360         if ((pulse_present = get_bits1(gb))) {
01361             if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01362                 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01363                 return -1;
01364             }
01365             if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01366                 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01367                 return -1;
01368             }
01369         }
01370         if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01371             return -1;
01372         if (get_bits1(gb)) {
01373             av_log_missing_feature(ac->avctx, "SSR", 1);
01374             return -1;
01375         }
01376     }
01377 
01378     if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01379         return -1;
01380 
01381     if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01382         apply_prediction(ac, sce);
01383 
01384     return 0;
01385 }
01386 
01390 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
01391 {
01392     const IndividualChannelStream *ics = &cpe->ch[0].ics;
01393     float *ch0 = cpe->ch[0].coeffs;
01394     float *ch1 = cpe->ch[1].coeffs;
01395     int g, i, group, idx = 0;
01396     const uint16_t *offsets = ics->swb_offset;
01397     for (g = 0; g < ics->num_window_groups; g++) {
01398         for (i = 0; i < ics->max_sfb; i++, idx++) {
01399             if (cpe->ms_mask[idx] &&
01400                     cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01401                 for (group = 0; group < ics->group_len[g]; group++) {
01402                     ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
01403                                               ch1 + group * 128 + offsets[i],
01404                                               offsets[i+1] - offsets[i]);
01405                 }
01406             }
01407         }
01408         ch0 += ics->group_len[g] * 128;
01409         ch1 += ics->group_len[g] * 128;
01410     }
01411 }
01412 
01420 static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
01421 {
01422     const IndividualChannelStream *ics = &cpe->ch[1].ics;
01423     SingleChannelElement         *sce1 = &cpe->ch[1];
01424     float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01425     const uint16_t *offsets = ics->swb_offset;
01426     int g, group, i, k, idx = 0;
01427     int c;
01428     float scale;
01429     for (g = 0; g < ics->num_window_groups; g++) {
01430         for (i = 0; i < ics->max_sfb;) {
01431             if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01432                 const int bt_run_end = sce1->band_type_run_end[idx];
01433                 for (; i < bt_run_end; i++, idx++) {
01434                     c = -1 + 2 * (sce1->band_type[idx] - 14);
01435                     if (ms_present)
01436                         c *= 1 - 2 * cpe->ms_mask[idx];
01437                     scale = c * sce1->sf[idx];
01438                     for (group = 0; group < ics->group_len[g]; group++)
01439                         for (k = offsets[i]; k < offsets[i + 1]; k++)
01440                             coef1[group * 128 + k] = scale * coef0[group * 128 + k];
01441                 }
01442             } else {
01443                 int bt_run_end = sce1->band_type_run_end[idx];
01444                 idx += bt_run_end - i;
01445                 i    = bt_run_end;
01446             }
01447         }
01448         coef0 += ics->group_len[g] * 128;
01449         coef1 += ics->group_len[g] * 128;
01450     }
01451 }
01452 
01460 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
01461 {
01462     int i, ret, common_window, ms_present = 0;
01463 
01464     common_window = get_bits1(gb);
01465     if (common_window) {
01466         if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
01467             return -1;
01468         i = cpe->ch[1].ics.use_kb_window[0];
01469         cpe->ch[1].ics = cpe->ch[0].ics;
01470         cpe->ch[1].ics.use_kb_window[1] = i;
01471         ms_present = get_bits(gb, 2);
01472         if (ms_present == 3) {
01473             av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01474             return -1;
01475         } else if (ms_present)
01476             decode_mid_side_stereo(cpe, gb, ms_present);
01477     }
01478     if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01479         return ret;
01480     if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01481         return ret;
01482 
01483     if (common_window) {
01484         if (ms_present)
01485             apply_mid_side_stereo(ac, cpe);
01486         if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01487             apply_prediction(ac, &cpe->ch[0]);
01488             apply_prediction(ac, &cpe->ch[1]);
01489         }
01490     }
01491 
01492     apply_intensity_stereo(cpe, ms_present);
01493     return 0;
01494 }
01495 
01503 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
01504 {
01505     int num_gain = 0;
01506     int c, g, sfb, ret;
01507     int sign;
01508     float scale;
01509     SingleChannelElement *sce = &che->ch[0];
01510     ChannelCoupling     *coup = &che->coup;
01511 
01512     coup->coupling_point = 2 * get_bits1(gb);
01513     coup->num_coupled = get_bits(gb, 3);
01514     for (c = 0; c <= coup->num_coupled; c++) {
01515         num_gain++;
01516         coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01517         coup->id_select[c] = get_bits(gb, 4);
01518         if (coup->type[c] == TYPE_CPE) {
01519             coup->ch_select[c] = get_bits(gb, 2);
01520             if (coup->ch_select[c] == 3)
01521                 num_gain++;
01522         } else
01523             coup->ch_select[c] = 2;
01524     }
01525     coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
01526 
01527     sign  = get_bits(gb, 1);
01528     scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
01529 
01530     if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01531         return ret;
01532 
01533     for (c = 0; c < num_gain; c++) {
01534         int idx  = 0;
01535         int cge  = 1;
01536         int gain = 0;
01537         float gain_cache = 1.;
01538         if (c) {
01539             cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01540             gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01541             gain_cache = pow(scale, -gain);
01542         }
01543         if (coup->coupling_point == AFTER_IMDCT) {
01544             coup->gain[c][0] = gain_cache;
01545         } else {
01546             for (g = 0; g < sce->ics.num_window_groups; g++) {
01547                 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01548                     if (sce->band_type[idx] != ZERO_BT) {
01549                         if (!cge) {
01550                             int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01551                             if (t) {
01552                                 int s = 1;
01553                                 t = gain += t;
01554                                 if (sign) {
01555                                     s  -= 2 * (t & 0x1);
01556                                     t >>= 1;
01557                                 }
01558                                 gain_cache = pow(scale, -t) * s;
01559                             }
01560                         }
01561                         coup->gain[c][idx] = gain_cache;
01562                     }
01563                 }
01564             }
01565         }
01566     }
01567     return 0;
01568 }
01569 
01575 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
01576                                          GetBitContext *gb)
01577 {
01578     int i;
01579     int num_excl_chan = 0;
01580 
01581     do {
01582         for (i = 0; i < 7; i++)
01583             che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01584     } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01585 
01586     return num_excl_chan / 7;
01587 }
01588 
01596 static int decode_dynamic_range(DynamicRangeControl *che_drc,
01597                                 GetBitContext *gb, int cnt)
01598 {
01599     int n             = 1;
01600     int drc_num_bands = 1;
01601     int i;
01602 
01603     /* pce_tag_present? */
01604     if (get_bits1(gb)) {
01605         che_drc->pce_instance_tag  = get_bits(gb, 4);
01606         skip_bits(gb, 4); // tag_reserved_bits
01607         n++;
01608     }
01609 
01610     /* excluded_chns_present? */
01611     if (get_bits1(gb)) {
01612         n += decode_drc_channel_exclusions(che_drc, gb);
01613     }
01614 
01615     /* drc_bands_present? */
01616     if (get_bits1(gb)) {
01617         che_drc->band_incr            = get_bits(gb, 4);
01618         che_drc->interpolation_scheme = get_bits(gb, 4);
01619         n++;
01620         drc_num_bands += che_drc->band_incr;
01621         for (i = 0; i < drc_num_bands; i++) {
01622             che_drc->band_top[i] = get_bits(gb, 8);
01623             n++;
01624         }
01625     }
01626 
01627     /* prog_ref_level_present? */
01628     if (get_bits1(gb)) {
01629         che_drc->prog_ref_level = get_bits(gb, 7);
01630         skip_bits1(gb); // prog_ref_level_reserved_bits
01631         n++;
01632     }
01633 
01634     for (i = 0; i < drc_num_bands; i++) {
01635         che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01636         che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01637         n++;
01638     }
01639 
01640     return n;
01641 }
01642 
01650 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
01651                                     ChannelElement *che, enum RawDataBlockType elem_type)
01652 {
01653     int crc_flag = 0;
01654     int res = cnt;
01655     switch (get_bits(gb, 4)) { // extension type
01656     case EXT_SBR_DATA_CRC:
01657         crc_flag++;
01658     case EXT_SBR_DATA:
01659         if (!che) {
01660             av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
01661             return res;
01662         } else if (!ac->m4ac.sbr) {
01663             av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
01664             skip_bits_long(gb, 8 * cnt - 4);
01665             return res;
01666         } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
01667             av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
01668             skip_bits_long(gb, 8 * cnt - 4);
01669             return res;
01670         } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
01671             ac->m4ac.sbr = 1;
01672             ac->m4ac.ps = 1;
01673             output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
01674         } else {
01675             ac->m4ac.sbr = 1;
01676         }
01677         res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
01678         break;
01679     case EXT_DYNAMIC_RANGE:
01680         res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01681         break;
01682     case EXT_FILL:
01683     case EXT_FILL_DATA:
01684     case EXT_DATA_ELEMENT:
01685     default:
01686         skip_bits_long(gb, 8 * cnt - 4);
01687         break;
01688     };
01689     return res;
01690 }
01691 
01698 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
01699                       IndividualChannelStream *ics, int decode)
01700 {
01701     const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
01702     int w, filt, m, i;
01703     int bottom, top, order, start, end, size, inc;
01704     float lpc[TNS_MAX_ORDER];
01705 
01706     for (w = 0; w < ics->num_windows; w++) {
01707         bottom = ics->num_swb;
01708         for (filt = 0; filt < tns->n_filt[w]; filt++) {
01709             top    = bottom;
01710             bottom = FFMAX(0, top - tns->length[w][filt]);
01711             order  = tns->order[w][filt];
01712             if (order == 0)
01713                 continue;
01714 
01715             // tns_decode_coef
01716             compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01717 
01718             start = ics->swb_offset[FFMIN(bottom, mmm)];
01719             end   = ics->swb_offset[FFMIN(   top, mmm)];
01720             if ((size = end - start) <= 0)
01721                 continue;
01722             if (tns->direction[w][filt]) {
01723                 inc = -1;
01724                 start = end - 1;
01725             } else {
01726                 inc = 1;
01727             }
01728             start += w * 128;
01729 
01730             // ar filter
01731             for (m = 0; m < size; m++, start += inc)
01732                 for (i = 1; i <= FFMIN(m, order); i++)
01733                     coef[start] -= coef[start - i * inc] * lpc[i - 1];
01734         }
01735     }
01736 }
01737 
01741 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
01742 {
01743     IndividualChannelStream *ics = &sce->ics;
01744     float *in    = sce->coeffs;
01745     float *out   = sce->ret;
01746     float *saved = sce->saved;
01747     const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01748     const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01749     const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01750     float *buf  = ac->buf_mdct;
01751     float *temp = ac->temp;
01752     int i;
01753 
01754     // imdct
01755     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01756         if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
01757             av_log(ac->avctx, AV_LOG_WARNING,
01758                    "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
01759                    "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
01760         for (i = 0; i < 1024; i += 128)
01761             ff_imdct_half(&ac->mdct_small, buf + i, in + i);
01762     } else
01763         ff_imdct_half(&ac->mdct, buf, in);
01764 
01765     /* window overlapping
01766      * NOTE: To simplify the overlapping code, all 'meaningless' short to long
01767      * and long to short transitions are considered to be short to short
01768      * transitions. This leaves just two cases (long to long and short to short)
01769      * with a little special sauce for EIGHT_SHORT_SEQUENCE.
01770      */
01771     if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01772             (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01773         ac->dsp.vector_fmul_window(    out,               saved,            buf,         lwindow_prev, bias, 512);
01774     } else {
01775         for (i = 0; i < 448; i++)
01776             out[i] = saved[i] + bias;
01777 
01778         if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01779             ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, bias, 64);
01780             ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      bias, 64);
01781             ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      bias, 64);
01782             ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      bias, 64);
01783             ac->dsp.vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      bias, 64);
01784             memcpy(                    out + 448 + 4*128, temp, 64 * sizeof(float));
01785         } else {
01786             ac->dsp.vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, bias, 64);
01787             for (i = 576; i < 1024; i++)
01788                 out[i] = buf[i-512] + bias;
01789         }
01790     }
01791 
01792     // buffer update
01793     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01794         for (i = 0; i < 64; i++)
01795             saved[i] = temp[64 + i] - bias;
01796         ac->dsp.vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
01797         ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
01798         ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
01799         memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
01800     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01801         memcpy(                    saved,       buf + 512,        448 * sizeof(float));
01802         memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
01803     } else { // LONG_STOP or ONLY_LONG
01804         memcpy(                    saved,       buf + 512,        512 * sizeof(float));
01805     }
01806 }
01807 
01813 static void apply_dependent_coupling(AACContext *ac,
01814                                      SingleChannelElement *target,
01815                                      ChannelElement *cce, int index)
01816 {
01817     IndividualChannelStream *ics = &cce->ch[0].ics;
01818     const uint16_t *offsets = ics->swb_offset;
01819     float *dest = target->coeffs;
01820     const float *src = cce->ch[0].coeffs;
01821     int g, i, group, k, idx = 0;
01822     if (ac->m4ac.object_type == AOT_AAC_LTP) {
01823         av_log(ac->avctx, AV_LOG_ERROR,
01824                "Dependent coupling is not supported together with LTP\n");
01825         return;
01826     }
01827     for (g = 0; g < ics->num_window_groups; g++) {
01828         for (i = 0; i < ics->max_sfb; i++, idx++) {
01829             if (cce->ch[0].band_type[idx] != ZERO_BT) {
01830                 const float gain = cce->coup.gain[index][idx];
01831                 for (group = 0; group < ics->group_len[g]; group++) {
01832                     for (k = offsets[i]; k < offsets[i + 1]; k++) {
01833                         // XXX dsputil-ize
01834                         dest[group * 128 + k] += gain * src[group * 128 + k];
01835                     }
01836                 }
01837             }
01838         }
01839         dest += ics->group_len[g] * 128;
01840         src  += ics->group_len[g] * 128;
01841     }
01842 }
01843 
01849 static void apply_independent_coupling(AACContext *ac,
01850                                        SingleChannelElement *target,
01851                                        ChannelElement *cce, int index)
01852 {
01853     int i;
01854     const float gain = cce->coup.gain[index][0];
01855     const float bias = ac->add_bias;
01856     const float *src = cce->ch[0].ret;
01857     float *dest = target->ret;
01858     const int len = 1024 << (ac->m4ac.sbr == 1);
01859 
01860     for (i = 0; i < len; i++)
01861         dest[i] += gain * (src[i] - bias);
01862 }
01863 
01870 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
01871                                    enum RawDataBlockType type, int elem_id,
01872                                    enum CouplingPoint coupling_point,
01873                                    void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
01874 {
01875     int i, c;
01876 
01877     for (i = 0; i < MAX_ELEM_ID; i++) {
01878         ChannelElement *cce = ac->che[TYPE_CCE][i];
01879         int index = 0;
01880 
01881         if (cce && cce->coup.coupling_point == coupling_point) {
01882             ChannelCoupling *coup = &cce->coup;
01883 
01884             for (c = 0; c <= coup->num_coupled; c++) {
01885                 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
01886                     if (coup->ch_select[c] != 1) {
01887                         apply_coupling_method(ac, &cc->ch[0], cce, index);
01888                         if (coup->ch_select[c] != 0)
01889                             index++;
01890                     }
01891                     if (coup->ch_select[c] != 2)
01892                         apply_coupling_method(ac, &cc->ch[1], cce, index++);
01893                 } else
01894                     index += 1 + (coup->ch_select[c] == 3);
01895             }
01896         }
01897     }
01898 }
01899 
01903 static void spectral_to_sample(AACContext *ac)
01904 {
01905     int i, type;
01906     float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
01907     for (type = 3; type >= 0; type--) {
01908         for (i = 0; i < MAX_ELEM_ID; i++) {
01909             ChannelElement *che = ac->che[type][i];
01910             if (che) {
01911                 if (type <= TYPE_CPE)
01912                     apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
01913                 if (che->ch[0].tns.present)
01914                     apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
01915                 if (che->ch[1].tns.present)
01916                     apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
01917                 if (type <= TYPE_CPE)
01918                     apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
01919                 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
01920                     imdct_and_windowing(ac, &che->ch[0], imdct_bias);
01921                     if (type == TYPE_CPE) {
01922                         imdct_and_windowing(ac, &che->ch[1], imdct_bias);
01923                     }
01924                     if (ac->m4ac.sbr > 0) {
01925                         ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
01926                     }
01927                 }
01928                 if (type <= TYPE_CCE)
01929                     apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
01930             }
01931         }
01932     }
01933 }
01934 
01935 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
01936 {
01937     int size;
01938     AACADTSHeaderInfo hdr_info;
01939 
01940     size = ff_aac_parse_header(gb, &hdr_info);
01941     if (size > 0) {
01942         if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
01943             enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
01944             memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
01945             ac->m4ac.chan_config = hdr_info.chan_config;
01946             if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
01947                 return -7;
01948             if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
01949                 return -7;
01950         } else if (ac->output_configured != OC_LOCKED) {
01951             ac->output_configured = OC_NONE;
01952         }
01953         if (ac->output_configured != OC_LOCKED) {
01954             ac->m4ac.sbr = -1;
01955             ac->m4ac.ps  = -1;
01956         }
01957         ac->m4ac.sample_rate     = hdr_info.sample_rate;
01958         ac->m4ac.sampling_index  = hdr_info.sampling_index;
01959         ac->m4ac.object_type     = hdr_info.object_type;
01960         if (!ac->avctx->sample_rate)
01961             ac->avctx->sample_rate = hdr_info.sample_rate;
01962         if (hdr_info.num_aac_frames == 1) {
01963             if (!hdr_info.crc_absent)
01964                 skip_bits(gb, 16);
01965         } else {
01966             av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
01967             return -1;
01968         }
01969     }
01970     return size;
01971 }
01972 
01973 static int aac_decode_frame(AVCodecContext *avctx, void *data,
01974                             int *data_size, AVPacket *avpkt)
01975 {
01976     const uint8_t *buf = avpkt->data;
01977     int buf_size = avpkt->size;
01978     AACContext *ac = avctx->priv_data;
01979     ChannelElement *che = NULL, *che_prev = NULL;
01980     GetBitContext gb;
01981     enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
01982     int err, elem_id, data_size_tmp;
01983     int buf_consumed;
01984     int samples = 0, multiplier;
01985     int buf_offset;
01986 
01987     init_get_bits(&gb, buf, buf_size * 8);
01988 
01989     if (show_bits(&gb, 12) == 0xfff) {
01990         if (parse_adts_frame_header(ac, &gb) < 0) {
01991             av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
01992             return -1;
01993         }
01994         if (ac->m4ac.sampling_index > 12) {
01995             av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
01996             return -1;
01997         }
01998     }
01999 
02000     memset(ac->tags_seen_this_frame, 0, sizeof(ac->tags_seen_this_frame));
02001     // parse
02002     while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
02003         elem_id = get_bits(&gb, 4);
02004 
02005         if (elem_type < TYPE_DSE) {
02006             if (!(che=get_che(ac, elem_type, elem_id))) {
02007                 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
02008                        elem_type, elem_id);
02009                 return -1;
02010             }
02011             samples = 1024;
02012         }
02013 
02014         switch (elem_type) {
02015 
02016         case TYPE_SCE:
02017             err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
02018             break;
02019 
02020         case TYPE_CPE:
02021             err = decode_cpe(ac, &gb, che);
02022             break;
02023 
02024         case TYPE_CCE:
02025             err = decode_cce(ac, &gb, che);
02026             break;
02027 
02028         case TYPE_LFE:
02029             err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
02030             break;
02031 
02032         case TYPE_DSE:
02033             err = skip_data_stream_element(ac, &gb);
02034             break;
02035 
02036         case TYPE_PCE: {
02037             enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
02038             memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
02039             if ((err = decode_pce(ac, new_che_pos, &gb)))
02040                 break;
02041             if (ac->output_configured > OC_TRIAL_PCE)
02042                 av_log(avctx, AV_LOG_ERROR,
02043                        "Not evaluating a further program_config_element as this construct is dubious at best.\n");
02044             else
02045                 err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
02046             break;
02047         }
02048 
02049         case TYPE_FIL:
02050             if (elem_id == 15)
02051                 elem_id += get_bits(&gb, 8) - 1;
02052             if (get_bits_left(&gb) < 8 * elem_id) {
02053                     av_log(avctx, AV_LOG_ERROR, overread_err);
02054                     return -1;
02055             }
02056             while (elem_id > 0)
02057                 elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
02058             err = 0; /* FIXME */
02059             break;
02060 
02061         default:
02062             err = -1; /* should not happen, but keeps compiler happy */
02063             break;
02064         }
02065 
02066         che_prev       = che;
02067         elem_type_prev = elem_type;
02068 
02069         if (err)
02070             return err;
02071 
02072         if (get_bits_left(&gb) < 3) {
02073             av_log(avctx, AV_LOG_ERROR, overread_err);
02074             return -1;
02075         }
02076     }
02077 
02078     spectral_to_sample(ac);
02079 
02080     multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
02081     samples <<= multiplier;
02082     if (ac->output_configured < OC_LOCKED) {
02083         avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
02084         avctx->frame_size = samples;
02085     }
02086 
02087     data_size_tmp = samples * avctx->channels * sizeof(int16_t);
02088     if (*data_size < data_size_tmp) {
02089         av_log(avctx, AV_LOG_ERROR,
02090                "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
02091                *data_size, data_size_tmp);
02092         return -1;
02093     }
02094     *data_size = data_size_tmp;
02095 
02096     if (samples)
02097         ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
02098 
02099     if (ac->output_configured)
02100         ac->output_configured = OC_LOCKED;
02101 
02102     buf_consumed = (get_bits_count(&gb) + 7) >> 3;
02103     for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
02104         if (buf[buf_offset])
02105             break;
02106 
02107     return buf_size > buf_offset ? buf_consumed : buf_size;
02108 }
02109 
02110 static av_cold int aac_decode_close(AVCodecContext *avctx)
02111 {
02112     AACContext *ac = avctx->priv_data;
02113     int i, type;
02114 
02115     for (i = 0; i < MAX_ELEM_ID; i++) {
02116         for (type = 0; type < 4; type++) {
02117             if (ac->che[type][i])
02118                 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
02119             av_freep(&ac->che[type][i]);
02120         }
02121     }
02122 
02123     ff_mdct_end(&ac->mdct);
02124     ff_mdct_end(&ac->mdct_small);
02125     return 0;
02126 }
02127 
02128 AVCodec aac_decoder = {
02129     "aac",
02130     AVMEDIA_TYPE_AUDIO,
02131     CODEC_ID_AAC,
02132     sizeof(AACContext),
02133     aac_decode_init,
02134     NULL,
02135     aac_decode_close,
02136     aac_decode_frame,
02137     .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
02138     .sample_fmts = (const enum SampleFormat[]) {
02139         SAMPLE_FMT_S16,SAMPLE_FMT_NONE
02140     },
02141     .channel_layouts = aac_channel_layout,
02142 };

Generated on Fri Sep 16 2011 17:17:32 for FFmpeg by  doxygen 1.7.1