| 1 |
/* |
| 2 |
* Copyright (C) 2005-2020 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 "VideoBufferDMA.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "utils/BufferObject.h" |
| 13 |
#include "utils/DRMHelpers.h" |
| 14 |
#include "utils/log.h" |
| 15 |
|
| 16 |
extern "C" |
| 17 |
{ |
| 18 |
#include <libavutil/imgutils.h> |
| 19 |
#include <libavutil/pixdesc.h> |
| 20 |
} |
| 21 |
|
| 22 |
CVideoBufferDMA::CVideoBufferDMA(IVideoBufferPool& pool, int id, uint32_t fourcc, uint64_t size) |
| 23 |
: CVideoBufferDRMPRIMEFFmpeg(pool, id), |
| 24 |
m_bo(CBufferObject::GetBufferObject(true)), |
| 25 |
m_fourcc(fourcc), |
| 26 |
m_size(size) |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
CVideoBufferDMA::~CVideoBufferDMA() |
| 31 |
{ |
| 32 |
Destroy(); |
| 33 |
} |
| 34 |
|
| 35 |
AVDRMFrameDescriptor* CVideoBufferDMA::GetDescriptor() const |
| 36 |
{ |
| 37 |
return const_cast<AVDRMFrameDescriptor*>(&m_descriptor); |
| 38 |
} |
| 39 |
|
| 40 |
uint8_t* CVideoBufferDMA::GetMemPtr() |
| 41 |
{ |
| 42 |
return m_addr; |
| 43 |
} |
| 44 |
|
| 45 |
void CVideoBufferDMA::GetPlanes(uint8_t* (&planes)[YuvImage::MAX_PLANES]) |
| 46 |
{ |
| 47 |
for (uint32_t i = 0; i < YuvImage::MAX_PLANES; i++) |
| 48 |
planes[i] = m_addr + m_offsets[i]; |
| 49 |
} |
| 50 |
|
| 51 |
void CVideoBufferDMA::GetStrides(int (&strides)[YuvImage::MAX_PLANES]) |
| 52 |
{ |
| 53 |
for (uint32_t i = 0; i < YuvImage::MAX_PLANES; i++) |
| 54 |
strides[i] = m_strides[i]; |
| 55 |
} |
| 56 |
|
| 57 |
void CVideoBufferDMA::SetDimensions(int width, int height) |
| 58 |
{ |
| 59 |
SetDimensions(width, height, m_strides, m_offsets); |
| 60 |
} |
| 61 |
|
| 62 |
void CVideoBufferDMA::SetDimensions(int width, |
| 63 |
int height, |
| 64 |
const int (&strides)[YuvImage::MAX_PLANES]) |
| 65 |
{ |
| 66 |
SetDimensions(width, height, strides, m_offsets); |
| 67 |
} |
| 68 |
|
| 69 |
void CVideoBufferDMA::SetDimensions(int width, |
| 70 |
int height, |
| 71 |
const int (&strides)[YuvImage::MAX_PLANES], |
| 72 |
const int (&planeOffsets)[YuvImage::MAX_PLANES]) |
| 73 |
{ |
| 74 |
m_width = width; |
| 75 |
m_height = height; |
| 76 |
|
| 77 |
AVDRMFrameDescriptor* descriptor = &m_descriptor; |
| 78 |
descriptor->nb_objects = 1; |
| 79 |
descriptor->objects[0].fd = m_fd; |
| 80 |
descriptor->nb_layers = 1; |
| 81 |
|
| 82 |
AVDRMLayerDescriptor* layer = &descriptor->layers[0]; |
| 83 |
layer->format = m_fourcc; |
| 84 |
layer->nb_planes = m_planes; |
| 85 |
|
| 86 |
for (uint32_t i = 0; i < m_planes; i++) |
| 87 |
{ |
| 88 |
layer->planes[i].offset = planeOffsets[i]; |
| 89 |
layer->planes[i].pitch = strides[i]; |
| 90 |
} |
| 91 |
|
| 92 |
if (CServiceBroker::GetLogging().CanLogComponent(LOGVIDEO)) |
| 93 |
{ |
| 94 |
std::string planeStr; |
| 95 |
for (uint32_t plane = 0; plane < m_planes; plane++) |
| 96 |
planeStr.append(fmt::format("\nplane[{}]: stride={}\toffset={}", plane, strides[plane], |
| 97 |
planeOffsets[plane])); |
| 98 |
|
| 99 |
CLog::Log(LOGDEBUG, LOGVIDEO, "CVideoBufferDMA::{} - frame layout id={} fourcc={}{}", |
| 100 |
__FUNCTION__, m_id, DRMHELPERS::FourCCToString(m_fourcc), planeStr); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
bool CVideoBufferDMA::Alloc() |
| 105 |
{ |
| 106 |
if (!m_bo->CreateBufferObject(m_size)) |
| 107 |
return false; |
| 108 |
|
| 109 |
m_fd = m_bo->GetFd(); |
| 110 |
m_addr = m_bo->GetMemory(); |
| 111 |
m_planes = 3; // CAddonVideoCodec only requests AV_PIX_FMT_YUV420P for now |
| 112 |
|
| 113 |
CLog::Log(LOGDEBUG, LOGVIDEO, "CVideoBufferDMA::{} - id={} fourcc={} fd={} size={} addr={}", |
| 114 |
__FUNCTION__, m_id, DRMHELPERS::FourCCToString(m_fourcc), m_fd, m_size, |
| 115 |
fmt::ptr(m_addr)); |
| 116 |
|
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
void CVideoBufferDMA::Export(AVFrame* frame, uint32_t width, uint32_t height) |
| 121 |
{ |
| 122 |
AVPixelFormat pix_fmt = static_cast<AVPixelFormat>(frame->format); |
| 123 |
m_planes = av_pix_fmt_count_planes(pix_fmt); |
| 124 |
int h_shift; |
| 125 |
int v_shift; |
| 126 |
|
| 127 |
if (av_pix_fmt_get_chroma_sub_sample(pix_fmt, &h_shift, &v_shift)) |
| 128 |
throw std::runtime_error("unable to determine chroma_sub_sample: " + |
| 129 |
std::string(av_get_pix_fmt_name(pix_fmt))); |
| 130 |
|
| 131 |
if (m_planes < 2 || m_planes > 3) |
| 132 |
throw std::runtime_error("only 2 or 3 plane formats supported: " + |
| 133 |
std::string(av_get_pix_fmt_name(pix_fmt))); |
| 134 |
|
| 135 |
for (uint32_t plane = 0; plane < m_planes; plane++) |
| 136 |
{ |
| 137 |
m_strides[plane] = |
| 138 |
av_image_get_linesize(static_cast<AVPixelFormat>(frame->format), width, plane); |
| 139 |
} |
| 140 |
m_offsets[0] = 0; |
| 141 |
m_offsets[1] = m_strides[0] * height; |
| 142 |
m_offsets[2] = m_offsets[1] + (m_strides[1] * height >> v_shift); |
| 143 |
|
| 144 |
if (CServiceBroker::GetLogging().CanLogComponent(LOGVIDEO)) |
| 145 |
{ |
| 146 |
std::string planeStr; |
| 147 |
for (uint32_t plane = 0; plane < m_planes; plane++) |
| 148 |
planeStr.append(fmt::format("\nplane[{}]: stride={}\toffset={}", plane, m_strides[plane], |
| 149 |
m_offsets[plane])); |
| 150 |
|
| 151 |
CLog::Log(LOGDEBUG, LOGVIDEO, "CVideoBufferDMA::{} - frame layout id={} fourcc={}{}", |
| 152 |
__FUNCTION__, m_id, DRMHELPERS::FourCCToString(m_fourcc), planeStr); |
| 153 |
} |
| 154 |
|
| 155 |
for (uint32_t i = 0; i < AV_NUM_DATA_POINTERS; i++) |
| 156 |
{ |
| 157 |
frame->data[i] = i < m_planes ? m_addr + m_offsets[i] : nullptr; |
| 158 |
frame->linesize[i] = i < m_planes ? m_strides[i] : 0; |
| 159 |
frame->buf[i] = i == 0 ? frame->opaque_ref : nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
frame->extended_data = frame->data; |
| 163 |
frame->opaque_ref = nullptr; |
| 164 |
} |
| 165 |
|
| 166 |
void CVideoBufferDMA::SyncStart() |
| 167 |
{ |
| 168 |
m_bo->SyncStart(); |
| 169 |
} |
| 170 |
|
| 171 |
void CVideoBufferDMA::SyncEnd() |
| 172 |
{ |
| 173 |
m_bo->SyncEnd(); |
| 174 |
} |
| 175 |
|
| 176 |
void CVideoBufferDMA::Destroy() |
| 177 |
{ |
| 178 |
m_bo->ReleaseMemory(); |
| 179 |
m_bo->DestroyBufferObject(); |
| 180 |
|
| 181 |
for (auto& offset : m_offsets) |
| 182 |
offset = 0; |
| 183 |
|
| 184 |
for (auto& stride : m_strides) |
| 185 |
stride = 0; |
| 186 |
|
| 187 |
m_planes = 0; |
| 188 |
m_width = 0; |
| 189 |
m_height = 0; |
| 190 |
m_fourcc = 0; |
| 191 |
m_size = 0; |
| 192 |
m_addr = nullptr; |
| 193 |
m_fd = -1; |
| 194 |
} |