Used in Myst/Riven and other titles released by Broderbund.

Utilizes PCM_U8,ADPCM_IMA_APC,MP2 codecs.
Includes loop points.

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

diff --git a/Changelog b/Changelog
index a4241958bc..885b8348b2 100644
--- a/Changelog
+++ b/Changelog
@@ -4,7 +4,7 @@ releases are sorted from youngest to oldest.
 version <next>:
 - Extend AMF Color Converter (vf_vpp_amf) HDR capabilities
 - LCEVC track muxing support in MP4 muxer
-
+- Broderbund Mohawk demuxer
 
 version 8.1:
 - ffprobe -codec option
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 47ac1989f2..f2fa2de70a 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -472,6 +472,8 @@ 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
+    @tab Audio format used by Broderbund.
 @item BRSTM                     @tab   @tab X
     @tab Audio format used on the Nintendo Wii.
 @item BW64                      @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index ec8aa551d7..04dc0f68de 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -384,6 +384,7 @@ OBJS-$(CONFIG_MMF_DEMUXER)               += mmf.o
 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_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 6ec361fb7b..e1549083d7 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -287,6 +287,7 @@ extern const FFInputFormat  ff_mmf_demuxer;
 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 FFInputFormat  ff_mov_demuxer;
 extern const FFOutputFormat ff_mov_muxer;
 extern const FFOutputFormat ff_mp2_muxer;
