[FFmpeg-devel] [PATCH] AAC Encoder, Round 2

Kostya kostya.shishkov
Sat Aug 23 17:31:30 CEST 2008


I'm back (feeling even worse than before but nm).

Here is $subj is in a form of diff against FFmpeg SVN.
-------------- next part --------------
Index: aacenc.c
===================================================================
--- aacenc.c	(revision 14920)
+++ aacenc.c	(working copy)
@@ -28,6 +28,7 @@
  *              TODOs:
  * psy model selection with some option
  * add sane pulse detection
+ * add temporal noise shaping
  ***********************************/
 
 #include "avcodec.h"
@@ -118,6 +119,29 @@
     swb_size_128_16, swb_size_128_16, swb_size_128_8
 };
 
+/** spectral coefficients codebook information */
+static const struct {
+    int16_t maxval;         ///< maximum possible value
+     int8_t range;          ///< value used in vector calculation
+} aac_cb_info[] = {
+    {    0, -1 }, // zero codebook
+    {    1,  3 },
+    {    1,  3 },
+    {    2,  3 },
+    {    2,  3 },
+    {    4,  9 },
+    {    4,  9 },
+    {    7,  8 },
+    {    7,  8 },
+    {   12, 13 },
+    {   12, 13 },
+    { 8191, 17 },
+    {   -1, -1 }, // reserved
+    {   -1, -1 }, // perceptual noise substitution
+    {   -1, -1 }, // intensity out-of-phase
+    {   -1, -1 }, // intensity in-phase
+};
+
 /** bits needed to code codebook run value for long windows */
 static const uint8_t run_value_bits_long[64] = {
      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
@@ -211,10 +235,6 @@
         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);
@@ -229,7 +249,7 @@
     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){
+                       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;
     }
@@ -239,14 +259,65 @@
     return 0;
 }
 
