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