forked from xbmc/xbmc
/
VideoBuffer.h
182 lines (153 loc) · 5.66 KB
/
VideoBuffer.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
/*
* Copyright (C) 2005-2017 Team XBMC
* http://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, see
* <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "threads/CriticalSection.h"
#include <atomic>
#include <deque>
#include <list>
#include <memory>
#include <vector>
extern "C" {
#include "libavutil/pixfmt.h"
}
struct YuvImage
{
static const int MAX_PLANES = 4;
uint8_t* plane[MAX_PLANES];
int planesize[MAX_PLANES];
int stride[MAX_PLANES];
unsigned int width;
unsigned int height;
unsigned int cshift_x; // this is the chroma shift used
unsigned int cshift_y;
unsigned int bpp; // bytes per pixel
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#define BUFFER_STATE_DECODER 0x01;
#define BUFFER_STATE_RENDER 0x02;
class CVideoBuffer;
class IVideoBufferPool;
class CVideoBufferManager;
class IVideoBufferPool : public std::enable_shared_from_this<IVideoBufferPool>
{
public:
virtual ~IVideoBufferPool() = default;
// get a free buffer from the pool, sets ref count to 1
virtual CVideoBuffer* Get() = 0;
// called by buffer when ref count goes to zero
virtual void Return(int id) = 0;
// required if pool is registered with BufferManager BM call configure
// as soon as it knows parameters: pixFmx, size
virtual void Configure(AVPixelFormat format, int size) {};
// required if pool is registered with BufferManager
virtual bool IsConfigured() { return false;};
// required if pool is registered with BufferManager
// called before Get() to check if buffer pool is suitable
virtual bool IsCompatible(AVPixelFormat format, int size) { return false;};
// callback when BM releases buffer pool. i.e. before a new codec is created
// clients can register a new pool on this callback
virtual void Released(CVideoBufferManager &videoBufferManager) {};
// call on Get() before returning buffer to caller
std::shared_ptr<IVideoBufferPool> GetPtr() { return shared_from_this(); };
};
class CVideoBuffer
{
public:
CVideoBuffer() = delete;
virtual ~CVideoBuffer() = default;
void Acquire();
void Acquire(std::shared_ptr<IVideoBufferPool> pool);
void Release();
int GetId() const { return m_id; };
virtual AVPixelFormat GetFormat();
virtual uint8_t* GetMemPtr() { return nullptr; };
virtual void GetPlanes(uint8_t*(&planes)[YuvImage::MAX_PLANES]) {};
virtual void GetStrides(int(&strides)[YuvImage::MAX_PLANES]) {};
virtual void SetDimensions(int width, int height, const int (&strides)[YuvImage::MAX_PLANES]) {};
virtual void SetDimensions(int width, int height, const int (&strides)[YuvImage::MAX_PLANES], const int (&planeOffsets)[YuvImage::MAX_PLANES]) {};
static bool CopyPicture(YuvImage* pDst, YuvImage *pSrc);
static bool CopyNV12Picture(YuvImage* pDst, YuvImage *pSrc);
static bool CopyYUV422PackedPicture(YuvImage* pDst, YuvImage *pSrc);
protected:
CVideoBuffer(int id);
AVPixelFormat m_pixFormat = AV_PIX_FMT_NONE;
std::atomic_int m_refCount;
int m_id;
std::shared_ptr<IVideoBufferPool> m_pool;
};
class CVideoBufferSysMem : public CVideoBuffer
{
public:
CVideoBufferSysMem(IVideoBufferPool &pool, int id, AVPixelFormat format, int size);
~CVideoBufferSysMem() override;
uint8_t* GetMemPtr() override;
void GetPlanes(uint8_t*(&planes)[YuvImage::MAX_PLANES]) override;
void GetStrides(int(&strides)[YuvImage::MAX_PLANES]) override;
void SetDimensions(int width, int height, const int (&strides)[YuvImage::MAX_PLANES]) override;
void SetDimensions(int width, int height, const int (&strides)[YuvImage::MAX_PLANES], const int (&planeOffsets)[YuvImage::MAX_PLANES]) override;
bool Alloc();
protected:
int m_width = 0;
int m_height = 0;
int m_size = 0;
uint8_t *m_data = nullptr;
YuvImage m_image;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CVideoBufferPoolSysMem : public IVideoBufferPool
{
public:
CVideoBuffer* Get() override;
void Return(int id) override;
void Configure(AVPixelFormat format, int size) override;
bool IsConfigured() override;
bool IsCompatible(AVPixelFormat format, int size) override;
protected:
int m_width = 0;
int m_height = 0;
int m_size = 0;
AVPixelFormat m_pixFormat = AV_PIX_FMT_NONE;
bool m_configured = false;
CCriticalSection m_critSection;
std::vector<CVideoBufferSysMem*> m_all;
std::deque<int> m_used;
std::deque<int> m_free;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CVideoBufferManager
{
public:
CVideoBufferManager();
void RegisterPool(std::shared_ptr<IVideoBufferPool> pool);
void ReleasePools();
CVideoBuffer* Get(AVPixelFormat format, int size);
protected:
CCriticalSection m_critSection;
std::list<std::shared_ptr<IVideoBufferPool>> m_pools;
private:
CVideoBufferManager (const CVideoBufferManager&) = delete;
CVideoBufferManager& operator= (const CVideoBufferManager&) = delete;
};