This adds muxer support based from avformat/mohawkdec.

Signed-off-by: Alstruit <[email protected]>
---
 Changelog                 |   2 +
 doc/general_contents.texi |   2 +-
 libavformat/Makefile      |   1 +
 libavformat/allformats.c  |   1 +
 libavformat/mohawkenc.c   | 153 ++++++++++++++++++++++++++++++++++++++
 libavformat/version.h     |   2 +-
 6 files changed, 159 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/mohawkenc.c

diff --git a/Changelog b/Changelog
index 885b8348b2..c4822a5b65 100644
--- a/Changelog
+++ b/Changelog
@@ -5,6 +5,8 @@ version <next>:
 - Extend AMF Color Converter (vf_vpp_amf) HDR capabilities
 - LCEVC track muxing support in MP4 muxer
 - Broderbund Mohawk demuxer
+- Broderbund Mohawk muxer
+
 
 version 8.1:
 - ffprobe -codec option
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index f2fa2de70a..93854bf059 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -472,7 +472,7 @@ library:
     @tab Used in the game Flash Traffic: City of Angels.
 @item BFSTM                     @tab   @tab X
     @tab Audio format used on the Nintendo WiiU (based on BRSTM).
-@item Broderbund .mhk Mohawk    @tab   @tab X
+@item Broderbund .mhk Mohawk    @tab X @tab X
     @tab Audio format used by Broderbund.
 @item BRSTM                     @tab   @tab X
     @tab Audio format used on the Nintendo Wii.
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 04dc0f68de..f112554f45 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -385,6 +385,7 @@ OBJS-$(CONFIG_MMF_MUXER)                 += mmf.o rawenc.o
 OBJS-$(CONFIG_MODS_DEMUXER)              += mods.o
 OBJS-$(CONFIG_MOFLEX_DEMUXER)            += moflex.o
 OBJS-$(CONFIG_MOHAWK_DEMUXER)            += mohawkdec.o
+OBJS-$(CONFIG_MOHAWK_MUXER)              += mohawkenc.o
 OBJS-$(CONFIG_MOV_DEMUXER)               += mov.o mov_chan.o mov_esds.o \
                                             qtpalette.o replaygain.o 
dovi_isom.o \
                                             dvdclut.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index e1549083d7..0dbbc345f9 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -288,6 +288,7 @@ extern const FFOutputFormat ff_mmf_muxer;
 extern const FFInputFormat  ff_mods_demuxer;
 extern const FFInputFormat  ff_moflex_demuxer;
 extern const FFInputFormat  ff_mohawk_demuxer;
+extern const FFOutputFormat ff_mohawk_muxer;
 extern const FFInputFormat  ff_mov_demuxer;
 extern const FFOutputFormat ff_mov_muxer;
 extern const FFOutputFormat ff_mp2_muxer;
