FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_bwdif.c
Date: 2026-04-30 13:16:32
Exec Total Coverage
Lines: 72 78 92.3%
Functions: 4 4 100.0%
Branches: 51 62 82.3%

Line Branch Exec Source
1 /*
2 * BobWeaver Deinterlacing Filter
3 * Copyright (C) 2016 Thomas Mundt <loudmax@yahoo.de>
4 *
5 * Based on YADIF (Yet Another Deinterlacing Filter)
6 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
7 * 2010 James Darnley <james.darnley@gmail.com>
8 *
9 * With use of Weston 3 Field Deinterlacing Filter algorithm
10 * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
11 * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
12 * Based on the process described by Martin Weston for BBC R&D
13 *
14 * This file is part of FFmpeg.
15 *
16 * FFmpeg is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * FFmpeg is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with FFmpeg; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31 #include "libavutil/common.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "avfilter.h"
35 #include "bwdifdsp.h"
36 #include "ccfifo.h"
37 #include "filters.h"
38 #include "yadif.h"
39
40 typedef struct BWDIFContext {
41 YADIFContext yadif;
42 BWDIFDSPContext dsp;
43 } BWDIFContext;
44
45 typedef struct ThreadData {
46 AVFrame *frame;
47 int plane;
48 int w, h;
49 int parity;
50 int tff;
51 } ThreadData;
52
53 // Round job start line down to multiple of 4 so that if filter_line3 exists
54 // and the frame is a multiple of 4 high then filter_line will never be called
55 6588 static inline int job_start(const int jobnr, const int nb_jobs, const int h)
56 {
57
2/2
✓ Branch 0 taken 6222 times.
✓ Branch 1 taken 366 times.
6588 return jobnr >= nb_jobs ? h : ((h * jobnr) / nb_jobs) & ~3;
58 }
59
60 3294 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
61 {
62 3294 BWDIFContext *s = ctx->priv;
63 3294 YADIFContext *yadif = &s->yadif;
64 3294 ThreadData *td = arg;
65 3294 int linesize = yadif->cur->linesize[td->plane];
66 3294 int clip_max = (1 << (yadif->csp->comp[td->plane].depth)) - 1;
67 3294 int df = (yadif->csp->comp[td->plane].depth + 7) / 8;
68 3294 int refs = linesize / df;
69 3294 int slice_start = job_start(jobnr, nb_jobs, td->h);
70 3294 int slice_end = job_start(jobnr + 1, nb_jobs, td->h);
71 int y;
72
73
2/2
✓ Branch 0 taken 140544 times.
✓ Branch 1 taken 3294 times.
143838 for (y = slice_start; y < slice_end; y++) {
74
2/2
✓ Branch 0 taken 70272 times.
✓ Branch 1 taken 70272 times.
140544 if ((y ^ td->parity) & 1) {
75 70272 uint8_t *prev = &yadif->prev->data[td->plane][y * linesize];
76 70272 uint8_t *cur = &yadif->cur ->data[td->plane][y * linesize];
77 70272 uint8_t *next = &yadif->next->data[td->plane][y * linesize];
78 70272 uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
79
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 68544 times.
70272 if (yadif->current_field == YADIF_FIELD_END) {
80
4/4
✓ Branch 0 taken 1710 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1701 times.
1728 if ((y < 3) || ((y + 3) >= td->h)) {
81
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
99 s->dsp.filter_edge(dst, prev, cur, next, td->w,
82
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 24 times.
27 (y + df) < td->h ? refs : -refs,
83 y > (df - 1) ? -refs : refs,
84 27 refs << 1, -(refs << 1),
85
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 td->parity ^ td->tff, clip_max,
86
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 (y < 2) || ((y + 3) > td->h) ? 0 : 1);
87 } else {
88
2/4
✓ Branch 0 taken 1701 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1701 times.
3402 s->dsp.filter_intra(dst, cur, td->w, (y + df) < td->h ? refs : -refs,
89 y > (df - 1) ? -refs : refs,
90
2/2
✓ Branch 0 taken 1695 times.
✓ Branch 1 taken 6 times.
1701 (y + 3*df) < td->h ? 3 * refs : -refs,
91 1701 y > (3*df - 1) ? -3 * refs : refs,
92
2/2
✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 3 times.
1701 td->parity ^ td->tff, clip_max);
93 }
94
4/4
✓ Branch 0 taken 67830 times.
✓ Branch 1 taken 714 times.
✓ Branch 2 taken 714 times.
✓ Branch 3 taken 67116 times.
68544 } else if ((y < 4) || ((y + 5) > td->h)) {
95
2/2
✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 357 times.
5355 s->dsp.filter_edge(dst, prev, cur, next, td->w,
96
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1248 times.
1428 (y + df) < td->h ? refs : -refs,
97 y > (df - 1) ? -refs : refs,
98 1428 refs << 1, -(refs << 1),
99
2/2
✓ Branch 0 taken 1161 times.
✓ Branch 1 taken 267 times.
1428 td->parity ^ td->tff, clip_max,
100
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 357 times.
1071 (y < 2) || ((y + 3) > td->h) ? 0 : 1);
101
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 67116 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
67116 } else if (s->dsp.filter_line3 && y + 2 < slice_end && y + 6 < td->h) {
102 s->dsp.filter_line3(dst, td->frame->linesize[td->plane],
103 prev, cur, next, linesize, td->w,
104 td->parity ^ td->tff, clip_max);
105 y += 2;
106 } else {
107 67116 s->dsp.filter_line(dst, prev, cur, next, td->w,
108 67116 refs, -refs, refs << 1, -(refs << 1),
109 67116 3 * refs, -3 * refs, refs << 2, -(refs << 2),
110 67116 td->parity ^ td->tff, clip_max);
111 }
112 } else {
113 70272 memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
114 70272 &yadif->cur->data[td->plane][y * linesize], td->w * df);
115 }
116 }
117 3294 return 0;
118 }
119
120 122 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
121 int parity, int tff)
122 {
123 122 BWDIFContext *bwdif = ctx->priv;
124 122 YADIFContext *yadif = &bwdif->yadif;
125 122 ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
126 int i;
127
128
2/2
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 122 times.
488 for (i = 0; i < yadif->csp->nb_components; i++) {
129 366 int w = dstpic->width;
130 366 int h = dstpic->height;
131
132
4/4
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 122 times.
✓ Branch 3 taken 122 times.
366 if (i == 1 || i == 2) {
133 244 w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
134 244 h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
135 }
136
137 366 td.w = w;
138 366 td.h = h;
139 366 td.plane = i;
140
141 366 ff_filter_execute(ctx, filter_slice, &td, NULL,
142
1/2
✓ Branch 0 taken 366 times.
✗ Branch 1 not taken.
366 FFMIN((h+3)/4, ff_filter_get_nb_threads(ctx)));
143 }
144
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 119 times.
122 if (yadif->current_field == YADIF_FIELD_END) {
145 3 yadif->current_field = YADIF_FIELD_NORMAL;
146 }
147 122 }
148
149 static const enum AVPixelFormat pix_fmts[] = {
150 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV420P,
151 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
152 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
153 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
154 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
155 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
156 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
157 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
158 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
159 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
160 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
161 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
162 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
163 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
164 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
165 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
166 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
167 AV_PIX_FMT_NONE
168 };
169
170 3 static int config_props(AVFilterLink *link)
171 {
172 3 AVFilterContext *ctx = link->src;
173 3 BWDIFContext *s = link->src->priv;
174 3 YADIFContext *yadif = &s->yadif;
175 int ret;
176
177 3 ret = ff_yadif_config_output_common(link);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
179 return AVERROR(EINVAL);
180
181 3 yadif->csp = av_pix_fmt_desc_get(link->format);
182 3 yadif->filter = filter;
183
184
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (AV_CEIL_RSHIFT(link->w, yadif->csp->log2_chroma_w) < 3 || AV_CEIL_RSHIFT(link->h, yadif->csp->log2_chroma_h) < 4) {
185 av_log(ctx, AV_LOG_ERROR, "Video with planes less than 3 columns or 4 lines is not supported\n");
186 return AVERROR(EINVAL);
187 }
188
189 3 ff_bwdif_init_filter_line(&s->dsp, yadif->csp->comp[0].depth);
190
191 3 return 0;
192 }
193
194
195 #define OFFSET(x) offsetof(YADIFContext, x)
196 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
197
198 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
199
200 static const AVOption bwdif_options[] = {
201 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FIELD}, 0, 1, FLAGS, .unit = "mode"},
202 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
203 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
204
205 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
206 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
207 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
208 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
209
210 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
211 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
212 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
213
214 { NULL }
215 };
216
217 AVFILTER_DEFINE_CLASS(bwdif);
218
219 static const AVFilterPad avfilter_vf_bwdif_inputs[] = {
220 {
221 .name = "default",
222 .type = AVMEDIA_TYPE_VIDEO,
223 .filter_frame = ff_yadif_filter_frame,
224 },
225 };
226
227 static const AVFilterPad avfilter_vf_bwdif_outputs[] = {
228 {
229 .name = "default",
230 .type = AVMEDIA_TYPE_VIDEO,
231 .request_frame = ff_yadif_request_frame,
232 .config_props = config_props,
233 },
234 };
235
236 const FFFilter ff_vf_bwdif = {
237 .p.name = "bwdif",
238 .p.description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
239 .p.priv_class = &bwdif_class,
240 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
241 .priv_size = sizeof(BWDIFContext),
242 .uninit = ff_yadif_uninit,
243 FILTER_INPUTS(avfilter_vf_bwdif_inputs),
244 FILTER_OUTPUTS(avfilter_vf_bwdif_outputs),
245 FILTER_PIXFMTS_ARRAY(pix_fmts),
246 };
247