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
gst_video_test_src_pattern_get_type(void)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
gst_video_test_src_animation_mode_get_type(void)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
gst_video_test_src_motion_type_get_type(void)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
gst_video_test_src_class_init(GstVideoTestSrcClass * klass)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 contant 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 
381 static void
gst_video_test_src_init(GstVideoTestSrc * src)382 gst_video_test_src_init (GstVideoTestSrc * src)
383 {
384   gst_video_test_src_set_pattern (src, DEFAULT_PATTERN);
385 
386   src->timestamp_offset = DEFAULT_TIMESTAMP_OFFSET;
387   src->foreground_color = DEFAULT_FOREGROUND_COLOR;
388   src->background_color = DEFAULT_BACKGROUND_COLOR;
389   src->horizontal_speed = DEFAULT_HORIZONTAL_SPEED;
390   src->random_state = 0;
391 
392   /* we operate in time */
393   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
394   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
395 
396   src->animation_mode = DEFAULT_ANIMATION_MODE;
397   src->motion_type = DEFAULT_MOTION_TYPE;
398   src->flip = DEFAULT_FLIP;
399 
400 }
401 
402 static GstCaps *
gst_video_test_src_src_fixate(GstBaseSrc * bsrc,GstCaps * caps)403 gst_video_test_src_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
404 {
405   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (bsrc);
406   GstStructure *structure;
407 
408   /* Check if foreground color has alpha, if it is the case,
409    * force color format with an alpha channel downstream */
410   if (src->foreground_color >> 24 != 255) {
411     guint i;
412     GstCaps *alpha_only_caps = gst_caps_new_empty ();
413 
414     for (i = 0; i < gst_caps_get_size (caps); i++) {
415       const GstVideoFormatInfo *info;
416       const GValue *formats =
417           gst_structure_get_value (gst_caps_get_structure (caps, i),
418           "format");
419 
420       if (GST_VALUE_HOLDS_LIST (formats)) {
421         GValue possible_formats = { 0, };
422         guint list_size = gst_value_list_get_size (formats);
423         guint index;
424 
425         g_value_init (&possible_formats, GST_TYPE_LIST);
426         for (index = 0; index < list_size; index++) {
427           const GValue *list_item = gst_value_list_get_value (formats, index);
428           info =
429               gst_video_format_get_info (gst_video_format_from_string
430               (g_value_get_string (list_item)));
431           if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (info)) {
432             GValue tmp = { 0, };
433 
434             gst_value_init_and_copy (&tmp, list_item);
435             gst_value_list_append_value (&possible_formats, &tmp);
436           }
437         }
438 
439         if (gst_value_list_get_size (&possible_formats)) {
440           GstStructure *astruct =
441               gst_structure_copy (gst_caps_get_structure (caps, i));
442 
443           gst_structure_set_value (astruct, "format", &possible_formats);
444           gst_caps_append_structure (alpha_only_caps, astruct);
445         }
446 
447       } else if (G_VALUE_HOLDS_STRING (formats)) {
448         info =
449             gst_video_format_get_info (gst_video_format_from_string
450             (g_value_get_string (formats)));
451 
452         if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (info)) {
453           gst_caps_append_structure (alpha_only_caps,
454               gst_structure_copy (gst_caps_get_structure (caps, i)));
455         }
456       } else {
457         g_assert_not_reached ();
458         GST_WARNING ("Unexpected type for video 'format' field: %s",
459             G_VALUE_TYPE_NAME (formats));
460       }
461     }
462 
463     if (gst_caps_is_empty (alpha_only_caps)) {
464       GST_WARNING_OBJECT (src,
465           "Foreground color contains alpha, but downstream can't support alpha.");
466     } else {
467       gst_caps_replace (&caps, alpha_only_caps);
468     }
469   }
470 
471   caps = gst_caps_make_writable (caps);
472   structure = gst_caps_get_structure (caps, 0);
473 
474   gst_structure_fixate_field_nearest_int (structure, "width", 320);
475   gst_structure_fixate_field_nearest_int (structure, "height", 240);
476 
477   if (gst_structure_has_field (structure, "framerate"))
478     gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
479   else
480     gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, 30, 1, NULL);
481 
482   if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
483     gst_structure_fixate_field_nearest_fraction (structure,
484         "pixel-aspect-ratio", 1, 1);
485   else
486     gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
487         NULL);
488 
489   if (gst_structure_has_field (structure, "colorimetry"))
490     gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
491   if (gst_structure_has_field (structure, "chroma-site"))
492     gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
493 
494   if (gst_structure_has_field (structure, "interlace-mode"))
495     gst_structure_fixate_field_string (structure, "interlace-mode",
496         "progressive");
497   else
498     gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
499         "progressive", NULL);
500 
501   if (gst_structure_has_field (structure, "multiview-mode"))
502     gst_structure_fixate_field_string (structure, "multiview-mode",
503         gst_video_multiview_mode_to_caps_string
504         (GST_VIDEO_MULTIVIEW_MODE_MONO));
505   else
506     gst_structure_set (structure, "multiview-mode", G_TYPE_STRING,
507         gst_video_multiview_mode_to_caps_string (GST_VIDEO_MULTIVIEW_MODE_MONO),
508         NULL);
509 
510   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
511 
512   return caps;
513 }
514 
515 static void
gst_video_test_src_set_pattern(GstVideoTestSrc * videotestsrc,int pattern_type)516 gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
517     int pattern_type)
518 {
519   videotestsrc->pattern_type = pattern_type;
520 
521   GST_DEBUG_OBJECT (videotestsrc, "setting pattern to %d", pattern_type);
522 
523   switch (pattern_type) {
524     case GST_VIDEO_TEST_SRC_SMPTE:
525       videotestsrc->make_image = gst_video_test_src_smpte;
526       break;
527     case GST_VIDEO_TEST_SRC_SNOW:
528       videotestsrc->make_image = gst_video_test_src_snow;
529       break;
530     case GST_VIDEO_TEST_SRC_BLACK:
531       videotestsrc->make_image = gst_video_test_src_black;
532       break;
533     case GST_VIDEO_TEST_SRC_WHITE:
534       videotestsrc->make_image = gst_video_test_src_white;
535       break;
536     case GST_VIDEO_TEST_SRC_RED:
537       videotestsrc->make_image = gst_video_test_src_red;
538       break;
539     case GST_VIDEO_TEST_SRC_GREEN:
540       videotestsrc->make_image = gst_video_test_src_green;
541       break;
542     case GST_VIDEO_TEST_SRC_BLUE:
543       videotestsrc->make_image = gst_video_test_src_blue;
544       break;
545     case GST_VIDEO_TEST_SRC_CHECKERS1:
546       videotestsrc->make_image = gst_video_test_src_checkers1;
547       break;
548     case GST_VIDEO_TEST_SRC_CHECKERS2:
549       videotestsrc->make_image = gst_video_test_src_checkers2;
550       break;
551     case GST_VIDEO_TEST_SRC_CHECKERS4:
552       videotestsrc->make_image = gst_video_test_src_checkers4;
553       break;
554     case GST_VIDEO_TEST_SRC_CHECKERS8:
555       videotestsrc->make_image = gst_video_test_src_checkers8;
556       break;
557     case GST_VIDEO_TEST_SRC_CIRCULAR:
558       videotestsrc->make_image = gst_video_test_src_circular;
559       break;
560     case GST_VIDEO_TEST_SRC_BLINK:
561       videotestsrc->make_image = gst_video_test_src_blink;
562       break;
563     case GST_VIDEO_TEST_SRC_SMPTE75:
564       videotestsrc->make_image = gst_video_test_src_smpte75;
565       break;
566     case GST_VIDEO_TEST_SRC_ZONE_PLATE:
567       videotestsrc->make_image = gst_video_test_src_zoneplate;
568       break;
569     case GST_VIDEO_TEST_SRC_GAMUT:
570       videotestsrc->make_image = gst_video_test_src_gamut;
571       break;
572     case GST_VIDEO_TEST_SRC_CHROMA_ZONE_PLATE:
573       videotestsrc->make_image = gst_video_test_src_chromazoneplate;
574       break;
575     case GST_VIDEO_TEST_SRC_SOLID:
576       videotestsrc->make_image = gst_video_test_src_solid;
577       break;
578     case GST_VIDEO_TEST_SRC_BALL:
579       videotestsrc->make_image = gst_video_test_src_ball;
580       break;
581     case GST_VIDEO_TEST_SRC_SMPTE100:
582       videotestsrc->make_image = gst_video_test_src_smpte100;
583       break;
584     case GST_VIDEO_TEST_SRC_BAR:
585       videotestsrc->make_image = gst_video_test_src_bar;
586       break;
587     case GST_VIDEO_TEST_SRC_PINWHEEL:
588       videotestsrc->make_image = gst_video_test_src_pinwheel;
589       break;
590     case GST_VIDEO_TEST_SRC_SPOKES:
591       videotestsrc->make_image = gst_video_test_src_spokes;
592       break;
593     case GST_VIDEO_TEST_SRC_GRADIENT:
594       videotestsrc->make_image = gst_video_test_src_gradient;
595       break;
596     case GST_VIDEO_TEST_SRC_COLORS:
597       videotestsrc->make_image = gst_video_test_src_colors;
598       break;
599     default:
600       g_assert_not_reached ();
601   }
602 }
603 
604 static void
gst_video_test_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)605 gst_video_test_src_set_property (GObject * object, guint prop_id,
606     const GValue * value, GParamSpec * pspec)
607 {
608   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
609 
610   switch (prop_id) {
611     case PROP_PATTERN:
612       gst_video_test_src_set_pattern (src, g_value_get_enum (value));
613       break;
614     case PROP_TIMESTAMP_OFFSET:
615       src->timestamp_offset = g_value_get_int64 (value);
616       break;
617     case PROP_IS_LIVE:
618       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
619       break;
620     case PROP_K0:
621       src->k0 = g_value_get_int (value);
622       break;
623     case PROP_KX:
624       src->kx = g_value_get_int (value);
625       break;
626     case PROP_KY:
627       src->ky = g_value_get_int (value);
628       break;
629     case PROP_KT:
630       src->kt = g_value_get_int (value);
631       break;
632     case PROP_KXT:
633       src->kxt = g_value_get_int (value);
634       break;
635     case PROP_KYT:
636       src->kyt = g_value_get_int (value);
637       break;
638     case PROP_KXY:
639       src->kxy = g_value_get_int (value);
640       break;
641     case PROP_KX2:
642       src->kx2 = g_value_get_int (value);
643       break;
644     case PROP_KY2:
645       src->ky2 = g_value_get_int (value);
646       break;
647     case PROP_KT2:
648       src->kt2 = g_value_get_int (value);
649       break;
650     case PROP_XOFFSET:
651       src->xoffset = g_value_get_int (value);
652       break;
653     case PROP_YOFFSET:
654       src->yoffset = g_value_get_int (value);
655       break;
656     case PROP_FOREGROUND_COLOR:
657       src->foreground_color = g_value_get_uint (value);
658       break;
659     case PROP_BACKGROUND_COLOR:
660       src->background_color = g_value_get_uint (value);
661       break;
662     case PROP_HORIZONTAL_SPEED:
663       src->horizontal_speed = g_value_get_int (value);
664       break;
665     case PROP_ANIMATION_MODE:
666       src->animation_mode = g_value_get_enum (value);
667       break;
668     case PROP_MOTION_TYPE:
669       src->motion_type = g_value_get_enum (value);
670       break;
671     case PROP_FLIP:
672       src->flip = g_value_get_boolean (value);
673       break;
674     default:
675       break;
676   }
677 }
678 
679 static void
gst_video_test_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)680 gst_video_test_src_get_property (GObject * object, guint prop_id,
681     GValue * value, GParamSpec * pspec)
682 {
683   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
684 
685   switch (prop_id) {
686     case PROP_PATTERN:
687       g_value_set_enum (value, src->pattern_type);
688       break;
689     case PROP_TIMESTAMP_OFFSET:
690       g_value_set_int64 (value, src->timestamp_offset);
691       break;
692     case PROP_IS_LIVE:
693       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
694       break;
695     case PROP_K0:
696       g_value_set_int (value, src->k0);
697       break;
698     case PROP_KX:
699       g_value_set_int (value, src->kx);
700       break;
701     case PROP_KY:
702       g_value_set_int (value, src->ky);
703       break;
704     case PROP_KT:
705       g_value_set_int (value, src->kt);
706       break;
707     case PROP_KXT:
708       g_value_set_int (value, src->kxt);
709       break;
710     case PROP_KYT:
711       g_value_set_int (value, src->kyt);
712       break;
713     case PROP_KXY:
714       g_value_set_int (value, src->kxy);
715       break;
716     case PROP_KX2:
717       g_value_set_int (value, src->kx2);
718       break;
719     case PROP_KY2:
720       g_value_set_int (value, src->ky2);
721       break;
722     case PROP_KT2:
723       g_value_set_int (value, src->kt2);
724       break;
725     case PROP_XOFFSET:
726       g_value_set_int (value, src->xoffset);
727       break;
728     case PROP_YOFFSET:
729       g_value_set_int (value, src->yoffset);
730       break;
731     case PROP_FOREGROUND_COLOR:
732       g_value_set_uint (value, src->foreground_color);
733       break;
734     case PROP_BACKGROUND_COLOR:
735       g_value_set_uint (value, src->background_color);
736       break;
737     case PROP_HORIZONTAL_SPEED:
738       g_value_set_int (value, src->horizontal_speed);
739       break;
740     case PROP_ANIMATION_MODE:
741       g_value_set_enum (value, src->animation_mode);
742       break;
743     case PROP_MOTION_TYPE:
744       g_value_set_enum (value, src->motion_type);
745       break;
746     case PROP_FLIP:
747       g_value_set_boolean (value, src->flip);
748       break;
749     default:
750       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
751       break;
752   }
753 }
754 
755 static gboolean
gst_video_test_src_parse_caps(const GstCaps * caps,gint * width,gint * height,gint * fps_n,gint * fps_d,GstVideoColorimetry * colorimetry,gint * x_inv,gint * y_inv)756 gst_video_test_src_parse_caps (const GstCaps * caps,
757     gint * width, gint * height, gint * fps_n, gint * fps_d,
758     GstVideoColorimetry * colorimetry, gint * x_inv, gint * y_inv)
759 {
760   const GstStructure *structure;
761   GstPadLinkReturn ret;
762   const GValue *framerate;
763   const gchar *str;
764 
765   GST_DEBUG ("parsing caps");
766 
767   structure = gst_caps_get_structure (caps, 0);
768 
769   ret = gst_structure_get_int (structure, "width", width);
770   ret &= gst_structure_get_int (structure, "height", height);
771   framerate = gst_structure_get_value (structure, "framerate");
772 
773   if (framerate) {
774     *fps_n = gst_value_get_fraction_numerator (framerate);
775     *fps_d = gst_value_get_fraction_denominator (framerate);
776   } else
777     goto no_framerate;
778 
779   if ((str = gst_structure_get_string (structure, "colorimetry")))
780     gst_video_colorimetry_from_string (colorimetry, str);
781 
782   if ((str = gst_structure_get_string (structure, "format"))) {
783     if (g_str_equal (str, "bggr")) {
784       *x_inv = *y_inv = 0;
785     } else if (g_str_equal (str, "rggb")) {
786       *x_inv = *y_inv = 1;
787     } else if (g_str_equal (str, "grbg")) {
788       *x_inv = 0;
789       *y_inv = 1;
790     } else if (g_str_equal (str, "gbrg")) {
791       *x_inv = 1;
792       *y_inv = 0;
793     } else
794       goto invalid_format;
795   }
796   return ret;
797 
798   /* ERRORS */
799 no_framerate:
800   {
801     GST_DEBUG ("videotestsrc no framerate given");
802     return FALSE;
803   }
804 invalid_format:
805   {
806     GST_DEBUG ("videotestsrc invalid bayer format given");
807     return FALSE;
808   }
809 }
810 
811 static gboolean
gst_video_test_src_decide_allocation(GstBaseSrc * bsrc,GstQuery * query)812 gst_video_test_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
813 {
814   GstVideoTestSrc *videotestsrc;
815   GstBufferPool *pool;
816   gboolean update;
817   guint size, min, max;
818   GstStructure *config;
819   GstCaps *caps = NULL;
820 
821   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
822 
823   if (gst_query_get_n_allocation_pools (query) > 0) {
824     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
825 
826     /* adjust size */
827     size = MAX (size, videotestsrc->info.size);
828     update = TRUE;
829   } else {
830     pool = NULL;
831     size = videotestsrc->info.size;
832     min = max = 0;
833     update = FALSE;
834   }
835 
836   /* no downstream pool, make our own */
837   if (pool == NULL) {
838     if (videotestsrc->bayer)
839       pool = gst_buffer_pool_new ();
840     else
841       pool = gst_video_buffer_pool_new ();
842   }
843 
844   config = gst_buffer_pool_get_config (pool);
845 
846   gst_query_parse_allocation (query, &caps, NULL);
847   if (caps)
848     gst_buffer_pool_config_set_params (config, caps, size, min, max);
849 
850   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
851     gst_buffer_pool_config_add_option (config,
852         GST_BUFFER_POOL_OPTION_VIDEO_META);
853   }
854   gst_buffer_pool_set_config (pool, config);
855 
856   if (update)
857     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
858   else
859     gst_query_add_allocation_pool (query, pool, size, min, max);
860 
861   if (pool)
862     gst_object_unref (pool);
863 
864   return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
865 }
866 
867 static gboolean
gst_video_test_src_setcaps(GstBaseSrc * bsrc,GstCaps * caps)868 gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
869 {
870   const GstStructure *structure;
871   GstVideoTestSrc *videotestsrc;
872   GstVideoInfo info;
873   guint i;
874   guint n_lines;
875   gint offset;
876 
877   videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
878 
879   structure = gst_caps_get_structure (caps, 0);
880 
881   GST_OBJECT_LOCK (videotestsrc);
882 
883   if (gst_structure_has_name (structure, "video/x-raw")) {
884     /* we can use the parsing code */
885     if (!gst_video_info_from_caps (&info, caps))
886       goto parse_failed;
887 
888   } else if (gst_structure_has_name (structure, "video/x-bayer")) {
889     gint x_inv = 0, y_inv = 0;
890 
891     gst_video_info_init (&info);
892 
893     info.finfo = gst_video_format_get_info (GST_VIDEO_FORMAT_GRAY8);
894 
895     if (!gst_video_test_src_parse_caps (caps, &info.width, &info.height,
896             &info.fps_n, &info.fps_d, &info.colorimetry, &x_inv, &y_inv))
897       goto parse_failed;
898 
899     info.size = GST_ROUND_UP_4 (info.width) * info.height;
900     info.stride[0] = GST_ROUND_UP_4 (info.width);
901 
902     videotestsrc->bayer = TRUE;
903     videotestsrc->x_invert = x_inv;
904     videotestsrc->y_invert = y_inv;
905   } else {
906     goto unsupported_caps;
907   }
908 
909   /* create chroma subsampler */
910   if (videotestsrc->subsample)
911     gst_video_chroma_resample_free (videotestsrc->subsample);
912   videotestsrc->subsample = gst_video_chroma_resample_new (0,
913       info.chroma_site, 0, info.finfo->unpack_format, -info.finfo->w_sub[2],
914       -info.finfo->h_sub[2]);
915 
916   for (i = 0; i < videotestsrc->n_lines; i++)
917     g_free (videotestsrc->lines[i]);
918   g_free (videotestsrc->lines);
919 
920   if (videotestsrc->subsample != NULL) {
921     gst_video_chroma_resample_get_info (videotestsrc->subsample,
922         &n_lines, &offset);
923   } else {
924     n_lines = 1;
925     offset = 0;
926   }
927 
928   videotestsrc->lines = g_malloc (sizeof (gpointer) * n_lines);
929   for (i = 0; i < n_lines; i++)
930     videotestsrc->lines[i] = g_malloc ((info.width + 16) * 8);
931   videotestsrc->n_lines = n_lines;
932   videotestsrc->offset = offset;
933 
934   /* looks ok here */
935   videotestsrc->info = info;
936 
937   GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
938       info.width, info.height, info.fps_n, info.fps_d);
939 
940   g_free (videotestsrc->tmpline);
941   g_free (videotestsrc->tmpline2);
942   g_free (videotestsrc->tmpline_u8);
943   g_free (videotestsrc->tmpline_u16);
944   videotestsrc->tmpline_u8 = g_malloc (info.width + 8);
945   videotestsrc->tmpline = g_malloc ((info.width + 8) * 4);
946   videotestsrc->tmpline2 = g_malloc ((info.width + 8) * 4);
947   videotestsrc->tmpline_u16 = g_malloc ((info.width + 16) * 8);
948 
949   videotestsrc->accum_rtime += videotestsrc->running_time;
950   videotestsrc->accum_frames += videotestsrc->n_frames;
951 
952   videotestsrc->running_time = 0;
953   videotestsrc->n_frames = 0;
954 
955   GST_OBJECT_UNLOCK (videotestsrc);
956 
957   return TRUE;
958 
959   /* ERRORS */
960 parse_failed:
961   {
962     GST_OBJECT_UNLOCK (videotestsrc);
963     GST_DEBUG_OBJECT (bsrc, "failed to parse caps");
964     return FALSE;
965   }
966 unsupported_caps:
967   {
968     GST_OBJECT_UNLOCK (videotestsrc);
969     GST_DEBUG_OBJECT (bsrc, "unsupported caps: %" GST_PTR_FORMAT, caps);
970     return FALSE;
971   }
972 }
973 
974 static gboolean
gst_video_test_src_query(GstBaseSrc * bsrc,GstQuery * query)975 gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
976 {
977   gboolean res = FALSE;
978   GstVideoTestSrc *src;
979 
980   src = GST_VIDEO_TEST_SRC (bsrc);
981 
982   switch (GST_QUERY_TYPE (query)) {
983     case GST_QUERY_CONVERT:
984     {
985       GstFormat src_fmt, dest_fmt;
986       gint64 src_val, dest_val;
987 
988       GST_OBJECT_LOCK (src);
989       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
990       res =
991           gst_video_info_convert (&src->info, src_fmt, src_val, dest_fmt,
992           &dest_val);
993       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
994       GST_OBJECT_UNLOCK (src);
995       break;
996     }
997     case GST_QUERY_LATENCY:
998     {
999       GST_OBJECT_LOCK (src);
1000       if (src->info.fps_n > 0) {
1001         GstClockTime latency;
1002 
1003         latency =
1004             gst_util_uint64_scale (GST_SECOND, src->info.fps_d,
1005             src->info.fps_n);
1006         GST_OBJECT_UNLOCK (src);
1007         gst_query_set_latency (query,
1008             gst_base_src_is_live (GST_BASE_SRC_CAST (src)), latency,
1009             GST_CLOCK_TIME_NONE);
1010         GST_DEBUG_OBJECT (src, "Reporting latency of %" GST_TIME_FORMAT,
1011             GST_TIME_ARGS (latency));
1012         res = TRUE;
1013       } else {
1014         GST_OBJECT_UNLOCK (src);
1015       }
1016       break;
1017     }
1018     case GST_QUERY_DURATION:{
1019       if (bsrc->num_buffers != -1) {
1020         GstFormat format;
1021 
1022         gst_query_parse_duration (query, &format, NULL);
1023         switch (format) {
1024           case GST_FORMAT_TIME:{
1025             gint64 dur;
1026 
1027             GST_OBJECT_LOCK (src);
1028             dur = gst_util_uint64_scale_int_round (bsrc->num_buffers
1029                 * GST_SECOND, src->info.fps_d, src->info.fps_n);
1030             res = TRUE;
1031             gst_query_set_duration (query, GST_FORMAT_TIME, dur);
1032             GST_OBJECT_UNLOCK (src);
1033             goto done;
1034           }
1035           case GST_FORMAT_BYTES:
1036             GST_OBJECT_LOCK (src);
1037             res = TRUE;
1038             gst_query_set_duration (query, GST_FORMAT_BYTES,
1039                 bsrc->num_buffers * src->info.size);
1040             GST_OBJECT_UNLOCK (src);
1041             goto done;
1042           default:
1043             break;
1044         }
1045       }
1046       /* fall through */
1047     }
1048     default:
1049       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1050       break;
1051   }
1052 done:
1053   return res;
1054 }
1055 
1056 static void
gst_video_test_src_get_times(GstBaseSrc * basesrc,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)1057 gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
1058     GstClockTime * start, GstClockTime * end)
1059 {
1060   /* for live sources, sync on the timestamp of the buffer */
1061   if (gst_base_src_is_live (basesrc)) {
1062     GstClockTime timestamp = GST_BUFFER_PTS (buffer);
1063 
1064     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1065       /* get duration to calculate end time */
1066       GstClockTime duration = GST_BUFFER_DURATION (buffer);
1067 
1068       if (GST_CLOCK_TIME_IS_VALID (duration)) {
1069         *end = timestamp + duration;
1070       }
1071       *start = timestamp;
1072     }
1073   } else {
1074     *start = -1;
1075     *end = -1;
1076   }
1077 }
1078 
1079 static gboolean
gst_video_test_src_do_seek(GstBaseSrc * bsrc,GstSegment * segment)1080 gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1081 {
1082   GstClockTime position;
1083   GstVideoTestSrc *src;
1084 
1085   src = GST_VIDEO_TEST_SRC (bsrc);
1086 
1087   segment->time = segment->start;
1088   position = segment->position;
1089   src->reverse = segment->rate < 0;
1090 
1091   /* now move to the position indicated */
1092   if (src->info.fps_n) {
1093     src->n_frames = gst_util_uint64_scale (position,
1094         src->info.fps_n, src->info.fps_d * GST_SECOND);
1095   } else {
1096     src->n_frames = 0;
1097   }
1098   src->accum_frames = 0;
1099   src->accum_rtime = 0;
1100   if (src->info.fps_n) {
1101     src->running_time = gst_util_uint64_scale (src->n_frames,
1102         src->info.fps_d * GST_SECOND, src->info.fps_n);
1103   } else {
1104     /* FIXME : Not sure what to set here */
1105     src->running_time = 0;
1106   }
1107 
1108   g_assert (src->running_time <= position);
1109 
1110   return TRUE;
1111 }
1112 
1113 static gboolean
gst_video_test_src_is_seekable(GstBaseSrc * psrc)1114 gst_video_test_src_is_seekable (GstBaseSrc * psrc)
1115 {
1116   /* we're seekable... */
1117   return TRUE;
1118 }
1119 
1120 static GstFlowReturn
gst_video_test_src_fill(GstPushSrc * psrc,GstBuffer * buffer)1121 gst_video_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
1122 {
1123   GstVideoTestSrc *src;
1124   GstClockTime next_time;
1125   GstVideoFrame frame;
1126   gconstpointer pal;
1127   gsize palsize;
1128 
1129   src = GST_VIDEO_TEST_SRC (psrc);
1130 
1131   if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&src->info) ==
1132           GST_VIDEO_FORMAT_UNKNOWN))
1133     goto not_negotiated;
1134 
1135   /* 0 framerate and we are at the second frame, eos */
1136   if (G_UNLIKELY (src->info.fps_n == 0 && src->n_frames == 1))
1137     goto eos;
1138 
1139   if (G_UNLIKELY (src->n_frames == -1)) {
1140     /* EOS for reverse playback */
1141     goto eos;
1142   }
1143 
1144   GST_LOG_OBJECT (src,
1145       "creating buffer from pool for frame %" G_GINT64_FORMAT, src->n_frames);
1146 
1147   if (!gst_video_frame_map (&frame, &src->info, buffer, GST_MAP_WRITE))
1148     goto invalid_frame;
1149 
1150   GST_BUFFER_PTS (buffer) =
1151       src->accum_rtime + src->timestamp_offset + src->running_time;
1152   GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
1153 
1154   gst_object_sync_values (GST_OBJECT (psrc), GST_BUFFER_PTS (buffer));
1155 
1156   src->make_image (src, GST_BUFFER_PTS (buffer), &frame);
1157 
1158   if ((pal = gst_video_format_get_palette (GST_VIDEO_FRAME_FORMAT (&frame),
1159               &palsize))) {
1160     memcpy (GST_VIDEO_FRAME_PLANE_DATA (&frame, 1), pal, palsize);
1161   }
1162 
1163   gst_video_frame_unmap (&frame);
1164 
1165   GST_DEBUG_OBJECT (src, "Timestamp: %" GST_TIME_FORMAT " = accumulated %"
1166       GST_TIME_FORMAT " + offset: %"
1167       GST_TIME_FORMAT " + running time: %" GST_TIME_FORMAT,
1168       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), GST_TIME_ARGS (src->accum_rtime),
1169       GST_TIME_ARGS (src->timestamp_offset), GST_TIME_ARGS (src->running_time));
1170 
1171   GST_BUFFER_OFFSET (buffer) = src->accum_frames + src->n_frames;
1172   if (src->reverse) {
1173     src->n_frames--;
1174   } else {
1175     src->n_frames++;
1176   }
1177   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET (buffer) + 1;
1178   if (src->info.fps_n) {
1179     next_time = gst_util_uint64_scale (src->n_frames,
1180         src->info.fps_d * GST_SECOND, src->info.fps_n);
1181     if (src->reverse) {
1182       GST_BUFFER_DURATION (buffer) = src->running_time - next_time;
1183     } else {
1184       GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
1185     }
1186   } else {
1187     next_time = src->timestamp_offset;
1188     /* NONE means forever */
1189     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1190   }
1191 
1192   src->running_time = next_time;
1193 
1194   return GST_FLOW_OK;
1195 
1196 not_negotiated:
1197   {
1198     return GST_FLOW_NOT_NEGOTIATED;
1199   }
1200 eos:
1201   {
1202     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
1203     return GST_FLOW_EOS;
1204   }
1205 invalid_frame:
1206   {
1207     GST_DEBUG_OBJECT (src, "invalid frame");
1208     return GST_FLOW_OK;
1209   }
1210 }
1211 
1212 static gboolean
gst_video_test_src_start(GstBaseSrc * basesrc)1213 gst_video_test_src_start (GstBaseSrc * basesrc)
1214 {
1215   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
1216 
1217   GST_OBJECT_LOCK (src);
1218   src->running_time = 0;
1219   src->n_frames = 0;
1220   src->accum_frames = 0;
1221   src->accum_rtime = 0;
1222 
1223   gst_video_info_init (&src->info);
1224   GST_OBJECT_UNLOCK (src);
1225 
1226   return TRUE;
1227 }
1228 
1229 static gboolean
gst_video_test_src_stop(GstBaseSrc * basesrc)1230 gst_video_test_src_stop (GstBaseSrc * basesrc)
1231 {
1232   GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);
1233   guint i;
1234 
1235   g_free (src->tmpline);
1236   src->tmpline = NULL;
1237   g_free (src->tmpline2);
1238   src->tmpline2 = NULL;
1239   g_free (src->tmpline_u8);
1240   src->tmpline_u8 = NULL;
1241   g_free (src->tmpline_u16);
1242   src->tmpline_u16 = NULL;
1243   if (src->subsample)
1244     gst_video_chroma_resample_free (src->subsample);
1245   src->subsample = NULL;
1246 
1247   for (i = 0; i < src->n_lines; i++)
1248     g_free (src->lines[i]);
1249   g_free (src->lines);
1250   src->n_lines = 0;
1251   src->lines = NULL;
1252 
1253   return TRUE;
1254 }
1255 
1256 static gboolean
plugin_init(GstPlugin * plugin)1257 plugin_init (GstPlugin * plugin)
1258 {
1259   GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
1260       "Video Test Source");
1261 
1262   return gst_element_register (plugin, "videotestsrc", GST_RANK_NONE,
1263       GST_TYPE_VIDEO_TEST_SRC);
1264 }
1265 
1266 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1267     GST_VERSION_MINOR,
1268     videotestsrc,
1269     "Creates a test video stream",
1270     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
1271