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