FFmpeg  4.3.7
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/imgutils.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct FieldHintContext {
31  const AVClass *class;
32 
34  FILE *hint;
35  int mode;
36 
38 
39  int64_t line;
40  int nb_planes;
41  int eof;
42  int planewidth[4];
43  int planeheight[4];
45 
46 #define OFFSET(x) offsetof(FieldHintContext, x)
47 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48 
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" },
54  { NULL }
55 };
56 
57 AVFILTER_DEFINE_CLASS(fieldhint);
58 
60 {
61  FieldHintContext *s = ctx->priv;
62  int ret;
63 
64  if (!s->hint_file_str) {
65  av_log(ctx, AV_LOG_ERROR, "Hint file must be set.\n");
66  return AVERROR(EINVAL);
67  }
68  s->hint = av_fopen_utf8(s->hint_file_str, "r");
69  if (!s->hint) {
70  ret = AVERROR(errno);
71  av_log(ctx, AV_LOG_ERROR, "%s: %s\n", s->hint_file_str, av_err2str(ret));
72  return ret;
73  }
74 
75  return 0;
76 }
77 
79 {
81  int fmt, ret;
82 
83  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
85  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
86  desc->flags & AV_PIX_FMT_FLAG_PAL ||
88  (ret = ff_add_format(&pix_fmts, fmt)) < 0)
89  return ret;
90  }
91 
92  return ff_set_common_formats(ctx, pix_fmts);
93 }
94 
95 static int config_input(AVFilterLink *inlink)
96 {
97  FieldHintContext *s = inlink->dst->priv;
99  int ret;
100 
101  if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
102  return ret;
103 
104  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
105  s->planeheight[0] = s->planeheight[3] = inlink->h;
106 
108 
109  return 0;
110 }
111 
112 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
113 {
114  AVFilterContext *ctx = inlink->dst;
115  AVFilterLink *outlink = ctx->outputs[0];
116  FieldHintContext *s = ctx->priv;
117  AVFrame *out, *top, *bottom;
118  char buf[1024] = { 0 };
119  int64_t tf, bf;
120  int tfactor = 0, bfactor = 1;
121  char hint = '=', field = '=';
122  int p;
123 
124  av_frame_free(&s->frame[0]);
125  s->frame[0] = s->frame[1];
126  s->frame[1] = s->frame[2];
127  s->frame[2] = in;
128  if (!s->frame[1])
129  return 0;
130  else if (!s->frame[0]) {
131  s->frame[0] = av_frame_clone(s->frame[1]);
132  if (!s->frame[0])
133  return AVERROR(ENOMEM);
134  }
135 
136  while (1) {
137  if (fgets(buf, sizeof(buf)-1, s->hint)) {
138  s->line++;
139  if (buf[0] == '#' || buf[0] == ';') {
140  continue;
141  } else if (sscanf(buf, "%"PRId64",%"PRId64" %c %c", &tf, &bf, &hint, &field) == 4) {
142  ;
143  } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
144  ;
145  } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
146  ;
147  } else {
148  av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
149  return AVERROR_INVALIDDATA;
150  }
151  switch (s->mode) {
152  case 0:
153  if (tf > outlink->frame_count_in + 1 || tf < FFMAX(0, outlink->frame_count_in - 1) ||
154  bf > outlink->frame_count_in + 1 || bf < FFMAX(0, outlink->frame_count_in - 1)) {
155  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);
156  return AVERROR_INVALIDDATA;
157  }
158  break;
159  case 1:
160  if (tf > 1 || tf < -1 ||
161  bf > 1 || bf < -1) {
162  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);
163  return AVERROR_INVALIDDATA;
164  }
165  };
166  break;
167  } else {
168  av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inlink->frame_count_out);
169  return AVERROR_INVALIDDATA;
170  }
171  }
172 
173  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
174  if (!out)
175  return AVERROR(ENOMEM);
176  av_frame_copy_props(out, s->frame[1]);
177 
178  switch (s->mode) {
179  case 0:
180  top = s->frame[tf - outlink->frame_count_in + 1];
181  bottom = s->frame[bf - outlink->frame_count_in + 1];
182  break;
183  case 1:
184  top = s->frame[1 + tf];
185  bottom = s->frame[1 + bf];
186  break;
187  default:
188  av_assert0(0);
189  }
190 
191  switch (field) {
192  case 'b':
193  tfactor = 1;
194  top = bottom;
195  break;
196  case 't':
197  bfactor = 0;
198  bottom = top;
199  break;
200  case '=':
201  break;
202  default:
203  av_log(ctx, AV_LOG_ERROR, "Invalid field: %c.\n", field);
204  av_frame_free(&out);
205  return AVERROR(EINVAL);
206  }
207 
208  switch (hint) {
209  case '+':
210  out->interlaced_frame = 1;
211  break;
212  case '-':
213  out->interlaced_frame = 0;
214  break;
215  case '=':
216  break;
217  case 'b':
218  tfactor = 1;
219  top = bottom;
220  break;
221  case 't':
222  bfactor = 0;
223  bottom = top;
224  break;
225  default:
226  av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
227  av_frame_free(&out);
228  return AVERROR(EINVAL);
229  }
230 
231  for (p = 0; p < s->nb_planes; p++) {
232  av_image_copy_plane(out->data[p],
233  out->linesize[p] * 2,
234  top->data[p] + tfactor * top->linesize[p],
235  top->linesize[p] * 2,
236  s->planewidth[p],
237  (s->planeheight[p] + 1) / 2);
238  av_image_copy_plane(out->data[p] + out->linesize[p],
239  out->linesize[p] * 2,
240  bottom->data[p] + bfactor * bottom->linesize[p],
241  bottom->linesize[p] * 2,
242  s->planewidth[p],
243  (s->planeheight[p] + 1) / 2);
244  }
245 
246  return ff_filter_frame(outlink, out);
247 }
248 
249 static int request_frame(AVFilterLink *outlink)
250 {
251  AVFilterContext *ctx = outlink->src;
252  FieldHintContext *s = ctx->priv;
253  int ret;
254 
255  if (s->eof)
256  return AVERROR_EOF;
257 
258  ret = ff_request_frame(ctx->inputs[0]);
259  if (ret == AVERROR_EOF && s->frame[2]) {
260  AVFrame *next = av_frame_clone(s->frame[2]);
261  if (!next)
262  return AVERROR(ENOMEM);
263  ret = filter_frame(ctx->inputs[0], next);
264  s->eof = 1;
265  }
266 
267  return ret;
268 }
269 
271 {
272  FieldHintContext *s = ctx->priv;
273 
274  if (s->hint)
275  fclose(s->hint);
276  s->hint = NULL;
277 
278  av_frame_free(&s->frame[0]);
279  av_frame_free(&s->frame[1]);
280  av_frame_free(&s->frame[2]);
281 }
282 
283 static const AVFilterPad inputs[] = {
284  {
285  .name = "default",
286  .type = AVMEDIA_TYPE_VIDEO,
287  .config_props = config_input,
288  .filter_frame = filter_frame,
289  },
290  { NULL }
291 };
292 
293 static const AVFilterPad outputs[] = {
294  {
295  .name = "default",
296  .type = AVMEDIA_TYPE_VIDEO,
297  .request_frame = request_frame,
298  },
299  { NULL }
300 };
301 
303  .name = "fieldhint",
304  .description = NULL_IF_CONFIG_SMALL("Field matching using hints."),
305  .priv_size = sizeof(FieldHintContext),
306  .priv_class = &fieldhint_class,
307  .init = init,
308  .uninit = uninit,
310  .inputs = inputs,
311  .outputs = outputs,
312 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
#define FLAGS
Definition: vf_fieldhint.c:47
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2589
Main libavfilter public API header.
static const AVFilterPad inputs[]
Definition: vf_fieldhint.c:283
const char * desc
Definition: nvenc.c:79
FILE * av_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:158
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104
#define OFFSET(x)
Definition: vf_fieldhint.c:46
#define tf
Definition: regdef.h:73
const char * name
Pad name.
Definition: internal.h:60
static av_cold int init(AVFilterContext *ctx)
Definition: vf_fieldhint.c:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
#define av_cold
Definition: attributes.h:88
AVOptions.
#define AVERROR_EOF
End of file.
Definition: error.h:55
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:447
#define av_log(a,...)
static int request_frame(AVFilterLink *outlink)
Definition: vf_fieldhint.c:249
AVFilter ff_vf_fieldhint
Definition: vf_fieldhint.c:302
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:605
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void * priv
private data for use by the filter
Definition: avfilter.h:353
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldhint.c:78
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:350
#define FFMAX(a, b)
Definition: common.h:94
AVFILTER_DEFINE_CLASS(fieldhint)
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:541
char * hint_file_str
Definition: vf_fieldhint.c:33
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
static const AVOption fieldhint_options[]
Definition: vf_fieldhint.c:49
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
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
const char * name
Filter name.
Definition: avfilter.h:148
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
static const AVFilterPad outputs[]
Definition: vf_fieldhint.c:293
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
static int config_input(AVFilterLink *inlink)
Definition: vf_fieldhint.c:95
AVFrame * frame[3]
Definition: vf_fieldhint.c:37
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_fieldhint.c:270
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
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:338
internal API functions
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_fieldhint.c:112
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:659
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58