[FFmpeg-devel] [PATCH v4 4/6] avformat: add apm muxer

Zane van Iperen zane at zanevaniperen.com
Sat Jun 20 13:59:38 EEST 2020


Signed-off-by: Zane van Iperen <zane at zanevaniperen.com>
---
 Changelog                |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/apm.c        | 110 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 111 insertions(+), 2 deletions(-)

diff --git a/Changelog b/Changelog
index a60e7d2eb8..8d8a0fc2c7 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version <next>:
 - AudioToolbox output device
 - MacCaption demuxer
+- Rayman 2 APM muxer
 
 
 version 4.3:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index a4113fe644..62d8cbb54e 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 97fd06debb..25b077bcf3 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 57d23200b0..9c9548baab 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 at 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);
@@ -198,3 +201,106 @@ 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_ERROR, "Stream not seekable, unable to write output file\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static int apm_write_header(AVFormatContext *s)
+{
+    uint8_t buf[APM_FILE_EXTRADATA_SIZE] = { 0 };
+    AVCodecParameters *par = s->streams[0]->codecpar;
+
+    /*
+     * Bodge a WAVEFORMATEX manually, ff_put_wav_header() can't
+     * be used because of the extra 2 bytes.
+     */
+    avio_wl16(s->pb, APM_TAG_CODEC);
+    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) {
+        av_log(s, AV_LOG_ERROR,
+               "Filesize %"PRId64" invalid for APM, output file will be broken\n",
+               file_size);
+        return AVERROR(ERANGE);
+    }
+
+    avio_seek(s->pb, 24, SEEK_SET);
+    avio_wl32(s->pb, (uint32_t)file_size);
+    avio_wl32(s->pb, (uint32_t)data_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
-- 
2.25.1




More information about the ffmpeg-devel mailing list