2 * Simbiosis game demuxer
4 * Copyright (C) 2021 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/internal.h"
29 #define IMX_TAG MKTAG('I', 'M', 'A', 'X')
31 typedef struct SimbiosisIMXDemuxContext {
32 uint8_t pal[AVPALETTE_SIZE];
34 } SimbiosisIMXDemuxContext;
36 static int simbiosis_imx_probe(const AVProbeData *p)
38 if (AV_RL32(p->buf) != IMX_TAG)
40 if (AV_RN32(p->buf+4) == 0)
42 if (AV_RN16(p->buf+8) == 0)
44 if (AV_RL16(p->buf+10) != 0x102)
47 return AVPROBE_SCORE_EXTENSION + 10;
50 static int simbiosis_imx_read_header(AVFormatContext *s)
52 AVIOContext *pb = s->pb;
56 vst = avformat_new_stream(s, NULL);
57 ast = avformat_new_stream(s, NULL);
59 return AVERROR(ENOMEM);
63 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
64 vst->codecpar->codec_tag = 0;
65 vst->codecpar->format = AV_PIX_FMT_PAL8;
66 vst->codecpar->codec_id = AV_CODEC_ID_SIMBIOSIS_IMX;
68 vst->nb_frames = avio_rl32(pb);
72 avpriv_set_pts_info(vst, 64, 1, rate);
74 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
75 ast->codecpar->codec_tag = 0;
76 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
77 ast->codecpar->channels = 1;
78 ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
79 ast->codecpar->sample_rate = 22050;
82 avpriv_set_pts_info(ast, 64, 1, 22050);
87 static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
89 AVIOContext *pb = s->pb;
90 SimbiosisIMXDemuxContext *imx = s->priv_data;
91 uint32_t chunk_size, chunk_type;
92 int64_t pos = avio_tell(pb);
99 chunk_size = avio_rl32(pb);
100 chunk_type = avio_rl32(pb);
102 switch (chunk_type) {
112 for (int i = 0; i < chunk_size / 3; i++) {
113 unsigned r = avio_r8(pb) << 18;
114 unsigned g = avio_r8(pb) << 10;
115 unsigned b = avio_r8(pb) << 2;
117 AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
119 imx->pal_changed = 1;
123 return AVERROR_INVALIDDATA;
129 ret = av_get_packet(pb, pkt, chunk_size);
133 if (imx->pal_changed && idx == 0) {
134 uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
137 return AVERROR(ENOMEM);
138 memcpy(pal, imx->pal, AVPALETTE_SIZE);
139 imx->pal_changed = 0;
140 pkt->flags |= AV_PKT_FLAG_KEY;
144 pkt->stream_index = idx;
145 pkt->duration = idx ? chunk_size : 1;
150 AVInputFormat ff_simbiosis_imx_demuxer = {
151 .name = "simbiosis_imx",
152 .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
153 .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
154 .read_probe = simbiosis_imx_probe,
155 .read_header = simbiosis_imx_read_header,
156 .read_packet = simbiosis_imx_read_packet,
158 .flags = AVFMT_GENERIC_INDEX,