2 * Wing Commander III Movie (.mve) File Demuxer
3 * Copyright (c) 2003 The FFmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
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/
30 #include "libavutil/avstring.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
37 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
38 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
39 #define PC__TAG MKTAG('_', 'P', 'C', '_')
40 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
41 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
42 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
43 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
44 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
45 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
46 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
47 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
48 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
49 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
51 /* video resolution unless otherwise specified */
52 #define WC3_DEFAULT_WIDTH 320
53 #define WC3_DEFAULT_HEIGHT 165
55 /* always use the same PCM audio parameters */
56 #define WC3_SAMPLE_RATE 22050
57 #define WC3_AUDIO_CHANNELS 1
58 #define WC3_AUDIO_BITS 16
60 /* nice, constant framerate */
61 #define WC3_FRAME_FPS 15
63 #define PALETTE_SIZE (256 * 3)
65 typedef struct Wc3DemuxContext {
69 int video_stream_index;
70 int audio_stream_index;
76 static int wc3_read_close(AVFormatContext *s)
78 Wc3DemuxContext *wc3 = s->priv_data;
80 if (wc3->vpkt.size > 0)
81 av_packet_unref(&wc3->vpkt);
86 static int wc3_probe(const AVProbeData *p)
91 if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
92 (AV_RL32(&p->buf[8]) != MOVE_TAG))
95 return AVPROBE_SCORE_MAX;
98 static int wc3_read_header(AVFormatContext *s)
100 Wc3DemuxContext *wc3 = s->priv_data;
101 AVIOContext *pb = s->pb;
102 unsigned int fourcc_tag;
108 /* default context members */
109 wc3->width = WC3_DEFAULT_WIDTH;
110 wc3->height = WC3_DEFAULT_HEIGHT;
112 wc3->video_stream_index = wc3->audio_stream_index = 0;
113 av_init_packet(&wc3->vpkt);
114 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
116 /* skip the first 3 32-bit numbers */
119 /* traverse through the chunks and load the header information before
120 * the first BRCH tag */
121 fourcc_tag = avio_rl32(pb);
122 size = (avio_rb32(pb) + 1) & (~1);
125 switch (fourcc_tag) {
129 /* SOND unknown, INDX unnecessary; ignore both */
134 /* number of palettes, unneeded */
139 /* load up the name */
140 buffer = av_malloc(size+1);
143 ret = AVERROR(ENOMEM);
146 if ((ret = avio_read(pb, buffer, size)) != size) {
152 av_dict_set(&s->metadata, "title", buffer,
153 AV_DICT_DONT_STRDUP_VAL);
157 /* video resolution override */
158 wc3->width = avio_rl32(pb);
159 wc3->height = avio_rl32(pb);
163 /* one of several palettes */
164 avio_seek(pb, -8, SEEK_CUR);
165 av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
169 av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
170 av_fourcc2str(fourcc_tag));
171 ret = AVERROR_INVALIDDATA;
175 fourcc_tag = avio_rl32(pb);
176 /* chunk sizes are 16-bit aligned */
177 size = (avio_rb32(pb) + 1) & (~1);
183 } while (fourcc_tag != BRCH_TAG);
185 /* initialize the decoder streams */
186 st = avformat_new_stream(s, NULL);
188 ret = AVERROR(ENOMEM);
191 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
192 wc3->video_stream_index = st->index;
193 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
194 st->codecpar->codec_id = AV_CODEC_ID_XAN_WC3;
195 st->codecpar->codec_tag = 0; /* no fourcc */
196 st->codecpar->width = wc3->width;
197 st->codecpar->height = wc3->height;
199 st = avformat_new_stream(s, NULL);
201 ret = AVERROR(ENOMEM);
204 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
205 wc3->audio_stream_index = st->index;
206 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
207 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
208 st->codecpar->codec_tag = 1;
209 st->codecpar->channels = WC3_AUDIO_CHANNELS;
210 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
211 st->codecpar->bits_per_coded_sample = WC3_AUDIO_BITS;
212 st->codecpar->sample_rate = WC3_SAMPLE_RATE;
213 st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
214 st->codecpar->bits_per_coded_sample;
215 st->codecpar->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
223 static int wc3_read_packet(AVFormatContext *s,
226 Wc3DemuxContext *wc3 = s->priv_data;
227 AVIOContext *pb = s->pb;
228 unsigned int fourcc_tag;
232 unsigned char text[1024];
234 while (!packet_read) {
236 fourcc_tag = avio_rl32(pb);
237 /* chunk sizes are 16-bit aligned */
238 size = (avio_rb32(pb) + 1) & (~1);
242 switch (fourcc_tag) {
249 /* load up new palette */
250 avio_seek(pb, -8, SEEK_CUR);
251 av_append_packet(pb, &wc3->vpkt, 8 + 4);
255 /* send out video chunk */
256 avio_seek(pb, -8, SEEK_CUR);
257 ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
258 // ignore error if we have some data
259 if (wc3->vpkt.size > 0)
262 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
263 pkt->stream_index = wc3->video_stream_index;
270 if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
274 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
275 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
276 return AVERROR_INVALIDDATA;
277 av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
279 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
280 return AVERROR_INVALIDDATA;
281 av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
283 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
284 return AVERROR_INVALIDDATA;
285 av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
290 /* send out audio chunk */
291 ret= av_get_packet(pb, pkt, size);
292 pkt->stream_index = wc3->audio_stream_index;
295 /* time to advance pts */
302 av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
303 av_fourcc2str(fourcc_tag));
304 ret = AVERROR_INVALIDDATA;
313 AVInputFormat ff_wc3_demuxer = {
315 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
316 .priv_data_size = sizeof(Wc3DemuxContext),
317 .read_probe = wc3_probe,
318 .read_header = wc3_read_header,
319 .read_packet = wc3_read_packet,
320 .read_close = wc3_read_close,