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