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