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