• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * GStreamer
4  * Copyright (C) 2004 Billy Biggs <vektor@dumbterm.net>
5  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
6  * Copyright (C) 2008,2010 Sebastian Dröge <slomo@collabora.co.uk>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /*
25  * This file contains code from ffmpeg, see http://ffmpeg.org/ (LGPL)
26  * and modifications by Billy Biggs.
27  *
28  * Relicensed for GStreamer from GPL to LGPL with permit from Billy Biggs.
29  * See: http://bugzilla.gnome.org/show_bug.cgi?id=163578
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include "gstdeinterlacemethod.h"
37 #include <string.h>
38 #ifdef HAVE_ORC
39 #include <orc/orc.h>
40 #endif
41 #include "tvtime.h"
42 
43 #define GST_TYPE_DEINTERLACE_METHOD_VFIR	(gst_deinterlace_method_vfir_get_type ())
44 #define GST_IS_DEINTERLACE_METHOD_VFIR(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DEINTERLACE_METHOD_VFIR))
45 #define GST_IS_DEINTERLACE_METHOD_VFIR_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_DEINTERLACE_METHOD_VFIR))
46 #define GST_DEINTERLACE_METHOD_VFIR_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_DEINTERLACE_METHOD_VFIR, GstDeinterlaceMethodVFIRClass))
47 #define GST_DEINTERLACE_METHOD_VFIR(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_DEINTERLACE_METHOD_VFIR, GstDeinterlaceMethodVFIR))
48 #define GST_DEINTERLACE_METHOD_VFIR_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_DEINTERLACE_METHOD_VFIR, GstDeinterlaceMethodVFIRClass))
49 #define GST_DEINTERLACE_METHOD_VFIR_CAST(obj)	((GstDeinterlaceMethodVFIR*)(obj))
50 
51 GType gst_deinterlace_method_vfir_get_type (void);
52 
53 typedef GstDeinterlaceSimpleMethod GstDeinterlaceMethodVFIR;
54 
55 typedef GstDeinterlaceSimpleMethodClass GstDeinterlaceMethodVFIRClass;
56 
57 /*
58  * The MPEG2 spec uses a slightly harsher filter, they specify
59  * [-1 8 2 8 -1].  ffmpeg uses a similar filter but with more of
60  * a tendency to blur than to use the local information.  The
61  * filter taps here are: [-1 4 2 4 -1].
62  */
63 
64  /*
65   * C implementation.
66   */
67 static inline void
deinterlace_c(guint8 * dst,const guint8 * lum_m4,const guint8 * lum_m3,const guint8 * lum_m2,const guint8 * lum_m1,const guint8 * lum,gint size)68 deinterlace_c (guint8 * dst, const guint8 * lum_m4, const guint8 * lum_m3,
69     const guint8 * lum_m2, const guint8 * lum_m1, const guint8 * lum, gint size)
70 {
71   if (lum_m2 == NULL) {
72     deinterlace_line_linear (dst, lum_m1, lum_m3, size);
73   } else {
74     deinterlace_line_vfir (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
75   }
76 }
77 
78 static void
deinterlace_line_packed_c(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines,guint size)79 deinterlace_line_packed_c (GstDeinterlaceSimpleMethod * self, guint8 * dst,
80     const GstDeinterlaceScanlineData * scanlines, guint size)
81 {
82   const guint8 *lum_m4 = scanlines->tt1;
83   const guint8 *lum_m3 = scanlines->t0;
84   const guint8 *lum_m2 = scanlines->m1;
85   const guint8 *lum_m1 = scanlines->b0;
86   const guint8 *lum = scanlines->bb1;
87 
88   deinterlace_c (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
89 }
90 
91 static void
deinterlace_line_planar_y_c(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines,guint size)92 deinterlace_line_planar_y_c (GstDeinterlaceSimpleMethod * self, guint8 * dst,
93     const GstDeinterlaceScanlineData * scanlines, guint size)
94 {
95   const guint8 *lum_m4 = scanlines->tt1;
96   const guint8 *lum_m3 = scanlines->t0;
97   const guint8 *lum_m2 = scanlines->m1;
98   const guint8 *lum_m1 = scanlines->b0;
99   const guint8 *lum = scanlines->bb1;
100 
101   deinterlace_c (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
102 }
103 
104 static void
deinterlace_line_planar_u_c(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines,guint size)105 deinterlace_line_planar_u_c (GstDeinterlaceSimpleMethod * self, guint8 * dst,
106     const GstDeinterlaceScanlineData * scanlines, guint size)
107 {
108   const guint8 *lum_m4 = scanlines->tt1;
109   const guint8 *lum_m3 = scanlines->t0;
110   const guint8 *lum_m2 = scanlines->m1;
111   const guint8 *lum_m1 = scanlines->b0;
112   const guint8 *lum = scanlines->bb1;
113 
114   deinterlace_c (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
115 }
116 
117 static void
deinterlace_line_planar_v_c(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines,guint size)118 deinterlace_line_planar_v_c (GstDeinterlaceSimpleMethod * self, guint8 * dst,
119     const GstDeinterlaceScanlineData * scanlines, guint size)
120 {
121   const guint8 *lum_m4 = scanlines->tt1;
122   const guint8 *lum_m3 = scanlines->t0;
123   const guint8 *lum_m2 = scanlines->m1;
124   const guint8 *lum_m1 = scanlines->b0;
125   const guint8 *lum = scanlines->bb1;
126 
127   deinterlace_c (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
128 }
129 
130 #undef BUILD_X86_ASM
131 
132 #ifdef BUILD_X86_ASM
133 #include "mmx.h"
134 static void
deinterlace_mmx(guint8 * dst,const guint8 * lum_m4,const guint8 * lum_m3,const guint8 * lum_m2,const guint8 * lum_m1,const guint8 * lum,gint size)135 deinterlace_mmx (guint8 * dst, const guint8 * lum_m4, const guint8 * lum_m3,
136     const guint8 * lum_m2, const guint8 * lum_m1, const guint8 * lum, gint size)
137 {
138   mmx_t rounder;
139 
140   rounder.uw[0] = 4;
141   rounder.uw[1] = 4;
142   rounder.uw[2] = 4;
143   rounder.uw[3] = 4;
144   pxor_r2r (mm7, mm7);
145   movq_m2r (rounder, mm6);
146 
147   for (; size > 3; size -= 4) {
148     movd_m2r (*lum_m4, mm0);
149     movd_m2r (*lum_m3, mm1);
150     movd_m2r (*lum_m2, mm2);
151     movd_m2r (*lum_m1, mm3);
152     movd_m2r (*lum, mm4);
153     punpcklbw_r2r (mm7, mm0);
154     punpcklbw_r2r (mm7, mm1);
155     punpcklbw_r2r (mm7, mm2);
156     punpcklbw_r2r (mm7, mm3);
157     punpcklbw_r2r (mm7, mm4);
158     paddw_r2r (mm3, mm1);
159     psllw_i2r (1, mm2);
160     paddw_r2r (mm4, mm0);
161     psllw_i2r (2, mm1);         // 2
162     paddw_r2r (mm6, mm2);
163     paddw_r2r (mm2, mm1);
164     psubusw_r2r (mm0, mm1);
165     psrlw_i2r (3, mm1);         // 3
166     packuswb_r2r (mm7, mm1);
167     movd_r2m (mm1, *dst);
168     lum_m4 += 4;
169     lum_m3 += 4;
170     lum_m2 += 4;
171     lum_m1 += 4;
172     lum += 4;
173     dst += 4;
174   }
175   emms ();
176 
177   /* Handle odd widths */
178   if (size > 0)
179     deinterlace_c (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
180 }
181 
182 static void
deinterlace_line_packed_mmx(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines)183 deinterlace_line_packed_mmx (GstDeinterlaceSimpleMethod * self, guint8 * dst,
184     const GstDeinterlaceScanlineData * scanlines)
185 {
186   const guint8 *lum_m4 = scanlines->tt1;
187   const guint8 *lum_m3 = scanlines->t0;
188   const guint8 *lum_m2 = scanlines->m1;
189   const guint8 *lum_m1 = scanlines->b0;
190   const guint8 *lum = scanlines->bb1;
191   gint size = self->parent.row_stride[0];
192 
193   deinterlace_mmx (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
194 }
195 
196 static void
deinterlace_line_planar_y_mmx(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines)197 deinterlace_line_planar_y_mmx (GstDeinterlaceSimpleMethod * self, guint8 * dst,
198     const GstDeinterlaceScanlineData * scanlines)
199 {
200   const guint8 *lum_m4 = scanlines->tt1;
201   const guint8 *lum_m3 = scanlines->t0;
202   const guint8 *lum_m2 = scanlines->m1;
203   const guint8 *lum_m1 = scanlines->b0;
204   const guint8 *lum = scanlines->bb1;
205   gint size = self->parent.row_stride[0];
206 
207   deinterlace_mmx (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
208 }
209 
210 static void
deinterlace_line_planar_u_mmx(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines)211 deinterlace_line_planar_u_mmx (GstDeinterlaceSimpleMethod * self, guint8 * dst,
212     const GstDeinterlaceScanlineData * scanlines)
213 {
214   const guint8 *lum_m4 = scanlines->tt1;
215   const guint8 *lum_m3 = scanlines->t0;
216   const guint8 *lum_m2 = scanlines->m1;
217   const guint8 *lum_m1 = scanlines->b0;
218   const guint8 *lum = scanlines->bb1;
219   gint size = self->parent.row_stride[1];
220 
221   deinterlace_mmx (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
222 }
223 
224 static void
deinterlace_line_planar_v_mmx(GstDeinterlaceSimpleMethod * self,guint8 * dst,const GstDeinterlaceScanlineData * scanlines)225 deinterlace_line_planar_v_mmx (GstDeinterlaceSimpleMethod * self, guint8 * dst,
226     const GstDeinterlaceScanlineData * scanlines)
227 {
228   const guint8 *lum_m4 = scanlines->tt1;
229   const guint8 *lum_m3 = scanlines->t0;
230   const guint8 *lum_m2 = scanlines->m1;
231   const guint8 *lum_m1 = scanlines->b0;
232   const guint8 *lum = scanlines->bb1;
233   gint size = self->parent.row_stride[2];
234 
235   deinterlace_mmx (dst, lum_m4, lum_m3, lum_m2, lum_m1, lum, size);
236 }
237 #endif
238 
239 G_DEFINE_TYPE (GstDeinterlaceMethodVFIR, gst_deinterlace_method_vfir,
240     GST_TYPE_DEINTERLACE_SIMPLE_METHOD);
241 
242 static void
gst_deinterlace_method_vfir_class_init(GstDeinterlaceMethodVFIRClass * klass)243 gst_deinterlace_method_vfir_class_init (GstDeinterlaceMethodVFIRClass * klass)
244 {
245   GstDeinterlaceMethodClass *dim_class = (GstDeinterlaceMethodClass *) klass;
246   GstDeinterlaceSimpleMethodClass *dism_class =
247       (GstDeinterlaceSimpleMethodClass *) klass;
248 #ifdef BUILD_X86_ASM
249   guint cpu_flags =
250       orc_target_get_default_flags (orc_target_get_by_name ("mmx"));
251 #endif
252 
253   dim_class->fields_required = 2;
254   dim_class->name = "Blur Vertical";
255   dim_class->nick = "vfir";
256   dim_class->latency = 1;
257 
258 #ifdef BUILD_X86_ASM
259   if (cpu_flags & ORC_TARGET_MMX_MMX) {
260     dism_class->interpolate_scanline_ayuv = deinterlace_line_packed_mmx;
261     dism_class->interpolate_scanline_yuy2 = deinterlace_line_packed_mmx;
262     dism_class->interpolate_scanline_yvyu = deinterlace_line_packed_mmx;
263     dism_class->interpolate_scanline_uyvy = deinterlace_line_packed_mmx;
264     dism_class->interpolate_scanline_nv12 = deinterlace_line_packed_mmx;
265     dism_class->interpolate_scanline_nv21 = deinterlace_line_packed_mmx;
266     dism_class->interpolate_scanline_argb = deinterlace_line_packed_mmx;
267     dism_class->interpolate_scanline_abgr = deinterlace_line_packed_mmx;
268     dism_class->interpolate_scanline_rgba = deinterlace_line_packed_mmx;
269     dism_class->interpolate_scanline_bgra = deinterlace_line_packed_mmx;
270     dism_class->interpolate_scanline_rgb = deinterlace_line_packed_mmx;
271     dism_class->interpolate_scanline_bgr = deinterlace_line_packed_mmx;
272     dism_class->interpolate_scanline_planar_y = deinterlace_line_planar_y_mmx;
273     dism_class->interpolate_scanline_planar_u = deinterlace_line_planar_u_mmx;
274     dism_class->interpolate_scanline_planar_v = deinterlace_line_planar_v_mmx;
275   } else {
276     dism_class->interpolate_scanline_yuy2 = deinterlace_line_packed_c;
277     dism_class->interpolate_scanline_yvyu = deinterlace_line_packed_c;
278     dism_class->interpolate_scanline_uyvy = deinterlace_line_packed_c;
279     dism_class->interpolate_scanline_ayuv = deinterlace_line_packed_c;
280     dism_class->interpolate_scanline_nv12 = deinterlace_line_packed_c;
281     dism_class->interpolate_scanline_nv21 = deinterlace_line_packed_c;
282     dism_class->interpolate_scanline_argb = deinterlace_line_packed_c;
283     dism_class->interpolate_scanline_abgr = deinterlace_line_packed_c;
284     dism_class->interpolate_scanline_rgba = deinterlace_line_packed_c;
285     dism_class->interpolate_scanline_bgra = deinterlace_line_packed_c;
286     dism_class->interpolate_scanline_rgb = deinterlace_line_packed_c;
287     dism_class->interpolate_scanline_bgr = deinterlace_line_packed_c;
288     dism_class->interpolate_scanline_planar_y = deinterlace_line_planar_y_c;
289     dism_class->interpolate_scanline_planar_u = deinterlace_line_planar_u_c;
290     dism_class->interpolate_scanline_planar_v = deinterlace_line_planar_v_c;
291   }
292 #else
293   dism_class->interpolate_scanline_ayuv = deinterlace_line_packed_c;
294   dism_class->interpolate_scanline_yuy2 = deinterlace_line_packed_c;
295   dism_class->interpolate_scanline_yvyu = deinterlace_line_packed_c;
296   dism_class->interpolate_scanline_uyvy = deinterlace_line_packed_c;
297   dism_class->interpolate_scanline_nv12 = deinterlace_line_packed_c;
298   dism_class->interpolate_scanline_nv21 = deinterlace_line_packed_c;
299   dism_class->interpolate_scanline_argb = deinterlace_line_packed_c;
300   dism_class->interpolate_scanline_abgr = deinterlace_line_packed_c;
301   dism_class->interpolate_scanline_rgba = deinterlace_line_packed_c;
302   dism_class->interpolate_scanline_bgra = deinterlace_line_packed_c;
303   dism_class->interpolate_scanline_rgb = deinterlace_line_packed_c;
304   dism_class->interpolate_scanline_bgr = deinterlace_line_packed_c;
305   dism_class->interpolate_scanline_planar_y = deinterlace_line_planar_y_c;
306   dism_class->interpolate_scanline_planar_u = deinterlace_line_planar_u_c;
307   dism_class->interpolate_scanline_planar_v = deinterlace_line_planar_v_c;
308 #endif
309 }
310 
311 static void
gst_deinterlace_method_vfir_init(GstDeinterlaceMethodVFIR * self)312 gst_deinterlace_method_vfir_init (GstDeinterlaceMethodVFIR * self)
313 {
314 }
315