annotate MediaPlayer.cpp @ 2346:37bb43874e59

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