[libav-commits] g722enc: split encoding into separate functions for trellis vs. no trellis

Justin Ruggles git at libav.org
Sat Jan 7 19:56:39 CET 2012


Module: libav
Branch: master
Commit: 34093ba0819c8f00f024a49cc2cebe0400f020d4

Author:    Justin Ruggles <justin.ruggles at gmail.com>
Committer: Justin Ruggles <justin.ruggles at gmail.com>
Date:      Thu Jan  5 23:34:09 2012 -0500

g722enc: split encoding into separate functions for trellis vs. no trellis

---

 libavcodec/g722enc.c |   53 +++++++++++++++++++++++++++++++------------------
 1 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/libavcodec/g722enc.c b/libavcodec/g722enc.c
index 1eed784..470770c 100644
--- a/libavcodec/g722enc.c
+++ b/libavcodec/g722enc.c
@@ -117,13 +117,12 @@ static inline int encode_low(const struct G722Band* state, int xlow)
     return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
 }
 
-static int g722_encode_trellis(AVCodecContext *avctx,
-                               uint8_t *dst, int buf_size, void *data)
+static void g722_encode_trellis(G722Context *c, int trellis,
+                                uint8_t *dst, int nb_samples,
+                                const int16_t *samples)
 {
-    G722Context *c = avctx->priv_data;
-    const int16_t *samples = data;
     int i, j, k;
-    int frontier = 1 << avctx->trellis;
+    int frontier = 1 << trellis;
     struct TrellisNode **nodes[2];
     struct TrellisNode **nodes_next[2];
     int pathn[2] = {0, 0}, froze = -1;
@@ -139,7 +138,7 @@ static int g722_encode_trellis(AVCodecContext *avctx,
         nodes[i][0]->state = c->band[i];
     }
 
-    for (i = 0; i < buf_size; i++) {
+    for (i = 0; i < nb_samples >> 1; i++) {
         int xlow, xhigh;
         struct TrellisNode *next[2];
         int heap_pos[2] = {0, 0};
@@ -271,8 +270,28 @@ static int g722_encode_trellis(AVCodecContext *avctx,
     }
     c->band[0] = nodes[0][0]->state;
     c->band[1] = nodes[1][0]->state;
+}
+
+static av_always_inline void encode_byte(G722Context *c, uint8_t *dst,
+                                         const int16_t *samples)
+{
+    int xlow, xhigh, ilow, ihigh;
+    filter_samples(c, samples, &xlow, &xhigh);
+    ihigh = encode_high(&c->band[1], xhigh);
+    ilow  = encode_low (&c->band[0], xlow);
+    ff_g722_update_high_predictor(&c->band[1], c->band[1].scale_factor *
+                                ff_g722_high_inv_quant[ihigh] >> 10, ihigh);
+    ff_g722_update_low_predictor(&c->band[0], ilow >> 2);
+    *dst = ihigh << 6 | ilow;
+}
 
-    return i;
+static void g722_encode_no_trellis(G722Context *c,
+                                   uint8_t *dst, int nb_samples,
+                                   const int16_t *samples)
+{
+    int i;
+    for (i = 0; i < nb_samples; i += 2)
+        encode_byte(c, dst++, &samples[i]);
 }
 
 static int g722_encode_frame(AVCodecContext *avctx,
@@ -280,22 +299,16 @@ static int g722_encode_frame(AVCodecContext *avctx,
 {
     G722Context *c = avctx->priv_data;
     const int16_t *samples = data;
-    int i;
+    int nb_samples;
+
+    nb_samples = buf_size * 2;
 
     if (avctx->trellis)
-        return g722_encode_trellis(avctx, dst, buf_size, data);
+        g722_encode_trellis(c, avctx->trellis, dst, nb_samples, samples);
+    else
+        g722_encode_no_trellis(c, dst, nb_samples, samples);
 
-    for (i = 0; i < buf_size; i++) {
-        int xlow, xhigh, ihigh, ilow;
-        filter_samples(c, &samples[2*i], &xlow, &xhigh);
-        ihigh = encode_high(&c->band[1], xhigh);
-        ilow  = encode_low(&c->band[0], xlow);
-        ff_g722_update_high_predictor(&c->band[1], c->band[1].scale_factor *
-                                      ff_g722_high_inv_quant[ihigh] >> 10, ihigh);
-        ff_g722_update_low_predictor(&c->band[0], ilow >> 2);
-        *dst++ = ihigh << 6 | ilow;
-    }
-    return i;
+    return buf_size;
 }
 
 AVCodec ff_adpcm_g722_encoder = {



More information about the libav-commits mailing list