1 /*
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>
6  *
7  * This file is part of Libav.
8  *
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.
13  *
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.
18  *
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.
22  */
23 
24 #include "config.h"
25 
26 #include <gstyadif.h>
27 #include <string.h>
28 
29 #undef NDEBUG
30 #include <assert.h>
31 
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)
37 
38 
39 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
40 
41 #define CHECK(j)\
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;\
48 
49 #define FILTER \
50     for (x = 0;  x < w; x++) { \
51         int c = cur[mrefs]; \
52         int d = (prev2[0] + next2[0])>>1; \
53         int e = cur[prefs]; \
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; \
60  \
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; \
64  \
65             CHECK(-1) CHECK(-2) }} }} \
66             CHECK( 1) CHECK( 2) }} }} \
67         } \
68         if (mode < 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)); \
73  \
74             diff = FFMAX3(diff, min, -max); \
75         } \
76  \
77         if (spatial_pred > d + diff) \
78            spatial_pred = d + diff; \
79         else if (spatial_pred < d - diff) \
80            spatial_pred = d - diff; \
81  \
82         dst[0] = spatial_pred; \
83  \
84         dst++; \
85         cur++; \
86         prev++; \
87         next++; \
88         prev2++; \
89         next2++; \
90     }
91 
92 static void
filter_line_c(guint8 * dst,guint8 * prev,guint8 * cur,guint8 * next,int w,int prefs,int mrefs,int parity,int mode)93 filter_line_c (guint8 * dst,
94     guint8 * prev, guint8 * cur, guint8 * next,
95     int w, int prefs, int mrefs, int parity, int mode)
96 {
97   int x;
98   guint8 *prev2 = parity ? prev : cur;
99   guint8 *next2 = parity ? cur : next;
100 
101 FILTER}
102 
103 #if 0
104 static void
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)
108 {
109   int x;
110   guint16 *prev2 = parity ? prev : cur;
111   guint16 *next2 = parity ? cur : next;
112   mrefs /= 2;
113   prefs /= 2;
114 
115 FILTER}
116 #endif
117 
118 void yadif_filter (GstYadif * yadif, int parity, int tff);
119 #ifdef HAVE_CPU_X86_64
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);
123 #endif
124 
125 void
yadif_filter(GstYadif * yadif,int parity,int tff)126 yadif_filter (GstYadif * yadif, int parity, int tff)
127 {
128   int y, i;
129   const GstVideoInfo *vi = &yadif->video_info;
130   const GstVideoFormatInfo *vfi = vi->finfo;
131 
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);
141 
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
150         if (0) {
151           filter_line_c (dst, prev, cur, next, w,
152               y + 1 < h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
153         } else {
154           filter_line_x86_64 (dst, prev, cur, next, w,
155               y + 1 < h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
156         }
157 #else
158         filter_line_c (dst, prev, cur, next, w,
159             y + 1 < h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
160 #endif
161       } else {
162         guint8 *dst = dest_data + y * refs;
163         guint8 *cur = cur_data + y * refs;
164 
165         memcpy (dst, cur, w * df);
166       }
167     }
168   }
169 
170 #if 0
171   emms_c ();
172 #endif
173 }
174