2 * Copyright (C) 2006 David A. Schleef ds@schleef.org
3 * Copyright (C) 2019 Cesar Fabian Orccon Chipana
4 * Copyright (C) 2020 Thibault Saunier <tsaunier@igalia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
19 * Boston, MA 02110-1335, USA.
23 * SECTION:element-imagesequencesrc
25 * Stream image sequences from image files.
28 * gst-launch-1.0 imagesequencesrc location=image-%05d.jpg start-index=1 stop-index=50 framerate=24/1 ! decodebin ! videoconvert ! autovideosink
31 * This elements implements the #GstURIHandler interface meaning that you can use it with playbin,
32 * (make sure to quote the URI for the filename pattern, like: `%2505d` instead of the `%05d` you would use
33 * when dealing with the location).
35 * Note that you can pass the #imagesequencesrc:framerate, #imagesequencesrc:start-index and #imagesequencesrc:stop-index
36 * properties directly in the URI using its 'query' component, for example:
39 * gst-launch-1.0 playbin uri="imagesequence://path/to/image-%2505d.jpeg?start-index=0&framerate=30/1"
48 #include <gst/base/gsttypefindhelper.h>
50 #include "gstimagesequencesrc.h"
52 #define LOCK(self) (g_rec_mutex_lock (&self->fields_lock))
53 #define UNLOCK(self) (g_rec_mutex_unlock (&self->fields_lock))
55 static GstFlowReturn gst_image_sequence_src_create (GstPushSrc * src,
59 static void gst_image_sequence_src_set_property (GObject * object,
60 guint prop_id, const GValue * value, GParamSpec * pspec);
61 static void gst_image_sequence_src_get_property (GObject * object,
62 guint prop_id, GValue * value, GParamSpec * pspec);
63 static GstCaps *gst_image_sequence_src_getcaps (GstBaseSrc * src,
65 static gboolean gst_image_sequence_src_query (GstBaseSrc * src,
67 static void gst_image_sequence_src_set_caps (GstImageSequenceSrc * self,
69 static void gst_image_sequence_src_set_duration (GstImageSequenceSrc * self);
70 static gint gst_image_sequence_src_count_frames (GstImageSequenceSrc * self,
74 static GstStaticPadTemplate gst_image_sequence_src_pad_template =
75 GST_STATIC_PAD_TEMPLATE ("src",
80 GST_DEBUG_CATEGORY_STATIC (gst_image_sequence_src_debug);
81 #define GST_CAT_DEFAULT gst_image_sequence_src_debug
92 #define DEFAULT_LOCATION "%05d"
93 #define DEFAULT_START_INDEX 0
94 #define DEFAULT_STOP_INDEX -1
95 #define DEFAULT_FRAMERATE 30
97 /* Call with LOCK taken */
99 gst_image_sequence_src_set_location (GstImageSequenceSrc * self,
100 const gchar * location)
103 if (location != NULL)
104 self->path = g_strdup (location);
111 /*** GSTURIHANDLER INTERFACE *************************************************/
114 gst_image_sequence_src_uri_get_type (GType type)
119 static const gchar *const *
120 gst_image_sequence_src_uri_get_protocols (GType type)
122 static const gchar *protocols[] = { "imagesequence", NULL };
128 gst_image_sequence_src_uri_get_uri (GstURIHandler * handler)
130 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (handler);
135 uri = gst_uri_to_string (self->uri);
137 uri = gst_uri_construct ("imagesequence", self->path);
144 gst_image_sequence_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
147 gchar *hostname = NULL, *location = NULL, *path, *tmp;
148 gboolean ret = FALSE;
149 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (handler);
150 GstUri *ruri = gst_uri_from_string (uri);
151 GHashTable *query = NULL;
154 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
155 "imagesequencesrc URI is invalid: '%s'", uri);
161 g_clear_pointer (&self->uri, gst_uri_unref);
163 path = gst_uri_get_path (ruri);
164 tmp = gst_filename_to_uri (path, err);
165 location = g_filename_from_uri (tmp, &hostname, err);
168 query = gst_uri_get_query_table (ruri);
169 if (!location || (err != NULL && *err != NULL)) {
170 GST_WARNING_OBJECT (self, "Invalid URI '%s' for imagesequencesrc: %s", uri,
171 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
175 if (hostname && strcmp (hostname, "localhost")) {
176 /* Only 'localhost' is permitted */
177 GST_WARNING_OBJECT (self, "Invalid hostname '%s' for filesrc", hostname);
178 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
179 "File URI with invalid hostname '%s'", hostname);
183 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
184 * correctly on windows, it leaves them with an extra backslash
185 * at the start if they're of the mozilla-style file://///host/path/file
186 * form. Correct this.
188 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
189 memmove (location, location + 1, strlen (location + 1) + 1);
192 ret = gst_image_sequence_src_set_location (self, location);
198 g_hash_table_iter_init (&iter, query);
199 while (g_hash_table_iter_next (&iter, &key, &value)) {
200 GST_INFO_OBJECT (self, "Setting property from URI: %s=%s", (gchar *) key,
202 gst_util_set_object_arg (G_OBJECT (self), key, value);
211 g_clear_pointer (&query, g_hash_table_unref);
217 gst_image_sequence_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
219 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
221 iface->get_type = gst_image_sequence_src_uri_get_type;
222 iface->get_protocols = gst_image_sequence_src_uri_get_protocols;
223 iface->get_uri = gst_image_sequence_src_uri_get_uri;
224 iface->set_uri = gst_image_sequence_src_uri_set_uri;
227 #define gst_image_sequence_src_parent_class parent_class
229 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_image_sequence_src_uri_handler_init); \
230 GST_DEBUG_CATEGORY_INIT (gst_image_sequence_src_debug, "imagesequencesrc", \
231 0, "imagesequencesrc element");
232 G_DEFINE_TYPE_WITH_CODE (GstImageSequenceSrc, gst_image_sequence_src,
233 GST_TYPE_PUSH_SRC, _do_init);
234 GST_ELEMENT_REGISTER_DEFINE (imagesequencesrc, "imagesequencesrc",
235 GST_RANK_NONE, gst_image_sequence_src_get_type ());
238 is_seekable (GstBaseSrc * src)
240 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (src);
242 if ((self->n_frames != 0) && (self->fps_n) && (self->fps_d))
249 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
251 GstImageSequenceSrc *self;
253 self = GST_IMAGE_SEQUENCE_SRC (bsrc);
255 self->reverse = segment->rate < 0;
257 segment->time = segment->start;
262 segment->position * self->fps_n / (self->fps_d * GST_SECOND);
268 gst_image_sequence_src_finalize (GObject * object)
270 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
272 g_clear_pointer (&self->path, g_free);
273 g_rec_mutex_clear (&self->fields_lock);
275 G_OBJECT_CLASS (parent_class)->finalize (object);
279 gst_image_sequence_src_dispose (GObject * object)
281 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
283 gst_clear_caps (&self->caps);
284 g_clear_pointer (&self->uri, gst_uri_unref);
286 G_OBJECT_CLASS (parent_class)->dispose (object);
290 gst_image_sequence_src_class_init (GstImageSequenceSrcClass * klass)
292 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
293 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
294 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
295 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
297 gobject_class->set_property = gst_image_sequence_src_set_property;
298 gobject_class->get_property = gst_image_sequence_src_get_property;
301 g_object_class_install_property (gobject_class, PROP_LOCATION,
302 g_param_spec_string ("location", "File Location",
303 "Pattern to create file names of input files. File names are "
304 "created by calling sprintf() with the pattern and the current "
305 "index.", DEFAULT_LOCATION,
306 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_START_INDEX,
308 g_param_spec_int ("start-index", "Start Index",
309 "Start value of index. The initial value of index can be set "
310 "either by setting index or start-index. When the end of the loop "
311 "is reached, the index will be set to the value start-index.",
312 0, INT_MAX, DEFAULT_START_INDEX,
313 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_STOP_INDEX,
315 g_param_spec_int ("stop-index", "Stop Index",
316 "Stop value of index. The special value -1 means no stop.",
317 -1, INT_MAX, DEFAULT_STOP_INDEX,
318 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_FRAMERATE,
320 gst_param_spec_fraction ("framerate", "Framerate",
321 "The output framerate.",
322 1, 1, G_MAXINT, 1, DEFAULT_FRAMERATE, 1,
323 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
325 gobject_class->finalize = gst_image_sequence_src_finalize;
326 gobject_class->dispose = gst_image_sequence_src_dispose;
328 gstbasesrc_class->get_caps = gst_image_sequence_src_getcaps;
329 gstbasesrc_class->query = gst_image_sequence_src_query;
330 gstbasesrc_class->is_seekable = is_seekable;
331 gstbasesrc_class->do_seek = do_seek;
333 gstpushsrc_class->create = gst_image_sequence_src_create;
335 gst_element_class_add_static_pad_template (gstelement_class,
336 &gst_image_sequence_src_pad_template);
337 gst_element_class_set_static_metadata (gstelement_class,
338 "Image Sequence Source", "Source/File/Video",
339 "Create a video stream from a sequence of image files",
340 "Cesar Fabian Orccon Chipana <cfoch.fabian@gmail.com>\n"
341 "Thibault Saunier <tsaunier@igalia.com>");
345 gst_image_sequence_src_init (GstImageSequenceSrc * self)
349 GST_DEBUG_CATEGORY_INIT (gst_image_sequence_src_debug, "imagesequencesrc", 0,
350 "imagesequencesrc element");
352 bsrc = GST_BASE_SRC (self);
353 gst_base_src_set_format (bsrc, GST_FORMAT_TIME);
355 g_rec_mutex_init (&self->fields_lock);
356 self->start_index = DEFAULT_START_INDEX;
358 self->stop_index = DEFAULT_STOP_INDEX;
359 self->path = g_strdup (DEFAULT_LOCATION);
367 gst_image_sequence_src_getcaps (GstBaseSrc * src, GstCaps * filter)
369 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (src);
371 GST_DEBUG_OBJECT (self, "returning %" GST_PTR_FORMAT, self->caps);
375 return gst_caps_intersect_full (filter, self->caps,
376 GST_CAPS_INTERSECT_FIRST);
378 return gst_caps_ref (filter);
381 return gst_caps_new_any ();
385 gst_image_sequence_src_query (GstBaseSrc * bsrc, GstQuery * query)
388 GstImageSequenceSrc *self;
390 self = GST_IMAGE_SEQUENCE_SRC (bsrc);
392 switch (GST_QUERY_TYPE (query)) {
393 case GST_QUERY_DURATION:
397 gst_query_parse_duration (query, &format, NULL);
400 case GST_FORMAT_TIME:
402 if (self->n_frames <= 0) {
403 gst_image_sequence_src_count_frames (self, FALSE);
404 gst_image_sequence_src_set_duration (self);
407 if (self->n_frames > 0)
408 gst_query_set_duration (query, format, self->duration);
414 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
419 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
427 gst_image_sequence_src_set_property (GObject * object, guint prop_id,
428 const GValue * value, GParamSpec * pspec)
430 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
435 gst_image_sequence_src_set_location (self, g_value_get_string (value));
437 case PROP_START_INDEX:
438 self->start_index = g_value_get_int (value);
439 gst_image_sequence_src_count_frames (self, FALSE);
441 case PROP_STOP_INDEX:
442 self->stop_index = g_value_get_int (value);
443 gst_image_sequence_src_count_frames (self, FALSE);
446 self->fps_n = gst_value_get_fraction_numerator (value);
447 self->fps_d = gst_value_get_fraction_denominator (value);
450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 gst_image_sequence_src_get_property (GObject * object, guint prop_id,
458 GValue * value, GParamSpec * pspec)
460 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
465 g_value_set_string (value, self->path);
467 case PROP_START_INDEX:
468 g_value_set_int (value, self->start_index);
470 case PROP_STOP_INDEX:
471 g_value_set_int (value, self->stop_index);
474 self->fps_n = gst_value_get_fraction_numerator (value);
475 self->fps_d = gst_value_get_fraction_denominator (value);
476 GST_DEBUG_OBJECT (self, "Set (framerate) property to (%d/%d)",
477 self->fps_n, self->fps_d);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gst_image_sequence_src_count_frames (GstImageSequenceSrc * self,
491 if (can_read && self->stop_index < 0 && self->path) {
494 for (i = self->start_index;; i++) {
495 gchar *filename = g_strdup_printf (self->path, i);
497 if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
505 if (i > self->start_index)
506 self->stop_index = i;
509 if (self->stop_index >= self->start_index)
510 self->n_frames = self->stop_index - self->start_index + 1;
511 return self->n_frames;
515 gst_image_sequence_src_set_caps (GstImageSequenceSrc * self, GstCaps * caps)
519 g_assert (caps != NULL);
520 new_caps = gst_caps_copy (caps);
522 if (self->n_frames > 0) {
523 GValue fps = G_VALUE_INIT;
524 g_value_init (&fps, GST_TYPE_FRACTION);
525 gst_value_set_fraction (&fps, self->fps_n, self->fps_d);
526 gst_caps_set_value (new_caps, "framerate", &fps);
527 g_value_unset (&fps);
530 gst_caps_replace (&self->caps, new_caps);
531 gst_pad_set_caps (GST_BASE_SRC_PAD (self), new_caps);
533 GST_DEBUG_OBJECT (self, "Setting new caps: %" GST_PTR_FORMAT, new_caps);
535 gst_caps_unref (new_caps);
540 gst_image_sequence_src_set_duration (GstImageSequenceSrc * self)
542 GstClockTime old_duration = self->duration;
544 if (self->n_frames <= 0)
547 /* Calculate duration */
549 gst_util_uint64_scale (GST_SECOND * self->n_frames, self->fps_d,
552 if (self->duration != old_duration) {
554 gst_element_post_message (GST_ELEMENT (self),
555 gst_message_new_duration_changed (GST_OBJECT (self)));
562 gst_image_sequence_src_get_filename (GstImageSequenceSrc * self)
566 GST_DEBUG ("Reading filename at index %d.", self->index);
567 filename = g_strdup_printf (self->path, self->index);
573 gst_image_sequence_src_create (GstPushSrc * src, GstBuffer ** buffer)
575 GstImageSequenceSrc *self;
581 GError *error = NULL;
582 gint fps_n, fps_d, start_index, stop_index;
584 self = GST_IMAGE_SEQUENCE_SRC (src);
587 start_index = self->start_index;
588 stop_index = self->stop_index;
589 if (self->index > stop_index && stop_index > 0) {
595 if (self->index < self->start_index)
596 self->index = self->start_index;
598 g_assert (start_index <= self->index &&
599 (self->index <= stop_index || stop_index <= 0));
601 filename = gst_image_sequence_src_get_filename (self);
609 ret = g_file_get_contents (filename, &data, &size, &error);
613 buf = gst_buffer_new_wrapped (data, size);
617 caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
619 GST_ELEMENT_ERROR (self, STREAM, TYPE_NOT_FOUND, (NULL),
620 ("Could not determine image type."));
622 return GST_FLOW_NOT_SUPPORTED;
626 gst_image_sequence_src_count_frames (self, TRUE);
627 gst_image_sequence_src_set_duration (self);
630 gst_image_sequence_src_set_caps (self, caps);
631 gst_caps_unref (caps);
634 GST_BUFFER_PTS (buf) =
635 gst_util_uint64_scale_ceil ((self->index - start_index) * GST_SECOND,
637 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
638 GST_BUFFER_OFFSET (buf) = self->index - start_index;
639 GST_LOG_OBJECT (self, "index: %d, %s - %" GST_PTR_FORMAT, self->index,
645 self->index += self->reverse ? -1 : 1;
651 GST_ELEMENT_ERROR (self, RESOURCE, READ,
652 ("Error while reading from file \"%s\".", filename),
653 ("%s", error->message));
654 g_error_free (error);
656 GST_ELEMENT_ERROR (self, RESOURCE, READ,
657 ("Error while reading from file \"%s\".", filename),
658 ("%s", g_strerror (errno)));
661 return GST_FLOW_ERROR;