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
00034 #include "avcodec.h"
00035 #include "bitstream.h"
00036 #include "dsputil.h"
00037 #include "mpeg4audio.h"
00038
00039 #include "aacpsy.h"
00040 #include "aac.h"
00041 #include "aactab.h"
00042
00043 static const uint8_t swb_size_1024_96[] = {
00044 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
00045 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
00046 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00047 };
00048
00049 static const uint8_t swb_size_1024_64[] = {
00050 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
00051 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
00052 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
00053 };
00054
00055 static const uint8_t swb_size_1024_48[] = {
00056 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00057 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00058 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
00059 96
00060 };
00061
00062 static const uint8_t swb_size_1024_32[] = {
00063 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
00064 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
00065 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
00066 };
00067
00068 static const uint8_t swb_size_1024_24[] = {
00069 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00070 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
00071 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
00072 };
00073
00074 static const uint8_t swb_size_1024_16[] = {
00075 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00076 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
00077 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
00078 };
00079
00080 static const uint8_t swb_size_1024_8[] = {
00081 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00082 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
00083 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
00084 };
00085
00086 static const uint8_t * const swb_size_1024[] = {
00087 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
00088 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
00089 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
00090 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
00091 };
00092
00093 static const uint8_t swb_size_128_96[] = {
00094 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
00095 };
00096
00097 static const uint8_t swb_size_128_48[] = {
00098 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
00099 };
00100
00101 static const uint8_t swb_size_128_24[] = {
00102 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
00103 };
00104
00105 static const uint8_t swb_size_128_16[] = {
00106 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
00107 };
00108
00109 static const uint8_t swb_size_128_8[] = {
00110 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
00111 };
00112
00113 static const uint8_t * const swb_size_128[] = {
00114
00115
00116 swb_size_128_96, swb_size_128_96, swb_size_128_96,
00117 swb_size_128_48, swb_size_128_48, swb_size_128_48,
00118 swb_size_128_24, swb_size_128_24, swb_size_128_16,
00119 swb_size_128_16, swb_size_128_16, swb_size_128_8
00120 };
00121
00123 static const uint8_t run_value_bits_long[64] = {
00124 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00125 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
00126 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
00127 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
00128 };
00129
00131 static const uint8_t run_value_bits_short[16] = {
00132 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
00133 };
00134
00135 static const uint8_t* const run_value_bits[2] = {
00136 run_value_bits_long, run_value_bits_short
00137 };
00138
00140 static const uint8_t aac_chan_configs[6][5] = {
00141 {1, TYPE_SCE},
00142 {1, TYPE_CPE},
00143 {2, TYPE_SCE, TYPE_CPE},
00144 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE},
00145 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE},
00146 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE},
00147 };
00148
00152 typedef struct BandCodingPath {
00153 int prev_idx;
00154 int codebook;
00155 int bits;
00156 } BandCodingPath;
00157
00161 typedef struct {
00162 PutBitContext pb;
00163 MDCTContext mdct1024;
00164 MDCTContext mdct128;
00165 DSPContext dsp;
00166 DECLARE_ALIGNED_16(FFTSample, output[2048]);
00167 int16_t* samples;
00168
00169 int samplerate_index;
00170
00171 ChannelElement *cpe;
00172 AACPsyContext psy;
00173 int last_frame;
00174 } AACEncContext;
00175
00180 static void put_audio_specific_config(AVCodecContext *avctx)
00181 {
00182 PutBitContext pb;
00183 AACEncContext *s = avctx->priv_data;
00184
00185 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
00186 put_bits(&pb, 5, 2);
00187 put_bits(&pb, 4, s->samplerate_index);
00188 put_bits(&pb, 4, avctx->channels);
00189
00190 put_bits(&pb, 1, 0);
00191 put_bits(&pb, 1, 0);
00192 put_bits(&pb, 1, 0);
00193 flush_put_bits(&pb);
00194 }
00195
00196 static av_cold int aac_encode_init(AVCodecContext *avctx)
00197 {
00198 AACEncContext *s = avctx->priv_data;
00199 int i;
00200
00201 avctx->frame_size = 1024;
00202
00203 for(i = 0; i < 16; i++)
00204 if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
00205 break;
00206 if(i == 16){
00207 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
00208 return -1;
00209 }
00210 if(avctx->channels > 6){
00211 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
00212 return -1;
00213 }
00214 s->samplerate_index = i;
00215
00216 dsputil_init(&s->dsp, avctx);
00217 ff_mdct_init(&s->mdct1024, 11, 0);
00218 ff_mdct_init(&s->mdct128, 8, 0);
00219
00220 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00221 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00222 ff_sine_window_init(ff_sine_1024, 1024);
00223 ff_sine_window_init(ff_sine_128, 128);
00224
00225 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
00226 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
00227 if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP,
00228 aac_chan_configs[avctx->channels-1][0], 0,
00229 swb_size_1024[i], ff_aac_num_swb_1024[i], swb_size_128[i], ff_aac_num_swb_128[i]) < 0){
00230 av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
00231 return -1;
00232 }
00233 avctx->extradata = av_malloc(2);
00234 avctx->extradata_size = 2;
00235 put_audio_specific_config(avctx);
00236 return 0;
00237 }
00238
00243 static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
00244 {
00245 int i;
00246
00247 put_bits(&s->pb, 1, 0);
00248 put_bits(&s->pb, 2, info->window_sequence[0]);
00249 put_bits(&s->pb, 1, info->use_kb_window[0]);
00250 if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
00251 put_bits(&s->pb, 6, info->max_sfb);
00252 put_bits(&s->pb, 1, 0);
00253 }else{
00254 put_bits(&s->pb, 4, info->max_sfb);
00255 for(i = 1; i < info->num_windows; i++)
00256 put_bits(&s->pb, 1, info->group_len[i]);
00257 }
00258 }
00259
00263 static int calculate_band_sign_bits(AACEncContext *s, SingleChannelElement *sce,
00264 int group_len, int start, int size)
00265 {
00266 int bits = 0;
00267 int i, w;
00268 for(w = 0; w < group_len; w++){
00269 for(i = 0; i < size; i++){
00270 if(sce->icoefs[start + i])
00271 bits++;
00272 }
00273 start += 128;
00274 }
00275 return bits;
00276 }
00277
00281 static void encode_pulses(AACEncContext *s, Pulse *pulse)
00282 {
00283 int i;
00284
00285 put_bits(&s->pb, 1, !!pulse->num_pulse);
00286 if(!pulse->num_pulse) return;
00287
00288 put_bits(&s->pb, 2, pulse->num_pulse - 1);
00289 put_bits(&s->pb, 6, pulse->start);
00290 for(i = 0; i < pulse->num_pulse; i++){
00291 put_bits(&s->pb, 5, pulse->pos[i]);
00292 put_bits(&s->pb, 4, pulse->amp[i]);
00293 }
00294 }
00295
00299 static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
00300 {
00301 int start, i, w, w2, wg;
00302
00303 w = 0;
00304 for(wg = 0; wg < sce->ics.num_window_groups; wg++){
00305 start = 0;
00306 for(i = 0; i < sce->ics.max_sfb; i++){
00307 if(sce->zeroes[w*16 + i]){
00308 start += sce->ics.swb_sizes[i];
00309 continue;
00310 }
00311 for(w2 = w; w2 < w + sce->ics.group_len[wg]; w2++){
00312 encode_band_coeffs(s, sce, start + w2*128,
00313 sce->ics.swb_sizes[i],
00314 sce->band_type[w*16 + i]);
00315 }
00316 start += sce->ics.swb_sizes[i];
00317 }
00318 w += sce->ics.group_len[wg];
00319 }
00320 }
00321
00325 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
00326 {
00327 int i, namelen, padbits;
00328
00329 namelen = strlen(name) + 2;
00330 put_bits(&s->pb, 3, TYPE_FIL);
00331 put_bits(&s->pb, 4, FFMIN(namelen, 15));
00332 if(namelen >= 15)
00333 put_bits(&s->pb, 8, namelen - 16);
00334 put_bits(&s->pb, 4, 0);
00335 padbits = 8 - (put_bits_count(&s->pb) & 7);
00336 align_put_bits(&s->pb);
00337 for(i = 0; i < namelen - 2; i++)
00338 put_bits(&s->pb, 8, name[i]);
00339 put_bits(&s->pb, 12 - padbits, 0);
00340 }
00341
00342 static av_cold int aac_encode_end(AVCodecContext *avctx)
00343 {
00344 AACEncContext *s = avctx->priv_data;
00345
00346 ff_mdct_end(&s->mdct1024);
00347 ff_mdct_end(&s->mdct128);
00348 ff_aac_psy_end(&s->psy);
00349 av_freep(&s->samples);
00350 av_freep(&s->cpe);
00351 return 0;
00352 }
00353
00354 AVCodec aac_encoder = {
00355 "aac",
00356 CODEC_TYPE_AUDIO,
00357 CODEC_ID_AAC,
00358 sizeof(AACEncContext),
00359 aac_encode_init,
00360 aac_encode_frame,
00361 aac_encode_end,
00362 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
00363 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00364 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
00365 };