FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_epx.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 136 139 97.8%
Branches: 95 101 94.1%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/opt.h"
20 #include "libavutil/pixdesc.h"
21 #include "internal.h"
22
23 typedef struct EPXContext {
24 const AVClass *class;
25
26 int n;
27
28 int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
29 } EPXContext;
30
31 typedef struct ThreadData {
32 AVFrame *in, *out;
33 } ThreadData;
34
35 #define OFFSET(x) offsetof(EPXContext, x)
36 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
37 static const AVOption epx_options[] = {
38 { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS },
39 { NULL }
40 };
41
42 AVFILTER_DEFINE_CLASS(epx);
43
44 18 static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
45 {
46 18 ThreadData *td = arg;
47 18 const AVFrame *in = td->in;
48 18 AVFrame *out = td->out;
49 18 const int slice_start = (in->height * jobnr ) / nb_jobs;
50 18 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
51
52
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (int p = 0; p < 1; p++) {
53 18 const int width = in->width;
54 18 const int height = in->height;
55 18 const int src_linesize = in->linesize[p] / 4;
56 18 const int dst_linesize = out->linesize[p] / 4;
57 18 const uint32_t *src = (const uint32_t *)in->data[p];
58 18 uint32_t *dst = (uint32_t *)out->data[p];
59 const uint32_t *src_line[3];
60
61 18 src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
62 18 src_line[1] = src + src_linesize * slice_start;
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
64
65
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 18 times.
536 for (int y = slice_start; y < slice_end; y++) {
66 uint32_t *dst_line[2];
67
68 518 dst_line[0] = dst + dst_linesize*2*y;
69 518 dst_line[1] = dst + dst_linesize*(2*y+1);
70
71
2/2
✓ Branch 0 taken 98938 times.
✓ Branch 1 taken 518 times.
99456 for (int x = 0; x < width; x++) {
72 uint32_t E0, E1, E2, E3;
73 uint32_t B, D, E, F, H;
74
75 98938 B = src_line[0][x];
76
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 D = src_line[1][FFMAX(x-1, 0)];
77 98938 E = src_line[1][x];
78
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 F = src_line[1][FFMIN(x+1, width - 1)];
79 98938 H = src_line[2][x];
80
81
4/4
✓ Branch 0 taken 38981 times.
✓ Branch 1 taken 59957 times.
✓ Branch 2 taken 23048 times.
✓ Branch 3 taken 15933 times.
98938 if (B != H && D != F) {
82
2/2
✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
23048 E0 = D == B ? D : E;
83
2/2
✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
23048 E1 = B == F ? F : E;
84
2/2
✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
23048 E2 = D == H ? D : E;
85
2/2
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 15399 times.
23048 E3 = H == F ? F : E;
86 } else {
87 75890 E0 = E;
88 75890 E1 = E;
89 75890 E2 = E;
90 75890 E3 = E;
91 }
92
93 98938 dst_line[0][x*2] = E0;
94 98938 dst_line[0][x*2+1] = E1;
95 98938 dst_line[1][x*2] = E2;
96 98938 dst_line[1][x*2+1] = E3;
97 }
98
99 518 src_line[0] = src_line[1];
100 518 src_line[1] = src_line[2];
101 518 src_line[2] = src_line[1];
102
103
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4 times.
518 if (y < height - 2)
104 514 src_line[2] += src_linesize;
105 }
106 }
107
108 18 return 0;
109 }
110
111 18 static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
112 {
113 18 ThreadData *td = arg;
114 18 const AVFrame *in = td->in;
115 18 AVFrame *out = td->out;
116 18 const int slice_start = (in->height * jobnr ) / nb_jobs;
117 18 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
118
119
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (int p = 0; p < 1; p++) {
120 18 const int width = in->width;
121 18 const int height = in->height;
122 18 const int src_linesize = in->linesize[p] / 4;
123 18 const int dst_linesize = out->linesize[p] / 4;
124 18 const uint32_t *src = (const uint32_t *)in->data[p];
125 18 uint32_t *dst = (uint32_t *)out->data[p];
126 const uint32_t *src_line[3];
127
128 18 src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
129 18 src_line[1] = src + src_linesize * slice_start;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
131
132
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 18 times.
536 for (int y = slice_start; y < slice_end; y++) {
133 uint32_t *dst_line[3];
134
135 518 dst_line[0] = dst + dst_linesize*3*y;
136 518 dst_line[1] = dst + dst_linesize*(3*y+1);
137 518 dst_line[2] = dst + dst_linesize*(3*y+2);
138
139
2/2
✓ Branch 0 taken 98938 times.
✓ Branch 1 taken 518 times.
99456 for (int x = 0; x < width; x++) {
140 uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8;
141 uint32_t A, B, C, D, E, F, G, H, I;
142
143
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 A = src_line[0][FFMAX(x-1, 0)];
144 98938 B = src_line[0][x];
145
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 C = src_line[0][FFMIN(x+1, width - 1)];
146
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 D = src_line[1][FFMAX(x-1, 0)];
147 98938 E = src_line[1][x];
148
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 F = src_line[1][FFMIN(x+1, width - 1)];
149
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 G = src_line[2][FFMAX(x-1, 0)];
150 98938 H = src_line[2][x];
151
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 I = src_line[2][FFMIN(x+1, width - 1)];
152
153
4/4
✓ Branch 0 taken 38981 times.
✓ Branch 1 taken 59957 times.
✓ Branch 2 taken 23048 times.
✓ Branch 3 taken 15933 times.
98938 if (B != H && D != F) {
154
2/2
✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
23048 E0 = D == B ? D : E;
155
8/8
✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
✓ Branch 2 taken 4088 times.
✓ Branch 3 taken 3555 times.
✓ Branch 4 taken 6676 times.
✓ Branch 5 taken 12817 times.
✓ Branch 6 taken 3280 times.
✓ Branch 7 taken 3396 times.
23048 E1 = (D == B && E != C) || (B == F && E != A) ? B : E;
156
2/2
✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
23048 E2 = B == F ? F : E;
157
8/8
✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
✓ Branch 2 taken 4026 times.
✓ Branch 3 taken 3617 times.
✓ Branch 4 taken 6524 times.
✓ Branch 5 taken 12907 times.
✓ Branch 6 taken 3140 times.
✓ Branch 7 taken 3384 times.
23048 E3 = (D == B && E != G) || (D == H && E != A) ? D : E;
158 23048 E4 = E;
159
8/8
✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
✓ Branch 2 taken 3412 times.
✓ Branch 3 taken 3264 times.
✓ Branch 4 taken 7649 times.
✓ Branch 5 taken 12135 times.
✓ Branch 6 taken 3610 times.
✓ Branch 7 taken 4039 times.
23048 E5 = (B == F && E != I) || (H == F && E != C) ? F : E;
160
2/2
✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
23048 E6 = D == H ? D : E;
161
8/8
✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
✓ Branch 2 taken 3296 times.
✓ Branch 3 taken 3228 times.
✓ Branch 4 taken 7649 times.
✓ Branch 5 taken 12171 times.
✓ Branch 6 taken 3594 times.
✓ Branch 7 taken 4055 times.
23048 E7 = (D == H && E != I) || (H == F && E != G) ? H : E;
162
2/2
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 15399 times.
23048 E8 = H == F ? F : E;
163 } else {
164 75890 E0 = E;
165 75890 E1 = E;
166 75890 E2 = E;
167 75890 E3 = E;
168 75890 E4 = E;
169 75890 E5 = E;
170 75890 E6 = E;
171 75890 E7 = E;
172 75890 E8 = E;
173 }
174
175 98938 dst_line[0][x*3] = E0;
176 98938 dst_line[0][x*3+1] = E1;
177 98938 dst_line[0][x*3+2] = E2;
178 98938 dst_line[1][x*3] = E3;
179 98938 dst_line[1][x*3+1] = E4;
180 98938 dst_line[1][x*3+2] = E5;
181 98938 dst_line[2][x*3] = E6;
182 98938 dst_line[2][x*3+1] = E7;
183 98938 dst_line[2][x*3+2] = E8;
184 }
185
186 518 src_line[0] = src_line[1];
187 518 src_line[1] = src_line[2];
188 518 src_line[2] = src_line[1];
189
190
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4 times.
518 if (y < height - 2)
191 514 src_line[2] += src_linesize;
192 }
193 }
194
195 18 return 0;
196 }
197
198 4 static int config_output(AVFilterLink *outlink)
199 {
200 4 AVFilterContext *ctx = outlink->src;
201 4 EPXContext *s = ctx->priv;
202 4 AVFilterLink *inlink = ctx->inputs[0];
203 const AVPixFmtDescriptor *desc;
204
205 4 desc = av_pix_fmt_desc_get(outlink->format);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!desc)
207 return AVERROR_BUG;
208
209 4 outlink->w = inlink->w * s->n;
210 4 outlink->h = inlink->h * s->n;
211
212
2/3
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 switch (s->n) {
213 2 case 2:
214 2 s->epx_slice = epx2_slice;
215 2 break;
216 2 case 3:
217 2 s->epx_slice = epx3_slice;
218 2 break;
219 }
220
221 4 return 0;
222 }
223
224 static const enum AVPixelFormat pix_fmts[] = {
225 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
226 AV_PIX_FMT_NONE,
227 };
228
229 4 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
230 {
231 4 AVFilterContext *ctx = inlink->dst;
232 4 AVFilterLink *outlink = ctx->outputs[0];
233 4 EPXContext *s = ctx->priv;
234 ThreadData td;
235
236 4 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!out) {
238 av_frame_free(&in);
239 return AVERROR(ENOMEM);
240 }
241
242 4 av_frame_copy_props(out, in);
243
244 4 td.in = in, td.out = out;
245 4 ff_filter_execute(ctx, s->epx_slice, &td, NULL,
246
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
247
248 4 av_frame_free(&in);
249 4 return ff_filter_frame(outlink, out);
250 }
251
252 static const AVFilterPad inputs[] = {
253 {
254 .name = "default",
255 .type = AVMEDIA_TYPE_VIDEO,
256 .filter_frame = filter_frame,
257 },
258 };
259
260 static const AVFilterPad outputs[] = {
261 {
262 .name = "default",
263 .type = AVMEDIA_TYPE_VIDEO,
264 .config_props = config_output,
265 },
266 };
267
268 const AVFilter ff_vf_epx = {
269 .name = "epx",
270 .description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."),
271 FILTER_INPUTS(inputs),
272 FILTER_OUTPUTS(outputs),
273 FILTER_PIXFMTS_ARRAY(pix_fmts),
274 .priv_size = sizeof(EPXContext),
275 .priv_class = &epx_class,
276 .flags = AVFILTER_FLAG_SLICE_THREADS,
277 };
278