Libav 0.7.1
libavcodec/aacenc.c
Go to the documentation of this file.
00001 /*
00002  * AAC encoder
00003  * Copyright (C) 2008 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 /***********************************
00028  *              TODOs:
00029  * add sane pulse detection
00030  * add temporal noise shaping
00031  ***********************************/
00032 
00033 #include "libavutil/opt.h"
00034 #include "avcodec.h"
00035 #include "put_bits.h"
00036 #include "dsputil.h"
00037 #include "mpeg4audio.h"
00038 #include "kbdwin.h"
00039 #include "sinewin.h"
00040 
00041 #include "aac.h"
00042 #include "aactab.h"
00043 #include "aacenc.h"
00044 
00045 #include "psymodel.h"
00046 
00047 #define AAC_MAX_CHANNELS 6
00048 
00049 static const uint8_t swb_size_1024_96[] = {
00050     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
00051     12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
00052     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00053 };
00054 
00055 static const uint8_t swb_size_1024_64[] = {
00056     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
00057     12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
00058     40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
00059 };
00060 
00061 static const uint8_t swb_size_1024_48[] = {
00062     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00063     12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00064     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
00065     96
00066 };
00067 
00068 static const uint8_t swb_size_1024_32[] = {
00069     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00070     12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00071     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
00072 };
00073 
00074 static const uint8_t swb_size_1024_24[] = {
00075     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00076     12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
00077     32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
00078 };
00079 
00080 static const uint8_t swb_size_1024_16[] = {
00081     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00082     12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
00083     32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
00084 };
00085 
00086 static const uint8_t swb_size_1024_8[] = {
00087     12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00088     16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
00089     32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
00090 };
00091 
00092 static const uint8_t *swb_size_1024[] = {
00093     swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
00094     swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
00095     swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
00096     swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
00097 };
00098 
00099 static const uint8_t swb_size_128_96[] = {
00100     4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
00101 };
00102 
00103 static const uint8_t swb_size_128_48[] = {
00104     4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
00105 };
00106 
00107 static const uint8_t swb_size_128_24[] = {
00108     4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
00109 };
00110 
00111 static const uint8_t swb_size_128_16[] = {
00112     4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
00113 };
00114 
00115 static const uint8_t swb_size_128_8[] = {
00116     4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
00117 };
00118 
00119 static const uint8_t *swb_size_128[] = {
00120     /* the last entry on the following row is swb_size_128_64 but is a
00121        duplicate of swb_size_128_96 */
00122     swb_size_128_96, swb_size_128_96, swb_size_128_96,
00123     swb_size_128_48, swb_size_128_48, swb_size_128_48,
00124     swb_size_128_24, swb_size_128_24, swb_size_128_16,
00125     swb_size_128_16, swb_size_128_16, swb_size_128_8
00126 };
00127 
00129 static const uint8_t aac_chan_configs[6][5] = {
00130  {1, TYPE_SCE},                               // 1 channel  - single channel element
00131  {1, TYPE_CPE},                               // 2 channels - channel pair
00132  {2, TYPE_SCE, TYPE_CPE},                     // 3 channels - center + stereo
00133  {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},           // 4 channels - front center + stereo + back center
00134  {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},           // 5 channels - front center + stereo + back stereo
00135  {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
00136 };
00137 
00142 static void put_audio_specific_config(AVCodecContext *avctx)
00143 {
00144     PutBitContext pb;
00145     AACEncContext *s = avctx->priv_data;
00146 
00147     init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
00148     put_bits(&pb, 5, 2); //object type - AAC-LC
00149     put_bits(&pb, 4, s->samplerate_index); //sample rate index
00150     put_bits(&pb, 4, avctx->channels);
00151     //GASpecificConfig
00152     put_bits(&pb, 1, 0); //frame length - 1024 samples
00153     put_bits(&pb, 1, 0); //does not depend on core coder
00154     put_bits(&pb, 1, 0); //is not extension
00155 
00156     //Explicitly Mark SBR absent
00157     put_bits(&pb, 11, 0x2b7); //sync extension
00158     put_bits(&pb, 5,  AOT_SBR);
00159     put_bits(&pb, 1,  0);
00160     flush_put_bits(&pb);
00161 }
00162 
00163 static av_cold int aac_encode_init(AVCodecContext *avctx)
00164 {
00165     AACEncContext *s = avctx->priv_data;
00166     int i;
00167     const uint8_t *sizes[2];
00168     int lengths[2];
00169 
00170     avctx->frame_size = 1024;
00171 
00172     for (i = 0; i < 16; i++)
00173         if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
00174             break;
00175     if (i == 16) {
00176         av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
00177         return -1;
00178     }
00179     if (avctx->channels > AAC_MAX_CHANNELS) {
00180         av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
00181         return -1;
00182     }
00183     if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
00184         av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
00185         return -1;
00186     }
00187     if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
00188         av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
00189         return -1;
00190     }
00191     s->samplerate_index = i;
00192 
00193     dsputil_init(&s->dsp, avctx);
00194     ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
00195     ff_mdct_init(&s->mdct128,   8, 0, 1.0);
00196     // window init
00197     ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00198     ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00199     ff_init_ff_sine_windows(10);
00200     ff_init_ff_sine_windows(7);
00201 
00202     s->samples            = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
00203     s->cpe                = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
00204     avctx->extradata      = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
00205     avctx->extradata_size = 5;
00206     put_audio_specific_config(avctx);
00207 
00208     sizes[0]   = swb_size_1024[i];
00209     sizes[1]   = swb_size_128[i];
00210     lengths[0] = ff_aac_num_swb_1024[i];
00211     lengths[1] = ff_aac_num_swb_128[i];
00212     ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
00213     s->psypp = ff_psy_preprocess_init(avctx);
00214     s->coder = &ff_aac_coders[2];
00215 
00216     s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00217 
00218     ff_aac_tableinit();
00219 
00220     return 0;
00221 }
00222 
00223 static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
00224                                   SingleChannelElement *sce, short *audio)
00225 {
00226     int i, k;
00227     const int chans = avctx->channels;
00228     const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00229     const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
00230     const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
00231     float *output = sce->ret;
00232 
00233     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00234         memcpy(output, sce->saved, sizeof(float)*1024);
00235         if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
00236             memset(output, 0, sizeof(output[0]) * 448);
00237             for (i = 448; i < 576; i++)
00238                 output[i] = sce->saved[i] * pwindow[i - 448];
00239             for (i = 576; i < 704; i++)
00240                 output[i] = sce->saved[i];
00241         }
00242         if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
00243             for (i = 0; i < 1024; i++) {
00244                 output[i+1024]         = audio[i * chans] * lwindow[1024 - i - 1];
00245                 sce->saved[i] = audio[i * chans] * lwindow[i];
00246             }
00247         } else {
00248             for (i = 0; i < 448; i++)
00249                 output[i+1024]         = audio[i * chans];
00250             for (; i < 576; i++)
00251                 output[i+1024]         = audio[i * chans] * swindow[576 - i - 1];
00252             memset(output+1024+576, 0, sizeof(output[0]) * 448);
00253             for (i = 0; i < 1024; i++)
00254                 sce->saved[i] = audio[i * chans];
00255         }
00256         s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
00257     } else {
00258         for (k = 0; k < 1024; k += 128) {
00259             for (i = 448 + k; i < 448 + k + 256; i++)
00260                 output[i - 448 - k] = (i < 1024)
00261                                          ? sce->saved[i]
00262                                          : audio[(i-1024)*chans];
00263             s->dsp.vector_fmul        (output,     output, k ?  swindow : pwindow, 128);
00264             s->dsp.vector_fmul_reverse(output+128, output+128, swindow, 128);
00265             s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + k, output);
00266         }
00267         for (i = 0; i < 1024; i++)
00268             sce->saved[i] = audio[i * chans];
00269     }
00270 }
00271 
00276 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00277 {
00278     int w;
00279 
00280     put_bits(&s->pb, 1, 0);                // ics_reserved bit
00281     put_bits(&s->pb, 2, info->window_sequence[0]);
00282     put_bits(&s->pb, 1, info->use_kb_window[0]);
00283     if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00284         put_bits(&s->pb, 6, info->max_sfb);
00285         put_bits(&s->pb, 1, 0);            // no prediction
00286     } else {
00287         put_bits(&s->pb, 4, info->max_sfb);
00288         for (w = 1; w < 8; w++)
00289             put_bits(&s->pb, 1, !info->group_len[w]);
00290     }
00291 }
00292 
00297 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
00298 {
00299     int i, w;
00300 
00301     put_bits(pb, 2, cpe->ms_mode);
00302     if (cpe->ms_mode == 1)
00303         for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
00304             for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
00305                 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
00306 }
00307 
00311 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
00312 {
00313     int i, w, w2, g, ch;
00314     int start, maxsfb, cmaxsfb;
00315 
00316     for (ch = 0; ch < chans; ch++) {
00317         IndividualChannelStream *ics = &cpe->ch[ch].ics;
00318         start = 0;
00319         maxsfb = 0;
00320         cpe->ch[ch].pulse.num_pulse = 0;
00321         for (w = 0; w < ics->num_windows*16; w += 16) {
00322             for (g = 0; g < ics->num_swb; g++) {
00323                 //apply M/S
00324                 if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
00325                     for (i = 0; i < ics->swb_sizes[g]; i++) {
00326                         cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
00327                         cpe->ch[1].coeffs[start+i] =  cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
00328                     }
00329                 }
00330                 start += ics->swb_sizes[g];
00331             }
00332             for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
00333                 ;
00334             maxsfb = FFMAX(maxsfb, cmaxsfb);
00335         }
00336         ics->max_sfb = maxsfb;
00337 
00338         //adjust zero bands for window groups
00339         for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
00340             for (g = 0; g < ics->max_sfb; g++) {
00341                 i = 1;
00342                 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
00343                     if (!cpe->ch[ch].zeroes[w2*16 + g]) {
00344                         i = 0;
00345                         break;
00346                     }
00347                 }
00348                 cpe->ch[ch].zeroes[w*16 + g] = i;
00349             }
00350         }
00351     }
00352 
00353     if (chans > 1 && cpe->common_window) {
00354         IndividualChannelStream *ics0 = &cpe->ch[0].ics;
00355         IndividualChannelStream *ics1 = &cpe->ch[1].ics;
00356         int msc = 0;
00357         ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
00358         ics1->max_sfb = ics0->max_sfb;
00359         for (w = 0; w < ics0->num_windows*16; w += 16)
00360             for (i = 0; i < ics0->max_sfb; i++)
00361                 if (cpe->ms_mask[w+i])
00362                     msc++;
00363         if (msc == 0 || ics0->max_sfb == 0)
00364             cpe->ms_mode = 0;
00365         else
00366             cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
00367     }
00368 }
00369 
00373 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
00374 {
00375     int w;
00376 
00377     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00378         s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
00379 }
00380 
00384 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
00385                                  SingleChannelElement *sce)
00386 {
00387     int off = sce->sf_idx[0], diff;
00388     int i, w;
00389 
00390     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00391         for (i = 0; i < sce->ics.max_sfb; i++) {
00392             if (!sce->zeroes[w*16 + i]) {
00393                 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
00394                 if (diff < 0 || diff > 120)
00395                     av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
00396                 off = sce->sf_idx[w*16 + i];
00397                 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00398             }
00399         }
00400     }
00401 }
00402 
00406 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00407 {
00408     int i;
00409 
00410     put_bits(&s->pb, 1, !!pulse->num_pulse);
00411     if (!pulse->num_pulse)
00412         return;
00413 
00414     put_bits(&s->pb, 2, pulse->num_pulse - 1);
00415     put_bits(&s->pb, 6, pulse->start);
00416     for (i = 0; i < pulse->num_pulse; i++) {
00417         put_bits(&s->pb, 5, pulse->pos[i]);
00418         put_bits(&s->pb, 4, pulse->amp[i]);
00419     }
00420 }
00421 
00425 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00426 {
00427     int start, i, w, w2;
00428 
00429     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00430         start = 0;
00431         for (i = 0; i < sce->ics.max_sfb; i++) {
00432             if (sce->zeroes[w*16 + i]) {
00433                 start += sce->ics.swb_sizes[i];
00434                 continue;
00435             }
00436             for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00437                 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00438                                                    sce->ics.swb_sizes[i],
00439                                                    sce->sf_idx[w*16 + i],
00440                                                    sce->band_type[w*16 + i],
00441                                                    s->lambda);
00442             start += sce->ics.swb_sizes[i];
00443         }
00444     }
00445 }
00446 
00450 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00451                                      SingleChannelElement *sce,
00452                                      int common_window)
00453 {
00454     put_bits(&s->pb, 8, sce->sf_idx[0]);
00455     if (!common_window)
00456         put_ics_info(s, &sce->ics);
00457     encode_band_info(s, sce);
00458     encode_scale_factors(avctx, s, sce);
00459     encode_pulses(s, &sce->pulse);
00460     put_bits(&s->pb, 1, 0); //tns
00461     put_bits(&s->pb, 1, 0); //ssr
00462     encode_spectral_coeffs(s, sce);
00463     return 0;
00464 }
00465 
00469 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
00470                                const char *name)
00471 {
00472     int i, namelen, padbits;
00473 
00474     namelen = strlen(name) + 2;
00475     put_bits(&s->pb, 3, TYPE_FIL);
00476     put_bits(&s->pb, 4, FFMIN(namelen, 15));
00477     if (namelen >= 15)
00478         put_bits(&s->pb, 8, namelen - 16);
00479     put_bits(&s->pb, 4, 0); //extension type - filler
00480     padbits = 8 - (put_bits_count(&s->pb) & 7);
00481     align_put_bits(&s->pb);
00482     for (i = 0; i < namelen - 2; i++)
00483         put_bits(&s->pb, 8, name[i]);
00484     put_bits(&s->pb, 12 - padbits, 0);
00485 }
00486 
00487 static int aac_encode_frame(AVCodecContext *avctx,
00488                             uint8_t *frame, int buf_size, void *data)
00489 {
00490     AACEncContext *s = avctx->priv_data;
00491     int16_t *samples = s->samples, *samples2, *la;
00492     ChannelElement *cpe;
00493     int i, ch, w, g, chans, tag, start_ch;
00494     const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
00495     int chan_el_counter[4];
00496     FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
00497 
00498     if (s->last_frame)
00499         return 0;
00500     if (data) {
00501         if (!s->psypp) {
00502             memcpy(s->samples + 1024 * avctx->channels, data,
00503                    1024 * avctx->channels * sizeof(s->samples[0]));
00504         } else {
00505             start_ch = 0;
00506             samples2 = s->samples + 1024 * avctx->channels;
00507             for (i = 0; i < chan_map[0]; i++) {
00508                 tag = chan_map[i+1];
00509                 chans = tag == TYPE_CPE ? 2 : 1;
00510                 ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
00511                                   samples2 + start_ch, start_ch, chans);
00512                 start_ch += chans;
00513             }
00514         }
00515     }
00516     if (!avctx->frame_number) {
00517         memcpy(s->samples, s->samples + 1024 * avctx->channels,
00518                1024 * avctx->channels * sizeof(s->samples[0]));
00519         return 0;
00520     }
00521 
00522     start_ch = 0;
00523     for (i = 0; i < chan_map[0]; i++) {
00524         FFPsyWindowInfo* wi = windows + start_ch;
00525         tag      = chan_map[i+1];
00526         chans    = tag == TYPE_CPE ? 2 : 1;
00527         cpe      = &s->cpe[i];
00528         for (ch = 0; ch < chans; ch++) {
00529             IndividualChannelStream *ics = &cpe->ch[ch].ics;
00530             int cur_channel = start_ch + ch;
00531             samples2 = samples + cur_channel;
00532             la       = samples2 + (448+64) * avctx->channels;
00533             if (!data)
00534                 la = NULL;
00535             if (tag == TYPE_LFE) {
00536                 wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
00537                 wi[ch].window_shape   = 0;
00538                 wi[ch].num_windows    = 1;
00539                 wi[ch].grouping[0]    = 1;
00540             } else {
00541                 wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel,
00542                                               ics->window_sequence[0]);
00543             }
00544             ics->window_sequence[1] = ics->window_sequence[0];
00545             ics->window_sequence[0] = wi[ch].window_type[0];
00546             ics->use_kb_window[1]   = ics->use_kb_window[0];
00547             ics->use_kb_window[0]   = wi[ch].window_shape;
00548             ics->num_windows        = wi[ch].num_windows;
00549             ics->swb_sizes          = s->psy.bands    [ics->num_windows == 8];
00550             ics->num_swb            = tag == TYPE_LFE ? 12 : s->psy.num_bands[ics->num_windows == 8];
00551             for (w = 0; w < ics->num_windows; w++)
00552                 ics->group_len[w] = wi[ch].grouping[w];
00553 
00554             apply_window_and_mdct(avctx, s, &cpe->ch[ch], samples2);
00555         }
00556         start_ch += chans;
00557     }
00558     do {
00559         int frame_bits;
00560         init_put_bits(&s->pb, frame, buf_size*8);
00561         if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
00562             put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
00563         start_ch = 0;
00564         memset(chan_el_counter, 0, sizeof(chan_el_counter));
00565         for (i = 0; i < chan_map[0]; i++) {
00566             FFPsyWindowInfo* wi = windows + start_ch;
00567             tag      = chan_map[i+1];
00568             chans    = tag == TYPE_CPE ? 2 : 1;
00569             cpe      = &s->cpe[i];
00570             put_bits(&s->pb, 3, tag);
00571             put_bits(&s->pb, 4, chan_el_counter[tag]++);
00572             for (ch = 0; ch < chans; ch++) {
00573                 s->cur_channel = start_ch + ch;
00574                 s->psy.model->analyze(&s->psy, s->cur_channel, cpe->ch[ch].coeffs, &wi[ch]);
00575                 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
00576             }
00577             cpe->common_window = 0;
00578             if (chans > 1
00579                 && wi[0].window_type[0] == wi[1].window_type[0]
00580                 && wi[0].window_shape   == wi[1].window_shape) {
00581 
00582                 cpe->common_window = 1;
00583                 for (w = 0; w < wi[0].num_windows; w++) {
00584                     if (wi[0].grouping[w] != wi[1].grouping[w]) {
00585                         cpe->common_window = 0;
00586                         break;
00587                     }
00588                 }
00589             }
00590             s->cur_channel = start_ch;
00591             if (s->options.stereo_mode && cpe->common_window) {
00592                 if (s->options.stereo_mode > 0) {
00593                     IndividualChannelStream *ics = &cpe->ch[0].ics;
00594                     for (w = 0; w < ics->num_windows; w += ics->group_len[w])
00595                         for (g = 0;  g < ics->num_swb; g++)
00596                             cpe->ms_mask[w*16+g] = 1;
00597                 } else if (s->coder->search_for_ms) {
00598                     s->coder->search_for_ms(s, cpe, s->lambda);
00599                 }
00600             }
00601             adjust_frame_information(s, cpe, chans);
00602             if (chans == 2) {
00603                 put_bits(&s->pb, 1, cpe->common_window);
00604                 if (cpe->common_window) {
00605                     put_ics_info(s, &cpe->ch[0].ics);
00606                     encode_ms_info(&s->pb, cpe);
00607                 }
00608             }
00609             for (ch = 0; ch < chans; ch++) {
00610                 s->cur_channel = start_ch + ch;
00611                 encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
00612             }
00613             start_ch += chans;
00614         }
00615 
00616         frame_bits = put_bits_count(&s->pb);
00617         if (frame_bits <= 6144 * avctx->channels - 3) {
00618             s->psy.bitres.bits = frame_bits / avctx->channels;
00619             break;
00620         }
00621 
00622         s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
00623 
00624     } while (1);
00625 
00626     put_bits(&s->pb, 3, TYPE_END);
00627     flush_put_bits(&s->pb);
00628     avctx->frame_bits = put_bits_count(&s->pb);
00629 
00630     // rate control stuff
00631     if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
00632         float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
00633         s->lambda *= ratio;
00634         s->lambda = FFMIN(s->lambda, 65536.f);
00635     }
00636 
00637     if (!data)
00638         s->last_frame = 1;
00639     memcpy(s->samples, s->samples + 1024 * avctx->channels,
00640            1024 * avctx->channels * sizeof(s->samples[0]));
00641     return put_bits_count(&s->pb)>>3;
00642 }
00643 
00644 static av_cold int aac_encode_end(AVCodecContext *avctx)
00645 {
00646     AACEncContext *s = avctx->priv_data;
00647 
00648     ff_mdct_end(&s->mdct1024);
00649     ff_mdct_end(&s->mdct128);
00650     ff_psy_end(&s->psy);
00651     ff_psy_preprocess_end(s->psypp);
00652     av_freep(&s->samples);
00653     av_freep(&s->cpe);
00654     return 0;
00655 }
00656 
00657 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
00658 static const AVOption aacenc_options[] = {
00659     {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), FF_OPT_TYPE_INT, {.dbl = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
00660         {"auto",     "Selected by the Encoder", 0, FF_OPT_TYPE_CONST, {.dbl = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00661         {"ms_off",   "Disable Mid/Side coding", 0, FF_OPT_TYPE_CONST, {.dbl =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00662         {"ms_force", "Force Mid/Side for the whole frame if possible", 0, FF_OPT_TYPE_CONST, {.dbl =  1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00663     {NULL}
00664 };
00665 
00666 static const AVClass aacenc_class = {
00667     "AAC encoder",
00668     av_default_item_name,
00669     aacenc_options,
00670     LIBAVUTIL_VERSION_INT,
00671 };
00672 
00673 AVCodec ff_aac_encoder = {
00674     "aac",
00675     AVMEDIA_TYPE_AUDIO,
00676     CODEC_ID_AAC,
00677     sizeof(AACEncContext),
00678     aac_encode_init,
00679     aac_encode_frame,
00680     aac_encode_end,
00681     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
00682     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00683     .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
00684     .priv_class = &aacenc_class,
00685 };