[FFmpeg-devel] [PATCH] g722 decoder, no licensing fame

Kenan Gillet kenan.gillet
Thu Apr 9 17:14:58 CEST 2009


On Thu, Apr 9, 2009 at 12:12 AM, Diego Biurrun <diego at biurrun.de> wrote:
> On Wed, Apr 08, 2009 at 09:23:51PM -0700, Kenan Gillet wrote:
>>
>> here is a version 3 of the patch:
>> - it includes the Diego's suggested changes
>
> It does not.

darn, i double checked the changes, and just ended up attaching the
wrong file :(

anyway correct patch attached,

thanks Diego

Kenan
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog	(revision 18382)
+++ Changelog	(working copy)
@@ -8,6 +8,7 @@
 - PCX encoder
 - RTP packetization of H.263
 - RTP packetization of AMR
+- G.722 ADPCM audio encoder/decoder
 
 
 
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 18382)
+++ doc/general.texi	(working copy)
@@ -482,6 +483,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  X  @tab  X
 @item ADPCM G.726            @tab  X  @tab  X
 @item ADPCM IMA AMV          @tab     @tab  X
     @tab Used in AMV files
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h	(revision 18382)
+++ libavcodec/avcodec.h	(working copy)
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVCODEC_VERSION_MAJOR 52
-#define LIBAVCODEC_VERSION_MINOR 25
+#define LIBAVCODEC_VERSION_MINOR 26
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
@@ -249,6 +249,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/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c	(revision 18382)
+++ libavcodec/allcodecs.c	(working copy)
@@ -272,6 +272,7 @@
     REGISTER_DECODER (ADPCM_EA_R2, adpcm_ea_r2);
     REGISTER_DECODER (ADPCM_EA_R3, adpcm_ea_r3);
     REGISTER_DECODER (ADPCM_EA_XAS, adpcm_ea_xas);
+    REGISTER_ENCDEC  (ADPCM_G722, adpcm_g722);
     REGISTER_ENCDEC  (ADPCM_G726, adpcm_g726);
     REGISTER_DECODER (ADPCM_IMA_AMV, adpcm_ima_amv);
     REGISTER_DECODER (ADPCM_IMA_DK3, adpcm_ima_dk3);
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 18382)
+++ libavcodec/Makefile	(working copy)
@@ -329,6 +329,8 @@
 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)         += g722.o
