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

libavcodec/aac.c

Go to the documentation of this file.
00001 /*
00002  * AAC decoder
00003  * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
00004  * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00030 /*
00031  * supported tools
00032  *
00033  * Support?             Name
00034  * N (code in SoC repo) gain control
00035  * Y                    block switching
00036  * Y                    window shapes - standard
00037  * N                    window shapes - Low Delay
00038  * Y                    filterbank - standard
00039  * N (code in SoC repo) filterbank - Scalable Sample Rate
00040  * Y                    Temporal Noise Shaping
00041  * N (code in SoC repo) Long Term Prediction
00042  * Y                    intensity stereo
00043  * Y                    channel coupling
00044  * Y                    frequency domain prediction
00045  * Y                    Perceptual Noise Substitution
00046  * Y                    Mid/Side stereo
00047  * N                    Scalable Inverse AAC Quantization
00048  * N                    Frequency Selective Switch
00049  * N                    upsampling filter
00050  * Y                    quantization & coding - AAC
00051  * N                    quantization & coding - TwinVQ
00052  * N                    quantization & coding - BSAC
00053  * N                    AAC Error Resilience tools
00054  * N                    Error Resilience payload syntax
00055  * N                    Error Protection tool
00056  * N                    CELP
00057  * N                    Silence Compression
00058  * N                    HVXC
00059  * N                    HVXC 4kbits/s VR
00060  * N                    Structured Audio tools
00061  * N                    Structured Audio Sample Bank Format
00062  * N                    MIDI
00063  * N                    Harmonic and Individual Lines plus Noise
00064  * N                    Text-To-Speech Interface
00065  * N (in progress)      Spectral Band Replication
00066  * Y (not in this code) Layer-1
00067  * Y (not in this code) Layer-2
00068  * Y (not in this code) Layer-3
00069  * N                    SinuSoidal Coding (Transient, Sinusoid, Noise)
00070  * N (planned)          Parametric Stereo
00071  * N                    Direct Stream Transfer
00072  *
00073  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
00074  *       - HE AAC v2 comprises LC AAC with Spectral Band Replication and
00075            Parametric Stereo.
00076  */
00077 
00078 
00079 #include "avcodec.h"
00080 #include "internal.h"
00081 #include "bitstream.h"
00082 #include "dsputil.h"
00083 #include "lpc.h"
00084 
00085 #include "aac.h"
00086 #include "aactab.h"
00087 #include "aacdectab.h"
00088 #include "mpeg4audio.h"
00089 #include "aac_parser.h"
00090 
00091 #include <assert.h>
00092 #include <errno.h>
00093 #include <math.h>
00094 #include <string.h>
00095 
00096 static VLC vlc_scalefactors;
00097 static VLC vlc_spectral[11];
00098 
00099 
00100 static ChannelElement* get_che(AACContext *ac, int type, int elem_id) {
00101     static const int8_t tags_per_config[16] = { 0, 1, 1, 2, 3, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0 };
00102     if (ac->tag_che_map[type][elem_id]) {
00103         return ac->tag_che_map[type][elem_id];
00104     }
00105     if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
00106         return NULL;
00107     }
00108     switch (ac->m4ac.chan_config) {
00109         case 7:
00110             if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00111                 ac->tags_mapped++;
00112                 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00113             }
00114         case 6:
00115             /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
00116                instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have
00117                encountered such a stream, transfer the LFE[0] element to SCE[1] */
00118             if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00119                 ac->tags_mapped++;
00120                 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00121             }
00122         case 5:
00123             if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00124                 ac->tags_mapped++;
00125                 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00126             }
00127         case 4:
00128             if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
00129                 ac->tags_mapped++;
00130                 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00131             }
00132         case 3:
00133         case 2:
00134             if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
00135                 ac->tags_mapped++;
00136                 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00137             } else if (ac->m4ac.chan_config == 2) {
00138                 return NULL;
00139             }
00140         case 1:
00141             if (!ac->tags_mapped && type == TYPE_SCE) {
00142                 ac->tags_mapped++;
00143                 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00144             }
00145         default:
00146             return NULL;
00147     }
00148 }
00149 
00158 static int output_configure(AACContext *ac, enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00159         enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], int channel_config) {
00160     AVCodecContext *avctx = ac->avccontext;
00161     int i, type, channels = 0;
00162 
00163     if(!memcmp(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])))
00164         return 0; /* no change */
00165 
00166     memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00167 
00168     /* Allocate or free elements depending on if they are in the
00169      * current program configuration.
00170      *
00171      * Set up default 1:1 output mapping.
00172      *
00173      * For a 5.1 stream the output order will be:
00174      *    [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
00175      */
00176 
00177     for(i = 0; i < MAX_ELEM_ID; i++) {
00178         for(type = 0; type < 4; type++) {
00179             if(che_pos[type][i]) {
00180                 if(!ac->che[type][i] && !(ac->che[type][i] = av_mallocz(sizeof(ChannelElement))))
00181                     return AVERROR(ENOMEM);
00182                 if(type != TYPE_CCE) {
00183                     ac->output_data[channels++] = ac->che[type][i]->ch[0].ret;
00184                     if(type == TYPE_CPE) {
00185                         ac->output_data[channels++] = ac->che[type][i]->ch[1].ret;
00186                     }
00187                 }
00188             } else
00189                 av_freep(&ac->che[type][i]);
00190         }
00191     }
00192 
00193     if (channel_config) {
00194         memset(ac->tag_che_map, 0,       4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00195         ac->tags_mapped = 0;
00196     } else {
00197         memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00198         ac->tags_mapped = 4*MAX_ELEM_ID;
00199     }
00200 
00201     avctx->channels = channels;
00202 
00203     return 0;
00204 }
00205 
00213 static void decode_channel_map(enum ChannelPosition *cpe_map,
00214         enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) {
00215     while(n--) {
00216         enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
00217         map[get_bits(gb, 4)] = type;
00218     }
00219 }
00220 
00228 static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00229         GetBitContext * gb) {
00230     int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00231 
00232     skip_bits(gb, 2);  // object_type
00233 
00234     sampling_index = get_bits(gb, 4);
00235     if(sampling_index > 12) {
00236         av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
00237         return -1;
00238     }
00239     ac->m4ac.sampling_index = sampling_index;
00240     ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index];
00241     num_front       = get_bits(gb, 4);
00242     num_side        = get_bits(gb, 4);
00243     num_back        = get_bits(gb, 4);
00244     num_lfe         = get_bits(gb, 2);
00245     num_assoc_data  = get_bits(gb, 3);
00246     num_cc          = get_bits(gb, 4);
00247 
00248     if (get_bits1(gb))
00249         skip_bits(gb, 4); // mono_mixdown_tag
00250     if (get_bits1(gb))
00251         skip_bits(gb, 4); // stereo_mixdown_tag
00252 
00253     if (get_bits1(gb))
00254         skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
00255 
00256     decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00257     decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE,  gb, num_side );
00258     decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK,  gb, num_back );
00259     decode_channel_map(NULL,                  new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE,   gb, num_lfe  );
00260 
00261     skip_bits_long(gb, 4 * num_assoc_data);
00262 
00263     decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC,    gb, num_cc   );
00264 
00265     align_get_bits(gb);
00266 
00267     /* comment field, first byte is length */
00268     skip_bits_long(gb, 8 * get_bits(gb, 8));
00269     return 0;
00270 }
00271 
00280 static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00281         int channel_config)
00282 {
00283     if(channel_config < 1 || channel_config > 7) {
00284         av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00285                channel_config);
00286         return -1;
00287     }
00288 
00289     /* default channel configurations:
00290      *
00291      * 1ch : front center (mono)
00292      * 2ch : L + R (stereo)
00293      * 3ch : front center + L + R
00294      * 4ch : front center + L + R + back center
00295      * 5ch : front center + L + R + back stereo
00296      * 6ch : front center + L + R + back stereo + LFE
00297      * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
00298      */
00299 
00300     if(channel_config != 2)
00301         new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
00302     if(channel_config > 1)
00303         new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
00304     if(channel_config == 4)
00305         new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;  // back center
00306     if(channel_config > 4)
00307         new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00308                                  = AAC_CHANNEL_BACK;  // back stereo
00309     if(channel_config > 5)
00310         new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;   // LFE
00311     if(channel_config == 7)
00312         new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
00313 
00314     return 0;
00315 }
00316 
00322 static int decode_ga_specific_config(AACContext * ac, GetBitContext * gb, int channel_config) {
00323     enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00324     int extension_flag, ret;
00325 
00326     if(get_bits1(gb)) {  // frameLengthFlag
00327         ff_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1);
00328         return -1;
00329     }
00330 
00331     if (get_bits1(gb))       // dependsOnCoreCoder
00332         skip_bits(gb, 14);   // coreCoderDelay
00333     extension_flag = get_bits1(gb);
00334 
00335     if(ac->m4ac.object_type == AOT_AAC_SCALABLE ||
00336        ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
00337         skip_bits(gb, 3);     // layerNr
00338 
00339     memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00340     if (channel_config == 0) {
00341         skip_bits(gb, 4);  // element_instance_tag
00342         if((ret = decode_pce(ac, new_che_pos, gb)))
00343             return ret;
00344     } else {
00345         if((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
00346             return ret;
00347     }
00348     if((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config)))
00349         return ret;
00350 
00351     if (extension_flag) {
00352         switch (ac->m4ac.object_type) {
00353             case AOT_ER_BSAC:
00354                 skip_bits(gb, 5);    // numOfSubFrame
00355                 skip_bits(gb, 11);   // layer_length
00356                 break;
00357             case AOT_ER_AAC_LC:
00358             case AOT_ER_AAC_LTP:
00359             case AOT_ER_AAC_SCALABLE:
00360             case AOT_ER_AAC_LD:
00361                 skip_bits(gb, 3);  /* aacSectionDataResilienceFlag
00362                                     * aacScalefactorDataResilienceFlag
00363                                     * aacSpectralDataResilienceFlag
00364                                     */
00365                 break;
00366         }
00367         skip_bits1(gb);    // extensionFlag3 (TBD in version 3)
00368     }
00369     return 0;
00370 }
00371 
00380 static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) {
00381     GetBitContext gb;
00382     int i;
00383 
00384     init_get_bits(&gb, data, data_size * 8);
00385 
00386     if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
00387         return -1;
00388     if(ac->m4ac.sampling_index > 12) {
00389         av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
00390         return -1;
00391     }
00392 
00393     skip_bits_long(&gb, i);
00394 
00395     switch (ac->m4ac.object_type) {
00396     case AOT_AAC_MAIN:
00397     case AOT_AAC_LC:
00398         if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
00399             return -1;
00400         break;
00401     default:
00402         av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00403                ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
00404         return -1;
00405     }
00406     return 0;
00407 }
00408 
00416 static av_always_inline int lcg_random(int previous_val) {
00417     return previous_val * 1664525 + 1013904223;
00418 }
00419 
00420 static void reset_predict_state(PredictorState * ps) {
00421     ps->r0 = 0.0f;
00422     ps->r1 = 0.0f;
00423     ps->cor0 = 0.0f;
00424     ps->cor1 = 0.0f;
00425     ps->var0 = 1.0f;
00426     ps->var1 = 1.0f;
00427 }
00428 
00429 static void reset_all_predictors(PredictorState * ps) {
00430     int i;
00431     for (i = 0; i < MAX_PREDICTORS; i++)
00432         reset_predict_state(&ps[i]);
00433 }
00434 
00435 static void reset_predictor_group(PredictorState * ps, int group_num) {
00436     int i;
00437     for (i = group_num-1; i < MAX_PREDICTORS; i+=30)
00438         reset_predict_state(&ps[i]);
00439 }
00440 
00441 static av_cold int aac_decode_init(AVCodecContext * avccontext) {
00442     AACContext * ac = avccontext->priv_data;
00443     int i;
00444 
00445     ac->avccontext = avccontext;
00446 
00447     if (avccontext->extradata_size > 0) {
00448         if(decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
00449             return -1;
00450         avccontext->sample_rate = ac->m4ac.sample_rate;
00451     } else if (avccontext->channels > 0) {
00452         enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00453         memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00454         if(set_default_channel_config(ac, new_che_pos, avccontext->channels - (avccontext->channels == 8)))
00455             return -1;
00456         if(output_configure(ac, ac->che_pos, new_che_pos, 1))
00457             return -1;
00458         ac->m4ac.sample_rate = avccontext->sample_rate;
00459     } else {
00460         ff_log_missing_feature(ac->avccontext, "Implicit channel configuration is", 0);
00461         return -1;
00462     }
00463 
00464     avccontext->sample_fmt  = SAMPLE_FMT_S16;
00465     avccontext->frame_size  = 1024;
00466 
00467     AAC_INIT_VLC_STATIC( 0, 144);
00468     AAC_INIT_VLC_STATIC( 1, 114);
00469     AAC_INIT_VLC_STATIC( 2, 188);
00470     AAC_INIT_VLC_STATIC( 3, 180);
00471     AAC_INIT_VLC_STATIC( 4, 172);
00472     AAC_INIT_VLC_STATIC( 5, 140);
00473     AAC_INIT_VLC_STATIC( 6, 168);
00474     AAC_INIT_VLC_STATIC( 7, 114);
00475     AAC_INIT_VLC_STATIC( 8, 262);
00476     AAC_INIT_VLC_STATIC( 9, 248);
00477     AAC_INIT_VLC_STATIC(10, 384);
00478 
00479     dsputil_init(&ac->dsp, avccontext);
00480 
00481     ac->random_state = 0x1f2e3d4c;
00482 
00483     // -1024 - Compensate wrong IMDCT method.
00484     // 32768 - Required to scale values to the correct range for the bias method
00485     //         for float to int16 conversion.
00486 
00487     if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
00488         ac->add_bias = 385.0f;
00489         ac->sf_scale = 1. / (-1024. * 32768.);
00490         ac->sf_offset = 0;
00491     } else {
00492         ac->add_bias = 0.0f;
00493         ac->sf_scale = 1. / -1024.;
00494         ac->sf_offset = 60;
00495     }
00496 
00497 #if !CONFIG_HARDCODED_TABLES
00498     for (i = 0; i < 428; i++)
00499         ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
00500 #endif /* CONFIG_HARDCODED_TABLES */
00501 
00502     INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00503         ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00504         ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00505         352);
00506 
00507     ff_mdct_init(&ac->mdct, 11, 1);
00508     ff_mdct_init(&ac->mdct_small, 8, 1);
00509     // window initialization
00510     ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00511     ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00512     ff_sine_window_init(ff_sine_1024, 1024);
00513     ff_sine_window_init(ff_sine_128, 128);
00514 
00515     return 0;
00516 }
00517 
00521 static void skip_data_stream_element(GetBitContext * gb) {
00522     int byte_align = get_bits1(gb);
00523     int count = get_bits(gb, 8);
00524     if (count == 255)
00525         count += get_bits(gb, 8);
00526     if (byte_align)
00527         align_get_bits(gb);
00528     skip_bits_long(gb, 8 * count);
00529 }
00530 
00531 static int decode_prediction(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb) {
00532     int sfb;
00533     if (get_bits1(gb)) {
00534         ics->predictor_reset_group = get_bits(gb, 5);
00535         if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00536             av_log(ac->avccontext, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00537             return -1;
00538         }
00539     }
00540     for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00541         ics->prediction_used[sfb] = get_bits1(gb);
00542     }
00543     return 0;
00544 }
00545 
00551 static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) {
00552     if (get_bits1(gb)) {
00553         av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
00554         memset(ics, 0, sizeof(IndividualChannelStream));
00555         return -1;
00556     }
00557     ics->window_sequence[1] = ics->window_sequence[0];
00558     ics->window_sequence[0] = get_bits(gb, 2);
00559     ics->use_kb_window[1] = ics->use_kb_window[0];
00560     ics->use_kb_window[0] = get_bits1(gb);
00561     ics->num_window_groups = 1;
00562     ics->group_len[0] = 1;
00563     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00564         int i;
00565         ics->max_sfb = get_bits(gb, 4);
00566         for (i = 0; i < 7; i++) {
00567             if (get_bits1(gb)) {
00568                 ics->group_len[ics->num_window_groups-1]++;
00569             } else {
00570                 ics->num_window_groups++;
00571                 ics->group_len[ics->num_window_groups-1] = 1;
00572             }
00573         }
00574         ics->num_windows   = 8;
00575         ics->swb_offset    =      swb_offset_128[ac->m4ac.sampling_index];
00576         ics->num_swb       =  ff_aac_num_swb_128[ac->m4ac.sampling_index];
00577         ics->tns_max_bands =   tns_max_bands_128[ac->m4ac.sampling_index];
00578         ics->predictor_present = 0;
00579     } else {
00580         ics->max_sfb       = get_bits(gb, 6);
00581         ics->num_windows   = 1;
00582         ics->swb_offset    =     swb_offset_1024[ac->m4ac.sampling_index];
00583         ics->num_swb       = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00584         ics->tns_max_bands =  tns_max_bands_1024[ac->m4ac.sampling_index];
00585         ics->predictor_present = get_bits1(gb);
00586         ics->predictor_reset_group = 0;
00587         if (ics->predictor_present) {
00588             if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00589                 if (decode_prediction(ac, ics, gb)) {
00590                     memset(ics, 0, sizeof(IndividualChannelStream));
00591                     return -1;
00592                 }
00593             } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00594                 av_log(ac->avccontext, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00595                 memset(ics, 0, sizeof(IndividualChannelStream));
00596                 return -1;
00597             } else {
00598                 ff_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1);
00599                 memset(ics, 0, sizeof(IndividualChannelStream));
00600                 return -1;
00601             }
00602         }
00603     }
00604 
00605     if(ics->max_sfb > ics->num_swb) {
00606         av_log(ac->avccontext, AV_LOG_ERROR,
00607             "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00608             ics->max_sfb, ics->num_swb);
00609         memset(ics, 0, sizeof(IndividualChannelStream));
00610         return -1;
00611     }
00612 
00613     return 0;
00614 }
00615 
00624 static int decode_band_types(AACContext * ac, enum BandType band_type[120],
00625         int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) {
00626     int g, idx = 0;
00627     const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00628     for (g = 0; g < ics->num_window_groups; g++) {
00629         int k = 0;
00630         while (k < ics->max_sfb) {
00631             uint8_t sect_len = k;
00632             int sect_len_incr;
00633             int sect_band_type = get_bits(gb, 4);
00634             if (sect_band_type == 12) {
00635                 av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
00636                 return -1;
00637             }
00638             while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1)
00639                 sect_len += sect_len_incr;
00640             sect_len += sect_len_incr;
00641             if (sect_len > ics->max_sfb || sect_len == 0) {
00642                 av_log(ac->avccontext, AV_LOG_ERROR,
00643                     "Number of bands (%d) is invalid, limit (%d).\n",
00644                     sect_len, ics->max_sfb);
00645                 return -1;
00646             }
00647             for (; k < sect_len; k++) {
00648                 band_type        [idx]   = sect_band_type;
00649                 band_type_run_end[idx++] = sect_len;
00650             }
00651         }
00652     }
00653     return 0;
00654 }
00655 
00666 static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb,
00667         unsigned int global_gain, IndividualChannelStream * ics,
00668         enum BandType band_type[120], int band_type_run_end[120]) {
00669     const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
00670     int g, i, idx = 0;
00671     int offset[3] = { global_gain, global_gain - 90, 100 };
00672     int noise_flag = 1;
00673     static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00674     for (g = 0; g < ics->num_window_groups; g++) {
00675         for (i = 0; i < ics->max_sfb;) {
00676             int run_end = band_type_run_end[idx];
00677             if (band_type[idx] == ZERO_BT) {
00678                 for(; i < run_end; i++, idx++)
00679                     sf[idx] = 0.;
00680             }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00681                 for(; i < run_end; i++, idx++) {
00682                     offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00683                     if(offset[2] > 255U) {
00684                         av_log(ac->avccontext, AV_LOG_ERROR,
00685                             "%s (%d) out of range.\n", sf_str[2], offset[2]);
00686                         return -1;
00687                     }
00688                     sf[idx]  = ff_aac_pow2sf_tab[-offset[2] + 300];
00689                 }
00690             }else if(band_type[idx] == NOISE_BT) {
00691                 for(; i < run_end; i++, idx++) {
00692                     if(noise_flag-- > 0)
00693                         offset[1] += get_bits(gb, 9) - 256;
00694                     else
00695                         offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00696                     if(offset[1] > 255U) {
00697                         av_log(ac->avccontext, AV_LOG_ERROR,
00698                             "%s (%d) out of range.\n", sf_str[1], offset[1]);
00699                         return -1;
00700                     }
00701                     sf[idx]  = -ff_aac_pow2sf_tab[ offset[1] + sf_offset + 100];
00702                 }
00703             }else {
00704                 for(; i < run_end; i++, idx++) {
00705                     offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00706                     if(offset[0] > 255U) {
00707                         av_log(ac->avccontext, AV_LOG_ERROR,
00708                             "%s (%d) out of range.\n", sf_str[0], offset[0]);
00709                         return -1;
00710                     }
00711                     sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
00712                 }
00713             }
00714         }
00715     }
00716     return 0;
00717 }
00718 
00722 static int decode_pulses(Pulse * pulse, GetBitContext * gb, const uint16_t * swb_offset, int num_swb) {
00723     int i, pulse_swb;
00724     pulse->num_pulse = get_bits(gb, 2) + 1;
00725     pulse_swb        = get_bits(gb, 6);
00726     if (pulse_swb >= num_swb)
00727         return -1;
00728     pulse->pos[0]    = swb_offset[pulse_swb];
00729     pulse->pos[0]   += get_bits(gb, 5);
00730     if (pulse->pos[0] > 1023)
00731         return -1;
00732     pulse->amp[0]    = get_bits(gb, 4);
00733     for (i = 1; i < pulse->num_pulse; i++) {
00734         pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i-1];
00735         if (pulse->pos[i] > 1023)
00736             return -1;
00737         pulse->amp[i] = get_bits(gb, 4);
00738     }
00739     return 0;
00740 }
00741 
00747 static int decode_tns(AACContext * ac, TemporalNoiseShaping * tns,
00748         GetBitContext * gb, const IndividualChannelStream * ics) {
00749     int w, filt, i, coef_len, coef_res, coef_compress;
00750     const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00751     const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00752     for (w = 0; w < ics->num_windows; w++) {
00753         if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00754             coef_res = get_bits1(gb);
00755 
00756             for (filt = 0; filt < tns->n_filt[w]; filt++) {
00757                 int tmp2_idx;
00758                 tns->length[w][filt] = get_bits(gb, 6 - 2*is8);
00759 
00760                 if ((tns->order[w][filt] = get_bits(gb, 5 - 2*is8)) > tns_max_order) {
00761                     av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.",
00762                            tns->order[w][filt], tns_max_order);
00763                     tns->order[w][filt] = 0;
00764                     return -1;
00765                 }
00766                 if (tns->order[w][filt]) {
00767                     tns->direction[w][filt] = get_bits1(gb);
00768                     coef_compress = get_bits1(gb);
00769                     coef_len = coef_res + 3 - coef_compress;
00770                     tmp2_idx = 2*coef_compress + coef_res;
00771 
00772                     for (i = 0; i < tns->order[w][filt]; i++)
00773                         tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00774                 }
00775             }
00776         }
00777     }
00778     return 0;
00779 }
00780 
00788 static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb,
00789         int ms_present) {
00790     int idx;
00791     if (ms_present == 1) {
00792         for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00793             cpe->ms_mask[idx] = get_bits1(gb);
00794     } else if (ms_present == 2) {
00795         memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00796     }
00797 }
00798 
00811 static int decode_spectrum_and_dequant(AACContext * ac, float coef[1024], GetBitContext * gb, float sf[120],
00812         int pulse_present, const Pulse * pulse, const IndividualChannelStream * ics, enum BandType band_type[120]) {
00813     int i, k, g, idx = 0;
00814     const int c = 1024/ics->num_windows;
00815     const uint16_t * offsets = ics->swb_offset;
00816     float *coef_base = coef;
00817     static const float sign_lookup[] = { 1.0f, -1.0f };
00818 
00819     for (g = 0; g < ics->num_windows; g++)
00820         memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float)*(c - offsets[ics->max_sfb]));
00821 
00822     for (g = 0; g < ics->num_window_groups; g++) {
00823         for (i = 0; i < ics->max_sfb; i++, idx++) {
00824             const int cur_band_type = band_type[idx];
00825             const int dim = cur_band_type >= FIRST_PAIR_BT ? 2 : 4;
00826             const int is_cb_unsigned = IS_CODEBOOK_UNSIGNED(cur_band_type);
00827             int group;
00828             if (cur_band_type == ZERO_BT || cur_band_type == INTENSITY_BT2 || cur_band_type == INTENSITY_BT) {
00829                 for (group = 0; group < ics->group_len[g]; group++) {
00830                     memset(coef + group * 128 + offsets[i], 0, (offsets[i+1] - offsets[i])*sizeof(float));
00831                 }
00832             }else if (cur_band_type == NOISE_BT) {
00833                 for (group = 0; group < ics->group_len[g]; group++) {
00834                     float scale;
00835                     float band_energy = 0;
00836                     for (k = offsets[i]; k < offsets[i+1]; k++) {
00837                         ac->random_state  = lcg_random(ac->random_state);
00838                         coef[group*128+k] = ac->random_state;
00839                         band_energy += coef[group*128+k]*coef[group*128+k];
00840                     }
00841                     scale = sf[idx] / sqrtf(band_energy);
00842                     for (k = offsets[i]; k < offsets[i+1]; k++) {
00843                         coef[group*128+k] *= scale;
00844                     }
00845                 }
00846             }else {
00847                 for (group = 0; group < ics->group_len[g]; group++) {
00848                     for (k = offsets[i]; k < offsets[i+1]; k += dim) {
00849                         const int index = get_vlc2(gb, vlc_spectral[cur_band_type - 1].table, 6, 3);
00850                         const int coef_tmp_idx = (group << 7) + k;
00851                         const float *vq_ptr;
00852                         int j;
00853                         if(index >= ff_aac_spectral_sizes[cur_band_type - 1]) {
00854                             av_log(ac->avccontext, AV_LOG_ERROR,
00855                                 "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
00856                                 cur_band_type - 1, index, ff_aac_spectral_sizes[cur_band_type - 1]);
00857                             return -1;
00858                         }
00859                         vq_ptr = &ff_aac_codebook_vectors[cur_band_type - 1][index * dim];
00860                         if (is_cb_unsigned) {
00861                             if (vq_ptr[0]) coef[coef_tmp_idx    ] = sign_lookup[get_bits1(gb)];
00862                             if (vq_ptr[1]) coef[coef_tmp_idx + 1] = sign_lookup[get_bits1(gb)];
00863                             if (dim == 4) {
00864                                 if (vq_ptr[2]) coef[coef_tmp_idx + 2] = sign_lookup[get_bits1(gb)];
00865                                 if (vq_ptr[3]) coef[coef_tmp_idx + 3] = sign_lookup[get_bits1(gb)];
00866                             }
00867                             if (cur_band_type == ESC_BT) {
00868                                 for (j = 0; j < 2; j++) {
00869                                     if (vq_ptr[j] == 64.0f) {
00870                                         int n = 4;
00871                                         /* The total length of escape_sequence must be < 22 bits according
00872                                            to the specification (i.e. max is 11111111110xxxxxxxxxx). */
00873                                         while (get_bits1(gb) && n < 15) n++;
00874                                         if(n == 15) {
00875                                             av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
00876                                             return -1;
00877                                         }
00878                                         n = (1<<n) + get_bits(gb, n);
00879                                         coef[coef_tmp_idx + j] *= cbrtf(n) * n;
00880                                     }else
00881                                         coef[coef_tmp_idx + j] *= vq_ptr[j];
00882                                 }
00883                             }else
00884                             {
00885                                 coef[coef_tmp_idx    ] *= vq_ptr[0];
00886                                 coef[coef_tmp_idx + 1] *= vq_ptr[1];
00887                                 if (dim == 4) {
00888                                     coef[coef_tmp_idx + 2] *= vq_ptr[2];
00889                                     coef[coef_tmp_idx + 3] *= vq_ptr[3];
00890                                 }
00891                             }
00892                         }else {
00893                             coef[coef_tmp_idx    ] = vq_ptr[0];
00894                             coef[coef_tmp_idx + 1] = vq_ptr[1];
00895                             if (dim == 4) {
00896                                 coef[coef_tmp_idx + 2] = vq_ptr[2];
00897                                 coef[coef_tmp_idx + 3] = vq_ptr[3];
00898                             }
00899                         }
00900                         coef[coef_tmp_idx    ] *= sf[idx];
00901                         coef[coef_tmp_idx + 1] *= sf[idx];
00902                         if (dim == 4) {
00903                             coef[coef_tmp_idx + 2] *= sf[idx];
00904                             coef[coef_tmp_idx + 3] *= sf[idx];
00905                         }
00906                     }
00907                 }
00908             }
00909         }
00910         coef += ics->group_len[g]<<7;
00911     }
00912 
00913     if (pulse_present) {
00914         idx = 0;
00915         for(i = 0; i < pulse->num_pulse; i++){
00916             float co  = coef_base[ pulse->pos[i] ];
00917             while(offsets[idx + 1] <= pulse->pos[i])
00918                 idx++;
00919             if (band_type[idx] != NOISE_BT && sf[idx]) {
00920                 float ico = -pulse->amp[i];
00921                 if (co) {
00922                     co /= sf[idx];
00923                     ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
00924                 }
00925                 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
00926             }
00927         }
00928     }
00929     return 0;
00930 }
00931 
00932 static av_always_inline float flt16_round(float pf) {
00933     int exp;
00934     pf = frexpf(pf, &exp);
00935     pf = ldexpf(roundf(ldexpf(pf, 8)), exp-8);
00936     return pf;
00937 }
00938 
00939 static av_always_inline float flt16_even(float pf) {
00940     int exp;
00941     pf = frexpf(pf, &exp);
00942     pf = ldexpf(rintf(ldexpf(pf, 8)), exp-8);
00943     return pf;
00944 }
00945 
00946 static av_always_inline float flt16_trunc(float pf) {
00947     int exp;
00948     pf = frexpf(pf, &exp);
00949     pf = ldexpf(truncf(ldexpf(pf, 8)), exp-8);
00950     return pf;
00951 }
00952 
00953 static void predict(AACContext * ac, PredictorState * ps, float* coef, int output_enable) {
00954     const float a     = 0.953125; // 61.0/64
00955     const float alpha = 0.90625;  // 29.0/32
00956     float e0, e1;
00957     float pv;
00958     float k1, k2;
00959 
00960     k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
00961     k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
00962 
00963     pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
00964     if (output_enable)
00965         *coef += pv * ac->sf_scale;
00966 
00967     e0 = *coef / ac->sf_scale;
00968     e1 = e0 - k1 * ps->r0;
00969 
00970     ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
00971     ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
00972     ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
00973     ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
00974 
00975     ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
00976     ps->r0 = flt16_trunc(a * e0);
00977 }
00978 
00982 static void apply_prediction(AACContext * ac, SingleChannelElement * sce) {
00983     int sfb, k;
00984 
00985     if (!sce->ics.predictor_initialized) {
00986         reset_all_predictors(sce->predictor_state);
00987         sce->ics.predictor_initialized = 1;
00988     }
00989 
00990     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00991         for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
00992             for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
00993                 predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
00994                     sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
00995             }
00996         }
00997         if (sce->ics.predictor_reset_group)
00998             reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
00999     } else
01000         reset_all_predictors(sce->predictor_state);
01001 }
01002 
01011 static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) {
01012     Pulse pulse;
01013     TemporalNoiseShaping * tns = &sce->tns;
01014     IndividualChannelStream * ics = &sce->ics;
01015     float * out = sce->coeffs;
01016     int global_gain, pulse_present = 0;
01017 
01018     /* This assignment is to silence a GCC warning about the variable being used
01019      * uninitialized when in fact it always is.
01020      */
01021     pulse.num_pulse = 0;
01022 
01023     global_gain = get_bits(gb, 8);
01024 
01025     if (!common_window && !scale_flag) {
01026         if (decode_ics_info(ac, ics, gb, 0) < 0)
01027             return -1;
01028     }
01029 
01030     if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01031         return -1;
01032     if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01033         return -1;
01034 
01035     pulse_present = 0;
01036     if (!scale_flag) {
01037         if ((pulse_present = get_bits1(gb))) {
01038             if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01039                 av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01040                 return -1;
01041             }
01042             if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01043                 av_log(ac->avccontext, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01044                 return -1;
01045             }
01046         }
01047         if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01048             return -1;
01049         if (get_bits1(gb)) {
01050             ff_log_missing_feature(ac->avccontext, "SSR", 1);
01051             return -1;
01052         }
01053     }
01054 
01055     if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01056         return -1;
01057 
01058     if(ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01059         apply_prediction(ac, sce);
01060 
01061     return 0;
01062 }
01063 
01067 static void apply_mid_side_stereo(ChannelElement * cpe) {
01068     const IndividualChannelStream * ics = &cpe->ch[0].ics;
01069     float *ch0 = cpe->ch[0].coeffs;
01070     float *ch1 = cpe->ch[1].coeffs;
01071     int g, i, k, group, idx = 0;
01072     const uint16_t * offsets = ics->swb_offset;
01073     for (g = 0; g < ics->num_window_groups; g++) {
01074         for (i = 0; i < ics->max_sfb; i++, idx++) {
01075             if (cpe->ms_mask[idx] &&
01076                 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01077                 for (group = 0; group < ics->group_len[g]; group++) {
01078                     for (k = offsets[i]; k < offsets[i+1]; k++) {
01079                         float tmp = ch0[group*128 + k] - ch1[group*128 + k];
01080                         ch0[group*128 + k] += ch1[group*128 + k];
01081                         ch1[group*128 + k] = tmp;
01082                     }
01083                 }
01084             }
01085         }
01086         ch0 += ics->group_len[g]*128;
01087         ch1 += ics->group_len[g]*128;
01088     }
01089 }
01090 
01098 static void apply_intensity_stereo(ChannelElement * cpe, int ms_present) {
01099     const IndividualChannelStream * ics = &cpe->ch[1].ics;
01100     SingleChannelElement * sce1 = &cpe->ch[1];
01101     float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01102     const uint16_t * offsets = ics->swb_offset;
01103     int g, group, i, k, idx = 0;
01104     int c;
01105     float scale;
01106     for (g = 0; g < ics->num_window_groups; g++) {
01107         for (i = 0; i < ics->max_sfb;) {
01108             if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01109                 const int bt_run_end = sce1->band_type_run_end[idx];
01110                 for (; i < bt_run_end; i++, idx++) {
01111                     c = -1 + 2 * (sce1->band_type[idx] - 14);
01112                     if (ms_present)
01113                         c *= 1 - 2 * cpe->ms_mask[idx];
01114                     scale = c * sce1->sf[idx];
01115                     for (group = 0; group < ics->group_len[g]; group++)
01116                         for (k = offsets[i]; k < offsets[i+1]; k++)
01117                             coef1[group*128 + k] = scale * coef0[group*128 + k];
01118                 }
01119             } else {
01120                 int bt_run_end = sce1->band_type_run_end[idx];
01121                 idx += bt_run_end - i;
01122                 i    = bt_run_end;
01123             }
01124         }
01125         coef0 += ics->group_len[g]*128;
01126         coef1 += ics->group_len[g]*128;
01127     }
01128 }
01129 
01137 static int decode_cpe(AACContext * ac, GetBitContext * gb, ChannelElement * cpe) {
01138     int i, ret, common_window, ms_present = 0;
01139 
01140     common_window = get_bits1(gb);
01141     if (common_window) {
01142         if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
01143             return -1;
01144         i = cpe->ch[1].ics.use_kb_window[0];
01145         cpe->ch[1].ics = cpe->ch[0].ics;
01146         cpe->ch[1].ics.use_kb_window[1] = i;
01147         ms_present = get_bits(gb, 2);
01148         if(ms_present == 3) {
01149             av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01150             return -1;
01151         } else if(ms_present)
01152             decode_mid_side_stereo(cpe, gb, ms_present);
01153     }
01154     if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01155         return ret;
01156     if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01157         return ret;
01158 
01159     if (common_window) {
01160         if (ms_present)
01161             apply_mid_side_stereo(cpe);
01162         if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01163             apply_prediction(ac, &cpe->ch[0]);
01164             apply_prediction(ac, &cpe->ch[1]);
01165         }
01166     }
01167 
01168     apply_intensity_stereo(cpe, ms_present);
01169     return 0;
01170 }
01171 
01179 static int decode_cce(AACContext * ac, GetBitContext * gb, ChannelElement * che) {
01180     int num_gain = 0;
01181     int c, g, sfb, ret;
01182     int sign;
01183     float scale;
01184     SingleChannelElement * sce = &che->ch[0];
01185     ChannelCoupling * coup     = &che->coup;
01186 
01187     coup->coupling_point = 2*get_bits1(gb);
01188     coup->num_coupled = get_bits(gb, 3);
01189     for (c = 0; c <= coup->num_coupled; c++) {
01190         num_gain++;
01191         coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01192         coup->id_select[c] = get_bits(gb, 4);
01193         if (coup->type[c] == TYPE_CPE) {
01194             coup->ch_select[c] = get_bits(gb, 2);
01195             if (coup->ch_select[c] == 3)
01196                 num_gain++;
01197         } else
01198             coup->ch_select[c] = 2;
01199     }
01200     coup->coupling_point += get_bits1(gb);
01201 
01202     if (coup->coupling_point == 2) {
01203         av_log(ac->avccontext, AV_LOG_ERROR,
01204             "Independently switched CCE with 'invalid' domain signalled.\n");
01205         memset(coup, 0, sizeof(ChannelCoupling));
01206         return -1;
01207     }
01208 
01209     sign = get_bits(gb, 1);
01210     scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
01211 
01212     if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01213         return ret;
01214 
01215     for (c = 0; c < num_gain; c++) {
01216         int idx = 0;
01217         int cge = 1;
01218         int gain = 0;
01219         float gain_cache = 1.;
01220         if (c) {
01221             cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01222             gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01223             gain_cache = pow(scale, -gain);
01224         }
01225         if (coup->coupling_point == AFTER_IMDCT) {
01226             coup->gain[c][0] = gain_cache;
01227         } else {
01228             for (g = 0; g < sce->ics.num_window_groups; g++) {
01229                 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01230                     if (sce->band_type[idx] != ZERO_BT) {
01231                         if (!cge) {
01232                             int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01233                                 if (t) {
01234                                 int s = 1;
01235                                 t = gain += t;
01236                                 if (sign) {
01237                                     s  -= 2 * (t & 0x1);
01238                                     t >>= 1;
01239                                 }
01240                                 gain_cache = pow(scale, -t) * s;
01241                             }
01242                         }
01243                         coup->gain[c][idx] = gain_cache;
01244                     }
01245                 }
01246             }
01247         }
01248     }
01249     return 0;
01250 }
01251 
01260 static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) {
01261     // TODO : sbr_extension implementation
01262     ff_log_missing_feature(ac->avccontext, "SBR", 0);
01263     skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type
01264     return cnt;
01265 }
01266 
01272 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext * gb) {
01273     int i;
01274     int num_excl_chan = 0;
01275 
01276     do {
01277         for (i = 0; i < 7; i++)
01278             che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01279     } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01280 
01281     return num_excl_chan / 7;
01282 }
01283 
01291 static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) {
01292     int n = 1;
01293     int drc_num_bands = 1;
01294     int i;
01295 
01296     /* pce_tag_present? */
01297     if(get_bits1(gb)) {
01298         che_drc->pce_instance_tag  = get_bits(gb, 4);
01299         skip_bits(gb, 4); // tag_reserved_bits
01300         n++;
01301     }
01302 
01303     /* excluded_chns_present? */
01304     if(get_bits1(gb)) {
01305         n += decode_drc_channel_exclusions(che_drc, gb);
01306     }
01307 
01308     /* drc_bands_present? */
01309     if (get_bits1(gb)) {
01310         che_drc->band_incr            = get_bits(gb, 4);
01311         che_drc->interpolation_scheme = get_bits(gb, 4);
01312         n++;
01313         drc_num_bands += che_drc->band_incr;
01314         for (i = 0; i < drc_num_bands; i++) {
01315             che_drc->band_top[i] = get_bits(gb, 8);
01316             n++;
01317         }
01318     }
01319 
01320     /* prog_ref_level_present? */
01321     if (get_bits1(gb)) {
01322         che_drc->prog_ref_level = get_bits(gb, 7);
01323         skip_bits1(gb); // prog_ref_level_reserved_bits
01324         n++;
01325     }
01326 
01327     for (i = 0; i < drc_num_bands; i++) {
01328         che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01329         che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01330         n++;
01331     }
01332 
01333     return n;
01334 }
01335 
01343 static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) {
01344     int crc_flag = 0;
01345     int res = cnt;
01346     switch (get_bits(gb, 4)) { // extension type
01347         case EXT_SBR_DATA_CRC:
01348             crc_flag++;
01349         case EXT_SBR_DATA:
01350             res = decode_sbr_extension(ac, gb, crc_flag, cnt);
01351             break;
01352         case EXT_DYNAMIC_RANGE:
01353             res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01354             break;
01355         case EXT_FILL:
01356         case EXT_FILL_DATA:
01357         case EXT_DATA_ELEMENT:
01358         default:
01359             skip_bits_long(gb, 8*cnt - 4);
01360             break;
01361     };
01362     return res;
01363 }
01364 
01371 static void apply_tns(float coef[1024], TemporalNoiseShaping * tns, IndividualChannelStream * ics, int decode) {
01372     const int mmm = FFMIN(ics->tns_max_bands,  ics->max_sfb);
01373     int w, filt, m, i;
01374     int bottom, top, order, start, end, size, inc;
01375     float lpc[TNS_MAX_ORDER];
01376 
01377     for (w = 0; w < ics->num_windows; w++) {
01378         bottom = ics->num_swb;
01379         for (filt = 0; filt < tns->n_filt[w]; filt++) {
01380             top    = bottom;
01381             bottom = FFMAX(0, top - tns->length[w][filt]);
01382             order  = tns->order[w][filt];
01383             if (order == 0)
01384                 continue;
01385 
01386             // tns_decode_coef
01387             compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01388 
01389             start = ics->swb_offset[FFMIN(bottom, mmm)];
01390             end   = ics->swb_offset[FFMIN(   top, mmm)];
01391             if ((size = end - start) <= 0)
01392                 continue;
01393             if (tns->direction[w][filt]) {
01394                 inc = -1; start = end - 1;
01395             } else {
01396                 inc = 1;
01397             }
01398             start += w * 128;
01399 
01400             // ar filter
01401             for (m = 0; m < size; m++, start += inc)
01402                 for (i = 1; i <= FFMIN(m, order); i++)
01403                     coef[start] -= coef[start - i*inc] * lpc[i-1];
01404         }
01405     }
01406 }
01407 
01411 static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) {
01412     IndividualChannelStream * ics = &sce->ics;
01413     float * in = sce->coeffs;
01414     float * out = sce->ret;
01415     float * saved = sce->saved;
01416     const float * swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01417     const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01418     const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01419     float * buf = ac->buf_mdct;
01420     float * temp = ac->temp;
01421     int i;
01422 
01423     // imdct
01424     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01425         if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
01426             av_log(ac->avccontext, AV_LOG_WARNING,
01427                    "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
01428                    "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
01429         for (i = 0; i < 1024; i += 128)
01430             ff_imdct_half(&ac->mdct_small, buf + i, in + i);
01431     } else
01432         ff_imdct_half(&ac->mdct, buf, in);
01433 
01434     /* window overlapping
01435      * NOTE: To simplify the overlapping code, all 'meaningless' short to long
01436      * and long to short transitions are considered to be short to short
01437      * transitions. This leaves just two cases (long to long and short to short)
01438      * with a little special sauce for EIGHT_SHORT_SEQUENCE.
01439      */
01440     if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01441         (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01442         ac->dsp.vector_fmul_window(    out,               saved,            buf,         lwindow_prev, ac->add_bias, 512);
01443     } else {
01444         for (i = 0; i < 448; i++)
01445             out[i] = saved[i] + ac->add_bias;
01446 
01447         if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01448             ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, ac->add_bias, 64);
01449             ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      ac->add_bias, 64);
01450             ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      ac->add_bias, 64);
01451             ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      ac->add_bias, 64);
01452             ac->dsp.vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      ac->add_bias, 64);
01453             memcpy(                    out + 448 + 4*128, temp, 64 * sizeof(float));
01454         } else {
01455             ac->dsp.vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, ac->add_bias, 64);
01456             for (i = 576; i < 1024; i++)
01457                 out[i] = buf[i-512] + ac->add_bias;
01458         }
01459     }
01460 
01461     // buffer update
01462     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01463         for (i = 0; i < 64; i++)
01464             saved[i] = temp[64 + i] - ac->add_bias;
01465         ac->dsp.vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
01466         ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
01467         ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
01468         memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
01469     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01470         memcpy(                    saved,       buf + 512,        448 * sizeof(float));
01471         memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
01472     } else { // LONG_STOP or ONLY_LONG
01473         memcpy(                    saved,       buf + 512,        512 * sizeof(float));
01474     }
01475 }
01476 
01482 static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index) {
01483     IndividualChannelStream * ics = &cce->ch[0].ics;
01484     const uint16_t * offsets = ics->swb_offset;
01485     float * dest = target->coeffs;
01486     const float * src = cce->ch[0].coeffs;
01487     int g, i, group, k, idx = 0;
01488     if(ac->m4ac.object_type == AOT_AAC_LTP) {
01489         av_log(ac->avccontext, AV_LOG_ERROR,
01490                "Dependent coupling is not supported together with LTP\n");
01491         return;
01492     }
01493     for (g = 0; g < ics->num_window_groups; g++) {
01494         for (i = 0; i < ics->max_sfb; i++, idx++) {
01495             if (cce->ch[0].band_type[idx] != ZERO_BT) {
01496                 for (group = 0; group < ics->group_len[g]; group++) {
01497                     for (k = offsets[i]; k < offsets[i+1]; k++) {
01498                         // XXX dsputil-ize
01499                         dest[group*128+k] += cce->coup.gain[index][idx] * src[group*128+k];
01500                     }
01501                 }
01502             }
01503         }
01504         dest += ics->group_len[g]*128;
01505         src  += ics->group_len[g]*128;
01506     }
01507 }
01508 
01514 static void apply_independent_coupling(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index) {
01515     int i;
01516     const float gain = cce->coup.gain[index][0];
01517     const float bias = ac->add_bias;
01518     const float* src = cce->ch[0].ret;
01519     float* dest = target->ret;
01520 
01521     for (i = 0; i < 1024; i++)
01522         dest[i] += gain * (src[i] - bias);
01523 }
01524 
01531 static void apply_channel_coupling(AACContext * ac, ChannelElement * cc,
01532         enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point,
01533         void (*apply_coupling_method)(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index))
01534 {
01535     int i, c;
01536 
01537     for (i = 0; i < MAX_ELEM_ID; i++) {
01538         ChannelElement *cce = ac->che[TYPE_CCE][i];
01539         int index = 0;
01540 
01541         if (cce && cce->coup.coupling_point == coupling_point) {
01542             ChannelCoupling * coup = &cce->coup;
01543 
01544             for (c = 0; c <= coup->num_coupled; c++) {
01545                 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
01546                     if (coup->ch_select[c] != 1) {
01547                         apply_coupling_method(ac, &cc->ch[0], cce, index);
01548                         if (coup->ch_select[c] != 0)
01549                             index++;
01550                     }
01551                     if (coup->ch_select[c] != 2)
01552                         apply_coupling_method(ac, &cc->ch[1], cce, index++);
01553                 } else
01554                     index += 1 + (coup->ch_select[c] == 3);
01555             }
01556         }
01557     }
01558 }
01559 
01563 static void spectral_to_sample(AACContext * ac) {
01564     int i, type;
01565     for(type = 3; type >= 0; type--) {
01566         for (i = 0; i < MAX_ELEM_ID; i++) {
01567             ChannelElement *che = ac->che[type][i];
01568             if(che) {
01569                 if(type <= TYPE_CPE)
01570                     apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
01571                 if(che->ch[0].tns.present)
01572                     apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
01573                 if(che->ch[1].tns.present)
01574                     apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
01575                 if(type <= TYPE_CPE)
01576                     apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
01577                 if(type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT)
01578                     imdct_and_windowing(ac, &che->ch[0]);
01579                 if(type == TYPE_CPE)
01580                     imdct_and_windowing(ac, &che->ch[1]);
01581                 if(type <= TYPE_CCE)
01582                     apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
01583             }
01584         }
01585     }
01586 }
01587 
01588 static int parse_adts_frame_header(AACContext * ac, GetBitContext * gb) {
01589 
01590     int size;
01591     AACADTSHeaderInfo hdr_info;
01592 
01593     size = ff_aac_parse_header(gb, &hdr_info);
01594     if (size > 0) {
01595         if (hdr_info.chan_config)
01596             ac->m4ac.chan_config = hdr_info.chan_config;
01597         ac->m4ac.sample_rate     = hdr_info.sample_rate;
01598         ac->m4ac.sampling_index  = hdr_info.sampling_index;
01599         ac->m4ac.object_type     = hdr_info.object_type;
01600         if (hdr_info.num_aac_frames == 1) {
01601             if (!hdr_info.crc_absent)
01602                 skip_bits(gb, 16);
01603         } else {
01604             ff_log_missing_feature(ac->avccontext, "More than one AAC RDB per ADTS frame is", 0);
01605             return -1;
01606         }
01607     }
01608     return size;
01609 }
01610 
01611 static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, const uint8_t * buf, int buf_size) {
01612     AACContext * ac = avccontext->priv_data;
01613     ChannelElement * che = NULL;
01614     GetBitContext gb;
01615     enum RawDataBlockType elem_type;
01616     int err, elem_id, data_size_tmp;
01617 
01618     init_get_bits(&gb, buf, buf_size*8);
01619 
01620     if (show_bits(&gb, 12) == 0xfff) {
01621         if ((err = parse_adts_frame_header(ac, &gb)) < 0) {
01622             av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
01623             return -1;
01624         }
01625         if (ac->m4ac.sampling_index > 12) {
01626             av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
01627             return -1;
01628         }
01629     }
01630 
01631     // parse
01632     while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
01633         elem_id = get_bits(&gb, 4);
01634         err = -1;
01635 
01636         if(elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) {
01637             av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id);
01638             return -1;
01639         }
01640 
01641         switch (elem_type) {
01642 
01643         case TYPE_SCE:
01644             err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
01645             break;
01646 
01647         case TYPE_CPE:
01648             err = decode_cpe(ac, &gb, che);
01649             break;
01650 
01651         case TYPE_CCE:
01652             err = decode_cce(ac, &gb, che);
01653             break;
01654 
01655         case TYPE_LFE:
01656             err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
01657             break;
01658 
01659         case TYPE_DSE:
01660             skip_data_stream_element(&gb);
01661             err = 0;
01662             break;
01663 
01664         case TYPE_PCE:
01665         {
01666             enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
01667             memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
01668             if((err = decode_pce(ac, new_che_pos, &gb)))
01669                 break;
01670             err = output_configure(ac, ac->che_pos, new_che_pos, 0);
01671             break;
01672         }
01673 
01674         case TYPE_FIL:
01675             if (elem_id == 15)
01676                 elem_id += get_bits(&gb, 8) - 1;
01677             while (elem_id > 0)
01678                 elem_id -= decode_extension_payload(ac, &gb, elem_id);
01679             err = 0; /* FIXME */
01680             break;
01681 
01682         default:
01683             err = -1; /* should not happen, but keeps compiler happy */
01684             break;
01685         }
01686 
01687         if(err)
01688             return err;
01689     }
01690 
01691     spectral_to_sample(ac);
01692 
01693     if (!ac->is_saved) {
01694         ac->is_saved = 1;
01695         *data_size = 0;
01696         return buf_size;
01697     }
01698 
01699     data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);
01700     if(*data_size < data_size_tmp) {
01701         av_log(avccontext, AV_LOG_ERROR,
01702                "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
01703                *data_size, data_size_tmp);
01704         return -1;
01705     }
01706     *data_size = data_size_tmp;
01707 
01708     ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);
01709 
01710     return buf_size;
01711 }
01712 
01713 static av_cold int aac_decode_close(AVCodecContext * avccontext) {
01714     AACContext * ac = avccontext->priv_data;
01715     int i, type;
01716 
01717     for (i = 0; i < MAX_ELEM_ID; i++) {
01718         for(type = 0; type < 4; type++)
01719             av_freep(&ac->che[type][i]);
01720     }
01721 
01722     ff_mdct_end(&ac->mdct);
01723     ff_mdct_end(&ac->mdct_small);
01724     return 0 ;
01725 }
01726 
01727 AVCodec aac_decoder = {
01728     "aac",
01729     CODEC_TYPE_AUDIO,
01730     CODEC_ID_AAC,
01731     sizeof(AACContext),
01732     aac_decode_init,
01733     NULL,
01734     aac_decode_close,
01735     aac_decode_frame,
01736     .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
01737     .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
01738 };

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1