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