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