FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/yadif_common.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 77 113 68.1%
Functions: 4 5 80.0%
Branches: 50 92 54.3%

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 "internal.h"
25 #include "video.h"
26 #include "yadif.h"
27
28 277 static int return_frame(AVFilterContext *ctx, int is_second)
29 {
30 277 YADIFContext *yadif = ctx->priv;
31 277 AVFilterLink *link = ctx->outputs[0];
32 int tff, ret;
33
34
1/2
✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
277 if (yadif->parity == -1) {
35 277 tff = (yadif->cur->flags & AV_FRAME_FLAG_INTERLACED) ?
36
2/4
✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 277 times.
277 !!(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 217 times.
277 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 #if FF_API_INTERLACED_FRAME
48 FF_DISABLE_DEPRECATION_WARNINGS
49 60 yadif->out->interlaced_frame = 0;
50 FF_ENABLE_DEPRECATION_WARNINGS
51 #endif
52 60 yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (yadif->current_field == YADIF_FIELD_BACK_END)
54 yadif->current_field = YADIF_FIELD_END;
55 }
56
57 277 yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
58
59
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 217 times.
277 if (is_second) {
60 60 int64_t cur_pts = yadif->cur->pts;
61 60 int64_t next_pts = yadif->next->pts;
62
63
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) {
64 60 yadif->out->pts = cur_pts + next_pts;
65 } else {
66 yadif->out->pts = AV_NOPTS_VALUE;
67 }
68 }
69
70 277 ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
71 277 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
72
73
4/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 155 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 60 times.
277 yadif->frame_pending = (yadif->mode&1) && !is_second;
74 277 return ret;
75 }
76
77 1106 static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
78 {
79 int i;
80
2/2
✓ Branch 0 taken 3318 times.
✓ Branch 1 taken 1106 times.
4424 for (i = 0; i < yadif->csp->nb_components; i++)
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3318 times.
3318 if (a->linesize[i] != b->linesize[i])
82 return 1;
83 1106 return 0;
84 }
85
86 static void fixstride(AVFilterLink *link, AVFrame *f)
87 {
88 AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
89 if(!dst)
90 return;
91 av_frame_copy_props(dst, f);
92 av_image_copy2(dst->data, dst->linesize,
93 f->data, f->linesize,
94 dst->format, dst->width, dst->height);
95 av_frame_unref(f);
96 av_frame_move_ref(f, dst);
97 av_frame_free(&dst);
98 }
99
100 224 int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
101 {
102 224 AVFilterContext *ctx = link->dst;
103 224 YADIFContext *yadif = ctx->priv;
104
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 av_assert0(frame);
106
107 224 ff_ccfifo_extract(&yadif->cc_fifo, frame);
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 if (yadif->frame_pending)
110 return_frame(ctx, 1);
111
112
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 14 times.
224 if (yadif->prev)
113 210 av_frame_free(&yadif->prev);
114 224 yadif->prev = yadif->cur;
115 224 yadif->cur = yadif->next;
116 224 yadif->next = frame;
117
118
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 217 times.
224 if (!yadif->cur) {
119 7 yadif->cur = av_frame_clone(yadif->next);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!yadif->cur)
121 return AVERROR(ENOMEM);
122 7 yadif->current_field = YADIF_FIELD_END;
123 }
124
125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
224 if (checkstride(yadif, yadif->next, yadif->cur)) {
126 av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
127 fixstride(link, yadif->next);
128 }
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
224 if (checkstride(yadif, yadif->next, yadif->cur))
130 fixstride(link, yadif->cur);
131
3/4
✓ Branch 0 taken 217 times.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 217 times.
224 if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
132 fixstride(link, yadif->prev);
133
4/6
✓ Branch 1 taken 224 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 217 times.
✓ Branch 4 taken 7 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 217 times.
224 if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
134 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
135 return -1;
136 }
137
138
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 217 times.
224 if (!yadif->prev)
139 7 return 0;
140
141
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
217 if ((yadif->deint && !(yadif->cur->flags & AV_FRAME_FLAG_INTERLACED)) ||
142
1/2
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
217 ctx->is_disabled ||
143
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
217 (yadif->deint && !(yadif->prev->flags & AV_FRAME_FLAG_INTERLACED) && yadif->prev->repeat_pict) ||
144
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
217 (yadif->deint && !(yadif->next->flags & AV_FRAME_FLAG_INTERLACED) && yadif->next->repeat_pict)
145 ) {
146 yadif->out = av_frame_clone(yadif->cur);
147 if (!yadif->out)
148 return AVERROR(ENOMEM);
149
150 ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
151 av_frame_free(&yadif->prev);
152 if (yadif->out->pts != AV_NOPTS_VALUE)
153 yadif->out->pts *= 2;
154 return ff_filter_frame(ctx->outputs[0], yadif->out);
155 }
156
157 217 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 if (!yadif->out)
159 return AVERROR(ENOMEM);
160
161 217 av_frame_copy_props(yadif->out, yadif->cur);
162 #if FF_API_INTERLACED_FRAME
163 FF_DISABLE_DEPRECATION_WARNINGS
164 217 yadif->out->interlaced_frame = 0;
165 FF_ENABLE_DEPRECATION_WARNINGS
166 #endif
167 217 yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
168
169
1/2
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
217 if (yadif->out->pts != AV_NOPTS_VALUE)
170 217 yadif->out->pts *= 2;
171
172 217 return return_frame(ctx, 0);
173 }
174
175 277 int ff_yadif_request_frame(AVFilterLink *link)
176 {
177 277 AVFilterContext *ctx = link->src;
178 277 YADIFContext *yadif = ctx->priv;
179 int ret;
180
181
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 217 times.
277 if (yadif->frame_pending) {
182 60 return_frame(ctx, 1);
183 60 return 0;
184 }
185
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 if (yadif->eof)
187 return AVERROR_EOF;
188
189 217 ret = ff_request_frame(ctx->inputs[0]);
190
191
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
217 if (ret == AVERROR_EOF && yadif->cur) {
192 7 AVFrame *next = av_frame_clone(yadif->next);
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!next)
195 return AVERROR(ENOMEM);
196
197 7 yadif->current_field = YADIF_FIELD_BACK_END;
198 7 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
199
200 7 ff_yadif_filter_frame(ctx->inputs[0], next);
201 7 yadif->eof = 1;
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
210 } else if (ret < 0) {
203 return ret;
204 }
205
206 217 return 0;
207 }
208
209 #define OFFSET(x) offsetof(YADIFContext, x)
210 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
211
212 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
213
214 const AVOption ff_yadif_options[] = {
215 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
216 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
217 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
218 CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
219 CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
220
221 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
222 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
223 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
224 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
225
226 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
227 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
228 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
229
230 { NULL }
231 };
232