+static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
+                                  SingleChannelElement *sce, short *audio, int channel)
+{
+    int i, j, k;
+    const float * lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
+    const float * swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
+    const float * pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
+
+    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
+        memcpy(s->output, sce->saved, sizeof(float)*1024);
+        if(sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE){
+            memset(s->output, 0, sizeof(s->output[0]) * 448);
+            for(i = 448; i < 576; i++)
+                s->output[i] = sce->saved[i] * pwindow[i - 448];
+            for(i = 576; i < 704; i++)
+                s->output[i] = sce->saved[i];
+        }
+        if(sce->ics.window_sequence[0] != LONG_START_SEQUENCE){
+            j = channel;
+            for (i = 0; i < 1024; i++, j += avctx->channels){
+                s->output[i+1024]         = audio[j] * lwindow[1024 - i - 1];
+                sce->saved[i] = audio[j] * lwindow[i];
+            }
+        }else{
+            j = channel;
+            for(i = 0; i < 448; i++, j += avctx->channels)
+                s->output[i+1024]         = audio[j];
+            for(i = 448; i < 576; i++, j += avctx->channels)
+                s->output[i+1024]         = audio[j] * swindow[576 - i - 1];
+            memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448);
+            j = channel;
+            for(i = 0; i < 1024; i++, j += avctx->channels)
+                sce->saved[i] = audio[j];
+        }
+        ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output);
+    }else{
+        j = channel;
+        for (k = 0; k < 1024; k += 128) {
+            for(i = 448 + k; i < 448 + k + 256; i++)
+                s->output[i - 448 - k] = (i < 1024)
+                                         ? sce->saved[i]
+                                         : audio[channel + (i-1024)*avctx->channels];
+            s->dsp.vector_fmul        (s->output,     k ?  swindow : pwindow, 128);
+            s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128);
+            ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output);
+        }
+        j = channel;
+        for(i = 0; i < 1024; i++, j += avctx->channels)
+            sce->saved[i] = audio[j];
+    }
+}
+
 /**
  * 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;
+    int wg;
 
     put_bits(&s->pb, 1, 0);                // ics_reserved bit
     put_bits(&s->pb, 2, info->window_sequence[0]);
@@ -256,12 +327,294 @@
         put_bits(&s->pb, 1, 0);            // no prediction
     }else{
         put_bits(&s->pb, 4, info->max_sfb);
-        for(i = 1; i < info->num_windows; i++)
-            put_bits(&s->pb, 1, info->group_len[i]);
+        for(wg = 0; wg < info->num_window_groups; wg++){
+            if(wg)
+                put_bits(&s->pb, 1, 0);
+            if(info->group_len[wg] > 1)
+                put_sbits(&s->pb, info->group_len[wg] - 1, 0xFF);
+        }
     }
 }
 
 /**
+ * Encode MS data.
+ * @see 4.6.8.1 "Joint Coding - M/S Stereo"
+ */
+static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
+{
+    int i, w, wg;
+
+    put_bits(pb, 2, cpe->ms_mode);
+    if(cpe->ms_mode == 1){
+        w = 0;
+        for(wg = 0; wg < cpe->ch[0].ics.num_window_groups; wg++){
+            for(i = 0; i < cpe->ch[0].ics.max_sfb; i++)
+                put_bits(pb, 1, cpe->ms_mask[w + i]);
+            w += cpe->ch[0].ics.group_len[wg]*16;
+        }
+    }
+}
+
+/**
+ * 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;
+}
+
+/**
+ * Calculate the number of bits needed to code given band with given codebook.
+ *
+ * @param s         encoder context
+ * @param sce       channel element
+ * @param group_len window group length
+ * @param start     scalefactor band position in spectral coefficients
+ * @param size      scalefactor band size
+ * @param cb        codebook number
+ */
+static int calculate_band_bits(AACEncContext *s, SingleChannelElement *sce,
+                               int group_len, int start, int size, int cb)
+{
+    int i, j, w;
+    int bits = 0, dim, idx;
+    int range = aac_cb_info[cb].range;
+
+    if(range == -1) return 0;
+    cb--;
+    dim = cb < FIRST_PAIR_BT ? 4 : 2;
+
+    if(IS_CODEBOOK_UNSIGNED(cb)){
+        int coef_abs[2];
+        for(w = 0; w < group_len; w++){
+            for(i = 0; i < size; i += dim){
+                idx = 0;
+                for(j = 0; j < dim; j++){
+                    coef_abs[j] = FFABS(sce->icoefs[start+i+j]);
+                    idx = idx * range + FFMIN(coef_abs[j], 16);
+                    if(cb == ESC_BT && coef_abs[j] > 15)
+                        bits += av_log2(coef_abs[j]) * 2 - 4 + 1;
+                }
+                bits += ff_aac_spectral_bits[cb][idx];
+            }
+            start += 128;
+        }
+    }else{
+        for(w = 0; w < group_len; w++){
+            for(i = 0; i < size; i += dim){
+                idx = sce->icoefs[start+i];
+                for(j = 1; j < dim; j++)
+                    idx = idx * range + sce->icoefs[start+i+j];
+                //it turned out that all signed codebooks use the same offset for index coding
+                idx += 40;
+                bits += ff_aac_spectral_bits[cb][idx];
+            }
+            start += 128;
+        }
+    }
+    return bits;
+}
+
+/**
+ * Encode band info for single window group bands.
+ */
+static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
+                                     int win, int group_len)
+{
+    BandCodingPath path[64];
+    int band_bits[64][12];
+    int maxval;
+    int w, swb, cb, start, start2, size;
+    int i, j;
+    const int max_sfb = sce->ics.max_sfb;
+    const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
+    const int run_esc = (1 << run_bits) - 1;
+    int bits, sbits, idx, count;
+    int stack[64], stack_len;
+
+    start = win*128;
+    for(swb = 0; swb < max_sfb; swb++){
+        maxval = 0;
+        start2 = start;
+        size = sce->ics.swb_sizes[swb];
+        if(sce->zeroes[win*16 + swb])
+            maxval = 0;
+        else{
+            for(w = 0; w < group_len; w++){
+                for(i = start2; i < start2 + size; i++){
+                    maxval = FFMAX(maxval, FFABS(sce->icoefs[i]));
+                }
+                start2 += 128;
+            }
+        }
+        sbits = calculate_band_sign_bits(s, sce, group_len, start, size);
+        for(cb = 0; cb < 12; cb++){
+            if(aac_cb_info[cb].maxval < maxval)
+                band_bits[swb][cb] = INT_MAX;
+            else{
+                band_bits[swb][cb] = calculate_band_bits(s, sce, group_len, start, size, cb);
+                if(IS_CODEBOOK_UNSIGNED(cb-1)){
+                    band_bits[swb][cb] += sbits;
+                }
+            }
+        }
+        start += sce->ics.swb_sizes[swb];
+    }
+    path[0].bits = 0;
+    for(i = 1; i <= max_sfb; i++)
+        path[i].bits = INT_MAX;
+    for(i = 0; i < max_sfb; i++){
+        for(cb = 0; cb < 12; cb++){
+            int sum = 0;
+            for(j = 1; j <= max_sfb - i; j++){
+                if(band_bits[i+j-1][cb] == INT_MAX)
+                    break;
+                sum += band_bits[i+j-1][cb];
+                bits = sum + path[i].bits + run_value_bits[sce->ics.num_windows == 8][j];
+                if(bits < path[i+j].bits){
+                    path[i+j].bits     = bits;
+                    path[i+j].codebook = cb;
+                    path[i+j].prev_idx = i;
+                }
+            }
+        }
+    }
+    assert(path[max_sfb].bits != INT_MAX);
+
+    //convert resulting path from backward-linked list
+    stack_len = 0;
+    idx = max_sfb;
+    while(idx > 0){
+        stack[stack_len++] = idx;
+        idx = path[idx].prev_idx;
+    }
+
+    //perform actual band info encoding
+    start = 0;
+    for(i = stack_len - 1; i >= 0; i--){
+        put_bits(&s->pb, 4, path[stack[i]].codebook);
+        count = stack[i] - path[stack[i]].prev_idx;
+        memset(sce->zeroes + win*16 + start, !path[stack[i]].codebook, count);
+        //XXX: memset when band_type is also uint8_t
+        for(j = 0; j < count; j++){
+            sce->band_type[win*16 + start] =  path[stack[i]].codebook;
+            start++;
+        }
+        while(count >= run_esc){
+            put_bits(&s->pb, run_bits, run_esc);
+            count -= run_esc;
+        }
+        put_bits(&s->pb, run_bits, count);
+    }
+}
+
+/**
+ * Encode the coefficients of one scalefactor band with selected codebook.
+ */
+static void encode_band_coeffs(AACEncContext *s, SingleChannelElement *sce,
+                               int start, int size, int cb)
+{
+    const uint8_t  *bits  = ff_aac_spectral_bits [cb - 1];
+    const uint16_t *codes = ff_aac_spectral_codes[cb - 1];
+    const int range = aac_cb_info[cb].range;
+    const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2;
+    int i, j, idx;
+
+    //do not encode zero or special codebooks
+    if(range == -1) return;
+
+    if(cb == ESC_BT){
+        int coef_abs[2];
+        for(i = start; i < start + size; i += 2){
+            idx = 0;
+            for(j = 0; j < 2; j++){
+                coef_abs[j] = FFABS(sce->icoefs[i+j]);
+                idx = idx*17 + FFMIN(coef_abs[j], 16);
+            }
+            put_bits(&s->pb, bits[idx], codes[idx]);
+            //output signs
+            for(j = 0; j < 2; j++)
+                if(sce->icoefs[i+j])
+                    put_bits(&s->pb, 1, sce->icoefs[i+j] < 0);
+            //output escape values
+            for(j = 0; j < 2; j++)
+                if(coef_abs[j] > 15){
+                    int len = av_log2(coef_abs[j]);
+
+                    put_bits(&s->pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
+                    put_bits(&s->pb, len, coef_abs[j] & ((1 << len) - 1));
+                }
+        }
+    }else if(IS_CODEBOOK_UNSIGNED(cb)){
+        for(i = start; i < start + size; i += dim){
+            idx = FFABS(sce->icoefs[i]);
+            for(j = 1; j < dim; j++)
+                idx = idx * range + FFABS(sce->icoefs[i+j]);
+            put_bits(&s->pb, bits[idx], codes[idx]);
+            //output signs
+            for(j = 0; j < dim; j++)
+                if(sce->icoefs[i+j])
+                    put_bits(&s->pb, 1, sce->icoefs[i+j] < 0);
+        }
+    }else{
+        for(i = start; i < start + size; i += dim){
+            idx = sce->icoefs[i];
+            for(j = 1; j < dim; j++)
+                idx = idx * range + sce->icoefs[i+j];
+            //it turned out that all signed codebooks use the same offset for index coding
+            idx += 40;
+            put_bits(&s->pb, bits[idx], codes[idx]);
+        }
+    }
+}
+
+/**
+ * Encode scalefactor band coding type.
+ */
+static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
+{
+    int w, wg;
+
+    w = 0;
+    for(wg = 0; wg < sce->ics.num_window_groups; wg++){
+        encode_window_bands_info(s, sce, w, sce->ics.group_len[wg]);
+        w += sce->ics.group_len[wg];
+    }
+}
+
+/**
+ * Encode scalefactors.
+ */
+static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, int global_gain)
+{
+    int off = global_gain, diff;
+    int i, w, wg;
+
+    w = 0;
+    for(wg = 0; wg < sce->ics.num_window_groups; wg++){
+        for(i = 0; i < sce->ics.max_sfb; i++){
+            if(!sce->zeroes[w*16 + i]){
+                diff = sce->sf_idx[w*16 + i] - off + SCALE_DIFF_ZERO;
+                if(diff < 0 || diff > 120) av_log(avctx, AV_LOG_ERROR, "Scalefactor difference is too big to be coded\n");
+                off = sce->sf_idx[w*16 + i];
+                put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
+            }
+        }
+        w += sce->ics.group_len[wg];
+    }
+}
+
+/**
  * Encode pulse data.
  */
 static void encode_pulses(AACEncContext *s, Pulse *pulse)
@@ -295,7 +648,7 @@
                 continue;
             }
             for(w2 = w; w2 < w + sce->ics.group_len[wg]; w2++){
-                encode_band_coeffs(s, cpe, channel, start + w2*128,
+                encode_band_coeffs(s, sce, start + w2*128,
                                    sce->ics.swb_sizes[i],
                                    sce->band_type[w*16 + i]);
             }
@@ -306,6 +659,44 @@
 }
 
 /**
+ * Encode one channel of audio data.
+ */
+static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, int common_window)
+{
+    int g, w, wg;
+    int global_gain = 0, last = 256;
+
+    //determine global gain as standard recommends - the first scalefactor value
+    w = 0;
+    for(wg = 0; wg < sce->ics.num_window_groups; wg++){
+        for(g = sce->ics.max_sfb - 1; g >= 0; g--){
+            if(sce->sf_idx[w + g] == 256)
+                sce->sf_idx[w + g] = last;
+            else
+                last = sce->sf_idx[w + g];
+        }
+        for(g = 0; g < sce->ics.max_sfb; g++){
+            if(sce->sf_idx[w + g] == 256)
+                sce->sf_idx[w + g] = last;
+            else
+                last = sce->sf_idx[w + g];
+        }
+        w += sce->ics.group_len[wg]*16;
+    }
+    global_gain = last;
+
+    put_bits(&s->pb, 8, global_gain);
+    if(!common_window) put_ics_info(s, &sce->ics);
+    encode_band_info(s, sce);
+    encode_scale_factors(avctx, s, sce, global_gain);
+    encode_pulses(s, &sce->pulse);
+    put_bits(&s->pb, 1, 0); //tns
+    put_bits(&s->pb, 1, 0); //ssr
+    encode_spectral_coeffs(s, sce);
+    return 0;
+}
+
+/**
  * Write some auxiliary information about the created AAC file.
  */
 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
