]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
Merge remote-tracking branch 'qatar/master'
[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  * Based on the test pattern generator demuxer by Nicolas George:
25  * http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2007-October/037845.html
26  */
27
28 #include <float.h>
29
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32 #include "avfilter.h"
33
34 typedef struct {
35     const AVClass *class;
36     int h, w;
37     unsigned int nb_frame;
38     AVRational time_base;
39     int64_t pts, max_pts;
40     char *size;                 ///< video frame size
41     char *rate;                 ///< video frame rate
42     char *duration;             ///< total duration of the generated video
43 } TestSourceContext;
44
45 #define OFFSET(x) offsetof(TestSourceContext, x)
46
47 static const AVOption testsrc_options[]= {
48     { "size",     "set video size",     OFFSET(size),     FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
49     { "s",        "set video size",     OFFSET(size),     FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
50     { "rate",     "set video rate",     OFFSET(rate),     FF_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
51     { "r",        "set video rate",     OFFSET(rate),     FF_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
52     { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
53     { NULL },
54 };
55
56 static const char *testsrc_get_name(void *ctx)
57 {
58     return "testsrc";
59 }
60
61 static const AVClass testsrc_class = {
62     "TestSourceContext",
63     testsrc_get_name,
64     testsrc_options
65 };
66
67 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
68 {
69     TestSourceContext *test = ctx->priv;
70     AVRational frame_rate_q;
71     int64_t duration = -1;
72     int ret = 0;
73
74     test->class = &testsrc_class;
75     av_opt_set_defaults2(test, 0, 0);
76
77     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
78         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
79         return ret;
80     }
81
82     if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
83         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
84         return ret;
85     }
86
87     if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
88         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
89         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
90         return ret;
91     }
92
93     if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
94         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
95         return ret;
96     }
97
98     test->time_base.num = frame_rate_q.den;
99     test->time_base.den = frame_rate_q.num;
100     test->max_pts = duration >= 0 ?
101         av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
102     test->nb_frame = 0;
103     test->pts = 0;
104
105     av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f\n",
106            test->w, test->h, frame_rate_q.num, frame_rate_q.den,
107            duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
108     return 0;
109 }
110
111 static int config_props(AVFilterLink *outlink)
112 {
113     TestSourceContext *test = outlink->src->priv;
114
115     outlink->w = test->w;
116     outlink->h = test->h;
117     outlink->time_base = test->time_base;
118
119     return 0;
120 }
121
122 /**
123  * Fill a rectangle with value val.
124  *
125  * @param val the RGB value to set
126  * @param dst pointer to the destination buffer to fill
127  * @param dst_linesize linesize of destination
128  * @param segment_width width of the segment
129  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
130  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
131  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
132  * @param h height of the rectangle to draw, expressed as a number of segment_width units
133  */
134 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
135                            unsigned x, unsigned y, unsigned w, unsigned h)
136 {
137     int i;
138     int step = 3;
139
140     dst += segment_width * (step * x + y * dst_linesize);
141     w *= segment_width * step;
142     h *= segment_width;
143     for (i = 0; i < h; i++) {
144         memset(dst, val, w);
145         dst += dst_linesize;
146     }
147 }
148
149 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
150                        unsigned segment_width)
151 {
152 #define TOP_HBAR        1
153 #define MID_HBAR        2
154 #define BOT_HBAR        4
155 #define LEFT_TOP_VBAR   8
156 #define LEFT_BOT_VBAR  16
157 #define RIGHT_TOP_VBAR 32
158 #define RIGHT_BOT_VBAR 64
159     struct {
160         int x, y, w, h;
161     } segments[] = {
162         { 1,  0, 5, 1 }, /* TOP_HBAR */
163         { 1,  6, 5, 1 }, /* MID_HBAR */
164         { 1, 12, 5, 1 }, /* BOT_HBAR */
165         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
166         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
167         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
168         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
169     };
170     static const unsigned char masks[10] = {
171         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
172         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
173         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
174         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
175         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
176         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
177         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
178         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
179         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
180         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
181     };
182     unsigned mask = masks[digit];
183     int i;
184
185     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
186     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
187         if (mask & (1<<i))
188             draw_rectangle(255, dst, dst_linesize, segment_width,
189                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
190 }
191
192 #define GRADIENT_SIZE (6 * 256)
193
194 static void fill_picture(TestSourceContext *test, AVFilterBufferRef *picref)
195 {
196     uint8_t *p, *p0;
197     int x, y;
198     int color, color_rest;
199     int icolor;
200     int radius;
201     int quad0, quad;
202     int dquad_x, dquad_y;
203     int grad, dgrad, rgrad, drgrad;
204     int seg_size;
205     int second;
206     int i;
207     uint8_t *data = picref->data[0];
208     int width  = picref->video->w;
209     int height = picref->video->h;
210
211     /* draw colored bars and circle */
212     radius = (width + height) / 4;
213     quad0 = width * width / 4 + height * height / 4 - radius * radius;
214     dquad_y = 1 - height;
215     p0 = data;
216     for (y = 0; y < height; y++) {
217         p = p0;
218         color = 0;
219         color_rest = 0;
220         quad = quad0;
221         dquad_x = 1 - width;
222         for (x = 0; x < width; x++) {
223             icolor = color;
224             if (quad < 0)
225                 icolor ^= 7;
226             quad += dquad_x;
227             dquad_x += 2;
228             *(p++) = icolor & 1 ? 255 : 0;
229             *(p++) = icolor & 2 ? 255 : 0;
230             *(p++) = icolor & 4 ? 255 : 0;
231             color_rest += 8;
232             if (color_rest >= width) {
233                 color_rest -= width;
234                 color++;
235             }
236         }
237         quad0 += dquad_y;
238         dquad_y += 2;
239         p0 += picref->linesize[0];
240     }
241
242     /* draw sliding color line */
243     p = data + picref->linesize[0] * height * 3/4;
244     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
245         GRADIENT_SIZE;
246     rgrad = 0;
247     dgrad = GRADIENT_SIZE / width;
248     drgrad = GRADIENT_SIZE % width;
249     for (x = 0; x < width; x++) {
250         *(p++) =
251             grad < 256 || grad >= 5 * 256 ? 255 :
252             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
253             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
254         *(p++) =
255             grad >= 4 * 256 ? 0 :
256             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
257             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
258         *(p++) =
259             grad < 2 * 256 ? 0 :
260             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
261             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
262         grad += dgrad;
263         rgrad += drgrad;
264         if (rgrad >= GRADIENT_SIZE) {
265             grad++;
266             rgrad -= GRADIENT_SIZE;
267         }
268         if (grad >= GRADIENT_SIZE)
269             grad -= GRADIENT_SIZE;
270     }
271     for (y = height / 8; y > 0; y--) {
272         memcpy(p, p - picref->linesize[0], 3 * width);
273         p += picref->linesize[0];
274     }
275
276     /* draw digits */
277     seg_size = width / 80;
278     if (seg_size >= 1 && height >= 13 * seg_size) {
279         second = test->nb_frame * test->time_base.num / test->time_base.den;
280         x = width - (width - seg_size * 64) / 2;
281         y = (height - seg_size * 13) / 2;
282         p = data + (x*3 + y * picref->linesize[0]);
283         for (i = 0; i < 8; i++) {
284             p -= 3 * 8 * seg_size;
285             draw_digit(second % 10, p, picref->linesize[0], seg_size);
286             second /= 10;
287             if (second == 0)
288                 break;
289         }
290     }
291 }
292
293 static int request_frame(AVFilterLink *outlink)
294 {
295     TestSourceContext *test = outlink->src->priv;
296     AVFilterBufferRef *picref;
297
298     if (test->max_pts >= 0 && test->pts > test->max_pts)
299         return AVERROR_EOF;
300     picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
301                                        test->w, test->h);
302     picref->pts = test->pts++;
303     test->nb_frame++;
304     fill_picture(test, picref);
305
306     avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
307     avfilter_draw_slice(outlink, 0, picref->video->h, 1);
308     avfilter_end_frame(outlink);
309     avfilter_unref_buffer(picref);
310
311     return 0;
312 }
313
314 static int query_formats(AVFilterContext *ctx)
315 {
316     static const enum PixelFormat pix_fmts[] = {
317         PIX_FMT_RGB24, PIX_FMT_NONE
318     };
319     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
320     return 0;
321 }
322
323 AVFilter avfilter_vsrc_testsrc = {
324     .name      = "testsrc",
325     .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
326     .priv_size = sizeof(TestSourceContext),
327     .init      = init,
328
329     .query_formats   = query_formats,
330
331     .inputs    = (AVFilterPad[]) {{ .name = NULL}},
332
333     .outputs   = (AVFilterPad[]) {{ .name = "default",
334                                     .type = AVMEDIA_TYPE_VIDEO,
335                                     .request_frame = request_frame,
336                                     .config_props  = config_props, },
337                                   { .name = NULL }},
338 };