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