d82ce2d96d05f54faafc3f2e1e4b53f5d6bc4c65
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-videotestsrc
23  *
24  * The videotestsrc element is used to produce test video data in a wide variety
25  * of formats. The video test data produced can be controlled with the "pattern"
26  * property.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch -v videotestsrc pattern=snow ! ximagesink
32  * ]| Shows random noise in an X window.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include "gstvideotestsrc.h"
40 #include "gstvideotestsrcorc.h"
41 #include "videotestsrc.h"
42
43 #include <string.h>
44 #include <stdlib.h>
45
46 GST_DEBUG_CATEGORY_STATIC (video_test_src_debug);
47 #define GST_CAT_DEFAULT video_test_src_debug
48
49 #define DEFAULT_PATTERN            GST_VIDEO_TEST_SRC_SMPTE
50 #define DEFAULT_TIMESTAMP_OFFSET   0
51 #define DEFAULT_IS_LIVE            FALSE
52 #define DEFAULT_COLOR_SPEC         GST_VIDEO_TEST_SRC_BT601
53 #define DEFAULT_FOREGROUND_COLOR   0xffffffff
54 #define DEFAULT_BACKGROUND_COLOR   0xff000000
55 #define DEFAULT_HORIZONTAL_SPEED   0
56
57 enum
58 {
59   PROP_0,
60   PROP_PATTERN,
61   PROP_TIMESTAMP_OFFSET,
62   PROP_IS_LIVE,
63   PROP_K0,
64   PROP_KX,
65   PROP_KY,
66   PROP_KT,
67   PROP_KXT,
68   PROP_KYT,
69   PROP_KXY,
70   PROP_KX2,
71   PROP_KY2,
72   PROP_KT2,
73   PROP_XOFFSET,
74   PROP_YOFFSET,
75   PROP_FOREGROUND_COLOR,
76   PROP_BACKGROUND_COLOR,
77   PROP_HORIZONTAL_SPEED,
78   PROP_LAST
79 };
80
81
82 #define VTS_VIDEO_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ";" \
83   "video/x-bayer, format=(string) { bggr, rggb, grbg, gbrg }, "        \
84   "width = " GST_VIDEO_SIZE_RANGE ", "                                 \
85   "height = " GST_VIDEO_SIZE_RANGE ", "                                \
86   "framerate = " GST_VIDEO_FPS_RANGE
87
88
89 static GstStaticPadTemplate gst_video_test_src_template =
90 GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS (VTS_VIDEO_CAPS)
94     );
95
96 #define gst_video_test_src_parent_class parent_class
97 G_DEFINE_TYPE (GstVideoTestSrc, gst_video_test_src, GST_TYPE_PUSH_SRC);
98
99 static void gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
100     int pattern_type);
101 static void gst_video_test_src_set_property (GObject * object, guint prop_id,
102     const GValue * value, GParamSpec * pspec);
103 static void gst_video_test_src_get_property (GObject * object, guint prop_id,
104     GValue * value, GParamSpec * pspec);
105
106 static gboolean gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
107 static GstCaps *gst_video_test_src_src_fixate (GstBaseSrc * bsrc,
108     GstCaps * caps);
109
110 static gboolean gst_video_test_src_is_seekable (GstBaseSrc * psrc);
111 static gboolean gst_video_test_src_do_seek (GstBaseSrc * bsrc,
112     GstSegment * segment);
113 static gboolean gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query);
114
115 static void gst_video_test_src_get_times (GstBaseSrc * basesrc,
116     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
117 static gboolean gst_video_test_src_decide_allocation (GstBaseSrc * bsrc,
118     GstQuery * query);
119 static GstFlowReturn gst_video_test_src_fill (GstPushSrc * psrc,
120     GstBuffer * buffer);
121 static gboolean gst_video_test_src_start (GstBaseSrc * basesrc);
122 static gboolean gst_video_test_src_stop (GstBaseSrc * basesrc);
123
124 #define GST_TYPE_VIDEO_TEST_SRC_PATTERN (gst_video_test_src_pattern_get_type ())
125 static GType
126 gst_video_test_src_pattern_get_type (void)
127 {
128   static GType video_test_src_pattern_type = 0;
129   static const GEnumValue pattern_types[] = {
130     {GST_VIDEO_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
131     {GST_VIDEO_TEST_SRC_SNOW, "Random (television snow)", "snow"},
132     {GST_VIDEO_TEST_SRC_BLACK, "100% Black", "black"},
133     {GST_VIDEO_TEST_SRC_WHITE, "100% White", "white"},
134     {GST_VIDEO_TEST_SRC_RED, "Red", "red"},
135     {GST_VIDEO_TEST_SRC_GREEN, "Green", "green"},
136     {GST_VIDEO_TEST_SRC_BLUE, "Blue", "blue"},
137     {GST_VIDEO_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
138     {GST_VIDEO_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
139     {GST_VIDEO_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
140     {GST_VIDEO_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
141     {GST_VIDEO_TEST_SRC_CIRCULAR, "Circular", "circular"},
142     {GST_VIDEO_TEST_SRC_BLINK, "Blink", "blink"},
143     {GST_VIDEO_TEST_SRC_SMPTE75, "SMPTE 75% color bars", "smpte75"},
144     {GST_VIDEO_TEST_SRC_ZONE_PLATE, "Zone plate", "zone-plate"},
145     {GST_VIDEO_TEST_SRC_GAMUT, "Gamut checkers", "gamut"},
146     {GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE, "Chroma zone plate",
147         "chroma-zone-plate"},
148     {GST_VIDEO_TEST_SRC_SOLID, "Solid color", "solid-color"},
149     {GST_VIDEO_TEST_SRC_BALL, "Moving ball", "ball"},
150     {GST_VIDEO_TEST_SRC_SMPTE100, "SMPTE 100% color bars", "smpte100"},
151     {GST_VIDEO_TEST_SRC_BAR, "Bar", "bar"},
152     {GST_VIDEO_TEST_SRC_PINWHEEL, "Pinwheel", "pinwheel"},
153     {GST_VIDEO_TEST_SRC_SPOKES, "Spokes", "spokes"},
154     {0, NULL, NULL}
155   };
156
157   if (!video_test_src_pattern_type) {
158     video_test_src_pattern_type =
159         g_enum_register_static ("GstVideoTestSrcPattern", pattern_types);
160   }
161   return video_test_src_pattern_type;
162 }
163
164 static void
165 gst_video_test_src_class_init (GstVideoTestSrcClass * klass)
166 {
167   GObjectClass *gobject_class;
168   GstElementClass *gstelement_class;
169   GstBaseSrcClass *gstbasesrc_class;
170   GstPushSrcClass *gstpushsrc_class;
171
172   gobject_class = (GObjectClass *) klass;
173   gstelement_class = (GstElementClass *) klass;
174   gstbasesrc_class = (GstBaseSrcClass *) klass;
175   gstpushsrc_class = (GstPushSrcClass *) klass;
176
177   gobject_class->set_property = gst_video_test_src_set_property;
178   gobject_class->get_property = gst_video_test_src_get_property;
179
180   g_object_class_install_property (gobject_class, PROP_PATTERN,
181       g_param_spec_enum ("pattern", "Pattern",
182           "Type of test pattern to generate", GST_TYPE_VIDEO_TEST_SRC_PATTERN,
183           DEFAULT_PATTERN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184   g_object_class_install_property (gobject_class, PROP_TIMESTAMP_OFFSET,
185       g_param_spec_int64 ("timestamp-offset", "Timestamp offset",
186           "An offset added to timestamps set on buffers (in ns)", G_MININT64,
187           G_MAXINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
189       g_param_spec_boolean ("is-live", "Is Live",
190           "Whether to act as a live source", DEFAULT_IS_LIVE,
191           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192   g_object_class_install_property (gobject_class, PROP_K0,
193       g_param_spec_int ("k0", "Zoneplate zero order phase",
194           "Zoneplate zero order phase, for generating plain fields or phase offsets",
195           G_MININT32, G_MAXINT32, 0,
196           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197   g_object_class_install_property (gobject_class, PROP_KX,
198       g_param_spec_int ("kx", "Zoneplate 1st order x phase",
199           "Zoneplate 1st order x phase, for generating constant horizontal frequencies",
200           G_MININT32, G_MAXINT32, 0,
201           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202   g_object_class_install_property (gobject_class, PROP_KY,
203       g_param_spec_int ("ky", "Zoneplate 1st order y phase",
204           "Zoneplate 1st order y phase, for generating contant vertical frequencies",
205           G_MININT32, G_MAXINT32, 0,
206           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207   g_object_class_install_property (gobject_class, PROP_KT,
208       g_param_spec_int ("kt", "Zoneplate 1st order t phase",
209           "Zoneplate 1st order t phase, for generating phase rotation as a function of time",
210           G_MININT32, G_MAXINT32, 0,
211           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212   g_object_class_install_property (gobject_class, PROP_KXT,
213       g_param_spec_int ("kxt", "Zoneplate x*t product phase",
214           "Zoneplate x*t product phase, normalised to kxy/256 cycles per vertical pixel at width/2 from origin",
215           G_MININT32, G_MAXINT32, 0,
216           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217   g_object_class_install_property (gobject_class, PROP_KYT,
218       g_param_spec_int ("kyt", "Zoneplate y*t product phase",
219           "Zoneplate y*t product phase", G_MININT32, G_MAXINT32, 0,
220           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221   g_object_class_install_property (gobject_class, PROP_KXY,
222       g_param_spec_int ("kxy", "Zoneplate x*y product phase",
223           "Zoneplate x*y product phase", G_MININT32, G_MAXINT32, 0,
224           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225   g_object_class_install_property (gobject_class, PROP_KX2,
226       g_param_spec_int ("kx2", "Zoneplate 2nd order x phase",
227           "Zoneplate 2nd order x phase, normalised to kx2/256 cycles per horizontal pixel at width/2 from origin",
228           G_MININT32, G_MAXINT32, 0,
229           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230   g_object_class_install_property (gobject_class, PROP_KY2,
231       g_param_spec_int ("ky2", "Zoneplate 2nd order y phase",
232           "Zoneplate 2nd order y phase, normailsed to ky2/256 cycles per vertical pixel at height/2 from origin",
233           G_MININT32, G_MAXINT32, 0,
234           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235   g_object_class_install_property (gobject_class, PROP_KT2,
236       g_param_spec_int ("kt2", "Zoneplate 2nd order t phase",
237           "Zoneplate 2nd order t phase, t*t/256 cycles per picture", G_MININT32,
238           G_MAXINT32, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239   g_object_class_install_property (gobject_class, PROP_XOFFSET,
240       g_param_spec_int ("xoffset", "Zoneplate 2nd order products x offset",
241           "Zoneplate 2nd order products x offset", G_MININT32, G_MAXINT32, 0,
242           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243   g_object_class_install_property (gobject_class, PROP_YOFFSET,
244       g_param_spec_int ("yoffset", "Zoneplate 2nd order products y offset",
245           "Zoneplate 2nd order products y offset", G_MININT32, G_MAXINT32, 0,
246           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247   /**
248    * GstVideoTestSrc:foreground-color
249    *
250    * Color to use for solid-color pattern and foreground color of other
251    * patterns.  Default is white (0xffffffff).
252    */
253   g_object_class_install_property (gobject_class, PROP_FOREGROUND_COLOR,
254       g_param_spec_uint ("foreground-color", "Foreground Color",
255           "Foreground color to use (big-endian ARGB)", 0, G_MAXUINT32,
256           DEFAULT_FOREGROUND_COLOR,
257           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
258   /**
259    * GstVideoTestSrc:background-color
260    *
261    * Color to use for background color of some patterns.  Default is
262    * black (0xff000000).
263    */
264   g_object_class_install_property (gobject_class, PROP_BACKGROUND_COLOR,
265       g_param_spec_uint ("background-color", "Background Color",
266           "Background color to use (big-endian ARGB)", 0, G_MAXUINT32,
267           DEFAULT_BACKGROUND_COLOR,
268           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
269
270   g_object_class_install_property (gobject_class, PROP_HORIZONTAL_SPEED,
271       g_param_spec_int ("horizontal-speed", "Horizontal Speed",
272           "Scroll image number of pixels per frame (positive is scroll to the left)",
273           G_MININT32, G_MAXINT32, DEFAULT_HORIZONTAL_SPEED,
274           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275
276   gst_element_class_set_static_metadata (gstelement_class,
277       "Video test source", "Source/Video",
278       "Creates a test video stream", "David A. Schleef <ds@schleef.org>");
279
280   gst_element_class_add_pad_template (gstelement_class,
281       gst_static_pad_template_get (&gst_video_test_src_template));
282
283   gstbasesrc_class->set_caps = gst_video_test_src_setcaps;
284   gstbasesrc_class->fixate = gst_video_test_src_src_fixate;
285   gstbasesrc_class->is_seekable = gst_video_test_src_is_seekable;
286   gstbasesrc_class->do_seek = gst_video_test_src_do_seek;
287   gstbasesrc_class->query = gst_video_test_src_query;
288   gstbasesrc_class->get_times = gst_video_test_src_get_times;
289   gstbasesrc_class->start = gst_video_test_src_start;
290   gstbasesrc_class->stop = gst_video_test_src_stop;
291   gstbasesrc_class->decide_allocation = gst_video_test_src_decide_allocation;
292
293   gstpushsrc_class->fill = gst_video_test_src_fill;
294 }
295
296 static void
297 gst_video_test_src_init (GstVideoTestSrc * src)
298 {
299   gst_video_test_src_set_pattern (src, DEFAULT_PATTERN);
300
301   src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
302   src->foreground_color = DEFAULT_FOREGROUND_COLOR;
303   src->background_color = DEFAULT_BACKGROUND_COLOR;
304   src->horizontal_speed = DEFAULT_HORIZONTAL_SPEED;
305
306   /* we operate in time */
307   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
308   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
309 }
310
311 static GstCaps *
312 gst_video_test_src_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
313 {
314   GstStructure *structure;
315
316   caps = gst_caps_make_writable (caps);
317
318   structure = gst_caps_get_structure (caps, 0);
319
320   gst_structure_fixate_field_nearest_int (structure, "width", 320);
321   gst_structure_fixate_field_nearest_int (structure, "height", 240);
322   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
323
324   if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
325     gst_structure_fixate_field_nearest_fraction (structure,
326         "pixel-aspect-ratio", 1, 1);
327   else
328     gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
329         NULL);
330
331   if (gst_structure_has_field (structure, "colorimetry"))
332     gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
333   if (gst_structure_has_field (structure, "chroma-site"))
334     gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
335
336   if (gst_structure_has_field (structure, "interlace-mode"))
337     gst_structure_fixate_field_string (structure, "interlace-mode",
338         "progressive");
339   else
340     gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
341         "progressive", NULL);
342
343   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
344
345   return caps;
346 }
347
348 static void
349 gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
350     int pattern_type)
351 {
352   videotestsrc->pattern_type = pattern_type;
353
354   GST_DEBUG_OBJECT (videotestsrc, "setting pattern to %d", pattern_type);
355
356   switch (pattern_type) {
357     case GST_VIDEO_TEST_SRC_SMPTE:
358       videotestsrc->make_image = gst_video_test_src_smpte;
359       break;
360     case GST_VIDEO_TEST_SRC_SNOW:
361       videotestsrc->make_image = gst_video_test_src_snow;
362       break;
363     case GST_VIDEO_TEST_SRC_BLACK:
364       videotestsrc->make_image = gst_video_test_src_black;
365       break;
366     case GST_VIDEO_TEST_SRC_WHITE:
367       videotestsrc->make_image = gst_video_test_src_white;
368       break;
369     case GST_VIDEO_TEST_SRC_RED:
370       videotestsrc->make_image = gst_video_test_src_red;
371       break;
372     case GST_VIDEO_TEST_SRC_GREEN:
373       videotestsrc->make_image = gst_video_test_src_green;
374       break;
375     case GST_VIDEO_TEST_SRC_BLUE:
376       videotestsrc->make_image = gst_video_test_src_blue;
377       break;
378     case GST_VIDEO_TEST_SRC_CHECKERS1:
379       videotestsrc->make_image = gst_video_test_src_checkers1;
380       break;
381     case GST_VIDEO_TEST_SRC_CHECKERS2:
382       videotestsrc->make_image = gst_video_test_src_checkers2;
383       break;
384     case GST_VIDEO_TEST_SRC_CHECKERS4:
385       videotestsrc->make_image = gst_video_test_src_checkers4;
386       break;
387     case GST_VIDEO_TEST_SRC_CHECKERS8:
388       videotestsrc->make_image = gst_video_test_src_checkers8;
389       break;
390     case GST_VIDEO_TEST_SRC_CIRCULAR:
391       videotestsrc->make_image = gst_video_test_src_circular;
392       break;
393     case GST_VIDEO_TEST_SRC_BLINK:
394       videotestsrc->make_image = gst_video_test_src_blink;
395       break;
396     case GST_VIDEO_TEST_SRC_SMPTE75:
397       videotestsrc->make_image = gst_video_test_src_smpte75;
398       break;
399     case GST_VIDEO_TEST_SRC_ZONE_PLATE:
400       videotestsrc->make_image = gst_video_test_src_zoneplate;
401       break;
402     case GST_VIDEO_TEST_SRC_GAMUT:
403       videotestsrc->make_image = gst_video_test_src_gamut;
404       break;
405     case GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE:
406       videotestsrc->make_image = gst_video_test_src_chromazoneplate;
407       break;
408     case GST_VIDEO_TEST_SRC_SOLID:
409       videotestsrc->make_image = gst_video_test_src_solid;
410       break;
411     case GST_VIDEO_TEST_SRC_BALL:
412       videotestsrc->make_image = gst_video_test_src_ball;
413       break;
414     case GST_VIDEO_TEST_SRC_SMPTE100:
415       videotestsrc->make_image = gst_video_test_src_smpte100;
416       break;
417     case GST_VIDEO_TEST_SRC_BAR:
418       videotestsrc->make_image = gst_video_test_src_bar;
419       break;
420     case GST_VIDEO_TEST_SRC_PINWHEEL:
421       videotestsrc->make_image = gst_video_test_src_pinwheel;
422       break;
423     case GST_VIDEO_TEST_SRC_SPOKES:
424       videotestsrc->make_image = gst_video_test_src_spokes;
425       break;
426     default:
427       g_assert_not_reached ();
428   }
429 }
430
431 static void
432 gst_video_test_src_set_property (GObject * object, guint prop_id,
433     const GValue * value, GParamSpec * pspec)
434 {
435   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
436
437   switch (prop_id) {
438     case PROP_PATTERN:
439       gst_video_test_src_set_pattern (src, g_value_get_enum (value));
440       break;
441     case PROP_TIMESTAMP_OFFSET:
442       src->timestamp_offset = g_value_get_int64 (value);
443       break;
444     case PROP_IS_LIVE:
445       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
446       break;
447     case PROP_K0:
448       src->k0 = g_value_get_int (value);
449       break;
450     case PROP_KX:
451       src->kx = g_value_get_int (value);
452       break;
453     case PROP_KY:
454       src->ky = g_value_get_int (value);
455       break;
456     case PROP_KT:
457       src->kt = g_value_get_int (value);
458       break;
459     case PROP_KXT:
460       src->kxt = g_value_get_int (value);
461       break;
462     case PROP_KYT:
463       src->kyt = g_value_get_int (value);
464       break;
465     case PROP_KXY:
466       src->kxy = g_value_get_int (value);
467       break;
468     case PROP_KX2:
469       src->kx2 = g_value_get_int (value);
470       break;
471     case PROP_KY2:
472       src->ky2 = g_value_get_int (value);
473       break;
474     case PROP_KT2:
475       src->kt2 = g_value_get_int (value);
476       break;
477     case PROP_XOFFSET:
478       src->xoffset = g_value_get_int (value);
479       break;
480     case PROP_YOFFSET:
481       src->yoffset = g_value_get_int (value);
482       break;
483     case PROP_FOREGROUND_COLOR:
484       src->foreground_color = g_value_get_uint (value);
485       break;
486     case PROP_BACKGROUND_COLOR:
487       src->background_color = g_value_get_uint (value);
488       break;
489     case PROP_HORIZONTAL_SPEED:
490       src->horizontal_speed = g_value_get_int (value);
491     default:
492       break;
493   }
494 }
495
496 static void
497 gst_video_test_src_get_property (GObject * object, guint prop_id,
498     GValue * value, GParamSpec * pspec)
499 {
500   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
501
502   switch (prop_id) {
503     case PROP_PATTERN:
504       g_value_set_enum (value, src->pattern_type);
505       break;
506     case PROP_TIMESTAMP_OFFSET:
507       g_value_set_int64 (value, src->timestamp_offset);
508       break;
509     case PROP_IS_LIVE:
510       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
511       break;
512     case PROP_K0:
513       g_value_set_int (value, src->k0);
514       break;
515     case PROP_KX:
516       g_value_set_int (value, src->kx);
517       break;
518     case PROP_KY:
519       g_value_set_int (value, src->ky);
520       break;
521     case PROP_KT:
522       g_value_set_int (value, src->kt);
523       break;
524     case PROP_KXT:
525       g_value_set_int (value, src->kxt);
526       break;
527     case PROP_KYT:
528       g_value_set_int (value, src->kyt);
529       break;
530     case PROP_KXY:
531       g_value_set_int (value, src->kxy);
532       break;
533     case PROP_KX2:
534       g_value_set_int (value, src->kx2);
535       break;
536     case PROP_KY2:
537       g_value_set_int (value, src->ky2);
538       break;
539     case PROP_KT2:
540       g_value_set_int (value, src->kt2);
541       break;
542     case PROP_XOFFSET:
543       g_value_set_int (value, src->xoffset);
544       break;
545     case PROP_YOFFSET:
546       g_value_set_int (value, src->yoffset);
547       break;
548     case PROP_FOREGROUND_COLOR:
549       g_value_set_uint (value, src->foreground_color);
550       break;
551     case PROP_BACKGROUND_COLOR:
552       g_value_set_uint (value, src->background_color);
553       break;
554     case PROP_HORIZONTAL_SPEED:
555       g_value_set_int (value, src->horizontal_speed);
556       break;
557     default:
558       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559       break;
560   }
561 }
562
563 static gboolean
564 gst_video_test_src_parse_caps (const GstCaps * caps,
565     gint * width, gint * height, gint * fps_n, gint * fps_d,
566     GstVideoColorimetry * colorimetry, gint * x_inv, gint * y_inv)
567 {
568   const GstStructure *structure;
569   GstPadLinkReturn ret;
570   const GValue *framerate;
571   const gchar *str;
572
573   GST_DEBUG ("parsing caps");
574
575   structure = gst_caps_get_structure (caps, 0);
576
577   ret = gst_structure_get_int (structure, "width", width);
578   ret &= gst_structure_get_int (structure, "height", height);
579   framerate = gst_structure_get_value (structure, "framerate");
580
581   if (framerate) {
582     *fps_n = gst_value_get_fraction_numerator (framerate);
583     *fps_d = gst_value_get_fraction_denominator (framerate);
584   } else
585     goto no_framerate;
586
587   if ((str = gst_structure_get_string (structure, "colorimetry")))
588     gst_video_colorimetry_from_string (colorimetry, str);
589
590   if ((str = gst_structure_get_string (structure, "format"))) {
591     if (g_str_equal (str, "bggr")) {
592       *x_inv = *y_inv = 0;
593     } else if (g_str_equal (str, "rggb")) {
594       *x_inv = *y_inv = 1;
595     } else if (g_str_equal (str, "grbg")) {
596       *x_inv = 0;
597       *y_inv = 1;
598     } else if (g_str_equal (str, "grbg")) {
599       *x_inv = 1;
600       *y_inv = 0;
601     } else
602       goto invalid_format;
603   }
604   return ret;
605
606   /* ERRORS */
607 no_framerate:
608   {
609     GST_DEBUG ("videotestsrc no framerate given");
610     return FALSE;
611   }
612 invalid_format:
613   {
614     GST_DEBUG ("videotestsrc invalid bayer format given");
615     return FALSE;
616   }
617 }
618
619 static gboolean
620 gst_video_test_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
621 {
622   GstVideoTestSrc *videotestsrc;
623   GstBufferPool *pool;
624   gboolean update;
625   guint size, min, max;
626   GstStructure *config;
627
628   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
629
630   if (gst_query_get_n_allocation_pools (query) > 0) {
631     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
632
633     /* adjust size */
634     size = MAX (size, videotestsrc->info.size);
635     update = TRUE;
636   } else {
637     pool = NULL;
638     size = videotestsrc->info.size;
639     min = max = 0;
640     update = FALSE;
641   }
642
643   /* no downstream pool, make our own */
644   if (pool == NULL) {
645     if (videotestsrc->bayer)
646       pool = gst_buffer_pool_new ();
647     else
648       pool = gst_video_buffer_pool_new ();
649   }
650
651   config = gst_buffer_pool_get_config (pool);
652   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
653     gst_buffer_pool_config_add_option (config,
654         GST_BUFFER_POOL_OPTION_VIDEO_META);
655   }
656   gst_buffer_pool_set_config (pool, config);
657
658   if (update)
659     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
660   else
661     gst_query_add_allocation_pool (query, pool, size, min, max);
662
663   if (pool)
664     gst_object_unref (pool);
665
666   return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
667 }
668
669 static gboolean
670 gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
671 {
672   const GstStructure *structure;
673   GstVideoTestSrc *videotestsrc;
674   GstVideoInfo info;
675   guint i;
676   guint n_lines;
677   gint offset;
678
679   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
680
681   structure = gst_caps_get_structure (caps, 0);
682
683   if (gst_structure_has_name (structure, "video/x-raw")) {
684     /* we can use the parsing code */
685     if (!gst_video_info_from_caps (&info, caps))
686       goto parse_failed;
687
688   } else if (gst_structure_has_name (structure, "video/x-bayer")) {
689     gint x_inv = 0, y_inv = 0;
690
691     gst_video_info_init (&info);
692
693     info.finfo = gst_video_format_get_info (GST_VIDEO_FORMAT_GRAY8);
694
695     if (!gst_video_test_src_parse_caps (caps, &info.width, &info.height,
696             &info.fps_n, &info.fps_d, &info.colorimetry, &x_inv, &y_inv))
697       goto parse_failed;
698
699     info.size = GST_ROUND_UP_4 (info.width) * info.height;
700     info.stride[0] = GST_ROUND_UP_4 (info.width);
701
702     videotestsrc->bayer = TRUE;
703     videotestsrc->x_invert = x_inv;
704     videotestsrc->y_invert = y_inv;
705   }
706
707   /* create chroma subsampler */
708   if (videotestsrc->subsample)
709     gst_video_chroma_resample_free (videotestsrc->subsample);
710   videotestsrc->subsample = gst_video_chroma_resample_new (0,
711       info.chroma_site, 0, info.finfo->unpack_format, -info.finfo->w_sub[2],
712       -info.finfo->h_sub[2]);
713
714   for (i = 0; i < videotestsrc->n_lines; i++)
715     g_free (videotestsrc->lines[i]);
716   g_free (videotestsrc->lines);
717
718   if (videotestsrc->subsample != NULL) {
719     gst_video_chroma_resample_get_info (videotestsrc->subsample,
720         &n_lines, &offset);
721   } else {
722     n_lines = 1;
723     offset = 0;
724   }
725
726   videotestsrc->lines = g_malloc (sizeof (gpointer) * n_lines);
727   for (i = 0; i < n_lines; i++)
728     videotestsrc->lines[i] = g_malloc ((info.width + 16) * 8);
729   videotestsrc->n_lines = n_lines;
730   videotestsrc->offset = offset;
731
732   /* looks ok here */
733   videotestsrc->info = info;
734
735   GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
736       info.width, info.height, info.fps_n, info.fps_d);
737
738   g_free (videotestsrc->tmpline);
739   g_free (videotestsrc->tmpline2);
740   g_free (videotestsrc->tmpline_u8);
741   g_free (videotestsrc->tmpline_u16);
742   videotestsrc->tmpline_u8 = g_malloc (info.width + 8);
743   videotestsrc->tmpline = g_malloc ((info.width + 8) * 4);
744   videotestsrc->tmpline2 = g_malloc ((info.width + 8) * 4);
745   videotestsrc->tmpline_u16 = g_malloc ((info.width + 16) * 8);
746
747   videotestsrc->accum_rtime += videotestsrc->running_time;
748   videotestsrc->accum_frames += videotestsrc->n_frames;
749
750   videotestsrc->running_time = 0;
751   videotestsrc->n_frames = 0;
752
753   return TRUE;
754
755   /* ERRORS */
756 parse_failed:
757   {
758     GST_DEBUG_OBJECT (bsrc, "failed to parse caps");
759     return FALSE;
760   }
761 }
762
763 static gboolean
764 gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
765 {
766   gboolean res;
767   GstVideoTestSrc *src;
768
769   src = GST_VIDEO_TEST_SRC (bsrc);
770
771   switch (GST_QUERY_TYPE (query)) {
772     case GST_QUERY_CONVERT:
773     {
774       GstFormat src_fmt, dest_fmt;
775       gint64 src_val, dest_val;
776
777       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
778       res =
779           gst_video_info_convert (&src->info, src_fmt, src_val, dest_fmt,
780           &dest_val);
781       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
782       break;
783     }
784     case GST_QUERY_DURATION:{
785       if (bsrc->num_buffers != -1) {
786         GstFormat format;
787
788         gst_query_parse_duration (query, &format, NULL);
789         switch (format) {
790           case GST_FORMAT_TIME:{
791             gint64 dur = gst_util_uint64_scale_int_round (bsrc->num_buffers
792                 * GST_SECOND, src->info.fps_d, src->info.fps_n);
793             res = TRUE;
794             gst_query_set_duration (query, GST_FORMAT_TIME, dur);
795             goto done;
796           }
797           case GST_FORMAT_BYTES:
798             res = TRUE;
799             gst_query_set_duration (query, GST_FORMAT_BYTES,
800                 bsrc->num_buffers * src->info.size);
801             goto done;
802           default:
803             break;
804         }
805       }
806       /* fall through */
807     }
808     default:
809       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
810       break;
811   }
812 done:
813   return res;
814 }
815
816 static void
817 gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
818     GstClockTime * start, GstClockTime * end)
819 {
820   /* for live sources, sync on the timestamp of the buffer */
821   if (gst_base_src_is_live (basesrc)) {
822     GstClockTime timestamp = GST_BUFFER_DTS (buffer);
823
824     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
825       /* get duration to calculate end time */
826       GstClockTime duration = GST_BUFFER_DURATION (buffer);
827
828       if (GST_CLOCK_TIME_IS_VALID (duration)) {
829         *end = timestamp + duration;
830       }
831       *start = timestamp;
832     }
833   } else {
834     *start = -1;
835     *end = -1;
836   }
837 }
838
839 static gboolean
840 gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
841 {
842   GstClockTime position;
843   GstVideoTestSrc *src;
844
845   src = GST_VIDEO_TEST_SRC (bsrc);
846
847   segment->time = segment->start;
848   position = segment->position;
849   src->reverse = segment->rate < 0;
850
851   /* now move to the position indicated */
852   if (src->info.fps_n) {
853     src->n_frames = gst_util_uint64_scale (position,
854         src->info.fps_n, src->info.fps_d * GST_SECOND);
855   } else {
856     src->n_frames = 0;
857   }
858   src->accum_frames = 0;
859   src->accum_rtime = 0;
860   if (src->info.fps_n) {
861     src->running_time = gst_util_uint64_scale (src->n_frames,
862         src->info.fps_d * GST_SECOND, src->info.fps_n);
863   } else {
864     /* FIXME : Not sure what to set here */
865     src->running_time = 0;
866   }
867
868   g_assert (src->running_time <= position);
869
870   return TRUE;
871 }
872
873 static gboolean
874 gst_video_test_src_is_seekable (GstBaseSrc * psrc)
875 {
876   /* we're seekable... */
877   return TRUE;
878 }
879
880 static GstFlowReturn
881 gst_video_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
882 {
883   GstVideoTestSrc *src;
884   GstClockTime next_time;
885   GstVideoFrame frame;
886   gconstpointer pal;
887   gsize palsize;
888
889   src = GST_VIDEO_TEST_SRC (psrc);
890
891   if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&src->info) ==
892           GST_VIDEO_FORMAT_UNKNOWN))
893     goto not_negotiated;
894
895   /* 0 framerate and we are at the second frame, eos */
896   if (G_UNLIKELY (src->info.fps_n == 0 && src->n_frames == 1))
897     goto eos;
898
899   if (G_UNLIKELY (src->n_frames == -1)) {
900     /* EOS for reverse playback */
901     goto eos;
902   }
903
904   GST_LOG_OBJECT (src,
905       "creating buffer from pool for frame %d", (gint) src->n_frames);
906
907   if (!gst_video_frame_map (&frame, &src->info, buffer, GST_MAP_WRITE))
908     goto invalid_frame;
909
910   GST_BUFFER_DTS (buffer) =
911       src->accum_rtime + src->timestamp_offset + src->running_time;
912   GST_BUFFER_PTS (buffer) = GST_BUFFER_DTS (buffer);
913
914   gst_object_sync_values (GST_OBJECT (psrc), GST_BUFFER_DTS (buffer));
915
916   src->make_image (src, &frame);
917
918   if ((pal = gst_video_format_get_palette (GST_VIDEO_FRAME_FORMAT (&frame),
919               &palsize))) {
920     memcpy (GST_VIDEO_FRAME_PLANE_DATA (&frame, 1), pal, palsize);
921   }
922
923   gst_video_frame_unmap (&frame);
924
925   GST_DEBUG_OBJECT (src, "Timestamp: %" GST_TIME_FORMAT " = accumulated %"
926       GST_TIME_FORMAT " + offset: %"
927       GST_TIME_FORMAT " + running time: %" GST_TIME_FORMAT,
928       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), GST_TIME_ARGS (src->accum_rtime),
929       GST_TIME_ARGS (src->timestamp_offset), GST_TIME_ARGS (src->running_time));
930
931   GST_BUFFER_OFFSET (buffer) = src->accum_frames + src->n_frames;
932   if (src->reverse) {
933     src->n_frames--;
934   } else {
935     src->n_frames++;
936   }
937   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET (buffer) + 1;
938   if (src->info.fps_n) {
939     next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
940         src->info.fps_d, src->info.fps_n);
941     if (src->reverse) {
942       GST_BUFFER_DURATION (buffer) = src->running_time - next_time;
943     } else {
944       GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
945     }
946   } else {
947     next_time = src->timestamp_offset;
948     /* NONE means forever */
949     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
950   }
951
952   src->running_time = next_time;
953
954   return GST_FLOW_OK;
955
956 not_negotiated:
957   {
958     return GST_FLOW_NOT_NEGOTIATED;
959   }
960 eos:
961   {
962     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
963     return GST_FLOW_EOS;
964   }
965 invalid_frame:
966   {
967     GST_DEBUG_OBJECT (src, "invalid frame");
968     return GST_FLOW_OK;
969   }
970 }
971
972 static gboolean
973 gst_video_test_src_start (GstBaseSrc * basesrc)
974 {
975   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
976
977   src->running_time = 0;
978   src->n_frames = 0;
979   src->accum_frames = 0;
980   src->accum_rtime = 0;
981
982   gst_video_info_init (&src->info);
983
984   return TRUE;
985 }
986
987 static gboolean
988 gst_video_test_src_stop (GstBaseSrc * basesrc)
989 {
990   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
991   guint i;
992
993   g_free (src->tmpline);
994   src->tmpline = NULL;
995   g_free (src->tmpline2);
996   src->tmpline2 = NULL;
997   g_free (src->tmpline_u8);
998   src->tmpline_u8 = NULL;
999   g_free (src->tmpline_u16);
1000   src->tmpline_u16 = NULL;
1001   if (src->subsample)
1002     gst_video_chroma_resample_free (src->subsample);
1003   src->subsample = NULL;
1004
1005   for (i = 0; i < src->n_lines; i++)
1006     g_free (src->lines[i]);
1007   g_free (src->lines);
1008   src->n_lines = 0;
1009   src->lines = NULL;
1010
1011   return TRUE;
1012 }
1013
1014 static gboolean
1015 plugin_init (GstPlugin * plugin)
1016 {
1017   GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
1018       "Video Test Source");
1019
1020   return gst_element_register (plugin, "videotestsrc", GST_RANK_NONE,
1021       GST_TYPE_VIDEO_TEST_SRC);
1022 }
1023
1024 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1025     GST_VERSION_MINOR,
1026     videotestsrc,
1027     "Creates a test video stream",
1028     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)