diff --git a/libavformat/mohawkdec.c b/libavformat/mohawkdec.c
new file mode 100644
index 0000000000..2f96661c6d
--- /dev/null
+++ b/libavformat/mohawkdec.c
@@ -0,0 +1,212 @@
+/*
+ * Broderbund Mohawk (MHWK) demuxer
+ *
+ * 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 "libavutil/intreadwrite.h"
+#include "libavutil/channel_layout.h"
+#include "avformat.h"
+#include "internal.h"
+#include "demux.h"
+#include "avio.h"
+
+/* Magic bytes definitions - Big Endian */
+#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 MKTAG_CUE  MKBETAG('C', 'u', 'e', '#')
+#define MKTAG_ADPC MKBETAG('A', 'D', 'P', 'C')
+
+#define MOHAWK_PACKET_SIZE 4096
+#define MOHAWK_DATA_HEADER_SIZE 20
+
+typedef struct MohawkContext {
+    int64_t remaining_size;
+} MohawkContext;
+
+static int mohawk_probe(const AVProbeData *p)
+{
+    /* Check for "MHWK" at 0x00 and "WAVE" at 0x08 */
+    if (AV_RB32(p->buf) == MKTAG_MHWK) {
+        if (AV_RB32(p->buf + 8) == MKTAG_WAVE)
+            return AVPROBE_SCORE_MAX;
+    }
+    return 0;
+}
+
+static int mohawk_read_header(AVFormatContext *s)
+{
+    MohawkContext *c = s->priv_data;
+    AVIOContext *pb = s->pb;
+    AVStream *st;
+    uint32_t chunk_id, chunk_size, magic;
+    uint32_t sample_rate, num_samples, loop_start = 0, loop_end = 0;
+    uint16_t format, loop_flag;
+    int channels;
+
+    magic = avio_rb32(pb);
+    if (magic != MKTAG_MHWK)
+        return AVERROR_INVALIDDATA;
+
+    /* Skip file size (4 bytes) */
+    avio_skip(pb, 4);
+
+    /* Check WAVE tag */
+    if (avio_rb32(pb) != MKTAG_WAVE) {
+        av_log(s, AV_LOG_ERROR, "WAVE tag not found.\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* Chunk traversal */
+    while (!avio_feof(pb)) {
+        chunk_id   = avio_rb32(pb);
+        chunk_size = avio_rb32(pb);
+
+        av_log(s, AV_LOG_DEBUG, "Chunk: 0x%08x Size: %u\n", chunk_id, 
chunk_size);
+
+        if (chunk_id == MKTAG_DATA) {
+            if (chunk_size < MOHAWK_DATA_HEADER_SIZE) {
+                av_log(s, AV_LOG_ERROR, "Data chunk too small: %u\n", 
chunk_size);
+                return AVERROR_INVALIDDATA;
+            }
+            c->remaining_size = chunk_size - MOHAWK_DATA_HEADER_SIZE;
+            break;
+        } else if (chunk_id == MKTAG_CUE || chunk_id == MKTAG_ADPC) {
+            avio_skip(pb, chunk_size);
+        } else {
+            int64_t file_size = avio_size(pb);
+            if (file_size > 0 && chunk_size > file_size) {
+                 av_log(s, AV_LOG_WARNING, "Impossible chunk size: %u > 
%"PRId64".\n", chunk_size, file_size);
+                 return AVERROR_INVALIDDATA;
+            }
+            avio_skip(pb, chunk_size);
+        }
+    }
+
+    if (chunk_id != MKTAG_DATA) {
+        av_log(s, AV_LOG_ERROR, "Data chunk not found.\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* Data Chunk Header - 20 bytes - Always Big Endian */
+    sample_rate = avio_rb16(pb);
+    num_samples = avio_rb32(pb);
+    avio_r8(pb); /* Skip sample width (Unused) 0x06 */
+    channels = avio_r8(pb); /* 0x07 */
+    format = avio_rb16(pb); /* 0x08 */
+    loop_flag = avio_rb16(pb); /* 0x0A */
+
+    av_log(s, AV_LOG_DEBUG, "Format: 0x%04x | Rate: %u | Channels: %d | Loop 
Flag: 0x%04x\n",
+           format, sample_rate, channels, loop_flag);
+
+    if (loop_flag == 0xFFFF) {
+        loop_start = avio_rb32(pb);
+        loop_end   = avio_rb32(pb);
+    } else {
+        avio_skip(pb, 8);
+    }
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->sample_rate = sample_rate;
+
+    if (channels <= 0) {
+        av_log(s, AV_LOG_WARNING, "Invalid channel count %d, defaulting to 
Mono.\n", channels);
+        channels = 1;
+    }
+
+    av_channel_layout_default(&st->codecpar->ch_layout, channels);
+
+    /* Codec Mapping */
+    switch (format) {
+    case 0x0000: /* 8-bit unsigned PCM */
+        st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
+        break;
+    case 0x0001: /* ADPCM APC */
+        /* Use ADPCM_IMA_APC (Cryo APC).
+         * Properties:
+         * 1. Raw stream (no block headers)
+         * 2. High-Low nibble order -> Fixes "hissing" seen with Low-High 
decoders like WS.
+         */
+        st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_APC;
+        st->codecpar->bits_per_coded_sample = 4;
+        st->codecpar->block_align = 0;
+        break;
+    case 0x0002: /* MPEG Layer II */
+        st->codecpar->codec_id = AV_CODEC_ID_MP2;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
+        break;
+    default:
+        av_log(s, AV_LOG_ERROR, "Unknown Mohawk audio format: 0x%04x\n", 
format);
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* Loop Metadata */
+    if (loop_flag == 0xFFFF) {
+        char buf[32];
+        snprintf(buf, sizeof(buf), "%u", loop_start);
+        av_dict_set(&st->metadata, "loop_start", buf, 0);
+        snprintf(buf, sizeof(buf), "%u", loop_end);
+        av_dict_set(&st->metadata, "loop_end", buf, 0);
+    }
+
+    if (num_samples > 0)
+        st->duration = num_samples;
+
+    avpriv_set_pts_info(st, 64, 1, sample_rate);
+
+    return 0;
+}
+
+static int mohawk_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    MohawkContext *c = s->priv_data;
+    int ret;
+    int64_t size_to_read;
+
+    if (c->remaining_size <= 0)
+        return AVERROR_EOF;
+
+    size_to_read = FFMIN(MOHAWK_PACKET_SIZE, c->remaining_size);
+
+    ret = av_get_packet(s->pb, pkt, size_to_read);
+    if (ret < 0)
+        return ret;
+
+    c->remaining_size -= ret;
+    pkt->stream_index = 0;
+    pkt->pts = AV_NOPTS_VALUE;
+    pkt->dts = AV_NOPTS_VALUE;
+
+    return ret;
+}
+
+const FFInputFormat ff_mohawk_demuxer = {
+    .p.name         = "mohawk",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("Broderbund Mohawk"),
+    .p.extensions   = "mhk",
+    .p.priv_class   = NULL,
+    .p.flags        = AVFMT_NOTIMESTAMPS,
+    .priv_data_size = sizeof(MohawkContext),
+    .read_probe     = mohawk_probe,
+    .read_header    = mohawk_read_header,
+    .read_packet    = mohawk_read_packet,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index cd62aaf1ca..14bf700274 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,8 +31,8 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  13
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MINOR  14
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.53.0

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

Reply via email to