2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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.
23 * MP test source, ported from MPlayer libmpcodecs/vf_test.c
26 #include "libavutil/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
50 typedef struct MPTestContext {
52 unsigned int frame_nb;
56 char *size, *rate, *duration;
60 #define OFFSET(x) offsetof(MPTestContext, x)
62 static const AVOption mptestsrc_options[]= {
63 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
64 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
65 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
66 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
68 { "test", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.dbl=TEST_ALL}, 0, INT_MAX, 0, "test" },
69 { "t", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.dbl=TEST_ALL}, 0, INT_MAX, 0, "test" },
70 { "dc_luma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_DC_LUMA}, INT_MIN, INT_MAX, 0, "test" },
71 { "dc_chroma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_DC_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
72 { "freq_luma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_FREQ_LUMA}, INT_MIN, INT_MAX, 0, "test" },
73 { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
74 { "amp_luma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_AMP_LUMA}, INT_MIN, INT_MAX, 0, "test" },
75 { "amp_chroma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_AMP_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
76 { "cbp", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_CBP}, INT_MIN, INT_MAX, 0, "test" },
77 { "mv", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_MV}, INT_MIN, INT_MAX, 0, "test" },
78 { "ring1", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_RING1}, INT_MIN, INT_MAX, 0, "test" },
79 { "ring2", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_RING2}, INT_MIN, INT_MAX, 0, "test" },
80 { "all", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_ALL}, INT_MIN, INT_MAX, 0, "test" },
85 static const char *mptestsrc_get_name(void *ctx)
90 static const AVClass mptestsrc_class = {
98 static void init_idct(void)
102 for (i = 0; i < 8; i++) {
103 double s = i == 0 ? sqrt(0.125) : 0.5;
105 for (j = 0; j < 8; j++)
106 c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
110 static void idct(uint8_t *dst, int dst_linesize, int src[64])
115 for (i = 0; i < 8; i++) {
116 for (j = 0; j < 8; j++) {
119 for (k = 0; k < 8; k++)
120 sum += c[k*8+j] * src[8*i+k];
126 for (j = 0; j < 8; j++) {
127 for (i = 0; i < 8; i++) {
130 for (k = 0; k < 8; k++)
131 sum += c[k*8+i]*tmp[8*k+j];
133 dst[dst_linesize*i + j] = av_clip((int)floor(sum+0.5), 0, 255);
138 static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
142 for (y = 0; y < h; y++)
143 for (x = 0; x < w; x++)
144 dst[x + y*dst_linesize] = color;
147 static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
151 memset(src, 0, 64*sizeof(int));
155 idct(dst, dst_linesize, src);
158 static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
160 if (cbp&1) draw_basis(dst[0] , dst_linesize[0], amp, 1, dc);
161 if (cbp&2) draw_basis(dst[0]+8 , dst_linesize[0], amp, 1, dc);
162 if (cbp&4) draw_basis(dst[0]+ 8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
163 if (cbp&8) draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
164 if (cbp&16) draw_basis(dst[1] , dst_linesize[1], amp, 1, dc);
165 if (cbp&32) draw_basis(dst[2] , dst_linesize[2], amp, 1, dc);
168 static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
170 const int step = FFMAX(256/(w*h/256), 1);
171 int x, y, color = off;
173 for (y = 0; y < h; y += 16) {
174 for (x = 0; x < w; x += 16) {
175 draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
181 static void freq_test(uint8_t *dst, int dst_linesize, int off)
185 for (y = 0; y < 8*16; y += 16) {
186 for (x = 0; x < 8*16; x += 16) {
187 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
193 static void amp_test(uint8_t *dst, int dst_linesize, int off)
197 for (y = 0; y < 16*16; y += 16) {
198 for (x = 0; x < 16*16; x += 16) {
199 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
205 static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
209 for (y = 0; y < 16*8; y += 16) {
210 for (x = 0; x < 16*8; x += 16) {
212 dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
213 dst1[1] = dst[1] + x + y* dst_linesize[1];
214 dst1[2] = dst[2] + x + y* dst_linesize[2];
216 draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
222 static void mv_test(uint8_t *dst, int dst_linesize, int off)
226 for (y = 0; y < 16*16; y++) {
229 for (x = 0; x < 16*16; x++)
230 dst[x + y*dst_linesize] = x + off*8/(y/32+1);
234 static void ring1_test(uint8_t *dst, int dst_linesize, int off)
238 for (y = off; y < 16*16; y += 16) {
239 for (x = off; x < 16*16; x += 16) {
240 draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
246 static void ring2_test(uint8_t *dst, int dst_linesize, int off)
250 for (y = 0; y < 16*16; y++) {
251 for (x = 0; x < 16*16; x++) {
252 double d = sqrt((x-8*16)*(x-8*16) + (y-8*16)*(y-8*16));
253 double r = d/20 - (int)(d/20);
255 dst[x + y*dst_linesize] = 255;
256 dst[x + y*dst_linesize+256] = 0;
258 dst[x + y*dst_linesize] = x;
259 dst[x + y*dst_linesize+256] = x;
265 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
267 MPTestContext *test = ctx->priv;
268 AVRational frame_rate_q;
269 int64_t duration = -1;
272 test->class = &mptestsrc_class;
273 av_opt_set_defaults(test);
275 if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
276 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
280 if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
281 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
282 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
286 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
287 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
291 test->time_base.num = frame_rate_q.den;
292 test->time_base.den = frame_rate_q.num;
293 test->max_pts = duration >= 0 ?
294 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
298 av_log(ctx, AV_LOG_INFO, "rate:%d/%d duration:%f\n",
299 frame_rate_q.num, frame_rate_q.den,
300 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
306 static int config_props(AVFilterLink *outlink)
308 AVFilterContext *ctx = outlink->src;
309 MPTestContext *test = ctx->priv;
310 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[outlink->format];
312 test->hsub = pix_desc->log2_chroma_w;
313 test->vsub = pix_desc->log2_chroma_h;
317 outlink->time_base = test->time_base;
322 static int query_formats(AVFilterContext *ctx)
324 static const enum PixelFormat pix_fmts[] = {
325 PIX_FMT_YUV420P, PIX_FMT_NONE
328 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
332 static int request_frame(AVFilterLink *outlink)
334 MPTestContext *test = outlink->src->priv;
335 AVFilterBufferRef *picref;
336 int w = WIDTH, h = HEIGHT, ch = h>>test->vsub;
337 unsigned int frame = test->frame_nb;
338 enum test_type tt = test->test;
340 if (test->max_pts >= 0 && test->pts > test->max_pts)
342 picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, w, h);
343 picref->pts = test->pts++;
346 memset(picref->data[0], 0, picref->linesize[0] * h);
347 memset(picref->data[1], 128, picref->linesize[1] * ch);
348 memset(picref->data[2], 128, picref->linesize[2] * ch);
350 if (tt == TEST_ALL && frame%30) /* draw a black frame at the beginning of each test */
351 tt = (frame/30)%(TEST_NB-1);
354 case TEST_DC_LUMA: dc_test(picref->data[0], picref->linesize[0], 256, 256, frame%30); break;
355 case TEST_DC_CHROMA: dc_test(picref->data[1], picref->linesize[1], 256, 256, frame%30); break;
356 case TEST_FREQ_LUMA: freq_test(picref->data[0], picref->linesize[0], frame%30); break;
357 case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], frame%30); break;
358 case TEST_AMP_LUMA: amp_test(picref->data[0], picref->linesize[0], frame%30); break;
359 case TEST_AMP_CHROMA: amp_test(picref->data[1], picref->linesize[1], frame%30); break;
360 case TEST_CBP: cbp_test(picref->data , picref->linesize , frame%30); break;
361 case TEST_MV: mv_test(picref->data[0], picref->linesize[0], frame%30); break;
362 case TEST_RING1: ring1_test(picref->data[0], picref->linesize[0], frame%30); break;
363 case TEST_RING2: ring2_test(picref->data[0], picref->linesize[0], frame%30); break;
368 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
369 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
370 avfilter_end_frame(outlink);
371 avfilter_unref_buffer(picref);
376 AVFilter avfilter_vsrc_mptestsrc = {
378 .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
379 .priv_size = sizeof(MPTestContext),
382 .query_formats = query_formats,
384 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
386 .outputs = (const AVFilterPad[]) {{ .name = "default",
387 .type = AVMEDIA_TYPE_VIDEO,
388 .config_props = config_props,
389 .request_frame = request_frame,
390 .config_props = config_props, },