1 /* GStreamer
2  * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26 
27 #include "gstvideosegmentclip.h"
28 
29 static GstStaticPadTemplate sink_pad_template =
30 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
31     GST_STATIC_CAPS ("video/x-raw, framerate=" GST_VIDEO_FPS_RANGE));
32 
33 static GstStaticPadTemplate src_pad_template =
34 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
35     GST_STATIC_CAPS ("video/x-raw, framerate=" GST_VIDEO_FPS_RANGE));
36 
37 static void gst_video_segment_clip_reset (GstSegmentClip * self);
38 static GstFlowReturn gst_video_segment_clip_clip_buffer (GstSegmentClip * self,
39     GstBuffer * buffer, GstBuffer ** outbuf);
40 static gboolean gst_video_segment_clip_set_caps (GstSegmentClip * self,
41     GstCaps * caps);
42 
43 GST_DEBUG_CATEGORY_STATIC (gst_video_segment_clip_debug);
44 #define GST_CAT_DEFAULT gst_video_segment_clip_debug
45 
46 G_DEFINE_TYPE (GstVideoSegmentClip, gst_video_segment_clip,
47     GST_TYPE_SEGMENT_CLIP);
48 
49 static void
gst_video_segment_clip_class_init(GstVideoSegmentClipClass * klass)50 gst_video_segment_clip_class_init (GstVideoSegmentClipClass * klass)
51 {
52   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
53   GstSegmentClipClass *segment_clip_klass = GST_SEGMENT_CLIP_CLASS (klass);
54 
55   GST_DEBUG_CATEGORY_INIT (gst_video_segment_clip_debug, "videosegmentclip", 0,
56       "videosegmentclip element");
57 
58   gst_element_class_set_static_metadata (element_class,
59       "Video buffer segment clipper",
60       "Filter/Video",
61       "Clips video buffers to the configured segment",
62       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
63 
64   gst_element_class_add_static_pad_template (element_class, &sink_pad_template);
65   gst_element_class_add_static_pad_template (element_class, &src_pad_template);
66 
67   segment_clip_klass->reset = GST_DEBUG_FUNCPTR (gst_video_segment_clip_reset);
68   segment_clip_klass->set_caps =
69       GST_DEBUG_FUNCPTR (gst_video_segment_clip_set_caps);
70   segment_clip_klass->clip_buffer =
71       GST_DEBUG_FUNCPTR (gst_video_segment_clip_clip_buffer);
72 }
73 
74 static void
gst_video_segment_clip_init(GstVideoSegmentClip * self)75 gst_video_segment_clip_init (GstVideoSegmentClip * self)
76 {
77 }
78 
79 static void
gst_video_segment_clip_reset(GstSegmentClip * base)80 gst_video_segment_clip_reset (GstSegmentClip * base)
81 {
82   GstVideoSegmentClip *self = GST_VIDEO_SEGMENT_CLIP (base);
83 
84   GST_DEBUG_OBJECT (self, "Resetting internal state");
85 
86   self->fps_n = self->fps_d = 0;
87 }
88 
89 
90 static GstFlowReturn
gst_video_segment_clip_clip_buffer(GstSegmentClip * base,GstBuffer * buffer,GstBuffer ** outbuf)91 gst_video_segment_clip_clip_buffer (GstSegmentClip * base, GstBuffer * buffer,
92     GstBuffer ** outbuf)
93 {
94   GstVideoSegmentClip *self = GST_VIDEO_SEGMENT_CLIP (base);
95   GstSegment *segment = &base->segment;
96   GstClockTime timestamp, duration;
97   guint64 cstart, cstop;
98   gboolean in_seg;
99 
100   if (!self->fps_d) {
101     GST_ERROR_OBJECT (self, "Not negotiated yet");
102     gst_buffer_unref (buffer);
103     return GST_FLOW_NOT_NEGOTIATED;
104   }
105 
106   if (segment->format != GST_FORMAT_TIME) {
107     GST_DEBUG_OBJECT (self, "Unsupported segment format %s",
108         gst_format_get_name (segment->format));
109     *outbuf = buffer;
110     return GST_FLOW_OK;
111   }
112 
113   if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
114     GST_WARNING_OBJECT (self, "Buffer without valid timestamp");
115     *outbuf = buffer;
116     return GST_FLOW_OK;
117   }
118 
119   if (self->fps_n == 0) {
120     *outbuf = buffer;
121     return GST_FLOW_OK;
122   }
123 
124   timestamp = GST_BUFFER_TIMESTAMP (buffer);
125   duration = GST_BUFFER_DURATION (buffer);
126   if (!GST_CLOCK_TIME_IS_VALID (duration))
127     duration = gst_util_uint64_scale (GST_SECOND, self->fps_d, self->fps_n);
128 
129   in_seg =
130       gst_segment_clip (segment, GST_FORMAT_TIME, timestamp,
131       timestamp + duration, &cstart, &cstop);
132   if (in_seg) {
133     if (timestamp != cstart || timestamp + duration != cstop) {
134       *outbuf = gst_buffer_make_writable (buffer);
135 
136       GST_BUFFER_TIMESTAMP (*outbuf) = cstart;
137       GST_BUFFER_DURATION (*outbuf) = cstop - cstart;
138     } else {
139       *outbuf = buffer;
140     }
141   } else {
142     GST_DEBUG_OBJECT (self, "Buffer outside the configured segment");
143 
144     gst_buffer_unref (buffer);
145     if (segment->rate >= 0) {
146       if (segment->stop != -1 && timestamp >= segment->stop)
147         return GST_FLOW_EOS;
148     } else {
149       if (segment->start != -1 && timestamp + duration <= segment->start)
150         return GST_FLOW_EOS;
151     }
152   }
153 
154 
155   return GST_FLOW_OK;
156 }
157 
158 static gboolean
gst_video_segment_clip_set_caps(GstSegmentClip * base,GstCaps * caps)159 gst_video_segment_clip_set_caps (GstSegmentClip * base, GstCaps * caps)
160 {
161   GstVideoSegmentClip *self = GST_VIDEO_SEGMENT_CLIP (base);
162   gboolean ret;
163   GstStructure *s;
164   gint fps_n, fps_d;
165 
166   s = gst_caps_get_structure (caps, 0);
167 
168   ret = gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)
169       && (fps_d != 0);
170 
171   if (ret) {
172     GST_DEBUG_OBJECT (self, "Configured framerate %d/%d", fps_n, fps_d);
173     self->fps_n = fps_n;
174     self->fps_d = fps_d;
175   }
176 
177   return ret;
178 }
179