2 * Copyright (c) 2005 Edward Hervey <bilboed@bilboed.com>
3 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-imagefreeze
24 * The imagefreeze element generates a still frame video stream from
25 * the input. It duplicates the first frame with the framerate requested
26 * by downstream, allows seeking and answers queries.
29 * <title>Example launch line</title>
31 * gst-launch -v filesrc location=some.png ! decodebin2 ! imagefreeze ! autovideosink
32 * ]| This pipeline shows a still frame stream of a PNG file.
36 /* This is based on the imagefreeze element from PiTiVi:
37 * http://git.gnome.org/browse/pitivi/tree/pitivi/elements/imagefreeze.py
44 #include <gst/glib-compat-private.h>
46 #include "gstimagefreeze.h"
48 static void gst_image_freeze_finalize (GObject * object);
50 static void gst_image_freeze_reset (GstImageFreeze * self);
52 static GstStateChangeReturn gst_image_freeze_change_state (GstElement * element,
53 GstStateChange transition);
55 static GstFlowReturn gst_image_freeze_sink_chain (GstPad * pad,
56 GstObject * parent, GstBuffer * buffer);
57 static gboolean gst_image_freeze_sink_event (GstPad * pad, GstObject * parent,
59 static gboolean gst_image_freeze_sink_setcaps (GstImageFreeze * self,
61 static GstCaps *gst_image_freeze_sink_getcaps (GstImageFreeze * self,
63 static gboolean gst_image_freeze_sink_query (GstPad * pad, GstObject * parent,
65 static void gst_image_freeze_src_loop (GstPad * pad);
66 static gboolean gst_image_freeze_src_event (GstPad * pad, GstObject * parent,
68 static gboolean gst_image_freeze_src_query (GstPad * pad, GstObject * parent,
71 static GstStaticPadTemplate sink_pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
74 GST_STATIC_CAPS ("video/x-raw"));
76 static GstStaticPadTemplate src_pad_template =
77 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
78 GST_STATIC_CAPS ("video/x-raw"));
80 GST_DEBUG_CATEGORY_STATIC (gst_image_freeze_debug);
81 #define GST_CAT_DEFAULT gst_image_freeze_debug
83 #define gst_image_freeze_parent_class parent_class
84 G_DEFINE_TYPE (GstImageFreeze, gst_image_freeze, GST_TYPE_ELEMENT);
88 gst_image_freeze_class_init (GstImageFreezeClass * klass)
90 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
93 gobject_class->finalize = gst_image_freeze_finalize;
95 gstelement_class->change_state =
96 GST_DEBUG_FUNCPTR (gst_image_freeze_change_state);
98 gst_element_class_set_details_simple (gstelement_class,
99 "Still frame stream generator",
101 "Generates a still frame stream from an image",
102 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
104 gst_element_class_add_pad_template (gstelement_class,
105 gst_static_pad_template_get (&sink_pad_template));
106 gst_element_class_add_pad_template (gstelement_class,
107 gst_static_pad_template_get (&src_pad_template));
111 gst_image_freeze_init (GstImageFreeze * self)
113 self->sinkpad = gst_pad_new_from_static_template (&sink_pad_template, "sink");
114 gst_pad_set_chain_function (self->sinkpad,
115 GST_DEBUG_FUNCPTR (gst_image_freeze_sink_chain));
116 gst_pad_set_event_function (self->sinkpad,
117 GST_DEBUG_FUNCPTR (gst_image_freeze_sink_event));
118 gst_pad_set_query_function (self->sinkpad,
119 GST_DEBUG_FUNCPTR (gst_image_freeze_sink_query));
120 GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
121 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
123 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
124 gst_pad_set_event_function (self->srcpad,
125 GST_DEBUG_FUNCPTR (gst_image_freeze_src_event));
126 gst_pad_set_query_function (self->srcpad,
127 GST_DEBUG_FUNCPTR (gst_image_freeze_src_query));
128 gst_pad_use_fixed_caps (self->srcpad);
129 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
131 g_mutex_init (&self->lock);
133 gst_image_freeze_reset (self);
137 gst_image_freeze_finalize (GObject * object)
139 GstImageFreeze *self = GST_IMAGE_FREEZE (object);
141 gst_image_freeze_reset (self);
143 g_mutex_clear (&self->lock);
145 G_OBJECT_CLASS (parent_class)->finalize (object);
149 gst_image_freeze_reset (GstImageFreeze * self)
151 GST_DEBUG_OBJECT (self, "Resetting internal state");
153 g_mutex_lock (&self->lock);
154 gst_buffer_replace (&self->buffer, NULL);
156 gst_segment_init (&self->segment, GST_FORMAT_TIME);
157 self->need_segment = TRUE;
159 self->fps_n = self->fps_d = 0;
161 g_mutex_unlock (&self->lock);
163 g_atomic_int_set (&self->seeking, 0);
167 gst_image_freeze_sink_setcaps (GstImageFreeze * self, GstCaps * caps)
169 gboolean ret = FALSE;
172 GstCaps *othercaps, *intersection;
177 caps = gst_caps_make_writable (gst_caps_ref (caps));
179 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
181 s = gst_caps_get_structure (caps, 0);
183 /* 1. Remove framerate */
184 gst_structure_remove_field (s, "framerate");
185 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1,
188 /* 2. Intersect with template caps */
189 othercaps = (GstCaps *) gst_pad_get_pad_template_caps (pad);
190 intersection = gst_caps_intersect (caps, othercaps);
191 GST_DEBUG_OBJECT (pad, "Intersecting: %" GST_PTR_FORMAT, caps);
192 GST_DEBUG_OBJECT (pad, "with: %" GST_PTR_FORMAT, othercaps);
193 GST_DEBUG_OBJECT (pad, "gave: %" GST_PTR_FORMAT, intersection);
194 gst_caps_unref (caps);
196 intersection = othercaps = NULL;
198 /* 3. Intersect with downstream peer caps */
199 othercaps = gst_pad_peer_query_caps (self->srcpad, NULL);
201 intersection = gst_caps_intersect (caps, othercaps);
202 GST_DEBUG_OBJECT (pad, "Intersecting: %" GST_PTR_FORMAT, caps);
203 GST_DEBUG_OBJECT (pad, "with: %" GST_PTR_FORMAT, othercaps);
204 GST_DEBUG_OBJECT (pad, "gave: %" GST_PTR_FORMAT, intersection);
205 gst_caps_unref (othercaps);
206 gst_caps_unref (caps);
208 intersection = othercaps = NULL;
211 /* 4. For every candidate check if it's accepted downstream
212 * and fixate framerate to nearest 25/1 */
213 n = gst_caps_get_size (caps);
214 for (i = 0; i < n; i++) {
215 GstCaps *candidate = gst_caps_new_empty ();
216 GstStructure *s = gst_structure_copy (gst_caps_get_structure (caps, i));
218 gst_caps_append_structure (candidate, s);
219 if (gst_pad_peer_query_accept_caps (self->srcpad, candidate)) {
220 if (gst_structure_has_field_typed (s, "framerate", GST_TYPE_FRACTION) ||
221 gst_structure_fixate_field_nearest_fraction (s, "framerate", 25, 1)) {
222 gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d);
224 g_mutex_lock (&self->lock);
227 g_mutex_unlock (&self->lock);
228 GST_DEBUG_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, candidate);
229 gst_pad_set_caps (self->srcpad, candidate);
230 gst_caps_unref (candidate);
234 GST_WARNING_OBJECT (pad, "Invalid caps with framerate %d/%d", fps_n,
239 gst_caps_unref (candidate);
244 GST_ERROR_OBJECT (pad, "No usable caps found");
246 gst_caps_unref (caps);
251 /* remove framerate in writable @caps */
253 gst_image_freeze_remove_fps (GstImageFreeze * self, GstCaps * caps)
257 n = gst_caps_get_size (caps);
258 for (i = 0; i < n; i++) {
259 GstStructure *s = gst_caps_get_structure (caps, i);
261 gst_structure_remove_field (s, "framerate");
262 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
268 gst_image_freeze_sink_getcaps (GstImageFreeze * self, GstCaps * filter)
270 GstCaps *ret, *tmp, *templ;
274 if (gst_pad_has_current_caps (pad)) {
275 ret = gst_pad_get_current_caps (pad);
280 filter = gst_caps_copy (filter);
281 gst_image_freeze_remove_fps (self, filter);
283 templ = gst_pad_get_pad_template_caps (pad);
284 tmp = gst_pad_peer_query_caps (self->srcpad, filter);
286 GST_LOG_OBJECT (self, "peer caps %" GST_PTR_FORMAT, tmp);
287 ret = gst_caps_intersect (tmp, templ);
288 gst_caps_unref (tmp);
290 GST_LOG_OBJECT (self, "going to copy");
291 ret = gst_caps_copy (templ);
293 gst_caps_unref (templ);
295 gst_caps_unref (filter);
297 ret = gst_caps_make_writable (ret);
298 gst_image_freeze_remove_fps (self, ret);
301 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
307 gst_image_freeze_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
309 GstImageFreeze *self = GST_IMAGE_FREEZE (parent);
312 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
313 gst_query_type_get_name (GST_QUERY_TYPE (query)));
315 switch (GST_QUERY_TYPE (query)) {
320 gst_query_parse_caps (query, &caps);
321 caps = gst_image_freeze_sink_getcaps (self, caps);
322 gst_query_set_caps_result (query, caps);
323 gst_caps_unref (caps);
328 ret = gst_pad_query_default (pad, parent, query);
335 gst_image_freeze_convert (GstImageFreeze * self,
336 GstFormat src_format, gint64 src_value,
337 GstFormat * dest_format, gint64 * dest_value)
339 gboolean ret = FALSE;
341 if (src_format == *dest_format) {
342 *dest_value = src_value;
346 if (src_value == -1) {
351 switch (src_format) {
352 case GST_FORMAT_DEFAULT:{
353 switch (*dest_format) {
354 case GST_FORMAT_TIME:
355 g_mutex_lock (&self->lock);
356 if (self->fps_n == 0)
360 gst_util_uint64_scale (src_value, GST_SECOND * self->fps_d,
362 g_mutex_unlock (&self->lock);
370 case GST_FORMAT_TIME:{
371 switch (*dest_format) {
372 case GST_FORMAT_DEFAULT:
373 g_mutex_lock (&self->lock);
375 gst_util_uint64_scale (src_value, self->fps_n,
376 self->fps_d * GST_SECOND);
377 g_mutex_unlock (&self->lock);
393 gst_image_freeze_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
395 GstImageFreeze *self = GST_IMAGE_FREEZE (parent);
396 gboolean ret = FALSE;
398 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
399 gst_query_type_get_name (GST_QUERY_TYPE (query)));
401 switch (GST_QUERY_TYPE (query)) {
402 case GST_QUERY_CONVERT:{
403 GstFormat src_format, dest_format;
404 gint64 src_value, dest_value;
406 gst_query_parse_convert (query, &src_format, &src_value, &dest_format,
409 gst_image_freeze_convert (self, src_format, src_value, &dest_format,
412 gst_query_set_convert (query, src_format, src_value, dest_format,
416 case GST_QUERY_POSITION:{
420 gst_query_parse_position (query, &format, NULL);
422 case GST_FORMAT_DEFAULT:{
423 g_mutex_lock (&self->lock);
424 position = self->offset;
425 g_mutex_unlock (&self->lock);
429 case GST_FORMAT_TIME:{
430 g_mutex_lock (&self->lock);
431 position = self->segment.position;
432 g_mutex_unlock (&self->lock);
441 gst_query_set_position (query, format, position);
442 GST_DEBUG_OBJECT (pad,
443 "Returning position %" G_GINT64_FORMAT " in format %s", position,
444 gst_format_get_name (format));
446 GST_DEBUG_OBJECT (pad, "Position query failed");
450 case GST_QUERY_DURATION:{
454 gst_query_parse_duration (query, &format, NULL);
456 case GST_FORMAT_TIME:{
457 g_mutex_lock (&self->lock);
458 duration = self->segment.stop;
459 g_mutex_unlock (&self->lock);
463 case GST_FORMAT_DEFAULT:{
464 g_mutex_lock (&self->lock);
465 duration = self->segment.stop;
468 gst_util_uint64_scale (duration, self->fps_n,
469 GST_SECOND * self->fps_d);
470 g_mutex_unlock (&self->lock);
479 gst_query_set_duration (query, format, duration);
480 GST_DEBUG_OBJECT (pad,
481 "Returning duration %" G_GINT64_FORMAT " in format %s", duration,
482 gst_format_get_name (format));
484 GST_DEBUG_OBJECT (pad, "Duration query failed");
488 case GST_QUERY_SEEKING:{
492 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
493 seekable = (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT);
495 gst_query_set_seeking (query, format, seekable, (seekable ? 0 : -1), -1);
509 gst_image_freeze_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
511 GstImageFreeze *self = GST_IMAGE_FREEZE (parent);
514 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
516 switch (GST_EVENT_TYPE (event)) {
521 gst_event_parse_caps (event, &caps);
522 gst_image_freeze_sink_setcaps (self, caps);
523 gst_event_unref (event);
529 /* if we receive EOS before a buffer arrives, then let it pass */
530 GST_DEBUG_OBJECT (self, "EOS without input buffer, passing on");
531 ret = gst_pad_push_event (self->srcpad, event);
535 case GST_EVENT_SEGMENT:
536 GST_DEBUG_OBJECT (pad, "Dropping event");
537 gst_event_unref (event);
540 case GST_EVENT_FLUSH_START:
541 gst_image_freeze_reset (self);
544 ret = gst_pad_push_event (self->srcpad, event);
552 gst_image_freeze_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
554 GstImageFreeze *self = GST_IMAGE_FREEZE (parent);
557 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
559 switch (GST_EVENT_TYPE (event)) {
560 case GST_EVENT_NAVIGATION:
562 case GST_EVENT_LATENCY:
564 GST_DEBUG_OBJECT (pad, "Dropping event");
565 gst_event_unref (event);
568 case GST_EVENT_SEEK:{
572 GstSeekType start_type, stop_type;
578 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
580 gst_event_unref (event);
582 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
584 if (format != GST_FORMAT_TIME && format != GST_FORMAT_DEFAULT) {
585 GST_ERROR_OBJECT (pad, "Seek in invalid format: %s",
586 gst_format_get_name (format));
591 if (format == GST_FORMAT_DEFAULT) {
592 format = GST_FORMAT_TIME;
593 if (!gst_image_freeze_convert (self, GST_FORMAT_DEFAULT, start, &format,
595 || !gst_image_freeze_convert (self, GST_FORMAT_DEFAULT, stop,
597 || start == -1 || stop == -1) {
598 GST_ERROR_OBJECT (pad,
599 "Failed to convert seek from DEFAULT format into TIME format");
608 g_atomic_int_set (&self->seeking, 1);
609 e = gst_event_new_flush_start ();
610 gst_pad_push_event (self->srcpad, e);
612 gst_pad_pause_task (self->srcpad);
615 GST_PAD_STREAM_LOCK (self->srcpad);
617 g_mutex_lock (&self->lock);
619 gst_segment_do_seek (&self->segment, rate, format, flags, start_type,
620 start, stop_type, stop, NULL);
621 self->need_segment = TRUE;
622 last_stop = self->segment.position;
624 start_task = self->buffer != NULL;
625 g_mutex_unlock (&self->lock);
630 e = gst_event_new_flush_stop (TRUE);
631 gst_pad_push_event (self->srcpad, e);
632 g_atomic_int_set (&self->seeking, 0);
635 if (flags & GST_SEEK_FLAG_SEGMENT) {
638 m = gst_message_new_segment_start (GST_OBJECT (self),
640 gst_element_post_message (GST_ELEMENT (self), m);
643 GST_PAD_STREAM_UNLOCK (self->srcpad);
645 GST_DEBUG_OBJECT (pad, "Seek successful");
648 g_mutex_lock (&self->lock);
650 if (self->buffer != NULL)
651 gst_pad_start_task (self->srcpad,
652 (GstTaskFunction) gst_image_freeze_src_loop, self->srcpad);
654 g_mutex_unlock (&self->lock);
660 case GST_EVENT_FLUSH_START:
661 gst_image_freeze_reset (self);
664 ret = gst_pad_push_event (self->sinkpad, event);
672 gst_image_freeze_sink_chain (GstPad * pad, GstObject * parent,
675 GstImageFreeze *self = GST_IMAGE_FREEZE (parent);
677 g_mutex_lock (&self->lock);
679 GST_DEBUG_OBJECT (pad, "Already have a buffer, dropping");
680 gst_buffer_unref (buffer);
681 g_mutex_unlock (&self->lock);
685 self->buffer = buffer;
687 gst_pad_start_task (self->srcpad, (GstTaskFunction) gst_image_freeze_src_loop,
689 g_mutex_unlock (&self->lock);
694 gst_image_freeze_src_loop (GstPad * pad)
696 GstImageFreeze *self = GST_IMAGE_FREEZE (GST_PAD_PARENT (pad));
699 GstClockTime timestamp, timestamp_end;
700 guint64 cstart, cstop;
701 gboolean in_seg, eos;
703 g_mutex_lock (&self->lock);
705 GST_ERROR_OBJECT (pad, "Have no buffer yet");
706 g_mutex_unlock (&self->lock);
707 gst_pad_pause_task (self->srcpad);
710 buffer = gst_buffer_ref (self->buffer);
711 buffer = gst_buffer_make_writable (buffer);
712 g_mutex_unlock (&self->lock);
714 if (self->need_segment) {
717 GST_DEBUG_OBJECT (pad, "Pushing NEWSEGMENT event: %" GST_SEGMENT_FORMAT,
719 e = gst_event_new_segment (&self->segment);
721 g_mutex_lock (&self->lock);
722 if (self->segment.rate >= 0) {
724 gst_util_uint64_scale (self->segment.start, self->fps_n,
725 self->fps_d * GST_SECOND);
728 gst_util_uint64_scale (self->segment.stop, self->fps_n,
729 self->fps_d * GST_SECOND);
731 g_mutex_unlock (&self->lock);
733 self->need_segment = FALSE;
735 gst_pad_push_event (self->srcpad, e);
738 g_mutex_lock (&self->lock);
739 offset = self->offset;
741 if (self->fps_n != 0) {
743 gst_util_uint64_scale (offset, self->fps_d * GST_SECOND, self->fps_n);
745 gst_util_uint64_scale (offset + 1, self->fps_d * GST_SECOND,
748 timestamp = self->segment.start;
749 timestamp_end = GST_CLOCK_TIME_NONE;
752 eos = (self->fps_n == 0 && offset > 0) ||
753 (self->segment.rate >= 0 && self->segment.stop != -1
754 && timestamp > self->segment.stop) || (self->segment.rate < 0
755 && offset == 0) || (self->segment.rate < 0
756 && self->segment.start != -1 && timestamp_end < self->segment.start);
758 if (self->fps_n == 0 && offset > 0)
762 gst_segment_clip (&self->segment, GST_FORMAT_TIME, timestamp,
763 timestamp_end, &cstart, &cstop);
766 self->segment.position = cstart;
768 if (self->segment.rate >= 0)
772 g_mutex_unlock (&self->lock);
774 GST_DEBUG_OBJECT (pad, "Handling buffer with timestamp %" GST_TIME_FORMAT,
775 GST_TIME_ARGS (timestamp));
780 GST_BUFFER_TIMESTAMP (buffer) = cstart;
781 GST_BUFFER_DURATION (buffer) = cstop - cstart;
782 GST_BUFFER_OFFSET (buffer) = offset;
783 GST_BUFFER_OFFSET_END (buffer) = offset + 1;
784 ret = gst_pad_push (self->srcpad, buffer);
785 GST_DEBUG_OBJECT (pad, "Pushing buffer resulted in %s",
786 gst_flow_get_name (ret));
787 if (ret != GST_FLOW_OK)
788 gst_pad_pause_task (self->srcpad);
790 gst_buffer_unref (buffer);
794 if ((self->segment.flags & GST_SEEK_FLAG_SEGMENT)) {
797 GST_DEBUG_OBJECT (pad, "Sending segment done at end of segment");
798 if (self->segment.rate >= 0)
799 m = gst_message_new_segment_done (GST_OBJECT_CAST (self),
800 GST_FORMAT_TIME, self->segment.stop);
802 m = gst_message_new_segment_done (GST_OBJECT_CAST (self),
803 GST_FORMAT_TIME, self->segment.start);
804 gst_element_post_message (GST_ELEMENT_CAST (self), m);
806 GST_DEBUG_OBJECT (pad, "Sending EOS at end of segment");
807 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
809 gst_pad_pause_task (self->srcpad);
813 static GstStateChangeReturn
814 gst_image_freeze_change_state (GstElement * element, GstStateChange transition)
816 GstImageFreeze *self = GST_IMAGE_FREEZE (element);
817 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
819 switch (transition) {
820 case GST_STATE_CHANGE_READY_TO_PAUSED:
821 gst_image_freeze_reset (self);
823 case GST_STATE_CHANGE_PAUSED_TO_READY:
824 gst_pad_stop_task (self->srcpad);
825 gst_image_freeze_reset (self);
831 if (GST_ELEMENT_CLASS (parent_class)->change_state)
832 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
834 switch (transition) {
843 plugin_init (GstPlugin * plugin)
845 GST_DEBUG_CATEGORY_INIT (gst_image_freeze_debug, "imagefreeze", 0,
846 "imagefreeze element");
848 if (!gst_element_register (plugin, "imagefreeze", GST_RANK_NONE,
849 GST_TYPE_IMAGE_FREEZE))
855 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
858 "Still frame stream generator",
859 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)