+OBJS-$(CONFIG_ADPCM_G722_ENCODER)         += g722.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/g722.c
===================================================================
--- libavcodec/g722.c	(revision 0)
+++ libavcodec/g722.c	(revision 0)
@@ -0,0 +1,350 @@
+/*
+ * 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/g722dec.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 bitrates - 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 separate 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 prev_qtzd_reconst;   ///< previous quantized reconstructed signal
+        int16_t pole_mem[2];         ///< second-order pole section coefficient buffer
+        int32_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 int8_t 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
+};
+
+/**
+ * quadrature mirror filter (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,
+};
+
+
+/**
+ * adaptive predictor
+ *
+ * @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_qtzd_reconst;
+
+    const int 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]^cur_diff) < 0 ? -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] = av_clip_int16(cur_diff << 1);
+
+    band->s_zero = 0;
+    for (i = 5;  i >= 0;  i--)
+        band->s_zero += (band->zero_mem[i]*band->diff_mem[i]) >> 15;
+
+
+    cur_qtzd_reconst = av_clip_int16((band->s_predictor + cur_diff) << 1);
+    band->s_predictor = av_clip_int16(band->s_zero +
+                                      MULL(band->pole_mem[0], cur_qtzd_reconst,        15) +
+                                      MULL(band->pole_mem[1], band->prev_qtzd_reconst, 15));
+    band->prev_qtzd_reconst = cur_qtzd_reconst;
+}
+
+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 << -shift : wd1 >> shift) << 2;
+}
+
+static void update_low_predictor(struct G722Band *band, const int ilow)
+{
+    do_adaptive_prediction(band, MULL(band->scale_factor, qm4[ilow], 15));
+
+    // quantizer adaptation
+    band->log_factor   = av_clip(MULL(band->log_factor, 127, 7) + qm3[ilow], 0, 18432);
+    band->scale_factor = scale(band->log_factor, 8);
+}
+
+static void update_high_predictor(struct G722Band *band, const int dhigh, const int ihigh)
+{
+    do_adaptive_prediction(band, dhigh);
+
+    // quantizer adaptation
+    band->log_factor   = av_clip(MULL(band->log_factor, 127, 7) + wh[ihigh&1], 0, 22528);
+    band->scale_factor = scale(band->log_factor, 10);
+}
+
+static void apply_qmf(int16_t *prev_samples, int *xout1, int *xout2)
+{
+    int i;
+
+    *xout1 = 0;
+    *xout2 = 0;
+    for (i = 0;  i < 12;  i++) {
+        MAC16(*xout2, prev_samples[2*i  ], qmf_coeffs[i   ]);
+        MAC16(*xout1, prev_samples[2*i+1], qmf_coeffs[11-i]);
+    }
+    memmove(prev_samples, prev_samples + 2, 22*sizeof(prev_samples[0]));
+}
+
+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:
+        if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
+            av_log(avctx, AV_LOG_ERROR, "Unsupported bitrate [%d] only 48Kb, 56Kb and 64Kb are supported.\n", avctx->bit_rate);
+            return -1;
+        }
+        av_log(avctx, AV_LOG_WARNING, "Unsupported bitrate [%d], trying with 64Kb.\n", avctx->bit_rate);
+        avctx->bit_rate    = 64000;
+        c->bits_per_sample = 8;
+    }
+
+    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;
+}
+
+#if CONFIG_ADPCM_G722_DECODER
+
+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
+};
+
+static const int16_t *qms[3] = { qm6, qm5, qm4 };
+
+static int g722_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
+{
+    G722Context *c = avctx->priv_data;
+    int16_t *out_buf = data;
+    const uint8_t *buf = avpkt->data;
+    int j, out_len = 0;
+    const int shift = 8 - c->bits_per_sample;
+    const int16_t *quantizer_table = qms[shift];
+
+    for (j = 0;  j < avpkt->size; j++) {
+        const int ilow = buf[j] & (0x3F >> shift);
+        const int rlow = av_clip(MULL(c->band[0].scale_factor, quantizer_table[ilow], 15) +
+                                 c->band[0].s_predictor, -16384, 16383);
+
+        update_low_predictor(&c->band[0], ilow >> (2 - shift));
+
+        if (avctx->sample_rate == 16000) {
+            const int ihigh = (buf[j] >> (6 - shift)) &  0x03;
+            const int dhigh = MULL(c->band[1].scale_factor, qm2[ihigh], 15);
+            const int rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383);
+            int xout1, xout2;
+
+            update_high_predictor(&c->band[1], dhigh, ihigh);
+
+            c->prev_samples[22] = rlow + rhigh;
+            c->prev_samples[23] = rlow - rhigh;
+            apply_qmf(c->prev_samples, &xout1, &xout2);
+            out_buf[out_len++] = av_clip_int16(xout1 >> 12);
+            out_buf[out_len++] = av_clip_int16(xout2 >> 12);
+        } else
+            out_buf[out_len++] = rlow;
+    }
+    *data_size = out_len << 1;
+    return avpkt->size;
+}
+
+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"),
+};
+#endif
+
+#if CONFIG_ADPCM_G722_ENCODER
+static const int16_t q6[29] = {
+   35,   72,  110,  150,  190,  233,  276,  323,
+  370,  422,  473,  530,  587,  650,  714,  786,
+  858,  940, 1023, 1121, 1219, 1339, 1458, 1612,
+  1765, 1980, 2195, 2557, 2919
+};
+
+static int g722_encode_frame(AVCodecContext *avctx,
+                             uint8_t *dst, int buf_size, void *data)
+{
+    G722Context *c = avctx->priv_data;
+    const int16_t *samples = data;
+
+    int diff, limit;
+    int i, j;
+
+    for (j = 0;  j < buf_size; ) {
+        int xlow,  rlow;
+        if (avctx->sample_rate == 16000) {
+            int xout1, xout2, xhigh, diff, pred, index;
+
+            c->prev_samples[22] = samples[j++];
+            c->prev_samples[23] = samples[j++];
+            apply_qmf(c->prev_samples, &xout1, &xout2);
+            xlow  = (xout1 + xout2) >> 14;
+            xhigh = (xout1 - xout2) >> 14;
+
+            diff = av_clip_int16(xhigh - c->band[1].s_predictor);
+            pred = MULL(564, c->band[1].scale_factor, 12);
+            index = diff >= 0 ? (diff  <  pred) + 2
+                              :  diff >= -pred;
+
+            update_high_predictor(&c->band[1], MULL(c->band[1].scale_factor, qm2[index], 15), index);
+
+            *dst = (index << 6);
+        } else
+            xlow = samples[j++];
+
+        diff = av_clip_int16(xlow - c->band[0].s_predictor);
+        limit = diff >= 0 ? diff : -(diff + 1);
+        for (i = 0;  i < 29 && limit >= (q6[i]*c->band[0].scale_factor) >> 12;  i++)
+            ;
+        *dst |=
+        rlow  = (diff < 0 ? (i < 2 ? 63 : 33)  :  61) - i;
+
+        update_low_predictor(&c->band[0], rlow >> 2);
+
+        *dst++ >>= 8 - c->bits_per_sample;
+    }
+    return avctx->sample_rate == 8000 ? buf_size : buf_size >> 1;
+}
+
+AVCodec adpcm_g722_encoder = {
+    .name           = "g722",
+    .type           = CODEC_TYPE_AUDIO,
+    .id             = CODEC_ID_ADPCM_G722,
+    .priv_data_size = sizeof(G722Context),
+    .init           = g722_init,
+    .encode         = g722_encode_frame,
+    .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
+    .sample_fmts    = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
+};
+
+#endif
-------------- next part --------------
Index: libavformat/avformat.h
===================================================================
--- libavformat/avformat.h	(revision 18382)
+++ libavformat/avformat.h	(working copy)
@@ -22,7 +22,7 @@
 #define AVFORMAT_AVFORMAT_H
 
 #define LIBAVFORMAT_VERSION_MAJOR 52
-#define LIBAVFORMAT_VERSION_MINOR 32
+#define LIBAVFORMAT_VERSION_MINOR 33
 #define LIBAVFORMAT_VERSION_MICRO  0
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
Index: libavformat/allformats.c
===================================================================
--- libavformat/allformats.c	(revision 18382)
+++ libavformat/allformats.c	(working copy)
@@ -83,6 +83,7 @@
     REGISTER_MUXDEMUX (FLV, flv);
     REGISTER_DEMUXER  (FOURXM, fourxm);
     REGISTER_MUXER    (FRAMECRC, framecrc);
+    REGISTER_MUXDEMUX (G722, g722);
     REGISTER_MUXER    (GIF, gif);
     REGISTER_DEMUXER  (GSM, gsm);
     REGISTER_MUXDEMUX (GXF, gxf);
Index: libavformat/raw.c
===================================================================
--- libavformat/raw.c	(revision 18382)
+++ libavformat/raw.c	(working copy)
@@ -782,6 +782,36 @@
 };
 #endif
 
+#if CONFIG_G722_DEMUXER
+AVInputFormat g722_demuxer = {
+    "g722",
+    NULL_IF_CONFIG_SMALL("raw G.722"),
+    0,
+    NULL,
+    raw_read_header,
+    ff_raw_read_partial_packet,
+    .flags= AVFMT_GENERIC_INDEX,
+    .extensions = "g722,722",
+    .value = CODEC_ID_ADPCM_G722,
+};
+#endif
+
+#if CONFIG_G722_MUXER
+AVOutputFormat g722_muxer = {
+    "g722",
+    NULL_IF_CONFIG_SMALL("raw G.722"),
+    "audio/G722",
+    "g722",
+    0,
+    CODEC_ID_ADPCM_G722,
+    CODEC_ID_NONE,
+    NULL,
+    raw_write_packet,
+    .flags= AVFMT_NOTIMESTAMPS,
+};
+#endif
+
+
 #if CONFIG_GSM_DEMUXER
 AVInputFormat gsm_demuxer = {
     "gsm",



More information about the ffmpeg-devel mailing list