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