1 /*
2 * Simbiosis game demuxer
3 *
4 * Copyright (C) 2021 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "avformat.h"
24 #include "internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/internal.h"
28
29 #define IMX_TAG MKTAG('I', 'M', 'A', 'X')
30
31 typedef struct SimbiosisIMXDemuxContext {
32 uint8_t pal[AVPALETTE_SIZE];
33 int pal_changed;
34 int64_t first_video_packet_pos;
35 } SimbiosisIMXDemuxContext;
36
simbiosis_imx_probe(const AVProbeData *p)37 static int simbiosis_imx_probe(const AVProbeData *p)
38 {
39 if (AV_RL32(p->buf) != IMX_TAG)
40 return 0;
41 if (AV_RN32(p->buf+4) == 0)
42 return 0;
43 if (AV_RN16(p->buf+8) == 0)
44 return 0;
45 if (AV_RL16(p->buf+10) != 0x102)
46 return 0;
47
48 return AVPROBE_SCORE_EXTENSION + 10;
49 }
50
simbiosis_imx_read_header(AVFormatContext *s)51 static int simbiosis_imx_read_header(AVFormatContext *s)
52 {
53 AVIOContext *pb = s->pb;
54 AVStream *vst, *ast;
55 int rate;
56
57 vst = avformat_new_stream(s, NULL);
58 ast = avformat_new_stream(s, NULL);
59 if (!vst || !ast)
60 return AVERROR(ENOMEM);
61
62 avio_skip(pb, 4);
63
64 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
65 vst->codecpar->codec_tag = 0;
66 vst->codecpar->format = AV_PIX_FMT_PAL8;
67 vst->codecpar->codec_id = AV_CODEC_ID_SIMBIOSIS_IMX;
68 vst->start_time = 0;
69 vst->duration =
70 vst->nb_frames = avio_rl32(pb);
71 rate = avio_rl16(pb);
72 avio_skip(pb, 12);
73
74 avpriv_set_pts_info(vst, 64, 1, rate);
75
76 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
77 ast->codecpar->codec_tag = 0;
78 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
79 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
80 ast->codecpar->sample_rate = 22050;
81 ast->start_time = 0;
82
83 avpriv_set_pts_info(ast, 64, 1, 22050);
84
85 return 0;
86 }
87
simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)88 static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
89 {
90 AVIOContext *pb = s->pb;
91 SimbiosisIMXDemuxContext *imx = s->priv_data;
92 uint32_t chunk_size, chunk_type;
93 int64_t pos = avio_tell(pb);
94 int ret, idx = -1;
95
96 retry:
97 if (avio_feof(pb))
98 return AVERROR_EOF;
99
100 chunk_size = avio_rl32(pb);
101 chunk_type = avio_rl32(pb);
102
103 switch (chunk_type) {
104 case 0xAAFF:
105 return AVERROR_EOF;
106 case 0xAA99:
107 idx = 1;
108 break;
109 case 0xAA97:
110 idx = 0;
111 if (!imx->first_video_packet_pos)
112 imx->first_video_packet_pos = pos;
113 break;
114 case 0xAA98:
115 if (chunk_size > 256 * 3)
116 return AVERROR_INVALIDDATA;
117 for (int i = 0; i < chunk_size / 3; i++) {
118 unsigned r = avio_r8(pb) << 18;
119 unsigned g = avio_r8(pb) << 10;
120 unsigned b = avio_r8(pb) << 2;
121
122 AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
123 }
124 imx->pal_changed = 1;
125 idx = -1;
126 break;
127 default:
128 return AVERROR_INVALIDDATA;
129 }
130
131 if (idx == -1)
132 goto retry;
133
134 ret = av_get_packet(pb, pkt, chunk_size);
135 if (ret < 0)
136 return ret;
137
138 if (imx->pal_changed && idx == 0) {
139 uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
140 AVPALETTE_SIZE);
141 if (!pal)
142 return AVERROR(ENOMEM);
143 memcpy(pal, imx->pal, AVPALETTE_SIZE);
144 imx->pal_changed = 0;
145 if (pos <= imx->first_video_packet_pos)
146 pkt->flags |= AV_PKT_FLAG_KEY;
147 } else if (idx == 1) {
148 pkt->flags |= AV_PKT_FLAG_KEY;
149 }
150
151 pkt->pos = pos;
152 pkt->stream_index = idx;
153 pkt->duration = idx ? chunk_size : 1;
154
155 return ret;
156 }
157
158 const AVInputFormat ff_simbiosis_imx_demuxer = {
159 .name = "simbiosis_imx",
160 .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
161 .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
162 .read_probe = simbiosis_imx_probe,
163 .read_header = simbiosis_imx_read_header,
164 .read_packet = simbiosis_imx_read_packet,
165 .extensions = "imx",
166 .flags = AVFMT_GENERIC_INDEX,
167 };
168