1 /*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 /**
22 * @file
23 * MP test source, ported from MPlayer libmpcodecs/vf_test.c
24 */
25
26 #include "libavutil/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "formats.h"
33 #include "video.h"
34
35 #define WIDTH 512
36 #define HEIGHT 512
37
38 enum test_type {
39 TEST_DC_LUMA,
40 TEST_DC_CHROMA,
41 TEST_FREQ_LUMA,
42 TEST_FREQ_CHROMA,
43 TEST_AMP_LUMA,
44 TEST_AMP_CHROMA,
45 TEST_CBP,
46 TEST_MV,
47 TEST_RING1,
48 TEST_RING2,
49 TEST_ALL,
50 TEST_NB
51 };
52
53 typedef struct MPTestContext {
54 const AVClass *class;
55 AVRational frame_rate;
56 int64_t pts, max_pts, duration;
57 int64_t max_frames;
58 int hsub, vsub;
59 int test; ///< test_type
60 } MPTestContext;
61
62 #define OFFSET(x) offsetof(MPTestContext, x)
63 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
64 static const AVOption mptestsrc_options[]= {
65 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
66 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
67 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
68 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
69
70 { "test", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
71 { "t", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
72 { "dc_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA}, INT_MIN, INT_MAX, FLAGS, "test" },
73 { "dc_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
74 { "freq_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA}, INT_MIN, INT_MAX, FLAGS, "test" },
75 { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
76 { "amp_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA}, INT_MIN, INT_MAX, FLAGS, "test" },
77 { "amp_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
78 { "cbp", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP}, INT_MIN, INT_MAX, FLAGS, "test" },
79 { "mv", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV}, INT_MIN, INT_MAX, FLAGS, "test" },
80 { "ring1", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1}, INT_MIN, INT_MAX, FLAGS, "test" },
81 { "ring2", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2}, INT_MIN, INT_MAX, FLAGS, "test" },
82 { "all", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL}, INT_MIN, INT_MAX, FLAGS, "test" },
83 { "max_frames", "Set the maximum number of frames generated for each test", OFFSET(max_frames),
84 AV_OPT_TYPE_INT64, {.i64 = 30}, 1, INT64_MAX, FLAGS },
85 { "m", "Set the maximum number of frames generated for each test", OFFSET(max_frames),
86 AV_OPT_TYPE_INT64, {.i64 = 30}, 1, INT64_MAX, FLAGS },
87 { NULL }
88 };
89
90 AVFILTER_DEFINE_CLASS(mptestsrc);
91
92 static double c[64];
93
init_idct(void)94 static void init_idct(void)
95 {
96 int i, j;
97
98 for (i = 0; i < 8; i++) {
99 double s = i == 0 ? sqrt(0.125) : 0.5;
100
101 for (j = 0; j < 8; j++)
102 c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
103 }
104 }
105
idct(uint8_t * dst,int dst_linesize,int src[64])106 static void idct(uint8_t *dst, int dst_linesize, int src[64])
107 {
108 int i, j, k;
109 double tmp[64];
110
111 for (i = 0; i < 8; i++) {
112 for (j = 0; j < 8; j++) {
113 double sum = 0.0;
114
115 for (k = 0; k < 8; k++)
116 sum += c[k*8+j] * src[8*i+k];
117
118 tmp[8*i+j] = sum;
119 }
120 }
121
122 for (j = 0; j < 8; j++) {
123 for (i = 0; i < 8; i++) {
124 double sum = 0.0;
125
126 for (k = 0; k < 8; k++)
127 sum += c[k*8+i]*tmp[8*k+j];
128
129 dst[dst_linesize*i + j] = av_clip_uint8(lrint(sum));
130 }
131 }
132 }
133
draw_dc(uint8_t * dst,int dst_linesize,int color,int w,int h)134 static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
135 {
136 int x, y;
137
138 for (y = 0; y < h; y++)
139 for (x = 0; x < w; x++)
140 dst[x + y*dst_linesize] = color;
141 }
142
draw_basis(uint8_t * dst,int dst_linesize,int amp,int freq,int dc)143 static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
144 {
145 int src[64];
146
147 memset(src, 0, 64*sizeof(int));
148 src[0] = dc;
149 if (amp)
150 src[freq] = amp;
151 idct(dst, dst_linesize, src);
152 }
153
draw_cbp(uint8_t * dst[3],int dst_linesize[3],int cbp,int amp,int dc)154 static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
155 {
156 if (cbp&1) draw_basis(dst[0] , dst_linesize[0], amp, 1, dc);
157 if (cbp&2) draw_basis(dst[0]+8 , dst_linesize[0], amp, 1, dc);
158 if (cbp&4) draw_basis(dst[0]+ 8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
159 if (cbp&8) draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
160 if (cbp&16) draw_basis(dst[1] , dst_linesize[1], amp, 1, dc);
161 if (cbp&32) draw_basis(dst[2] , dst_linesize[2], amp, 1, dc);
162 }
163
dc_test(uint8_t * dst,int dst_linesize,int w,int h,int off)164 static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
165 {
166 const int step = FFMAX(256/(w*h/256), 1);
167 int x, y, color = off;
168
169 for (y = 0; y < h; y += 16) {
170 for (x = 0; x < w; x += 16) {
171 draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
172 color += step;
173 }
174 }
175 }
176
freq_test(uint8_t * dst,int dst_linesize,int off)177 static void freq_test(uint8_t *dst, int dst_linesize, int off)
178 {
179 int x, y, freq = 0;
180
181 for (y = 0; y < 8*16; y += 16) {
182 for (x = 0; x < 8*16; x += 16) {
183 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
184 freq++;
185 }
186 }
187 }
188
amp_test(uint8_t * dst,int dst_linesize,int off)189 static void amp_test(uint8_t *dst, int dst_linesize, int off)
190 {
191 int x, y, amp = off;
192
193 for (y = 0; y < 16*16; y += 16) {
194 for (x = 0; x < 16*16; x += 16) {
195 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
196 amp++;
197 }
198 }
199 }
200
cbp_test(uint8_t * dst[3],int dst_linesize[3],int off)201 static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
202 {
203 int x, y, cbp = 0;
204
205 for (y = 0; y < 16*8; y += 16) {
206 for (x = 0; x < 16*8; x += 16) {
207 uint8_t *dst1[3];
208 dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
209 dst1[1] = dst[1] + x + y* dst_linesize[1];
210 dst1[2] = dst[2] + x + y* dst_linesize[2];
211
212 draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
213 cbp++;
214 }
215 }
216 }
217
mv_test(uint8_t * dst,int dst_linesize,int off)218 static void mv_test(uint8_t *dst, int dst_linesize, int off)
219 {
220 int x, y;
221
222 for (y = 0; y < 16*16; y++) {
223 if (y&16)
224 continue;
225 for (x = 0; x < 16*16; x++)
226 dst[x + y*dst_linesize] = x + off*8/(y/32+1);
227 }
228 }
229
ring1_test(uint8_t * dst,int dst_linesize,int off)230 static void ring1_test(uint8_t *dst, int dst_linesize, int off)
231 {
232 int x, y, color = 0;
233
234 for (y = off; y < 16*16; y += 16) {
235 for (x = off; x < 16*16; x += 16) {
236 draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
237 color++;
238 }
239 }
240 }
241
ring2_test(uint8_t * dst,int dst_linesize,int off)242 static void ring2_test(uint8_t *dst, int dst_linesize, int off)
243 {
244 int x, y;
245
246 for (y = 0; y < 16*16; y++) {
247 for (x = 0; x < 16*16; x++) {
248 double d = hypot(x-8*16, y-8*16);
249 double r = d/20 - (int)(d/20);
250 if (r < off/30.0) {
251 dst[x + y*dst_linesize] = 255;
252 dst[x + y*dst_linesize+256] = 0;
253 } else {
254 dst[x + y*dst_linesize] = x;
255 dst[x + y*dst_linesize+256] = x;
256 }
257 }
258 }
259 }
260
init(AVFilterContext * ctx)261 static av_cold int init(AVFilterContext *ctx)
262 {
263 MPTestContext *test = ctx->priv;
264
265 test->max_pts = test->duration >= 0 ?
266 av_rescale_q(test->duration, AV_TIME_BASE_Q, av_inv_q(test->frame_rate)) : -1;
267 test->pts = 0;
268
269 av_log(ctx, AV_LOG_VERBOSE, "rate:%d/%d duration:%f\n",
270 test->frame_rate.num, test->frame_rate.den,
271 test->duration < 0 ? -1 : test->max_pts * av_q2d(av_inv_q(test->frame_rate)));
272 init_idct();
273
274 return 0;
275 }
276
config_props(AVFilterLink * outlink)277 static int config_props(AVFilterLink *outlink)
278 {
279 AVFilterContext *ctx = outlink->src;
280 MPTestContext *test = ctx->priv;
281 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(outlink->format);
282
283 test->hsub = pix_desc->log2_chroma_w;
284 test->vsub = pix_desc->log2_chroma_h;
285
286 outlink->w = WIDTH;
287 outlink->h = HEIGHT;
288 outlink->time_base = av_inv_q(test->frame_rate);
289
290 return 0;
291 }
292
query_formats(AVFilterContext * ctx)293 static int query_formats(AVFilterContext *ctx)
294 {
295 static const enum AVPixelFormat pix_fmts[] = {
296 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
297 };
298
299 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
300 if (!fmts_list)
301 return AVERROR(ENOMEM);
302 return ff_set_common_formats(ctx, fmts_list);
303 }
304
request_frame(AVFilterLink * outlink)305 static int request_frame(AVFilterLink *outlink)
306 {
307 MPTestContext *test = outlink->src->priv;
308 AVFrame *picref;
309 int w = WIDTH, h = HEIGHT,
310 cw = AV_CEIL_RSHIFT(w, test->hsub), ch = AV_CEIL_RSHIFT(h, test->vsub);
311 uint64_t frame = outlink->frame_count_in / test->max_frames;
312 uint64_t mod = outlink->frame_count_in % test->max_frames;
313 enum test_type tt = test->test;
314 int i;
315
316 if (test->max_pts >= 0 && test->pts > test->max_pts)
317 return AVERROR_EOF;
318 picref = ff_get_video_buffer(outlink, w, h);
319 if (!picref)
320 return AVERROR(ENOMEM);
321 picref->pts = test->pts++;
322
323 // clean image
324 for (i = 0; i < h; i++)
325 memset(picref->data[0] + i*picref->linesize[0], 0, w);
326 for (i = 0; i < ch; i++) {
327 memset(picref->data[1] + i*picref->linesize[1], 128, cw);
328 memset(picref->data[2] + i*picref->linesize[2], 128, cw);
329 }
330
331 if (tt == TEST_ALL && mod) /* draw a black frame at the beginning of each test */
332 tt = frame%(TEST_NB-1);
333
334 switch (tt) {
335 case TEST_DC_LUMA: dc_test(picref->data[0], picref->linesize[0], 256, 256, mod); break;
336 case TEST_DC_CHROMA: dc_test(picref->data[1], picref->linesize[1], 256, 256, mod); break;
337 case TEST_FREQ_LUMA: freq_test(picref->data[0], picref->linesize[0], mod); break;
338 case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], mod); break;
339 case TEST_AMP_LUMA: amp_test(picref->data[0], picref->linesize[0], mod); break;
340 case TEST_AMP_CHROMA: amp_test(picref->data[1], picref->linesize[1], mod); break;
341 case TEST_CBP: cbp_test(picref->data , picref->linesize , mod); break;
342 case TEST_MV: mv_test(picref->data[0], picref->linesize[0], mod); break;
343 case TEST_RING1: ring1_test(picref->data[0], picref->linesize[0], mod); break;
344 case TEST_RING2: ring2_test(picref->data[0], picref->linesize[0], mod); break;
345 }
346
347 return ff_filter_frame(outlink, picref);
348 }
349
350 static const AVFilterPad mptestsrc_outputs[] = {
351 {
352 .name = "default",
353 .type = AVMEDIA_TYPE_VIDEO,
354 .request_frame = request_frame,
355 .config_props = config_props,
356 },
357 { NULL }
358 };
359
360 AVFilter ff_vsrc_mptestsrc = {
361 .name = "mptestsrc",
362 .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
363 .priv_size = sizeof(MPTestContext),
364 .priv_class = &mptestsrc_class,
365 .init = init,
366 .query_formats = query_formats,
367 .inputs = NULL,
368 .outputs = mptestsrc_outputs,
369 };
370