4
* Copyright (C) 2004 Billy Biggs <vektor@dumbterm.net>
5
* Copyright (C) 2008 Sebastian Dröge <slomo@collabora.co.uk>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Library General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
24
* Relicensed for GStreamer from GPL to LGPL with permit from Billy Biggs.
25
* See: http://bugzilla.gnome.org/show_bug.cgi?id=163578
32
#include "greedyhmacros.h"
40
#include "gstdeinterlace2.h"
42
#define GST_TYPE_DEINTERLACE_METHOD_GREEDY_H (gst_deinterlace_method_greedy_h_get_type ())
43
#define GST_IS_DEINTERLACE_METHOD_GREEDY_H(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H))
44
#define GST_IS_DEINTERLACE_METHOD_GREEDY_H_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H))
45
#define GST_DEINTERLACE_METHOD_GREEDY_H_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H, GstDeinterlaceMethodGreedyHClass))
46
#define GST_DEINTERLACE_METHOD_GREEDY_H(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H, GstDeinterlaceMethodGreedyH))
47
#define GST_DEINTERLACE_METHOD_GREEDY_H_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H, GstDeinterlaceMethodGreedyHClass))
48
#define GST_DEINTERLACE_METHOD_GREEDY_H_CAST(obj) ((GstDeinterlaceMethodGreedyH*)(obj))
50
GType gst_deinterlace_method_greedy_h_get_type (void);
54
GstDeinterlaceMethod parent;
56
guint max_comb, motion_threshold, motion_sense;
57
} GstDeinterlaceMethodGreedyH;
61
GstDeinterlaceMethodClass parent_class;
62
void (*scanline) (GstDeinterlaceMethodGreedyH * self, uint8_t * L2,
63
uint8_t * L1, uint8_t * L3, uint8_t * L2P, uint8_t * Dest, int size);
64
} GstDeinterlaceMethodGreedyHClass;
67
greedyDScaler_C (GstDeinterlaceMethodGreedyH * self, uint8_t * L1, uint8_t * L2,
68
uint8_t * L3, uint8_t * L2P, uint8_t * Dest, int size)
71
uint8_t l1_l, l1_1_l, l3_l, l3_1_l;
72
uint8_t l1_c, l1_1_c, l3_c, l3_1_c;
73
uint8_t avg_l, avg_c, avg_l_1, avg_c_1;
74
uint8_t avg_l__1 = 0, avg_c__1 = 0;
75
uint8_t avg_s_l, avg_s_c;
76
uint8_t avg_sc_l, avg_sc_c;
77
uint8_t best_l, best_c;
80
uint8_t l2_l, l2_c, lp2_l, lp2_c;
81
uint8_t l2_l_diff, l2_c_diff, lp2_l_diff, lp2_c_diff;
82
uint8_t min_l, min_c, max_l, max_c;
83
guint max_comb = self->max_comb;
84
guint motion_sense = self->motion_sense;
85
guint motion_threshold = self->motion_threshold;
87
for (Pos = 0; Pos < size; Pos += 2) {
93
if (Pos == size - 1) {
105
/* Average of L1 and L3 */
106
avg_l = (l1_l + l3_l) / 2;
107
avg_c = (l1_c + l3_c) / 2;
114
/* Average of next L1 and next L3 */
115
avg_l_1 = (l1_1_l + l3_1_l) / 2;
116
avg_c_1 = (l1_1_c + l3_1_c) / 2;
118
/* Calculate average of one pixel forward and previous */
119
avg_s_l = (avg_l__1 + avg_l_1) / 2;
120
avg_s_c = (avg_c__1 + avg_c_1) / 2;
122
/* Calculate average of center and surrounding pixels */
123
avg_sc_l = (avg_l + avg_s_l) / 2;
124
avg_sc_c = (avg_c + avg_s_c) / 2;
130
/* Get best L2/L2P, i.e. least diff from above average */
136
l2_l_diff = ABS (l2_l - avg_sc_l);
137
l2_c_diff = ABS (l2_c - avg_sc_c);
139
lp2_l_diff = ABS (lp2_l - avg_sc_l);
140
lp2_c_diff = ABS (lp2_c - avg_sc_c);
142
if (l2_l_diff > lp2_l_diff)
147
if (l2_c_diff > lp2_c_diff)
152
/* Clip this best L2/L2P by L1/L3 and allow to differ by GreedyMaxComb */
153
max_l = MAX (l1_l, l3_l);
154
min_l = MIN (l1_l, l3_l);
156
if (max_l < 256 - max_comb)
161
if (min_l > max_comb)
166
max_c = MAX (l1_c, l3_c);
167
min_c = MIN (l1_c, l3_c);
169
if (max_c < 256 - max_comb)
174
if (min_c > max_comb)
179
out_l = CLAMP (best_l, min_l, max_l);
180
out_c = CLAMP (best_c, min_c, max_c);
182
/* Do motion compensation for luma, i.e. how much
183
* the weave pixel differs */
184
mov_l = ABS (l2_l - lp2_l);
185
if (mov_l > motion_threshold)
186
mov_l -= motion_threshold;
190
mov_l = mov_l * motion_sense;
194
/* Weighted sum on clipped weave pixel and average */
195
out_l = (out_l * (256 - mov_l) + avg_sc_l * mov_l) / 256;
211
#define SIMD_TYPE MMXEXT
212
#define FUNCT_NAME greedyDScaler_MMXEXT
213
#include "greedyh.asm"
219
#define SIMD_TYPE 3DNOW
220
#define FUNCT_NAME greedyDScaler_3DNOW
221
#include "greedyh.asm"
227
#define SIMD_TYPE MMX
228
#define FUNCT_NAME greedyDScaler_MMX
229
#include "greedyh.asm"
237
deinterlace_frame_di_greedyh (GstDeinterlaceMethod * d_method,
238
GstDeinterlace2 * object)
240
GstDeinterlaceMethodGreedyH *self =
241
GST_DEINTERLACE_METHOD_GREEDY_H (d_method);
242
GstDeinterlaceMethodGreedyHClass *klass =
243
GST_DEINTERLACE_METHOD_GREEDY_H_GET_CLASS (self);
246
unsigned int Pitch = object->field_stride;
248
unsigned char *L1; // ptr to Line1, of 3
249
unsigned char *L2; // ptr to Line2, the weave line
250
unsigned char *L3; // ptr to Line3
252
unsigned char *L2P; // ptr to prev Line2
253
unsigned char *Dest = GST_BUFFER_DATA (object->out_buf);
255
// copy first even line no matter what, and the first odd line if we're
256
// processing an EVEN field. (note diff from other deint rtns.)
258
if (object->field_history[object->history_count - 1].flags ==
259
PICTURE_INTERLACED_BOTTOM) {
262
L1 = GST_BUFFER_DATA (object->field_history[object->history_count - 2].buf);
263
L2 = GST_BUFFER_DATA (object->field_history[object->history_count - 1].buf);
266
GST_BUFFER_DATA (object->field_history[object->history_count - 3].buf);
268
// copy first even line
269
oil_memcpy (Dest, L1, object->line_length);
270
Dest += object->output_stride;
273
L1 = GST_BUFFER_DATA (object->field_history[object->history_count - 2].buf);
274
L2 = GST_BUFFER_DATA (object->field_history[object->history_count -
278
GST_BUFFER_DATA (object->field_history[object->history_count - 3].buf) +
281
// copy first even line
282
oil_memcpy (Dest, GST_BUFFER_DATA (object->field_history[0].buf),
283
object->line_length);
284
Dest += object->output_stride;
285
// then first odd line
286
oil_memcpy (Dest, L1, object->line_length);
287
Dest += object->output_stride;
290
for (Line = 0; Line < (object->field_height - 1); ++Line) {
291
klass->scanline (self, L1, L2, L3, L2P, Dest, object->line_length);
292
Dest += object->output_stride;
293
oil_memcpy (Dest, L3, object->line_length);
294
Dest += object->output_stride;
303
oil_memcpy (Dest, L2, object->line_length);
307
G_DEFINE_TYPE (GstDeinterlaceMethodGreedyH, gst_deinterlace_method_greedy_h,
308
GST_TYPE_DEINTERLACE_METHOD);
314
ARG_MOTION_THRESHOLD,
319
gst_deinterlace_method_greedy_h_set_property (GObject * object, guint prop_id,
320
const GValue * value, GParamSpec * pspec)
322
GstDeinterlaceMethodGreedyH *self = GST_DEINTERLACE_METHOD_GREEDY_H (object);
326
self->max_comb = g_value_get_uint (value);
328
case ARG_MOTION_THRESHOLD:
329
self->motion_threshold = g_value_get_uint (value);
331
case ARG_MOTION_SENSE:
332
self->motion_sense = g_value_get_uint (value);
335
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340
gst_deinterlace_method_greedy_h_get_property (GObject * object, guint prop_id,
341
GValue * value, GParamSpec * pspec)
343
GstDeinterlaceMethodGreedyH *self = GST_DEINTERLACE_METHOD_GREEDY_H (object);
347
g_value_set_uint (value, self->max_comb);
349
case ARG_MOTION_THRESHOLD:
350
g_value_set_uint (value, self->motion_threshold);
352
case ARG_MOTION_SENSE:
353
g_value_set_uint (value, self->motion_sense);
356
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361
gst_deinterlace_method_greedy_h_class_init (GstDeinterlaceMethodGreedyHClass *
364
GstDeinterlaceMethodClass *dim_class = (GstDeinterlaceMethodClass *) klass;
365
GObjectClass *gobject_class = (GObjectClass *) klass;
367
guint cpu_flags = oil_cpu_get_flags ();
370
gobject_class->set_property = gst_deinterlace_method_greedy_h_set_property;
371
gobject_class->get_property = gst_deinterlace_method_greedy_h_get_property;
373
g_object_class_install_property (gobject_class, ARG_MAX_COMB,
374
g_param_spec_uint ("max-comb",
376
"Max Comb", 0, 255, 5, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
379
g_object_class_install_property (gobject_class, ARG_MOTION_THRESHOLD,
380
g_param_spec_uint ("motion-threshold",
383
0, 255, 25, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
386
g_object_class_install_property (gobject_class, ARG_MOTION_SENSE,
387
g_param_spec_uint ("motion-sense",
390
0, 255, 30, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
393
dim_class->fields_required = 4;
394
dim_class->deinterlace_frame = deinterlace_frame_di_greedyh;
395
dim_class->name = "Motion Adaptive: Advanced Detection";
396
dim_class->nick = "greedyh";
397
dim_class->latency = 1;
400
if (cpu_flags & OIL_IMPL_FLAG_MMXEXT) {
401
klass->scanline = greedyDScaler_MMXEXT;
402
} else if (cpu_flags & OIL_IMPL_FLAG_3DNOW) {
403
klass->scanline = greedyDScaler_3DNOW;
404
} else if (cpu_flags & OIL_IMPL_FLAG_MMX) {
405
klass->scanline = greedyDScaler_MMX;
407
klass->scanline = greedyDScaler_C;
410
klass->scanline = greedyDScaler_C;
415
gst_deinterlace_method_greedy_h_init (GstDeinterlaceMethodGreedyH * self)
418
self->motion_threshold = 25;
419
self->motion_sense = 30;