@@ -325,6 +716,80 @@
     put_bits(&s->pb, 12 - padbits, 0);
 }
 
+static int aac_encode_frame(AVCodecContext *avctx,
+                            uint8_t *frame, int buf_size, void *data)
+{
+    AACEncContext *s = avctx->priv_data;
+    int16_t *samples = s->samples, *samples2, *la;
+    ChannelElement *cpe;
+    int i, j, chans, tag, start_ch;
+    const uint8_t *chan_map = aac_chan_configs[avctx->channels-1];
+    int chan_el_counter[4];
+
+    if(s->last_frame)
+        return 0;
+    if(data){
+        if((s->psy.flags & PSY_MODEL_NO_PREPROC) == PSY_MODEL_NO_PREPROC){
+            memcpy(s->samples + 1024 * avctx->channels, data, 1024 * avctx->channels * sizeof(s->samples[0]));
+        }else{
+            start_ch = 0;
+            samples2 = s->samples + 1024 * avctx->channels;
+            for(i = 0; i < chan_map[0]; i++){
+                tag = chan_map[i+1];
+                chans = tag == TYPE_CPE ? 2 : 1;
+                ff_aac_psy_preprocess(&s->psy, (uint16_t*)data + start_ch, samples2 + start_ch, i, tag);
+                start_ch += chans;
+            }
+        }
+    }
+    if(!avctx->frame_number){
+        memcpy(s->samples, s->samples + 1024 * avctx->channels, 1024 * avctx->channels * sizeof(s->samples[0]));
+        return 0;
+    }
+
+    init_put_bits(&s->pb, frame, buf_size*8);
+    if((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT)){
+        put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
+    }
+    start_ch = 0;
+    memset(chan_el_counter, 0, sizeof(chan_el_counter));
+    for(i = 0; i < chan_map[0]; i++){
+        tag = chan_map[i+1];
+        chans = tag == TYPE_CPE ? 2 : 1;
+        cpe = &s->cpe[i];
+        samples2 = samples + start_ch;
+        la = samples2 + 1024 * avctx->channels + start_ch;
+        if(!data) la = NULL;
+        ff_aac_psy_suggest_window(&s->psy, samples2, la, i, tag, cpe);
+        for(j = 0; j < chans; j++){
+            apply_window_and_mdct(avctx, s, &cpe->ch[j], samples2, j);
+        }
+        ff_aac_psy_analyze(&s->psy, i, tag, cpe);
+        put_bits(&s->pb, 3, tag);
+        put_bits(&s->pb, 4, chan_el_counter[tag]++);
+        if(chans == 2){
+            put_bits(&s->pb, 1, cpe->common_window);
+            if(cpe->common_window){
+                put_ics_info(s, &cpe->ch[0].ics);
+                encode_ms_info(&s->pb, cpe);
+            }
+        }
+        for(j = 0; j < chans; j++){
+            encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
+        }
+        start_ch += chans;
+    }
+
+    put_bits(&s->pb, 3, TYPE_END);
+    flush_put_bits(&s->pb);
+    avctx->frame_bits = put_bits_count(&s->pb);
+
+    if(!data)
+        s->last_frame = 1;
+    memcpy(s->samples, s->samples + 1024 * avctx->channels, 1024 * avctx->channels * sizeof(s->samples[0]));
+    return put_bits_count(&s->pb)>>3;
+}
+
 static av_cold int aac_encode_end(AVCodecContext *avctx)
 {
     AACEncContext *s = avctx->priv_data;
Index: aac.h
===================================================================
--- aac.h	(revision 14920)
+++ aac.h	(working copy)
@@ -133,6 +133,12 @@
     AFTER_IMDCT = 3,
 };
 
+#define SCALE_DIV_512    36    ///< scalefactor difference that corresponds to scale difference in 512 times
+#define SCALE_ONE_POS   140    ///< scalefactor index that corresponds to scale=1.0
+#define SCALE_MAX_POS   255    ///< scalefactor index maximum value
+#define SCALE_MAX_DIFF   60    ///< maximum scalefactor difference allowed by standard
+#define SCALE_DIFF_ZERO  60    ///< codebook index corresponding to zero scalefactor indices difference
+
 /**
  * Individual Channel Stream
  */
@@ -143,6 +149,7 @@
     int num_window_groups;
     uint8_t group_len[8];
     const uint16_t *swb_offset; ///< table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular window
+    const uint8_t *swb_sizes;   ///< table of scalefactor band sizes for a particular window
     int num_swb;                ///< number of scalefactor window bands
     int num_windows;
     int tns_max_bands;
@@ -178,6 +185,7 @@
 
 typedef struct {
     int num_pulse;
+    int start;
     int pos[4];
     int amp[4];
 } Pulse;
@@ -202,12 +210,16 @@
 typedef struct {
     IndividualChannelStream ics;
     TemporalNoiseShaping tns;
-    enum BandType band_type[120];             ///< band types
+    Pulse pulse;
+    enum BandType band_type[128];             ///< band types
     int band_type_run_end[120];               ///< band type run end points
     float sf[120];                            ///< scalefactors
+    int sf_idx[128];                          ///< scalefactor indices (used by encoder)
+    uint8_t zeroes[128];                      ///< band is not coded (used by encoder)
     DECLARE_ALIGNED_16(float, coeffs[1024]);  ///< coefficients for IMDCT
-    DECLARE_ALIGNED_16(float, saved[512]);    ///< overlap
+    DECLARE_ALIGNED_16(float, saved[1024]);   ///< overlap
     DECLARE_ALIGNED_16(float, ret[1024]);     ///< PCM output
+    DECLARE_ALIGNED_16(int,   icoefs[1024]);  ///< integer coefficients for encoding
 } SingleChannelElement;
 
 /**
@@ -215,6 +227,8 @@
  */
 typedef struct {
     // CPE specific
+    int common_window;        ///< Set if channels share a common 'IndividualChannelStream' in bitstream.
+    int     ms_mode;          ///< Signals mid/side stereo flags coding mode (used by encoder)
     uint8_t ms_mask[120];     ///< Set if mid/side stereo is used for each scalefactor window band
     // shared
     SingleChannelElement ch[2];
Index: aacpsy.c
===================================================================
--- aacpsy.c	(revision 14920)
+++ aacpsy.c	(working copy)
@@ -51,6 +51,23 @@
     return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191);
 }
 
