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-combdetect
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 GST_ELEMENT_REGISTER_DEFINE (combdetect, "combdetect", GST_RANK_NONE,
97 GST_TYPE_COMB_DETECT);
98
99 static void
gst_comb_detect_class_init(GstCombDetectClass * klass)100 gst_comb_detect_class_init (GstCombDetectClass * klass)
101 {
102 GstBaseTransformClass *base_transform_class =
103 GST_BASE_TRANSFORM_CLASS (klass);
104 GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass);
105
106 /* Setting up pads and setting metadata should be moved to
107 base_class_init if you intend to subclass this class. */
108 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
109 &gst_comb_detect_sink_template);
110 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
111 &gst_comb_detect_src_template);
112
113 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
114 "Comb Detect", "Video/Filter", "Detect combing artifacts in video stream",
115 "David Schleef <ds@schleef.org>");
116
117 base_transform_class->transform_caps =
118 GST_DEBUG_FUNCPTR (gst_comb_detect_transform_caps);
119 video_filter_class->set_info = GST_DEBUG_FUNCPTR (gst_comb_detect_set_info);
120 video_filter_class->transform_frame =
121 GST_DEBUG_FUNCPTR (gst_comb_detect_transform_frame);
122
123 }
124
125 static void
gst_comb_detect_init(GstCombDetect * combdetect)126 gst_comb_detect_init (GstCombDetect * combdetect)
127 {
128 }
129
130
131 static GstCaps *
gst_comb_detect_transform_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)132 gst_comb_detect_transform_caps (GstBaseTransform * trans,
133 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
134 {
135 GstCaps *othercaps;
136 int i;
137
138 othercaps = gst_caps_copy (caps);
139
140 if (direction == GST_PAD_SRC) {
141 GValue value = G_VALUE_INIT;
142 GValue v = G_VALUE_INIT;
143
144 g_value_init (&value, GST_TYPE_LIST);
145 g_value_init (&v, G_TYPE_STRING);
146
147 g_value_set_string (&v, "interleaved");
148 gst_value_list_append_value (&value, &v);
149 g_value_set_string (&v, "mixed");
150 gst_value_list_append_value (&value, &v);
151 g_value_set_string (&v, "progressive");
152 gst_value_list_append_value (&value, &v);
153
154 for (i = 0; i < gst_caps_get_size (othercaps); i++) {
155 GstStructure *structure = gst_caps_get_structure (othercaps, i);
156 gst_structure_set_value (structure, "interlace-mode", &value);
157 }
158 g_value_unset (&value);
159 g_value_unset (&v);
160 } else {
161 for (i = 0; i < gst_caps_get_size (othercaps); i++) {
162 GstStructure *structure = gst_caps_get_structure (othercaps, i);
163 gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
164 "progressive", NULL);
165 }
166 }
167
168 if (filter) {
169 GstCaps *intersect;
170
171 intersect = gst_caps_intersect (othercaps, filter);
172 gst_caps_unref (othercaps);
173 othercaps = intersect;
174 }
175
176 return othercaps;
177 }
178
179
180 static gboolean
gst_comb_detect_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)181 gst_comb_detect_set_info (GstVideoFilter * filter,
182 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
183 GstVideoInfo * out_info)
184 {
185 GstCombDetect *combdetect = GST_COMB_DETECT (filter);
186
187 memcpy (&combdetect->vinfo, in_info, sizeof (GstVideoInfo));
188
189 return TRUE;
190 }
191
192 static GstFlowReturn
gst_comb_detect_transform_frame(GstVideoFilter * filter,GstVideoFrame * inframe,GstVideoFrame * outframe)193 gst_comb_detect_transform_frame (GstVideoFilter * filter,
194 GstVideoFrame * inframe, GstVideoFrame * outframe)
195 {
196 static int z;
197 int k;
198 int height;
199 int width;
200
201 #define GET_LINE(frame,comp,line) (((unsigned char *)(frame)->data[k]) + \
202 (line) * GST_VIDEO_FRAME_COMP_STRIDE((frame), (comp)))
203
204 z++;
205
206 for (k = 1; k < 3; k++) {
207 int i;
208 height = GST_VIDEO_FRAME_COMP_HEIGHT (outframe, k);
209 width = GST_VIDEO_FRAME_COMP_WIDTH (outframe, k);
210 for (i = 0; i < height; i++) {
211 memcpy (GET_LINE (outframe, k, i), GET_LINE (inframe, k, i), width);
212 }
213 }
214
215 {
216 int j;
217 int thisline[MAX_WIDTH];
218 int score = 0;
219
220 height = GST_VIDEO_FRAME_COMP_HEIGHT (outframe, 0);
221 width = GST_VIDEO_FRAME_COMP_WIDTH (outframe, 0);
222
223 memset (thisline, 0, sizeof (thisline));
224
225 k = 0;
226 for (j = 0; j < height; j++) {
227 int i;
228 if (j < 2 || j >= height - 2) {
229 guint8 *dest = GET_LINE (outframe, 0, j);
230 guint8 *src = GET_LINE (inframe, 0, j);
231 for (i = 0; i < width; i++) {
232 dest[i] = src[i] / 2;
233 }
234 } else {
235 guint8 *dest = GET_LINE (outframe, 0, j);
236 guint8 *src1 = GET_LINE (inframe, 0, j - 1);
237 guint8 *src2 = GET_LINE (inframe, 0, j);
238 guint8 *src3 = GET_LINE (inframe, 0, j + 1);
239
240 for (i = 0; i < width; i++) {
241 if (src2[i] < MIN (src1[i], src3[i]) - 5 ||
242 src2[i] > MAX (src1[i], src3[i]) + 5) {
243 if (i > 0) {
244 thisline[i] += thisline[i - 1];
245 }
246 thisline[i]++;
247 if (thisline[i] > 1000)
248 thisline[i] = 1000;
249 } else {
250 thisline[i] = 0;
251 }
252 if (thisline[i] > 100) {
253 dest[i] = ((i + j + z) & 0x4) ? 235 : 16;
254 score++;
255 } else {
256 dest[i] = src2[i];
257 }
258 }
259 }
260 }
261
262 if (score > 10)
263 GST_DEBUG ("score %d", score);
264 }
265
266 return GST_FLOW_OK;
267 }
268