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