[FFmpeg-soc] [soc] soc g723.1 067d6998c2c8eef46e60d5406f209a12f8967b5a

Mohamed Naufal naufal11 at gmail.com
Sat Jun 19 18:53:58 CEST 2010


- Log -----------------------------------------------------------------
commit 067d6998c2c8eef46e60d5406f209a12f8967b5a
Author: Naufal <naufal11 at gmail.com>
Date:   Sat Jun 19 22:14:19 2010 +0530

   Generate fixed codebook excitation vector

diff --git a/libavcodec/g723_1.c b/libavcodec/g723_1.c
index a70088c..096de1d 100755
--- a/libavcodec/g723_1.c
+++ b/libavcodec/g723_1.c
@@ -9,6 +9,7 @@ typedef struct g723_1_context {
     int8_t lsp_index[LSP_BANDS];
     int16_t prev_lsp[LPC_ORDER];
     int16_t pitch_lag[2];
+    int16_t prev_excitation[PITCH_MAX];
     G723_1_Subframe subframe[4];
     FrameType cur_frame_type;
     FrameType past_frame_type;
@@ -258,6 +259,78 @@ static void lsp_interpolate(int16_t *lpc, int16_t
*cur_lsp, int16_t *prev_lsp)
     }
 }

+/*
+ * Generate fixed codebook excitation vector
+ *
+ * @param vector decoded excitation vector
+ * @param index  the current subframe index
+ */
+static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe subfrm,
+                               Rate cur_rate, int16_t pitch_lag, int index)
+{
+    int temp, i, j;
+
+    memset(vector, 0, SUBFRAMES);
+
+    if (cur_rate == Rate6k3) {
+        if (subfrm.pulse_pos >= max_pos[index])
+            return;
+
+        // Decode amplitudes and positions
+        j = PULSE_MAX - pulses[index];
+        temp = subfrm.pulse_pos;
+        for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
+            temp -= combinatorial_table[j][i];
+            if (temp >= 0)
+                break;
+            temp += combinatorial_table[j++][i];
+            if (!(subfrm.pulse_sign & (1 << (PULSE_MAX - j)))) {
+                vector[subfrm.grid_index + GRID_SIZE * i] =
+                                        -fixed_cb_gain[subfrm.amp_index];
+            } else {
+                vector[subfrm.grid_index + GRID_SIZE * i] =
+                                         fixed_cb_gain[subfrm.amp_index];
+            }
+            if (j == PULSE_MAX)
+                break;
+        }
+        // Generate Dirac train
+        if (subfrm.trans_gain == 1) {
+            int16_t temp_vector[SUBFRAME_LEN];
+            memcpy(temp_vector, vector, SUBFRAME_LEN);
+            for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag)
+                for (j = 0; j < SUBFRAME_LEN - i; j++)
+                    vector[i + j] += temp_vector[j];
+        }
+    } else { // Rate5k3
+        int16_t cb_gain  = fixed_cb_gain[subfrm.amp_index];
+        int16_t cb_shift = subfrm.grid_index;
+        int16_t cb_sign  = subfrm.pulse_sign;
+        int16_t cb_pos   = subfrm.pulse_pos;
+        int offset, beta, lag, temp;
+
+        for (i = 0; i < 8; i += 2) {
+            offset =  ((cb_pos & 7) << 3) + cb_shift + i;
+            vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
+            cb_pos  >>= 3;
+            cb_sign >>= 1;
+        }
+
+        // Enhance harmonic components
+        lag  = pitch_contrib[subfrm.ad_cb_gain << 1] + pitch_lag +
+               subfrm.ad_cb_lag - 1;
+        beta = pitch_contrib[(subfrm.ad_cb_lag << 1) + 1];
+
+        if (lag < SUBFRAME_LEN - 2) {
+            for (i = lag; i < SUBFRAME_LEN; i++) {
+                temp = av_clip_int16(beta * vector[i - lag]);
+                vector[i] = av_clip_int16(vector[i] + temp);
+            }
+        }
+    }
+}
+
+
 static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
                               int *data_size, AVPacket *avpkt)
 {
@@ -267,7 +340,10 @@ static int g723_1_decode_frame(AVCodecContext
*avctx, void *data,

     int16_t cur_lsp[LPC_ORDER];
     int16_t lpc[SUBFRAMES * LPC_ORDER + 4];
-    int bad_frame, erased_frames;
+    int16_t temp_vector[SUBFRAMES + PITCH_MAX];
+    int16_t *vector_ptr;
+    int16_t interp_gain;
+    int bad_frame, erased_frames, i;

     if (!buf_size || buf_size < frame_size[buf[0] & 3]) {
         *data_size = 0;
@@ -288,6 +364,19 @@ static int g723_1_decode_frame(AVCodecContext
*avctx, void *data,

         inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
         lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
+
+        // Generate the excitation for the frame
+        memcpy(temp_vector, p->prev_excitation, PITCH_MAX);
+        vector_ptr = temp_vector + PITCH_MAX;
+        if (!erased_frames) {
+            // Update interpolation gain memory
+            interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
+                                        p->subframe[3].amp_index) >> 1];
+            for (i = 0; i < SUBFRAMES; i++) {
+                gen_fcb_excitation(vector_ptr, p->subframe[i], p->cur_rate,
+                                   p->pitch_lag[i >> 1], i);
+            }
+        }
     }

     return frame_size[p->cur_frame_type];
diff --git a/libavcodec/g723_1_data.h b/libavcodec/g723_1_data.h
index 4d6a9f0..86a749b 100644
--- a/libavcodec/g723_1_data.h
+++ b/libavcodec/g723_1_data.h
@@ -5,6 +5,9 @@
 #define LSP_BANDS    3
 #define LSP_CB_SIZE  256
 #define PITCH_MIN    18
+#define PITCH_MAX    (PITCH_MIN + 127)
+#define GRID_SIZE    2
+#define PULSE_MAX    6
 #define GAIN_LEVELS  24

 /*
@@ -902,3 +905,113 @@ static const int16_t lsp_band2[LSP_CB_SIZE * 4] = {
    3633,   2336,   2408,   1453,
    2923,   3517,   2567,   1318,
 };
+
+static const int16_t fixed_cb_gain[GAIN_LEVELS] = {
+     1,    2,    3,    4,    6,    9,   13,   18,
+    26,   38,   55,   80,  115,  166,  240,  348,
+   502,  726, 1050, 1517, 2193, 3170, 4582, 6623,
+};
+
+/*
+ * Used for the coding/decoding of the pulses positions
+ * for the MP-MLQ codebook
+ */
+static const int combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE] = {
+   {118755, 98280, 80730, 65780L, 53130,
+     42504, 33649, 26334,  20349, 15504,
+     11628,  8568,  6188,   4368,  3003,
+      2002,  1287,   792,    462,   252,
+       126,    56,    21,      6,     1,
+         0,     0,     0,      0,     0},
+
+   { 23751, 20475, 17550,  14950, 12650,
+     10626,  8855,  7315,   5985,  4845,
+      3876,  3060,  2380,   1820,  1365,
+      1001,   715,   495,    330,   210,
+       126,    70,    35,     15,     5,
+         1,     0,     0,      0,     0},
+
+   {  3654,  3276,  2925,   2600,  2300,
+      2024,  1771,  1540,   1330,  1140,
+       969,   816,   680,    560,   455,
+       364,   286,   220,    165,   120,
+        84,    56,    35,     20,    10,
+         4,     1,     0,      0,     0},
+
+   {   406,   378,   351,    325,   300,
+       276,   253,   231,    210,   190,
+       171,   153,   136,    120,   105,
+        91,    78,    66,     55,    45,
+        36,    28,    21,     15,    10,
+         6,     3,     1,      0,     0},
+
+   {    29,    28,    27,     26,    25,
+        24,    23,    22,     21,    20,
+        19,    18,    17,     16,    15,
+        14,    13,    12,     11,    10,
+         9,     8,     7,      6,     5,
+         4,     3,     2,      1,     0},
+
+   {     1,     1,     1,      1,     1,
+         1,     1,     1,      1,     1,
+         1,     1,     1,      1,     1,
+         1,     1,     1,      1,     1,
+         1,     1,     1,      1,     1,
+         1,     1,     1,      1,     1},
+};
+
+/*
+ * Number of non-zero pulses in the MP-MLQ excitation
+ */
+static const int8_t pulses[4] = {6, 5, 6, 5};
+
+/*
+ * Size of the MP-MLQ fixed excitation codebooks
+ */
+static const int max_pos[4] = {593775, 142506, 593775, 142506};
+
+static const int16_t pitch_contrib[340] = {
+   60,     0,  0,  2489, 60,     0,  0,  5217,
+    1,  6171,  0,  3953,  0, 10364,  1,  9357,
+   -1,  8843,  1,  9396,  0,  5794, -1, 10816,
+    2, 11606, -2, 12072,  0,  8616,  1, 12170,
+    0, 14440,  0,  7787, -1, 13721,  0, 18205,
+    0, 14471,  0, 15807,  1, 15275,  0, 13480,
+   -1, 18375, -1,     0,  1, 11194, -1, 13010,
+    1, 18836, -2, 20354,  1, 16233, -1,     0,
+   60,     0,  0, 12130,  0, 13385,  1, 17834,
+    1, 20875,  0, 21996,  1,     0,  1, 18277,
+   -1, 21321,  1, 13738, -1, 19094, -1, 20387,
+   -1,     0,  0, 21008, 60,     0, -2, 22807,
+    0, 15900,  1,     0,  0, 17989, -1, 22259,
+    1, 24395,  1, 23138,  0, 23948,  1, 22997,
+    2, 22604, -1, 25942,  0, 26246,  1, 25321,
+    0, 26423,  0, 24061,  0, 27247, 60,     0,
+   -1, 25572,  1, 23918,  1, 25930,  2, 26408,
+   -1, 19049,  1, 27357, -1, 24538, 60,     0,
+   -1, 25093,  0, 28549,  1,     0,  0, 22793,
+   -1, 25659,  0, 29377,  0, 30276,  0, 26198,
+    1, 22521, -1, 28919,  0, 27384,  1, 30162,
+   -1,     0,  0, 24237, -1, 30062,  0, 21763,
+    1, 30917, 60,     0,  0, 31284,  0, 29433,
+    1, 26821,  1, 28655,  0, 31327,  2, 30799,
+    1, 31389,  0, 32322,  1, 31760, -2, 31830,
+    0, 26936, -1, 31180,  1, 30875,  0, 27873,
+   -1, 30429,  1, 31050,  0,     0,  0, 31912,
+    1, 31611,  0, 31565,  0, 25557,  0, 31357,
+   60,     0,  1, 29536,  1, 28985, -1, 26984,
+   -1, 31587,  2, 30836, -2, 31133,  0, 30243,
+   -1, 30742, -1, 32090, 60,     0,  2, 30902,
+   60,     0,  0, 30027,  0, 29042, 60,     0,
+    0, 31756,  0, 24553,  0, 25636, -2, 30501,
+   60,     0, -1, 29617,  0, 30649, 60,     0,
+    0, 29274,  2, 30415,  0, 27480,  0, 31213,
+   -1, 28147,  0, 30600,  1, 31652,  2, 29068,
+   60,     0,  1, 28571,  1, 28730,  1, 31422,
+    0, 28257,  0, 24797, 60,     0,  0,     0,
+   60,     0,  0, 22105,  0, 27852, 60,     0,
+   60,     0, -1, 24214,  0, 24642,  0, 23305,
+   60,     0, 60,     0,  1, 22883,  0, 21601,
+   60,     0,  2, 25650, 60,     0, -2, 31253,
+   -2, 25144,  0, 17998
+};
--
http://github.com/naufal/ffmpeg-soc


More information about the FFmpeg-soc mailing list