]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
doxygen: qdm2: Drop documentation for non-existing function parameters
[ffmpeg] / libavfilter / vsrc_testsrc.c
1 /*
2  * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3  * Copyright (c) 2011 Stefano Sabatini
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Misc test sources.
25  *
26  * testsrc is based on the test pattern generator demuxer by Nicolas George:
27  * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
28  *
29  * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
30  * Michael Niedermayer.
31  */
32
33 #include <float.h>
34
35 #include "libavutil/mathematics.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/parseutils.h"
39 #include "avfilter.h"
40 #include "formats.h"
41 #include "internal.h"
42 #include "video.h"
43
44 typedef struct {
45     const AVClass *class;
46     int h, w;
47     unsigned int nb_frame;
48     AVRational time_base;
49     int64_t pts, max_pts;
50     char *size;                 ///< video frame size
51     char *rate;                 ///< video frame rate
52     char *duration;             ///< total duration of the generated video
53     AVRational sar;             ///< sample aspect ratio
54
55     void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
56
57     /* only used by rgbtest */
58     int rgba_map[4];
59 } TestSourceContext;
60
61 #define OFFSET(x) offsetof(TestSourceContext, x)
62
63 static const AVOption testsrc_options[] = {
64     { "size",     "set video size",     OFFSET(size),     FF_OPT_TYPE_STRING, {.str = "320x240"}},
65     { "s",        "set video size",     OFFSET(size),     FF_OPT_TYPE_STRING, {.str = "320x240"}},
66     { "rate",     "set video rate",     OFFSET(rate),     FF_OPT_TYPE_STRING, {.str = "25"},    },
67     { "r",        "set video rate",     OFFSET(rate),     FF_OPT_TYPE_STRING, {.str = "25"},    },
68     { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL},    },
69     { "sar",      "set video sample aspect ratio", OFFSET(sar), FF_OPT_TYPE_RATIONAL, {1},  0, INT_MAX },
70     { NULL },
71 };
72
73 static av_cold int init_common(AVFilterContext *ctx, const char *args)
74 {
75     TestSourceContext *test = ctx->priv;
76     AVRational frame_rate_q;
77     int64_t duration = -1;
78     int ret = 0;
79
80     av_opt_set_defaults(test);
81
82     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
83         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
84         return ret;
85     }
86
87     if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
88         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
89         return ret;
90     }
91
92     if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
93         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
94         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
95         return ret;
96     }
97
98     if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
99         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
100         return ret;
101     }
102
103     test->time_base.num = frame_rate_q.den;
104     test->time_base.den = frame_rate_q.num;
105     test->max_pts = duration >= 0 ?
106         av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
107     test->nb_frame = 0;
108     test->pts = 0;
109
110     av_log(ctx, AV_LOG_DEBUG, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
111            test->w, test->h, frame_rate_q.num, frame_rate_q.den,
112            duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
113            test->sar.num, test->sar.den);
114     return 0;
115 }
116
117 static int config_props(AVFilterLink *outlink)
118 {
119     TestSourceContext *test = outlink->src->priv;
120
121     outlink->w = test->w;
122     outlink->h = test->h;
123     outlink->sample_aspect_ratio = test->sar;
124     outlink->time_base = test->time_base;
125
126     return 0;
127 }
128
129 static int request_frame(AVFilterLink *outlink)
130 {
131     TestSourceContext *test = outlink->src->priv;
132     AVFilterBufferRef *picref;
133     int ret;
134
135     if (test->max_pts >= 0 && test->pts > test->max_pts)
136         return AVERROR_EOF;
137     picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
138     if (!picref)
139         return AVERROR(ENOMEM);
140
141     picref->pts = test->pts++;
142     picref->pos = -1;
143     picref->video->key_frame = 1;
144     picref->video->interlaced = 0;
145     picref->video->pict_type = AV_PICTURE_TYPE_I;
146     picref->video->pixel_aspect = test->sar;
147     test->nb_frame++;
148     test->fill_picture_fn(outlink->src, picref);
149
150     if ((ret = ff_start_frame(outlink, picref)) < 0 ||
151         (ret = ff_draw_slice(outlink, 0, test->h, 1)) < 0 ||
152         (ret = ff_end_frame(outlink)) < 0)
153         return ret;
154
155     return 0;
156 }
157
158 #if CONFIG_TESTSRC_FILTER
159
160 static const char *testsrc_get_name(void *ctx)
161 {
162     return "testsrc";
163 }
164
165 static const AVClass testsrc_class = {
166     .class_name = "TestSourceContext",
167     .item_name  = testsrc_get_name,
168     .option     = testsrc_options,
169 };
170
171 /**
172  * Fill a rectangle with value val.
173  *
174  * @param val the RGB value to set
175  * @param dst pointer to the destination buffer to fill
176  * @param dst_linesize linesize of destination
177  * @param segment_width width of the segment
178  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
179  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
180  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
181  * @param h height of the rectangle to draw, expressed as a number of segment_width units
182  */
183 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
184                            unsigned x, unsigned y, unsigned w, unsigned h)
185 {
186     int i;
187     int step = 3;
188
189     dst += segment_width * (step * x + y * dst_linesize);
190     w *= segment_width * step;
191     h *= segment_width;
192     for (i = 0; i < h; i++) {
193         memset(dst, val, w);
194         dst += dst_linesize;
195     }
196 }
197
198 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
199                        unsigned segment_width)
200 {
201 #define TOP_HBAR        1
202 #define MID_HBAR        2
203 #define BOT_HBAR        4
204 #define LEFT_TOP_VBAR   8
205 #define LEFT_BOT_VBAR  16
206 #define RIGHT_TOP_VBAR 32
207 #define RIGHT_BOT_VBAR 64
208     struct {
209         int x, y, w, h;
210     } segments[] = {
211         { 1,  0, 5, 1 }, /* TOP_HBAR */
212         { 1,  6, 5, 1 }, /* MID_HBAR */
213         { 1, 12, 5, 1 }, /* BOT_HBAR */
214         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
215         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
216         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
217         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
218     };
219     static const unsigned char masks[10] = {
220         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
221         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
222         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
223         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
224         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
225         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
226         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
227         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
228         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
229         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
230     };
231     unsigned mask = masks[digit];
232     int i;
233
234     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
235     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
236         if (mask & (1<<i))
237             draw_rectangle(255, dst, dst_linesize, segment_width,
238                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
239 }
240
241 #define GRADIENT_SIZE (6 * 256)
242
243 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
244 {
245     TestSourceContext *test = ctx->priv;
246     uint8_t *p, *p0;
247     int x, y;
248     int color, color_rest;
249     int icolor;
250     int radius;
251     int quad0, quad;
252     int dquad_x, dquad_y;
253     int grad, dgrad, rgrad, drgrad;
254     int seg_size;
255     int second;
256     int i;
257     uint8_t *data = picref->data[0];
258     int width  = picref->video->w;
259     int height = picref->video->h;
260
261     /* draw colored bars and circle */
262     radius = (width + height) / 4;
263     quad0 = width * width / 4 + height * height / 4 - radius * radius;
264     dquad_y = 1 - height;
265     p0 = data;
266     for (y = 0; y < height; y++) {
267         p = p0;
268         color = 0;
269         color_rest = 0;
270         quad = quad0;
271         dquad_x = 1 - width;
272         for (x = 0; x < width; x++) {
273             icolor = color;
274             if (quad < 0)
275                 icolor ^= 7;
276             quad += dquad_x;
277             dquad_x += 2;
278             *(p++) = icolor & 1 ? 255 : 0;
279             *(p++) = icolor & 2 ? 255 : 0;
280             *(p++) = icolor & 4 ? 255 : 0;
281             color_rest += 8;
282             if (color_rest >= width) {
283                 color_rest -= width;
284                 color++;
285             }
286         }
287         quad0 += dquad_y;
288         dquad_y += 2;
289         p0 += picref->linesize[0];
290     }
291
292     /* draw sliding color line */
293     p = data + picref->linesize[0] * height * 3/4;
294     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
295         GRADIENT_SIZE;
296     rgrad = 0;
297     dgrad = GRADIENT_SIZE / width;
298     drgrad = GRADIENT_SIZE % width;
299     for (x = 0; x < width; x++) {
300         *(p++) =
301             grad < 256 || grad >= 5 * 256 ? 255 :
302             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
303             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
304         *(p++) =
305             grad >= 4 * 256 ? 0 :
306             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
307             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
308         *(p++) =
309             grad < 2 * 256 ? 0 :
310             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
311             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
312         grad += dgrad;
313         rgrad += drgrad;
314         if (rgrad >= GRADIENT_SIZE) {
315             grad++;
316             rgrad -= GRADIENT_SIZE;
317         }
318         if (grad >= GRADIENT_SIZE)
319             grad -= GRADIENT_SIZE;
320     }
321     for (y = height / 8; y > 0; y--) {
322         memcpy(p, p - picref->linesize[0], 3 * width);
323         p += picref->linesize[0];
324     }
325
326     /* draw digits */
327     seg_size = width / 80;
328     if (seg_size >= 1 && height >= 13 * seg_size) {
329         second = test->nb_frame * test->time_base.num / test->time_base.den;
330         x = width - (width - seg_size * 64) / 2;
331         y = (height - seg_size * 13) / 2;
332         p = data + (x*3 + y * picref->linesize[0]);
333         for (i = 0; i < 8; i++) {
334             p -= 3 * 8 * seg_size;
335             draw_digit(second % 10, p, picref->linesize[0], seg_size);
336             second /= 10;
337             if (second == 0)
338                 break;
339         }
340     }
341 }
342
343 static av_cold int test_init(AVFilterContext *ctx, const char *args)
344 {
345     TestSourceContext *test = ctx->priv;
346
347     test->class = &testsrc_class;
348     test->fill_picture_fn = test_fill_picture;
349     return init_common(ctx, args);
350 }
351
352 static int test_query_formats(AVFilterContext *ctx)
353 {
354     static const enum PixelFormat pix_fmts[] = {
355         PIX_FMT_RGB24, PIX_FMT_NONE
356     };
357     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
358     return 0;
359 }
360
361 AVFilter avfilter_vsrc_testsrc = {
362     .name          = "testsrc",
363     .description   = NULL_IF_CONFIG_SMALL("Generate test pattern."),
364     .priv_size     = sizeof(TestSourceContext),
365     .init          = test_init,
366
367     .query_formats = test_query_formats,
368
369     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
370
371     .outputs   = (const AVFilterPad[]) {{ .name = "default",
372                                           .type = AVMEDIA_TYPE_VIDEO,
373                                           .request_frame = request_frame,
374                                           .config_props  = config_props, },
375                                         { .name = NULL }},
376 };
377
378 #endif /* CONFIG_TESTSRC_FILTER */
379
380 #if CONFIG_RGBTESTSRC_FILTER
381
382 static const char *rgbtestsrc_get_name(void *ctx)
383 {
384     return "rgbtestsrc";
385 }
386
387 static const AVClass rgbtestsrc_class = {
388     .class_name = "RGBTestSourceContext",
389     .item_name  = rgbtestsrc_get_name,
390     .option     = testsrc_options,
391 };
392
393 #define R 0
394 #define G 1
395 #define B 2
396 #define A 3
397
398 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
399                               int x, int y, int r, int g, int b, enum PixelFormat fmt,
400                               int rgba_map[4])
401 {
402     int32_t v;
403     uint8_t *p;
404
405     switch (fmt) {
406     case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
407     case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
408     case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
409     case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
410     case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
411     case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
412     case PIX_FMT_RGB24:
413     case PIX_FMT_BGR24:
414         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
415         p = dst + 3*x + y*dst_linesize;
416         AV_WL24(p, v);
417         break;
418     case PIX_FMT_RGBA:
419     case PIX_FMT_BGRA:
420     case PIX_FMT_ARGB:
421     case PIX_FMT_ABGR:
422         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
423         p = dst + 4*x + y*dst_linesize;
424         AV_WL32(p, v);
425         break;
426     }
427 }
428
429 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
430 {
431     TestSourceContext *test = ctx->priv;
432     int x, y, w = picref->video->w, h = picref->video->h;
433
434     for (y = 0; y < h; y++) {
435          for (x = 0; x < picref->video->w; x++) {
436              int c = 256*x/w;
437              int r = 0, g = 0, b = 0;
438
439              if      (3*y < h  ) r = c;
440              else if (3*y < 2*h) g = c;
441              else                b = c;
442
443              rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
444                                ctx->outputs[0]->format, test->rgba_map);
445          }
446      }
447 }
448
449 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
450 {
451     TestSourceContext *test = ctx->priv;
452
453     test->class = &rgbtestsrc_class;
454     test->fill_picture_fn = rgbtest_fill_picture;
455     return init_common(ctx, args);
456 }
457
458 static int rgbtest_query_formats(AVFilterContext *ctx)
459 {
460     static const enum PixelFormat pix_fmts[] = {
461         PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
462         PIX_FMT_BGR24, PIX_FMT_RGB24,
463         PIX_FMT_RGB444, PIX_FMT_BGR444,
464         PIX_FMT_RGB565, PIX_FMT_BGR565,
465         PIX_FMT_RGB555, PIX_FMT_BGR555,
466         PIX_FMT_NONE
467     };
468     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
469     return 0;
470 }
471
472 static int rgbtest_config_props(AVFilterLink *outlink)
473 {
474     TestSourceContext *test = outlink->src->priv;
475
476     switch (outlink->format) {
477     case PIX_FMT_ARGB:  test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
478     case PIX_FMT_ABGR:  test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
479     case PIX_FMT_RGBA:
480     case PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
481     case PIX_FMT_BGRA:
482     case PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
483     }
484
485     return config_props(outlink);
486 }
487
488 AVFilter avfilter_vsrc_rgbtestsrc = {
489     .name          = "rgbtestsrc",
490     .description   = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
491     .priv_size     = sizeof(TestSourceContext),
492     .init          = rgbtest_init,
493
494     .query_formats = rgbtest_query_formats,
495
496     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
497
498     .outputs   = (const AVFilterPad[]) {{ .name = "default",
499                                           .type = AVMEDIA_TYPE_VIDEO,
500                                           .request_frame = request_frame,
501                                           .config_props  = rgbtest_config_props, },
502                                         { .name = NULL }},
503 };
504
505 #endif /* CONFIG_RGBTESTSRC_FILTER */