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