*/
/**
- * @file aacenc.c
+ * @file libavcodec/aacenc.c
* AAC encoder
*/
* TODOs:
* psy model selection with some option
* add sane pulse detection
+ * add temporal noise shaping
***********************************/
#include "avcodec.h"
32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
};
-static const uint8_t *swb_size_1024[] = {
+static const uint8_t * const swb_size_1024[] = {
swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
};
-static const uint8_t *swb_size_128[] = {
+static const uint8_t * const swb_size_128[] = {
/* the last entry on the following row is swb_size_128_64 but is a
duplicate of swb_size_128_96 */
swb_size_128_96, swb_size_128_96, swb_size_128_96,
3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
};
-static const uint8_t* run_value_bits[2] = {
+static const uint8_t* const run_value_bits[2] = {
run_value_bits_long, run_value_bits_short
};
MDCTContext mdct1024; ///< long (1024 samples) frame transform context
MDCTContext mdct128; ///< short (128 samples) frame transform context
DSPContext dsp;
+ DECLARE_ALIGNED_16(FFTSample, output[2048]); ///< temporary buffer for MDCT input coefficients
+ int16_t* samples; ///< saved preprocessed input
+
+ int samplerate_index; ///< MPEG-4 samplerate index
+
+ ChannelElement *cpe; ///< channel elements
AACPsyContext psy; ///< psychoacoustic model context
int last_frame;
} AACEncContext;
return -1;
}
s->samplerate_index = i;
- s->swb_sizes1024 = swb_size_1024[i];
- s->swb_num1024 = ff_aac_num_swb_1024[i];
- s->swb_sizes128 = swb_size_128[i];
- s->swb_num128 = ff_aac_num_swb_128[i];
dsputil_init(&s->dsp, avctx);
ff_mdct_init(&s->mdct1024, 11, 0);
s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
- if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP, aac_chan_configs[avctx->channels-1][0], 0, s->swb_sizes1024, s->swb_num1024, s->swb_sizes128, s->swb_num128) < 0){
+ if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP,
+ aac_chan_configs[avctx->channels-1][0], 0,
+ swb_size_1024[i], ff_aac_num_swb_1024[i], swb_size_128[i], ff_aac_num_swb_128[i]) < 0){
av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
return -1;
}
* Encode ics_info element.
* @see Table 4.6 (syntax of ics_info)
*/
-static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
+static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
{
- AACEncContext *s = avctx->priv_data;
int i;
put_bits(&s->pb, 1, 0); // ics_reserved bit
}
}
+/**
+ * Calculate the number of bits needed to code all coefficient signs in current band.
+ */
+static int calculate_band_sign_bits(AACEncContext *s, SingleChannelElement *sce,
+ int group_len, int start, int size)
+{
+ int bits = 0;
+ int i, w;
+ for(w = 0; w < group_len; w++){
+ for(i = 0; i < size; i++){
+ if(sce->icoefs[start + i])
+ bits++;
+ }
+ start += 128;
+ }
+ return bits;
+}
+
/**
* Encode pulse data.
*/
-static void encode_pulses(AVCodecContext *avctx, AACEncContext *s, Pulse *pulse, int channel)
+static void encode_pulses(AACEncContext *s, Pulse *pulse)
{
int i;
/**
* Encode spectral coefficients processed by psychoacoustic model.
*/
-static void encode_spectral_coeffs(AVCodecContext *avctx, AACEncContext *s, ChannelElement *cpe, int channel)
+static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
{
int start, i, w, w2, wg;
w = 0;
- for(wg = 0; wg < cpe->ch[channel].ics.num_window_groups; wg++){
+ for(wg = 0; wg < sce->ics.num_window_groups; wg++){
start = 0;
- for(i = 0; i < cpe->ch[channel].ics.max_sfb; i++){
- if(cpe->ch[channel].zeroes[w*16 + i]){
- start += cpe->ch[channel].ics.swb_sizes[i];
+ for(i = 0; i < sce->ics.max_sfb; i++){
+ if(sce->zeroes[w*16 + i]){
+ start += sce->ics.swb_sizes[i];
continue;
}
- for(w2 = w; w2 < w + cpe->ch[channel].ics.group_len[wg]; w2++){
- encode_band_coeffs(s, cpe, channel, start + w2*128, cpe->ch[channel].ics.swb_sizes[i], cpe->ch[channel].band_type[w*16 + i]);
+ for(w2 = w; w2 < w + sce->ics.group_len[wg]; w2++){
+ encode_band_coeffs(s, sce, start + w2*128,
+ sce->ics.swb_sizes[i],
+ sce->band_type[w*16 + i]);
}
- start += cpe->ch[channel].ics.swb_sizes[i];
+ start += sce->ics.swb_sizes[i];
}
- w += cpe->ch[channel].ics.group_len[wg];
+ w += sce->ics.group_len[wg];
}
}