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