Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/wc3movie.c
Line
Count
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 "avio_internal.h"
37
#include "demux.h"
38
#include "internal.h"
39
40
819k
#define FORM_TAG MKTAG('F', 'O', 'R', 'M')
41
10.4k
#define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
42
415
#define  PC__TAG MKTAG('_', 'P', 'C', '_')
43
474
#define SOND_TAG MKTAG('S', 'O', 'N', 'D')
44
2.96k
#define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
45
941
#define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
46
356
#define PALT_TAG MKTAG('P', 'A', 'L', 'T')
47
909
#define INDX_TAG MKTAG('I', 'N', 'D', 'X')
48
5.47k
#define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
49
1.71k
#define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
50
637k
#define VGA__TAG MKTAG('V', 'G', 'A', ' ')
51
747
#define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
52
984k
#define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
53
54
/* video resolution unless otherwise specified */
55
3.19k
#define WC3_DEFAULT_WIDTH 320
56
3.19k
#define WC3_DEFAULT_HEIGHT 165
57
58
/* always use the same PCM audio parameters */
59
2.20k
#define WC3_SAMPLE_RATE 22050
60
4.41k
#define WC3_AUDIO_BITS 16
61
62
/* nice, constant framerate */
63
4.41k
#define WC3_FRAME_FPS 15
64
65
356
#define PALETTE_SIZE (256 * 3)
66
67
typedef struct Wc3DemuxContext {
68
    int width;
69
    int height;
70
    int64_t pts;
71
    int video_stream_index;
72
    int audio_stream_index;
73
74
    AVPacket *vpkt;
75
76
} Wc3DemuxContext;
77
78
static int wc3_read_close(AVFormatContext *s)
79
3.19k
{
80
3.19k
    Wc3DemuxContext *wc3 = s->priv_data;
81
82
3.19k
    av_packet_free(&wc3->vpkt);
83
84
3.19k
    return 0;
85
3.19k
}
86
87
static int wc3_probe(const AVProbeData *p)
88
936k
{
89
936k
    if (p->buf_size < 12)
90
117k
        return 0;
91
92
819k
    if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
93
10.4k
        (AV_RL32(&p->buf[8]) != MOVE_TAG))
94
819k
        return 0;
95
96
36
    return AVPROBE_SCORE_MAX;
97
819k
}
98
99
static int wc3_read_header(AVFormatContext *s)
100
3.19k
{
101
3.19k
    Wc3DemuxContext *wc3 = s->priv_data;
102
3.19k
    AVIOContext *pb = s->pb;
103
3.19k
    unsigned int fourcc_tag;
104
3.19k
    unsigned int size;
105
3.19k
    AVStream *st;
106
3.19k
    int ret = 0;
107
3.19k
    char *buffer;
108
109
    /* default context members */
110
3.19k
    wc3->width = WC3_DEFAULT_WIDTH;
111
3.19k
    wc3->height = WC3_DEFAULT_HEIGHT;
112
3.19k
    wc3->pts = 0;
113
3.19k
    wc3->video_stream_index = wc3->audio_stream_index = 0;
114
3.19k
    wc3->vpkt = av_packet_alloc();
115
3.19k
    if (!wc3->vpkt)
116
0
        return AVERROR(ENOMEM);
117
118
    /* skip the first 3 32-bit numbers */
119
3.19k
    avio_skip(pb, 12);
120
121
    /* traverse through the chunks and load the header information before
122
     * the first BRCH tag */
123
3.19k
    fourcc_tag = avio_rl32(pb);
124
3.19k
    size = (avio_rb32(pb) + 1) & (~1);
125
126
6.13k
    do {
127
6.13k
        switch (fourcc_tag) {
128
129
474
        case SOND_TAG:
130
909
        case INDX_TAG:
131
            /* SOND unknown, INDX unnecessary; ignore both */
132
909
            avio_skip(pb, size);
133
909
            break;
134
135
415
        case PC__TAG:
136
            /* number of palettes, unneeded */
137
415
            avio_skip(pb, 12);
138
415
            break;
139
140
2.96k
        case BNAM_TAG:
141
            /* load up the name */
142
2.96k
            buffer = av_malloc(size+1);
143
2.96k
            if (!buffer)
144
31
                return AVERROR(ENOMEM);
145
2.93k
            if ((ret = ffio_read_size(pb, buffer, size)) < 0) {
146
91
                av_freep(&buffer);
147
91
                return ret;
148
91
            }
149
2.83k
            buffer[size] = 0;
150
2.83k
            av_dict_set(&s->metadata, "title", buffer,
151
2.83k
                                   AV_DICT_DONT_STRDUP_VAL);
152
2.83k
            break;
153
154
941
        case SIZE_TAG:
155
            /* video resolution override */
156
941
            wc3->width  = avio_rl32(pb);
157
941
            wc3->height = avio_rl32(pb);
158
941
            break;
159
160
356
        case PALT_TAG:
161
            /* one of several palettes */
162
356
            avio_seek(pb, -8, SEEK_CUR);
163
356
            av_append_packet(pb, wc3->vpkt, 8 + PALETTE_SIZE);
164
356
            break;
165
166
551
        default:
167
551
            av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
168
551
                   av_fourcc2str(fourcc_tag));
169
551
            return AVERROR_INVALIDDATA;
170
6.13k
        }
171
172
5.46k
        fourcc_tag = avio_rl32(pb);
173
        /* chunk sizes are 16-bit aligned */
174
5.46k
        size = (avio_rb32(pb) + 1) & (~1);
175
5.46k
        if (avio_feof(pb))
176
317
            return AVERROR_INVALIDDATA;
177
178
5.46k
    } while (fourcc_tag != BRCH_TAG);
