[FFmpeg-devel] [PATCH] vf_tinterlace: add vertical low-pass-filter option to mode 4 and 5

Stefano Sabatini stefasab at gmail.com
Sat Dec 22 13:10:08 CET 2012


On date Friday 2012-12-21 12:58:07 +0000, Mark Himsley encoded:
> On 19/12/2012 17:17, Mark Himsley wrote:
> > Low-pass filtering is required when creating an interlaced destination
> > from a progressive source which contains high-frequency vertical detail.
> > Filtering will reduce interlace 'twitter' and Moire patterning.
> 
> minor update to correct the consts.

Sorry for the slow reply.

> 
> -- 
> Mark

> diff --git a/doc/filters.texi b/doc/filters.texi
> index a2b4fbe..0198f9a 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -3900,9 +3900,28 @@ Perform various types of temporal field interlacing.
>  Frames are counted starting from 1, so the first input frame is
>  considered odd.
>  
> -This filter accepts a single option @option{mode} specifying the mode,
> -which can be specified either by specyfing @code{mode=VALUE} either
> -specifying the value alone. Available values are:
> +This filter accepts upto two options in the form of @var{key}=@var{value}
> +pairs separated by ":".
> +Alternatively, the @var{mode} option can be specified as a value alone,
> +optionally followed by a ":" and a @var{key}=@var{value} option.
> +
> +A description of the accepted options follows.
> +
> + at table @option
> +
> + at item mode
> +Specifies the mode of the interlacing. This option can also be
> +specified as a value alone. See below for a list of values for this option.
> +

> + at item filter
> +Specifies whether a vertical low-pass filtering will be applied.
> +The default value is @code{0}, meaning no filtering will be applied.
> +The value of @code{1} will enable vertical low-pass filtering for
> + at var{mode} @code{4} and @code{5}.

As an alternative we might consider adding flags=+low_pass_filter

