FFmpeg  4.4.7
vf_kerndeint.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Jeremy Tran
3  * Copyright (c) 2004 Tobias Diedrich
4  * Copyright (c) 2003 Donald A. Graft
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 /**
24  * @file
25  * Kernel Deinterlacer
26  * Ported from MPlayer libmpcodecs/vf_kerndeint.c.
27  */
28 
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 
38 typedef struct KerndeintContext {
39  const AVClass *class;
40  int frame; ///< frame count, starting from 0
42  int vsub;
44  uint8_t *tmp_data [4]; ///< temporary plane data buffer
45  int tmp_linesize[4]; ///< temporary plane byte linesize
46  int tmp_bwidth [4]; ///< temporary plane byte width
48 
49 #define OFFSET(x) offsetof(KerndeintContext, x)
50 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption kerndeint_options[] = {
52  { "thresh", "set the threshold", OFFSET(thresh), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
53  { "map", "set the map", OFFSET(map), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
54  { "order", "set the order", OFFSET(order), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
55  { "sharp", "set sharpening", OFFSET(sharp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
56  { "twoway", "set twoway", OFFSET(twoway), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
57  { NULL }
58 };
59 
61 
63 {
64  KerndeintContext *kerndeint = ctx->priv;
65 
66  av_freep(&kerndeint->tmp_data[0]);
67 }
68 
70 {
71  static const enum AVPixelFormat pix_fmts[] = {
79  };
80 
82  if (!fmts_list)
83  return AVERROR(ENOMEM);
84  return ff_set_common_formats(ctx, fmts_list);
85 }
86 
87 static int config_props(AVFilterLink *inlink)
88 {
89  KerndeintContext *kerndeint = inlink->dst->priv;
91  int ret;
92 
94  kerndeint->vsub = desc->log2_chroma_h;
95  if (AV_CEIL_RSHIFT(inlink->h, kerndeint->vsub) < 4) {
96  av_log(inlink->dst, AV_LOG_ERROR,
97  "Input height %d is too small; minimum chroma plane height is 4\n",
98  inlink->h);
99  return AVERROR(EINVAL);
100  }
101 
102  ret = av_image_alloc(kerndeint->tmp_data, kerndeint->tmp_linesize,
103  inlink->w, inlink->h, inlink->format, 16);
104  if (ret < 0)
105  return ret;
106  memset(kerndeint->tmp_data[0], 0, ret);
107 
108  if ((ret = av_image_fill_linesizes(kerndeint->tmp_bwidth, inlink->format, inlink->w)) < 0)
109  return ret;
110 
111  return 0;
112 }
113 
114 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
115 {
116  KerndeintContext *kerndeint = inlink->dst->priv;
117  AVFilterLink *outlink = inlink->dst->outputs[0];
118  AVFrame *outpic;
119  const uint8_t *prvp; ///< Previous field's pixel line number n
120  const uint8_t *prvpp; ///< Previous field's pixel line number (n - 1)
121  const uint8_t *prvpn; ///< Previous field's pixel line number (n + 1)
122  const uint8_t *prvppp; ///< Previous field's pixel line number (n - 2)
123  const uint8_t *prvpnn; ///< Previous field's pixel line number (n + 2)
124  const uint8_t *prvp4p; ///< Previous field's pixel line number (n - 4)
125  const uint8_t *prvp4n; ///< Previous field's pixel line number (n + 4)
126 
127  const uint8_t *srcp; ///< Current field's pixel line number n
128  const uint8_t *srcpp; ///< Current field's pixel line number (n - 1)
129  const uint8_t *srcpn; ///< Current field's pixel line number (n + 1)
130  const uint8_t *srcppp; ///< Current field's pixel line number (n - 2)
131  const uint8_t *srcpnn; ///< Current field's pixel line number (n + 2)
132  const uint8_t *srcp3p; ///< Current field's pixel line number (n - 3)
133  const uint8_t *srcp3n; ///< Current field's pixel line number (n + 3)
134  const uint8_t *srcp4p; ///< Current field's pixel line number (n - 4)
135  const uint8_t *srcp4n; ///< Current field's pixel line number (n + 4)
136 
137  uint8_t *dstp, *dstp_saved;
138  const uint8_t *srcp_saved;
139 
140  int src_linesize, psrc_linesize, dst_linesize, bwidth;
141  int x, y, plane, val, hi, lo, g, h, n = kerndeint->frame++;
142  double valf;
143 
144  const int thresh = kerndeint->thresh;
145  const int order = kerndeint->order;
146  const int map = kerndeint->map;
147  const int sharp = kerndeint->sharp;
148  const int twoway = kerndeint->twoway;
149 
150  const int is_packed_rgb = kerndeint->is_packed_rgb;
151 
152  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
153  if (!outpic) {
155  return AVERROR(ENOMEM);
156  }
157  av_frame_copy_props(outpic, inpic);
158  outpic->interlaced_frame = 0;
159 
160  for (plane = 0; plane < 4 && inpic->data[plane] && inpic->linesize[plane]; plane++) {
161  h = plane == 0 ? inlink->h : AV_CEIL_RSHIFT(inlink->h, kerndeint->vsub);
162  bwidth = kerndeint->tmp_bwidth[plane];
163 
164  srcp_saved = inpic->data[plane];
165  src_linesize = inpic->linesize[plane];
166  psrc_linesize = kerndeint->tmp_linesize[plane];
167  dstp_saved = outpic->data[plane];
168  dst_linesize = outpic->linesize[plane];
169  srcp = srcp_saved + (1 - order) * src_linesize;
170  dstp = dstp_saved + (1 - order) * dst_linesize;
171 
172  for (y = 0; y < h; y += 2) {
173  memcpy(dstp, srcp, bwidth);
174  srcp += 2 * src_linesize;
175  dstp += 2 * dst_linesize;
176  }
177 
178  // Copy through the lines that will be missed below.
179  memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, bwidth);
180  memcpy(dstp_saved + (2 + order ) * dst_linesize, srcp_saved + (3 - order) * src_linesize, bwidth);
181  memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, bwidth);
182  memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, bwidth);
183 
184  /* For the other field choose adaptively between using the previous field
185  or the interpolant from the current field. */
186  prvp = kerndeint->tmp_data[plane] + 5 * psrc_linesize - (1 - order) * psrc_linesize;
187  prvpp = prvp - psrc_linesize;
188  prvppp = prvp - 2 * psrc_linesize;
189  prvp4p = prvp - 4 * psrc_linesize;
190  prvpn = prvp + psrc_linesize;
191  prvpnn = prvp + 2 * psrc_linesize;
192  prvp4n = prvp + 4 * psrc_linesize;
193 
194  srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize;
195  srcpp = srcp - src_linesize;
196  srcppp = srcp - 2 * src_linesize;
197  srcp3p = srcp - 3 * src_linesize;
198  srcp4p = srcp - 4 * src_linesize;
199 
200  srcpn = srcp + src_linesize;
201  srcpnn = srcp + 2 * src_linesize;
202  srcp3n = srcp + 3 * src_linesize;
203  srcp4n = srcp + 4 * src_linesize;
204 
205  dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize;
206 
207  for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) {
208  for (x = 0; x < bwidth; x++) {
209  if (thresh == 0 || n == 0 ||
210  (abs((int)prvp[x] - (int)srcp[x]) > thresh) ||
211  (abs((int)prvpp[x] - (int)srcpp[x]) > thresh) ||
212  (abs((int)prvpn[x] - (int)srcpn[x]) > thresh)) {
213  if (map) {
214  g = x & ~3;
215 
216  if (is_packed_rgb) {
217  AV_WB32(dstp + g, 0xffffffff);
218  x = g + 3;
219  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
220  // y <- 235, u <- 128, y <- 235, v <- 128
221  AV_WB32(dstp + g, 0xeb80eb80);
222  x = g + 3;
223  } else {
224  dstp[x] = plane == 0 ? 235 : 128;
225  }
226  } else {
227  if (is_packed_rgb) {
228  hi = 255;
229  lo = 0;
230  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
231  hi = x & 1 ? 240 : 235;
232  lo = 16;
233  } else {
234  hi = plane == 0 ? 235 : 240;
235  lo = 16;
236  }
237 
238  if (sharp) {
239  if (twoway) {
240  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
241  + 0.170 * ((int)srcp[x] + (int)prvp[x])
242  - 0.116 * ((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
243  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
244  + 0.031 * ((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
245  } else {
246  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
247  + 0.170 * ((int)prvp[x])
248  - 0.116 * ((int)prvppp[x] + (int)prvpnn[x])
249  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
250  + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]);
251  }
252  dstp[x] = av_clip(valf, lo, hi);
253  } else {
254  if (twoway) {
255  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)srcp[x] + (int)prvp[x])
256  - (int)(srcppp[x]) - (int)(srcpnn[x])
257  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
258  } else {
259  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)prvp[x])
260  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
261  }
262  dstp[x] = av_clip(val, lo, hi);
263  }
264  }
265  } else {
266  dstp[x] = srcp[x];
267  }
268  }
269  prvp += 2 * psrc_linesize;
270  prvpp += 2 * psrc_linesize;
271  prvppp += 2 * psrc_linesize;
272  prvpn += 2 * psrc_linesize;
273  prvpnn += 2 * psrc_linesize;
274  prvp4p += 2 * psrc_linesize;
275  prvp4n += 2 * psrc_linesize;
276  srcp += 2 * src_linesize;
277  srcpp += 2 * src_linesize;
278  srcppp += 2 * src_linesize;
279  srcp3p += 2 * src_linesize;
280  srcp4p += 2 * src_linesize;
281  srcpn += 2 * src_linesize;
282  srcpnn += 2 * src_linesize;
283  srcp3n += 2 * src_linesize;
284  srcp4n += 2 * src_linesize;
285  dstp += 2 * dst_linesize;
286  }
287 
288  srcp = inpic->data[plane];
289  dstp = kerndeint->tmp_data[plane];
290  av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h);
291  }
292 
294  return ff_filter_frame(outlink, outpic);
295 }
296 
297 static const AVFilterPad kerndeint_inputs[] = {
298  {
299  .name = "default",
300  .type = AVMEDIA_TYPE_VIDEO,
301  .filter_frame = filter_frame,
302  .config_props = config_props,
303  },
304  { NULL }
305 };
306 
307 static const AVFilterPad kerndeint_outputs[] = {
308  {
309  .name = "default",
310  .type = AVMEDIA_TYPE_VIDEO,
311  },
312  { NULL }
313 };
314 
315 
317  .name = "kerndeint",
318  .description = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."),
319  .priv_size = sizeof(KerndeintContext),
320  .priv_class = &kerndeint_class,
321  .uninit = uninit,
325 };
static double val(void *priv, double ch)
Definition: aeval.c:76
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
Main libavfilter public API header.
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define av_clip
Definition: common.h:122
#define NULL
Definition: coverity.c:32
#define abs(x)
Definition: cuda_runtime.h:35
int
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:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#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
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:373
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:216
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 VDPAUPixFmtMap * map
misc image utilities
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char * desc
Definition: libsvtav1.c:79
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:239
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:67
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:237
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:465
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
int frame
frame count, starting from 0
Definition: vf_kerndeint.c:40
int tmp_bwidth[4]
temporary plane byte width
Definition: vf_kerndeint.c:46
int tmp_linesize[4]
temporary plane byte linesize
Definition: vf_kerndeint.c:45
uint8_t * tmp_data[4]
temporary plane data buffer
Definition: vf_kerndeint.c:44
#define av_freep(p)
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
const char * g
Definition: vf_curves.c:117
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_kerndeint.c:114
static int query_formats(AVFilterContext *ctx)
Definition: vf_kerndeint.c:69
AVFILTER_DEFINE_CLASS(kerndeint)
#define FLAGS
Definition: vf_kerndeint.c:50
static const AVFilterPad kerndeint_outputs[]
Definition: vf_kerndeint.c:307
static int config_props(AVFilterLink *inlink)
Definition: vf_kerndeint.c:87
AVFilter ff_vf_kerndeint
Definition: vf_kerndeint.c:316
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_kerndeint.c:62
#define OFFSET(x)
Definition: vf_kerndeint.c:49
static const AVOption kerndeint_options[]
Definition: vf_kerndeint.c:51
static const AVFilterPad kerndeint_inputs[]
Definition: vf_kerndeint.c:297
av_frame_free & inpic
Definition: vf_mcdeint.c:281
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