FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_fieldhint.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 0 157 0.0%
Functions: 0 6 0.0%
Branches: 0 72 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2016 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/avassert.h"
22 #include "libavutil/file_open.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 enum HintModes {
33 ABSOLUTE_HINT,
34 RELATIVE_HINT,
35 PATTERN_HINT,
36 NB_HINTS,
37 };
38
39 typedef struct FieldHintContext {
40 const AVClass *class;
41
42 char *hint_file_str;
43 FILE *hint;
44 int mode;
45
46 AVFrame *frame[3];
47
48 int64_t line;
49 int nb_planes;
50 int eof;
51 int planewidth[4];
52 int planeheight[4];
53 } FieldHintContext;
54
55 #define OFFSET(x) offsetof(FieldHintContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57
58 static const AVOption fieldhint_options[] = {
59 { "hint", "set hint file", OFFSET(hint_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
60 { "mode", "set hint mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_HINTS-1, FLAGS, .unit = "mode" },
61 { "absolute", 0, 0, AV_OPT_TYPE_CONST, {.i64=ABSOLUTE_HINT}, 0, 0, FLAGS, .unit = "mode" },
62 { "relative", 0, 0, AV_OPT_TYPE_CONST, {.i64=RELATIVE_HINT}, 0, 0, FLAGS, .unit = "mode" },
63 { "pattern", 0, 0, AV_OPT_TYPE_CONST, {.i64=PATTERN_HINT}, 0, 0, FLAGS, .unit = "mode" },
64 { NULL }
65 };
66
67 AVFILTER_DEFINE_CLASS(fieldhint);
68
69 static av_cold int init(AVFilterContext *ctx)
70 {
71 FieldHintContext *s = ctx->priv;
72 int ret;
73
74 if (!s->hint_file_str) {
75 av_log(ctx, AV_LOG_ERROR, "Hint file must be set.\n");
76 return AVERROR(EINVAL);
77 }
78 s->hint = avpriv_fopen_utf8(s->hint_file_str, "r");
79 if (!s->hint) {
80 ret = AVERROR(errno);
81 av_log(ctx, AV_LOG_ERROR, "%s: %s\n", s->hint_file_str, av_err2str(ret));
82 return ret;
83 }
84
85 return 0;
86 }
87
88 static int query_formats(AVFilterContext *ctx)
89 {
90 int reject_flags = AV_PIX_FMT_FLAG_BITSTREAM |
91 AV_PIX_FMT_FLAG_HWACCEL |
92 AV_PIX_FMT_FLAG_PAL;
93
94 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
95 }
96
97 static int config_input(AVFilterLink *inlink)
98 {
99 FieldHintContext *s = inlink->dst->priv;
100 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
101 int ret;
102
103 if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
104 return ret;
105
106 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
107 s->planeheight[0] = s->planeheight[3] = inlink->h;
108
109 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
110
111 return 0;
112 }
113
114 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
115 {
116 AVFilterContext *ctx = inlink->dst;
117 AVFilterLink *outlink = ctx->outputs[0];
118 FieldHintContext *s = ctx->priv;
119 AVFrame *out, *top, *bottom;
120 char buf[1024] = { 0 };
121 int64_t tf, bf;
122 int tfactor = 0, bfactor = 1;
123 char hint = '=', field = '=';
124 int p;
125
126 av_frame_free(&s->frame[0]);
127 s->frame[0] = s->frame[1];
128 s->frame[1] = s->frame[2];
129 s->frame[2] = in;
130 if (!s->frame[1])
131 return 0;
132 else if (!s->frame[0]) {
133 s->frame[0] = av_frame_clone(s->frame[1]);
134 if (!s->frame[0])
135 return AVERROR(ENOMEM);
136 }
137
138 while (1) {
139 if (fgets(buf, sizeof(buf)-1, s->hint)) {
140 s->line++;
141 if (buf[0] == '#' || buf[0] == ';') {
142 continue;
143 } else if (sscanf(buf, "%"PRId64",%"PRId64" %c %c", &tf, &bf, &hint, &field) == 4) {
144 ;
145 } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
146 ;
147 } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
148 ;
149 } else {
150 av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
151 return AVERROR_INVALIDDATA;
152 }
153 switch (s->mode) {
154 case ABSOLUTE_HINT:
155 if (tf > outlink->frame_count_in + 1 || tf < FFMAX(0, outlink->frame_count_in - 1) ||
156 bf > outlink->frame_count_in + 1 || bf < FFMAX(0, outlink->frame_count_in - 1)) {
157 av_log(ctx, AV_LOG_ERROR, "Out of range frames %"PRId64" and/or %"PRId64" on line %"PRId64" for %"PRId64". input frame.\n", tf, bf, s->line, inlink->frame_count_out);
158 return AVERROR_INVALIDDATA;
159 }
160 break;
161 case PATTERN_HINT:
162 case RELATIVE_HINT:
163 if (tf > 1 || tf < -1 ||
164 bf > 1 || bf < -1) {
165 av_log(ctx, AV_LOG_ERROR, "Out of range %"PRId64" and/or %"PRId64" on line %"PRId64" for %"PRId64". input frame.\n", tf, bf, s->line, inlink->frame_count_out);
166 return AVERROR_INVALIDDATA;
167 }
168 break;
169 default:
170 return AVERROR_BUG;
171 };
172 break;
173 } else {
174 if (s->mode == PATTERN_HINT) {
175 fseek(s->hint, 0, SEEK_SET);
176 continue;
177 }
178 av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inlink->frame_count_out);
179 return AVERROR_INVALIDDATA;
180 }
181 }
182
183 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
184 if (!out)
185 return AVERROR(ENOMEM);
186 av_frame_copy_props(out, s->frame[1]);
187
188 switch (s->mode) {
189 case ABSOLUTE_HINT:
190 top = s->frame[tf - outlink->frame_count_in + 1];
191 bottom = s->frame[bf - outlink->frame_count_in + 1];
192 break;
193 case PATTERN_HINT:
194 case RELATIVE_HINT:
195 top = s->frame[1 + tf];
196 bottom = s->frame[1 + bf];
197 break;
198 default:
199 av_assert0(0);
200 }
201
202 switch (field) {
203 case 'b':
204 tfactor = 1;
205 top = bottom;
206 break;
207 case 't':
208 bfactor = 0;
209 bottom = top;
210 break;
211 case '=':
212 break;
213 default:
214 av_log(ctx, AV_LOG_ERROR, "Invalid field: %c.\n", field);
215 av_frame_free(&out);
216 return AVERROR(EINVAL);
217 }
218
219 switch (hint) {
220 case '+':
221 #if FF_API_INTERLACED_FRAME
222 FF_DISABLE_DEPRECATION_WARNINGS
223 out->interlaced_frame = 1;
224 FF_ENABLE_DEPRECATION_WARNINGS
225 #endif
226 out->flags |= AV_FRAME_FLAG_INTERLACED;
227 break;
228 case '-':
229 #if FF_API_INTERLACED_FRAME
230 FF_DISABLE_DEPRECATION_WARNINGS
231 out->interlaced_frame = 0;
232 FF_ENABLE_DEPRECATION_WARNINGS
233 #endif
234 out->flags &= ~AV_FRAME_FLAG_INTERLACED;
235 break;
236 case '=':
237 break;
238 case 'b':
239 tfactor = 1;
240 top = bottom;
241 break;
242 case 't':
243 bfactor = 0;
244 bottom = top;
245 break;
246 default:
247 av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
248 av_frame_free(&out);
249 return AVERROR(EINVAL);
250 }
251
252 for (p = 0; p < s->nb_planes; p++) {
253 av_image_copy_plane(out->data[p],
254 out->linesize[p] * 2,
255 top->data[p] + tfactor * top->linesize[p],
256 top->linesize[p] * 2,
257 s->planewidth[p],
258 (s->planeheight[p] + 1) / 2);
259 av_image_copy_plane(out->data[p] + out->linesize[p],
260 out->linesize[p] * 2,
261 bottom->data[p] + bfactor * bottom->linesize[p],
262 bottom->linesize[p] * 2,
263 s->planewidth[p],
264 (s->planeheight[p] + 1) / 2);
265 }
266
267 return ff_filter_frame(outlink, out);
268 }
269
270 static int request_frame(AVFilterLink *outlink)
271 {
272 AVFilterContext *ctx = outlink->src;
273 FieldHintContext *s = ctx->priv;
274 int ret;
275
276 if (s->eof)
277 return AVERROR_EOF;
278
279 ret = ff_request_frame(ctx->inputs[0]);
280 if (ret == AVERROR_EOF && s->frame[2]) {
281 AVFrame *next = av_frame_clone(s->frame[2]);
282 if (!next)
283 return AVERROR(ENOMEM);
284 ret = filter_frame(ctx->inputs[0], next);
285 s->eof = 1;
286 }
287
288 return ret;
289 }
290
291 static av_cold void uninit(AVFilterContext *ctx)
292 {
293 FieldHintContext *s = ctx->priv;
294
295 if (s->hint)
296 fclose(s->hint);
297 s->hint = NULL;
298
299 av_frame_free(&s->frame[0]);
300 av_frame_free(&s->frame[1]);
301 av_frame_free(&s->frame[2]);
302 }
303
304 static const AVFilterPad inputs[] = {
305 {
306 .name = "default",
307 .type = AVMEDIA_TYPE_VIDEO,
308 .config_props = config_input,
309 .filter_frame = filter_frame,
310 },
311 };
312
313 static const AVFilterPad outputs[] = {
314 {
315 .name = "default",
316 .type = AVMEDIA_TYPE_VIDEO,
317 .request_frame = request_frame,
318 },
319 };
320
321 const AVFilter ff_vf_fieldhint = {
322 .name = "fieldhint",
323 .description = NULL_IF_CONFIG_SMALL("Field matching using hints."),
324 .priv_size = sizeof(FieldHintContext),
325 .priv_class = &fieldhint_class,
326 .init = init,
327 .uninit = uninit,
328 FILTER_INPUTS(inputs),
329 FILTER_OUTPUTS(outputs),
330 FILTER_QUERY_FUNC(query_formats),
331 };
332