-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmediadecoder.h
242 lines (184 loc) · 6.19 KB
/
mediadecoder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma once
#include "mediaformat.h"
#include "curl.h"
#include "result.h"
#include "subtitle.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}
#ifdef WIN32
#pragma warning( push )
#pragma warning( disable : 26495) // uninitialized variable
#endif
#include <boost/lockfree/queue.hpp>
#include <boost/function.hpp>
#ifdef WIN32
#pragma warning( pop )
#endif
#include <string>
#include <vector>
#include <thread>
namespace mediadecoder
{
static const uint32_t NUM_FRAME_DATA_POINTERS = 4;
static const uint32_t DEFAULT_SUBTITLE_DURATION_SEC = 4;
static const uint32_t MAX_FRAME_RATE = 120;
// forward declaration
struct Stream;
struct Producer;
// types
typedef boost::function<void (Stream*, Producer*, AVFrame*, AVPacket*)> DecoderCallback;
struct Stream
{
AVCodecParameters* codecParameters = nullptr;
AVCodec* codec = nullptr;
AVCodecContext* codecContext = nullptr;
AVStream* stream = nullptr;
int32_t streamIndex = -1;
// processing
DecoderCallback processCallback;
};
struct VideoStream : public Stream
{
// video stream output format
VideoFormat outputFormat = VF_INVALID;
// sws_scale context if required
SwsContext* swsContext = nullptr;
// destination format
AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
// reformat buffer size if reformat is required
uint32_t reformatBufferSize = 0;
uint32_t width = 0;
uint32_t height = 0;
uint32_t framesPerSecond = 0;
};
struct AudioStream : public Stream
{
SampleFormat sampleFormat = SF_FMT_INVALID;
uint32_t sampleRate = 0;
uint32_t channels = 0;
AudioChannelList channelMapping;
std::map<uint32_t, uint32_t> channelInputToOutputMapping;
};
struct SubtitleStream : public Stream
{
subtitle::SubStationAlphaHeader* subtitleHeader = nullptr;
};
struct SubtitleSubRip
{
std::shared_ptr<subtitle::SubRip> subs;
subtitle::SubRipDialogueList::const_iterator posIt;
};
struct VideoFrame
{
uint8_t* buffers[NUM_FRAME_DATA_POINTERS] = { nullptr, nullptr, nullptr, nullptr};
int32_t lineSize[NUM_FRAME_DATA_POINTERS] = { 0, 0, 0, 0 };
uint32_t width = 0;
uint32_t height = 0;
uint64_t timeUs = 0;
};
struct AudioFrame
{
uint8_t* samples = nullptr;
uint32_t sampleSize = 0;
uint32_t nbSamples = 0;
uint32_t channels = 0;
uint64_t timeUs = 0;
std::atomic<bool> inUse = false;
};
struct Subtitle
{
// ass subtitle
subtitle::SubStationAlphaHeader* header = nullptr;
subtitle::SubStationAlphaDialogue* dialogue = nullptr;
// text subtitle
std::vector<std::string> text;
uint64_t startTimeUs = 0;
uint64_t endTimeUs = 0;
std::string fontName = "Arial";
uint32_t fontSize = 16;
glm::vec3 color = {1.0f, 1.0f, 1.0f};
};
template<typename T>
using FrameQueue = boost::lockfree::queue<T, boost::lockfree::fixed_sized<true> >;
typedef FrameQueue<AudioFrame*> AudioQueue;
typedef FrameQueue<VideoFrame*> VideoQueue;
typedef FrameQueue<Subtitle*> SubtitleQueue;
struct Decoder
{
AVFormatContext* avFormatContext = nullptr;
VideoStream* videoStream = nullptr;
AudioStream* audioStream = nullptr;
std::vector<SubtitleStream*> subtitleStreams;
std::map<uint32_t, SubtitleSubRip> subRips;
std::vector<int32_t> subtitleIndexes = {-1};
uint32_t subtitleIndex = 0;
uint32_t nextSubtitleIndex = 0;
Producer* producer = nullptr;
curl::Session* curl = nullptr;
};
struct Producer
{
Decoder* decoder = nullptr;
// playback frames
VideoQueue* videoQueue = nullptr;
AudioQueue* audioQueue = nullptr;
std::atomic<uint32_t> videoQueueSize = 0;
std::atomic<uint32_t> audioQueueSize = 0;
std::atomic<uint32_t> subtitleQueueSize = 0;
uint32_t videoQueueCapacity = 0;
uint32_t audioQueueCapacity = 0;
uint32_t subtitleQueueCapacity = 0;
// frame pools
VideoQueue* videoFramePool = nullptr;
AudioQueue* audioFramePool = nullptr;
// subtitle
SubtitleQueue* subtitleQueue = nullptr;
// media streams owned by the decoder
std::vector<Stream*> streams;
std::thread thread;
std::atomic<bool> quitting = false;
uint64_t currentDecodingTimeUs = 0;
// seeking
std::atomic<bool> seeking;
uint64_t seekTime = 0;
// eof
std::atomic<bool> done = false;;
};
Result Init();
void SetOutputFormat(const VideoFormatList&);
// decoder
Result Create(Decoder*& decoder);
Result Open(Decoder*& decoder, const std::string& filename);
// video format
VideoFormat GetOutputFormat(Decoder*);
uint32_t GetVideoWidth(Decoder* decoder);
uint32_t GetVideoHeight(Decoder* decoder);
uint32_t GetFramesPerSecond(Decoder* decoder);
// audio format
uint32_t GetAudioNumChannels(Decoder*);
uint32_t GetAudioSampleRate(Decoder*);
SampleFormat GetAudioSampleFormat(Decoder*);
uint64_t GetDuration(Decoder* decoder);
bool GetHaveAudio(Decoder* decoder);
bool GetHaveVideo(Decoder* decoder);
// subtitle
void AddSubtitleTrack(Decoder*, std::shared_ptr<subtitle::SubRip> track);
void ToggleSubtitleTrack(Decoder*);
void Destroy(Decoder*&);
// producer / consumer
Result Create(Producer*& producer, Decoder*);
void Destroy(Producer*&);
void Seek(Producer*,uint64_t timeUs);
bool IsSeeking(Producer*);
bool Consume(Producer*, VideoFrame*& frame);
bool Consume(Producer*, AudioFrame*& frame);
bool Consume(Producer*, Subtitle*& sub);
void Release(Producer*,VideoFrame*);
void Release(Producer*,AudioFrame*);
void Release(Producer*,Subtitle*);
void WaitForPlayback(Producer*);
};