[FFmpeg-cvslog] avcodec: add adpcm_ima_amv encoder

Zane van Iperen git at videolan.org
Mon Nov 9 07:12:32 EET 2020


ffmpeg | branch: master | Zane van Iperen <zane at zanevaniperen.com> | Mon Nov  2 15:47:29 2020 +1000| [555f5c1fc5ae0c4e7b0431dc3166c3fcf3f4e979] | committer: Zane van Iperen

avcodec: add adpcm_ima_amv encoder

Fixes ticket #747.

Signed-off-by: Zane van Iperen <zane at zanevaniperen.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=555f5c1fc5ae0c4e7b0431dc3166c3fcf3f4e979
---

 doc/general_contents.texi |  2 +-
 libavcodec/adpcmenc.c     | 58 ++++++++++++++++++++++++++++++++++++++++++++++-
 libavcodec/allcodecs.c    |  1 +
 libavcodec/utils.c        |  1 +
 4 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index ab9a6a2a18..7033816b5a 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -1106,7 +1106,7 @@ following image formats are supported:
 @item ADPCM Electronic Arts XAS @tab     @tab  X
 @item ADPCM G.722            @tab  X  @tab  X
 @item ADPCM G.726            @tab  X  @tab  X
- at item ADPCM IMA AMV          @tab     @tab  X
+ at item ADPCM IMA AMV          @tab  X  @tab  X
     @tab Used in AMV files
 @item ADPCM IMA Cunning Developments  @tab     @tab  X
 @item ADPCM IMA Electronic Arts EACS  @tab     @tab  X
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index 5670da1dea..20568cfc89 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -74,7 +74,12 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (s->block_size & (s->block_size - 1)) {
+    /*
+     * AMV's block size has to match that of the corresponding video
+     * stream. Relax the POT requirement.
+     */
+    if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
+        (s->block_size & (s->block_size - 1))) {
         av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
         return AVERROR(EINVAL);
     }
@@ -161,6 +166,20 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         avctx->frame_size  = s->block_size * 2 / avctx->channels;
         avctx->block_align = s->block_size;
         break;
+    case AV_CODEC_ID_ADPCM_IMA_AMV:
+        if (avctx->sample_rate != 22050) {
+            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
+            return AVERROR(EINVAL);
+        }
+
+        if (avctx->channels != 1) {
+            av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
+            return AVERROR(EINVAL);
+        }
+
+        avctx->frame_size  = s->block_size;
+        avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
+        break;
     case AV_CODEC_ID_ADPCM_IMA_APM:
         avctx->frame_size  = s->block_size * 2 / avctx->channels;
         avctx->block_align = s->block_size;
@@ -338,6 +357,7 @@ static void adpcm_compress_trellis(AVCodecContext *avctx,
     nodes[0]->sample2 = c->sample2;
     if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
         version == AV_CODEC_ID_ADPCM_IMA_QT  ||
+        version == AV_CODEC_ID_ADPCM_IMA_AMV ||
         version == AV_CODEC_ID_ADPCM_SWF)
         nodes[0]->sample1 = c->prev_sample;
     if (version == AV_CODEC_ID_ADPCM_MS)
@@ -442,6 +462,7 @@ static void adpcm_compress_trellis(AVCodecContext *avctx,
                 }
             } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
                        version == AV_CODEC_ID_ADPCM_IMA_QT  ||
+                       version == AV_CODEC_ID_ADPCM_IMA_AMV ||
                        version == AV_CODEC_ID_ADPCM_SWF) {
 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
                 const int predictor = nodes[j]->sample1;\
@@ -834,6 +855,40 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         flush_put_bits(&pb);
         break;
     }
+    case AV_CODEC_ID_ADPCM_IMA_AMV:
+    {
+        av_assert0(avctx->channels == 1);
+
+        c->status[0].prev_sample = *samples;
+        bytestream_put_le16(&dst, c->status[0].prev_sample);
+        bytestream_put_byte(&dst, c->status[0].step_index);
+        bytestream_put_byte(&dst, 0);
+        bytestream_put_le32(&dst, avctx->frame_size);
+
+        if (avctx->trellis > 0) {
+            n = frame->nb_samples >> 1;
+
+            if (!(buf = av_malloc(2 * n)))
+                return AVERROR(ENOMEM);
+
+            adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, avctx->channels);
+            for (i = 0; i < n; i++)
+                bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
+
+            samples += 2 * n;
+        } else for (n = frame->nb_samples >> 1; n > 0; n--) {
+            int nibble;
+            nibble  = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
+            nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
+            bytestream_put_byte(&dst, nibble);
+        }
+
+        if (avctx->frame_size & 1) {
+            int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
+            bytestream_put_byte(&dst, nibble);
+        }
+        break;
+    }
     case AV_CODEC_ID_ADPCM_ARGO:
     {
         PutBitContext pb;
@@ -927,6 +982,7 @@ AVCodec ff_ ## name_ ## _encoder = {                                       \
 }
 
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO,    adpcm_argo,    sample_fmts_p, 0,                             "ADPCM Argonaut Games");
+ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts,   0,                             "ADPCM IMA AMV");
 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_ALP, adpcm_ima_alp, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,                             "ADPCM IMA QuickTime");
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 16fd92b802..8bdc0d6bf7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -614,6 +614,7 @@ extern AVCodec ff_adpcm_g726_decoder;
 extern AVCodec ff_adpcm_g726le_encoder;
 extern AVCodec ff_adpcm_g726le_decoder;
 extern AVCodec ff_adpcm_ima_amv_decoder;
+extern AVCodec ff_adpcm_ima_amv_encoder;
 extern AVCodec ff_adpcm_ima_alp_decoder;
 extern AVCodec ff_adpcm_ima_alp_encoder;
 extern AVCodec ff_adpcm_ima_apc_decoder;
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 008e95c239..c10539593e 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1491,6 +1491,7 @@ int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
     case AV_CODEC_ID_ADPCM_ARGO:
     case AV_CODEC_ID_ADPCM_CT:
     case AV_CODEC_ID_ADPCM_IMA_ALP:
+    case AV_CODEC_ID_ADPCM_IMA_AMV:
     case AV_CODEC_ID_ADPCM_IMA_APC:
     case AV_CODEC_ID_ADPCM_IMA_APM:
     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:



More information about the ffmpeg-cvslog mailing list