[FFmpeg-devel] [PATCH] G722 decoder

Kenan Gillet kenan.gillet
Sat Mar 21 08:43:35 CET 2009


Hi,

Here is an implementation of a bitexact G.722 decoder.
It is based upon the patch G722_decoder_by_Chas_Williams at [1]
and has been tested against the reference implementation found in ITU-G.711 .

A sample can be found at /MPlayer/incoming/g722decoder
In order to play the file, we need to be able force the bitrate from
the comment line  (patch #1)
and to use a specific demuxer (patch #2)

So far I have only found 64kbps / 16Khz raw samples, but i tested the
other bitrate (48kbps, 56 kbps)
against the reference implementation

you can test the uploaded sample with the command:
ffmpeg -ac 1 -ab 64000 -ar 16000 -i conf-adminmenu-162.g722 output.wav

Thanks,

Kenan

[1] http://wiki.multimedia.cx/index.php?title=Interesting_Patches#G722_decoder_by_Chas_Williams:
-------------- next part --------------
Index: ffmpeg.c
===================================================================
--- ffmpeg.c	(revision 18096)
+++ ffmpeg.c	(working copy)
@@ -150,6 +150,7 @@
 
 static int intra_only = 0;
 static int audio_sample_rate = 44100;
+static int audio_bit_rate;
 static int64_t channel_layout = 0;
 #define QSCALE_NONE -99999
 static float audio_qscale = QSCALE_NONE;
@@ -2345,12 +2346,14 @@
 static int opt_bitrate(const char *opt, const char *arg)
 {
     int codec_type = opt[0]=='a' ? CODEC_TYPE_AUDIO : CODEC_TYPE_VIDEO;
+    int bit_rate;
 
     opt_default(opt, arg);
 
-    if (av_get_int(avcodec_opts[codec_type], "b", NULL) < 1000)
+    if ((bit_rate = av_get_int(avcodec_opts[codec_type], "b", NULL)) < 1000)
         fprintf(stderr, "WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s\n");
-
+    if (codec_type == CODEC_TYPE_AUDIO)
+        audio_bit_rate = bit_rate;
     return 0;
 }
 
@@ -2819,6 +2822,7 @@
     memset(ap, 0, sizeof(*ap));
     ap->prealloced_context = 1;
     ap->sample_rate = audio_sample_rate;
+    ap->bit_rate = audio_bit_rate;
     ap->channels = audio_channels;
     ap->time_base.den = frame_rate.num;
     ap->time_base.num = frame_rate.den;
@@ -2892,6 +2896,7 @@
             channel_layout = enc->channel_layout;
             audio_channels = enc->channels;
             audio_sample_rate = enc->sample_rate;
+            audio_bit_rate = enc->bit_rate;
             audio_sample_fmt = enc->sample_fmt;
             input_codecs[nb_icodecs++] = avcodec_find_decoder_by_name(audio_codec_name);
             if(audio_disable)
@@ -3211,6 +3216,7 @@
     }
     nb_ocodecs++;
     audio_enc->sample_rate = audio_sample_rate;
+    audio_enc->bit_rate = audio_bit_rate;
     audio_enc->time_base= (AVRational){1, audio_sample_rate};
     if (audio_language) {
         av_metadata_set(&st->metadata, "language", audio_language);
Index: libavformat/avformat.h
===================================================================
--- libavformat/avformat.h	(revision 18096)
+++ libavformat/avformat.h	(working copy)
@@ -261,6 +261,7 @@
     enum CodecID video_codec_id;
     enum CodecID audio_codec_id;
 #endif
+    int bit_rate;
 } AVFormatParameters;
 
 //! Demuxer will use url_fopen, no opened file should be provided by the caller.
Index: libavformat/raw.c
===================================================================
--- libavformat/raw.c	(revision 18096)
+++ libavformat/raw.c	(working copy)
@@ -82,6 +82,7 @@
         switch(st->codec->codec_type) {
         case CODEC_TYPE_AUDIO:
             st->codec->sample_rate = ap->sample_rate;
+            st->codec->bit_rate = ap->bit_rate;
             if(ap->channels) st->codec->channels = ap->channels;
             else             st->codec->channels = 1;
             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog	(revision 18096)
+++ Changelog	(working copy)
@@ -6,6 +6,7 @@
 - VQF demuxer
 - Alpha channel scaler
 - PCX encoder
+- G.722 ADPCM audio decoder
 
 
 
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 18096)
+++ doc/general.texi	(working copy)
@@ -482,6 +482,7 @@
 @item ADPCM Electronic Arts R2  @tab     @tab  X
 @item ADPCM Electronic Arts R3  @tab     @tab  X
 @item ADPCM Electronic Arts XAS @tab     @tab  X
+ at item ADPCM G.722            @tab     @tab  X
 @item ADPCM G.726            @tab  X  @tab  X
 @item ADPCM IMA AMV          @tab     @tab  X
     @tab Used in AMV files
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 18096)
+++ libavcodec/Makefile	(working copy)
@@ -318,6 +318,7 @@
 OBJS-$(CONFIG_ADPCM_EA_R2_DECODER)        += adpcm.o
 OBJS-$(CONFIG_ADPCM_EA_R3_DECODER)        += adpcm.o
 OBJS-$(CONFIG_ADPCM_EA_XAS_DECODER)       += adpcm.o
