FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_bwdif.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 66 72 91.7%
Functions: 4 4 100.0%
Branches: 41 50 82.0%

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 "internal.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 6426 static inline int job_start(const int jobnr, const int nb_jobs, const int h)
56 {
57
2/2
✓ Branch 0 taken 6069 times.
✓ Branch 1 taken 357 times.
6426 return jobnr >= nb_jobs ? h : ((h * jobnr) / nb_jobs) & ~3;
58 }
59
60 3213 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
61 {
62 3213 BWDIFContext *s = ctx->priv;
63 3213 YADIFContext *yadif = &s->yadif;
64 3213 ThreadData *td = arg;
65 3213 int linesize = yadif->cur->linesize[td->plane];
66 3213 int clip_max = (1 << (yadif->csp->comp[td->plane].depth)) - 1;
67 3213 int df = (yadif->csp->comp[td->plane].depth + 7) / 8;
68 3213 int refs = linesize / df;
69 3213 int slice_start = job_start(jobnr, nb_jobs, td->h);
70 3213 int slice_end = job_start(jobnr + 1, nb_jobs, td->h);
71 int y;
72
73
2/2
✓ Branch 0 taken 137088 times.
✓ Branch 1 taken 3213 times.
140301 for (y = slice_start; y < slice_end; y++) {
74
2/2
✓ Branch 0 taken 68544 times.
✓ Branch 1 taken 68544 times.
137088 if ((y ^ td->parity) & 1) {
75 68544 uint8_t *prev = &yadif->prev->data[td->plane][y * linesize];
76 68544 uint8_t *cur = &yadif->cur ->data[td->plane][y * linesize];
77 68544 uint8_t *next = &yadif->next->data[td->plane][y * linesize];
78 68544 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 66816 times.
68544 if (yadif->current_field == YADIF_FIELD_END) {
80
4/4
✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1725 times.
3456 s->dsp.filter_intra(dst, cur, td->w, (y + df) < td->h ? refs : -refs,
81 y > (df - 1) ? -refs : refs,
82
2/2
✓ Branch 0 taken 1713 times.
✓ Branch 1 taken 15 times.
1728 (y + 3*df) < td->h ? 3 * refs : -refs,
83 1728 y > (3*df - 1) ? -3 * refs : refs,
84
2/2
✓ Branch 0 taken 1707 times.
✓ Branch 1 taken 21 times.
1728 td->parity ^ td->tff, clip_max);
85
4/4
✓ Branch 0 taken 66120 times.
✓ Branch 1 taken 696 times.
✓ Branch 2 taken 696 times.
✓ Branch 3 taken 65424 times.
66816 } else if ((y < 4) || ((y + 5) > td->h)) {
86
2/2
✓ Branch 0 taken 1044 times.
✓ Branch 1 taken 348 times.
5220 s->dsp.filter_edge(dst, prev, cur, next, td->w,
87
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 1218 times.
1392 (y + df) < td->h ? refs : -refs,
88 y > (df - 1) ? -refs : refs,
89 1392 refs << 1, -(refs << 1),
90
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 261 times.
1392 td->parity ^ td->tff, clip_max,
91
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 348 times.
1044 (y < 2) || ((y + 3) > td->h) ? 0 : 1);
92
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 65424 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
65424 } else if (s->dsp.filter_line3 && y + 2 < slice_end && y + 6 < td->h) {
93 s->dsp.filter_line3(dst, td->frame->linesize[td->plane],
94 prev, cur, next, linesize, td->w,
95 td->parity ^ td->tff, clip_max);
96 y += 2;
97 } else {
98 65424 s->dsp.filter_line(dst, prev, cur, next, td->w,
99 65424 refs, -refs, refs << 1, -(refs << 1),
100 65424 3 * refs, -3 * refs, refs << 2, -(refs << 2),
101 65424 td->parity ^ td->tff, clip_max);
102 }
103 } else {
104 68544 memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
105 68544 &yadif->cur->data[td->plane][y * linesize], td->w * df);
106 }
107 }
108 3213 return 0;
109 }
110
111 119 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
112 int parity, int tff)
113 {
114 119 BWDIFContext *bwdif = ctx->priv;
115 119 YADIFContext *yadif = &bwdif->yadif;
116 119 ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
117 int i;
118
119
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 119 times.
476 for (i = 0; i < yadif->csp->nb_components; i++) {
120 357 int w = dstpic->width;
121 357 int h = dstpic->height;
122
123
4/4
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 119 times.
357 if (i == 1 || i == 2) {
124 238 w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
125 238 h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
126 }
127
128 357 td.w = w;
129 357 td.h = h;
130 357 td.plane = i;
131
132 357 ff_filter_execute(ctx, filter_slice, &td, NULL,
133
1/2
✓ Branch 0 taken 357 times.
✗ Branch 1 not taken.
357 FFMIN((h+3)/4, ff_filter_get_nb_threads(ctx)));
134 }
135
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 116 times.
119 if (yadif->current_field == YADIF_FIELD_END) {
136 3 yadif->current_field = YADIF_FIELD_NORMAL;
137 }
138 119 }
139
140 static const enum AVPixelFormat pix_fmts[] = {
141 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV420P,
142 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
143 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
144 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
145 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
146 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
147 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
148 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
149 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
150 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
151 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
152 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
153 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
154 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
155 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
156 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
157 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
158 AV_PIX_FMT_NONE
159 };
160
161 3 static int config_props(AVFilterLink *link)
162 {
163 3 AVFilterContext *ctx = link->src;
164 3 BWDIFContext *s = link->src->priv;
165 3 YADIFContext *yadif = &s->yadif;
166 int ret;
167
168 3 ret = ff_yadif_config_output_common(link);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
170 return AVERROR(EINVAL);
171
172 3 yadif->csp = av_pix_fmt_desc_get(link->format);
173 3 yadif->filter = filter;
174
175
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) {
176 av_log(ctx, AV_LOG_ERROR, "Video with planes less than 3 columns or 4 lines is not supported\n");
177 return AVERROR(EINVAL);
178 }
179
180 3 ff_bwdif_init_filter_line(&s->dsp, yadif->csp->comp[0].depth);
181
182 3 return 0;
183 }
184
185
186 #define OFFSET(x) offsetof(YADIFContext, x)
187 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
188
189 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
190
191 static const AVOption bwdif_options[] = {
192 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FIELD}, 0, 1, FLAGS, .unit = "mode"},
193 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
194 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
195
196 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
197 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
198 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
199 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
200
201 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
202 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
203 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
204
205 { NULL }
206 };
207
208 AVFILTER_DEFINE_CLASS(bwdif);
209
210 static const AVFilterPad avfilter_vf_bwdif_inputs[] = {
211 {
212 .name = "default",
213 .type = AVMEDIA_TYPE_VIDEO,
214 .filter_frame = ff_yadif_filter_frame,
215 },
216 };
217
218 static const AVFilterPad avfilter_vf_bwdif_outputs[] = {
219 {
220 .name = "default",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .request_frame = ff_yadif_request_frame,
223 .config_props = config_props,
224 },
225 };
226
227 const AVFilter ff_vf_bwdif = {
228 .name = "bwdif",
229 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
230 .priv_size = sizeof(BWDIFContext),
231 .priv_class = &bwdif_class,
232 .uninit = ff_yadif_uninit,
233 FILTER_INPUTS(avfilter_vf_bwdif_inputs),
234 FILTER_OUTPUTS(avfilter_vf_bwdif_outputs),
235 FILTER_PIXFMTS_ARRAY(pix_fmts),
236 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
237 };
238