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