FFmpeg
Loading...
Searching...
No Matches
vsrc_mptestsrc.c
Go to the documentation of this file.
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/opt.h"
27#include "libavutil/pixdesc.h"
28#include "avfilter.h"
29#include "filters.h"
30#include "video.h"
31
32#define WIDTH 512
33#define HEIGHT 512
34
49
58
59#define OFFSET(x) offsetof(MPTestContext, x)
60#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61static const AVOption mptestsrc_options[]= {
62 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
63 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
64 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
65 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
66
67 { "test", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, .unit = "test" },
68 { "t", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, .unit = "test" },
69 { "dc_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
70 { "dc_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
71 { "freq_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
72 { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
73 { "amp_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
74 { "amp_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
75 { "cbp", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
76 { "mv", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
77 { "ring1", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
78 { "ring2", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
79 { "all", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
80 { "max_frames", "Set the maximum number of frames generated for each test", OFFSET(max_frames),
81 AV_OPT_TYPE_INT64, {.i64 = 30}, 1, INT64_MAX, FLAGS },
82 { "m", "Set the maximum number of frames generated for each test", OFFSET(max_frames),
83 AV_OPT_TYPE_INT64, {.i64 = 30}, 1, INT64_MAX, FLAGS },
84 { NULL }
85};
86
88
89static double c[64];
90
91static void init_idct(void)
92{
93 int i, j;
94
95 for (i = 0; i < 8; i++) {
96 double s = i == 0 ? sqrt(0.125) : 0.5;
97
98 for (j = 0; j < 8; j++)
99 c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
100 }
101}
102
103static void idct(uint8_t *dst, int dst_linesize, int src[64])
104{
105 int i, j, k;
106 double tmp[64];
107
108 for (i = 0; i < 8; i++) {
109 for (j = 0; j < 8; j++) {
110 double sum = 0.0;
111
112 for (k = 0; k < 8; k++)
113 sum += c[k*8+j] * src[8*i+k];
114
115 tmp[8*i+j] = sum;
116 }
117 }
118
119 for (j = 0; j < 8; j++) {
120 for (i = 0; i < 8; i++) {
121 double sum = 0.0;
122
123 for (k = 0; k < 8; k++)
124 sum += c[k*8+i]*tmp[8*k+j];
125
126 dst[dst_linesize*i + j] = av_clip_uint8(lrint(sum));
127 }
128 }
129}
130
131static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
132{
133 int x, y;
134
135 for (y = 0; y < h; y++)
136 for (x = 0; x < w; x++)
137 dst[x + y*dst_linesize] = color;
138}
139
140static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
141{
142 int src[64];
143
144 memset(src, 0, 64*sizeof(int));
145 src[0] = dc;
146 if (amp)
147 src[freq] = amp;
148 idct(dst, dst_linesize, src);
149}
150
151static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
152{
153 if (cbp&1) draw_basis(dst[0] , dst_linesize[0], amp, 1, dc);
154 if (cbp&2) draw_basis(dst[0]+8 , dst_linesize[0], amp, 1, dc);
155 if (cbp&4) draw_basis(dst[0]+ 8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
156 if (cbp&8) draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
157 if (cbp&16) draw_basis(dst[1] , dst_linesize[1], amp, 1, dc);
158 if (cbp&32) draw_basis(dst[2] , dst_linesize[2], amp, 1, dc);
159}
160
161static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
162{
163 const int step = FFMAX(256/(w*h/256), 1);
164 int x, y, color = off;
165
166 for (y = 0; y < h; y += 16) {
167 for (x = 0; x < w; x += 16) {
168 draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
169 color += step;
170 }
171 }
172}
173
174static void freq_test(uint8_t *dst, int dst_linesize, int off)
175{
176 int x, y, freq = 0;
177
178 for (y = 0; y < 8*16; y += 16) {
179 for (x = 0; x < 8*16; x += 16) {
180 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
181 freq++;
182 }
183 }
184}
185
186static void amp_test(uint8_t *dst, int dst_linesize, int off)
187{
188 int x, y, amp = off;
189
190 for (y = 0; y < 16*16; y += 16) {
191 for (x = 0; x < 16*16; x += 16) {
192 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
193 amp++;
194 }
195 }
196}
197
198static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
199{
200 int x, y, cbp = 0;
201
202 for (y = 0; y < 16*8; y += 16) {
203 for (x = 0; x < 16*8; x += 16) {
204 uint8_t *dst1[3];
205 dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
206 dst1[1] = dst[1] + x + y* dst_linesize[1];
207 dst1[2] = dst[2] + x + y* dst_linesize[2];
208
209 draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
210 cbp++;
211 }
212 }
213}
214
215static void mv_test(uint8_t *dst, int dst_linesize, int off)
216{
217 int x, y;
218
219 for (y = 0; y < 16*16; y++) {
220 if (y&16)
221 continue;
222 for (x = 0; x < 16*16; x++)
223 dst[x + y*dst_linesize] = x + off*8/(y/32+1);
224 }
225}
226
227static void ring1_test(uint8_t *dst, int dst_linesize, int off)
228{
229 int x, y, color = 0;
230
231 for (y = off; y < 16*16; y += 16) {
232 for (x = off; x < 16*16; x += 16) {
233 draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
234 color++;
235 }
236 }
237}
238
239static void ring2_test(uint8_t *dst, int dst_linesize, int off)
240{
241 int x, y;
242
243 for (y = 0; y < 16*16; y++) {
244 for (x = 0; x < 16*16; x++) {
245 double d = hypot(x-8*16, y-8*16);
246 double r = d/20 - (int)(d/20);
247 if (r < off/30.0) {
248 dst[x + y*dst_linesize] = 255;
249 dst[x + y*dst_linesize+256] = 0;
250 } else {
251 dst[x + y*dst_linesize] = x;
252 dst[x + y*dst_linesize+256] = x;
253 }
254 }
255 }
256}
257
259{
260 MPTestContext *test = ctx->priv;
261
262 test->max_pts = test->duration >= 0 ?
263 av_rescale_q(test->duration, AV_TIME_BASE_Q, av_inv_q(test->frame_rate)) : -1;
264 test->pts = 0;
265
266 av_log(ctx, AV_LOG_VERBOSE, "rate:%d/%d duration:%f\n",
267 test->frame_rate.num, test->frame_rate.den,
268 test->duration < 0 ? -1 : test->max_pts * av_q2d(av_inv_q(test->frame_rate)));
269 init_idct();
270
271 return 0;
272}
273
274static int config_props(AVFilterLink *outlink)
275{
276 AVFilterContext *ctx = outlink->src;
277 FilterLink *l = ff_filter_link(outlink);
278 MPTestContext *test = ctx->priv;
279 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(outlink->format);
280
281 test->hsub = pix_desc->log2_chroma_w;
282 test->vsub = pix_desc->log2_chroma_h;
283
284 outlink->w = WIDTH;
285 outlink->h = HEIGHT;
286 outlink->time_base = av_inv_q(test->frame_rate);
287 l->frame_rate = test->frame_rate;
288
289 return 0;
290}
291
292static int request_frame(AVFilterLink *outlink)
293{
294 FilterLink *outl = ff_filter_link(outlink);
295 MPTestContext *test = outlink->src->priv;
296 AVFrame *picref;
297 int w = WIDTH, h = HEIGHT,
298 cw = AV_CEIL_RSHIFT(w, test->hsub), ch = AV_CEIL_RSHIFT(h, test->vsub);
299 uint64_t frame = outl->frame_count_in / test->max_frames;
300 uint64_t mod = outl->frame_count_in % test->max_frames;
301 enum test_type tt = test->test;
302 int i;
303
304 if (test->max_pts >= 0 && test->pts > test->max_pts)
305 return AVERROR_EOF;
306 picref = ff_get_video_buffer(outlink, w, h);
307 if (!picref)
308 return AVERROR(ENOMEM);
309 picref->pts = test->pts++;
310 picref->duration = 1;
311
312 // clean image
313 for (i = 0; i < h; i++)
314 memset(picref->data[0] + i*picref->linesize[0], 0, w);
315 for (i = 0; i < ch; i++) {
316 memset(picref->data[1] + i*picref->linesize[1], 128, cw);
317 memset(picref->data[2] + i*picref->linesize[2], 128, cw);
318 }
319
320 if (tt == TEST_ALL && mod) /* draw a black frame at the beginning of each test */
321 tt = frame%(TEST_NB-1);
322
323 switch (tt) {
324 case TEST_DC_LUMA: dc_test(picref->data[0], picref->linesize[0], 256, 256, mod); break;
325 case TEST_DC_CHROMA: dc_test(picref->data[1], picref->linesize[1], 256, 256, mod); break;
326 case TEST_FREQ_LUMA: freq_test(picref->data[0], picref->linesize[0], mod); break;
327 case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], mod); break;
328 case TEST_AMP_LUMA: amp_test(picref->data[0], picref->linesize[0], mod); break;
329 case TEST_AMP_CHROMA: amp_test(picref->data[1], picref->linesize[1], mod); break;
330 case TEST_CBP: cbp_test(picref->data , picref->linesize , mod); break;
331 case TEST_MV: mv_test(picref->data[0], picref->linesize[0], mod); break;
332 case TEST_RING1: ring1_test(picref->data[0], picref->linesize[0], mod); break;
333 case TEST_RING2: ring2_test(picref->data[0], picref->linesize[0], mod); break;
334 }
335
336 return ff_filter_frame(outlink, picref);
337}
338
340 {
341 .name = "default",
342 .type = AVMEDIA_TYPE_VIDEO,
343 .request_frame = request_frame,
344 .config_props = config_props,
345 },
346};
347
349 .p.name = "mptestsrc",
350 .p.description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
351 .p.priv_class = &mptestsrc_class,
352 .p.inputs = NULL,
353 .priv_size = sizeof(MPTestContext),
354 .init = init,
357};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
const FFFilter ff_vsrc_mptestsrc
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip_uint8
Definition common.h:106
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int64_t duration
Definition ffplay.c:330
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define r
Definition input.c:42
#define WIDTH
Definition c93.c:45
#define HEIGHT
Definition c93.c:46
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_const double hypot(double x, double y)
Definition libm.h:368
uint8_t w
Definition llvidencdsp.c:39
#define FFMAX(a, b)
Definition macros.h:47
#define M_PI
Definition mathematics.h:67
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:820
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
Rational number (pair of numerator and denominator).
Definition rational.h:58
AVRational frame_rate
int test
test_type
Definition idctdsp.c:35
#define lrint
Definition tablegen.h:53
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition vf_v360.c:755
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static const AVOption mptestsrc_options[]
static double c[64]
static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
static int config_props(AVFilterLink *outlink)
static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
static void mv_test(uint8_t *dst, int dst_linesize, int off)
static void idct(uint8_t *dst, int dst_linesize, int src[64])
static void init_idct(void)
static void amp_test(uint8_t *dst, int dst_linesize, int off)
static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
static void freq_test(uint8_t *dst, int dst_linesize, int off)
static int request_frame(AVFilterLink *outlink)
static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
static const AVFilterPad mptestsrc_outputs[]
static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
test_type
@ TEST_ALL
@ TEST_CBP
@ TEST_RING2
@ TEST_DC_CHROMA
@ TEST_RING1
@ TEST_DC_LUMA
@ TEST_FREQ_CHROMA
@ TEST_FREQ_LUMA
@ TEST_NB
@ TEST_AMP_LUMA
@ TEST_AMP_CHROMA
@ TEST_MV
#define OFFSET(x)
static void ring1_test(uint8_t *dst, int dst_linesize, int off)
static void ring2_test(uint8_t *dst, int dst_linesize, int off)