FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/yadif_common.c
Date: 2022-12-06 04:36:11
Exec Total Coverage
Lines: 73 108 67.6%
Functions: 4 5 80.0%
Branches: 49 90 54.4%

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