diff mbox series

[FFmpeg-devel,4/7] avformat: add apm muxer

Message ID 20200609001730.961175-5-zane@zanevaniperen.com
State Superseded
Headers show
Series adpcm_ima_apm encoder + apm muxer | expand

Checks

Context Check Description
andriy/default pending
andriy/make_warn warning New warnings during build
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Zane van Iperen June 9, 2020, 12:19 a.m. UTC
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
---
 Changelog                |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/apm.c        | 112 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 113 insertions(+), 2 deletions(-)

Comments

Andreas Rheinhardt June 10, 2020, 7:04 p.m. UTC | #1
Zane van Iperen:
> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
> ---
>  Changelog                |   1 +
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/apm.c        | 112 ++++++++++++++++++++++++++++++++++++++-
>  4 files changed, 113 insertions(+), 2 deletions(-)
> 
> diff --git a/Changelog b/Changelog
> index 3c82f2ebd6..1f5250bb35 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -78,6 +78,7 @@ version 4.3:
>  - PFM decoder
>  - dblur video filter
>  - Real War KVAG muxer
> +- Rayman 2 APM muxer
>  
>  
>  version 4.2:
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index bb09dc6563..a2691cb626 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -94,6 +94,7 @@ OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
>  OBJS-$(CONFIG_APC_DEMUXER)               += apc.o
>  OBJS-$(CONFIG_APE_DEMUXER)               += ape.o apetag.o img2.o
>  OBJS-$(CONFIG_APM_DEMUXER)               += apm.o
> +OBJS-$(CONFIG_APM_MUXER)                 += apm.o rawenc.o
>  OBJS-$(CONFIG_APNG_DEMUXER)              += apngdec.o
>  OBJS-$(CONFIG_APNG_MUXER)                += apngenc.o
>  OBJS-$(CONFIG_APTX_DEMUXER)              += aptxdec.o rawdec.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index a7c5c9db89..0c8788ef42 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -55,6 +55,7 @@ extern AVInputFormat  ff_anm_demuxer;
>  extern AVInputFormat  ff_apc_demuxer;
>  extern AVInputFormat  ff_ape_demuxer;
>  extern AVInputFormat  ff_apm_demuxer;
> +extern AVOutputFormat ff_apm_muxer;
>  extern AVInputFormat  ff_apng_demuxer;
>  extern AVOutputFormat ff_apng_muxer;
>  extern AVInputFormat  ff_aptx_demuxer;
> diff --git a/libavformat/apm.c b/libavformat/apm.c
> index 16ac6a2b1a..46a0c67b79 100644
> --- a/libavformat/apm.c
> +++ b/libavformat/apm.c
> @@ -1,5 +1,5 @@
>  /*
> - * Rayman 2 APM Demuxer
> + * Rayman 2 APM (De)muxer
>   *
>   * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
>   *
> @@ -21,6 +21,8 @@
>   */
>  #include "avformat.h"
>  #include "internal.h"
> +#include "rawenc.h"
> +#include "libavutil/avassert.h"
>  #include "libavutil/internal.h"
>  #include "libavutil/intreadwrite.h"
>  
> @@ -48,13 +50,14 @@ typedef struct APMExtraData {
>      uint32_t    magic;
>      uint32_t    file_size;
>      uint32_t    data_size;
> -    uint32_t    unk1;
> +    int32_t     unk1;
>      uint32_t    unk2;
>      APMState    state;
>      uint32_t    unk3[7];
>      uint32_t    data;
>  } APMExtraData;
>  
> +#if CONFIG_APM_DEMUXER
>  static void apm_parse_extradata(APMExtraData *ed, const uint8_t *buf)
>  {
>      ed->magic                 = AV_RL32(buf + 0);
> @@ -195,3 +198,108 @@ AVInputFormat ff_apm_demuxer = {
>      .read_header    = apm_read_header,
>      .read_packet    = apm_read_packet
>  };
> +#endif
> +
> +#if CONFIG_APM_MUXER
> +static int apm_write_init(AVFormatContext *s)
> +{
> +    AVCodecParameters *par;
> +
> +    if (s->nb_streams != 1) {
> +        av_log(s, AV_LOG_ERROR, "APM files have exactly one stream\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    par = s->streams[0]->codecpar;
> +
> +    if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_APM) {
> +        av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
> +               avcodec_get_name(par->codec_id));
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (par->channels > 2) {
> +        av_log(s, AV_LOG_ERROR, "APM files only support up to 2 channels\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (par->extradata_size != APM_EXTRADATA_SIZE) {
> +        av_log(s, AV_LOG_ERROR, "Invalid/missing extradata\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
> +        av_log(s, AV_LOG_WARNING, "Stream not seekable, unable to write output file\n");

You are erroring out here, so this should be AV_LOG_ERROR.

> +        return AVERROR(EINVAL);
> +    }
> +
> +    return 0;
> +}
> +
> +static int apm_write_header(AVFormatContext *s)
> +{
> +    APMExtraData extradata;

Unused.

> +    uint8_t buf[APM_FILE_EXTRADATA_SIZE];

You can initialize this buf by = { 0 }; here and don't need to rely on
memset.

> +    AVCodecParameters *par = s->streams[0]->codecpar;
> +
> +    memset(buf, 0, APM_FILE_EXTRADATA_SIZE);
> +
> +    /*
> +     * Bodge a WAVEFORMATEX manually, ff_put_wav_header() can't
> +     * be used because of the extra 2 bytes.
> +     */
> +    avio_wl16(s->pb, APM_CODEC_TAG);
> +    avio_wl16(s->pb, par->channels);
> +    avio_wl32(s->pb, par->sample_rate);
> +    avio_wl32(s->pb, par->sample_rate * par->channels * 2);
> +    avio_wl16(s->pb, par->block_align);
> +    avio_wl16(s->pb, par->bits_per_coded_sample);
> +    avio_wl16(s->pb, APM_FILE_EXTRADATA_SIZE);
> +
> +    avio_wl16(s->pb, 0); /* pad */
> +
> +    /*
> +     * Build the extradata. Assume the codec's given us correct data.
> +     * File and data sizes are fixed later.
> +     */
> +    AV_WL32(buf +  0, APM_TAG_VS12); /* magic */
> +    AV_WL32(buf + 12, -1);           /* unk1, always seems to be -1 */
> +    memcpy( buf + 20, par->extradata, APM_EXTRADATA_SIZE);
> +    AV_WL32(buf + 76, APM_TAG_DATA); /* data */
> +
> +    avio_write(s->pb, buf, APM_FILE_EXTRADATA_SIZE);
> +    return 0;
> +}
> +
> +static int apm_write_trailer(AVFormatContext *s)
> +{
> +    int64_t file_size, data_size;
> +
> +    file_size = avio_tell(s->pb);
> +    data_size = file_size - (APM_FILE_HEADER_SIZE + 2 + APM_FILE_EXTRADATA_SIZE);
> +
> +    if (file_size < UINT32_MAX && data_size < UINT32_MAX) {

If file_size < UINT32_MAX, then the check for data_size could only be
wrong if the calculation of data_size overflowed which would be
undefined behaviour.

> +        avio_seek(s->pb, 24, SEEK_SET);
> +        avio_wl32(s->pb, (uint32_t)file_size);
> +        avio_wl32(s->pb, (uint32_t)data_size);
> +    } else {
> +        av_log(s, AV_LOG_WARNING,
> +               "Filesize %"PRId64" invalid for APM, output file will be broken\n",
> +               file_size);

In this case you should not only emit a warning, but return an error
(and the message should be AV_LOG_ERROR).

> +    }
> +
> +    return 0;
> +}
> +
> +AVOutputFormat ff_apm_muxer = {
> +    .name           = "apm",
> +    .long_name      = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
> +    .extensions     = "apm",
> +    .audio_codec    = AV_CODEC_ID_ADPCM_IMA_APM,
> +    .video_codec    = AV_CODEC_ID_NONE,
> +    .init           = apm_write_init,
> +    .write_header   = apm_write_header,
> +    .write_packet   = ff_raw_write_packet,
> +    .write_trailer  = apm_write_trailer
> +};
> +#endif
>
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index 3c82f2ebd6..1f5250bb35 100644
--- a/Changelog
+++ b/Changelog
@@ -78,6 +78,7 @@  version 4.3:
 - PFM decoder
 - dblur video filter
 - Real War KVAG muxer
+- Rayman 2 APM muxer
 
 
 version 4.2:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index bb09dc6563..a2691cb626 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -94,6 +94,7 @@  OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
 OBJS-$(CONFIG_APC_DEMUXER)               += apc.o
 OBJS-$(CONFIG_APE_DEMUXER)               += ape.o apetag.o img2.o
 OBJS-$(CONFIG_APM_DEMUXER)               += apm.o
+OBJS-$(CONFIG_APM_MUXER)                 += apm.o rawenc.o
 OBJS-$(CONFIG_APNG_DEMUXER)              += apngdec.o
 OBJS-$(CONFIG_APNG_MUXER)                += apngenc.o
 OBJS-$(CONFIG_APTX_DEMUXER)              += aptxdec.o rawdec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index a7c5c9db89..0c8788ef42 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -55,6 +55,7 @@  extern AVInputFormat  ff_anm_demuxer;
 extern AVInputFormat  ff_apc_demuxer;
 extern AVInputFormat  ff_ape_demuxer;
 extern AVInputFormat  ff_apm_demuxer;
+extern AVOutputFormat ff_apm_muxer;
 extern AVInputFormat  ff_apng_demuxer;
 extern AVOutputFormat ff_apng_muxer;
 extern AVInputFormat  ff_aptx_demuxer;
diff --git a/libavformat/apm.c b/libavformat/apm.c
index 16ac6a2b1a..46a0c67b79 100644
--- a/libavformat/apm.c
+++ b/libavformat/apm.c
@@ -1,5 +1,5 @@ 
 /*
- * Rayman 2 APM Demuxer
+ * Rayman 2 APM (De)muxer
  *
  * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
  *
@@ -21,6 +21,8 @@ 
  */
 #include "avformat.h"
 #include "internal.h"
+#include "rawenc.h"
+#include "libavutil/avassert.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 
@@ -48,13 +50,14 @@  typedef struct APMExtraData {
     uint32_t    magic;
     uint32_t    file_size;
     uint32_t    data_size;
-    uint32_t    unk1;
+    int32_t     unk1;
     uint32_t    unk2;
     APMState    state;
     uint32_t    unk3[7];
     uint32_t    data;
 } APMExtraData;
 
+#if CONFIG_APM_DEMUXER
 static void apm_parse_extradata(APMExtraData *ed, const uint8_t *buf)
 {
     ed->magic                 = AV_RL32(buf + 0);
@@ -195,3 +198,108 @@  AVInputFormat ff_apm_demuxer = {
     .read_header    = apm_read_header,
     .read_packet    = apm_read_packet
 };
+#endif
+
+#if CONFIG_APM_MUXER
+static int apm_write_init(AVFormatContext *s)
+{
+    AVCodecParameters *par;
+
+    if (s->nb_streams != 1) {
+        av_log(s, AV_LOG_ERROR, "APM files have exactly one stream\n");
+        return AVERROR(EINVAL);
+    }
+
+    par = s->streams[0]->codecpar;
+
+    if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_APM) {
+        av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
+               avcodec_get_name(par->codec_id));
+        return AVERROR(EINVAL);
+    }
+
+    if (par->channels > 2) {
+        av_log(s, AV_LOG_ERROR, "APM files only support up to 2 channels\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (par->extradata_size != APM_EXTRADATA_SIZE) {
+        av_log(s, AV_LOG_ERROR, "Invalid/missing extradata\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
+        av_log(s, AV_LOG_WARNING, "Stream not seekable, unable to write output file\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static int apm_write_header(AVFormatContext *s)
+{
+    APMExtraData extradata;
+    uint8_t buf[APM_FILE_EXTRADATA_SIZE];
+    AVCodecParameters *par = s->streams[0]->codecpar;
+
+    memset(buf, 0, APM_FILE_EXTRADATA_SIZE);
+
+    /*
+     * Bodge a WAVEFORMATEX manually, ff_put_wav_header() can't
+     * be used because of the extra 2 bytes.
+     */
+    avio_wl16(s->pb, APM_CODEC_TAG);
+    avio_wl16(s->pb, par->channels);
+    avio_wl32(s->pb, par->sample_rate);
+    avio_wl32(s->pb, par->sample_rate * par->channels * 2);
+    avio_wl16(s->pb, par->block_align);
+    avio_wl16(s->pb, par->bits_per_coded_sample);
+    avio_wl16(s->pb, APM_FILE_EXTRADATA_SIZE);
+
+    avio_wl16(s->pb, 0); /* pad */
+
+    /*
+     * Build the extradata. Assume the codec's given us correct data.
+     * File and data sizes are fixed later.
+     */
+    AV_WL32(buf +  0, APM_TAG_VS12); /* magic */
+    AV_WL32(buf + 12, -1);           /* unk1, always seems to be -1 */
+    memcpy( buf + 20, par->extradata, APM_EXTRADATA_SIZE);
+    AV_WL32(buf + 76, APM_TAG_DATA); /* data */
+
+    avio_write(s->pb, buf, APM_FILE_EXTRADATA_SIZE);
+    return 0;
+}
+
+static int apm_write_trailer(AVFormatContext *s)
+{
+    int64_t file_size, data_size;
+
+    file_size = avio_tell(s->pb);
+    data_size = file_size - (APM_FILE_HEADER_SIZE + 2 + APM_FILE_EXTRADATA_SIZE);
+
+    if (file_size < UINT32_MAX && data_size < UINT32_MAX) {
+        avio_seek(s->pb, 24, SEEK_SET);
+        avio_wl32(s->pb, (uint32_t)file_size);
+        avio_wl32(s->pb, (uint32_t)data_size);
+    } else {
+        av_log(s, AV_LOG_WARNING,
+               "Filesize %"PRId64" invalid for APM, output file will be broken\n",
+               file_size);
+    }
+
+    return 0;
+}
+
+AVOutputFormat ff_apm_muxer = {
+    .name           = "apm",
+    .long_name      = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
+    .extensions     = "apm",
+    .audio_codec    = AV_CODEC_ID_ADPCM_IMA_APM,
+    .video_codec    = AV_CODEC_ID_NONE,
+    .init           = apm_write_init,
+    .write_header   = apm_write_header,
+    .write_packet   = ff_raw_write_packet,
+    .write_trailer  = apm_write_trailer
+};
+#endif