diff --git a/libavformat/mohawkenc.c b/libavformat/mohawkenc.c
new file mode 100644
index 0000000000..23e4520e4e
--- /dev/null
+++ b/libavformat/mohawkenc.c
@@ -0,0 +1,153 @@
+/*
+ * Broderbund Mohawk (MHWK) muxer
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#include "libavutil/intreadwrite.h"
+#include "libavutil/dict.h"
+#include "avformat.h"
+#include "internal.h"
+#include "mux.h"
+
+#define MKTAG_MHWK MKBETAG('M', 'H', 'W', 'K')
+#define MKTAG_WAVE MKBETAG('W', 'A', 'V', 'E')
+#define MKTAG_DATA MKBETAG('D', 'a', 't', 'a')
+#define MOHAWK_DATA_HEADER_SIZE 20
+
+typedef struct MohawkMuxContext {
+    int64_t file_size_offset;
+    int64_t data_size_offset;
+    int64_t data_bytes_written;
+} MohawkMuxContext;
+
+static int mohawk_write_header(AVFormatContext *s)
+{
+    MohawkMuxContext *ctx = s->priv_data;
+    AVIOContext *pb = s->pb;
+    AVStream *st = s->streams[0];
+    uint16_t format_id = 0;
+    uint16_t loop_flag = 0x0000;
+    uint32_t loop_start = 0;
+    uint32_t loop_end = 0;
+    AVDictionaryEntry *loop_start_tag;
+    AVDictionaryEntry *loop_end_tag;
+
+    if (s->nb_streams != 1 || st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
+        av_log(s, AV_LOG_ERROR, "Mohawk muxer only supports a single audio 
stream.\n");
+        return AVERROR(EINVAL);
+    }
+
+    switch (st->codecpar->codec_id) {
+    case AV_CODEC_ID_PCM_U8:
+        format_id = 0x0000;
+        break;
+    case AV_CODEC_ID_ADPCM_IMA_APC:
+        format_id = 0x0001;
+        break;
+    case AV_CODEC_ID_MP2:
+        format_id = 0x0002;
+        break;
+    default:
+        av_log(s, AV_LOG_ERROR, "Unsupported audio codec for Mohawk 
container.\n");
+        return AVERROR(EINVAL);
+    }
+
+    /* Check for loop points in stream metadata */
+    loop_start_tag = av_dict_get(st->metadata, "loop_start", NULL, 0);
+    loop_end_tag   = av_dict_get(st->metadata, "loop_end", NULL, 0);
+
+    if (loop_start_tag && loop_end_tag) {
+        loop_flag = 0xFFFF;
+        loop_start = strtoul(loop_start_tag->value, NULL, 10);
+        loop_end   = strtoul(loop_end_tag->value, NULL, 10);
+        av_log(s, AV_LOG_VERBOSE, "Loop points found: %u to %u\n", loop_start, 
loop_end);
+    }
+
+    avio_wb32(pb, MKTAG_MHWK);
+
+    /* Record file size offset and write placeholder */
+    ctx->file_size_offset = avio_tell(pb);
+    avio_wb32(pb, 0);
+
+    avio_wb32(pb, MKTAG_WAVE);
+    avio_wb32(pb, MKTAG_DATA);
+
+    /* Record Data chunk size offset and write placeholder */
+    ctx->data_size_offset = avio_tell(pb);
+    avio_wb32(pb, 0);
+
+    /* Write 20-byte Data header */
+    avio_wb16(pb, st->codecpar->sample_rate);
+    avio_wb32(pb, 0); /* num_samples placeholder, ignored if 0 */
+    avio_w8(pb, 0x06); /* standard sample width filler */
+    avio_w8(pb, st->codecpar->ch_layout.nb_channels);
+    avio_wb16(pb, format_id);
+    avio_wb16(pb, loop_flag);
+    avio_wb32(pb, loop_start);
+    avio_wb32(pb, loop_end);
+
+    ctx->data_bytes_written = 0;
+
+    return 0;
+}
+
+static int mohawk_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    MohawkMuxContext *ctx = s->priv_data;
+    AVIOContext *pb = s->pb;
+
+    avio_write(pb, pkt->data, pkt->size);
+    ctx->data_bytes_written += pkt->size;
+
+    return 0;
+}
+
+static int mohawk_write_trailer(AVFormatContext *s)
+{
+    MohawkMuxContext *ctx = s->priv_data;
+    AVIOContext *pb = s->pb;
+    int64_t file_size = avio_tell(pb);
+
+    if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
+        /* Update overall file size */
+        avio_seek(pb, ctx->file_size_offset, SEEK_SET);
+        avio_wb32(pb, (uint32_t)file_size);
+
+        /* Update Data chunk size (20-byte header + payload) */
+        avio_seek(pb, ctx->data_size_offset, SEEK_SET);
+        avio_wb32(pb, (uint32_t)(ctx->data_bytes_written + 
MOHAWK_DATA_HEADER_SIZE));
+
+        avio_seek(pb, file_size, SEEK_SET);
+    }
+
+    return 0;
+}
+
+const FFOutputFormat ff_mohawk_muxer = {
+    .p.name         = "mohawk",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("Broderbund Mohawk"),
+    .p.extensions   = "mhk",
+    .p.audio_codec  = AV_CODEC_ID_PCM_U8,
+    .p.video_codec  = AV_CODEC_ID_NONE,
+    .p.flags        = AVFMT_NOTIMESTAMPS,
+    .priv_data_size = sizeof(MohawkMuxContext),
+    .write_header   = mohawk_write_header,
+    .write_packet   = mohawk_write_packet,
+    .write_trailer  = mohawk_write_trailer,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 14bf700274..eaf74946de 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  14
+#define LIBAVFORMAT_VERSION_MINOR  15
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.53.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to