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