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>
49 #include <glib/gi18n-lib.h>
51 #include "gstimagesequencesrc.h"
53 #define LOCK(self) (g_rec_mutex_lock (&self->fields_lock))
54 #define UNLOCK(self) (g_rec_mutex_unlock (&self->fields_lock))
56 static GstFlowReturn gst_image_sequence_src_create (GstPushSrc * src,
60 static void gst_image_sequence_src_set_property (GObject * object,
61 guint prop_id, const GValue * value, GParamSpec * pspec);
62 static void gst_image_sequence_src_get_property (GObject * object,
63 guint prop_id, GValue * value, GParamSpec * pspec);
64 static GstCaps *gst_image_sequence_src_getcaps (GstBaseSrc * src,
66 static gboolean gst_image_sequence_src_query (GstBaseSrc * src,
68 static void gst_image_sequence_src_set_caps (GstImageSequenceSrc * self,
70 static void gst_image_sequence_src_set_duration (GstImageSequenceSrc * self);
71 static gint gst_image_sequence_src_count_frames (GstImageSequenceSrc * self,
75 static GstStaticPadTemplate gst_image_sequence_src_pad_template =
76 GST_STATIC_PAD_TEMPLATE ("src",
81 GST_DEBUG_CATEGORY_STATIC (gst_image_sequence_src_debug);
82 #define GST_CAT_DEFAULT gst_image_sequence_src_debug
93 #define DEFAULT_LOCATION "%05d"
94 #define DEFAULT_START_INDEX 0
95 #define DEFAULT_STOP_INDEX -1
96 #define DEFAULT_FRAMERATE 30
98 /* Call with LOCK taken */
100 gst_image_sequence_src_set_location (GstImageSequenceSrc * self,
101 const gchar * location)
104 if (location != NULL)
105 self->path = g_strdup (location);
112 /*** GSTURIHANDLER INTERFACE *************************************************/
115 gst_image_sequence_src_uri_get_type (GType type)
120 static const gchar *const *
121 gst_image_sequence_src_uri_get_protocols (GType type)
123 static const gchar *protocols[] = { "imagesequence", NULL };
129 gst_image_sequence_src_uri_get_uri (GstURIHandler * handler)
131 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (handler);
136 uri = gst_uri_to_string (self->uri);
138 uri = gst_uri_construct ("imagesequence", self->path);
145 gst_image_sequence_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
148 gchar *hostname = NULL, *location = NULL, *path, *tmp;
149 gboolean ret = FALSE;
150 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (handler);
151 GstUri *ruri = gst_uri_from_string (uri);
152 GHashTable *query = NULL;
155 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
156 "imagesequencesrc URI is invalid: '%s'", uri);
162 g_clear_pointer (&self->uri, gst_uri_unref);
164 path = gst_uri_get_path (ruri);
165 tmp = gst_filename_to_uri (path, err);
166 location = g_filename_from_uri (tmp, &hostname, err);
169 query = gst_uri_get_query_table (ruri);
170 if (!location || (err != NULL && *err != NULL)) {
171 GST_WARNING_OBJECT (self, "Invalid URI '%s' for imagesequencesrc: %s", uri,
172 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
176 if (hostname && strcmp (hostname, "localhost")) {
177 /* Only 'localhost' is permitted */
178 GST_WARNING_OBJECT (self, "Invalid hostname '%s' for filesrc", hostname);
179 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
180 "File URI with invalid hostname '%s'", hostname);
184 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
185 * correctly on windows, it leaves them with an extra backslash
186 * at the start if they're of the mozilla-style file://///host/path/file
187 * form. Correct this.
189 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
190 memmove (location, location + 1, strlen (location + 1) + 1);
193 ret = gst_image_sequence_src_set_location (self, location);
199 g_hash_table_iter_init (&iter, query);
200 while (g_hash_table_iter_next (&iter, &key, &value)) {
201 GST_INFO_OBJECT (self, "Setting property from URI: %s=%s", (gchar *) key,
203 gst_util_set_object_arg (G_OBJECT (self), key, value);
212 g_clear_pointer (&query, g_hash_table_unref);
218 gst_image_sequence_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
220 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
222 iface->get_type = gst_image_sequence_src_uri_get_type;
223 iface->get_protocols = gst_image_sequence_src_uri_get_protocols;
224 iface->get_uri = gst_image_sequence_src_uri_get_uri;
225 iface->set_uri = gst_image_sequence_src_uri_set_uri;
228 #define gst_image_sequence_src_parent_class parent_class
230 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_image_sequence_src_uri_handler_init); \
231 GST_DEBUG_CATEGORY_INIT (gst_image_sequence_src_debug, "imagesequencesrc", \
232 0, "imagesequencesrc element");
233 G_DEFINE_TYPE_WITH_CODE (GstImageSequenceSrc, gst_image_sequence_src,
234 GST_TYPE_PUSH_SRC, _do_init);
235 GST_ELEMENT_REGISTER_DEFINE (imagesequencesrc, "imagesequencesrc",
236 GST_RANK_NONE, gst_image_sequence_src_get_type ());
239 is_seekable (GstBaseSrc * src)
241 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (src);
243 if ((self->n_frames != 0) && (self->fps_n) && (self->fps_d))
250 do_seek (GstBaseSrc * bsrc, GstSegment * segment)
252 GstImageSequenceSrc *self;
254 self = GST_IMAGE_SEQUENCE_SRC (bsrc);
256 self->reverse = segment->rate < 0;
258 segment->time = segment->start;
263 segment->position * self->fps_n / (self->fps_d * GST_SECOND);
269 gst_image_sequence_src_finalize (GObject * object)
271 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
273 g_clear_pointer (&self->path, g_free);
274 g_rec_mutex_clear (&self->fields_lock);
276 G_OBJECT_CLASS (parent_class)->finalize (object);
280 gst_image_sequence_src_dispose (GObject * object)
282 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
284 gst_clear_caps (&self->caps);
285 g_clear_pointer (&self->uri, gst_uri_unref);
287 G_OBJECT_CLASS (parent_class)->dispose (object);
291 gst_image_sequence_src_class_init (GstImageSequenceSrcClass * klass)
293 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
294 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
295 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
296 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
298 gobject_class->set_property = gst_image_sequence_src_set_property;
299 gobject_class->get_property = gst_image_sequence_src_get_property;
302 g_object_class_install_property (gobject_class, PROP_LOCATION,
303 g_param_spec_string ("location", "File Location",
304 "Pattern to create file names of input files. File names are "
305 "created by calling sprintf() with the pattern and the current "
306 "index.", DEFAULT_LOCATION,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_START_INDEX,
309 g_param_spec_int ("start-index", "Start Index",
310 "Start value of index. The initial value of index can be set "
311 "either by setting index or start-index. When the end of the loop "
312 "is reached, the index will be set to the value start-index.",
313 0, INT_MAX, DEFAULT_START_INDEX,
314 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_STOP_INDEX,
316 g_param_spec_int ("stop-index", "Stop Index",
317 "Stop value of index. The special value -1 means no stop.",
318 -1, INT_MAX, DEFAULT_STOP_INDEX,
319 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (gobject_class, PROP_FRAMERATE,
321 gst_param_spec_fraction ("framerate", "Framerate",
322 "The output framerate.",
323 1, 1, G_MAXINT, 1, DEFAULT_FRAMERATE, 1,
324 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
326 gobject_class->finalize = gst_image_sequence_src_finalize;
327 gobject_class->dispose = gst_image_sequence_src_dispose;
329 gstbasesrc_class->get_caps = gst_image_sequence_src_getcaps;
330 gstbasesrc_class->query = gst_image_sequence_src_query;
331 gstbasesrc_class->is_seekable = is_seekable;
332 gstbasesrc_class->do_seek = do_seek;
334 gstpushsrc_class->create = gst_image_sequence_src_create;
336 gst_element_class_add_static_pad_template (gstelement_class,
337 &gst_image_sequence_src_pad_template);
338 gst_element_class_set_static_metadata (gstelement_class,
339 "Image Sequence Source", "Source/File/Video",
340 "Create a video stream from a sequence of image files",
341 "Cesar Fabian Orccon Chipana <cfoch.fabian@gmail.com>\n"
342 "Thibault Saunier <tsaunier@igalia.com>");
346 gst_image_sequence_src_init (GstImageSequenceSrc * self)
350 GST_DEBUG_CATEGORY_INIT (gst_image_sequence_src_debug, "imagesequencesrc", 0,
351 "imagesequencesrc element");
353 bsrc = GST_BASE_SRC (self);
354 gst_base_src_set_format (bsrc, GST_FORMAT_TIME);
356 g_rec_mutex_init (&self->fields_lock);
357 self->start_index = DEFAULT_START_INDEX;
359 self->stop_index = DEFAULT_STOP_INDEX;
360 self->path = g_strdup (DEFAULT_LOCATION);
368 gst_image_sequence_src_getcaps (GstBaseSrc * src, GstCaps * filter)
370 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (src);
372 GST_DEBUG_OBJECT (self, "returning %" GST_PTR_FORMAT, self->caps);
376 return gst_caps_intersect_full (filter, self->caps,
377 GST_CAPS_INTERSECT_FIRST);
379 return gst_caps_ref (filter);
382 return gst_caps_new_any ();
386 gst_image_sequence_src_query (GstBaseSrc * bsrc, GstQuery * query)
389 GstImageSequenceSrc *self;
391 self = GST_IMAGE_SEQUENCE_SRC (bsrc);
393 switch (GST_QUERY_TYPE (query)) {
394 case GST_QUERY_DURATION:
398 gst_query_parse_duration (query, &format, NULL);
401 case GST_FORMAT_TIME:
403 if (self->n_frames <= 0) {
404 gst_image_sequence_src_count_frames (self, FALSE);
405 gst_image_sequence_src_set_duration (self);
408 if (self->n_frames > 0)
409 gst_query_set_duration (query, format, self->duration);
415 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
420 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
428 gst_image_sequence_src_set_property (GObject * object, guint prop_id,
429 const GValue * value, GParamSpec * pspec)
431 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
436 gst_image_sequence_src_set_location (self, g_value_get_string (value));
438 case PROP_START_INDEX:
439 self->start_index = g_value_get_int (value);
440 gst_image_sequence_src_count_frames (self, FALSE);
442 case PROP_STOP_INDEX:
443 self->stop_index = g_value_get_int (value);
444 gst_image_sequence_src_count_frames (self, FALSE);
447 self->fps_n = gst_value_get_fraction_numerator (value);
448 self->fps_d = gst_value_get_fraction_denominator (value);
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gst_image_sequence_src_get_property (GObject * object, guint prop_id,
459 GValue * value, GParamSpec * pspec)
461 GstImageSequenceSrc *self = GST_IMAGE_SEQUENCE_SRC (object);
466 g_value_set_string (value, self->path);
468 case PROP_START_INDEX:
469 g_value_set_int (value, self->start_index);
471 case PROP_STOP_INDEX:
472 g_value_set_int (value, self->stop_index);
475 self->fps_n = gst_value_get_fraction_numerator (value);
476 self->fps_d = gst_value_get_fraction_denominator (value);
477 GST_DEBUG_OBJECT (self, "Set (framerate) property to (%d/%d)",
478 self->fps_n, self->fps_d);
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
489 gst_image_sequence_src_count_frames (GstImageSequenceSrc * self,
492 if (can_read && self->stop_index < 0 && self->path) {
495 for (i = self->start_index;; i++) {
496 gchar *filename = g_strdup_printf (self->path, i);
498 if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
506 if (i > self->start_index)
507 self->stop_index = i;
510 if (self->stop_index >= self->start_index)
511 self->n_frames = self->stop_index - self->start_index + 1;
512 return self->n_frames;
516 gst_image_sequence_src_set_caps (GstImageSequenceSrc * self, GstCaps * caps)
520 g_assert (caps != NULL);
521 new_caps = gst_caps_copy (caps);
523 if (self->n_frames > 0) {
524 GValue fps = G_VALUE_INIT;
525 g_value_init (&fps, GST_TYPE_FRACTION);
526 gst_value_set_fraction (&fps, self->fps_n, self->fps_d);
527 gst_caps_set_value (new_caps, "framerate", &fps);
528 g_value_unset (&fps);
531 gst_caps_replace (&self->caps, new_caps);
532 gst_pad_set_caps (GST_BASE_SRC_PAD (self), new_caps);
534 GST_DEBUG_OBJECT (self, "Setting new caps: %" GST_PTR_FORMAT, new_caps);
536 gst_caps_unref (new_caps);
541 gst_image_sequence_src_set_duration (GstImageSequenceSrc * self)
543 GstClockTime old_duration = self->duration;
545 if (self->n_frames <= 0)
548 /* Calculate duration */
550 gst_util_uint64_scale (GST_SECOND * self->n_frames, self->fps_d,
553 if (self->duration != old_duration) {
555 gst_element_post_message (GST_ELEMENT (self),
556 gst_message_new_duration_changed (GST_OBJECT (self)));
563 gst_image_sequence_src_get_filename (GstImageSequenceSrc * self)
567 GST_DEBUG ("Reading filename at index %d.", self->index);
568 if (self->path != NULL) {
569 filename = g_strdup_printf (self->path, self->index);
571 GST_WARNING_OBJECT (self, "No filename location set!");
579 gst_image_sequence_src_create (GstPushSrc * src, GstBuffer ** buffer)
581 GstImageSequenceSrc *self;
587 GError *error = NULL;
588 gint fps_n, fps_d, start_index, stop_index;
590 self = GST_IMAGE_SEQUENCE_SRC (src);
593 start_index = self->start_index;
594 stop_index = self->stop_index;
595 if (self->index > stop_index && stop_index > 0) {
601 if (self->index < self->start_index)
602 self->index = self->start_index;
604 g_assert (start_index <= self->index &&
605 (self->index <= stop_index || stop_index <= 0));
607 filename = gst_image_sequence_src_get_filename (self);
613 goto error_no_filename;
615 ret = g_file_get_contents (filename, &data, &size, &error);
619 buf = gst_buffer_new_wrapped (data, size);
623 caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
625 GST_ELEMENT_ERROR (self, STREAM, TYPE_NOT_FOUND, (NULL),
626 ("Could not determine image type."));
628 return GST_FLOW_NOT_SUPPORTED;
632 gst_image_sequence_src_count_frames (self, TRUE);
633 gst_image_sequence_src_set_duration (self);
636 gst_image_sequence_src_set_caps (self, caps);
637 gst_caps_unref (caps);
640 GST_BUFFER_PTS (buf) =
641 gst_util_uint64_scale_ceil ((self->index - start_index) * GST_SECOND,
643 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
644 GST_BUFFER_OFFSET (buf) = self->index - start_index;
645 GST_LOG_OBJECT (self, "index: %d, %s - %" GST_PTR_FORMAT, self->index,
651 self->index += self->reverse ? -1 : 1;
656 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
657 (_("No file name specified for reading.")), (NULL));
658 return GST_FLOW_ERROR;
663 GST_ELEMENT_ERROR (self, RESOURCE, READ,
664 ("Error while reading from file \"%s\".", filename),
665 ("%s", error->message));
666 g_error_free (error);
668 GST_ELEMENT_ERROR (self, RESOURCE, READ,
669 ("Error while reading from file \"%s\".", filename),
670 ("%s", g_strerror (errno)));
673 return GST_FLOW_ERROR;