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