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

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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
00138 static const uint8_t channel_maps[][AAC_MAX_CHANNELS] = {
00139     { 0 },
00140     { 0, 1 },
00141     { 2, 0, 1 },
00142     { 2, 0, 1, 3 },
00143     { 2, 0, 1, 3, 4 },
00144     { 2, 0, 1, 4, 5, 3 },
00145 };
00146 
00151 static void put_audio_specific_config(AVCodecContext *avctx)
00152 {
00153     PutBitContext pb;
00154     AACEncContext *s = avctx->priv_data;
00155 
00156     init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
00157     put_bits(&pb, 5, 2); //object type - AAC-LC
00158     put_bits(&pb, 4, s->samplerate_index); //sample rate index
00159     put_bits(&pb, 4, avctx->channels);
00160     //GASpecificConfig
00161     put_bits(&pb, 1, 0); //frame length - 1024 samples
00162     put_bits(&pb, 1, 0); //does not depend on core coder
00163     put_bits(&pb, 1, 0); //is not extension
00164 
00165     //Explicitly Mark SBR absent
00166     put_bits(&pb, 11, 0x2b7); //sync extension
00167     put_bits(&pb, 5,  AOT_SBR);
00168     put_bits(&pb, 1,  0);
00169     flush_put_bits(&pb);
00170 }
00171 
00172 static av_cold int aac_encode_init(AVCodecContext *avctx)
00173 {
00174     AACEncContext *s = avctx->priv_data;
00175     int i;
00176     const uint8_t *sizes[2];
00177     int lengths[2];
00178 
00179     avctx->frame_size = 1024;
00180 
00181     for (i = 0; i < 16; i++)
00182         if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
00183             break;
00184     if (i == 16) {
00185         av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
00186         return -1;
00187     }
00188     if (avctx->channels > AAC_MAX_CHANNELS) {
00189         av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
00190         return -1;
00191     }
00192     if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
00193         av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
00194         return -1;
00195     }
00196     if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
00197         av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
00198         return -1;
00199     }
00200     s->samplerate_index = i;
00201 
00202     dsputil_init(&s->dsp, avctx);
00203     ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
00204     ff_mdct_init(&s->mdct128,   8, 0, 1.0);
00205     // window init
00206     ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00207     ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00208     ff_init_ff_sine_windows(10);
00209     ff_init_ff_sine_windows(7);
00210 
00211     s->samples            = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
00212     s->cpe                = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
00213     avctx->extradata      = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
00214     avctx->extradata_size = 5;
00215     put_audio_specific_config(avctx);
00216 
00217     sizes[0]   = swb_size_1024[i];
00218     sizes[1]   = swb_size_128[i];
00219     lengths[0] = ff_aac_num_swb_1024[i];
00220     lengths[1] = ff_aac_num_swb_128[i];
00221     ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
00222     s->psypp = ff_psy_preprocess_init(avctx);
00223     s->coder = &ff_aac_coders[2];
00224 
00225     s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00226 
00227     ff_aac_tableinit();
00228 
00229     return 0;
00230 }
00231 
00232 static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
00233                                   SingleChannelElement *sce, short *audio)
00234 {
00235     int i, k;
00236     const int chans = avctx->channels;
00237     const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00238     const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
00239     const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
00240     float *output = sce->ret;
00241 
00242     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00243         memcpy(output, sce->saved, sizeof(float)*1024);
00244         if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
00245             memset(output, 0, sizeof(output[0]) * 448);
00246             for (i = 448; i < 576; i++)
00247                 output[i] = sce->saved[i] * pwindow[i - 448];
00248             for (i = 576; i < 704; i++)
00249                 output[i] = sce->saved[i];
00250         }
00251         if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
00252             for (i = 0; i < 1024; i++) {
00253                 output[i+1024]         = audio[i * chans] * lwindow[1024 - i - 1];
00254                 sce->saved[i] = audio[i * chans] * lwindow[i];
00255             }
00256         } else {
00257             for (i = 0; i < 448; i++)
00258                 output[i+1024]         = audio[i * chans];
00259             for (; i < 576; i++)
00260                 output[i+1024]         = audio[i * chans] * swindow[576 - i - 1];
00261             memset(output+1024+576, 0, sizeof(output[0]) * 448);
00262             for (i = 0; i < 1024; i++)
00263                 sce->saved[i] = audio[i * chans];
00264         }
00265         s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
00266     } else {
00267         for (k = 0; k < 1024; k += 128) {
00268             for (i = 448 + k; i < 448 + k + 256; i++)
00269                 output[i - 448 - k] = (i < 1024)
00270                                          ? sce->saved[i]
00271                                          : audio[(i-1024)*chans];
00272             s->dsp.vector_fmul        (output,     output, k ?  swindow : pwindow, 128);
00273             s->dsp.vector_fmul_reverse(output+128, output+128, swindow, 128);
00274             s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + k, output);
00275         }
00276         for (i = 0; i < 1024; i++)
00277             sce->saved[i] = audio[i * chans];
00278     }
00279 }
00280 
00285 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00286 {
00287     int w;
00288 
00289     put_bits(&s->pb, 1, 0);                // ics_reserved bit
00290     put_bits(&s->pb, 2, info->window_sequence[0]);
00291     put_bits(&s->pb, 1, info->use_kb_window[0]);
00292     if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00293         put_bits(&s->pb, 6, info->max_sfb);
00294         put_bits(&s->pb, 1, 0);            // no prediction
00295     } else {
00296         put_bits(&s->pb, 4, info->max_sfb);
00297         for (w = 1; w < 8; w++)
00298             put_bits(&s->pb, 1, !info->group_len[w]);
00299     }
00300 }
00301 
00306 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
00307 {
00308     int i, w;
00309 
00310     put_bits(pb, 2, cpe->ms_mode);
00311     if (cpe->ms_mode == 1)
00312         for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
00313             for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
00314                 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
00315 }
00316 
00320 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
00321 {
00322     int i, w, w2, g, ch;
00323     int start, maxsfb, cmaxsfb;
00324 
00325     for (ch = 0; ch < chans; ch++) {
00326         IndividualChannelStream *ics = &cpe->ch[ch].ics;
00327         start = 0;
00328         maxsfb = 0;
00329         cpe->ch[ch].pulse.num_pulse = 0;
00330         for (w = 0; w < ics->num_windows*16; w += 16) {
00331             for (g = 0; g < ics->num_swb; g++) {
00332                 //apply M/S
00333                 if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
00334                     for (i = 0; i < ics->swb_sizes[g]; i++) {
00335                         cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
00336                         cpe->ch[1].coeffs[start+i] =  cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
00337                     }
00338                 }
00339                 start += ics->swb_sizes[g];
00340             }
00341             for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
00342                 ;
00343             maxsfb = FFMAX(maxsfb, cmaxsfb);
00344         }
00345         ics->max_sfb = maxsfb;
00346 
00347         //adjust zero bands for window groups
00348         for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
00349             for (g = 0; g < ics->max_sfb; g++) {
00350                 i = 1;
00351                 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
00352                     if (!cpe->ch[ch].zeroes[w2*16 + g]) {
00353                         i = 0;
00354                         break;
00355                     }
00356                 }
00357                 cpe->ch[ch].zeroes[w*16 + g] = i;
00358             }
00359         }
00360     }
00361 
00362     if (chans > 1 && cpe->common_window) {
00363         IndividualChannelStream *ics0 = &cpe->ch[0].ics;
00364         IndividualChannelStream *ics1 = &cpe->ch[1].ics;
00365         int msc = 0;
00366         ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
00367         ics1->max_sfb = ics0->max_sfb;
00368         for (w = 0; w < ics0->num_windows*16; w += 16)
00369             for (i = 0; i < ics0->max_sfb; i++)
00370                 if (cpe->ms_mask[w+i])
00371                     msc++;
00372         if (msc == 0 || ics0->max_sfb == 0)
00373             cpe->ms_mode = 0;
00374         else
00375             cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
00376     }
00377 }
00378 
00382 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
00383 {
00384     int w;
00385 
00386     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00387         s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
00388 }
00389 
00393 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
00394                                  SingleChannelElement *sce)
00395 {
00396     int off = sce->sf_idx[0], diff;
00397     int i, w;
00398 
00399     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00400         for (i = 0; i < sce->ics.max_sfb; i++) {
00401             if (!sce->zeroes[w*16 + i]) {
00402                 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
00403                 if (diff < 0 || diff > 120)
00404                     av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
00405                 off = sce->sf_idx[w*16 + i];
00406                 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00407             }
00408         }
00409     }
00410 }
00411 
00415 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00416 {
00417     int i;
00418 
00419     put_bits(&s->pb, 1, !!pulse->num_pulse);
00420     if (!pulse->num_pulse)
00421         return;
00422 
00423     put_bits(&s->pb, 2, pulse->num_pulse - 1);
00424     put_bits(&s->pb, 6, pulse->start);
00425     for (i = 0; i < pulse->num_pulse; i++) {
00426         put_bits(&s->pb, 5, pulse->pos[i]);
00427         put_bits(&s->pb, 4, pulse->amp[i]);
00428     }
00429 }
00430 
00434 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00435 {
00436     int start, i, w, w2;
00437 
00438     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00439         start = 0;
00440         for (i = 0; i < sce->ics.max_sfb; i++) {
00441             if (sce->zeroes[w*16 + i]) {
00442                 start += sce->ics.swb_sizes[i];
00443                 continue;
00444             }
00445             for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00446                 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00447                                                    sce->ics.swb_sizes[i],
00448                                                    sce->sf_idx[w*16 + i],
00449                                                    sce->band_type[w*16 + i],
00450                                                    s->lambda);
00451             start += sce->ics.swb_sizes[i];
00452         }
00453     }
00454 }
00455 
00459 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00460                                      SingleChannelElement *sce,
00461                                      int common_window)
00462 {
00463     put_bits(&s->pb, 8, sce->sf_idx[0]);
00464     if (!common_window)
00465         put_ics_info(s, &sce->ics);
00466     encode_band_info(s, sce);
00467     encode_scale_factors(avctx, s, sce);
00468     encode_pulses(s, &sce->pulse);
00469     put_bits(&s->pb, 1, 0); //tns
00470     put_bits(&s->pb, 1, 0); //ssr
00471     encode_spectral_coeffs(s, sce);
00472     return 0;
00473 }
00474 
00478 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
00479                                const char *name)
00480 {
00481     int i, namelen, padbits;
00482 
00483     namelen = strlen(name) + 2;
00484     put_bits(&s->pb, 3, TYPE_FIL);
00485     put_bits(&s->pb, 4, FFMIN(namelen, 15));
00486     if (namelen >= 15)
00487         put_bits(&s->pb, 8, namelen - 16);
00488     put_bits(&s->pb, 4, 0); //extension type - filler
00489     padbits = 8 - (put_bits_count(&s->pb) & 7);
00490     align_put_bits(&s->pb);
00491     for (i = 0; i < namelen - 2; i++)
00492         put_bits(&s->pb, 8, name[i]);
00493     put_bits(&s->pb, 12 - padbits, 0);
00494 }
00495 
00496 static int aac_encode_frame(AVCodecContext *avctx,
00497                             uint8_t *frame, int buf_size, void *data)
00498 {
00499     AACEncContext *s = avctx->priv_data;
00500     int16_t *samples = s->samples, *samples2, *la;
00501     ChannelElement *cpe;
00502     int i, ch, w, g, chans, tag, start_ch;
00503     const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
00504     int chan_el_counter[4];
00505     FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
00506 
00507     if (s->last_frame)
00508         return 0;
00509     if (data) {
00510         if (!s->psypp) {
00511             if (avctx->channels <= 2) {
00512                 memcpy(s->samples + 1024 * avctx->channels, data,
00513                        1024 * avctx->channels * sizeof(s->samples[0]));
00514             } else {
00515                 for (i = 0; i < 1024; i++)
00516                     for (ch = 0; ch < avctx->channels; ch++)
00517                         s->samples[(i + 1024) * avctx->channels + ch] =
00518                             ((int16_t*)data)[i * avctx->channels +
00519                                              channel_maps[avctx->channels-1][ch]];
00520             }
00521         } else {
00522             start_ch = 0;
00523             samples2 = s->samples + 1024 * avctx->channels;
00524             for (i = 0; i < chan_map[0]; i++) {
00525                 tag = chan_map[i+1];
00526                 chans = tag == TYPE_CPE ? 2 : 1;
00527                 ff_psy_preprocess(s->psypp,
00528                                   (uint16_t*)data + channel_maps[avctx->channels-1][start_ch],
00529                                   samples2 + start_ch, start_ch, chans);
00530                 start_ch += chans;
00531             }
00532         }
00533     }
00534     if (!avctx->frame_number) {
00535         memcpy(s->samples, s->samples + 1024 * avctx->channels,
00536                1024 * avctx->channels * sizeof(s->samples[0]));
00537         return 0;
00538     }
00539 
00540     start_ch = 0;
00541     for (i = 0; i < chan_map[0]; i++) {
00542         FFPsyWindowInfo* wi = windows + start_ch;
00543         tag      = chan_map[i+1];
00544         chans    = tag == TYPE_CPE ? 2 : 1;
00545         cpe      = &s->cpe[i];
00546         for (ch = 0; ch < chans; ch++) {
00547             IndividualChannelStream *ics = &cpe->ch[ch].ics;
00548             int cur_channel = start_ch + ch;
00549             samples2 = samples + cur_channel;
00550             la       = samples2 + (448+64) * avctx->channels;
00551             if (!data)
00552                 la = NULL;
00553             if (tag == TYPE_LFE) {
00554                 wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
00555                 wi[ch].window_shape   = 0;
00556                 wi[ch].num_windows    = 1;
00557                 wi[ch].grouping[0]    = 1;
00558             } else {
00559                 wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel,
00560                                               ics->window_sequence[0]);
00561             }
00562             ics->window_sequence[1] = ics->window_sequence[0];
00563             ics->window_sequence[0] = wi[ch].window_type[0];
00564             ics->use_kb_window[1]   = ics->use_kb_window[0];
00565             ics->use_kb_window[0]   = wi[ch].window_shape;
00566             ics->num_windows        = wi[ch].num_windows;
00567             ics->swb_sizes          = s->psy.bands    [ics->num_windows == 8];
00568             ics->num_swb            = tag == TYPE_LFE ? 12 : s->psy.num_bands[ics->num_windows == 8];
00569             for (w = 0; w < ics->num_windows; w++)
00570                 ics->group_len[w] = wi[ch].grouping[w];
00571 
00572             apply_window_and_mdct(avctx, s, &cpe->ch[ch], samples2);
00573         }
00574         start_ch += chans;
00575     }
00576     do {
00577         int frame_bits;
00578         init_put_bits(&s->pb, frame, buf_size*8);
00579         if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
00580             put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
00581         start_ch = 0;
00582         memset(chan_el_counter, 0, sizeof(chan_el_counter));
00583         for (i = 0; i < chan_map[0]; i++) {
00584             FFPsyWindowInfo* wi = windows + start_ch;
00585             tag      = chan_map[i+1];
00586             chans    = tag == TYPE_CPE ? 2 : 1;
00587             cpe      = &s->cpe[i];
00588             put_bits(&s->pb, 3, tag);
00589             put_bits(&s->pb, 4, chan_el_counter[tag]++);
00590             for (ch = 0; ch < chans; ch++) {
00591                 s->cur_channel = start_ch + ch;
00592                 s->psy.model->analyze(&s->psy, s->cur_channel, cpe->ch[ch].coeffs, &wi[ch]);
00593                 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
00594             }
00595             cpe->common_window = 0;
00596             if (chans > 1
00597                 && wi[0].window_type[0] == wi[1].window_type[0]
00598                 && wi[0].window_shape   == wi[1].window_shape) {
00599 
00600                 cpe->common_window = 1;
00601                 for (w = 0; w < wi[0].num_windows; w++) {
00602                     if (wi[0].grouping[w] != wi[1].grouping[w]) {
00603                         cpe->common_window = 0;
00604                         break;
00605                     }
00606                 }
00607             }
00608             s->cur_channel = start_ch;
00609             if (s->options.stereo_mode && cpe->common_window) {
00610                 if (s->options.stereo_mode > 0) {
00611                     IndividualChannelStream *ics = &cpe->ch[0].ics;
00612                     for (w = 0; w < ics->num_windows; w += ics->group_len[w])
00613                         for (g = 0;  g < ics->num_swb; g++)
00614                             cpe->ms_mask[w*16+g] = 1;
00615                 } else if (s->coder->search_for_ms) {
00616                     s->coder->search_for_ms(s, cpe, s->lambda);
00617                 }
00618             }
00619             adjust_frame_information(s, cpe, chans);
00620             if (chans == 2) {
00621                 put_bits(&s->pb, 1, cpe->common_window);
00622                 if (cpe->common_window) {
00623                     put_ics_info(s, &cpe->ch[0].ics);
00624                     encode_ms_info(&s->pb, cpe);
00625                 }
00626             }
00627             for (ch = 0; ch < chans; ch++) {
00628                 s->cur_channel = start_ch + ch;
00629                 encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
00630             }
00631             start_ch += chans;
00632         }
00633 
00634         frame_bits = put_bits_count(&s->pb);
00635         if (frame_bits <= 6144 * avctx->channels - 3) {
00636             s->psy.bitres.bits = frame_bits / avctx->channels;
00637             break;
00638         }
00639 
00640         s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
00641 
00642     } while (1);
00643 
00644     put_bits(&s->pb, 3, TYPE_END);
00645     flush_put_bits(&s->pb);
00646     avctx->frame_bits = put_bits_count(&s->pb);
00647 
00648     // rate control stuff
00649     if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
00650         float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
00651         s->lambda *= ratio;
00652         s->lambda = FFMIN(s->lambda, 65536.f);
00653     }
00654 
00655     if (!data)
00656         s->last_frame = 1;
00657     memcpy(s->samples, s->samples + 1024 * avctx->channels,
00658            1024 * avctx->channels * sizeof(s->samples[0]));
00659     return put_bits_count(&s->pb)>>3;
00660 }
00661 
00662 static av_cold int aac_encode_end(AVCodecContext *avctx)
00663 {
00664     AACEncContext *s = avctx->priv_data;
00665 
00666     ff_mdct_end(&s->mdct1024);
00667     ff_mdct_end(&s->mdct128);
00668     ff_psy_end(&s->psy);
00669     ff_psy_preprocess_end(s->psypp);
00670     av_freep(&s->samples);
00671     av_freep(&s->cpe);
00672     return 0;
00673 }
00674 
00675 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
00676 static const AVOption aacenc_options[] = {
00677     {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), FF_OPT_TYPE_INT, {.dbl = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
00678         {"auto",     "Selected by the Encoder", 0, FF_OPT_TYPE_CONST, {.dbl = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00679         {"ms_off",   "Disable Mid/Side coding", 0, FF_OPT_TYPE_CONST, {.dbl =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00680         {"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"},
00681     {NULL}
00682 };
00683 
00684 static const AVClass aacenc_class = {
00685     "AAC encoder",
00686     av_default_item_name,
00687     aacenc_options,
00688     LIBAVUTIL_VERSION_INT,
00689 };
00690 
00691 AVCodec ff_aac_encoder = {
00692     "aac",
00693     AVMEDIA_TYPE_AUDIO,
00694     CODEC_ID_AAC,
00695     sizeof(AACEncContext),
00696     aac_encode_init,
00697     aac_encode_frame,
00698     aac_encode_end,
00699     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
00700     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00701     .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
00702     .priv_class = &aacenc_class,
00703 };

Generated on Fri Feb 22 2013 07:24:25 for FFmpeg by  doxygen 1.7.1