FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_yadif.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3  * 2010 James Darnley <james.darnley@gmail.com>
4  *
5  * FFmpeg is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "libavutil/avassert.h"
21 #include "libavutil/cpu.h"
22 #include "libavutil/common.h"
23 #include "libavutil/opt.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 typedef struct ThreadData {
36  int plane;
37  int w, h;
38  int parity;
39  int tff;
40 } ThreadData;
41 
42 #define CHECK(j)\
43  { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
44  + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
45  + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
46  if (score < spatial_score) {\
47  spatial_score= score;\
48  spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
49 
50 /* The is_not_edge argument here controls when the code will enter a branch
51  * which reads up to and including x-3 and x+3. */
52 
53 #define FILTER(start, end, is_not_edge) \
54  for (x = start; x < end; x++) { \
55  int c = cur[mrefs]; \
56  int d = (prev2[0] + next2[0])>>1; \
57  int e = cur[prefs]; \
58  int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
59  int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
60  int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
61  int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
62  int spatial_pred = (c+e) >> 1; \
63  \
64  if (is_not_edge) {\
65  int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
66  + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
67  CHECK(-1) CHECK(-2) }} }} \
68  CHECK( 1) CHECK( 2) }} }} \
69  }\
70  \
71  if (mode < 2) { \
72  int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
73  int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
74  int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
75  int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
76  \
77  diff = FFMAX3(diff, min, -max); \
78  } \
79  \
80  if (spatial_pred > d + diff) \
81  spatial_pred = d + diff; \
82  else if (spatial_pred < d - diff) \
83  spatial_pred = d - diff; \
84  \
85  dst[0] = spatial_pred; \
86  \
87  dst++; \
88  cur++; \
89  prev++; \
90  next++; \
91  prev2++; \
92  next2++; \
93  }
94 
95 static void filter_line_c(void *dst1,
96  void *prev1, void *cur1, void *next1,
97  int w, int prefs, int mrefs, int parity, int mode)
98 {
99  uint8_t *dst = dst1;
100  uint8_t *prev = prev1;
101  uint8_t *cur = cur1;
102  uint8_t *next = next1;
103  int x;
104  uint8_t *prev2 = parity ? prev : cur ;
105  uint8_t *next2 = parity ? cur : next;
106 
107  /* The function is called with the pointers already pointing to data[3] and
108  * with 6 subtracted from the width. This allows the FILTER macro to be
109  * called so that it processes all the pixels normally. A constant value of
110  * true for is_not_edge lets the compiler ignore the if statement. */
111  FILTER(0, w, 1)
112 }
113 
114 #define MAX_ALIGN 8
115 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
116  int w, int prefs, int mrefs, int parity, int mode)
117 {
118  uint8_t *dst = dst1;
119  uint8_t *prev = prev1;
120  uint8_t *cur = cur1;
121  uint8_t *next = next1;
122  int x;
123  uint8_t *prev2 = parity ? prev : cur ;
124  uint8_t *next2 = parity ? cur : next;
125 
126  /* Only edge pixels need to be processed here. A constant value of false
127  * for is_not_edge should let the compiler ignore the whole branch. */
128  FILTER(0, 3, 0)
129 
130  dst = (uint8_t*)dst1 + w - (MAX_ALIGN-1);
131  prev = (uint8_t*)prev1 + w - (MAX_ALIGN-1);
132  cur = (uint8_t*)cur1 + w - (MAX_ALIGN-1);
133  next = (uint8_t*)next1 + w - (MAX_ALIGN-1);
134  prev2 = (uint8_t*)(parity ? prev : cur);
135  next2 = (uint8_t*)(parity ? cur : next);
136 
137  FILTER(w - (MAX_ALIGN-1), w - 3, 1)
138  FILTER(w - 3, w, 0)
139 }
140 
141 
142 static void filter_line_c_16bit(void *dst1,
143  void *prev1, void *cur1, void *next1,
144  int w, int prefs, int mrefs, int parity,
145  int mode)
146 {
147  uint16_t *dst = dst1;
148  uint16_t *prev = prev1;
149  uint16_t *cur = cur1;
150  uint16_t *next = next1;
151  int x;
152  uint16_t *prev2 = parity ? prev : cur ;
153  uint16_t *next2 = parity ? cur : next;
154  mrefs /= 2;
155  prefs /= 2;
156 
157  FILTER(0, w, 1)
158 }
159 
160 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
161  int w, int prefs, int mrefs, int parity, int mode)
162 {
163  uint16_t *dst = dst1;
164  uint16_t *prev = prev1;
165  uint16_t *cur = cur1;
166  uint16_t *next = next1;
167  int x;
168  uint16_t *prev2 = parity ? prev : cur ;
169  uint16_t *next2 = parity ? cur : next;
170  mrefs /= 2;
171  prefs /= 2;
172 
173  FILTER(0, 3, 0)
174 
175  dst = (uint16_t*)dst1 + w - (MAX_ALIGN/2-1);
176  prev = (uint16_t*)prev1 + w - (MAX_ALIGN/2-1);
177  cur = (uint16_t*)cur1 + w - (MAX_ALIGN/2-1);
178  next = (uint16_t*)next1 + w - (MAX_ALIGN/2-1);
179  prev2 = (uint16_t*)(parity ? prev : cur);
180  next2 = (uint16_t*)(parity ? cur : next);
181 
182  FILTER(w - (MAX_ALIGN/2-1), w - 3, 1)
183  FILTER(w - 3, w, 0)
184 }
185 
186 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
187 {
188  YADIFContext *s = ctx->priv;
189  ThreadData *td = arg;
190  int refs = s->cur->linesize[td->plane];
191  int df = (s->csp->comp[td->plane].depth_minus1 + 8) / 8;
192  int pix_3 = 3 * df;
193  int slice_start = (td->h * jobnr ) / nb_jobs;
194  int slice_end = (td->h * (jobnr+1)) / nb_jobs;
195  int y;
196 
197  /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
198  * we need to call the c variant which avoids this for border pixels
199  */
200  for (y = slice_start; y < slice_end; y++) {
201  if ((y ^ td->parity) & 1) {
202  uint8_t *prev = &s->prev->data[td->plane][y * refs];
203  uint8_t *cur = &s->cur ->data[td->plane][y * refs];
204  uint8_t *next = &s->next->data[td->plane][y * refs];
205  uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
206  int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
207  s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
208  next + pix_3, td->w - (3 + MAX_ALIGN/df-1),
209  y + 1 < td->h ? refs : -refs,
210  y ? -refs : refs,
211  td->parity ^ td->tff, mode);
212  s->filter_edges(dst, prev, cur, next, td->w,
213  y + 1 < td->h ? refs : -refs,
214  y ? -refs : refs,
215  td->parity ^ td->tff, mode);
216  } else {
217  memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
218  &s->cur->data[td->plane][y * refs], td->w * df);
219  }
220  }
221  return 0;
222 }
223 
224 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
225  int parity, int tff)
226 {
227  YADIFContext *yadif = ctx->priv;
228  ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
229  int i;
230 
231  for (i = 0; i < yadif->csp->nb_components; i++) {
232  int w = dstpic->width;
233  int h = dstpic->height;
234 
235  if (i == 1 || i == 2) {
236  w = FF_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
237  h = FF_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
238  }
239 
240 
241  td.w = w;
242  td.h = h;
243  td.plane = i;
244 
245  ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
246  }
247 
248  emms_c();
249 }
250 
251 static int return_frame(AVFilterContext *ctx, int is_second)
252 {
253  YADIFContext *yadif = ctx->priv;
254  AVFilterLink *link = ctx->outputs[0];
255  int tff, ret;
256 
257  if (yadif->parity == -1) {
258  tff = yadif->cur->interlaced_frame ?
259  yadif->cur->top_field_first : 1;
260  } else {
261  tff = yadif->parity ^ 1;
262  }
263 
264  if (is_second) {
265  yadif->out = ff_get_video_buffer(link, link->w, link->h);
266  if (!yadif->out)
267  return AVERROR(ENOMEM);
268 
269  av_frame_copy_props(yadif->out, yadif->cur);
270  yadif->out->interlaced_frame = 0;
271  }
272 
273  filter(ctx, yadif->out, tff ^ !is_second, tff);
274 
275  if (is_second) {
276  int64_t cur_pts = yadif->cur->pts;
277  int64_t next_pts = yadif->next->pts;
278 
279  if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
280  yadif->out->pts = cur_pts + next_pts;
281  } else {
282  yadif->out->pts = AV_NOPTS_VALUE;
283  }
284  }
285  ret = ff_filter_frame(ctx->outputs[0], yadif->out);
286 
287  yadif->frame_pending = (yadif->mode&1) && !is_second;
288  return ret;
289 }
290 
292 {
293  AVFilterContext *ctx = link->dst;
294  YADIFContext *yadif = ctx->priv;
295 
296  av_assert0(frame);
297 
298  if (yadif->frame_pending)
299  return_frame(ctx, 1);
300 
301  if (yadif->prev)
302  av_frame_free(&yadif->prev);
303  yadif->prev = yadif->cur;
304  yadif->cur = yadif->next;
305  yadif->next = frame;
306 
307  if (!yadif->cur)
308  return 0;
309 
310  if ((yadif->deint && !yadif->cur->interlaced_frame) || ctx->is_disabled) {
311  yadif->out = av_frame_clone(yadif->cur);
312  if (!yadif->out)
313  return AVERROR(ENOMEM);
314 
315  av_frame_free(&yadif->prev);
316  if (yadif->out->pts != AV_NOPTS_VALUE)
317  yadif->out->pts *= 2;
318  return ff_filter_frame(ctx->outputs[0], yadif->out);
319  }
320 
321  if (!yadif->prev &&
322  !(yadif->prev = av_frame_clone(yadif->cur)))
323  return AVERROR(ENOMEM);
324 
325  yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
326  if (!yadif->out)
327  return AVERROR(ENOMEM);
328 
329  av_frame_copy_props(yadif->out, yadif->cur);
330  yadif->out->interlaced_frame = 0;
331 
332  if (yadif->out->pts != AV_NOPTS_VALUE)
333  yadif->out->pts *= 2;
334 
335  return return_frame(ctx, 0);
336 }
337 
338 static int request_frame(AVFilterLink *link)
339 {
340  AVFilterContext *ctx = link->src;
341  YADIFContext *yadif = ctx->priv;
342 
343  if (yadif->frame_pending) {
344  return_frame(ctx, 1);
345  return 0;
346  }
347 
348  do {
349  int ret;
350 
351  if (yadif->eof)
352  return AVERROR_EOF;
353 
354  ret = ff_request_frame(link->src->inputs[0]);
355 
356  if (ret == AVERROR_EOF && yadif->cur) {
357  AVFrame *next = av_frame_clone(yadif->next);
358 
359  if (!next)
360  return AVERROR(ENOMEM);
361 
362  next->pts = yadif->next->pts * 2 - yadif->cur->pts;
363 
364  filter_frame(link->src->inputs[0], next);
365  yadif->eof = 1;
366  } else if (ret < 0) {
367  return ret;
368  }
369  } while (!yadif->cur);
370 
371  return 0;
372 }
373 
374 static av_cold void uninit(AVFilterContext *ctx)
375 {
376  YADIFContext *yadif = ctx->priv;
377 
378  av_frame_free(&yadif->prev);
379  av_frame_free(&yadif->cur );
380  av_frame_free(&yadif->next);
381 }
382 
384 {
385  static const enum AVPixelFormat pix_fmts[] = {
417  };
418 
420 
421  return 0;
422 }
423 
424 static int config_props(AVFilterLink *link)
425 {
426  AVFilterContext *ctx = link->src;
427  YADIFContext *s = link->src->priv;
428 
429  link->time_base.num = link->src->inputs[0]->time_base.num;
430  link->time_base.den = link->src->inputs[0]->time_base.den * 2;
431  link->w = link->src->inputs[0]->w;
432  link->h = link->src->inputs[0]->h;
433 
434  if(s->mode&1)
435  link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
436 
437  if (link->w < 3 || link->h < 3) {
438  av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
439  return AVERROR(EINVAL);
440  }
441 
442  s->csp = av_pix_fmt_desc_get(link->format);
443  if (s->csp->comp[0].depth_minus1 / 8 == 1) {
444  s->filter_line = filter_line_c_16bit;
445  s->filter_edges = filter_edges_16bit;
446  } else {
447  s->filter_line = filter_line_c;
448  s->filter_edges = filter_edges;
449  }
450 
451  if (ARCH_X86)
453 
454  return 0;
455 }
456 
457 
458 #define OFFSET(x) offsetof(YADIFContext, x)
459 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
460 
461 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
462 
463 static const AVOption yadif_options[] = {
464  { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
465  CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
466  CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
467  CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
468  CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
469 
470  { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
471  CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
472  CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
473  CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
474 
475  { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
476  CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
477  CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
478 
479  {NULL},
480 };
481 
482 AVFILTER_DEFINE_CLASS(yadif);
483 
485  {
486  .name = "default",
487  .type = AVMEDIA_TYPE_VIDEO,
488  .filter_frame = filter_frame,
489  },
490  { NULL }
491 };
492 
494  {
495  .name = "default",
496  .type = AVMEDIA_TYPE_VIDEO,
497  .request_frame = request_frame,
498  .config_props = config_props,
499  },
500  { NULL }
501 };
502 
504  .name = "yadif",
505  .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
506 
507  .priv_size = sizeof(YADIFContext),
508  .priv_class = &yadif_class,
509  .uninit = uninit,
511 
512  .inputs = avfilter_vf_yadif_inputs,
513  .outputs = avfilter_vf_yadif_outputs,
515 };