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