+/**
+ * Convert coefficients to integers.
+ * @return sum of coefficient absolute values
+ */
+static inline int quantize_coeffs(float *in, int *out, int size, int scale_idx)
+{
+    int i, sign, sum = 0;
+    const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    for(i = 0; i < size; i++){
+        sign = in[i] > 0.0;
+        out[i] = quant(in[i], Q);
+        sum += out[i];
+        if(sign) out[i] = -out[i];
+    }
+    return sum;
+}
+
 static inline float get_approximate_quant_error(float *c, int size, int scale_idx)
 {
     int i;
@@ -68,11 +85,87 @@
 }
 
 /**
+ * Produce integer coefficients from scalefactors provided by the model.
+ */
+static void psy_create_output(AACPsyContext *apc, ChannelElement *cpe, int chans)
+{
+    int i, w, w2, wg, g, ch;
+    int start, sum, maxsfb, cmaxsfb;
+
+    for(ch = 0; ch < chans; ch++){
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        start = 0;
+        maxsfb = 0;
+        cpe->ch[ch].pulse.num_pulse = 0;
+        for(w = 0; w < ics->num_windows*16; w += 16){
+            for(g = 0; g < ics->num_swb; g++){
+                sum = 0;
+                //apply M/S
+                if(!ch && cpe->ms_mask[w + g]){
+                    for(i = 0; i < ics->swb_sizes[g]; i++){
+                        cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0;
+                        cpe->ch[1].coeffs[start+i] =  cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
+                    }
+                }
+                if(!cpe->ch[ch].zeroes[w + g])
+                    sum = quantize_coeffs(cpe->ch[ch].coeffs + start,
+                                          cpe->ch[ch].icoefs + start,
+                                          ics->swb_sizes[g],
+                                          cpe->ch[ch].sf_idx[w + g]);
+                else
+                    memset(cpe->ch[ch].icoefs + start, 0, ics->swb_sizes[g] * sizeof(cpe->ch[0].icoefs[0]));
+                cpe->ch[ch].zeroes[w + g] = !sum;
+                start += ics->swb_sizes[g];
+            }
+            for(cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w+cmaxsfb-1]; cmaxsfb--);
+            maxsfb = FFMAX(maxsfb, cmaxsfb);
+        }
+        ics->max_sfb = maxsfb;
+
+        //adjust zero bands for window groups
+        w = 0;
+        for(wg = 0; wg < ics->num_window_groups; wg++){
+            for(g = 0; g < ics->max_sfb; g++){
+                i = 1;
+                for(w2 = 0; w2 < ics->group_len[wg]*16; w2 += 16){
+                    if(!cpe->ch[ch].zeroes[w + w2 + g]){
+                        i = 0;
+                        break;
+                    }
+                }
+                cpe->ch[ch].zeroes[w + g] = i;
+            }
+            w += ics->group_len[wg] * 16;
+        }
+    }
+
+    if(chans > 1 && cpe->common_window){
+        IndividualChannelStream *ics0 = &cpe->ch[0].ics;
+        IndividualChannelStream *ics1 = &cpe->ch[1].ics;
+        int msc = 0;
+        ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
+        ics1->max_sfb = ics0->max_sfb;
+        for(w = 0; w < ics0->num_windows*16; w += 16)
+            for(i = 0; i < ics0->max_sfb; i++)
+                if(cpe->ms_mask[w+i]) msc++;
+        if(msc == 0 || ics0->max_sfb == 0) cpe->ms_mode = 0;
+        else cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2;
+    }
+}
+
+/**
  * constants for 3GPP AAC psychoacoustic model
  * @{
  */
+#define PSY_3GPP_C1 3.0f                    // log2(8.0)
+#define PSY_3GPP_C2 1.32192809488736234787f // log2(2.5)
+#define PSY_3GPP_C3 0.55935730170421255071f // 1 - C2/C1
+
 #define PSY_3GPP_SPREAD_LOW  1.5f // spreading factor for ascending threshold spreading  (15 dB/Bark)
 #define PSY_3GPP_SPREAD_HI   3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
+
+#define PSY_3GPP_RPEMIN      0.01f
+#define PSY_3GPP_RPELEV      2.0f
 /**
  * @}
  */
@@ -83,9 +176,33 @@
 typedef struct Psy3gppBand{
     float energy;    ///< band energy
     float ffac;      ///< form factor
+    float thr;       ///< energy threshold
+    float pe;        ///< perceptual entropy
+    float a;         ///< constant part in perceptual entropy
+    float b;         ///< variable part in perceptual entropy
+    float nl;        ///< predicted number of lines left after quantization
+    float min_snr;   ///< minimal SNR
+    float thr_quiet; ///< threshold in quiet
 }Psy3gppBand;
 
 /**
+ * single/pair channel context for psychoacoustic model
+ */
+typedef struct Psy3gppChannel{
+    float       a[2];                       ///< parameter used for perceptual entropy - constant part
+    float       b[2];                       ///< parameter used for perceptual entropy - variable part
+    float       pe[2];                      ///< channel perceptual entropy
+    float       thr[2];                     ///< channel thresholds sum
+    Psy3gppBand band[2][128];               ///< bands information
+    Psy3gppBand prev_band[2][128];          ///< bands information from the previous frame
+
+    float       win_energy[2];              ///< sliding average of channel energy
+    float       iir_state[2][2];            ///< hi-pass IIR filter state
+    uint8_t     next_grouping[2];           ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
+    enum WindowSequence next_window_seq[2]; ///< window sequence to be used in the next frame
+}Psy3gppChannel;
+
+/**
  * psychoacoustic model frame type-dependent coefficients
  */
 typedef struct Psy3gppCoeffs{
@@ -96,9 +213,617 @@
 }Psy3gppCoeffs;
 
 /**
+ * 3GPP TS26.403-inspired psychoacoustic model specific data
+ */
+typedef struct Psy3gppContext{
+    Psy3gppCoeffs psy_coef[2];
+    int         reservoir;    ///< bit reservoir fullness
+    int         avg_bits;     ///< average frame size of bits for CBR
+    Psy3gppChannel *ch;
+}Psy3gppContext;
+
+/**
  * Calculate Bark value for given line.
  */
 static inline float calc_bark(float f)
 {
     return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
 }
