2502
|
1 #define _CRTDBG_MAP_ALLOC
|
2567
|
2 #define _CRT_SECURE_NO_WARNINGS
|
2502
|
3 #include <stdlib.h>
|
|
4 #include <crtdbg.h>
|
|
5 #include <vector>
|
|
6 #include <deque>
|
|
7
|
2541
|
8 #include "Engine/Engine.h"
|
2502
|
9
|
|
10 #include "IO/Mouse.h"
|
|
11 #include "GUI/GUIWindow.h"
|
|
12 #include "Media/Audio/OpenALSoundProvider.h"
|
|
13 #include "MediaPlayer.h"
|
|
14 #include "Media/Video/Bink_Smacker.h"
|
|
15 #include "Media/Audio/AudioPlayer.h"
|
|
16 #include "Engine/Timer.h"
|
|
17 #include "Engine/Graphics/Render.h"
|
2508
|
18 #include "Engine/MMT.h"
|
2502
|
19
|
2541
|
20 #include "Game/MainMenu.h"
|
|
21
|
2502
|
22
|
|
23 #pragma comment(lib, "Version.lib")
|
|
24
|
2541
|
25
|
2502
|
26 using namespace Media;
|
|
27
|
|
28 Media::MPlayer *pMediaPlayer = nullptr;
|
|
29 Media::IMovie *pMovie_Track;
|
|
30 Media::ITrack *pAudio_Track;
|
|
31 Movie *movie;
|
|
32
|
|
33 int mSourceID;
|
|
34
|
|
35 void PlayMovie(const wchar_t * pFilename);
|
|
36 void PlayAudio(const wchar_t * pFilename);
|
|
37 void LoadMovie(const char *);
|
|
38
|
|
39 class MemoryStream
|
|
40 {
|
|
41 public:
|
|
42 inline MemoryStream(void *data, size_t data_size)
|
|
43 {
|
|
44 this->data_size = data_size;
|
|
45 this->data = data;
|
|
46 this->current_pos = 0;
|
|
47 }
|
|
48 inline MemoryStream()
|
|
49 {
|
|
50 this->data_size = 0;
|
|
51 this->data = nullptr;
|
|
52 this->current_pos = 0;
|
|
53 }
|
|
54
|
|
55 inline ~MemoryStream()
|
|
56 {
|
|
57 //Log::Warning(L"Destructor: data delete %u", data);
|
|
58 if (data)
|
|
59 delete [] data;
|
|
60 }
|
|
61
|
|
62 inline size_t Write(void *buffer, size_t num_bytes)
|
|
63 {
|
|
64 if (!data)
|
|
65 {
|
|
66 data_size = 32 + num_bytes;
|
|
67 data = new char[data_size];
|
|
68 //Log::Warning(L"new data %u", data);
|
|
69 current_pos = 0;
|
|
70 }
|
|
71 else if (current_pos + num_bytes >= data_size)
|
|
72 {
|
|
73 int new_data_size = data_size + num_bytes + data_size / 8 + 4;
|
|
74 auto new_data = new char[new_data_size];
|
|
75 //Log::Warning(L"new new_data %u", new_data);
|
|
76
|
|
77 memcpy(new_data, data, data_size);
|
|
78 //Log::Warning(L"data delete %u", data);
|
|
79 delete [] data;
|
|
80
|
|
81 data_size = new_data_size;
|
|
82 data = new_data;
|
|
83 }
|
|
84 memcpy((char *)data + current_pos, buffer, num_bytes);
|
|
85 current_pos += num_bytes;
|
|
86 return num_bytes;
|
|
87 }
|
|
88
|
|
89 inline size_t Read(void *buffer, size_t num_bytes)
|
|
90 {
|
|
91 size_t read_size = min(num_bytes, data_size - current_pos);
|
|
92 if (read_size)
|
|
93 {
|
|
94 memcpy(buffer, (char *)data + current_pos, read_size);
|
|
95 current_pos += read_size;
|
|
96 }
|
|
97 return read_size;
|
|
98 }
|
|
99
|
|
100 inline void Reset()
|
|
101 {
|
|
102 current_pos = 0;
|
|
103 }
|
|
104 inline void SeekToEnd()
|
|
105 {
|
|
106 current_pos = data_size;
|
|
107 }
|
|
108
|
|
109 inline size_t Unwind(size_t bytes)
|
|
110 {
|
|
111 if (bytes > current_pos)
|
|
112 current_pos = 0;
|
|
113 else
|
|
114 current_pos -= bytes;
|
|
115 return current_pos;
|
|
116 }
|
|
117
|
|
118 inline size_t Rewind(size_t bytes)
|
|
119 {
|
|
120 if (current_pos + bytes >= data_size)
|
|
121 current_pos = data_size;
|
|
122 else
|
|
123 current_pos += bytes;
|
|
124 return current_pos;
|
|
125 }
|
|
126
|
|
127 inline size_t Size() const {return data_size;}
|
|
128 inline size_t Current() const {return current_pos;}
|
|
129 inline void *Ptr() const {return data;}
|
|
130
|
|
131 private:
|
|
132 void *data;
|
|
133 size_t data_size;
|
|
134 size_t current_pos;
|
|
135 };
|
|
136
|
|
137 OpenALSoundProvider *provider = nullptr;
|
|
138
|
|
139 static int av_num_bytes_per_sample(AVSampleFormat sample_fmt)
|
|
140 {
|
|
141 switch (sample_fmt)
|
|
142 {
|
|
143 case AV_SAMPLE_FMT_U8:
|
|
144 case AV_SAMPLE_FMT_U8P:
|
|
145 return 1;
|
|
146
|
|
147 case AV_SAMPLE_FMT_S16:
|
|
148 case AV_SAMPLE_FMT_S16P:
|
|
149 return 2;
|
|
150
|
|
151 case AV_SAMPLE_FMT_S32:
|
|
152 case AV_SAMPLE_FMT_S32P:
|
|
153 case AV_SAMPLE_FMT_FLT:
|
|
154 case AV_SAMPLE_FMT_FLTP:
|
|
155 return 4;
|
|
156
|
|
157 case AV_SAMPLE_FMT_DBL:
|
|
158 case AV_SAMPLE_FMT_DBLP:
|
|
159 return 8;
|
|
160
|
|
161 default:
|
|
162 case AV_SAMPLE_FMT_NONE:
|
|
163 Error("Invalid av sample format: %u", sample_fmt);
|
|
164 }
|
|
165 return 0;
|
|
166 }
|
|
167
|
|
168 struct AVStreamWrapper
|
|
169 {
|
|
170 inline AVStreamWrapper()
|
|
171 {
|
|
172 this->type = AVMEDIA_TYPE_UNKNOWN;
|
|
173 this->stream_idx = -1;
|
|
174 this->stream = nullptr;
|
|
175 this->dec = nullptr;
|
|
176 this->dec_ctx = nullptr;
|
|
177 }
|
|
178
|
|
179 inline void Release()
|
|
180 {
|
|
181 type = AVMEDIA_TYPE_UNKNOWN;
|
|
182 stream_idx = -1;
|
|
183 stream = nullptr;
|
|
184 dec = nullptr;
|
|
185 if (dec_ctx)
|
|
186 {
|
|
187 // Close the codec
|
|
188 avcodec_close(dec_ctx);
|
|
189 Log::Warning(L"close decoder context file\n");
|
|
190 dec_ctx = nullptr;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 AVMediaType type;
|
|
195 int stream_idx;
|
|
196 AVStream *stream;
|
|
197 AVCodec *dec;
|
|
198 AVCodecContext *dec_ctx;
|
|
199 };
|
|
200
|
|
201 struct AVAudioStream: public AVStreamWrapper
|
|
202 {
|
|
203 inline AVAudioStream():AVStreamWrapper()
|
|
204 {
|
|
205 this->bytes_per_sample = 0;
|
|
206 this->bytes_per_second = 0;
|
|
207 }
|
|
208
|
|
209 int bytes_per_sample;
|
|
210 int bytes_per_second;
|
|
211 };
|
|
212
|
|
213 struct AVVideoStream: public AVStreamWrapper
|
|
214 {
|
|
215 inline AVVideoStream(): AVStreamWrapper()
|
|
216 {
|
|
217 this->frames_per_second = 0.0;
|
|
218 }
|
|
219
|
|
220 double frames_per_second;
|
|
221 };
|
|
222
|
|
223 static bool av_open_stream(AVFormatContext *format_ctx, AVMediaType type, AVStreamWrapper *out_stream)
|
|
224 {
|
|
225 int stream_idx = av_find_best_stream(format_ctx, type, -1, -1, nullptr, 0);
|
|
226 if (stream_idx >= 0)
|
|
227 {
|
|
228 auto stream = format_ctx->streams[stream_idx];
|
|
229 auto dec_ctx = stream->codec;
|
|
230
|
|
231 // Find the decoder for the video stream
|
|
232 auto dec = avcodec_find_decoder(dec_ctx->codec_id);
|
|
233 if (dec)
|
|
234 {
|
|
235 // Open codec
|
|
236 if (avcodec_open2(dec_ctx, dec, nullptr) >= 0)
|
|
237 {
|
|
238 out_stream->type = type;
|
|
239 out_stream->stream_idx = stream_idx;
|
|
240 out_stream->stream = stream;
|
|
241 out_stream->dec = dec;
|
|
242 out_stream->dec_ctx = dec_ctx;
|
|
243 return true;
|
|
244 }
|
|
245 fprintf(stderr, "ffmpeg: Could not open codec\n");
|
|
246 return false;
|
|
247 }
|
|
248 fprintf(stderr, "ffmpeg: Unable to open codec\n");
|
|
249 return false;
|
|
250 }
|
|
251 fprintf(stderr, "ffmpeg: Didn't find a stream\n");
|
|
252 return false;
|
|
253 }
|
|
254
|
|
255 static bool av_open_audio_stream(AVFormatContext *format_ctx, AVAudioStream *out_stream)
|
|
256 {
|
2504
|
257 if (av_open_stream(format_ctx, AVMEDIA_TYPE_AUDIO, out_stream))
|
|
258 //return Error("Audio stream not found"), false;
|
2502
|
259
|
|
260 // we support only 2-channel audio for now
|
|
261 //if (out_stream->dec_ctx->channels != 2)
|
|
262 //{
|
|
263 // out_stream->Release();
|
|
264 // return Error("Unsupported number of channels: %u", out_stream->dec_ctx->channels), false;
|
|
265 //}
|
2504
|
266 {
|
2502
|
267 out_stream->bytes_per_sample = av_num_bytes_per_sample(out_stream->dec_ctx->sample_fmt);
|
|
268 out_stream->bytes_per_second = out_stream->dec_ctx->channels * out_stream->dec_ctx->sample_rate * out_stream->bytes_per_sample;
|
2504
|
269 }
|
2502
|
270 return true;
|
|
271 }
|
|
272
|
|
273 static bool av_open_video_stream(AVFormatContext *format_ctx, AVVideoStream *out_stream)
|
|
274 {
|
|
275 if (!av_open_stream(format_ctx, AVMEDIA_TYPE_VIDEO, out_stream))
|
2524
|
276 {
|
|
277 Error("Video stream not found");
|
|
278 false;
|
|
279 }
|
2502
|
280
|
|
281 out_stream->frames_per_second = (double)out_stream->dec_ctx->time_base.den / (double)out_stream->dec_ctx->time_base.num;
|
|
282 return true;
|
|
283 }
|
|
284
|
|
285 void InterleaveAudioData(MemoryStream *stream, AVSampleFormat src_format, int num_channels, int num_samples, uint8_t **channels)
|
|
286 {
|
|
287 unsigned int bytes_per_sample;
|
|
288 switch (src_format)
|
|
289 {
|
|
290 case AV_SAMPLE_FMT_U8:
|
|
291 case AV_SAMPLE_FMT_U8P:
|
|
292 __debugbreak();
|
|
293
|
|
294 case AV_SAMPLE_FMT_S16:
|
|
295 bytes_per_sample = sizeof(__int16);
|
|
296 stream->Write(channels[0], num_channels * num_samples * bytes_per_sample);
|
|
297 break;
|
|
298
|
|
299 case AV_SAMPLE_FMT_S16P:
|
|
300 {
|
|
301 bytes_per_sample = sizeof(__int16);
|
|
302 for (int i = 0; i < num_samples; ++i)
|
|
303 for (int j = 0; j < num_channels; ++j)
|
|
304 stream->Write(channels[j] + i * bytes_per_sample, bytes_per_sample);
|
|
305 }
|
|
306 break;
|
|
307
|
|
308 case AV_SAMPLE_FMT_FLT:
|
|
309 {
|
|
310 SwrContext *converter = swr_alloc();
|
|
311 av_opt_set_int(converter, "in_channel_layout", av_get_default_channel_layout(2), 0);
|
|
312 //av_opt_set_int(converter, "in_sample_rate", sample_ra, 0);
|
|
313 av_opt_set_sample_fmt(converter, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
|
|
314
|
|
315 av_opt_set_int(converter, "out_channel_layout", av_get_default_channel_layout(2), 0);
|
|
316 //av_opt_set_int(converter, "out_sample_rate", dst_sample_rate, 0);
|
|
317 av_opt_set_sample_fmt(converter, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
|
|
318
|
|
319 if (swr_init(converter) < 0)
|
|
320 {
|
2540
|
321 Log::Warning(L"swr_init: failed");
|
|
322 //__debugbreak();
|
2502
|
323 swr_free(&converter);
|
|
324 return;
|
|
325 }
|
|
326
|
|
327 uint8_t **dst_channels;
|
|
328 int dst_linesize[8];
|
|
329 //int dst_nb_channels = av_get_channel_layout_nb_channels(dst_channel_layout);
|
|
330 if (av_samples_alloc_array_and_samples(&dst_channels, dst_linesize, 2, num_channels * num_samples, AV_SAMPLE_FMT_S16, 0) < 0)
|
|
331 {
|
2540
|
332 Log::Warning(L"av_samples_alloc_array_and_samples: failed");
|
|
333 //__debugbreak();
|
2502
|
334 swr_free(&converter);
|
|
335 return;
|
|
336 }
|
|
337
|
|
338 if (swr_convert(converter, dst_channels, num_channels * num_samples, (const uint8_t **)channels, num_channels * num_samples) >= 0) //Invalid partial memory access, Uninitialized memory access
|
|
339
|
2540
|
340 stream->Write(dst_channels[0], num_channels * num_samples * sizeof(__int16));
|
2502
|
341 else
|
2540
|
342 //__debugbreak();
|
|
343 Log::Warning(L"swr_convert: failed");
|
2502
|
344
|
|
345 av_free(dst_channels[0]);
|
|
346 swr_free(&converter);
|
|
347 }
|
|
348 break;
|
|
349
|
|
350 default:
|
|
351 __debugbreak();
|
|
352 //if (Resample(next_frame->avframe, next_frame->avframe->channel_layout, next_frame->avframe->sample_rate,
|
|
353 // av_get_default_channel_layout(2), next_frame->avframe->sample_rate, AV_SAMPLE_FMT_S16P, resampled_data))
|
|
354 }
|
|
355 }
|
|
356
|
|
357 const uint16_t ff_wma_critical_freqs[25] = {
|
|
358 100, 200, 300, 400, 510, 630, 770, 920,
|
|
359 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150,
|
|
360 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500,
|
|
361 24500,
|
|
362 };
|
|
363 extern const uint16_t ff_wma_critical_freqs[25];
|
|
364 static float quant_table[96];
|
|
365
|
|
366 bool DecodeAudioFrame(AVCodecContext *dec_ctx, AVPacket *avpacket, AVFrame *avframe, MemoryStream *out_audio_data, int *out_num_audio_samples)
|
|
367 {
|
|
368 volatile int decoded = false;
|
|
369 do
|
|
370 {
|
|
371 if (avcodec_decode_audio4(dec_ctx, avframe, (int *)&decoded, avpacket) < 0) //Uninitialized portail memory access
|
|
372 {
|
|
373 log("Cannot decode audio frame\n");
|
|
374 return false;
|
|
375 }
|
|
376
|
|
377 if (!decoded)
|
2508
|
378 {
|
2502
|
379 log("Cannot decode audio frame in one piece\n");
|
2508
|
380 break;
|
|
381 }
|
|
382
|
2502
|
383 } while (!decoded);
|
|
384
|
|
385 switch (dec_ctx->codec_id)
|
|
386 {
|
|
387 case AV_CODEC_ID_BINKAUDIO_DCT:
|
|
388 {
|
|
389 __debugbreak();
|
|
390 }
|
|
391 case AV_CODEC_ID_BINKAUDIO_RDFT:
|
|
392 {//pts samples dpts
|
|
393 // 0 960
|
|
394 //17280 960 17280 18x960
|
|
395 //18240 960 960 1x960
|
|
396 //20160 960 1920 2x960
|
|
397 //21120 960 960 1x960
|
|
398 //23040 960 1920 2x960
|
|
399 /*static int bink_next_pts = 0;
|
|
400
|
|
401 // there's a gap in the sound - fill empty samples in
|
|
402 if (bink_next_pts < avpacket->pts)
|
|
403 {
|
|
404 short silence[1024];
|
|
405 memset(silence, 0, sizeof(silence));
|
|
406
|
|
407 int samples_to_fill = /*dec_ctx->channels * (avpacket->pts - bink_next_pts);
|
|
408 while (samples_to_fill > 0)
|
|
409 {
|
|
410 int samples_to_fill_this_step = samples_to_fill >= 1024 ? 1024 : samples_to_fill;
|
|
411 out_audio_data->Write(silence, samples_to_fill_this_step * sizeof(short));
|
|
412
|
|
413 samples_to_fill -= samples_to_fill_this_step;
|
|
414 }
|
|
415 }
|
|
416
|
|
417 bink_next_pts = avpacket->pts + /*dec_ctx->channels * avframe->nb_samples; */
|
|
418
|
|
419 AVFrame frame;
|
|
420 int first;
|
|
421 int version_b;
|
|
422 int frame_len;
|
|
423 int overlap_len;
|
|
424 int block_size;
|
|
425 int num_bands;
|
|
426 unsigned int *bands;
|
|
427 float root;
|
|
428 int sample_rate = dec_ctx->sample_rate;
|
|
429 int sample_rate_half;
|
|
430 int i;
|
|
431 int frame_len_bits;
|
|
432 int channels;
|
|
433
|
|
434 //compresses audio in chunks of varying sizes depending on sample rate:
|
|
435 // if sample rate < 22050, frame size is 2048 samples
|
|
436 // if sample rate < 44100, frame size is 4096 samples
|
|
437 // else, frame size is 8192 samples
|
|
438
|
|
439
|
|
440 /* determine frame length */
|
|
441 if (dec_ctx->sample_rate < 22050)
|
|
442 frame_len_bits = 9;
|
|
443 else if (dec_ctx->sample_rate < 44100)
|
|
444 frame_len_bits = 10;
|
|
445 else
|
|
446 frame_len_bits = 11;
|
|
447
|
|
448 if (dec_ctx->channels < 1 || dec_ctx->channels > 2)
|
|
449 {
|
|
450 av_log(dec_ctx, AV_LOG_ERROR, "invalid number of channels: %d\n", dec_ctx->channels);
|
|
451 return AVERROR_INVALIDDATA;
|
|
452 }
|
|
453
|
|
454 version_b = dec_ctx->extradata_size >= 4 && dec_ctx->extradata[3] == 'b';
|
|
455 if (version_b)
|
|
456 __debugbreak();
|
|
457
|
|
458 if (dec_ctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
|
|
459 {
|
|
460 // audio is already interleaved for the RDFT format variant
|
|
461 dec_ctx->sample_fmt = AV_SAMPLE_FMT_FLT;
|
|
462 sample_rate *= dec_ctx->channels;
|
|
463 channels = 1;
|
|
464 if (!version_b)
|
|
465 frame_len_bits += av_log2(dec_ctx->channels);
|
|
466 }
|
|
467 else
|
|
468 {
|
|
469 channels = dec_ctx->channels;
|
|
470 dec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
|
|
471 }
|
|
472
|
|
473 frame_len = 1 << frame_len_bits; //2048
|
|
474
|
|
475 //a frame is windowed with the previous frame; the size of the window is frame size / 16
|
|
476 overlap_len = frame_len / 16; //128
|
|
477 block_size = (frame_len - overlap_len) * channels; //1920
|
|
478
|
|
479 //compute half the sample rate as (sample rate + 1) / 2;
|
|
480 //initialize an array of band frequencies corresponding to an array of 25 critical frequencies (same as WMA, apparently),
|
|
481 // any for which the critical frequencies are less than half the sample rate
|
|
482
|
|
483 sample_rate_half = (sample_rate + 1) / 2; //22050
|
|
484 if (dec_ctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
|
|
485 root = 2.0 / (sqrt(float(frame_len)) * 32768.0);
|
|
486 else
|
|
487 root = frame_len / (sqrt(float(frame_len)) * 32768.0);
|
|
488 for (i = 0; i < 96; i++)
|
|
489 {
|
|
490 /* constant is result of 0.066399999/log10(M_E) */
|
|
491 quant_table[i] = expf(i * 0.15289164787221953823f) * root;
|
|
492 }
|
|
493
|
|
494 /* calculate number of bands */
|
|
495 //bands calculation:
|
|
496 //bands[0] = 1;
|
|
497 //foreach (i in 1..# of bands-1):
|
|
498 //bands[i] = crit_freq[i-1] * (frame length / 2) / (sample rate / 2);
|
|
499 //bands[# of bands] = frame length / 2
|
|
500 for (num_bands = 1; num_bands < 25; num_bands++)
|
|
501 if (sample_rate_half <= ff_wma_critical_freqs[num_bands - 1])
|
|
502 break;
|
|
503
|
|
504 bands = (unsigned int *)(av_malloc((num_bands + 1) * sizeof(*bands)));
|
|
505 if (!bands)
|
|
506 return AVERROR(ENOMEM);
|
|
507
|
|
508 /* populate bands data */
|
|
509 bands[0] = 2;
|
|
510 for (i = 1; i < num_bands; i++)
|
|
511 bands[i] = (ff_wma_critical_freqs[i - 1] * frame_len / sample_rate_half) & ~1;
|
|
512 bands[num_bands] = frame_len;
|
|
513
|
|
514 first = 1;
|
|
515
|
|
516 //ff_rdft_init(&trans.rdft, frame_len_bits, DFT_C2R);
|
|
517
|
|
518 avcodec_get_frame_defaults(&frame);
|
|
519 dec_ctx->coded_frame = &frame;
|
|
520 }
|
|
521 break;
|
|
522 /*
|
|
523 case AV_CODEC_ID_SMACKAUDIO:
|
|
524 {
|
|
525 static int smack_debug_next_audio_time = 0;
|
|
526 if (smack_debug_next_audio_time != packet->pts)
|
|
527 {
|
|
528 Error("There's a gap in the sound before frame %u\n", num_audio_frames);
|
|
529 __debugbreak(); // there's a gap in the sound
|
|
530 }
|
|
531
|
|
532 int num_actual_data_channels = 0;
|
|
533 switch (dec_ctx->sample_fmt)
|
|
534 {
|
|
535 case AV_SAMPLE_FMT_U8:
|
|
536 case AV_SAMPLE_FMT_S16:
|
|
537 case AV_SAMPLE_FMT_S32:
|
|
538 case AV_SAMPLE_FMT_FLT:
|
|
539 case AV_SAMPLE_FMT_DBL:
|
|
540 num_actual_data_channels = 1;
|
|
541 break;
|
|
542
|
|
543 case AV_SAMPLE_FMT_U8P:
|
|
544 case AV_SAMPLE_FMT_S16P:
|
|
545 case AV_SAMPLE_FMT_S32P:
|
|
546 case AV_SAMPLE_FMT_FLTP:
|
|
547 case AV_SAMPLE_FMT_DBLP:
|
|
548 num_actual_data_channels = dec_ctx->channels;
|
|
549 break;
|
|
550
|
|
551 default:
|
|
552 case AV_SAMPLE_FMT_NONE:
|
|
553 case AV_SAMPLE_FMT_NB:
|
|
554 __debugbreak();
|
|
555 }
|
|
556
|
|
557 smack_debug_next_audio_time += dec_ctx->channels * frame->nb_samples * bytes_per_sample;
|
|
558 Assert(frame->avframe->linesize[0] == audio.dec_ctx->channels * frame->avframe->nb_samples * audio.bytes_per_sample / num_actual_data_channels,
|
|
559 "Smack audio size mismatch in frame %u in %s\n", audio_num_read_frames, movie_filename);
|
|
560
|
|
561 frame->play_time = (double)frame->avpacket->pts / (double)audio.bytes_per_second;
|
|
562 }
|
|
563 break;
|
|
564
|
|
565 case AV_CODEC_ID_MP3:
|
|
566 {
|
|
567 static int mp3_samples_decoded_so_far = 0;
|
|
568 static int mp3_prev_samples_count = frame->avframe->nb_samples; // mp3 seems to always feed same amount of samples
|
|
569 frame->play_time = (double)mp3_samples_decoded_so_far / (double)audio.dec_ctx->sample_rate;
|
|
570
|
|
571 mp3_samples_decoded_so_far += frame->avframe->nb_samples;
|
|
572 Assert(mp3_prev_samples_count == frame->avframe->nb_samples,
|
|
573 "MP3 audio have variable sample count in frame %u in %s\n", audio_num_read_frames, movie_filename);
|
|
574 }
|
|
575 break;
|
|
576
|
|
577 default:
|
|
578 {
|
|
579 __debugbreak();
|
|
580 double samples_per_second = (double)audio.dec_ctx->time_base.den / (double)audio.dec_ctx->time_base.num;
|
|
581 double play_length = frame->avframe->nb_samples / samples_per_second;
|
|
582 frame->play_time = (double)frame->avpacket->pts / samples_per_second;
|
|
583 }
|
|
584 break;*/
|
|
585 }
|
|
586
|
|
587 if (!avframe->channel_layout)
|
|
588 {
|
|
589 log("Audio channel layout not specified, rolling back to default\n");
|
|
590 avframe->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
|
|
591 }
|
|
592
|
|
593 *out_num_audio_samples = dec_ctx->channels * avframe->nb_samples;
|
|
594 InterleaveAudioData(out_audio_data, dec_ctx->sample_fmt,
|
|
595 dec_ctx->channels, avframe->nb_samples, avframe->data);
|
|
596 return true;
|
|
597 }
|
|
598
|
|
599 bool LoadAudioTrack(AVFormatContext *format_ctx, AVCodecContext *dec_ctx, int audio_stream_idx, MemoryStream *out_audio_stream, int *out_num_audio_frames, int *out_num_audio_samples)
|
|
600 {
|
|
601 out_audio_stream->Reset();
|
|
602
|
|
603 AVFrame *frame = avcodec_alloc_frame();
|
|
604
|
|
605 AVPacket *packet = new AVPacket;
|
|
606 av_init_packet(packet);
|
|
607
|
|
608 int num_audio_frames = 0;
|
|
609 int num_audio_samples = 0;
|
|
610
|
|
611 while (av_read_frame(format_ctx, packet) >= 0)
|
|
612 {
|
|
613 // Is this a packet from the audio stream?
|
|
614 if (packet->stream_index != audio_stream_idx)
|
|
615 {
|
|
616 //log("Suspicious stream id %u in %s", packet->stream_index, filenamea);
|
|
617 continue;
|
|
618 }
|
|
619
|
|
620 // Decode audio frame
|
|
621 int num_samples_decoded;
|
|
622 DecodeAudioFrame(dec_ctx, packet, frame, out_audio_stream, &num_samples_decoded);
|
|
623
|
|
624 num_audio_samples += num_samples_decoded;
|
|
625 num_audio_frames++;
|
|
626 }
|
|
627 *out_num_audio_frames = num_audio_frames;
|
|
628 *out_num_audio_samples = num_audio_samples;
|
|
629
|
|
630 avcodec_free_frame(&frame);
|
|
631 delete frame;
|
|
632 av_free_packet(packet);
|
|
633 delete packet;
|
|
634
|
|
635 return true;
|
|
636 }
|
|
637
|
|
638 class Track: public Media::ITrack
|
|
639 {
|
|
640 public:
|
|
641 inline Track()
|
|
642 {
|
|
643 this->format_ctx = nullptr;
|
|
644 this->audio_num_samples = 0;
|
|
645 }
|
|
646
|
|
647 void Release()
|
|
648 {
|
|
649 ReleaseAvcodec();
|
|
650 }
|
|
651
|
|
652 void ReleaseAvcodec()
|
|
653 {
|
|
654 audio.Release();
|
|
655 if (format_ctx)
|
|
656 {
|
|
657 av_close_input_file(format_ctx);
|
|
658 Log::Warning(L"close audio format context file\n");
|
|
659 format_ctx = nullptr;
|
|
660 }
|
|
661 }
|
|
662
|
|
663 bool LoadAudio(const wchar_t *filename)
|
|
664 {
|
|
665 char filenamea[1024];
|
|
666 sprintf(filenamea, "%S", filename);
|
|
667 // Open audio file
|
|
668 if (avformat_open_input(&format_ctx, filenamea, nullptr, nullptr) >= 0)
|
|
669 {
|
|
670 // Retrieve stream information
|
|
671 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
|
|
672 {
|
|
673 // Dump information about file onto standard error
|
|
674 av_dump_format(format_ctx, 0, filenamea, 0);
|
|
675
|
|
676 if (!av_open_audio_stream(format_ctx, &audio))
|
|
677 {
|
|
678 Error("Cannot open strack: %s", filenamea);
|
|
679 return Release(), false;
|
|
680 }
|
|
681
|
|
682 MemoryStream audio_plain_data;
|
|
683 int num_audio_frames;
|
|
684 int num_audio_samples;
|
|
685
|
|
686 if (LoadAudioTrack(format_ctx, audio.dec_ctx, audio.stream_idx, &audio_plain_data, &num_audio_frames, &num_audio_samples))
|
|
687 {
|
|
688 /*#ifdef _DEBUG
|
|
689 char debug_filename[1024];
|
|
690 sprintf(debug_filename, "%s.wav", filenamea);
|
|
691 FILE *wav = fopen(debug_filename, "w+b");
|
|
692
|
|
693 extern void write_wav_header(FILE *wav, int channel_count = 2, int sample_rate = 22050, int bytes_per_sample = 2);
|
|
694 write_wav_header(wav, audio.dec_ctx->channels, audio.dec_ctx->sample_rate, audio.bytes_per_sample);
|
|
695
|
|
696 fwrite(audio_plain_data.Ptr(), audio_plain_data.Current(), 1, wav);
|
|
697
|
|
698 extern void fix_wav_header(FILE *wav, int wav_bytes_in_stream);
|
|
699 fix_wav_header(wav, audio_plain_data.Current());
|
|
700 #endif*/
|
|
701
|
|
702 device_buffer = provider->CreateTrack16(audio.dec_ctx->channels, audio.dec_ctx->sample_rate, 2, num_audio_samples, audio_plain_data.Ptr());
|
|
703
|
|
704 Release();
|
|
705 return true;
|
|
706 }
|
|
707 }
|
|
708 Release();
|
|
709 fprintf(stderr, "ffmpeg: Unable to find stream info\n");
|
|
710 return false;
|
|
711 }
|
|
712 fprintf(stderr, "ffmpeg: Unable to open input file\n");
|
|
713 return false;
|
|
714 }
|
|
715
|
|
716 virtual void Play(bool loop)
|
|
717 {
|
|
718 provider->PlayTrack16(device_buffer, loop);
|
|
719 mSourceID = device_buffer->source_id;
|
|
720 }
|
|
721
|
|
722 protected:
|
|
723 AVFormatContext *format_ctx;
|
|
724 AVAudioStream audio;
|
|
725 int audio_num_samples;
|
|
726
|
|
727 bool stopped;
|
|
728
|
|
729 OpenALSoundProvider::TrackBuffer *device_buffer;
|
|
730 };
|
|
731
|
|
732 class Movie: public Media::IMovie
|
|
733 {
|
|
734 public:
|
|
735 inline Movie()
|
|
736 {
|
|
737 this->movie_filename[0] = 0;
|
|
738 this->width = 0;
|
|
739 this->height = 0;
|
|
740 this->format_ctx = nullptr;
|
|
741 this->end_of_file = false;
|
|
742 this->playback_time = 0.0;
|
|
743
|
|
744 this->num_audio_frames = 0;
|
|
745 this->num_audio_samples = 0;
|
|
746
|
|
747 this->last_resampled_frame_num = -1;
|
|
748 memset(last_resampled_frame_data, 0, sizeof(last_resampled_frame_data));
|
|
749 memset(last_resampled_frame_linesize, 0, sizeof(last_resampled_frame_linesize));
|
|
750
|
|
751 audio_data_in_device = nullptr;
|
|
752 decoding_packet = nullptr;
|
|
753 ioBuffer = nullptr;
|
|
754 format_ctx = nullptr;
|
|
755 avioContext = nullptr;
|
|
756 }
|
|
757
|
|
758 virtual ~Movie() {}
|
|
759
|
|
760 virtual void Release()
|
|
761 {
|
|
762 ReleaseAVCodec();
|
|
763
|
|
764 if (audio_data_in_device)
|
|
765 provider->DeleteStreamingTrack(&audio_data_in_device);
|
|
766 }
|
|
767
|
|
768 inline void ReleaseAVCodec()
|
|
769 {
|
|
770 audio.Release();
|
|
771 video.Release();
|
|
772
|
|
773 if (format_ctx)
|
|
774 {
|
|
775 // Close the video file
|
|
776 av_close_input_file(format_ctx);
|
|
777 Log::Warning(L"close video format context file\n");
|
|
778 format_ctx = nullptr;
|
|
779 }
|
|
780 if(avioContext)
|
|
781 {
|
|
782 av_free(avioContext);
|
|
783 avioContext = nullptr;
|
|
784 }
|
|
785 if (ioBuffer)
|
|
786 {
|
|
787 //av_free(ioBuffer);
|
|
788 ioBuffer = nullptr;
|
|
789 }
|
|
790 av_free_packet(decoding_packet);
|
|
791 delete decoding_packet;
|
|
792 avcodec_free_frame(&decoding_frame);
|
|
793 delete decoding_frame;
|
|
794 if (last_resampled_frame_data[0])
|
|
795 av_freep(&last_resampled_frame_data[0]);
|
|
796
|
|
797 }
|
|
798
|
|
799 bool Load(const wchar_t *filename, int dst_width, int dst_height, int cache_ms) //
|
|
800 {
|
|
801 char filenamea[1024];
|
|
802 sprintf(filenamea, "%S", filename);
|
|
803 sprintf(movie_filename, "%S", filename);
|
|
804
|
|
805 width = dst_width;
|
|
806 height = dst_height;
|
|
807 // Open video file
|
|
808 if (avformat_open_input(&format_ctx, filenamea, nullptr, nullptr) >= 0)
|
|
809 {
|
|
810 // Retrieve stream information
|
|
811 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
|
|
812 {
|
|
813 // Dump information about file onto standard error
|
|
814 av_dump_format(format_ctx, 0, filenamea, 0);
|
|
815
|
2504
|
816 /*if (!av_open_audio_stream(format_ctx, &audio))
|
2502
|
817 {
|
|
818 Error("Cannot open audio stream: %s", filenamea);
|
|
819 return Release(), false;
|
2504
|
820 }*/
|
|
821 av_open_audio_stream(format_ctx, &audio);
|
2502
|
822
|
|
823 if (!av_open_video_stream(format_ctx, &video))
|
|
824 {
|
|
825 Error("Cannot open video stream: %s", filenamea);
|
|
826 return Release(), false;
|
|
827 }
|
|
828
|
|
829 //Ritor1: include
|
|
830 if (_stricmp("binkvideo", video.dec->name) )
|
|
831 {
|
|
832 pMediaPlayer->current_movie_width = video.dec_ctx->width;
|
|
833 pMediaPlayer->current_movie_height = video.dec_ctx->height;
|
|
834 }
|
|
835 else
|
|
836 {
|
|
837 pMediaPlayer->current_movie_width = width;
|
|
838 pMediaPlayer->current_movie_height = height;
|
|
839 }
|
|
840 //
|
|
841 decoding_packet = new AVPacket;
|
|
842 av_init_packet(decoding_packet);
|
|
843
|
|
844 // Allocate video frame
|
|
845 decoding_frame = avcodec_alloc_frame();
|
|
846
|
2504
|
847 if (audio.stream_idx >= 0)
|
|
848 audio_data_in_device = provider->CreateStreamingTrack16(audio.dec_ctx->channels, audio.dec_ctx->sample_rate, 2);
|
2502
|
849 return true;
|
|
850 }
|
|
851 fprintf(stderr, "ffmpeg: Unable to find stream info\n");
|
|
852 return Release(), false;
|
|
853 }
|
|
854 fprintf(stderr, "ffmpeg: Unable to open input file\n");
|
|
855 return Release(), false;
|
|
856 }
|
|
857
|
|
858 bool LoadFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
|
|
859 {
|
|
860 if (!ioBuffer)
|
|
861 ioBuffer = (unsigned char *)av_malloc(0x4000 + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
|
|
862 if (!avioContext)
|
|
863 avioContext = avio_alloc_context(ioBuffer, 0x4000, 0, h, readFunction, NULL, seekFunction);
|
|
864 if (!format_ctx)
|
|
865 format_ctx = avformat_alloc_context();
|
|
866 format_ctx->pb = avioContext;
|
|
867 return Load(L"dummyFilename", width, height, 0);
|
|
868 }
|
|
869
|
|
870 virtual void GetNextFrame(double dt, void *dst_surface)
|
|
871 {
|
|
872 playback_time += dt;
|
|
873
|
|
874 AVPacket *avpacket = decoding_packet;
|
|
875 AVFrame *avframe = decoding_frame;
|
|
876
|
|
877 avcodec_get_frame_defaults(avframe);
|
|
878
|
|
879 int desired_frame_number = floor(playback_time * video.dec_ctx->time_base.den / video.dec_ctx->time_base.num + 0.5);
|
|
880 if (last_resampled_frame_num == desired_frame_number)
|
|
881 {
|
|
882 memcpy(dst_surface, last_resampled_frame_data[0], pMediaPlayer->current_movie_height * last_resampled_frame_linesize[0]);
|
|
883 return;
|
|
884 }
|
|
885
|
|
886 volatile int frameFinished = false;
|
|
887
|
|
888 // keep reading packets until we hit the end or find a video packet
|
|
889 do
|
|
890 {
|
|
891 if (pMediaPlayer->loop_current_file)
|
|
892 {
|
|
893 //Now seek back to the beginning of the stream
|
|
894 if (video.dec_ctx->frame_number >= video.stream->duration - 1 )
|
|
895 pMediaPlayer->bPlaying_Movie = false;
|
|
896 }
|
|
897 if (av_read_frame(format_ctx, avpacket) < 0)
|
|
898 {
|
|
899 // probably movie is finished
|
|
900 pMediaPlayer->bPlaying_Movie = false;
|
|
901 av_free_packet(avpacket);
|
|
902 return;
|
|
903 }
|
|
904 // Is this a packet from the video stream?
|
|
905 // audio packet - queue into playing
|
|
906 if (avpacket->stream_index == audio.stream_idx)
|
|
907 {
|
|
908 MemoryStream audio_data;
|
|
909 if (DecodeAudioFrame(audio.dec_ctx, avpacket, avframe, &audio_data, &num_audio_samples))
|
|
910 provider->Stream16(audio_data_in_device, num_audio_samples, audio_data.Ptr());
|
|
911 //continue;
|
|
912 }
|
|
913
|
|
914 // Decode video frame
|
|
915 // video packet - decode & maybe show
|
|
916 else if (avpacket->stream_index == video.stream_idx)
|
|
917 {
|
|
918 do
|
|
919 {
|
|
920 if (avcodec_decode_video2(video.dec_ctx, avframe, (int *)&frameFinished, avpacket) < 0)
|
|
921 __debugbreak();
|
|
922 } while (!frameFinished);
|
|
923 }
|
|
924 else __debugbreak(); // unknown stream
|
|
925 }
|
|
926 while (avpacket->stream_index != video.stream_idx ||
|
|
927 avpacket->pts != desired_frame_number);
|
|
928
|
|
929 if (frameFinished)
|
|
930 {
|
|
931 if (last_resampled_frame_data[0])
|
|
932 av_freep(&last_resampled_frame_data[0]);
|
|
933
|
|
934 AVPixelFormat rescaled_format = AV_PIX_FMT_RGB32;
|
|
935 uint8_t *rescaled_data[4] = {nullptr, nullptr, nullptr, nullptr};
|
|
936 int rescaled_linesize[4] = {0, 0, 0, 0};
|
|
937
|
|
938 if (av_image_alloc(rescaled_data, rescaled_linesize, pMediaPlayer->current_movie_width, pMediaPlayer->current_movie_height, rescaled_format, 1) >= 0)
|
|
939 {
|
|
940 SwsContext *converter = sws_getContext(avframe->width, avframe->height, (AVPixelFormat)avframe->format,
|
|
941 pMediaPlayer->current_movie_width, pMediaPlayer->current_movie_height, rescaled_format,
|
|
942 SWS_BICUBIC, nullptr, nullptr, nullptr);
|
|
943 sws_scale(converter, avframe->data, avframe->linesize, 0, avframe->height, rescaled_data, rescaled_linesize);
|
|
944 sws_freeContext(converter);
|
|
945
|
|
946 memcpy(dst_surface, rescaled_data[0], pMediaPlayer->current_movie_height * rescaled_linesize[0]);
|
|
947
|
|
948 last_resampled_frame_num = desired_frame_number;
|
|
949 memcpy(last_resampled_frame_data, rescaled_data, sizeof(rescaled_data));
|
|
950 memcpy(last_resampled_frame_linesize, rescaled_linesize, sizeof(rescaled_linesize));
|
|
951 }
|
|
952 }
|
|
953 else
|
|
954 memset(dst_surface, 0, width * pMediaPlayer->current_movie_height * 4);
|
|
955
|
|
956 // Free the packet that was allocated by av_read_frame
|
|
957 av_free_packet(avpacket);
|
|
958 }
|
|
959
|
|
960 virtual void Play()
|
|
961 {
|
|
962 }
|
|
963
|
|
964 protected:
|
|
965 char movie_filename[256];
|
|
966 int width;
|
|
967 int height;
|
|
968 bool stopped;
|
|
969 AVFormatContext *format_ctx;
|
|
970 double playback_time;
|
|
971 bool end_of_file;
|
|
972
|
|
973 AVPacket *decoding_packet;
|
|
974 AVFrame *decoding_frame;
|
|
975
|
|
976 AVAudioStream audio;
|
|
977 int num_audio_frames;
|
|
978 int num_audio_samples;
|
|
979 unsigned char *ioBuffer;
|
|
980 AVIOContext *avioContext;
|
|
981 OpenALSoundProvider::StreamingTrackBuffer *audio_data_in_device;
|
|
982
|
|
983 AVVideoStream video;
|
|
984 int last_resampled_frame_num;
|
|
985 uint8_t *last_resampled_frame_data[4];
|
|
986 int last_resampled_frame_linesize[4];
|
|
987 };
|
|
988
|
|
989 ITrack *MPlayer::LoadTrack(const wchar_t *filename)
|
|
990 {
|
|
991 auto audio_track = new Track;
|
|
992 Log::Warning(L"allocation dynamic memory for audio_track\n");
|
|
993 if (!audio_track->LoadAudio(filename))
|
|
994 {
|
|
995 delete audio_track;
|
|
996 Log::Warning(L"delete dynamic memory for audio_track\n");
|
|
997 audio_track = nullptr;
|
|
998 }
|
|
999 return audio_track;
|
|
1000 }
|
|
1001
|
|
1002 IMovie *MPlayer::LoadMovie(const wchar_t *filename, int width, int height, int cache_ms) //
|
|
1003 {
|
|
1004 movie = new Movie;
|
|
1005 Log::Warning(L"allocation dynamic memory for movie\n");
|
|
1006 if (!movie->Load(filename, width, height, cache_ms))
|
|
1007 {
|
|
1008 delete movie;
|
|
1009 Log::Warning(L"delete dynamic memory for movie\n");
|
|
1010 movie = nullptr;
|
|
1011 }
|
|
1012 return movie;
|
|
1013 }
|
|
1014
|
|
1015
|
|
1016 //for video/////////////////////////////////////////////////////////////////
|
|
1017
|
|
1018 //----- (004BE9D8) --------------------------------------------------------
|
|
1019 void MPlayer::Initialize(OSWindow *target_window)
|
|
1020 {
|
2519
|
1021 window = target_window;
|
|
1022
|
|
1023 if (bNoVideo)
|
|
1024 return;
|
|
1025
|
2502
|
1026 DWORD NumberOfBytesRead; // [sp+10h] [bp-4h]@9
|
|
1027
|
|
1028
|
|
1029 unsigned int uBinkVersionMajor = -1,
|
|
1030 uBinkVersionMinor = -1;
|
|
1031 //GetDllVersion(L"BINKW32.DLL", &uBinkVersionMajor, &uBinkVersionMinor);
|
|
1032 //uBinkVersion = (unsigned __int64)uBinkVersionMajor << 32 | uBinkVersionMinor;
|
|
1033
|
|
1034 strcpy(pTmpBuf.data(), "anims\\might7.vid");
|
|
1035 hMightVid = CreateFileW(L"anims\\might7.vid", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0x8000080, 0);
|
|
1036 if ( hMightVid == INVALID_HANDLE_VALUE )
|
|
1037 {
|
|
1038 sprintf(pTmpBuf2.data(), "Can't open file - anims\\%s.smk", pTmpBuf.data());
|
|
1039 MessageBoxA(0, pTmpBuf2.data(), "Video File Error", 0);
|
|
1040 return;
|
|
1041 }
|
|
1042 strcpy(pTmpBuf.data(), "anims\\magic7.vid");
|
|
1043 hMagicVid = CreateFileW(L"anims\\magic7.vid", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0x8000080, 0);
|
|
1044 if ( hMagicVid == INVALID_HANDLE_VALUE )
|
|
1045 {
|
|
1046 if ( !bCanLoadFromCD )
|
|
1047 {
|
|
1048 sprintf(pTmpBuf2.data(), "Can't open file - anims\\%s.smk", pTmpBuf.data());
|
|
1049 MessageBoxA(0, pTmpBuf2.data(), "Video File Error", 0);
|
|
1050 return;
|
|
1051 }
|
|
1052 sprintf(pTmpBuf2.data(), "%c:\\%s", (unsigned __int8)cMM7GameCDDriveLetter, pTmpBuf.data());
|
|
1053 hMagicVid = CreateFileA(pTmpBuf2.data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0x8000080, 0);
|
|
1054 if ( hMagicVid == (HANDLE)INVALID_HANDLE_VALUE )
|
|
1055 {
|
|
1056 sprintf(pTmpBuf2.data(), "Can't open file - %s", pTmpBuf.data());
|
|
1057 MessageBoxA(0, pTmpBuf2.data(), "Video File Error", 0);
|
|
1058 return;
|
|
1059 }
|
|
1060 }
|
|
1061 ReadFile(hMightVid, &uNumMightVideoHeaders, 4, &NumberOfBytesRead, 0);
|
|
1062 ReadFile(hMagicVid, &uNumMagicVideoHeaders, 4, &NumberOfBytesRead, 0);
|
|
1063 pMightVideoHeaders = (MovieHeader *)malloc(sizeof(MovieHeader) * uNumMightVideoHeaders + 2);
|
|
1064 pMagicVideoHeaders = (MovieHeader *)malloc(sizeof(MovieHeader) * uNumMagicVideoHeaders + 2);
|
|
1065 ReadFile(hMightVid, pMightVideoHeaders, 44 * uNumMightVideoHeaders, &NumberOfBytesRead, 0);
|
|
1066 ReadFile(hMagicVid, pMagicVideoHeaders, 44 * uNumMagicVideoHeaders, &NumberOfBytesRead, 0);
|
|
1067 }
|
|
1068
|
|
1069 //----- (004BF411) --------------------------------------------------------
|
|
1070 void MPlayer::OpenFullscreenMovie(const char *pFilename, unsigned int bLoop/*, int ScreenSizeFlag*/)
|
|
1071 {
|
|
1072 if (!this->bPlaying_Movie)
|
|
1073 {
|
|
1074 pEventTimer->Pause();
|
2510
|
1075 if ( use_music_folder )
|
|
1076 alSourcePause(mSourceID);
|
|
1077 else if (pAudioPlayer->hAILRedbook)
|
|
1078 AIL_redbook_pause(pAudioPlayer->hAILRedbook);
|
2502
|
1079
|
|
1080 bStopBeforeSchedule = false;
|
|
1081 bFirstFrame = false;
|
|
1082 this->bLoopPlaying = bLoop;
|
|
1083 LoadMovie(pFilename);
|
|
1084 return;
|
|
1085 }
|
|
1086 }
|
|
1087
|
|
1088 //----- (004BF28F) --------------------------------------------------------
|
|
1089 void MPlayer::OpenHouseMovie(const char *pMovieName, unsigned int a3_1)
|
|
1090 {
|
|
1091 if (!this->bPlaying_Movie)
|
|
1092 {
|
|
1093 //Prepare();
|
|
1094 pEventTimer->Pause();
|
2510
|
1095 if ( use_music_folder )
|
|
1096 alSourcePause(mSourceID);
|
|
1097 else if (pAudioPlayer->hAILRedbook)
|
2502
|
1098 AIL_redbook_pause(pAudioPlayer->hAILRedbook);
|
|
1099
|
|
1100 bStopBeforeSchedule = false;
|
|
1101 bFirstFrame = false;
|
|
1102
|
|
1103 this->bLoopPlaying = a3_1;
|
|
1104 /*if ( LOBYTE(this->field_104) == 1 )
|
|
1105 {
|
|
1106 MessageBoxA(nullptr, "Unsupported Bink playback!", "E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Video.cpp:925", 0);
|
|
1107 return;
|
|
1108 }*/
|
|
1109
|
|
1110 LoadMovie(pMovieName);
|
|
1111 time_video_begin = GetTickCount();
|
|
1112 }
|
|
1113 }
|
|
1114
|
|
1115 //----- (004BE70E) --------------------------------------------------------
|
|
1116 void MPlayer::FullscreenMovieLoop(const char *pMovieName, int a2/*, int ScreenSizeFlag, int a4*/)
|
|
1117 {
|
|
1118 int v4; // ebp@1
|
|
1119 MSG Msg; // [sp+Ch] [bp-1Ch]@12
|
|
1120
|
|
1121 v4 = a2;
|
|
1122 if ( dword_6BE364_game_settings_1 & (GAME_SETTINGS_NO_HOUSE_ANIM | GAME_SETTINGS_NO_INTRO) ||
|
|
1123 bNoVideo)
|
|
1124 return;
|
|
1125
|
|
1126 if ( a2 == 2 )
|
|
1127 v4 = 0;
|
|
1128 ShowCursor(0);
|
|
1129 OpenFullscreenMovie(pMovieName, 0);
|
|
1130 bPlaying_Movie = 1;
|
|
1131 field_44 = v4;
|
|
1132 pRenderer->ClearTarget(0);
|
2541
|
1133 current_screen_type = SCREEN_VIDEO;
|
2502
|
1134
|
|
1135 auto hwnd = pMediaPlayer->window->GetApiHandle();
|
|
1136
|
|
1137 RECT rc_client;
|
|
1138 GetClientRect(hwnd, &rc_client);
|
|
1139 int client_width = rc_client.right - rc_client.left,
|
|
1140 client_height = rc_client.bottom - rc_client.top;
|
|
1141
|
|
1142 HDC dc = GetDC(hwnd);
|
|
1143 HDC back_dc = CreateCompatibleDC(dc);
|
|
1144 HBITMAP back_bmp = CreateCompatibleBitmap(dc, client_width, client_height);
|
|
1145 auto frame_buffer = new char[client_width * client_height * 4];
|
|
1146 SelectObject(back_dc, back_bmp);
|
|
1147
|
|
1148 DWORD t = GetTickCount();
|
|
1149
|
|
1150 bPlaying_Movie = true;
|
|
1151
|
|
1152 while (true)
|
|
1153 {
|
|
1154 if (pMediaPlayer->bStopBeforeSchedule)
|
|
1155 break;
|
|
1156 while (PeekMessageA(&Msg, hwnd, 0, 0, PM_REMOVE))
|
|
1157 {
|
|
1158 if (Msg.message == WM_QUIT)
|
2541
|
1159 Engine_DeinitializeAndTerminate(0);
|
2502
|
1160 if (Msg.message == WM_PAINT)
|
|
1161 break;
|
|
1162 TranslateMessage(&Msg);
|
|
1163 DispatchMessageA(&Msg);
|
|
1164 }
|
|
1165
|
|
1166 double dt = (GetTickCount() - t) / 1000.0;
|
|
1167 t = GetTickCount();
|
|
1168
|
|
1169 pMovie_Track->GetNextFrame(dt, frame_buffer);
|
|
1170
|
|
1171 if (!bPlaying_Movie)
|
|
1172 break;
|
|
1173
|
|
1174 if (frame_buffer)
|
|
1175 {
|
|
1176 // draw to hwnd
|
|
1177 BITMAPINFO bmi;
|
|
1178 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
1179 bmi.bmiHeader.biWidth = client_width;
|
|
1180 bmi.bmiHeader.biHeight = -client_height;
|
|
1181 bmi.bmiHeader.biPlanes = 1;
|
|
1182 bmi.bmiHeader.biBitCount = 32;
|
|
1183 bmi.bmiHeader.biCompression = BI_RGB;
|
|
1184 bmi.bmiHeader.biSizeImage = 0;
|
|
1185 bmi.bmiHeader.biXPelsPerMeter = 0;
|
|
1186 bmi.bmiHeader.biYPelsPerMeter = 0;
|
|
1187 bmi.bmiHeader.biClrUsed = 0;
|
|
1188 bmi.bmiHeader.biClrImportant = 0;
|
|
1189 GetDIBits(back_dc, back_bmp, 0, client_height, 0, &bmi, DIB_RGB_COLORS);
|
|
1190 SetDIBits(back_dc, back_bmp, 0, client_height, frame_buffer, &bmi, DIB_RGB_COLORS);
|
|
1191 BitBlt(dc, 0, 0, client_width, client_height, back_dc, 0, 0, SRCCOPY);
|
|
1192 }
|
|
1193
|
2541
|
1194 MainMenu_EventLoop();
|
2502
|
1195
|
|
1196 if (pMediaPlayer->bStopBeforeSchedule == 1)
|
|
1197 Sleep(1000);
|
|
1198 }
|
|
1199 delete [] frame_buffer;
|
|
1200 DeleteObject(back_bmp);
|
|
1201 DeleteObject(back_dc);
|
|
1202 ReleaseDC(hwnd, dc);
|
|
1203
|
|
1204 pMediaPlayer->Unload();
|
|
1205
|
|
1206 //if (a4 == 1)
|
2541
|
1207 current_screen_type = SCREEN_GAME;
|
2502
|
1208
|
|
1209 pMediaPlayer->bPlaying_Movie = false;
|
|
1210
|
|
1211 ShowCursor(1);
|
|
1212
|
2541
|
1213 /*if ( current_screen_type == SCREEN_VIDEO )
|
|
1214 current_screen_type = SCREEN_GAME;*/
|
2502
|
1215 }
|
|
1216
|
|
1217 void MPlayer::HouseMovieLoop()
|
|
1218 {
|
|
1219 if (pMovie_Track && !bNoVideo)
|
|
1220 {
|
|
1221 pRenderer->BeginScene();
|
|
1222 pMouse->DrawCursorToTarget();
|
|
1223
|
|
1224 Log::Warning(L"smacker");
|
|
1225 loop_current_file = true;
|
|
1226 pRenderer->BeginScene();
|
|
1227 if (!bPlaying_Movie)//reload
|
|
1228 {
|
|
1229 unsigned int width = game_viewport_width;
|
|
1230 unsigned int height = game_viewport_height;
|
|
1231 MovieRelease();
|
|
1232
|
|
1233 SetFilePointer(hVidFile, uOffset, nullptr, FILE_BEGIN);
|
|
1234 pMovie_Track = nullptr;
|
|
1235 Log::Warning(L"reload pMovie_Track");
|
|
1236 pMovie_Track = pMediaPlayer->LoadMovieFromLOD(hVidFile, &readFunction, &seekFunction, width, height);
|
|
1237 bPlaying_Movie = true;
|
|
1238 }
|
|
1239 //else
|
|
1240 //{
|
|
1241 double dt = (GetTickCount() - time_video_begin) / 1000.0;
|
|
1242 //dt = 1.0/15.0;
|
|
1243 time_video_begin = GetTickCount();
|
|
1244
|
|
1245 //log("dt=%.5f\n", dt);
|
|
1246
|
|
1247 auto image = new char[current_movie_width * current_movie_height * 4];
|
|
1248
|
|
1249 pMovie_Track->GetNextFrame(dt, image);
|
|
1250
|
|
1251 int image_array[460 * 344];//game_viewport_width * game_viewport_height
|
|
1252 if (image)
|
|
1253 {
|
|
1254 memcpy(image_array, image, sizeof (image_array));
|
|
1255 for (unsigned int y = 8; y < 8 + game_viewport_height; ++y)//
|
|
1256 {
|
|
1257 for (unsigned int x = 8; x < 8 + game_viewport_width; ++x)
|
|
1258 {
|
|
1259 auto p = (unsigned __int32 *)pRenderer->pTargetSurface + x + y * pRenderer->uTargetSurfacePitch;
|
|
1260 *p = image_array[((x - 8) + ((y - 8)*game_viewport_width))];
|
|
1261 }
|
|
1262 }
|
|
1263 delete[] image;
|
|
1264 }
|
|
1265 //}
|
|
1266 pRenderer->EndScene();
|
|
1267 pMouse->ReadCursorWithItem();
|
|
1268 pRenderer->EndScene();
|
|
1269 }
|
|
1270 }
|
|
1271
|
|
1272 //----- (004BF73A) --------------------------------------------------------
|
|
1273 void MPlayer::SelectMovieType()
|
|
1274 {
|
|
1275 char Source[32]; // [sp+Ch] [bp-40h]@1
|
|
1276
|
|
1277 strcpy(Source, this->pCurrentMovieName);
|
|
1278 pMediaPlayer->Unload();
|
|
1279 if ( this->uMovieType == 1 )
|
|
1280 OpenHouseMovie(Source, LOBYTE(this->bLoopPlaying));
|
|
1281 else if ( this->uMovieType == 2 )
|
|
1282 OpenFullscreenMovie(Source, LOBYTE(this->bLoopPlaying));
|
|
1283 else
|
|
1284 __debugbreak();
|
|
1285 }
|
|
1286
|
|
1287 void MPlayer::LoadMovie(const char *pFilename)
|
|
1288 {
|
|
1289 char pVideoNameBik[120]; // [sp+Ch] [bp-28h]@2
|
|
1290 char pVideoNameSmk[120]; // [sp+Ch] [bp-28h]@2
|
|
1291
|
|
1292 sprintf(pVideoNameBik, "%s.bik", pFilename);
|
|
1293 sprintf(pVideoNameSmk, "%s.smk", pFilename);
|
|
1294 for (uint i = 0; i < uNumMightVideoHeaders; ++i)
|
|
1295 {
|
|
1296 if (!_stricmp(pVideoNameSmk, pMightVideoHeaders[i].pVideoName))
|
|
1297 {
|
|
1298 hVidFile = hMightVid;
|
|
1299 uOffset = pMightVideoHeaders[i].uFileOffset;
|
|
1300 uSize = pMightVideoHeaders[i + 1].uFileOffset - uOffset;
|
|
1301 this->uMovieType = 2;
|
|
1302 }
|
|
1303 }
|
|
1304 for (uint i = 0; i < uNumMagicVideoHeaders; ++i)
|
|
1305 {
|
|
1306 if (!_stricmp(pVideoNameBik, pMagicVideoHeaders[i].pVideoName))
|
|
1307 {
|
|
1308 hVidFile = hMagicVid;
|
|
1309 uOffset = pMagicVideoHeaders[i].uFileOffset;
|
|
1310 uSize = pMagicVideoHeaders[i + 1].uFileOffset - uOffset;
|
|
1311 this->uMovieType = 1;
|
|
1312 }
|
|
1313 if (!_stricmp(pVideoNameSmk, pMagicVideoHeaders[i].pVideoName))
|
|
1314 {
|
|
1315 hVidFile = hMagicVid;
|
|
1316 uOffset = pMagicVideoHeaders[i].uFileOffset;
|
|
1317 uSize = pMagicVideoHeaders[i + 1].uFileOffset - uOffset;
|
|
1318 this->uMovieType = 2;
|
|
1319 }
|
|
1320 }
|
|
1321 if (!hVidFile)
|
|
1322 {
|
|
1323 pMediaPlayer->Unload();
|
|
1324 MessageBoxA(0, "MediaPlayer error", "MediaPlayer Error", 0);
|
|
1325 return;
|
|
1326 }
|
|
1327
|
|
1328 SetFilePointer(hVidFile, uOffset, 0, FILE_BEGIN);
|
|
1329 strcpy(this->pCurrentMovieName, pFilename);
|
|
1330
|
|
1331 auto hwnd = pMediaPlayer->window->GetApiHandle();
|
|
1332 RECT rc_client;
|
|
1333 GetClientRect(hwnd, &rc_client);
|
|
1334 int client_width = rc_client.right - rc_client.left,
|
|
1335 client_height = rc_client.bottom - rc_client.top;
|
|
1336
|
|
1337 pMovie_Track = pMediaPlayer->LoadMovieFromLOD(hVidFile, &readFunction, &seekFunction, client_width, client_height);
|
|
1338 }
|
|
1339
|
|
1340 //----- (004BF794) --------------------------------------------------------
|
|
1341 void MPlayer::ShowMM7IntroVideo_and_LoadingScreen()
|
|
1342 {
|
|
1343 RGBTexture tex; // [sp+Ch] [bp-30h]@1
|
|
1344 unsigned int uTrackStartMS; // [sp+34h] [bp-8h]@8
|
|
1345 unsigned int uTrackEndMS; // [sp+38h] [bp-4h]@8
|
|
1346
|
|
1347 pMediaPlayer->bStopBeforeSchedule = false;
|
|
1348 // pMediaPlayer->pResetflag = 0;
|
|
1349 bGameoverLoop = true;
|
|
1350 if (!bNoVideo)
|
|
1351 {
|
|
1352 pRenderer->PresentBlackScreen();
|
|
1353 if ( !pMediaPlayer->bStopBeforeSchedule )
|
|
1354 PlayFullscreenMovie(MOVIE_Intro, true);
|
|
1355 }
|
|
1356
|
|
1357 tex.Load("mm6title.pcx", 2);
|
|
1358 pRenderer->BeginScene();
|
|
1359 pRenderer->DrawTextureRGB(0, 0, &tex);
|
|
1360 free(tex.pPixels);
|
|
1361 tex.pPixels = 0;
|
|
1362
|
|
1363 //LoadFonts_and_DrawCopyrightWindow();
|
|
1364 DrawMM7CopyrightWindow();
|
|
1365
|
|
1366 pRenderer->EndScene();
|
|
1367 pRenderer->Present();
|
|
1368
|
|
1369 #ifndef _DEBUG
|
|
1370 Sleep(1500); // let the copyright window stay for a while
|
|
1371 #endif
|
|
1372
|
2508
|
1373 if (!bNoSound )
|
2502
|
1374 {
|
2508
|
1375 if ( use_music_folder )
|
|
1376 {
|
|
1377 PlayAudio(L"Music\\14.mp3");
|
|
1378 alSourcef (mSourceID, AL_GAIN, pSoundVolumeLevels[uMusicVolimeMultiplier]);
|
|
1379 }
|
|
1380 else if ( pAudioPlayer->hAILRedbook )
|
|
1381 {
|
|
1382 pAudioPlayer->SetMusicVolume((signed __int64)(pSoundVolumeLevels[uMusicVolimeMultiplier] * 64.0));
|
|
1383 AIL_redbook_stop(pAudioPlayer->hAILRedbook);
|
|
1384 AIL_redbook_track_info(pAudioPlayer->hAILRedbook, 14, &uTrackStartMS, &uTrackEndMS);
|
|
1385 AIL_redbook_play(pAudioPlayer->hAILRedbook, uTrackStartMS + 1, uTrackEndMS);
|
|
1386 }
|
2502
|
1387 }
|
|
1388 bGameoverLoop = false;
|
|
1389 }
|
|
1390
|
|
1391 //----- (004BEBD7) --------------------------------------------------------
|
|
1392 void MPlayer::Unload()
|
|
1393 {
|
|
1394 bPlaying_Movie = false;
|
|
1395 uMovieType = 0;
|
|
1396 memset(pCurrentMovieName, 0, 0x40);
|
2510
|
1397 if ( !bGameoverLoop && pMediaPlayer->current_movie_width == 460)
|
|
1398 {
|
|
1399 if ( use_music_folder )
|
|
1400 alSourcePlay(mSourceID);
|
|
1401 else if ( pAudioPlayer->hAILRedbook )
|
|
1402 AIL_redbook_resume(pAudioPlayer->hAILRedbook);
|
|
1403 }
|
2502
|
1404 pEventTimer->Resume();
|
|
1405
|
|
1406 pMovie_Track->Release();
|
|
1407 delete pMovie_Track;
|
|
1408 pMovie_Track = nullptr;
|
|
1409 }
|
|
1410
|
|
1411 int MPlayer::readFunction(void* opaque, uint8_t* buf, int buf_size)
|
|
1412 {
|
|
1413 HANDLE stream = (HANDLE)opaque;
|
|
1414 //int numBytes = stream->read((char*)buf, buf_size);
|
|
1415 int numBytes;
|
|
1416 ReadFile(stream, (char *)buf, buf_size, (LPDWORD)&numBytes, NULL);
|
|
1417 return numBytes;
|
|
1418 }
|
|
1419
|
|
1420 int64_t MPlayer::seekFunction(void* opaque, int64_t offset, int whence)
|
|
1421 {
|
|
1422 if (whence == AVSEEK_SIZE)
|
|
1423 return pMediaPlayer->uSize;
|
|
1424 HANDLE h = (HANDLE)opaque;
|
|
1425 LARGE_INTEGER li;
|
|
1426 li.QuadPart = offset;
|
|
1427
|
|
1428 if (!SetFilePointerEx(h, li, (PLARGE_INTEGER)&li, FILE_BEGIN))
|
|
1429 return -1;
|
|
1430 return li.QuadPart;
|
|
1431 }
|
|
1432
|
|
1433 //for video//////////////////////////////////////////////////////////////////
|
|
1434
|
|
1435
|
|
1436
|
|
1437 IMovie *MPlayer::LoadMovieFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
|
|
1438 {
|
|
1439 movie = new Movie;
|
|
1440 Log::Warning(L"allocation dynamic memory for movie\n");
|
|
1441 if (movie)
|
|
1442 {
|
|
1443 if (movie->LoadFromLOD(h, readFunction, seekFunction, width, height))
|
|
1444 return movie;
|
|
1445 delete movie;
|
|
1446 Log::Warning(L"delete dynamic memory for movie\n");
|
|
1447 }
|
|
1448 return nullptr;
|
|
1449 }
|
|
1450
|
|
1451 void MovieRelease()
|
|
1452 {
|
|
1453 movie->Release();
|
|
1454 delete movie;
|
|
1455 Log::Warning(L"delete dynamic memory for movie\n");
|
|
1456 movie = nullptr;
|
|
1457 }
|
|
1458
|
|
1459
|
|
1460 //for audio///////////////////////////////////////////////////////
|
|
1461 //----- (004AB818) --------------------------------------------------------
|
|
1462 void MPlayer::LoadAudioSnd()
|
|
1463 {
|
|
1464 DWORD NumberOfBytesRead; // [sp+Ch] [bp-4h]@3
|
|
1465
|
|
1466 hAudioSnd = CreateFileA("Sounds\\Audio.snd", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0x8000080u, 0);
|
|
1467 if (hAudioSnd == INVALID_HANDLE_VALUE)
|
|
1468 {
|
|
1469 Log::Warning(L"Can't open file: %s", L"Sounds\\Audio.snd");
|
|
1470 return;
|
|
1471 }
|
|
1472
|
|
1473 ReadFile(hAudioSnd, &uNumSoundHeaders, 4, &NumberOfBytesRead, 0);
|
|
1474 pSoundHeaders = nullptr;
|
|
1475 pSoundHeaders = (SoundHeader *)malloc(52 * uNumSoundHeaders + 2);
|
|
1476 ReadFile(hAudioSnd, pSoundHeaders, 52 * uNumSoundHeaders, &NumberOfBytesRead, 0);
|
|
1477 }
|
|
1478 //for audio///////////////////////////////////////////////////////
|
|
1479
|
|
1480 void av_logger(void *, int, const char *format, va_list args)
|
|
1481 {
|
|
1482 va_list va;
|
|
1483 va_start(va, format);
|
|
1484 char msg[256];
|
|
1485 vsprintf(msg, format, va);
|
|
1486 va_end(va);
|
|
1487
|
|
1488 log("av: %s", msg);
|
|
1489 }
|
|
1490
|
|
1491 MPlayer::MPlayer()
|
|
1492 {
|
|
1493 bPlaying_Movie = false;
|
|
1494
|
|
1495 static int libavcodec_initialized = false;
|
|
1496
|
|
1497 if (!libavcodec_initialized)
|
|
1498 {
|
|
1499 av_log_set_callback(av_logger);
|
|
1500 avcodec_register_all();
|
|
1501
|
|
1502 // Register all available file formats and codecs
|
|
1503 av_register_all();
|
|
1504
|
|
1505 libavcodec_initialized = true;
|
|
1506 }
|
|
1507
|
|
1508 bStopBeforeSchedule = false;
|
|
1509 pMovie_Track = nullptr;
|
|
1510
|
|
1511 if (!provider)
|
|
1512 {
|
|
1513 provider = new OpenALSoundProvider;
|
|
1514 Log::Warning(L"allocation dynamic memory for provider\n");
|
|
1515 provider->Initialize();
|
|
1516 }
|
|
1517 LoadAudioSnd();
|
|
1518 }
|
|
1519
|
|
1520 MPlayer::~MPlayer()
|
|
1521 {
|
|
1522 delete provider;
|
|
1523 Log::Warning(L"delete dynamic memory for provider\n");
|
|
1524
|
|
1525 bStopBeforeSchedule = false;
|
|
1526 // pResetflag = 0;
|
|
1527 pVideoFrame.Release();
|
|
1528 }
|
|
1529
|
|
1530 void PlayAudio(const wchar_t * pFilename)
|
|
1531 {
|
|
1532 pAudio_Track = pMediaPlayer->LoadTrack(pFilename);
|
|
1533 pAudio_Track->Play();
|
|
1534 delete pAudio_Track;
|
|
1535 Log::Warning(L"delete dynamic memory for pAudio_Track\n");
|
|
1536 pAudio_Track = nullptr;
|
|
1537 }
|
|
1538
|
|
1539 void PlayMovie(const wchar_t * pFilename)
|
|
1540 {
|
|
1541 Media::IMovie *Movie_track = pMediaPlayer->LoadMovie(pFilename, 640, 480, 0);
|
|
1542 Movie_track->Play();
|
|
1543 }
|
|
1544
|