update for allocation query changes
[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_details_simple (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
614   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
615
616   if (gst_query_get_n_allocation_pools (query) > 0) {
617     GstBufferPool *pool;
618     guint size, min, max;
619
620     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
621
622     /* adjust size */
623     size = MAX (size, videotestsrc->info.size);
624
625     if (pool) {
626       GstStructure *config;
627
628       config = gst_buffer_pool_get_config (pool);
629       gst_buffer_pool_config_add_option (config,
630           GST_BUFFER_POOL_OPTION_VIDEO_META);
631       gst_buffer_pool_set_config (pool, config);
632     }
633     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
634
635     if (pool)
636       gst_object_unref (pool);
637   }
638   return TRUE;
639 }
640
641 static gboolean
642 gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
643 {
644   struct format_list_struct *format;
645   const GstStructure *structure;
646   GstVideoTestSrc *videotestsrc;
647   GstVideoInfo info;
648
649   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
650
651   structure = gst_caps_get_structure (caps, 0);
652
653   if (gst_structure_has_name (structure, "video/x-raw")) {
654     /* we can use the parsing code */
655     if (!gst_video_info_from_caps (&info, caps))
656       goto parse_failed;
657
658   } else if (gst_structure_has_name (structure, "video/x-bayer")) {
659     if (!gst_video_test_src_parse_caps (caps, &info.width, &info.height,
660             &info.fps_n, &info.fps_d, &info.colorimetry))
661       goto parse_failed;
662
663     info.size =
664         gst_video_test_src_get_size (videotestsrc, info.width, info.height);
665   }
666
667   if (!(format = paintinfo_find_by_structure (structure)))
668     goto unknown_format;
669
670   /* looks ok here */
671   videotestsrc->format = format;
672   videotestsrc->info = info;
673
674   GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
675       info.width, info.height, info.fps_n, info.fps_d);
676
677   g_free (videotestsrc->tmpline);
678   g_free (videotestsrc->tmpline2);
679   g_free (videotestsrc->tmpline_u8);
680   videotestsrc->tmpline_u8 = g_malloc (info.width + 8);
681   videotestsrc->tmpline = g_malloc ((info.width + 8) * 4);
682   videotestsrc->tmpline2 = g_malloc ((info.width + 8) * 4);
683
684   return TRUE;
685
686   /* ERRORS */
687 parse_failed:
688   {
689     GST_DEBUG_OBJECT (bsrc, "failed to parse caps");
690     return FALSE;
691   }
692 unknown_format:
693   {
694     GST_DEBUG ("videotestsrc format not found");
695     return FALSE;
696   }
697 }
698
699 static gboolean
700 gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
701 {
702   gboolean res;
703   GstVideoTestSrc *src;
704
705   src = GST_VIDEO_TEST_SRC (bsrc);
706
707   switch (GST_QUERY_TYPE (query)) {
708     case GST_QUERY_CONVERT:
709     {
710       GstFormat src_fmt, dest_fmt;
711       gint64 src_val, dest_val;
712
713       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
714       res =
715           gst_video_info_convert (&src->info, src_fmt, src_val, dest_fmt,
716           &dest_val);
717       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
718       break;
719     }
720     default:
721       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
722       break;
723   }
724   return res;
725 }
726
727 static void
728 gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
729     GstClockTime * start, GstClockTime * end)
730 {
731   /* for live sources, sync on the timestamp of the buffer */
732   if (gst_base_src_is_live (basesrc)) {
733     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
734
735     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
736       /* get duration to calculate end time */
737       GstClockTime duration = GST_BUFFER_DURATION (buffer);
738
739       if (GST_CLOCK_TIME_IS_VALID (duration)) {
740         *end = timestamp + duration;
741       }
742       *start = timestamp;
743     }
744   } else {
745     *start = -1;
746     *end = -1;
747   }
748 }
749
750 static gboolean
751 gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
752 {
753   GstClockTime position;
754   GstVideoTestSrc *src;
755
756   src = GST_VIDEO_TEST_SRC (bsrc);
757
758   segment->time = segment->start;
759   position = segment->position;
760
761   /* now move to the position indicated */
762   if (src->info.fps_n) {
763     src->n_frames = gst_util_uint64_scale (position,
764         src->info.fps_n, src->info.fps_d * GST_SECOND);
765   } else {
766     src->n_frames = 0;
767   }
768   if (src->info.fps_n) {
769     src->running_time = gst_util_uint64_scale (src->n_frames,
770         src->info.fps_d * GST_SECOND, src->info.fps_n);
771   } else {
772     /* FIXME : Not sure what to set here */
773     src->running_time = 0;
774   }
775
776   g_assert (src->running_time <= position);
777
778   return TRUE;
779 }
780
781 static gboolean
782 gst_video_test_src_is_seekable (GstBaseSrc * psrc)
783 {
784   /* we're seekable... */
785   return TRUE;
786 }
787
788 static GstFlowReturn
789 gst_video_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
790 {
791   GstVideoTestSrc *src;
792   GstClockTime next_time;
793   GstVideoFrame frame;
794
795   src = GST_VIDEO_TEST_SRC (psrc);
796
797   if (G_UNLIKELY (src->format == NULL))
798     goto not_negotiated;
799
800   /* 0 framerate and we are at the second frame, eos */
801   if (G_UNLIKELY (src->info.fps_n == 0 && src->n_frames == 1))
802     goto eos;
803
804   GST_LOG_OBJECT (src,
805       "creating buffer from pool for frame %d", (gint) src->n_frames);
806
807   if (!gst_video_frame_map (&frame, &src->info, buffer, GST_MAP_WRITE))
808     goto invalid_frame;
809
810   src->make_image (src, &frame);
811
812   gst_video_frame_unmap (&frame);
813
814   GST_BUFFER_TIMESTAMP (buffer) = src->timestamp_offset + src->running_time;
815   GST_BUFFER_OFFSET (buffer) = src->n_frames;
816   src->n_frames++;
817   GST_BUFFER_OFFSET_END (buffer) = src->n_frames;
818   if (src->info.fps_n) {
819     next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
820         src->info.fps_d, src->info.fps_n);
821     GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
822   } else {
823     next_time = src->timestamp_offset;
824     /* NONE means forever */
825     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
826   }
827
828   src->running_time = next_time;
829
830   return GST_FLOW_OK;
831
832 not_negotiated:
833   {
834     GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
835         ("format wasn't negotiated before get function"));
836     return GST_FLOW_NOT_NEGOTIATED;
837   }
838 eos:
839   {
840     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
841     return GST_FLOW_EOS;
842   }
843 invalid_frame:
844   {
845     GST_DEBUG_OBJECT (src, "invalid frame");
846     return GST_FLOW_OK;
847   }
848 }
849
850 static gboolean
851 gst_video_test_src_start (GstBaseSrc * basesrc)
852 {
853   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
854
855   src->running_time = 0;
856   src->n_frames = 0;
857
858   return TRUE;
859 }
860
861 static gboolean
862 gst_video_test_src_stop (GstBaseSrc * basesrc)
863 {
864   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
865
866   g_free (src->tmpline);
867   src->tmpline = NULL;
868   g_free (src->tmpline2);
869   src->tmpline2 = NULL;
870   g_free (src->tmpline_u8);
871   src->tmpline_u8 = NULL;
872
873   return TRUE;
874 }
875
876 static gboolean
877 plugin_init (GstPlugin * plugin)
878 {
879   gst_videotestsrc_orc_init ();
880
881   GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
882       "Video Test Source");
883
884   return gst_element_register (plugin, "videotestsrc", GST_RANK_NONE,
885       GST_TYPE_VIDEO_TEST_SRC);
886 }
887
888 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
889     GST_VERSION_MINOR,
890     "videotestsrc",
891     "Creates a test video stream",
892     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)