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