FFmpeg coverage


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