FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_epx.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 136 139 97.8%
Functions: 4 4 100.0%
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 #include "video.h"
23
24 typedef struct EPXContext {
25 const AVClass *class;
26
27 int n;
28
29 int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
30 } EPXContext;
31
32 typedef struct ThreadData {
33 AVFrame *in, *out;
34 } ThreadData;
35
36 #define OFFSET(x) offsetof(EPXContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
38 static const AVOption epx_options[] = {
39 { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS },
40 { NULL }
41 };
42
43 AVFILTER_DEFINE_CLASS(epx);
44
45 18 static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
46 {
47 18 ThreadData *td = arg;
48 18 const AVFrame *in = td->in;
49 18 AVFrame *out = td->out;
50 18 const int slice_start = (in->height * jobnr ) / nb_jobs;
51 18 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
52
53
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (int p = 0; p < 1; p++) {
54 18 const int width = in->width;
55 18 const int height = in->height;
56 18 const int src_linesize = in->linesize[p] / 4;
57 18 const int dst_linesize = out->linesize[p] / 4;
58 18 const uint32_t *src = (const uint32_t *)in->data[p];
59 18 uint32_t *dst = (uint32_t *)out->data[p];
60 const uint32_t *src_line[3];
61
62 18 src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
63 18 src_line[1] = src + src_linesize * slice_start;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
65
66
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 18 times.
536 for (int y = slice_start; y < slice_end; y++) {
67 uint32_t *dst_line[2];
68
69 518 dst_line[0] = dst + dst_linesize*2*y;
70 518 dst_line[1] = dst + dst_linesize*(2*y+1);
71
72
2/2
✓ Branch 0 taken 98938 times.
✓ Branch 1 taken 518 times.
99456 for (int x = 0; x < width; x++) {
73 uint32_t E0, E1, E2, E3;
74 uint32_t B, D, E, F, H;
75
76 98938 B = src_line[0][x];
77
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 D = src_line[1][FFMAX(x-1, 0)];
78 98938 E = src_line[1][x];
79
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 F = src_line[1][FFMIN(x+1, width - 1)];
80 98938 H = src_line[2][x];
81
82
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) {
83
2/2
✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
23048 E0 = D == B ? D : E;
84
2/2
✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
23048 E1 = B == F ? F : E;
85
2/2
✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
23048 E2 = D == H ? D : E;
86
2/2
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 15399 times.
23048 E3 = H == F ? F : E;
87 } else {
88 75890 E0 = E;
89 75890 E1 = E;
90 75890 E2 = E;
91 75890 E3 = E;
92 }
93
94 98938 dst_line[0][x*2] = E0;
95 98938 dst_line[0][x*2+1] = E1;
96 98938 dst_line[1][x*2] = E2;
97 98938 dst_line[1][x*2+1] = E3;
98 }
99
100 518 src_line[0] = src_line[1];
101 518 src_line[1] = src_line[2];
102 518 src_line[2] = src_line[1];
103
104
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4 times.
518 if (y < height - 2)
105 514 src_line[2] += src_linesize;
106 }
107 }
108
109 18 return 0;
110 }
111
112 18 static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
113 {
114 18 ThreadData *td = arg;
115 18 const AVFrame *in = td->in;
116 18 AVFrame *out = td->out;
117 18 const int slice_start = (in->height * jobnr ) / nb_jobs;
118 18 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
119
120
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (int p = 0; p < 1; p++) {
121 18 const int width = in->width;
122 18 const int height = in->height;
123 18 const int src_linesize = in->linesize[p] / 4;
124 18 const int dst_linesize = out->linesize[p] / 4;
125 18 const uint32_t *src = (const uint32_t *)in->data[p];
126 18 uint32_t *dst = (uint32_t *)out->data[p];
127 const uint32_t *src_line[3];
128
129 18 src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
130 18 src_line[1] = src + src_linesize * slice_start;
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
132
133
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 18 times.
536 for (int y = slice_start; y < slice_end; y++) {
134 uint32_t *dst_line[3];
135
136 518 dst_line[0] = dst + dst_linesize*3*y;
137 518 dst_line[1] = dst + dst_linesize*(3*y+1);
138 518 dst_line[2] = dst + dst_linesize*(3*y+2);
139
140
2/2
✓ Branch 0 taken 98938 times.
✓ Branch 1 taken 518 times.
99456 for (int x = 0; x < width; x++) {
141 uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8;
142 uint32_t A, B, C, D, E, F, G, H, I;
143
144
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 A = src_line[0][FFMAX(x-1, 0)];
145 98938 B = src_line[0][x];
146
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 C = src_line[0][FFMIN(x+1, width - 1)];
147
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 D = src_line[1][FFMAX(x-1, 0)];
148 98938 E = src_line[1][x];
149
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 F = src_line[1][FFMIN(x+1, width - 1)];
150
2/2
✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
98938 G = src_line[2][FFMAX(x-1, 0)];
151 98938 H = src_line[2][x];
152
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
98938 I = src_line[2][FFMIN(x+1, width - 1)];
153
154
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) {
155
2/2
✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
23048 E0 = D == B ? D : E;
156
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;
157
2/2
✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
23048 E2 = B == F ? F : E;
158
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;
159 23048 E4 = E;
160
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;
161
2/2
✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
23048 E6 = D == H ? D : E;
162
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;
163
2/2
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 15399 times.
23048 E8 = H == F ? F : E;
164 } else {
165 75890 E0 = E;
166 75890 E1 = E;
167 75890 E2 = E;
168 75890 E3 = E;
169 75890 E4 = E;
170 75890 E5 = E;
171 75890 E6 = E;
172 75890 E7 = E;
173 75890 E8 = E;
174 }
175
176 98938 dst_line[0][x*3] = E0;
177 98938 dst_line[0][x*3+1] = E1;
178 98938 dst_line[0][x*3+2] = E2;
179 98938 dst_line[1][x*3] = E3;
180 98938 dst_line[1][x*3+1] = E4;
181 98938 dst_line[1][x*3+2] = E5;
182 98938 dst_line[2][x*3] = E6;
183 98938 dst_line[2][x*3+1] = E7;
184 98938 dst_line[2][x*3+2] = E8;
185 }
186
187 518 src_line[0] = src_line[1];
188 518 src_line[1] = src_line[2];
189 518 src_line[2] = src_line[1];
190
191
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4 times.
518 if (y < height - 2)
192 514 src_line[2] += src_linesize;
193 }
194 }
195
196 18 return 0;
197 }
198
199 4 static int config_output(AVFilterLink *outlink)
200 {
201 4 AVFilterContext *ctx = outlink->src;
202 4 EPXContext *s = ctx->priv;
203 4 AVFilterLink *inlink = ctx->inputs[0];
204 const AVPixFmtDescriptor *desc;
205
206 4 desc = av_pix_fmt_desc_get(outlink->format);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!desc)
208 return AVERROR_BUG;
209
210 4 outlink->w = inlink->w * s->n;
211 4 outlink->h = inlink->h * s->n;
212
213
2/3
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 switch (s->n) {
214 2 case 2:
215 2 s->epx_slice = epx2_slice;
216 2 break;
217 2 case 3:
218 2 s->epx_slice = epx3_slice;
219 2 break;
220 }
221
222 4 return 0;
223 }
224
225 static const enum AVPixelFormat pix_fmts[] = {
226 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
227 AV_PIX_FMT_NONE,
228 };
229
230 4 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
231 {
232 4 AVFilterContext *ctx = inlink->dst;
233 4 AVFilterLink *outlink = ctx->outputs[0];
234 4 EPXContext *s = ctx->priv;
235 ThreadData td;
236
237 4 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!out) {
239 av_frame_free(&in);
240 return AVERROR(ENOMEM);
241 }
242
243 4 av_frame_copy_props(out, in);
244
245 4 td.in = in, td.out = out;
246 4 ff_filter_execute(ctx, s->epx_slice, &td, NULL,
247
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
248
249 4 av_frame_free(&in);
250 4 return ff_filter_frame(outlink, out);
251 }
252
253 static const AVFilterPad inputs[] = {
254 {
255 .name = "default",
256 .type = AVMEDIA_TYPE_VIDEO,
257 .filter_frame = filter_frame,
258 },
259 };
260
261 static const AVFilterPad outputs[] = {
262 {
263 .name = "default",
264 .type = AVMEDIA_TYPE_VIDEO,
265 .config_props = config_output,
266 },
267 };
268
269 const AVFilter ff_vf_epx = {
270 .name = "epx",
271 .description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."),
272 FILTER_INPUTS(inputs),
273 FILTER_OUTPUTS(outputs),
274 FILTER_PIXFMTS_ARRAY(pix_fmts),
275 .priv_size = sizeof(EPXContext),
276 .priv_class = &epx_class,
277 .flags = AVFILTER_FLAG_SLICE_THREADS,
278 };
279