9939b9186e18bd94f8960a85b284461a7d1a97c0
[platform/upstream/gstreamer.git] / gst / videotestsrc / gstvideotestsrc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2002> David A. Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-videotestsrc
23  *
24  * The videotestsrc element is used to produce test video data in a wide variety
25  * of formats. The video test data produced can be controlled with the "pattern"
26  * property.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch -v videotestsrc pattern=snow ! ximagesink
32  * ]| Shows random noise in an X window.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include "gstvideotestsrc.h"
40 #include "gstvideotestsrcorc.h"
41 #include "videotestsrc.h"
42
43 #include <string.h>
44 #include <stdlib.h>
45
46 GST_DEBUG_CATEGORY_STATIC (video_test_src_debug);
47 #define GST_CAT_DEFAULT video_test_src_debug
48
49 #define DEFAULT_PATTERN            GST_VIDEO_TEST_SRC_SMPTE
50 #define DEFAULT_TIMESTAMP_OFFSET   0
51 #define DEFAULT_IS_LIVE            FALSE
52 #define DEFAULT_COLOR_SPEC         GST_VIDEO_TEST_SRC_BT601
53 #define DEFAULT_FOREGROUND_COLOR   0xffffffff
54 #define DEFAULT_BACKGROUND_COLOR   0xff000000
55 #define DEFAULT_HORIZONTAL_SPEED   0
56
57 enum
58 {
59   PROP_0,
60   PROP_PATTERN,
61   PROP_TIMESTAMP_OFFSET,
62   PROP_IS_LIVE,
63   PROP_K0,
64   PROP_KX,
65   PROP_KY,
66   PROP_KT,
67   PROP_KXT,
68   PROP_KYT,
69   PROP_KXY,
70   PROP_KX2,
71   PROP_KY2,
72   PROP_KT2,
73   PROP_XOFFSET,
74   PROP_YOFFSET,
75   PROP_FOREGROUND_COLOR,
76   PROP_BACKGROUND_COLOR,
77   PROP_HORIZONTAL_SPEED,
78   PROP_LAST
79 };
80
81
82 #define VTS_VIDEO_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ";" \
83   "video/x-bayer, format=(string) { bggr, rggb, grbg, gbrg }, "        \
84   "width = " GST_VIDEO_SIZE_RANGE ", "                                 \
85   "height = " GST_VIDEO_SIZE_RANGE ", "                                \
86   "framerate = " GST_VIDEO_FPS_RANGE
87
88
89 static GstStaticPadTemplate gst_video_test_src_template =
90 GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS (VTS_VIDEO_CAPS)
94     );
95
96 #define gst_video_test_src_parent_class parent_class
97 G_DEFINE_TYPE (GstVideoTestSrc, gst_video_test_src, GST_TYPE_PUSH_SRC);
98
99 static void gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
100     int pattern_type);
101 static void gst_video_test_src_set_property (GObject * object, guint prop_id,
102     const GValue * value, GParamSpec * pspec);
103 static void gst_video_test_src_get_property (GObject * object, guint prop_id,
104     GValue * value, GParamSpec * pspec);
105
106 static gboolean gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
107 static GstCaps *gst_video_test_src_src_fixate (GstBaseSrc * bsrc,
108     GstCaps * caps);
109
110 static gboolean gst_video_test_src_is_seekable (GstBaseSrc * psrc);
111 static gboolean gst_video_test_src_do_seek (GstBaseSrc * bsrc,
112     GstSegment * segment);
113 static gboolean gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query);
114
115 static void gst_video_test_src_get_times (GstBaseSrc * basesrc,
116     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
117 static gboolean gst_video_test_src_decide_allocation (GstBaseSrc * bsrc,
118     GstQuery * query);
119 static GstFlowReturn gst_video_test_src_fill (GstPushSrc * psrc,
120     GstBuffer * buffer);
121 static gboolean gst_video_test_src_start (GstBaseSrc * basesrc);
122 static gboolean gst_video_test_src_stop (GstBaseSrc * basesrc);
123
124 #define GST_TYPE_VIDEO_TEST_SRC_PATTERN (gst_video_test_src_pattern_get_type ())
125 static GType
126 gst_video_test_src_pattern_get_type (void)
127 {
128   static GType video_test_src_pattern_type = 0;
129   static const GEnumValue pattern_types[] = {
130     {GST_VIDEO_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
131     {GST_VIDEO_TEST_SRC_SNOW, "Random (television snow)", "snow"},
132     {GST_VIDEO_TEST_SRC_BLACK, "100% Black", "black"},
133     {GST_VIDEO_TEST_SRC_WHITE, "100% White", "white"},
134     {GST_VIDEO_TEST_SRC_RED, "Red", "red"},
135     {GST_VIDEO_TEST_SRC_GREEN, "Green", "green"},
136     {GST_VIDEO_TEST_SRC_BLUE, "Blue", "blue"},
137     {GST_VIDEO_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
138     {GST_VIDEO_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
139     {GST_VIDEO_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
140     {GST_VIDEO_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
141     {GST_VIDEO_TEST_SRC_CIRCULAR, "Circular", "circular"},
142     {GST_VIDEO_TEST_SRC_BLINK, "Blink", "blink"},
143     {GST_VIDEO_TEST_SRC_SMPTE75, "SMPTE 75% color bars", "smpte75"},
144     {GST_VIDEO_TEST_SRC_ZONE_PLATE, "Zone plate", "zone-plate"},
145     {GST_VIDEO_TEST_SRC_GAMUT, "Gamut checkers", "gamut"},
146     {GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE, "Chroma zone plate",
147         "chroma-zone-plate"},
148     {GST_VIDEO_TEST_SRC_SOLID, "Solid color", "solid-color"},
149     {GST_VIDEO_TEST_SRC_BALL, "Moving ball", "ball"},
150     {GST_VIDEO_TEST_SRC_SMPTE100, "SMPTE 100% color bars", "smpte100"},
151     {GST_VIDEO_TEST_SRC_BAR, "Bar", "bar"},
152     {0, NULL, NULL}
153   };
154
155   if (!video_test_src_pattern_type) {
156     video_test_src_pattern_type =
157         g_enum_register_static ("GstVideoTestSrcPattern", pattern_types);
158   }
159   return video_test_src_pattern_type;
160 }
161
162 static void
163 gst_video_test_src_class_init (GstVideoTestSrcClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *gstelement_class;
167   GstBaseSrcClass *gstbasesrc_class;
168   GstPushSrcClass *gstpushsrc_class;
169
170   gobject_class = (GObjectClass *) klass;
171   gstelement_class = (GstElementClass *) klass;
172   gstbasesrc_class = (GstBaseSrcClass *) klass;
173   gstpushsrc_class = (GstPushSrcClass *) klass;
174
175   gobject_class->set_property = gst_video_test_src_set_property;
176   gobject_class->get_property = gst_video_test_src_get_property;
177
178   g_object_class_install_property (gobject_class, PROP_PATTERN,
179       g_param_spec_enum ("pattern", "Pattern",
180           "Type of test pattern to generate", GST_TYPE_VIDEO_TEST_SRC_PATTERN,
181           DEFAULT_PATTERN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182   g_object_class_install_property (gobject_class, PROP_TIMESTAMP_OFFSET,
183       g_param_spec_int64 ("timestamp-offset", "Timestamp offset",
184           "An offset added to timestamps set on buffers (in ns)", G_MININT64,
185           G_MAXINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
187       g_param_spec_boolean ("is-live", "Is Live",
188           "Whether to act as a live source", DEFAULT_IS_LIVE,
189           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190   g_object_class_install_property (gobject_class, PROP_K0,
191       g_param_spec_int ("k0", "Zoneplate zero order phase",
192           "Zoneplate zero order phase, for generating plain fields or phase offsets",
193           G_MININT32, G_MAXINT32, 0,
194           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195   g_object_class_install_property (gobject_class, PROP_KX,
196       g_param_spec_int ("kx", "Zoneplate 1st order x phase",
197           "Zoneplate 1st order x phase, for generating constant horizontal frequencies",
198           G_MININT32, G_MAXINT32, 0,
199           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200   g_object_class_install_property (gobject_class, PROP_KY,
201       g_param_spec_int ("ky", "Zoneplate 1st order y phase",
202           "Zoneplate 1st order y phase, for generating contant vertical frequencies",
203           G_MININT32, G_MAXINT32, 0,
204           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205   g_object_class_install_property (gobject_class, PROP_KT,
206       g_param_spec_int ("kt", "Zoneplate 1st order t phase",
207           "Zoneplate 1st order t phase, for generating phase rotation as a function of time",
208           G_MININT32, G_MAXINT32, 0,
209           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210   g_object_class_install_property (gobject_class, PROP_KXT,
211       g_param_spec_int ("kxt", "Zoneplate x*t product phase",
212           "Zoneplate x*t product phase, normalised to kxy/256 cycles per vertical pixel at width/2 from origin",
213           G_MININT32, G_MAXINT32, 0,
214           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215   g_object_class_install_property (gobject_class, PROP_KYT,
216       g_param_spec_int ("kyt", "Zoneplate y*t product phase",
217           "Zoneplate y*t product phase", G_MININT32, G_MAXINT32, 0,
218           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219   g_object_class_install_property (gobject_class, PROP_KXY,
220       g_param_spec_int ("kxy", "Zoneplate x*y product phase",
221           "Zoneplate x*y product phase", G_MININT32, G_MAXINT32, 0,
222           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223   g_object_class_install_property (gobject_class, PROP_KX2,
224       g_param_spec_int ("kx2", "Zoneplate 2nd order x phase",
225           "Zoneplate 2nd order x phase, normalised to kx2/256 cycles per horizontal pixel at width/2 from origin",
226           G_MININT32, G_MAXINT32, 0,
227           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228   g_object_class_install_property (gobject_class, PROP_KY2,
229       g_param_spec_int ("ky2", "Zoneplate 2nd order y phase",
230           "Zoneplate 2nd order y phase, normailsed to ky2/256 cycles per vertical pixel at height/2 from origin",
231           G_MININT32, G_MAXINT32, 0,
232           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233   g_object_class_install_property (gobject_class, PROP_KT2,
234       g_param_spec_int ("kt2", "Zoneplate 2nd order t phase",
235           "Zoneplate 2nd order t phase, t*t/256 cycles per picture", G_MININT32,
236           G_MAXINT32, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237   g_object_class_install_property (gobject_class, PROP_XOFFSET,
238       g_param_spec_int ("xoffset", "Zoneplate 2nd order products x offset",
239           "Zoneplate 2nd order products x offset", G_MININT32, G_MAXINT32, 0,
240           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241   g_object_class_install_property (gobject_class, PROP_YOFFSET,
242       g_param_spec_int ("yoffset", "Zoneplate 2nd order products y offset",
243           "Zoneplate 2nd order products y offset", G_MININT32, G_MAXINT32, 0,
244           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245   /**
246    * GstVideoTestSrc:foreground-color
247    *
248    * Color to use for solid-color pattern and foreground color of other
249    * patterns.  Default is white (0xffffffff).
250    *
251    * Since: 0.10.31
252    **/
253   g_object_class_install_property (gobject_class, PROP_FOREGROUND_COLOR,
254       g_param_spec_uint ("foreground-color", "Foreground Color",
255           "Foreground color to use (big-endian ARGB)", 0, G_MAXUINT32,
256           DEFAULT_FOREGROUND_COLOR,
257           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258   /**
259    * GstVideoTestSrc:background-color
260    *
261    * Color to use for background color of some patterns.  Default is
262    * black (0xff000000).
263    *
264    * Since: 0.10.31
265    **/
266   g_object_class_install_property (gobject_class, PROP_BACKGROUND_COLOR,
267       g_param_spec_uint ("background-color", "Background Color",
268           "Background color to use (big-endian ARGB)", 0, G_MAXUINT32,
269           DEFAULT_BACKGROUND_COLOR,
270           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271
272   g_object_class_install_property (gobject_class, PROP_HORIZONTAL_SPEED,
273       g_param_spec_int ("horizontal-speed", "Horizontal Speed",
274           "Scroll image number of pixels per frame (positive is scroll to the left)",
275           G_MININT32, G_MAXINT32, DEFAULT_HORIZONTAL_SPEED,
276           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278   gst_element_class_set_static_metadata (gstelement_class,
279       "Video test source", "Source/Video",
280       "Creates a test video stream", "David A. Schleef <ds@schleef.org>");
281
282   gst_element_class_add_pad_template (gstelement_class,
283       gst_static_pad_template_get (&gst_video_test_src_template));
284
285   gstbasesrc_class->set_caps = gst_video_test_src_setcaps;
286   gstbasesrc_class->fixate = gst_video_test_src_src_fixate;
287   gstbasesrc_class->is_seekable = gst_video_test_src_is_seekable;
288   gstbasesrc_class->do_seek = gst_video_test_src_do_seek;
289   gstbasesrc_class->query = gst_video_test_src_query;
290   gstbasesrc_class->get_times = gst_video_test_src_get_times;
291   gstbasesrc_class->start = gst_video_test_src_start;
292   gstbasesrc_class->stop = gst_video_test_src_stop;
293   gstbasesrc_class->decide_allocation = gst_video_test_src_decide_allocation;
294
295   gstpushsrc_class->fill = gst_video_test_src_fill;
296 }
297
298 static void
299 gst_video_test_src_init (GstVideoTestSrc * src)
300 {
301   gst_video_test_src_set_pattern (src, DEFAULT_PATTERN);
302
303   src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
304   src->foreground_color = DEFAULT_FOREGROUND_COLOR;
305   src->background_color = DEFAULT_BACKGROUND_COLOR;
306   src->horizontal_speed = DEFAULT_HORIZONTAL_SPEED;
307
308   /* we operate in time */
309   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
310   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
311 }
312
313 static GstCaps *
314 gst_video_test_src_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
315 {
316   GstStructure *structure;
317
318   caps = gst_caps_make_writable (caps);
319
320   structure = gst_caps_get_structure (caps, 0);
321
322   gst_structure_fixate_field_nearest_int (structure, "width", 320);
323   gst_structure_fixate_field_nearest_int (structure, "height", 240);
324   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
325   if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
326     gst_structure_fixate_field_nearest_fraction (structure,
327         "pixel-aspect-ratio", 1, 1);
328   if (gst_structure_has_field (structure, "colorimetry"))
329     gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
330   if (gst_structure_has_field (structure, "chroma-site"))
331     gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
332
333   if (gst_structure_has_field (structure, "interlace-mode"))
334     gst_structure_fixate_field_string (structure, "interlace-mode",
335         "progressive");
336
337   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
338
339   return caps;
340 }
341
342 static void
343 gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
344     int pattern_type)
345 {
346   videotestsrc->pattern_type = pattern_type;
347
348   GST_DEBUG_OBJECT (videotestsrc, "setting pattern to %d", pattern_type);
349
350   switch (pattern_type) {
351     case GST_VIDEO_TEST_SRC_SMPTE:
352       videotestsrc->make_image = gst_video_test_src_smpte;
353       break;
354     case GST_VIDEO_TEST_SRC_SNOW:
355       videotestsrc->make_image = gst_video_test_src_snow;
356       break;
357     case GST_VIDEO_TEST_SRC_BLACK:
358       videotestsrc->make_image = gst_video_test_src_black;
359       break;
360     case GST_VIDEO_TEST_SRC_WHITE:
361       videotestsrc->make_image = gst_video_test_src_white;
362       break;
363     case GST_VIDEO_TEST_SRC_RED:
364       videotestsrc->make_image = gst_video_test_src_red;
365       break;
366     case GST_VIDEO_TEST_SRC_GREEN:
367       videotestsrc->make_image = gst_video_test_src_green;
368       break;
369     case GST_VIDEO_TEST_SRC_BLUE:
370       videotestsrc->make_image = gst_video_test_src_blue;
371       break;
372     case GST_VIDEO_TEST_SRC_CHECKERS1:
373       videotestsrc->make_image = gst_video_test_src_checkers1;
374       break;
375     case GST_VIDEO_TEST_SRC_CHECKERS2:
376       videotestsrc->make_image = gst_video_test_src_checkers2;
377       break;
378     case GST_VIDEO_TEST_SRC_CHECKERS4:
379       videotestsrc->make_image = gst_video_test_src_checkers4;
380       break;
381     case GST_VIDEO_TEST_SRC_CHECKERS8:
382       videotestsrc->make_image = gst_video_test_src_checkers8;
383       break;
384     case GST_VIDEO_TEST_SRC_CIRCULAR:
385       videotestsrc->make_image = gst_video_test_src_circular;
386       break;
387     case GST_VIDEO_TEST_SRC_BLINK:
388       videotestsrc->make_image = gst_video_test_src_blink;
389       break;
390     case GST_VIDEO_TEST_SRC_SMPTE75:
391       videotestsrc->make_image = gst_video_test_src_smpte75;
392       break;
393     case GST_VIDEO_TEST_SRC_ZONE_PLATE:
394       videotestsrc->make_image = gst_video_test_src_zoneplate;
395       break;
396     case GST_VIDEO_TEST_SRC_GAMUT:
397       videotestsrc->make_image = gst_video_test_src_gamut;
398       break;
399     case GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE:
400       videotestsrc->make_image = gst_video_test_src_chromazoneplate;
401       break;
402     case GST_VIDEO_TEST_SRC_SOLID:
403       videotestsrc->make_image = gst_video_test_src_solid;
404       break;
405     case GST_VIDEO_TEST_SRC_BALL:
406       videotestsrc->make_image = gst_video_test_src_ball;
407       break;
408     case GST_VIDEO_TEST_SRC_SMPTE100:
409       videotestsrc->make_image = gst_video_test_src_smpte100;
410       break;
411     case GST_VIDEO_TEST_SRC_BAR:
412       videotestsrc->make_image = gst_video_test_src_bar;
413       break;
414     default:
415       g_assert_not_reached ();
416   }
417 }
418
419 static void
420 gst_video_test_src_set_property (GObject * object, guint prop_id,
421     const GValue * value, GParamSpec * pspec)
422 {
423   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
424
425   switch (prop_id) {
426     case PROP_PATTERN:
427       gst_video_test_src_set_pattern (src, g_value_get_enum (value));
428       break;
429     case PROP_TIMESTAMP_OFFSET:
430       src->timestamp_offset = g_value_get_int64 (value);
431       break;
432     case PROP_IS_LIVE:
433       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
434       break;
435     case PROP_K0:
436       src->k0 = g_value_get_int (value);
437       break;
438     case PROP_KX:
439       src->kx = g_value_get_int (value);
440       break;
441     case PROP_KY:
442       src->ky = g_value_get_int (value);
443       break;
444     case PROP_KT:
445       src->kt = g_value_get_int (value);
446       break;
447     case PROP_KXT:
448       src->kxt = g_value_get_int (value);
449       break;
450     case PROP_KYT:
451       src->kyt = g_value_get_int (value);
452       break;
453     case PROP_KXY:
454       src->kxy = g_value_get_int (value);
455       break;
456     case PROP_KX2:
457       src->kx2 = g_value_get_int (value);
458       break;
459     case PROP_KY2:
460       src->ky2 = g_value_get_int (value);
461       break;
462     case PROP_KT2:
463       src->kt2 = g_value_get_int (value);
464       break;
465     case PROP_XOFFSET:
466       src->xoffset = g_value_get_int (value);
467       break;
468     case PROP_YOFFSET:
469       src->yoffset = g_value_get_int (value);
470       break;
471     case PROP_FOREGROUND_COLOR:
472       src->foreground_color = g_value_get_uint (value);
473       break;
474     case PROP_BACKGROUND_COLOR:
475       src->background_color = g_value_get_uint (value);
476       break;
477     case PROP_HORIZONTAL_SPEED:
478       src->horizontal_speed = g_value_get_int (value);
479     default:
480       break;
481   }
482 }
483
484 static void
485 gst_video_test_src_get_property (GObject * object, guint prop_id,
486     GValue * value, GParamSpec * pspec)
487 {
488   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
489
490   switch (prop_id) {
491     case PROP_PATTERN:
492       g_value_set_enum (value, src->pattern_type);
493       break;
494     case PROP_TIMESTAMP_OFFSET:
495       g_value_set_int64 (value, src->timestamp_offset);
496       break;
497     case PROP_IS_LIVE:
498       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
499       break;
500     case PROP_K0:
501       g_value_set_int (value, src->k0);
502       break;
503     case PROP_KX:
504       g_value_set_int (value, src->kx);
505       break;
506     case PROP_KY:
507       g_value_set_int (value, src->ky);
508       break;
509     case PROP_KT:
510       g_value_set_int (value, src->kt);
511       break;
512     case PROP_KXT:
513       g_value_set_int (value, src->kxt);
514       break;
515     case PROP_KYT:
516       g_value_set_int (value, src->kyt);
517       break;
518     case PROP_KXY:
519       g_value_set_int (value, src->kxy);
520       break;
521     case PROP_KX2:
522       g_value_set_int (value, src->kx2);
523       break;
524     case PROP_KY2:
525       g_value_set_int (value, src->ky2);
526       break;
527     case PROP_KT2:
528       g_value_set_int (value, src->kt2);
529       break;
530     case PROP_XOFFSET:
531       g_value_set_int (value, src->xoffset);
532       break;
533     case PROP_YOFFSET:
534       g_value_set_int (value, src->yoffset);
535       break;
536     case PROP_FOREGROUND_COLOR:
537       g_value_set_uint (value, src->foreground_color);
538       break;
539     case PROP_BACKGROUND_COLOR:
540       g_value_set_uint (value, src->background_color);
541       break;
542     case PROP_HORIZONTAL_SPEED:
543       g_value_set_int (value, src->horizontal_speed);
544       break;
545     default:
546       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547       break;
548   }
549 }
550
551 static gboolean
552 gst_video_test_src_parse_caps (const GstCaps * caps,
553     gint * width, gint * height, gint * fps_n, gint * fps_d,
554     GstVideoColorimetry * colorimetry, gint * x_inv, gint * y_inv)
555 {
556   const GstStructure *structure;
557   GstPadLinkReturn ret;
558   const GValue *framerate;
559   const gchar *str;
560
561   GST_DEBUG ("parsing caps");
562
563   structure = gst_caps_get_structure (caps, 0);
564
565   ret = gst_structure_get_int (structure, "width", width);
566   ret &= gst_structure_get_int (structure, "height", height);
567   framerate = gst_structure_get_value (structure, "framerate");
568
569   if (framerate) {
570     *fps_n = gst_value_get_fraction_numerator (framerate);
571     *fps_d = gst_value_get_fraction_denominator (framerate);
572   } else
573     goto no_framerate;
574
575   if ((str = gst_structure_get_string (structure, "colorimetry")))
576     gst_video_colorimetry_from_string (colorimetry, str);
577
578   if ((str = gst_structure_get_string (structure, "format"))) {
579     if (g_str_equal (str, "bggr")) {
580       *x_inv = *y_inv = 0;
581     } else if (g_str_equal (str, "rggb")) {
582       *x_inv = *y_inv = 1;
583     } else if (g_str_equal (str, "grbg")) {
584       *x_inv = 0;
585       *y_inv = 1;
586     } else if (g_str_equal (str, "grbg")) {
587       *x_inv = 1;
588       *y_inv = 0;
589     } else
590       goto invalid_format;
591   }
592   return ret;
593
594   /* ERRORS */
595 no_framerate:
596   {
597     GST_DEBUG ("videotestsrc no framerate given");
598     return FALSE;
599   }
600 invalid_format:
601   {
602     GST_DEBUG ("videotestsrc invalid bayer format given");
603     return FALSE;
604   }
605 }
606
607 static gboolean
608 gst_video_test_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
609 {
610   GstVideoTestSrc *videotestsrc;
611   GstBufferPool *pool;
612   gboolean update;
613   guint size, min, max;
614   GstStructure *config;
615
616   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
617
618   if (gst_query_get_n_allocation_pools (query) > 0) {
619     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
620
621     /* adjust size */
622     size = MAX (size, videotestsrc->info.size);
623     update = TRUE;
624   } else {
625     pool = NULL;
626     size = videotestsrc->info.size;
627     min = max = 0;
628     update = FALSE;
629   }
630
631   /* no downstream pool, make our own */
632   if (pool == NULL) {
633     if (videotestsrc->bayer)
634       pool = gst_buffer_pool_new ();
635     else
636       pool = gst_video_buffer_pool_new ();
637   }
638
639   config = gst_buffer_pool_get_config (pool);
640   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
641     gst_buffer_pool_config_add_option (config,
642         GST_BUFFER_POOL_OPTION_VIDEO_META);
643   }
644   gst_buffer_pool_set_config (pool, config);
645
646   if (update)
647     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
648   else
649     gst_query_add_allocation_pool (query, pool, size, min, max);
650
651   if (pool)
652     gst_object_unref (pool);
653
654   return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
655 }
656
657 static void
658 fill_palette_RGB8P (guint32 * palette)
659 {
660   /* build poor man's palette, taken from ffmpegcolorspace */
661   static const guint8 pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
662   gint i, r, g, b;
663
664   i = 0;
665   for (r = 0; r < 6; r++) {
666     for (g = 0; g < 6; g++) {
667       for (b = 0; b < 6; b++) {
668         palette[i++] =
669             (0xffU << 24) | (pal_value[r] << 16) | (pal_value[g] << 8) |
670             pal_value[b];
671       }
672     }
673   }
674   palette[i++] = 0;             /* 100% transparent, i == 6*6*6 */
675   while (i < 256)
676     palette[i++] = 0xff000000;
677 }
678
679 static gboolean
680 gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
681 {
682   const GstStructure *structure;
683   GstVideoTestSrc *videotestsrc;
684   GstVideoInfo info;
685
686   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
687
688   structure = gst_caps_get_structure (caps, 0);
689
690   if (gst_structure_has_name (structure, "video/x-raw")) {
691     /* we can use the parsing code */
692     if (!gst_video_info_from_caps (&info, caps))
693       goto parse_failed;
694
695   } else if (gst_structure_has_name (structure, "video/x-bayer")) {
696     gint x_inv = 0, y_inv = 0;
697
698     gst_video_info_init (&info);
699
700     info.finfo = gst_video_format_get_info (GST_VIDEO_FORMAT_GRAY8);
701
702     if (!gst_video_test_src_parse_caps (caps, &info.width, &info.height,
703             &info.fps_n, &info.fps_d, &info.colorimetry, &x_inv, &y_inv))
704       goto parse_failed;
705
706     info.size = GST_ROUND_UP_4 (info.width) * info.height;
707     info.stride[0] = GST_ROUND_UP_4 (info.width);
708
709     videotestsrc->bayer = TRUE;
710     videotestsrc->x_invert = x_inv;
711     videotestsrc->y_invert = y_inv;
712   }
713
714   /* looks ok here */
715   videotestsrc->info = info;
716
717   GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
718       info.width, info.height, info.fps_n, info.fps_d);
719
720   g_free (videotestsrc->tmpline);
721   g_free (videotestsrc->tmpline2);
722   g_free (videotestsrc->tmpline_u8);
723   g_free (videotestsrc->tmpline_u16);
724   g_free (videotestsrc->palette);
725   videotestsrc->tmpline_u8 = g_malloc (info.width + 8);
726   videotestsrc->tmpline = g_malloc ((info.width + 8) * 4);
727   videotestsrc->tmpline2 = g_malloc ((info.width + 8) * 4);
728   videotestsrc->tmpline_u16 = g_malloc ((info.width + 16) * 8);
729
730   if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_RGB8P) {
731     videotestsrc->palette = g_new (guint32, 256);
732     fill_palette_RGB8P (videotestsrc->palette);
733   } else {
734     videotestsrc->palette = NULL;
735   }
736
737   videotestsrc->accum_rtime += videotestsrc->running_time;
738   videotestsrc->accum_frames += videotestsrc->n_frames;
739
740   videotestsrc->running_time = 0;
741   videotestsrc->n_frames = 0;
742
743   return TRUE;
744
745   /* ERRORS */
746 parse_failed:
747   {
748     GST_DEBUG_OBJECT (bsrc, "failed to parse caps");
749     return FALSE;
750   }
751 }
752
753 static gboolean
754 gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
755 {
756   gboolean res;
757   GstVideoTestSrc *src;
758
759   src = GST_VIDEO_TEST_SRC (bsrc);
760
761   switch (GST_QUERY_TYPE (query)) {
762     case GST_QUERY_CONVERT:
763     {
764       GstFormat src_fmt, dest_fmt;
765       gint64 src_val, dest_val;
766
767       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
768       res =
769           gst_video_info_convert (&src->info, src_fmt, src_val, dest_fmt,
770           &dest_val);
771       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
772       break;
773     }
774     default:
775       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
776       break;
777   }
778   return res;
779 }
780
781 static void
782 gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
783     GstClockTime * start, GstClockTime * end)
784 {
785   /* for live sources, sync on the timestamp of the buffer */
786   if (gst_base_src_is_live (basesrc)) {
787     GstClockTime timestamp = GST_BUFFER_DTS (buffer);
788
789     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
790       /* get duration to calculate end time */
791       GstClockTime duration = GST_BUFFER_DURATION (buffer);
792
793       if (GST_CLOCK_TIME_IS_VALID (duration)) {
794         *end = timestamp + duration;
795       }
796       *start = timestamp;
797     }
798   } else {
799     *start = -1;
800     *end = -1;
801   }
802 }
803
804 static gboolean
805 gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
806 {
807   GstClockTime position;
808   GstVideoTestSrc *src;
809
810   src = GST_VIDEO_TEST_SRC (bsrc);
811
812   segment->time = segment->start;
813   position = segment->position;
814
815   /* now move to the position indicated */
816   if (src->info.fps_n) {
817     src->n_frames = gst_util_uint64_scale (position,
818         src->info.fps_n, src->info.fps_d * GST_SECOND);
819   } else {
820     src->n_frames = 0;
821   }
822   src->accum_frames = 0;
823   src->accum_rtime = 0;
824   if (src->info.fps_n) {
825     src->running_time = gst_util_uint64_scale (src->n_frames,
826         src->info.fps_d * GST_SECOND, src->info.fps_n);
827   } else {
828     /* FIXME : Not sure what to set here */
829     src->running_time = 0;
830   }
831
832   g_assert (src->running_time <= position);
833
834   return TRUE;
835 }
836
837 static gboolean
838 gst_video_test_src_is_seekable (GstBaseSrc * psrc)
839 {
840   /* we're seekable... */
841   return TRUE;
842 }
843
844 static GstFlowReturn
845 gst_video_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
846 {
847   GstVideoTestSrc *src;
848   GstClockTime next_time;
849   GstVideoFrame frame;
850
851   src = GST_VIDEO_TEST_SRC (psrc);
852
853   if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&src->info) ==
854           GST_VIDEO_FORMAT_UNKNOWN))
855     goto not_negotiated;
856
857   /* 0 framerate and we are at the second frame, eos */
858   if (G_UNLIKELY (src->info.fps_n == 0 && src->n_frames == 1))
859     goto eos;
860
861   GST_LOG_OBJECT (src,
862       "creating buffer from pool for frame %d", (gint) src->n_frames);
863
864   if (!gst_video_frame_map (&frame, &src->info, buffer, GST_MAP_WRITE))
865     goto invalid_frame;
866
867   src->make_image (src, &frame);
868
869   if (src->palette) {
870     memcpy (GST_VIDEO_FRAME_PLANE_DATA (&frame, 1), src->palette, 256 * 4);
871   }
872
873   gst_video_frame_unmap (&frame);
874
875   GST_BUFFER_DTS (buffer) =
876       src->accum_rtime + src->timestamp_offset + src->running_time;
877   GST_BUFFER_PTS (buffer) = GST_BUFFER_DTS (buffer);
878
879   GST_DEBUG_OBJECT (src, "Timestamp: %" GST_TIME_FORMAT " = accumulated %"
880       GST_TIME_FORMAT " + offset: %"
881       GST_TIME_FORMAT " + running time: %" GST_TIME_FORMAT,
882       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), GST_TIME_ARGS (src->accum_rtime),
883       GST_TIME_ARGS (src->timestamp_offset), GST_TIME_ARGS (src->running_time));
884
885   GST_BUFFER_OFFSET (buffer) = src->accum_frames + src->n_frames;
886   src->n_frames++;
887   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET (buffer) + 1;
888   if (src->info.fps_n) {
889     next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
890         src->info.fps_d, src->info.fps_n);
891     GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
892   } else {
893     next_time = src->timestamp_offset;
894     /* NONE means forever */
895     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
896   }
897
898   src->running_time = next_time;
899
900   return GST_FLOW_OK;
901
902 not_negotiated:
903   {
904     GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
905         ("format wasn't negotiated before get function"));
906     return GST_FLOW_NOT_NEGOTIATED;
907   }
908 eos:
909   {
910     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
911     return GST_FLOW_EOS;
912   }
913 invalid_frame:
914   {
915     GST_DEBUG_OBJECT (src, "invalid frame");
916     return GST_FLOW_OK;
917   }
918 }
919
920 static gboolean
921 gst_video_test_src_start (GstBaseSrc * basesrc)
922 {
923   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
924
925   src->running_time = 0;
926   src->n_frames = 0;
927   src->accum_frames = 0;
928   src->accum_rtime = 0;
929
930   return TRUE;
931 }
932
933 static gboolean
934 gst_video_test_src_stop (GstBaseSrc * basesrc)
935 {
936   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
937
938   g_free (src->tmpline);
939   src->tmpline = NULL;
940   g_free (src->tmpline2);
941   src->tmpline2 = NULL;
942   g_free (src->tmpline_u8);
943   src->tmpline_u8 = NULL;
944   g_free (src->tmpline_u16);
945   src->tmpline_u16 = NULL;
946   g_free (src->palette);
947   src->palette = NULL;
948
949   return TRUE;
950 }
951
952 static gboolean
953 plugin_init (GstPlugin * plugin)
954 {
955   GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
956       "Video Test Source");
957
958   return gst_element_register (plugin, "videotestsrc", GST_RANK_NONE,
959       GST_TYPE_VIDEO_TEST_SRC);
960 }
961
962 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
963     GST_VERSION_MINOR,
964     videotestsrc,
965     "Creates a test video stream",
966     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)