+
+#define ATH_ADD 4
+/**
+ * Calculate ATH value for given frequency.
+ * Borrowed from Lame.
+ */
+static inline float ath(float f, float add)
+{
+    f /= 1000.0f;
+    return   3.64 * pow(f, -0.8)
+            - 6.8  * exp(-0.6  * (f - 3.4) * (f - 3.4))
+            + 6.0  * exp(-0.15 * (f - 8.7) * (f - 8.7))
+            + (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
+}
+
+static av_cold int psy_3gpp_init(AACPsyContext *apc, int elements)
+{
+    Psy3gppContext *pctx;
+    float barks[1024];
+    int i, j, g, start;
+    float prev, minscale, minath;
+    apc->model_priv_data = av_mallocz(sizeof(Psy3gppContext));
+    pctx = (Psy3gppContext*) apc->model_priv_data;
+
+    for(i = 0; i < 1024; i++)
+        barks[i] = calc_bark(i * apc->avctx->sample_rate / 2048.0);
+    minath = ath(3410, ATH_ADD);
+    for(j = 0; j < 2; j++){
+        Psy3gppCoeffs *coeffs = &pctx->psy_coef[j];
+        int bands = j ? apc->num_bands128 : apc->num_bands1024;
+        i = 0;
+        prev = 0.0;
+        for(g = 0; g < bands; g++){
+            i += j ? apc->bands128[g] : apc->bands1024[g];
+            coeffs->barks[g] = (barks[i - 1] + prev) / 2.0;
+            prev = barks[i - 1];
+        }
+        for(g = 0; g < bands - 1; g++){
+            coeffs->spread_low[g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_LOW);
+            coeffs->spread_hi [g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_HI);
+        }
+        start = 0;
+        for(g = 0; g < bands; g++){
+            int size = j ? apc->bands128[g] : apc->bands1024[g];
+            minscale = ath(apc->avctx->sample_rate * start / 1024.0, ATH_ADD);
+            for(i = 1; i < size; i++){
+                minscale = fminf(minscale, ath(apc->avctx->sample_rate * (start + i) / 1024.0 / 2.0, ATH_ADD));
+            }
+            coeffs->ath[g] = minscale - minath;
+            start += size;
+        }
+    }
+
+    pctx->avg_bits = apc->avctx->bit_rate * 1024 / apc->avctx->sample_rate;
+    pctx->ch = av_mallocz(sizeof(Psy3gppChannel) * elements);
+    return 0;
+}
+
+/**
+ * IIR filter used in block switching decision
+ */
+static float iir_filter(int in, float state[2])
+{
+    float ret;
+
+    ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
+    state[0] = in;
+    state[1] = ret;
+    return ret;
+}
+
+/**
+ * window grouping information stored as bits (0 - new group, 1 - group continues)
+ */
+static const uint8_t window_grouping[9] = {
+    0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
+};
+
+/**
+ * Tell encoder which window types to use.
+ * @see 3GPP TS26.403 5.4.1 "Blockswitching"
+ */
+static void psy_3gpp_window(AACPsyContext *apc, int16_t *audio, int16_t *la,
+                            int tag, int type, ChannelElement *cpe)
+{
+    int ch;
+    int chans = type == TYPE_CPE ? 2 : 1;
+    int i, j;
+    int br = apc->avctx->bit_rate / apc->avctx->channels;
+    int attack_ratio = (br <= 16000 + 8000*chans) ? 18 : 10;
+    Psy3gppContext *pctx = (Psy3gppContext*) apc->model_priv_data;
+    Psy3gppChannel *pch = &pctx->ch[tag];
+    uint8_t grouping[2];
+    enum WindowSequence win[2];
+    IndividualChannelStream *ics0 = &cpe->ch[0].ics, *ics1 = &cpe->ch[1].ics;
+
+    if(la && !(apc->flags & PSY_MODEL_NO_SWITCH)){
+        float s[8], v;
+        for(ch = 0; ch < chans; ch++){
+            enum WindowSequence last_window_sequence = cpe->ch[ch].ics.window_sequence[0];
+            int switch_to_eight = 0;
+            float sum = 0.0, sum2 = 0.0;
+            int attack_n = 0;
+            for(i = 0; i < 8; i++){
+                for(j = 0; j < 128; j++){
+                    v = iir_filter(audio[(i*128+j)*apc->avctx->channels+ch], pch->iir_state[ch]);
+                    sum += v*v;
+                }
+                s[i] = sum;
+                sum2 += sum;
+            }
+            for(i = 0; i < 8; i++){
+                if(s[i] > pch->win_energy[ch] * attack_ratio){
+                    attack_n = i + 1;
+                    switch_to_eight = 1;
+                    break;
+                }
+            }
+            pch->win_energy[ch] = pch->win_energy[ch]*7/8 + sum2/64;
+
+            switch(last_window_sequence){
+            case ONLY_LONG_SEQUENCE:
+                win[ch] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
+                grouping[ch] = 0;
+                break;
+            case LONG_START_SEQUENCE:
+                win[ch] = EIGHT_SHORT_SEQUENCE;
+                grouping[ch] = pch->next_grouping[ch];
+                break;
+            case LONG_STOP_SEQUENCE:
+                win[ch] = ONLY_LONG_SEQUENCE;
+                grouping[ch] = 0;
+                break;
+            case EIGHT_SHORT_SEQUENCE:
+                win[ch] = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
+                grouping[ch] = switch_to_eight ? pch->next_grouping[ch] : 0;
+                break;
+            }
+            pch->next_grouping[ch] = window_grouping[attack_n];
+        }
+    }else{
+        for(ch = 0; ch < chans; ch++){
+            IndividualChannelStream *ics = &cpe->ch[ch].ics;
+            win[ch] = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
+                      ? EIGHT_SHORT_SEQUENCE
+                      : ONLY_LONG_SEQUENCE;
+            grouping[ch] = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
+        }
+    }
+
+    for(ch = 0; ch < chans; ch++){
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        ics->window_sequence[0] = win[ch];
+        ics->use_kb_window[0] = 1;
+        if(win[ch] != EIGHT_SHORT_SEQUENCE){
+            ics->num_windows = 1;
+            ics->swb_sizes = apc->bands1024;
+            ics->num_swb = apc->num_bands1024;
+            ics->num_window_groups = 1;
+            ics->group_len[0] = 1;
+        }else{
+            ics->num_windows = 8;
+            ics->swb_sizes = apc->bands128;
+            ics->num_swb = apc->num_bands128;
+            ics->num_window_groups = 0;
+            ics->group_len[0] = 1;
+            for(i = 0; i < 8; i++){
+                if((grouping[ch] >> i) & 1){
+                    ics->group_len[ics->num_window_groups - 1]++;
+                }else{
+                    ics->num_window_groups++;
+                    ics->group_len[ics->num_window_groups - 1] = 1;
+                }
+            }
+        }
+    }
+    cpe->common_window = 0;
+    if(chans > 1
+            && ics0->window_sequence[0] == ics1->window_sequence[0]
+            && ics0->use_kb_window[0]   == ics1->use_kb_window[0]
+            && !(ics0->window_sequence[0] == EIGHT_SHORT_SEQUENCE && grouping[0] != grouping[1]))
+        cpe->common_window = 1;
+    if(PSY_MODEL_MODE(apc->flags) > PSY_MODE_QUALITY){
+        av_log(apc->avctx, AV_LOG_ERROR, "Unknown mode %d, defaulting to CBR\n", PSY_MODEL_MODE(apc->flags));
+    }
+}
+
+/**
+ * Modify threshold by adding some value in loudness domain.
+ * @see 3GPP TS26.403 5.6.1.1.1 "Addition of noise with equal loudness"
+ */
+static inline float modify_thr(float thr, float r){
+    float t;
+    t = pow(thr, 0.25) + r;
+    return (t*t)*(t*t);
+}
+
+/**
+ * Calculate perceptual entropy and its corresponding values for one band.
+ * @see 3GPP TS26.403 5.6.1.3 "Calculation of the reduction value"
+ */
+static void calc_pe(Psy3gppBand *band, int band_width)
+{
+    if(band->energy <= band->thr){
+        band->a  = 0.0f;
+        band->b  = 0.0f;
+        band->nl = 0.0f;
+        return;
+    }
+    band->nl = band->ffac / pow(band->energy/band_width, 0.25);
+    if(band->energy >= band->thr * 8.0){
+        band->a = band->nl * log2(band->energy);
+        band->b = band->nl;
+    }else{
+        band->a = band->nl * (PSY_3GPP_C2 + PSY_3GPP_C3 * log2(band->energy));
+        band->b = band->nl * PSY_3GPP_C3;
+    }
+    band->pe = band->a - band->b * log2(band->thr);
+    band->min_snr = 1.0 / (pow(2.0, band->pe / band_width) - 1.5);
+    band->min_snr = av_clipf(band->min_snr, 1.26f, 316.2277f);
+}
+
+/**
+ * Determine scalefactor from band threshold and form factor.
+ * @see 3GPP TS26.403 5.4 5.6.2 "Scalefactor determination"
+ */
+static inline int determine_scalefactor(Psy3gppBand *band)
+{
+    //spec gives constant for lg() but we scaled it for log2()
+    return (int)(2.66667 * log2(6.75*band->thr/band->ffac));
+}
+
+/**
+ * Determine scalefactors and prepare coefficients for encoding.
+ * @see 3GPP TS26.403 5.4 "Psychoacoustic model"
+ */
+static void psy_3gpp_process(AACPsyContext *apc, int tag, int type, ChannelElement *cpe)
+{
+    int start;
+    int ch, w, wg, g, i;
+    int prev_scale;
+    Psy3gppContext *pctx = (Psy3gppContext*) apc->model_priv_data;
+    float pe_target;
+    int bits_avail;
+    int chans = type == TYPE_CPE ? 2 : 1;
+    Psy3gppChannel *pch = &pctx->ch[tag];
+
+    //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
+    memset(pch->band, 0, sizeof(pch->band));
+    for(ch = 0; ch < chans; ch++){
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        start = 0;
+        for(w = 0; w < ics->num_windows*16; w += 16){
+            for(g = 0; g < ics->num_swb; g++){
+                Psy3gppBand *band = &pch->band[ch][w+g];
+                for(i = 0; i < ics->swb_sizes[g]; i++)
+                    band->energy +=  cpe->ch[ch].coeffs[start+i] * cpe->ch[ch].coeffs[start+i];
+                band->energy *= 1.0f / (512*512);
+                band->thr = band->energy * 0.001258925f;
+                start += ics->swb_sizes[g];
+                if(band->energy != 0.0){
+                    float ffac = 0.0;
+
+                    for(i = 0; i < ics->swb_sizes[g]; i++)
+                        ffac += sqrt(FFABS(cpe->ch[ch].coeffs[start+i]));
+                    band->ffac = ffac / sqrt(512.0);
+                }
+            }
+        }
+    }
+
+    //modify thresholds - spread, threshold in quiet - 5.4.3 "Spreaded Energy Calculation"
+    for(ch = 0; ch < chans; ch++){
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        Psy3gppCoeffs *coeffs = &pctx->psy_coef[ics->num_windows == 8];
+        for(w = 0; w < ics->num_windows*16; w += 16){
+            Psy3gppBand *band = &pch->band[ch][w];
+            for(g = 1; g < ics->num_swb; g++){
+                band[g].thr = FFMAX(band[g].thr, band[g-1].thr * coeffs->spread_low[g-1]);
+            }
+            for(g = ics->num_swb - 2; g >= 0; g--){
+                band[g].thr = FFMAX(band[g].thr, band[g+1].thr * coeffs->spread_hi [g+1]);
+            }
+            for(g = 0; g < ics->num_swb; g++){
+                band[g].thr_quiet = FFMAX(band[g].thr, coeffs->ath[g]);
+                band[g].thr_quiet = fmaxf(PSY_3GPP_RPEMIN*band[g].thr_quiet,
+                                          fminf(band[g].thr_quiet,
+                                                PSY_3GPP_RPELEV*pch->prev_band[ch][w+g].thr_quiet));
+                band[g].thr = FFMAX(band[g].thr, band[g].thr_quiet * 0.25);
+            }
+        }
+    }
+
+    // M/S detection - 5.5.2 "Mid/Side Stereo"
+    if(chans > 1 && cpe->common_window){
+        start = 0;
+        for(w = 0; w < cpe->ch[0].ics.num_windows*16; w += 16){
+            for(g = 0; g < cpe->ch[0].ics.num_swb; g++){
+                Psy3gppBand *band0 = &pch->band[0][w+g];
+                Psy3gppBand *band1 = &pch->band[1][w+g];
+                double en_m = 0.0, en_s = 0.0, ff_m = 0.0, ff_s = 0.0, minthr;
+                float m, s;
+
+                cpe->ms_mask[w+g] = 0;
+                if(band0->energy == 0.0 || band1->energy == 0.0)
+                    continue;
+                for(i = 0; i < cpe->ch[0].ics.swb_sizes[g]; i++){
+                    m = cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i];
+                    s = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
+                    en_m += m*m;
+                    en_s += s*s;
+                }
+                en_m *= 1.0f / (512*512*4);
+                en_s *= 1.0f / (512*512*4);
+                minthr = FFMIN(band0->thr, band1->thr);
+                if(minthr * minthr * band0->energy * band1->energy >= band0->thr * band1->thr * en_m * en_s){
+                    cpe->ms_mask[w+g] = 1;
+                    band0->energy = en_m;
+                    band1->energy = en_s;
+                    band0->thr = en_m * 0.001258925f;
+                    band1->thr = en_s * 0.001258925f;
+                    for(i = 0; i < cpe->ch[0].ics.swb_sizes[g]; i++){
+                        m = cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i];
+                        s = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];
+                        ff_m += sqrt(fabs(m));
+                        ff_s += sqrt(fabs(s));
+                    }
+                    band0->ffac = ff_m * (1.0f / 32.0f); // sqrt(512)*sqrt(2)
+                    band1->ffac = ff_s * (1.0f / 32.0f);
+                }
+            }
+        }
+    }
+
+    for(ch = 0; ch < chans; ch++){
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        pch->a[ch] = pch->b[ch] = pch->pe[ch] = pch->thr[ch] = 0.0f;
+        for(w = 0; w < ics->num_windows*16; w += 16){
+            for(g = 0; g < ics->num_swb; g++){
+                Psy3gppBand *band = &pch->band[ch][w+g];
+                if(band->energy != 0.0)
+                    calc_pe(band, ics->swb_sizes[g]);
+                if(band->thr < band->energy){
+                    pch->a[ch]   += band->a;
+                    pch->b[ch]   += band->b;
+                    pch->pe[ch]  += band->pe;
+                    pch->thr[ch] += band->thr;
+                }
+            }
+        }
+    }
+
+    switch(PSY_MODEL_MODE(apc->flags)){
+    case PSY_MODE_CBR:
+    case PSY_MODE_ABR:
+        //bitrate reduction - 5.6.1 "Reduction of psychoacoustic requirements"
+        if(PSY_MODEL_MODE(apc->flags) != PSY_MODE_ABR){
+            pctx->reservoir += pctx->avg_bits - apc->avctx->frame_bits;
+            bits_avail = pctx->avg_bits + pctx->reservoir;
+            bits_avail = FFMIN(bits_avail, pctx->avg_bits * 1.5);
+            pe_target = 1.18f * bits_avail / apc->avctx->channels * chans;
+        }else{
+            pe_target = pctx->avg_bits / apc->avctx->channels * chans;
+        }
+        for(i = 0; i < 2; i++){
+            float t0, pe, r, a0 = 0.0f, pe0 = 0.0f, b0 = 0.0f;
+            for(ch = 0; ch < chans; ch++){
+                a0  += pch->a[ch];
+                b0  += pch->b[ch];
+                pe0 += pch->pe[ch];
+            }
+            if(pe0 == 0.0f) break;
+            t0 = pow(2.0, (a0 - pe0)       / (4.0 * b0));
+            r  = pow(2.0, (a0 - pe_target) / (4.0 * b0)) - t0;
+
+            //add correction factor to thresholds and recalculate perceptual entropy
+            for(ch = 0; ch < chans; ch++){
+                IndividualChannelStream *ics = &cpe->ch[ch].ics;
+                pch->a[ch] = pch->b[ch] = pch->pe[ch] = pch->thr[ch] = 0.0;
+                pe = 0.0f;
+                for(w = 0; w < ics->num_windows*16; w += 16){
+                    for(g = 0; g < ics->num_swb; g++){
+                        Psy3gppBand *band = &pch->band[ch][w+g];
+                        band->thr = modify_thr(band->thr, r);
+                        calc_pe(band, ics->swb_sizes[g]);
+                        if(band->thr < band->energy){
+                            pch->a[ch]   += band->a;
+                            pch->b[ch]   += band->b;
+                            pch->pe[ch]  += band->pe;
+                            pch->thr[ch] += band->thr;
+                        }
+                    }
+                }
+            }
+        }
+
+        //determine scalefactors - 5.6.2 "Scalefactor determination"
+        for(ch = 0; ch < chans; ch++){
+            IndividualChannelStream *ics = &cpe->ch[ch].ics;
+            for(w = 0; w < ics->num_windows*16; w += 16){
+                for(g = 0; g < ics->num_swb; g++){
+                    Psy3gppBand *band = &pch->band[ch][w+g];
+                    cpe->ch[ch].zeroes[w+g] = band->thr >= band->energy;
+                    if(cpe->ch[ch].zeroes[w+g]) continue;
+                    cpe->ch[ch].sf_idx[w+g] = determine_scalefactor(band);
+                }
+            }
+        }
+        break;
+    case PSY_MODE_QUALITY:
+        for(ch = 0; ch < chans; ch++){
+            IndividualChannelStream *ics = &cpe->ch[ch].ics;
+            start = 0;
+            for(w = 0; w < ics->num_windows*16; w += 16){
+                for(g = 0; g < ics->num_swb; g++){
+                    Psy3gppBand *band = &pch->band[ch][w+g];
+                    if(band->thr >= band->energy){
+                        cpe->ch[ch].sf_idx[w+g] = 0;
+                        cpe->ch[ch].zeroes[w+g] = 1;
+                    }else{
+                        cpe->ch[ch].zeroes[w+g] = 0;
+                        cpe->ch[ch].sf_idx[w+g] = determine_scalefactor(band);
+                        while(cpe->ch[ch].sf_idx[w+g] > 3){
+                            float dist = get_approximate_quant_error(cpe->ch[ch].coeffs + start,
+                                                         ics->swb_sizes[g],
+                                                         SCALE_ONE_POS + cpe->ch[ch].sf_idx[w+g]);
+                            if(dist < band->thr) break;
+                            cpe->ch[ch].sf_idx[w+g] -= 3;
+                        }
+                    }
+                    start += ics->swb_sizes[g];
+                }
+            }
+        }
+        break;
+    }
+
+    //limit scalefactors
+    for(ch = 0; ch < chans; ch++){
+        int min_scale = 256;
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        for(w = 0; w < ics->num_windows*16; w += 16)
+            for(g = 0; g < ics->num_swb; g++){
+                if(cpe->ch[ch].zeroes[w + g]) continue;
+                min_scale = FFMIN(min_scale, cpe->ch[ch].sf_idx[w + g]);
+            }
+        for(w = 0; w < ics->num_windows*16; w += 16)
+            for(g = 0; g < ics->num_swb; g++){
+                if(cpe->ch[ch].zeroes[w + g]) continue;
+                cpe->ch[ch].sf_idx[w + g] = FFMIN(cpe->ch[ch].sf_idx[w + g], min_scale + SCALE_MAX_DIFF);
+            }
+        for(w = 0; w < ics->num_windows*16; w += 16)
+            for(g = 0; g < ics->num_swb; g++){
+                if(cpe->ch[ch].zeroes[w + g])
+                    cpe->ch[ch].sf_idx[w + g] = 256;
+                else
+                    cpe->ch[ch].sf_idx[w + g] = av_clip(SCALE_ONE_POS + cpe->ch[ch].sf_idx[w + g],
+                                                        0,
+                                                        SCALE_MAX_POS);
+            }
+
+        //adjust scalefactors for window groups
+        w = 0;
+        for(wg = 0; wg < ics->num_window_groups; wg++){
+            int min_scale = 256;
+
+            for(g = 0; g < ics->num_swb; g++){
+                for(i = w; i < w + ics->group_len[wg]*16; i += 16){
+                    if(cpe->ch[ch].zeroes[i + g]) continue;
+                    min_scale = FFMIN(min_scale, cpe->ch[ch].sf_idx[i + g]);
+                }
+                for(i = w; i < w + ics->group_len[wg]*16; i += 16)
+                    cpe->ch[ch].sf_idx[i + g] = min_scale;
+            }
+            w += ics->group_len[wg] * 16;
+        }
+    }
+
+    memcpy(pch->prev_band, pch->band, sizeof(pch->band));
+    psy_create_output(apc, cpe, chans);
+}
+
+static av_cold void psy_3gpp_end(AACPsyContext *apc)
+{
+    Psy3gppContext *pctx = (Psy3gppContext*) apc->model_priv_data;
+    av_freep(&pctx->ch);
+    av_freep(&apc->model_priv_data);
+}
+
+static const AACPsyModel psy_models[AAC_NB_PSY_MODELS] =
+{
+    {
+       "3GPP TS 26.403-inspired model",
+        psy_3gpp_init,
+        psy_3gpp_window,
+        psy_3gpp_process,
+        psy_3gpp_end,
+    },
+};
+
+int av_cold ff_aac_psy_init(AACPsyContext *ctx, AVCodecContext *avctx,
+                            enum AACPsyModelType model, int elements, int flags,
+                            const uint8_t *bands1024, int num_bands1024,
+                            const uint8_t *bands128,  int num_bands128)
+{
+    int i;
+
+    if(model < 0 || model >= AAC_NB_PSY_MODELS){
+         av_log(avctx, AV_LOG_ERROR, "Invalid psy model\n");
+         return -1;
+    }
+
+#ifndef CONFIG_HARDCODED_TABLES
+   for (i = 0; i < 316; i++)
+        ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
+#endif /* CONFIG_HARDCODED_TABLES */
+
+    ctx->avctx = avctx;
+    ctx->flags = flags;
+    ctx->bands1024 = bands1024;
+    ctx->num_bands1024 = num_bands1024;
+    ctx->bands128 = bands128;
+    ctx->num_bands128 = num_bands128;
+    ctx->model = &psy_models[model];
+
+    if(ctx->flags & PSY_MODEL_NO_ST_ATT || PSY_MODEL_MODE(ctx->flags) == PSY_MODE_QUALITY){
+        ctx->flags |= PSY_MODEL_NO_ST_ATT;
+        ctx->stereo_att = 0.5f;
+    }else{
+        ctx->stereo_att = av_clipf(avctx->bit_rate / elements / 192000.0, 0.0f, 0.5f);
+    }
+    if(ctx->flags & PSY_MODEL_NO_LOWPASS || PSY_MODEL_MODE(ctx->flags) == PSY_MODE_QUALITY){
+        ctx->flags |= PSY_MODEL_NO_LOWPASS;
+    }else{
+        int cutoff;
+        cutoff = avctx->bit_rate / elements / 8;
+        if(ff_lowpass_filter_init_coeffs(&ctx->lp_coeffs, avctx->sample_rate/2, cutoff) < 0){
+            ctx->flags |= PSY_MODEL_NO_LOWPASS;
+        }else{
+            ctx->lp_state = av_mallocz(sizeof(LPFilterState) * elements * 2);
+        }
+    }
+    if(ctx->model->init)
+        return ctx->model->init(ctx, elements);
+    return 0;
+}
+
+void ff_aac_psy_suggest_window(AACPsyContext *ctx, int16_t *audio, int16_t *la,
+                               int tag, int type, ChannelElement *cpe)
+{
+    ctx->model->window(ctx, audio, la, tag, type, cpe);
+}
+
+void ff_aac_psy_analyze(AACPsyContext *ctx, int tag, int type, ChannelElement *cpe)
+{
+    ctx->model->process(ctx, tag, type, cpe);
+}
+
+void av_cold ff_aac_psy_end(AACPsyContext *ctx)
+{
+    av_freep(&ctx->lp_state);
+    if(ctx->model->end)
+        return ctx->model->end(ctx);
+}
+
+void ff_aac_psy_preprocess(AACPsyContext *ctx, int16_t *audio, int16_t *dest, int tag, int type)
+{
+    int chans = type == TYPE_CPE ? 2 : 1;
+    const int chstride = ctx->avctx->channels;
+    int i, ch;
+    float t[2];
+
+    if(chans == 1){
+        for(ch = 0; ch < chans; ch++){
+            for(i = 0; i < 1024; i++){
+                dest[i * chstride + ch] = audio[i * chstride + ch];
+            }
+        }
+    }else{
+        for(i = 0; i < 1024; i++){
+            if(ctx->flags & PSY_MODEL_NO_ST_ATT){
+                for(ch = 0; ch < 2; ch++)
+                    t[ch] = audio[i * chstride + ch];
+            }else{
+                t[0] = audio[i * chstride + 0] * (0.5 + ctx->stereo_att) + audio[i * chstride + 1] * (0.5 - ctx->stereo_att);
+                t[1] = audio[i * chstride + 0] * (0.5 - ctx->stereo_att) + audio[i * chstride + 1] * (0.5 + ctx->stereo_att);
+            }
+            if(!(ctx->flags & PSY_MODEL_NO_LOWPASS)){
+                LPFilterState *is = (LPFilterState*)ctx->lp_state + tag*2;
+                for(ch = 0; ch < 2; ch++)
+                    t[ch] = ff_lowpass_filter(&ctx->lp_coeffs, is + ch, t[ch]);
+            }
+            for(ch = 0; ch < 2; ch++)
+                dest[i * chstride + ch] = av_clip_int16(t[ch]);
+        }
+    }
+}
+
Index: aacpsy.h
===================================================================
--- aacpsy.h	(revision 14920)
+++ aacpsy.h	(working copy)
@@ -27,20 +27,109 @@
 #include "lowpass.h"
 
 enum AACPsyModelType{
-    AAC_PSY_TEST,              ///< a sample model to exercise encoder
     AAC_PSY_3GPP,              ///< model following recommendations from 3GPP TS 26.403
 
     AAC_NB_PSY_MODELS          ///< total number of psychoacoustic models, since it's not a part of the ABI new models can be added freely
 };
 
