FFmpeg
vf_fieldhint.c
Go to the documentation of this file.
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 "filters.h"
29 #include "formats.h"
30 #include "video.h"
31 
32 enum HintModes {
37 };
38 
39 typedef struct FieldHintContext {
40  const AVClass *class;
41 
43  FILE *hint;
44  int mode;
45 
47 
49  int nb_planes;
50  int eof;
51  int planewidth[4];
52  int planeheight[4];
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 
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 
89 {
90  int reject_flags = AV_PIX_FMT_FLAG_BITSTREAM |
93 
94  return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
95 }
96 
98 {
99  FieldHintContext *s = inlink->dst->priv;
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 
115 {
117  AVFilterContext *ctx = inlink->dst;
118  AVFilterLink *outlink = ctx->outputs[0];
119  FilterLink *outl = ff_filter_link(outlink);
120  FieldHintContext *s = ctx->priv;
121  AVFrame *out, *top, *bottom;
122  char buf[1024] = { 0 };
123  int64_t tf, bf;
124  int tfactor = 0, bfactor = 1;
125  char hint = '=', field = '=';
126  int p;
127 
128  av_frame_free(&s->frame[0]);
129  s->frame[0] = s->frame[1];
130  s->frame[1] = s->frame[2];
131  s->frame[2] = in;
132  if (!s->frame[1])
133  return 0;
134  else if (!s->frame[0]) {
135  s->frame[0] = av_frame_clone(s->frame[1]);
136  if (!s->frame[0])
137  return AVERROR(ENOMEM);
138  }
139 
140  while (1) {
141  if (fgets(buf, sizeof(buf)-1, s->hint)) {
142  s->line++;
143  if (buf[0] == '#' || buf[0] == ';') {
144  continue;
145  } else if (sscanf(buf, "%"PRId64",%"PRId64" %c %c", &tf, &bf, &hint, &field) == 4) {
146  ;
147  } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
148  ;
149  } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
150  ;
151  } else {
152  av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
153  return AVERROR_INVALIDDATA;
154  }
155  switch (s->mode) {
156  case ABSOLUTE_HINT:
157  if (tf > outl->frame_count_in + 1 || tf < FFMAX(0, outl->frame_count_in - 1) ||
158  bf > outl->frame_count_in + 1 || bf < FFMAX(0, outl->frame_count_in - 1)) {
159  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, inl->frame_count_out);
160  return AVERROR_INVALIDDATA;
161  }
162  break;
163  case PATTERN_HINT:
164  case RELATIVE_HINT:
165  if (tf > 1 || tf < -1 ||
166  bf > 1 || bf < -1) {
167  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, inl->frame_count_out);
168  return AVERROR_INVALIDDATA;
169  }
170  break;
171  default:
172  return AVERROR_BUG;
173  };
174  break;
175  } else {
176  if (s->mode == PATTERN_HINT) {
177  fseek(s->hint, 0, SEEK_SET);
178  continue;
179  }
180  av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inl->frame_count_out);
181  return AVERROR_INVALIDDATA;
182  }
183  }
184 
185  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
186  if (!out)
187  return AVERROR(ENOMEM);
188  av_frame_copy_props(out, s->frame[1]);
189 
190  switch (s->mode) {
191  case ABSOLUTE_HINT:
192  top = s->frame[tf - outl->frame_count_in + 1];
193  bottom = s->frame[bf - outl->frame_count_in + 1];
194  break;
195  case PATTERN_HINT:
196  case RELATIVE_HINT:
197  top = s->frame[1 + tf];
198  bottom = s->frame[1 + bf];
199  break;
200  default:
201  av_assert0(0);
202  }
203 
204  switch (field) {
205  case 'b':
206  tfactor = 1;
207  top = bottom;
208  break;
209  case 't':
210  bfactor = 0;
211  bottom = top;
212  break;
213  case '=':
214  break;
215  default:
216  av_log(ctx, AV_LOG_ERROR, "Invalid field: %c.\n", field);
217  av_frame_free(&out);
218  return AVERROR(EINVAL);
219  }
220 
221  switch (hint) {
222  case '+':
223 #if FF_API_INTERLACED_FRAME
225  out->interlaced_frame = 1;
227 #endif
228  out->flags |= AV_FRAME_FLAG_INTERLACED;
229  break;
230  case '-':
231 #if FF_API_INTERLACED_FRAME
233  out->interlaced_frame = 0;
235 #endif
236  out->flags &= ~AV_FRAME_FLAG_INTERLACED;
237  break;
238  case '=':
239  break;
240  case 'b':
241  tfactor = 1;
242  top = bottom;
243  break;
244  case 't':
245  bfactor = 0;
246  bottom = top;
247  break;
248  default:
249  av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
250  av_frame_free(&out);
251  return AVERROR(EINVAL);
252  }
253 
254  for (p = 0; p < s->nb_planes; p++) {
255  av_image_copy_plane(out->data[p],
256  out->linesize[p] * 2,
257  top->data[p] + tfactor * top->linesize[p],
258  top->linesize[p] * 2,
259  s->planewidth[p],
260  (s->planeheight[p] + 1) / 2);
261  av_image_copy_plane(out->data[p] + out->linesize[p],
262  out->linesize[p] * 2,
263  bottom->data[p] + bfactor * bottom->linesize[p],
264  bottom->linesize[p] * 2,
265  s->planewidth[p],
266  (s->planeheight[p] + 1) / 2);
267  }
268 
269  return ff_filter_frame(outlink, out);
270 }
271 
272 static int request_frame(AVFilterLink *outlink)
273 {
274  AVFilterContext *ctx = outlink->src;
275  FieldHintContext *s = ctx->priv;
276  int ret;
277 
278  if (s->eof)
279  return AVERROR_EOF;
280 
281  ret = ff_request_frame(ctx->inputs[0]);
282  if (ret == AVERROR_EOF && s->frame[2]) {
283  AVFrame *next = av_frame_clone(s->frame[2]);
284  if (!next)
285  return AVERROR(ENOMEM);
286  ret = filter_frame(ctx->inputs[0], next);
287  s->eof = 1;
288  }
289 
290  return ret;
291 }
292 
294 {
295  FieldHintContext *s = ctx->priv;
296 
297  if (s->hint)
298  fclose(s->hint);
299  s->hint = NULL;
300 
301  av_frame_free(&s->frame[0]);
302  av_frame_free(&s->frame[1]);
303  av_frame_free(&s->frame[2]);
304 }
305 
306 static const AVFilterPad inputs[] = {
307  {
308  .name = "default",
309  .type = AVMEDIA_TYPE_VIDEO,
310  .config_props = config_input,
311  .filter_frame = filter_frame,
312  },
313 };
314 
315 static const AVFilterPad outputs[] = {
316  {
317  .name = "default",
318  .type = AVMEDIA_TYPE_VIDEO,
319  .request_frame = request_frame,
320  },
321 };
322 
324  .name = "fieldhint",
325  .description = NULL_IF_CONFIG_SMALL("Field matching using hints."),
326  .priv_size = sizeof(FieldHintContext),
327  .priv_class = &fieldhint_class,
328  .init = init,
329  .uninit = uninit,
333 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
FieldHintContext
Definition: vf_fieldhint.c:39
inputs
static const AVFilterPad inputs[]
Definition: vf_fieldhint.c:306
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1023
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FieldHintContext::hint
FILE * hint
Definition: vf_fieldhint.c:43
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:161
FieldHintContext::nb_planes
int nb_planes
Definition: vf_fieldhint.c:49
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:380
pixdesc.h
FLAGS
#define FLAGS
Definition: vf_fieldhint.c:56
ff_vf_fieldhint
const AVFilter ff_vf_fieldhint
Definition: vf_fieldhint.c:323
AVOption
AVOption.
Definition: opt.h:429
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:472
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_fieldhint.c:69
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:401
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
OFFSET
#define OFFSET(x)
Definition: vf_fieldhint.c:55
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3005
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
FieldHintContext::planeheight
int planeheight[4]
Definition: vf_fieldhint.c:52
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: vf_fieldhint.c:272
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:867
av_image_fill_linesizes
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
bf
#define bf(fn, bd, opt)
Definition: vvcdsp_init.c:66
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldhint.c:88
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:596
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
file_open.h
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
FieldHintContext::mode
int mode
Definition: vf_fieldhint.c:44
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:712
FieldHintContext::hint_file_str
char * hint_file_str
Definition: vf_fieldhint.c:42
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(fieldhint)
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
ABSOLUTE_HINT
@ ABSOLUTE_HINT
Definition: vf_fieldhint.c:33
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
FieldHintContext::frame
AVFrame * frame[3]
Definition: vf_fieldhint.c:46
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
AV_PIX_FMT_FLAG_BITSTREAM
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:124
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:553
internal.h
fieldhint_options
static const AVOption fieldhint_options[]
Definition: vf_fieldhint.c:58
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
avpriv_fopen_utf8
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:161
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:639
FieldHintContext::planewidth
int planewidth[4]
Definition: vf_fieldhint.c:51
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_fieldhint.c:97
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
PATTERN_HINT
@ PATTERN_HINT
Definition: vf_fieldhint.c:35
NB_HINTS
@ NB_HINTS
Definition: vf_fieldhint.c:36
mode
mode
Definition: ebur128.h:83
FieldHintContext::line
int64_t line
Definition: vf_fieldhint.c:48
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
outputs
static const AVFilterPad outputs[]
Definition: vf_fieldhint.c:315
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
RELATIVE_HINT
@ RELATIVE_HINT
Definition: vf_fieldhint.c:34
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_fieldhint.c:114
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_fieldhint.c:293
imgutils.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:425
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236
HintModes
HintModes
Definition: vf_fieldhint.c:32
FieldHintContext::eof
int eof
Definition: vf_fieldhint.c:50