[FFmpeg-cvslog] dcaenc: Use Huffman codes for Bit Allocation Index

Daniil Cherednik git at videolan.org
Fri Jan 20 12:04:22 EET 2017


ffmpeg | branch: master | Daniil Cherednik <dan.cherednik at gmail.com> | Wed Jan 18 17:26:27 2017 +0300| [9a619bef5492a664c1e80a74c5779e28763179f3] | committer: Rostislav Pehlivanov

dcaenc: Use Huffman codes for Bit Allocation Index

Reviewed-by: Rostislav Pehlivanov <atomnuker at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=9a619bef5492a664c1e80a74c5779e28763179f3
---

 libavcodec/dcaenc.c  | 47 ++++++++++++++++++++++++++++++++++++++++++-----
 libavcodec/dcahuff.c | 27 +++++++++++++++++++++++----
 libavcodec/dcahuff.h |  3 +++
 3 files changed, 68 insertions(+), 9 deletions(-)

diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index ff7b0eb..3c5c33c 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -67,6 +67,7 @@ typedef struct DCAEncContext {
     int32_t peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS];
     int32_t downsampled_lfe[DCA_LFE_SAMPLES];
     int32_t masking_curve_cb[SUBSUBFRAMES][256];
+    int32_t bit_allocation_sel[MAX_CHANNELS];
     int abits[MAX_CHANNELS][DCAENC_SUBBANDS];
     int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS];
     softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS];
@@ -147,6 +148,8 @@ static int encode_init(AVCodecContext *avctx)
         for (j = 0; j < DCA_CODE_BOOKS; j++) {
             c->quant_index_sel[i][j] = ff_dca_quant_index_group_size[j];
         }
+        /* 6 - no Huffman */
+        c->bit_allocation_sel[i] = 6;
     }
 
     for (i = 0; i < 9; i++) {
@@ -672,6 +675,33 @@ static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7], uint32_t clc
     return bits;
 }
 
+static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
+{
+    uint8_t i;
+    uint32_t t;
+    int32_t best_sel = 6;
+    int32_t best_bits = bands * 5;
+
+    /* Check do we have subband which cannot be encoded by Huffman tables */
+    for (i = 0; i < bands; i++) {
+        if (abits[i] > 12) {
+            *res = best_sel;
+            return best_bits;
+        }
+    }
+
+    for (i = 0; i < DCA_BITALLOC_12_COUNT; i++) {
+        t = ff_dca_vlc_calc_alloc_bits(abits, bands, i);
+        if (t < best_bits) {
+            best_bits = t;
+            best_sel = i;
+        }
+    }
+
+    *res = best_sel;
+    return best_bits;
+}
+
 static int init_quantization_noise(DCAEncContext *c, int noise)
 {
     int ch, band, ret = 0;
@@ -679,7 +709,7 @@ static int init_quantization_noise(DCAEncContext *c, int noise)
     uint32_t clc_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS];
     uint32_t bits_counter = 0;
 
-    c->consumed_bits = 132 + 493 * c->fullband_channels;
+    c->consumed_bits = 132 + 333 * c->fullband_channels;
     if (c->lfe_channel)
         c->consumed_bits += 72;
 
@@ -702,6 +732,7 @@ static int init_quantization_noise(DCAEncContext *c, int noise)
                 ret |= USED_1ABITS;
             }
         }
+        c->consumed_bits += set_best_abits_code(c->abits[ch], 32, &c->bit_allocation_sel[ch]);
     }
 
     /* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
@@ -908,7 +939,7 @@ static void put_primary_audio_header(DCAEncContext *c)
 
     /* Bit allocation quantizer select: linear 5-bit */
     for (ch = 0; ch < c->fullband_channels; ch++)
-        put_bits(&c->pb, 3, 6);
+        put_bits(&c->pb, 3, c->bit_allocation_sel[ch]);
 
     /* Quantization index codebook select */
     for (i = 0; i < DCA_CODE_BOOKS; i++)
@@ -974,9 +1005,15 @@ static void put_subframe(DCAEncContext *c, int subframe)
 
     /* Prediction VQ address: not transmitted */
     /* Bit allocation index */
