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