00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028
00029
00030
00031
00032
00033 #include "avcodec.h"
00034 #include "put_bits.h"
00035 #include "dsputil.h"
00036 #include "mpeg4audio.h"
00037
00038 #include "aac.h"
00039 #include "aactab.h"
00040 #include "aacenc.h"
00041
00042 #include "psymodel.h"
00043
00044 static const uint8_t swb_size_1024_96[] = {
00045 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
00046 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
00047 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00048 };
00049
00050 static const uint8_t swb_size_1024_64[] = {
00051 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
00052 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
00053 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
00054 };
00055
00056 static const uint8_t swb_size_1024_48[] = {
00057 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00058 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00059 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
00060 96
00061 };
00062
00063 static const uint8_t swb_size_1024_32[] = {
00064 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00065 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00066 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
00067 };
00068
00069 static const uint8_t swb_size_1024_24[] = {
00070 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00071 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
00072 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
00073 };
00074
00075 static const uint8_t swb_size_1024_16[] = {
00076 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00077 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
00078 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
00079 };
00080
00081 static const uint8_t swb_size_1024_8[] = {
00082 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00083 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
00084 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
00085 };
00086
00087 static const uint8_t *swb_size_1024[] = {
00088 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
00089 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
00090 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
00091 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
00092 };
00093
00094 static const uint8_t swb_size_128_96[] = {
00095 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
00096 };
00097
00098 static const uint8_t swb_size_128_48[] = {
00099 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
00100 };
00101
00102 static const uint8_t swb_size_128_24[] = {
00103 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
00104 };
00105
00106 static const uint8_t swb_size_128_16[] = {
00107 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
00108 };
00109
00110 static const uint8_t swb_size_128_8[] = {
00111 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
00112 };
00113
00114 static const uint8_t *swb_size_128[] = {
00115
00116
00117 swb_size_128_96, swb_size_128_96, swb_size_128_96,
00118 swb_size_128_48, swb_size_128_48, swb_size_128_48,
00119 swb_size_128_24, swb_size_128_24, swb_size_128_16,
00120 swb_size_128_16, swb_size_128_16, swb_size_128_8
00121 };
00122
00124 static const uint8_t aac_chan_configs[6][5] = {
00125 {1, TYPE_SCE},
00126 {1, TYPE_CPE},
00127 {2, TYPE_SCE, TYPE_CPE},
00128 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},
00129 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},
00130 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE},
00131 };
00132
00137 static void put_audio_specific_config(AVCodecContext *avctx)
00138 {
00139 PutBitContext pb;
00140 AACEncContext *s = avctx->priv_data;
00141
00142 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
00143 put_bits(&pb, 5, 2);
00144 put_bits(&pb, 4, s->samplerate_index);
00145 put_bits(&pb, 4, avctx->channels);
00146
00147 put_bits(&pb, 1, 0);
00148 put_bits(&pb, 1, 0);
00149 put_bits(&pb, 1, 0);
00150 flush_put_bits(&pb);
00151 }
00152
00153 static av_cold int aac_encode_init(AVCodecContext *avctx)
00154 {
00155 AACEncContext *s = avctx->priv_data;
00156 int i;
00157 const uint8_t *sizes[2];
00158 int lengths[2];
00159
00160 avctx->frame_size = 1024;
00161
00162 for (i = 0; i < 16; i++)
00163 if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
00164 break;
00165 if (i == 16) {
00166 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
00167 return -1;
00168 }
00169 if (avctx->channels > 6) {
00170 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
00171 return -1;
00172 }
00173 if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
00174 av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
00175 return -1;
00176 }
00177 s->samplerate_index = i;
00178
00179 dsputil_init(&s->dsp, avctx);
00180 ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
00181 ff_mdct_init(&s->mdct128, 8, 0, 1.0);
00182
00183 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00184 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00185 ff_init_ff_sine_windows(10);
00186 ff_init_ff_sine_windows(7);
00187
00188 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
00189 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
00190 avctx->extradata = av_malloc(2);
00191 avctx->extradata_size = 2;
00192 put_audio_specific_config(avctx);
00193
00194 sizes[0] = swb_size_1024[i];
00195 sizes[1] = swb_size_128[i];
00196 lengths[0] = ff_aac_num_swb_1024[i];
00197 lengths[1] = ff_aac_num_swb_128[i];
00198 ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
00199 s->psypp = ff_psy_preprocess_init(avctx);
00200 s->coder = &ff_aac_coders[0];
00201
00202 s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00203 #if !CONFIG_HARDCODED_TABLES
00204 for (i = 0; i < 428; i++)
00205 ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
00206 #endif
00207
00208 if (avctx->channels > 5)
00209 av_log(avctx, AV_LOG_ERROR, "This encoder does not yet enforce the restrictions on LFEs. "
00210 "The output will most likely be an illegal bitstream.\n");
00211
00212 return 0;
00213 }
00214
00215 static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
00216 SingleChannelElement *sce, short *audio, int channel)
00217 {
00218 int i, j, k;
00219 const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00220 const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
00221 const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
00222
00223 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00224 memcpy(s->output, sce->saved, sizeof(float)*1024);
00225 if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
00226 memset(s->output, 0, sizeof(s->output[0]) * 448);
00227 for (i = 448; i < 576; i++)
00228 s->output[i] = sce->saved[i] * pwindow[i - 448];
00229 for (i = 576; i < 704; i++)
00230 s->output[i] = sce->saved[i];
00231 }
00232 if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
00233 j = channel;
00234 for (i = 0; i < 1024; i++, j += avctx->channels) {
00235 s->output[i+1024] = audio[j] * lwindow[1024 - i - 1];
00236 sce->saved[i] = audio[j] * lwindow[i];
00237 }
00238 } else {
00239 j = channel;
00240 for (i = 0; i < 448; i++, j += avctx->channels)
00241 s->output[i+1024] = audio[j];
00242 for (i = 448; i < 576; i++, j += avctx->channels)
00243 s->output[i+1024] = audio[j] * swindow[576 - i - 1];
00244 memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448);
00245 j = channel;
00246 for (i = 0; i < 1024; i++, j += avctx->channels)
00247 sce->saved[i] = audio[j];
00248 }
00249 ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output);
00250 } else {
00251 j = channel;
00252 for (k = 0; k < 1024; k += 128) {
00253 for (i = 448 + k; i < 448 + k + 256; i++)
00254 s->output[i - 448 - k] = (i < 1024)
00255 ? sce->saved[i]
00256 : audio[channel + (i-1024)*avctx->channels];
00257 s->dsp.vector_fmul (s->output, k ? swindow : pwindow, 128);
00258 s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128);
00259 ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output);
00260 }
00261 j = channel;
00262 for (i = 0; i < 1024; i++, j += avctx->channels)
00263 sce->saved[i] = audio[j];
00264 }
00265 }
00266
00271 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00272 {
00273 int w;
00274
00275 put_bits(&s->pb, 1, 0);
00276 put_bits(&s->pb, 2, info->window_sequence[0]);
00277 put_bits(&s->pb, 1, info->use_kb_window[0]);
00278 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00279 put_bits(&s->pb, 6, info->max_sfb);
00280 put_bits(&s->pb, 1, 0);
00281 } else {
00282 put_bits(&s->pb, 4, info->max_sfb);
00283 for (w = 1; w < 8; w++)
00284 put_bits(&s->pb, 1, !info->group_len[w]);
00285 }
00286 }
00287
00292 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
00293 {
00294 int i, w;
00295
00296 put_bits(pb, 2, cpe->ms_mode);
00297 if (cpe->ms_mode == 1)
00298 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
00299 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
00300 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
00301 }
00302
00306 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
00307 {
00308 int i, w, w2, g, ch;
00309 int start, sum, maxsfb, cmaxsfb;
00310
00311 for (ch = 0; ch < chans; ch++) {
00312 IndividualChannelStream *ics = &cpe->ch[ch].ics;
00313 start = 0;
00314 maxsfb = 0;
00315 cpe->ch[ch].pulse.num_pulse = 0;
00316 for (w = 0; w < ics->num_windows*16; w += 16) {
00317 for (g = 0; g < ics->num_swb; g++) {
00318 sum = 0;
00319
00320 if (!ch && cpe->ms_mask[w + g]) {
00321 for (i = 0; i < ics->swb_sizes[g]; i++) {
00322 cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
00323 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
00324 }
00325 }
00326 start += ics->swb_sizes[g];
00327 }
00328 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
00329 ;
00330 maxsfb = FFMAX(maxsfb, cmaxsfb);
00331 }
00332 ics->max_sfb = maxsfb;
00333
00334
00335 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
00336 for (g = 0; g < ics->max_sfb; g++) {
00337 i = 1;
00338 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
00339 if (!cpe->ch[ch].zeroes[w2*16 + g]) {
00340 i = 0;
00341 break;
00342 }
00343 }
00344 cpe->ch[ch].zeroes[w*16 + g] = i;
00345 }
00346 }
00347 }
00348
00349 if (chans > 1 && cpe->common_window) {
00350 IndividualChannelStream *ics0 = &cpe->ch[0].ics;
00351 IndividualChannelStream *ics1 = &cpe->ch[1].ics;
00352 int msc = 0;
00353 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
00354 ics1->max_sfb = ics0->max_sfb;
00355 for (w = 0; w < ics0->num_windows*16; w += 16)
00356 for (i = 0; i < ics0->max_sfb; i++)
00357 if (cpe->ms_mask[w+i])
00358 msc++;
00359 if (msc == 0 || ics0->max_sfb == 0)
00360 cpe->ms_mode = 0;
00361 else
00362 cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
00363 }
00364 }
00365
00369 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
00370 {
00371 int w;
00372
00373 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00374 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
00375 }
00376
00380 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
00381 SingleChannelElement *sce)
00382 {
00383 int off = sce->sf_idx[0], diff;
00384 int i, w;
00385
00386 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00387 for (i = 0; i < sce->ics.max_sfb; i++) {
00388 if (!sce->zeroes[w*16 + i]) {
00389 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
00390 if (diff < 0 || diff > 120)
00391 av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
00392 off = sce->sf_idx[w*16 + i];
00393 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00394 }
00395 }
00396 }
00397 }
00398
00402 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00403 {
00404 int i;
00405
00406 put_bits(&s->pb, 1, !!pulse->num_pulse);
00407 if (!pulse->num_pulse)
00408 return;
00409
00410 put_bits(&s->pb, 2, pulse->num_pulse - 1);
00411 put_bits(&s->pb, 6, pulse->start);
00412 for (i = 0; i < pulse->num_pulse; i++) {
00413 put_bits(&s->pb, 5, pulse->pos[i]);
00414 put_bits(&s->pb, 4, pulse->amp[i]);
00415 }
00416 }
00417
00421 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00422 {
00423 int start, i, w, w2;
00424
00425 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00426 start = 0;
00427 for (i = 0; i < sce->ics.max_sfb; i++) {
00428 if (sce->zeroes[w*16 + i]) {
00429 start += sce->ics.swb_sizes[i];
00430 continue;
00431 }
00432 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00433 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00434 sce->ics.swb_sizes[i],
00435 sce->sf_idx[w*16 + i],
00436 sce->band_type[w*16 + i],
00437 s->lambda);
00438 start += sce->ics.swb_sizes[i];
00439 }
00440 }
00441 }
00442
00446 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00447 SingleChannelElement *sce,
00448 int common_window)
00449 {
00450 put_bits(&s->pb, 8, sce->sf_idx[0]);
00451 if (!common_window)
00452 put_ics_info(s, &sce->ics);
00453 encode_band_info(s, sce);
00454 encode_scale_factors(avctx, s, sce);
00455 encode_pulses(s, &sce->pulse);
00456 put_bits(&s->pb, 1, 0);
00457 put_bits(&s->pb, 1, 0);
00458 encode_spectral_coeffs(s, sce);
00459 return 0;
00460 }
00461
00465 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
00466 const char *name)
00467 {
00468 int i, namelen, padbits;
00469
00470 namelen = strlen(name) + 2;
00471 put_bits(&s->pb, 3, TYPE_FIL);
00472 put_bits(&s->pb, 4, FFMIN(namelen, 15));
00473 if (namelen >= 15)
00474 put_bits(&s->pb, 8, namelen - 16);
00475 put_bits(&s->pb, 4, 0);
00476 padbits = 8 - (put_bits_count(&s->pb) & 7);
00477 align_put_bits(&s->pb);
00478 for (i = 0; i < namelen - 2; i++)
00479 put_bits(&s->pb, 8, name[i]);
00480 put_bits(&s->pb, 12 - padbits, 0);
00481 }
00482
00483 static int aac_encode_frame(AVCodecContext *avctx,
00484 uint8_t *frame, int buf_size, void *data)
00485 {
00486 AACEncContext *s = avctx->priv_data;
00487 int16_t *samples = s->samples, *samples2, *la;
00488 ChannelElement *cpe;
00489 int i, j, chans, tag, start_ch;
00490 const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
00491 int chan_el_counter[4];
00492 FFPsyWindowInfo windows[avctx->channels];
00493
00494 if (s->last_frame)
00495 return 0;
00496 if (data) {
00497 if (!s->psypp) {
00498 memcpy(s->samples + 1024 * avctx->channels, data,
00499 1024 * avctx->channels * sizeof(s->samples[0]));
00500 } else {
00501 start_ch = 0;
00502 samples2 = s->samples + 1024 * avctx->channels;
00503 for (i = 0; i < chan_map[0]; i++) {
00504 tag = chan_map[i+1];
00505 chans = tag == TYPE_CPE ? 2 : 1;
00506 ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
00507 samples2 + start_ch, start_ch, chans);
00508 start_ch += chans;
00509 }
00510 }
00511 }
00512 if (!avctx->frame_number) {
00513 memcpy(s->samples, s->samples + 1024 * avctx->channels,
00514 1024 * avctx->channels * sizeof(s->samples[0]));
00515 return 0;
00516 }
00517
00518 start_ch = 0;
00519 for (i = 0; i < chan_map[0]; i++) {
00520 FFPsyWindowInfo* wi = windows + start_ch;
00521 tag = chan_map[i+1];
00522 chans = tag == TYPE_CPE ? 2 : 1;
00523 cpe = &s->cpe[i];
00524 samples2 = samples + start_ch;
00525 la = samples2 + 1024 * avctx->channels + start_ch;
00526 if (!data)
00527 la = NULL;
00528 for (j = 0; j < chans; j++) {
00529 IndividualChannelStream *ics = &cpe->ch[j].ics;
00530 int k;
00531 wi[j] = ff_psy_suggest_window(&s->psy, samples2, la, start_ch + j, ics->window_sequence[0]);
00532 ics->window_sequence[1] = ics->window_sequence[0];
00533 ics->window_sequence[0] = wi[j].window_type[0];
00534 ics->use_kb_window[1] = ics->use_kb_window[0];
00535 ics->use_kb_window[0] = wi[j].window_shape;
00536 ics->num_windows = wi[j].num_windows;
00537 ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
00538 ics->num_swb = s->psy.num_bands[ics->num_windows == 8];
00539 for (k = 0; k < ics->num_windows; k++)
00540 ics->group_len[k] = wi[j].grouping[k];
00541
00542 s->cur_channel = start_ch + j;
00543 apply_window_and_mdct(avctx, s, &cpe->ch[j], samples2, j);
00544 }
00545 start_ch += chans;
00546 }
00547 do {
00548 int frame_bits;
00549 init_put_bits(&s->pb, frame, buf_size*8);
00550 if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
00551 put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
00552 start_ch = 0;
00553 memset(chan_el_counter, 0, sizeof(chan_el_counter));
00554 for (i = 0; i < chan_map[0]; i++) {
00555 FFPsyWindowInfo* wi = windows + start_ch;
00556 tag = chan_map[i+1];
00557 chans = tag == TYPE_CPE ? 2 : 1;
00558 cpe = &s->cpe[i];
00559 for (j = 0; j < chans; j++) {
00560 s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda);
00561 }
00562 cpe->common_window = 0;
00563 if (chans > 1
00564 && wi[0].window_type[0] == wi[1].window_type[0]
00565 && wi[0].window_shape == wi[1].window_shape) {
00566
00567 cpe->common_window = 1;
00568 for (j = 0; j < wi[0].num_windows; j++) {
00569 if (wi[0].grouping[j] != wi[1].grouping[j]) {
00570 cpe->common_window = 0;
00571 break;
00572 }
00573 }
00574 }
00575 if (cpe->common_window && s->coder->search_for_ms)
00576 s->coder->search_for_ms(s, cpe, s->lambda);
00577 adjust_frame_information(s, cpe, chans);
00578 put_bits(&s->pb, 3, tag);
00579 put_bits(&s->pb, 4, chan_el_counter[tag]++);
00580 if (chans == 2) {
00581 put_bits(&s->pb, 1, cpe->common_window);
00582 if (cpe->common_window) {
00583 put_ics_info(s, &cpe->ch[0].ics);
00584 encode_ms_info(&s->pb, cpe);
00585 }
00586 }
00587 for (j = 0; j < chans; j++) {
00588 s->cur_channel = start_ch + j;
00589 ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
00590 encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
00591 }
00592 start_ch += chans;
00593 }
00594
00595 frame_bits = put_bits_count(&s->pb);
00596 if (frame_bits <= 6144 * avctx->channels - 3)
00597 break;
00598
00599 s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
00600
00601 } while (1);
00602
00603 put_bits(&s->pb, 3, TYPE_END);
00604 flush_put_bits(&s->pb);
00605 avctx->frame_bits = put_bits_count(&s->pb);
00606
00607
00608 if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
00609 float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
00610 s->lambda *= ratio;
00611 s->lambda = FFMIN(s->lambda, 65536.f);
00612 }
00613
00614 if (!data)
00615 s->last_frame = 1;
00616 memcpy(s->samples, s->samples + 1024 * avctx->channels,
00617 1024 * avctx->channels * sizeof(s->samples[0]));
00618 return put_bits_count(&s->pb)>>3;
00619 }
00620
00621 static av_cold int aac_encode_end(AVCodecContext *avctx)
00622 {
00623 AACEncContext *s = avctx->priv_data;
00624
00625 ff_mdct_end(&s->mdct1024);
00626 ff_mdct_end(&s->mdct128);
00627 ff_psy_end(&s->psy);
00628 ff_psy_preprocess_end(s->psypp);
00629 av_freep(&s->samples);
00630 av_freep(&s->cpe);
00631 return 0;
00632 }
00633
00634 AVCodec aac_encoder = {
00635 "aac",
00636 AVMEDIA_TYPE_AUDIO,
00637 CODEC_ID_AAC,
00638 sizeof(AACEncContext),
00639 aac_encode_init,
00640 aac_encode_frame,
00641 aac_encode_end,
00642 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
00643 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00644 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
00645 };