diff mbox

[FFmpeg-devel,3/3] avcodec: add ADPCM AGM decoder

Message ID 20190410114553.16723-3-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol April 10, 2019, 11:45 a.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/Makefile     |  1 +
 libavcodec/adpcm.c      | 58 +++++++++++++++++++++++++++++++++++++++++
 libavcodec/allcodecs.c  |  1 +
 libavcodec/avcodec.h    |  1 +
 libavcodec/codec_desc.c |  7 +++++
 libavformat/riff.c      |  1 +
 6 files changed, 69 insertions(+)

Comments

Paul B Mahol April 10, 2019, 5:40 p.m. UTC | #1
On 4/10/19, Paul B Mahol <onemda@gmail.com> wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavcodec/Makefile     |  1 +
>  libavcodec/adpcm.c      | 58 +++++++++++++++++++++++++++++++++++++++++
>  libavcodec/allcodecs.c  |  1 +
>  libavcodec/avcodec.h    |  1 +
>  libavcodec/codec_desc.c |  7 +++++
>  libavformat/riff.c      |  1 +
>  6 files changed, 69 insertions(+)
>

Gonna apply this set soon.
diff mbox

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index de873c1643..02849f91ec 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -808,6 +808,7 @@  OBJS-$(CONFIG_ADPCM_4XM_DECODER)          += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_ADX_DECODER)          += adxdec.o adx.o
 OBJS-$(CONFIG_ADPCM_ADX_ENCODER)          += adxenc.o adx.o
 OBJS-$(CONFIG_ADPCM_AFC_DECODER)          += adpcm.o adpcm_data.o
+OBJS-$(CONFIG_ADPCM_AGM_DECODER)          += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_AICA_DECODER)         += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_CT_DECODER)           += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_DTK_DECODER)          += adpcm.o adpcm_data.o
diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index aa9c7c5c4f..a607b2cc70 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -177,6 +177,50 @@  static av_cold int adpcm_decode_init(AVCodecContext * avctx)
     return 0;
 }
 
+static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
+{
+    int delta, pred, step, add;
+
+    pred = c->predictor;
+    delta = nibble & 7;
+    step = c->step;
+    add = (delta * 2 + 1) * step;
+    if (add < 0)
+        add = add + 7;
+
+    if ((nibble & 8) == 0)
+        pred = av_clip(pred + (add >> 3), -32767, 32767);
+    else
+        pred = av_clip(pred - (add >> 3), -32767, 32767);
+
+    switch (delta) {
+    case 7:
+        step *= 0x99;
+        break;
+    case 6:
+        c->step = av_clip(c->step * 2, 127, 24576);
+        c->predictor = pred;
+        return pred;
+    case 5:
+        step *= 0x66;
+        break;
+    case 4:
+        step *= 0x4d;
+        break;
+    default:
+        step *= 0x39;
+        break;
+    }
+
+    if (step < 0)
+        step += 0x3f;
+
+    c->step = step >> 6;
+    c->step = av_clip(c->step, 127, 24576);
+    c->predictor = pred;
+    return pred;
+}
+
 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
 {
     int step_index;
@@ -549,6 +593,7 @@  static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
     header_size = 0;
     switch (avctx->codec->id) {
         case AV_CODEC_ID_ADPCM_4XM:
+        case AV_CODEC_ID_ADPCM_AGM:
         case AV_CODEC_ID_ADPCM_IMA_DAT4:
         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
         case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
@@ -863,6 +908,18 @@  static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
             }
         }
         break;
+    case AV_CODEC_ID_ADPCM_AGM:
+        for (i = 0; i < avctx->channels; i++)
+            c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
+        for (i = 0; i < avctx->channels; i++)
+            c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
+
+        for (n = 0; n < nb_samples; n++) {
+            int v = bytestream2_get_byteu(&gb);
+            *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
+            *samples++ = adpcm_agm_expand_nibble(&c->status[avctx->channels > 1], v >> 4 );
+        }
+        break;
     case AV_CODEC_ID_ADPCM_MS:
     {
         int block_predictor;
@@ -1729,6 +1786,7 @@  AVCodec ff_ ## name_ ## _decoder = {                        \
 /* Note: Do not forget to add new entries to the Makefile as well. */
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
+ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM,         sample_fmts_s16,  adpcm_agm,         "ADPCM AmuseGraphics Movie");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA,        sample_fmts_s16p, adpcm_aica,        "ADPCM Yamaha AICA");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 71fd74a07e..9ae8a779b3 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -574,6 +574,7 @@  extern AVCodec ff_adpcm_4xm_decoder;
 extern AVCodec ff_adpcm_adx_encoder;
 extern AVCodec ff_adpcm_adx_decoder;
 extern AVCodec ff_adpcm_afc_decoder;
+extern AVCodec ff_adpcm_agm_decoder;
 extern AVCodec ff_adpcm_aica_decoder;
 extern AVCodec ff_adpcm_ct_decoder;
 extern AVCodec ff_adpcm_dtk_decoder;
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index cafc65fce5..4218cff6c4 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -539,6 +539,7 @@  enum AVCodecID {
     AV_CODEC_ID_ADPCM_AICA,
     AV_CODEC_ID_ADPCM_IMA_DAT4,
     AV_CODEC_ID_ADPCM_MTAF,
+    AV_CODEC_ID_ADPCM_AGM,
 
     /* AMR */
     AV_CODEC_ID_AMR_NB = 0x12000,
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 80c21a8c6c..8295221d15 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2255,6 +2255,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("ADPCM MTAF"),
         .props     = AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_ADPCM_AGM,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "adpcm_agm",
+        .long_name = NULL_IF_CONFIG_SMALL("ADPCM AmuseGraphics Movie AGM"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
 
     /* AMR */
     {
diff --git a/libavformat/riff.c b/libavformat/riff.c
index 394481becd..741a6515df 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -605,5 +605,6 @@  const AVCodecGuid ff_codec_wav_guids[] = {
     { AV_CODEC_ID_ATRAC9,   { 0xD2, 0x42, 0xE1, 0x47, 0xBA, 0x36, 0x8D, 0x4D, 0x88, 0xFC, 0x61, 0x65, 0x4F, 0x8C, 0x83, 0x6C } },
     { AV_CODEC_ID_EAC3,     { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } },
     { AV_CODEC_ID_MP2,      { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
+    { AV_CODEC_ID_ADPCM_AGM,{ 0x82, 0xEC, 0x1F, 0x6A, 0xCA, 0xDB, 0x19, 0x45, 0xBD, 0xE7, 0x56, 0xD3, 0xB3, 0xEF, 0x98, 0x1D } },
     { AV_CODEC_ID_NONE }
 };