[FFmpeg-devel] [PATCH 2/2] avformat: add Simbiosis IMX demuxer

Paul B Mahol onemda at gmail.com
Wed Feb 17 21:42:37 EET 2021


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/imx.c        | 160 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 162 insertions(+)
 create mode 100644 libavformat/imx.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 7bbf000cdc..e4d1f82c78 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -504,6 +504,7 @@ OBJS-$(CONFIG_SEGMENT_MUXER)             += segment.o
 OBJS-$(CONFIG_SER_DEMUXER)               += serdec.o
 OBJS-$(CONFIG_SHORTEN_DEMUXER)           += shortendec.o rawdec.o
 OBJS-$(CONFIG_SIFF_DEMUXER)              += siff.o
+OBJS-$(CONFIG_SIMBIOSIS_IMX_DEMUXER)     += imx.o
 OBJS-$(CONFIG_SINGLEJPEG_MUXER)          += rawenc.o
 OBJS-$(CONFIG_SLN_DEMUXER)               += pcmdec.o pcm.o
 OBJS-$(CONFIG_SMACKER_DEMUXER)           += smacker.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index f837ddabc8..3b69423508 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -403,6 +403,7 @@ extern AVOutputFormat ff_stream_segment_muxer;
 extern AVInputFormat  ff_ser_demuxer;
 extern AVInputFormat  ff_shorten_demuxer;
 extern AVInputFormat  ff_siff_demuxer;
+extern AVInputFormat  ff_simbiosis_imx_demuxer;
 extern AVOutputFormat ff_singlejpeg_muxer;
 extern AVInputFormat  ff_sln_demuxer;
 extern AVInputFormat  ff_smacker_demuxer;
diff --git a/libavformat/imx.c b/libavformat/imx.c
new file mode 100644
index 0000000000..66f18177a5
--- /dev/null
+++ b/libavformat/imx.c
@@ -0,0 +1,160 @@
+/*
+ * Simbiosis game demuxer
+ *
+ * Copyright (C) 2021 Paul B Mahol
+ *
+ * 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 "avformat.h"
+#include "internal.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/avassert.h"
+#include "libavutil/internal.h"
+#include "argo_asf.h"
+
+#define IMX_TAG MKTAG('I', 'M', 'A', 'X')
+
+typedef struct SimbiosisIMXDemuxContext {
+    uint8_t pal[AVPALETTE_SIZE];
+    int pal_changed;
+} SimbiosisIMXDemuxContext;
+
+static int simbiosis_imx_probe(const AVProbeData *p)
+{
+    if (AV_RL32(p->buf) != IMX_TAG)
+        return 0;
+    if (AV_RN32(p->buf+4) == 0)
+        return 0;
+    if (AV_RN16(p->buf+8) == 0)
+        return 0;
+    if (AV_RL16(p->buf+10) != 0x102)
+        return 0;
+
+    return AVPROBE_SCORE_EXTENSION + 10;
+}
+
+static int simbiosis_imx_read_header(AVFormatContext *s)
+{
+    AVIOContext *pb = s->pb;
+    AVStream *vst, *ast;
+    int rate;
+
+    vst = avformat_new_stream(s, NULL);
+    ast = avformat_new_stream(s, NULL);
+    if (!vst || !ast)
+        return AVERROR(ENOMEM);
+
+    avio_skip(pb, 4);
+
+    vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+    vst->codecpar->codec_tag  = 0;
+    vst->codecpar->format     = AV_PIX_FMT_PAL8;
+    vst->codecpar->codec_id   = AV_CODEC_ID_SIMBIOSIS_IMX;
+    vst->start_time = 0;
+    vst->nb_frames = avio_rl32(pb);
+    rate = avio_rl16(pb);
+    avio_skip(pb, 12);
+
+    avpriv_set_pts_info(vst, 64, 1, rate);
+
+    ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    ast->codecpar->codec_tag  = 0;
+    ast->codecpar->codec_id   = AV_CODEC_ID_PCM_U8;
+    ast->codecpar->channels   = 1;
+    ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
+    ast->codecpar->sample_rate = 22050;
+    ast->start_time = 0;
+
+    avpriv_set_pts_info(ast, 64, 1, 22050);
+
+    return 0;
+}
+
+static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVIOContext *pb = s->pb;
+    SimbiosisIMXDemuxContext *imx = s->priv_data;
+    uint32_t chunk_size, chunk_type;
+    int64_t pos = avio_tell(pb);
+    int ret, idx = -1;
+
+retry:
+    if (avio_feof(pb))
+        return AVERROR_EOF;
+
+    chunk_size = avio_rl32(pb);
+    chunk_type = avio_rl32(pb);
+
+    switch (chunk_type) {
+    case 0xAAFF:
+        return AVERROR_EOF;
+    case 0xAA99:
+        idx = 1;
+        break;
+    case 0xAA97:
+        idx = 0;
+        break;
+    case 0xAA98:
+        for (int i = 0; i < chunk_size / 3; i++) {
+            unsigned r = avio_r8(pb) << 18;
+            unsigned g = avio_r8(pb) << 10;
+            unsigned b = avio_r8(pb) <<  2;
+
+            AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
+        }
+        imx->pal_changed = 1;
+        idx = -1;
+        break;
+    default:
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (idx == -1)
+        goto retry;
+
+    ret = av_get_packet(pb, pkt, chunk_size);
+    if (ret < 0)
+        return ret;
+
+    if (imx->pal_changed && idx == 0) {
+        uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
+                                               AVPALETTE_SIZE);
+        if (!pal)
+            return AVERROR(ENOMEM);
+        memcpy(pal, imx->pal, AVPALETTE_SIZE);
+        imx->pal_changed = 0;
+        pkt->flags |= AV_PKT_FLAG_KEY;
+    }
+
+    pkt->pos = pos;
+    pkt->stream_index = idx;
+    pkt->duration = idx ? chunk_size : 1;
+
+    return ret;
+}
+
+AVInputFormat ff_simbiosis_imx_demuxer = {
+    .name           = "simbiosis_imx",
+    .long_name      = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
+    .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
+    .read_probe     = simbiosis_imx_probe,
+    .read_header    = simbiosis_imx_read_header,
+    .read_packet    = simbiosis_imx_read_packet,
+    .extensions     = "imx",
+    .flags          = AVFMT_GENERIC_INDEX,
+};
-- 
2.17.1



More information about the ffmpeg-devel mailing list