]> git.sesse.net Git - ffmpeg/commitdiff
avcodec: add adpcm_argo encoder
authorZane van Iperen <zane@zanevaniperen.com>
Sun, 26 Jul 2020 14:23:14 +0000 (00:23 +1000)
committerZane van Iperen <zane@zanevaniperen.com>
Fri, 7 Aug 2020 13:04:28 +0000 (23:04 +1000)
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
Changelog
doc/general.texi
libavcodec/Makefile
libavcodec/adpcmenc.c
libavcodec/allcodecs.c
libavcodec/utils.c
libavcodec/version.h

index 959a2289fff9318fa85b28009a79075f58f2e7a4..0f0fbf2abb830dca494f50056eca6a7bfc1e9ca5 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -11,6 +11,7 @@ version <next>:
 - Rayman 2 APM muxer
 - AV1 encoding support SVT-AV1
 - Cineform HD encoder
+- ADPCM Argonaut Games encoder
 
 
 version 4.3:
index dfcfd394e67c25a962dad4d1eb5279316eb0435e..bfe2e9841649437272a3831b453c98b92cad2bad 100644 (file)
@@ -1098,7 +1098,7 @@ following image formats are supported:
 @item ACELP.KELVIN           @tab     @tab  X
 @item ADPCM 4X Movie         @tab     @tab  X
 @item APDCM Yamaha AICA      @tab     @tab  X
-@item ADPCM Argonaut Games   @tab     @tab  X
+@item ADPCM Argonaut Games   @tab X   @tab  X
 @item ADPCM CDROM XA         @tab     @tab  X
 @item ADPCM Creative Technology @tab     @tab  X
     @tab 16 -> 4, 8 -> 4, 8 -> 3, 8 -> 2
index 1417aeef9892ab604ac42336dedcf607d6267871..fc4294816e21bf0a318b3dba6222b5aae676e408 100644 (file)
@@ -837,6 +837,7 @@ 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_ARGO_DECODER)         += adpcm.o adpcm_data.o
+OBJS-$(CONFIG_ADPCM_ARGO_ENCODER)         += adpcm.o adpcmenc.o
 OBJS-$(CONFIG_ADPCM_CT_DECODER)           += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_DTK_DECODER)          += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_EA_DECODER)           += adpcm.o adpcm_data.o
index adb7bf0bbf719c9ee2c7f242ae1d2e40758359ce..24bd31c4a9cfc0904e0efc1b9b6f6127e7991719 100644 (file)
@@ -78,7 +78,8 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         }
 
         if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
-            avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM) {
+            avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
+            avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
             /*
              * The current trellis implementation doesn't work for extended
              * runs of samples without periodic resets. Disallow it.
@@ -156,6 +157,10 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
             return AVERROR(ENOMEM);
         avctx->extradata_size = 28;
         break;
+    case AV_CODEC_ID_ADPCM_ARGO:
+        avctx->frame_size = 32;
+        avctx->block_align = 17 * avctx->channels;
+        break;
     default:
         return AVERROR(EINVAL);
     }
@@ -481,6 +486,46 @@ static void adpcm_compress_trellis(AVCodecContext *avctx,
     c->idelta     = nodes[0]->step;
 }
 
+static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
+                                             int shift, int flag)
+{
+    int nibble;
+
+    if (flag)
+        nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
+    else
+        nibble = 4 * s - 4 * cs->sample1;
+
+    return (nibble >> shift) & 0x0F;
+}
+
+static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
+                                         const int16_t *samples, int nsamples,
+                                         int shift, int flag)
+{
+    int64_t error = 0;
+
+    if (pb) {
+        put_bits(pb, 4, shift - 2);
+        put_bits(pb, 1, 0);
+        put_bits(pb, 1, !!flag);
+        put_bits(pb, 2, 0);
+    }
+
+    for (int n = 0; n < nsamples; n++) {
+        /* Compress the nibble, then expand it to see how much precision we've lost. */
+        int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
+        int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
+
+        error += abs(samples[n] - sample);
+
+        if (pb)
+            put_bits(pb, 4, nibble);
+    }
+
+    return error;
+}
+
 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                               const AVFrame *frame, int *got_packet_ptr)
 {
@@ -741,6 +786,44 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         flush_put_bits(&pb);
         break;
     }
+    case AV_CODEC_ID_ADPCM_ARGO:
+    {
+        PutBitContext pb;
+        init_put_bits(&pb, dst, pkt_size);
+
+        av_assert0(frame->nb_samples == 32);
+
+        for (ch = 0; ch < avctx->channels; ch++) {
+            int64_t error  = INT64_MAX, tmperr = INT64_MAX;
+            int     shift  = 2, flag = 0;
+            int     saved1 = c->status[ch].sample1;
+            int     saved2 = c->status[ch].sample2;
+
+            /* Find the optimal coefficients, bail early if we find a perfect result. */
+            for (int s = 2; s < 18 && tmperr != 0; s++) {
+                for (int f = 0; f < 2 && tmperr != 0; f++) {
+                    c->status[ch].sample1 = saved1;
+                    c->status[ch].sample2 = saved2;
+                    tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
+                                                       frame->nb_samples, s, f);
+                    if (tmperr < error) {
+                        shift = s;
+                        flag  = f;
+                        error = tmperr;
+                    }
+                }
+            }
+
+            /* Now actually do the encode. */
+            c->status[ch].sample1 = saved1;
+            c->status[ch].sample2 = saved2;
+            adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
+                                      frame->nb_samples, shift, flag);
+        }
+
+        flush_put_bits(&pb);
+        break;
+    }
     default:
         return AVERROR(EINVAL);
     }
@@ -773,6 +856,7 @@ AVCodec ff_ ## name_ ## _encoder = {                                       \
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
 }
 
+ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO,    adpcm_argo,    sample_fmts_p, 0,                             "ADPCM Argonaut Games");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,                             "ADPCM IMA QuickTime");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
index 8d1b15ab8528325d83e662040bae1f81b1316c66..4bd830e5d0b033ae077d78c92159e7c74a446c10 100644 (file)
@@ -591,6 +591,7 @@ extern AVCodec ff_adpcm_afc_decoder;
 extern AVCodec ff_adpcm_agm_decoder;
 extern AVCodec ff_adpcm_aica_decoder;
 extern AVCodec ff_adpcm_argo_decoder;
+extern AVCodec ff_adpcm_argo_encoder;
 extern AVCodec ff_adpcm_ct_decoder;
 extern AVCodec ff_adpcm_dtk_decoder;
 extern AVCodec ff_adpcm_ea_decoder;
index a379d62802084d6701f8301dfdff8a60958c7733..5a2a90b0307158809fd914661e31ac89ac855617 100644 (file)
@@ -1479,6 +1479,7 @@ int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
     switch (codec_id) {
     case AV_CODEC_ID_8SVX_EXP:
     case AV_CODEC_ID_8SVX_FIB:
+    case AV_CODEC_ID_ADPCM_ARGO:
     case AV_CODEC_ID_ADPCM_CT:
     case AV_CODEC_ID_ADPCM_IMA_APC:
     case AV_CODEC_ID_ADPCM_IMA_APM:
index f66919617aa4fbf7c4d5a516cf57226b7adec2d1..a3f9f828ee4ee9def79694706bc384e10be2b5db 100644 (file)
@@ -28,7 +28,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  58
-#define LIBAVCODEC_VERSION_MINOR  99
+#define LIBAVCODEC_VERSION_MINOR 100
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \