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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#include <climits>
#include "mythconfig.h"
extern "C" {
#include "libavcodec/avcodec.h"
}
#include "frame.h"
#include "mythlogging.h"
#include "myth_imgconvert.h"
#include "pgm.h"
// TODO: verify this
/*
* N.B.: this is really C code, but LOG, #define'd in mythlogging.h, is in
* a C++ header file, so this has to be compiled with a C++ compiler, which
* means this has to be a C++ source file.
*/
static enum PixelFormat pixelTypeOfVideoFrameType(VideoFrameType codec)
{
/* XXX: how to map VideoFrameType values to PixelFormat values??? */
switch (codec) {
case FMT_YV12: return PIX_FMT_YUV420P;
default: break;
}
return PIX_FMT_NONE;
}
int pgm_fill(AVPicture *dst, const VideoFrame *frame)
{
enum PixelFormat srcfmt;
AVPicture src;
if ((srcfmt = pixelTypeOfVideoFrameType(frame->codec)) == PIX_FMT_NONE)
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_fill unknown codec: %1")
.arg(frame->codec));
return -1;
}
if (avpicture_fill(&src, frame->buf, srcfmt, frame->width,
frame->height) < 0)
{
LOG(VB_COMMFLAG, LOG_ERR, "pgm_fill avpicture_fill failed");
return -1;
}
if (myth_sws_img_convert(dst, PIX_FMT_GRAY8, &src, srcfmt, frame->width,
frame->height))
{
LOG(VB_COMMFLAG, LOG_ERR, "pgm_fill img_convert failed");
return -1;
}
return 0;
}
int pgm_read(unsigned char *buf, int width, int height, const char *filename)
{
FILE *fp;
int nn, fwidth, fheight, maxgray, rr;
if (!(fp = fopen(filename, "r")))
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_read fopen %1 failed: %2")
.arg(filename).arg(strerror(errno)));
return -1;
}
if ((nn = fscanf(fp, "P5\n%20d %20d\n%20d\n",
&fwidth, &fheight, &maxgray)) != 3)
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_read fscanf %1 failed: %2")
.arg(filename).arg(strerror(errno)));
goto error;
}
if (fwidth != width || fheight != height || maxgray != UCHAR_MAX)
{
LOG(VB_COMMFLAG, LOG_ERR,
QString("pgm_read header (%1x%2,%3) != (%4x%5,%6)")
.arg(fwidth).arg(fheight).arg(maxgray)
.arg(width).arg(height).arg(UCHAR_MAX));
goto error;
}
for (rr = 0; rr < height; rr++)
{
if (fread(buf + rr * width, 1, width, fp) != (size_t)width)
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_read fread %1 failed: %2")
.arg(filename).arg(strerror(errno)));
goto error;
}
}
(void)fclose(fp);
return 0;
error:
(void)fclose(fp);
return -1;
}
int pgm_write(const unsigned char *buf, int width, int height,
const char *filename)
{
/* Copied from libavcodec/apiexample.c */
FILE *fp;
int rr;
if (!(fp = fopen(filename, "w")))
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_write fopen %1 failed: %2")
.arg(filename).arg(strerror(errno)));
return -1;
}
(void)fprintf(fp, "P5\n%d %d\n%d\n", width, height, UCHAR_MAX);
for (rr = 0; rr < height; rr++)
{
if (fwrite(buf + rr * width, 1, width, fp) != (size_t)width)
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_write fwrite %1 failed: %2")
.arg(filename).arg(strerror(errno)));
goto error;
}
}
(void)fclose(fp);
return 0;
error:
(void)fclose(fp);
return -1;
}
static int pgm_expand(AVPicture *dst, const AVPicture *src, int srcheight,
int extratop, int extraright, int extrabottom,
int extraleft)
{
/* Pad all edges with the edge color. */
const int srcwidth = src->linesize[0];
const int newwidth = srcwidth + extraleft + extraright;
const int newheight = srcheight + extratop + extrabottom;
const unsigned char *srcdata;
int rr;
/* Copy the image. */
for (rr = 0; rr < srcheight; rr++)
memcpy(dst->data[0] + (rr + extratop) * newwidth + extraleft,
src->data[0] + rr * srcwidth,
srcwidth);
/* Pad the top. */
srcdata = src->data[0];
for (rr = 0; rr < extratop; rr++)
memcpy(dst->data[0] + rr * newwidth + extraleft, srcdata, srcwidth);
/* Pad the bottom. */
srcdata = src->data[0] + (srcheight - 1) * srcwidth;
for (rr = extratop + srcheight; rr < newheight; rr++)
memcpy(dst->data[0] + rr * newwidth + extraleft, srcdata, srcwidth);
/* Pad the left. */
for (rr = 0; rr < newheight; rr++)
memset(dst->data[0] + rr * newwidth,
dst->data[0][rr * newwidth + extraleft],
extraleft);
/* Pad the right. */
for (rr = 0; rr < newheight; rr++)
memset(dst->data[0] + rr * newwidth + extraleft + srcwidth,
dst->data[0][rr * newwidth + extraleft + srcwidth - 1],
extraright);
return 0;
}
static int pgm_expand_uniform(AVPicture *dst, const AVPicture *src,
int srcheight, int extramargin)
{
return pgm_expand(dst, src, srcheight,
extramargin, extramargin, extramargin, extramargin);
}
int pgm_crop(AVPicture *dst, const AVPicture *src, int srcheight,
int srcrow, int srccol, int cropwidth, int cropheight)
{
const int srcwidth = src->linesize[0];
int rr;
if (dst->linesize[0] != cropwidth)
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_crop want width %1, have %2")
.arg(cropwidth).arg(dst->linesize[0]));
return -1;
}
for (rr = 0; rr < cropheight; rr++)
memcpy(dst->data[0] + rr * cropwidth,
src->data[0] + (srcrow + rr) * srcwidth + srccol,
cropwidth);
(void)srcheight; /* gcc */
return 0;
}
int pgm_overlay(AVPicture *dst, const AVPicture *s1, int s1height,
int s1row, int s1col, const AVPicture *s2, int s2height)
{
const int dstwidth = dst->linesize[0];
const int s1width = s1->linesize[0];
const int s2width = s2->linesize[0];
int rr;
if (dstwidth != s1width)
{
LOG(VB_COMMFLAG, LOG_ERR, QString("pgm_overlay want width %1, have %2")
.arg(s1width).arg(dst->linesize[0]));
return -1;
}
av_picture_copy(dst, s1, PIX_FMT_GRAY8, s1width, s1height);
/* Overwrite overlay area of "dst" with "s2". */
for (rr = 0; rr < s2height; rr++)
memcpy(dst->data[0] + (s1row + rr) * s1width + s1col,
s2->data[0] + rr * s2width,
s2width);
return 0;
}
int pgm_convolve_radial(AVPicture *dst, AVPicture *s1, AVPicture *s2,
const AVPicture *src, int srcheight,
const double *mask, int mask_radius)
{
/*
* Pad and convolve an image.
*
* "s1" and "s2" are caller-pre-allocated "scratch space" (avoid repeated
* per-frame allocation/deallocation).
*
* Remove noise from image; smooth by convolving with a Gaussian mask. See
* http://www.cogs.susx.ac.uk/users/davidy/teachvision/vision0.html
*
* Optimization for radially-symmetric masks: implement a single
* two-dimensional convolution with two commutative single-dimensional
* convolutions.
*/
const int srcwidth = src->linesize[0];
const int newwidth = srcwidth + 2 * mask_radius;
const int newheight = srcheight + 2 * mask_radius;
int ii, rr, cc, rr2, cc2;
double sum;
/* Get a padded copy of the src image for use by the convolutions. */
if (pgm_expand_uniform(s1, src, srcheight, mask_radius))
return -1;
/* copy s1 to s2 and dst */
av_picture_copy(s2, s1, PIX_FMT_GRAY8, newwidth, newheight);
av_picture_copy(dst, s1, PIX_FMT_GRAY8, newwidth, newheight);
/* "s1" convolve with column vector => "s2" */
rr2 = mask_radius + srcheight;
cc2 = mask_radius + srcwidth;
for (rr = mask_radius; rr < rr2; rr++)
{
for (cc = mask_radius; cc < cc2; cc++)
{
sum = 0;
for (ii = -mask_radius; ii <= mask_radius; ii++)
{
sum += mask[ii + mask_radius] *
s1->data[0][(rr + ii) * newwidth + cc];
}
s2->data[0][rr * newwidth + cc] = (unsigned char)(sum + 0.5);
}
}
/* "s2" convolve with row vector => "dst" */
for (rr = mask_radius; rr < rr2; rr++)
{
for (cc = mask_radius; cc < cc2; cc++)
{
sum = 0;
for (ii = -mask_radius; ii <= mask_radius; ii++)
{
sum += mask[ii + mask_radius] *
s2->data[0][rr * newwidth + cc + ii];
}
dst->data[0][rr * newwidth + cc] = (unsigned char)(sum + 0.5);
}
}
return 0;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|