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