]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
lavfi: decrease logging level of message showing initial 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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/opt.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/parseutils.h"
38 #include "avfilter.h"
39 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42
43 typedef struct {
44     const AVClass *class;
45     int w, h;
46     unsigned int nb_frame;
47     AVRational time_base;
48     int64_t pts, max_pts;
49     char *rate;                 ///< video frame rate
50     char *duration;             ///< total duration of the generated video
51     AVRational sar;             ///< sample aspect ratio
52     int nb_decimals;
53
54     void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
55
56     /* only used by rgbtest */
57     int rgba_map[4];
58 } TestSourceContext;
59
60 #define OFFSET(x) offsetof(TestSourceContext, x)
61
62 static const AVOption testsrc_options[]= {
63     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
64     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
65     { "rate",     "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
66     { "r",        "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
67     { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
68     { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
69     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX },
70     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0},  INT_MIN, INT_MAX },
71     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0},  INT_MIN, INT_MAX },
72     { NULL },
73 };
74
75 static av_cold int init(AVFilterContext *ctx, const char *args)
76 {
77     TestSourceContext *test = ctx->priv;
78     AVRational frame_rate_q;
79     int64_t duration = -1;
80     int ret = 0;
81
82     av_opt_set_defaults(test);
83
84     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
85         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
86         return ret;
87     }
88
89     if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0) {
90         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
91         return ret;
92     }
93     av_freep(&test->rate);
94
95     if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
96         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
97         return ret;
98     }
99     av_freep(&test->duration);
100
101     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
102         av_log(ctx, AV_LOG_WARNING,
103                "Option 'decimals' is ignored with source '%s'\n",
104                ctx->filter->name);
105     }
106
107     test->time_base.num = frame_rate_q.den;
108     test->time_base.den = frame_rate_q.num;
109     test->max_pts = duration >= 0 ?
110         av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
111     test->nb_frame = 0;
112     test->pts = 0;
113
114     av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
115            test->w, test->h, frame_rate_q.num, frame_rate_q.den,
116            duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
117            test->sar.num, test->sar.den);
118     return 0;
119 }
120
121 static int config_props(AVFilterLink *outlink)
122 {
123     TestSourceContext *test = outlink->src->priv;
124
125     outlink->w = test->w;
126     outlink->h = test->h;
127     outlink->sample_aspect_ratio = test->sar;
128     outlink->time_base = test->time_base;
129
130     return 0;
131 }
132
133 static int request_frame(AVFilterLink *outlink)
134 {
135     TestSourceContext *test = outlink->src->priv;
136     AVFilterBufferRef *picref;
137
138     if (test->max_pts >= 0 && test->pts >= test->max_pts)
139         return AVERROR_EOF;
140     picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
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->sample_aspect_ratio = test->sar;
147     test->fill_picture_fn(outlink->src, picref);
148     test->nb_frame++;
149
150     ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
151     ff_draw_slice(outlink, 0, picref->video->h, 1);
152     ff_end_frame(outlink);
153     avfilter_unref_buffer(picref);
154
155     return 0;
156 }
157
158 #if CONFIG_NULLSRC_FILTER
159
160 static const AVClass nullsrc_class = {
161     .class_name = "nullsrc",
162     .item_name  = av_default_item_name,
163     .option     = testsrc_options,
164     .version    = LIBAVUTIL_VERSION_INT,
165     .category   = AV_CLASS_CATEGORY_FILTER,
166 };
167
168 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
169
170 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
171 {
172     TestSourceContext *test = ctx->priv;
173
174     test->class = &nullsrc_class;
175     test->fill_picture_fn = nullsrc_fill_picture;
176     return init(ctx, args);
177 }
178
179 AVFilter avfilter_vsrc_nullsrc = {
180     .name        = "nullsrc",
181     .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
182     .init       = nullsrc_init,
183     .priv_size  = sizeof(TestSourceContext),
184
185     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
186     .outputs   = (const AVFilterPad[]) {{ .name = "default",
187                                     .type = AVMEDIA_TYPE_VIDEO,
188                                     .request_frame = request_frame,
189                                     .config_props  = config_props, },
190                                   { .name = NULL}},
191 };
192
193 #endif /* CONFIG_NULLSRC_FILTER */
194
195 #if CONFIG_TESTSRC_FILTER
196
197 AVFILTER_DEFINE_CLASS(testsrc);
198
199 /**
200  * Fill a rectangle with value val.
201  *
202  * @param val the RGB value to set
203  * @param dst pointer to the destination buffer to fill
204  * @param dst_linesize linesize of destination
205  * @param segment_width width of the segment
206  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
207  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
208  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
209  * @param h height of the rectangle to draw, expressed as a number of segment_width units
210  */
211 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
212                            unsigned x, unsigned y, unsigned w, unsigned h)
213 {
214     int i;
215     int step = 3;
216
217     dst += segment_width * (step * x + y * dst_linesize);
218     w *= segment_width * step;
219     h *= segment_width;
220     for (i = 0; i < h; i++) {
221         memset(dst, val, w);
222         dst += dst_linesize;
223     }
224 }
225
226 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
227                        unsigned segment_width)
228 {
229 #define TOP_HBAR        1
230 #define MID_HBAR        2
231 #define BOT_HBAR        4
232 #define LEFT_TOP_VBAR   8
233 #define LEFT_BOT_VBAR  16
234 #define RIGHT_TOP_VBAR 32
235 #define RIGHT_BOT_VBAR 64
236     struct {
237         int x, y, w, h;
238     } segments[] = {
239         { 1,  0, 5, 1 }, /* TOP_HBAR */
240         { 1,  6, 5, 1 }, /* MID_HBAR */
241         { 1, 12, 5, 1 }, /* BOT_HBAR */
242         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
243         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
244         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
245         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
246     };
247     static const unsigned char masks[10] = {
248         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
249         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
250         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
251         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
252         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
253         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
254         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
255         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
256         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
257         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
258     };
259     unsigned mask = masks[digit];
260     int i;
261
262     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
263     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
264         if (mask & (1<<i))
265             draw_rectangle(255, dst, dst_linesize, segment_width,
266                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
267 }
268
269 #define GRADIENT_SIZE (6 * 256)
270
271 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
272 {
273     TestSourceContext *test = ctx->priv;
274     uint8_t *p, *p0;
275     int x, y;
276     int color, color_rest;
277     int icolor;
278     int radius;
279     int quad0, quad;
280     int dquad_x, dquad_y;
281     int grad, dgrad, rgrad, drgrad;
282     int seg_size;
283     int second;
284     int i;
285     uint8_t *data = picref->data[0];
286     int width  = picref->video->w;
287     int height = picref->video->h;
288
289     /* draw colored bars and circle */
290     radius = (width + height) / 4;
291     quad0 = width * width / 4 + height * height / 4 - radius * radius;
292     dquad_y = 1 - height;
293     p0 = data;
294     for (y = 0; y < height; y++) {
295         p = p0;
296         color = 0;
297         color_rest = 0;
298         quad = quad0;
299         dquad_x = 1 - width;
300         for (x = 0; x < width; x++) {
301             icolor = color;
302             if (quad < 0)
303                 icolor ^= 7;
304             quad += dquad_x;
305             dquad_x += 2;
306             *(p++) = icolor & 1 ? 255 : 0;
307             *(p++) = icolor & 2 ? 255 : 0;
308             *(p++) = icolor & 4 ? 255 : 0;
309             color_rest += 8;
310             if (color_rest >= width) {
311                 color_rest -= width;
312                 color++;
313             }
314         }
315         quad0 += dquad_y;
316         dquad_y += 2;
317         p0 += picref->linesize[0];
318     }
319
320     /* draw sliding color line */
321     p0 = p = data + picref->linesize[0] * height * 3/4;
322     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
323         GRADIENT_SIZE;
324     rgrad = 0;
325     dgrad = GRADIENT_SIZE / width;
326     drgrad = GRADIENT_SIZE % width;
327     for (x = 0; x < width; x++) {
328         *(p++) =
329             grad < 256 || grad >= 5 * 256 ? 255 :
330             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
331             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
332         *(p++) =
333             grad >= 4 * 256 ? 0 :
334             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
335             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
336         *(p++) =
337             grad < 2 * 256 ? 0 :
338             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
339             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
340         grad += dgrad;
341         rgrad += drgrad;
342         if (rgrad >= GRADIENT_SIZE) {
343             grad++;
344             rgrad -= GRADIENT_SIZE;
345         }
346         if (grad >= GRADIENT_SIZE)
347             grad -= GRADIENT_SIZE;
348     }
349     p = p0;
350     for (y = height / 8; y > 0; y--) {
351         memcpy(p+picref->linesize[0], p, 3 * width);
352         p += picref->linesize[0];
353     }
354
355     /* draw digits */
356     seg_size = width / 80;
357     if (seg_size >= 1 && height >= 13 * seg_size) {
358         double time = av_q2d(test->time_base) * test->nb_frame *
359                       pow(10, test->nb_decimals);
360         if (time > INT_MAX)
361             return;
362         second = (int)time;
363         x = width - (width - seg_size * 64) / 2;
364         y = (height - seg_size * 13) / 2;
365         p = data + (x*3 + y * picref->linesize[0]);
366         for (i = 0; i < 8; i++) {
367             p -= 3 * 8 * seg_size;
368             draw_digit(second % 10, p, picref->linesize[0], seg_size);
369             second /= 10;
370             if (second == 0)
371                 break;
372         }
373     }
374 }
375
376 static av_cold int test_init(AVFilterContext *ctx, const char *args)
377 {
378     TestSourceContext *test = ctx->priv;
379
380     test->class = &testsrc_class;
381     test->fill_picture_fn = test_fill_picture;
382     return init(ctx, args);
383 }
384
385 static int test_query_formats(AVFilterContext *ctx)
386 {
387     static const enum PixelFormat pix_fmts[] = {
388         PIX_FMT_RGB24, PIX_FMT_NONE
389     };
390     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
391     return 0;
392 }
393
394 AVFilter avfilter_vsrc_testsrc = {
395     .name      = "testsrc",
396     .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
397     .priv_size = sizeof(TestSourceContext),
398     .init      = test_init,
399
400     .query_formats   = test_query_formats,
401
402     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
403
404     .outputs   = (const AVFilterPad[]) {{ .name = "default",
405                                     .type = AVMEDIA_TYPE_VIDEO,
406                                     .request_frame = request_frame,
407                                     .config_props  = config_props, },
408                                   { .name = NULL }},
409 };
410
411 #endif /* CONFIG_TESTSRC_FILTER */
412
413 #if CONFIG_RGBTESTSRC_FILTER
414
415 static const AVClass rgbtestsrc_class = {
416     .class_name = "rgbtestsrc",
417     .item_name  = av_default_item_name,
418     .option     = testsrc_options,
419     .version    = LIBAVUTIL_VERSION_INT,
420     .category   = AV_CLASS_CATEGORY_FILTER,
421 };
422
423 #define R 0
424 #define G 1
425 #define B 2
426 #define A 3
427
428 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
429                               int x, int y, int r, int g, int b, enum PixelFormat fmt,
430                               int rgba_map[4])
431 {
432     int32_t v;
433     uint8_t *p;
434
435     switch (fmt) {
436     case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
437     case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
438     case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
439     case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
440     case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
441     case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
442     case PIX_FMT_RGB24:
443     case PIX_FMT_BGR24:
444         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
445         p = dst + 3*x + y*dst_linesize;
446         AV_WL24(p, v);
447         break;
448     case PIX_FMT_RGBA:
449     case PIX_FMT_BGRA:
450     case PIX_FMT_ARGB:
451     case PIX_FMT_ABGR:
452         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
453         p = dst + 4*x + y*dst_linesize;
454         AV_WL32(p, v);
455         break;
456     }
457 }
458
459 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
460 {
461     TestSourceContext *test = ctx->priv;
462     int x, y, w = picref->video->w, h = picref->video->h;
463
464     for (y = 0; y < h; y++) {
465          for (x = 0; x < picref->video->w; x++) {
466              int c = 256*x/w;
467              int r = 0, g = 0, b = 0;
468
469              if      (3*y < h  ) r = c;
470              else if (3*y < 2*h) g = c;
471              else                b = c;
472
473              rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
474                                ctx->outputs[0]->format, test->rgba_map);
475          }
476      }
477 }
478
479 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
480 {
481     TestSourceContext *test = ctx->priv;
482
483     test->class = &rgbtestsrc_class;
484     test->fill_picture_fn = rgbtest_fill_picture;
485     return init(ctx, args);
486 }
487
488 static int rgbtest_query_formats(AVFilterContext *ctx)
489 {
490     static const enum PixelFormat pix_fmts[] = {
491         PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
492         PIX_FMT_BGR24, PIX_FMT_RGB24,
493         PIX_FMT_RGB444, PIX_FMT_BGR444,
494         PIX_FMT_RGB565, PIX_FMT_BGR565,
495         PIX_FMT_RGB555, PIX_FMT_BGR555,
496         PIX_FMT_NONE
497     };
498     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
499     return 0;
500 }
501
502 static int rgbtest_config_props(AVFilterLink *outlink)
503 {
504     TestSourceContext *test = outlink->src->priv;
505
506     switch (outlink->format) {
507     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;
508     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;
509     case PIX_FMT_RGBA:
510     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;
511     case PIX_FMT_BGRA:
512     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;
513     }
514
515     return config_props(outlink);
516 }
517
518 AVFilter avfilter_vsrc_rgbtestsrc = {
519     .name      = "rgbtestsrc",
520     .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
521     .priv_size = sizeof(TestSourceContext),
522     .init      = rgbtest_init,
523
524     .query_formats   = rgbtest_query_formats,
525
526     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
527
528     .outputs   = (const AVFilterPad[]) {{ .name = "default",
529                                     .type = AVMEDIA_TYPE_VIDEO,
530                                     .request_frame = request_frame,
531                                     .config_props  = rgbtest_config_props, },
532                                   { .name = NULL }},
533 };
534
535 #endif /* CONFIG_RGBTESTSRC_FILTER */