Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2012 Laurent de Soras | ||
3 | * Copyright (c) 2013 Fredrik Mellbin | ||
4 | * Copyright (c) 2015 Paul B Mahol | ||
5 | * Copyright (c) 2015 James Darnley | ||
6 | * | ||
7 | * This file is part of FFmpeg. | ||
8 | * | ||
9 | * FFmpeg is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU Lesser General Public | ||
11 | * License as published by the Free Software Foundation; either | ||
12 | * version 2.1 of the License, or (at your option) any later version. | ||
13 | * | ||
14 | * FFmpeg 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 GNU | ||
17 | * Lesser General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public | ||
20 | * License along with FFmpeg; if not, write to the Free Software | ||
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
22 | */ | ||
23 | |||
24 | #include "libavutil/imgutils.h" | ||
25 | #include "libavutil/opt.h" | ||
26 | #include "libavutil/pixdesc.h" | ||
27 | #include "libavutil/qsort.h" | ||
28 | #include "avfilter.h" | ||
29 | #include "filters.h" | ||
30 | #include "removegrain.h" | ||
31 | #include "video.h" | ||
32 | |||
33 | #define OFFSET(x) offsetof(RemoveGrainContext, x) | ||
34 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
35 | |||
36 | static const AVOption removegrain_options[] = { | ||
37 | { "m0", "set mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS }, | ||
38 | { "m1", "set mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS }, | ||
39 | { "m2", "set mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS }, | ||
40 | { "m3", "set mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS }, | ||
41 | {NULL} | ||
42 | }; | ||
43 | |||
44 | AVFILTER_DEFINE_CLASS(removegrain); | ||
45 | |||
46 | static const enum AVPixelFormat pix_fmts[] = { | ||
47 | AV_PIX_FMT_GRAY8, | ||
48 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, | ||
49 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, | ||
50 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, | ||
51 | AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, | ||
52 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, | ||
53 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, | ||
54 | AV_PIX_FMT_NONE | ||
55 | }; | ||
56 | |||
57 | #define REMOVE_GRAIN_SORT_AXIS \ | ||
58 | const int ma1 = FFMAX(a1, a8); \ | ||
59 | const int mi1 = FFMIN(a1, a8); \ | ||
60 | const int ma2 = FFMAX(a2, a7); \ | ||
61 | const int mi2 = FFMIN(a2, a7); \ | ||
62 | const int ma3 = FFMAX(a3, a6); \ | ||
63 | const int mi3 = FFMIN(a3, a6); \ | ||
64 | const int ma4 = FFMAX(a4, a5); \ | ||
65 | const int mi4 = FFMIN(a4, a5); | ||
66 | |||
67 | 299032 | static int mode01(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
68 | { | ||
69 | 299032 | const int mi = FFMIN(FFMIN(FFMIN(a1, a2), FFMIN(a3, a4)), FFMIN(FFMIN(a5, a6), FFMIN(a7, a8))); | |
70 | 299032 | const int ma = FFMAX(FFMAX(FFMAX(a1, a2), FFMAX(a3, a4)), FFMAX(FFMAX(a5, a6), FFMAX(a7, a8))); | |
71 | |||
72 | 299032 | return av_clip(c, mi, ma); | |
73 | } | ||
74 | |||
75 | 16185717 | static int cmp_int(const void *p1, const void *p2) | |
76 | { | ||
77 | 16185717 | int left = *(const int *)p1; | |
78 | 16185717 | int right = *(const int *)p2; | |
79 | 16185717 | return FFDIFFSIGN(left, right); | |
80 | } | ||
81 | |||
82 | 299032 | static int mode02(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
83 | { | ||
84 | 299032 | int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 }; | |
85 | |||
86 |
44/44✓ Branch 0 taken 860883 times.
✓ Branch 1 taken 312456 times.
✓ Branch 3 taken 375538 times.
✓ Branch 4 taken 485345 times.
✓ Branch 6 taken 69650 times.
✓ Branch 7 taken 305888 times.
✓ Branch 9 taken 196964 times.
✓ Branch 10 taken 288381 times.
✓ Branch 12 taken 354268 times.
✓ Branch 13 taken 506615 times.
✓ Branch 14 taken 253217 times.
✓ Branch 15 taken 607666 times.
✓ Branch 16 taken 1303467 times.
✓ Branch 17 taken 245585 times.
✓ Branch 19 taken 812205 times.
✓ Branch 20 taken 491262 times.
✓ Branch 21 taken 1070603 times.
✓ Branch 22 taken 534679 times.
✓ Branch 24 taken 868435 times.
✓ Branch 25 taken 202168 times.
✓ Branch 26 taken 202168 times.
✓ Branch 27 taken 534679 times.
✓ Branch 28 taken 736847 times.
✓ Branch 29 taken 607666 times.
✓ Branch 30 taken 191405 times.
✓ Branch 31 taken 416261 times.
✓ Branch 32 taken 174734 times.
✓ Branch 33 taken 16671 times.
✓ Branch 34 taken 29210 times.
✓ Branch 35 taken 145524 times.
✓ Branch 36 taken 126064 times.
✓ Branch 37 taken 20113 times.
✓ Branch 39 taken 100296 times.
✓ Branch 40 taken 25768 times.
✓ Branch 41 taken 20113 times.
✓ Branch 42 taken 25768 times.
✓ Branch 43 taken 285761 times.
✓ Branch 44 taken 301792 times.
✓ Branch 46 taken 99775 times.
✓ Branch 47 taken 212681 times.
✓ Branch 48 taken 1173339 times.
✓ Branch 49 taken 300799 times.
✓ Branch 50 taken 886585 times.
✓ Branch 51 taken 299032 times.
|
4898619 | AV_QSORT(a, 8, int, cmp_int); |
87 | |||
88 | 299032 | return av_clip(c, a[2 - 1 ], a[7 - 1]); | |
89 | } | ||
90 | |||
91 | 299032 | static int mode03(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
92 | { | ||
93 | 299032 | int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 }; | |
94 | |||
95 |
44/44✓ Branch 0 taken 860883 times.
✓ Branch 1 taken 312456 times.
✓ Branch 3 taken 375538 times.
✓ Branch 4 taken 485345 times.
✓ Branch 6 taken 69650 times.
✓ Branch 7 taken 305888 times.
✓ Branch 9 taken 196964 times.
✓ Branch 10 taken 288381 times.
✓ Branch 12 taken 354268 times.
✓ Branch 13 taken 506615 times.
✓ Branch 14 taken 253217 times.
✓ Branch 15 taken 607666 times.
✓ Branch 16 taken 1303467 times.
✓ Branch 17 taken 245585 times.
✓ Branch 19 taken 812205 times.
✓ Branch 20 taken 491262 times.
✓ Branch 21 taken 1070603 times.
✓ Branch 22 taken 534679 times.
✓ Branch 24 taken 868435 times.
✓ Branch 25 taken 202168 times.
✓ Branch 26 taken 202168 times.
✓ Branch 27 taken 534679 times.
✓ Branch 28 taken 736847 times.
✓ Branch 29 taken 607666 times.
✓ Branch 30 taken 191405 times.
✓ Branch 31 taken 416261 times.
✓ Branch 32 taken 174734 times.
✓ Branch 33 taken 16671 times.
✓ Branch 34 taken 29210 times.
✓ Branch 35 taken 145524 times.
✓ Branch 36 taken 126064 times.
✓ Branch 37 taken 20113 times.
✓ Branch 39 taken 100296 times.
✓ Branch 40 taken 25768 times.
✓ Branch 41 taken 20113 times.
✓ Branch 42 taken 25768 times.
✓ Branch 43 taken 285761 times.
✓ Branch 44 taken 301792 times.
✓ Branch 46 taken 99775 times.
✓ Branch 47 taken 212681 times.
✓ Branch 48 taken 1173339 times.
✓ Branch 49 taken 300799 times.
✓ Branch 50 taken 886585 times.
✓ Branch 51 taken 299032 times.
|
4898619 | AV_QSORT(a, 8, int, cmp_int); |
96 | |||
97 | 299032 | return av_clip(c, a[3 - 1 ], a[6 - 1]); | |
98 | } | ||
99 | |||
100 | 299032 | static int mode04(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
101 | { | ||
102 | 299032 | int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 }; | |
103 | |||
104 |
44/44✓ Branch 0 taken 860883 times.
✓ Branch 1 taken 312456 times.
✓ Branch 3 taken 375538 times.
✓ Branch 4 taken 485345 times.
✓ Branch 6 taken 69650 times.
✓ Branch 7 taken 305888 times.
✓ Branch 9 taken 196964 times.
✓ Branch 10 taken 288381 times.
✓ Branch 12 taken 354268 times.
✓ Branch 13 taken 506615 times.
✓ Branch 14 taken 253217 times.
✓ Branch 15 taken 607666 times.
✓ Branch 16 taken 1303467 times.
✓ Branch 17 taken 245585 times.
✓ Branch 19 taken 812205 times.
✓ Branch 20 taken 491262 times.
✓ Branch 21 taken 1070603 times.
✓ Branch 22 taken 534679 times.
✓ Branch 24 taken 868435 times.
✓ Branch 25 taken 202168 times.
✓ Branch 26 taken 202168 times.
✓ Branch 27 taken 534679 times.
✓ Branch 28 taken 736847 times.
✓ Branch 29 taken 607666 times.
✓ Branch 30 taken 191405 times.
✓ Branch 31 taken 416261 times.
✓ Branch 32 taken 174734 times.
✓ Branch 33 taken 16671 times.
✓ Branch 34 taken 29210 times.
✓ Branch 35 taken 145524 times.
✓ Branch 36 taken 126064 times.
✓ Branch 37 taken 20113 times.
✓ Branch 39 taken 100296 times.
✓ Branch 40 taken 25768 times.
✓ Branch 41 taken 20113 times.
✓ Branch 42 taken 25768 times.
✓ Branch 43 taken 285761 times.
✓ Branch 44 taken 301792 times.
✓ Branch 46 taken 99775 times.
✓ Branch 47 taken 212681 times.
✓ Branch 48 taken 1173339 times.
✓ Branch 49 taken 300799 times.
✓ Branch 50 taken 886585 times.
✓ Branch 51 taken 299032 times.
|
4898619 | AV_QSORT(a, 8, int, cmp_int); |
105 | |||
106 | 299032 | return av_clip(c, a[4 - 1 ], a[5 - 1]); | |
107 | } | ||
108 | |||
109 | 299032 | static int mode05(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
110 | { | ||
111 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
112 | |||
113 | 299032 | const int c1 = FFABS(c - av_clip(c, mi1, ma1)); | |
114 | 299032 | const int c2 = FFABS(c - av_clip(c, mi2, ma2)); | |
115 | 299032 | const int c3 = FFABS(c - av_clip(c, mi3, ma3)); | |
116 | 299032 | const int c4 = FFABS(c - av_clip(c, mi4, ma4)); | |
117 | |||
118 | 299032 | const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4)); | |
119 | |||
120 | /* When adding SIMD notice the return order here: 4, 2, 3, 1. */ | ||
121 |
2/2✓ Branch 0 taken 232702 times.
✓ Branch 1 taken 66330 times.
|
299032 | if (mindiff == c4) { |
122 | 232702 | return av_clip(c, mi4, ma4); | |
123 |
2/2✓ Branch 0 taken 29812 times.
✓ Branch 1 taken 36518 times.
|
66330 | } else if (mindiff == c2) { |
124 | 29812 | return av_clip(c, mi2, ma2); | |
125 |
2/2✓ Branch 0 taken 28341 times.
✓ Branch 1 taken 8177 times.
|
36518 | } else if (mindiff == c3) { |
126 | 28341 | return av_clip(c, mi3, ma3); | |
127 | } | ||
128 | |||
129 | 8177 | return av_clip(c, mi1, ma1); | |
130 | } | ||
131 | |||
132 | 299032 | static int mode06(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
133 | { | ||
134 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
135 | |||
136 | 299032 | const int d1 = ma1 - mi1; | |
137 | 299032 | const int d2 = ma2 - mi2; | |
138 | 299032 | const int d3 = ma3 - mi3; | |
139 | 299032 | const int d4 = ma4 - mi4; | |
140 | |||
141 | 299032 | const int cli1 = av_clip(c, mi1, ma1); | |
142 | 299032 | const int cli2 = av_clip(c, mi2, ma2); | |
143 | 299032 | const int cli3 = av_clip(c, mi3, ma3); | |
144 | 299032 | const int cli4 = av_clip(c, mi4, ma4); | |
145 | |||
146 | 299032 | const int c1 = av_clip_uint16((FFABS(c - cli1) << 1) + d1); | |
147 | 299032 | const int c2 = av_clip_uint16((FFABS(c - cli2) << 1) + d2); | |
148 | 299032 | const int c3 = av_clip_uint16((FFABS(c - cli3) << 1) + d3); | |
149 | 299032 | const int c4 = av_clip_uint16((FFABS(c - cli4) << 1) + d4); | |
150 | |||
151 | 299032 | const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4)); | |
152 | |||
153 |
2/2✓ Branch 0 taken 60423 times.
✓ Branch 1 taken 238609 times.
|
299032 | if (mindiff == c4) { |
154 | 60423 | return cli4; | |
155 |
2/2✓ Branch 0 taken 53708 times.
✓ Branch 1 taken 184901 times.
|
238609 | } else if (mindiff == c2) { |
156 | 53708 | return cli2; | |
157 |
2/2✓ Branch 0 taken 170425 times.
✓ Branch 1 taken 14476 times.
|
184901 | } else if (mindiff == c3) { |
158 | 170425 | return cli3; | |
159 | } | ||
160 | |||
161 | 14476 | return cli1; | |
162 | } | ||
163 | |||
164 | 299032 | static int mode07(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
165 | { | ||
166 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
167 | |||
168 | 299032 | const int d1 = ma1 - mi1; | |
169 | 299032 | const int d2 = ma2 - mi2; | |
170 | 299032 | const int d3 = ma3 - mi3; | |
171 | 299032 | const int d4 = ma4 - mi4; | |
172 | |||
173 | 299032 | const int cli1 = av_clip(c, mi1, ma1); | |
174 | 299032 | const int cli2 = av_clip(c, mi2, ma2); | |
175 | 299032 | const int cli3 = av_clip(c, mi3, ma3); | |
176 | 299032 | const int cli4 = av_clip(c, mi4, ma4); | |
177 | |||
178 | 299032 | const int c1 = FFABS(c - cli1) + d1; | |
179 | 299032 | const int c2 = FFABS(c - cli2) + d2; | |
180 | 299032 | const int c3 = FFABS(c - cli3) + d3; | |
181 | 299032 | const int c4 = FFABS(c - cli4) + d4; | |
182 | |||
183 | 299032 | const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4)); | |
184 | |||
185 |
2/2✓ Branch 0 taken 60658 times.
✓ Branch 1 taken 238374 times.
|
299032 | if (mindiff == c4) { |
186 | 60658 | return cli4; | |
187 |
2/2✓ Branch 0 taken 55773 times.
✓ Branch 1 taken 182601 times.
|
238374 | } else if (mindiff == c2) { |
188 | 55773 | return cli2; | |
189 |
2/2✓ Branch 0 taken 168299 times.
✓ Branch 1 taken 14302 times.
|
182601 | } else if (mindiff == c3) { |
190 | 168299 | return cli3; | |
191 | } | ||
192 | |||
193 | 14302 | return cli1; | |
194 | } | ||
195 | |||
196 | 299032 | static int mode08(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
197 | { | ||
198 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
199 | |||
200 | 299032 | const int d1 = ma1 - mi1; | |
201 | 299032 | const int d2 = ma2 - mi2; | |
202 | 299032 | const int d3 = ma3 - mi3; | |
203 | 299032 | const int d4 = ma4 - mi4; | |
204 | |||
205 | 299032 | const int cli1 = av_clip(c, mi1, ma1); | |
206 | 299032 | const int cli2 = av_clip(c, mi2, ma2); | |
207 | 299032 | const int cli3 = av_clip(c, mi3, ma3); | |
208 | 299032 | const int cli4 = av_clip(c, mi4, ma4); | |
209 | |||
210 | 299032 | const int c1 = av_clip_uint16(FFABS(c - cli1) + (d1 << 1)); | |
211 | 299032 | const int c2 = av_clip_uint16(FFABS(c - cli2) + (d2 << 1)); | |
212 | 299032 | const int c3 = av_clip_uint16(FFABS(c - cli3) + (d3 << 1)); | |
213 | 299032 | const int c4 = av_clip_uint16(FFABS(c - cli4) + (d4 << 1)); | |
214 | |||
215 | 299032 | const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4)); | |
216 | |||
217 |
2/2✓ Branch 0 taken 62194 times.
✓ Branch 1 taken 236838 times.
|
299032 | if (mindiff == c4) { |
218 | 62194 | return cli4; | |
219 |
2/2✓ Branch 0 taken 50307 times.
✓ Branch 1 taken 186531 times.
|
236838 | } else if (mindiff == c2) { |
220 | 50307 | return cli2; | |
221 |
2/2✓ Branch 0 taken 166320 times.
✓ Branch 1 taken 20211 times.
|
186531 | } else if (mindiff == c3) { |
222 | 166320 | return cli3; | |
223 | } | ||
224 | |||
225 | 20211 | return cli1; | |
226 | } | ||
227 | |||
228 | 299032 | static int mode09(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
229 | { | ||
230 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
231 | |||
232 | 299032 | const int d1 = ma1 - mi1; | |
233 | 299032 | const int d2 = ma2 - mi2; | |
234 | 299032 | const int d3 = ma3 - mi3; | |
235 | 299032 | const int d4 = ma4 - mi4; | |
236 | |||
237 | 299032 | const int mindiff = FFMIN(FFMIN(d1, d2), FFMIN(d3, d4)); | |
238 | |||
239 |
2/2✓ Branch 0 taken 62551 times.
✓ Branch 1 taken 236481 times.
|
299032 | if (mindiff == d4) { |
240 | 62551 | return av_clip(c, mi4, ma4); | |
241 |
2/2✓ Branch 0 taken 50330 times.
✓ Branch 1 taken 186151 times.
|
236481 | } else if (mindiff == d2) { |
242 | 50330 | return av_clip(c, mi2, ma2); | |
243 |
2/2✓ Branch 0 taken 165787 times.
✓ Branch 1 taken 20364 times.
|
186151 | } else if (mindiff == d3) { |
244 | 165787 | return av_clip(c, mi3, ma3); | |
245 | } | ||
246 | |||
247 | 20364 | return av_clip(c, mi1, ma1); | |
248 | } | ||
249 | |||
250 | 299032 | static int mode10(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
251 | { | ||
252 | 299032 | const int d1 = FFABS(c - a1); | |
253 | 299032 | const int d2 = FFABS(c - a2); | |
254 | 299032 | const int d3 = FFABS(c - a3); | |
255 | 299032 | const int d4 = FFABS(c - a4); | |
256 | 299032 | const int d5 = FFABS(c - a5); | |
257 | 299032 | const int d6 = FFABS(c - a6); | |
258 | 299032 | const int d7 = FFABS(c - a7); | |
259 | 299032 | const int d8 = FFABS(c - a8); | |
260 | |||
261 | 299032 | const int mindiff = FFMIN(FFMIN(FFMIN(d1, d2), FFMIN(d3, d4)), | |
262 | FFMIN(FFMIN(d5, d6), FFMIN(d7, d8))); | ||
263 | |||
264 |
2/2✓ Branch 0 taken 28483 times.
✓ Branch 1 taken 270549 times.
|
299032 | if (mindiff == d7) return a7; |
265 |
2/2✓ Branch 0 taken 9255 times.
✓ Branch 1 taken 261294 times.
|
270549 | if (mindiff == d8) return a8; |
266 |
2/2✓ Branch 0 taken 96531 times.
✓ Branch 1 taken 164763 times.
|
261294 | if (mindiff == d6) return a6; |
267 |
2/2✓ Branch 0 taken 25442 times.
✓ Branch 1 taken 139321 times.
|
164763 | if (mindiff == d2) return a2; |
268 |
2/2✓ Branch 0 taken 82553 times.
✓ Branch 1 taken 56768 times.
|
139321 | if (mindiff == d3) return a3; |
269 |
2/2✓ Branch 0 taken 7142 times.
✓ Branch 1 taken 49626 times.
|
56768 | if (mindiff == d1) return a1; |
270 |
2/2✓ Branch 0 taken 37857 times.
✓ Branch 1 taken 11769 times.
|
49626 | if (mindiff == d5) return a5; |
271 | |||
272 | 11769 | return a4; | |
273 | } | ||
274 | |||
275 | 598064 | static int mode1112(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
276 | { | ||
277 | 598064 | const int sum = 4 * c + 2 * (a2 + a4 + a5 + a7) + a1 + a3 + a6 + a8; | |
278 | 598064 | const int val = (sum + 8) >> 4; | |
279 | |||
280 | 598064 | return val; | |
281 | } | ||
282 | |||
283 | 299032 | static int mode1314(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
284 | { | ||
285 | 299032 | const int d1 = FFABS(a1 - a8); | |
286 | 299032 | const int d2 = FFABS(a2 - a7); | |
287 | 299032 | const int d3 = FFABS(a3 - a6); | |
288 | |||
289 | 299032 | const int mindiff = FFMIN(FFMIN(d1, d2), d3); | |
290 | |||
291 |
2/2✓ Branch 0 taken 59292 times.
✓ Branch 1 taken 239740 times.
|
299032 | if (mindiff == d2) { |
292 | 59292 | return (a2 + a7 + 1) >> 1; | |
293 | } | ||
294 |
2/2✓ Branch 0 taken 199746 times.
✓ Branch 1 taken 39994 times.
|
239740 | if (mindiff == d3) { |
295 | 199746 | return (a3 + a6 + 1) >> 1; | |
296 | } | ||
297 | |||
298 | 39994 | return (a1 + a8 + 1) >> 1; | |
299 | } | ||
300 | |||
301 | 299032 | static int mode1516(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
302 | { | ||
303 | 299032 | const int d1 = FFABS(a1 - a8); | |
304 | 299032 | const int d2 = FFABS(a2 - a7); | |
305 | 299032 | const int d3 = FFABS(a3 - a6); | |
306 | |||
307 | 299032 | const int mindiff = FFMIN(FFMIN(d1, d2), d3); | |
308 | 299032 | const int average = (2 * (a2 + a7) + a1 + a3 + a6 + a8 + 4) >> 3; | |
309 | |||
310 |
2/2✓ Branch 0 taken 59292 times.
✓ Branch 1 taken 239740 times.
|
299032 | if (mindiff == d2) { |
311 | 59292 | return av_clip(average, FFMIN(a2, a7), FFMAX(a2, a7)); | |
312 | } | ||
313 |
2/2✓ Branch 0 taken 199746 times.
✓ Branch 1 taken 39994 times.
|
239740 | if (mindiff == d3) { |
314 | 199746 | return av_clip(average, FFMIN(a3, a6), FFMAX(a3, a6)); | |
315 | } | ||
316 | |||
317 | 39994 | return av_clip(average, FFMIN(a1, a8), FFMAX(a1, a8)); | |
318 | } | ||
319 | |||
320 | 299032 | static int mode17(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
321 | { | ||
322 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
323 | |||
324 | 299032 | const int l = FFMAX(FFMAX(mi1, mi2), FFMAX(mi3, mi4)); | |
325 | 299032 | const int u = FFMIN(FFMIN(ma1, ma2), FFMIN(ma3, ma4)); | |
326 | |||
327 | 299032 | return av_clip(c, FFMIN(l, u), FFMAX(l, u)); | |
328 | } | ||
329 | |||
330 | 299032 | static int mode18(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
331 | { | ||
332 | 299032 | const int d1 = FFMAX(FFABS(c - a1), FFABS(c - a8)); | |
333 | 299032 | const int d2 = FFMAX(FFABS(c - a2), FFABS(c - a7)); | |
334 | 299032 | const int d3 = FFMAX(FFABS(c - a3), FFABS(c - a6)); | |
335 | 299032 | const int d4 = FFMAX(FFABS(c - a4), FFABS(c - a5)); | |
336 | |||
337 | 299032 | const int mindiff = FFMIN(FFMIN(d1, d2), FFMIN(d3, d4)); | |
338 | |||
339 |
2/2✓ Branch 0 taken 60820 times.
✓ Branch 1 taken 238212 times.
|
299032 | if (mindiff == d4) { |
340 | 60820 | return av_clip(c, FFMIN(a4, a5), FFMAX(a4, a5)); | |
341 | } | ||
342 |
2/2✓ Branch 0 taken 53824 times.
✓ Branch 1 taken 184388 times.
|
238212 | if (mindiff == d2) { |
343 | 53824 | return av_clip(c, FFMIN(a2, a7), FFMAX(a2, a7)); | |
344 | } | ||
345 |
2/2✓ Branch 0 taken 170276 times.
✓ Branch 1 taken 14112 times.
|
184388 | if (mindiff == d3) { |
346 | 170276 | return av_clip(c, FFMIN(a3, a6), FFMAX(a3, a6)); | |
347 | } | ||
348 | |||
349 | 14112 | return av_clip(c, FFMIN(a1, a8), FFMAX(a1, a8)); | |
350 | } | ||
351 | |||
352 | 299032 | static int mode19(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
353 | { | ||
354 | 299032 | const int sum = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8; | |
355 | 299032 | const int val = (sum + 4) >> 3; | |
356 | |||
357 | 299032 | return val; | |
358 | } | ||
359 | |||
360 | 299032 | static int mode20(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
361 | { | ||
362 | 299032 | const int sum = a1 + a2 + a3 + a4 + c + a5 + a6 + a7 + a8; | |
363 | 299032 | const int val = (sum + 4) / 9; | |
364 | |||
365 | 299032 | return val; | |
366 | } | ||
367 | |||
368 | 299032 | static int mode21(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
369 | { | ||
370 | 299032 | const int l1l = (a1 + a8) >> 1; | |
371 | 299032 | const int l2l = (a2 + a7) >> 1; | |
372 | 299032 | const int l3l = (a3 + a6) >> 1; | |
373 | 299032 | const int l4l = (a4 + a5) >> 1; | |
374 | |||
375 | 299032 | const int l1h = (a1 + a8 + 1) >> 1; | |
376 | 299032 | const int l2h = (a2 + a7 + 1) >> 1; | |
377 | 299032 | const int l3h = (a3 + a6 + 1) >> 1; | |
378 | 299032 | const int l4h = (a4 + a5 + 1) >> 1; | |
379 | |||
380 | 299032 | const int mi = FFMIN(FFMIN(l1l, l2l), FFMIN(l3l, l4l)); | |
381 | 299032 | const int ma = FFMAX(FFMAX(l1h, l2h), FFMAX(l3h, l4h)); | |
382 | |||
383 | 299032 | return av_clip(c, mi, ma); | |
384 | } | ||
385 | |||
386 | 299032 | static int mode22(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
387 | { | ||
388 | 299032 | const int l1 = (a1 + a8 + 1) >> 1; | |
389 | 299032 | const int l2 = (a2 + a7 + 1) >> 1; | |
390 | 299032 | const int l3 = (a3 + a6 + 1) >> 1; | |
391 | 299032 | const int l4 = (a4 + a5 + 1) >> 1; | |
392 | |||
393 | 299032 | const int mi = FFMIN(FFMIN(l1, l2), FFMIN(l3, l4)); | |
394 | 299032 | const int ma = FFMAX(FFMAX(l1, l2), FFMAX(l3, l4)); | |
395 | |||
396 | 299032 | return av_clip(c, mi, ma); | |
397 | } | ||
398 | |||
399 | 299032 | static int mode23(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
400 | { | ||
401 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
402 | |||
403 | 299032 | const int linediff1 = ma1 - mi1; | |
404 | 299032 | const int linediff2 = ma2 - mi2; | |
405 | 299032 | const int linediff3 = ma3 - mi3; | |
406 | 299032 | const int linediff4 = ma4 - mi4; | |
407 | |||
408 | 299032 | const int u1 = FFMIN(c - ma1, linediff1); | |
409 | 299032 | const int u2 = FFMIN(c - ma2, linediff2); | |
410 | 299032 | const int u3 = FFMIN(c - ma3, linediff3); | |
411 | 299032 | const int u4 = FFMIN(c - ma4, linediff4); | |
412 | 299032 | const int u = FFMAX(FFMAX(FFMAX(u1, u2), FFMAX(u3, u4)), 0); | |
413 | |||
414 | 299032 | const int d1 = FFMIN(mi1 - c, linediff1); | |
415 | 299032 | const int d2 = FFMIN(mi2 - c, linediff2); | |
416 | 299032 | const int d3 = FFMIN(mi3 - c, linediff3); | |
417 | 299032 | const int d4 = FFMIN(mi4 - c, linediff4); | |
418 | 299032 | const int d = FFMAX(FFMAX(FFMAX(d1, d2), FFMAX(d3, d4)), 0); | |
419 | |||
420 | 299032 | return c - u + d; // This probably will never overflow. | |
421 | } | ||
422 | |||
423 | 299032 | static int mode24(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
424 | { | ||
425 | 299032 | REMOVE_GRAIN_SORT_AXIS | |
426 | |||
427 | 299032 | const int linediff1 = ma1 - mi1; | |
428 | 299032 | const int linediff2 = ma2 - mi2; | |
429 | 299032 | const int linediff3 = ma3 - mi3; | |
430 | 299032 | const int linediff4 = ma4 - mi4; | |
431 | |||
432 | 299032 | const int tu1 = c - ma1; | |
433 | 299032 | const int tu2 = c - ma2; | |
434 | 299032 | const int tu3 = c - ma3; | |
435 | 299032 | const int tu4 = c - ma4; | |
436 | |||
437 | 299032 | const int u1 = FFMIN(tu1, linediff1 - tu1); | |
438 | 299032 | const int u2 = FFMIN(tu2, linediff2 - tu2); | |
439 | 299032 | const int u3 = FFMIN(tu3, linediff3 - tu3); | |
440 | 299032 | const int u4 = FFMIN(tu4, linediff4 - tu4); | |
441 | 299032 | const int u = FFMAX(FFMAX(FFMAX(u1, u2), FFMAX(u3, u4)), 0); | |
442 | |||
443 | 299032 | const int td1 = mi1 - c; | |
444 | 299032 | const int td2 = mi2 - c; | |
445 | 299032 | const int td3 = mi3 - c; | |
446 | 299032 | const int td4 = mi4 - c; | |
447 | |||
448 | 299032 | const int d1 = FFMIN(td1, linediff1 - td1); | |
449 | 299032 | const int d2 = FFMIN(td2, linediff2 - td2); | |
450 | 299032 | const int d3 = FFMIN(td3, linediff3 - td3); | |
451 | 299032 | const int d4 = FFMIN(td4, linediff4 - td4); | |
452 | 299032 | const int d = FFMAX(FFMAX(FFMAX(d1, d2), FFMAX(d3, d4)), 0); | |
453 | |||
454 | 299032 | return c - u + d; // This probably will never overflow. | |
455 | } | ||
456 | |||
457 | 25 | static int config_input(AVFilterLink *inlink) | |
458 | { | ||
459 | 25 | RemoveGrainContext *s = inlink->dst->priv; | |
460 | 25 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
461 | int i; | ||
462 | |||
463 | 25 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
464 | |||
465 | 25 | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); | |
466 | 25 | s->planeheight[0] = s->planeheight[3] = inlink->h; | |
467 | 25 | s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); | |
468 | 25 | s->planewidth[0] = s->planewidth[3] = inlink->w; | |
469 | |||
470 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 25 times.
|
100 | for (i = 0; i < s->nb_planes; i++) { |
471 |
25/25✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 14 taken 3 times.
✓ Branch 15 taken 3 times.
✓ Branch 16 taken 3 times.
✓ Branch 17 taken 3 times.
✓ Branch 18 taken 3 times.
✓ Branch 19 taken 3 times.
✓ Branch 20 taken 3 times.
✓ Branch 21 taken 3 times.
✓ Branch 22 taken 3 times.
✓ Branch 23 taken 3 times.
✓ Branch 24 taken 3 times.
|
75 | switch (s->mode[i]) { |
472 | 3 | case 1: s->rg[i] = mode01; break; | |
473 | 3 | case 2: s->rg[i] = mode02; break; | |
474 | 3 | case 3: s->rg[i] = mode03; break; | |
475 | 3 | case 4: s->rg[i] = mode04; break; | |
476 | 3 | case 5: s->rg[i] = mode05; break; | |
477 | 3 | case 6: s->rg[i] = mode06; break; | |
478 | 3 | case 7: s->rg[i] = mode07; break; | |
479 | 3 | case 8: s->rg[i] = mode08; break; | |
480 | 3 | case 9: s->rg[i] = mode09; break; | |
481 | 3 | case 10: s->rg[i] = mode10; break; | |
482 | 3 | case 11: s->rg[i] = mode1112; break; | |
483 | 3 | case 12: s->rg[i] = mode1112; break; | |
484 | 3 | case 13: s->skip_odd = 1; | |
485 | 3 | s->rg[i] = mode1314; break; | |
486 | 3 | case 14: s->skip_even = 1; | |
487 | 3 | s->rg[i] = mode1314; break; | |
488 | 3 | case 15: s->skip_odd = 1; | |
489 | 3 | s->rg[i] = mode1516; break; | |
490 | 3 | case 16: s->skip_even = 1; | |
491 | 3 | s->rg[i] = mode1516; break; | |
492 | 3 | case 17: s->rg[i] = mode17; break; | |
493 | 3 | case 18: s->rg[i] = mode18; break; | |
494 | 3 | case 19: s->rg[i] = mode19; break; | |
495 | 3 | case 20: s->rg[i] = mode20; break; | |
496 | 3 | case 21: s->rg[i] = mode21; break; | |
497 | 3 | case 22: s->rg[i] = mode22; break; | |
498 | 3 | case 23: s->rg[i] = mode23; break; | |
499 | 3 | case 24: s->rg[i] = mode24; break; | |
500 | } | ||
501 | } | ||
502 | |||
503 | #if ARCH_X86 | ||
504 | 25 | ff_removegrain_init_x86(s); | |
505 | #endif | ||
506 | |||
507 | 25 | return 0; | |
508 | } | ||
509 | |||
510 | typedef struct ThreadData { | ||
511 | AVFrame *in, *out; | ||
512 | int plane; | ||
513 | } ThreadData; | ||
514 | |||
515 | 1296 | static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
516 | { | ||
517 | 1296 | RemoveGrainContext *s = ctx->priv; | |
518 | 1296 | ThreadData *td = arg; | |
519 | 1296 | AVFrame *in = td->in; | |
520 | 1296 | AVFrame *out = td->out; | |
521 | 1296 | const int i = td->plane; | |
522 | 1296 | const int height = s->planeheight[i]; | |
523 | 1296 | const int om = in->linesize[i] - 1; | |
524 | 1296 | const int o0 = in->linesize[i] ; | |
525 | 1296 | const int op = in->linesize[i] + 1; | |
526 | 1296 | int start = (height * jobnr ) / nb_jobs; | |
527 | 1296 | int end = (height * (jobnr+1)) / nb_jobs; | |
528 | int x, y; | ||
529 | |||
530 | 1296 | start = FFMAX(1, start); | |
531 | 1296 | end = FFMIN(height-1, end); | |
532 |
2/2✓ Branch 0 taken 27360 times.
✓ Branch 1 taken 1296 times.
|
28656 | for (y = start; y < end; y++) { |
533 | 27360 | uint8_t *dst = out->data[i]; | |
534 | 27360 | uint8_t *src = in->data[i]; | |
535 | |||
536 | 27360 | src = in->data[i] + y * in->linesize[i]; | |
537 | 27360 | dst = out->data[i] + y * out->linesize[i]; | |
538 | |||
539 |
4/4✓ Branch 0 taken 2280 times.
✓ Branch 1 taken 25080 times.
✓ Branch 2 taken 1140 times.
✓ Branch 3 taken 1140 times.
|
27360 | if (s->skip_even && !(y & 1)) { |
540 | 1140 | memcpy(dst, src, s->planewidth[i]); | |
541 | 1140 | continue; | |
542 | } | ||
543 |
4/4✓ Branch 0 taken 2280 times.
✓ Branch 1 taken 23940 times.
✓ Branch 2 taken 1140 times.
✓ Branch 3 taken 1140 times.
|
26220 | if (s->skip_odd && y & 1) { |
544 | 1140 | memcpy(dst, src, s->planewidth[i]); | |
545 | 1140 | continue; | |
546 | } | ||
547 | |||
548 | 25080 | *dst++ = *src++; | |
549 | |||
550 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25080 times.
|
25080 | if (s->fl[i]) { |
551 | ✗ | int w_asm = (s->planewidth[i] - 2) & ~15; | |
552 | |||
553 | ✗ | s->fl[i](dst, src, in->linesize[i], w_asm); | |
554 | |||
555 | ✗ | x = 1 + w_asm; | |
556 | ✗ | dst += w_asm; | |
557 | ✗ | src += w_asm; | |
558 | } else | ||
559 | 25080 | x = 1; | |
560 | |||
561 |
2/2✓ Branch 0 taken 6578704 times.
✓ Branch 1 taken 25080 times.
|
6603784 | for (; x < s->planewidth[i] - 1; x++) { |
562 | 6578704 | const int a1 = src[-op]; | |
563 | 6578704 | const int a2 = src[-o0]; | |
564 | 6578704 | const int a3 = src[-om]; | |
565 | 6578704 | const int a4 = src[-1 ]; | |
566 | 6578704 | const int c = src[ 0 ]; | |
567 | 6578704 | const int a5 = src[ 1 ]; | |
568 | 6578704 | const int a6 = src[ om]; | |
569 | 6578704 | const int a7 = src[ o0]; | |
570 | 6578704 | const int a8 = src[ op]; | |
571 | |||
572 | 6578704 | const int res = s->rg[i](c, a1, a2, a3, a4, a5, a6, a7, a8); | |
573 | |||
574 | 6578704 | *dst = res; | |
575 | 6578704 | dst++, src++; | |
576 | } | ||
577 | 25080 | dst[0] = src[0]; | |
578 | } | ||
579 | |||
580 | 1296 | return 0; | |
581 | } | ||
582 | |||
583 | 50 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
584 | { | ||
585 | 50 | AVFilterContext *ctx = inlink->dst; | |
586 | 50 | AVFilterLink *outlink = ctx->outputs[0]; | |
587 | 50 | RemoveGrainContext *s = ctx->priv; | |
588 | ThreadData td; | ||
589 | AVFrame *out; | ||
590 | int i; | ||
591 | |||
592 | 50 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
593 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (!out) { |
594 | ✗ | av_frame_free(&in); | |
595 | ✗ | return AVERROR(ENOMEM); | |
596 | } | ||
597 | 50 | av_frame_copy_props(out, in); | |
598 | |||
599 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 50 times.
|
200 | for (i = 0; i < s->nb_planes; i++) { |
600 | 150 | uint8_t *dst = out->data[i]; | |
601 | 150 | uint8_t *src = in->data[i]; | |
602 | |||
603 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
|
150 | if (s->mode[i] == 0) { |
604 | 6 | av_image_copy_plane(dst, out->linesize[i], | |
605 | 6 | src, in->linesize[i], | |
606 | s->planewidth[i], s->planeheight[i]); | ||
607 | 6 | continue; | |
608 | } | ||
609 | |||
610 | 144 | memcpy(dst, src, s->planewidth[i]); | |
611 | |||
612 | 144 | td.in = in; td.out = out; td.plane = i; | |
613 | 144 | ff_filter_execute(ctx, filter_slice, &td, NULL, | |
614 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | FFMIN(s->planeheight[i], ff_filter_get_nb_threads(ctx))); |
615 | |||
616 | 144 | src = in->data[i] + (s->planeheight[i] - 1) * in->linesize[i]; | |
617 | 144 | dst = out->data[i] + (s->planeheight[i] - 1) * out->linesize[i]; | |
618 | 144 | memcpy(dst, src, s->planewidth[i]); | |
619 | } | ||
620 | |||
621 | 50 | av_frame_free(&in); | |
622 | 50 | return ff_filter_frame(outlink, out); | |
623 | } | ||
624 | |||
625 | static const AVFilterPad removegrain_inputs[] = { | ||
626 | { | ||
627 | .name = "default", | ||
628 | .type = AVMEDIA_TYPE_VIDEO, | ||
629 | .filter_frame = filter_frame, | ||
630 | .config_props = config_input, | ||
631 | }, | ||
632 | }; | ||
633 | |||
634 | const FFFilter ff_vf_removegrain = { | ||
635 | .p.name = "removegrain", | ||
636 | .p.description = NULL_IF_CONFIG_SMALL("Remove grain."), | ||
637 | .p.priv_class = &removegrain_class, | ||
638 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, | ||
639 | .priv_size = sizeof(RemoveGrainContext), | ||
640 | FILTER_INPUTS(removegrain_inputs), | ||
641 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
642 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
643 | }; | ||
644 |