-    for (ch = 0; ch < c->fullband_channels; ch++)
-        for (band = 0; band < DCAENC_SUBBANDS; band++)
-            put_bits(&c->pb, 5, c->abits[ch][band]);
+    for (ch = 0; ch < c->fullband_channels; ch++) {
+        if (c->bit_allocation_sel[ch] == 6) {
+            for (band = 0; band < DCAENC_SUBBANDS; band++) {
+                put_bits(&c->pb, 5, c->abits[ch][band]);
+            }
+        } else {
+            ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS, c->bit_allocation_sel[ch]);
+        }
+    }
 
     if (SUBSUBFRAMES > 1) {
         /* Transition mode: none for each channel and subband */
diff --git a/libavcodec/dcahuff.c b/libavcodec/dcahuff.c
index 9fb42a6..0a3eeb4 100644
--- a/libavcodec/dcahuff.c
+++ b/libavcodec/dcahuff.c
@@ -42,13 +42,12 @@ static const uint8_t tmode_bits[TMODE_COUNT][4] = {
     { 2, 2, 2, 2 }
 };
 
-#define BITALLOC_12_COUNT    5
 #define BITALLOC_12_VLC_BITS 9
-static const uint8_t bitalloc_12_vlc_bits[BITALLOC_12_COUNT] = {
+static const uint8_t bitalloc_12_vlc_bits[DCA_BITALLOC_12_COUNT] = {
     9, 7, 7, 9, 9
 };
 
-static const uint16_t bitalloc_12_codes[BITALLOC_12_COUNT][12] = {
+static const uint16_t bitalloc_12_codes[DCA_BITALLOC_12_COUNT][12] = {
     { 0x0000, 0x0002, 0x0006, 0x000E, 0x001E, 0x003E, 0x00FF, 0x00FE,
       0x01FB, 0x01FA, 0x01F9, 0x01F8, },
     { 0x0001, 0x0000, 0x0002, 0x000F, 0x000C, 0x001D, 0x0039, 0x0038,
@@ -61,7 +60,7 @@ static const uint16_t bitalloc_12_codes[BITALLOC_12_COUNT][12] = {
       0x0079, 0x0078, 0x00FB, 0x00FA, }
 };
 
-static const uint8_t bitalloc_12_bits[BITALLOC_12_COUNT][12] = {
+static const uint8_t bitalloc_12_bits[DCA_BITALLOC_12_COUNT][12] = {
     { 1, 2, 3, 4, 5, 6, 8, 8, 9, 9,  9,  9 },
     { 1, 2, 3, 5, 5, 6, 7, 7, 7, 7,  7,  7 },
     { 2, 3, 3, 3, 3, 4, 4, 4, 5, 6,  7,  7 },
@@ -1357,3 +1356,23 @@ void ff_dca_vlc_enc_quant(PutBitContext *pb, int *values, uint8_t n, uint8_t sel
         put_bits(pb, bitalloc_bits[table][sel][id], bitalloc_codes[table][sel][id]);
     }
 }
+
+uint32_t ff_dca_vlc_calc_alloc_bits(int *values, uint8_t n, uint8_t sel)
+{
+    uint8_t i, id;
+    uint32_t sum = 0;
+    for (i = 0; i < n; i++) {
+        id = values[i] - 1;
+        sum += bitalloc_12_bits[sel][id];
+    }
+    return sum;
+}
+
+void ff_dca_vlc_enc_alloc(PutBitContext *pb, int *values, uint8_t n, uint8_t sel)
+{
+    uint8_t i, id;
+    for (i = 0; i < n; i++) {
+        id = values[i] - 1;
+        put_bits(pb, bitalloc_12_bits[sel][id], bitalloc_12_codes[sel][id]);
+    }
+}
diff --git a/libavcodec/dcahuff.h b/libavcodec/dcahuff.h
index c017622..02b0e37 100644
--- a/libavcodec/dcahuff.h
+++ b/libavcodec/dcahuff.h
@@ -30,6 +30,7 @@
 #include "put_bits.h"
 
 #define DCA_CODE_BOOKS      10
+#define DCA_BITALLOC_12_COUNT    5
 
 typedef struct DCAVLC {
     int offset;         ///< Code values offset
@@ -58,5 +59,7 @@ extern VLC  ff_dca_vlc_rsd;
 av_cold void ff_dca_init_vlcs(void);
 uint32_t ff_dca_vlc_calc_quant_bits(int *values, uint8_t n, uint8_t sel, uint8_t abits);
 void ff_dca_vlc_enc_quant(PutBitContext *pb, int *values, uint8_t n, uint8_t sel, uint8_t abits);
+uint32_t ff_dca_vlc_calc_alloc_bits(int *values, uint8_t n, uint8_t sel);
+void ff_dca_vlc_enc_alloc(PutBitContext *pb, int *values, uint8_t n, uint8_t sel);
 
 #endif /* AVCODEC_DCAHUFF_H */



More information about the ffmpeg-cvslog mailing list