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_buf;
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_buf;
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_buf;
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_buf;
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_buf;
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(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 av_assert0(diff >= 0 && diff <= 120);
00389 off = sce->sf_idx[w*16 + i];
00390 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
00391 }
00392 }
00393 }
00394 }
00395
00399 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00400 {
00401 int i;
00402
00403 put_bits(&s->pb, 1, !!pulse->num_pulse);
00404 if (!pulse->num_pulse)
00405 return;
00406
00407 put_bits(&s->pb, 2, pulse->num_pulse - 1);
00408 put_bits(&s->pb, 6, pulse->start);
00409 for (i = 0; i < pulse->num_pulse; i++) {
00410 put_bits(&s->pb, 5, pulse->pos[i]);
00411 put_bits(&s->pb, 4, pulse->amp[i]);
00412 }
00413 }
00414
00418 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00419 {
00420 int start, i, w, w2;
00421
00422 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00423 start = 0;
00424 for (i = 0; i < sce->ics.max_sfb; i++) {
00425 if (sce->zeroes[w*16 + i]) {
00426 start += sce->ics.swb_sizes[i];
00427 continue;
00428 }
00429 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)
00430 s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128,
00431 sce->ics.swb_sizes[i],
00432 sce->sf_idx[w*16 + i],
00433 sce->band_type[w*16 + i],
00434 s->lambda);
00435 start += sce->ics.swb_sizes[i];
00436 }
00437 }
00438 }
00439
00443 static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
00444 SingleChannelElement *sce,
00445 int common_window)
00446 {
00447 put_bits(&s->pb, 8, sce->sf_idx[0]);
00448 if (!common_window)
00449 put_ics_info(s, &sce->ics);
00450 encode_band_info(s, sce);
00451 encode_scale_factors(avctx, s, sce);
00452 encode_pulses(s, &sce->pulse);
00453 put_bits(&s->pb, 1, 0);
00454 put_bits(&s->pb, 1, 0);
00455 encode_spectral_coeffs(s, sce);
00456 return 0;
00457 }
00458
00462 static void put_bitstream_info(AACEncContext *s, const char *name)
00463 {
00464 int i, namelen, padbits;
00465
00466 namelen = strlen(name) + 2;
00467 put_bits(&s->pb, 3, TYPE_FIL);
00468 put_bits(&s->pb, 4, FFMIN(namelen, 15));
00469 if (namelen >= 15)
00470 put_bits(&s->pb, 8, namelen - 14);
00471 put_bits(&s->pb, 4, 0);
00472 padbits = -put_bits_count(&s->pb) & 7;
00473 avpriv_align_put_bits(&s->pb);
00474 for (i = 0; i < namelen - 2; i++)
00475 put_bits(&s->pb, 8, name[i]);
00476 put_bits(&s->pb, 12 - padbits, 0);
00477 }
00478
00479
00480
00481
00482
00483 static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
00484 {
00485 int ch;
00486 int end = 2048 + (frame ? frame->nb_samples : 0);
00487 const uint8_t *channel_map = aac_chan_maps[s->channels - 1];
00488
00489
00490 for (ch = 0; ch < s->channels; ch++) {
00491
00492 memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
00493
00494
00495 if (frame) {
00496 memcpy(&s->planar_samples[ch][2048],
00497 frame->extended_data[channel_map[ch]],
00498 frame->nb_samples * sizeof(s->planar_samples[0][0]));
00499 }
00500 memset(&s->planar_samples[ch][end], 0,
00501 (3072 - end) * sizeof(s->planar_samples[0][0]));
00502 }
00503 }
00504
00505 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00506 const AVFrame *frame, int *got_packet_ptr)
00507 {
00508 AACEncContext *s = avctx->priv_data;
00509 float **samples = s->planar_samples, *samples2, *la, *overlap;
00510 ChannelElement *cpe;
00511 int i, ch, w, g, chans, tag, start_ch, ret;
00512 int chan_el_counter[4];
00513 FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
00514
00515 if (s->last_frame == 2)
00516 return 0;
00517
00518
00519 if (frame) {
00520 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00521 return ret;
00522 }
00523
00524 copy_input_samples(s, frame);
00525 if (s->psypp)
00526 ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
00527
00528 if (!avctx->frame_number)
00529 return 0;
00530
00531 start_ch = 0;
00532 for (i = 0; i < s->chan_map[0]; i++) {
00533 FFPsyWindowInfo* wi = windows + start_ch;
00534 tag = s->chan_map[i+1];
00535 chans = tag == TYPE_CPE ? 2 : 1;
00536 cpe = &s->cpe[i];
00537 for (ch = 0; ch < chans; ch++) {
00538 IndividualChannelStream *ics = &cpe->ch[ch].ics;
00539 int cur_channel = start_ch + ch;
00540 overlap = &samples[cur_channel][0];
00541 samples2 = overlap + 1024;
00542 la = samples2 + (448+64);
00543 if (!frame)
00544 la = NULL;
00545 if (tag == TYPE_LFE) {
00546 wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
00547 wi[ch].window_shape = 0;
00548 wi[ch].num_windows = 1;
00549 wi[ch].grouping[0] = 1;
00550
00551
00552
00553
00554
00555 ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
00556 } else {
00557 wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel,
00558 ics->window_sequence[0]);
00559 }
00560 ics->window_sequence[1] = ics->window_sequence[0];
00561 ics->window_sequence[0] = wi[ch].window_type[0];
00562 ics->use_kb_window[1] = ics->use_kb_window[0];
00563 ics->use_kb_window[0] = wi[ch].window_shape;
00564 ics->num_windows = wi[ch].num_windows;
00565 ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
00566 ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
00567 for (w = 0; w < ics->num_windows; w++)
00568 ics->group_len[w] = wi[ch].grouping[w];
00569
00570 apply_window_and_mdct(s, &cpe->ch[ch], overlap);
00571 }
00572 start_ch += chans;
00573 }
00574 if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels))) {
00575 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00576 return ret;
00577 }
00578 do {
00579 int frame_bits;
00580
00581 init_put_bits(&s->pb, avpkt->data, avpkt->size);
00582
00583 if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
00584 put_bitstream_info(s, LIBAVCODEC_IDENT);
00585 start_ch = 0;
00586 memset(chan_el_counter, 0, sizeof(chan_el_counter));
00587 for (i = 0; i < s->chan_map[0]; i++) {
00588 FFPsyWindowInfo* wi = windows + start_ch;
00589 const float *coeffs[2];
00590 tag = s->chan_map[i+1];
00591 chans = tag == TYPE_CPE ? 2 : 1;
00592 cpe = &s->cpe[i];
00593 put_bits(&s->pb, 3, tag);
00594 put_bits(&s->pb, 4, chan_el_counter[tag]++);
00595 for (ch = 0; ch < chans; ch++)
00596 coeffs[ch] = cpe->ch[ch].coeffs;
00597 s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
00598 for (ch = 0; ch < chans; ch++) {
00599 s->cur_channel = start_ch * 2 + ch;
00600 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
00601 }
00602 cpe->common_window = 0;
00603 if (chans > 1
00604 && wi[0].window_type[0] == wi[1].window_type[0]
00605 && wi[0].window_shape == wi[1].window_shape) {
00606
00607 cpe->common_window = 1;
00608 for (w = 0; w < wi[0].num_windows; w++) {
00609 if (wi[0].grouping[w] != wi[1].grouping[w]) {
00610 cpe->common_window = 0;
00611 break;
00612 }
00613 }
00614 }
00615 s->cur_channel = start_ch * 2;
00616 if (s->options.stereo_mode && cpe->common_window) {
00617 if (s->options.stereo_mode > 0) {
00618 IndividualChannelStream *ics = &cpe->ch[0].ics;
00619 for (w = 0; w < ics->num_windows; w += ics->group_len[w])
00620 for (g = 0; g < ics->num_swb; g++)
00621 cpe->ms_mask[w*16+g] = 1;
00622 } else if (s->coder->search_for_ms) {
00623 s->coder->search_for_ms(s, cpe, s->lambda);
00624 }
00625 }
00626 adjust_frame_information(cpe, chans);
00627 if (chans == 2) {
00628 put_bits(&s->pb, 1, cpe->common_window);
00629 if (cpe->common_window) {
00630 put_ics_info(s, &cpe->ch[0].ics);
00631 encode_ms_info(&s->pb, cpe);
00632 }
00633 }
00634 for (ch = 0; ch < chans; ch++) {
00635 s->cur_channel = start_ch + ch;
00636 encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
00637 }
00638 start_ch += chans;
00639 }
00640
00641 frame_bits = put_bits_count(&s->pb);
00642 if (frame_bits <= 6144 * s->channels - 3) {
00643 s->psy.bitres.bits = frame_bits / s->channels;
00644 break;
00645 }
00646
00647 s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
00648
00649 } while (1);
00650
00651 put_bits(&s->pb, 3, TYPE_END);
00652 flush_put_bits(&s->pb);
00653 avctx->frame_bits = put_bits_count(&s->pb);
00654
00655
00656 if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
00657 float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits;
00658 s->lambda *= ratio;
00659 s->lambda = FFMIN(s->lambda, 65536.f);
00660 }
00661
00662 if (!frame)
00663 s->last_frame++;
00664
00665 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00666 &avpkt->duration);
00667
00668 avpkt->size = put_bits_count(&s->pb) >> 3;
00669 *got_packet_ptr = 1;
00670 return 0;
00671 }
00672
00673 static av_cold int aac_encode_end(AVCodecContext *avctx)
00674 {
00675 AACEncContext *s = avctx->priv_data;
00676
00677 ff_mdct_end(&s->mdct1024);
00678 ff_mdct_end(&s->mdct128);
00679 ff_psy_end(&s->psy);
00680 if (s->psypp)
00681 ff_psy_preprocess_end(s->psypp);
00682 av_freep(&s->buffer.samples);
00683 av_freep(&s->cpe);
00684 ff_af_queue_close(&s->afq);
00685 #if FF_API_OLD_ENCODE_AUDIO
00686 av_freep(&avctx->coded_frame);
00687 #endif
00688 return 0;
00689 }
00690
00691 static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
00692 {
00693 int ret = 0;
00694
00695 ff_dsputil_init(&s->dsp, avctx);
00696 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
00697
00698
00699 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00700 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00701 ff_init_ff_sine_windows(10);
00702 ff_init_ff_sine_windows(7);
00703
00704 if (ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0))
00705 return ret;
00706 if (ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0))
00707 return ret;
00708
00709 return 0;
00710 }
00711
00712 static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
00713 {
00714 int ch;
00715 FF_ALLOCZ_OR_GOTO(avctx, s->buffer.samples, 3 * 1024 * s->channels * sizeof(s->buffer.samples[0]), alloc_fail);
00716 FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], alloc_fail);
00717 FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + FF_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
00718
00719 for(ch = 0; ch < s->channels; ch++)
00720 s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
00721
00722 #if FF_API_OLD_ENCODE_AUDIO
00723 if (!(avctx->coded_frame = avcodec_alloc_frame()))
00724 goto alloc_fail;
00725 #endif
00726
00727 return 0;
00728 alloc_fail:
00729 return AVERROR(ENOMEM);
00730 }
00731
00732 static av_cold int aac_encode_init(AVCodecContext *avctx)
00733 {
00734 AACEncContext *s = avctx->priv_data;
00735 int i, ret = 0;
00736 const uint8_t *sizes[2];
00737 uint8_t grouping[AAC_MAX_CHANNELS];
00738 int lengths[2];
00739
00740 avctx->frame_size = 1024;
00741
00742 for (i = 0; i < 16; i++)
00743 if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
00744 break;
00745
00746 s->channels = avctx->channels;
00747
00748 ERROR_IF(i == 16,
00749 "Unsupported sample rate %d\n", avctx->sample_rate);
00750 ERROR_IF(s->channels > AAC_MAX_CHANNELS,
00751 "Unsupported number of channels: %d\n", s->channels);
00752 ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
00753 "Unsupported profile %d\n", avctx->profile);
00754 ERROR_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
00755 "Too many bits per frame requested\n");
00756
00757 s->samplerate_index = i;
00758
00759 s->chan_map = aac_chan_configs[s->channels-1];
00760
00761 if (ret = dsp_init(avctx, s))
00762 goto fail;
00763
00764 if (ret = alloc_buffers(avctx, s))
00765 goto fail;
00766
00767 avctx->extradata_size = 5;
00768 put_audio_specific_config(avctx);
00769
00770 sizes[0] = swb_size_1024[i];
00771 sizes[1] = swb_size_128[i];
00772 lengths[0] = ff_aac_num_swb_1024[i];
00773 lengths[1] = ff_aac_num_swb_128[i];
00774 for (i = 0; i < s->chan_map[0]; i++)
00775 grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
00776 if (ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping))
00777 goto fail;
00778 s->psypp = ff_psy_preprocess_init(avctx);
00779 s->coder = &ff_aac_coders[s->options.aac_coder];
00780
00781 s->lambda = avctx->global_quality ? avctx->global_quality : 120;
00782
00783 ff_aac_tableinit();
00784
00785 for (i = 0; i < 428; i++)
00786 ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
00787
00788 avctx->delay = 1024;
00789 ff_af_queue_init(avctx, &s->afq);
00790
00791 return 0;
00792 fail:
00793 aac_encode_end(avctx);
00794 return ret;
00795 }
00796
00797 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
00798 static const AVOption aacenc_options[] = {
00799 {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), AV_OPT_TYPE_INT, {.i64 = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
00800 {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00801 {"ms_off", "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
00802 {"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"},
00803 {"aac_coder", "", offsetof(AACEncContext, options.aac_coder), AV_OPT_TYPE_INT, {.i64 = 2}, 0, AAC_CODER_NB-1, AACENC_FLAGS},
00804 {NULL}
00805 };
00806
00807 static const AVClass aacenc_class = {
00808 "AAC encoder",
00809 av_default_item_name,
00810 aacenc_options,
00811 LIBAVUTIL_VERSION_INT,
00812 };
00813
00814
00815
00816 static const int mpeg4audio_sample_rates[16] = {
00817 96000, 88200, 64000, 48000, 44100, 32000,
00818 24000, 22050, 16000, 12000, 11025, 8000, 7350
00819 };
00820
00821 AVCodec ff_aac_encoder = {
00822 .name = "aac",
00823 .type = AVMEDIA_TYPE_AUDIO,
00824 .id = AV_CODEC_ID_AAC,
00825 .priv_data_size = sizeof(AACEncContext),
00826 .init = aac_encode_init,
00827 .encode2 = aac_encode_frame,
00828 .close = aac_encode_end,
00829 .supported_samplerates = mpeg4audio_sample_rates,
00830 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY |
00831 CODEC_CAP_EXPERIMENTAL,
00832 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
00833 AV_SAMPLE_FMT_NONE },
00834 .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
00835 .priv_class = &aacenc_class,
00836 };