Debian Codegraph @

libmve/mveplay.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2024 D2X Project
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define AUDIO
19
20 #include <chrono>
21 #include <cstring>
22 #include <deque>
23 #include <memory>
24 #include <thread>
25
26 #include "byteswap.h"
27 #include "chrono_timer.h"
28 #include "decoders.h"
29 #include "mvelib.h"
30 #include "mve_audio.h"
31
32 #ifdef AUDIO
33 #include "movie_sound.h"
34 #endif
35
36 #define MVE_OPCODE_ENDOFSTREAM 0x00 // mcmd_end
37 #define MVE_OPCODE_ENDOFCHUNK 0x01 // mcmd_next
38 #define MVE_OPCODE_CREATETIMER 0x02 // mcmd_syncInit
39 #define MVE_OPCODE_INITAUDIOBUFFERS 0x03 // mcmd_sndConfigure
40 #define MVE_OPCODE_STARTSTOPAUDIO 0x04 // mcmd_sndSync
41 #define MVE_OPCODE_INITVIDEOBUFFERS 0x05 // mcmd_nfConfig
42 #define MVE_OPCODE_UNKNOWN_06 0x06 // mcmd_nfDecomp
43 #define MVE_OPCODE_DISPLAYVIDEO 0x07 // mcmd_sfShowFrame
44 #define MVE_OPCODE_AUDIOFRAMEDATA 0x08 // mcmd_sndAdd
45 #define MVE_OPCODE_AUDIOFRAMESILENCE 0x09 // mcmd_sndSilence
46 #define MVE_OPCODE_INITVIDEOMODE 0x0A // mcmd_gfxMode
47 #define MVE_OPCODE_UNKNOWN_0B 0x0B // mcmd_palMakeSynthPalette
48 #define MVE_OPCODE_SETPALETTE 0x0C // mcmd_palLoadPalette
49 #define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D // mcmd_palLoadCompPalette
50 #define MVE_OPCODE_UNKNOWN_0E 0x0E // mcmd_nfChanges
51 #define MVE_OPCODE_SETDECODINGMAP 0x0F // mcmd_nfParms
52 #define MVE_OPCODE_UNKNOWN_10 0x10 // mcmd_nfDecompChg
53 #define MVE_OPCODE_VIDEODATA 0x11 // mcmd_nfPkDecomp
54
55 #define MVE_AUDIO_FLAGS_STEREO 1
56 #define MVE_AUDIO_FLAGS_16BIT 2
57 #define MVE_AUDIO_FLAGS_COMPRESSED 4
58
59 int g_spdFactorNum = 0;
60 static int g_spdFactorDenom = 10;
61 static int g_frameUpdated = 0;
62
63 static short get_short(const unsigned char *data) {
64 return D3::convert_le<int16_t>(data[0] | (data[1] << 8));
65 }
66
67 static unsigned short get_ushort(const unsigned char *data) {
68 return D3::convert_le<uint16_t>(data[0] | (data[1] << 8));
69 }
70
71 static int get_int(const unsigned char *data) {
72 return D3::convert_le<int32_t>(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
73 }
74
75 static unsigned int unhandled_chunks[32 * 256];
76
77 static int default_seg_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
78 unhandled_chunks[major << 8 | minor]++;
79 return 1;
80 }
81
82 /*************************
83 * general handlers
84 *************************/
85 static int end_movie_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
86 return 0;
87 }
88
89 /*************************
90 * timer handlers
91 *************************/
92
93 /*
94 * timer variables
95 */
96 static int timer_created = 0;
97 static int micro_frame_delay = 0;
98 static int timer_started = 0;
99 static unsigned long int timer_expire = 0;
100
101 unsigned long int timer_getmicroseconds() {
102 return D3::ChronoTimer::GetTimeUS();
103 }
104
105 void timer_sleepmicroseconds(unsigned long int usec) {
106 std::this_thread::sleep_for(std::chrono::microseconds(usec));
107 }
108
109 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
110 long temp;
111
112 if (timer_created)
113 return 1;
114 else
115 timer_created = 1;
116
117 micro_frame_delay = get_int(data) * (int)get_short(data + 4);
118 if (g_spdFactorNum != 0) {
119 temp = micro_frame_delay;
120 temp *= g_spdFactorNum;
121 temp /= g_spdFactorDenom;
122 micro_frame_delay = (int)temp;
123 }
124
125 return 1;
126 }
127
128 static void timer_stop() {
129 timer_expire = 0;
130 timer_started = 0;
131 }
132
133 static void timer_start() {
134 timer_expire = timer_getmicroseconds();
135 timer_expire += micro_frame_delay;
136 timer_started = 1;
137 }
138
139 #ifdef AUDIO
140 static std::unique_ptr<D3::MovieSoundDevice> snd_ds;
141 static int mve_audio_enabled = 1;
142 #else
143 static int mve_audio_enabled = 0;
144 #endif
145
146 static void do_timer_wait() {
147 uint64_t ts;
148 uint64_t tv;
149
150 if (!timer_started)
151 return;
152
153 tv = timer_getmicroseconds();
154 if (tv > timer_expire)
155 goto end;
156
157 ts = timer_expire - tv;
158
159 timer_sleepmicroseconds(ts);
160
161 end:
162 timer_expire += micro_frame_delay;
163 }
164
165 /*************************
166 * audio handlers
167 *************************/
168
169 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len,
170 void *context) {
171 #ifdef AUDIO
172 if (!mve_audio_enabled)
173 return 1;
174
175 int flags = get_ushort(data + 2);
176 int sample_rate = get_ushort(data + 4);
177 int desired_buffer = get_int(data + 6);
178
179 int channels = (flags & MVE_AUDIO_FLAGS_STEREO) ? 2 : 1;
180 int sample_size = (flags & MVE_AUDIO_FLAGS_16BIT) ? 2 : 1;
181
182 bool is_compressed = false;
183 if (minor > 0 && (flags & MVE_AUDIO_FLAGS_COMPRESSED)) {
184 is_compressed = true;
185 }
186
187 snd_ds = std::make_unique<D3::MovieSoundDevice>(sample_rate, sample_size, channels, 4096, is_compressed);
188 #endif
189
190 return 1;
191 }
192
193 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
194 #ifdef AUDIO
195 if (snd_ds && snd_ds->IsInitialized()) {
196 snd_ds->Play();
197 }
198 #endif
199 return 1;
200 }
201
202 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
203 #ifdef AUDIO
204 static const int selected_chan = 1;
205 if (snd_ds && snd_ds->IsInitialized()) {
206 int chan = get_ushort(data + 2);
207 int size = get_ushort(data + 4);
208 if (chan & selected_chan) {
209 void *buf = malloc(size);
210 if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
211 mveaudio_process((char *)buf, data, snd_ds->GetSampleSize(), snd_ds->IsCompressed());
212 } else {
213 // SILENCE, MORTALS!
214 memset(data, 0, size);
215 }
216 snd_ds->FillBuffer((char *)buf, size);
217 free(buf);
218 }
219 }
220 #endif
221
222 return 1;
223 }
224
225 /*************************
226 * video handlers
227 *************************/
228
229 static int videobuf_created = 0;
230 static int video_initialized = 0;
231 int g_width, g_height;
232 void *g_vBuffers = nullptr, *g_vBackBuf1, *g_vBackBuf2;
233
234 static int g_destX, g_destY;
235 static int g_screenWidth, g_screenHeight;
236 static unsigned char *g_pCurMap = nullptr;
237 static int g_nMapLength = 0;
238 static int g_truecolor;
239
240 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len,
241 void *context) {
242 short w, h;
243 short count, truecolor;
244
245 if (videobuf_created)
246 return 1;
247 else
248 videobuf_created = 1;
249
250 w = get_short(data);
251 h = get_short(data + 2);
252
253 if (minor > 0) {
254 count = get_short(data + 4);
255 } else {
256 count = 1;
257 }
258
259 if (minor > 1) {
260 truecolor = get_short(data + 6);
261 } else {
262 truecolor = 0;
263 }
264
265 g_width = w << 3;
266 g_height = h << 3;
267
268 /* TODO: * 4 causes crashes on some files */
269 /* only malloc once */
270 if (g_vBuffers == nullptr)
271 g_vBackBuf1 = g_vBuffers = mve_alloc(g_width * g_height * 8);
272 if (truecolor) {
273 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
274 } else {
275 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
276 }
277
278 memset(g_vBackBuf1, 0, g_width * g_height * 4);
279
280 g_truecolor = truecolor;
281
282 return 1;
283 }
284
285 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len,
286 void *context) {
287 if (g_destX == -1) // center it
288 g_destX = (g_screenWidth - g_width) >> 1;
289 if (g_destY == -1) // center it
290 g_destY = (g_screenHeight - g_height) >> 1;
291
292 mve_showframe((unsigned char *)g_vBackBuf1, g_width, g_height, 0, 0, g_width, g_height, g_destX, g_destY,
293 g_truecolor);
294
295 g_frameUpdated = 1;
296
297 return 1;
298 }
299
300 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
301 short width, height;
302
303 if (video_initialized)
304 return 1; /* maybe we actually need to change width/height here? */
305 else
306 video_initialized = 1;
307
308 width = get_short(data);
309 height = get_short(data + 2);
310 g_screenWidth = width;
311 g_screenHeight = height;
312
313 return 1;
314 }
315
316 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len,
317 void *context) {
318 short start, count;
319 unsigned char *p;
320
321 start = get_short(data);
322 count = get_short(data + 2);
323
324 p = data + 4;
325
326 mve_setpalette(p - 3 * start, start, count);
327
328 return 1;
329 }
330
331 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len,
332 void *context) {
333 g_pCurMap = data;
334 g_nMapLength = len;
335 return 1;
336 }
337
338 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
339 unsigned char *temp;
340
341 short nFrameHot = get_short(data);
342 short nFrameCold = get_short(data + 2);
343 short nXoffset = get_short(data + 4);
344 short nYoffset = get_short(data + 6);
345 short nXsize = get_short(data + 8);
346 short nYsize = get_short(data + 10);
347 unsigned short nFlags = get_ushort(data + 12);
348
349 if (nFlags & 1) {
350 temp = (unsigned char *)g_vBackBuf1;
351 g_vBackBuf1 = g_vBackBuf2;
352 g_vBackBuf2 = temp;
353 }
354
355 /* convert the frame */
356 if (g_truecolor) {
357 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data + 14, len - 14);
358 } else {
359 decodeFrame8((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data + 14, len - 14);
360 }
361
362 return 1;
363 }
364
365 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) {
366 g_pCurMap = nullptr;
367 return 1;
368 }
369
370 void MVE_ioCallbacks(mve_cb_Read io_read) { mve_read = io_read; }
371
372 void MVE_memCallbacks(mve_cb_Alloc mem_alloc, mve_cb_Free mem_free) {
373 mve_alloc = mem_alloc;
374 mve_free = mem_free;
375 }
376
377 void MVE_sfCallbacks(mve_cb_ShowFrame showframe) { mve_showframe = showframe; }
378
379 void MVE_palCallbacks(mve_cb_SetPalette setpalette) { mve_setpalette = setpalette; }
380
381 MVESTREAM* MVE_rmPrepMovie(void *src, int x, int y, int track) {
382 MVESTREAM *mve;
383 mve = mve_open(src);
384
385 if (!mve)
386 return nullptr;
387
388 g_destX = x;
389 g_destY = y;
390
391 for (int i = 0; i < 32; i++)
392 mve_set_handler(mve, i, default_seg_handler);
393
394 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
395 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
396 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
397 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
398 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
399 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
400
401 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
402 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
403 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
404 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
405
406 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
407 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
408
409 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
410
411 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
412
413 mve_play_next_chunk(mve); /* video initialization chunk */
414 if (mve_audio_enabled)
415 mve_play_next_chunk(mve); /* audio initialization chunk */
416
417 return mve;
418 }
419
420 void MVE_getVideoSpec(MVE_videoSpec *vSpec) {
421 vSpec->screenWidth = g_screenWidth;
422 vSpec->screenHeight = g_screenHeight;
423 vSpec->width = g_width;
424 vSpec->height = g_height;
425 vSpec->truecolor = g_truecolor;
426 }
427
428 int MVE_rmStepMovie(MVESTREAM *mve) {
429 static int init_timer = 0;
430 int cont = 1;
431
432 if (!timer_started)
433 timer_start();
434
435 while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
436 cont = mve_play_next_chunk(mve);
437 g_frameUpdated = 0;
438
439 if (!cont)
440 return MVE_ERR_EOF;
441
442 if (micro_frame_delay && !init_timer) {
443 timer_start();
444 init_timer = 1;
445 }
446
447 do_timer_wait();
448
449 return 0;
450 }
451
452 void MVE_rmEndMovie(MVESTREAM *mve) {
453 timer_stop();
454 timer_created = 0;
455
456 #ifdef AUDIO
457 snd_ds.reset();
458 #endif
459
460 mve_free(g_vBuffers);
461 g_vBuffers = nullptr;
462 g_pCurMap = nullptr;
463 g_nMapLength = 0;
464 videobuf_created = 0;
465 video_initialized = 0;
466
467 mve_close(mve);
468 mve = nullptr;
469 }
470
471 void MVE_rmHoldMovie() { timer_started = 0; }
472
473 void MVE_sndInit(const bool enable) {
474 #ifdef AUDIO
475 if (enable) {
476 mve_audio_enabled = 1;
477 } else {
478 mve_audio_enabled = 0;
479 }
480 #endif
481 }