Browse code

vf_yadif: fix out-of line reads

Some changes in the border pixels, visually indistinguishable.

Anton Khirnov authored on 2013/02/08 02:31:45
Showing 5 changed files
... ...
@@ -34,15 +34,15 @@
34 34
 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
35 35
 
36 36
 #define CHECK(j)\
37
-    {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
37
+    {   int score = FFABS(cur[mrefs + off_left + (j)] - cur[prefs + off_left - (j)])\
38 38
                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
39
-                  + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
39
+                  + FFABS(cur[mrefs + off_right + (j)] - cur[prefs + off_right - (j)]);\
40 40
         if (score < spatial_score) {\
41 41
             spatial_score= score;\
42 42
             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
43 43
 
44
-#define FILTER \
45
-    for (x = 0;  x < w; x++) { \
44
+#define FILTER(start, end) \
45
+    for (x = start;  x < end; x++) { \
46 46
         int c = cur[mrefs]; \
47 47
         int d = (prev2[0] + next2[0])>>1; \
48 48
         int e = cur[prefs]; \
... ...
@@ -51,11 +51,15 @@
51 51
         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
52 52
         int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
53 53
         int spatial_pred = (c+e) >> 1; \
54
-        int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
55
-                          + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
54
+        int off_right = (x < w - 1) ? 1 : -1;\
55
+        int off_left  = x ? -1 : 1;\
56
+        int spatial_score = FFABS(cur[mrefs + off_left]  - cur[prefs + off_left]) + FFABS(c-e) \
57
+                          + FFABS(cur[mrefs + off_right] - cur[prefs + off_right]) - 1; \
56 58
  \
57
-        CHECK(-1) CHECK(-2) }} }} \
58
-        CHECK( 1) CHECK( 2) }} }} \
59
+        if (x > 2 && x < w - 3) {\
60
+            CHECK(-1) CHECK(-2) }} }} \
61
+            CHECK( 1) CHECK( 2) }} }} \
62
+        }\
59 63
  \
60 64
         if (mode < 2) { \
61 65
             int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
... ...
@@ -93,9 +97,34 @@ static void filter_line_c(void *dst1,
93 93
     uint8_t *prev2 = parity ? prev : cur ;
94 94
     uint8_t *next2 = parity ? cur  : next;
95 95
 
96
-    FILTER
96
+    FILTER(0, w)
97 97
 }
98 98
 
99
+static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
100
+                         int w, int prefs, int mrefs, int parity, int mode,
101
+                         int l_edge)
102
+{
103
+    uint8_t *dst  = dst1;
104
+    uint8_t *prev = prev1;
105
+    uint8_t *cur  = cur1;
106
+    uint8_t *next = next1;
107
+    int x;
108
+    uint8_t *prev2 = parity ? prev : cur ;
109
+    uint8_t *next2 = parity ? cur  : next;
110
+
111
+    FILTER(0, l_edge)
112
+
113
+    dst  = (uint8_t*)dst1  + w - 3;
114
+    prev = (uint8_t*)prev1 + w - 3;
115
+    cur  = (uint8_t*)cur1  + w - 3;
116
+    next = (uint8_t*)next1 + w - 3;
117
+    prev2 = (uint8_t*)(parity ? prev : cur);
118
+    next2 = (uint8_t*)(parity ? cur  : next);
119
+
120
+    FILTER(w - 3, w)
121
+}
122
+
123
+
99 124
 static void filter_line_c_16bit(void *dst1,
100 125
                                 void *prev1, void *cur1, void *next1,
101 126
                                 int w, int prefs, int mrefs, int parity,
... ...
@@ -111,7 +140,31 @@ static void filter_line_c_16bit(void *dst1,
111 111
     mrefs /= 2;
112 112
     prefs /= 2;
113 113
 
114
-    FILTER
114
+    FILTER(0, w)
115
+}
116
+
117
+static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
118
+                               int w, int prefs, int mrefs, int parity, int mode,
119
+                               int l_edge)
120
+{
121
+    uint16_t *dst  = dst1;
122
+    uint16_t *prev = prev1;
123
+    uint16_t *cur  = cur1;
124
+    uint16_t *next = next1;
125
+    int x;
126
+    uint16_t *prev2 = parity ? prev : cur ;
127
+    uint16_t *next2 = parity ? cur  : next;
128
+
129
+    FILTER(0, l_edge)
130
+
131
+    dst   = (uint16_t*)dst1  + w - 3;
132
+    prev  = (uint16_t*)prev1 + w - 3;
133
+    cur   = (uint16_t*)cur1  + w - 3;
134
+    next  = (uint16_t*)next1 + w - 3;
135
+    prev2 = (uint16_t*)(parity ? prev : cur);
136
+    next2 = (uint16_t*)(parity ? cur  : next);
137
+
138
+    FILTER(w - 3, w)
115 139
 }
