summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZane van Iperen <zane@zanevaniperen.com>2020-06-11 09:07:46 +1000
committerZane van Iperen <zane@zanevaniperen.com>2020-07-21 11:36:14 +1000
commit4eb6ec3a8564aa404f8ce792899a46c461b7766b (patch)
tree8e04f17f3ee45e1c670c4268efe2fe1fb061b028
parent80dda809811ea68cc7d7e4064b51fc433c54384e (diff)
avformat: add apm muxer
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
-rw-r--r--Changelog1
-rw-r--r--libavformat/Makefile1
-rw-r--r--libavformat/allformats.c1
-rw-r--r--libavformat/apm.c112
-rw-r--r--libavformat/version.h2
5 files changed, 115 insertions, 2 deletions
diff --git a/Changelog b/Changelog
index 2db3e50060..c37ffa82e1 100644
--- a/Changelog
+++ b/Changelog
@@ -8,6 +8,7 @@ version <next>:
- chromanr video filter
- VDPAU accelerated HEVC 10/12bit decoding
- ADPCM IMA Ubisoft APM encoder
+- 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 f8527b1fd4..fd9e46e233 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 fe95a9ad7a..4c5844c83e 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"
@@ -55,6 +57,7 @@ typedef struct APMExtraData {
uint32_t data;
} APMExtraData;
+#if CONFIG_APM_DEMUXER
static void apm_parse_extradata(APMExtraData *ext, const uint8_t *buf)
{
ext->magic = AV_RL32(buf + 0);
@@ -205,3 +208,110 @@ 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->sample_rate > (INT_MAX / 8)) {
+ av_log(s, AV_LOG_ERROR, "Sample rate too large\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);
+ /* This is the wrong calculation, but it's what the orginal files have. */
+ 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_wl32(s->pb, APM_FILE_EXTRADATA_SIZE);
+
+ /*
+ * 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, 0xFFFFFFFF); /* unk1 */
+ 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
diff --git a/libavformat/version.h b/libavformat/version.h
index 75c03fde0a..33cebed85e 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
// Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
// Also please add any ticket numbers that you believe might be affected here
#define LIBAVFORMAT_VERSION_MAJOR 58
-#define LIBAVFORMAT_VERSION_MINOR 48
+#define LIBAVFORMAT_VERSION_MINOR 49
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \