2 * Copyright (C) 2013 David Schleef <ds@schleef.org>
3 * Copyright (C) 2013 Rdio, Inc. <ingestions@rd.io>
4 * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
5 * 2010 James Darnley <james.darnley@gmail.com>
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with Libav; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #define FFABS(a) ABS(a)
33 #define FFMIN(a,b) MIN(a,b)
34 #define FFMAX(a,b) MAX(a,b)
35 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
36 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
39 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
42 { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
43 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
44 + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
45 if (score < spatial_score) {\
46 spatial_score= score;\
47 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
50 for (x = 0; x < w; x++) { \
52 int d = (prev2[0] + next2[0])>>1; \
54 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
55 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
56 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
57 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
58 int spatial_pred = (c+e) >> 1; \
59 int spatial_score = -1; \
61 if (mrefs > 0 && prefs > 0) { \
62 spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
63 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
65 CHECK(-1) CHECK(-2) }} }} \
66 CHECK( 1) CHECK( 2) }} }} \
69 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
70 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
71 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
72 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
74 diff = FFMAX3(diff, min, -max); \
77 if (spatial_pred > d + diff) \
78 spatial_pred = d + diff; \
79 else if (spatial_pred < d - diff) \
80 spatial_pred = d - diff; \
82 dst[0] = spatial_pred; \
93 filter_line_c (guint8 * dst,
94 guint8 * prev, guint8 * cur, guint8 * next,
95 int w, int prefs, int mrefs, int parity, int mode)
98 guint8 *prev2 = parity ? prev : cur;
99 guint8 *next2 = parity ? cur : next;
105 filter_line_c_16bit (guint16 * dst,
106 guint16 * prev, guint16 * cur, guint16 * next,
107 int w, int prefs, int mrefs, int parity, int mode)
110 guint16 *prev2 = parity ? prev : cur;
111 guint16 *next2 = parity ? cur : next;
118 void yadif_filter (GstYadif * yadif, int parity, int tff);
119 #if HAVE_CPU_X86_64 && (defined(__GNUC__) || defined(__clang__))
120 void filter_line_x86_64 (guint8 * dst,
121 guint8 * prev, guint8 * cur, guint8 * next,
122 int w, int prefs, int mrefs, int parity, int mode);
126 yadif_filter (GstYadif * yadif, int parity, int tff)
129 const GstVideoInfo *vi = &yadif->video_info;
130 const GstVideoFormatInfo *vfi = vi->finfo;
132 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_COMPONENTS (vfi); i++) {
133 int w = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vfi, i, vi->width);
134 int h = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vfi, i, vi->height);
135 int refs = GST_VIDEO_INFO_COMP_STRIDE (vi, i);
136 int df = GST_VIDEO_INFO_COMP_PSTRIDE (vi, i);
137 guint8 *prev_data = GST_VIDEO_FRAME_COMP_DATA (&yadif->prev_frame, i);
138 guint8 *cur_data = GST_VIDEO_FRAME_COMP_DATA (&yadif->cur_frame, i);
139 guint8 *next_data = GST_VIDEO_FRAME_COMP_DATA (&yadif->next_frame, i);
140 guint8 *dest_data = GST_VIDEO_FRAME_COMP_DATA (&yadif->dest_frame, i);
142 for (y = 0; y < h; y++) {
143 if ((y ^ parity) & 1) {
144 guint8 *prev = prev_data + y * refs;
145 guint8 *cur = cur_data + y * refs;
146 guint8 *next = next_data + y * refs;
147 guint8 *dst = dest_data + y * refs;
148 int mode = ((y == 1) || (y + 2 == h)) ? 2 : yadif->mode;
149 #if HAVE_CPU_X86_64 && (defined(__GNUC__) || defined(__clang__))
151 filter_line_c (dst, prev, cur, next, w,
152 y + 1 < h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
154 filter_line_x86_64 (dst, prev, cur, next, w,
155 y + 1 < h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
158 filter_line_c (dst, prev, cur, next, w,
159 y + 1 < h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
162 guint8 *dst = dest_data + y * refs;
163 guint8 *cur = cur_data + y * refs;
165 memcpy (dst, cur, w * df);