videotestsrc: allow per feature registration
[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  * @title: videotestsrc
24  *
25  * The videotestsrc element is used to produce test video data in a wide variety
26  * of formats. The video test data produced can be controlled with the "pattern"
27  * property.
28  *
29  * By default the videotestsrc will generate data indefinitely, but if the
30  * #GstBaseSrc:num-buffers property is non-zero it will instead generate a
31  * fixed number of video frames and then send EOS.
32  *
33  * ## Example launch line
34  * |[
35  * gst-launch-1.0 -v videotestsrc pattern=snow ! video/x-raw,width=1280,height=720 ! autovideosink
36  * ]|
37  *  Shows random noise in a video window.
38  *
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 #include "gstvideotestsrc.h"
45 #include "gstvideotestsrcorc.h"
46 #include "videotestsrc.h"
47
48 #include <string.h>
49 #include <stdlib.h>
50
51 GST_DEBUG_CATEGORY_STATIC (video_test_src_debug);
52 #define GST_CAT_DEFAULT video_test_src_debug
53
54 #define DEFAULT_PATTERN            GST_VIDEO_TEST_SRC_SMPTE
55 #define DEFAULT_ANIMATION_MODE     GST_VIDEO_TEST_SRC_FRAMES
56 #define DEFAULT_MOTION_TYPE        GST_VIDEO_TEST_SRC_WAVY
57 #define DEFAULT_FLIP               FALSE
58 #define DEFAULT_TIMESTAMP_OFFSET   0
59 #define DEFAULT_IS_LIVE            FALSE
60 #define DEFAULT_COLOR_SPEC         GST_VIDEO_TEST_SRC_BT601
61 #define DEFAULT_FOREGROUND_COLOR   0xffffffff
62 #define DEFAULT_BACKGROUND_COLOR   0xff000000
63 #define DEFAULT_HORIZONTAL_SPEED   0
64
65 enum
66 {
67   PROP_0,
68   PROP_PATTERN,
69   PROP_TIMESTAMP_OFFSET,
70   PROP_IS_LIVE,
71   PROP_K0,
72   PROP_KX,
73   PROP_KY,
74   PROP_KT,
75   PROP_KXT,
76   PROP_KYT,
77   PROP_KXY,
78   PROP_KX2,
79   PROP_KY2,
80   PROP_KT2,
81   PROP_XOFFSET,
82   PROP_YOFFSET,
83   PROP_FOREGROUND_COLOR,
84   PROP_BACKGROUND_COLOR,
85   PROP_HORIZONTAL_SPEED,
86   PROP_ANIMATION_MODE,
87   PROP_MOTION_TYPE,
88   PROP_FLIP,
89   PROP_LAST
90 };
91
92
93 #define VTS_VIDEO_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) "," \
94   "multiview-mode = { mono, left, right }"                              \
95   ";" \
96   "video/x-bayer, format=(string) { bggr, rggb, grbg, gbrg }, "        \
97   "width = " GST_VIDEO_SIZE_RANGE ", "                                 \
98   "height = " GST_VIDEO_SIZE_RANGE ", "                                \
99   "framerate = " GST_VIDEO_FPS_RANGE ", "                              \
100   "multiview-mode = { mono, left, right }"
101
102
103 static GstStaticPadTemplate gst_video_test_src_template =
104 GST_STATIC_PAD_TEMPLATE ("src",
105     GST_PAD_SRC,
106     GST_PAD_ALWAYS,
107     GST_STATIC_CAPS (VTS_VIDEO_CAPS)
108     );
109
110 #define gst_video_test_src_parent_class parent_class
111 G_DEFINE_TYPE (GstVideoTestSrc, gst_video_test_src, GST_TYPE_PUSH_SRC);
112 GST_ELEMENT_REGISTER_DEFINE (videotestsrc, "videotestsrc",
113     GST_RANK_NONE, GST_TYPE_VIDEO_TEST_SRC);
114
115 static void gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
116     int pattern_type);
117 static void gst_video_test_src_set_property (GObject * object, guint prop_id,
118     const GValue * value, GParamSpec * pspec);
119 static void gst_video_test_src_get_property (GObject * object, guint prop_id,
120     GValue * value, GParamSpec * pspec);
121
122 static gboolean gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
123 static GstCaps *gst_video_test_src_src_fixate (GstBaseSrc * bsrc,
124     GstCaps * caps);
125
126 static gboolean gst_video_test_src_is_seekable (GstBaseSrc * psrc);
127 static gboolean gst_video_test_src_do_seek (GstBaseSrc * bsrc,
128     GstSegment * segment);
129 static gboolean gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query);
130
131 static void gst_video_test_src_get_times (GstBaseSrc * basesrc,
132     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
133 static gboolean gst_video_test_src_decide_allocation (GstBaseSrc * bsrc,
134     GstQuery * query);
135 static GstFlowReturn gst_video_test_src_fill (GstPushSrc * psrc,
136     GstBuffer * buffer);
137 static gboolean gst_video_test_src_start (GstBaseSrc * basesrc);
138 static gboolean gst_video_test_src_stop (GstBaseSrc * basesrc);
139
140 #define GST_TYPE_VIDEO_TEST_SRC_PATTERN (gst_video_test_src_pattern_get_type ())
141 static GType
142 gst_video_test_src_pattern_get_type (void)
143 {
144   static GType video_test_src_pattern_type = 0;
145   static const GEnumValue pattern_types[] = {
146     {GST_VIDEO_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
147     {GST_VIDEO_TEST_SRC_SNOW, "Random (television snow)", "snow"},
148     {GST_VIDEO_TEST_SRC_BLACK, "100% Black", "black"},
149     {GST_VIDEO_TEST_SRC_WHITE, "100% White", "white"},
150     {GST_VIDEO_TEST_SRC_RED, "Red", "red"},
151     {GST_VIDEO_TEST_SRC_GREEN, "Green", "green"},
152     {GST_VIDEO_TEST_SRC_BLUE, "Blue", "blue"},
153     {GST_VIDEO_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
154     {GST_VIDEO_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
155     {GST_VIDEO_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
156     {GST_VIDEO_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
157     {GST_VIDEO_TEST_SRC_CIRCULAR, "Circular", "circular"},
158     {GST_VIDEO_TEST_SRC_BLINK, "Blink", "blink"},
159     {GST_VIDEO_TEST_SRC_SMPTE75, "SMPTE 75% color bars", "smpte75"},
160     {GST_VIDEO_TEST_SRC_ZONE_PLATE, "Zone plate", "zone-plate"},
161     {GST_VIDEO_TEST_SRC_GAMUT, "Gamut checkers", "gamut"},
162     {GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE, "Chroma zone plate",
163         "chroma-zone-plate"},
164     {GST_VIDEO_TEST_SRC_SOLID, "Solid color", "solid-color"},
165     {GST_VIDEO_TEST_SRC_BALL, "Moving ball", "ball"},
166     {GST_VIDEO_TEST_SRC_SMPTE100, "SMPTE 100% color bars", "smpte100"},
167     {GST_VIDEO_TEST_SRC_BAR, "Bar", "bar"},
168     {GST_VIDEO_TEST_SRC_PINWHEEL, "Pinwheel", "pinwheel"},
169     {GST_VIDEO_TEST_SRC_SPOKES, "Spokes", "spokes"},
170     {GST_VIDEO_TEST_SRC_GRADIENT, "Gradient", "gradient"},
171     {GST_VIDEO_TEST_SRC_COLORS, "Colors", "colors"},
172     {0, NULL, NULL}
173   };
174
175   if (!video_test_src_pattern_type) {
176     video_test_src_pattern_type =
177         g_enum_register_static ("GstVideoTestSrcPattern", pattern_types);
178   }
179   return video_test_src_pattern_type;
180 }
181
182
183 /*"animation-mode", which can
184  * take the following values: frames (current behaviour that should stay the
185  * default), running time, wall clock time.
186  */
187
188 #define GST_TYPE_VIDEO_TEST_SRC_ANIMATION_MODE (gst_video_test_src_animation_mode_get_type ())
189 static GType
190 gst_video_test_src_animation_mode_get_type (void)
191 {
192   static GType video_test_src_animation_mode = 0;
193   static const GEnumValue animation_modes[] = {
194
195     {GST_VIDEO_TEST_SRC_FRAMES, "frame count", "frames"},
196     {GST_VIDEO_TEST_SRC_WALL_TIME, "wall clock time", "wall-time"},
197     {GST_VIDEO_TEST_SRC_RUNNING_TIME, "running time", "running-time"},
198     {0, NULL, NULL}
199   };
200
201   if (!video_test_src_animation_mode) {
202     video_test_src_animation_mode =
203         g_enum_register_static ("GstVideoTestSrcAnimationMode",
204         animation_modes);
205   }
206   return video_test_src_animation_mode;
207 }
208
209
210 #define GST_TYPE_VIDEO_TEST_SRC_MOTION_TYPE (gst_video_test_src_motion_type_get_type ())
211 static GType
212 gst_video_test_src_motion_type_get_type (void)
213 {
214   static GType video_test_src_motion_type = 0;
215   static const GEnumValue motion_types[] = {
216     {GST_VIDEO_TEST_SRC_WAVY, "Ball waves back and forth, up and down",
217         "wavy"},
218     {GST_VIDEO_TEST_SRC_SWEEP, "1 revolution per second", "sweep"},
219     {GST_VIDEO_TEST_SRC_HSWEEP, "1/2 revolution per second, then reset to top",
220         "hsweep"},
221     {0, NULL, NULL}
222   };
223
224   if (!video_test_src_motion_type) {
225     video_test_src_motion_type =
226         g_enum_register_static ("GstVideoTestSrcMotionType", motion_types);
227   }
228   return video_test_src_motion_type;
229 }
230
231
232 static void
233 gst_video_test_src_class_init (GstVideoTestSrcClass * klass)
234 {
235   GObjectClass *gobject_class;
236   GstElementClass *gstelement_class;
237   GstBaseSrcClass *gstbasesrc_class;
238   GstPushSrcClass *gstpushsrc_class;
239
240   gobject_class = (GObjectClass *) klass;
241   gstelement_class = (GstElementClass *) klass;
242   gstbasesrc_class = (GstBaseSrcClass *) klass;
243   gstpushsrc_class = (GstPushSrcClass *) klass;
244
245   gobject_class->set_property = gst_video_test_src_set_property;
246   gobject_class->get_property = gst_video_test_src_get_property;
247
248   g_object_class_install_property (gobject_class, PROP_PATTERN,
249       g_param_spec_enum ("pattern", "Pattern",
250           "Type of test pattern to generate", GST_TYPE_VIDEO_TEST_SRC_PATTERN,
251           DEFAULT_PATTERN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252
253   g_object_class_install_property (gobject_class, PROP_ANIMATION_MODE,
254       g_param_spec_enum ("animation-mode", "Animation mode",
255           "For pattern=ball, which counter defines the position of the ball.",
256           GST_TYPE_VIDEO_TEST_SRC_ANIMATION_MODE, DEFAULT_ANIMATION_MODE,
257           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258
259   g_object_class_install_property (gobject_class, PROP_MOTION_TYPE,
260       g_param_spec_enum ("motion", "Motion",
261           "For pattern=ball, what motion the ball does",
262           GST_TYPE_VIDEO_TEST_SRC_MOTION_TYPE, DEFAULT_MOTION_TYPE,
263           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264
265   g_object_class_install_property (gobject_class, PROP_FLIP,
266       g_param_spec_boolean ("flip", "Flip",
267           "For pattern=ball, invert colors every second.",
268           DEFAULT_FLIP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269
270   g_object_class_install_property (gobject_class, PROP_TIMESTAMP_OFFSET,
271       g_param_spec_int64 ("timestamp-offset", "Timestamp offset",
272           "An offset added to timestamps set on buffers (in ns)", 0,
273           (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
274           0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
276       g_param_spec_boolean ("is-live", "Is Live",
277           "Whether to act as a live source", DEFAULT_IS_LIVE,
278           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279   g_object_class_install_property (gobject_class, PROP_K0,
280       g_param_spec_int ("k0", "Zoneplate zero order phase",
281           "Zoneplate zero order phase, for generating plain fields or phase offsets",
282           G_MININT32, G_MAXINT32, 0,
283           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284   g_object_class_install_property (gobject_class, PROP_KX,
285       g_param_spec_int ("kx", "Zoneplate 1st order x phase",
286           "Zoneplate 1st order x phase, for generating constant horizontal frequencies",
287           G_MININT32, G_MAXINT32, 0,
288           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289   g_object_class_install_property (gobject_class, PROP_KY,
290       g_param_spec_int ("ky", "Zoneplate 1st order y phase",
291           "Zoneplate 1st order y phase, for generating content vertical frequencies",
292           G_MININT32, G_MAXINT32, 0,
293           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294   g_object_class_install_property (gobject_class, PROP_KT,
295       g_param_spec_int ("kt", "Zoneplate 1st order t phase",
296           "Zoneplate 1st order t phase, for generating phase rotation as a function of time",
297           G_MININT32, G_MAXINT32, 0,
298           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299   g_object_class_install_property (gobject_class, PROP_KXT,
300       g_param_spec_int ("kxt", "Zoneplate x*t product phase",
301           "Zoneplate x*t product phase, normalised to kxy/256 cycles per vertical pixel at width/2 from origin",
302           G_MININT32, G_MAXINT32, 0,
303           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304   g_object_class_install_property (gobject_class, PROP_KYT,
305       g_param_spec_int ("kyt", "Zoneplate y*t product phase",
306           "Zoneplate y*t product phase", G_MININT32, G_MAXINT32, 0,
307           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308   g_object_class_install_property (gobject_class, PROP_KXY,
309       g_param_spec_int ("kxy", "Zoneplate x*y product phase",
310           "Zoneplate x*y product phase", G_MININT32, G_MAXINT32, 0,
311           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312   g_object_class_install_property (gobject_class, PROP_KX2,
313       g_param_spec_int ("kx2", "Zoneplate 2nd order x phase",
314           "Zoneplate 2nd order x phase, normalised to kx2/256 cycles per horizontal pixel at width/2 from origin",
315           G_MININT32, G_MAXINT32, 0,
316           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317   g_object_class_install_property (gobject_class, PROP_KY2,
318       g_param_spec_int ("ky2", "Zoneplate 2nd order y phase",
319           "Zoneplate 2nd order y phase, normailsed to ky2/256 cycles per vertical pixel at height/2 from origin",
320           G_MININT32, G_MAXINT32, 0,
321           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322   g_object_class_install_property (gobject_class, PROP_KT2,
323       g_param_spec_int ("kt2", "Zoneplate 2nd order t phase",
324           "Zoneplate 2nd order t phase, t*t/256 cycles per picture", G_MININT32,
325           G_MAXINT32, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326   g_object_class_install_property (gobject_class, PROP_XOFFSET,
327       g_param_spec_int ("xoffset", "Zoneplate 2nd order products x offset",
328           "Zoneplate 2nd order products x offset", G_MININT32, G_MAXINT32, 0,
329           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330   g_object_class_install_property (gobject_class, PROP_YOFFSET,
331       g_param_spec_int ("yoffset", "Zoneplate 2nd order products y offset",
332           "Zoneplate 2nd order products y offset", G_MININT32, G_MAXINT32, 0,
333           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334   /**
335    * GstVideoTestSrc:foreground-color
336    *
337    * Color to use for solid-color pattern and foreground color of other
338    * patterns.  Default is white (0xffffffff).
339    */
340   g_object_class_install_property (gobject_class, PROP_FOREGROUND_COLOR,
341       g_param_spec_uint ("foreground-color", "Foreground Color",
342           "Foreground color to use (big-endian ARGB)", 0, G_MAXUINT32,
343           DEFAULT_FOREGROUND_COLOR,
344           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
345   /**
346    * GstVideoTestSrc:background-color
347    *
348    * Color to use for background color of some patterns.  Default is
349    * black (0xff000000).
350    */
351   g_object_class_install_property (gobject_class, PROP_BACKGROUND_COLOR,
352       g_param_spec_uint ("background-color", "Background Color",
353           "Background color to use (big-endian ARGB)", 0, G_MAXUINT32,
354           DEFAULT_BACKGROUND_COLOR,
355           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
356
357   g_object_class_install_property (gobject_class, PROP_HORIZONTAL_SPEED,
358       g_param_spec_int ("horizontal-speed", "Horizontal Speed",
359           "Scroll image number of pixels per frame (positive is scroll to the left)",
360           G_MININT32, G_MAXINT32, DEFAULT_HORIZONTAL_SPEED,
361           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362
363   gst_element_class_set_static_metadata (gstelement_class,
364       "Video test source", "Source/Video",
365       "Creates a test video stream", "David A. Schleef <ds@schleef.org>");
366
367   gst_element_class_add_static_pad_template (gstelement_class,
368       &gst_video_test_src_template);
369
370   gstbasesrc_class->set_caps = gst_video_test_src_setcaps;
371   gstbasesrc_class->fixate = gst_video_test_src_src_fixate;
372   gstbasesrc_class->is_seekable = gst_video_test_src_is_seekable;
373   gstbasesrc_class->do_seek = gst_video_test_src_do_seek;
374   gstbasesrc_class->query = gst_video_test_src_query;
375   gstbasesrc_class->get_times = gst_video_test_src_get_times;
376   gstbasesrc_class->start = gst_video_test_src_start;
377   gstbasesrc_class->stop = gst_video_test_src_stop;
378   gstbasesrc_class->decide_allocation = gst_video_test_src_decide_allocation;
379
380   gstpushsrc_class->fill = gst_video_test_src_fill;
381
382   gst_type_mark_as_plugin_api (GST_TYPE_VIDEO_TEST_SRC_ANIMATION_MODE, 0);
383   gst_type_mark_as_plugin_api (GST_TYPE_VIDEO_TEST_SRC_MOTION_TYPE, 0);
384   gst_type_mark_as_plugin_api (GST_TYPE_VIDEO_TEST_SRC_PATTERN, 0);
385 }
386
387 static void
388 gst_video_test_src_init (GstVideoTestSrc * src)
389 {
390   gst_video_test_src_set_pattern (src, DEFAULT_PATTERN);
391
392   src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
393   src->foreground_color = DEFAULT_FOREGROUND_COLOR;
394   src->background_color = DEFAULT_BACKGROUND_COLOR;
395   src->horizontal_speed = DEFAULT_HORIZONTAL_SPEED;
396   src->random_state = 0;
397
398   /* we operate in time */
399   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
400   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
401
402   src->animation_mode = DEFAULT_ANIMATION_MODE;
403   src->motion_type = DEFAULT_MOTION_TYPE;
404   src->flip = DEFAULT_FLIP;
405
406 }
407
408 static GstCaps *
409 gst_video_test_src_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
410 {
411   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (bsrc);
412   GstStructure *structure;
413
414   /* Check if foreground color has alpha, if it is the case,
415    * force color format with an alpha channel downstream */
416   if (src->foreground_color >> 24 != 255) {
417     guint i;
418     GstCaps *alpha_only_caps = gst_caps_new_empty ();
419
420     for (i = 0; i < gst_caps_get_size (caps); i++) {
421       const GstVideoFormatInfo *info;
422       const GValue *formats =
423           gst_structure_get_value (gst_caps_get_structure (caps, i),
424           "format");
425
426       if (GST_VALUE_HOLDS_LIST (formats)) {
427         GValue possible_formats = { 0, };
428         guint list_size = gst_value_list_get_size (formats);
429         guint index;
430
431         g_value_init (&possible_formats, GST_TYPE_LIST);
432         for (index = 0; index < list_size; index++) {
433           const GValue *list_item = gst_value_list_get_value (formats, index);
434           info =
435               gst_video_format_get_info (gst_video_format_from_string
436               (g_value_get_string (list_item)));
437           if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (info)) {
438             GValue tmp = { 0, };
439
440             gst_value_init_and_copy (&tmp, list_item);
441             gst_value_list_append_value (&possible_formats, &tmp);
442           }
443         }
444
445         if (gst_value_list_get_size (&possible_formats)) {
446           GstStructure *astruct =
447               gst_structure_copy (gst_caps_get_structure (caps, i));
448
449           gst_structure_set_value (astruct, "format", &possible_formats);
450           gst_caps_append_structure (alpha_only_caps, astruct);
451         }
452
453       } else if (G_VALUE_HOLDS_STRING (formats)) {
454         info =
455             gst_video_format_get_info (gst_video_format_from_string
456             (g_value_get_string (formats)));
457
458         if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (info)) {
459           gst_caps_append_structure (alpha_only_caps,
460               gst_structure_copy (gst_caps_get_structure (caps, i)));
461         }
462       } else {
463         g_assert_not_reached ();
464         GST_WARNING ("Unexpected type for video 'format' field: %s",
465             G_VALUE_TYPE_NAME (formats));
466       }
467     }
468
469     if (gst_caps_is_empty (alpha_only_caps)) {
470       GST_WARNING_OBJECT (src,
471           "Foreground color contains alpha, but downstream can't support alpha.");
472     } else {
473       gst_caps_replace (&caps, alpha_only_caps);
474     }
475   }
476
477   caps = gst_caps_make_writable (caps);
478   structure = gst_caps_get_structure (caps, 0);
479
480   gst_structure_fixate_field_nearest_int (structure, "width", 320);
481   gst_structure_fixate_field_nearest_int (structure, "height", 240);
482
483   if (gst_structure_has_field (structure, "framerate"))
484     gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
485   else
486     gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, 30, 1, NULL);
487
488   if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
489     gst_structure_fixate_field_nearest_fraction (structure,
490         "pixel-aspect-ratio", 1, 1);
491   else
492     gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
493         NULL);
494
495   if (gst_structure_has_field (structure, "colorimetry"))
496     gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
497   if (gst_structure_has_field (structure, "chroma-site"))
498     gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
499
500   if (gst_structure_has_field (structure, "interlace-mode"))
501     gst_structure_fixate_field_string (structure, "interlace-mode",
502         "progressive");
503   else
504     gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
505         "progressive", NULL);
506
507   if (gst_structure_has_field (structure, "multiview-mode"))
508     gst_structure_fixate_field_string (structure, "multiview-mode",
509         gst_video_multiview_mode_to_caps_string
510         (GST_VIDEO_MULTIVIEW_MODE_MONO));
511   else
512     gst_structure_set (structure, "multiview-mode", G_TYPE_STRING,
513         gst_video_multiview_mode_to_caps_string (GST_VIDEO_MULTIVIEW_MODE_MONO),
514         NULL);
515
516   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
517
518   return caps;
519 }
520
521 static void
522 gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
523     int pattern_type)
524 {
525   videotestsrc->pattern_type = pattern_type;
526
527   GST_DEBUG_OBJECT (videotestsrc, "setting pattern to %d", pattern_type);
528
529   switch (pattern_type) {
530     case GST_VIDEO_TEST_SRC_SMPTE:
531       videotestsrc->make_image = gst_video_test_src_smpte;
532       break;
533     case GST_VIDEO_TEST_SRC_SNOW:
534       videotestsrc->make_image = gst_video_test_src_snow;
535       break;
536     case GST_VIDEO_TEST_SRC_BLACK:
537       videotestsrc->make_image = gst_video_test_src_black;
538       break;
539     case GST_VIDEO_TEST_SRC_WHITE:
540       videotestsrc->make_image = gst_video_test_src_white;
541       break;
542     case GST_VIDEO_TEST_SRC_RED:
543       videotestsrc->make_image = gst_video_test_src_red;
544       break;
545     case GST_VIDEO_TEST_SRC_GREEN:
546       videotestsrc->make_image = gst_video_test_src_green;
547       break;
548     case GST_VIDEO_TEST_SRC_BLUE:
549       videotestsrc->make_image = gst_video_test_src_blue;
550       break;
551     case GST_VIDEO_TEST_SRC_CHECKERS1:
552       videotestsrc->make_image = gst_video_test_src_checkers1;
553       break;
554     case GST_VIDEO_TEST_SRC_CHECKERS2:
555       videotestsrc->make_image = gst_video_test_src_checkers2;
556       break;
557     case GST_VIDEO_TEST_SRC_CHECKERS4:
558       videotestsrc->make_image = gst_video_test_src_checkers4;
559       break;
560     case GST_VIDEO_TEST_SRC_CHECKERS8:
561       videotestsrc->make_image = gst_video_test_src_checkers8;
562       break;
563     case GST_VIDEO_TEST_SRC_CIRCULAR:
564       videotestsrc->make_image = gst_video_test_src_circular;
565       break;
566     case GST_VIDEO_TEST_SRC_BLINK:
567       videotestsrc->make_image = gst_video_test_src_blink;
568       break;
569     case GST_VIDEO_TEST_SRC_SMPTE75:
570       videotestsrc->make_image = gst_video_test_src_smpte75;
571       break;
572     case GST_VIDEO_TEST_SRC_ZONE_PLATE:
573       videotestsrc->make_image = gst_video_test_src_zoneplate;
574       break;
575     case GST_VIDEO_TEST_SRC_GAMUT:
576       videotestsrc->make_image = gst_video_test_src_gamut;
577       break;
578     case GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE:
579       videotestsrc->make_image = gst_video_test_src_chromazoneplate;
580       break;
581     case GST_VIDEO_TEST_SRC_SOLID:
582       videotestsrc->make_image = gst_video_test_src_solid;
583       break;
584     case GST_VIDEO_TEST_SRC_BALL:
585       videotestsrc->make_image = gst_video_test_src_ball;
586       break;
587     case GST_VIDEO_TEST_SRC_SMPTE100:
588       videotestsrc->make_image = gst_video_test_src_smpte100;
589       break;
590     case GST_VIDEO_TEST_SRC_BAR:
591       videotestsrc->make_image = gst_video_test_src_bar;
592       break;
593     case GST_VIDEO_TEST_SRC_PINWHEEL:
594       videotestsrc->make_image = gst_video_test_src_pinwheel;
595       break;
596     case GST_VIDEO_TEST_SRC_SPOKES:
597       videotestsrc->make_image = gst_video_test_src_spokes;
598       break;
599     case GST_VIDEO_TEST_SRC_GRADIENT:
600       videotestsrc->make_image = gst_video_test_src_gradient;
601       break;
602     case GST_VIDEO_TEST_SRC_COLORS:
603       videotestsrc->make_image = gst_video_test_src_colors;
604       break;
605     default:
606       g_assert_not_reached ();
607   }
608 }
609
610 static void
611 gst_video_test_src_set_property (GObject * object, guint prop_id,
612     const GValue * value, GParamSpec * pspec)
613 {
614   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
615
616   switch (prop_id) {
617     case PROP_PATTERN:
618       gst_video_test_src_set_pattern (src, g_value_get_enum (value));
619       break;
620     case PROP_TIMESTAMP_OFFSET:
621       src->timestamp_offset = g_value_get_int64 (value);
622       break;
623     case PROP_IS_LIVE:
624       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
625       break;
626     case PROP_K0:
627       src->k0 = g_value_get_int (value);
628       break;
629     case PROP_KX:
630       src->kx = g_value_get_int (value);
631       break;
632     case PROP_KY:
633       src->ky = g_value_get_int (value);
634       break;
635     case PROP_KT:
636       src->kt = g_value_get_int (value);
637       break;
638     case PROP_KXT:
639       src->kxt = g_value_get_int (value);
640       break;
641     case PROP_KYT:
642       src->kyt = g_value_get_int (value);
643       break;
644     case PROP_KXY:
645       src->kxy = g_value_get_int (value);
646       break;
647     case PROP_KX2:
648       src->kx2 = g_value_get_int (value);
649       break;
650     case PROP_KY2:
651       src->ky2 = g_value_get_int (value);
652       break;
653     case PROP_KT2:
654       src->kt2 = g_value_get_int (value);
655       break;
656     case PROP_XOFFSET:
657       src->xoffset = g_value_get_int (value);
658       break;
659     case PROP_YOFFSET:
660       src->yoffset = g_value_get_int (value);
661       break;
662     case PROP_FOREGROUND_COLOR:
663       src->foreground_color = g_value_get_uint (value);
664       break;
665     case PROP_BACKGROUND_COLOR:
666       src->background_color = g_value_get_uint (value);
667       break;
668     case PROP_HORIZONTAL_SPEED:
669       src->horizontal_speed = g_value_get_int (value);
670       break;
671     case PROP_ANIMATION_MODE:
672       src->animation_mode = g_value_get_enum (value);
673       break;
674     case PROP_MOTION_TYPE:
675       src->motion_type = g_value_get_enum (value);
676       break;
677     case PROP_FLIP:
678       src->flip = g_value_get_boolean (value);
679       break;
680     default:
681       break;
682   }
683 }
684
685 static void
686 gst_video_test_src_get_property (GObject * object, guint prop_id,
687     GValue * value, GParamSpec * pspec)
688 {
689   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
690
691   switch (prop_id) {
692     case PROP_PATTERN:
693       g_value_set_enum (value, src->pattern_type);
694       break;
695     case PROP_TIMESTAMP_OFFSET:
696       g_value_set_int64 (value, src->timestamp_offset);
697       break;
698     case PROP_IS_LIVE:
699       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
700       break;
701     case PROP_K0:
702       g_value_set_int (value, src->k0);
703       break;
704     case PROP_KX:
705       g_value_set_int (value, src->kx);
706       break;
707     case PROP_KY:
708       g_value_set_int (value, src->ky);
709       break;
710     case PROP_KT:
711       g_value_set_int (value, src->kt);
712       break;
713     case PROP_KXT:
714       g_value_set_int (value, src->kxt);
715       break;
716     case PROP_KYT:
717       g_value_set_int (value, src->kyt);
718       break;
719     case PROP_KXY:
720       g_value_set_int (value, src->kxy);
721       break;
722     case PROP_KX2:
723       g_value_set_int (value, src->kx2);
724       break;
725     case PROP_KY2:
726       g_value_set_int (value, src->ky2);
727       break;
728     case PROP_KT2:
729       g_value_set_int (value, src->kt2);
730       break;
731     case PROP_XOFFSET:
732       g_value_set_int (value, src->xoffset);
733       break;
734     case PROP_YOFFSET:
735       g_value_set_int (value, src->yoffset);
736       break;
737     case PROP_FOREGROUND_COLOR:
738       g_value_set_uint (value, src->foreground_color);
739       break;
740     case PROP_BACKGROUND_COLOR:
741       g_value_set_uint (value, src->background_color);
742       break;
743     case PROP_HORIZONTAL_SPEED:
744       g_value_set_int (value, src->horizontal_speed);
745       break;
746     case PROP_ANIMATION_MODE:
747       g_value_set_enum (value, src->animation_mode);
748       break;
749     case PROP_MOTION_TYPE:
750       g_value_set_enum (value, src->motion_type);
751       break;
752     case PROP_FLIP:
753       g_value_set_boolean (value, src->flip);
754       break;
755     default:
756       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
757       break;
758   }
759 }
760
761 static gboolean
762 gst_video_test_src_parse_caps (const GstCaps * caps,
763     gint * width, gint * height, gint * fps_n, gint * fps_d,
764     GstVideoColorimetry * colorimetry, gint * x_inv, gint * y_inv)
765 {
766   const GstStructure *structure;
767   GstPadLinkReturn ret;
768   const GValue *framerate;
769   const gchar *str;
770
771   GST_DEBUG ("parsing caps");
772
773   structure = gst_caps_get_structure (caps, 0);
774
775   ret = gst_structure_get_int (structure, "width", width);
776   ret &= gst_structure_get_int (structure, "height", height);
777   framerate = gst_structure_get_value (structure, "framerate");
778
779   if (framerate) {
780     *fps_n = gst_value_get_fraction_numerator (framerate);
781     *fps_d = gst_value_get_fraction_denominator (framerate);
782   } else
783     goto no_framerate;
784
785   if ((str = gst_structure_get_string (structure, "colorimetry")))
786     gst_video_colorimetry_from_string (colorimetry, str);
787
788   if ((str = gst_structure_get_string (structure, "format"))) {
789     if (g_str_equal (str, "bggr")) {
790       *x_inv = *y_inv = 0;
791     } else if (g_str_equal (str, "rggb")) {
792       *x_inv = *y_inv = 1;
793     } else if (g_str_equal (str, "grbg")) {
794       *x_inv = 0;
795       *y_inv = 1;
796     } else if (g_str_equal (str, "gbrg")) {
797       *x_inv = 1;
798       *y_inv = 0;
799     } else
800       goto invalid_format;
801   }
802   return ret;
803
804   /* ERRORS */
805 no_framerate:
806   {
807     GST_DEBUG ("videotestsrc no framerate given");
808     return FALSE;
809   }
810 invalid_format:
811   {
812     GST_DEBUG ("videotestsrc invalid bayer format given");
813     return FALSE;
814   }
815 }
816
817 static gboolean
818 gst_video_test_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
819 {
820   GstVideoTestSrc *videotestsrc;
821   GstBufferPool *pool;
822   gboolean update;
823   guint size, min, max;
824   GstStructure *config;
825   GstCaps *caps = NULL;
826
827   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
828
829   if (gst_query_get_n_allocation_pools (query) > 0) {
830     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
831
832     /* adjust size */
833     size = MAX (size, videotestsrc->info.size);
834     update = TRUE;
835   } else {
836     pool = NULL;
837     size = videotestsrc->info.size;
838     min = max = 0;
839     update = FALSE;
840   }
841
842   /* no downstream pool, make our own */
843   if (pool == NULL) {
844     if (videotestsrc->bayer)
845       pool = gst_buffer_pool_new ();
846     else
847       pool = gst_video_buffer_pool_new ();
848   }
849
850   config = gst_buffer_pool_get_config (pool);
851
852   gst_query_parse_allocation (query, &caps, NULL);
853   if (caps)
854     gst_buffer_pool_config_set_params (config, caps, size, min, max);
855
856   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
857     gst_buffer_pool_config_add_option (config,
858         GST_BUFFER_POOL_OPTION_VIDEO_META);
859   }
860   gst_buffer_pool_set_config (pool, config);
861
862   if (update)
863     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
864   else
865     gst_query_add_allocation_pool (query, pool, size, min, max);
866
867   if (pool)
868     gst_object_unref (pool);
869
870   return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
871 }
872
873 static gboolean
874 gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
875 {
876   const GstStructure *structure;
877   GstVideoTestSrc *videotestsrc;
878   GstVideoInfo info;
879   guint i;
880   guint n_lines;
881   gint offset;
882
883   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
884
885   structure = gst_caps_get_structure (caps, 0);
886
887   GST_OBJECT_LOCK (videotestsrc);
888
889   if (gst_structure_has_name (structure, "video/x-raw")) {
890     /* we can use the parsing code */
891     if (!gst_video_info_from_caps (&info, caps))
892       goto parse_failed;
893
894   } else if (gst_structure_has_name (structure, "video/x-bayer")) {
895     gint x_inv = 0, y_inv = 0;
896
897     gst_video_info_init (&info);
898
899     info.finfo = gst_video_format_get_info (GST_VIDEO_FORMAT_GRAY8);
900
901     if (!gst_video_test_src_parse_caps (caps, &info.width, &info.height,
902             &info.fps_n, &info.fps_d, &info.colorimetry, &x_inv, &y_inv))
903       goto parse_failed;
904
905     info.size = GST_ROUND_UP_4 (info.width) * info.height;
906     info.stride[0] = GST_ROUND_UP_4 (info.width);
907
908     videotestsrc->bayer = TRUE;
909     videotestsrc->x_invert = x_inv;
910     videotestsrc->y_invert = y_inv;
911   } else {
912     goto unsupported_caps;
913   }
914
915   /* create chroma subsampler */
916   if (videotestsrc->subsample)
917     gst_video_chroma_resample_free (videotestsrc->subsample);
918   videotestsrc->subsample = gst_video_chroma_resample_new (0,
919       info.chroma_site, 0, info.finfo->unpack_format, -info.finfo->w_sub[2],
920       -info.finfo->h_sub[2]);
921
922   for (i = 0; i < videotestsrc->n_lines; i++)
923     g_free (videotestsrc->lines[i]);
924   g_free (videotestsrc->lines);
925
926   if (videotestsrc->subsample != NULL) {
927     gst_video_chroma_resample_get_info (videotestsrc->subsample,
928         &n_lines, &offset);
929   } else {
930     n_lines = 1;
931     offset = 0;
932   }
933
934   videotestsrc->lines = g_malloc (sizeof (gpointer) * n_lines);
935   for (i = 0; i < n_lines; i++)
936     videotestsrc->lines[i] = g_malloc ((info.width + 16) * 8);
937   videotestsrc->n_lines = n_lines;
938   videotestsrc->offset = offset;
939
940   /* looks ok here */
941   videotestsrc->info = info;
942
943   GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
944       info.width, info.height, info.fps_n, info.fps_d);
945
946   g_free (videotestsrc->tmpline);
947   g_free (videotestsrc->tmpline2);
948   g_free (videotestsrc->tmpline_u8);
949   g_free (videotestsrc->tmpline_u16);
950   videotestsrc->tmpline_u8 = g_malloc (info.width + 8);
951   videotestsrc->tmpline = g_malloc ((info.width + 8) * 4);
952   videotestsrc->tmpline2 = g_malloc ((info.width + 8) * 4);
953   videotestsrc->tmpline_u16 = g_malloc ((info.width + 16) * 8);
954
955   videotestsrc->accum_rtime += videotestsrc->running_time;
956   videotestsrc->accum_frames += videotestsrc->n_frames;
957
958   videotestsrc->running_time = 0;
959   videotestsrc->n_frames = 0;
960
961   GST_OBJECT_UNLOCK (videotestsrc);
962
963   return TRUE;
964
965   /* ERRORS */
966 parse_failed:
967   {
968     GST_OBJECT_UNLOCK (videotestsrc);
969     GST_DEBUG_OBJECT (bsrc, "failed to parse caps");
970     return FALSE;
971   }
972 unsupported_caps:
973   {
974     GST_OBJECT_UNLOCK (videotestsrc);
975     GST_DEBUG_OBJECT (bsrc, "unsupported caps: %" GST_PTR_FORMAT, caps);
976     return FALSE;
977   }
978 }
979
980 static gboolean
981 gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
982 {
983   gboolean res = FALSE;
984   GstVideoTestSrc *src;
985
986   src = GST_VIDEO_TEST_SRC (bsrc);
987
988   switch (GST_QUERY_TYPE (query)) {
989     case GST_QUERY_CONVERT:
990     {
991       GstFormat src_fmt, dest_fmt;
992       gint64 src_val, dest_val;
993
994       GST_OBJECT_LOCK (src);
995       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
996       res =
997           gst_video_info_convert (&src->info, src_fmt, src_val, dest_fmt,
998           &dest_val);
999       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1000       GST_OBJECT_UNLOCK (src);
1001       break;
1002     }
1003     case GST_QUERY_LATENCY:
1004     {
1005       GST_OBJECT_LOCK (src);
1006       if (src->info.fps_n > 0) {
1007         GstClockTime latency;
1008
1009         latency =
1010             gst_util_uint64_scale (GST_SECOND, src->info.fps_d,
1011             src->info.fps_n);
1012         GST_OBJECT_UNLOCK (src);
1013         gst_query_set_latency (query,
1014             gst_base_src_is_live (GST_BASE_SRC_CAST (src)), latency,
1015             GST_CLOCK_TIME_NONE);
1016         GST_DEBUG_OBJECT (src, "Reporting latency of %" GST_TIME_FORMAT,
1017             GST_TIME_ARGS (latency));
1018         res = TRUE;
1019       } else {
1020         GST_OBJECT_UNLOCK (src);
1021       }
1022       break;
1023     }
1024     case GST_QUERY_DURATION:{
1025       if (bsrc->num_buffers != -1) {
1026         GstFormat format;
1027
1028         gst_query_parse_duration (query, &format, NULL);
1029         switch (format) {
1030           case GST_FORMAT_TIME:{
1031             gint64 dur;
1032
1033             GST_OBJECT_LOCK (src);
1034             if (src->info.fps_n) {
1035               dur = gst_util_uint64_scale_int_round (bsrc->num_buffers
1036                   * GST_SECOND, src->info.fps_d, src->info.fps_n);
1037               res = TRUE;
1038               gst_query_set_duration (query, GST_FORMAT_TIME, dur);
1039             }
1040             GST_OBJECT_UNLOCK (src);
1041             goto done;
1042           }
1043           case GST_FORMAT_BYTES:
1044             GST_OBJECT_LOCK (src);
1045             res = TRUE;
1046             gst_query_set_duration (query, GST_FORMAT_BYTES,
1047                 bsrc->num_buffers * src->info.size);
1048             GST_OBJECT_UNLOCK (src);
1049             goto done;
1050           default:
1051             break;
1052         }
1053       }
1054       /* fall through */
1055     }
1056     default:
1057       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1058       break;
1059   }
1060 done:
1061   return res;
1062 }
1063
1064 static void
1065 gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
1066     GstClockTime * start, GstClockTime * end)
1067 {
1068   /* for live sources, sync on the timestamp of the buffer */
1069   if (gst_base_src_is_live (basesrc)) {
1070     GstClockTime timestamp = GST_BUFFER_PTS (buffer);
1071
1072     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1073       /* get duration to calculate end time */
1074       GstClockTime duration = GST_BUFFER_DURATION (buffer);
1075
1076       if (GST_CLOCK_TIME_IS_VALID (duration)) {
1077         *end = timestamp + duration;
1078       }
1079       *start = timestamp;
1080     }
1081   } else {
1082     *start = -1;
1083     *end = -1;
1084   }
1085 }
1086
1087 static gboolean
1088 gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1089 {
1090   GstClockTime position;
1091   GstVideoTestSrc *src;
1092
1093   src = GST_VIDEO_TEST_SRC (bsrc);
1094
1095   segment->time = segment->start;
1096   position = segment->position;
1097   src->reverse = segment->rate < 0;
1098
1099   /* now move to the position indicated */
1100   if (src->info.fps_n) {
1101     src->n_frames = gst_util_uint64_scale (position,
1102         src->info.fps_n, src->info.fps_d * GST_SECOND);
1103   } else {
1104     src->n_frames = 0;
1105   }
1106   src->accum_frames = 0;
1107   src->accum_rtime = 0;
1108   if (src->info.fps_n) {
1109     src->running_time = gst_util_uint64_scale (src->n_frames,
1110         src->info.fps_d * GST_SECOND, src->info.fps_n);
1111   } else {
1112     /* FIXME : Not sure what to set here */
1113     src->running_time = 0;
1114   }
1115
1116   g_assert (src->running_time <= position);
1117
1118   return TRUE;
1119 }
1120
1121 static gboolean
1122 gst_video_test_src_is_seekable (GstBaseSrc * psrc)
1123 {
1124   /* we're seekable... */
1125   return TRUE;
1126 }
1127
1128 static GstFlowReturn
1129 gst_video_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
1130 {
1131   GstVideoTestSrc *src;
1132   GstClockTime next_time;
1133   GstVideoFrame frame;
1134   gconstpointer pal;
1135   gsize palsize;
1136
1137   src = GST_VIDEO_TEST_SRC (psrc);
1138
1139   if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&src->info) ==
1140           GST_VIDEO_FORMAT_UNKNOWN))
1141     goto not_negotiated;
1142
1143   /* 0 framerate and we are at the second frame, eos */
1144   if (G_UNLIKELY (src->info.fps_n == 0 && src->n_frames == 1))
1145     goto eos;
1146
1147   if (G_UNLIKELY (src->n_frames == -1)) {
1148     /* EOS for reverse playback */
1149     goto eos;
1150   }
1151
1152   GST_LOG_OBJECT (src,
1153       "creating buffer from pool for frame %" G_GINT64_FORMAT, src->n_frames);
1154
1155   if (!gst_video_frame_map (&frame, &src->info, buffer, GST_MAP_WRITE))
1156     goto invalid_frame;
1157
1158   GST_BUFFER_PTS (buffer) =
1159       src->accum_rtime + src->timestamp_offset + src->running_time;
1160   GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1161
1162   gst_object_sync_values (GST_OBJECT (psrc), GST_BUFFER_PTS (buffer));
1163
1164   src->make_image (src, GST_BUFFER_PTS (buffer), &frame);
1165
1166   if ((pal = gst_video_format_get_palette (GST_VIDEO_FRAME_FORMAT (&frame),
1167               &palsize))) {
1168     memcpy (GST_VIDEO_FRAME_PLANE_DATA (&frame, 1), pal, palsize);
1169   }
1170
1171   gst_video_frame_unmap (&frame);
1172
1173   GST_DEBUG_OBJECT (src, "Timestamp: %" GST_TIME_FORMAT " = accumulated %"
1174       GST_TIME_FORMAT " + offset: %"
1175       GST_TIME_FORMAT " + running time: %" GST_TIME_FORMAT,
1176       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), GST_TIME_ARGS (src->accum_rtime),
1177       GST_TIME_ARGS (src->timestamp_offset), GST_TIME_ARGS (src->running_time));
1178
1179   GST_BUFFER_OFFSET (buffer) = src->accum_frames + src->n_frames;
1180   if (src->reverse) {
1181     src->n_frames--;
1182   } else {
1183     src->n_frames++;
1184   }
1185   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET (buffer) + 1;
1186   if (src->info.fps_n) {
1187     next_time = gst_util_uint64_scale (src->n_frames,
1188         src->info.fps_d * GST_SECOND, src->info.fps_n);
1189     if (src->reverse) {
1190       /* We already decremented to next frame */
1191       GstClockTime prev_pts = gst_util_uint64_scale (src->n_frames + 2,
1192           src->info.fps_d * GST_SECOND, src->info.fps_n);
1193
1194       GST_BUFFER_DURATION (buffer) = prev_pts - GST_BUFFER_PTS (buffer);
1195     } else {
1196       GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
1197     }
1198   } else {
1199     next_time = src->timestamp_offset;
1200     /* NONE means forever */
1201     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1202   }
1203
1204   src->running_time = next_time;
1205
1206   return GST_FLOW_OK;
1207
1208 not_negotiated:
1209   {
1210     return GST_FLOW_NOT_NEGOTIATED;
1211   }
1212 eos:
1213   {
1214     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
1215     return GST_FLOW_EOS;
1216   }
1217 invalid_frame:
1218   {
1219     GST_DEBUG_OBJECT (src, "invalid frame");
1220     return GST_FLOW_OK;
1221   }
1222 }
1223
1224 static gboolean
1225 gst_video_test_src_start (GstBaseSrc * basesrc)
1226 {
1227   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
1228
1229   GST_OBJECT_LOCK (src);
1230   src->running_time = 0;
1231   src->n_frames = 0;
1232   src->accum_frames = 0;
1233   src->accum_rtime = 0;
1234
1235   gst_video_info_init (&src->info);
1236   GST_OBJECT_UNLOCK (src);
1237
1238   return TRUE;
1239 }
1240
1241 static gboolean
1242 gst_video_test_src_stop (GstBaseSrc * basesrc)
1243 {
1244   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
1245   guint i;
1246
1247   g_free (src->tmpline);
1248   src->tmpline = NULL;
1249   g_free (src->tmpline2);
1250   src->tmpline2 = NULL;
1251   g_free (src->tmpline_u8);
1252   src->tmpline_u8 = NULL;
1253   g_free (src->tmpline_u16);
1254   src->tmpline_u16 = NULL;
1255   if (src->subsample)
1256     gst_video_chroma_resample_free (src->subsample);
1257   src->subsample = NULL;
1258
1259   for (i = 0; i < src->n_lines; i++)
1260     g_free (src->lines[i]);
1261   g_free (src->lines);
1262   src->n_lines = 0;
1263   src->lines = NULL;
1264
1265   return TRUE;
1266 }
1267
1268 static gboolean
1269 plugin_init (GstPlugin * plugin)
1270 {
1271   GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
1272       "Video Test Source");
1273
1274   return GST_ELEMENT_REGISTER (videotestsrc, plugin);
1275 }
1276
1277 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1278     GST_VERSION_MINOR,
1279     videotestsrc,
1280     "Creates a test video stream",
1281     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)