> +
> + at end table
> +
> +Available values for @var{mode} are:
>  
>  @table @samp
>  @item merge, 0
> diff --git a/libavfilter/vf_tinterlace.c b/libavfilter/vf_tinterlace.c
> index 843a66c..df88674 100644
> --- a/libavfilter/vf_tinterlace.c
> +++ b/libavfilter/vf_tinterlace.c
> @@ -45,6 +45,7 @@ enum TInterlaceMode {
>  typedef struct {
>      const AVClass *class;
>      enum TInterlaceMode mode;   ///< interlace mode selected
> +    int filter;                 ///< interlace filtering enabled
>      int frame;                  ///< number of the output frame
>      int vsub;                   ///< chroma vertical subsampling
>      AVFilterBufferRef *cur;
> @@ -65,6 +66,7 @@ static const AVOption tinterlace_options[] = {
>      {"interleave_top",    "interleave top and bottom fields",             0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP},    INT_MIN, INT_MAX, FLAGS, "mode"},
>      {"interleave_bottom", "interleave bottom and top fields",             0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
>      {"interlacex2",       "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2},       INT_MIN, INT_MAX, FLAGS, "mode"},
> +    {"filter",            "enable interlace filtering", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "filter"},
>  
>      {NULL}
>  };
> @@ -142,8 +144,13 @@ static int config_out_props(AVFilterLink *outlink)
>                     tinterlace->black_linesize[i] * h);
>          }
>      }
> -    av_log(ctx, AV_LOG_VERBOSE, "mode:%d h:%d -> h:%d\n",
> -           tinterlace->mode, inlink->h, outlink->h);
> +    if (tinterlace->filter && !(tinterlace->mode == MODE_INTERLEAVE_TOP
> +                             || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
> +        av_log(ctx, AV_LOG_WARNING, "filter not required for mode:%d\n", tinterlace->mode);
> +        tinterlace->filter = 0;
> +    }
> +    av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
> +           tinterlace->mode, tinterlace->filter ? "on" : "off", inlink->h, outlink->h);
>  
>      return 0;
>  }
> @@ -189,6 +196,66 @@ void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
>      }
>  }
>  
> +/**

> + * Copy with low-pass filter picture field from src to dst.

> + * Low-pass filtering is required when creating an interlaced destination from
> + * a progressive source which contains high-frequency vertical detail.
> + * Filtering will reduce interlace 'twitter' and Moiré patterning.

Note: this may be moved to user docs.

> + * Other than low-pass filtering, this functions identicaly to
> + * copy_picture_field() above.

Note: would it make sense to move this to a separate filter?

> + *
> + * @param src_field copy from upper, lower field or both
> + * @param interleave leave a padding line between each copied line
> + * @param dst_field copy to upper or lower field,
> + *        only meaningful when interleave is selected
> + */
> +static inline
> +void filter_copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
> +                               const uint8_t *src[4], int src_linesize[4],
> +                               enum AVPixelFormat format, int w, int src_h,
> +                               int src_field, int interleave, int dst_field)
> +{
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
> +    int plane, vsub = desc->log2_chroma_h;
> +    int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
> +
> +    for (plane = 0; plane < desc->nb_components; plane++) {
> +        int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
> +        int linesize = av_image_get_linesize(format, w, plane);
> +        uint8_t *dstp = dst[plane];
> +        const uint8_t *srcp = src[plane];
> +        int srcp_linesize;
> +        int dstp_linesize;
> +
> +        if (linesize < 0)
> +            return;
> +
> +        lines /= k;
> +        if (src_field == FIELD_LOWER)
> +            srcp += src_linesize[plane];
> +        if (interleave && dst_field == FIELD_LOWER)
> +            dstp += dst_linesize[plane];
> +
> +        srcp_linesize = src_linesize[plane] * k;
> +        dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
> +
> +        for (int h = lines; h > 0; h--) {
> +            const uint8_t *srcp_above = srcp - src_linesize[plane];
> +            const uint8_t *srcp_below = srcp + src_linesize[plane];
> +            if (h == lines) srcp_above = srcp; // there is no line above
> +            if (h == 1) srcp_below = srcp;     // there is no line below
> +            for (int i = 0; i < linesize; i++) {
> +                // this calculation is an integer representation of
> +                // '0.5 * current + 0.25 * above + 0.25 + below'
> +                // '1 +' is for rounding
> +                dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
> +            }
> +            dstp += dstp_linesize;
> +            srcp += srcp_linesize;
> +        }
> +    }
> +}
> +
>  static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
>  {
>      AVFilterContext *ctx = inlink->dst;
> @@ -267,17 +334,29 @@ static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
>          avfilter_copy_buffer_ref_props(out, cur);
>          out->video->interlaced = 1;
>          out->video->top_field_first = tff;
> -
> -        /* copy upper/lower field from cur */
> -        copy_picture_field(out->data, out->linesize,
> -                           (const uint8_t **)cur->data, cur->linesize,
> -                           inlink->format, inlink->w, inlink->h,
> -                           tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
> -        /* copy lower/upper field from next */
> -        copy_picture_field(out->data, out->linesize,
> -                           (const uint8_t **)next->data, next->linesize,
> -                           inlink->format, inlink->w, inlink->h,
> -                           tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);

> +        if (tinterlace->filter) {
> +            /* copy upper/lower field from cur */
> +            filter_copy_picture_field(out->data, out->linesize,
> +                                      (const uint8_t **)cur->data, cur->linesize,
> +                                      inlink->format, inlink->w, inlink->h,
> +                                      tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
> +            /* copy lower/upper field from next */
> +            filter_copy_picture_field(out->data, out->linesize,
> +                                      (const uint8_t **)next->data, next->linesize,
> +                                      inlink->format, inlink->w, inlink->h,
> +                                      tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
> +        } else {
> +            /* copy upper/lower field from cur */
> +            copy_picture_field(out->data, out->linesize,
> +                               (const uint8_t **)cur->data, cur->linesize,
> +                               inlink->format, inlink->w, inlink->h,
> +                               tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
> +            /* copy lower/upper field from next */
> +            copy_picture_field(out->data, out->linesize,
> +                               (const uint8_t **)next->data, next->linesize,
> +                               inlink->format, inlink->w, inlink->h,
> +                               tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
> +        }

simpler: add a parameter to copy_picture_field so you avoid
duplication here.
-- 
FFmpeg = Fancy & Free Multipurpose Patchable Extravagant Gadget


More information about the ffmpeg-devel mailing list