diff mbox series

[FFmpeg-devel,v3,1/2] avcodec: add decoder for Rayman 2's ADPCM variant

Message ID 20200220113124.8083-2-zane@zanevaniperen.com
State Accepted
Headers show
Series Ubisoft Rayman 2 APM demuxer + decoder.
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Zane van Iperen Feb. 20, 2020, 11:31 a.m. UTC
Adds support for the ADPCM variant used in Rayman 2's files.

Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
---
 libavcodec/Makefile     |  1 +
 libavcodec/adpcm.c      | 21 +++++++++++++++++++++
 libavcodec/allcodecs.c  |  1 +
 libavcodec/avcodec.h    |  1 +
 libavcodec/codec_desc.c |  7 +++++++
 libavcodec/version.h    |  2 +-
 6 files changed, 32 insertions(+), 1 deletion(-)

Comments

Paul B Mahol Feb. 20, 2020, 1:49 p.m. UTC | #1
On 2/20/20, Zane van Iperen <zane@zanevaniperen.com> wrote:
> Adds support for the ADPCM variant used in Rayman 2's files.
>
> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
> ---
>  libavcodec/Makefile     |  1 +
>  libavcodec/adpcm.c      | 21 +++++++++++++++++++++
>  libavcodec/allcodecs.c  |  1 +
>  libavcodec/avcodec.h    |  1 +
>  libavcodec/codec_desc.c |  7 +++++++
>  libavcodec/version.h    |  2 +-
>  6 files changed, 32 insertions(+), 1 deletion(-)
>
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 71eeb60901..1ade20613f 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -834,6 +834,7 @@ OBJS-$(CONFIG_ADPCM_G726LE_DECODER)       += g726.o
>  OBJS-$(CONFIG_ADPCM_G726LE_ENCODER)       += g726.o
>  OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o adpcm_data.o
>  OBJS-$(CONFIG_ADPCM_IMA_APC_DECODER)      += adpcm.o adpcm_data.o
> +OBJS-$(CONFIG_ADPCM_IMA_APM_DECODER)      += adpcm.o adpcm_data.o
>  OBJS-$(CONFIG_ADPCM_IMA_DAT4_DECODER)     += adpcm.o adpcm_data.o
>  OBJS-$(CONFIG_ADPCM_IMA_DK3_DECODER)      += adpcm.o adpcm_data.o
>  OBJS-$(CONFIG_ADPCM_IMA_DK4_DECODER)      += adpcm.o adpcm_data.o
> diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
> index f5d20ddd81..9bc5175d24 100644
> --- a/libavcodec/adpcm.c
> +++ b/libavcodec/adpcm.c
> @@ -14,6 +14,7 @@
>   * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
>   * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
>   * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen
> (zane@zanevaniperen.com)
> + * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
>   *
>   * This file is part of FFmpeg.
>   *
> @@ -150,6 +151,14 @@ static av_cold int adpcm_decode_init(AVCodecContext *
> avctx)
>              c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata
> + 4), 18);
>          }
>          break;
> +    case AV_CODEC_ID_ADPCM_IMA_APM:
> +        if (avctx->extradata && avctx->extradata_size >= 16) {
> +            c->status[0].predictor  = AV_RL32(avctx->extradata +  0);
> +            c->status[0].step_index = AV_RL32(avctx->extradata +  4);
> +            c->status[1].predictor  = AV_RL32(avctx->extradata +  8);
> +            c->status[1].step_index = AV_RL32(avctx->extradata + 12);
> +        }
> +        break;
>      case AV_CODEC_ID_ADPCM_IMA_WS:
>          if (avctx->extradata && avctx->extradata_size >= 2)
>              c->vqa_version = AV_RL16(avctx->extradata);
> @@ -665,6 +674,7 @@ static int get_nb_samples(AVCodecContext *avctx,
> GetByteContext *gb,
>      case AV_CODEC_ID_ADPCM_YAMAHA:
>      case AV_CODEC_ID_ADPCM_AICA:
>      case AV_CODEC_ID_ADPCM_IMA_SSI:
> +    case AV_CODEC_ID_ADPCM_IMA_APM:
>          nb_samples = buf_size * 2 / ch;
>          break;
>      }
> @@ -1227,6 +1237,16 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
> void *data,
>              *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v &
> 0x0F, 3);
>          }
>          break;
> +    case AV_CODEC_ID_ADPCM_IMA_APM:
> +        for (n = nb_samples / 2; n > 0; n--) {
> +            for (channel = 0; channel < avctx->channels; channel++) {
> +                int v = bytestream2_get_byteu(&gb);
> +                *samples++  =
> adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  , 3);
> +                samples[st] =
> adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F, 3);

This looks wrong. You increase samples twice, above and down bellow.

Also, codec looks same as SSI just with optional extradata?

