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