Debian Codegraph @

xbmc/cores/VideoPlayer/Buffers/VideoBuffer.cpp

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "VideoBuffer.h"
10
11 #include "utils/log.h"
12
13 #include <mutex>
14 #include <string.h>
15 #include <utility>
16
17 //-----------------------------------------------------------------------------
18 // CVideoBuffer
19 //-----------------------------------------------------------------------------
20
21 CVideoBuffer::CVideoBuffer(int id)
22 {
23 m_id = id;
24 m_refCount = 0;
25 }
26
27 void CVideoBuffer::Acquire()
28 {
29 m_refCount++;
30 }
31
32 void CVideoBuffer::Acquire(std::shared_ptr<IVideoBufferPool> pool)
33 {
34 m_refCount++;
35 m_pool = std::move(pool);
36 }
37
38 void CVideoBuffer::Release()
39 {
40 if (--m_refCount <= 0 && m_pool)
41 {
42 std::shared_ptr<IVideoBufferPool> pool = m_pool->GetPtr();
43 m_pool = nullptr;
44 pool->Return(m_id);
45 }
46 }
47
48 AVPixelFormat CVideoBuffer::GetFormat()
49 {
50 return m_pixFormat;
51 }
52
53 bool CVideoBuffer::CopyPicture(YuvImage* pDst, YuvImage *pSrc)
54 {
55 uint8_t *s = pSrc->plane[0];
56 uint8_t *d = pDst->plane[0];
57 int w = pDst->width * pDst->bpp;
58 int h = pDst->height;
59 if ((w == pSrc->stride[0]) && (pSrc->stride[0] == pDst->stride[0]))
60 {
61 memcpy(d, s, w*h);
62 }
63 else
64 {
65 for (int y = 0; y < h; y++)
66 {
67 memcpy(d, s, w);
68 s += pSrc->stride[0];
69 d += pDst->stride[0];
70 }
71 }
72 s = pSrc->plane[1];
73 d = pDst->plane[1];
74 w =(pDst->width >> pDst->cshift_x) * pDst->bpp;
75 h =(pDst->height >> pDst->cshift_y);
76 if ((w == pSrc->stride[1]) && (pSrc->stride[1] == pDst->stride[1]))
77 {
78 memcpy(d, s, w*h);
79 }
80 else
81 {
82 for (int y = 0; y < h; y++)
83 {
84 memcpy(d, s, w);
85 s += pSrc->stride[1];
86 d += pDst->stride[1];
87 }
88 }
89 s = pSrc->plane[2];
90 d = pDst->plane[2];
91 if ((w == pSrc->stride[2]) && (pSrc->stride[2] == pDst->stride[2]))
92 {
93 memcpy(d, s, w*h);
94 }
95 else
96 {
97 for (int y = 0; y < h; y++)
98 {
99 memcpy(d, s, w);
100 s += pSrc->stride[2];
101 d += pDst->stride[2];
102 }
103 }
104 return true;
105 }
106
107
108 bool CVideoBuffer::CopyNV12Picture(YuvImage* pDst, YuvImage *pSrc)
109 {
110 uint8_t *s = pSrc->plane[0];
111 uint8_t *d = pDst->plane[0];
112 int w = pDst->width;
113 int h = pDst->height;
114 // Copy Y
115 if ((w == pSrc->stride[0]) && (pSrc->stride[0] == pDst->stride[0]))
116 {
117 memcpy(d, s, w*h);
118 }
119 else
120 {
121 for (int y = 0; y < h; y++)
122 {
123 memcpy(d, s, w);
124 s += pSrc->stride[0];
125 d += pDst->stride[0];
126 }
127 }
128
129 s = pSrc->plane[1];
130 d = pDst->plane[1];
131 w = pDst->width;
132 h = pDst->height >> 1;
133 // Copy packed UV (width is same as for Y as it's both U and V components)
134 if ((w == pSrc->stride[1]) && (pSrc->stride[1] == pDst->stride[1]))
135 {
136 memcpy(d, s, w*h);
137 }
138 else
139 {
140 for (int y = 0; y < h; y++)
141 {
142 memcpy(d, s, w);
143 s += pSrc->stride[1];
144 d += pDst->stride[1];
145 }
146 }
147
148 return true;
149 }
150
151 bool CVideoBuffer::CopyYUV422PackedPicture(YuvImage* pDst, YuvImage *pSrc)
152 {
153 uint8_t *s = pSrc->plane[0];
154 uint8_t *d = pDst->plane[0];
155 int w = pDst->width;
156 int h = pDst->height;
157
158 // Copy YUYV
159 if ((w * 2 == pSrc->stride[0]) && (pSrc->stride[0] == pDst->stride[0]))
160 {
161 memcpy(d, s, w*h*2);
162 }
163 else
164 {
165 for (int y = 0; y < h; y++)
166 {
167 memcpy(d, s, w*2);
168 s += pSrc->stride[0];
169 d += pDst->stride[0];
170 }
171 }
172
173 return true;
174 }
175
176 CVideoBufferSysMem::CVideoBufferSysMem(IVideoBufferPool &pool, int id, AVPixelFormat format, int size)
177 : CVideoBuffer(id)
178 {
179 m_pixFormat = format;
180 m_size = size;
181 memset(&m_image, 0, sizeof(YuvImage));
182 }
183
184 CVideoBufferSysMem::~CVideoBufferSysMem()
185 {
186 delete[] m_data;
187 }
188
189 uint8_t* CVideoBufferSysMem::GetMemPtr()
190 {
191 return m_data;
192 }
193
194 void CVideoBufferSysMem::GetPlanes(uint8_t*(&planes)[YuvImage::MAX_PLANES])
195 {
196 planes[0] = m_image.plane[0];
197 planes[1] = m_image.plane[1];
198 planes[2] = m_image.plane[2];
199 }
200
201 void CVideoBufferSysMem::GetStrides(int(&strides)[YuvImage::MAX_PLANES])
202 {
203 strides[0] = m_image.stride[0];
204 strides[1] = m_image.stride[1];
205 strides[2] = m_image.stride[2];
206 }
207
208 void CVideoBufferSysMem::SetPixelFormat(const AVPixelFormat pixFormat)
209 {
210 if (pixFormat == AV_PIX_FMT_NONE)
211 {
212 CLog::LogF(LOGERROR, "Pixel format not valid.");
213 return;
214 }
215 m_pixFormat = pixFormat;
216 }
217
218 void CVideoBufferSysMem::SetDimensions(int width, int height, const int (&strides)[YuvImage::MAX_PLANES])
219 {
220 m_width = width;
221 m_height = height;
222
223 m_image.width = m_width;
224 m_image.height = m_height;
225 m_image.stride[0] = strides[0];
226 m_image.stride[1] = strides[1];
227 m_image.stride[2] = strides[2];
228 m_image.cshift_x = 1;
229 m_image.cshift_y = 1;
230 m_image.bpp = 1;
231
232 if (m_pixFormat == AV_PIX_FMT_YUV420P ||
233 m_pixFormat == AV_PIX_FMT_YUV420P16 ||
234 m_pixFormat == AV_PIX_FMT_YUV420P14 ||
235 m_pixFormat == AV_PIX_FMT_YUV420P12 ||
236 m_pixFormat == AV_PIX_FMT_YUV420P10 ||
237 m_pixFormat == AV_PIX_FMT_YUV420P9)
238 {
239 if (m_pixFormat != AV_PIX_FMT_YUV420P)
240 m_image.bpp = 2;
241
242 m_image.planesize[0] = m_image.stride[0] * m_image.height;
243 m_image.planesize[1] = m_image.stride[1] * (m_image.height >> m_image.cshift_y);
244 m_image.planesize[2] = m_image.stride[2] * (m_image.height >> m_image.cshift_y);
245 }
246 else if (m_pixFormat == AV_PIX_FMT_NV12)
247 {
248 // Y plane
249 m_image.planesize[0] = m_image.stride[0] * m_image.height;
250 // packed UV plane
251 m_image.planesize[1] = m_image.stride[1] * m_image.height / 2;
252 // third plane is not used
253 m_image.planesize[2] = 0;
254 }
255 else if (m_pixFormat == AV_PIX_FMT_YUYV422 ||
256 m_pixFormat == AV_PIX_FMT_UYVY422)
257 {
258 // packed YUYV plane
259 m_image.planesize[0] = m_image.stride[0] * m_image.height;
260 // second plane is not used
261 m_image.planesize[1] = 0;
262 // third plane is not used
263 m_image.planesize[2] = 0;
264 }
265
266 m_image.plane[0] = m_data;
267 m_image.plane[1] = m_data + m_image.planesize[0];
268 m_image.plane[2] = m_image.plane[1] + m_image.planesize[1];
269 }
270
271 void CVideoBufferSysMem::SetDimensions(int width, int height, const int (&strides)[YuvImage::MAX_PLANES], const int (&planeOffsets)[YuvImage::MAX_PLANES])
272 {
273 SetDimensions(width, height, strides);
274
275 m_image.plane[0] = m_data + planeOffsets[0];
276 m_image.plane[1] = m_data + planeOffsets[1];
277 m_image.plane[2] = m_data + planeOffsets[2];
278 }
279
280 bool CVideoBufferSysMem::Alloc()
281 {
282 m_data = new uint8_t[m_size];
283 return true;
284 }
285
286
287 //-----------------------------------------------------------------------------
288 // CVideoBufferPool
289 //-----------------------------------------------------------------------------
290
291 CVideoBufferPoolSysMem::~CVideoBufferPoolSysMem()
292 {
293 std::unique_lock<CCriticalSection> lock(m_critSection);
294
295 for (auto buf : m_all)
296 {
297 delete buf;
298 }
299 }
300
301 CVideoBuffer* CVideoBufferPoolSysMem::Get()
302 {
303 std::unique_lock<CCriticalSection> lock(m_critSection);
304
305 CVideoBufferSysMem *buf = nullptr;
306 if (!m_free.empty())
307 {
308 int idx = m_free.front();
309 m_free.pop_front();
310 m_used.push_back(idx);
311 buf = m_all[idx];
312 }
313 else
314 {
315 int id = m_all.size();
316 buf = new CVideoBufferSysMem(*this, id, m_pixFormat, m_size);
317 buf->Alloc();
318 m_all.push_back(buf);
319 m_used.push_back(id);
320 }
321
322 buf->Acquire(GetPtr());
323 return buf;
324 }
325
326 void CVideoBufferPoolSysMem::Return(int id)
327 {
328 std::unique_lock<CCriticalSection> lock(m_critSection);
329
330 auto it = m_used.begin();
331 while (it != m_used.end())
332 {
333 if (*it == id)
334 {
335 m_used.erase(it);
336 break;
337 }
338 else
339 ++it;
340 }
341 m_free.push_back(id);
342
343 if (m_bm && m_used.empty())
344 {
345 (m_bm->*m_cbDispose)(this);
346 }
347 }
348
349 void CVideoBufferPoolSysMem::Configure(AVPixelFormat format, int size)
350 {
351 m_pixFormat = format;
352 m_size = size;
353 m_configured = true;
354 }
355
356 inline bool CVideoBufferPoolSysMem::IsConfigured()
357 {
358 return m_configured;
359 }
360
361 bool CVideoBufferPoolSysMem::IsCompatible(AVPixelFormat format, int size)
362 {
363 if (m_pixFormat == format &&
364 m_size == size)
365 return true;
366
367 return false;
368 }
369
370 void CVideoBufferPoolSysMem::Discard(CVideoBufferManager *bm, ReadyToDispose cb)
371 {
372 std::unique_lock<CCriticalSection> lock(m_critSection);
373 m_bm = bm;
374 m_cbDispose = cb;
375
376 if (m_used.empty())
377 (m_bm->*m_cbDispose)(this);
378 }
379
380 std::shared_ptr<IVideoBufferPool> CVideoBufferPoolSysMem::CreatePool()
381 {
382 return std::make_shared<CVideoBufferPoolSysMem>();
383 }
384
385 //-----------------------------------------------------------------------------
386 // CVideoBufferManager
387 //-----------------------------------------------------------------------------
388
389 CVideoBufferManager::CVideoBufferManager()
390 {
391 std::unique_lock<CCriticalSection> lock(m_critSection);
392 RegisterPoolFactory("SysMem", &CVideoBufferPoolSysMem::CreatePool);
393 }
394
395 void CVideoBufferManager::RegisterPool(const std::shared_ptr<IVideoBufferPool>& pool)
396 {
397 std::unique_lock<CCriticalSection> lock(m_critSection);
398 // preferred pools are to the front
399 m_pools.push_front(pool);
400 }
401
402 void CVideoBufferManager::RegisterPoolFactory(const std::string& id, CreatePoolFunc createFunc)
403 {
404 std::unique_lock<CCriticalSection> lock(m_critSection);
405 m_poolFactories[id] = createFunc;
406 }
407
408 void CVideoBufferManager::ReleasePools()
409 {
410 std::unique_lock<CCriticalSection> lock(m_critSection);
411 std::list<std::shared_ptr<IVideoBufferPool>> pools = m_pools;
412 m_pools.clear();
413
414 m_discardedPools = pools;
415
416 for (const auto& pool : pools)
417 {
418 pool->Discard(this, &CVideoBufferManager::ReadyForDisposal);
419 }
420 }
421
422 void CVideoBufferManager::ReleasePool(IVideoBufferPool *pool)
423 {
424 std::unique_lock<CCriticalSection> lock(m_critSection);
425
426 for (auto it = m_pools.begin(); it != m_pools.end(); ++it)
427 {
428 if ((*it).get() == pool)
429 {
430 m_discardedPools.push_back(*it);
431 m_pools.erase(it);
432 pool->Discard(this, &CVideoBufferManager::ReadyForDisposal);
433 break;
434 }
435 }
436 }
437
438 void CVideoBufferManager::ReadyForDisposal(IVideoBufferPool *pool)
439 {
440 std::unique_lock<CCriticalSection> lock(m_critSection);
441
442 for (auto it = m_discardedPools.begin(); it != m_discardedPools.end(); ++it)
443 {
444 if ((*it).get() == pool)
445 {
446 pool->Released(*this);
447 m_discardedPools.erase(it);
448 break;
449 }
450 }
451 }
452
453 CVideoBuffer* CVideoBufferManager::Get(AVPixelFormat format, int size, IVideoBufferPool **pPool)
454 {
455 std::unique_lock<CCriticalSection> lock(m_critSection);
456 for (const auto& pool : m_pools)
457 {
458 if (!pool->IsConfigured())
459 {
460 pool->Configure(format, size);
461 }
462 if (pool->IsCompatible(format, size))
463 {
464 return pool->Get();
465 }
466 }
467
468 for (const auto& fact : m_poolFactories)
469 {
470 std::shared_ptr<IVideoBufferPool> pool = fact.second();
471 m_pools.push_front(pool);
472 pool->Configure(format, size);
473 if (pPool)
474 *pPool = pool.get();
475 return pool->Get();
476 }
477 return nullptr;
478 }