e37ce2a05613577b1275734f9e08c4b30ec351cd
[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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, 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 gst_video_test_src_parent_class parent_class
83 G_DEFINE_TYPE (GstVideoTestSrc, gst_video_test_src, GST_TYPE_PUSH_SRC);
84
85 static void gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
86     int pattern_type);
87 static void gst_video_test_src_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_video_test_src_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec);
91
92 static GstCaps *gst_video_test_src_getcaps (GstBaseSrc * bsrc,
93     GstCaps * filter);
94 static gboolean gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
95 static GstCaps *gst_video_test_src_src_fixate (GstBaseSrc * bsrc,
96     GstCaps * caps);
97
98 static gboolean gst_video_test_src_is_seekable (GstBaseSrc * psrc);
99 static gboolean gst_video_test_src_do_seek (GstBaseSrc * bsrc,
100     GstSegment * segment);
101 static gboolean gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query);
102
103 static void gst_video_test_src_get_times (GstBaseSrc * basesrc,
104     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
105 static gboolean gst_video_test_src_decide_allocation (GstBaseSrc * bsrc,
106     GstQuery * query);
107 static GstFlowReturn gst_video_test_src_fill (GstPushSrc * psrc,
108     GstBuffer * buffer);
109 static gboolean gst_video_test_src_start (GstBaseSrc * basesrc);
110 static gboolean gst_video_test_src_stop (GstBaseSrc * basesrc);
111
112 #define GST_TYPE_VIDEO_TEST_SRC_PATTERN (gst_video_test_src_pattern_get_type ())
113 static GType
114 gst_video_test_src_pattern_get_type (void)
115 {
116   static GType video_test_src_pattern_type = 0;
117   static const GEnumValue pattern_types[] = {
118     {GST_VIDEO_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
119     {GST_VIDEO_TEST_SRC_SNOW, "Random (television snow)", "snow"},
120     {GST_VIDEO_TEST_SRC_BLACK, "100% Black", "black"},
121     {GST_VIDEO_TEST_SRC_WHITE, "100% White", "white"},
122     {GST_VIDEO_TEST_SRC_RED, "Red", "red"},
123     {GST_VIDEO_TEST_SRC_GREEN, "Green", "green"},
124     {GST_VIDEO_TEST_SRC_BLUE, "Blue", "blue"},
125     {GST_VIDEO_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
126     {GST_VIDEO_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
127     {GST_VIDEO_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
128     {GST_VIDEO_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
129     {GST_VIDEO_TEST_SRC_CIRCULAR, "Circular", "circular"},
130     {GST_VIDEO_TEST_SRC_BLINK, "Blink", "blink"},
131     {GST_VIDEO_TEST_SRC_SMPTE75, "SMPTE 75% color bars", "smpte75"},
132     {GST_VIDEO_TEST_SRC_ZONE_PLATE, "Zone plate", "zone-plate"},
133     {GST_VIDEO_TEST_SRC_GAMUT, "Gamut checkers", "gamut"},
134     {GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE, "Chroma zone plate",
135         "chroma-zone-plate"},
136     {GST_VIDEO_TEST_SRC_SOLID, "Solid color", "solid-color"},
137     {GST_VIDEO_TEST_SRC_BALL, "Moving ball", "ball"},
138     {GST_VIDEO_TEST_SRC_SMPTE100, "SMPTE 100% color bars", "smpte100"},
139     {GST_VIDEO_TEST_SRC_BAR, "Bar", "bar"},
140     {0, NULL, NULL}
141   };
142
143   if (!video_test_src_pattern_type) {
144     video_test_src_pattern_type =
145         g_enum_register_static ("GstVideoTestSrcPattern", pattern_types);
146   }
147   return video_test_src_pattern_type;
148 }
149
150 static void
151 gst_video_test_src_class_init (GstVideoTestSrcClass * klass)
152 {
153   GObjectClass *gobject_class;
154   GstElementClass *gstelement_class;
155   GstBaseSrcClass *gstbasesrc_class;
156   GstPushSrcClass *gstpushsrc_class;
157   GstCaps *templ_caps;
158
159   gobject_class = (GObjectClass *) klass;
160   gstelement_class = (GstElementClass *) klass;
161   gstbasesrc_class = (GstBaseSrcClass *) klass;
162   gstpushsrc_class = (GstPushSrcClass *) klass;
163
164   gobject_class->set_property = gst_video_test_src_set_property;
165   gobject_class->get_property = gst_video_test_src_get_property;
166
167   g_object_class_install_property (gobject_class, PROP_PATTERN,
168       g_param_spec_enum ("pattern", "Pattern",
169           "Type of test pattern to generate", GST_TYPE_VIDEO_TEST_SRC_PATTERN,
170           DEFAULT_PATTERN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171   g_object_class_install_property (gobject_class, PROP_TIMESTAMP_OFFSET,
172       g_param_spec_int64 ("timestamp-offset", "Timestamp offset",
173           "An offset added to timestamps set on buffers (in ns)", G_MININT64,
174           G_MAXINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
176       g_param_spec_boolean ("is-live", "Is Live",
177           "Whether to act as a live source", DEFAULT_IS_LIVE,
178           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179   g_object_class_install_property (gobject_class, PROP_K0,
180       g_param_spec_int ("k0", "Zoneplate zero order phase",
181           "Zoneplate zero order phase, for generating plain fields or phase offsets",
182           G_MININT32, G_MAXINT32, 0,
183           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184   g_object_class_install_property (gobject_class, PROP_KX,
185       g_param_spec_int ("kx", "Zoneplate 1st order x phase",
186           "Zoneplate 1st order x phase, for generating constant horizontal frequencies",
187           G_MININT32, G_MAXINT32, 0,
188           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189   g_object_class_install_property (gobject_class, PROP_KY,
190       g_param_spec_int ("ky", "Zoneplate 1st order y phase",
191           "Zoneplate 1st order y phase, for generating contant vertical frequencies",
192           G_MININT32, G_MAXINT32, 0,
193           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194   g_object_class_install_property (gobject_class, PROP_KT,
195       g_param_spec_int ("kt", "Zoneplate 1st order t phase",
196           "Zoneplate 1st order t phase, for generating phase rotation as a function of time",
197           G_MININT32, G_MAXINT32, 0,
198           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199   g_object_class_install_property (gobject_class, PROP_KXT,
200       g_param_spec_int ("kxt", "Zoneplate x*t product phase",
201           "Zoneplate x*t product phase, normalised to kxy/256 cycles per vertical pixel at width/2 from origin",
202           G_MININT32, G_MAXINT32, 0,
203           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204   g_object_class_install_property (gobject_class, PROP_KYT,
205       g_param_spec_int ("kyt", "Zoneplate y*t product phase",
206           "Zoneplate y*t product phase", G_MININT32, G_MAXINT32, 0,
207           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208   g_object_class_install_property (gobject_class, PROP_KXY,
209       g_param_spec_int ("kxy", "Zoneplate x*y product phase",
210           "Zoneplate x*y product phase", G_MININT32, G_MAXINT32, 0,
211           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212   g_object_class_install_property (gobject_class, PROP_KX2,
213       g_param_spec_int ("kx2", "Zoneplate 2nd order x phase",
214           "Zoneplate 2nd order x phase, normalised to kx2/256 cycles per horizontal pixel at width/2 from origin",
215           G_MININT32, G_MAXINT32, 0,
216           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217   g_object_class_install_property (gobject_class, PROP_KY2,
218       g_param_spec_int ("ky2", "Zoneplate 2nd order y phase",
219           "Zoneplate 2nd order y phase, normailsed to ky2/256 cycles per vertical pixel at height/2 from origin",
220           G_MININT32, G_MAXINT32, 0,
221           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222   g_object_class_install_property (gobject_class, PROP_KT2,
223       g_param_spec_int ("kt2", "Zoneplate 2nd order t phase",
224           "Zoneplate 2nd order t phase, t*t/256 cycles per picture", G_MININT32,
225           G_MAXINT32, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226   g_object_class_install_property (gobject_class, PROP_XOFFSET,
227       g_param_spec_int ("xoffset", "Zoneplate 2nd order products x offset",
228           "Zoneplate 2nd order products x offset", G_MININT32, G_MAXINT32, 0,
229           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230   g_object_class_install_property (gobject_class, PROP_YOFFSET,
231       g_param_spec_int ("yoffset", "Zoneplate 2nd order products y offset",
232           "Zoneplate 2nd order products y offset", G_MININT32, G_MAXINT32, 0,
233           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234   /**
235    * GstVideoTestSrc:foreground-color
236    *
237    * Color to use for solid-color pattern and foreground color of other
238    * patterns.  Default is white (0xffffffff).
239    *
240    * Since: 0.10.31
241    **/
242   g_object_class_install_property (gobject_class, PROP_FOREGROUND_COLOR,
243       g_param_spec_uint ("foreground-color", "Foreground Color",
244           "Foreground color to use (big-endian ARGB)", 0, G_MAXUINT32,
245           DEFAULT_FOREGROUND_COLOR,
246           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247   /**
248    * GstVideoTestSrc:background-color
249    *
250    * Color to use for background color of some patterns.  Default is
251    * black (0xff000000).
252    *
253    * Since: 0.10.31
254    **/
255   g_object_class_install_property (gobject_class, PROP_BACKGROUND_COLOR,
256       g_param_spec_uint ("background-color", "Background Color",
257           "Background color to use (big-endian ARGB)", 0, G_MAXUINT32,
258           DEFAULT_BACKGROUND_COLOR,
259           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260
261   g_object_class_install_property (gobject_class, PROP_HORIZONTAL_SPEED,
262       g_param_spec_int ("horizontal-speed", "Horizontal Speed",
263           "Scroll image number of pixels per frame (positive is scroll to the left)",
264           G_MININT32, G_MAXINT32, DEFAULT_HORIZONTAL_SPEED,
265           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266
267   gst_element_class_set_static_metadata (gstelement_class,
268       "Video test source", "Source/Video",
269       "Creates a test video stream", "David A. Schleef <ds@schleef.org>");
270
271   templ_caps = gst_video_test_src_getcaps (NULL, NULL);
272   gst_element_class_add_pad_template (gstelement_class,
273       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, templ_caps));
274   gst_caps_unref (templ_caps);
275
276   gstbasesrc_class->get_caps = gst_video_test_src_getcaps;
277   gstbasesrc_class->set_caps = gst_video_test_src_setcaps;
278   gstbasesrc_class->fixate = gst_video_test_src_src_fixate;
279   gstbasesrc_class->is_seekable = gst_video_test_src_is_seekable;
280   gstbasesrc_class->do_seek = gst_video_test_src_do_seek;
281   gstbasesrc_class->query = gst_video_test_src_query;
282   gstbasesrc_class->get_times = gst_video_test_src_get_times;
283   gstbasesrc_class->start = gst_video_test_src_start;
284   gstbasesrc_class->stop = gst_video_test_src_stop;
285   gstbasesrc_class->decide_allocation = gst_video_test_src_decide_allocation;
286
287   gstpushsrc_class->fill = gst_video_test_src_fill;
288 }
289
290 static void
291 gst_video_test_src_init (GstVideoTestSrc * src)
292 {
293   gst_video_test_src_set_pattern (src, DEFAULT_PATTERN);
294
295   src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
296   src->foreground_color = DEFAULT_FOREGROUND_COLOR;
297   src->background_color = DEFAULT_BACKGROUND_COLOR;
298   src->horizontal_speed = DEFAULT_HORIZONTAL_SPEED;
299
300   /* we operate in time */
301   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
302   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
303 }
304
305 static GstCaps *
306 gst_video_test_src_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
307 {
308   GstStructure *structure;
309
310   caps = gst_caps_make_writable (caps);
311
312   structure = gst_caps_get_structure (caps, 0);
313
314   gst_structure_fixate_field_nearest_int (structure, "width", 320);
315   gst_structure_fixate_field_nearest_int (structure, "height", 240);
316   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
317   if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
318     gst_structure_fixate_field_nearest_fraction (structure,
319         "pixel-aspect-ratio", 1, 1);
320   if (gst_structure_has_field (structure, "colorimetry"))
321     gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
322   if (gst_structure_has_field (structure, "chroma-site"))
323     gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
324
325   if (gst_structure_has_field (structure, "interlaced"))
326     gst_structure_fixate_field_boolean (structure, "interlaced", FALSE);
327
328   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
329
330   return caps;
331 }
332
333 static void
334 gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
335     int pattern_type)
336 {
337   videotestsrc->pattern_type = pattern_type;
338
339   GST_DEBUG_OBJECT (videotestsrc, "setting pattern to %d", pattern_type);
340
341   switch (pattern_type) {
342     case GST_VIDEO_TEST_SRC_SMPTE:
343       videotestsrc->make_image = gst_video_test_src_smpte;
344       break;
345     case GST_VIDEO_TEST_SRC_SNOW:
346       videotestsrc->make_image = gst_video_test_src_snow;
347       break;
348     case GST_VIDEO_TEST_SRC_BLACK:
349       videotestsrc->make_image = gst_video_test_src_black;
350       break;
351     case GST_VIDEO_TEST_SRC_WHITE:
352       videotestsrc->make_image = gst_video_test_src_white;
353       break;
354     case GST_VIDEO_TEST_SRC_RED:
355       videotestsrc->make_image = gst_video_test_src_red;
356       break;
357     case GST_VIDEO_TEST_SRC_GREEN:
358       videotestsrc->make_image = gst_video_test_src_green;
359       break;
360     case GST_VIDEO_TEST_SRC_BLUE:
361       videotestsrc->make_image = gst_video_test_src_blue;
362       break;
363     case GST_VIDEO_TEST_SRC_CHECKERS1:
364       videotestsrc->make_image = gst_video_test_src_checkers1;
365       break;
366     case GST_VIDEO_TEST_SRC_CHECKERS2:
367       videotestsrc->make_image = gst_video_test_src_checkers2;
368       break;
369     case GST_VIDEO_TEST_SRC_CHECKERS4:
370       videotestsrc->make_image = gst_video_test_src_checkers4;
371       break;
372     case GST_VIDEO_TEST_SRC_CHECKERS8:
373       videotestsrc->make_image = gst_video_test_src_checkers8;
374       break;
375     case GST_VIDEO_TEST_SRC_CIRCULAR:
376       videotestsrc->make_image = gst_video_test_src_circular;
377       break;
378     case GST_VIDEO_TEST_SRC_BLINK:
379       videotestsrc->make_image = gst_video_test_src_blink;
380       break;
381     case GST_VIDEO_TEST_SRC_SMPTE75:
382       videotestsrc->make_image = gst_video_test_src_smpte75;
383       break;
384     case GST_VIDEO_TEST_SRC_ZONE_PLATE:
385       videotestsrc->make_image = gst_video_test_src_zoneplate;
386       break;
387     case GST_VIDEO_TEST_SRC_GAMUT:
388       videotestsrc->make_image = gst_video_test_src_gamut;
389       break;
390     case GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE:
391       videotestsrc->make_image = gst_video_test_src_chromazoneplate;
392       break;
393     case GST_VIDEO_TEST_SRC_SOLID:
394       videotestsrc->make_image = gst_video_test_src_solid;
395       break;
396     case GST_VIDEO_TEST_SRC_BALL:
397       videotestsrc->make_image = gst_video_test_src_ball;
398       break;
399     case GST_VIDEO_TEST_SRC_SMPTE100:
400       videotestsrc->make_image = gst_video_test_src_smpte100;
401       break;
402     case GST_VIDEO_TEST_SRC_BAR:
403       videotestsrc->make_image = gst_video_test_src_bar;
404       break;
405     default:
406       g_assert_not_reached ();
407   }
408 }
409
410 static void
411 gst_video_test_src_set_property (GObject * object, guint prop_id,
412     const GValue * value, GParamSpec * pspec)
413 {
414   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
415
416   switch (prop_id) {
417     case PROP_PATTERN:
418       gst_video_test_src_set_pattern (src, g_value_get_enum (value));
419       break;
420     case PROP_TIMESTAMP_OFFSET:
421       src->timestamp_offset = g_value_get_int64 (value);
422       break;
423     case PROP_IS_LIVE:
424       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
425       break;
426     case PROP_K0:
427       src->k0 = g_value_get_int (value);
428       break;
429     case PROP_KX:
430       src->kx = g_value_get_int (value);
431       break;
432     case PROP_KY:
433       src->ky = g_value_get_int (value);
434       break;
435     case PROP_KT:
436       src->kt = g_value_get_int (value);
437       break;
438     case PROP_KXT:
439       src->kxt = g_value_get_int (value);
440       break;
441     case PROP_KYT:
442       src->kyt = g_value_get_int (value);
443       break;
444     case PROP_KXY:
445       src->kxy = g_value_get_int (value);
446       break;
447     case PROP_KX2:
448       src->kx2 = g_value_get_int (value);
449       break;
450     case PROP_KY2:
451       src->ky2 = g_value_get_int (value);
452       break;
453     case PROP_KT2:
454       src->kt2 = g_value_get_int (value);
455       break;
456     case PROP_XOFFSET:
457       src->xoffset = g_value_get_int (value);
458       break;
459     case PROP_YOFFSET:
460       src->yoffset = g_value_get_int (value);
461       break;
462     case PROP_FOREGROUND_COLOR:
463       src->foreground_color = g_value_get_uint (value);
464       break;
465     case PROP_BACKGROUND_COLOR:
466       src->background_color = g_value_get_uint (value);
467       break;
468     case PROP_HORIZONTAL_SPEED:
469       src->horizontal_speed = g_value_get_int (value);
470     default:
471       break;
472   }
473 }
474
475 static void
476 gst_video_test_src_get_property (GObject * object, guint prop_id,
477     GValue * value, GParamSpec * pspec)
478 {
479   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
480
481   switch (prop_id) {
482     case PROP_PATTERN:
483       g_value_set_enum (value, src->pattern_type);
484       break;
485     case PROP_TIMESTAMP_OFFSET:
486       g_value_set_int64 (value, src->timestamp_offset);
487       break;
488     case PROP_IS_LIVE:
489       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
490       break;
491     case PROP_K0:
492       g_value_set_int (value, src->k0);
493       break;
494     case PROP_KX:
495       g_value_set_int (value, src->kx);
496       break;
497     case PROP_KY:
498       g_value_set_int (value, src->ky);
499       break;
500     case PROP_KT:
501       g_value_set_int (value, src->kt);
502       break;
503     case PROP_KXT:
504       g_value_set_int (value, src->kxt);
505       break;
506     case PROP_KYT:
507       g_value_set_int (value, src->kyt);
508       break;
509     case PROP_KXY:
510       g_value_set_int (value, src->kxy);
511       break;
512     case PROP_KX2:
513       g_value_set_int (value, src->kx2);
514       break;
515     case PROP_KY2:
516       g_value_set_int (value, src->ky2);
517       break;
518     case PROP_KT2:
519       g_value_set_int (value, src->kt2);
520       break;
521     case PROP_XOFFSET:
522       g_value_set_int (value, src->xoffset);
523       break;
524     case PROP_YOFFSET:
525       g_value_set_int (value, src->yoffset);
526       break;
527     case PROP_FOREGROUND_COLOR:
528       g_value_set_uint (value, src->foreground_color);
529       break;
530     case PROP_BACKGROUND_COLOR:
531       g_value_set_uint (value, src->background_color);
532       break;
533     case PROP_HORIZONTAL_SPEED:
534       g_value_set_int (value, src->horizontal_speed);
535       break;
536     default:
537       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
538       break;
539   }
540 }
541
542 /* threadsafe because this gets called as the plugin is loaded */
543 static GstCaps *
544 gst_video_test_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
545 {
546   static GstCaps *capslist = NULL;
547
548   if (!capslist) {
549     GstCaps *caps;
550     GstStructure *structure;
551     int i;
552
553     caps = gst_caps_new_empty ();
554     for (i = 0; i < n_formats; i++) {
555       structure = paint_get_structure (format_list + i);
556       gst_structure_set (structure,
557           "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
558           "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
559           "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
560       gst_caps_append_structure (caps, structure);
561     }
562
563     capslist = caps;
564   }
565
566   if (filter)
567     return gst_caps_intersect_full (filter, capslist, GST_CAPS_INTERSECT_FIRST);
568   else
569     return gst_caps_ref (capslist);
570 }
571
572 static gboolean
573 gst_video_test_src_parse_caps (const GstCaps * caps,
574     gint * width, gint * height, gint * fps_n, gint * fps_d,
575     GstVideoColorimetry * colorimetry)
576 {
577   const GstStructure *structure;
578   GstPadLinkReturn ret;
579   const GValue *framerate;
580   const gchar *csp;
581
582   GST_DEBUG ("parsing caps");
583
584   structure = gst_caps_get_structure (caps, 0);
585
586   ret = gst_structure_get_int (structure, "width", width);
587   ret &= gst_structure_get_int (structure, "height", height);
588   framerate = gst_structure_get_value (structure, "framerate");
589
590   if (framerate) {
591     *fps_n = gst_value_get_fraction_numerator (framerate);
592     *fps_d = gst_value_get_fraction_denominator (framerate);
593   } else
594     goto no_framerate;
595
596   if ((csp = gst_structure_get_string (structure, "colorimetry")))
597     gst_video_colorimetry_from_string (colorimetry, csp);
598
599   return ret;
600
601   /* ERRORS */
602 no_framerate:
603   {
604     GST_DEBUG ("videotestsrc no framerate given");
605     return FALSE;
606   }
607 }
608
609 static gboolean
610 gst_video_test_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
611 {
612   GstVideoTestSrc *videotestsrc;
613   GstBufferPool *pool;
614   gboolean update;
615   guint size, min, max;
616   GstStructure *config;
617
618   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
619
620   if (gst_query_get_n_allocation_pools (query) > 0) {
621     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
622
623     /* adjust size */
624     size = MAX (size, videotestsrc->info.size);
625     update = TRUE;
626   } else {
627     pool = NULL;
628     size = videotestsrc->info.size;
629     min = max = 0;
630     update = FALSE;
631   }
632
633   /* no downstream pool, make our own */
634   if (pool == NULL) {
635     pool = gst_video_buffer_pool_new ();
636   }
637
638   config = gst_buffer_pool_get_config (pool);
639   if (gst_query_has_allocation_meta (query, GST_VIDEO_META_API_TYPE)) {
640     gst_buffer_pool_config_add_option (config,
641         GST_BUFFER_POOL_OPTION_VIDEO_META);
642   }
643   gst_buffer_pool_set_config (pool, config);
644
645   if (update)
646     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
647   else
648     gst_query_add_allocation_pool (query, pool, size, min, max);
649
650   if (pool)
651     gst_object_unref (pool);
652
653   return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
654 }
655
656 static gboolean
657 gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
658 {
659   struct format_list_struct *format;
660   const GstStructure *structure;
661   GstVideoTestSrc *videotestsrc;
662   GstVideoInfo info;
663
664   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
665
666   structure = gst_caps_get_structure (caps, 0);
667
668   if (gst_structure_has_name (structure, "video/x-raw")) {
669     /* we can use the parsing code */
670     if (!gst_video_info_from_caps (&info, caps))
671       goto parse_failed;
672
673   } else if (gst_structure_has_name (structure, "video/x-bayer")) {
674     if (!gst_video_test_src_parse_caps (caps, &info.width, &info.height,
675             &info.fps_n, &info.fps_d, &info.colorimetry))
676       goto parse_failed;
677
678     info.size =
679         gst_video_test_src_get_size (videotestsrc, info.width, info.height);
680   }
681
682   if (!(format = paintinfo_find_by_structure (structure)))
683     goto unknown_format;
684
685   /* looks ok here */
686   videotestsrc->format = format;
687   videotestsrc->info = info;
688
689   GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
690       info.width, info.height, info.fps_n, info.fps_d);
691
692   g_free (videotestsrc->tmpline);
693   g_free (videotestsrc->tmpline2);
694   g_free (videotestsrc->tmpline_u8);
695   videotestsrc->tmpline_u8 = g_malloc (info.width + 8);
696   videotestsrc->tmpline = g_malloc ((info.width + 8) * 4);
697   videotestsrc->tmpline2 = g_malloc ((info.width + 8) * 4);
698
699   return TRUE;
700
701   /* ERRORS */
702 parse_failed:
703   {
704     GST_DEBUG_OBJECT (bsrc, "failed to parse caps");
705     return FALSE;
706   }
707 unknown_format:
708   {
709     GST_DEBUG ("videotestsrc format not found");
710     return FALSE;
711   }
712 }
713
714 static gboolean
715 gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
716 {
717   gboolean res;
718   GstVideoTestSrc *src;
719
720   src = GST_VIDEO_TEST_SRC (bsrc);
721
722   switch (GST_QUERY_TYPE (query)) {
723     case GST_QUERY_CONVERT:
724     {
725       GstFormat src_fmt, dest_fmt;
726       gint64 src_val, dest_val;
727
728       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
729       res =
730           gst_video_info_convert (&src->info, src_fmt, src_val, dest_fmt,
731           &dest_val);
732       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
733       break;
734     }
735     default:
736       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
737       break;
738   }
739   return res;
740 }
741
742 static void
743 gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
744     GstClockTime * start, GstClockTime * end)
745 {
746   /* for live sources, sync on the timestamp of the buffer */
747   if (gst_base_src_is_live (basesrc)) {
748     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
749
750     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
751       /* get duration to calculate end time */
752       GstClockTime duration = GST_BUFFER_DURATION (buffer);
753
754       if (GST_CLOCK_TIME_IS_VALID (duration)) {
755         *end = timestamp + duration;
756       }
757       *start = timestamp;
758     }
759   } else {
760     *start = -1;
761     *end = -1;
762   }
763 }
764
765 static gboolean
766 gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
767 {
768   GstClockTime position;
769   GstVideoTestSrc *src;
770
771   src = GST_VIDEO_TEST_SRC (bsrc);
772
773   segment->time = segment->start;
774   position = segment->position;
775
776   /* now move to the position indicated */
777   if (src->info.fps_n) {
778     src->n_frames = gst_util_uint64_scale (position,
779         src->info.fps_n, src->info.fps_d * GST_SECOND);
780   } else {
781     src->n_frames = 0;
782   }
783   if (src->info.fps_n) {
784     src->running_time = gst_util_uint64_scale (src->n_frames,
785         src->info.fps_d * GST_SECOND, src->info.fps_n);
786   } else {
787     /* FIXME : Not sure what to set here */
788     src->running_time = 0;
789   }
790
791   g_assert (src->running_time <= position);
792
793   return TRUE;
794 }
795
796 static gboolean
797 gst_video_test_src_is_seekable (GstBaseSrc * psrc)
798 {
799   /* we're seekable... */
800   return TRUE;
801 }
802
803 static GstFlowReturn
804 gst_video_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
805 {
806   GstVideoTestSrc *src;
807   GstClockTime next_time;
808   GstVideoFrame frame;
809
810   src = GST_VIDEO_TEST_SRC (psrc);
811
812   if (G_UNLIKELY (src->format == NULL))
813     goto not_negotiated;
814
815   /* 0 framerate and we are at the second frame, eos */
816   if (G_UNLIKELY (src->info.fps_n == 0 && src->n_frames == 1))
817     goto eos;
818
819   GST_LOG_OBJECT (src,
820       "creating buffer from pool for frame %d", (gint) src->n_frames);
821
822   if (!gst_video_frame_map (&frame, &src->info, buffer, GST_MAP_WRITE))
823     goto invalid_frame;
824
825   src->make_image (src, &frame);
826
827   gst_video_frame_unmap (&frame);
828
829   GST_BUFFER_TIMESTAMP (buffer) = src->timestamp_offset + src->running_time;
830   GST_BUFFER_OFFSET (buffer) = src->n_frames;
831   src->n_frames++;
832   GST_BUFFER_OFFSET_END (buffer) = src->n_frames;
833   if (src->info.fps_n) {
834     next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
835         src->info.fps_d, src->info.fps_n);
836     GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
837   } else {
838     next_time = src->timestamp_offset;
839     /* NONE means forever */
840     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
841   }
842
843   src->running_time = next_time;
844
845   return GST_FLOW_OK;
846
847 not_negotiated:
848   {
849     GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
850         ("format wasn't negotiated before get function"));
851     return GST_FLOW_NOT_NEGOTIATED;
852   }
853 eos:
854   {
855     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
856     return GST_FLOW_EOS;
857   }
858 invalid_frame:
859   {
860     GST_DEBUG_OBJECT (src, "invalid frame");
861     return GST_FLOW_OK;
862   }
863 }
864
865 static gboolean
866 gst_video_test_src_start (GstBaseSrc * basesrc)
867 {
868   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
869
870   src->running_time = 0;
871   src->n_frames = 0;
872
873   return TRUE;
874 }
875
876 static gboolean
877 gst_video_test_src_stop (GstBaseSrc * basesrc)
878 {
879   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
880
881   g_free (src->tmpline);
882   src->tmpline = NULL;
883   g_free (src->tmpline2);
884   src->tmpline2 = NULL;
885   g_free (src->tmpline_u8);
886   src->tmpline_u8 = NULL;
887
888   return TRUE;
889 }
890
891 static gboolean
892 plugin_init (GstPlugin * plugin)
893 {
894   GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
895       "Video Test Source");
896
897   return gst_element_register (plugin, "videotestsrc", GST_RANK_NONE,
898       GST_TYPE_VIDEO_TEST_SRC);
899 }
900
901 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
902     GST_VERSION_MINOR,
903     videotestsrc,
904     "Creates a test video stream",
905     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)