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/float_dsp.h"
00034 #include "libavutil/opt.h"
00035 #include "avcodec.h"
00036 #include "put_bits.h"
00037 #include "dsputil.h"
00038 #include "internal.h"
00039 #include "mpeg4audio.h"
00040 #include "kbdwin.h"
00041 #include "sinewin.h"
00042
00043 #include "aac.h"
00044 #include "aactab.h"
00045 #include "aacenc.h"
00046
00047 #include "psymodel.h"
00048
00049 #define AAC_MAX_CHANNELS 6
00050
00051 #define ERROR_IF(cond, ...) \
00052 if (cond) { \
00053 av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
00054 return AVERROR(EINVAL); \
00055 }
00056
00057 float ff_aac_pow34sf_tab[428];
00058
00059 static const uint8_t swb_size_1024_96[] = {
00060 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
00061 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
00062 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00063 };
00064
00065 static const uint8_t swb_size_1024_64[] = {
00066 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
00067 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
00068 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
00069 };
00070
00071 static const uint8_t swb_size_1024_48[] = {
00072 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00073 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00074 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
00075 96
00076 };
00077
00078 static const uint8_t swb_size_1024_32[] = {
00079 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00080 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00081 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
00082 };
00083
00084 static const uint8_t swb_size_1024_24[] = {
00085 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00086 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
00087 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
00088 };
00089
00090 static const uint8_t swb_size_1024_16[] = {
00091 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00092 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
00093 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
00094 };
00095
00096 static const uint8_t swb_size_1024_8[] = {
00097 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00098 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
00099 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
00100 };
00101
00102 static const uint8_t *swb_size_1024[] = {
00103 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
00104 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
00105 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
00106 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
00107 };
00108
00109 static const uint8_t swb_size_128_96[] = {
00110 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
00111 };
00112
00113 static const uint8_t swb_size_128_48[] = {
00114 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
00115 };
00116
00117 static const uint8_t swb_size_128_24[] = {
00118 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
00119 };
00120
00121 static const uint8_t swb_size_128_16[] = {
00122 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
00123 };
00124
00125 static const uint8_t swb_size_128_8[] = {
00126 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
00127 };
00128
00129 static const uint8_t *swb_size_128[] = {
00130
00131
00132 swb_size_128_96, swb_size_128_96, swb_size_128_96,
00133 swb_size_128_48, swb_size_128_48, swb_size_128_48,
00134 swb_size_128_24, swb_size_128_24, swb_size_128_16,
00135 swb_size_128_16, swb_size_128_16, swb_size_128_8
00136 };
00137
00139 static const uint8_t aac_chan_configs[6][5] = {
00140 {1, TYPE_SCE},
00141 {1, TYPE_CPE},
00142 {2, TYPE_SCE, TYPE_CPE},
00143 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},
00144 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},
00145 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE},
00146 };
00147
00151 static const uint8_t aac_chan_maps[AAC_MAX_CHANNELS][AAC_MAX_CHANNELS] = {
00152 { 0 },
00153 { 0, 1 },
00154 { 2, 0, 1 },
00155 { 2, 0, 1, 3 },
00156 { 2, 0, 1, 3, 4 },
00157 { 2, 0, 1, 4, 5, 3 },
00158 };
00159
00164 static void put_audio_specific_config(AVCodecContext *avctx)
00165 {
00166 PutBitContext pb;
00167 AACEncContext *s = avctx->priv_data;
00168
00169 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
00170 put_bits(&pb, 5, 2);
00171 put_bits(&pb, 4, s->samplerate_index);
00172 put_bits(&pb, 4, s->channels);
00173
00174 put_bits(&pb, 1, 0);
00175 put_bits(&pb, 1, 0);
00176 put_bits(&pb, 1, 0);
00177
00178
00179 put_bits(&pb, 11, 0x2b7);
00180 put_bits(&pb, 5, AOT_SBR);
00181 put_bits(&pb, 1, 0);
00182 flush_put_bits(&pb);
00183 }
00184
00185 #define WINDOW_FUNC(type) \
00186 static void apply_ ##type ##_window(DSPContext *dsp, AVFloatDSPContext *fdsp, \
00187 SingleChannelElement *sce, \
00188 const float *audio)
00189
00190 WINDOW_FUNC(only_long)
00191 {
00192 const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00193 const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00194 float *out = sce->ret;
00195
00196 fdsp->vector_fmul (out, audio, lwindow, 1024);
00197 dsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
00198 }
00199
00200 WINDOW_FUNC(long_start)
00201 {
00202 const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00203 const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
00204 float *out = sce->ret;
00205
00206 fdsp->vector_fmul(out, audio, lwindow, 1024);
00207 memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
00208 dsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
00209 memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
00210 }
00211
00212 WINDOW_FUNC(long_stop)
00213 {
00214 const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
00215 const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
00216 float *out = sce->ret;
00217
00218 memset(out, 0, sizeof(out[0]) * 448);
00219 fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
00220 memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
00221 dsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
00222 }
00223
00224 WINDOW_FUNC(eight_short)
00225 {
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 const float *in = audio + 448;
00229 float *out = sce->ret;
00230 int w;
00231
00232 for (w = 0; w < 8; w++) {
00233 fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
00234 out += 128;
00235 in += 128;
00236 dsp->vector_fmul_reverse(out, in, swindow, 128);
00237 out += 128;
00238 }
00239 }
00240
00241 static void (*const apply_window[4])(DSPContext *dsp, AVFloatDSPContext *fdsp,
00242 SingleChannelElement *sce,
00243 const float *audio) = {
00244 [ONLY_LONG_SEQUENCE] = apply_only_long_window,
00245 [LONG_START_SEQUENCE] = apply_long_start_window,
00246 [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
00247 [LONG_STOP_SEQUENCE] = apply_long_stop_window
00248 };
00249
00250 static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,
00251 float *audio)
00252 {
00253 int i;
00254 float *output = sce->ret;
00255
00256 apply_window[sce->ics.window_sequence[0]](&s->dsp, &s->fdsp, sce, audio);
00257
00258 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE)
00259 s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);
00260 else
00261 for (i = 0; i < 1024; i += 128)
00262 s->mdct128.mdct_calc(&s->mdct128, sce->coeffs + i, output + i*2);
00263 memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
00264 }
00265
00270 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00271 {
00272 int w;
00273
00274 put_bits(&s->pb, 1, 0);
00275 put_bits(&s->pb, 2, info->window_sequence[0]);
00276 put_bits(&s->pb, 1, info->use_kb_window[0]);
00277 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00278 put_bits(&s->pb, 6, info->max_sfb);
00279 put_bits(&s->pb, 1, 0);
00280 } else {
00281 put_bits(&s->pb, 4, info->max_sfb);
00282 for (w = 1; w < 8; w++)
00283 put_bits(&s->pb, 1, !info->group_len[w]);
00284 }
00285 }
00286
00291 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
00292 {
00293 int i, w;
00294
00295 put_bits(pb, 2, cpe->ms_mode);
00296 if (cpe->ms_mode == 1)
00297 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
00298 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
00299 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
00300 }
00301
00305 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans)
00306 {
00307 int i, w, w2, g, ch;
00308 int start, maxsfb, cmaxsfb;
00309
00310 for (ch = 0; ch < chans; ch++) {
00311 IndividualChannelStream *ics = &cpe->ch[ch].ics;
00312 start = 0;
00313 maxsfb = 0;
00314 cpe->ch[ch].pulse.num_pulse = 0;
00315 for (w = 0; w < ics->num_windows*16; w += 16) {
00316 for (g = 0; g < ics->num_swb; g++) {
00317
00318 if (cpe->common_window && !ch && cpe->ms_mask[w + g]) {
00319 for (i = 0; i < ics->swb_sizes[g]; i++) {
00320 cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
00321 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
00322 }
00323 }
00324 start += ics->swb_sizes[g];
00325 }
00326 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--)
00327 ;
00328 maxsfb = FFMAX(maxsfb, cmaxsfb);
00329 }
00330 ics->max_sfb = maxsfb;
00331
00332
00333 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
00334 for (g = 0; g < ics->max_sfb; g++) {
00335 i = 1;
00336 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
00337 if (!cpe->ch[ch].zeroes[w2*16 + g]) {
00338 i = 0;
00339 break;
00340 }
00341 }
00342 cpe->ch[ch].zeroes[w*16 + g] = i;
00343 }
00344 }
00345 }
00346
00347 if (chans > 1 && cpe->common_window) {
00348 IndividualChannelStream *ics0 = &cpe->ch[0].ics;
00349 IndividualChannelStream *ics1 = &cpe->ch[1].ics;
00350 int msc = 0;
00351 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
00352 ics1->max_sfb = ics0->max_sfb;
00353 for (w = 0; w < ics0->num_windows*16; w += 16)
00354 for (i = 0; i < ics0->max_sfb; i++)
00355 if (cpe->ms_mask[w+i])
00356 msc++;
00357 if (msc == 0 || ics0->max_sfb == 0)
00358 cpe->ms_mode = 0;
00359 else
00360 cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;
00361 }
00362 }
00363
00367 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
00368 {
00369 int w;
00370
00371 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00372 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
00373 }
00374
00378 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
00379 SingleChannelElement *sce)
00380 {
00381 int off = sce->sf_idx[0], diff;
00382 int i, w;
00383
00384 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00385 for (i = 0; i < sce->ics.max_sfb; i++) {
00386 if (!sce->zeroes[w*16 + i]) {
00387 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
00388 if (diff < 0 || diff > 120)
00389 av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
00390 off = sce->sf_idx[w*16 + i];
00391 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00392 }
00393 }
00394 }
00395 }
00396
00400 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00401 {
00402 int i;
00403
00404 put_bits(&s->pb, 1, !!pulse->num_pulse);
00405 if (!pulse->num_pulse)
00406 return;
00407
00408 put_bits(&s->pb, 2, pulse->num_pulse - 1);
00409 put_bits(&s->pb, 6, pulse->start);
00410 for (i = 0; i < pulse->num_pulse; i++) {
00411 put_bits(&s->pb, 5, pulse->pos[i]);
00412 put_bits(&s->pb, 4, pulse->amp[i]);
00413 }
00414 }
00415
00419 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00420 {
00421 int start, i, w, w2;
00422
00423 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00424 start = 0;
00425 for (i = 0; i < sce->ics.max_sfb; i++) {
00426 if (sce->zeroes[w*16 + i]) {
00427 start += sce->ics.swb_sizes[i];
00428 continue;
00429 }
00430 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00431 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00432 sce->ics.swb_sizes[i],
00433 sce->sf_idx[w*16 + i],
00434 sce->band_type[w*16 + i],
00435 s->lambda);
00436 start += sce->ics.swb_sizes[i];
00437 }
00438 }
00439 }
00440
00444 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00445 SingleChannelElement *sce,
00446 int common_window)
00447 {
00448 put_bits(&s->pb, 8, sce->sf_idx[0]);
00449 if (!common_window)
00450 put_ics_info(s, &sce->ics);
00451 encode_band_info(s, sce);
00452 encode_scale_factors(avctx, s, sce);
00453 encode_pulses(s, &sce->pulse);
00454 put_bits(&s->pb, 1, 0);
00455 put_bits(&s->pb, 1, 0);
00456 encode_spectral_coeffs(s, sce);
00457 return 0;
00458 }
00459
00463 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
00464 const char *name)
00465 {
00466 int i, namelen, padbits;
00467
00468 namelen = strlen(name) + 2;
00469 put_bits(&s->pb, 3, TYPE_FIL);
00470 put_bits(&s->pb, 4, FFMIN(namelen, 15));
00471 if (namelen >= 15)
00472 put_bits(&s->pb, 8, namelen - 14);
00473 put_bits(&s->pb, 4, 0);
00474 padbits = -put_bits_count(&s->pb) & 7;
00475 avpriv_align_put_bits(&s->pb);
00476 for (i = 0; i < namelen - 2; i++)
00477 put_bits(&s->pb, 8, name[i]);
00478 put_bits(&s->pb, 12 - padbits, 0);
00479 }
00480
00481
00482
00483
00484
00485 static void deinterleave_input_samples(AACEncContext *s, const AVFrame *frame)
00486 {
00487 int ch, i;
00488 const int sinc = s->channels;
00489 const uint8_t *channel_map = aac_chan_maps[sinc - 1];
00490
00491
00492 for (ch = 0; ch < sinc; ch++) {
00493
00494 memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
00495
00496
00497 i = 2048;
00498 if (frame) {
00499 const float *sptr = ((const float *)frame->data[0]) + channel_map[ch];
00500 for (; i < 2048 + frame->nb_samples; i++) {
00501 s->planar_samples[ch][i] = *sptr;
00502 sptr += sinc;
00503 }
00504 }
00505 memset(&s->planar_samples[ch][i], 0,
00506 (3072 - i) * sizeof(s->planar_samples[0][0]));
00507 }
00508 }
00509
00510 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00511 const AVFrame *frame, int *got_packet_ptr)
00512 {
00513 AACEncContext *s = avctx->priv_data;
00514 float **samples = s->planar_samples, *samples2, *la, *overlap;
00515 ChannelElement *cpe;
00516 int i, ch, w, g, chans, tag, start_ch, ret;
00517 int chan_el_counter[4];
00518 FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
00519
00520 if (s->last_frame == 2)
00521 return 0;
00522
00523
00524 if (frame) {
00525 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00526 return ret;
00527 }
00528
00529 deinterleave_input_samples(s, frame);
00530 if (s->psypp)
00531 ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
00532
00533 if (!avctx->frame_number)
00534 return 0;
00535
00536 start_ch = 0;
00537 for (i = 0; i < s->chan_map[0]; i++) {
00538 FFPsyWindowInfo* wi = windows + start_ch;
00539 tag = s->chan_map[i+1];
00540 chans = tag == TYPE_CPE ? 2 : 1;
00541 cpe = &s->cpe[i];
00542 for (ch = 0; ch < chans; ch++) {
00543 IndividualChannelStream *ics = &cpe->ch[ch].ics;
00544 int cur_channel = start_ch + ch;
00545 overlap = &samples[cur_channel][0];
00546 samples2 = overlap + 1024;
00547 la = samples2 + (448+64);
00548 if (!frame)
00549 la = NULL;
00550 if (tag == TYPE_LFE) {
00551 wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
00552 wi[ch].window_shape = 0;
00553 wi[ch].num_windows = 1;
00554 wi[ch].grouping[0] = 1;
00555
00556
00557
00558
00559
00560 ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
00561 } else {
00562 wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel,
00563 ics->window_sequence[0]);
00564 }
00565 ics->window_sequence[1] = ics->window_sequence[0];
00566 ics->window_sequence[0] = wi[ch].window_type[0];
00567 ics->use_kb_window[1] = ics->use_kb_window[0];
00568 ics->use_kb_window[0] = wi[ch].window_shape;
00569 ics->num_windows = wi[ch].num_windows;
00570 ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
00571 ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
00572 for (w = 0; w < ics->num_windows; w++)
00573 ics->group_len[w] = wi[ch].grouping[w];
00574
00575 apply_window_and_mdct(s, &cpe->ch[ch], overlap);
00576 }
00577 start_ch += chans;
00578 }
00579 if ((ret = ff_alloc_packet2(avctx, avpkt, 768 * s->channels))) {
00580 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00581 return ret;
00582 }
00583 do {
00584 int frame_bits;
00585
00586 init_put_bits(&s->pb, avpkt->data, avpkt->size);
00587
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 * s->channels - 3) {
00648 s->psy.bitres.bits = frame_bits / s->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 (!frame)
00668 s->last_frame++;
00669
00670 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00671 &avpkt->duration);
00672
00673 avpkt->size = put_bits_count(&s->pb) >> 3;
00674 *got_packet_ptr = 1;
00675 return 0;
00676 }
00677
00678 static av_cold int aac_encode_end(AVCodecContext *avctx)
00679 {
00680 AACEncContext *s = avctx->priv_data;
00681
00682 ff_mdct_end(&s->mdct1024);
00683 ff_mdct_end(&s->mdct128);
00684 ff_psy_end(&s->psy);
00685 if (s->psypp)
00686 ff_psy_preprocess_end(s->psypp);
00687 av_freep(&s->buffer.samples);
00688 av_freep(&s->cpe);
00689 ff_af_queue_close(&s->afq);
00690 #if FF_API_OLD_ENCODE_AUDIO
00691 av_freep(&avctx->coded_frame);
00692 #endif
00693 return 0;
00694 }
00695
00696 static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
00697 {
00698 int ret = 0;
00699
00700 ff_dsputil_init(&s->dsp, avctx);
00701 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
00702
00703
00704 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00705 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00706 ff_init_ff_sine_windows(10);
00707 ff_init_ff_sine_windows(7);
00708
00709 if (ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0))
00710 return ret;
00711 if (ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0))
00712 return ret;
00713
00714 return 0;
00715 }
00716
00717 static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
00718 {
00719 int ch;
00720 FF_ALLOCZ_OR_GOTO(avctx, s->buffer.samples, 3 * 1024 * s->channels * sizeof(s->buffer.samples[0]), alloc_fail);
00721 FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], alloc_fail);
00722 FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + FF_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
00723
00724 for(ch = 0; ch < s->channels; ch++)
00725 s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
00726
00727 #if FF_API_OLD_ENCODE_AUDIO
00728 if (!(avctx->coded_frame = avcodec_alloc_frame()))
00729 goto alloc_fail;
00730 #endif
00731
00732 return 0;
00733 alloc_fail:
00734 return AVERROR(ENOMEM);
00735 }
00736
00737 static av_cold int aac_encode_init(AVCodecContext *avctx)
00738 {
00739 AACEncContext *s = avctx->priv_data;
00740 int i, ret = 0;
00741 const uint8_t *sizes[2];
00742 uint8_t grouping[AAC_MAX_CHANNELS];
00743 int lengths[2];
00744
00745 avctx->frame_size = 1024;
00746
00747 for (i = 0; i < 16; i++)
00748 if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
00749 break;
00750
00751 s->channels = avctx->channels;
00752
00753 ERROR_IF(i == 16,
00754 "Unsupported sample rate %d\n", avctx->sample_rate);
00755 ERROR_IF(s->channels > AAC_MAX_CHANNELS,
00756 "Unsupported number of channels: %d\n", s->channels);
00757 ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
00758 "Unsupported profile %d\n", avctx->profile);
00759 ERROR_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
00760 "Too many bits per frame requested\n");
00761
00762 s->samplerate_index = i;
00763
00764 s->chan_map = aac_chan_configs[s->channels-1];
00765
00766 if (ret = dsp_init(avctx, s))
00767 goto fail;
00768
00769 if (ret = alloc_buffers(avctx, s))
00770 goto fail;
00771
00772 avctx->extradata_size = 5;
00773 put_audio_specific_config(avctx);
00774
00775 sizes[0] = swb_size_1024[i];
00776 sizes[1] = swb_size_128[i];
00777 lengths[0] = ff_aac_num_swb_1024[i];
00778 lengths[1] = ff_aac_num_swb_128[i];
00779 for (i = 0; i < s->chan_map[0]; i++)
00780 grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
00781 if (ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping))
00782 goto fail;
00783 s->psypp = ff_psy_preprocess_init(avctx);
00784 s->coder = &ff_aac_coders[s->options.aac_coder];
00785
00786 s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00787
00788 ff_aac_tableinit();
00789
00790 for (i = 0; i < 428; i++)
00791 ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
00792
00793 avctx->delay = 1024;
00794 ff_af_queue_init(avctx, &s->afq);
00795
00796 return 0;
00797 fail:
00798 aac_encode_end(avctx);
00799 return ret;
00800 }
00801
00802 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
00803 static const AVOption aacenc_options[] = {
00804 {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), AV_OPT_TYPE_INT, {.i64 = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
00805 {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00806 {"ms_off", "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00807 {"ms_force", "Force Mid/Side for the whole frame if possible", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00808 {"aac_coder", "", offsetof(AACEncContext, options.aac_coder), AV_OPT_TYPE_INT, {.i64 = 2}, 0, AAC_CODER_NB-1, AACENC_FLAGS},
00809 {NULL}
00810 };
00811
00812 static const AVClass aacenc_class = {
00813 "AAC encoder",
00814 av_default_item_name,
00815 aacenc_options,
00816 LIBAVUTIL_VERSION_INT,
00817 };
00818
00819 AVCodec ff_aac_encoder = {
00820 .name = "aac",
00821 .type = AVMEDIA_TYPE_AUDIO,
00822 .id = AV_CODEC_ID_AAC,
00823 .priv_data_size = sizeof(AACEncContext),
00824 .init = aac_encode_init,
00825 .encode2 = aac_encode_frame,
00826 .close = aac_encode_end,
00827 .supported_samplerates = avpriv_mpeg4audio_sample_rates,
00828 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY |
00829 CODEC_CAP_EXPERIMENTAL,
00830 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
00831 AV_SAMPLE_FMT_NONE },
00832 .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
00833 .priv_class = &aacenc_class,
00834 };