FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/wc3movie.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 108 142 76.1%
Functions: 4 4 100.0%
Branches: 31 54 57.4%

Line Branch Exec Source
1 /*
2 * Wing Commander III Movie (.mve) File Demuxer
3 * Copyright (c) 2003 The FFmpeg project
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Wing Commander III Movie file demuxer
25 * by Mike Melanson (melanson@pcisys.net)
26 * for more information on the WC3 .mve file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 */
29
30 #include "libavutil/avstring.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
34 #include "libavutil/mem.h"
35 #include "avformat.h"
36 #include "demux.h"
37 #include "internal.h"
38
39 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
40 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
41 #define PC__TAG MKTAG('_', 'P', 'C', '_')
42 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
43 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
44 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
45 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
46 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
47 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
48 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
49 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
50 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
51 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
52
53 /* video resolution unless otherwise specified */
54 #define WC3_DEFAULT_WIDTH 320
55 #define WC3_DEFAULT_HEIGHT 165
56
57 /* always use the same PCM audio parameters */
58 #define WC3_SAMPLE_RATE 22050
59 #define WC3_AUDIO_BITS 16
60
61 /* nice, constant framerate */
62 #define WC3_FRAME_FPS 15
63
64 #define PALETTE_SIZE (256 * 3)
65
66 typedef struct Wc3DemuxContext {
67 int width;
68 int height;
69 int64_t pts;
70 int video_stream_index;
71 int audio_stream_index;
72
73 AVPacket *vpkt;
74
75 } Wc3DemuxContext;
76
77 1 static int wc3_read_close(AVFormatContext *s)
78 {
79 1 Wc3DemuxContext *wc3 = s->priv_data;
80
81 1 av_packet_free(&wc3->vpkt);
82
83 1 return 0;
84 }
85
86 7186 static int wc3_probe(const AVProbeData *p)
87 {
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
7186 if (p->buf_size < 12)
89 return 0;
90
91
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7165 times.
7186 if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
92
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 (AV_RL32(&p->buf[8]) != MOVE_TAG))
93 7185 return 0;
94
95 1 return AVPROBE_SCORE_MAX;
96 }
97
98 1 static int wc3_read_header(AVFormatContext *s)
99 {
100 1 Wc3DemuxContext *wc3 = s->priv_data;
101 1 AVIOContext *pb = s->pb;
102 unsigned int fourcc_tag;
103 unsigned int size;
104 AVStream *st;
105 1 int ret = 0;
106 char *buffer;
107
108 /* default context members */
109 1 wc3->width = WC3_DEFAULT_WIDTH;
110 1 wc3->height = WC3_DEFAULT_HEIGHT;
111 1 wc3->pts = 0;
112 1 wc3->video_stream_index = wc3->audio_stream_index = 0;
113 1 wc3->vpkt = av_packet_alloc();
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!wc3->vpkt)
115 return AVERROR(ENOMEM);
116
117 /* skip the first 3 32-bit numbers */
118 1 avio_skip(pb, 12);
119
120 /* traverse through the chunks and load the header information before
121 * the first BRCH tag */
122 1 fourcc_tag = avio_rl32(pb);
123 1 size = (avio_rb32(pb) + 1) & (~1);
124
125 do {
126
3/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
27 switch (fourcc_tag) {
127
128 2 case SOND_TAG:
129 case INDX_TAG:
130 /* SOND unknown, INDX unnecessary; ignore both */
131 2 avio_skip(pb, size);
132 2 break;
133
134 1 case PC__TAG:
135 /* number of palettes, unneeded */
136 1 avio_skip(pb, 12);
137 1 break;
138
139 case BNAM_TAG:
140 /* load up the name */
141 buffer = av_malloc(size+1);
142 if (!buffer)
143 return AVERROR(ENOMEM);
144 if ((ret = avio_read(pb, buffer, size)) != size) {
145 av_freep(&buffer);
146 return AVERROR(EIO);
147 }
148 buffer[size] = 0;
149 av_dict_set(&s->metadata, "title", buffer,
150 AV_DICT_DONT_STRDUP_VAL);
151 break;
152
153 case SIZE_TAG:
154 /* video resolution override */
155 wc3->width = avio_rl32(pb);
156 wc3->height = avio_rl32(pb);
157 break;
158
159 24 case PALT_TAG:
160 /* one of several palettes */
161 24 avio_seek(pb, -8, SEEK_CUR);
162 24 av_append_packet(pb, wc3->vpkt, 8 + PALETTE_SIZE);
163 24 break;
164
165 default:
166 av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
167 av_fourcc2str(fourcc_tag));
168 return AVERROR_INVALIDDATA;
169 }
170
171 27 fourcc_tag = avio_rl32(pb);
172 /* chunk sizes are 16-bit aligned */
173 27 size = (avio_rb32(pb) + 1) & (~1);
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (avio_feof(pb))
175 return AVERROR(EIO);
176
177
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
27 } while (fourcc_tag != BRCH_TAG);
178
179 /* initialize the decoder streams */
180 1 st = avformat_new_stream(s, NULL);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
182 return AVERROR(ENOMEM);
183 1 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
184 1 wc3->video_stream_index = st->index;
185 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
186 1 st->codecpar->codec_id = AV_CODEC_ID_XAN_WC3;
187 1 st->codecpar->codec_tag = 0; /* no fourcc */
188 1 st->codecpar->width = wc3->width;
189 1 st->codecpar->height = wc3->height;
190
191 1 st = avformat_new_stream(s, NULL);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
193 return AVERROR(ENOMEM);
194 1 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
195 1 wc3->audio_stream_index = st->index;
196 1 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
197 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
198 1 st->codecpar->codec_tag = 1;
199 1 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
200 1 st->codecpar->bits_per_coded_sample = WC3_AUDIO_BITS;
201 1 st->codecpar->sample_rate = WC3_SAMPLE_RATE;
202 1 st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate *
203 1 st->codecpar->bits_per_coded_sample;
204 1 st->codecpar->block_align = WC3_AUDIO_BITS * st->codecpar->ch_layout.nb_channels;
205
206 1 return 0;
207 }
208
209 71 static int wc3_read_packet(AVFormatContext *s,
210 AVPacket *pkt)
211 {
212 71 Wc3DemuxContext *wc3 = s->priv_data;
213 71 AVIOContext *pb = s->pb;
214 unsigned int fourcc_tag;
215 unsigned int size;
216 71 int packet_read = 0;
217 71 int ret = 0;
218 unsigned char text[1024];
219
220
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 70 times.
143 while (!packet_read) {
221
222 73 fourcc_tag = avio_rl32(pb);
223 /* chunk sizes are 16-bit aligned */
224 73 size = (avio_rb32(pb) + 1) & (~1);
225
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 72 times.
73 if (avio_feof(pb))
226 1 return AVERROR(EIO);
227
228
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
72 switch (fourcc_tag) {
229
230 case BRCH_TAG:
231 /* no-op */
232 72 break;
233
234 1 case SHOT_TAG:
235 /* load up new palette */
236 1 avio_seek(pb, -8, SEEK_CUR);
237 1 av_append_packet(pb, wc3->vpkt, 8 + 4);
238 1 break;
239
240 35 case VGA__TAG:
241 /* send out video chunk */
242 35 avio_seek(pb, -8, SEEK_CUR);
243 35 ret= av_append_packet(pb, wc3->vpkt, 8 + size);
244 // ignore error if we have some data
245
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (wc3->vpkt->size > 0)
246 35 ret = 0;
247 35 av_packet_move_ref(pkt, wc3->vpkt);
248 35 pkt->stream_index = wc3->video_stream_index;
249 35 pkt->pts = wc3->pts;
250 35 packet_read = 1;
251 35 break;
252
253 1 case TEXT_TAG:
254 /* subtitle chunk */
255
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
256 ret = AVERROR(EIO);
257 else {
258 1 int i = 0;
259 1 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
260
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
261 return AVERROR_INVALIDDATA;
262 1 av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
263 1 i += text[i] + 1;
264
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
265 return AVERROR_INVALIDDATA;
266 1 av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
267 1 i += text[i] + 1;
268
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
269 return AVERROR_INVALIDDATA;
270 1 av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
271 }
272 1 break;
273
274 35 case AUDI_TAG:
275 /* send out audio chunk */
276 35 ret= av_get_packet(pb, pkt, size);
277 35 pkt->stream_index = wc3->audio_stream_index;
278 35 pkt->pts = wc3->pts;
279
280 /* time to advance pts */
281 35 wc3->pts++;
282
283 35 packet_read = 1;
284 35 break;
285
286 default:
287 av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
288 av_fourcc2str(fourcc_tag));
289 ret = AVERROR_INVALIDDATA;
290 packet_read = 1;
291 break;
292 }
293 }
294
295 70 return ret;
296 }
297
298 const FFInputFormat ff_wc3_demuxer = {
299 .p.name = "wc3movie",
300 .p.long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
301 .priv_data_size = sizeof(Wc3DemuxContext),
302 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
303 .read_probe = wc3_probe,
304 .read_header = wc3_read_header,
305 .read_packet = wc3_read_packet,
306 .read_close = wc3_read_close,
307 };
308