+enum AACPsyModelMode{
+    PSY_MODE_CBR,              ///< follow bitrate as closely as possible
+    PSY_MODE_ABR,              ///< try to achieve bitrate but actual bitrate may differ significantly
+    PSY_MODE_QUALITY,          ///< try to achieve set quality instead of bitrate
+};
+
+#define PSY_MODEL_MODE_MASK  0x0000000F ///< bit fields for storing mode (CBR, ABR, VBR)
+#define PSY_MODEL_NO_SWITCH  0x00000020 ///< disable window switching
+#define PSY_MODEL_NO_ST_ATT  0x00000040 ///< disable stereo attenuation
+#define PSY_MODEL_NO_LOWPASS 0x00000080 ///< disable low-pass filtering
+
+#define PSY_MODEL_NO_PREPROC (PSY_MODEL_NO_ST_ATT | PSY_MODEL_NO_LOWPASS)
+
+#define PSY_MODEL_MODE(a)  ((a) & PSY_MODEL_MODE_MASK)
+
 /**
  * context used by psychoacoustic model
  */
 typedef struct AACPsyContext {
     AVCodecContext *avctx;            ///< encoder context
+
+    int flags;                        ///< model flags
+    const uint8_t *bands1024;         ///< scalefactor band sizes for long (1024 samples) frame
+    int num_bands1024;                ///< number of scalefactor bands for long frame
+    const uint8_t *bands128;          ///< scalefactor band sizes for short (128 samples) frame
+    int num_bands128;                 ///< number of scalefactor bands for short frame
+
+    const struct AACPsyModel *model;  ///< pointer to the psychoacoustic model implementation
+    void* model_priv_data;            ///< psychoacoustic model implementation private data
+
+    float stereo_att;                 ///< stereo attenuation factor
+    LPFilterCoeffs lp_coeffs;         ///< lowpass filter coefficients
+    LPFilterState *lp_state;          ///< lowpass filter state
 }AACPsyContext;
 