> +            }
> +            samples += avctx->channels;
> +        }
> +        break;
>      case AV_CODEC_ID_ADPCM_IMA_OKI:
>          while (bytestream2_get_bytes_left(&gb) > 0) {
>              int v = bytestream2_get_byteu(&gb);
> @@ -1965,6 +1985,7 @@ ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,
> sample_fmts_s16p, adpcm_ea_r3,
>  ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p,
> adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
>  ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,
> adpcm_ima_amv,     "ADPCM IMA AMV");
>  ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,
> adpcm_ima_apc,     "ADPCM IMA CRYO APC");
> +ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM,     sample_fmts_s16p,
> adpcm_ima_apm,     "ADPCM IMA Ubisoft APM");
>  ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,
> adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
>  ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,
> adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
>  ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,
> adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 103f34fd32..6f543ea96f 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -599,6 +599,7 @@ extern AVCodec ff_adpcm_g726le_encoder;
>  extern AVCodec ff_adpcm_g726le_decoder;
>  extern AVCodec ff_adpcm_ima_amv_decoder;
>  extern AVCodec ff_adpcm_ima_apc_decoder;
> +extern AVCodec ff_adpcm_ima_apm_decoder;
>  extern AVCodec ff_adpcm_ima_dat4_decoder;
>  extern AVCodec ff_adpcm_ima_dk3_decoder;
>  extern AVCodec ff_adpcm_ima_dk4_decoder;
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 368341ba93..f9d1af6ef9 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -549,6 +549,7 @@ enum AVCodecID {
>      AV_CODEC_ID_ADPCM_ARGO,
>      AV_CODEC_ID_ADPCM_IMA_SSI,
>      AV_CODEC_ID_ADPCM_ZORK,
> +    AV_CODEC_ID_ADPCM_IMA_APM,
>
>      /* AMR */
>      AV_CODEC_ID_AMR_NB = 0x12000,
> diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
> index 609c7501fd..c5c379885f 100644
> --- a/libavcodec/codec_desc.c
> +++ b/libavcodec/codec_desc.c
> @@ -2318,6 +2318,13 @@ static const AVCodecDescriptor codec_descriptors[] =
> {
>          .long_name = NULL_IF_CONFIG_SMALL("ADPCM Zork"),
>          .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
>      },
> +    {
> +        .id        = AV_CODEC_ID_ADPCM_IMA_APM,
> +        .type      = AVMEDIA_TYPE_AUDIO,
> +        .name      = "adpcm_ima_apm",
> +        .long_name = NULL_IF_CONFIG_SMALL("ADPCM IMA Ubisoft APM"),
> +        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
> +    },
>
>      /* AMR */
>      {
> diff --git a/libavcodec/version.h b/libavcodec/version.h
> index 5c8147f681..b534b9260d 100644
> --- a/libavcodec/version.h
> +++ b/libavcodec/version.h
> @@ -28,7 +28,7 @@
>  #include "libavutil/version.h"
>
>  #define LIBAVCODEC_VERSION_MAJOR  58
> -#define LIBAVCODEC_VERSION_MINOR  70
> +#define LIBAVCODEC_VERSION_MINOR  71
>  #define LIBAVCODEC_VERSION_MICRO 100
>
>  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
> --
> 2.17.1
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Zane van Iperen Feb. 20, 2020, 2:49 p.m. UTC | #2
20/2/20 11:49 pm, Paul B Mahol пишет:
> 
> On 2/20/20, Zane van Iperen <zane@zanevaniperen.com> wrote:
>> Adds support for the ADPCM variant used in Rayman 2's files.
>>

>> +    case AV_CODEC_ID_ADPCM_IMA_APM:
>> +        for (n = nb_samples / 2; n > 0; n--) {
>> +            for (channel = 0; channel < avctx->channels; channel++) {
>> +                int v = bytestream2_get_byteu(&gb);
>> +                *samples++  =
>> adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  , 3);
>> +                samples[st] =
>> adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F, 3);
> 
> This looks wrong. You increase samples twice, above and down bellow.
>
> Also, codec looks same as SSI just with optional extradata?

It's right. The channels are interleaved per-byte, not per-nibble as in SSI.
If you look underneath AV_CODEC_ID_ADPCM_IMA_WS, you'll see a similar construction.

In the v2 of this patch, I was doing this:

for (channel = 0; bytestream2_get_bytes_left(&gb) > 0;) {
	int v = bytestream2_get_byteu(&gb);
	*samples_p[channel]++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4, 3);
	*samples_p[channel]++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F, 3);
	channel = (channel + 1) % avctx->channels;
}

/* Changing samples_p changes the frame itself. Undo any damage. */
for (channel = 0; channel < avctx->channels; channel++)
	samples_p[channel] -= nb_samples;

It's the same thing (except using S16P instead of S16), but I thought the IMA_WS version
seemed neater.

Regardless, my test files give the same hashes with both versions:

$ ./ffmpeg -loglevel error -i main\ title.apm -map 0:a -f md5 -
MD5=c6db6dac4372e6413624d20dcdbf33ac

