FFmpeg
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/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 
35 #include "avfilter.h"
36 #include "filters.h"
37 #include "video.h"
38 
39 typedef struct KerndeintContext {
40  const AVClass *class;
41  int frame; ///< frame count, starting from 0
43  int vsub;
45  uint8_t *tmp_data [4]; ///< temporary plane data buffer
46  int tmp_linesize[4]; ///< temporary plane byte linesize
47  int tmp_bwidth [4]; ///< temporary plane byte width
49 
50 #define OFFSET(x) offsetof(KerndeintContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 static const AVOption kerndeint_options[] = {
53  { "thresh", "set the threshold", OFFSET(thresh), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
54  { "map", "set the map", OFFSET(map), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
55  { "order", "set the order", OFFSET(order), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
56  { "sharp", "set sharpening", OFFSET(sharp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
57  { "twoway", "set twoway", OFFSET(twoway), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
58  { NULL }
59 };
60 
61 AVFILTER_DEFINE_CLASS(kerndeint);
62 
64 {
65  KerndeintContext *kerndeint = ctx->priv;
66 
67  av_freep(&kerndeint->tmp_data[0]);
68 }
69 
70 static const enum AVPixelFormat pix_fmts[] = {
78 };
79 
81 {
82  KerndeintContext *kerndeint = inlink->dst->priv;
84  int ret;
85 
87  kerndeint->vsub = desc->log2_chroma_h;
88 
89  ret = av_image_alloc(kerndeint->tmp_data, kerndeint->tmp_linesize,
90  inlink->w, inlink->h, inlink->format, 16);
91  if (ret < 0)
92  return ret;
93  memset(kerndeint->tmp_data[0], 0, ret);
94 
95  if ((ret = av_image_fill_linesizes(kerndeint->tmp_bwidth, inlink->format, inlink->w)) < 0)
96  return ret;
97 
98  return 0;
99 }
100 
102 {
103  KerndeintContext *kerndeint = inlink->dst->priv;
104  AVFilterLink *outlink = inlink->dst->outputs[0];
105  AVFrame *outpic;
106  const uint8_t *prvp; ///< Previous field's pixel line number n
107  const uint8_t *prvpp; ///< Previous field's pixel line number (n - 1)
108  const uint8_t *prvpn; ///< Previous field's pixel line number (n + 1)
109  const uint8_t *prvppp; ///< Previous field's pixel line number (n - 2)
110  const uint8_t *prvpnn; ///< Previous field's pixel line number (n + 2)
111  const uint8_t *prvp4p; ///< Previous field's pixel line number (n - 4)
112  const uint8_t *prvp4n; ///< Previous field's pixel line number (n + 4)
113 
114  const uint8_t *srcp; ///< Current field's pixel line number n
115  const uint8_t *srcpp; ///< Current field's pixel line number (n - 1)
116  const uint8_t *srcpn; ///< Current field's pixel line number (n + 1)
117  const uint8_t *srcppp; ///< Current field's pixel line number (n - 2)
118  const uint8_t *srcpnn; ///< Current field's pixel line number (n + 2)
119  const uint8_t *srcp3p; ///< Current field's pixel line number (n - 3)
120  const uint8_t *srcp3n; ///< Current field's pixel line number (n + 3)
121  const uint8_t *srcp4p; ///< Current field's pixel line number (n - 4)
122  const uint8_t *srcp4n; ///< Current field's pixel line number (n + 4)
123 
124  uint8_t *dstp, *dstp_saved;
125  const uint8_t *srcp_saved;
126 
127  int src_linesize, psrc_linesize, dst_linesize, bwidth;
128  int x, y, plane, val, hi, lo, g, h, n = kerndeint->frame++;
129  double valf;
130 
131  const int thresh = kerndeint->thresh;
132  const int order = kerndeint->order;
133  const int map = kerndeint->map;
134  const int sharp = kerndeint->sharp;
135  const int twoway = kerndeint->twoway;
136 
137  const int is_packed_rgb = kerndeint->is_packed_rgb;
138 
139  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
140  if (!outpic) {
142  return AVERROR(ENOMEM);
143  }
144  av_frame_copy_props(outpic, inpic);
145 #if FF_API_INTERLACED_FRAME
147  outpic->interlaced_frame = 0;
149 #endif
150  outpic->flags &= ~AV_FRAME_FLAG_INTERLACED;
151 
152  for (plane = 0; plane < 4 && inpic->data[plane] && inpic->linesize[plane]; plane++) {
153  h = plane == 0 ? inlink->h : AV_CEIL_RSHIFT(inlink->h, kerndeint->vsub);
154  bwidth = kerndeint->tmp_bwidth[plane];
155 
156  srcp_saved = inpic->data[plane];
157  src_linesize = inpic->linesize[plane];
158  psrc_linesize = kerndeint->tmp_linesize[plane];
159  dstp_saved = outpic->data[plane];
160  dst_linesize = outpic->linesize[plane];
161  srcp = srcp_saved + (1 - order) * src_linesize;
162  dstp = dstp_saved + (1 - order) * dst_linesize;
163 
164  for (y = 0; y < h; y += 2) {
165  memcpy(dstp, srcp, bwidth);
166  srcp += 2 * src_linesize;
167  dstp += 2 * dst_linesize;
168  }
169 
170  // Copy through the lines that will be missed below.
171  memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, bwidth);
172  memcpy(dstp_saved + (2 + order ) * dst_linesize, srcp_saved + (3 - order) * src_linesize, bwidth);
173  memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, bwidth);
174  memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, bwidth);
175 
176  /* For the other field choose adaptively between using the previous field
177  or the interpolant from the current field. */
178  prvp = kerndeint->tmp_data[plane] + 5 * psrc_linesize - (1 - order) * psrc_linesize;
179  prvpp = prvp - psrc_linesize;
180  prvppp = prvp - 2 * psrc_linesize;
181  prvp4p = prvp - 4 * psrc_linesize;
182  prvpn = prvp + psrc_linesize;
183  prvpnn = prvp + 2 * psrc_linesize;
184  prvp4n = prvp + 4 * psrc_linesize;
185 
186  srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize;
187  srcpp = srcp - src_linesize;
188  srcppp = srcp - 2 * src_linesize;
189  srcp3p = srcp - 3 * src_linesize;
190  srcp4p = srcp - 4 * src_linesize;
191 
192  srcpn = srcp + src_linesize;
193  srcpnn = srcp + 2 * src_linesize;
194  srcp3n = srcp + 3 * src_linesize;
195  srcp4n = srcp + 4 * src_linesize;
196 
197  dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize;
198 
199  for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) {
200  for (x = 0; x < bwidth; x++) {
201  if (thresh == 0 || n == 0 ||
202  (abs((int)prvp[x] - (int)srcp[x]) > thresh) ||
203  (abs((int)prvpp[x] - (int)srcpp[x]) > thresh) ||
204  (abs((int)prvpn[x] - (int)srcpn[x]) > thresh)) {
205  if (map) {
206  g = x & ~3;
207 
208  if (is_packed_rgb) {
209  AV_WB32(dstp + g, 0xffffffff);
210  x = g + 3;
211  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
212  // y <- 235, u <- 128, y <- 235, v <- 128
213  AV_WB32(dstp + g, 0xeb80eb80);
214  x = g + 3;
215  } else {
216  dstp[x] = plane == 0 ? 235 : 128;
217  }
218  } else {
219  if (is_packed_rgb) {
220  hi = 255;
221  lo = 0;
222  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
223  hi = x & 1 ? 240 : 235;
224  lo = 16;
225  } else {
226  hi = plane == 0 ? 235 : 240;
227  lo = 16;
228  }
229 
230  if (sharp) {
231  if (twoway) {
232  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
233  + 0.170 * ((int)srcp[x] + (int)prvp[x])
234  - 0.116 * ((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
235  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
236  + 0.031 * ((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
237  } else {
238  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
239  + 0.170 * ((int)prvp[x])
240  - 0.116 * ((int)prvppp[x] + (int)prvpnn[x])
241  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
242  + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]);
243  }
244  dstp[x] = av_clip(valf, lo, hi);
245  } else {
246  if (twoway) {
247  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)srcp[x] + (int)prvp[x])
248  - (int)(srcppp[x]) - (int)(srcpnn[x])
249  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
250  } else {
251  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)prvp[x])
252  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
253  }
254  dstp[x] = av_clip(val, lo, hi);
255  }
256  }
257  } else {
258  dstp[x] = srcp[x];
259  }
260  }
261  prvp += 2 * psrc_linesize;
262  prvpp += 2 * psrc_linesize;
263  prvppp += 2 * psrc_linesize;
264  prvpn += 2 * psrc_linesize;
265  prvpnn += 2 * psrc_linesize;
266  prvp4p += 2 * psrc_linesize;
267  prvp4n += 2 * psrc_linesize;
268  srcp += 2 * src_linesize;
269  srcpp += 2 * src_linesize;
270  srcppp += 2 * src_linesize;
271  srcp3p += 2 * src_linesize;
272  srcp4p += 2 * src_linesize;
273  srcpn += 2 * src_linesize;
274  srcpnn += 2 * src_linesize;
275  srcp3n += 2 * src_linesize;
276  srcp4n += 2 * src_linesize;
277  dstp += 2 * dst_linesize;
278  }
279 
280  srcp = inpic->data[plane];
281  dstp = kerndeint->tmp_data[plane];
282  av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h);
283  }
284 
286  return ff_filter_frame(outlink, outpic);
287 }
288 
289 static const AVFilterPad kerndeint_inputs[] = {
290  {
291  .name = "default",
292  .type = AVMEDIA_TYPE_VIDEO,
293  .filter_frame = filter_frame,
294  .config_props = config_props,
295  },
296 };
297 
298 
300  .name = "kerndeint",
301  .description = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."),
302  .priv_size = sizeof(KerndeintContext),
303  .priv_class = &kerndeint_class,
304  .uninit = uninit,
308 };
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
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
av_clip
#define av_clip
Definition: common.h:100
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
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: filters.h:242
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_kerndeint.c:63
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
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:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
AVOption
AVOption.
Definition: opt.h:429
KerndeintContext::tmp_data
uint8_t * tmp_data[4]
temporary plane data buffer
Definition: vf_kerndeint.c:45
kerndeint_inputs
static const AVFilterPad kerndeint_inputs[]
Definition: vf_kerndeint.c:289
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:661
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:410
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
KerndeintContext
Definition: vf_kerndeint.c:39
val
static double val(void *priv, double ch)
Definition: aeval.c:77
KerndeintContext::twoway
int twoway
Definition: vf_kerndeint.c:42
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AVFrame::interlaced_frame
attribute_deprecated int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:566
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(kerndeint)
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_kerndeint.c:80
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
intreadwrite.h
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
g
const char * g
Definition: vf_curves.c:128
filters.h
KerndeintContext::order
int order
Definition: vf_kerndeint.c:42
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
KerndeintContext::map
int map
Definition: vf_kerndeint.c:42
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
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:713
inpic
av_frame_free & inpic
Definition: vf_mcdeint.c:285
AV_PIX_FMT_YUYV422
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:74
ff_vf_kerndeint
const AVFilter ff_vf_kerndeint
Definition: vf_kerndeint.c:299
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_kerndeint.c:70
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:265
abs
#define abs(x)
Definition: cuda_runtime.h:35
AVPixFmtDescriptor::flags
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:94
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
KerndeintContext::tmp_linesize
int tmp_linesize[4]
temporary plane byte linesize
Definition: vf_kerndeint.c:46
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:415
av_image_alloc
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:218
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
AV_PIX_FMT_FLAG_RGB
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:136
KerndeintContext::is_packed_rgb
int is_packed_rgb
Definition: vf_kerndeint.c:44
AV_PIX_FMT_RGB0
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:263
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_kerndeint.c:101
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
FLAGS
#define FLAGS
Definition: vf_kerndeint.c:51
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:648
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_0BGR
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:264
KerndeintContext::frame
int frame
frame count, starting from 0
Definition: vf_kerndeint.c:41
KerndeintContext::tmp_bwidth
int tmp_bwidth[4]
temporary plane byte width
Definition: vf_kerndeint.c:47
KerndeintContext::thresh
int thresh
Definition: vf_kerndeint.c:42
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
KerndeintContext::vsub
int vsub
Definition: vf_kerndeint.c:43
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
mem.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
OFFSET
#define OFFSET(x)
Definition: vf_kerndeint.c:50
kerndeint_options
static const AVOption kerndeint_options[]
Definition: vf_kerndeint.c:52
imgutils.h
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:434
AV_PIX_FMT_0RGB
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:262
KerndeintContext::sharp
int sharp
Definition: vf_kerndeint.c:42
h
h
Definition: vp9dsp_template.c:2070