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 #include "kbdwin.h"
00038 #include "sinewin.h"
00039
00040 #include "aac.h"
00041 #include "aactab.h"
00042 #include "aacenc.h"
00043
00044 #include "psymodel.h"
00045
00046 #define AAC_MAX_CHANNELS 6
00047
00048 static const uint8_t swb_size_1024_96[] = {
00049 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
00050 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
00051 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00052 };
00053
00054 static const uint8_t swb_size_1024_64[] = {
00055 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
00056 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
00057 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
00058 };
00059
00060 static const uint8_t swb_size_1024_48[] = {
00061 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00062 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00063 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
00064 96
00065 };
00066
00067 static const uint8_t swb_size_1024_32[] = {
00068 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00069 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00070 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
00071 };
00072
00073 static const uint8_t swb_size_1024_24[] = {
00074 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00075 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
00076 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
00077 };
00078
00079 static const uint8_t swb_size_1024_16[] = {
00080 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00081 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
00082 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
00083 };
00084
00085 static const uint8_t swb_size_1024_8[] = {
00086 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00087 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
00088 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
00089 };
00090
00091 static const uint8_t *swb_size_1024[] = {
00092 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
00093 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
00094 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
00095 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
00096 };
00097
00098 static const uint8_t swb_size_128_96[] = {
00099 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
00100 };
00101
00102 static const uint8_t swb_size_128_48[] = {
00103 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
00104 };
00105
00106 static const uint8_t swb_size_128_24[] = {
00107 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
00108 };
00109
00110 static const uint8_t swb_size_128_16[] = {
00111 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
00112 };
00113
00114 static const uint8_t swb_size_128_8[] = {
00115 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
00116 };
00117
00118 static const uint8_t *swb_size_128[] = {
00119
00120
00121 swb_size_128_96, swb_size_128_96, swb_size_128_96,
00122 swb_size_128_48, swb_size_128_48, swb_size_128_48,
00123 swb_size_128_24, swb_size_128_24, swb_size_128_16,
00124 swb_size_128_16, swb_size_128_16, swb_size_128_8
00125 };
00126
00128 static const uint8_t aac_chan_configs[6][5] = {
00129 {1, TYPE_SCE},
00130 {1, TYPE_CPE},
00131 {2, TYPE_SCE, TYPE_CPE},
00132 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},
00133 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},
00134 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE},
00135 };
00136
00141 static void put_audio_specific_config(AVCodecContext *avctx)
00142 {
00143 PutBitContext pb;
00144 AACEncContext *s = avctx->priv_data;
00145
00146 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
00147 put_bits(&pb, 5, 2);
00148 put_bits(&pb, 4, s->samplerate_index);
00149 put_bits(&pb, 4, avctx->channels);
00150
00151 put_bits(&pb, 1, 0);
00152 put_bits(&pb, 1, 0);
00153 put_bits(&pb, 1, 0);
00154
00155
00156 put_bits(&pb, 11, 0x2b7);
00157 put_bits(&pb, 5, AOT_SBR);
00158 put_bits(&pb, 1, 0);
00159 flush_put_bits(&pb);
00160 }
00161
00162 static av_cold int aac_encode_init(AVCodecContext *avctx)
00163 {
00164 AACEncContext *s = avctx->priv_data;
00165 int i;
00166 const uint8_t *sizes[2];
00167 int lengths[2];
00168
00169 avctx->frame_size = 1024;
00170
00171 for (i = 0; i < 16; i++)
00172 if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
00173 break;
00174 if (i == 16) {
00175 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
00176 return -1;
00177 }
00178 if (avctx->channels > AAC_MAX_CHANNELS) {
00179 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
00180 return -1;
00181 }
00182 if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
00183 av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
00184 return -1;
00185 }
00186 if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
00187 av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
00188 return -1;
00189 }
00190 s->samplerate_index = i;
00191
00192 dsputil_init(&s->dsp, avctx);
00193 ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
00194 ff_mdct_init(&s->mdct128, 8, 0, 1.0);
00195
00196 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00197 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00198 ff_init_ff_sine_windows(10);
00199 ff_init_ff_sine_windows(7);
00200
00201 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
00202 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
00203 avctx->extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
00204 avctx->extradata_size = 5;
00205 put_audio_specific_config(avctx);
00206
00207 sizes[0] = swb_size_1024[i];
00208 sizes[1] = swb_size_128[i];
00209 lengths[0] = ff_aac_num_swb_1024[i];
00210 lengths[1] = ff_aac_num_swb_128[i];
00211 ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
00212 s->psypp = ff_psy_preprocess_init(avctx);
00213 s->coder = &ff_aac_coders[2];
00214
00215 s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00216
00217 ff_aac_tableinit();
00218
00219 return 0;
00220 }
00221
00222 static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
00223 SingleChannelElement *sce, short *audio)
00224 {
00225 int i, k;
00226 const int chans = avctx->channels;
00227 const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00228 const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
00229 const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
00230 float *output = sce->ret;
00231
00232 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00233 memcpy(output, sce->saved, sizeof(float)*1024);
00234 if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
00235 memset(output, 0, sizeof(output[0]) * 448);
00236 for (i = 448; i < 576; i++)
00237 output[i] = sce->saved[i] * pwindow[i - 448];
00238 for (i = 576; i < 704; i++)
00239 output[i] = sce->saved[i];
00240 }
00241 if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
00242 for (i = 0; i < 1024; i++) {
00243 output[i+1024] = audio[i * chans] * lwindow[1024 - i - 1];
00244 sce->saved[i] = audio[i * chans] * lwindow[i];
00245 }
00246 } else {
00247 for (i = 0; i < 448; i++)
00248 output[i+1024] = audio[i * chans];
00249 for (; i < 576; i++)
00250 output[i+1024] = audio[i * chans] * swindow[576 - i - 1];
00251 memset(output+1024+576, 0, sizeof(output[0]) * 448);
00252 for (i = 0; i < 1024; i++)
00253 sce->saved[i] = audio[i * chans];
00254 }
00255 s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
00256 } else {
00257 for (k = 0; k < 1024; k += 128) {
00258 for (i = 448 + k; i < 448 + k + 256; i++)
00259 output[i - 448 - k] = (i < 1024)
00260 ? sce->saved[i]
00261 : audio[(i-1024)*chans];
00262 s->dsp.vector_fmul (output, output, k ? swindow : pwindow, 128);
00263 s->dsp.vector_fmul_reverse(output+128, output+128, swindow, 128);
00264 s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + k, output);
00265 }
00266 for (i = 0; i < 1024; i++)
00267 sce->saved[i] = audio[i * chans];
00268 }
00269 }
00270
00275 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00276 {
00277 int w;
00278
00279 put_bits(&s->pb, 1, 0);
00280 put_bits(&s->pb, 2, info->window_sequence[0]);
00281 put_bits(&s->pb, 1, info->use_kb_window[0]);
00282 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00283 put_bits(&s->pb, 6, info->max_sfb);
00284 put_bits(&s->pb, 1, 0);
00285 } else {
00286 put_bits(&s->pb, 4, info->max_sfb);
00287 for (w = 1; w < 8; w++)
00288 put_bits(&s->pb, 1, !info->group_len[w]);
00289 }
00290 }
00291
00296 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
00297 {
00298 int i, w;
00299
00300 put_bits(pb, 2, cpe->ms_mode);
00301 if (cpe->ms_mode == 1)
00302 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
00303 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
00304 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
00305 }
00306
00310 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
00311 {
00312 int i, w, w2, g, ch;
00313 int start, maxsfb, cmaxsfb;
00314
00315 for (ch = 0; ch < chans; ch++) {
00316 IndividualChannelStream *ics = &cpe->ch[ch].ics;
00317 start = 0;
00318 maxsfb = 0;
00319 cpe->ch[ch].pulse.num_pulse = 0;
00320 for (w = 0; w < ics->num_windows*16; w += 16) {
00321 for (g = 0; g < ics->num_swb; g++) {
00322
00323 if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
00324 for (i = 0; i < ics->swb_sizes[g]; i++) {
00325 cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
00326 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
00327 }
00328 }
00329 start += ics->swb_sizes[g];
00330 }
00331 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
00332 ;
00333 maxsfb = FFMAX(maxsfb, cmaxsfb);
00334 }
00335 ics->max_sfb = maxsfb;
00336
00337
00338 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
00339 for (g = 0; g < ics->max_sfb; g++) {
00340 i = 1;
00341 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
00342 if (!cpe->ch[ch].zeroes[w2*16 + g]) {
00343 i = 0;
00344 break;
00345 }
00346 }
00347 cpe->ch[ch].zeroes[w*16 + g] = i;
00348 }
00349 }
00350 }
00351
00352 if (chans > 1 && cpe->common_window) {
00353 IndividualChannelStream *ics0 = &cpe->ch[0].ics;
00354 IndividualChannelStream *ics1 = &cpe->ch[1].ics;
00355 int msc = 0;
00356 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
00357 ics1->max_sfb = ics0->max_sfb;
00358 for (w = 0; w < ics0->num_windows*16; w += 16)
00359 for (i = 0; i < ics0->max_sfb; i++)
00360 if (cpe->ms_mask[w+i])
00361 msc++;
00362 if (msc == 0 || ics0->max_sfb == 0)
00363 cpe->ms_mode = 0;
00364 else
00365 cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
00366 }
00367 }
00368
00372 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
00373 {
00374 int w;
00375
00376 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00377 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
00378 }
00379
00383 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
00384 SingleChannelElement *sce)
00385 {
00386 int off = sce->sf_idx[0], diff;
00387 int i, w;
00388
00389 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00390 for (i = 0; i < sce->ics.max_sfb; i++) {
00391 if (!sce->zeroes[w*16 + i]) {
00392 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
00393 if (diff < 0 || diff > 120)
00394 av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
00395 off = sce->sf_idx[w*16 + i];
00396 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00397 }
00398 }
00399 }
00400 }
00401
00405 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00406 {
00407 int i;
00408
00409 put_bits(&s->pb, 1, !!pulse->num_pulse);
00410 if (!pulse->num_pulse)
00411 return;
00412
00413 put_bits(&s->pb, 2, pulse->num_pulse - 1);
00414 put_bits(&s->pb, 6, pulse->start);
00415 for (i = 0; i < pulse->num_pulse; i++) {
00416 put_bits(&s->pb, 5, pulse->pos[i]);
00417 put_bits(&s->pb, 4, pulse->amp[i]);
00418 }
00419 }
00420
00424 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00425 {
00426 int start, i, w, w2;
00427
00428 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00429 start = 0;
00430 for (i = 0; i < sce->ics.max_sfb; i++) {
00431 if (sce->zeroes[w*16 + i]) {
00432 start += sce->ics.swb_sizes[i];
00433 continue;
00434 }
00435 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00436 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00437 sce->ics.swb_sizes[i],
00438 sce->sf_idx[w*16 + i],
00439 sce->band_type[w*16 + i],
00440 s->lambda);
00441 start += sce->ics.swb_sizes[i];
00442 }
00443 }
00444 }
00445
00449 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00450 SingleChannelElement *sce,
00451 int common_window)
00452 {
00453 put_bits(&s->pb, 8, sce->sf_idx[0]);
00454 if (!common_window)
00455 put_ics_info(s, &sce->ics);
00456 encode_band_info(s, sce);
00457 encode_scale_factors(avctx, s, sce);
00458 encode_pulses(s, &sce->pulse);
00459 put_bits(&s->pb, 1, 0);
00460 put_bits(&s->pb, 1, 0);
00461 encode_spectral_coeffs(s, sce);
00462 return 0;
00463 }
00464
00468 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
00469 const char *name)
00470 {
00471 int i, namelen, padbits;
00472
00473 namelen = strlen(name) + 2;
00474 put_bits(&s->pb, 3, TYPE_FIL);
00475 put_bits(&s->pb, 4, FFMIN(namelen, 15));
00476 if (namelen >= 15)
00477 put_bits(&s->pb, 8, namelen - 16);
00478 put_bits(&s->pb, 4, 0);
00479 padbits = 8 - (put_bits_count(&s->pb) & 7);
00480 align_put_bits(&s->pb);
00481 for (i = 0; i < namelen - 2; i++)
00482 put_bits(&s->pb, 8, name[i]);
00483 put_bits(&s->pb, 12 - padbits, 0);
00484 }
00485
00486 static int aac_encode_frame(AVCodecContext *avctx,
00487 uint8_t *frame, int buf_size, void *data)
00488 {
00489 AACEncContext *s = avctx->priv_data;
00490 int16_t *samples = s->samples, *samples2, *la;
00491 ChannelElement *cpe;
00492 int i, j, chans, tag, start_ch;
00493 const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
00494 int chan_el_counter[4];
00495 FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
00496
00497 if (s->last_frame)
00498 return 0;
00499 if (data) {
00500 if (!s->psypp) {
00501 memcpy(s->samples + 1024 * avctx->channels, data,
00502 1024 * avctx->channels * sizeof(s->samples[0]));
00503 } else {
00504 start_ch = 0;
00505 samples2 = s->samples + 1024 * avctx->channels;
00506 for (i = 0; i < chan_map[0]; i++) {
00507 tag = chan_map[i+1];
00508 chans = tag == TYPE_CPE ? 2 : 1;
00509 ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
00510 samples2 + start_ch, start_ch, chans);
00511 start_ch += chans;
00512 }
00513 }
00514 }
00515 if (!avctx->frame_number) {
00516 memcpy(s->samples, s->samples + 1024 * avctx->channels,
00517 1024 * avctx->channels * sizeof(s->samples[0]));
00518 return 0;
00519 }
00520
00521 start_ch = 0;
00522 for (i = 0; i < chan_map[0]; i++) {
00523 FFPsyWindowInfo* wi = windows + start_ch;
00524 tag = chan_map[i+1];
00525 chans = tag == TYPE_CPE ? 2 : 1;
00526 cpe = &s->cpe[i];
00527 for (j = 0; j < chans; j++) {
00528 IndividualChannelStream *ics = &cpe->ch[j].ics;
00529 int k;
00530 int cur_channel = start_ch + j;
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[j].window_type[0] = ONLY_LONG_SEQUENCE;
00537 wi[j].window_shape = 0;
00538 wi[j].num_windows = 1;
00539 wi[j].grouping[0] = 1;
00540 } else {
00541 wi[j] = ff_psy_suggest_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[j].window_type[0];
00546 ics->use_kb_window[1] = ics->use_kb_window[0];
00547 ics->use_kb_window[0] = wi[j].window_shape;
00548 ics->num_windows = wi[j].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 (k = 0; k < ics->num_windows; k++)
00552 ics->group_len[k] = wi[j].grouping[k];
00553
00554 apply_window_and_mdct(avctx, s, &cpe->ch[j], 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 (j = 0; j < chans; j++) {
00573 s->cur_channel = start_ch + j;
00574 ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
00575 s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], 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 (j = 0; j < wi[0].num_windows; j++) {
00584 if (wi[0].grouping[j] != wi[1].grouping[j]) {
00585 cpe->common_window = 0;
00586 break;
00587 }
00588 }
00589 }
00590 s->cur_channel = start_ch;
00591 if (cpe->common_window && s->coder->search_for_ms)
00592 s->coder->search_for_ms(s, cpe, s->lambda);
00593 adjust_frame_information(s, cpe, chans);
00594 if (chans == 2) {
00595 put_bits(&s->pb, 1, cpe->common_window);
00596 if (cpe->common_window) {
00597 put_ics_info(s, &cpe->ch[0].ics);
00598 encode_ms_info(&s->pb, cpe);
00599 }
00600 }
00601 for (j = 0; j < chans; j++) {
00602 s->cur_channel = start_ch + j;
00603 encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
00604 }
00605 start_ch += chans;
00606 }
00607
00608 frame_bits = put_bits_count(&s->pb);
00609 if (frame_bits <= 6144 * avctx->channels - 3)
00610 break;
00611
00612 s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
00613
00614 } while (1);
00615
00616 put_bits(&s->pb, 3, TYPE_END);
00617 flush_put_bits(&s->pb);
00618 avctx->frame_bits = put_bits_count(&s->pb);
00619
00620
00621 if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
00622 float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
00623 s->lambda *= ratio;
00624 s->lambda = FFMIN(s->lambda, 65536.f);
00625 }
00626
00627 if (!data)
00628 s->last_frame = 1;
00629 memcpy(s->samples, s->samples + 1024 * avctx->channels,
00630 1024 * avctx->channels * sizeof(s->samples[0]));
00631 return put_bits_count(&s->pb)>>3;
00632 }
00633
00634 static av_cold int aac_encode_end(AVCodecContext *avctx)
00635 {
00636 AACEncContext *s = avctx->priv_data;
00637
00638 ff_mdct_end(&s->mdct1024);
00639 ff_mdct_end(&s->mdct128);
00640 ff_psy_end(&s->psy);
00641 ff_psy_preprocess_end(s->psypp);
00642 av_freep(&s->samples);
00643 av_freep(&s->cpe);
00644 return 0;
00645 }
00646
00647 AVCodec ff_aac_encoder = {
00648 "aac",
00649 AVMEDIA_TYPE_AUDIO,
00650 CODEC_ID_AAC,
00651 sizeof(AACEncContext),
00652 aac_encode_init,
00653 aac_encode_frame,
00654 aac_encode_end,
00655 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
00656 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00657 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
00658 };