annotate MediaPlayer.cpp @ 2319:9987f93d7e1f

stuff.h
author Ritor1
date Thu, 20 Mar 2014 18:44:27 +0600
parents 58be29479e75
children 182effc4b0ee
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
58be29479e75 add files
Ritor1
parents:
diff changeset
122
58be29479e75 add files
Ritor1
parents:
diff changeset
123
58be29479e75 add files
Ritor1
parents:
diff changeset
124 OpenALSoundProvider *provider = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
125
58be29479e75 add files
Ritor1
parents:
diff changeset
126
58be29479e75 add files
Ritor1
parents:
diff changeset
127
58be29479e75 add files
Ritor1
parents:
diff changeset
128 static int av_num_bytes_per_sample(AVSampleFormat sample_fmt)
58be29479e75 add files
Ritor1
parents:
diff changeset
129 {
58be29479e75 add files
Ritor1
parents:
diff changeset
130 switch (sample_fmt)
58be29479e75 add files
Ritor1
parents:
diff changeset
131 {
58be29479e75 add files
Ritor1
parents:
diff changeset
132 case AV_SAMPLE_FMT_U8:
58be29479e75 add files
Ritor1
parents:
diff changeset
133 case AV_SAMPLE_FMT_U8P:
58be29479e75 add files
Ritor1
parents:
diff changeset
134 return 1;
58be29479e75 add files
Ritor1
parents:
diff changeset
135
58be29479e75 add files
Ritor1
parents:
diff changeset
136 case AV_SAMPLE_FMT_S16:
58be29479e75 add files
Ritor1
parents:
diff changeset
137 case AV_SAMPLE_FMT_S16P:
58be29479e75 add files
Ritor1
parents:
diff changeset
138 return 2;
58be29479e75 add files
Ritor1
parents:
diff changeset
139
58be29479e75 add files
Ritor1
parents:
diff changeset
140 case AV_SAMPLE_FMT_S32:
58be29479e75 add files
Ritor1
parents:
diff changeset
141 case AV_SAMPLE_FMT_S32P:
58be29479e75 add files
Ritor1
parents:
diff changeset
142 case AV_SAMPLE_FMT_FLT:
58be29479e75 add files
Ritor1
parents:
diff changeset
143 case AV_SAMPLE_FMT_FLTP:
58be29479e75 add files
Ritor1
parents:
diff changeset
144 return 4;
58be29479e75 add files
Ritor1
parents:
diff changeset
145
58be29479e75 add files
Ritor1
parents:
diff changeset
146 case AV_SAMPLE_FMT_DBL:
58be29479e75 add files
Ritor1
parents:
diff changeset
147 case AV_SAMPLE_FMT_DBLP:
58be29479e75 add files
Ritor1
parents:
diff changeset
148 return 8;
58be29479e75 add files
Ritor1
parents:
diff changeset
149
58be29479e75 add files
Ritor1
parents:
diff changeset
150 default:
58be29479e75 add files
Ritor1
parents:
diff changeset
151 case AV_SAMPLE_FMT_NONE:
58be29479e75 add files
Ritor1
parents:
diff changeset
152 Error("Invalid av sample format: %u", sample_fmt);
58be29479e75 add files
Ritor1
parents:
diff changeset
153 }
58be29479e75 add files
Ritor1
parents:
diff changeset
154 return 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
155 }
58be29479e75 add files
Ritor1
parents:
diff changeset
156
58be29479e75 add files
Ritor1
parents:
diff changeset
157
58be29479e75 add files
Ritor1
parents:
diff changeset
158
58be29479e75 add files
Ritor1
parents:
diff changeset
159 struct AVStreamWrapper
58be29479e75 add files
Ritor1
parents:
diff changeset
160 {
58be29479e75 add files
Ritor1
parents:
diff changeset
161 inline AVStreamWrapper()
58be29479e75 add files
Ritor1
parents:
diff changeset
162 {
58be29479e75 add files
Ritor1
parents:
diff changeset
163 this->type = AVMEDIA_TYPE_UNKNOWN;
58be29479e75 add files
Ritor1
parents:
diff changeset
164 this->stream_idx = -1;
58be29479e75 add files
Ritor1
parents:
diff changeset
165 this->stream = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
166 this->dec = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
167 this->dec_ctx = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
168 }
58be29479e75 add files
Ritor1
parents:
diff changeset
169
58be29479e75 add files
Ritor1
parents:
diff changeset
170 inline void Release()
58be29479e75 add files
Ritor1
parents:
diff changeset
171 {
58be29479e75 add files
Ritor1
parents:
diff changeset
172 type = AVMEDIA_TYPE_UNKNOWN;
58be29479e75 add files
Ritor1
parents:
diff changeset
173 stream_idx = -1;
58be29479e75 add files
Ritor1
parents:
diff changeset
174 stream = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
175 dec = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
176 if (dec_ctx)
58be29479e75 add files
Ritor1
parents:
diff changeset
177 {
58be29479e75 add files
Ritor1
parents:
diff changeset
178 avcodec_close(dec_ctx);
58be29479e75 add files
Ritor1
parents:
diff changeset
179 dec_ctx = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
180 }
58be29479e75 add files
Ritor1
parents:
diff changeset
181 }
58be29479e75 add files
Ritor1
parents:
diff changeset
182
58be29479e75 add files
Ritor1
parents:
diff changeset
183 AVMediaType type;
58be29479e75 add files
Ritor1
parents:
diff changeset
184 int stream_idx;
58be29479e75 add files
Ritor1
parents:
diff changeset
185 AVStream *stream;
58be29479e75 add files
Ritor1
parents:
diff changeset
186 AVCodec *dec;
58be29479e75 add files
Ritor1
parents:
diff changeset
187 AVCodecContext *dec_ctx;
58be29479e75 add files
Ritor1
parents:
diff changeset
188 };
58be29479e75 add files
Ritor1
parents:
diff changeset
189
58be29479e75 add files
Ritor1
parents:
diff changeset
190 struct AVAudioStream: public AVStreamWrapper
58be29479e75 add files
Ritor1
parents:
diff changeset
191 {
58be29479e75 add files
Ritor1
parents:
diff changeset
192 inline AVAudioStream():
58be29479e75 add files
Ritor1
parents:
diff changeset
193 AVStreamWrapper()
58be29479e75 add files
Ritor1
parents:
diff changeset
194 {
58be29479e75 add files
Ritor1
parents:
diff changeset
195 this->bytes_per_sample = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
196 this->bytes_per_second = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
197 }
58be29479e75 add files
Ritor1
parents:
diff changeset
198
58be29479e75 add files
Ritor1
parents:
diff changeset
199 int bytes_per_sample;
58be29479e75 add files
Ritor1
parents:
diff changeset
200 int bytes_per_second;
58be29479e75 add files
Ritor1
parents:
diff changeset
201 };
58be29479e75 add files
Ritor1
parents:
diff changeset
202
58be29479e75 add files
Ritor1
parents:
diff changeset
203 struct AVVideoStream: public AVStreamWrapper
58be29479e75 add files
Ritor1
parents:
diff changeset
204 {
58be29479e75 add files
Ritor1
parents:
diff changeset
205 inline AVVideoStream():
58be29479e75 add files
Ritor1
parents:
diff changeset
206 AVStreamWrapper()
58be29479e75 add files
Ritor1
parents:
diff changeset
207 {
58be29479e75 add files
Ritor1
parents:
diff changeset
208 this->frames_per_second = 0.0;
58be29479e75 add files
Ritor1
parents:
diff changeset
209 }
58be29479e75 add files
Ritor1
parents:
diff changeset
210
58be29479e75 add files
Ritor1
parents:
diff changeset
211 double frames_per_second;
58be29479e75 add files
Ritor1
parents:
diff changeset
212 };
58be29479e75 add files
Ritor1
parents:
diff changeset
213
58be29479e75 add files
Ritor1
parents:
diff changeset
214 static bool av_open_stream(AVFormatContext *format_ctx, AVMediaType type, AVStreamWrapper *out_stream)
58be29479e75 add files
Ritor1
parents:
diff changeset
215 {
58be29479e75 add files
Ritor1
parents:
diff changeset
216 int stream_idx = av_find_best_stream(format_ctx, type, -1, -1, nullptr, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
217 if (stream_idx >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
218 {
58be29479e75 add files
Ritor1
parents:
diff changeset
219 auto stream = format_ctx->streams[stream_idx];
58be29479e75 add files
Ritor1
parents:
diff changeset
220 auto dec_ctx = stream->codec;
58be29479e75 add files
Ritor1
parents:
diff changeset
221 auto dec = avcodec_find_decoder(dec_ctx->codec_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
222 if (dec)
58be29479e75 add files
Ritor1
parents:
diff changeset
223 {
58be29479e75 add files
Ritor1
parents:
diff changeset
224 if (avcodec_open2(dec_ctx, dec, nullptr) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
225 {
58be29479e75 add files
Ritor1
parents:
diff changeset
226 out_stream->type = type;
58be29479e75 add files
Ritor1
parents:
diff changeset
227 out_stream->stream_idx = stream_idx;
58be29479e75 add files
Ritor1
parents:
diff changeset
228 out_stream->stream = stream;
58be29479e75 add files
Ritor1
parents:
diff changeset
229 out_stream->dec = dec;
58be29479e75 add files
Ritor1
parents:
diff changeset
230 out_stream->dec_ctx = dec_ctx;
58be29479e75 add files
Ritor1
parents:
diff changeset
231 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
232 }
58be29479e75 add files
Ritor1
parents:
diff changeset
233 }
58be29479e75 add files
Ritor1
parents:
diff changeset
234 }
58be29479e75 add files
Ritor1
parents:
diff changeset
235 return false;
58be29479e75 add files
Ritor1
parents:
diff changeset
236 }
58be29479e75 add files
Ritor1
parents:
diff changeset
237
58be29479e75 add files
Ritor1
parents:
diff changeset
238 static bool av_open_audio_stream(AVFormatContext *format_ctx, AVAudioStream *out_stream)
58be29479e75 add files
Ritor1
parents:
diff changeset
239 {
58be29479e75 add files
Ritor1
parents:
diff changeset
240 if (!av_open_stream(format_ctx, AVMEDIA_TYPE_AUDIO, out_stream))
58be29479e75 add files
Ritor1
parents:
diff changeset
241 return Error("Audio stream not found"), false;
58be29479e75 add files
Ritor1
parents:
diff changeset
242
58be29479e75 add files
Ritor1
parents:
diff changeset
243 // we support only 2-channel audio for now
58be29479e75 add files
Ritor1
parents:
diff changeset
244 if (out_stream->dec_ctx->channels != 2)
58be29479e75 add files
Ritor1
parents:
diff changeset
245 {
58be29479e75 add files
Ritor1
parents:
diff changeset
246 out_stream->Release();
58be29479e75 add files
Ritor1
parents:
diff changeset
247 return Error("Unsupported number of channels: %u", out_stream->dec_ctx->channels), false;
58be29479e75 add files
Ritor1
parents:
diff changeset
248 }
58be29479e75 add files
Ritor1
parents:
diff changeset
249
58be29479e75 add files
Ritor1
parents:
diff changeset
250 out_stream->bytes_per_sample = av_num_bytes_per_sample(out_stream->dec_ctx->sample_fmt);
58be29479e75 add files
Ritor1
parents:
diff changeset
251 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
252
58be29479e75 add files
Ritor1
parents:
diff changeset
253 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
254 }
58be29479e75 add files
Ritor1
parents:
diff changeset
255
58be29479e75 add files
Ritor1
parents:
diff changeset
256 static bool av_open_video_stream(AVFormatContext *format_ctx, AVVideoStream *out_stream)
58be29479e75 add files
Ritor1
parents:
diff changeset
257 {
58be29479e75 add files
Ritor1
parents:
diff changeset
258 if (!av_open_stream(format_ctx, AVMEDIA_TYPE_VIDEO, out_stream))
58be29479e75 add files
Ritor1
parents:
diff changeset
259 return Error("Video stream not found"), false;
58be29479e75 add files
Ritor1
parents:
diff changeset
260
58be29479e75 add files
Ritor1
parents:
diff changeset
261 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
262 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
263 }
58be29479e75 add files
Ritor1
parents:
diff changeset
264
58be29479e75 add files
Ritor1
parents:
diff changeset
265
58be29479e75 add files
Ritor1
parents:
diff changeset
266
58be29479e75 add files
Ritor1
parents:
diff changeset
267 void InterleaveAudioData(MemoryStream *stream, AVSampleFormat src_format, int num_channels, int num_samples, uint8_t **channels)
58be29479e75 add files
Ritor1
parents:
diff changeset
268 {
58be29479e75 add files
Ritor1
parents:
diff changeset
269 unsigned int bytes_per_sample;
58be29479e75 add files
Ritor1
parents:
diff changeset
270 switch (src_format)
58be29479e75 add files
Ritor1
parents:
diff changeset
271 {
58be29479e75 add files
Ritor1
parents:
diff changeset
272 case AV_SAMPLE_FMT_U8:
58be29479e75 add files
Ritor1
parents:
diff changeset
273 case AV_SAMPLE_FMT_U8P:
58be29479e75 add files
Ritor1
parents:
diff changeset
274 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
275
58be29479e75 add files
Ritor1
parents:
diff changeset
276 case AV_SAMPLE_FMT_S16:
58be29479e75 add files
Ritor1
parents:
diff changeset
277 bytes_per_sample = sizeof(__int16);
58be29479e75 add files
Ritor1
parents:
diff changeset
278 stream->Write(channels[0], num_channels * num_samples * bytes_per_sample);
58be29479e75 add files
Ritor1
parents:
diff changeset
279 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
280
58be29479e75 add files
Ritor1
parents:
diff changeset
281 case AV_SAMPLE_FMT_S16P:
58be29479e75 add files
Ritor1
parents:
diff changeset
282 {
58be29479e75 add files
Ritor1
parents:
diff changeset
283 bytes_per_sample = sizeof(__int16);
58be29479e75 add files
Ritor1
parents:
diff changeset
284 for (int i = 0; i < num_samples; ++i)
58be29479e75 add files
Ritor1
parents:
diff changeset
285 for (int j = 0; j < num_channels; ++j)
58be29479e75 add files
Ritor1
parents:
diff changeset
286 stream->Write(channels[j] + i * bytes_per_sample, bytes_per_sample);
58be29479e75 add files
Ritor1
parents:
diff changeset
287 }
58be29479e75 add files
Ritor1
parents:
diff changeset
288 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
289
58be29479e75 add files
Ritor1
parents:
diff changeset
290 case AV_SAMPLE_FMT_FLT:
58be29479e75 add files
Ritor1
parents:
diff changeset
291 {
58be29479e75 add files
Ritor1
parents:
diff changeset
292 SwrContext *converter = swr_alloc();
58be29479e75 add files
Ritor1
parents:
diff changeset
293 av_opt_set_int(converter, "in_channel_layout", av_get_default_channel_layout(2), 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
294 //av_opt_set_int(converter, "in_sample_rate", sample_ra, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
295 av_opt_set_sample_fmt(converter, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
296
58be29479e75 add files
Ritor1
parents:
diff changeset
297 av_opt_set_int(converter, "out_channel_layout", av_get_default_channel_layout(2), 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
298 //av_opt_set_int(converter, "out_sample_rate", dst_sample_rate, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
299 av_opt_set_sample_fmt(converter, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
300
58be29479e75 add files
Ritor1
parents:
diff changeset
301 if (swr_init(converter) < 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
302 {
58be29479e75 add files
Ritor1
parents:
diff changeset
303 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
304 swr_free(&converter);
58be29479e75 add files
Ritor1
parents:
diff changeset
305 return;
58be29479e75 add files
Ritor1
parents:
diff changeset
306 }
58be29479e75 add files
Ritor1
parents:
diff changeset
307
58be29479e75 add files
Ritor1
parents:
diff changeset
308 uint8_t **dst_channels;
58be29479e75 add files
Ritor1
parents:
diff changeset
309 int dst_linesize[8];
58be29479e75 add files
Ritor1
parents:
diff changeset
310 //int dst_nb_channels = av_get_channel_layout_nb_channels(dst_channel_layout);
58be29479e75 add files
Ritor1
parents:
diff changeset
311 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
312 {
58be29479e75 add files
Ritor1
parents:
diff changeset
313 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
314 swr_free(&converter);
58be29479e75 add files
Ritor1
parents:
diff changeset
315 return;
58be29479e75 add files
Ritor1
parents:
diff changeset
316 }
58be29479e75 add files
Ritor1
parents:
diff changeset
317
58be29479e75 add files
Ritor1
parents:
diff changeset
318 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
319 stream->Write(dst_channels[0], num_channels * num_samples * sizeof(__int16));
58be29479e75 add files
Ritor1
parents:
diff changeset
320 else
58be29479e75 add files
Ritor1
parents:
diff changeset
321 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
322
58be29479e75 add files
Ritor1
parents:
diff changeset
323 av_free(dst_channels[0]);
58be29479e75 add files
Ritor1
parents:
diff changeset
324 swr_free(&converter);
58be29479e75 add files
Ritor1
parents:
diff changeset
325 }
58be29479e75 add files
Ritor1
parents:
diff changeset
326 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
327
58be29479e75 add files
Ritor1
parents:
diff changeset
328 default:
58be29479e75 add files
Ritor1
parents:
diff changeset
329 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
330 //if (Resample(next_frame->avframe, next_frame->avframe->channel_layout, next_frame->avframe->sample_rate,
58be29479e75 add files
Ritor1
parents:
diff changeset
331 // av_get_default_channel_layout(2), next_frame->avframe->sample_rate, AV_SAMPLE_FMT_S16P, resampled_data))
58be29479e75 add files
Ritor1
parents:
diff changeset
332 }
58be29479e75 add files
Ritor1
parents:
diff changeset
333 }
58be29479e75 add files
Ritor1
parents:
diff changeset
334
58be29479e75 add files
Ritor1
parents:
diff changeset
335
58be29479e75 add files
Ritor1
parents:
diff changeset
336
58be29479e75 add files
Ritor1
parents:
diff changeset
337 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
338 {
58be29479e75 add files
Ritor1
parents:
diff changeset
339 volatile int decoded = false;
58be29479e75 add files
Ritor1
parents:
diff changeset
340 do
58be29479e75 add files
Ritor1
parents:
diff changeset
341 {
58be29479e75 add files
Ritor1
parents:
diff changeset
342 if (avcodec_decode_audio4(dec_ctx, avframe, (int *)&decoded, avpacket) < 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
343 {
58be29479e75 add files
Ritor1
parents:
diff changeset
344 log("Cannot decode audio frame\n");
58be29479e75 add files
Ritor1
parents:
diff changeset
345 return false;
58be29479e75 add files
Ritor1
parents:
diff changeset
346 }
58be29479e75 add files
Ritor1
parents:
diff changeset
347
58be29479e75 add files
Ritor1
parents:
diff changeset
348 if (!decoded)
58be29479e75 add files
Ritor1
parents:
diff changeset
349 log("Cannot decode audio frame in one piece\n");
58be29479e75 add files
Ritor1
parents:
diff changeset
350 } while (!decoded);
58be29479e75 add files
Ritor1
parents:
diff changeset
351
58be29479e75 add files
Ritor1
parents:
diff changeset
352 switch (dec_ctx->codec_id)
58be29479e75 add files
Ritor1
parents:
diff changeset
353 {
58be29479e75 add files
Ritor1
parents:
diff changeset
354 case AV_CODEC_ID_BINKAUDIO_RDFT:
58be29479e75 add files
Ritor1
parents:
diff changeset
355 {//pts samples dpts
58be29479e75 add files
Ritor1
parents:
diff changeset
356 // 0 960
58be29479e75 add files
Ritor1
parents:
diff changeset
357 //17280 960 17280 18x960
58be29479e75 add files
Ritor1
parents:
diff changeset
358 //18240 960 960 1x960
58be29479e75 add files
Ritor1
parents:
diff changeset
359 //20160 960 1920 2x960
58be29479e75 add files
Ritor1
parents:
diff changeset
360 //21120 960 960 1x960
58be29479e75 add files
Ritor1
parents:
diff changeset
361 //23040 960 1920 2x960
58be29479e75 add files
Ritor1
parents:
diff changeset
362 static int bink_next_pts = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
363
58be29479e75 add files
Ritor1
parents:
diff changeset
364 // there's a gap in the sound - fill empty samples in
58be29479e75 add files
Ritor1
parents:
diff changeset
365 if (bink_next_pts < avpacket->pts)
58be29479e75 add files
Ritor1
parents:
diff changeset
366 {
58be29479e75 add files
Ritor1
parents:
diff changeset
367 short silence[1024];
58be29479e75 add files
Ritor1
parents:
diff changeset
368 memset(silence, 0, sizeof(silence));
58be29479e75 add files
Ritor1
parents:
diff changeset
369
58be29479e75 add files
Ritor1
parents:
diff changeset
370 int samples_to_fill = /*dec_ctx->channels * */(avpacket->pts - bink_next_pts);
58be29479e75 add files
Ritor1
parents:
diff changeset
371 while (samples_to_fill > 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
372 {
58be29479e75 add files
Ritor1
parents:
diff changeset
373 int samples_to_fill_this_step = samples_to_fill >= 1024 ? 1024 : samples_to_fill;
58be29479e75 add files
Ritor1
parents:
diff changeset
374 out_audio_data->Write(silence, samples_to_fill_this_step * sizeof(short));
58be29479e75 add files
Ritor1
parents:
diff changeset
375
58be29479e75 add files
Ritor1
parents:
diff changeset
376 samples_to_fill -= samples_to_fill_this_step;
58be29479e75 add files
Ritor1
parents:
diff changeset
377 }
58be29479e75 add files
Ritor1
parents:
diff changeset
378 }
58be29479e75 add files
Ritor1
parents:
diff changeset
379
58be29479e75 add files
Ritor1
parents:
diff changeset
380 bink_next_pts = avpacket->pts + /*dec_ctx->channels * */avframe->nb_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
381 }
58be29479e75 add files
Ritor1
parents:
diff changeset
382 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
383 /*
58be29479e75 add files
Ritor1
parents:
diff changeset
384 case AV_CODEC_ID_SMACKAUDIO:
58be29479e75 add files
Ritor1
parents:
diff changeset
385 {
58be29479e75 add files
Ritor1
parents:
diff changeset
386 static int smack_debug_next_audio_time = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
387 if (smack_debug_next_audio_time != packet->pts)
58be29479e75 add files
Ritor1
parents:
diff changeset
388 {
58be29479e75 add files
Ritor1
parents:
diff changeset
389 Error("There's a gap in the sound before frame %u\n", num_audio_frames);
58be29479e75 add files
Ritor1
parents:
diff changeset
390 __debugbreak(); // there's a gap in the sound
58be29479e75 add files
Ritor1
parents:
diff changeset
391 }
58be29479e75 add files
Ritor1
parents:
diff changeset
392
58be29479e75 add files
Ritor1
parents:
diff changeset
393 int num_actual_data_channels = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
394 switch (dec_ctx->sample_fmt)
58be29479e75 add files
Ritor1
parents:
diff changeset
395 {
58be29479e75 add files
Ritor1
parents:
diff changeset
396 case AV_SAMPLE_FMT_U8:
58be29479e75 add files
Ritor1
parents:
diff changeset
397 case AV_SAMPLE_FMT_S16:
58be29479e75 add files
Ritor1
parents:
diff changeset
398 case AV_SAMPLE_FMT_S32:
58be29479e75 add files
Ritor1
parents:
diff changeset
399 case AV_SAMPLE_FMT_FLT:
58be29479e75 add files
Ritor1
parents:
diff changeset
400 case AV_SAMPLE_FMT_DBL:
58be29479e75 add files
Ritor1
parents:
diff changeset
401 num_actual_data_channels = 1;
58be29479e75 add files
Ritor1
parents:
diff changeset
402 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
403
58be29479e75 add files
Ritor1
parents:
diff changeset
404 case AV_SAMPLE_FMT_U8P:
58be29479e75 add files
Ritor1
parents:
diff changeset
405 case AV_SAMPLE_FMT_S16P:
58be29479e75 add files
Ritor1
parents:
diff changeset
406 case AV_SAMPLE_FMT_S32P:
58be29479e75 add files
Ritor1
parents:
diff changeset
407 case AV_SAMPLE_FMT_FLTP:
58be29479e75 add files
Ritor1
parents:
diff changeset
408 case AV_SAMPLE_FMT_DBLP:
58be29479e75 add files
Ritor1
parents:
diff changeset
409 num_actual_data_channels = dec_ctx->channels;
58be29479e75 add files
Ritor1
parents:
diff changeset
410 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
411
58be29479e75 add files
Ritor1
parents:
diff changeset
412 default:
58be29479e75 add files
Ritor1
parents:
diff changeset
413 case AV_SAMPLE_FMT_NONE:
58be29479e75 add files
Ritor1
parents:
diff changeset
414 case AV_SAMPLE_FMT_NB:
58be29479e75 add files
Ritor1
parents:
diff changeset
415 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
416 }
58be29479e75 add files
Ritor1
parents:
diff changeset
417
58be29479e75 add files
Ritor1
parents:
diff changeset
418 smack_debug_next_audio_time += dec_ctx->channels * frame->nb_samples * bytes_per_sample;
58be29479e75 add files
Ritor1
parents:
diff changeset
419 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
420 "Smack audio size mismatch in frame %u in %s\n", audio_num_read_frames, movie_filename);
58be29479e75 add files
Ritor1
parents:
diff changeset
421
58be29479e75 add files
Ritor1
parents:
diff changeset
422 frame->play_time = (double)frame->avpacket->pts / (double)audio.bytes_per_second;
58be29479e75 add files
Ritor1
parents:
diff changeset
423 }
58be29479e75 add files
Ritor1
parents:
diff changeset
424 break;
58be29479e75 add files
Ritor1
parents:
diff changeset
425
58be29479e75 add files
Ritor1
parents:
diff changeset
426 case AV_CODEC_ID_MP3:
58be29479e75 add files
Ritor1
parents:
diff changeset
427 {
58be29479e75 add files
Ritor1
parents:
diff changeset
428 static int mp3_samples_decoded_so_far = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
429 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
430 frame->play_time = (double)mp3_samples_decoded_so_far / (double)audio.dec_ctx->sample_rate;
58be29479e75 add files
Ritor1
parents:
diff changeset
431
58be29479e75 add files
Ritor1
parents:
diff changeset
432 mp3_samples_decoded_so_far += frame->avframe->nb_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
433 Assert(mp3_prev_samples_count == frame->avframe->nb_samples,
58be29479e75 add files
Ritor1
parents:
diff changeset
434 "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
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 default:
58be29479e75 add files
Ritor1
parents:
diff changeset
439 {
58be29479e75 add files
Ritor1
parents:
diff changeset
440 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
441 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
442 double play_length = frame->avframe->nb_samples / samples_per_second;
58be29479e75 add files
Ritor1
parents:
diff changeset
443 frame->play_time = (double)frame->avpacket->pts / samples_per_second;
58be29479e75 add files
Ritor1
parents:
diff changeset
444 }
58be29479e75 add files
Ritor1
parents:
diff changeset
445 break;*/
58be29479e75 add files
Ritor1
parents:
diff changeset
446 }
58be29479e75 add files
Ritor1
parents:
diff changeset
447
58be29479e75 add files
Ritor1
parents:
diff changeset
448 if (!avframe->channel_layout)
58be29479e75 add files
Ritor1
parents:
diff changeset
449 {
58be29479e75 add files
Ritor1
parents:
diff changeset
450 log("Audio channel layout not specified, rolling back to default\n");
58be29479e75 add files
Ritor1
parents:
diff changeset
451 avframe->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
58be29479e75 add files
Ritor1
parents:
diff changeset
452 }
58be29479e75 add files
Ritor1
parents:
diff changeset
453
58be29479e75 add files
Ritor1
parents:
diff changeset
454 *out_num_audio_samples = dec_ctx->channels * avframe->nb_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
455 InterleaveAudioData(out_audio_data, dec_ctx->sample_fmt,
58be29479e75 add files
Ritor1
parents:
diff changeset
456 dec_ctx->channels, avframe->nb_samples, avframe->data);
58be29479e75 add files
Ritor1
parents:
diff changeset
457 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
458 }
58be29479e75 add files
Ritor1
parents:
diff changeset
459
58be29479e75 add files
Ritor1
parents:
diff changeset
460
58be29479e75 add files
Ritor1
parents:
diff changeset
461 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
462 {
58be29479e75 add files
Ritor1
parents:
diff changeset
463 out_audio_stream->Reset();
58be29479e75 add files
Ritor1
parents:
diff changeset
464
58be29479e75 add files
Ritor1
parents:
diff changeset
465 AVFrame *frame = avcodec_alloc_frame();
58be29479e75 add files
Ritor1
parents:
diff changeset
466 AVPacket *packet = new AVPacket;
58be29479e75 add files
Ritor1
parents:
diff changeset
467 av_init_packet(packet);
58be29479e75 add files
Ritor1
parents:
diff changeset
468
58be29479e75 add files
Ritor1
parents:
diff changeset
469 int num_audio_frames = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
470 int num_audio_samples = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
471 while (av_read_frame(format_ctx, packet) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
472 {
58be29479e75 add files
Ritor1
parents:
diff changeset
473 if (packet->stream_index != audio_stream_idx)
58be29479e75 add files
Ritor1
parents:
diff changeset
474 {
58be29479e75 add files
Ritor1
parents:
diff changeset
475 //log("Suspicious stream id %u in %s", packet->stream_index, filenamea);
58be29479e75 add files
Ritor1
parents:
diff changeset
476 continue;
58be29479e75 add files
Ritor1
parents:
diff changeset
477 }
58be29479e75 add files
Ritor1
parents:
diff changeset
478
58be29479e75 add files
Ritor1
parents:
diff changeset
479 int num_samples_decoded;
58be29479e75 add files
Ritor1
parents:
diff changeset
480 DecodeAudioFrame(dec_ctx, packet, frame, out_audio_stream, &num_samples_decoded);
58be29479e75 add files
Ritor1
parents:
diff changeset
481
58be29479e75 add files
Ritor1
parents:
diff changeset
482 num_audio_samples += num_samples_decoded;
58be29479e75 add files
Ritor1
parents:
diff changeset
483 num_audio_frames++;
58be29479e75 add files
Ritor1
parents:
diff changeset
484 }
58be29479e75 add files
Ritor1
parents:
diff changeset
485 *out_num_audio_frames = num_audio_frames;
58be29479e75 add files
Ritor1
parents:
diff changeset
486 *out_num_audio_samples = num_audio_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
487
58be29479e75 add files
Ritor1
parents:
diff changeset
488 avcodec_free_frame(&frame);
58be29479e75 add files
Ritor1
parents:
diff changeset
489 delete packet;
58be29479e75 add files
Ritor1
parents:
diff changeset
490
58be29479e75 add files
Ritor1
parents:
diff changeset
491 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
492 }
58be29479e75 add files
Ritor1
parents:
diff changeset
493
58be29479e75 add files
Ritor1
parents:
diff changeset
494
58be29479e75 add files
Ritor1
parents:
diff changeset
495 class Track: public Media::ITrack
58be29479e75 add files
Ritor1
parents:
diff changeset
496 {
58be29479e75 add files
Ritor1
parents:
diff changeset
497 public:
58be29479e75 add files
Ritor1
parents:
diff changeset
498 inline Track()
58be29479e75 add files
Ritor1
parents:
diff changeset
499 {
58be29479e75 add files
Ritor1
parents:
diff changeset
500 this->format_ctx = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
501 this->audio_num_samples = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
502 }
58be29479e75 add files
Ritor1
parents:
diff changeset
503
58be29479e75 add files
Ritor1
parents:
diff changeset
504 void Release()
58be29479e75 add files
Ritor1
parents:
diff changeset
505 {
58be29479e75 add files
Ritor1
parents:
diff changeset
506 ReleaseAvcodec();
58be29479e75 add files
Ritor1
parents:
diff changeset
507 }
58be29479e75 add files
Ritor1
parents:
diff changeset
508
58be29479e75 add files
Ritor1
parents:
diff changeset
509 void ReleaseAvcodec()
58be29479e75 add files
Ritor1
parents:
diff changeset
510 {
58be29479e75 add files
Ritor1
parents:
diff changeset
511 audio.Release();
58be29479e75 add files
Ritor1
parents:
diff changeset
512 if (format_ctx)
58be29479e75 add files
Ritor1
parents:
diff changeset
513 {
58be29479e75 add files
Ritor1
parents:
diff changeset
514 av_close_input_file(format_ctx);
58be29479e75 add files
Ritor1
parents:
diff changeset
515 format_ctx = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
516 }
58be29479e75 add files
Ritor1
parents:
diff changeset
517 }
58be29479e75 add files
Ritor1
parents:
diff changeset
518
58be29479e75 add files
Ritor1
parents:
diff changeset
519 bool Load(const wchar_t *filename)
58be29479e75 add files
Ritor1
parents:
diff changeset
520 {
58be29479e75 add files
Ritor1
parents:
diff changeset
521 char filenamea[1024];
58be29479e75 add files
Ritor1
parents:
diff changeset
522 sprintf(filenamea, "%S", filename);
58be29479e75 add files
Ritor1
parents:
diff changeset
523
58be29479e75 add files
Ritor1
parents:
diff changeset
524 if (avformat_open_input(&format_ctx, filenamea, nullptr, nullptr) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
525 {
58be29479e75 add files
Ritor1
parents:
diff changeset
526 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
527 {
58be29479e75 add files
Ritor1
parents:
diff changeset
528 av_dump_format(format_ctx, 0, filenamea, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
529
58be29479e75 add files
Ritor1
parents:
diff changeset
530 if (!av_open_audio_stream(format_ctx, &audio))
58be29479e75 add files
Ritor1
parents:
diff changeset
531 {
58be29479e75 add files
Ritor1
parents:
diff changeset
532 Error("Cannot open strack: %s", filenamea);
58be29479e75 add files
Ritor1
parents:
diff changeset
533 return Release(), false;
58be29479e75 add files
Ritor1
parents:
diff changeset
534 }
58be29479e75 add files
Ritor1
parents:
diff changeset
535
58be29479e75 add files
Ritor1
parents:
diff changeset
536 MemoryStream audio_plain_data;
58be29479e75 add files
Ritor1
parents:
diff changeset
537 int num_audio_frames;
58be29479e75 add files
Ritor1
parents:
diff changeset
538 int num_audio_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
539 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
540 {
58be29479e75 add files
Ritor1
parents:
diff changeset
541 /*#ifdef _DEBUG
58be29479e75 add files
Ritor1
parents:
diff changeset
542 char debug_filename[1024];
58be29479e75 add files
Ritor1
parents:
diff changeset
543 sprintf(debug_filename, "%s.wav", filenamea);
58be29479e75 add files
Ritor1
parents:
diff changeset
544 FILE *wav = fopen(debug_filename, "w+b");
58be29479e75 add files
Ritor1
parents:
diff changeset
545
58be29479e75 add files
Ritor1
parents:
diff changeset
546 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
547 write_wav_header(wav, audio.dec_ctx->channels, audio.dec_ctx->sample_rate, audio.bytes_per_sample);
58be29479e75 add files
Ritor1
parents:
diff changeset
548
58be29479e75 add files
Ritor1
parents:
diff changeset
549 fwrite(audio_plain_data.Ptr(), audio_plain_data.Current(), 1, wav);
58be29479e75 add files
Ritor1
parents:
diff changeset
550
58be29479e75 add files
Ritor1
parents:
diff changeset
551 extern void fix_wav_header(FILE *wav, int wav_bytes_in_stream);
58be29479e75 add files
Ritor1
parents:
diff changeset
552 fix_wav_header(wav, audio_plain_data.Current());
58be29479e75 add files
Ritor1
parents:
diff changeset
553 #endif*/
58be29479e75 add files
Ritor1
parents:
diff changeset
554
58be29479e75 add files
Ritor1
parents:
diff changeset
555 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
556
58be29479e75 add files
Ritor1
parents:
diff changeset
557 ReleaseAvcodec();
58be29479e75 add files
Ritor1
parents:
diff changeset
558 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
559 }
58be29479e75 add files
Ritor1
parents:
diff changeset
560 }
58be29479e75 add files
Ritor1
parents:
diff changeset
561 Release();
58be29479e75 add files
Ritor1
parents:
diff changeset
562 }
58be29479e75 add files
Ritor1
parents:
diff changeset
563 return false;
58be29479e75 add files
Ritor1
parents:
diff changeset
564 }
58be29479e75 add files
Ritor1
parents:
diff changeset
565
58be29479e75 add files
Ritor1
parents:
diff changeset
566 virtual void Play(bool loop)
58be29479e75 add files
Ritor1
parents:
diff changeset
567 {
58be29479e75 add files
Ritor1
parents:
diff changeset
568 provider->PlayTrack16(device_buffer, loop);
58be29479e75 add files
Ritor1
parents:
diff changeset
569 }
58be29479e75 add files
Ritor1
parents:
diff changeset
570
58be29479e75 add files
Ritor1
parents:
diff changeset
571
58be29479e75 add files
Ritor1
parents:
diff changeset
572 protected:
58be29479e75 add files
Ritor1
parents:
diff changeset
573 AVFormatContext *format_ctx;
58be29479e75 add files
Ritor1
parents:
diff changeset
574 AVAudioStream audio;
58be29479e75 add files
Ritor1
parents:
diff changeset
575 int audio_num_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
576
58be29479e75 add files
Ritor1
parents:
diff changeset
577 OpenALSoundProvider::TrackBuffer *device_buffer;
58be29479e75 add files
Ritor1
parents:
diff changeset
578 };
58be29479e75 add files
Ritor1
parents:
diff changeset
579
58be29479e75 add files
Ritor1
parents:
diff changeset
580
58be29479e75 add files
Ritor1
parents:
diff changeset
581
58be29479e75 add files
Ritor1
parents:
diff changeset
582 class Movie: public Media::IMovie
58be29479e75 add files
Ritor1
parents:
diff changeset
583 {
58be29479e75 add files
Ritor1
parents:
diff changeset
584 public:
58be29479e75 add files
Ritor1
parents:
diff changeset
585 inline Movie()
58be29479e75 add files
Ritor1
parents:
diff changeset
586 {
58be29479e75 add files
Ritor1
parents:
diff changeset
587 this->movie_filename[0] = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
588 this->width = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
589 this->height = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
590 this->format_ctx = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
591 this->end_of_file = false;
58be29479e75 add files
Ritor1
parents:
diff changeset
592 this->playback_time = 0.0;
58be29479e75 add files
Ritor1
parents:
diff changeset
593
58be29479e75 add files
Ritor1
parents:
diff changeset
594 this->num_audio_frames = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
595 this->num_audio_samples = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
596
58be29479e75 add files
Ritor1
parents:
diff changeset
597 this->last_resampled_frame_num = -1;
58be29479e75 add files
Ritor1
parents:
diff changeset
598 memset(last_resampled_frame_data, 0, sizeof(last_resampled_frame_data));
58be29479e75 add files
Ritor1
parents:
diff changeset
599 memset(last_resampled_frame_linesize, 0, sizeof(last_resampled_frame_linesize));
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 inline void Release()
58be29479e75 add files
Ritor1
parents:
diff changeset
604 {
58be29479e75 add files
Ritor1
parents:
diff changeset
605 ReleaseAVCodec();
58be29479e75 add files
Ritor1
parents:
diff changeset
606 }
58be29479e75 add files
Ritor1
parents:
diff changeset
607
58be29479e75 add files
Ritor1
parents:
diff changeset
608 inline void ReleaseAVCodec()
58be29479e75 add files
Ritor1
parents:
diff changeset
609 {
58be29479e75 add files
Ritor1
parents:
diff changeset
610 audio.Release();
58be29479e75 add files
Ritor1
parents:
diff changeset
611 video.Release();
58be29479e75 add files
Ritor1
parents:
diff changeset
612 if (format_ctx)
58be29479e75 add files
Ritor1
parents:
diff changeset
613 {
58be29479e75 add files
Ritor1
parents:
diff changeset
614 av_close_input_file(format_ctx);
58be29479e75 add files
Ritor1
parents:
diff changeset
615 format_ctx = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
616 }
58be29479e75 add files
Ritor1
parents:
diff changeset
617 }
58be29479e75 add files
Ritor1
parents:
diff changeset
618
58be29479e75 add files
Ritor1
parents:
diff changeset
619 bool Load(const wchar_t *filename, int dst_width, int dst_height, int cache_ms)
58be29479e75 add files
Ritor1
parents:
diff changeset
620 {
58be29479e75 add files
Ritor1
parents:
diff changeset
621 char filenamea[1024];
58be29479e75 add files
Ritor1
parents:
diff changeset
622 sprintf(filenamea, "%S", filename);
58be29479e75 add files
Ritor1
parents:
diff changeset
623 sprintf(movie_filename, "%S", filename);
58be29479e75 add files
Ritor1
parents:
diff changeset
624
58be29479e75 add files
Ritor1
parents:
diff changeset
625 width = dst_width;
58be29479e75 add files
Ritor1
parents:
diff changeset
626 height = dst_height;
58be29479e75 add files
Ritor1
parents:
diff changeset
627 if (avformat_open_input(&format_ctx, filenamea, nullptr, nullptr) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
628 {
58be29479e75 add files
Ritor1
parents:
diff changeset
629 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
630 {
58be29479e75 add files
Ritor1
parents:
diff changeset
631 av_dump_format(format_ctx, 0, filenamea, 0);
58be29479e75 add files
Ritor1
parents:
diff changeset
632
58be29479e75 add files
Ritor1
parents:
diff changeset
633 if (!av_open_audio_stream(format_ctx, &audio))
58be29479e75 add files
Ritor1
parents:
diff changeset
634 {
58be29479e75 add files
Ritor1
parents:
diff changeset
635 Error("Cannot open audio stream: %s", filenamea);
58be29479e75 add files
Ritor1
parents:
diff changeset
636 return Release(), false;
58be29479e75 add files
Ritor1
parents:
diff changeset
637 }
58be29479e75 add files
Ritor1
parents:
diff changeset
638
58be29479e75 add files
Ritor1
parents:
diff changeset
639 if (!av_open_video_stream(format_ctx, &video))
58be29479e75 add files
Ritor1
parents:
diff changeset
640 {
58be29479e75 add files
Ritor1
parents:
diff changeset
641 Error("Cannot open video stream: %s", filenamea);
58be29479e75 add files
Ritor1
parents:
diff changeset
642 return Release(), false;
58be29479e75 add files
Ritor1
parents:
diff changeset
643 }
58be29479e75 add files
Ritor1
parents:
diff changeset
644
58be29479e75 add files
Ritor1
parents:
diff changeset
645 decoding_packet = new AVPacket;
58be29479e75 add files
Ritor1
parents:
diff changeset
646 av_init_packet(decoding_packet);
58be29479e75 add files
Ritor1
parents:
diff changeset
647
58be29479e75 add files
Ritor1
parents:
diff changeset
648 decoding_frame = avcodec_alloc_frame();
58be29479e75 add files
Ritor1
parents:
diff changeset
649
58be29479e75 add files
Ritor1
parents:
diff changeset
650 audio_data_in_device = provider->CreateStreamingTrack16(audio.dec_ctx->channels, audio.dec_ctx->sample_rate, 2);
58be29479e75 add files
Ritor1
parents:
diff changeset
651
58be29479e75 add files
Ritor1
parents:
diff changeset
652 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
653 }
58be29479e75 add files
Ritor1
parents:
diff changeset
654 }
58be29479e75 add files
Ritor1
parents:
diff changeset
655 return false;
58be29479e75 add files
Ritor1
parents:
diff changeset
656 }
58be29479e75 add files
Ritor1
parents:
diff changeset
657
58be29479e75 add files
Ritor1
parents:
diff changeset
658 virtual void Play()
58be29479e75 add files
Ritor1
parents:
diff changeset
659 {
58be29479e75 add files
Ritor1
parents:
diff changeset
660 }
58be29479e75 add files
Ritor1
parents:
diff changeset
661
2319
9987f93d7e1f stuff.h
Ritor1
parents: 2315
diff changeset
662 virtual void GetNextFrame(double dt, void *dst_surface)// рисует сразу на экран
2315
58be29479e75 add files
Ritor1
parents:
diff changeset
663 {
2319
9987f93d7e1f stuff.h
Ritor1
parents: 2315
diff changeset
664 playback_time += dt;//изменение времени
2315
58be29479e75 add files
Ritor1
parents:
diff changeset
665
58be29479e75 add files
Ritor1
parents:
diff changeset
666 AVPacket *avpacket = decoding_packet;
58be29479e75 add files
Ritor1
parents:
diff changeset
667 AVFrame *avframe = decoding_frame;
58be29479e75 add files
Ritor1
parents:
diff changeset
668 avcodec_get_frame_defaults(avframe);
58be29479e75 add files
Ritor1
parents:
diff changeset
669
58be29479e75 add files
Ritor1
parents:
diff changeset
670 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
671 if (last_resampled_frame_num == desired_frame_number)
58be29479e75 add files
Ritor1
parents:
diff changeset
672 {
58be29479e75 add files
Ritor1
parents:
diff changeset
673 memcpy(dst_surface, last_resampled_frame_data[0], height * last_resampled_frame_linesize[0]);
58be29479e75 add files
Ritor1
parents:
diff changeset
674 return;
58be29479e75 add files
Ritor1
parents:
diff changeset
675 }
58be29479e75 add files
Ritor1
parents:
diff changeset
676
58be29479e75 add files
Ritor1
parents:
diff changeset
677 volatile int decoded = false;
58be29479e75 add files
Ritor1
parents:
diff changeset
678 do
58be29479e75 add files
Ritor1
parents:
diff changeset
679 {
2319
9987f93d7e1f stuff.h
Ritor1
parents: 2315
diff changeset
680 if (av_read_frame(format_ctx, avpacket) < 0) //воспроизведение завершено
2315
58be29479e75 add files
Ritor1
parents:
diff changeset
681 {
58be29479e75 add files
Ritor1
parents:
diff changeset
682 // probably movie is finished
58be29479e75 add files
Ritor1
parents:
diff changeset
683 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
684 }
58be29479e75 add files
Ritor1
parents:
diff changeset
685
58be29479e75 add files
Ritor1
parents:
diff changeset
686 // audio packet - queue into playing
58be29479e75 add files
Ritor1
parents:
diff changeset
687 if (avpacket->stream_index == audio.stream_idx)
58be29479e75 add files
Ritor1
parents:
diff changeset
688 {
58be29479e75 add files
Ritor1
parents:
diff changeset
689 MemoryStream audio_data;
58be29479e75 add files
Ritor1
parents:
diff changeset
690 if (DecodeAudioFrame(audio.dec_ctx, avpacket, avframe, &audio_data, &num_audio_samples))
58be29479e75 add files
Ritor1
parents:
diff changeset
691 provider->Stream16(audio_data_in_device, num_audio_samples, audio_data.Ptr());
58be29479e75 add files
Ritor1
parents:
diff changeset
692 }
58be29479e75 add files
Ritor1
parents:
diff changeset
693 // video packet - decode & maybe show
58be29479e75 add files
Ritor1
parents:
diff changeset
694 else if (avpacket->stream_index == video.stream_idx)
58be29479e75 add files
Ritor1
parents:
diff changeset
695 {
58be29479e75 add files
Ritor1
parents:
diff changeset
696 do
58be29479e75 add files
Ritor1
parents:
diff changeset
697 {
58be29479e75 add files
Ritor1
parents:
diff changeset
698 if (avcodec_decode_video2(video.dec_ctx, avframe, (int *)&decoded, avpacket) < 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
699 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
700 } while (!decoded);
58be29479e75 add files
Ritor1
parents:
diff changeset
701 }
58be29479e75 add files
Ritor1
parents:
diff changeset
702
58be29479e75 add files
Ritor1
parents:
diff changeset
703 } while (avpacket->stream_index != video.stream_idx ||
58be29479e75 add files
Ritor1
parents:
diff changeset
704 avpacket->pts != desired_frame_number);
58be29479e75 add files
Ritor1
parents:
diff changeset
705
58be29479e75 add files
Ritor1
parents:
diff changeset
706 if (decoded)
58be29479e75 add files
Ritor1
parents:
diff changeset
707 {
58be29479e75 add files
Ritor1
parents:
diff changeset
708 if (last_resampled_frame_data[0])
58be29479e75 add files
Ritor1
parents:
diff changeset
709 av_freep(&last_resampled_frame_data[0]);
58be29479e75 add files
Ritor1
parents:
diff changeset
710
58be29479e75 add files
Ritor1
parents:
diff changeset
711 AVPixelFormat rescaled_format = AV_PIX_FMT_RGB32;
58be29479e75 add files
Ritor1
parents:
diff changeset
712 uint8_t *rescaled_data[4] = {nullptr, nullptr, nullptr, nullptr};
58be29479e75 add files
Ritor1
parents:
diff changeset
713 int rescaled_linesize[4] = {0, 0, 0, 0};
58be29479e75 add files
Ritor1
parents:
diff changeset
714 if (av_image_alloc(rescaled_data, rescaled_linesize, width, height, rescaled_format, 1) >= 0)
58be29479e75 add files
Ritor1
parents:
diff changeset
715 {
58be29479e75 add files
Ritor1
parents:
diff changeset
716 SwsContext *converter = sws_getContext(avframe->width, avframe->height, (AVPixelFormat)avframe->format,
58be29479e75 add files
Ritor1
parents:
diff changeset
717 width, height, rescaled_format,
58be29479e75 add files
Ritor1
parents:
diff changeset
718 SWS_BICUBIC, nullptr, nullptr, nullptr);
58be29479e75 add files
Ritor1
parents:
diff changeset
719 sws_scale(converter, avframe->data, avframe->linesize, 0, avframe->height, rescaled_data, rescaled_linesize);
58be29479e75 add files
Ritor1
parents:
diff changeset
720 sws_freeContext(converter);
58be29479e75 add files
Ritor1
parents:
diff changeset
721
58be29479e75 add files
Ritor1
parents:
diff changeset
722 memcpy(dst_surface, rescaled_data[0], height * rescaled_linesize[0]);
58be29479e75 add files
Ritor1
parents:
diff changeset
723
58be29479e75 add files
Ritor1
parents:
diff changeset
724 last_resampled_frame_num = desired_frame_number;
58be29479e75 add files
Ritor1
parents:
diff changeset
725 memcpy(last_resampled_frame_data, rescaled_data, sizeof(rescaled_data));
58be29479e75 add files
Ritor1
parents:
diff changeset
726 memcpy(last_resampled_frame_linesize, rescaled_linesize, sizeof(rescaled_linesize));
58be29479e75 add files
Ritor1
parents:
diff changeset
727 }
58be29479e75 add files
Ritor1
parents:
diff changeset
728 }
58be29479e75 add files
Ritor1
parents:
diff changeset
729 else
58be29479e75 add files
Ritor1
parents:
diff changeset
730 memset(dst_surface, 0, width * height * 4);
58be29479e75 add files
Ritor1
parents:
diff changeset
731 }
58be29479e75 add files
Ritor1
parents:
diff changeset
732
58be29479e75 add files
Ritor1
parents:
diff changeset
733 protected:
58be29479e75 add files
Ritor1
parents:
diff changeset
734 char movie_filename[256];
58be29479e75 add files
Ritor1
parents:
diff changeset
735 int width;
58be29479e75 add files
Ritor1
parents:
diff changeset
736 int height;
58be29479e75 add files
Ritor1
parents:
diff changeset
737 AVFormatContext *format_ctx;
58be29479e75 add files
Ritor1
parents:
diff changeset
738 double playback_time;
58be29479e75 add files
Ritor1
parents:
diff changeset
739 bool end_of_file;
58be29479e75 add files
Ritor1
parents:
diff changeset
740
58be29479e75 add files
Ritor1
parents:
diff changeset
741 AVPacket *decoding_packet;
58be29479e75 add files
Ritor1
parents:
diff changeset
742 AVFrame *decoding_frame;
58be29479e75 add files
Ritor1
parents:
diff changeset
743
58be29479e75 add files
Ritor1
parents:
diff changeset
744 AVAudioStream audio;
58be29479e75 add files
Ritor1
parents:
diff changeset
745 int num_audio_frames;
58be29479e75 add files
Ritor1
parents:
diff changeset
746 int num_audio_samples;
58be29479e75 add files
Ritor1
parents:
diff changeset
747 OpenALSoundProvider::StreamingTrackBuffer *audio_data_in_device;
58be29479e75 add files
Ritor1
parents:
diff changeset
748
58be29479e75 add files
Ritor1
parents:
diff changeset
749 AVVideoStream video;
58be29479e75 add files
Ritor1
parents:
diff changeset
750 int last_resampled_frame_num;
58be29479e75 add files
Ritor1
parents:
diff changeset
751 uint8_t *last_resampled_frame_data[4];
58be29479e75 add files
Ritor1
parents:
diff changeset
752 int last_resampled_frame_linesize[4];
58be29479e75 add files
Ritor1
parents:
diff changeset
753 };
58be29479e75 add files
Ritor1
parents:
diff changeset
754
58be29479e75 add files
Ritor1
parents:
diff changeset
755
58be29479e75 add files
Ritor1
parents:
diff changeset
756 ITrack *Player::LoadTrack(const wchar_t *filename)
58be29479e75 add files
Ritor1
parents:
diff changeset
757 {
58be29479e75 add files
Ritor1
parents:
diff changeset
758 auto track = new Track;
58be29479e75 add files
Ritor1
parents:
diff changeset
759 if (!track->Load(filename))
58be29479e75 add files
Ritor1
parents:
diff changeset
760 {
58be29479e75 add files
Ritor1
parents:
diff changeset
761 delete track;
58be29479e75 add files
Ritor1
parents:
diff changeset
762 track = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
763 }
58be29479e75 add files
Ritor1
parents:
diff changeset
764 return track;
58be29479e75 add files
Ritor1
parents:
diff changeset
765 }
58be29479e75 add files
Ritor1
parents:
diff changeset
766
58be29479e75 add files
Ritor1
parents:
diff changeset
767
58be29479e75 add files
Ritor1
parents:
diff changeset
768 IMovie *Player::LoadMovie(const wchar_t *filename, int width, int height, int cache_ms)
58be29479e75 add files
Ritor1
parents:
diff changeset
769 {
58be29479e75 add files
Ritor1
parents:
diff changeset
770 auto movie = new Movie;
58be29479e75 add files
Ritor1
parents:
diff changeset
771 if (!movie->Load(filename, width, height, cache_ms))
58be29479e75 add files
Ritor1
parents:
diff changeset
772 {
58be29479e75 add files
Ritor1
parents:
diff changeset
773 delete movie;
58be29479e75 add files
Ritor1
parents:
diff changeset
774 movie = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
775 }
58be29479e75 add files
Ritor1
parents:
diff changeset
776 return movie;
58be29479e75 add files
Ritor1
parents:
diff changeset
777 }
58be29479e75 add files
Ritor1
parents:
diff changeset
778
58be29479e75 add files
Ritor1
parents:
diff changeset
779
58be29479e75 add files
Ritor1
parents:
diff changeset
780
58be29479e75 add files
Ritor1
parents:
diff changeset
781
58be29479e75 add files
Ritor1
parents:
diff changeset
782
58be29479e75 add files
Ritor1
parents:
diff changeset
783
58be29479e75 add files
Ritor1
parents:
diff changeset
784 void av_logger(void *, int, const char *format, va_list args)
58be29479e75 add files
Ritor1
parents:
diff changeset
785 {
58be29479e75 add files
Ritor1
parents:
diff changeset
786 va_list va;
58be29479e75 add files
Ritor1
parents:
diff changeset
787 va_start(va, format);
58be29479e75 add files
Ritor1
parents:
diff changeset
788 char msg[256];
58be29479e75 add files
Ritor1
parents:
diff changeset
789 vsprintf(msg, format, va);
58be29479e75 add files
Ritor1
parents:
diff changeset
790 va_end(va);
58be29479e75 add files
Ritor1
parents:
diff changeset
791
58be29479e75 add files
Ritor1
parents:
diff changeset
792 log("av: %s", msg);
58be29479e75 add files
Ritor1
parents:
diff changeset
793 }
58be29479e75 add files
Ritor1
parents:
diff changeset
794
58be29479e75 add files
Ritor1
parents:
diff changeset
795 Player::Player()
58be29479e75 add files
Ritor1
parents:
diff changeset
796 {
58be29479e75 add files
Ritor1
parents:
diff changeset
797 static int libavcodec_initialized = false;
58be29479e75 add files
Ritor1
parents:
diff changeset
798
58be29479e75 add files
Ritor1
parents:
diff changeset
799 if (!libavcodec_initialized)
58be29479e75 add files
Ritor1
parents:
diff changeset
800 {
58be29479e75 add files
Ritor1
parents:
diff changeset
801 av_log_set_callback(av_logger);
58be29479e75 add files
Ritor1
parents:
diff changeset
802 avcodec_register_all();
58be29479e75 add files
Ritor1
parents:
diff changeset
803 av_register_all();
58be29479e75 add files
Ritor1
parents:
diff changeset
804
58be29479e75 add files
Ritor1
parents:
diff changeset
805 libavcodec_initialized = true;
58be29479e75 add files
Ritor1
parents:
diff changeset
806 }
58be29479e75 add files
Ritor1
parents:
diff changeset
807
58be29479e75 add files
Ritor1
parents:
diff changeset
808 if (!provider)
58be29479e75 add files
Ritor1
parents:
diff changeset
809 {
58be29479e75 add files
Ritor1
parents:
diff changeset
810 provider = new OpenALSoundProvider;
58be29479e75 add files
Ritor1
parents:
diff changeset
811 provider->Initialize();
58be29479e75 add files
Ritor1
parents:
diff changeset
812 }
58be29479e75 add files
Ritor1
parents:
diff changeset
813 }
58be29479e75 add files
Ritor1
parents:
diff changeset
814
58be29479e75 add files
Ritor1
parents:
diff changeset
815 Player::~Player()
58be29479e75 add files
Ritor1
parents:
diff changeset
816 {
58be29479e75 add files
Ritor1
parents:
diff changeset
817 }