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