FFmpeg
 All Data Structures 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;
43  uint8_t *tmp_data [4]; ///< temporary plane data buffer
44  int tmp_bwidth[4]; ///< temporary plane byte width
46 
47 #define OFFSET(x) offsetof(KerndeintContext, x)
48 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 static const AVOption kerndeint_options[] = {
50  { "thresh", "set the threshold", OFFSET(thresh), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
51  { "map", "set the map", OFFSET(map), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
52  { "order", "set the order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
53  { "sharp", "enable sharpening", OFFSET(sharp), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
54  { "twoway", "enable twoway", OFFSET(twoway), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
55  { NULL }
56 };
57 
58 AVFILTER_DEFINE_CLASS(kerndeint);
59 
60 static av_cold int init(AVFilterContext *ctx, const char *args)
61 {
62  KerndeintContext *kerndeint = ctx->priv;
63  const char const * shorthand[] = { "thresh", "map", "order", "sharp", "twoway", NULL };
64 
65  kerndeint->class = &kerndeint_class;
66  av_opt_set_defaults(kerndeint);
67 
68  return av_opt_set_from_string(kerndeint, args, shorthand, "=", ":");
69 }
70 
71 static av_cold void uninit(AVFilterContext *ctx)
72 {
73  KerndeintContext *kerndeint = ctx->priv;
74 
75  av_free(kerndeint->tmp_data[0]);
76  av_opt_free(kerndeint);
77 }
78 
80 {
81  static const enum PixelFormat pix_fmts[] = {
89  };
90 
92 
93  return 0;
94 }
95 
96 static int config_props(AVFilterLink *inlink)
97 {
98  KerndeintContext *kerndeint = inlink->dst->priv;
99  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
100  int ret;
101 
102  kerndeint->vsub = desc->log2_chroma_h;
103 
104  ret = av_image_alloc(kerndeint->tmp_data, kerndeint->tmp_bwidth,
105  inlink->w, inlink->h, inlink->format, 1);
106  if (ret < 0)
107  return ret;
108  memset(kerndeint->tmp_data[0], 0, ret);
109  return 0;
110 }
111 
112 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
113 {
114  KerndeintContext *kerndeint = inlink->dst->priv;
115  AVFilterLink *outlink = inlink->dst->outputs[0];
116  AVFilterBufferRef *outpic;
117  const uint8_t *prvp; ///< Previous field's pixel line number n
118  const uint8_t *prvpp; ///< Previous field's pixel line number (n - 1)
119  const uint8_t *prvpn; ///< Previous field's pixel line number (n + 1)
120  const uint8_t *prvppp; ///< Previous field's pixel line number (n - 2)
121  const uint8_t *prvpnn; ///< Previous field's pixel line number (n + 2)
122  const uint8_t *prvp4p; ///< Previous field's pixel line number (n - 4)
123  const uint8_t *prvp4n; ///< Previous field's pixel line number (n + 4)
124 
125  const uint8_t *srcp; ///< Current field's pixel line number n
126  const uint8_t *srcpp; ///< Current field's pixel line number (n - 1)
127  const uint8_t *srcpn; ///< Current field's pixel line number (n + 1)
128  const uint8_t *srcppp; ///< Current field's pixel line number (n - 2)
129  const uint8_t *srcpnn; ///< Current field's pixel line number (n + 2)
130  const uint8_t *srcp3p; ///< Current field's pixel line number (n - 3)
131  const uint8_t *srcp3n; ///< Current field's pixel line number (n + 3)
132  const uint8_t *srcp4p; ///< Current field's pixel line number (n - 4)
133  const uint8_t *srcp4n; ///< Current field's pixel line number (n + 4)
134 
135  uint8_t *dstp, *dstp_saved;
136  const uint8_t *srcp_saved;
137 
138  int src_linesize, psrc_linesize, dst_linesize, bwidth;
139  int x, y, plane, val, hi, lo, g, h, n = kerndeint->frame++;
140  double valf;
141 
142  const int thresh = kerndeint->thresh;
143  const int order = kerndeint->order;
144  const int map = kerndeint->map;
145  const int sharp = kerndeint->sharp;
146  const int twoway = kerndeint->twoway;
147 
148  const int is_packed_rgb = av_pix_fmt_desc_get(inlink->format)->flags & PIX_FMT_RGB;
149 
150  outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
151  if (!outpic) {
152  avfilter_unref_bufferp(&inpic);
153  return AVERROR(ENOMEM);
154  }
155  avfilter_copy_buffer_ref_props(outpic, inpic);
156  outpic->video->interlaced = 0;
157 
158  for (plane = 0; inpic->data[plane] && plane < 4; plane++) {
159  h = plane == 0 ? inlink->h : inlink->h >> kerndeint->vsub;
160  bwidth = kerndeint->tmp_bwidth[plane];
161 
162  srcp = srcp_saved = inpic->data[plane];
163  src_linesize = inpic->linesize[plane];
164  psrc_linesize = kerndeint->tmp_bwidth[plane];
165  dstp = dstp_saved = outpic->data[plane];
166  dst_linesize = outpic->linesize[plane];
167  srcp = srcp_saved + (1 - order) * src_linesize;
168  dstp = dstp_saved + (1 - order) * dst_linesize;
169 
170  for (y = 0; y < h; y += 2) {
171  memcpy(dstp, srcp, bwidth);
172  srcp += 2 * src_linesize;
173  dstp += 2 * dst_linesize;
174  }
175 
176  // Copy through the lines that will be missed below.
177  memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, bwidth);
178  memcpy(dstp_saved + (2 + order ) * dst_linesize, srcp_saved + (3 - order) * src_linesize, bwidth);
179  memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, bwidth);
180  memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, bwidth);
181 
182  /* For the other field choose adaptively between using the previous field
183  or the interpolant from the current field. */
184  prvp = kerndeint->tmp_data[plane] + 5 * psrc_linesize - (1 - order) * psrc_linesize;
185  prvpp = prvp - psrc_linesize;
186  prvppp = prvp - 2 * psrc_linesize;
187  prvp4p = prvp - 4 * psrc_linesize;
188  prvpn = prvp + psrc_linesize;
189  prvpnn = prvp + 2 * psrc_linesize;
190  prvp4n = prvp + 4 * psrc_linesize;
191 
192  srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize;
193  srcpp = srcp - src_linesize;
194  srcppp = srcp - 2 * src_linesize;
195  srcp3p = srcp - 3 * src_linesize;
196  srcp4p = srcp - 4 * src_linesize;
197 
198  srcpn = srcp + src_linesize;
199  srcpnn = srcp + 2 * src_linesize;
200  srcp3n = srcp + 3 * src_linesize;
201  srcp4n = srcp + 4 * src_linesize;
202 
203  dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize;
204 
205  for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) {
206  for (x = 0; x < bwidth; x++) {
207  if (thresh == 0 || n == 0 ||
208  (abs((int)prvp[x] - (int)srcp[x]) > thresh) ||
209  (abs((int)prvpp[x] - (int)srcpp[x]) > thresh) ||
210  (abs((int)prvpn[x] - (int)srcpn[x]) > thresh)) {
211  if (map) {
212  g = x & ~3;
213 
214  if (is_packed_rgb) {
215  AV_WB32(dstp + g, 0xffffffff);
216  x = g + 3;
217  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
218  // y <- 235, u <- 128, y <- 235, v <- 128
219  AV_WB32(dstp + g, 0xeb80eb80);
220  x = g + 3;
221  } else {
222  dstp[x] = plane == 0 ? 235 : 128;
223  }
224  } else {
225  if (is_packed_rgb) {
226  hi = 255;
227  lo = 0;
228  } else if (inlink->format == AV_PIX_FMT_YUYV422) {
229  hi = x & 1 ? 240 : 235;
230  lo = 16;
231  } else {
232  hi = plane == 0 ? 235 : 240;
233  lo = 16;
234  }
235 
236  if (sharp) {
237  if (twoway) {
238  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
239  + 0.170 * ((int)srcp[x] + (int)prvp[x])
240  - 0.116 * ((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
241  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
242  + 0.031 * ((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
243  } else {
244  valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x])
245  + 0.170 * ((int)prvp[x])
246  - 0.116 * ((int)prvppp[x] + (int)prvpnn[x])
247  - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
248  + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]);
249  }
250  dstp[x] = av_clip(valf, lo, hi);
251  } else {
252  if (twoway) {
253  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)srcp[x] + (int)prvp[x])
254  - (int)(srcppp[x]) - (int)(srcpnn[x])
255  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
256  } else {
257  val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)prvp[x])
258  - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
259  }
260  dstp[x] = av_clip(val, lo, hi);
261  }
262  }
263  } else {
264  dstp[x] = srcp[x];
265  }
266  }
267  prvp += 2 * psrc_linesize;
268  prvpp += 2 * psrc_linesize;
269  prvppp += 2 * psrc_linesize;
270  prvpn += 2 * psrc_linesize;
271  prvpnn += 2 * psrc_linesize;
272  prvp4p += 2 * psrc_linesize;
273  prvp4n += 2 * psrc_linesize;
274  srcp += 2 * src_linesize;
275  srcpp += 2 * src_linesize;
276  srcppp += 2 * src_linesize;
277  srcp3p += 2 * src_linesize;
278  srcp4p += 2 * src_linesize;
279  srcpn += 2 * src_linesize;
280  srcpnn += 2 * src_linesize;
281  srcp3n += 2 * src_linesize;
282  srcp4n += 2 * src_linesize;
283  dstp += 2 * dst_linesize;
284  }
285 
286  srcp = inpic->data[plane];
287  dstp = kerndeint->tmp_data[plane];
288  av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h);
289  }
290 
291  avfilter_unref_buffer(inpic);
292  return ff_filter_frame(outlink, outpic);
293 }
294 
295 static const AVFilterPad kerndeint_inputs[] = {
296  {
297  .name = "default",
298  .type = AVMEDIA_TYPE_VIDEO,
299  .filter_frame = filter_frame,
300  .config_props = config_props,
301  .min_perms = AV_PERM_READ,
302  },
303  { NULL }
304 };
305 
306 static const AVFilterPad kerndeint_outputs[] = {
307  {
308  .name = "default",
309  .type = AVMEDIA_TYPE_VIDEO,
310  },
311  { NULL }
312 };
313 
315  .name = "kerndeint",
316  .description = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."),
317  .priv_size = sizeof(KerndeintContext),
318  .init = init,
319  .uninit = uninit,
321 
322  .inputs = kerndeint_inputs,
323  .outputs = kerndeint_outputs,
324 
325  .priv_class = &kerndeint_class,
326 };