FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_tinterlace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
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  * temporal field interlace filter, ported from MPlayer/libmpcodecs
26  */
27 
28 #include "libavutil/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/avassert.h"
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "tinterlace.h"
34 
35 #define OFFSET(x) offsetof(TInterlaceContext, x)
36 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
37 #define TINTERLACE_FLAG_VLPF 01
38 #define TINTERLACE_FLAG_EXACT_TB 2
39 
40 static const AVOption tinterlace_options[] = {
41  {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
42  {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
43  {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
44  {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
45  {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
46  {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
47  {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
48  {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
49 
50  {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
51  {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
52  {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
53  {"exact_tb", "force a timebase which can represent timestamps exactly", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_EXACT_TB}, INT_MIN, INT_MAX, FLAGS, "flags" },
54 
55  {NULL}
56 };
57 
58 AVFILTER_DEFINE_CLASS(tinterlace);
59 
60 #define FULL_SCALE_YUVJ_FORMATS \
61  AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
62 
63 static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
65 };
66 
67 static const AVRational standard_tbs[] = {
68  {1, 25},
69  {1, 30},
70  {1001, 30000},
71 };
72 
74 {
75  static const enum AVPixelFormat pix_fmts[] = {
82  };
83 
84  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
85  if (!fmts_list)
86  return AVERROR(ENOMEM);
87  return ff_set_common_formats(ctx, fmts_list);
88 }
89 
90 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
91  const uint8_t *srcp_above, const uint8_t *srcp_below)
92 {
93  int i;
94  for (i = 0; i < width; i++) {
95  // this calculation is an integer representation of
96  // '0.5 * current + 0.25 * above + 0.25 * below'
97  // '1 +' is for rounding.
98  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
99  }
100 }
101 
102 static av_cold void uninit(AVFilterContext *ctx)
103 {
104  TInterlaceContext *tinterlace = ctx->priv;
105 
106  av_frame_free(&tinterlace->cur );
107  av_frame_free(&tinterlace->next);
108  av_freep(&tinterlace->black_data[0]);
109 }
110 
111 static int config_out_props(AVFilterLink *outlink)
112 {
113  AVFilterContext *ctx = outlink->src;
114  AVFilterLink *inlink = outlink->src->inputs[0];
115  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
116  TInterlaceContext *tinterlace = ctx->priv;
117  int i;
118 
119  tinterlace->vsub = desc->log2_chroma_h;
120  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
121  outlink->w = inlink->w;
122  outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
123  inlink->h*2 : inlink->h;
124 
125  if (tinterlace->mode == MODE_PAD) {
126  uint8_t black[4] = { 16, 128, 128, 16 };
127  int i, ret;
129  black[0] = black[3] = 0;
130  ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
131  outlink->w, outlink->h, outlink->format, 1);
132  if (ret < 0)
133  return ret;
134 
135  /* fill black picture with black */
136  for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
137  int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
138  memset(tinterlace->black_data[i], black[i],
139  tinterlace->black_linesize[i] * h);
140  }
141  }
142  if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
143  && !(tinterlace->mode == MODE_INTERLEAVE_TOP
144  || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
145  av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
146  tinterlace->mode);
147  tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
148  }
149  tinterlace->preout_time_base = inlink->time_base;
150  if (tinterlace->mode == MODE_INTERLACEX2) {
151  tinterlace->preout_time_base.den *= 2;
152  outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
153  outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2});
154  } else if (tinterlace->mode != MODE_PAD) {
155  outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
156  outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
157  }
158 
159  for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
160  if (!av_cmp_q(standard_tbs[i], outlink->time_base))
161  break;
162  }
163  if (i == FF_ARRAY_ELEMS(standard_tbs) ||
164  (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
165  outlink->time_base = tinterlace->preout_time_base;
166 
167  if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
168  tinterlace->lowpass_line = lowpass_line_c;
169  if (ARCH_X86)
170  ff_tinterlace_init_x86(tinterlace);
171  }
172 
173  av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
174  tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
175  inlink->h, outlink->h);
176 
177  return 0;
178 }
179 
180 #define FIELD_UPPER 0
181 #define FIELD_LOWER 1
182 #define FIELD_UPPER_AND_LOWER 2
183 
184 /**
185  * Copy picture field from src to dst.
186  *
187  * @param src_field copy from upper, lower field or both
188  * @param interleave leave a padding line between each copied line
189  * @param dst_field copy to upper or lower field,
190  * only meaningful when interleave is selected
191  * @param flags context flags
192  */
193 static inline
195  uint8_t *dst[4], int dst_linesize[4],
196  const uint8_t *src[4], int src_linesize[4],
197  enum AVPixelFormat format, int w, int src_h,
198  int src_field, int interleave, int dst_field,
199  int flags)
200 {
201  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
202  int hsub = desc->log2_chroma_w;
203  int plane, vsub = desc->log2_chroma_h;
204  int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
205  int h;
206 
207  for (plane = 0; plane < desc->nb_components; plane++) {
208  int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
209  int cols = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT( w, hsub) : w;
210  uint8_t *dstp = dst[plane];
211  const uint8_t *srcp = src[plane];
212 
213  lines = (lines + (src_field == FIELD_UPPER)) / k;
214  if (src_field == FIELD_LOWER)
215  srcp += src_linesize[plane];
216  if (interleave && dst_field == FIELD_LOWER)
217  dstp += dst_linesize[plane];
218  if (flags & TINTERLACE_FLAG_VLPF) {
219  // Low-pass filtering is required when creating an interlaced destination from
220  // a progressive source which contains high-frequency vertical detail.
221  // Filtering will reduce interlace 'twitter' and Moire patterning.
222  int srcp_linesize = src_linesize[plane] * k;
223  int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
224  for (h = lines; h > 0; h--) {
225  const uint8_t *srcp_above = srcp - src_linesize[plane];
226  const uint8_t *srcp_below = srcp + src_linesize[plane];
227  if (h == lines) srcp_above = srcp; // there is no line above
228  if (h == 1) srcp_below = srcp; // there is no line below
229 
230  tinterlace->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
231  dstp += dstp_linesize;
232  srcp += srcp_linesize;
233  }
234  } else {
235  av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
236  srcp, src_linesize[plane]*k, cols, lines);
237  }
238  }
239 }
240 
241 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
242 {
243  AVFilterContext *ctx = inlink->dst;
244  AVFilterLink *outlink = ctx->outputs[0];
245  TInterlaceContext *tinterlace = ctx->priv;
246  AVFrame *cur, *next, *out;
247  int field, tff, ret;
248 
249  av_frame_free(&tinterlace->cur);
250  tinterlace->cur = tinterlace->next;
251  tinterlace->next = picref;
252 
253  cur = tinterlace->cur;
254  next = tinterlace->next;
255  /* we need at least two frames */
256  if (!tinterlace->cur)
257  return 0;
258 
259  switch (tinterlace->mode) {
260  case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
261  * the lower field, generating a double-height video at half framerate */
262  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
263  if (!out)
264  return AVERROR(ENOMEM);
265  av_frame_copy_props(out, cur);
266  out->height = outlink->h;
267  out->interlaced_frame = 1;
268  out->top_field_first = 1;
269 
270  /* write odd frame lines into the upper field of the new frame */
271  copy_picture_field(tinterlace, out->data, out->linesize,
272  (const uint8_t **)cur->data, cur->linesize,
273  inlink->format, inlink->w, inlink->h,
274  FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
275  /* write even frame lines into the lower field of the new frame */
276  copy_picture_field(tinterlace, out->data, out->linesize,
277  (const uint8_t **)next->data, next->linesize,
278  inlink->format, inlink->w, inlink->h,
279  FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
280  av_frame_free(&tinterlace->next);
281  break;
282 
283  case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
284  case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
285  out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
286  if (!out)
287  return AVERROR(ENOMEM);
288  av_frame_free(&tinterlace->next);
289  break;
290 
291  case MODE_PAD: /* expand each frame to double height, but pad alternate
292  * lines with black; framerate unchanged */
293  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
294  if (!out)
295  return AVERROR(ENOMEM);
296  av_frame_copy_props(out, cur);
297  out->height = outlink->h;
298 
299  field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
300  /* copy upper and lower fields */
301  copy_picture_field(tinterlace, out->data, out->linesize,
302  (const uint8_t **)cur->data, cur->linesize,
303  inlink->format, inlink->w, inlink->h,
304  FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
305  /* pad with black the other field */
306  copy_picture_field(tinterlace, out->data, out->linesize,
307  (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
308  inlink->format, inlink->w, inlink->h,
309  FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
310  break;
311 
312  /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
313  * halving the frame rate and preserving image height */
314  case MODE_INTERLEAVE_TOP: /* top field first */
315  case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
316  tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
317  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
318  if (!out)
319  return AVERROR(ENOMEM);
320  av_frame_copy_props(out, cur);
321  out->interlaced_frame = 1;
322  out->top_field_first = tff;
323 
324  /* copy upper/lower field from cur */
325  copy_picture_field(tinterlace, out->data, out->linesize,
326  (const uint8_t **)cur->data, cur->linesize,
327  inlink->format, inlink->w, inlink->h,
328  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
329  tinterlace->flags);
330  /* copy lower/upper field from next */
331  copy_picture_field(tinterlace, out->data, out->linesize,
332  (const uint8_t **)next->data, next->linesize,
333  inlink->format, inlink->w, inlink->h,
334  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
335  tinterlace->flags);
336  av_frame_free(&tinterlace->next);
337  break;
338  case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
339  /* output current frame first */
340  out = av_frame_clone(cur);
341  if (!out)
342  return AVERROR(ENOMEM);
343  out->interlaced_frame = 1;
344  if (cur->pts != AV_NOPTS_VALUE)
345  out->pts = cur->pts*2;
346 
347  out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
348  if ((ret = ff_filter_frame(outlink, out)) < 0)
349  return ret;
350 
351  /* output mix of current and next frame */
352  tff = next->top_field_first;
353  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
354  if (!out)
355  return AVERROR(ENOMEM);
356  av_frame_copy_props(out, next);
357  out->interlaced_frame = 1;
358  out->top_field_first = !tff;
359 
360  if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
361  out->pts = cur->pts + next->pts;
362  else
363  out->pts = AV_NOPTS_VALUE;
364  /* write current frame second field lines into the second field of the new frame */
365  copy_picture_field(tinterlace, out->data, out->linesize,
366  (const uint8_t **)cur->data, cur->linesize,
367  inlink->format, inlink->w, inlink->h,
368  tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
369  tinterlace->flags);
370  /* write next frame first field lines into the first field of the new frame */
371  copy_picture_field(tinterlace, out->data, out->linesize,
372  (const uint8_t **)next->data, next->linesize,
373  inlink->format, inlink->w, inlink->h,
374  tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
375  tinterlace->flags);
376  break;
377  default:
378  av_assert0(0);
379  }
380 
381  out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
382  ret = ff_filter_frame(outlink, out);
383  tinterlace->frame++;
384 
385  return ret;
386 }
387 
388 static const AVFilterPad tinterlace_inputs[] = {
389  {
390  .name = "default",
391  .type = AVMEDIA_TYPE_VIDEO,
392  .filter_frame = filter_frame,
393  },
394  { NULL }
395 };
396 
397 static const AVFilterPad tinterlace_outputs[] = {
398  {
399  .name = "default",
400  .type = AVMEDIA_TYPE_VIDEO,
401  .config_props = config_out_props,
402  },
403  { NULL }
404 };
405 
407  .name = "tinterlace",
408  .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
409  .priv_size = sizeof(TInterlaceContext),
410  .uninit = uninit,
412  .inputs = tinterlace_inputs,
413  .outputs = tinterlace_outputs,
414  .priv_class = &tinterlace_class,
415 };