The fate reference is updated because the previous test skipped a sample in
each encode() call due each input frame having an odd number of samples.
---
 libavcodec/g722enc.c  |   62 +++++++++++++++++++++++++++++++++---------------
 tests/ref/acodec/g722 |    8 +++---
 2 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/libavcodec/g722enc.c b/libavcodec/g722enc.c
index 1eed784..7b89955 100644
--- a/libavcodec/g722enc.c
+++ b/libavcodec/g722enc.c
@@ -56,6 +56,8 @@ static av_cold int g722_encode_init(AVCodecContext * avctx)
         }
     }
 
+    avctx->frame_size = PREV_SAMPLES_BUF_SIZE - 22;
+
     return 0;
 }
 
@@ -117,13 +119,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 +140,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 +272,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 +301,22 @@ static int g722_encode_frame(AVCodecContext *avctx,
 {
     G722Context *c = avctx->priv_data;
     const int16_t *samples = data;
-    int i;
+    int nb_samples;
 
-    if (avctx->trellis)
-        return g722_encode_trellis(avctx, dst, buf_size, data);
+    nb_samples = avctx->frame_size - (avctx->frame_size & 1);
 
-    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;
+    if (avctx->trellis)
+        g722_encode_trellis(c, avctx->trellis, dst, nb_samples, data);
+    else
+        g722_encode_no_trellis(c, dst, nb_samples, data);
+
+    /* handle last frame with odd frame_size */
+    if (nb_samples < avctx->frame_size) {
+        int16_t last_samples[2] = { samples[nb_samples], samples[nb_samples] };
+        encode_byte(c, &dst[nb_samples >> 1], last_samples);
     }
-    return i;
+
+    return (avctx->frame_size + 1) >> 1;
 }
 
 AVCodec ff_adpcm_g722_encoder = {
@@ -306,6 +327,7 @@ AVCodec ff_adpcm_g722_encoder = {
     .init           = g722_encode_init,
     .close          = g722_encode_close,
     .encode         = g722_encode_frame,
+    .capabilities   = CODEC_CAP_SMALL_LAST_FRAME,
     .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
     .sample_fmts    = (const enum 
AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
 };
diff --git a/tests/ref/acodec/g722 b/tests/ref/acodec/g722
index a1fc72a..6ea492a 100644
--- a/tests/ref/acodec/g722
+++ b/tests/ref/acodec/g722
@@ -1,4 +1,4 @@
-b380355e0360b4e50ee78f33fd60a0f5 *./tests/data/acodec/g722.wav
-47991 ./tests/data/acodec/g722.wav
-82fdd5bb059336e0550de7ba5947c5bb *./tests/data/g722.acodec.out.wav
-stddev: 8860.44 PSNR: 17.38 MAXDIFF:33814 bytes:   191732/  1058400
+1975cc4a3521e374b33ae042e182f6b6 *./tests/data/acodec/g722.wav
+48053 ./tests/data/acodec/g722.wav
+ade04cdcf249e6946395f109b077dd62 *./tests/data/g722.acodec.out.wav
+stddev: 8841.24 PSNR: 17.40 MAXDIFF:36225 bytes:   191980/  1058400
-- 
1.7.1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to