FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/yadif_common.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 98 151 64.9%
Functions: 6 7 85.7%
Branches: 57 108 52.8%

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 268 static int return_frame(AVFilterContext *ctx, int is_second)
29 {
30 268 YADIFContext *yadif = ctx->priv;
31 268 AVFilterLink *link = ctx->outputs[0];
32 int tff, ret;
33
34
1/2
✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
268 if (yadif->parity == -1) {
35 268 tff = (yadif->cur->flags & AV_FRAME_FLAG_INTERLACED) ?
36
2/4
✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 268 times.
268 !!(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 58 times.
✓ Branch 1 taken 210 times.
268 if (is_second) {
42 58 yadif->out = ff_get_video_buffer(link, link->w, link->h);
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!yadif->out)
44 return AVERROR(ENOMEM);
45
46 58 av_frame_copy_props(yadif->out, yadif->cur);
47 58 yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (yadif->current_field == YADIF_FIELD_BACK_END)
49 yadif->current_field = YADIF_FIELD_END;
50 }
51
52 268 yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
53
54
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 210 times.
268 if (is_second) {
55 58 int64_t cur_pts = yadif->cur->pts;
56 58 int64_t next_pts = yadif->next->pts;
57
58
2/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
58 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
59 58 yadif->out->pts = cur_pts + next_pts;
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (yadif->pts_multiplier == 1) {
61 yadif->out->pts >>= 1;
62 yadif->out->duration >>= 1;
63 }
64 } else {
65 yadif->out->pts = AV_NOPTS_VALUE;
66 }
67 }
68
69 268 ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
70 268 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
71
72
4/4
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 58 times.
268 yadif->frame_pending = (yadif->mode&1) && !is_second;
73 268 return ret;
74 }
75
76 1071 static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
77 {
78 int i;
79
2/2
✓ Branch 0 taken 3213 times.
✓ Branch 1 taken 1071 times.
4284 for (i = 0; i < yadif->csp->nb_components; i++)
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3213 times.
3213 if (a->linesize[i] != b->linesize[i])
81 return 1;
82 1071 return 0;
83 }
84
85 static void fixstride(AVFilterLink *link, AVFrame *f)
86 {
87 AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
88 if(!dst)
89 return;
90 av_frame_copy_props(dst, f);
91 av_image_copy2(dst->data, dst->linesize,
92 f->data, f->linesize,
93 dst->format, dst->width, dst->height);
94 av_frame_unref(f);
95 av_frame_move_ref(f, dst);
96 av_frame_free(&dst);
97 }
98
99 217 int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
100 {
101 217 AVFilterContext *ctx = link->dst;
102 217 YADIFContext *yadif = ctx->priv;
103
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 av_assert0(frame);
105
106 217 ff_ccfifo_extract(&yadif->cc_fifo, frame);
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 if (yadif->frame_pending)
109 return_frame(ctx, 1);
110
111
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 14 times.
217 if (yadif->prev)
112 203 av_frame_free(&yadif->prev);
113 217 yadif->prev = yadif->cur;
114 217 yadif->cur = yadif->next;
115 217 yadif->next = frame;
116
117
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 210 times.
217 if (!yadif->cur) {
118 7 yadif->cur = av_frame_clone(yadif->next);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!yadif->cur)
120 return AVERROR(ENOMEM);
121 7 yadif->current_field = YADIF_FIELD_END;
122 }
123
124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 217 times.
217 if (checkstride(yadif, yadif->next, yadif->cur)) {
125 av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
126 fixstride(link, yadif->next);
127 }
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 217 times.
217 if (checkstride(yadif, yadif->next, yadif->cur))
129 fixstride(link, yadif->cur);
130
3/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 210 times.
217 if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
131 fixstride(link, yadif->prev);
132
4/6
✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 210 times.
✓ Branch 4 taken 7 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 210 times.
217 if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
133 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
134 return -1;
135 }
136
137
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 210 times.
217 if (!yadif->prev)
138 7 return 0;
139
140
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
210 if ((yadif->deint && !(yadif->cur->flags & AV_FRAME_FLAG_INTERLACED)) ||
141
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
210 ctx->is_disabled ||
142
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
210 (yadif->deint && !(yadif->prev->flags & AV_FRAME_FLAG_INTERLACED) && yadif->prev->repeat_pict) ||
143
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
210 (yadif->deint && !(yadif->next->flags & AV_FRAME_FLAG_INTERLACED) && yadif->next->repeat_pict)
144 ) {
145 yadif->out = av_frame_clone(yadif->cur);
146 if (!yadif->out)
147 return AVERROR(ENOMEM);
148
149 ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
150 av_frame_free(&yadif->prev);
151 if (yadif->out->pts != AV_NOPTS_VALUE)
152 yadif->out->pts *= yadif->pts_multiplier;
153 yadif->out->duration *= yadif->pts_multiplier;
154 return ff_filter_frame(ctx->outputs[0], yadif->out);
155 }
156
157 210 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
210 if (!yadif->out)
159 return AVERROR(ENOMEM);
160
161 210 av_frame_copy_props(yadif->out, yadif->cur);
162 210 yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
163
164
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
210 if (yadif->out->pts != AV_NOPTS_VALUE)
165 210 yadif->out->pts *= yadif->pts_multiplier;
166
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 60 times.
210 if (!(yadif->mode & 1))
167 150 yadif->out->duration *= yadif->pts_multiplier;
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 else if (yadif->pts_multiplier == 1)
169 yadif->out->duration >>= 1;
170
171 210 return return_frame(ctx, 0);
172 }
173
174 268 int ff_yadif_request_frame(AVFilterLink *link)
175 {
176 268 AVFilterContext *ctx = link->src;
177 268 YADIFContext *yadif = ctx->priv;
178 int ret;
179
180
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 210 times.
268 if (yadif->frame_pending) {
181 58 return_frame(ctx, 1);
182 58 return 0;
183 }
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
210 if (yadif->eof)
186 return AVERROR_EOF;
187
188 210 ret = ff_request_frame(ctx->inputs[0]);
189
190
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
210 if (ret == AVERROR_EOF && yadif->cur) {
191 AVFrame *next = av_frame_clone(yadif->next);
192
193 if (!next)
194 return AVERROR(ENOMEM);
195
196 yadif->current_field = YADIF_FIELD_BACK_END;
197 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
198
199 ff_yadif_filter_frame(ctx->inputs[0], next);
200 yadif->eof = 1;
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
210 } else if (ret < 0) {
202 return ret;
203 }
204
205 210 return 0;
206 }
207
208 7 int ff_yadif_config_output_common(AVFilterLink *outlink)
209 {
210 7 AVFilterContext *ctx = outlink->src;
211 7 FilterLink *il = ff_filter_link(ctx->inputs[0]);
212 7 FilterLink *ol = ff_filter_link(outlink);
213 7 YADIFContext *yadif = ctx->priv;
214 7 AVRational tb = ctx->inputs[0]->time_base;
215 int ret;
216
217
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 if (av_reduce(&outlink->time_base.num, &outlink->time_base.den, tb.num, tb.den * 2LL, INT_MAX)) {
218 7 yadif->pts_multiplier = 2;
219 } else {
220 av_log(ctx, AV_LOG_WARNING, "Cannot use exact output timebase\n");
221 outlink->time_base = tb;
222 yadif->pts_multiplier = 1;
223 }
224
225 7 outlink->w = ctx->inputs[0]->w;
226 7 outlink->h = ctx->inputs[0]->h;
227
228
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 if (outlink->w < 3 || outlink->h < 3) {
229 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
230 return AVERROR(EINVAL);
231 }
232
233
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if(yadif->mode & 1)
234 2 ol->frame_rate = av_mul_q(il->frame_rate,
235 2 (AVRational){2, 1});
236 else
237 5 ol->frame_rate = il->frame_rate;
238
239 7 ret = ff_ccfifo_init(&yadif->cc_fifo, ol->frame_rate, ctx);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0) {
241 av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
242 return ret;
243 }
244
245 7 return 0;
246 }
247
248 14 void ff_yadif_uninit(AVFilterContext *ctx)
249 {
250 14 YADIFContext *yadif = ctx->priv;
251
252 14 av_frame_free(&yadif->prev);
253 14 av_frame_free(&yadif->cur );
254 14 av_frame_free(&yadif->next);
255 14 ff_ccfifo_uninit(&yadif->cc_fifo);
256 14 }
257
258 #define OFFSET(x) offsetof(YADIFContext, x)
259 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
260
261 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
262
263 const AVOption ff_yadif_options[] = {
264 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, .unit = "mode"},
265 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
266 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
267 CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
268 CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
269
270 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
271 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
272 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
273 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
274
275 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
276 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
277 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
278
279 { NULL }
280 };
281