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 flush_put_bits(&pb);
00153 }
00154
00155 static av_cold int aac_encode_init(AVCodecContext *avctx)
00156 {
00157 AACEncContext *s = avctx->priv_data;
00158 int i;
00159 const uint8_t *sizes[2];
00160 int lengths[2];
00161
00162 avctx->frame_size = 1024;
00163
00164 for (i = 0; i < 16; i++)
00165 if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
00166 break;
00167 if (i == 16) {
00168 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
00169 return -1;
00170 }
00171 if (avctx->channels > AAC_MAX_CHANNELS) {
00172 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
00173 return -1;
00174 }
00175 if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) {
00176 av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile);
00177 return -1;
00178 }
00179 if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) {
00180 av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
00181 return -1;
00182 }
00183 s->samplerate_index = i;
00184
00185 dsputil_init(&s->dsp, avctx);
00186 ff_mdct_init(&s->mdct1024, 11, 0, 1.0);
00187 ff_mdct_init(&s->mdct128, 8, 0, 1.0);
00188
00189 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00190 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00191 ff_init_ff_sine_windows(10);
00192 ff_init_ff_sine_windows(7);
00193
00194 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
00195 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
00196 avctx->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
00197 avctx->extradata_size = 2;
00198 put_audio_specific_config(avctx);
00199
00200 sizes[0] = swb_size_1024[i];
00201 sizes[1] = swb_size_128[i];
00202 lengths[0] = ff_aac_num_swb_1024[i];
00203 lengths[1] = ff_aac_num_swb_128[i];
00204 ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
00205 s->psypp = ff_psy_preprocess_init(avctx);
00206 s->coder = &ff_aac_coders[2];
00207
00208 s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00209
00210 ff_aac_tableinit();
00211
00212 return 0;
00213 }
00214
00215 static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
00216 SingleChannelElement *sce, short *audio)
00217 {
00218 int i, k;
00219 const int chans = avctx->channels;
00220 const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00221 const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
00222 const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
00223
00224 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00225 memcpy(s->output, sce->saved, sizeof(float)*1024);
00226 if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) {
00227 memset(s->output, 0, sizeof(s->output[0]) * 448);
00228 for (i = 448; i < 576; i++)
00229 s->output[i] = sce->saved[i] * pwindow[i - 448];
00230 for (i = 576; i < 704; i++)
00231 s->output[i] = sce->saved[i];
00232 }
00233 if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
00234 for (i = 0; i < 1024; i++) {
00235 s->output[i+1024] = audio[i * chans] * lwindow[1024 - i - 1];
00236 sce->saved[i] = audio[i * chans] * lwindow[i];
00237 }
00238 } else {
00239 for (i = 0; i < 448; i++)
00240 s->output[i+1024] = audio[i * chans];
00241 for (; i < 576; i++)
00242 s->output[i+1024] = audio[i * chans] * swindow[576 - i - 1];
00243 memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448);
00244 for (i = 0; i < 1024; i++)
00245 sce->saved[i] = audio[i * chans];
00246 }
00247 ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output);
00248 } else {
00249 for (k = 0; k < 1024; k += 128) {
00250 for (i = 448 + k; i < 448 + k + 256; i++)
00251 s->output[i - 448 - k] = (i < 1024)
00252 ? sce->saved[i]
00253 : audio[(i-1024)*chans];
00254 s->dsp.vector_fmul (s->output, k ? swindow : pwindow, 128);
00255 s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128);
00256 ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output);
00257 }
00258 for (i = 0; i < 1024; i++)
00259 sce->saved[i] = audio[i * chans];
00260 }
00261 }
00262
00267 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00268 {
00269 int w;
00270
00271 put_bits(&s->pb, 1, 0);
00272 put_bits(&s->pb, 2, info->window_sequence[0]);
00273 put_bits(&s->pb, 1, info->use_kb_window[0]);
00274 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00275 put_bits(&s->pb, 6, info->max_sfb);
00276 put_bits(&s->pb, 1, 0);
00277 } else {
00278 put_bits(&s->pb, 4, info->max_sfb);
00279 for (w = 1; w < 8; w++)
00280 put_bits(&s->pb, 1, !info->group_len[w]);
00281 }
00282 }
00283
00288 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
00289 {
00290 int i, w;
00291
00292 put_bits(pb, 2, cpe->ms_mode);
00293 if (cpe->ms_mode == 1)
00294 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
00295 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
00296 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
00297 }
00298
00302 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
00303 {
00304 int i, w, w2, g, ch;
00305 int start, maxsfb, cmaxsfb;
00306
00307 for (ch = 0; ch < chans; ch++) {
00308 IndividualChannelStream *ics = &cpe->ch[ch].ics;
00309 start = 0;
00310 maxsfb = 0;
00311 cpe->ch[ch].pulse.num_pulse = 0;
00312 for (w = 0; w < ics->num_windows*16; w += 16) {
00313 for (g = 0; g < ics->num_swb; g++) {
00314
00315 if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
00316 for (i = 0; i < ics->swb_sizes[g]; i++) {
00317 cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
00318 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
00319 }
00320 }
00321 start += ics->swb_sizes[g];
00322 }
00323 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
00324 ;
00325 maxsfb = FFMAX(maxsfb, cmaxsfb);
00326 }
00327 ics->max_sfb = maxsfb;
00328
00329
00330 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
00331 for (g = 0; g < ics->max_sfb; g++) {
00332 i = 1;
00333 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
00334 if (!cpe->ch[ch].zeroes[w2*16 + g]) {
00335 i = 0;
00336 break;
00337 }
00338 }
00339 cpe->ch[ch].zeroes[w*16 + g] = i;
00340 }
00341 }
00342 }
00343
00344 if (chans > 1 && cpe->common_window) {
00345 IndividualChannelStream *ics0 = &cpe->ch[0].ics;
00346 IndividualChannelStream *ics1 = &cpe->ch[1].ics;
00347 int msc = 0;
00348 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
00349 ics1->max_sfb = ics0->max_sfb;
00350 for (w = 0; w < ics0->num_windows*16; w += 16)
00351 for (i = 0; i < ics0->max_sfb; i++)
00352 if (cpe->ms_mask[w+i])
00353 msc++;
00354 if (msc == 0 || ics0->max_sfb == 0)
00355 cpe->ms_mode = 0;
00356 else
00357 cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
00358 }
00359 }
00360
00364 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
00365 {
00366 int w;
00367
00368 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00369 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
00370 }
00371
00375 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
00376 SingleChannelElement *sce)
00377 {
00378 int off = sce->sf_idx[0], diff;
00379 int i, w;
00380
00381 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00382 for (i = 0; i < sce->ics.max_sfb; i++) {
00383 if (!sce->zeroes[w*16 + i]) {
00384 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
00385 if (diff < 0 || diff > 120)
00386 av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
00387 off = sce->sf_idx[w*16 + i];
00388 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00389 }
00390 }
00391 }
00392 }
00393
00397 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00398 {
00399 int i;
00400
00401 put_bits(&s->pb, 1, !!pulse->num_pulse);
00402 if (!pulse->num_pulse)
00403 return;
00404
00405 put_bits(&s->pb, 2, pulse->num_pulse - 1);
00406 put_bits(&s->pb, 6, pulse->start);
00407 for (i = 0; i < pulse->num_pulse; i++) {
00408 put_bits(&s->pb, 5, pulse->pos[i]);
00409 put_bits(&s->pb, 4, pulse->amp[i]);
00410 }
00411 }
00412
00416 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00417 {
00418 int start, i, w, w2;
00419
00420 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00421 start = 0;
00422 for (i = 0; i < sce->ics.max_sfb; i++) {
00423 if (sce->zeroes[w*16 + i]) {
00424 start += sce->ics.swb_sizes[i];
00425 continue;
00426 }
00427 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00428 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00429 sce->ics.swb_sizes[i],
00430 sce->sf_idx[w*16 + i],
00431 sce->band_type[w*16 + i],
00432 s->lambda);
00433 start += sce->ics.swb_sizes[i];
00434 }
00435 }
00436 }
00437
00441 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00442 SingleChannelElement *sce,
00443 int common_window)
00444 {
00445 put_bits(&s->pb, 8, sce->sf_idx[0]);
00446 if (!common_window)
00447 put_ics_info(s, &sce->ics);
00448 encode_band_info(s, sce);
00449 encode_scale_factors(avctx, s, sce);
00450 encode_pulses(s, &sce->pulse);
00451 put_bits(&s->pb, 1, 0);
00452 put_bits(&s->pb, 1, 0);
00453 encode_spectral_coeffs(s, sce);
00454 return 0;
00455 }
00456
00460 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
00461 const char *name)
00462 {
00463 int i, namelen, padbits;
00464
00465 namelen = strlen(name) + 2;
00466 put_bits(&s->pb, 3, TYPE_FIL);
00467 put_bits(&s->pb, 4, FFMIN(namelen, 15));
00468 if (namelen >= 15)
00469 put_bits(&s->pb, 8, namelen - 16);
00470 put_bits(&s->pb, 4, 0);
00471 padbits = 8 - (put_bits_count(&s->pb) & 7);
00472 align_put_bits(&s->pb);
00473 for (i = 0; i < namelen - 2; i++)
00474 put_bits(&s->pb, 8, name[i]);
00475 put_bits(&s->pb, 12 - padbits, 0);
00476 }
00477
00478 static int aac_encode_frame(AVCodecContext *avctx,
00479 uint8_t *frame, int buf_size, void *data)
00480 {
00481 AACEncContext *s = avctx->priv_data;
00482 int16_t *samples = s->samples, *samples2, *la;
00483 ChannelElement *cpe;
00484 int i, j, chans, tag, start_ch;
00485 const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
00486 int chan_el_counter[4];
00487 FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
00488
00489 if (s->last_frame)
00490 return 0;
00491 if (data) {
00492 if (!s->psypp) {
00493 memcpy(s->samples + 1024 * avctx->channels, data,
00494 1024 * avctx->channels * sizeof(s->samples[0]));
00495 } else {
00496 start_ch = 0;
00497 samples2 = s->samples + 1024 * avctx->channels;
00498 for (i = 0; i < chan_map[0]; i++) {
00499 tag = chan_map[i+1];
00500 chans = tag == TYPE_CPE ? 2 : 1;
00501 ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch,
00502 samples2 + start_ch, start_ch, chans);
00503 start_ch += chans;
00504 }
00505 }
00506 }
00507 if (!avctx->frame_number) {
00508 memcpy(s->samples, s->samples + 1024 * avctx->channels,
00509 1024 * avctx->channels * sizeof(s->samples[0]));
00510 return 0;
00511 }
00512
00513 start_ch = 0;
00514 for (i = 0; i < chan_map[0]; i++) {
00515 FFPsyWindowInfo* wi = windows + start_ch;
00516 tag = chan_map[i+1];
00517 chans = tag == TYPE_CPE ? 2 : 1;
00518 cpe = &s->cpe[i];
00519 for (j = 0; j < chans; j++) {
00520 IndividualChannelStream *ics = &cpe->ch[j].ics;
00521 int k;
00522 int cur_channel = start_ch + j;
00523 samples2 = samples + cur_channel;
00524 la = samples2 + (448+64) * avctx->channels;
00525 if (!data)
00526 la = NULL;
00527 if (tag == TYPE_LFE) {
00528 wi[j].window_type[0] = ONLY_LONG_SEQUENCE;
00529 wi[j].window_shape = 0;
00530 wi[j].num_windows = 1;
00531 wi[j].grouping[0] = 1;
00532 } else {
00533 wi[j] = ff_psy_suggest_window(&s->psy, samples2, la, cur_channel,
00534 ics->window_sequence[0]);
00535 }
00536 ics->window_sequence[1] = ics->window_sequence[0];
00537 ics->window_sequence[0] = wi[j].window_type[0];
00538 ics->use_kb_window[1] = ics->use_kb_window[0];
00539 ics->use_kb_window[0] = wi[j].window_shape;
00540 ics->num_windows = wi[j].num_windows;
00541 ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
00542 ics->num_swb = tag == TYPE_LFE ? 12 : s->psy.num_bands[ics->num_windows == 8];
00543 for (k = 0; k < ics->num_windows; k++)
00544 ics->group_len[k] = wi[j].grouping[k];
00545
00546 apply_window_and_mdct(avctx, s, &cpe->ch[j], samples2);
00547 }
00548 start_ch += chans;
00549 }
00550 do {
00551 int frame_bits;
00552 init_put_bits(&s->pb, frame, buf_size*8);
00553 if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
00554 put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
00555 start_ch = 0;
00556 memset(chan_el_counter, 0, sizeof(chan_el_counter));
00557 for (i = 0; i < chan_map[0]; i++) {
00558 FFPsyWindowInfo* wi = windows + start_ch;
00559 tag = chan_map[i+1];
00560 chans = tag == TYPE_CPE ? 2 : 1;
00561 cpe = &s->cpe[i];
00562 put_bits(&s->pb, 3, tag);
00563 put_bits(&s->pb, 4, chan_el_counter[tag]++);
00564 for (j = 0; j < chans; j++) {
00565 s->cur_channel = start_ch + j;
00566 ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
00567 s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda);
00568 }
00569 cpe->common_window = 0;
00570 if (chans > 1
00571 && wi[0].window_type[0] == wi[1].window_type[0]
00572 && wi[0].window_shape == wi[1].window_shape) {
00573
00574 cpe->common_window = 1;
00575 for (j = 0; j < wi[0].num_windows; j++) {
00576 if (wi[0].grouping[j] != wi[1].grouping[j]) {
00577 cpe->common_window = 0;
00578 break;
00579 }
00580 }
00581 }
00582 s->cur_channel = start_ch;
00583 if (cpe->common_window && s->coder->search_for_ms)
00584 s->coder->search_for_ms(s, cpe, s->lambda);
00585 adjust_frame_information(s, cpe, chans);
00586 if (chans == 2) {
00587 put_bits(&s->pb, 1, cpe->common_window);
00588 if (cpe->common_window) {
00589 put_ics_info(s, &cpe->ch[0].ics);
00590 encode_ms_info(&s->pb, cpe);
00591 }
00592 }
00593 for (j = 0; j < chans; j++) {
00594 s->cur_channel = start_ch + j;
00595 encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
00596 }
00597 start_ch += chans;
00598 }
00599
00600 frame_bits = put_bits_count(&s->pb);
00601 if (frame_bits <= 6144 * avctx->channels - 3)
00602 break;
00603
00604 s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
00605
00606 } while (1);
00607
00608 put_bits(&s->pb, 3, TYPE_END);
00609 flush_put_bits(&s->pb);
00610 avctx->frame_bits = put_bits_count(&s->pb);
00611
00612
00613 if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
00614 float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
00615 s->lambda *= ratio;
00616 s->lambda = FFMIN(s->lambda, 65536.f);
00617 }
00618
00619 if (!data)
00620 s->last_frame = 1;
00621 memcpy(s->samples, s->samples + 1024 * avctx->channels,
00622 1024 * avctx->channels * sizeof(s->samples[0]));
00623 return put_bits_count(&s->pb)>>3;
00624 }
00625
00626 static av_cold int aac_encode_end(AVCodecContext *avctx)
00627 {
00628 AACEncContext *s = avctx->priv_data;
00629
00630 ff_mdct_end(&s->mdct1024);
00631 ff_mdct_end(&s->mdct128);
00632 ff_psy_end(&s->psy);
00633 ff_psy_preprocess_end(s->psypp);
00634 av_freep(&s->samples);
00635 av_freep(&s->cpe);
00636 return 0;
00637 }
00638
00639 AVCodec aac_encoder = {
00640 "aac",
00641 AVMEDIA_TYPE_AUDIO,
00642 CODEC_ID_AAC,
00643 sizeof(AACEncContext),
00644 aac_encode_init,
00645 aac_encode_frame,
00646 aac_encode_end,
00647 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
00648 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00649 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
00650 };