+OBJS-$(CONFIG_ADPCM_G722_DECODER)         += g722dec.o
 OBJS-$(CONFIG_ADPCM_G726_DECODER)         += g726.o
 OBJS-$(CONFIG_ADPCM_G726_ENCODER)         += g726.o
 OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o
Index: libavcodec/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c	(revision 18096)
+++ libavcodec/allcodecs.c	(working copy)
@@ -268,6 +268,7 @@
     REGISTER_DECODER (ADPCM_EA_R3, adpcm_ea_r3);
     REGISTER_DECODER (ADPCM_EA_XAS, adpcm_ea_xas);
     REGISTER_ENCDEC  (ADPCM_G726, adpcm_g726);
+    REGISTER_DECODER (ADPCM_G722, adpcm_g722);
     REGISTER_DECODER (ADPCM_IMA_AMV, adpcm_ima_amv);
     REGISTER_DECODER (ADPCM_IMA_DK3, adpcm_ima_dk3);
     REGISTER_DECODER (ADPCM_IMA_DK4, adpcm_ima_dk4);
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h	(revision 18096)
+++ libavcodec/avcodec.h	(working copy)
@@ -247,6 +247,7 @@
     CODEC_ID_ADPCM_EA_XAS,
     CODEC_ID_ADPCM_EA_MAXIS_XA,
     CODEC_ID_ADPCM_IMA_ISS,
+    CODEC_ID_ADPCM_G722,
 
     /* AMR */
     CODEC_ID_AMR_NB= 0x12000,
