diff mbox series

[FFmpeg-devel,v2,4/7] avcodec: add adpcm_ima_apm encoder

Message ID 20200610233155.1050210-5-zane@zanevaniperen.com
State Superseded
Headers show
Series adpcm_ima_apm encoder + apm muxer
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Zane van Iperen June 10, 2020, 11:32 p.m. UTC
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
---
 doc/general.texi       |  2 +-
 libavcodec/Makefile    |  1 +
 libavcodec/adpcmenc.c  | 34 ++++++++++++++++++++++++++++++++--
 libavcodec/allcodecs.c |  1 +
 libavcodec/utils.c     |  1 +
 5 files changed, 36 insertions(+), 3 deletions(-)

Comments

Andreas Rheinhardt June 11, 2020, 2:23 a.m. UTC | #1
Zane van Iperen:
> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
> ---
>  doc/general.texi       |  2 +-
>  libavcodec/Makefile    |  1 +
>  libavcodec/adpcmenc.c  | 34 ++++++++++++++++++++++++++++++++--
>  libavcodec/allcodecs.c |  1 +
>  libavcodec/utils.c     |  1 +
>  5 files changed, 36 insertions(+), 3 deletions(-)
> 
> diff --git a/doc/general.texi b/doc/general.texi
> index 9b0ee96752..8df7445c36 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -1109,7 +1109,7 @@ following image formats are supported:
>  @item ADPCM IMA High Voltage Software ALP   @tab     @tab  X
>  @item ADPCM IMA QuickTime    @tab  X  @tab  X
>  @item ADPCM IMA Simon & Schuster Interactive   @tab  X  @tab  X
> -@item ADPCM IMA Ubisoft APM  @tab     @tab X
> +@item ADPCM IMA Ubisoft APM  @tab  X  @tab  X
>  @item ADPCM IMA Loki SDL MJPEG  @tab     @tab  X
>  @item ADPCM IMA WAV          @tab  X  @tab  X
>  @item ADPCM IMA Westwood     @tab     @tab  X
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 5a6ea59715..4ee0bf2d4c 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -853,6 +853,7 @@ OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o adpcm_data.o
>  OBJS-$(CONFIG_ADPCM_IMA_ALP_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_APM_ENCODER)      += adpcmenc.o adpcm_data.o
>  OBJS-$(CONFIG_ADPCM_IMA_CUNNING_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
> diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
> index d5fbc0b9a7..8396180cb6 100644
> --- a/libavcodec/adpcmenc.c
> +++ b/libavcodec/adpcmenc.c
> @@ -77,7 +77,9 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
>          return AVERROR(EINVAL);
>      }
>  
> -    if (avctx->trellis && avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI) {
> +    if (avctx->trellis &&
> +       (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
> +        avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM)) {
>          /*
>           * The current trellis implementation doesn't work for extended
>           * runs of samples without periodic resets. Disallow it.
> @@ -152,6 +154,14 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
>          avctx->frame_size = BLKSIZE * 2 / avctx->channels;
>          avctx->block_align = BLKSIZE;
>          break;
> +    case AV_CODEC_ID_ADPCM_IMA_APM:
> +        avctx->frame_size = BLKSIZE * 2 / avctx->channels;
> +        avctx->block_align = BLKSIZE;
> +
> +        if (!(avctx->extradata = av_mallocz(28)))

Missing padding. And zero-initializing the extradata is really enough?

> +            goto error;
> +        avctx->extradata_size = 28;
> +        break;
>      default:
>          ret = AVERROR(EINVAL);
>          goto error;
> @@ -496,7 +506,8 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
>  
>      if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
>          pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
> -    else if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI)
> +    else if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
> +             avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
>          pkt_size = (frame->nb_samples * avctx->channels) / 2;
>      else
>          pkt_size = avctx->block_align;
> @@ -717,6 +728,24 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
>                  *dst++  = nibble;
>              }
>          break;
> +    case AV_CODEC_ID_ADPCM_IMA_APM:
> +    {
> +        PutBitContext pb;
> +        init_put_bits(&pb, dst, pkt_size);
> +
> +        av_assert0(avctx->trellis == 0);
> +
> +        for (n = frame->nb_samples / 2; n > 0; n--) {
> +            for (ch = 0; ch < avctx->channels; ch++) {
> +                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
> +                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
> +            }
> +            samples += avctx->channels;
> +        }
> +
> +        flush_put_bits(&pb);
> +        break;
> +    }
>      default:
>          return AVERROR(EINVAL);
>      }
> @@ -751,6 +780,7 @@ AVCodec ff_ ## name_ ## _encoder = {                                       \
>      .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
>  }
>  
> +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");
>  ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV");
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 80f128cade..f7d560052e 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -607,6 +607,7 @@ extern AVCodec ff_adpcm_ima_amv_decoder;
>  extern AVCodec ff_adpcm_ima_alp_decoder;
>  extern AVCodec ff_adpcm_ima_apc_decoder;
>  extern AVCodec ff_adpcm_ima_apm_decoder;
> +extern AVCodec ff_adpcm_ima_apm_encoder;
>  extern AVCodec ff_adpcm_ima_cunning_decoder;
>  extern AVCodec ff_adpcm_ima_dat4_decoder;
>  extern AVCodec ff_adpcm_ima_dk3_decoder;
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index a9c69e30dd..b47c98a9b2 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -1472,6 +1472,7 @@ int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
>      case AV_CODEC_ID_8SVX_FIB:
>      case AV_CODEC_ID_ADPCM_CT:
>      case AV_CODEC_ID_ADPCM_IMA_APC:
> +    case AV_CODEC_ID_ADPCM_IMA_APM:
>      case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
>      case AV_CODEC_ID_ADPCM_IMA_OKI:
>      case AV_CODEC_ID_ADPCM_IMA_WS:
>
diff mbox series

Patch

diff --git a/doc/general.texi b/doc/general.texi
index 9b0ee96752..8df7445c36 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -1109,7 +1109,7 @@  following image formats are supported:
 @item ADPCM IMA High Voltage Software ALP   @tab     @tab  X
 @item ADPCM IMA QuickTime    @tab  X  @tab  X
 @item ADPCM IMA Simon & Schuster Interactive   @tab  X  @tab  X
-@item ADPCM IMA Ubisoft APM  @tab     @tab X
+@item ADPCM IMA Ubisoft APM  @tab  X  @tab  X
 @item ADPCM IMA Loki SDL MJPEG  @tab     @tab  X
 @item ADPCM IMA WAV          @tab  X  @tab  X
 @item ADPCM IMA Westwood     @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..4ee0bf2d4c 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -853,6 +853,7 @@  OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_IMA_ALP_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_APM_ENCODER)      += adpcmenc.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_IMA_CUNNING_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
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index d5fbc0b9a7..8396180cb6 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -77,7 +77,9 @@  static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (avctx->trellis && avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI) {
+    if (avctx->trellis &&
+       (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
+        avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM)) {
         /*
          * The current trellis implementation doesn't work for extended
          * runs of samples without periodic resets. Disallow it.
@@ -152,6 +154,14 @@  static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         avctx->frame_size = BLKSIZE * 2 / avctx->channels;
         avctx->block_align = BLKSIZE;
         break;
+    case AV_CODEC_ID_ADPCM_IMA_APM:
+        avctx->frame_size = BLKSIZE * 2 / avctx->channels;
+        avctx->block_align = BLKSIZE;
+
+        if (!(avctx->extradata = av_mallocz(28)))
+            goto error;
+        avctx->extradata_size = 28;
+        break;
     default:
         ret = AVERROR(EINVAL);
         goto error;
@@ -496,7 +506,8 @@  static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
         pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
-    else if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI)
+    else if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
+             avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
         pkt_size = (frame->nb_samples * avctx->channels) / 2;
     else
         pkt_size = avctx->block_align;
@@ -717,6 +728,24 @@  static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                 *dst++  = nibble;
             }
         break;
+    case AV_CODEC_ID_ADPCM_IMA_APM:
+    {
+        PutBitContext pb;
+        init_put_bits(&pb, dst, pkt_size);
+
+        av_assert0(avctx->trellis == 0);
+
+        for (n = frame->nb_samples / 2; n > 0; n--) {
+            for (ch = 0; ch < avctx->channels; ch++) {
+                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
+                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
+            }
+            samples += avctx->channels;
+        }
+
+        flush_put_bits(&pb);
+        break;
+    }
     default:
         return AVERROR(EINVAL);
     }
@@ -751,6 +780,7 @@  AVCodec ff_ ## name_ ## _encoder = {                                       \
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
 }
 
+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");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV");
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 80f128cade..f7d560052e 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -607,6 +607,7 @@  extern AVCodec ff_adpcm_ima_amv_decoder;
 extern AVCodec ff_adpcm_ima_alp_decoder;
 extern AVCodec ff_adpcm_ima_apc_decoder;
 extern AVCodec ff_adpcm_ima_apm_decoder;
+extern AVCodec ff_adpcm_ima_apm_encoder;
 extern AVCodec ff_adpcm_ima_cunning_decoder;
 extern AVCodec ff_adpcm_ima_dat4_decoder;
 extern AVCodec ff_adpcm_ima_dk3_decoder;
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index a9c69e30dd..b47c98a9b2 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1472,6 +1472,7 @@  int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
     case AV_CODEC_ID_8SVX_FIB:
     case AV_CODEC_ID_ADPCM_CT:
     case AV_CODEC_ID_ADPCM_IMA_APC:
+    case AV_CODEC_ID_ADPCM_IMA_APM:
     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
     case AV_CODEC_ID_ADPCM_IMA_OKI:
     case AV_CODEC_ID_ADPCM_IMA_WS: