| 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 |
//#define DEBUG |
| 8 |
|
| 9 |
#include "dxxsconf.h" |
| 10 |
#include <vector> |
| 11 |
#include <string.h> |
| 12 |
#include <time.h> |
| 13 |
#include <sys/time.h> |
| 14 |
#ifdef _WIN32 |
| 15 |
# include <windows.h> |
| 16 |
#else |
| 17 |
# include <errno.h> |
| 18 |
# include <fcntl.h> |
| 19 |
# ifdef macintosh |
| 20 |
# include <types.h> |
| 21 |
# include <OSUtils.h> |
| 22 |
# else |
| 23 |
# include <sys/types.h> |
| 24 |
# include <sys/stat.h> |
| 25 |
# include <unistd.h> |
| 26 |
# endif // macintosh |
| 27 |
#endif // _WIN32 |
| 28 |
|
| 29 |
#include <SDL.h> |
| 30 |
#if DXX_USE_SDLMIXER |
| 31 |
#include <SDL_mixer.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "config.h" |
| 35 |
#include "d_sdl_audio.h" |
| 36 |
#include "mvelib.h" |
| 37 |
#include "mve_audio.h" |
| 38 |
#include "byteutil.h" |
| 39 |
#include "decoders.h" |
| 40 |
#include "libmve.h" |
| 41 |
#include "args.h" |
| 42 |
#include "console.h" |
| 43 |
#include "u_mem.h" |
| 44 |
|
| 45 |
namespace d2x { |
| 46 |
|
| 47 |
int g_spdFactorNum{0}; |
| 48 |
static int g_spdFactorDenom=10; |
| 49 |
|
| 50 |
static int16_t get_short(const unsigned char *data) |
| 51 |
{ |
| 52 |
short value; |
| 53 |
value = data[0] | (data[1] << 8); |
| 54 |
return value; |
| 55 |
} |
| 56 |
|
| 57 |
static uint16_t get_ushort(const unsigned char *data) |
| 58 |
{ |
| 59 |
unsigned short value; |
| 60 |
value = data[0] | (data[1] << 8); |
| 61 |
return value; |
| 62 |
} |
| 63 |
|
| 64 |
static int32_t get_int(const unsigned char *data) |
| 65 |
{ |
| 66 |
int value; |
| 67 |
value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); |
| 68 |
return value; |
| 69 |
} |
| 70 |
|
| 71 |
/************************* |
| 72 |
* general handlers |
| 73 |
*************************/ |
| 74 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_endofstream() |
| 75 |
{ |
| 76 |
return MVESTREAM::handle_result::step_finished; |
| 77 |
} |
| 78 |
|
| 79 |
/************************* |
| 80 |
* timer handlers |
| 81 |
*************************/ |
| 82 |
|
| 83 |
/* |
| 84 |
* timer variables |
| 85 |
*/ |
| 86 |
static int micro_frame_delay=0; |
| 87 |
static int timer_started=0; |
| 88 |
static struct timeval timer_expire = {0, 0}; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
#ifndef DXX_HAVE_STRUCT_TIMESPEC |
| 93 |
struct timespec |
| 94 |
{ |
| 95 |
long int tv_sec; /* Seconds. */ |
| 96 |
long int tv_nsec; /* Nanoseconds. */ |
| 97 |
}; |
| 98 |
#endif |
| 99 |
|
| 100 |
#if defined(_WIN32) || defined(macintosh) |
| 101 |
int gettimeofday(struct timeval *tv, void *) |
| 102 |
{ |
| 103 |
static int counter = 0; |
| 104 |
#ifdef _WIN32 |
| 105 |
DWORD now = GetTickCount(); |
| 106 |
#else |
| 107 |
long now = TickCount(); |
| 108 |
#endif |
| 109 |
counter++; |
| 110 |
|
| 111 |
tv->tv_sec = now / 1000; |
| 112 |
tv->tv_usec = (now % 1000) * 1000 + counter; |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |
| 116 |
#endif // defined(_WIN32) || defined(macintosh) |
| 117 |
|
| 118 |
namespace d2x { |
| 119 |
|
| 120 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_createtimer(const unsigned char *data) |
| 121 |
{ |
| 122 |
|
| 123 |
#if !defined(_WIN32) && !defined(macintosh) // FIXME |
| 124 |
__extension__ long long temp; |
| 125 |
#else |
| 126 |
long temp; |
| 127 |
#endif |
| 128 |
|
| 129 |
if (timer_created) |
| 130 |
return MVESTREAM::handle_result::step_again; |
| 131 |
else |
| 132 |
timer_created = 1; |
| 133 |
|
| 134 |
micro_frame_delay = get_int(data) * static_cast<int>(get_short(data+4)); |
| 135 |
if (g_spdFactorNum != 0) |
| 136 |
{ |
| 137 |
temp = micro_frame_delay; |
| 138 |
temp *= g_spdFactorNum; |
| 139 |
temp /= g_spdFactorDenom; |
| 140 |
micro_frame_delay = static_cast<int>(temp); |
| 141 |
} |
| 142 |
return MVESTREAM::handle_result::step_again; |
| 143 |
} |
| 144 |
|
| 145 |
static void timer_stop(void) |
| 146 |
{ |
| 147 |
timer_expire.tv_sec = 0; |
| 148 |
timer_expire.tv_usec = 0; |
| 149 |
timer_started = 0; |
| 150 |
} |
| 151 |
|
| 152 |
static void timer_start(void) |
| 153 |
{ |
| 154 |
int nsec{0}; |
| 155 |
gettimeofday(&timer_expire, NULL); |
| 156 |
timer_expire.tv_usec += micro_frame_delay; |
| 157 |
if (timer_expire.tv_usec > 1000000) |
| 158 |
{ |
| 159 |
nsec = timer_expire.tv_usec / 1000000; |
| 160 |
timer_expire.tv_sec += nsec; |
| 161 |
timer_expire.tv_usec -= nsec*1000000; |
| 162 |
} |
| 163 |
timer_started=1; |
| 164 |
} |
| 165 |
|
| 166 |
static void do_timer_wait(void) |
| 167 |
{ |
| 168 |
int nsec{0}; |
| 169 |
struct timespec ts; |
| 170 |
struct timeval tv; |
| 171 |
if (! timer_started) |
| 172 |
return; |
| 173 |
|
| 174 |
gettimeofday(&tv, NULL); |
| 175 |
if (tv.tv_sec > timer_expire.tv_sec) |
| 176 |
goto end; |
| 177 |
else if (tv.tv_sec == timer_expire.tv_sec && tv.tv_usec >= timer_expire.tv_usec) |
| 178 |
goto end; |
| 179 |
|
| 180 |
ts.tv_sec = timer_expire.tv_sec - tv.tv_sec; |
| 181 |
ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec); |
| 182 |
if (ts.tv_nsec < 0) |
| 183 |
{ |
| 184 |
ts.tv_nsec += 1000000000UL; |
| 185 |
--ts.tv_sec; |
| 186 |
} |
| 187 |
#ifdef _WIN32 |
| 188 |
Sleep(ts.tv_sec * 1000 + ts.tv_nsec / 1000000); |
| 189 |
#elif defined(macintosh) |
| 190 |
Delay(ts.tv_sec * 1000 + ts.tv_nsec / 1000000, NULL); |
| 191 |
#else |
| 192 |
if (nanosleep(&ts, NULL) == -1 && errno == EINTR) |
| 193 |
exit(1); |
| 194 |
#endif |
| 195 |
|
| 196 |
end: |
| 197 |
timer_expire.tv_usec += micro_frame_delay; |
| 198 |
if (timer_expire.tv_usec > 1000000) |
| 199 |
{ |
| 200 |
nsec = timer_expire.tv_usec / 1000000; |
| 201 |
timer_expire.tv_sec += nsec; |
| 202 |
timer_expire.tv_usec -= nsec*1000000; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/************************* |
| 207 |
* audio handlers |
| 208 |
*************************/ |
| 209 |
|
| 210 |
namespace { |
| 211 |
|
| 212 |
enum : uint8_t |
| 213 |
{ |
| 214 |
MVE_AUDIO_FLAGS_STEREO = 1, |
| 215 |
MVE_AUDIO_FLAGS_16BIT = 2, |
| 216 |
MVE_AUDIO_FLAGS_COMPRESSED = 4, |
| 217 |
}; |
| 218 |
|
| 219 |
template <typename T> |
| 220 |
struct MVE_audio_clamp |
| 221 |
{ |
| 222 |
const unsigned scale; |
| 223 |
MVE_audio_clamp(const unsigned DigiVolume) : |
| 224 |
scale(DigiVolume) |
| 225 |
{ |
| 226 |
} |
| 227 |
T operator()(const T &i) const |
| 228 |
{ |
| 229 |
return (static_cast<int32_t>(i) * scale) / 8; |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
static void mve_audio_callback(void *userdata, unsigned char *stream, int len); |
| 236 |
|
| 237 |
static void mve_audio_callback(void *const vstream, unsigned char *stream, int len) |
| 238 |
{ |
| 239 |
auto &mvestream = *reinterpret_cast<MVESTREAM *>(vstream); |
| 240 |
#ifdef DXX_REPORT_TOTAL_LENGTH |
| 241 |
int total{0}; |
| 242 |
#endif |
| 243 |
if (mvestream.mve_audio_bufhead == mvestream.mve_audio_buftail) |
| 244 |
return /* 0 */; |
| 245 |
|
| 246 |
//con_printf(CON_CRITICAL, "+ <%d (%d), %d, %d>", mvestream.mve_audio_bufhead, mvestream.mve_audio_curbuf_curpos, mvestream.mve_audio_buftail, len); |
| 247 |
|
| 248 |
std::span<const std::byte> audio_buffer; |
| 249 |
while (mvestream.mve_audio_bufhead != mvestream.mve_audio_buftail /* while we have more buffers */ |
| 250 |
&& len > (audio_buffer = std::as_bytes(mvestream.mve_audio_buffers[mvestream.mve_audio_bufhead].span()).subspan(mvestream.mve_audio_curbuf_curpos)).size()) /* and while we need more data */ |
| 251 |
{ |
| 252 |
const auto length = audio_buffer.size(); |
| 253 |
memcpy(stream, /* cur output position */ |
| 254 |
audio_buffer.data(), /* cur input position */ |
| 255 |
length); /* cur input length */ |
| 256 |
|
| 257 |
#ifdef DXX_REPORT_TOTAL_LENGTH |
| 258 |
total += length; |
| 259 |
#endif |
| 260 |
stream += length; /* advance output */ |
| 261 |
len -= length; /* decrement avail ospace */ |
| 262 |
mvestream.mve_audio_buffers[mvestream.mve_audio_bufhead] = {}; /* free the buffer */ |
| 263 |
|
| 264 |
if (++mvestream.mve_audio_bufhead == mvestream.mve_audio_buffers.size()) /* next buffer */ |
| 265 |
mvestream.mve_audio_bufhead = 0; |
| 266 |
mvestream.mve_audio_curbuf_curpos = 0; |
| 267 |
} |
| 268 |
|
| 269 |
#ifdef DXX_REPORT_TOTAL_LENGTH |
| 270 |
//con_printf(CON_CRITICAL, "= <%d (%d), %d, %d>: %d", mvestream.mve_audio_bufhead, mvestream.mve_audio_curbuf_curpos, mvestream.mve_audio_buftail, len, total); |
| 271 |
#endif |
| 272 |
/* return total; */ |
| 273 |
|
| 274 |
if (len != 0 /* ospace remaining */ |
| 275 |
&& mvestream.mve_audio_bufhead != mvestream.mve_audio_buftail) /* buffers remaining */ |
| 276 |
{ |
| 277 |
memcpy(stream, /* dest */ |
| 278 |
(reinterpret_cast<uint8_t *>(mvestream.mve_audio_buffers[mvestream.mve_audio_bufhead].get())) + mvestream.mve_audio_curbuf_curpos, /* src */ |
| 279 |
len); /* length */ |
| 280 |
|
| 281 |
mvestream.mve_audio_curbuf_curpos += len; /* advance input */ |
| 282 |
stream += len; /* advance output (unnecessary) */ |
| 283 |
len -= len; /* advance output (unnecessary) */ |
| 284 |
|
| 285 |
auto &a = mvestream.mve_audio_buffers[mvestream.mve_audio_bufhead]; |
| 286 |
if (mvestream.mve_audio_curbuf_curpos >= a.span().size_bytes()) /* if this ends the current chunk */ |
| 287 |
{ |
| 288 |
a = {}; /* free buffer */ |
| 289 |
|
| 290 |
if (++mvestream.mve_audio_bufhead == mvestream.mve_audio_buffers.size()) /* next buffer */ |
| 291 |
mvestream.mve_audio_bufhead = 0; |
| 292 |
mvestream.mve_audio_curbuf_curpos = 0; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
//con_printf(CON_CRITICAL, "- <%d (%d), %d, %d>", mvestream.mve_audio_bufhead, mvestream.mve_audio_curbuf_curpos, mvestream.mve_audio_buftail, len); |
| 297 |
} |
| 298 |
|
| 299 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_initaudiobuffers(unsigned char minor, const unsigned char *data) |
| 300 |
{ |
| 301 |
if (mve_audio_enabled == MVE_play_sounds::silent) |
| 302 |
return MVESTREAM::handle_result::step_again; |
| 303 |
|
| 304 |
if (mve_audio_spec) |
| 305 |
return MVESTREAM::handle_result::step_again; |
| 306 |
|
| 307 |
auto flags{get_ushort(&data[2])}; |
| 308 |
const auto sample_rate{get_ushort(&data[4])}; |
| 309 |
|
| 310 |
const auto stereo{flags & MVE_AUDIO_FLAGS_STEREO}; |
| 311 |
const auto bitsize{flags & MVE_AUDIO_FLAGS_16BIT}; |
| 312 |
if (!minor) |
| 313 |
flags &= ~MVE_AUDIO_FLAGS_COMPRESSED; |
| 314 |
mve_audio_flags = flags; |
| 315 |
|
| 316 |
const unsigned format = (bitsize) |
| 317 |
? (words_bigendian ? AUDIO_S16MSB : AUDIO_S16LSB) |
| 318 |
: AUDIO_U8; |
| 319 |
|
| 320 |
if (CGameArg.SndDisableSdlMixer) |
| 321 |
{ |
| 322 |
con_puts(CON_CRITICAL, "creating audio buffers:"); |
| 323 |
const auto desired_buffer{get_int(&data[6])}; |
| 324 |
const auto compressed{flags & MVE_AUDIO_FLAGS_COMPRESSED}; |
| 325 |
con_printf(CON_CRITICAL, "sample rate = %d, desired buffer = %d, stereo = %d, bitsize = %d, compressed = %d", |
| 326 |
sample_rate, desired_buffer, stereo ? 1 : 0, bitsize ? 16 : 8, compressed ? 1 : 0); |
| 327 |
} |
| 328 |
|
| 329 |
mve_audio_spec = std::make_unique<SDL_AudioSpec>(); |
| 330 |
{ |
| 331 |
auto &s = *mve_audio_spec; |
| 332 |
s.freq = sample_rate; |
| 333 |
s.format = format; |
| 334 |
s.channels = (stereo) ? 2 : 1; |
| 335 |
s.samples = 4096; |
| 336 |
s.callback = mve_audio_callback; |
| 337 |
s.userdata = this; |
| 338 |
|
| 339 |
// MD2211: if using SDL_Mixer, we never reinit the sound system |
| 340 |
if (CGameArg.SndDisableSdlMixer) |
| 341 |
{ |
| 342 |
if (SDL_OpenAudio(&s, NULL) >= 0) { |
| 343 |
con_puts(CON_CRITICAL, " success"); |
| 344 |
} |
| 345 |
else { |
| 346 |
con_printf(CON_CRITICAL, " failure : %s", SDL_GetError()); |
| 347 |
mve_audio_spec = {}; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
#if DXX_USE_SDLMIXER |
| 352 |
else { |
| 353 |
// MD2211: using the same old SDL audio callback as a postmixer in SDL_mixer |
| 354 |
Mix_SetPostMix(s.callback, s.userdata); |
| 355 |
} |
| 356 |
#endif |
| 357 |
} |
| 358 |
|
| 359 |
mve_audio_buffers = {}; |
| 360 |
return MVESTREAM::handle_result::step_again; |
| 361 |
} |
| 362 |
|
| 363 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_startstopaudio() |
| 364 |
{ |
| 365 |
if (mve_audio_spec && !mve_audio_playing && mve_audio_bufhead != mve_audio_buftail) |
| 366 |
{ |
| 367 |
if (CGameArg.SndDisableSdlMixer) |
| 368 |
SDL_PauseAudio(0); |
| 369 |
#if DXX_USE_SDLMIXER |
| 370 |
else |
| 371 |
Mix_Pause(0); |
| 372 |
#endif |
| 373 |
mve_audio_playing = 1; |
| 374 |
} |
| 375 |
return MVESTREAM::handle_result::step_again; |
| 376 |
} |
| 377 |
|
| 378 |
/* Caution: the data length argument is unnamed here because it is sometimes |
| 379 |
* wrong. The first movie segment of the opening video of the game is |
| 380 |
* ill-formed: |
| 381 |
* - Its segment size according to mvelib is 2932. |
| 382 |
* - It has a 10 byte header: |
| 383 |
* - 2 bytes skipped |
| 384 |
* - 2 bytes of `chan` |
| 385 |
* - 2 bytes of `nsamp` |
| 386 |
* - 2 bytes of "current offset 0" |
| 387 |
* - 2 bytes of "current offset 1" |
| 388 |
* - It has an internal size of 2924, which is 2 bytes too long. |
| 389 |
* |
| 390 |
* If `std::span` is used to describe this segment, then the historically used |
| 391 |
* audio processing will read past the end of the `std::span`, which is |
| 392 |
* undefined and may trigger an assertion failure. Therefore, do not use |
| 393 |
* std::span to describe this block of memory. |
| 394 |
*/ |
| 395 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_audioframedata(const mve_opcode major, const unsigned char *data) |
| 396 |
{ |
| 397 |
static const int selected_chan=1; |
| 398 |
if (const auto mve_audio_spec = this->mve_audio_spec.get()) |
| 399 |
{ |
| 400 |
std::optional<RAII_SDL_LockAudio> lock_audio{ |
| 401 |
mve_audio_playing ? std::optional<RAII_SDL_LockAudio>(std::in_place) : std::nullopt |
| 402 |
}; |
| 403 |
|
| 404 |
const auto chan = get_ushort(data + 2); |
| 405 |
unsigned nsamp = get_ushort(data + 4); |
| 406 |
if (chan & selected_chan) |
| 407 |
{ |
| 408 |
decltype(mve_audio_buffers)::value_type p; |
| 409 |
const auto DigiVolume{CGameCfg.DigiVolume}; |
| 410 |
/* At volume 0 (minimum), no sound is wanted. */ |
| 411 |
if (DigiVolume && major == mve_opcode::audioframedata) { |
| 412 |
const auto flags{mve_audio_flags}; |
| 413 |
if (flags & MVE_AUDIO_FLAGS_COMPRESSED) { |
| 414 |
/* HACK: +4 mveaudio_uncompress adds 4 more bytes */ |
| 415 |
nsamp += 4; |
| 416 |
|
| 417 |
p = {nsamp / 2}; |
| 418 |
mveaudio_uncompress(p.span(), data); |
| 419 |
} else { |
| 420 |
nsamp -= 8; |
| 421 |
data += 8; |
| 422 |
|
| 423 |
p = {nsamp / 2}; |
| 424 |
memcpy(p.get(), data, nsamp); |
| 425 |
} |
| 426 |
if (DigiVolume < 8) |
| 427 |
{ |
| 428 |
/* At volume 8 (maximum), no scaling is needed. */ |
| 429 |
if (flags & MVE_AUDIO_FLAGS_16BIT) |
| 430 |
{ |
| 431 |
int16_t *const p16 = p.get(); |
| 432 |
std::transform(p16, reinterpret_cast<int16_t *>(reinterpret_cast<uint8_t *>(p16) + nsamp), p16, MVE_audio_clamp<int16_t>(DigiVolume)); |
| 433 |
} |
| 434 |
else |
| 435 |
{ |
| 436 |
int8_t *const p8 = reinterpret_cast<int8_t *>(p.get()); |
| 437 |
std::transform(p8, p8 + nsamp, p8, MVE_audio_clamp<int8_t>(DigiVolume)); |
| 438 |
} |
| 439 |
} |
| 440 |
} else { |
| 441 |
/* unique_span will value-initialize the buffer, so no explicit |
| 442 |
* initialization is necessary |
| 443 |
*/ |
| 444 |
p = {nsamp / 2}; |
| 445 |
} |
| 446 |
|
| 447 |
// MD2211: the following block does on-the-fly audio conversion for SDL_mixer |
| 448 |
#if DXX_USE_SDLMIXER |
| 449 |
if (!CGameArg.SndDisableSdlMixer) { |
| 450 |
// build converter: in = MVE format, out = SDL_mixer output |
| 451 |
int out_freq; |
| 452 |
Uint16 out_format; |
| 453 |
int out_channels; |
| 454 |
Mix_QuerySpec(&out_freq, &out_format, &out_channels); // get current output settings |
| 455 |
|
| 456 |
SDL_AudioCVT cvt{}; |
| 457 |
SDL_BuildAudioCVT(&cvt, mve_audio_spec->format, mve_audio_spec->channels, mve_audio_spec->freq, |
| 458 |
out_format, out_channels, out_freq); |
| 459 |
|
| 460 |
const auto cvtbuf = std::make_unique<uint8_t[]>(nsamp * cvt.len_mult); |
| 461 |
cvt.buf = cvtbuf.get(); |
| 462 |
cvt.len = nsamp; |
| 463 |
|
| 464 |
// read the audio buffer into the conversion buffer |
| 465 |
memcpy(cvt.buf, p.get(), nsamp); |
| 466 |
|
| 467 |
// do the conversion |
| 468 |
if (SDL_ConvertAudio(&cvt)) |
| 469 |
con_printf(CON_URGENT, "%s:%u: SDL_ConvertAudio failed: nsamp=%u out_format=%i out_channels=%i out_freq=%i", __FILE__, __LINE__, nsamp, out_format, out_channels, out_freq); |
| 470 |
else |
| 471 |
{ |
| 472 |
// copy back to the audio buffer |
| 473 |
const std::size_t converted_buffer_size = cvt.len_cvt; |
| 474 |
p = {}; // free the old audio buffer |
| 475 |
p = {converted_buffer_size / 2}; |
| 476 |
memcpy(p.get(), cvt.buf, converted_buffer_size); |
| 477 |
} |
| 478 |
} |
| 479 |
#endif |
| 480 |
mve_audio_buffers[mve_audio_buftail] = std::move(p); |
| 481 |
|
| 482 |
if (++mve_audio_buftail == mve_audio_buffers.size()) |
| 483 |
mve_audio_buftail = 0; |
| 484 |
|
| 485 |
if (mve_audio_buftail == mve_audio_bufhead) |
| 486 |
con_printf(CON_CRITICAL, "d'oh! buffer ring overrun (%d)", mve_audio_bufhead); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
return MVESTREAM::handle_result::step_again; |
| 491 |
} |
| 492 |
|
| 493 |
/************************* |
| 494 |
* video handlers |
| 495 |
*************************/ |
| 496 |
|
| 497 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_initvideobuffers(unsigned char minor, const unsigned char *data) |
| 498 |
{ |
| 499 |
short w, h; |
| 500 |
#ifdef DEBUG |
| 501 |
short count; |
| 502 |
#endif |
| 503 |
|
| 504 |
if (videobuf_created) |
| 505 |
return MVESTREAM::handle_result::step_again; |
| 506 |
else |
| 507 |
videobuf_created = 1; |
| 508 |
|
| 509 |
w = get_short(data); |
| 510 |
h = get_short(data+2); |
| 511 |
|
| 512 |
#ifdef DEBUG |
| 513 |
if (minor > 0) { |
| 514 |
count = get_short(data+4); |
| 515 |
} else { |
| 516 |
count = 1; |
| 517 |
} |
| 518 |
#endif |
| 519 |
|
| 520 |
if (minor > 1) { |
| 521 |
truecolor = !!get_short(data + 6); |
| 522 |
} else { |
| 523 |
truecolor = 0; |
| 524 |
} |
| 525 |
|
| 526 |
width = w << 3; |
| 527 |
height = h << 3; |
| 528 |
|
| 529 |
/* TODO: * 4 causes crashes on some files */ |
| 530 |
/* only malloc once */ |
| 531 |
vBuffers.assign(width * height * 8, 0); |
| 532 |
vBackBuf1 = vBuffers.data(); |
| 533 |
if (truecolor) { |
| 534 |
vBackBuf2 = reinterpret_cast<uint8_t *>(reinterpret_cast<uint16_t *>(vBackBuf1) + (width * height)); |
| 535 |
} else { |
| 536 |
vBackBuf2 = (vBackBuf1 + (width * height)); |
| 537 |
} |
| 538 |
#ifdef DEBUG |
| 539 |
con_printf(CON_CRITICAL, "DEBUG: w,h=%d,%d count=%d, tc=%d", w, h, count, truecolor); |
| 540 |
#endif |
| 541 |
|
| 542 |
return MVESTREAM::handle_result::step_again; |
| 543 |
} |
| 544 |
|
| 545 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_displayvideo() |
| 546 |
{ |
| 547 |
MovieShowFrame(vBackBuf1, destX, destY, width, height, screenWidth, screenHeight); |
| 548 |
|
| 549 |
frameUpdated = 1; |
| 550 |
|
| 551 |
return MVESTREAM::handle_result::step_again; |
| 552 |
} |
| 553 |
|
| 554 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_initvideomode(const unsigned char *data) |
| 555 |
{ |
| 556 |
short width, height; |
| 557 |
|
| 558 |
if (video_initialized) |
| 559 |
return MVESTREAM::handle_result::step_again; /* maybe we actually need to change width/height here? */ |
| 560 |
else |
| 561 |
video_initialized = 1; |
| 562 |
|
| 563 |
width = get_short(data); |
| 564 |
height = get_short(data+2); |
| 565 |
screenWidth = width; |
| 566 |
screenHeight = height; |
| 567 |
|
| 568 |
return MVESTREAM::handle_result::step_again; |
| 569 |
} |
| 570 |
|
| 571 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_setpalette(const unsigned char *data) |
| 572 |
{ |
| 573 |
short start, count; |
| 574 |
start = get_short(data); |
| 575 |
count = get_short(data+2); |
| 576 |
|
| 577 |
auto p = data + 4; |
| 578 |
MovieSetPalette(p - 3*start, start, count); |
| 579 |
return MVESTREAM::handle_result::step_again; |
| 580 |
} |
| 581 |
|
| 582 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_setdecodingmap(const unsigned char *data, int len) |
| 583 |
{ |
| 584 |
pCurMap = std::span<const uint8_t>(data, len); |
| 585 |
return MVESTREAM::handle_result::step_again; |
| 586 |
} |
| 587 |
|
| 588 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_videodata(const unsigned char *data, int len) |
| 589 |
{ |
| 590 |
unsigned short nFlags; |
| 591 |
|
| 592 |
// don't need those but kept for further reference |
| 593 |
// nFrameHot = get_short(data); |
| 594 |
// nFrameCold = get_short(data+2); |
| 595 |
// nXoffset = get_short(data+4); |
| 596 |
// nYoffset = get_short(data+6); |
| 597 |
// nXsize = get_short(data+8); |
| 598 |
// nYsize = get_short(data+10); |
| 599 |
nFlags = get_ushort(data+12); |
| 600 |
|
| 601 |
if (nFlags & 1) |
| 602 |
{ |
| 603 |
std::swap(vBackBuf1, vBackBuf2); |
| 604 |
} |
| 605 |
|
| 606 |
/* convert the frame */ |
| 607 |
if (truecolor) { |
| 608 |
decodeFrame16(reinterpret_cast<const uint16_t *>(vBackBuf2), width, height, vBackBuf1, pCurMap, data+14, len-14); |
| 609 |
} else { |
| 610 |
decodeFrame8(vBackBuf2, width, height, vBackBuf1, pCurMap, data+14, len-14); |
| 611 |
} |
| 612 |
|
| 613 |
return MVESTREAM::handle_result::step_again; |
| 614 |
} |
| 615 |
|
| 616 |
MVESTREAM::handle_result MVESTREAM::handle_mve_segment_endofchunk() |
| 617 |
{ |
| 618 |
pCurMap = {}; |
| 619 |
return MVESTREAM::handle_result::step_again; |
| 620 |
} |
| 621 |
|
| 622 |
MVESTREAM_ptr_t MVE_rmPrepMovie(const MVE_play_sounds audio_enabled, const int x, const int y, RWops_ptr src) |
| 623 |
{ |
| 624 |
MVESTREAM_ptr_t pMovie{mve_open(audio_enabled, x, y, std::move(src))}; |
| 625 |
if (!pMovie) |
| 626 |
return pMovie; |
| 627 |
|
| 628 |
auto &mve = *pMovie.get(); |
| 629 |
|
| 630 |
mve_play_next_chunk(mve); /* video initialization chunk */ |
| 631 |
mve_play_next_chunk(mve); /* audio initialization chunk */ |
| 632 |
return pMovie; |
| 633 |
} |
| 634 |
|
| 635 |
MVE_StepStatus MVE_rmStepMovie(MVESTREAM &mve) |
| 636 |
{ |
| 637 |
static int init_timer=0; |
| 638 |
|
| 639 |
if (!timer_started) |
| 640 |
timer_start(); |
| 641 |
|
| 642 |
for (;;) |
| 643 |
{ |
| 644 |
// make a "step" be a frame, not a chunk... |
| 645 |
if (mve_play_next_chunk(mve) == MVESTREAM::handle_result::step_finished) |
| 646 |
{ |
| 647 |
mve.frameUpdated = 0; |
| 648 |
return MVE_StepStatus::EndOfFile; |
| 649 |
} |
| 650 |
if (mve.frameUpdated) |
| 651 |
break; |
| 652 |
} |
| 653 |
mve.frameUpdated = 0; |
| 654 |
|
| 655 |
if (micro_frame_delay && !init_timer) { |
| 656 |
timer_start(); |
| 657 |
init_timer = 1; |
| 658 |
} |
| 659 |
|
| 660 |
do_timer_wait(); |
| 661 |
|
| 662 |
return MVE_StepStatus::Continue; |
| 663 |
} |
| 664 |
|
| 665 |
void MVE_rmEndMovie(std::unique_ptr<MVESTREAM> stream) |
| 666 |
{ |
| 667 |
timer_stop(); |
| 668 |
|
| 669 |
if (stream->mve_audio_spec) { |
| 670 |
// MD2211: if using SDL_Mixer, we never reinit sound, hence never close it |
| 671 |
if (CGameArg.SndDisableSdlMixer) |
| 672 |
{ |
| 673 |
SDL_CloseAudio(); |
| 674 |
} |
| 675 |
#if DXX_USE_SDLMIXER |
| 676 |
else |
| 677 |
Mix_SetPostMix(nullptr, nullptr); |
| 678 |
#endif |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
void MVE_rmHoldMovie() |
| 684 |
{ |
| 685 |
timer_started = 0; |
| 686 |
} |
| 687 |
|
| 688 |
} |