179
180
    /* initialize the decoder streams */
181
2.20k
    st = avformat_new_stream(s, NULL);
182
2.20k
    if (!st)
183
0
        return AVERROR(ENOMEM);
184
2.20k
    avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
185
2.20k
    wc3->video_stream_index = st->index;
186
2.20k
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
187
2.20k
    st->codecpar->codec_id = AV_CODEC_ID_XAN_WC3;
188
2.20k
    st->codecpar->codec_tag = 0;  /* no fourcc */
189
2.20k
    st->codecpar->width = wc3->width;
190
2.20k
    st->codecpar->height = wc3->height;
191
192
2.20k
    st = avformat_new_stream(s, NULL);
193
2.20k
    if (!st)
194
0
        return AVERROR(ENOMEM);
195
2.20k
    avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
196
2.20k
    wc3->audio_stream_index = st->index;
197
2.20k
    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
198
2.20k
    st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
199
2.20k
    st->codecpar->codec_tag = 1;
200
2.20k
    st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
201
2.20k
    st->codecpar->bits_per_coded_sample = WC3_AUDIO_BITS;
202
2.20k
    st->codecpar->sample_rate = WC3_SAMPLE_RATE;
203
2.20k
    st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate *
204
2.20k
        st->codecpar->bits_per_coded_sample;
205
2.20k
    st->codecpar->block_align = WC3_AUDIO_BITS * st->codecpar->ch_layout.nb_channels;
206
207
2.20k
    return 0;
208
2.20k
}
209
210
static int wc3_read_packet(AVFormatContext *s,
211
                           AVPacket *pkt)
212
1.62M
{
213
1.62M
    Wc3DemuxContext *wc3 = s->priv_data;
214
1.62M
    AVIOContext *pb = s->pb;
215
1.62M
    unsigned int fourcc_tag;
216
1.62M
    unsigned int size;
217
1.62M
    int packet_read = 0;
218
1.62M
    int ret = 0;
219
1.62M
    unsigned char text[1024];
220
221
3.25M
    while (!packet_read) {
222
223
1.62M
        fourcc_tag = avio_rl32(pb);
224
        /* chunk sizes are 16-bit aligned */
225
1.62M
        size = (avio_rb32(pb) + 1) & (~1);
226
1.62M
        if (avio_feof(pb))
227
2.55k
            return AVERROR_INVALIDDATA;
228
229
1.62M
        switch (fourcc_tag) {
230
231
328
        case BRCH_TAG:
232
            /* no-op */
233
328
            break;
234
235
1.71k
        case SHOT_TAG:
236
            /* load up new palette */
237
1.71k
            avio_seek(pb, -8, SEEK_CUR);
238
1.71k
            av_append_packet(pb, wc3->vpkt, 8 + 4);
239
1.71k
            break;
240
241
637k
        case VGA__TAG:
242
            /* send out video chunk */
243
637k
            avio_seek(pb, -8, SEEK_CUR);
244
637k
            ret= av_append_packet(pb, wc3->vpkt, 8 + size);
245
            // ignore error if we have some data
246
637k
            if (wc3->vpkt->size > 0)
247
8.32k
                ret = 0;
248
637k
            av_packet_move_ref(pkt, wc3->vpkt);
249
637k
            pkt->stream_index = wc3->video_stream_index;
250
637k
            pkt->pts = wc3->pts;
251
637k
            packet_read = 1;
252
637k
            break;
253
254
747
        case TEXT_TAG:
255
            /* subtitle chunk */
256
747
            if ((unsigned)size > sizeof(text))
257
257
                ret = AVERROR_INVALIDDATA;
258
490
            else if ((ret = ffio_read_size(pb, text, size)) == size) {
259
480
                int i = 0;
260
480
                av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
261
480
                if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
262
10
                    return AVERROR_INVALIDDATA;
263
470
                av_log (s, AV_LOG_DEBUG, "  inglish: %s\n", &text[i + 1]);
264
470
                i += text[i] + 1;
265
470
                if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
266
27
                    return AVERROR_INVALIDDATA;
267
443
                av_log (s, AV_LOG_DEBUG, "  doytsch: %s\n", &text[i + 1]);
268
443
                i += text[i] + 1;
269
443
                if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
270
26
                    return AVERROR_INVALIDDATA;
271
417
                av_log (s, AV_LOG_DEBUG, "  fronsay: %s\n", &text[i + 1]);
272
417
            }
273
684
            break;
274
275
984k
        case AUDI_TAG:
276
            /* send out audio chunk */
277
984k
            ret= av_get_packet(pb, pkt, size);
278
984k
            pkt->stream_index = wc3->audio_stream_index;
279
984k
            pkt->pts = wc3->pts;
280
281
            /* time to advance pts */
282
984k
            wc3->pts++;
283
284
984k
            packet_read = 1;
285
984k
            break;
286
287
756
        default:
288
756
            av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
289
756
                   av_fourcc2str(fourcc_tag));
290
756
            ret = AVERROR_INVALIDDATA;
291
756
            packet_read = 1;
292
756
            break;
293
1.62M
        }
294
1.62M
    }
295
296
1.62M
    return ret;
297
1.62M
}
298
299
const FFInputFormat ff_wc3_demuxer = {
300
    .p.name         = "wc3movie",
301
    .p.long_name    = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
302
    .priv_data_size = sizeof(Wc3DemuxContext),
303
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
304
    .read_probe     = wc3_probe,
305
    .read_header    = wc3_read_header,
306
    .read_packet    = wc3_read_packet,
307
    .read_close     = wc3_read_close,
308
};