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