+typedef struct AACPsyModel {
+    const char *name;
+    int   (*init)   (AACPsyContext *apc, int elements);
+    void  (*window) (AACPsyContext *apc, int16_t *audio, int16_t *la, int tag, int type, ChannelElement *cpe);
+    void  (*process)(AACPsyContext *apc, int tag, int type, ChannelElement *cpe);
+    void  (*end)    (AACPsyContext *apc);
+}AACPsyModel;
+
 /**
+ * Initialize psychoacoustic model.
+ *
+ * @param ctx           model context
+ * @param avctx         codec context
+ * @param model         model implementation that will be used
+ * @param elements      number of channel elements (single channel or channel pair) to handle by  model
+ * @param flags         model flags, may be ignored by model if unsupported
+ * @param bands1024     scalefactor band lengths for long (1024 samples) frame
+ * @param num_bands1024 number of scalefactor bands for long frame
+ * @param bands128      scalefactor band lengths for short (128 samples) frame
+ * @param num_bands128  number of scalefactor bands for short frame
+ *
+ * @return zero if successful, a negative value if not
+ */
+int ff_aac_psy_init(AACPsyContext *ctx, AVCodecContext *avctx,
+                    enum AACPsyModelType model, int elements, int flags,
+                    const uint8_t *bands1024, int num_bands1024,
+                    const uint8_t *bands128,  int num_bands128);
+
+/**
+ * Preprocess audio frame in order to compress it better.
+ *
+ * @param ctx   model context
+ * @param audio samples to preprocess
+ * @param dest  place to put filtered samples
+ * @param tag   number of channel element to analyze
+ * @param type  channel element type (e.g. ID_SCE or ID_CPE)
+ */
+void ff_aac_psy_preprocess(AACPsyContext *ctx, int16_t *audio, int16_t *dest, int tag, int type);
+
+/**
+ * Set window sequence and related parameters for channel element.
+ *
+ * @param ctx   model context
+ * @param audio samples for the current frame
+ * @param la    lookahead samples (NULL when unavailable)
+ * @param tag   number of channel element to analyze
+ * @param type  channel element type (e.g. ID_SCE or ID_CPE)
+ * @param cpe   pointer to the current channel element
+ */
+void ff_aac_psy_suggest_window(AACPsyContext *ctx, int16_t *audio, int16_t *la, int tag, int type, ChannelElement *cpe);
+
+/**
+ * Perform psychoacoustic analysis and output coefficients in integer form
+ * along with scalefactors, M/S flags, etc.
+ *
+ * @param ctx   model context
+ * @param tag   number of channel element to analyze
+ * @param type  channel element type (e.g. ID_SCE or ID_CPE)
+ * @param cpe   pointer to the current channel element
+ */
+void ff_aac_psy_analyze(AACPsyContext *ctx, int tag, int type, ChannelElement *cpe);
+
+/**
  * Cleanup model context at the end.
  *
  * @param ctx model context



More information about the ffmpeg-devel mailing list