forked from opdenkamp/xbmc
/
XVBA.h
385 lines (346 loc) · 10 KB
/
XVBA.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (C) 2005-2011 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#pragma once
#include "X11/Xlib.h"
#include "amd/amdxvba.h"
#include "DllAvCodec.h"
#include "DVDCodecs/Video/DVDVideoCodecFFmpeg.h"
#include "threads/Thread.h"
#include "threads/CriticalSection.h"
#include "threads/SharedSection.h"
#include "threads/Event.h"
#include "guilib/DispResource.h"
#include "guilib/Geometry.h"
#include "libavcodec/xvba.h"
#include "utils/ActorProtocol.h"
#include "settings/VideoSettings.h"
#include <GL/gl.h>
#include <GL/glx.h>
#include <vector>
#include <deque>
using namespace Actor;
namespace XVBA
{
//-----------------------------------------------------------------------------
// XVBA data structs
//-----------------------------------------------------------------------------
class CDecoder;
class CXVBAContext;
class COutput;
#define NUM_RENDER_PICS 9
/**
* Buffer statistics used to control number of frames in queue
*/
class CXvbaBufferStats
{
public:
uint16_t decodedPics;
uint16_t processedPics;
uint16_t renderPics;
uint64_t latency; // time decoder has waited for a frame, ideally there is no latency
int playSpeed;
bool canSkipDeint;
int processCmd;
void IncDecoded() { CSingleLock l(m_sec); decodedPics++;}
void DecDecoded() { CSingleLock l(m_sec); decodedPics--;}
void IncProcessed() { CSingleLock l(m_sec); processedPics++;}
void DecProcessed() { CSingleLock l(m_sec); processedPics--;}
void IncRender() { CSingleLock l(m_sec); renderPics++;}
void DecRender() { CSingleLock l(m_sec); renderPics--;}
void Reset() { CSingleLock l(m_sec); decodedPics=0; processedPics=0;renderPics=0;latency=0;}
void Get(uint16_t &decoded, uint16_t &processed, uint16_t &render) {CSingleLock l(m_sec); decoded = decodedPics, processed=processedPics, render=renderPics;}
void SetParams(uint64_t time, int speed) { CSingleLock l(m_sec); latency = time; playSpeed = speed; }
void GetParams(uint64_t &lat, int &speed) { CSingleLock l(m_sec); lat = latency; speed = playSpeed; }
void SetCmd(int cmd) { CSingleLock l(m_sec); processCmd = cmd; }
void GetCmd(int &cmd) { CSingleLock l(m_sec); cmd = processCmd; processCmd = 0; }
void SetCanSkipDeint(bool canSkip) { CSingleLock l(m_sec); canSkipDeint = canSkip; }
bool CanSkipDeint() { CSingleLock l(m_sec); if (canSkipDeint) return true; else return false;}
private:
CCriticalSection m_sec;
};
/**
* CXvbaConfig holds all configuration parameters needed by vdpau
* The structure is sent to the internal classes CMixer and COutput
* for init.
*/
struct CXvbaConfig
{
int surfaceWidth;
int surfaceHeight;
int vidWidth;
int vidHeight;
int outWidth;
int outHeight;
bool useSharedSurfaces;
CXVBAContext *context;
XVBADecodeCap decoderCap;
void *xvbaSession;
std::vector<xvba_render_state*> *videoSurfaces;
CCriticalSection *videoSurfaceSec;
CCriticalSection *apiSec;
CXvbaBufferStats *stats;
int numRenderBuffers;
uint32_t maxReferences;
};
/**
* Holds a decoded frame
* Input to COutput for further processing
*/
struct CXvbaDecodedPicture
{
DVDVideoPicture DVDPic;
xvba_render_state *render;
};
/**
* Ready to render textures
* Sent from COutput back to CDecoder
* Objects are referenced by DVDVideoPicture and are sent
* to renderer
*/
class CXvbaRenderPicture
{
friend class CDecoder;
friend class COutput;
public:
DVDVideoPicture DVDPic;
int texWidth, texHeight;
CRect crop;
GLuint texture;
uint32_t sourceIdx;
bool valid;
CDecoder *xvba;
CXvbaRenderPicture* Acquire();
long Release();
void Transfer();
private:
void ReturnUnused();
int refCount;
CCriticalSection *renderPicSection;
COutput *xvbaOutput;
};
//-----------------------------------------------------------------------------
// Output
//-----------------------------------------------------------------------------
/**
* Buffer pool holds allocated xvba and gl resources
* Embedded in COutput
*/
struct XvbaBufferPool
{
struct GLVideoSurface
{
unsigned int id;
bool used;
bool transferred;
GLuint texture;
void *glSurface;
xvba_render_state *render;
XVBA_SURFACE_FLAG field;
};
std::vector<GLVideoSurface> glSurfaces;
std::vector<CXvbaRenderPicture> allRenderPics;
std::deque<CXvbaRenderPicture*> usedRenderPics;
std::deque<CXvbaRenderPicture*> freeRenderPics;
CCriticalSection renderPicSec;
};
class COutputControlProtocol : public Protocol
{
public:
COutputControlProtocol(std::string name, CEvent* inEvent, CEvent *outEvent) : Protocol(name, inEvent, outEvent) {};
enum OutSignal
{
INIT,
FLUSH,
PRECLEANUP,
TIMEOUT,
};
enum InSignal
{
ACC,
ERROR,
STATS,
};
};
class COutputDataProtocol : public Protocol
{
public:
COutputDataProtocol(std::string name, CEvent* inEvent, CEvent *outEvent) : Protocol(name, inEvent, outEvent) {};
enum OutSignal
{
NEWFRAME = 0,
RETURNPIC,
};
enum InSignal
{
PICTURE,
};
};
/**
* COutput is embedded in CDecoder and embeds CMixer
* The class has its own OpenGl context which is shared with render thread
* COuput generated ready to render textures and passes them back to
* CDecoder
*/
class COutput : private CThread
{
public:
COutput(CEvent *inMsgEvent);
virtual ~COutput();
void Start();
void Dispose();
void TransferSurface(uint32_t source);
COutputControlProtocol m_controlPort;
COutputDataProtocol m_dataPort;
protected:
void OnStartup();
void OnExit();
void Process();
void StateMachine(int signal, Protocol *port, Message *msg);
bool HasWork();
bool IsDecodingFinished();
CXvbaRenderPicture* ProcessPicture();
void ProcessReturnPicture(CXvbaRenderPicture *pic);
int FindFreeSurface();
void InitCycle();
void FiniCycle();
bool Init();
bool Uninit();
void Flush();
bool CreateGlxContext();
bool DestroyGlxContext();
bool EnsureBufferPool();
void ReleaseBufferPool();
CEvent m_outMsgEvent;
CEvent *m_inMsgEvent;
int m_state;
bool m_bStateMachineSelfTrigger;
// extended state variables for state machine
int m_extTimeout;
bool m_xvbaError;
CXvbaConfig m_config;
XvbaBufferPool m_bufferPool;
Display *m_Display;
Window m_Window;
GLXContext m_glContext;
GLXWindow m_glWindow;
Pixmap m_pixmap;
GLXPixmap m_glPixmap;
std::queue<CXvbaDecodedPicture> m_decodedPics;
CXvbaDecodedPicture m_processPicture;
XVBA_SURFACE_FLAG m_field;
bool m_deinterlacing;
int m_deintStep;
bool m_deintSkip;
};
//-----------------------------------------------------------------------------
// XVBA decoder
//-----------------------------------------------------------------------------
class CXVBAContext
{
public:
static bool EnsureContext(CXVBAContext **ctx);
void *GetContext();
void Release();
private:
CXVBAContext();
void Close();
bool LoadSymbols();
bool CreateContext();
void DestroyContext();
static CXVBAContext *m_context;
static CCriticalSection m_section;
static Display *m_display;
int m_refCount;
static void *m_dlHandle;
void *m_xvbaContext;
};
class CDecoder : public CDVDVideoCodecFFmpeg::IHardwareDecoder,
public IDispResource
{
friend class CXvbaRenderPicture;
public:
struct pictureAge
{
int b_age;
int ip_age[2];
};
enum EDisplayState
{ XVBA_OPEN
, XVBA_RESET
, XVBA_LOST
, XVBA_ERROR
};
CDecoder();
virtual ~CDecoder();
virtual void OnLostDevice();
virtual void OnResetDevice();
virtual bool Open(AVCodecContext* avctx, const enum PixelFormat fmt, unsigned int surfaces = 0);
virtual int Decode (AVCodecContext* avctx, AVFrame* frame);
virtual bool GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture);
virtual void Reset();
virtual void Close();
virtual int Check(AVCodecContext* avctx);
virtual long Release();
virtual const std::string Name() { return "xvba"; }
virtual bool CanSkipDeint();
virtual void SetSpeed(int speed);
bool Supports(EINTERLACEMETHOD method);
long ReleasePicReference();
protected:
bool CreateSession(AVCodecContext* avctx);
void DestroySession();
bool EnsureDataControlBuffers(unsigned int num);
void ResetState();
void SetError(const char* function, const char* msg, int line);
bool IsSurfaceValid(xvba_render_state *render);
void ReturnRenderPicture(CXvbaRenderPicture *renderPic);
// callbacks for ffmpeg
static void FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic);
static void FFDrawSlice(struct AVCodecContext *avctx,
const AVFrame *src, int offset[4],
int y, int type, int height);
static int FFGetBuffer(AVCodecContext *avctx, AVFrame *pic);
DllAvUtil m_dllAvUtil;
CCriticalSection m_decoderSection;
CEvent m_displayEvent;
EDisplayState m_displayState;
CXvbaConfig m_xvbaConfig;
std::vector<xvba_render_state*> m_videoSurfaces;
CCriticalSection m_apiSec, m_videoSurfaceSec;
ThreadIdentifier m_decoderThread;
unsigned int m_decoderId;
struct XVBABufferPool
{
XVBABufferDescriptor *picture_descriptor_buffer;
XVBABufferDescriptor *iq_matrix_buffer;
XVBABufferDescriptor *data_buffer;
std::vector<XVBABufferDescriptor*> data_control_buffers;
};
XVBABufferPool m_xvbaBufferPool;
pictureAge picAge;
COutput m_xvbaOutput;
CXvbaBufferStats m_bufferStats;
CEvent m_inMsgEvent;
CXvbaRenderPicture *m_presentPicture;
int m_speed;
int m_codecControl;
};
}