FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_separatefields.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 72 79 91.1%
Functions: 6 6 100.0%
Branches: 21 32 65.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/pixdesc.h"
22 #include "avfilter.h"
23 #include "filters.h"
24 #include "video.h"
25
26 typedef struct SeparateFieldsContext {
27 int nb_planes;
28 AVFrame *second;
29 } SeparateFieldsContext;
30
31 1 static int config_props_output(AVFilterLink *outlink)
32 {
33 1 AVFilterContext *ctx = outlink->src;
34 1 SeparateFieldsContext *s = ctx->priv;
35 1 AVFilterLink *inlink = ctx->inputs[0];
36 1 FilterLink *il = ff_filter_link(inlink);
37 1 FilterLink *ol = ff_filter_link(outlink);
38
39 1 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
40
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (inlink->h & 1) {
42 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
43 return AVERROR_INVALIDDATA;
44 }
45
46 1 outlink->time_base.num = inlink->time_base.num;
47 1 outlink->time_base.den = inlink->time_base.den * 2;
48 1 ol->frame_rate.num = il->frame_rate.num * 2;
49 1 ol->frame_rate.den = il->frame_rate.den;
50 1 outlink->w = inlink->w;
51 1 outlink->h = inlink->h / 2;
52
53 1 return 0;
54 }
55
56 100 static void extract_field(AVFrame *frame, int nb_planes, int type)
57 {
58 int i;
59
60
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 100 times.
400 for (i = 0; i < nb_planes; i++) {
61
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
300 if (type)
62 150 frame->data[i] = frame->data[i] + frame->linesize[i];
63 300 frame->linesize[i] *= 2;
64 }
65 100 }
66
67 50 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
68 {
69 50 AVFilterContext *ctx = inlink->dst;
70 50 SeparateFieldsContext *s = ctx->priv;
71 50 AVFilterLink *outlink = ctx->outputs[0];
72 int ret;
73
74 50 inpicref->height = outlink->h;
75 50 inpicref->flags &= ~AV_FRAME_FLAG_INTERLACED;
76
77
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 49 times.
50 if (!s->second) {
78 1 goto clone;
79 } else {
80 49 AVFrame *second = s->second;
81
82 49 extract_field(second, s->nb_planes, !!(second->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
83
84
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (second->pts != AV_NOPTS_VALUE &&
85
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 inpicref->pts != AV_NOPTS_VALUE)
86 49 second->pts += inpicref->pts;
87 else
88 second->pts = AV_NOPTS_VALUE;
89
90 49 ret = ff_filter_frame(outlink, second);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
92 return ret;
93 49 clone:
94 50 s->second = av_frame_clone(inpicref);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!s->second)
96 return AVERROR(ENOMEM);
97 }
98
99 50 extract_field(inpicref, s->nb_planes, !(inpicref->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
100
101
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (inpicref->pts != AV_NOPTS_VALUE)
102 50 inpicref->pts *= 2;
103
104 50 return ff_filter_frame(outlink, inpicref);
105 }
106
107 1 static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
108 {
109 1 AVFilterContext *ctx = outlink->src;
110 1 SeparateFieldsContext *s = ctx->priv;
111 1 int ret = 0;
112
113
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->second) {
114 1 *out_pts = s->second->pts += pts;
115 1 extract_field(s->second, s->nb_planes, !!(s->second->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
116 1 ret = ff_filter_frame(outlink, s->second);
117 1 s->second = NULL;
118 }
119
120 1 return ret;
121 }
122
123 101 static int activate(AVFilterContext *ctx)
124 {
125 101 AVFilterLink *inlink = ctx->inputs[0];
126 101 AVFilterLink *outlink = ctx->outputs[0];
127 AVFrame *in;
128 int64_t pts;
129 int ret, status;
130
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
132
133 101 ret = ff_inlink_consume_frame(inlink, &in);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0)
135 return ret;
136
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 51 times.
101 if (ret > 0)
137 50 return filter_frame(inlink, in);
138
139
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 50 times.
51 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
140
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (status == AVERROR_EOF) {
141 1 int64_t out_pts = pts;
142
143 1 ret = flush_frame(outlink, pts, &out_pts);
144 1 ff_outlink_set_status(outlink, status, out_pts);
145 1 return ret;
146 }
147 }
148
149
1/2
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
50 FF_FILTER_FORWARD_WANTED(outlink, inlink);
150
151 return FFERROR_NOT_READY;
152 }
153
154 2 static av_cold void uninit(AVFilterContext *ctx)
155 {
156 2 SeparateFieldsContext *s = ctx->priv;
157
158 2 av_frame_free(&s->second);
159 2 }
160
161 static const AVFilterPad separatefields_outputs[] = {
162 {
163 .name = "default",
164 .type = AVMEDIA_TYPE_VIDEO,
165 .config_props = config_props_output,
166 },
167 };
168
169 const FFFilter ff_vf_separatefields = {
170 .p.name = "separatefields",
171 .p.description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
172 .priv_size = sizeof(SeparateFieldsContext),
173 .activate = activate,
174 .uninit = uninit,
175 FILTER_INPUTS(ff_video_default_filterpad),
176 FILTER_OUTPUTS(separatefields_outputs),
177 };
178