4 #include "libmythbase/mythconfig.h"
12 #include "libavcodec/avcodec.h"
13 #include "libavutil/imgutils.h"
24 static enum PixelFormat pixelTypeOfVideoFrameType(
VideoFrameType codec)
28 case FMT_YV12:
return AV_PIX_FMT_YUV420P;
31 return AV_PIX_FMT_NONE;
40 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_read fopen %1 failed: %2")
48 int nn = fscanf(fp,
"P5\n%20d %20d\n%20d\n", &fwidth, &fheight, &maxgray);
51 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_read fscanf %1 failed: %2")
56 if (fwidth != width || fheight != height || maxgray != UCHAR_MAX)
58 LOG(VB_COMMFLAG, LOG_ERR,
59 QString(
"pgm_read header (%1x%2,%3) != (%4x%5,%6)")
60 .arg(fwidth).arg(fheight).arg(maxgray)
61 .arg(width).arg(height).arg(UCHAR_MAX));
65 for (ptrdiff_t rr = 0; rr < height; rr++)
67 if (fread(buf + (rr * width), 1, width, fp) != (
size_t)width)
69 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_read fread %1 failed: %2")
83 int pgm_write(
const unsigned char *buf,
int width,
int height,
91 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_write fopen %1 failed: %2")
96 (void)fprintf(fp,
"P5\n%d %d\n%d\n", width, height, UCHAR_MAX);
97 for (ptrdiff_t rr = 0; rr < height; rr++)
99 if (fwrite(buf + (rr * width), 1, width, fp) != (size_t)width)
101 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_write fwrite %1 failed: %2")
116 int extratop,
int extraright,
int extrabottom,
120 const int srcwidth = src->linesize[0];
121 const int newwidth = srcwidth + extraleft + extraright;
122 const int newheight = srcheight + extratop + extrabottom;
125 for (ptrdiff_t rr = 0; rr < srcheight; rr++)
127 memcpy(dst->data[0] + ((rr + extratop) * newwidth) + extraleft,
128 src->data[0] + (rr * srcwidth),
133 const uchar *srcdata = src->data[0];
134 for (ptrdiff_t rr = 0; rr < extratop; rr++)
135 memcpy(dst->data[0] + (rr * newwidth) + extraleft, srcdata, srcwidth);
138 srcdata = src->data[0] +
static_cast<ptrdiff_t
>(srcheight - 1) * srcwidth;
139 for (ptrdiff_t rr = extratop + srcheight; rr < newheight; rr++)
140 memcpy(dst->data[0] + (rr * newwidth) + extraleft, srcdata, srcwidth);
143 for (ptrdiff_t rr = 0; rr < newheight; rr++)
145 memset(dst->data[0] + (rr * newwidth),
146 dst->data[0][(rr * newwidth) + extraleft],
151 for (ptrdiff_t rr = 0; rr < newheight; rr++)
153 memset(dst->data[0] + (rr * newwidth) + extraleft + srcwidth,
154 dst->data[0][(rr * newwidth) + extraleft + srcwidth - 1],
162 int srcheight,
int extramargin)
165 extramargin, extramargin, extramargin, extramargin);
169 [[maybe_unused]]
int srcheight,
170 int srcrow,
int srccol,
int cropwidth,
int cropheight)
172 const int srcwidth = src->linesize[0];
174 if (dst->linesize[0] != cropwidth)
176 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_crop want width %1, have %2")
177 .arg(cropwidth).arg(dst->linesize[0]));
181 for (ptrdiff_t rr = 0; rr < cropheight; rr++)
183 memcpy(dst->data[0] + (rr * cropwidth),
184 src->data[0] + ((srcrow + rr) * srcwidth) + srccol,
192 int s1row,
int s1col,
const AVFrame *s2,
int s2height)
194 const int dstwidth = dst->linesize[0];
195 const int s1width = s1->linesize[0];
196 const int s2width = s2->linesize[0];
198 if (dstwidth != s1width)
200 LOG(VB_COMMFLAG, LOG_ERR, QString(
"pgm_overlay want width %1, have %2")
201 .arg(s1width).arg(dst->linesize[0]));
207 std::array<const uint8_t*,4> src_data
208 {s1->data[0], s1->data[1], s1->data[2], s1->data[3]};
210 av_image_copy(dst->data, dst->linesize, src_data.data(), s1->linesize,
211 AV_PIX_FMT_GRAY8, s1width, s1height);
214 for (ptrdiff_t rr = 0; rr < s2height; rr++)
216 memcpy(dst->data[0] + ((s1row + rr) * s1width) + s1col,
217 s2->data[0] + (rr * s2width),
225 const AVFrame *src,
int srcheight,
226 const double *mask,
int mask_radius)
241 const int srcwidth = src->linesize[0];
242 const int newwidth = srcwidth + (2 * mask_radius);
243 const int newheight = srcheight + (2 * mask_radius);
253 std::array<const uint8_t*,4> src_data
254 {s1->data[0], s1->data[1], s1->data[2], s1->data[3]};
256 av_image_copy(s2->data, s2->linesize, src_data.data(), s1->linesize,
257 AV_PIX_FMT_GRAY8, newwidth, newheight);
258 av_image_copy(dst->data, dst->linesize, src_data.data(), s1->linesize,
259 AV_PIX_FMT_GRAY8, newwidth, newheight);
262 int rr2 = mask_radius + srcheight;
263 int cc2 = mask_radius + srcwidth;
264 for (
int rr = mask_radius; rr < rr2; rr++)
266 for (
int cc = mask_radius;
cc < cc2;
cc++)
269 for (
int ii = -mask_radius; ii <= mask_radius; ii++)
271 sum += mask[ii + mask_radius] *
272 s1->data[0][((rr + ii) * newwidth) +
cc];
274 s2->data[0][(rr * newwidth) +
cc] = lround(sum);
279 for (
int rr = mask_radius; rr < rr2; rr++)
281 for (
int cc = mask_radius;
cc < cc2;
cc++)
284 for (
int ii = -mask_radius; ii <= mask_radius; ii++)
286 sum += mask[ii + mask_radius] *
287 s2->data[0][(rr * newwidth) +
cc + ii];
289 dst->data[0][(rr * newwidth) +
cc] = lround(sum);