Libav
|
00001 /* 00002 * AAC encoder 00003 * Copyright (C) 2008 Konstantin Shishkov 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00027 /*********************************** 00028 * TODOs: 00029 * add sane pulse detection 00030 * add temporal noise shaping 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 static const uint8_t swb_size_1024_96[] = { 00045 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 00046 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44, 00047 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 00048 }; 00049 00050 static const uint8_t swb_size_1024_64[] = { 00051 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 00052 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36, 00053 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 00054 }; 00055 00056 static const uint8_t swb_size_1024_48[] = { 00057 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 00058 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 00059 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 00060 96 00061 }; 00062 00063 static const uint8_t swb_size_1024_32[] = { 00064 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 00065 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 00066 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 00067 }; 00068 00069 static const uint8_t swb_size_1024_24[] = { 00070 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 00071 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28, 00072 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64 00073 }; 00074 00075 static const uint8_t swb_size_1024_16[] = { 00076 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 00077 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28, 00078 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64 00079 }; 00080 00081 static const uint8_t swb_size_1024_8[] = { 00082 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 00083 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28, 00084 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80 00085 }; 00086 00087 static const uint8_t *swb_size_1024[] = { 00088 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64, 00089 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32, 00090 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16, 00091 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8 00092 }; 00093 00094 static const uint8_t swb_size_128_96[] = { 00095 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36 00096 }; 00097 00098 static const uint8_t swb_size_128_48[] = { 00099 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16 00100 }; 00101 00102 static const uint8_t swb_size_128_24[] = { 00103 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20 00104 }; 00105 00106 static const uint8_t swb_size_128_16[] = { 00107 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20 00108 }; 00109 00110 static const uint8_t swb_size_128_8[] = { 00111 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20 00112 }; 00113 00114 static const uint8_t *swb_size_128[] = { 00115 /* the last entry on the following row is swb_size_128_64 but is a 00116 duplicate of swb_size_128_96 */ 00117 swb_size_128_96, swb_size_128_96, swb_size_128_96, 00118 swb_size_128_48, swb_size_128_48, swb_size_128_48, 00119 swb_size_128_24, swb_size_128_24, swb_size_128_16, 00120 swb_size_128_16, swb_size_128_16, swb_size_128_8 00121 }; 00122 00124 static const uint8_t aac_chan_configs[6][5] = { 00125 {1, TYPE_SCE}, // 1 channel - single channel element 00126 {1, TYPE_CPE}, // 2 channels - channel pair 00127 {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo 00128 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center 00129 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo 00130 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE 00131 }; 00132 00137 static void put_audio_specific_config(AVCodecContext *avctx) 00138 { 00139 PutBitContext pb; 00140 AACEncContext *s = avctx->priv_data; 00141 00142 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8); 00143 put_bits(&pb, 5, 2); //object type - AAC-LC 00144 put_bits(&pb, 4, s->samplerate_index); //sample rate index 00145 put_bits(&pb, 4, avctx->channels); 00146 //GASpecificConfig 00147 put_bits(&pb, 1, 0); //frame length - 1024 samples 00148 put_bits(&pb, 1, 0); //does not depend on core coder 00149 put_bits(&pb, 1, 0); //is not extension 00150 flush_put_bits(&pb); 00151 } 00152 00153 static av_cold int aac_encode_init(AVCodecContext *avctx) 00154 { 00155 AACEncContext *s = avctx->priv_data; 00156 int i; 00157 const uint8_t *sizes[2]; 00158 int lengths[2]; 00159 00160 avctx->frame_size = 1024; 00161 00162 for (i = 0; i < 16; i++) 00163 if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) 00164 break; 00165 if (i == 16) { 00166 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate); 00167 return -1; 00168 } 00169 if (avctx->channels > 6) { 00170 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels); 00171 return -1; 00172 } 00173 if (avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW) { 00174 av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile); 00175 return -1; 00176 } 00177 if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) { 00178 av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n"); 00179 return -1; 00180 } 00181 s->samplerate_index = i; 00182 00183 dsputil_init(&s->dsp, avctx); 00184 ff_mdct_init(&s->mdct1024, 11, 0, 1.0); 00185 ff_mdct_init(&s->mdct128, 8, 0, 1.0); 00186 // window init 00187 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024); 00188 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128); 00189 ff_init_ff_sine_windows(10); 00190 ff_init_ff_sine_windows(7); 00191 00192 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0])); 00193 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]); 00194 avctx->extradata = av_malloc(2); 00195 avctx->extradata_size = 2; 00196 put_audio_specific_config(avctx); 00197 00198 sizes[0] = swb_size_1024[i]; 00199 sizes[1] = swb_size_128[i]; 00200 lengths[0] = ff_aac_num_swb_1024[i]; 00201 lengths[1] = ff_aac_num_swb_128[i]; 00202 ff_psy_init(&s->psy, avctx, 2, sizes, lengths); 00203 s->psypp = ff_psy_preprocess_init(avctx); 00204 s->coder = &ff_aac_coders[2]; 00205 00206 s->lambda = avctx->global_quality ? avctx->global_quality : 120; 00207 00208 ff_aac_tableinit(); 00209 00210 if (avctx->channels > 5) 00211 av_log(avctx, AV_LOG_ERROR, "This encoder does not yet enforce the restrictions on LFEs. " 00212 "The output will most likely be an illegal bitstream.\n"); 00213 00214 return 0; 00215 } 00216 00217 static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s, 00218 SingleChannelElement *sce, short *audio, int channel) 00219 { 00220 int i, j, k; 00221 const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; 00222 const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; 00223 const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; 00224 00225 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) { 00226 memcpy(s->output, sce->saved, sizeof(float)*1024); 00227 if (sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE) { 00228 memset(s->output, 0, sizeof(s->output[0]) * 448); 00229 for (i = 448; i < 576; i++) 00230 s->output[i] = sce->saved[i] * pwindow[i - 448]; 00231 for (i = 576; i < 704; i++) 00232 s->output[i] = sce->saved[i]; 00233 } 00234 if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) { 00235 for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) { 00236 s->output[i+1024] = audio[j] * lwindow[1024 - i - 1]; 00237 sce->saved[i] = audio[j] * lwindow[i]; 00238 } 00239 } else { 00240 for (i = 0, j = channel; i < 448; i++, j += avctx->channels) 00241 s->output[i+1024] = audio[j]; 00242 for (; i < 576; i++, j += avctx->channels) 00243 s->output[i+1024] = audio[j] * swindow[576 - i - 1]; 00244 memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448); 00245 for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) 00246 sce->saved[i] = audio[j]; 00247 } 00248 ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output); 00249 } else { 00250 for (k = 0; k < 1024; k += 128) { 00251 for (i = 448 + k; i < 448 + k + 256; i++) 00252 s->output[i - 448 - k] = (i < 1024) 00253 ? sce->saved[i] 00254 : audio[channel + (i-1024)*avctx->channels]; 00255 s->dsp.vector_fmul (s->output, k ? swindow : pwindow, 128); 00256 s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128); 00257 ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output); 00258 } 00259 for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) 00260 sce->saved[i] = audio[j]; 00261 } 00262 } 00263 00268 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info) 00269 { 00270 int w; 00271 00272 put_bits(&s->pb, 1, 0); // ics_reserved bit 00273 put_bits(&s->pb, 2, info->window_sequence[0]); 00274 put_bits(&s->pb, 1, info->use_kb_window[0]); 00275 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) { 00276 put_bits(&s->pb, 6, info->max_sfb); 00277 put_bits(&s->pb, 1, 0); // no prediction 00278 } else { 00279 put_bits(&s->pb, 4, info->max_sfb); 00280 for (w = 1; w < 8; w++) 00281 put_bits(&s->pb, 1, !info->group_len[w]); 00282 } 00283 } 00284 00289 static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe) 00290 { 00291 int i, w; 00292 00293 put_bits(pb, 2, cpe->ms_mode); 00294 if (cpe->ms_mode == 1) 00295 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w]) 00296 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++) 00297 put_bits(pb, 1, cpe->ms_mask[w*16 + i]); 00298 } 00299 00303 static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, int chans) 00304 { 00305 int i, w, w2, g, ch; 00306 int start, sum, maxsfb, cmaxsfb; 00307 00308 for (ch = 0; ch < chans; ch++) { 00309 IndividualChannelStream *ics = &cpe->ch[ch].ics; 00310 start = 0; 00311 maxsfb = 0; 00312 cpe->ch[ch].pulse.num_pulse = 0; 00313 for (w = 0; w < ics->num_windows*16; w += 16) { 00314 for (g = 0; g < ics->num_swb; g++) { 00315 sum = 0; 00316 //apply M/S 00317 if (!ch && cpe->ms_mask[w + g]) { 00318 for (i = 0; i < ics->swb_sizes[g]; i++) { 00319 cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0; 00320 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i]; 00321 } 00322 } 00323 start += ics->swb_sizes[g]; 00324 } 00325 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--) 00326 ; 00327 maxsfb = FFMAX(maxsfb, cmaxsfb); 00328 } 00329 ics->max_sfb = maxsfb; 00330 00331 //adjust zero bands for window groups 00332 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { 00333 for (g = 0; g < ics->max_sfb; g++) { 00334 i = 1; 00335 for (w2 = w; w2 < w + ics->group_len[w]; w2++) { 00336 if (!cpe->ch[ch].zeroes[w2*16 + g]) { 00337 i = 0; 00338 break; 00339 } 00340 } 00341 cpe->ch[ch].zeroes[w*16 + g] = i; 00342 } 00343 } 00344 } 00345 00346 if (chans > 1 && cpe->common_window) { 00347 IndividualChannelStream *ics0 = &cpe->ch[0].ics; 00348 IndividualChannelStream *ics1 = &cpe->ch[1].ics; 00349 int msc = 0; 00350 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb); 00351 ics1->max_sfb = ics0->max_sfb; 00352 for (w = 0; w < ics0->num_windows*16; w += 16) 00353 for (i = 0; i < ics0->max_sfb; i++) 00354 if (cpe->ms_mask[w+i]) 00355 msc++; 00356 if (msc == 0 || ics0->max_sfb == 0) 00357 cpe->ms_mode = 0; 00358 else 00359 cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2; 00360 } 00361 } 00362 00366 static void encode_band_info(AACEncContext *s, SingleChannelElement *sce) 00367 { 00368 int w; 00369 00370 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 00371 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda); 00372 } 00373 00377 static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s, 00378 SingleChannelElement *sce) 00379 { 00380 int off = sce->sf_idx[0], diff; 00381 int i, w; 00382 00383 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 00384 for (i = 0; i < sce->ics.max_sfb; i++) { 00385 if (!sce->zeroes[w*16 + i]) { 00386 diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO; 00387 if (diff < 0 || diff > 120) 00388 av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n"); 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); //tns 00454 put_bits(&s->pb, 1, 0); //ssr 00455 encode_spectral_coeffs(s, sce); 00456 return 0; 00457 } 00458 00462 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, 00463 const char *name) 00464 { 00465 int i, namelen, padbits; 00466 00467 namelen = strlen(name) + 2; 00468 put_bits(&s->pb, 3, TYPE_FIL); 00469 put_bits(&s->pb, 4, FFMIN(namelen, 15)); 00470 if (namelen >= 15) 00471 put_bits(&s->pb, 8, namelen - 16); 00472 put_bits(&s->pb, 4, 0); //extension type - filler 00473 padbits = 8 - (put_bits_count(&s->pb) & 7); 00474 align_put_bits(&s->pb); 00475 for (i = 0; i < namelen - 2; i++) 00476 put_bits(&s->pb, 8, name[i]); 00477 put_bits(&s->pb, 12 - padbits, 0); 00478 } 00479 00480 static int aac_encode_frame(AVCodecContext *avctx, 00481 uint8_t *frame, int buf_size, void *data) 00482 { 00483 AACEncContext *s = avctx->priv_data; 00484 int16_t *samples = s->samples, *samples2, *la; 00485 ChannelElement *cpe; 00486 int i, j, chans, tag, start_ch; 00487 const uint8_t *chan_map = aac_chan_configs[avctx->channels-1]; 00488 int chan_el_counter[4]; 00489 FFPsyWindowInfo windows[avctx->channels]; 00490 00491 if (s->last_frame) 00492 return 0; 00493 if (data) { 00494 if (!s->psypp) { 00495 memcpy(s->samples + 1024 * avctx->channels, data, 00496 1024 * avctx->channels * sizeof(s->samples[0])); 00497 } else { 00498 start_ch = 0; 00499 samples2 = s->samples + 1024 * avctx->channels; 00500 for (i = 0; i < chan_map[0]; i++) { 00501 tag = chan_map[i+1]; 00502 chans = tag == TYPE_CPE ? 2 : 1; 00503 ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch, 00504 samples2 + start_ch, start_ch, chans); 00505 start_ch += chans; 00506 } 00507 } 00508 } 00509 if (!avctx->frame_number) { 00510 memcpy(s->samples, s->samples + 1024 * avctx->channels, 00511 1024 * avctx->channels * sizeof(s->samples[0])); 00512 return 0; 00513 } 00514 00515 start_ch = 0; 00516 for (i = 0; i < chan_map[0]; i++) { 00517 FFPsyWindowInfo* wi = windows + start_ch; 00518 tag = chan_map[i+1]; 00519 chans = tag == TYPE_CPE ? 2 : 1; 00520 cpe = &s->cpe[i]; 00521 samples2 = samples + start_ch; 00522 la = samples2 + 1024 * avctx->channels + start_ch; 00523 if (!data) 00524 la = NULL; 00525 for (j = 0; j < chans; j++) { 00526 IndividualChannelStream *ics = &cpe->ch[j].ics; 00527 int k; 00528 wi[j] = ff_psy_suggest_window(&s->psy, samples2, la, start_ch + j, ics->window_sequence[0]); 00529 ics->window_sequence[1] = ics->window_sequence[0]; 00530 ics->window_sequence[0] = wi[j].window_type[0]; 00531 ics->use_kb_window[1] = ics->use_kb_window[0]; 00532 ics->use_kb_window[0] = wi[j].window_shape; 00533 ics->num_windows = wi[j].num_windows; 00534 ics->swb_sizes = s->psy.bands [ics->num_windows == 8]; 00535 ics->num_swb = s->psy.num_bands[ics->num_windows == 8]; 00536 for (k = 0; k < ics->num_windows; k++) 00537 ics->group_len[k] = wi[j].grouping[k]; 00538 00539 s->cur_channel = start_ch + j; 00540 apply_window_and_mdct(avctx, s, &cpe->ch[j], samples2, j); 00541 } 00542 start_ch += chans; 00543 } 00544 do { 00545 int frame_bits; 00546 init_put_bits(&s->pb, frame, buf_size*8); 00547 if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT)) 00548 put_bitstream_info(avctx, s, LIBAVCODEC_IDENT); 00549 start_ch = 0; 00550 memset(chan_el_counter, 0, sizeof(chan_el_counter)); 00551 for (i = 0; i < chan_map[0]; i++) { 00552 FFPsyWindowInfo* wi = windows + start_ch; 00553 tag = chan_map[i+1]; 00554 chans = tag == TYPE_CPE ? 2 : 1; 00555 cpe = &s->cpe[i]; 00556 for (j = 0; j < chans; j++) { 00557 s->cur_channel = start_ch + j; 00558 ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]); 00559 s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda); 00560 } 00561 cpe->common_window = 0; 00562 if (chans > 1 00563 && wi[0].window_type[0] == wi[1].window_type[0] 00564 && wi[0].window_shape == wi[1].window_shape) { 00565 00566 cpe->common_window = 1; 00567 for (j = 0; j < wi[0].num_windows; j++) { 00568 if (wi[0].grouping[j] != wi[1].grouping[j]) { 00569 cpe->common_window = 0; 00570 break; 00571 } 00572 } 00573 } 00574 s->cur_channel = start_ch; 00575 if (cpe->common_window && s->coder->search_for_ms) 00576 s->coder->search_for_ms(s, cpe, s->lambda); 00577 adjust_frame_information(s, cpe, chans); 00578 put_bits(&s->pb, 3, tag); 00579 put_bits(&s->pb, 4, chan_el_counter[tag]++); 00580 if (chans == 2) { 00581 put_bits(&s->pb, 1, cpe->common_window); 00582 if (cpe->common_window) { 00583 put_ics_info(s, &cpe->ch[0].ics); 00584 encode_ms_info(&s->pb, cpe); 00585 } 00586 } 00587 for (j = 0; j < chans; j++) { 00588 s->cur_channel = start_ch + j; 00589 encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window); 00590 } 00591 start_ch += chans; 00592 } 00593 00594 frame_bits = put_bits_count(&s->pb); 00595 if (frame_bits <= 6144 * avctx->channels - 3) 00596 break; 00597 00598 s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits; 00599 00600 } while (1); 00601 00602 put_bits(&s->pb, 3, TYPE_END); 00603 flush_put_bits(&s->pb); 00604 avctx->frame_bits = put_bits_count(&s->pb); 00605 00606 // rate control stuff 00607 if (!(avctx->flags & CODEC_FLAG_QSCALE)) { 00608 float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits; 00609 s->lambda *= ratio; 00610 s->lambda = FFMIN(s->lambda, 65536.f); 00611 } 00612 00613 if (!data) 00614 s->last_frame = 1; 00615 memcpy(s->samples, s->samples + 1024 * avctx->channels, 00616 1024 * avctx->channels * sizeof(s->samples[0])); 00617 return put_bits_count(&s->pb)>>3; 00618 } 00619 00620 static av_cold int aac_encode_end(AVCodecContext *avctx) 00621 { 00622 AACEncContext *s = avctx->priv_data; 00623 00624 ff_mdct_end(&s->mdct1024); 00625 ff_mdct_end(&s->mdct128); 00626 ff_psy_end(&s->psy); 00627 ff_psy_preprocess_end(s->psypp); 00628 av_freep(&s->samples); 00629 av_freep(&s->cpe); 00630 return 0; 00631 } 00632 00633 AVCodec aac_encoder = { 00634 "aac", 00635 AVMEDIA_TYPE_AUDIO, 00636 CODEC_ID_AAC, 00637 sizeof(AACEncContext), 00638 aac_encode_init, 00639 aac_encode_frame, 00640 aac_encode_end, 00641 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, 00642 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, 00643 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), 00644 };