2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
30 typedef struct FieldHintContext {
46 #define OFFSET(x) offsetof(FieldHintContext, x)
47 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 static const AVOption fieldhint_options[] = {
50 { "hint", "set hint file", OFFSET(hint_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
51 { "mode", "set hint mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
52 { "absolute", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
53 { "relative", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
57 AVFILTER_DEFINE_CLASS(fieldhint);
59 static av_cold int init(AVFilterContext *ctx)
61 FieldHintContext *s = ctx->priv;
64 if (!s->hint_file_str) {
65 av_log(ctx, AV_LOG_ERROR, "Hint file must be set.\n");
66 return AVERROR(EINVAL);
68 s->hint = av_fopen_utf8(s->hint_file_str, "r");
71 av_log(ctx, AV_LOG_ERROR, "%s: %s\n", s->hint_file_str, av_err2str(ret));
78 static int query_formats(AVFilterContext *ctx)
80 AVFilterFormats *formats = NULL;
83 ret = ff_formats_pixdesc_filter(&formats, 0,
84 AV_PIX_FMT_FLAG_HWACCEL |
85 AV_PIX_FMT_FLAG_BITSTREAM |
89 return ff_set_common_formats(ctx, formats);
92 static int config_input(AVFilterLink *inlink)
94 FieldHintContext *s = inlink->dst->priv;
95 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
98 if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
101 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
102 s->planeheight[0] = s->planeheight[3] = inlink->h;
104 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
109 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
111 AVFilterContext *ctx = inlink->dst;
112 AVFilterLink *outlink = ctx->outputs[0];
113 FieldHintContext *s = ctx->priv;
114 AVFrame *out, *top, *bottom;
115 char buf[1024] = { 0 };
117 int tfactor = 0, bfactor = 1;
118 char hint = '=', field = '=';
121 av_frame_free(&s->frame[0]);
122 s->frame[0] = s->frame[1];
123 s->frame[1] = s->frame[2];
127 else if (!s->frame[0]) {
128 s->frame[0] = av_frame_clone(s->frame[1]);
130 return AVERROR(ENOMEM);
134 if (fgets(buf, sizeof(buf)-1, s->hint)) {
136 if (buf[0] == '#' || buf[0] == ';') {
138 } else if (sscanf(buf, "%"PRId64",%"PRId64" %c %c", &tf, &bf, &hint, &field) == 4) {
140 } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
142 } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
145 av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
146 return AVERROR_INVALIDDATA;
150 if (tf > outlink->frame_count_in + 1 || tf < FFMAX(0, outlink->frame_count_in - 1) ||
151 bf > outlink->frame_count_in + 1 || bf < FFMAX(0, outlink->frame_count_in - 1)) {
152 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);
153 return AVERROR_INVALIDDATA;
157 if (tf > 1 || tf < -1 ||
159 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);
160 return AVERROR_INVALIDDATA;
165 av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inlink->frame_count_out);
166 return AVERROR_INVALIDDATA;
170 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
172 return AVERROR(ENOMEM);
173 av_frame_copy_props(out, s->frame[1]);
177 top = s->frame[tf - outlink->frame_count_in + 1];
178 bottom = s->frame[bf - outlink->frame_count_in + 1];
181 top = s->frame[1 + tf];
182 bottom = s->frame[1 + bf];
200 av_log(ctx, AV_LOG_ERROR, "Invalid field: %c.\n", field);
202 return AVERROR(EINVAL);
207 out->interlaced_frame = 1;
210 out->interlaced_frame = 0;
223 av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
225 return AVERROR(EINVAL);
228 for (p = 0; p < s->nb_planes; p++) {
229 av_image_copy_plane(out->data[p],
230 out->linesize[p] * 2,
231 top->data[p] + tfactor * top->linesize[p],
232 top->linesize[p] * 2,
234 (s->planeheight[p] + 1) / 2);
235 av_image_copy_plane(out->data[p] + out->linesize[p],
236 out->linesize[p] * 2,
237 bottom->data[p] + bfactor * bottom->linesize[p],
238 bottom->linesize[p] * 2,
240 (s->planeheight[p] + 1) / 2);
243 return ff_filter_frame(outlink, out);
246 static int request_frame(AVFilterLink *outlink)
248 AVFilterContext *ctx = outlink->src;
249 FieldHintContext *s = ctx->priv;
255 ret = ff_request_frame(ctx->inputs[0]);
256 if (ret == AVERROR_EOF && s->frame[2]) {
257 AVFrame *next = av_frame_clone(s->frame[2]);
259 return AVERROR(ENOMEM);
260 ret = filter_frame(ctx->inputs[0], next);
267 static av_cold void uninit(AVFilterContext *ctx)
269 FieldHintContext *s = ctx->priv;
275 av_frame_free(&s->frame[0]);
276 av_frame_free(&s->frame[1]);
277 av_frame_free(&s->frame[2]);
280 static const AVFilterPad inputs[] = {
283 .type = AVMEDIA_TYPE_VIDEO,
284 .config_props = config_input,
285 .filter_frame = filter_frame,
290 static const AVFilterPad outputs[] = {
293 .type = AVMEDIA_TYPE_VIDEO,
294 .request_frame = request_frame,
299 AVFilter ff_vf_fieldhint = {
301 .description = NULL_IF_CONFIG_SMALL("Field matching using hints."),
302 .priv_size = sizeof(FieldHintContext),
303 .priv_class = &fieldhint_class,
306 .query_formats = query_formats,