FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_mcdeint.c
Date: 2026-07-19 01:11:25
Exec Total Coverage
Lines: 104 122 85.2%
Functions: 3 3 100.0%
Branches: 58 71 81.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 /**
22 * @file
23 * Motion Compensation Deinterlacer
24 * Ported from MPlayer libmpcodecs/vf_mcdeint.c.
25 *
26 * Known Issues:
27 *
28 * The motion estimation is somewhat at the mercy of the input, if the
29 * input frames are created purely based on spatial interpolation then
30 * for example a thin black line or another random and not
31 * interpolateable pattern will cause problems.
32 * Note: completely ignoring the "unavailable" lines during motion
33 * estimation did not look any better, so the most obvious solution
34 * would be to improve tfields or penalize problematic motion vectors.
35 *
36 * If non iterative ME is used then snow currently ignores the OBMC
37 * window and as a result sometimes creates artifacts.
38 *
39 * Only past frames are used, we should ideally use future frames too,
40 * something like filtering the whole movie in forward and then
41 * backward direction seems like an interesting idea but the current
42 * filter framework is FAR from supporting such things.
43 *
44 * Combining the motion compensated image with the input image also is
45 * not as trivial as it seems, simple blindly taking even lines from
46 * one and odd ones from the other does not work at all as ME/MC
47 * sometimes has nothing in the previous frames which matches the
48 * current. The current algorithm has been found by trial and error
49 * and almost certainly can be improved...
50 */
51
52 #include "libavutil/attributes.h"
53 #include "libavutil/opt.h"
54 #include "libavcodec/avcodec.h"
55 #include "libavutil/pixdesc.h"
56 #include "avfilter.h"
57 #include "filters.h"
58 #include "video.h"
59
60 enum MCDeintMode {
61 MODE_FAST = 0,
62 MODE_MEDIUM,
63 MODE_SLOW,
64 MODE_EXTRA_SLOW,
65 MODE_NB,
66 };
67
68 enum MCDeintParity {
69 PARITY_TFF = 0, ///< top field first
70 PARITY_BFF = 1, ///< bottom field first
71 };
72
73 typedef struct MCDeintContext {
74 const AVClass *class;
75 int mode; ///< MCDeintMode
76 int parity; ///< MCDeintParity
77 int qp;
78 AVPacket *pkt;
79 AVFrame *frame_dec;
80 AVCodecContext *enc_ctx;
81 } MCDeintContext;
82
83 #define OFFSET(x) offsetof(MCDeintContext, x)
84 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
85 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
86
87 static const AVOption mcdeint_options[] = {
88 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_FAST}, 0, MODE_NB-1, FLAGS, .unit="mode" },
89 CONST("fast", NULL, MODE_FAST, "mode"),
90 CONST("medium", NULL, MODE_MEDIUM, "mode"),
91 CONST("slow", NULL, MODE_SLOW, "mode"),
92 CONST("extra_slow", NULL, MODE_EXTRA_SLOW, "mode"),
93
94 { "parity", "set the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=PARITY_BFF}, -1, 1, FLAGS, .unit = "parity" },
95 CONST("tff", "assume top field first", PARITY_TFF, "parity"),
96 CONST("bff", "assume bottom field first", PARITY_BFF, "parity"),
97
98 { "qp", "set qp", OFFSET(qp), AV_OPT_TYPE_INT, {.i64=1}, INT_MIN, INT_MAX, FLAGS },
99 { NULL }
100 };
101
102 AVFILTER_DEFINE_CLASS(mcdeint);
103
104 4 static int config_props(AVFilterLink *inlink)
105 {
106 4 AVFilterContext *ctx = inlink->dst;
107 4 MCDeintContext *mcdeint = ctx->priv;
108 const AVCodec *enc;
109 AVCodecContext *enc_ctx;
110 4 AVDictionary *opts = NULL;
111 int ret;
112
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!(enc = avcodec_find_encoder(AV_CODEC_ID_SNOW))) {
114 av_log(ctx, AV_LOG_ERROR, "Snow encoder is not enabled in libavcodec\n");
115 return AVERROR(EINVAL);
116 }
117
118 4 mcdeint->pkt = av_packet_alloc();
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!mcdeint->pkt)
120 return AVERROR(ENOMEM);
121 4 mcdeint->frame_dec = av_frame_alloc();
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!mcdeint->frame_dec)
123 return AVERROR(ENOMEM);
124 4 mcdeint->enc_ctx = avcodec_alloc_context3(enc);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!mcdeint->enc_ctx)
126 return AVERROR(ENOMEM);
127 4 enc_ctx = mcdeint->enc_ctx;
128 4 enc_ctx->width = inlink->w;
129 4 enc_ctx->height = inlink->h;
130 4 enc_ctx->time_base = (AVRational){1,25}; // meaningless
131 4 enc_ctx->gop_size = INT_MAX;
132 4 enc_ctx->max_b_frames = 0;
133 4 enc_ctx->pix_fmt = inlink->format;
134 4 enc_ctx->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_LOW_DELAY | AV_CODEC_FLAG_RECON_FRAME;
135 4 enc_ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
136 4 enc_ctx->global_quality = 1;
137 4 enc_ctx->me_cmp = enc_ctx->me_sub_cmp = FF_CMP_SAD;
138 4 enc_ctx->mb_cmp = FF_CMP_SSE;
139 4 av_dict_set(&opts, "memc_only", "1", 0);
140 4 av_dict_set(&opts, "no_bitstream", "1", 0);
141
142
3/5
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
4 switch (mcdeint->mode) {
143 case MODE_EXTRA_SLOW:
144 enc_ctx->refs = 3;
145 av_fallthrough;
146 2 case MODE_SLOW:
147 2 av_dict_set(&opts, "motion_est", "iter", 0);
148 av_fallthrough;
149 3 case MODE_MEDIUM:
150 3 enc_ctx->flags |= AV_CODEC_FLAG_4MV;
151 3 enc_ctx->dia_size = 2;
152 av_fallthrough;
153 4 case MODE_FAST:
154 4 enc_ctx->flags |= AV_CODEC_FLAG_QPEL;
155 }
156
157 4 ret = avcodec_open2(enc_ctx, enc, &opts);
158 4 av_dict_free(&opts);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
160 return ret;
161
162 4 return 0;
163 }
164
165 8 static av_cold void uninit(AVFilterContext *ctx)
166 {
167 8 MCDeintContext *mcdeint = ctx->priv;
168
169 8 av_packet_free(&mcdeint->pkt);
170 8 avcodec_free_context(&mcdeint->enc_ctx);
171 8 av_frame_free(&mcdeint->frame_dec);
172 8 }
173
174 70 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
175 {
176 70 MCDeintContext *mcdeint = inlink->dst->priv;
177 70 AVFilterLink *outlink = inlink->dst->outputs[0];
178 70 AVFrame *outpic, *frame_dec = mcdeint->frame_dec;
179 70 AVPacket *pkt = mcdeint->pkt;
180 70 const AVPixFmtDescriptor *pix_fmt_desc = av_pix_fmt_desc_get(inlink->format);
181 int x, y, i, ret;
182
183 70 outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (!outpic) {
185 av_frame_free(&inpic);
186 return AVERROR(ENOMEM);
187 }
188 70 av_frame_copy_props(outpic, inpic);
189 70 inpic->quality = mcdeint->qp * FF_QP2LAMBDA;
190
191 70 ret = avcodec_send_frame(mcdeint->enc_ctx, inpic);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0) {
193 av_log(mcdeint->enc_ctx, AV_LOG_ERROR, "Error sending a frame for encoding\n");
194 goto end;
195 }
196 70 ret = avcodec_receive_packet(mcdeint->enc_ctx, pkt);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0) {
198 av_log(mcdeint->enc_ctx, AV_LOG_ERROR, "Error receiving a packet from encoding\n");
199 goto end;
200 }
201 70 av_packet_unref(pkt);
202 70 ret = avcodec_receive_frame(mcdeint->enc_ctx, frame_dec);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0) {
204 av_log(mcdeint->enc_ctx, AV_LOG_ERROR, "Error receiving a frame from encoding\n");
205 goto end;
206 }
207
208
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 70 times.
280 for (i = 0; i < 3; i++) {
209 210 int is_chroma = !!i;
210 int w, h;
211
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 70 times.
210 if (is_chroma) {
212 140 w = AV_CEIL_RSHIFT(inlink->w, pix_fmt_desc->log2_chroma_w);
213 140 h = AV_CEIL_RSHIFT(inlink->h, pix_fmt_desc->log2_chroma_h);
214 } else {
215 70 w = inlink->w;
216 70 h = inlink->h;
217 }
218 210 int fils = frame_dec->linesize[i];
219 210 int srcs = inpic ->linesize[i];
220 210 int dsts = outpic ->linesize[i];
221
222
2/2
✓ Branch 0 taken 71832 times.
✓ Branch 1 taken 210 times.
72042 for (y = 0; y < h; y++) {
223
2/2
✓ Branch 0 taken 35916 times.
✓ Branch 1 taken 35916 times.
71832 if ((y ^ mcdeint->parity) & 1) {
224
2/2
✓ Branch 0 taken 19286976 times.
✓ Branch 1 taken 35916 times.
19322892 for (x = 0; x < w; x++) {
225 19286976 uint8_t *filp = &frame_dec->data[i][x + y*fils];
226 19286976 uint8_t *srcp = &inpic ->data[i][x + y*srcs];
227 19286976 uint8_t *dstp = &outpic ->data[i][x + y*dsts];
228
229
4/4
✓ Branch 0 taken 19240098 times.
✓ Branch 1 taken 46878 times.
✓ Branch 2 taken 19196100 times.
✓ Branch 3 taken 43998 times.
19286976 if (y > 0 && y < h-1){
230
4/4
✓ Branch 0 taken 19088982 times.
✓ Branch 1 taken 107118 times.
✓ Branch 2 taken 106938 times.
✓ Branch 3 taken 18982044 times.
19196100 int is_edge = x < 3 || x > w-4;
231 19196100 int diff0 = filp[-fils] - srcp[-srcs];
232 19196100 int diff1 = filp[+fils] - srcp[+srcs];
233 19196100 int temp = filp[0];
234
235 #define DELTA(j) av_clip(j, -x, w-1-x)
236
237 #define GET_SCORE_EDGE(j)\
238 FFABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\
239 FFABS(srcp[-srcs+DELTA(j) ] - srcp[+srcs+DELTA( -(j))])+\
240 FFABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))])
241
242 #define GET_SCORE(j)\
243 FFABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\
244 FFABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])+\
245 FFABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)])
246
247 #define CHECK_EDGE(j)\
248 { int score = GET_SCORE_EDGE(j);\
249 if (score < spatial_score){\
250 spatial_score = score;\
251 diff0 = filp[-fils+DELTA(j)] - srcp[-srcs+DELTA(j)];\
252 diff1 = filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\
253
254 #define CHECK(j)\
255 { int score = GET_SCORE(j);\
256 if (score < spatial_score){\
257 spatial_score= score;\
258 diff0 = filp[-fils+(j)] - srcp[-srcs+(j)];\
259 diff1 = filp[+fils-(j)] - srcp[+srcs-(j)];\
260
261
2/2
✓ Branch 0 taken 214056 times.
✓ Branch 1 taken 18982044 times.
19196100 if (is_edge) {
262 214056 int spatial_score = GET_SCORE_EDGE(0) - 1;
263
4/4
✓ Branch 0 taken 1922 times.
✓ Branch 1 taken 212134 times.
✓ Branch 2 taken 599 times.
✓ Branch 3 taken 1323 times.
214056 CHECK_EDGE(-1) CHECK_EDGE(-2) }} }}
264
4/4
✓ Branch 0 taken 1752 times.
✓ Branch 1 taken 212304 times.
✓ Branch 2 taken 586 times.
✓ Branch 3 taken 1166 times.
214056 CHECK_EDGE( 1) CHECK_EDGE( 2) }} }}
265 } else {
266 18982044 int spatial_score = GET_SCORE(0) - 1;
267
4/4
✓ Branch 0 taken 3318352 times.
✓ Branch 1 taken 15663692 times.
✓ Branch 2 taken 1399408 times.
✓ Branch 3 taken 1918944 times.
18982044 CHECK(-1) CHECK(-2) }} }}
268
4/4
✓ Branch 0 taken 2960014 times.
✓ Branch 1 taken 16022030 times.
✓ Branch 2 taken 1136916 times.
✓ Branch 3 taken 1823098 times.
18982044 CHECK( 1) CHECK( 2) }} }}
269 }
270
271
272
2/2
✓ Branch 0 taken 8155451 times.
✓ Branch 1 taken 11040649 times.
19196100 if (diff0 + diff1 > 0)
273 8155451 temp -= (diff0 + diff1 - FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
274 else
275 11040649 temp -= (diff0 + diff1 + FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19196100 times.
19196100 *filp = *dstp = temp > 255U ? ~(temp>>31) : temp;
277 } else {
278 90876 *dstp = *filp;
279 }
280 }
281 }
282 }
283
284
2/2
✓ Branch 0 taken 71832 times.
✓ Branch 1 taken 210 times.
72042 for (y = 0; y < h; y++) {
285
2/2
✓ Branch 0 taken 35916 times.
✓ Branch 1 taken 35916 times.
71832 if (!((y ^ mcdeint->parity) & 1)) {
286
2/2
✓ Branch 0 taken 19286976 times.
✓ Branch 1 taken 35916 times.
19322892 for (x = 0; x < w; x++) {
287 19286976 frame_dec->data[i][x + y*fils] =
288 19286976 outpic ->data[i][x + y*dsts] = inpic->data[i][x + y*srcs];
289 }
290 }
291 }
292 }
293 70 mcdeint->parity ^= 1;
294
295 70 end:
296 70 av_packet_unref(pkt);
297 70 av_frame_free(&inpic);
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ret < 0) {
299 av_frame_free(&outpic);
300 return ret;
301 }
302 70 return ff_filter_frame(outlink, outpic);
303 }
304
305 static const AVFilterPad mcdeint_inputs[] = {
306 {
307 .name = "default",
308 .type = AVMEDIA_TYPE_VIDEO,
309 .filter_frame = filter_frame,
310 .config_props = config_props,
311 },
312 };
313
314 const FFFilter ff_vf_mcdeint = {
315 .p.name = "mcdeint",
316 .p.description = NULL_IF_CONFIG_SMALL("Apply motion compensating deinterlacing."),
317 .p.priv_class = &mcdeint_class,
318 .priv_size = sizeof(MCDeintContext),
319 .uninit = uninit,
320 FILTER_INPUTS(mcdeint_inputs),
321 FILTER_OUTPUTS(ff_video_default_filterpad),
322 FILTER_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P),
323 };
324