Debian Codegraph @

d2x-rebirth/libmve/mvelib.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 #include <string.h> // for mem* functions
8 #if !defined(_WIN32) && !defined(macintosh)
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #endif
14
15 #include "mvelib.h"
16 #include <memory>
17
18 namespace d2x {
19
20 namespace {
21
22 static const char MVE_HEADER[] = "Interplay MVE File\x1A";
23 constexpr short MVE_HDRCONST1 = 0x001A;
24 constexpr short MVE_HDRCONST2 = 0x0100;
25 constexpr short MVE_HDRCONST3 = 0x1133;
26
27 /*
28 * private utility functions
29 */
30 static int16_t _mve_get_short(const unsigned char *data);
31 static uint16_t _mve_get_ushort(const unsigned char *data);
32
33 /*
34 * private functions for mvefile
35 */
36 static int _mvefile_read_header(const MVEFILE *movie);
37 static void _mvefile_set_buffer_size(MVEFILE *movie, std::size_t buf_size);
38 static int _mvefile_fetch_next_chunk(MVEFILE *movie);
39
40 /*
41 * private functions for mvestream
42 */
43 static void _mvestream_reset(MVESTREAM *movie);
44
45 /************************************************************
46 * public MVEFILE functions
47 ************************************************************/
48
49 /*
50 * open an MVE file
51 */
52 std::unique_ptr<MVEFILE> mvefile_open(RWops_ptr stream)
53 {
54 if (!stream)
55 return nullptr;
56 /* create the file */
57 auto file = std::make_unique<MVEFILE>(std::move(stream));
58
59 /* initialize the file */
60 _mvefile_set_buffer_size(file.get(), 1024);
61
62 /* verify the file's header */
63 if (! _mvefile_read_header(file.get()))
64 {
65 return nullptr;
66 }
67
68 /* now, prefetch the next chunk */
69 _mvefile_fetch_next_chunk(file.get());
70 return file;
71 }
72
73 /*
74 * reset a MVE file
75 */
76 static void mvefile_reset(MVEFILE *file)
77 {
78 /* initialize the file */
79 _mvefile_set_buffer_size(file, 1024);
80
81 /* verify the file's header */
82 if (! _mvefile_read_header(file))
83 {
84 *file = {};
85 }
86
87 /* now, prefetch the next chunk */
88 _mvefile_fetch_next_chunk(file);
89 }
90
91 static bool have_segment_header(const MVEFILE *movie)
92 {
93 /* if nothing is cached, fail */
94 if (movie->next_segment >= movie->cur_chunk.size())
95 return false;
96 /* if we don't have enough data to get a segment, fail */
97 if (movie->cur_chunk.size() - movie->next_segment <= 4)
98 return false;
99 return true;
100 }
101
102 }
103
104 /*
105 * get the size of the next segment
106 */
107 int_fast32_t mvefile_get_next_segment_size(const MVEFILE *movie)
108 {
109 if (!have_segment_header(movie))
110 return -1;
111 /* otherwise, get the data length */
112 return _mve_get_short(&movie->cur_chunk[movie->next_segment]);
113 }
114
115 /*
116 * get type of next segment in chunk (0xff if no more segments in chunk)
117 */
118 mve_opcode mvefile_get_next_segment_major(const MVEFILE *movie)
119 {
120 if (!have_segment_header(movie))
121 return mve_opcode::None;
122 /* otherwise, get the data length */
123 return mve_opcode{movie->cur_chunk[movie->next_segment + 2]};
124 }
125
126 /*
127 * get subtype (version) of next segment in chunk (0xff if no more segments in
128 * chunk)
129 */
130 unsigned char mvefile_get_next_segment_minor(const MVEFILE *movie)
131 {
132 if (!have_segment_header(movie))
133 return 0xff;
134 /* otherwise, get the data length */
135 return movie->cur_chunk[movie->next_segment + 3];
136 }
137
138 /*
139 * see next segment (return NULL if no next segment)
140 */
141 const unsigned char *mvefile_get_next_segment(const MVEFILE *movie)
142 {
143 if (!have_segment_header(movie))
144 return NULL;
145
146 /* otherwise, get the data length */
147 return &movie->cur_chunk[movie->next_segment + 4];
148 }
149
150 /*
151 * advance to next segment
152 */
153 void mvefile_advance_segment(MVEFILE *movie)
154 {
155 if (!have_segment_header(movie))
156 return;
157 /* else, advance to next segment */
158 movie->next_segment +=
159 (4 + _mve_get_ushort(&movie->cur_chunk[movie->next_segment]));
160 }
161
162 /*
163 * fetch the next chunk (return 0 if at end of stream)
164 */
165 int mvefile_fetch_next_chunk(MVEFILE *movie)
166 {
167 return _mvefile_fetch_next_chunk(movie);
168 }
169
170 /************************************************************
171 * public MVESTREAM functions
172 ************************************************************/
173
174 /*
175 * open an MVE stream
176 */
177 MVESTREAM_ptr_t mve_open(const MVE_play_sounds audio_enabled, const int x, const int y, RWops_ptr stream)
178 {
179 /* allocate */
180 auto movie = std::make_unique<MVESTREAM>(audio_enabled, x, y);
181
182 /* open */
183 movie->movie = mvefile_open(std::move(stream));
184 if (!movie->movie)
185 {
186 return nullptr;
187 }
188 return MVESTREAM_ptr_t(movie.release());
189 }
190
191 /*
192 * reset an MVE stream
193 */
194 void mve_reset(MVESTREAM *movie)
195 {
196 _mvestream_reset(movie);
197 }
198
199 /*
200 * play next chunk
201 */
202 MVESTREAM::handle_result mve_play_next_chunk(MVESTREAM &movie)
203 {
204 const auto m = movie.movie.get();
205 /* loop over segments */
206 /* advance to next segment */
207 for (;; mvefile_advance_segment(m))
208 {
209 const auto major = mvefile_get_next_segment_major(m);
210 if (major == mve_opcode::None)
211 break;
212 switch (major)
213 {
214 case mve_opcode::endofstream:
215 case mve_opcode::endofchunk:
216 case mve_opcode::createtimer:
217 case mve_opcode::initaudiobuffers:
218 case mve_opcode::startstopaudio:
219 case mve_opcode::initvideobuffers:
220 case mve_opcode::displayvideo:
221 case mve_opcode::audioframedata:
222 case mve_opcode::audioframesilence:
223 case mve_opcode::initvideomode:
224 case mve_opcode::setpalette:
225 case mve_opcode::setdecodingmap:
226 case mve_opcode::videodata:
227 break;
228 default:
229 continue;
230 }
231 const auto minor = mvefile_get_next_segment_minor(m);
232 const auto len = mvefile_get_next_segment_size(m);
233 const auto data = mvefile_get_next_segment(m);
234 MVESTREAM::handle_result r;
235 switch (major)
236 {
237 case mve_opcode::endofstream:
238 r = movie.handle_mve_segment_endofstream();
239 break;
240 case mve_opcode::endofchunk:
241 r = movie.handle_mve_segment_endofchunk();
242 break;
243 case mve_opcode::createtimer:
244 r = movie.handle_mve_segment_createtimer(data);
245 break;
246 case mve_opcode::initaudiobuffers:
247 r = movie.handle_mve_segment_initaudiobuffers(minor, data);
248 break;
249 case mve_opcode::startstopaudio:
250 r = movie.handle_mve_segment_startstopaudio();
251 break;
252 case mve_opcode::initvideobuffers:
253 r = movie.handle_mve_segment_initvideobuffers(minor, data);
254 break;
255 case mve_opcode::displayvideo:
256 r = movie.handle_mve_segment_displayvideo();
257 break;
258 case mve_opcode::audioframedata:
259 case mve_opcode::audioframesilence:
260 r = movie.handle_mve_segment_audioframedata(major, data);
261 break;
262 case mve_opcode::initvideomode:
263 r = movie.handle_mve_segment_initvideomode(data);
264 break;
265 case mve_opcode::setpalette:
266 r = movie.handle_mve_segment_setpalette(data);
267 break;
268 case mve_opcode::setdecodingmap:
269 r = movie.handle_mve_segment_setdecodingmap(data, len);
270 break;
271 case mve_opcode::videodata:
272 r = movie.handle_mve_segment_videodata(data, len);
273 break;
274 default:
275 continue;
276 }
277 if (r == MVESTREAM::handle_result::step_finished)
278 return r;
279 }
280
281 if (!mvefile_fetch_next_chunk(m))
282 return MVESTREAM::handle_result::step_finished;
283
284 /* return status */
285 return MVESTREAM::handle_result::step_again;
286 }
287
288 /************************************************************
289 * private functions
290 ************************************************************/
291
292 /*
293 * allocate an MVEFILE
294 */
295 MVEFILE::MVEFILE(RWops_ptr stream) :
296 stream{std::move(stream)}
297 {
298 }
299
300 /*
301 * free an MVE file
302 */
303 MVEFILE::~MVEFILE() = default;
304
305 namespace {
306
307 /*
308 * read and verify the header of the recently opened file
309 */
310 static int _mvefile_read_header(const MVEFILE *movie)
311 {
312 unsigned char buffer[26];
313
314 /* check the file is open */
315 if (! movie->stream)
316 return 0;
317
318 /* check the file is long enough */
319 if (!MovieFileRead(movie->stream.get(), buffer))
320 return 0;
321
322 /* check the signature */
323 if (memcmp(buffer, MVE_HEADER, 20))
324 return 0;
325
326 /* check the hard-coded constants */
327 if (_mve_get_short(buffer+20) != MVE_HDRCONST1)
328 return 0;
329 if (_mve_get_short(buffer+22) != MVE_HDRCONST2)
330 return 0;
331 if (_mve_get_short(buffer+24) != MVE_HDRCONST3)
332 return 0;
333
334 return 1;
335 }
336
337 static void _mvefile_set_buffer_size(MVEFILE *movie, std::size_t buf_size)
338 {
339 /* check if this would be a redundant operation */
340 if (buf_size <= movie->cur_chunk.size())
341 return;
342
343 /* allocate new buffer */
344 /* copy old data */
345 /* free old buffer */
346 /* install new buffer */
347 movie->cur_chunk.resize(100 + buf_size);
348 }
349
350 static int _mvefile_fetch_next_chunk(MVEFILE *movie)
351 {
352 unsigned char buffer[4];
353 unsigned short length;
354
355 /* fail if not open */
356 if (! movie->stream)
357 return 0;
358
359 /* fail if we can't read the next segment descriptor */
360 if (!MovieFileRead(movie->stream.get(), buffer))
361 return 0;
362
363 /* pull out the next length */
364 length = _mve_get_short(buffer);
365
366 /* make sure we've got sufficient space */
367 movie->cur_chunk.resize(length);
368
369 /* read the chunk */
370 if (!MovieFileRead(movie->stream.get(), movie->cur_chunk))
371 return 0;
372 movie->next_segment = 0;
373
374 return 1;
375 }
376
377 static int16_t _mve_get_short(const unsigned char *data)
378 {
379 short value;
380 value = data[0] | (data[1] << 8);
381 return value;
382 }
383
384 static uint16_t _mve_get_ushort(const unsigned char *data)
385 {
386 unsigned short value;
387 value = data[0] | (data[1] << 8);
388 return value;
389 }
390
391 }
392
393 /*
394 * allocate an MVESTREAM
395 */
396 MVESTREAM::MVESTREAM(const MVE_play_sounds mve_audio_enabled, const int x, const int y) :
397 mve_audio_enabled{mve_audio_enabled},
398 /* allocate and zero-initialize everything */
399 destX{x},
400 destY{y}
401 {
402 }
403
404 MVESTREAM::~MVESTREAM()
405 {
406 }
407
408 namespace {
409
410 /*
411 * reset an MVESTREAM
412 */
413 static void _mvestream_reset(MVESTREAM *movie)
414 {
415 mvefile_reset(movie->movie.get());
416 }
417
418 }
419
420 }