* temporal field interlace filter, ported from MPlayer/libmpcodecs
*/
+#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavutil/avassert.h"
#include "avfilter.h"
MODE_INTERLEAVE_TOP,
MODE_INTERLEAVE_BOTTOM,
MODE_INTERLACEX2,
-};
-
-static const char *tinterlace_mode_str[] = {
- "merge",
- "drop_even",
- "drop_odd",
- "pad",
- "interleave_top",
- "interleave_bottom",
- "interlacex2",
- NULL
+ MODE_NB,
};
typedef struct {
+ const AVClass *class;
enum TInterlaceMode mode; ///< interlace mode selected
+ int flags; ///< flags affecting interlacing algorithm
int frame; ///< number of the output frame
int vsub; ///< chroma vertical subsampling
AVFilterBufferRef *cur;
int black_linesize[4];
} TInterlaceContext;
+#define OFFSET(x) offsetof(TInterlaceContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+#define TINTERLACE_FLAG_VLPF 01
+
+static const AVOption tinterlace_options[] = {
+ {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
+ {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
+ {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
+ {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
+ {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
+ {"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"},
+
+ {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
+ {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
+ {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
+
+ {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(tinterlace);
+
#define FULL_SCALE_YUVJ_FORMATS \
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
static av_cold int init(AVFilterContext *ctx, const char *args)
{
TInterlaceContext *tinterlace = ctx->priv;
- int i;
- char c;
-
- tinterlace->mode = MODE_MERGE;
-
- if (args) {
- if (sscanf(args, "%d%c", (int *)&tinterlace->mode, &c) == 1) {
- if (tinterlace->mode > 6) {
- av_log(ctx, AV_LOG_ERROR,
- "Invalid mode '%s', use an integer between 0 and 6\n", args);
- return AVERROR(EINVAL);
- }
+ static const char *shorthand[] = { "mode", NULL };
- av_log(ctx, AV_LOG_WARNING,
- "Using numeric constant is deprecated, use symbolic values\n");
- } else {
- for (i = 0; tinterlace_mode_str[i]; i++) {
- if (!strcmp(tinterlace_mode_str[i], args)) {
- tinterlace->mode = i;
- break;
- }
- }
- if (!tinterlace_mode_str[i]) {
- av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'\n", args);
- return AVERROR(EINVAL);
- }
- }
- }
+ tinterlace->class = &tinterlace_class;
+ av_opt_set_defaults(tinterlace);
- return 0;
+ return av_opt_set_from_string(tinterlace, args, shorthand, "=", ":");
}
static av_cold void uninit(AVFilterContext *ctx)
{
TInterlaceContext *tinterlace = ctx->priv;
- if (tinterlace->cur ) avfilter_unref_bufferp(&tinterlace->cur );
- if (tinterlace->next) avfilter_unref_bufferp(&tinterlace->next);
+ avfilter_unref_bufferp(&tinterlace->cur );
+ avfilter_unref_bufferp(&tinterlace->next);
+ av_opt_free(tinterlace);
av_freep(&tinterlace->black_data[0]);
}
tinterlace->black_linesize[i] * h);
}
}
- av_log(ctx, AV_LOG_VERBOSE, "mode:%s h:%d -> h:%d\n",
- tinterlace_mode_str[tinterlace->mode], inlink->h, outlink->h);
+ if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
+ && !(tinterlace->mode == MODE_INTERLEAVE_TOP
+ || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
+ av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
+ tinterlace->mode);
+ tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
+ }
+ av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
+ tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
+ inlink->h, outlink->h);
return 0;
}
* @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
+ * @param flags context flags
*/
static inline
void 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)
+ int src_field, int interleave, int dst_field,
+ int flags)
{
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;
+ int h, i;
for (plane = 0; plane < desc->nb_components; plane++) {
int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
srcp += src_linesize[plane];
if (interleave && dst_field == FIELD_LOWER)
dstp += dst_linesize[plane];
- av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
+ if (flags & TINTERLACE_FLAG_VLPF) {
+ // 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.
+ int srcp_linesize = src_linesize[plane] * k;
+ int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
+ for (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 (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;
+ }
+ } else {
+ av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
srcp, src_linesize[plane]*k, linesize, lines);
+ }
}
}
-static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
{
AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
TInterlaceContext *tinterlace = ctx->priv;
+ AVFilterBufferRef *cur, *next, *out;
+ int field, tff, ret;
avfilter_unref_buffer(tinterlace->cur);
tinterlace->cur = tinterlace->next;
tinterlace->next = picref;
- inlink->cur_buf = NULL;
- return 0;
-}
-
-static int end_frame(AVFilterLink *inlink)
-{
- AVFilterContext *ctx = inlink->dst;
- AVFilterLink *outlink = ctx->outputs[0];
- TInterlaceContext *tinterlace = ctx->priv;
- AVFilterBufferRef *cur = tinterlace->cur;
- AVFilterBufferRef *next = tinterlace->next;
- AVFilterBufferRef *out = NULL;
- int field, tff;
+ cur = tinterlace->cur;
+ next = tinterlace->next;
/* we need at least two frames */
if (!tinterlace->cur)
return 0;
case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
* the lower field, generating a double-height video at half framerate */
out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+ if (!out)
+ return AVERROR(ENOMEM);
avfilter_copy_buffer_ref_props(out, cur);
out->video->h = outlink->h;
out->video->interlaced = 1;
copy_picture_field(out->data, out->linesize,
(const uint8_t **)cur->data, cur->linesize,
inlink->format, inlink->w, inlink->h,
- FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
+ FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
/* write even frame lines into the lower field of the new frame */
copy_picture_field(out->data, out->linesize,
(const uint8_t **)next->data, next->linesize,
inlink->format, inlink->w, inlink->h,
- FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
+ FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
avfilter_unref_bufferp(&tinterlace->next);
break;
copy_picture_field(out->data, out->linesize,
(const uint8_t **)cur->data, cur->linesize,
inlink->format, inlink->w, inlink->h,
- FIELD_UPPER_AND_LOWER, 1, field);
+ FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
/* pad with black the other field */
copy_picture_field(out->data, out->linesize,
(const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
inlink->format, inlink->w, inlink->h,
- FIELD_UPPER_AND_LOWER, 1, !field);
+ FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
break;
/* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+ if (!out)
+ return AVERROR(ENOMEM);
avfilter_copy_buffer_ref_props(out, cur);
out->video->interlaced = 1;
out->video->top_field_first = tff;
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);
+ tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
+ tinterlace->flags);
/* 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);
+ tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
+ tinterlace->flags);
avfilter_unref_bufferp(&tinterlace->next);
break;
case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
/* output current frame first */
out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
+ if (!out)
+ return AVERROR(ENOMEM);
out->video->interlaced = 1;
- ff_start_frame(outlink, out);
- ff_draw_slice(outlink, 0, outlink->h, 1);
- ff_end_frame(outlink);
+ if ((ret = ff_filter_frame(outlink, out)) < 0)
+ return ret;
/* output mix of current and next frame */
tff = next->video->top_field_first;
out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+ if (!out)
+ return AVERROR(ENOMEM);
avfilter_copy_buffer_ref_props(out, next);
out->video->interlaced = 1;
copy_picture_field(out->data, out->linesize,
(const uint8_t **)cur->data, cur->linesize,
inlink->format, inlink->w, inlink->h,
- tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
+ tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
+ tinterlace->flags);
/* write next frame first field lines into the first field of the new frame */
copy_picture_field(out->data, out->linesize,
(const uint8_t **)next->data, next->linesize,
inlink->format, inlink->w, inlink->h,
- tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
+ tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
+ tinterlace->flags);
break;
+ default:
+ av_assert0(0);
}
- ff_start_frame(outlink, out);
- ff_draw_slice(outlink, 0, outlink->h, 1);
- ff_end_frame(outlink);
-
+ ret = ff_filter_frame(outlink, out);
tinterlace->frame++;
- return 0;
+ return ret;
}
static int request_frame(AVFilterLink *outlink)
return 0;
}
-static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
-
static const AVFilterPad tinterlace_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
- .start_frame = start_frame,
- .draw_slice = null_draw_slice,
- .end_frame = end_frame,
+ .filter_frame = filter_frame,
},
{ NULL }
};
.query_formats = query_formats,
.inputs = tinterlace_inputs,
.outputs = tinterlace_outputs,
+ .priv_class = &tinterlace_class,
};