FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
39  const AVClass *class;
40  int frame; ///< frame count, starting from 0
41  int thresh, map, order, sharp, twoway;
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 
60 AVFILTER_DEFINE_CLASS(kerndeint);
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 
81  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
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 
96  ret = av_image_alloc(kerndeint->tmp_data, kerndeint->tmp_linesize,
97  inlink->w, inlink->h, inlink->format, 16);
98  if (ret < 0)
99  return ret;
100  memset(kerndeint->tmp_data[0], 0, ret);
101 
102  if ((ret = av_image_fill_linesizes(kerndeint->tmp_bwidth, inlink->format, inlink->w)) < 0)
103  return ret;
104 
105  return 0;
106 }
107 
108 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
109 {
110  KerndeintContext *kerndeint = inlink->dst->priv;
111  AVFilterLink *outlink = inlink->dst->outputs[0];
112  AVFrame *outpic;
113  const uint8_t *prvp; ///< Previous field's pixel line number n
114  const uint8_t *prvpp; ///< Previous field's pixel line number (n - 1)
115  const uint8_t *prvpn; ///< Previous field's pixel line number (n + 1)
116  const uint8_t *prvppp; ///< Previous field's pixel line number (n - 2)
117  const uint8_t *prvpnn; ///< Previous field's pixel line number (n + 2)
118  const uint8_t *prvp4p; ///< Previous field's pixel line number (n - 4)
119  const uint8_t *prvp4n; ///< Previous field's pixel line number (n + 4)
120 
121  const uint8_t *srcp; ///< Current field's pixel line number n
122  const uint8_t *srcpp; ///< Current field's pixel line number (n - 1)
123  const uint8_t *srcpn; ///< Current field's pixel line number (n + 1)
124  const uint8_t *srcppp; ///< Current field's pixel line number (n - 2)
125  const uint8_t *srcpnn; ///< Current field's pixel line number (n + 2)
126  const uint8_t *srcp3p; ///< Current field's pixel line number (n - 3)
127  const uint8_t *srcp3n; ///< Current field's pixel line number (n + 3)
128  const uint8_t *srcp4p; ///< Current field's pixel line number (n - 4)
129  const uint8_t *srcp4n; ///< Current field's pixel line number (n + 4)
130 
131  uint8_t *dstp, *dstp_saved;
132  const uint8_t *srcp_saved;
133 
134  int src_linesize, psrc_linesize, dst_linesize, bwidth;
135  int x, y, plane, val, hi, lo, g, h, n = kerndeint->frame++;
136  double valf;
137 
138  const int thresh = kerndeint->thresh;
139  const int order = kerndeint->order;
140  const int map = kerndeint->map;
141  const int sharp = kerndeint->sharp;
142  const int twoway = kerndeint->twoway;
143 
144  const int is_packed_rgb = kerndeint->is_packed_rgb;
145 
146  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
147  if (!outpic) {
148  av_frame_free(&inpic);
149  return AVERROR(ENOMEM);
150  }
151  av_frame_copy_props(outpic, inpic);
152  outpic->interlaced_frame = 0;
153 
154  for (plane = 0; plane < 4 && inpic->data[plane] && inpic->linesize[plane]; plane++) {
155  h = plane == 0 ? inlink->h : AV_CEIL_RSHIFT(inlink->h, kerndeint->vsub);
156  bwidth = kerndeint->tmp_bwidth[plane];
157 
158  srcp_saved = inpic->data[plane];
159  src_linesize = inpic->linesize[plane];
160  psrc_linesize = kerndeint->tmp_linesize[plane];
161  dstp_saved = outpic->data[plane];
162  dst_linesize = outpic->linesize[plane];
163  srcp = srcp_saved + (1 - order) * src_linesize;
164  dstp = dstp_saved + (1 - order) * dst_linesize;
165 
166  for (y = 0; y < h; y += 2) {
167  memcpy(dstp, srcp, bwidth);
168  srcp += 2 * src_linesize;
169  dstp += 2 * dst_linesize;
170  }
171 
172  // Copy through the lines that will be missed below.
173  memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, bwidth);
174  memcpy(dstp_saved + (2 + order ) * dst_linesize, srcp_saved + (3 - order) * src_linesize, bwidth);
175  memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, bwidth);
176  memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, bwidth);
177 
178  /* For the other field choose adaptively between using the previous field
179  or the interpolant from the current field. */
180  prvp = kerndeint->tmp_data[plane] + 5 * psrc_linesize - (1 - order) * psrc_linesize;
181  prvpp = prvp - psrc_linesize;
182  prvppp = prvp - 2 * psrc_linesize;
183  prvp4p = prvp - 4 * psrc_linesize;
184  prvpn = prvp + psrc_linesize;
185  prvpnn = prvp + 2 * psrc_linesize;
186  prvp4n = prvp + 4 * psrc_linesize;
187 
188  srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize;
189  srcpp = srcp - src_linesize;
190  srcppp = srcp - 2 * src_linesize;
191  srcp3p = srcp - 3 * src_linesize;
192  srcp4p = srcp - 4 * src_linesize;
193 
194  srcpn = srcp + src_linesize;
195  srcpnn = srcp + 2 * src_linesize;
196  srcp3n = srcp + 3 * src_linesize;
197  srcp4n = srcp + 4 * src_linesize;
198 
199  dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize;
200 
201  for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) {
202  for (x = 0; x < bwidth; x++) {
203  if (thresh == 0 || n == 0 ||
204  (abs((int)prvp[x] - (int)srcp[x]) > thresh) ||
205  (abs((int)prvpp[x] - (int)srcpp[x]) > thresh) ||
206  (abs((int)prvpn[x] - (int)srcpn[x]) > thresh)) {
207  if (map) {
208  g = x & ~3;
209 
210  if (is_packed_rgb) {
211  AV_WB32(dstp + g, 0xffffffff);
212  x = g + 3;
213  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
214  // y <- 235, u <- 128, y <- 235, v <- 128
215  AV_WB32(dstp + g, 0xeb80eb80);
216  x = g + 3;
217  } else {
218  dstp[x] = plane == 0 ? 235 : 128;
219  }
220  } else {
221  if (is_packed_rgb) {
222  hi = 255;
223  lo = 0;
224  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
225  hi = x & 1 ? 240 : 235;
226  lo = 16;
227  } else {
228  hi = plane == 0 ? 235 : 240;
229  lo = 16;
230  }
231 
232  if (sharp) {
233  if (twoway) {
234  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
235  + 0.170 * ((int)srcp[x] + (int)prvp[x])
236  - 0.116 * ((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
237  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
238  + 0.031 * ((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
239  } else {
240  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
241  + 0.170 * ((int)prvp[x])
242  - 0.116 * ((int)prvppp[x] + (int)prvpnn[x])
243  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
244  + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]);
245  }
246  dstp[x] = av_clip(valf, lo, hi);
247  } else {
248  if (twoway) {
249  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)srcp[x] + (int)prvp[x])
250  - (int)(srcppp[x]) - (int)(srcpnn[x])
251  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
252  } else {
253  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)prvp[x])
254  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
255  }
256  dstp[x] = av_clip(val, lo, hi);
257  }
258  }
259  } else {
260  dstp[x] = srcp[x];
261  }
262  }
263  prvp += 2 * psrc_linesize;
264  prvpp += 2 * psrc_linesize;
265  prvppp += 2 * psrc_linesize;
266  prvpn += 2 * psrc_linesize;
267  prvpnn += 2 * psrc_linesize;
268  prvp4p += 2 * psrc_linesize;
269  prvp4n += 2 * psrc_linesize;
270  srcp += 2 * src_linesize;
271  srcpp += 2 * src_linesize;
272  srcppp += 2 * src_linesize;
273  srcp3p += 2 * src_linesize;
274  srcp4p += 2 * src_linesize;
275  srcpn += 2 * src_linesize;
276  srcpnn += 2 * src_linesize;
277  srcp3n += 2 * src_linesize;
278  srcp4n += 2 * src_linesize;
279  dstp += 2 * dst_linesize;
280  }
281 
282  srcp = inpic->data[plane];
283  dstp = kerndeint->tmp_data[plane];
284  av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h);
285  }
286 
287  av_frame_free(&inpic);
288  return ff_filter_frame(outlink, outpic);
289 }
290 
291 static const AVFilterPad kerndeint_inputs[] = {
292  {
293  .name = "default",
294  .type = AVMEDIA_TYPE_VIDEO,
295  .filter_frame = filter_frame,
296  .config_props = config_props,
297  },
298  { NULL }
299 };
300 
301 static const AVFilterPad kerndeint_outputs[] = {
302  {
303  .name = "default",
304  .type = AVMEDIA_TYPE_VIDEO,
305  },
306  { NULL }
307 };
308 
309 
311  .name = "kerndeint",
312  .description = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."),
313  .priv_size = sizeof(KerndeintContext),
314  .priv_class = &kerndeint_class,
315  .uninit = uninit,
317  .inputs = kerndeint_inputs,
318  .outputs = kerndeint_outputs,
319 };
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
misc image utilities
#define OFFSET(x)
Definition: vf_kerndeint.c:49
Main libavfilter public API header.
const char * g
Definition: vf_curves.c:108
const char * desc
Definition: nvenc.c:89
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:191
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_kerndeint.c:62
static const AVFilterPad kerndeint_inputs[]
Definition: vf_kerndeint.c:291
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:252
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
uint8_t * tmp_data[4]
temporary plane data buffer
Definition: vf_kerndeint.c:44
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
BYTE int const BYTE * srcp
Definition: avisynth_c.h:676
const char * name
Pad name.
Definition: internal.h:59
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
uint8_t
#define av_cold
Definition: attributes.h:82
static const AVFilterPad kerndeint_outputs[]
Definition: vf_kerndeint.c:301
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:251
AVOptions.
av_frame_free & inpic
Definition: vf_mcdeint.c:281
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
AVFILTER_DEFINE_CLASS(kerndeint)
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:318
A filter pad used for either input or output.
Definition: internal.h:53
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:568
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
BYTE * dstp
Definition: avisynth_c.h:676
#define AVERROR(e)
Definition: error.h:43
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
static int config_props(AVFilterLink *inlink)
Definition: vf_kerndeint.c:87
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
void * priv
private data for use by the filter
Definition: avfilter.h:320
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
static int query_formats(AVFilterContext *ctx)
Definition: vf_kerndeint.c:69
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
AVFormatContext * ctx
Definition: movenc.c:48
int tmp_linesize[4]
temporary plane byte linesize
Definition: vf_kerndeint.c:45
int n
Definition: avisynth_c.h:547
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:142
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:88
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:253
const char * name
Filter name.
Definition: avfilter.h:146
const VDPAUPixFmtMap * map
static const AVOption kerndeint_options[]
Definition: vf_kerndeint.c:51
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_kerndeint.c:108
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
AVFilter ff_vf_kerndeint
Definition: vf_kerndeint.c:310
#define FLAGS
Definition: vf_kerndeint.c:50
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:305
#define av_freep(p)
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:287
internal API functions
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:250
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58