116 140
 
117 141
 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
... ...
@@ -125,6 +178,7 @@ static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
125 125
         int h = dstpic->video->h;
126 126
         int refs = yadif->cur->linesize[i];
127 127
         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
128
+        int l_edge, l_edge_pix;
128 129
 
129 130
         if (i == 1 || i == 2) {
130 131
         /* Why is this not part of the per-plane description thing? */
... ...
@@ -132,6 +186,12 @@ static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
132 132
             h >>= yadif->csp->log2_chroma_h;
133 133
         }
134 134
 
135
+        /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
136
+         * we need to call the c variant which avoids this for border pixels
137
+         */
138
+        l_edge     = yadif->req_align;
139
+        l_edge_pix = l_edge / df;
140
+
135 141
         for (y = 0; y < h; y++) {
136 142
             if ((y ^ parity) & 1) {
137 143
                 uint8_t *prev = &yadif->prev->data[i][y * refs];
... ...
@@ -139,10 +199,22 @@ static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
139 139
                 uint8_t *next = &yadif->next->data[i][y * refs];
140 140
                 uint8_t *dst  = &dstpic->data[i][y * dstpic->linesize[i]];
141 141
                 int     mode  = y == 1 || y + 2 == h ? 2 : yadif->mode;
142
-                yadif->filter_line(dst, prev, cur, next, w,
143
-                                   y + 1 < h ? refs : -refs,
144
-                                   y ? -refs : refs,
145
-                                   parity ^ tff, mode);
142
+                if (yadif->req_align) {
143
+                    yadif->filter_line(dst + l_edge, prev + l_edge, cur + l_edge,
144
+                                       next + l_edge, w - l_edge_pix - 3,
145
+                                       y + 1 < h ? refs : -refs,
146
+                                       y ? -refs : refs,
147
+                                       parity ^ tff, mode);
148
+                    yadif->filter_edges(dst, prev, cur, next, w,
149
+                                         y + 1 < h ? refs : -refs,
150
+                                         y ? -refs : refs,
151
+                                         parity ^ tff, mode, l_edge_pix);
152
+                } else {
153
+                    yadif->filter_line(dst, prev, cur, next + l_edge, w,
154
+                                       y + 1 < h ? refs : -refs,
155
+                                       y ? -refs : refs,
156
+                                       parity ^ tff, mode);
157
+                }
146 158
             } else {
147 159
                 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
148 160
                        &yadif->cur->data[i][y * refs], w * df);
... ...
@@ -391,9 +463,11 @@ static int config_props(AVFilterLink *link)
391 391
 
392 392
     s->csp = av_pix_fmt_desc_get(link->format);
393 393
     if (s->csp->comp[0].depth_minus1 / 8 == 1) {
394
-        s->filter_line = filter_line_c_16bit;
394
+        s->filter_line  = filter_line_c_16bit;
395
+        s->filter_edges = filter_edges_16bit;
395 396
     } else {
396
-        s->filter_line = filter_line_c;
397
+        s->filter_line  = filter_line_c;
398
+        s->filter_edges = filter_edges;
397 399
 
398 400
         if (ARCH_X86)
399 401
             ff_yadif_init_x86(s);
... ...
@@ -43,12 +43,18 @@ av_cold void ff_yadif_init_x86(YADIFContext *yadif)
43 43
 
44 44
 #if HAVE_YASM
45 45
 #if ARCH_X86_32
46
-    if (EXTERNAL_MMXEXT(cpu_flags))
46
+    if (EXTERNAL_MMXEXT(cpu_flags)) {
47 47
         yadif->filter_line = ff_yadif_filter_line_mmxext;
48
+        yadif->req_align   = 8;
49
+    }
48 50
 #endif /* ARCH_X86_32 */
49
-    if (EXTERNAL_SSE2(cpu_flags))
51
+    if (EXTERNAL_SSE2(cpu_flags)) {
50 52
         yadif->filter_line = ff_yadif_filter_line_sse2;
51
-    if (EXTERNAL_SSSE3(cpu_flags))
53
+        yadif->req_align   = 16;
54
+    }
55
+    if (EXTERNAL_SSSE3(cpu_flags)) {
52 56
         yadif->filter_line = ff_yadif_filter_line_ssse3;
57
+        yadif->req_align   = 16;
58
+    }
53 59
 #endif /* HAVE_YASM */
54 60
 }
... ...
@@ -50,9 +50,17 @@ typedef struct YADIFContext {
50 50
     AVFilterBufferRef *next;
51 51
     AVFilterBufferRef *prev;
52 52
     AVFilterBufferRef *out;
53
+
54
+    /**
55
+     * Required alignment for filter_line
56
+     */
57
+    int req_align;
53 58
     void (*filter_line)(void *dst,
54 59
                         void *prev, void *cur, void *next,
55 60
                         int w, int prefs, int mrefs, int parity, int mode);
61
+    void (*filter_edges)(void *dst, void *prev, void *cur, void *next,
62
+                         int w, int prefs, int mrefs, int parity, int mode,
63
+                         int l_edge);
56 64
 
57 65
     const AVPixFmtDescriptor *csp;
58 66
     int eof;
... ...
@@ -1,32 +1,32 @@
1 1
 #tb 0: 1/180000
2
-0,      64800,      64800,        0,   622080, 0x4440caef
3
-0,      72000,      72000,        0,   622080, 0xce67e69d
4
-0,      79200,      79200,        0,   622080, 0x1dbdc653
5
-0,      86400,      86400,        0,   622080, 0x82c591d1
6
-0,      93600,      93600,        0,   622080, 0x8193740b
7
-0,     100800,     100800,        0,   622080, 0xcb219711
8
-0,     108000,     108000,        0,   622080, 0x1870783b
9
-0,     115200,     115200,        0,   622080, 0x7080590b
10
-0,     122400,     122400,        0,   622080, 0x6df4175d
11
-0,     129600,     129600,        0,   622080, 0x6b530e95
12
-0,     136800,     136800,        0,   622080, 0x7f9d66f7
13
-0,     144000,     144000,        0,   622080, 0x338cda81
14
-0,     151200,     151200,        0,   622080, 0xb13797f8
15
-0,     158400,     158400,        0,   622080, 0xb51e7ca4
16
-0,     165600,     165600,        0,   622080, 0x353eed75
17
-0,     172800,     172800,        0,   622080, 0xf93e92b0
18
-0,     180000,     180000,        0,   622080, 0xd0811094
19
-0,     187200,     187200,        0,   622080, 0xb04a3141
20
-0,     194400,     194400,        0,   622080, 0x4ab84909
21
-0,     201600,     201600,        0,   622080, 0xa0fcb8fb
22
-0,     208800,     208800,        0,   622080, 0x9003aebb
23
-0,     216000,     216000,        0,   622080, 0x153faa3e
24
-0,     223200,     223200,        0,   622080, 0xae724063
25
-0,     230400,     230400,        0,   622080, 0xeb4de77a
26
-0,     237600,     237600,        0,   622080, 0x209ed8c7
27
-0,     244800,     244800,        0,   622080, 0xe2bbac96
28
-0,     252000,     252000,        0,   622080, 0xe945441e
29
-0,     259200,     259200,        0,   622080, 0x8f8cbd5f
30
-0,     266400,     266400,        0,   622080, 0xbc3cf717
31
-0,     273600,     273600,        0,   622080, 0x0109f125
32
-0,     280800,     280800,        0,   622080, 0x230c373f
2
+0,      64800,      64800,        0,   622080, 0x6331caee
3
+0,      72000,      72000,        0,   622080, 0xa459e690
4
+0,      79200,      79200,        0,   622080, 0x6429c648
5
+0,      86400,      86400,        0,   622080, 0xa49891ca
6
+0,      93600,      93600,        0,   622080, 0x2a887404
7
+0,     100800,     100800,        0,   622080, 0xe8d49705
8
+0,     108000,     108000,        0,   622080, 0x1b627835
9
+0,     115200,     115200,        0,   622080, 0x686858fd
10
+0,     122400,     122400,        0,   622080, 0x2675174f
11
+0,     129600,     129600,        0,   622080, 0x78470e7f
12
+0,     136800,     136800,        0,   622080, 0xffb366ec
13
+0,     144000,     144000,        0,   622080, 0xd575da72
14
+0,     151200,     151200,        0,   622080, 0x5fb297f7
15
+0,     158400,     158400,        0,   622080, 0xbac77ca0
16
+0,     165600,     165600,        0,   622080, 0x3276ed72
17
+0,     172800,     172800,        0,   622080, 0x264092b2
18
+0,     180000,     180000,        0,   622080, 0x20ba1094
19
+0,     187200,     187200,        0,   622080, 0x76cc3139
20
+0,     194400,     194400,        0,   622080, 0x469a4902
21
+0,     201600,     201600,        0,   622080, 0x0ed7b8f5
22
+0,     208800,     208800,        0,   622080, 0xdc51aeac
23
+0,     216000,     216000,        0,   622080, 0xee06aa36
24
+0,     223200,     223200,        0,   622080, 0x7372405f
25
+0,     230400,     230400,        0,   622080, 0x9e0ee776
26
+0,     237600,     237600,        0,   622080, 0x39e6d8c9
27
+0,     244800,     244800,        0,   622080, 0x51d9ac9a
28
+0,     252000,     252000,        0,   622080, 0x2b63441d
29
+0,     259200,     259200,        0,   622080, 0x58afbd5e
30
+0,     266400,     266400,        0,   622080, 0xb972f716
31
+0,     273600,     273600,        0,   622080, 0x6a6df129
32
+0,     280800,     280800,        0,   622080, 0x28b1373d
... ...
@@ -1,63 +1,63 @@
1 1
 #tb 0: 1/180000
2
-0,      64800,      64800,        0,   622080, 0x4440caef
3
-0,      68400,      68400,        0,   622080, 0xa5cea88b
4
-0,      72000,      72000,        0,   622080, 0xce67e69d
5
-0,      75600,      75600,        0,   622080, 0x9a57891f
6
-0,      79200,      79200,        0,   622080, 0x1dbdc653
7
-0,      82800,      82800,        0,   622080, 0xc171c0c5
8
-0,      86400,      86400,        0,   622080, 0x82c591d1
9
-0,      90000,      90000,        0,   622080, 0x20db9890
10
-0,      93600,      93600,        0,   622080, 0x8193740b
11
-0,      97200,      97200,        0,   622080, 0xdb181d52
12
-0,     100800,     100800,        0,   622080, 0xcb219711
13
-0,     104400,     104400,        0,   622080, 0xc2b913d1
14
-0,     108000,     108000,        0,   622080, 0x1870783b
15
-0,     111600,     111600,        0,   622080, 0xf1d9c5fb
16
-0,     115200,     115200,        0,   622080, 0x7080590b
17
-0,     118800,     118800,        0,   622080, 0x669c5775
18
-0,     122400,     122400,        0,   622080, 0x6df4175d
19
-0,     126000,     126000,        0,   622080, 0x01921a16
20
-0,     129600,     129600,        0,   622080, 0x6b530e95
21
-0,     133200,     133200,        0,   622080, 0xd5047bc9
22
-0,     136800,     136800,        0,   622080, 0x7f9d66f7
23
-0,     140400,     140400,        0,   622080, 0xa8b006eb
24
-0,     144000,     144000,        0,   622080, 0x338cda81
25
-0,     147600,     147600,        0,   622080, 0xf0e125a7
26
-0,     151200,     151200,        0,   622080, 0xb13797f8
27
-0,     154800,     154800,        0,   622080, 0x4afe2976
28
-0,     158400,     158400,        0,   622080, 0xb51e7ca4
29
-0,     162000,     162000,        0,   622080, 0x637fcbfe
30
-0,     165600,     165600,        0,   622080, 0x353eed75
31
-0,     169200,     169200,        0,   622080, 0xd9a8f5ac
32
-0,     172800,     172800,        0,   622080, 0xf93e92b0
33
-0,     176400,     176400,        0,   622080, 0x4540039f
34
-0,     180000,     180000,        0,   622080, 0xd0811094
35
-0,     183600,     183600,        0,   622080, 0x3039906f
36
-0,     187200,     187200,        0,   622080, 0xb04a3141
37
-0,     190800,     190800,        0,   622080, 0x638d2cf5
38
-0,     194400,     194400,        0,   622080, 0x4ab84909
39
-0,     198000,     198000,        0,   622080, 0x82de12ee
40
-0,     201600,     201600,        0,   622080, 0xa0fcb8fb
41
-0,     205200,     205200,        0,   622080, 0x7e849cc9
42
-0,     208800,     208800,        0,   622080, 0x9003aebb
43
-0,     212400,     212400,        0,   622080, 0xffe6f770
44
-0,     216000,     216000,        0,   622080, 0x153faa3e
45
-0,     219600,     219600,        0,   622080, 0xbf023231
46
-0,     223200,     223200,        0,   622080, 0xae724063
47
-0,     226800,     226800,        0,   622080, 0x15fe44b4
48
-0,     230400,     230400,        0,   622080, 0xeb4de77a
49
-0,     234000,     234000,        0,   622080, 0x380f8563
50
-0,     237600,     237600,        0,   622080, 0x209ed8c7
51
-0,     241200,     241200,        0,   622080, 0xb964d70f
52
-0,     244800,     244800,        0,   622080, 0xe2bbac96
53
-0,     248400,     248400,        0,   622080, 0x57e3f7f2
54
-0,     252000,     252000,        0,   622080, 0xe945441e
55
-0,     255600,     255600,        0,   622080, 0xd0afb742
56
-0,     259200,     259200,        0,   622080, 0x8f8cbd5f
57
-0,     262800,     262800,        0,   622080, 0xb9a15294
58
-0,     266400,     266400,        0,   622080, 0xbc3cf717
59
-0,     270000,     270000,        0,   622080, 0xb70b01a9
60
-0,     273600,     273600,        0,   622080, 0x0109f125
61
-0,     277200,     277200,        0,   622080, 0x5806371c
62
-0,     280800,     280800,        0,   622080, 0x230c373f
63
-0,     284400,     284400,        0,   622080, 0x82dfb1f2
2
+0,      64800,      64800,        0,   622080, 0x6331caee
3
+0,      68400,      68400,        0,   622080, 0x625da883
4
+0,      72000,      72000,        0,   622080, 0xa459e690
5
+0,      75600,      75600,        0,   622080, 0xce5d891e
6
+0,      79200,      79200,        0,   622080, 0x6429c648
7
+0,      82800,      82800,        0,   622080, 0x608cc0ba
8
+0,      86400,      86400,        0,   622080, 0xa49891ca
9
+0,      90000,      90000,        0,   622080, 0x9721987f
10
+0,      93600,      93600,        0,   622080, 0x2a887404
11
+0,      97200,      97200,        0,   622080, 0x60d71d47
12
+0,     100800,     100800,        0,   622080, 0xe8d49705
13
+0,     104400,     104400,        0,   622080, 0x821e13cb
14
+0,     108000,     108000,        0,   622080, 0x1b627835
15
+0,     111600,     111600,        0,   622080, 0x1806c5f4
16
+0,     115200,     115200,        0,   622080, 0x686858fd
17
+0,     118800,     118800,        0,   622080, 0xab865773
18
+0,     122400,     122400,        0,   622080, 0x2675174f
19
+0,     126000,     126000,        0,   622080, 0x43a61a14
20
+0,     129600,     129600,        0,   622080, 0x78470e7f
21
+0,     133200,     133200,        0,   622080, 0xeb877bc6
22
+0,     136800,     136800,        0,   622080, 0xffb366ec
23
+0,     140400,     140400,        0,   622080, 0xda0906e7
24
+0,     144000,     144000,        0,   622080, 0xd575da72
25
+0,     147600,     147600,        0,   622080, 0x23ae25a4
26
+0,     151200,     151200,        0,   622080, 0x5fb297f7
27
+0,     154800,     154800,        0,   622080, 0x99b32978
28
+0,     158400,     158400,        0,   622080, 0xbac77ca0
29
+0,     162000,     162000,        0,   622080, 0xc1cdcbf9
30
+0,     165600,     165600,        0,   622080, 0x3276ed72
31
+0,     169200,     169200,        0,   622080, 0x4061f5ab
32
+0,     172800,     172800,        0,   622080, 0x264092b2
33
+0,     176400,     176400,        0,   622080, 0xa4e2039e
34
+0,     180000,     180000,        0,   622080, 0x20ba1094
35
+0,     183600,     183600,        0,   622080, 0x984e906e
36
+0,     187200,     187200,        0,   622080, 0x76cc3139
37
+0,     190800,     190800,        0,   622080, 0xf70e2cf6
38
+0,     194400,     194400,        0,   622080, 0x469a4902
39
+0,     198000,     198000,        0,   622080, 0x235312e6
40
+0,     201600,     201600,        0,   622080, 0x0ed7b8f5
41
+0,     205200,     205200,        0,   622080, 0xd0269cc3
42
+0,     208800,     208800,        0,   622080, 0xdc51aeac
43
+0,     212400,     212400,        0,   622080, 0x1aa5f76e
44
+0,     216000,     216000,        0,   622080, 0xee06aa36
45
+0,     219600,     219600,        0,   622080, 0xa7103230
46
+0,     223200,     223200,        0,   622080, 0x7372405f
47
+0,     226800,     226800,        0,   622080, 0x8d7a44b5
48
+0,     230400,     230400,        0,   622080, 0x9e0ee776
49
+0,     234000,     234000,        0,   622080, 0xd41e8560
50
+0,     237600,     237600,        0,   622080, 0x39e6d8c9
51
+0,     241200,     241200,        0,   622080, 0x7a23d70c
52
+0,     244800,     244800,        0,   622080, 0x51d9ac9a
53
+0,     248400,     248400,        0,   622080, 0x8eacf7f2
54
+0,     252000,     252000,        0,   622080, 0x2b63441d
55
+0,     255600,     255600,        0,   622080, 0x9f71b742
56
+0,     259200,     259200,        0,   622080, 0x58afbd5e
57
+0,     262800,     262800,        0,   622080, 0x4d645292
58
+0,     266400,     266400,        0,   622080, 0xb972f716
59
+0,     270000,     270000,        0,   622080, 0xbb5d01a2
60
+0,     273600,     273600,        0,   622080, 0x6a6df129
61
+0,     277200,     277200,        0,   622080, 0x9e45371e
62
+0,     280800,     280800,        0,   622080, 0x28b1373d
63
+0,     284400,     284400,        0,   622080, 0xa1cdb1f2