FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_weave.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 68 78 87.2%
Branches: 14 28 50.0%

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/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "internal.h"
26
27 typedef struct WeaveContext {
28 const AVClass *class;
29 int first_field;
30 int double_weave;
31 int nb_planes;
32 int planeheight[4];
33 int linesize[4];
34
35 AVFrame *prev;
36 } WeaveContext;
37
38 #define OFFSET(x) offsetof(WeaveContext, x)
39 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
40
41 static const AVOption weave_options[] = {
42 { "first_field", "set first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
43 { "top", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
44 { "t", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
45 { "bottom", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
46 { "b", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
47 { NULL }
48 };
49
50 AVFILTER_DEFINE_CLASS_EXT(weave, "(double)weave", weave_options);
51
52 1 static int query_formats(AVFilterContext *ctx)
53 {
54 1 int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL;
55
56 1 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
57 }
58
59 1 static int config_props_output(AVFilterLink *outlink)
60 {
61 1 AVFilterContext *ctx = outlink->src;
62 1 WeaveContext *s = ctx->priv;
63 1 AVFilterLink *inlink = ctx->inputs[0];
64 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
65 int ret;
66
67
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!s->double_weave) {
68 1 outlink->time_base.num = inlink->time_base.num * 2;
69 1 outlink->time_base.den = inlink->time_base.den;
70 1 outlink->frame_rate.num = inlink->frame_rate.num;
71 1 outlink->frame_rate.den = inlink->frame_rate.den * 2;
72 }
73 1 outlink->w = inlink->w;
74 1 outlink->h = inlink->h * 2;
75
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
77 return ret;
78
79 1 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
80 1 s->planeheight[0] = s->planeheight[3] = inlink->h;
81
82 1 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
83
84 1 return 0;
85 }
86
87 typedef struct ThreadData {
88 AVFrame *in, *out;
89 } ThreadData;
90
91 225 static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
92 {
93 225 AVFilterLink *inlink = ctx->inputs[0];
94 225 WeaveContext *s = ctx->priv;
95 225 ThreadData *td = arg;
96 225 AVFrame *in = td->in;
97 225 AVFrame *out = td->out;
98
99
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
225 const int weave = (s->double_weave && !(inlink->frame_count_out & 1));
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 const int field1 = weave ? s->first_field : (!s->first_field);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 const int field2 = weave ? (!s->first_field) : s->first_field;
102
103
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 225 times.
900 for (int i = 0; i < s->nb_planes; i++) {
104 675 const int height = s->planeheight[i];
105 675 const int start = (height * jobnr) / nb_jobs;
106 675 const int end = (height * (jobnr+1)) / nb_jobs;
107
108 675 av_image_copy_plane(out->data[i] + out->linesize[i] * field1 +
109 675 out->linesize[i] * start * 2,
110 675 out->linesize[i] * 2,
111 675 in->data[i] + start * in->linesize[i],
112 in->linesize[i],
113 s->linesize[i], end - start);
114 675 av_image_copy_plane(out->data[i] + out->linesize[i] * field2 +
115 675 out->linesize[i] * start * 2,
116 675 out->linesize[i] * 2,
117 675 s->prev->data[i] + start * s->prev->linesize[i],
118 675 s->prev->linesize[i],
119 s->linesize[i], end - start);
120 }
121
122 225 return 0;
123 }
124
125 50 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
126 {
127 50 AVFilterContext *ctx = inlink->dst;
128 50 WeaveContext *s = ctx->priv;
129 50 AVFilterLink *outlink = ctx->outputs[0];
130 ThreadData td;
131 AVFrame *out;
132
133
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 if (!s->prev) {
134 25 s->prev = in;
135 25 return 0;
136 }
137
138 25 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!out) {
140 av_frame_free(&in);
141 av_frame_free(&s->prev);
142 return AVERROR(ENOMEM);
143 }
144 25 av_frame_copy_props(out, in);
145
146 25 td.out = out, td.in = in;
147 25 ff_filter_execute(ctx, weave_slice, &td, NULL,
148
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
149
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
151 25 out->interlaced_frame = 1;
152 25 out->top_field_first = !s->first_field;
153
154
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (!s->double_weave)
155 25 av_frame_free(&in);
156 25 av_frame_free(&s->prev);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (s->double_weave)
158 s->prev = in;
159 25 return ff_filter_frame(outlink, out);
160 }
161
162 1 static av_cold void uninit(AVFilterContext *ctx)
163 {
164 1 WeaveContext *s = ctx->priv;
165
166 1 av_frame_free(&s->prev);
167 1 }
168
169 static const AVFilterPad weave_inputs[] = {
170 {
171 .name = "default",
172 .type = AVMEDIA_TYPE_VIDEO,
173 .filter_frame = filter_frame,
174 },
175 };
176
177 static const AVFilterPad weave_outputs[] = {
178 {
179 .name = "default",
180 .type = AVMEDIA_TYPE_VIDEO,
181 .config_props = config_props_output,
182 },
183 };
184
185 const AVFilter ff_vf_weave = {
186 .name = "weave",
187 .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
188 .priv_size = sizeof(WeaveContext),
189 .priv_class = &weave_class,
190 .uninit = uninit,
191 FILTER_INPUTS(weave_inputs),
192 FILTER_OUTPUTS(weave_outputs),
193 FILTER_QUERY_FUNC(query_formats),
194 .flags = AVFILTER_FLAG_SLICE_THREADS,
195 };
196
197 static av_cold int init(AVFilterContext *ctx)
198 {
199 WeaveContext *s = ctx->priv;
200
201 if (!strcmp(ctx->filter->name, "doubleweave"))
202 s->double_weave = 1;
203
204 return 0;
205 }
206
207 const AVFilter ff_vf_doubleweave = {
208 .name = "doubleweave",
209 .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
210 .priv_class = &weave_class,
211 .priv_size = sizeof(WeaveContext),
212 .init = init,
213 .uninit = uninit,
214 FILTER_INPUTS(weave_inputs),
215 FILTER_OUTPUTS(weave_outputs),
216 FILTER_QUERY_FUNC(query_formats),
217 .flags = AVFILTER_FLAG_SLICE_THREADS,
218 };
219