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