21#include <libavutil/imgutils.h>
22#include <libswscale/swscale.h>
26#include <drm_fourcc.h>
33#define TRANSPARENT 0x00
34#define UNMULTIPLY(color, alpha) ((0xff * color) / alpha)
35#define BLEND(back, front, alpha) ((front * alpha) + (back * (255 - alpha))) / 255
46 if (buf->
PixFmt() == DRM_FORMAT_NV12)
47 return AV_PIX_FMT_NV12;
49 if (buf->
PixFmt() == DRM_FORMAT_YUV420)
50 return AV_PIX_FMT_YUV420P;
52 if (buf->
PixFmt() == DRM_FORMAT_ARGB8888)
53 return AV_PIX_FMT_RGBA;
55 if (buf->
PixFmt() == DRM_FORMAT_P030)
56 return AV_PIX_FMT_NONE;
58 return AV_PIX_FMT_NONE;
76 uint8_t *srcData[4], *dstData[4];
77 int srcLinesize[4], dstLinesize[4];
79 int srcW = buf->
Width();
83 if (src_pix_fmt == AV_PIX_FMT_NONE) {
84 LOGERROR(
"grab: %s: pixel format is not supported!", __FUNCTION__);
89 struct SwsContext *swsCtx;
96 LOGERROR(
"grab: %s: prime data is not mapped!", __FUNCTION__);
101 swsCtx = sws_getContext(srcW, srcH, src_pix_fmt,
102 dstW, dstH, dstPixFmt,
103 SWS_BILINEAR, NULL, NULL, NULL);
105 LOGERROR(
"grab: %s: Could not create swsCtx", __FUNCTION__);
106 munmap(buffer, buf->
Size(0));
110 if ((ret = av_image_alloc(dstData, dstLinesize, dstW, dstH, dstPixFmt, 1)) < 0) {
111 LOGERROR(
"grab: %s: Could not alloc dst image", __FUNCTION__);
112 munmap(buffer, buf->
Size(0));
113 sws_freeContext(swsCtx);
119 for (
int i = 0; i < buf->
NumPlanes(); i++) {
120 srcLinesize[i] = buf->
Pitch(i);
126 (
const uint8_t *
const*)srcData, srcLinesize, 0, srcH,
127 dstData, dstLinesize);
130 munmap(buffer, buf->
Size(0));
131 sws_freeContext(swsCtx);
134 LOGDEBUG2(
L_GRAB,
"grab: %s: return image at %p size %d", __FUNCTION__, dstData[0], dstBufsize);
152uint8_t *
ScaleRgb24(uint8_t *src,
int *size,
int srcW,
int srcH,
int dstW,
int dstH)
154 struct SwsContext *swsCtx;
160 uint8_t *srcData[4] = {src, NULL, NULL, NULL};
161 int srcLinesize[4] = {3 * srcW, 0, 0, 0};
163 swsCtx = sws_getContext(srcW, srcH, AV_PIX_FMT_RGB24,
164 dstW, dstH, AV_PIX_FMT_RGB24,
165 SWS_BILINEAR, NULL, NULL, NULL);
167 LOGERROR(
"grab: %s: Could not create swsCtx", __FUNCTION__);
171 if ((ret = av_image_alloc(dstData, dstLinesize, dstW, dstH, AV_PIX_FMT_RGB24, 1)) < 0) {
172 LOGERROR(
"grab: %s: Could not alloc dst image", __FUNCTION__);
173 sws_freeContext(swsCtx);
179 (
const uint8_t *
const*)srcData, srcLinesize, 0, srcH,
180 dstData, dstLinesize);
182 sws_freeContext(swsCtx);
185 LOGDEBUG2(
L_GRAB,
"grab: %s: return scaled image at %p size %d", __FUNCTION__, dstData[0], dstBufsize);
203void AlphaBlend(uint8_t *result, uint8_t *front, uint8_t *back,
const unsigned int width,
const unsigned int height)
205 for (
unsigned long index = 0; index < width * height; index++) {
206 const uint8_t frontAlpha = front[3];
209 for (
int i = 0; i < 3; i++) {
218 if (frontAlpha ==
OPAQUE) {
219 for (
int i = 0; i < 3; i++) {
220 result[i] = front[i];
228 const uint8_t backR = back[0];
229 const uint8_t backG = back[1];
230 const uint8_t backB = back[2];
232 const uint8_t frontR =
UNMULTIPLY(front[0], frontAlpha);
233 const uint8_t frontG =
UNMULTIPLY(front[1], frontAlpha);
234 const uint8_t frontB =
UNMULTIPLY(front[2], frontAlpha);
236 const uint8_t R =
BLEND(backR, frontR, frontAlpha);
237 const uint8_t G =
BLEND(backG, frontG, frontAlpha);
238 const uint8_t B =
BLEND(backB, frontB, frontAlpha);
264int BlitVideo(uint8_t *dst, uint8_t *src,
int dstW,
int dstH,
int dstX,
int dstY,
int srcW,
int srcH)
266 int srcStride = srcW * 3;
267 int dstStride = dstW * 3;
269 if ((dstX + srcW > dstW) || (dstY + srcH > dstH)) {
270 LOGDEBUG2(
L_GRAB,
"grab: %s: wrong dimensions, cropping not supported!", __FUNCTION__);
275 for (
int y = 0; y < srcH; y++) {
276 memcpy(&dst[((dstY + y) * dstStride + dstX * 3)], &src[y * srcStride], srcStride);
290 LOGDEBUG(
"Stream: %02x %02x %02x %02x %02x %02x %02x %02x %02x "
291 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
292 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x size %d",
293 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8],
294 data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17],
295 data[18], data[19], data[20], data[21], data[22], data[23], data[24], data[25], data[26],
296 data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], size);
#define UNMULTIPLY(color, alpha)
#define BLEND(back, front, alpha)
enum AVPixelFormat DrmFormatToAVFormat(cDrmBuffer *buf)
Convert a DRM format to a ffmpeg AV format.
void PrintStreamData(const uint8_t *data, int size)
Print raw stream data.
uint8_t * BufToRgb(cDrmBuffer *buf, int *size, int dstW, int dstH, enum AVPixelFormat dstPixFmt)
Convert a DRM buffer to rgb format image.
int BlitVideo(uint8_t *dst, uint8_t *src, int dstW, int dstH, int dstX, int dstY, int srcW, int srcH)
Blit the video on black background.
void AlphaBlend(uint8_t *result, uint8_t *front, uint8_t *back, const unsigned int width, const unsigned int height)
Blend two images.
uint8_t * ScaleRgb24(uint8_t *src, int *size, int srcW, int srcH, int dstW, int dstH)
Scale an image.
Some helper functions header file.
Logger class header file.