Index: libavcodec/g722dec.c
===================================================================
--- libavcodec/g722dec.c	(revision 0)
+++ libavcodec/g722dec.c	(revision 0)
@@ -0,0 +1,275 @@
+/*
+ * G.722 ADPCM audio decoder
+ *
+ * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
+ * Copyright (c) CMU 1993 Computer Science, Speech Group
+ *                        Chengxiang Lu and Alex Hauptmann
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+/**
+ * @file libavcodec/g722.c
+ *
+ * G.722 ADPCM audio codec
+ *
+ * This G.722 decoder is a bit exact implementation of the ITU G.722 specification
+ * for all three specified bit rates - 64000bps, 56000bps and 48000bps.
+ * It passes the ITU tests.
+ *
+ * @note For the 56000bps and 48000bps bitrates, the respectively 7 and 6 bits
+ *       codeword might be packed, so unpacking might be needed either
+ *       internally or as a seprate parser.
+ */
+
+#include <stdint.h>
+#include "avcodec.h"
+#include "mathops.h"
+
+typedef struct {
+    int bits_per_sample;             ///< 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps
+
+    int16_t prev_samples[24];        ///< memory of past 24 received (decoded) samples
+
+    /**
+     * The band[0] and band[1] correspond respectively to the lower band and higher band.
+     */
+    struct G722Band {
+        int16_t s_predictor;         ///< predictor output value
+        int32_t s_zero;              ///< zero section output signal
+        int8_t  part_reconst_mem[2]; ///< partially reconstructed signal memory
+        int16_t qtzd_reconst_mem[2]; ///< quantized reconstructed signal
+        int16_t pole_mem[2];         ///< second-order pole section coefficient buffer
+        int16_t diff_mem[6];         ///< quantizer difference signal memory
+        int16_t zero_mem[6];         ///< Seventh-order zero section coefficient buffer
+        int16_t log_factor;          ///< delayed logarithmic quantizer factor
+        int16_t scale_factor;        ///< delayed quantizer scale factor
+    } band[2];
+} G722Context;
+
+
+static const int sign_lookup[2] = { -1, 1 };
+
+static const int16_t ilb[32] = {
+  2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383,
+  2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834,
+  2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371,
+  3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008
+};
+static const int16_t wh[2]   = { 798, -214 };
+static const int16_t qm2[4]  = { -7408, -1616,  7408,   1616 };
+/**
+ * qm3[index] == wl[rl42[index]]
+ */
+static const int16_t qm3[16] = {
+   -60, 3042, 1198, 538, 334, 172,  58, -30,
+  3042, 1198,  538, 334, 172,  58, -30, -60
+};
+static const int16_t qm4[16] = {
+      0, -20456, -12896,  -8968, -6288,  -4240,  -2584,  -1200,
+  20456,  12896,   8968,  6288,   4240,   2584,   1200,      0
+};
+static const int16_t qm5[32] = {
+   -280,   -280, -23352, -17560, -14120, -11664, -9752, -8184,
+  -6864,  -5712,  -4696,  -3784,  -2960,  -2208, -1520,  -880,
+  23352,  17560,  14120,  11664,   9752,   8184,  6864,  5712,
+   4696,   3784,   2960,   2208,   1520,    880,   280,  -280
+};
+static const int16_t qm6[64] = {
+    -136,   -136,   -136,   -136, -24808, -21904, -19008, -16704,
+  -14984, -13512, -12280, -11192, -10232,  -9360,  -8576,  -7856,
+   -7192,  -6576,  -6000,  -5456,  -4944,  -4464,  -4008,  -3576,
+   -3168,  -2776,  -2400,  -2032,  -1688,  -1360,  -1040,   -728,
+   24808,  21904,  19008,  16704,  14984,  13512,  12280,  11192,
+   10232,   9360,   8576,   7856,   7192,   6576,   6000,   5456,
+    4944,   4464,   4008,   3576,   3168,   2776,   2400,   2032,
+    1688,   1360,   1040,    728,    432,    136,   -432,   -136
+};
+
+/**
+ * quadrature mirror filters (QMF) coefficients
+ *
+ * ITU-T G.722 Table 11
+ */
+static const int16_t qmf_coeffs[12] = {
+  3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
+};
+
+
+/**
+ * adpative preditor
+ *
+ * @note On x86 using the MULL macro in a loop is slower than not using the macro.
+ */
+static void do_adaptive_prediction(struct G722Band *band, const int cur_diff)
+{
+    int sg[2], limit, i, cur_part_reconst;
+
+    band->qtzd_reconst_mem[1] = band->qtzd_reconst_mem[0];
+    band->qtzd_reconst_mem[0] = av_clip_int16((band->s_predictor + cur_diff) << 1);
+
+    cur_part_reconst = band->s_zero + cur_diff < 0;
+
+    sg[0] = sign_lookup[cur_part_reconst != band->part_reconst_mem[0]];
+    sg[1] = sign_lookup[cur_part_reconst == band->part_reconst_mem[1]];
+    band->part_reconst_mem[1] = band->part_reconst_mem[0];
+    band->part_reconst_mem[0] = cur_part_reconst;
+
+    band->pole_mem[1] = av_clip((sg[0] * av_clip(band->pole_mem[0], -8191, 8191) >> 5) +
+                                (sg[1] << 7) + MULL(band->pole_mem[1], 127, 7), -12288, 12288);
+
+    limit = 15360 - band->pole_mem[1];
+    band->pole_mem[0] = av_clip(-192 * sg[0] + MULL(band->pole_mem[0], 255, 8), -limit, limit);
+
+
+    if(cur_diff) {
+        for (i = 0;  i < 6;  i++)
+            band->zero_mem[i] = ((band->zero_mem[i]*255) >> 8) +
+                                 (band->diff_mem[i] >> 15 == cur_diff >> 15 ? 128 : -128);
+    } else
+        for (i = 0; i < 6; i++)
+            band->zero_mem[i] = (band->zero_mem[i]*255) >> 8;
+
+    for (i = 5;  i > 0;  i--)
+        band->diff_mem[i] = band->diff_mem[i-1];
+    band->diff_mem[0] = cur_diff;
+
+    band->s_zero = 0;
+    for (i = 5;  i >= 0;  i--)
+        band->s_zero += (band->zero_mem[i]*av_clip_int16(band->diff_mem[i] << 1)) >> 15;
+    band->s_zero = av_clip_int16(band->s_zero);
+
+
+    band->s_predictor = av_clip_int16(band->s_zero +
+                                      MULL(band->pole_mem[0], band->qtzd_reconst_mem[0], 15) +
+                                      MULL(band->pole_mem[1], band->qtzd_reconst_mem[1], 15));
+}
+
+static int inline scale(const int log_factor, int shift) {
+    const int wd1 = ilb[(log_factor >> 6) & 31];
+    shift -= log_factor >> 11;
+    return shift < 0 ? wd1 << (2-shift) : (wd1 >> shift) << 2;
+}
+
+static int g722_decode_frame(AVCodecContext *avctx,
+                             void *data, int *data_size,
+                             const uint8_t *buf, const int buf_size)
+{
+    G722Context *c = avctx->priv_data;
+    int16_t *out_buf = data;
+    int j, out_len = 0;
+
+    for (j = 0;  j < buf_size; j++) {
+        const int ihigh = (buf[j] >> (c->bits_per_sample-2)) & 0x03;
+        int rlow, rhigh, ilow;
+
+        // inverse adaptive quantizer
+        if (c->bits_per_sample == 8) {
+            ilow = buf[j] & 0x3F;
+            rlow = qm6[ilow];
+            ilow >>= 2;
+        } else if (c->bits_per_sample == 7) {
+            ilow = buf[j] & 0x1F;
+            rlow = qm5[ilow];
+            ilow >>= 1;
+        } else {
+            ilow = buf[j] & 0x0F;
+            rlow = qm4[ilow];
+        }
+        rlow = av_clip(MULL(c->band[0].scale_factor, rlow, 15) + c->band[0].s_predictor,
+                       -16384, 16383);
+
+        do_adaptive_prediction(&c->band[0], MULL(c->band[0].scale_factor, qm4[ilow], 15));
+
+        // quantizer adaptation
+        c->band[0].log_factor = av_clip(MULL(c->band[0].log_factor, 127, 7) + qm3[ilow], 0, 18432);
+        c->band[0].scale_factor = scale(c->band[0].log_factor, 8);
+
+        if (avctx->sample_rate == 16000) {
+            int xout1 = 0, xout2 = 0, i;
+            const int dhigh = MULL(c->band[1].scale_factor, qm2[ihigh], 15);
+
+            // inverse adaptive quantizer
+            rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383);
+
+            do_adaptive_prediction(&c->band[1], dhigh);
+
+            // quantizer adaptation
+            c->band[1].log_factor   = av_clip(MULL(c->band[1].log_factor, 127, 7) + wh[ihigh&1],
+                                              0, 22528);
+            c->band[1].scale_factor = scale(c->band[1].log_factor, 10);
+
+            // Apply the receive QMF
+            memmove(c->prev_samples, c->prev_samples + 2, 22*sizeof(c->prev_samples[0]));
+            c->prev_samples[22] = rlow + rhigh;
+            c->prev_samples[23] = rlow - rhigh;
+
+            for (i = 0;  i < 12;  i++) {
+                MAC16(xout2, c->prev_samples[2*i  ], qmf_coeffs[i   ]);
+                MAC16(xout1, c->prev_samples[2*i+1], qmf_coeffs[11-i]);
+            }
+            out_buf[out_len++] = av_clip_int16(xout1 >> 12);
+            out_buf[out_len++] = av_clip_int16(xout2 >> 12);
+        } else
+            out_buf[out_len++] = (int16_t) rlow;
+    }
+    *data_size = out_len << 1;
+    return buf_size;
+}
+
+
+static av_cold int g722_init(AVCodecContext * avctx)
+{
+    G722Context *c = (G722Context *) avctx->priv_data;
+
+    if (avctx->channels != 1) {
+        av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
+        return -1;
+    }
+
+    switch (avctx->bit_rate) {
+    case 64000:
+    case 56000:
+    case 48000:
+        c->bits_per_sample = avctx->bit_rate/8000;
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Unsupported bitrate [%d] only 48Kb, 56Kb and 64Kb are supported.\n", avctx->bit_rate);
+        return -1;
+    }
+
+    if (avctx->sample_rate != 8000 && avctx->sample_rate != 16000) {
+        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate [%d] only 8KHz and 16KHz are supported.\n", avctx->sample_rate);
+        return -1;
+    }
+
+    c->band[0].scale_factor = 32;
+    c->band[1].scale_factor = 8;
+
+    return 0;
+}
+
+AVCodec adpcm_g722_decoder = {
+    .name           = "g722",
+    .type           = CODEC_TYPE_AUDIO,
+    .id             = CODEC_ID_ADPCM_G722,
+    .priv_data_size = sizeof(G722Context),
+    .init           = g722_init,
+    .decode         = g722_decode_frame,
+    .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
+};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 3-add-g722_muxdemux.patch
Type: application/octet-stream
Size: 1317 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090321/bbffd22c/attachment.obj>



More information about the ffmpeg-devel mailing list