2315
|
1 extern "C"
|
|
2 {
|
|
3 #include "lib/libavcodec/avcodec.h"
|
|
4 #include "lib/libavformat/avformat.h"
|
|
5 #include "lib/libavutil/avutil.h"
|
|
6 #include "lib/libavutil/imgutils.h"
|
|
7 #include "lib/libswscale/swscale.h"
|
|
8 #include "lib/libswresample/swresample.h"
|
|
9 #include "lib/libavutil/opt.h"
|
|
10 }
|
|
11 #pragma comment(lib, "avcodec.lib")
|
|
12 #pragma comment(lib, "avformat.lib")
|
|
13 #pragma comment(lib, "avutil.lib")
|
|
14 #pragma comment(lib, "swscale.lib")
|
|
15 #pragma comment(lib, "swresample.lib")
|
|
16
|
|
17 #include <vector>
|
|
18 #include <deque>
|
|
19
|
|
20 #include "stuff.h"
|
|
21 #include "OpenALSoundProvider.h"
|
|
22
|
|
23 #include "MediaPlayer.h"
|
|
24 using namespace Media;
|
2356
|
25
|
|
26 Media::MPlayer *pMediaPlayer;
|
|
27 Media::IMovie *pMovie;
|
|
28 Media::ITrack *pTrack;
|
|
29
|
2359
|
30 int mSourceID;
|
|
31
|
2356
|
32 void PlayMovie(const wchar_t * pFilename);
|
|
33 void PlayAudio(const wchar_t * pFilename);
|
|
34 void LoadMovie(const char *);
|
2347
|
35
|
2315
|
36 class MemoryStream
|
|
37 {
|
|
38 public:
|
|
39 inline MemoryStream(void *data, size_t data_size)
|
|
40 {
|
|
41 this->data_size = data_size;
|
|
42 this->data = data;
|
|
43 this->current_pos = 0;
|
|
44 }
|
|
45 inline MemoryStream()
|
|
46 {
|
|
47 this->data_size = 0;
|
|
48 this->data = nullptr;
|
|
49 this->current_pos = 0;
|
|
50 }
|
|
51
|
|
52 inline ~MemoryStream()
|
|
53 {
|
|
54 if (data)
|
|
55 delete [] data;
|
|
56 }
|
|
57
|
|
58 inline size_t Write(void *buffer, size_t num_bytes)
|
|
59 {
|
|
60 if (!data)
|
|
61 {
|
|
62 data_size = 32 + num_bytes;
|
|
63 data = new char[data_size];
|
|
64 current_pos = 0;
|
|
65 }
|
|
66 else if (current_pos + num_bytes >= data_size)
|
|
67 {
|
|
68 int new_data_size = data_size + num_bytes + data_size / 8 + 4;
|
|
69 auto new_data = new char[new_data_size];
|
|
70
|
|
71 memcpy(new_data, data, data_size);
|
|
72 delete [] data;
|
|
73
|
|
74 data_size = new_data_size;
|
|
75 data = new_data;
|
|
76 }
|
|
77 memcpy((char *)data + current_pos, buffer, num_bytes);
|
|
78 current_pos += num_bytes;
|
|
79 return num_bytes;
|
|
80 }
|
|
81
|
|
82 inline size_t Read(void *buffer, size_t num_bytes)
|
|
83 {
|
|
84 size_t read_size = min(num_bytes, data_size - current_pos);
|
|
85 if (read_size)
|
|
86 {
|
|
87 memcpy(buffer, (char *)data + current_pos, read_size);
|
|
88 current_pos += read_size;
|
|
89 }
|
|
90 return read_size;
|
|
91 }
|
|
92
|
|
93 inline void Reset()
|
|
94 {
|
|
95 current_pos = 0;
|
|
96 }
|
|
97 inline void SeekToEnd()
|
|
98 {
|
|
99 current_pos = data_size;
|
|
100 }
|
|
101
|
2347
|
102 inline size_t Unwind(size_t bytes)//зациклить???
|
2315
|
103 {
|
|
104 if (bytes > current_pos)
|
|
105 current_pos = 0;
|
|
106 else
|
|
107 current_pos -= bytes;
|
|
108 return current_pos;
|
|
109 }
|
|
110
|
|
111 inline size_t Rewind(size_t bytes)
|
|
112 {
|
|
113 if (current_pos + bytes >= data_size)
|
|
114 current_pos = data_size;
|
|
115 else
|
|
116 current_pos += bytes;
|
|
117 return current_pos;
|
|
118 }
|
|
119
|
|
120 inline size_t Size() const {return data_size;}
|
|
121 inline size_t Current() const {return current_pos;}
|
|
122 inline void *Ptr() const {return data;}
|
|
123
|
|
124 private:
|
|
125 void *data;
|
|
126 size_t data_size;
|
|
127 size_t current_pos;
|
|
128 };
|
|
129
|
2345
|
130 bool end_current_file;
|
|
131 bool loop_current_file;
|
|
132 DWORD time_video_begin;
|
|
133 int current_movie_width;
|
|
134 int current_movie_height;
|
2315
|
135
|
|
136 OpenALSoundProvider *provider = nullptr;
|
|
137
|
|
138 static int av_num_bytes_per_sample(AVSampleFormat sample_fmt)
|
|
139 {
|
|
140 switch (sample_fmt)
|
|
141 {
|
|
142 case AV_SAMPLE_FMT_U8:
|
|
143 case AV_SAMPLE_FMT_U8P:
|
|
144 return 1;
|
|
145
|
|
146 case AV_SAMPLE_FMT_S16:
|
|
147 case AV_SAMPLE_FMT_S16P:
|
|
148 return 2;
|
|
149
|
|
150 case AV_SAMPLE_FMT_S32:
|
|
151 case AV_SAMPLE_FMT_S32P:
|
|
152 case AV_SAMPLE_FMT_FLT:
|
|
153 case AV_SAMPLE_FMT_FLTP:
|
|
154 return 4;
|
|
155
|
|
156 case AV_SAMPLE_FMT_DBL:
|
|
157 case AV_SAMPLE_FMT_DBLP:
|
|
158 return 8;
|
|
159
|
|
160 default:
|
|
161 case AV_SAMPLE_FMT_NONE:
|
|
162 Error("Invalid av sample format: %u", sample_fmt);
|
|
163 }
|
|
164 return 0;
|
|
165 }
|
|
166
|
|
167 struct AVStreamWrapper
|
|
168 {
|
|
169 inline AVStreamWrapper()
|
|
170 {
|
|
171 this->type = AVMEDIA_TYPE_UNKNOWN;
|
|
172 this->stream_idx = -1;
|
|
173 this->stream = nullptr;
|
|
174 this->dec = nullptr;
|
|
175 this->dec_ctx = nullptr;
|
|
176 }
|
|
177
|
|
178 inline void Release()
|
|
179 {
|
|
180 type = AVMEDIA_TYPE_UNKNOWN;
|
|
181 stream_idx = -1;
|
|
182 stream = nullptr;
|
|
183 dec = nullptr;
|
|
184 if (dec_ctx)
|
|
185 {
|
2347
|
186 // Close the codec
|
2345
|
187 // закрытие видео кодека
|
2315
|
188 avcodec_close(dec_ctx);
|
|
189 dec_ctx = nullptr;
|
|
190 }
|
|
191 }
|
|
192
|
|
193 AVMediaType type;
|
|
194 int stream_idx;
|
|
195 AVStream *stream;
|
|
196 AVCodec *dec;
|
|
197 AVCodecContext *dec_ctx;
|
|
198 };
|
|
199
|
|
200 struct AVAudioStream: public AVStreamWrapper
|
|
201 {
|
|
202 inline AVAudioStream():
|
|
203 AVStreamWrapper()
|
|
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 {
|
|
215 inline AVVideoStream():
|
|
216 AVStreamWrapper()
|
|
217 {
|
|
218 this->frames_per_second = 0.0;
|
|
219 }
|
|
220
|
|
221 double frames_per_second;
|
|
222 };
|
|
223
|
|
224 static bool av_open_stream(AVFormatContext *format_ctx, AVMediaType type, AVStreamWrapper *out_stream)
|
|
225 {
|
2347
|
226 // найдем первый stream
|
2315
|
227 int stream_idx = av_find_best_stream(format_ctx, type, -1, -1, nullptr, 0);
|
|
228 if (stream_idx >= 0)
|
|
229 {
|
|
230 auto stream = format_ctx->streams[stream_idx];
|
2347
|
231 //Информация о кодеке в потоке называется «контекстом кодека» (AVCodecContext).
|
|
232 //Используя эту информацию, мы можем найти необходимый кодек (AVCodec) и открыть его.
|
|
233 // получаемм кодек
|
2315
|
234 auto dec_ctx = stream->codec;
|
2347
|
235
|
|
236 // Find the decoder for the video stream
|
2345
|
237 // ищем декодер
|
2315
|
238 auto dec = avcodec_find_decoder(dec_ctx->codec_id);
|
|
239 if (dec)
|
|
240 {
|
2347
|
241 // Open codec
|
|
242 // открываем кодек
|
2315
|
243 if (avcodec_open2(dec_ctx, dec, nullptr) >= 0)
|
|
244 {
|
|
245 out_stream->type = type;
|
|
246 out_stream->stream_idx = stream_idx;
|
|
247 out_stream->stream = stream;
|
|
248 out_stream->dec = dec;
|
|
249 out_stream->dec_ctx = dec_ctx;
|
|
250 return true;
|
|
251 }
|
2347
|
252 fprintf(stderr, "ffmpeg: Could not open codec\n");// Не открылся кодек
|
|
253 return false;
|
2315
|
254 }
|
2347
|
255 fprintf(stderr, "ffmpeg: Unable to open codec\n");// Кодек не найден
|
|
256 return false;
|
2315
|
257 }
|
2347
|
258 fprintf(stderr, "ffmpeg: Didn't find a stream\n");// Не найден stream
|
|
259 return false;
|
2315
|
260 }
|
|
261
|
|
262 static bool av_open_audio_stream(AVFormatContext *format_ctx, AVAudioStream *out_stream)
|
|
263 {
|
|
264 if (!av_open_stream(format_ctx, AVMEDIA_TYPE_AUDIO, out_stream))
|
|
265 return Error("Audio stream not found"), false;
|
|
266
|
|
267 // we support only 2-channel audio for now
|
2345
|
268 //if (out_stream->dec_ctx->channels != 2)//закомментировал потому что при воспроизведении jvc.bik вылетает на этом месте
|
|
269 //{
|
|
270 // out_stream->Release();
|
|
271 // return Error("Unsupported number of channels: %u", out_stream->dec_ctx->channels), false;
|
|
272 //}
|
2315
|
273
|
|
274 out_stream->bytes_per_sample = av_num_bytes_per_sample(out_stream->dec_ctx->sample_fmt);
|
|
275 out_stream->bytes_per_second = out_stream->dec_ctx->channels * out_stream->dec_ctx->sample_rate * out_stream->bytes_per_sample;
|
|
276
|
|
277 return true;
|
|
278 }
|
|
279
|
|
280 static bool av_open_video_stream(AVFormatContext *format_ctx, AVVideoStream *out_stream)
|
|
281 {
|
|
282 if (!av_open_stream(format_ctx, AVMEDIA_TYPE_VIDEO, out_stream))
|
|
283 return Error("Video stream not found"), false;
|
|
284
|
|
285 out_stream->frames_per_second = (double)out_stream->dec_ctx->time_base.den / (double)out_stream->dec_ctx->time_base.num;
|
|
286 return true;
|
|
287 }
|
|
288
|
|
289 void InterleaveAudioData(MemoryStream *stream, AVSampleFormat src_format, int num_channels, int num_samples, uint8_t **channels)
|
|
290 {
|
|
291 unsigned int bytes_per_sample;
|
|
292 switch (src_format)
|
|
293 {
|
|
294 case AV_SAMPLE_FMT_U8:
|
|
295 case AV_SAMPLE_FMT_U8P:
|
|
296 __debugbreak();
|
|
297
|
|
298 case AV_SAMPLE_FMT_S16:
|
|
299 bytes_per_sample = sizeof(__int16);
|
|
300 stream->Write(channels[0], num_channels * num_samples * bytes_per_sample);
|
|
301 break;
|
|
302
|
|
303 case AV_SAMPLE_FMT_S16P:
|
|
304 {
|
|
305 bytes_per_sample = sizeof(__int16);
|
|
306 for (int i = 0; i < num_samples; ++i)
|
|
307 for (int j = 0; j < num_channels; ++j)
|
|
308 stream->Write(channels[j] + i * bytes_per_sample, bytes_per_sample);
|
|
309 }
|
|
310 break;
|
|
311
|
|
312 case AV_SAMPLE_FMT_FLT:
|
|
313 {
|
|
314 SwrContext *converter = swr_alloc();
|
|
315 av_opt_set_int(converter, "in_channel_layout", av_get_default_channel_layout(2), 0);
|
|
316 //av_opt_set_int(converter, "in_sample_rate", sample_ra, 0);
|
|
317 av_opt_set_sample_fmt(converter, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
|
|
318
|
|
319 av_opt_set_int(converter, "out_channel_layout", av_get_default_channel_layout(2), 0);
|
|
320 //av_opt_set_int(converter, "out_sample_rate", dst_sample_rate, 0);
|
|
321 av_opt_set_sample_fmt(converter, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
|
|
322
|
|
323 if (swr_init(converter) < 0)
|
|
324 {
|
|
325 __debugbreak();
|
|
326 swr_free(&converter);
|
|
327 return;
|
|
328 }
|
|
329
|
|
330 uint8_t **dst_channels;
|
|
331 int dst_linesize[8];
|
|
332 //int dst_nb_channels = av_get_channel_layout_nb_channels(dst_channel_layout);
|
|
333 if (av_samples_alloc_array_and_samples(&dst_channels, dst_linesize, 2, num_channels * num_samples, AV_SAMPLE_FMT_S16, 0) < 0)
|
|
334 {
|
|
335 __debugbreak();
|
|
336 swr_free(&converter);
|
|
337 return;
|
|
338 }
|
|
339
|
|
340 if (swr_convert(converter, dst_channels, num_channels * num_samples, (const uint8_t **)channels, num_channels * num_samples) >= 0)
|
|
341 stream->Write(dst_channels[0], num_channels * num_samples * sizeof(__int16));
|
|
342 else
|
|
343 __debugbreak();
|
|
344
|
|
345 av_free(dst_channels[0]);
|
|
346 swr_free(&converter);
|
|
347 }
|
|
348 break;
|
|
349
|
|
350 default:
|
|
351 __debugbreak();
|
|
352 //if (Resample(next_frame->avframe, next_frame->avframe->channel_layout, next_frame->avframe->sample_rate,
|
|
353 // av_get_default_channel_layout(2), next_frame->avframe->sample_rate, AV_SAMPLE_FMT_S16P, resampled_data))
|
|
354 }
|
|
355 }
|
|
356
|
2356
|
357 const uint16_t ff_wma_critical_freqs[25] = {
|
|
358 100, 200, 300, 400, 510, 630, 770, 920,
|
|
359 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150,
|
|
360 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500,
|
|
361 24500,
|
|
362 };
|
|
363 extern const uint16_t ff_wma_critical_freqs[25];
|
|
364 static float quant_table[96];
|
|
365
|
2315
|
366 bool DecodeAudioFrame(AVCodecContext *dec_ctx, AVPacket *avpacket, AVFrame *avframe, MemoryStream *out_audio_data, int *out_num_audio_samples)
|
|
367 {
|
|
368 volatile int decoded = false;
|
|
369 do
|
|
370 {
|
2347
|
371 //Декодирование аудио-пакета осуществляется функцией avcodec_decode_audio4
|
2315
|
372 if (avcodec_decode_audio4(dec_ctx, avframe, (int *)&decoded, avpacket) < 0)
|
|
373 {
|
|
374 log("Cannot decode audio frame\n");
|
|
375 return false;
|
|
376 }
|
|
377
|
|
378 if (!decoded)
|
|
379 log("Cannot decode audio frame in one piece\n");
|
|
380 } while (!decoded);
|
|
381
|
|
382 switch (dec_ctx->codec_id)
|
|
383 {
|
2356
|
384 case AV_CODEC_ID_BINKAUDIO_DCT:
|
|
385 {
|
|
386 __debugbreak();
|
|
387 }
|
2315
|
388 case AV_CODEC_ID_BINKAUDIO_RDFT:
|
|
389 {//pts samples dpts
|
|
390 // 0 960
|
|
391 //17280 960 17280 18x960
|
|
392 //18240 960 960 1x960
|
|
393 //20160 960 1920 2x960
|
|
394 //21120 960 960 1x960
|
|
395 //23040 960 1920 2x960
|
2356
|
396 /*static int bink_next_pts = 0;
|
2315
|
397
|
|
398 // there's a gap in the sound - fill empty samples in
|
|
399 if (bink_next_pts < avpacket->pts)
|
|
400 {
|
|
401 short silence[1024];
|
|
402 memset(silence, 0, sizeof(silence));
|
|
403
|
2356
|
404 int samples_to_fill = /*dec_ctx->channels * (avpacket->pts - bink_next_pts);
|
2315
|
405 while (samples_to_fill > 0)
|
|
406 {
|
|
407 int samples_to_fill_this_step = samples_to_fill >= 1024 ? 1024 : samples_to_fill;
|
|
408 out_audio_data->Write(silence, samples_to_fill_this_step * sizeof(short));
|
|
409
|
|
410 samples_to_fill -= samples_to_fill_this_step;
|
|
411 }
|
|
412 }
|
|
413
|
2356
|
414 bink_next_pts = avpacket->pts + /*dec_ctx->channels * avframe->nb_samples; */
|
|
415
|
|
416 AVFrame frame;
|
|
417 int first;
|
|
418 int version_b;
|
|
419 int frame_len;
|
|
420 int overlap_len;
|
|
421 int block_size;
|
|
422 int num_bands;
|
|
423 unsigned int *bands;
|
|
424 float root;
|
|
425 int sample_rate = dec_ctx->sample_rate;
|
|
426 int sample_rate_half;
|
|
427 int i;
|
|
428 int frame_len_bits;
|
|
429 int channels;
|
|
430
|
|
431 //compresses audio in chunks of varying sizes depending on sample rate:
|
|
432 // if sample rate < 22050, frame size is 2048 samples
|
|
433 // if sample rate < 44100, frame size is 4096 samples
|
|
434 // else, frame size is 8192 samples
|
|
435
|
|
436 //сжимает аудио в куски различных размеров в зависимости от частоты дискретизации:
|
|
437 //если частота дискретизации < 22050, размер кадра составляет 2048 самплов
|
|
438 //если частота дискретизации < 44100, размер кадра составляет 4096 самплов
|
|
439 //или, размер кадра составляет 8192 самплов
|
|
440
|
|
441 /* determine frame length */
|
|
442 if (dec_ctx->sample_rate < 22050)
|
|
443 frame_len_bits = 9;
|
|
444 else if (dec_ctx->sample_rate < 44100)
|
|
445 frame_len_bits = 10;
|
|
446 else
|
|
447 frame_len_bits = 11;
|
|
448
|
|
449 //проверка количества каналов (не меньше 1 и не больше 2)
|
|
450 if (dec_ctx->channels < 1 || dec_ctx->channels > 2)
|
|
451 {
|
|
452 av_log(dec_ctx, AV_LOG_ERROR, "invalid number of channels: %d\n", dec_ctx->channels);
|
|
453 return AVERROR_INVALIDDATA;
|
|
454 }
|
|
455
|
|
456 version_b = dec_ctx->extradata_size >= 4 && dec_ctx->extradata[3] == 'b';
|
|
457 if (version_b)
|
|
458 __debugbreak();
|
|
459
|
|
460 if (dec_ctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
|
|
461 {
|
|
462 // audio is already interleaved for the RDFT format variant
|
|
463 dec_ctx->sample_fmt = AV_SAMPLE_FMT_FLT;
|
|
464 sample_rate *= dec_ctx->channels;
|
|
465 channels = 1;
|
|
466 if (!version_b)
|
|
467 frame_len_bits += av_log2(dec_ctx->channels);
|
|
468 }
|
|
469 else
|
|
470 {
|
|
471 channels = dec_ctx->channels;
|
|
472 dec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
|
|
473 }
|
|
474
|
|
475 frame_len = 1 << frame_len_bits; //2048
|
|
476
|
|
477 //a frame is windowed with the previous frame; the size of the window is frame size / 16
|
|
478 //кадр оконный с предыдущего кадра;Размер окна = размер кадра / 16
|
|
479 overlap_len = frame_len / 16; //128
|
|
480 block_size = (frame_len - overlap_len) * channels; //1920
|
|
481
|
|
482 //compute half the sample rate as (sample rate + 1) / 2;
|
|
483 //initialize an array of band frequencies corresponding to an array of 25 critical frequencies (same as WMA, apparently),
|
|
484 // any for which the critical frequencies are less than half the sample rate
|
|
485
|
|
486 //вычислить половину частоты дискретизации(частота дискретизации + 1) / 2;
|
|
487 //инициализировать массив группы частот, соответствующих массиву 25 критических частот (аналогично WMA, очевидно),
|
|
488 // любой, для которых критические частоты в два раза меньше частота дискретизации
|
|
489 sample_rate_half = (sample_rate + 1) / 2; //22050
|
|
490 if (dec_ctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
|
|
491 root = 2.0 / (sqrt(frame_len) * 32768.0);
|
|
492 else
|
|
493 root = frame_len / (sqrt(frame_len) * 32768.0);
|
|
494 for (i = 0; i < 96; i++)
|
|
495 {
|
|
496 /* constant is result of 0.066399999/log10(M_E) */
|
|
497 quant_table[i] = expf(i * 0.15289164787221953823f) * root;
|
|
498 }
|
|
499
|
|
500 /* calculate number of bands */
|
|
501 //bands calculation:
|
|
502 //bands[0] = 1;
|
|
503 //foreach (i in 1..# of bands-1):
|
|
504 //bands[i] = crit_freq[i-1] * (frame length / 2) / (sample rate / 2);
|
|
505 //bands[# of bands] = frame length / 2
|
|
506 for (num_bands = 1; num_bands < 25; num_bands++)
|
|
507 if (sample_rate_half <= ff_wma_critical_freqs[num_bands - 1])
|
|
508 break;
|
|
509
|
|
510 bands = (unsigned int *)(av_malloc((num_bands + 1) * sizeof(*bands)));
|
|
511 if (!bands)
|
|
512 return AVERROR(ENOMEM);
|
|
513
|
|
514 /* populate bands data */
|
|
515 bands[0] = 2;
|
|
516 for (i = 1; i < num_bands; i++)
|
|
517 bands[i] = (ff_wma_critical_freqs[i - 1] * frame_len / sample_rate_half) & ~1;
|
|
518 bands[num_bands] = frame_len;
|
|
519
|
|
520 first = 1;
|
|
521
|
|
522 //ff_rdft_init(&trans.rdft, frame_len_bits, DFT_C2R);
|
|
523
|
|
524 avcodec_get_frame_defaults(&frame);
|
|
525 dec_ctx->coded_frame = &frame;
|
2315
|
526 }
|
|
527 break;
|
|
528 /*
|
|
529 case AV_CODEC_ID_SMACKAUDIO:
|
|
530 {
|
|
531 static int smack_debug_next_audio_time = 0;
|
|
532 if (smack_debug_next_audio_time != packet->pts)
|
|
533 {
|
|
534 Error("There's a gap in the sound before frame %u\n", num_audio_frames);
|
|
535 __debugbreak(); // there's a gap in the sound
|
|
536 }
|
|
537
|
|
538 int num_actual_data_channels = 0;
|
|
539 switch (dec_ctx->sample_fmt)
|
|
540 {
|
|
541 case AV_SAMPLE_FMT_U8:
|
|
542 case AV_SAMPLE_FMT_S16:
|
|
543 case AV_SAMPLE_FMT_S32:
|
|
544 case AV_SAMPLE_FMT_FLT:
|
|
545 case AV_SAMPLE_FMT_DBL:
|
|
546 num_actual_data_channels = 1;
|
|
547 break;
|
|
548
|
|
549 case AV_SAMPLE_FMT_U8P:
|
|
550 case AV_SAMPLE_FMT_S16P:
|
|
551 case AV_SAMPLE_FMT_S32P:
|
|
552 case AV_SAMPLE_FMT_FLTP:
|
|
553 case AV_SAMPLE_FMT_DBLP:
|
|
554 num_actual_data_channels = dec_ctx->channels;
|
|
555 break;
|
|
556
|
|
557 default:
|
|
558 case AV_SAMPLE_FMT_NONE:
|
|
559 case AV_SAMPLE_FMT_NB:
|
|
560 __debugbreak();
|
|
561 }
|
|
562
|
|
563 smack_debug_next_audio_time += dec_ctx->channels * frame->nb_samples * bytes_per_sample;
|
|
564 Assert(frame->avframe->linesize[0] == audio.dec_ctx->channels * frame->avframe->nb_samples * audio.bytes_per_sample / num_actual_data_channels,
|
|
565 "Smack audio size mismatch in frame %u in %s\n", audio_num_read_frames, movie_filename);
|
|
566
|
|
567 frame->play_time = (double)frame->avpacket->pts / (double)audio.bytes_per_second;
|
|
568 }
|
|
569 break;
|
|
570
|
|
571 case AV_CODEC_ID_MP3:
|
|
572 {
|
|
573 static int mp3_samples_decoded_so_far = 0;
|
|
574 static int mp3_prev_samples_count = frame->avframe->nb_samples; // mp3 seems to always feed same amount of samples
|
|
575 frame->play_time = (double)mp3_samples_decoded_so_far / (double)audio.dec_ctx->sample_rate;
|
|
576
|
|
577 mp3_samples_decoded_so_far += frame->avframe->nb_samples;
|
|
578 Assert(mp3_prev_samples_count == frame->avframe->nb_samples,
|
|
579 "MP3 audio have variable sample count in frame %u in %s\n", audio_num_read_frames, movie_filename);
|
|
580 }
|
|
581 break;
|
|
582
|
|
583 default:
|
|
584 {
|
|
585 __debugbreak();
|
|
586 double samples_per_second = (double)audio.dec_ctx->time_base.den / (double)audio.dec_ctx->time_base.num;
|
|
587 double play_length = frame->avframe->nb_samples / samples_per_second;
|
|
588 frame->play_time = (double)frame->avpacket->pts / samples_per_second;
|
|
589 }
|
|
590 break;*/
|
|
591 }
|
|
592
|
|
593 if (!avframe->channel_layout)
|
|
594 {
|
|
595 log("Audio channel layout not specified, rolling back to default\n");
|
|
596 avframe->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
|
|
597 }
|
|
598
|
|
599 *out_num_audio_samples = dec_ctx->channels * avframe->nb_samples;
|
|
600 InterleaveAudioData(out_audio_data, dec_ctx->sample_fmt,
|
|
601 dec_ctx->channels, avframe->nb_samples, avframe->data);
|
|
602 return true;
|
|
603 }
|
|
604
|
|
605 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)
|
|
606 {
|
|
607 out_audio_stream->Reset();
|
|
608
|
2347
|
609 //Чтение аудио данных.
|
|
610 //Данные из файла читаются пакетами (AVPacket), а для воспроизведения используется фрейм (AVFrame).
|
|
611
|
|
612 //Выделим память для фрейма
|
2315
|
613 AVFrame *frame = avcodec_alloc_frame();
|
2347
|
614
|
2315
|
615 AVPacket *packet = new AVPacket;
|
|
616 av_init_packet(packet);
|
|
617
|
|
618 int num_audio_frames = 0;
|
|
619 int num_audio_samples = 0;
|
2347
|
620
|
2345
|
621 //чтение пакетов
|
2315
|
622 while (av_read_frame(format_ctx, packet) >= 0)
|
|
623 {
|
2347
|
624 // Is this a packet from the audio stream?
|
2345
|
625 //Принадлежит ли пакет к аудиопотоку
|
2315
|
626 if (packet->stream_index != audio_stream_idx)
|
|
627 {
|
|
628 //log("Suspicious stream id %u in %s", packet->stream_index, filenamea);
|
|
629 continue;
|
|
630 }
|
|
631
|
2347
|
632 // Decode audio frame
|
|
633 //Декодируем аудио фрейм
|
2315
|
634 int num_samples_decoded;
|
|
635 DecodeAudioFrame(dec_ctx, packet, frame, out_audio_stream, &num_samples_decoded);
|
|
636
|
|
637 num_audio_samples += num_samples_decoded;
|
|
638 num_audio_frames++;
|
|
639 }
|
|
640 *out_num_audio_frames = num_audio_frames;
|
|
641 *out_num_audio_samples = num_audio_samples;
|
|
642
|
2347
|
643 //Освободить память выделенную для фрейма
|
2315
|
644 avcodec_free_frame(&frame);
|
|
645 delete packet;
|
|
646
|
|
647 return true;
|
|
648 }
|
|
649
|
|
650 class Track: public Media::ITrack
|
|
651 {
|
|
652 public:
|
|
653 inline Track()
|
|
654 {
|
|
655 this->format_ctx = nullptr;
|
|
656 this->audio_num_samples = 0;
|
|
657 }
|
|
658
|
|
659 void Release()
|
|
660 {
|
|
661 ReleaseAvcodec();
|
|
662 }
|
|
663
|
|
664 void ReleaseAvcodec()
|
|
665 {
|
|
666 audio.Release();
|
|
667 if (format_ctx)
|
|
668 {
|
2345
|
669 // закрытия контекста файла
|
2315
|
670 av_close_input_file(format_ctx);
|
|
671 format_ctx = nullptr;
|
|
672 }
|
|
673 }
|
|
674
|
2347
|
675 bool LoadAudio(const wchar_t *filename) //Загрузка из папки для mp3
|
2315
|
676 {
|
|
677 char filenamea[1024];
|
|
678 sprintf(filenamea, "%S", filename);
|
2347
|
679 // Open audio file
|
|
680 //откроем входной файл(Шаг 2)
|
|
681 //Функция avformat_open_input читает файловый заголовок и сохраняет информацию о найденных форматах в структуре
|
|
682 //AVFormatContext. Остальные аргументы могут быть установлены в NULL, в этом случае libavformat использует
|
|
683 //автоматическое определение параметров.
|
2315
|
684 if (avformat_open_input(&format_ctx, filenamea, nullptr, nullptr) >= 0)
|
|
685 {
|
2347
|
686 // Retrieve stream information
|
|
687 //Т.к. avformat_open_input читает только заголовок файла, то следующим шагом нужно получить информацию о потоках
|
|
688 //в файле. Это делается функцией avformat_find_stream_info.
|
2315
|
689 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
|
|
690 {
|
2347
|
691 // Dump information about file onto standard error
|
|
692 //После этого format_context->streams содержит все существующие потоки файла.
|
|
693 //Их количество равно format_context->nb_streams.
|
|
694 //Вывести подробную информацию о файле и обо всех потоках можно функцией av_dump_format.
|
2315
|
695 av_dump_format(format_ctx, 0, filenamea, 0);
|
|
696
|
2347
|
697 //Открыть поток
|
2315
|
698 if (!av_open_audio_stream(format_ctx, &audio))
|
|
699 {
|
|
700 Error("Cannot open strack: %s", filenamea);
|
|
701 return Release(), false;
|
|
702 }
|
|
703
|
|
704 MemoryStream audio_plain_data;
|
|
705 int num_audio_frames;
|
|
706 int num_audio_samples;
|
2347
|
707
|
|
708 //Загрузить аудио трек
|
2315
|
709 if (LoadAudioTrack(format_ctx, audio.dec_ctx, audio.stream_idx, &audio_plain_data, &num_audio_frames, &num_audio_samples))
|
|
710 {
|
|
711 /*#ifdef _DEBUG
|
|
712 char debug_filename[1024];
|
|
713 sprintf(debug_filename, "%s.wav", filenamea);
|
|
714 FILE *wav = fopen(debug_filename, "w+b");
|
|
715
|
|
716 extern void write_wav_header(FILE *wav, int channel_count = 2, int sample_rate = 22050, int bytes_per_sample = 2);
|
|
717 write_wav_header(wav, audio.dec_ctx->channels, audio.dec_ctx->sample_rate, audio.bytes_per_sample);
|
|
718
|
|
719 fwrite(audio_plain_data.Ptr(), audio_plain_data.Current(), 1, wav);
|
|
720
|
|
721 extern void fix_wav_header(FILE *wav, int wav_bytes_in_stream);
|
|
722 fix_wav_header(wav, audio_plain_data.Current());
|
|
723 #endif*/
|
|
724
|
|
725 device_buffer = provider->CreateTrack16(audio.dec_ctx->channels, audio.dec_ctx->sample_rate, 2, num_audio_samples, audio_plain_data.Ptr());
|
|
726
|
|
727 ReleaseAvcodec();
|
|
728 return true;
|
|
729 }
|
|
730 }
|
|
731 Release();
|
2347
|
732 fprintf(stderr, "ffmpeg: Unable to find stream info\n"); //Не найден поток
|
2345
|
733 return false;
|
2315
|
734 }
|
2347
|
735 fprintf(stderr, "ffmpeg: Unable to open input file\n"); //Не может открыть файл
|
2315
|
736 return false;
|
|
737 }
|
|
738
|
|
739 virtual void Play(bool loop)
|
|
740 {
|
|
741 provider->PlayTrack16(device_buffer, loop);
|
2359
|
742 mSourceID = device_buffer->source_id;
|
2315
|
743 }
|
2347
|
744
|
2315
|
745 protected:
|
|
746 AVFormatContext *format_ctx;
|
|
747 AVAudioStream audio;
|
|
748 int audio_num_samples;
|
|
749
|
2345
|
750 bool stopped;
|
|
751
|
2315
|
752 OpenALSoundProvider::TrackBuffer *device_buffer;
|
|
753 };
|
|
754
|
|
755 class Movie: public Media::IMovie
|
|
756 {
|
|
757 public:
|
2345
|
758 int audio_stream_idx;
|
|
759 AVStream *audio_stream;//содержат информацию о аудио потоке
|
|
760 AVCodec *audio_stream_dec;
|
|
761 AVCodecContext *audio_stream_dec_ctx;
|
|
762
|
|
763 int video_stream_idx;
|
|
764 AVStream *video_stream;//содержат информацию о видео потоке
|
|
765 AVCodec *video_stream_dec;
|
|
766 AVCodecContext *video_stream_dec_ctx;
|
|
767
|
2315
|
768 inline Movie()
|
|
769 {
|
|
770 this->movie_filename[0] = 0;
|
|
771 this->width = 0;
|
|
772 this->height = 0;
|
|
773 this->format_ctx = nullptr;
|
|
774 this->end_of_file = false;
|
|
775 this->playback_time = 0.0;
|
|
776
|
|
777 this->num_audio_frames = 0;
|
|
778 this->num_audio_samples = 0;
|
|
779
|
|
780 this->last_resampled_frame_num = -1;
|
|
781 memset(last_resampled_frame_data, 0, sizeof(last_resampled_frame_data));
|
|
782 memset(last_resampled_frame_linesize, 0, sizeof(last_resampled_frame_linesize));
|
2345
|
783
|
|
784 decoding_packet = nullptr;
|
|
785 ioBuffer = nullptr;
|
|
786 format_ctx = nullptr;
|
|
787 avioContext = nullptr;
|
2315
|
788 }
|
2347
|
789
|
2315
|
790 inline void Release()
|
|
791 {
|
|
792 ReleaseAVCodec();
|
|
793 }
|
|
794
|
|
795 inline void ReleaseAVCodec()
|
|
796 {
|
|
797 audio.Release();
|
|
798 video.Release();
|
|
799 if (format_ctx)
|
|
800 {
|
2347
|
801 // Close the video file
|
|
802 // закрытие контекста файла(видео файла)
|
2315
|
803 av_close_input_file(format_ctx);
|
|
804 format_ctx = nullptr;
|
|
805 }
|
|
806 }
|
|
807
|
2347
|
808 bool Load(const wchar_t *filename, int dst_width, int dst_height, int cache_ms) //Загрузка
|
2315
|
809 {
|
|
810 char filenamea[1024];
|
|
811 sprintf(filenamea, "%S", filename);
|
|
812 sprintf(movie_filename, "%S", filename);
|
|
813
|
|
814 width = dst_width;
|
|
815 height = dst_height;
|
2347
|
816 // Open video file
|
|
817 //откроем входной файл(Шаг 2)
|
|
818 //Функция avformat_open_input читает файловый заголовок и сохраняет информацию о найденных форматах в структуре
|
|
819 //AVFormatContext. Остальные аргументы могут быть установлены в NULL, в этом случае libavformat использует
|
|
820 //автоматическое определение параметров. Последние 2 аргумента используются для формата файла и опций.
|
2315
|
821 if (avformat_open_input(&format_ctx, filenamea, nullptr, nullptr) >= 0)
|
|
822 {
|
2347
|
823 // Retrieve stream information
|
|
824 // Проверяем потоки
|
|
825 //Т.к. avformat_open_input читает только заголовок файла, то следующим шагом нужно получить информацию о потоках
|
|
826 //в файле. Это делается функцией avformat_find_stream_info.(Шаг 3)
|
2315
|
827 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
|
|
828 {
|
2347
|
829 // Dump information about file onto standard error
|
|
830 //Инициализируем pFormatCtx->streams
|
|
831 //После этого format_context->streams содержит все существующие потоки файла.
|
|
832 //Их количество равно format_context->nb_streams.
|
|
833 //Вывести подробную информацию о файле и обо всех потоках можно функцией av_dump_format.
|
2315
|
834 av_dump_format(format_ctx, 0, filenamea, 0);
|
|
835
|
2347
|
836 //pFormatCtx->streams - массив указателей, размера pFormatCtx->nb_streams, поищем тут потоки.
|
2315
|
837 if (!av_open_audio_stream(format_ctx, &audio))
|
|
838 {
|
|
839 Error("Cannot open audio stream: %s", filenamea);
|
|
840 return Release(), false;
|
|
841 }
|
|
842
|
|
843 if (!av_open_video_stream(format_ctx, &video))
|
|
844 {
|
|
845 Error("Cannot open video stream: %s", filenamea);
|
|
846 return Release(), false;
|
|
847 }
|
2347
|
848
|
|
849 //Ritor1: include
|
|
850 if (_stricmp("binkvideo", video.dec->name) )
|
|
851 {
|
2345
|
852 current_movie_width = video.dec_ctx->width;
|
|
853 current_movie_height = video.dec_ctx->height;
|
2347
|
854 }
|
|
855 else
|
|
856 {
|
2345
|
857 current_movie_width = width;
|
|
858 current_movie_height = height;
|
2347
|
859 }
|
|
860 //
|
2345
|
861
|
2315
|
862 decoding_packet = new AVPacket;
|
|
863 av_init_packet(decoding_packet);
|
|
864
|
2347
|
865 // Allocate video frame
|
|
866 //Выделим память для фрейма
|
2315
|
867 decoding_frame = avcodec_alloc_frame();
|
|
868
|
|
869 audio_data_in_device = provider->CreateStreamingTrack16(audio.dec_ctx->channels, audio.dec_ctx->sample_rate, 2);
|
|
870
|
|
871 return true;
|
|
872 }
|
2345
|
873 fprintf(stderr, "ffmpeg: Unable to find stream info\n");
|
2347
|
874 return Release(), false; // Не найдена информация о потоке
|
2315
|
875 }
|
2345
|
876 fprintf(stderr, "ffmpeg: Unable to open input file\n");
|
2347
|
877 return Release(), false; // Не может открыть файл
|
2315
|
878 }
|
2347
|
879
|
2345
|
880 bool LoadFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
|
|
881 {
|
|
882 if (!ioBuffer)
|
|
883 ioBuffer = (unsigned char *)av_malloc(16384 + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
|
|
884 if (!avioContext)
|
|
885 avioContext = avio_alloc_context(ioBuffer, 16384, 0, h, readFunction, NULL, seekFunction);
|
|
886 if (!format_ctx)
|
|
887 format_ctx = avformat_alloc_context();
|
|
888 format_ctx->pb = avioContext;
|
|
889 return Load(L"dummyFilename", width, height, 0);
|
|
890 }
|
|
891
|
2347
|
892 virtual void GetNextFrame(double dt, void *dst_surface)// Получить следующий фрейм
|
2315
|
893 {
|
2319
|
894 playback_time += dt;//изменение времени
|
2315
|
895
|
2347
|
896 //Данные из файла читаются пакетами (AVPacket), а для отображения используется фрейм (AVFrame).
|
2315
|
897 AVPacket *avpacket = decoding_packet;
|
|
898 AVFrame *avframe = decoding_frame;
|
2347
|
899
|
|
900 //???
|
2315
|
901 avcodec_get_frame_defaults(avframe);
|
|
902
|
|
903 int desired_frame_number = floor(playback_time * video.dec_ctx->time_base.den / video.dec_ctx->time_base.num + 0.5);
|
|
904 if (last_resampled_frame_num == desired_frame_number)
|
|
905 {
|
|
906 memcpy(dst_surface, last_resampled_frame_data[0], height * last_resampled_frame_linesize[0]);
|
|
907 return;
|
|
908 }
|
|
909
|
2347
|
910 volatile int frameFinished = false;
|
2359
|
911 do
|
2315
|
912 {
|
2359
|
913 if (av_read_frame(format_ctx, avpacket) < 0) //воспроизведение завершено
|
|
914 {
|
|
915 if (loop_current_file)
|
|
916 {
|
|
917 if (av_seek_frame(format_ctx, video.stream_idx, format_ctx->start_time, AVSEEK_FLAG_BACKWARD) < 0);
|
|
918 fprintf(stderr, "%s: error while seeking\n", format_ctx->filename);
|
|
919 while (avpacket->stream_index!=video.stream_idx)
|
|
920 {
|
|
921 av_free_packet(avpacket);
|
|
922 if (av_read_frame(format_ctx, avpacket)<0)
|
|
923 av_seek_frame(format_ctx, -1, format_ctx->start_time, AVSEEK_FLAG_BACKWARD);
|
|
924 }
|
|
925 }
|
|
926 else
|
|
927 {
|
|
928 end_current_file = true;
|
|
929 return;
|
|
930 }
|
|
931 // probably movie is finished
|
|
932 //__debugbreak();
|
|
933 }
|
2315
|
934 if (avpacket->stream_index == audio.stream_idx)
|
|
935 {
|
|
936 MemoryStream audio_data;
|
|
937 if (DecodeAudioFrame(audio.dec_ctx, avpacket, avframe, &audio_data, &num_audio_samples))
|
2359
|
938 provider->Stream16(audio_data_in_device, num_audio_samples, audio_data.Ptr());
|
|
939 //continue;
|
2315
|
940 }
|
2359
|
941 // video packet - decode & maybe show
|
2315
|
942 else if (avpacket->stream_index == video.stream_idx)
|
|
943 {
|
2359
|
944 do
|
2315
|
945 {
|
2359
|
946 if (avcodec_decode_video2(video.dec_ctx, avframe, (int *)&frameFinished, avpacket) < 0)
|
|
947 __debugbreak();
|
|
948 } while (!frameFinished);
|
|
949 }
|
|
950 }
|
|
951 while (avpacket->stream_index != video.stream_idx ||
|
|
952 avpacket->pts != desired_frame_number);
|
|
953
|
|
954 if (frameFinished)
|
|
955 {
|
|
956 if (last_resampled_frame_data[0])
|
|
957 av_freep(&last_resampled_frame_data[0]);
|
2315
|
958
|
2359
|
959 AVPixelFormat rescaled_format = AV_PIX_FMT_RGB32;
|
|
960 uint8_t *rescaled_data[4] = {nullptr, nullptr, nullptr, nullptr};
|
|
961 int rescaled_linesize[4] = {0, 0, 0, 0};
|
|
962 if (av_image_alloc(rescaled_data, rescaled_linesize, current_movie_width, current_movie_height, rescaled_format, 1) >= 0)
|
|
963 {
|
|
964 // создание контекста для преобразования
|
|
965 SwsContext *converter = sws_getContext(avframe->width, avframe->height, (AVPixelFormat)avframe->format,
|
|
966 current_movie_width, current_movie_height, rescaled_format,
|
|
967 SWS_BICUBIC, nullptr, nullptr, nullptr);
|
|
968 // преобразование кадра(масштабирование)
|
|
969 sws_scale(converter, avframe->data, avframe->linesize, 0, avframe->height, rescaled_data, rescaled_linesize);
|
|
970 sws_freeContext(converter);
|
2315
|
971
|
2359
|
972 //копирование в возвращаемую переменную
|
|
973 memcpy(dst_surface, rescaled_data[0], current_movie_height * rescaled_linesize[0]);
|
|
974
|
|
975 last_resampled_frame_num = desired_frame_number;
|
|
976 memcpy(last_resampled_frame_data, rescaled_data, sizeof(rescaled_data));
|
|
977 memcpy(last_resampled_frame_linesize, rescaled_linesize, sizeof(rescaled_linesize));
|
2315
|
978 }
|
|
979 }
|
|
980 else
|
2359
|
981 memset(dst_surface, 0, width * current_movie_height * 4);
|
2315
|
982 }
|
|
983
|
2347
|
984 virtual void Play()
|
|
985 {
|
|
986 }
|
2345
|
987
|
2315
|
988 protected:
|
|
989 char movie_filename[256];
|
|
990 int width;
|
|
991 int height;
|
2345
|
992 bool stopped;
|
2315
|
993 AVFormatContext *format_ctx;
|
|
994 double playback_time;
|
|
995 bool end_of_file;
|
|
996
|
|
997 AVPacket *decoding_packet;
|
|
998 AVFrame *decoding_frame;
|
|
999
|
|
1000 AVAudioStream audio;
|
|
1001 int num_audio_frames;
|
|
1002 int num_audio_samples;
|
2345
|
1003 unsigned char * ioBuffer;
|
|
1004 AVIOContext *avioContext;
|
2315
|
1005 OpenALSoundProvider::StreamingTrackBuffer *audio_data_in_device;
|
|
1006
|
|
1007 AVVideoStream video;
|
|
1008 int last_resampled_frame_num;
|
|
1009 uint8_t *last_resampled_frame_data[4];
|
|
1010 int last_resampled_frame_linesize[4];
|
2347
|
1011 };
|
2315
|
1012
|
2356
|
1013 ITrack *MPlayer::LoadTrack(const wchar_t *filename) //Загрузить mp3
|
2315
|
1014 {
|
|
1015 auto track = new Track;
|
2347
|
1016 if (!track->LoadAudio(filename))
|
2315
|
1017 {
|
|
1018 delete track;
|
|
1019 track = nullptr;
|
|
1020 }
|
|
1021 return track;
|
|
1022 }
|
|
1023
|
2356
|
1024 IMovie *MPlayer::LoadMovie(const wchar_t *filename, int width, int height, int cache_ms) //Загрузить видео
|
2315
|
1025 {
|
|
1026 auto movie = new Movie;
|
|
1027 if (!movie->Load(filename, width, height, cache_ms))
|
|
1028 {
|
|
1029 delete movie;
|
|
1030 movie = nullptr;
|
|
1031 }
|
|
1032 return movie;
|
|
1033 }
|
|
1034
|
2356
|
1035 IMovie *MPlayer::LoadMovieFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
|
2347
|
1036 {
|
|
1037 auto movie = new Movie;
|
|
1038 if (movie)
|
|
1039 {
|
|
1040 if (movie->LoadFromLOD(h, readFunction, seekFunction, width, height))
|
|
1041 return movie;
|
|
1042 delete movie;
|
|
1043 }
|
|
1044 return nullptr;
|
|
1045 }
|
2315
|
1046
|
|
1047 void av_logger(void *, int, const char *format, va_list args)
|
|
1048 {
|
|
1049 va_list va;
|
|
1050 va_start(va, format);
|
|
1051 char msg[256];
|
|
1052 vsprintf(msg, format, va);
|
|
1053 va_end(va);
|
|
1054
|
|
1055 log("av: %s", msg);
|
|
1056 }
|
|
1057
|
2356
|
1058 MPlayer::MPlayer()
|
2315
|
1059 {
|
|
1060 static int libavcodec_initialized = false;
|
|
1061
|
|
1062 if (!libavcodec_initialized)
|
|
1063 {
|
|
1064 av_log_set_callback(av_logger);
|
|
1065 avcodec_register_all();
|
2345
|
1066
|
2347
|
1067 // Register all available file formats and codecs
|
|
1068 //инициализируем библиотеку ffmpeg(Шаг 1)
|
|
1069 //Во время инициализации регистрируются все имеющиеся в библиотеке форматы файлов и кодеков.
|
|
1070 //После этого они будут использоваться автоматически при открытии файлов этого формата и с этими кодеками.
|
2315
|
1071 av_register_all();
|
|
1072
|
|
1073 libavcodec_initialized = true;
|
|
1074 }
|
|
1075
|
|
1076 if (!provider)
|
|
1077 {
|
|
1078 provider = new OpenALSoundProvider;
|
|
1079 provider->Initialize();
|
|
1080 }
|
|
1081 }
|
|
1082
|
2356
|
1083 MPlayer::~MPlayer()
|
2315
|
1084 {
|
2345
|
1085 }
|
2356
|
1086
|
|
1087 void PlayAudio(const wchar_t * pFilename)
|
|
1088 {
|
|
1089 pTrack = pMediaPlayer->LoadTrack(pFilename);
|
|
1090 pTrack->Play();
|
|
1091 }
|
|
1092
|
|
1093 void PlayMovie(const wchar_t * pFilename)
|
|
1094 {
|
|
1095 Media::IMovie *track = pMediaPlayer->LoadMovie(pFilename, 640, 480, 0);
|
|
1096 track->Play();
|
|
1097 }
|
|
1098
|
|
1099 //////////////////////////////////////////////////////////////////////////
|
|
1100 //Included from a VideoPlayer.cpp file/вставлено из файла VideoPlayer.cpp/
|
|
1101 //////////////////////////////////////////////////////////////////////////
|
|
1102
|
|
1103 //used in void VideoPlayer::Initialize(OSWindow *target_window) for open .vid files
|
|
1104 MovieHeader *pMightVideoHeaders;
|
|
1105 MovieHeader *pMagicVideoHeaders;
|
|
1106 HANDLE hMightVid;
|
|
1107 HANDLE hMagicVid;
|
|
1108 unsigned __int64 uBinkVersion;
|
|
1109 unsigned int uNumMightVideoHeaders;
|
|
1110 unsigned int uNumMagicVideoHeaders;
|
|
1111 //
|
|
1112
|