$ ./ffmpeg -loglevel error -i outro1.apm -map 0:a -f md5 -
MD5=a5cb56a035ab4b79adceba6fe4a428d2

If you think the v2 form is better, simply disregard v3. This is the only difference
between them.


>> +            }
>> +            samples += avctx->channels;
>> +        }
>> +        break;
>>       case AV_CODEC_ID_ADPCM_IMA_OKI:


Zane
diff mbox series

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 71eeb60901..1ade20613f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -834,6 +834,7 @@  OBJS-$(CONFIG_ADPCM_G726LE_DECODER)       += g726.o
 OBJS-$(CONFIG_ADPCM_G726LE_ENCODER)       += g726.o
 OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_IMA_APC_DECODER)      += adpcm.o adpcm_data.o
+OBJS-$(CONFIG_ADPCM_IMA_APM_DECODER)      += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_IMA_DAT4_DECODER)     += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_IMA_DK3_DECODER)      += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_IMA_DK4_DECODER)      += adpcm.o adpcm_data.o
diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index f5d20ddd81..9bc5175d24 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -14,6 +14,7 @@ 
  * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
  * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
+ * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
  *
  * This file is part of FFmpeg.
  *
@@ -150,6 +151,14 @@  static av_cold int adpcm_decode_init(AVCodecContext * avctx)
             c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
         }
         break;
+    case AV_CODEC_ID_ADPCM_IMA_APM:
+        if (avctx->extradata && avctx->extradata_size >= 16) {
+            c->status[0].predictor  = AV_RL32(avctx->extradata +  0);
+            c->status[0].step_index = AV_RL32(avctx->extradata +  4);
+            c->status[1].predictor  = AV_RL32(avctx->extradata +  8);
+            c->status[1].step_index = AV_RL32(avctx->extradata + 12);
+        }
+        break;
     case AV_CODEC_ID_ADPCM_IMA_WS:
         if (avctx->extradata && avctx->extradata_size >= 2)
             c->vqa_version = AV_RL16(avctx->extradata);
@@ -665,6 +674,7 @@  static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
     case AV_CODEC_ID_ADPCM_YAMAHA:
     case AV_CODEC_ID_ADPCM_AICA:
     case AV_CODEC_ID_ADPCM_IMA_SSI:
+    case AV_CODEC_ID_ADPCM_IMA_APM:
         nb_samples = buf_size * 2 / ch;
         break;
     }
@@ -1227,6 +1237,16 @@  static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F, 3);
         }
         break;
+    case AV_CODEC_ID_ADPCM_IMA_APM:
+        for (n = nb_samples / 2; n > 0; n--) {
+            for (channel = 0; channel < avctx->channels; channel++) {
+                int v = bytestream2_get_byteu(&gb);
+                *samples++  = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  , 3);
+                samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F, 3);
+            }
+            samples += avctx->channels;
+        }
+        break;
     case AV_CODEC_ID_ADPCM_IMA_OKI:
         while (bytestream2_get_bytes_left(&gb) > 0) {
             int v = bytestream2_get_byteu(&gb);
@@ -1965,6 +1985,7 @@  ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
+ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM,     sample_fmts_s16p, adpcm_ima_apm,     "ADPCM IMA Ubisoft APM");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,  adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 103f34fd32..6f543ea96f 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -599,6 +599,7 @@  extern AVCodec ff_adpcm_g726le_encoder;
 extern AVCodec ff_adpcm_g726le_decoder;
 extern AVCodec ff_adpcm_ima_amv_decoder;
 extern AVCodec ff_adpcm_ima_apc_decoder;
+extern AVCodec ff_adpcm_ima_apm_decoder;
 extern AVCodec ff_adpcm_ima_dat4_decoder;
 extern AVCodec ff_adpcm_ima_dk3_decoder;
 extern AVCodec ff_adpcm_ima_dk4_decoder;
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 368341ba93..f9d1af6ef9 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -549,6 +549,7 @@  enum AVCodecID {
     AV_CODEC_ID_ADPCM_ARGO,
     AV_CODEC_ID_ADPCM_IMA_SSI,
     AV_CODEC_ID_ADPCM_ZORK,
+    AV_CODEC_ID_ADPCM_IMA_APM,
 
     /* AMR */
     AV_CODEC_ID_AMR_NB = 0x12000,
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 609c7501fd..c5c379885f 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2318,6 +2318,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("ADPCM Zork"),
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_ADPCM_IMA_APM,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "adpcm_ima_apm",
+        .long_name = NULL_IF_CONFIG_SMALL("ADPCM IMA Ubisoft APM"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+    },
 
     /* AMR */
     {
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 5c8147f681..b534b9260d 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -28,7 +28,7 @@ 
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  58
-#define LIBAVCODEC_VERSION_MINOR  70
+#define LIBAVCODEC_VERSION_MINOR  71
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \