1 /* GStreamer
2  * Copyright (C) 2013 Rdio, Inc. <ingestions@rdio.com>
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 Street, Suite 500,
17  * Boston, MA 02110-1335, USA.
18  */
19 /**
20  * SECTION:element-gstcombdetect
21  * @title: gstcombdetect
22  *
23  * The combdetect element detects if combing artifacts are present in
24  * a raw video stream, and if so, marks them with a zebra stripe
25  * pattern.
26  *
27  * ## Example launch line
28  * |[
29  * gst-launch-1.0 -v filesrc location=file.mov ! decodebin ! combdetect !
30  *     xvimagesink
31  * ]|
32  *
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include <gst/gst.h>
40 #include <gst/video/video.h>
41 #include <gst/video/gstvideofilter.h>
42 #include "gstcombdetect.h"
43 
44 #include <string.h>
45 
46 GST_DEBUG_CATEGORY_STATIC (gst_comb_detect_debug_category);
47 #define GST_CAT_DEFAULT gst_comb_detect_debug_category
48 
49 /* prototypes */
50 
51 
52 static GstCaps *gst_comb_detect_transform_caps (GstBaseTransform * trans,
53     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
54 static gboolean gst_comb_detect_set_info (GstVideoFilter * filter,
55     GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
56     GstVideoInfo * out_info);
57 static GstFlowReturn gst_comb_detect_transform_frame (GstVideoFilter * filter,
58     GstVideoFrame * inframe, GstVideoFrame * outframe);
59 
60 enum
61 {
62   PROP_0
63 };
64 
65 /* pad templates */
66 
67 /* Yeah, the max width is hard-coded 2048. */
68 #define MAX_WIDTH 2048
69 #define VIDEO_CAPS \
70   "video/x-raw, " \
71   "format = (string) { I420, Y444, Y42B }, " \
72   "width = [1, 2048], " \
73   "height = " GST_VIDEO_SIZE_RANGE ", " \
74   "framerate = " GST_VIDEO_FPS_RANGE
75 
76 static GstStaticPadTemplate gst_comb_detect_sink_template =
77 GST_STATIC_PAD_TEMPLATE ("sink",
78     GST_PAD_SINK,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS (VIDEO_CAPS)
81     );
82 
83 static GstStaticPadTemplate gst_comb_detect_src_template =
84 GST_STATIC_PAD_TEMPLATE ("src",
85     GST_PAD_SRC,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS (VIDEO_CAPS)
88     );
89 
90 
91 /* class initialization */
92 
93 G_DEFINE_TYPE_WITH_CODE (GstCombDetect, gst_comb_detect, GST_TYPE_VIDEO_FILTER,
94     GST_DEBUG_CATEGORY_INIT (gst_comb_detect_debug_category, "combdetect", 0,
95         "debug category for combdetect element"));
96 
97 static void
gst_comb_detect_class_init(GstCombDetectClass * klass)98 gst_comb_detect_class_init (GstCombDetectClass * klass)
99 {
100   GstBaseTransformClass *base_transform_class =
101       GST_BASE_TRANSFORM_CLASS (klass);
102   GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass);
103 
104   /* Setting up pads and setting metadata should be moved to
105      base_class_init if you intend to subclass this class. */
106   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
107       &gst_comb_detect_sink_template);
108   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
109       &gst_comb_detect_src_template);
110 
111   gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
112       "Comb Detect", "Video/Filter", "Detect combing artifacts in video stream",
113       "David Schleef <ds@schleef.org>");
114 
115   base_transform_class->transform_caps =
116       GST_DEBUG_FUNCPTR (gst_comb_detect_transform_caps);
117   video_filter_class->set_info = GST_DEBUG_FUNCPTR (gst_comb_detect_set_info);
118   video_filter_class->transform_frame =
119       GST_DEBUG_FUNCPTR (gst_comb_detect_transform_frame);
120 
121 }
122 
123 static void
gst_comb_detect_init(GstCombDetect * combdetect)124 gst_comb_detect_init (GstCombDetect * combdetect)
125 {
126 }
127 
128 
129 static GstCaps *
gst_comb_detect_transform_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)130 gst_comb_detect_transform_caps (GstBaseTransform * trans,
131     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
132 {
133   GstCaps *othercaps;
134   int i;
135 
136   othercaps = gst_caps_copy (caps);
137 
138   if (direction == GST_PAD_SRC) {
139     GValue value = G_VALUE_INIT;
140     GValue v = G_VALUE_INIT;
141 
142     g_value_init (&value, GST_TYPE_LIST);
143     g_value_init (&v, G_TYPE_STRING);
144 
145     g_value_set_string (&v, "interleaved");
146     gst_value_list_append_value (&value, &v);
147     g_value_set_string (&v, "mixed");
148     gst_value_list_append_value (&value, &v);
149     g_value_set_string (&v, "progressive");
150     gst_value_list_append_value (&value, &v);
151 
152     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
153       GstStructure *structure = gst_caps_get_structure (othercaps, i);
154       gst_structure_set_value (structure, "interlace-mode", &value);
155     }
156     g_value_unset (&value);
157     g_value_unset (&v);
158   } else {
159     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
160       GstStructure *structure = gst_caps_get_structure (othercaps, i);
161       gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
162           "progressive", NULL);
163     }
164   }
165 
166   if (filter) {
167     GstCaps *intersect;
168 
169     intersect = gst_caps_intersect (othercaps, filter);
170     gst_caps_unref (othercaps);
171     othercaps = intersect;
172   }
173 
174   return othercaps;
175 }
176 
177 
178 static gboolean
gst_comb_detect_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)179 gst_comb_detect_set_info (GstVideoFilter * filter,
180     GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
181     GstVideoInfo * out_info)
182 {
183   GstCombDetect *combdetect = GST_COMB_DETECT (filter);
184 
185   memcpy (&combdetect->vinfo, in_info, sizeof (GstVideoInfo));
186 
187   return TRUE;
188 }
189 
190 static GstFlowReturn
gst_comb_detect_transform_frame(GstVideoFilter * filter,GstVideoFrame * inframe,GstVideoFrame * outframe)191 gst_comb_detect_transform_frame (GstVideoFilter * filter,
192     GstVideoFrame * inframe, GstVideoFrame * outframe)
193 {
194   static int z;
195   int k;
196   int height;
197   int width;
198 
199 #define GET_LINE(frame,comp,line) (((unsigned char *)(frame)->data[k]) + \
200       (line) * GST_VIDEO_FRAME_COMP_STRIDE((frame), (comp)))
201 
202   z++;
203 
204   for (k = 1; k < 3; k++) {
205     int i;
206     height = GST_VIDEO_FRAME_COMP_HEIGHT (outframe, k);
207     width = GST_VIDEO_FRAME_COMP_WIDTH (outframe, k);
208     for (i = 0; i < height; i++) {
209       memcpy (GET_LINE (outframe, k, i), GET_LINE (inframe, k, i), width);
210     }
211   }
212 
213   {
214     int j;
215     int thisline[MAX_WIDTH];
216     int score = 0;
217 
218     height = GST_VIDEO_FRAME_COMP_HEIGHT (outframe, 0);
219     width = GST_VIDEO_FRAME_COMP_WIDTH (outframe, 0);
220 
221     memset (thisline, 0, sizeof (thisline));
222 
223     k = 0;
224     for (j = 0; j < height; j++) {
225       int i;
226       if (j < 2 || j >= height - 2) {
227         guint8 *dest = GET_LINE (outframe, 0, j);
228         guint8 *src = GET_LINE (inframe, 0, j);
229         for (i = 0; i < width; i++) {
230           dest[i] = src[i] / 2;
231         }
232       } else {
233         guint8 *dest = GET_LINE (outframe, 0, j);
234         guint8 *src1 = GET_LINE (inframe, 0, j - 1);
235         guint8 *src2 = GET_LINE (inframe, 0, j);
236         guint8 *src3 = GET_LINE (inframe, 0, j + 1);
237 
238         for (i = 0; i < width; i++) {
239           if (src2[i] < MIN (src1[i], src3[i]) - 5 ||
240               src2[i] > MAX (src1[i], src3[i]) + 5) {
241             if (i > 0) {
242               thisline[i] += thisline[i - 1];
243             }
244             thisline[i]++;
245             if (thisline[i] > 1000)
246               thisline[i] = 1000;
247           } else {
248             thisline[i] = 0;
249           }
250           if (thisline[i] > 100) {
251             dest[i] = ((i + j + z) & 0x4) ? 235 : 16;
252             score++;
253           } else {
254             dest[i] = src2[i];
255           }
256         }
257       }
258     }
259 
260     if (score > 10)
261       GST_DEBUG ("score %d", score);
262   }
263 
264   return GST_FLOW_OK;
265 }
266