FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_removegrain.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 351 358 98.0%
Branches: 236 239 98.7%

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 "formats.h"
30 #include "internal.h"
31 #include "removegrain.h"
32 #include "video.h"
33
34 #define OFFSET(x) offsetof(RemoveGrainContext, x)
35 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
36
37 static const AVOption removegrain_options[] = {
38 { "m0", "set mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
39 { "m1", "set mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
40 { "m2", "set mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
41 { "m3", "set mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
42 {NULL}
43 };
44
45 AVFILTER_DEFINE_CLASS(removegrain);
46
47 static const enum AVPixelFormat pix_fmts[] = {
48 AV_PIX_FMT_GRAY8,
49 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
50 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
51 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
52 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
53 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
54 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
55 AV_PIX_FMT_NONE
56 };
57
58 #define REMOVE_GRAIN_SORT_AXIS \
59 const int ma1 = FFMAX(a1, a8); \
60 const int mi1 = FFMIN(a1, a8); \
61 const int ma2 = FFMAX(a2, a7); \
62 const int mi2 = FFMIN(a2, a7); \
63 const int ma3 = FFMAX(a3, a6); \
64 const int mi3 = FFMIN(a3, a6); \
65 const int ma4 = FFMAX(a4, a5); \
66 const int mi4 = FFMIN(a4, a5);
67
68 149516 static int mode01(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
69 {
70 149516 const int mi = FFMIN(FFMIN(FFMIN(a1, a2), FFMIN(a3, a4)), FFMIN(FFMIN(a5, a6), FFMIN(a7, a8)));
71 149516 const int ma = FFMAX(FFMAX(FFMAX(a1, a2), FFMAX(a3, a4)), FFMAX(FFMAX(a5, a6), FFMAX(a7, a8)));
72
73 149516 return av_clip(c, mi, ma);
74 }
75
76 8093199 static int cmp_int(const void *p1, const void *p2)
77 {
78 8093199 int left = *(const int *)p1;
79 8093199 int right = *(const int *)p2;
80 8093199 return FFDIFFSIGN(left, right);
81 }
82
83 149516 static int mode02(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
84 {
85 149516 int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
86
87
44/44
✓ Branch 0 taken 430440 times.
✓ Branch 1 taken 156136 times.
✓ Branch 3 taken 187782 times.
✓ Branch 4 taken 242658 times.
✓ Branch 6 taken 34892 times.
✓ Branch 7 taken 152890 times.
✓ Branch 9 taken 98467 times.
✓ Branch 10 taken 144191 times.
✓ Branch 12 taken 177089 times.
✓ Branch 13 taken 253351 times.
✓ Branch 14 taken 126581 times.
✓ Branch 15 taken 303859 times.
✓ Branch 16 taken 651639 times.
✓ Branch 17 taken 122851 times.
✓ Branch 19 taken 406078 times.
✓ Branch 20 taken 245561 times.
✓ Branch 21 taken 535241 times.
✓ Branch 22 taken 267232 times.
✓ Branch 24 taken 434061 times.
✓ Branch 25 taken 101180 times.
✓ Branch 26 taken 101180 times.
✓ Branch 27 taken 267232 times.
✓ Branch 28 taken 368412 times.
✓ Branch 29 taken 303859 times.
✓ Branch 30 taken 95793 times.
✓ Branch 31 taken 208066 times.
✓ Branch 32 taken 87423 times.
✓ Branch 33 taken 8370 times.
✓ Branch 34 taken 14692 times.
✓ Branch 35 taken 72731 times.
✓ Branch 36 taken 63397 times.
✓ Branch 37 taken 10134 times.
✓ Branch 39 taken 50469 times.
✓ Branch 40 taken 12928 times.
✓ Branch 41 taken 10134 times.
✓ Branch 42 taken 12928 times.
✓ Branch 43 taken 142955 times.
✓ Branch 44 taken 150770 times.
✓ Branch 46 taken 49845 times.
✓ Branch 47 taken 106291 times.
✓ Branch 48 taken 586576 times.
✓ Branch 49 taken 150390 times.
✓ Branch 50 taken 443241 times.
✓ Branch 51 taken 149516 times.
2145502 AV_QSORT(a, 8, int, cmp_int);
88
89 149516 return av_clip(c, a[2 - 1 ], a[7 - 1]);
90 }
91
92 149516 static int mode03(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
93 {
94 149516 int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
95
96
44/44
✓ Branch 0 taken 430440 times.
✓ Branch 1 taken 156136 times.
✓ Branch 3 taken 187782 times.
✓ Branch 4 taken 242658 times.
✓ Branch 6 taken 34892 times.
✓ Branch 7 taken 152890 times.
✓ Branch 9 taken 98467 times.
✓ Branch 10 taken 144191 times.
✓ Branch 12 taken 177089 times.
✓ Branch 13 taken 253351 times.
✓ Branch 14 taken 126581 times.
✓ Branch 15 taken 303859 times.
✓ Branch 16 taken 651639 times.
✓ Branch 17 taken 122851 times.
✓ Branch 19 taken 406078 times.
✓ Branch 20 taken 245561 times.
✓ Branch 21 taken 535241 times.
✓ Branch 22 taken 267232 times.
✓ Branch 24 taken 434061 times.
✓ Branch 25 taken 101180 times.
✓ Branch 26 taken 101180 times.
✓ Branch 27 taken 267232 times.
✓ Branch 28 taken 368412 times.
✓ Branch 29 taken 303859 times.
✓ Branch 30 taken 95793 times.
✓ Branch 31 taken 208066 times.
✓ Branch 32 taken 87423 times.
✓ Branch 33 taken 8370 times.
✓ Branch 34 taken 14692 times.
✓ Branch 35 taken 72731 times.
✓ Branch 36 taken 63397 times.
✓ Branch 37 taken 10134 times.
✓ Branch 39 taken 50469 times.
✓ Branch 40 taken 12928 times.
✓ Branch 41 taken 10134 times.
✓ Branch 42 taken 12928 times.
✓ Branch 43 taken 142955 times.
✓ Branch 44 taken 150770 times.
✓ Branch 46 taken 49845 times.
✓ Branch 47 taken 106291 times.
✓ Branch 48 taken 586576 times.
✓ Branch 49 taken 150390 times.
✓ Branch 50 taken 443241 times.
✓ Branch 51 taken 149516 times.
2145502 AV_QSORT(a, 8, int, cmp_int);
97
98 149516 return av_clip(c, a[3 - 1 ], a[6 - 1]);
99 }
100
101 149516 static int mode04(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
102 {
103 149516 int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
104
105
44/44
✓ Branch 0 taken 430440 times.
✓ Branch 1 taken 156136 times.
✓ Branch 3 taken 187782 times.
✓ Branch 4 taken 242658 times.
✓ Branch 6 taken 34892 times.
✓ Branch 7 taken 152890 times.
✓ Branch 9 taken 98467 times.
✓ Branch 10 taken 144191 times.
✓ Branch 12 taken 177089 times.
✓ Branch 13 taken 253351 times.
✓ Branch 14 taken 126581 times.
✓ Branch 15 taken 303859 times.
✓ Branch 16 taken 651639 times.
✓ Branch 17 taken 122851 times.
✓ Branch 19 taken 406078 times.
✓ Branch 20 taken 245561 times.
✓ Branch 21 taken 535241 times.
✓ Branch 22 taken 267232 times.
✓ Branch 24 taken 434061 times.
✓ Branch 25 taken 101180 times.
✓ Branch 26 taken 101180 times.
✓ Branch 27 taken 267232 times.
✓ Branch 28 taken 368412 times.
✓ Branch 29 taken 303859 times.
✓ Branch 30 taken 95793 times.
✓ Branch 31 taken 208066 times.
✓ Branch 32 taken 87423 times.
✓ Branch 33 taken 8370 times.
✓ Branch 34 taken 14692 times.
✓ Branch 35 taken 72731 times.
✓ Branch 36 taken 63397 times.
✓ Branch 37 taken 10134 times.
✓ Branch 39 taken 50469 times.
✓ Branch 40 taken 12928 times.
✓ Branch 41 taken 10134 times.
✓ Branch 42 taken 12928 times.
✓ Branch 43 taken 142955 times.
✓ Branch 44 taken 150770 times.
✓ Branch 46 taken 49845 times.
✓ Branch 47 taken 106291 times.
✓ Branch 48 taken 586576 times.
✓ Branch 49 taken 150390 times.
✓ Branch 50 taken 443241 times.
✓ Branch 51 taken 149516 times.
2145502 AV_QSORT(a, 8, int, cmp_int);
106
107 149516 return av_clip(c, a[4 - 1 ], a[5 - 1]);
108 }
109
110 149516 static int mode05(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
111 {
112 149516 REMOVE_GRAIN_SORT_AXIS
113
114 149516 const int c1 = FFABS(c - av_clip(c, mi1, ma1));
115 149516 const int c2 = FFABS(c - av_clip(c, mi2, ma2));
116 149516 const int c3 = FFABS(c - av_clip(c, mi3, ma3));
117 149516 const int c4 = FFABS(c - av_clip(c, mi4, ma4));
118
119 149516 const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
120
121 /* When adding SIMD notice the return order here: 4, 2, 3, 1. */
122
2/2
✓ Branch 0 taken 116368 times.
✓ Branch 1 taken 33148 times.
149516 if (mindiff == c4) {
123 116368 return av_clip(c, mi4, ma4);
124
2/2
✓ Branch 0 taken 14995 times.
✓ Branch 1 taken 18153 times.
33148 } else if (mindiff == c2) {
125 14995 return av_clip(c, mi2, ma2);
126
2/2
✓ Branch 0 taken 14086 times.
✓ Branch 1 taken 4067 times.
18153 } else if (mindiff == c3) {
127 14086 return av_clip(c, mi3, ma3);
128 }
129
130 4067 return av_clip(c, mi1, ma1);
131 }
132
133 149516 static int mode06(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
134 {
135 149516 REMOVE_GRAIN_SORT_AXIS
136
137 149516 const int d1 = ma1 - mi1;
138 149516 const int d2 = ma2 - mi2;
139 149516 const int d3 = ma3 - mi3;
140 149516 const int d4 = ma4 - mi4;
141
142 149516 const int cli1 = av_clip(c, mi1, ma1);
143 149516 const int cli2 = av_clip(c, mi2, ma2);
144 149516 const int cli3 = av_clip(c, mi3, ma3);
145 149516 const int cli4 = av_clip(c, mi4, ma4);
146
147 149516 const int c1 = av_clip_uint16((FFABS(c - cli1) << 1) + d1);
148 149516 const int c2 = av_clip_uint16((FFABS(c - cli2) << 1) + d2);
149 149516 const int c3 = av_clip_uint16((FFABS(c - cli3) << 1) + d3);
150 149516 const int c4 = av_clip_uint16((FFABS(c - cli4) << 1) + d4);
151
152 149516 const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
153
154
2/2
✓ Branch 0 taken 30362 times.
✓ Branch 1 taken 119154 times.
149516 if (mindiff == c4) {
155 30362 return cli4;
156
2/2
✓ Branch 0 taken 26836 times.
✓ Branch 1 taken 92318 times.
119154 } else if (mindiff == c2) {
157 26836 return cli2;
158
2/2
✓ Branch 0 taken 85122 times.
✓ Branch 1 taken 7196 times.
92318 } else if (mindiff == c3) {
159 85122 return cli3;
160 }
161
162 7196 return cli1;
163 }
164
165 149516 static int mode07(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
166 {
167 149516 REMOVE_GRAIN_SORT_AXIS
168
169 149516 const int d1 = ma1 - mi1;
170 149516 const int d2 = ma2 - mi2;
171 149516 const int d3 = ma3 - mi3;
172 149516 const int d4 = ma4 - mi4;
173
174 149516 const int cli1 = av_clip(c, mi1, ma1);
175 149516 const int cli2 = av_clip(c, mi2, ma2);
176 149516 const int cli3 = av_clip(c, mi3, ma3);
177 149516 const int cli4 = av_clip(c, mi4, ma4);
178
179 149516 const int c1 = FFABS(c - cli1) + d1;
180 149516 const int c2 = FFABS(c - cli2) + d2;
181 149516 const int c3 = FFABS(c - cli3) + d3;
182 149516 const int c4 = FFABS(c - cli4) + d4;
183
184 149516 const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
185
186
2/2
✓ Branch 0 taken 30479 times.
✓ Branch 1 taken 119037 times.
149516 if (mindiff == c4) {
187 30479 return cli4;
188
2/2
✓ Branch 0 taken 27791 times.
✓ Branch 1 taken 91246 times.
119037 } else if (mindiff == c2) {
189 27791 return cli2;
190
2/2
✓ Branch 0 taken 84095 times.
✓ Branch 1 taken 7151 times.
91246 } else if (mindiff == c3) {
191 84095 return cli3;
192 }
193
194 7151 return cli1;
195 }
196
197 149516 static int mode08(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
198 {
199 149516 REMOVE_GRAIN_SORT_AXIS
200
201 149516 const int d1 = ma1 - mi1;
202 149516 const int d2 = ma2 - mi2;
203 149516 const int d3 = ma3 - mi3;
204 149516 const int d4 = ma4 - mi4;
205
206 149516 const int cli1 = av_clip(c, mi1, ma1);
207 149516 const int cli2 = av_clip(c, mi2, ma2);
208 149516 const int cli3 = av_clip(c, mi3, ma3);
209 149516 const int cli4 = av_clip(c, mi4, ma4);
210
211 149516 const int c1 = av_clip_uint16(FFABS(c - cli1) + (d1 << 1));
212 149516 const int c2 = av_clip_uint16(FFABS(c - cli2) + (d2 << 1));
213 149516 const int c3 = av_clip_uint16(FFABS(c - cli3) + (d3 << 1));
214 149516 const int c4 = av_clip_uint16(FFABS(c - cli4) + (d4 << 1));
215
216 149516 const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
217
218
2/2
✓ Branch 0 taken 31274 times.
✓ Branch 1 taken 118242 times.
149516 if (mindiff == c4) {
219 31274 return cli4;
220
2/2
✓ Branch 0 taken 25063 times.
✓ Branch 1 taken 93179 times.
118242 } else if (mindiff == c2) {
221 25063 return cli2;
222
2/2
✓ Branch 0 taken 83083 times.
✓ Branch 1 taken 10096 times.
93179 } else if (mindiff == c3) {
223 83083 return cli3;
224 }
225
226 10096 return cli1;
227 }
228
229 149516 static int mode09(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
230 {
231 149516 REMOVE_GRAIN_SORT_AXIS
232
233 149516 const int d1 = ma1 - mi1;
234 149516 const int d2 = ma2 - mi2;
235 149516 const int d3 = ma3 - mi3;
236 149516 const int d4 = ma4 - mi4;
237
238 149516 const int mindiff = FFMIN(FFMIN(d1, d2), FFMIN(d3, d4));
239
240
2/2
✓ Branch 0 taken 31449 times.
✓ Branch 1 taken 118067 times.
149516 if (mindiff == d4) {
241 31449 return av_clip(c, mi4, ma4);
242
2/2
✓ Branch 0 taken 25061 times.
✓ Branch 1 taken 93006 times.
118067 } else if (mindiff == d2) {
243 25061 return av_clip(c, mi2, ma2);
244
2/2
✓ Branch 0 taken 82804 times.
✓ Branch 1 taken 10202 times.
93006 } else if (mindiff == d3) {
245 82804 return av_clip(c, mi3, ma3);
246 }
247
248 10202 return av_clip(c, mi1, ma1);
249 }
250
251 149516 static int mode10(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
252 {
253 149516 const int d1 = FFABS(c - a1);
254 149516 const int d2 = FFABS(c - a2);
255 149516 const int d3 = FFABS(c - a3);
256 149516 const int d4 = FFABS(c - a4);
257 149516 const int d5 = FFABS(c - a5);
258 149516 const int d6 = FFABS(c - a6);
259 149516 const int d7 = FFABS(c - a7);
260 149516 const int d8 = FFABS(c - a8);
261
262 149516 const int mindiff = FFMIN(FFMIN(FFMIN(d1, d2), FFMIN(d3, d4)),
263 FFMIN(FFMIN(d5, d6), FFMIN(d7, d8)));
264
265
2/2
✓ Branch 0 taken 14257 times.
✓ Branch 1 taken 135259 times.
149516 if (mindiff == d7) return a7;
266
2/2
✓ Branch 0 taken 4628 times.
✓ Branch 1 taken 130631 times.
135259 if (mindiff == d8) return a8;
267
2/2
✓ Branch 0 taken 48272 times.
✓ Branch 1 taken 82359 times.
130631 if (mindiff == d6) return a6;
268
2/2
✓ Branch 0 taken 12713 times.
✓ Branch 1 taken 69646 times.
82359 if (mindiff == d2) return a2;
269
2/2
✓ Branch 0 taken 41309 times.
✓ Branch 1 taken 28337 times.
69646 if (mindiff == d3) return a3;
270
2/2
✓ Branch 0 taken 3551 times.
✓ Branch 1 taken 24786 times.
28337 if (mindiff == d1) return a1;
271
2/2
✓ Branch 0 taken 18916 times.
✓ Branch 1 taken 5870 times.
24786 if (mindiff == d5) return a5;
272
273 5870 return a4;
274 }
275
276 299032 static int mode1112(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
277 {
278 299032 const int sum = 4 * c + 2 * (a2 + a4 + a5 + a7) + a1 + a3 + a6 + a8;
279 299032 const int val = (sum + 8) >> 4;
280
281 299032 return val;
282 }
283
284 149516 static int mode1314(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
285 {
286 149516 const int d1 = FFABS(a1 - a8);
287 149516 const int d2 = FFABS(a2 - a7);
288 149516 const int d3 = FFABS(a3 - a6);
289
290 149516 const int mindiff = FFMIN(FFMIN(d1, d2), d3);
291
292
2/2
✓ Branch 0 taken 29535 times.
✓ Branch 1 taken 119981 times.
149516 if (mindiff == d2) {
293 29535 return (a2 + a7 + 1) >> 1;
294 }
295
2/2
✓ Branch 0 taken 99814 times.
✓ Branch 1 taken 20167 times.
119981 if (mindiff == d3) {
296 99814 return (a3 + a6 + 1) >> 1;
297 }
298
299 20167 return (a1 + a8 + 1) >> 1;
300 }
301
302 149516 static int mode1516(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
303 {
304 149516 const int d1 = FFABS(a1 - a8);
305 149516 const int d2 = FFABS(a2 - a7);
306 149516 const int d3 = FFABS(a3 - a6);
307
308 149516 const int mindiff = FFMIN(FFMIN(d1, d2), d3);
309 149516 const int average = (2 * (a2 + a7) + a1 + a3 + a6 + a8 + 4) >> 3;
310
311
2/2
✓ Branch 0 taken 29535 times.
✓ Branch 1 taken 119981 times.
149516 if (mindiff == d2) {
312 29535 return av_clip(average, FFMIN(a2, a7), FFMAX(a2, a7));
313 }
314
2/2
✓ Branch 0 taken 99814 times.
✓ Branch 1 taken 20167 times.
119981 if (mindiff == d3) {
315 99814 return av_clip(average, FFMIN(a3, a6), FFMAX(a3, a6));
316 }
317
318 20167 return av_clip(average, FFMIN(a1, a8), FFMAX(a1, a8));
319 }
320
321 149516 static int mode17(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
322 {
323 149516 REMOVE_GRAIN_SORT_AXIS
324
325 149516 const int l = FFMAX(FFMAX(mi1, mi2), FFMAX(mi3, mi4));
326 149516 const int u = FFMIN(FFMIN(ma1, ma2), FFMIN(ma3, ma4));
327
328 149516 return av_clip(c, FFMIN(l, u), FFMAX(l, u));
329 }
330
331 149516 static int mode18(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
332 {
333 149516 const int d1 = FFMAX(FFABS(c - a1), FFABS(c - a8));
334 149516 const int d2 = FFMAX(FFABS(c - a2), FFABS(c - a7));
335 149516 const int d3 = FFMAX(FFABS(c - a3), FFABS(c - a6));
336 149516 const int d4 = FFMAX(FFABS(c - a4), FFABS(c - a5));
337
338 149516 const int mindiff = FFMIN(FFMIN(d1, d2), FFMIN(d3, d4));
339
340
2/2
✓ Branch 0 taken 30555 times.
✓ Branch 1 taken 118961 times.
149516 if (mindiff == d4) {
341 30555 return av_clip(c, FFMIN(a4, a5), FFMAX(a4, a5));
342 }
343
2/2
✓ Branch 0 taken 26866 times.
✓ Branch 1 taken 92095 times.
118961 if (mindiff == d2) {
344 26866 return av_clip(c, FFMIN(a2, a7), FFMAX(a2, a7));
345 }
346
2/2
✓ Branch 0 taken 85057 times.
✓ Branch 1 taken 7038 times.
92095 if (mindiff == d3) {
347 85057 return av_clip(c, FFMIN(a3, a6), FFMAX(a3, a6));
348 }
349
350 7038 return av_clip(c, FFMIN(a1, a8), FFMAX(a1, a8));
351 }
352
353 149516 static int mode19(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
354 {
355 149516 const int sum = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;
356 149516 const int val = (sum + 4) >> 3;
357
358 149516 return val;
359 }
360
361 149516 static int mode20(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
362 {
363 149516 const int sum = a1 + a2 + a3 + a4 + c + a5 + a6 + a7 + a8;
364 149516 const int val = (sum + 4) / 9;
365
366 149516 return val;
367 }
368
369 149516 static int mode21(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
370 {
371 149516 const int l1l = (a1 + a8) >> 1;
372 149516 const int l2l = (a2 + a7) >> 1;
373 149516 const int l3l = (a3 + a6) >> 1;
374 149516 const int l4l = (a4 + a5) >> 1;
375
376 149516 const int l1h = (a1 + a8 + 1) >> 1;
377 149516 const int l2h = (a2 + a7 + 1) >> 1;
378 149516 const int l3h = (a3 + a6 + 1) >> 1;
379 149516 const int l4h = (a4 + a5 + 1) >> 1;
380
381 149516 const int mi = FFMIN(FFMIN(l1l, l2l), FFMIN(l3l, l4l));
382 149516 const int ma = FFMAX(FFMAX(l1h, l2h), FFMAX(l3h, l4h));
383
384 149516 return av_clip(c, mi, ma);
385 }
386
387 149516 static int mode22(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
388 {
389 149516 const int l1 = (a1 + a8 + 1) >> 1;
390 149516 const int l2 = (a2 + a7 + 1) >> 1;
391 149516 const int l3 = (a3 + a6 + 1) >> 1;
392 149516 const int l4 = (a4 + a5 + 1) >> 1;
393
394 149516 const int mi = FFMIN(FFMIN(l1, l2), FFMIN(l3, l4));
395 149516 const int ma = FFMAX(FFMAX(l1, l2), FFMAX(l3, l4));
396
397 149516 return av_clip(c, mi, ma);
398 }
399
400 149516 static int mode23(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
401 {
402 149516 REMOVE_GRAIN_SORT_AXIS
403
404 149516 const int linediff1 = ma1 - mi1;
405 149516 const int linediff2 = ma2 - mi2;
406 149516 const int linediff3 = ma3 - mi3;
407 149516 const int linediff4 = ma4 - mi4;
408
409 149516 const int u1 = FFMIN(c - ma1, linediff1);
410 149516 const int u2 = FFMIN(c - ma2, linediff2);
411 149516 const int u3 = FFMIN(c - ma3, linediff3);
412 149516 const int u4 = FFMIN(c - ma4, linediff4);
413 149516 const int u = FFMAX(FFMAX(FFMAX(u1, u2), FFMAX(u3, u4)), 0);
414
415 149516 const int d1 = FFMIN(mi1 - c, linediff1);
416 149516 const int d2 = FFMIN(mi2 - c, linediff2);
417 149516 const int d3 = FFMIN(mi3 - c, linediff3);
418 149516 const int d4 = FFMIN(mi4 - c, linediff4);
419 149516 const int d = FFMAX(FFMAX(FFMAX(d1, d2), FFMAX(d3, d4)), 0);
420
421 149516 return c - u + d; // This probably will never overflow.
422 }
423
424 149516 static int mode24(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
425 {
426 149516 REMOVE_GRAIN_SORT_AXIS
427
428 149516 const int linediff1 = ma1 - mi1;
429 149516 const int linediff2 = ma2 - mi2;
430 149516 const int linediff3 = ma3 - mi3;
431 149516 const int linediff4 = ma4 - mi4;
432
433 149516 const int tu1 = c - ma1;
434 149516 const int tu2 = c - ma2;
435 149516 const int tu3 = c - ma3;
436 149516 const int tu4 = c - ma4;
437
438 149516 const int u1 = FFMIN(tu1, linediff1 - tu1);
439 149516 const int u2 = FFMIN(tu2, linediff2 - tu2);
440 149516 const int u3 = FFMIN(tu3, linediff3 - tu3);
441 149516 const int u4 = FFMIN(tu4, linediff4 - tu4);
442 149516 const int u = FFMAX(FFMAX(FFMAX(u1, u2), FFMAX(u3, u4)), 0);
443
444 149516 const int td1 = mi1 - c;
445 149516 const int td2 = mi2 - c;
446 149516 const int td3 = mi3 - c;
447 149516 const int td4 = mi4 - c;
448
449 149516 const int d1 = FFMIN(td1, linediff1 - td1);
450 149516 const int d2 = FFMIN(td2, linediff2 - td2);
451 149516 const int d3 = FFMIN(td3, linediff3 - td3);
452 149516 const int d4 = FFMIN(td4, linediff4 - td4);
453 149516 const int d = FFMAX(FFMAX(FFMAX(d1, d2), FFMAX(d3, d4)), 0);
454
455 149516 return c - u + d; // This probably will never overflow.
456 }
457
458 25 static int config_input(AVFilterLink *inlink)
459 {
460 25 RemoveGrainContext *s = inlink->dst->priv;
461 25 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
462 int i;
463
464 25 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
465
466 25 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
467 25 s->planeheight[0] = s->planeheight[3] = inlink->h;
468 25 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
469 25 s->planewidth[0] = s->planewidth[3] = inlink->w;
470
471
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 25 times.
100 for (i = 0; i < s->nb_planes; i++) {
472
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]) {
473 3 case 1: s->rg[i] = mode01; break;
474 3 case 2: s->rg[i] = mode02; break;
475 3 case 3: s->rg[i] = mode03; break;
476 3 case 4: s->rg[i] = mode04; break;
477 3 case 5: s->rg[i] = mode05; break;
478 3 case 6: s->rg[i] = mode06; break;
479 3 case 7: s->rg[i] = mode07; break;
480 3 case 8: s->rg[i] = mode08; break;
481 3 case 9: s->rg[i] = mode09; break;
482 3 case 10: s->rg[i] = mode10; break;
483 3 case 11: s->rg[i] = mode1112; break;
484 3 case 12: s->rg[i] = mode1112; break;
485 3 case 13: s->skip_odd = 1;
486 3 s->rg[i] = mode1314; break;
487 3 case 14: s->skip_even = 1;
488 3 s->rg[i] = mode1314; break;
489 3 case 15: s->skip_odd = 1;
490 3 s->rg[i] = mode1516; break;
491 3 case 16: s->skip_even = 1;
492 3 s->rg[i] = mode1516; break;
493 3 case 17: s->rg[i] = mode17; break;
494 3 case 18: s->rg[i] = mode18; break;
495 3 case 19: s->rg[i] = mode19; break;
496 3 case 20: s->rg[i] = mode20; break;
497 3 case 21: s->rg[i] = mode21; break;
498 3 case 22: s->rg[i] = mode22; break;
499 3 case 23: s->rg[i] = mode23; break;
500 3 case 24: s->rg[i] = mode24; break;
501 }
502 }
503
504 #if ARCH_X86
505 25 ff_removegrain_init_x86(s);
506 #endif
507
508 25 return 0;
509 }
510
511 typedef struct ThreadData {
512 AVFrame *in, *out;
513 int plane;
514 } ThreadData;
515
516 648 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
517 {
518 648 RemoveGrainContext *s = ctx->priv;
519 648 ThreadData *td = arg;
520 648 AVFrame *in = td->in;
521 648 AVFrame *out = td->out;
522 648 const int i = td->plane;
523 648 const int height = s->planeheight[i];
524 648 const int om = in->linesize[i] - 1;
525 648 const int o0 = in->linesize[i] ;
526 648 const int op = in->linesize[i] + 1;
527 648 int start = (height * jobnr ) / nb_jobs;
528 648 int end = (height * (jobnr+1)) / nb_jobs;
529 int x, y;
530
531 648 start = FFMAX(1, start);
532 648 end = FFMIN(height-1, end);
533
2/2
✓ Branch 0 taken 13680 times.
✓ Branch 1 taken 648 times.
14328 for (y = start; y < end; y++) {
534 13680 uint8_t *dst = out->data[i];
535 13680 uint8_t *src = in->data[i];
536
537 13680 src = in->data[i] + y * in->linesize[i];
538 13680 dst = out->data[i] + y * out->linesize[i];
539
540
4/4
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 12540 times.
✓ Branch 2 taken 570 times.
✓ Branch 3 taken 570 times.
13680 if (s->skip_even && !(y & 1)) {
541 570 memcpy(dst, src, s->planewidth[i]);
542 570 continue;
543 }
544
4/4
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 11970 times.
✓ Branch 2 taken 570 times.
✓ Branch 3 taken 570 times.
13110 if (s->skip_odd && y & 1) {
545 570 memcpy(dst, src, s->planewidth[i]);
546 570 continue;
547 }
548
549 12540 *dst++ = *src++;
550
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12540 times.
12540 if (s->fl[i]) {
552 int w_asm = (s->planewidth[i] - 2) & ~15;
553
554 s->fl[i](dst, src, in->linesize[i], w_asm);
555
556 x = 1 + w_asm;
557 dst += w_asm;
558 src += w_asm;
559 } else
560 12540 x = 1;
561
562
2/2
✓ Branch 0 taken 3289352 times.
✓ Branch 1 taken 12540 times.
3301892 for (; x < s->planewidth[i] - 1; x++) {
563 3289352 const int a1 = src[-op];
564 3289352 const int a2 = src[-o0];
565 3289352 const int a3 = src[-om];
566 3289352 const int a4 = src[-1 ];
567 3289352 const int c = src[ 0 ];
568 3289352 const int a5 = src[ 1 ];
569 3289352 const int a6 = src[ om];
570 3289352 const int a7 = src[ o0];
571 3289352 const int a8 = src[ op];
572
573 3289352 const int res = s->rg[i](c, a1, a2, a3, a4, a5, a6, a7, a8);
574
575 3289352 *dst = res;
576 3289352 dst++, src++;
577 }
578 12540 dst[0] = src[0];
579 }
580
581 648 return 0;
582 }
583
584 25 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
585 {
586 25 AVFilterContext *ctx = inlink->dst;
587 25 AVFilterLink *outlink = ctx->outputs[0];
588 25 RemoveGrainContext *s = ctx->priv;
589 ThreadData td;
590 AVFrame *out;
591 int i;
592
593 25 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!out) {
595 av_frame_free(&in);
596 return AVERROR(ENOMEM);
597 }
598 25 av_frame_copy_props(out, in);
599
600
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 25 times.
100 for (i = 0; i < s->nb_planes; i++) {
601 75 uint8_t *dst = out->data[i];
602 75 uint8_t *src = in->data[i];
603
604
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 72 times.
75 if (s->mode[i] == 0) {
605 3 av_image_copy_plane(dst, out->linesize[i],
606 3 src, in->linesize[i],
607 s->planewidth[i], s->planeheight[i]);
608 3 continue;
609 }
610
611 72 memcpy(dst, src, s->planewidth[i]);
612
613 72 td.in = in; td.out = out; td.plane = i;
614 72 ff_filter_execute(ctx, filter_slice, &td, NULL,
615
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 FFMIN(s->planeheight[i], ff_filter_get_nb_threads(ctx)));
616
617 72 src = in->data[i] + (s->planeheight[i] - 1) * in->linesize[i];
618 72 dst = out->data[i] + (s->planeheight[i] - 1) * out->linesize[i];
619 72 memcpy(dst, src, s->planewidth[i]);
620 }
621
622 25 av_frame_free(&in);
623 25 return ff_filter_frame(outlink, out);
624 }
625
626 static const AVFilterPad removegrain_inputs[] = {
627 {
628 .name = "default",
629 .type = AVMEDIA_TYPE_VIDEO,
630 .filter_frame = filter_frame,
631 .config_props = config_input,
632 },
633 };
634
635 static const AVFilterPad removegrain_outputs[] = {
636 {
637 .name = "default",
638 .type = AVMEDIA_TYPE_VIDEO,
639 },
640 };
641
642 const AVFilter ff_vf_removegrain = {
643 .name = "removegrain",
644 .description = NULL_IF_CONFIG_SMALL("Remove grain."),
645 .priv_size = sizeof(RemoveGrainContext),
646 FILTER_INPUTS(removegrain_inputs),
647 FILTER_OUTPUTS(removegrain_outputs),
648 FILTER_PIXFMTS_ARRAY(pix_fmts),
649 .priv_class = &removegrain_class,
650 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
651 };
652