FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/yadif_common.c
Date: 2026-10-17 06:17:55
Exec Total Coverage
Lines: 103 154 66.9%
Functions: 6 7 85.7%
Branches: 60 110 54.5%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3 * 2010 James Darnley <james.darnley@gmail.com>
4
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/imgutils.h"
24 #include "filters.h"
25 #include "video.h"
26 #include "yadif.h"
27
28 308 static int return_frame(AVFilterContext *ctx, int is_second)
29 {
30 308 YADIFContext *yadif = ctx->priv;
31 308 AVFilterLink *link = ctx->outputs[0];
32 int tff, ret;
33
34
1/2
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
308 if (yadif->parity == -1) {
35 308 tff = (yadif->cur->flags & AV_FRAME_FLAG_INTERLACED) ?
36
2/4
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 308 times.
308 !!(yadif->cur->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 1;
37 } else {
38 ✗ tff = yadif->parity ^ 1;
39 }
40
41
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 248 times.
308 if (is_second) {
42 60 yadif->out = ff_get_video_buffer(link, link->w, link->h);
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!yadif->out)
44 ✗ return AVERROR(ENOMEM);
45
46 60 av_frame_copy_props(yadif->out, yadif->cur);
47 60 yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (yadif->current_field == YADIF_FIELD_BACK_END)
49 ✗ yadif->current_field = YADIF_FIELD_END;
50 }
51
52 308 ret = yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (ret < 0) {
54 ✗ av_frame_free(&yadif->out);
55 ✗ return ret;
56 }
57
58
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 248 times.
308 if (is_second) {
59 60 int64_t cur_pts = yadif->cur->pts;
60 60 int64_t next_pts = yadif->next->pts;
61
62
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
63 60 yadif->out->pts = cur_pts + next_pts;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (yadif->pts_multiplier == 1) {
65 ✗ yadif->out->pts >>= 1;
66 ✗ yadif->out->duration >>= 1;
67 }
68 } else {
69 ✗ yadif->out->pts = AV_NOPTS_VALUE;
70 }
71 }
72
73 308 ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
74 308 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
75
76
4/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 60 times.
308 yadif->frame_pending = (yadif->mode&1) && !is_second;
77 308 return ret;
78 }
79
80 1264 static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
81 {
82 int i;
83
2/2
✓ Branch 0 taken 3792 times.
✓ Branch 1 taken 1264 times.
5056 for (i = 0; i < yadif->csp->nb_components; i++)
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3792 times.
3792 if (a->linesize[i] != b->linesize[i])
85 ✗ return 1;
86 1264 return 0;
87 }
88
89 ✗ static void fixstride(AVFilterLink *link, AVFrame *f)
90 {
91 ✗ AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
92 ✗ if(!dst)
93 ✗ return;
94 ✗ av_frame_copy_props(dst, f);
95 ✗ av_image_copy2(dst->data, dst->linesize,
96 ✗ f->data, f->linesize,
97 ✗ dst->format, dst->width, dst->height);
98 ✗ av_frame_unref(f);
99 ✗ av_frame_move_ref(f, dst);
100 ✗ av_frame_free(&dst);
101 }
102
103 256 int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
104 {
105 256 AVFilterContext *ctx = link->dst;
106 256 YADIFContext *yadif = ctx->priv;
107 int ret;
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256 times.
256 av_assert0(frame);
110
111 256 ff_ccfifo_extract(&yadif->cc_fifo, frame);
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256 times.
256 if (yadif->frame_pending) {
114 ✗ ret = return_frame(ctx, 1);
115 ✗ if (ret < 0) {
116 ✗ av_frame_free(&frame);
117 ✗ return ret;
118 }
119 }
120
121
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 16 times.
256 if (yadif->prev)
122 240 av_frame_free(&yadif->prev);
123 256 yadif->prev = yadif->cur;
124 256 yadif->cur = yadif->next;
125 256 yadif->next = frame;
126
127
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 248 times.
256 if (!yadif->cur) {
128 8 yadif->cur = av_frame_clone(yadif->next);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!yadif->cur)
130 ✗ return AVERROR(ENOMEM);
131 8 yadif->current_field = YADIF_FIELD_END;
132 }
133
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 256 times.
256 if (checkstride(yadif, yadif->next, yadif->cur)) {
135 ✗ av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
136 ✗ fixstride(link, yadif->next);
137 }
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 256 times.
256 if (checkstride(yadif, yadif->next, yadif->cur))
139 ✗ fixstride(link, yadif->cur);
140
3/4
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 248 times.
256 if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
141 ✗ fixstride(link, yadif->prev);
142
4/6
✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 248 times.
✓ Branch 4 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 248 times.
256 if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
143 ✗ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
144 ✗ return -1;
145 }
146
147
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 248 times.
256 if (!yadif->prev)
148 8 return 0;
149
150
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
248 if ((yadif->deint && !(yadif->cur->flags & AV_FRAME_FLAG_INTERLACED)) ||
151
1/2
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
248 ctx->is_disabled ||
152
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
248 (yadif->deint && !(yadif->prev->flags & AV_FRAME_FLAG_INTERLACED) && yadif->prev->repeat_pict) ||
153
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
248 (yadif->deint && !(yadif->next->flags & AV_FRAME_FLAG_INTERLACED) && yadif->next->repeat_pict)
154 ) {
155 ✗ yadif->out = av_frame_clone(yadif->cur);
156 ✗ if (!yadif->out)
157 ✗ return AVERROR(ENOMEM);
158
159 ✗ ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
160 ✗ av_frame_free(&yadif->prev);
161 ✗ if (yadif->out->pts != AV_NOPTS_VALUE)
162 ✗ yadif->out->pts *= yadif->pts_multiplier;
163 ✗ yadif->out->duration *= yadif->pts_multiplier;
164 ✗ return ff_filter_frame(ctx->outputs[0], yadif->out);
165 }
166
167 248 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (!yadif->out)
169 ✗ return AVERROR(ENOMEM);
170
171 248 av_frame_copy_props(yadif->out, yadif->cur);
172 248 yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
173
174
1/2
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
248 if (yadif->out->pts != AV_NOPTS_VALUE)
175 248 yadif->out->pts *= yadif->pts_multiplier;
176
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 62 times.
248 if (!(yadif->mode & 1))
177 186 yadif->out->duration *= yadif->pts_multiplier;
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 else if (yadif->pts_multiplier == 1)
179 ✗ yadif->out->duration >>= 1;
180
181 248 return return_frame(ctx, 0);
182 }
183
184 284 int ff_yadif_request_frame(AVFilterLink *link)
185 {
186 284 AVFilterContext *ctx = link->src;
187 284 YADIFContext *yadif = ctx->priv;
188 int ret;
189
190
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 224 times.
284 if (yadif->frame_pending)
191 60 return return_frame(ctx, 1);
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 if (yadif->eof)
194 ✗ return AVERROR_EOF;
195
196 224 ret = ff_request_frame(ctx->inputs[0]);
197
198
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
224 if (ret == AVERROR_EOF && yadif->cur) {
199 8 AVFrame *next = av_frame_clone(yadif->next);
200
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!next)
202 ✗ return AVERROR(ENOMEM);
203
204 8 yadif->current_field = YADIF_FIELD_BACK_END;
205 8 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
206
207 8 ret = ff_yadif_filter_frame(ctx->inputs[0], next);
208 8 yadif->eof = 1;
209 }
210
211 224 return ret;
212 }
213
214 8 int ff_yadif_config_output_common(AVFilterLink *outlink)
215 {
216 8 AVFilterContext *ctx = outlink->src;
217 8 FilterLink *il = ff_filter_link(ctx->inputs[0]);
218 8 FilterLink *ol = ff_filter_link(outlink);
219 8 YADIFContext *yadif = ctx->priv;
220 8 AVRational tb = ctx->inputs[0]->time_base;
221 int ret;
222
223
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 if (av_reduce(&outlink->time_base.num, &outlink->time_base.den, tb.num, tb.den * 2LL, INT_MAX)) {
224 8 yadif->pts_multiplier = 2;
225 } else {
226 ✗ av_log(ctx, AV_LOG_WARNING, "Cannot use exact output timebase\n");
227 ✗ outlink->time_base = tb;
228 ✗ yadif->pts_multiplier = 1;
229 }
230
231 8 outlink->w = ctx->inputs[0]->w;
232 8 outlink->h = ctx->inputs[0]->h;
233
234
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (outlink->w < 3 || outlink->h < 3) {
235 ✗ av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
236 ✗ return AVERROR(EINVAL);
237 }
238
239
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if(yadif->mode & 1)
240 2 ol->frame_rate = av_mul_q(il->frame_rate,
241 2 (AVRational){2, 1});
242 else
243 6 ol->frame_rate = il->frame_rate;
244
245 8 ret = ff_ccfifo_init(&yadif->cc_fifo, ol->frame_rate, ctx);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0) {
247 ✗ av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
248 ✗ return ret;
249 }
250
251 8 return 0;
252 }
253
254 16 void ff_yadif_uninit(AVFilterContext *ctx)
255 {
256 16 YADIFContext *yadif = ctx->priv;
257
258 16 av_frame_free(&yadif->prev);
259 16 av_frame_free(&yadif->cur );
260 16 av_frame_free(&yadif->next);
261 16 ff_ccfifo_uninit(&yadif->cc_fifo);
262 16 }
263
264 #define OFFSET(x) offsetof(YADIFContext, x)
265 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
266
267 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
268
269 const AVOption ff_yadif_options[] = {
270 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, .unit = "mode"},
271 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
272 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
273 CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
274 CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
275
276 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
277 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
278 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
279 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
280
281 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
282 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
283 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
284
285 { NULL }
286 };
287