vf_yadif.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
3  * 2010 James Darnley <james.darnley@gmail.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with Libav; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "libavutil/cpu.h"
23 #include "libavutil/common.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 #include "yadif.h"
30 
31 #undef NDEBUG
32 #include <assert.h>
33 
34 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
35 
36 #define CHECK(j)\
37  { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
38  + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
39  + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
40  if (score < spatial_score) {\
41  spatial_score= score;\
42  spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
43 
44 #define FILTER \
45  for (x = 0; x < w; x++) { \
46  int c = cur[mrefs]; \
47  int d = (prev2[0] + next2[0])>>1; \
48  int e = cur[prefs]; \
49  int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
50  int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
51  int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
52  int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
53  int spatial_pred = (c+e) >> 1; \
54  int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
55  + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
56  \
57  CHECK(-1) CHECK(-2) }} }} \
58  CHECK( 1) CHECK( 2) }} }} \
59  \
60  if (mode < 2) { \
61  int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
62  int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
63  int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
64  int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
65  \
66  diff = FFMAX3(diff, min, -max); \
67  } \
68  \
69  if (spatial_pred > d + diff) \
70  spatial_pred = d + diff; \
71  else if (spatial_pred < d - diff) \
72  spatial_pred = d - diff; \
73  \
74  dst[0] = spatial_pred; \
75  \
76  dst++; \
77  cur++; \
78  prev++; \
79  next++; \
80  prev2++; \
81  next2++; \
82  }
83 
84 static void filter_line_c(uint8_t *dst,
85  uint8_t *prev, uint8_t *cur, uint8_t *next,
86  int w, int prefs, int mrefs, int parity, int mode)
87 {
88  int x;
89  uint8_t *prev2 = parity ? prev : cur ;
90  uint8_t *next2 = parity ? cur : next;
91 
92  FILTER
93 }
94 
95 static void filter_line_c_16bit(uint16_t *dst,
96  uint16_t *prev, uint16_t *cur, uint16_t *next,
97  int w, int prefs, int mrefs, int parity,
98  int mode)
99 {
100  int x;
101  uint16_t *prev2 = parity ? prev : cur ;
102  uint16_t *next2 = parity ? cur : next;
103  mrefs /= 2;
104  prefs /= 2;
105 
106  FILTER
107 }
108 
109 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
110  int parity, int tff)
111 {
112  YADIFContext *yadif = ctx->priv;
113  int y, i;
114 
115  for (i = 0; i < yadif->csp->nb_components; i++) {
116  int w = dstpic->video->w;
117  int h = dstpic->video->h;
118  int refs = yadif->cur->linesize[i];
119  int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
120 
121  if (i == 1 || i == 2) {
122  /* Why is this not part of the per-plane description thing? */
123  w >>= yadif->csp->log2_chroma_w;
124  h >>= yadif->csp->log2_chroma_h;
125  }
126 
127  for (y = 0; y < h; y++) {
128  if ((y ^ parity) & 1) {
129  uint8_t *prev = &yadif->prev->data[i][y * refs];
130  uint8_t *cur = &yadif->cur ->data[i][y * refs];
131  uint8_t *next = &yadif->next->data[i][y * refs];
132  uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
133  int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
134  yadif->filter_line(dst, prev, cur, next, w,
135  y + 1 < h ? refs : -refs,
136  y ? -refs : refs,
137  parity ^ tff, mode);
138  } else {
139  memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
140  &yadif->cur->data[i][y * refs], w * df);
141  }
142  }
143  }
144 
145  emms_c();
146 }
147 
149  int w, int h)
150 {
151  AVFilterBufferRef *picref;
152  int width = FFALIGN(w, 32);
153  int height = FFALIGN(h + 2, 32);
154  int i;
155 
156  picref = ff_default_get_video_buffer(link, perms, width, height);
157 
158  picref->video->w = w;
159  picref->video->h = h;
160 
161  for (i = 0; i < 3; i++)
162  picref->data[i] += picref->linesize[i];
163 
164  return picref;
165 }
166 
167 static int return_frame(AVFilterContext *ctx, int is_second)
168 {
169  YADIFContext *yadif = ctx->priv;
170  AVFilterLink *link = ctx->outputs[0];
171  int tff, ret;
172 
173  if (yadif->parity == -1) {
174  tff = yadif->cur->video->interlaced ?
175  yadif->cur->video->top_field_first : 1;
176  } else {
177  tff = yadif->parity ^ 1;
178  }
179 
180  if (is_second) {
181  yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
182  if (!yadif->out)
183  return AVERROR(ENOMEM);
184 
185  avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
186  yadif->out->video->interlaced = 0;
187  }
188 
189  if (!yadif->csp)
190  yadif->csp = av_pix_fmt_desc_get(link->format);
191  if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
193 
194  filter(ctx, yadif->out, tff ^ !is_second, tff);
195 
196  if (is_second) {
197  int64_t cur_pts = yadif->cur->pts;
198  int64_t next_pts = yadif->next->pts;
199 
200  if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
201  yadif->out->pts = cur_pts + next_pts;
202  } else {
203  yadif->out->pts = AV_NOPTS_VALUE;
204  }
205  }
206  ret = ff_filter_frame(ctx->outputs[0], yadif->out);
207 
208  yadif->frame_pending = (yadif->mode&1) && !is_second;
209  return ret;
210 }
211 
212 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
213 {
214  AVFilterContext *ctx = link->dst;
215  YADIFContext *yadif = ctx->priv;
216 
217  if (yadif->frame_pending)
218  return_frame(ctx, 1);
219 
220  if (yadif->prev)
221  avfilter_unref_buffer(yadif->prev);
222  yadif->prev = yadif->cur;
223  yadif->cur = yadif->next;
224  yadif->next = picref;
225 
226  if (!yadif->cur)
227  return 0;
228 
229  if (yadif->auto_enable && !yadif->cur->video->interlaced) {
230  yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
231  if (!yadif->out)
232  return AVERROR(ENOMEM);
233 
234  avfilter_unref_bufferp(&yadif->prev);
235  if (yadif->out->pts != AV_NOPTS_VALUE)
236  yadif->out->pts *= 2;
237  return ff_filter_frame(ctx->outputs[0], yadif->out);
238  }
239 
240  if (!yadif->prev &&
241  !(yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ)))
242  return AVERROR(ENOMEM);
243 
244  yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
245  link->w, link->h);
246  if (!yadif->out)
247  return AVERROR(ENOMEM);
248 
249  avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
250  yadif->out->video->interlaced = 0;
251 
252  if (yadif->out->pts != AV_NOPTS_VALUE)
253  yadif->out->pts *= 2;
254 
255  return return_frame(ctx, 0);
256 }
257 
258 static int request_frame(AVFilterLink *link)
259 {
260  AVFilterContext *ctx = link->src;
261  YADIFContext *yadif = ctx->priv;
262 
263  if (yadif->frame_pending) {
264  return_frame(ctx, 1);
265  return 0;
266  }
267 
268  do {
269  int ret;
270 
271  if (yadif->eof)
272  return AVERROR_EOF;
273 
274  ret = ff_request_frame(link->src->inputs[0]);
275 
276  if (ret == AVERROR_EOF && yadif->next) {
277  AVFilterBufferRef *next =
279 
280  if (!next)
281  return AVERROR(ENOMEM);
282 
283  next->pts = yadif->next->pts * 2 - yadif->cur->pts;
284 
285  filter_frame(link->src->inputs[0], next);
286  yadif->eof = 1;
287  } else if (ret < 0) {
288  return ret;
289  }
290  } while (!yadif->cur);
291 
292  return 0;
293 }
294 
295 static int poll_frame(AVFilterLink *link)
296 {
297  YADIFContext *yadif = link->src->priv;
298  int ret, val;
299 
300  if (yadif->frame_pending)
301  return 1;
302 
303  val = ff_poll_frame(link->src->inputs[0]);
304  if (val <= 0)
305  return val;
306 
307  //FIXME change API to not requre this red tape
308  if (val == 1 && !yadif->next) {
309  if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
310  return ret;
311  val = ff_poll_frame(link->src->inputs[0]);
312  if (val <= 0)
313  return val;
314  }
315  assert(yadif->next || !val);
316 
317  if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
318  return val;
319 
320  return val * ((yadif->mode&1)+1);
321 }
322 
323 static av_cold void uninit(AVFilterContext *ctx)
324 {
325  YADIFContext *yadif = ctx->priv;
326 
327  if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
328  if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
329  if (yadif->next) avfilter_unref_bufferp(&yadif->next);
330 }
331 
333 {
334  static const enum AVPixelFormat pix_fmts[] = {
355  };
356 
358 
359  return 0;
360 }
361 
362 static av_cold int init(AVFilterContext *ctx, const char *args)
363 {
364  YADIFContext *yadif = ctx->priv;
365 
366  yadif->mode = 0;
367  yadif->parity = -1;
368  yadif->auto_enable = 0;
369  yadif->csp = NULL;
370 
371  if (args)
372  sscanf(args, "%d:%d:%d",
373  &yadif->mode, &yadif->parity, &yadif->auto_enable);
374 
375  yadif->filter_line = filter_line_c;
376 
377  if (ARCH_X86)
378  ff_yadif_init_x86(yadif);
379 
380  av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
381  yadif->mode, yadif->parity, yadif->auto_enable);
382 
383  return 0;
384 }
385 
386 static int config_props(AVFilterLink *link)
387 {
388  link->time_base.num = link->src->inputs[0]->time_base.num;
389  link->time_base.den = link->src->inputs[0]->time_base.den * 2;
390  link->w = link->src->inputs[0]->w;
391  link->h = link->src->inputs[0]->h;
392 
393  return 0;
394 }
395 
397  {
398  .name = "default",
399  .type = AVMEDIA_TYPE_VIDEO,
400  .get_video_buffer = get_video_buffer,
401  .filter_frame = filter_frame,
402  },
403  { NULL }
404 };
405 
407  {
408  .name = "default",
409  .type = AVMEDIA_TYPE_VIDEO,
410  .poll_frame = poll_frame,
411  .request_frame = request_frame,
412  .config_props = config_props,
413  },
414  { NULL }
415 };
416 
418  .name = "yadif",
419  .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
420 
421  .priv_size = sizeof(YADIFContext),
422  .init = init,
423  .uninit = uninit,
425 
426  .inputs = avfilter_vf_yadif_inputs,
427 
428  .outputs = avfilter_vf_yadif_outputs,
429 };