annotate MediaPlayer.cpp @ 2318:133882370e83

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