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