annotate MediaPlayer.cpp @ 2420:0313c761d203

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