123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- #include "libavutil/opt.h"
- #include "libavutil/imgutils.h"
- #include "libavutil/avassert.h"
- #include "avfilter.h"
- #include "internal.h"
- enum TInterlaceMode {
- MODE_MERGE = 0,
- MODE_DROP_EVEN,
- MODE_DROP_ODD,
- MODE_PAD,
- MODE_INTERLEAVE_TOP,
- MODE_INTERLEAVE_BOTTOM,
- MODE_INTERLACEX2,
- MODE_NB,
- };
- typedef struct {
- const AVClass *class;
- enum TInterlaceMode mode;
- int flags;
- int frame;
- int vsub;
- AVFrame *cur;
- AVFrame *next;
- uint8_t *black_data[4];
- 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 const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
- FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
- };
- static int query_formats(AVFilterContext *ctx)
- {
- static const enum AVPixelFormat pix_fmts[] = {
- AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
- AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
- AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
- AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
- AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
- AV_PIX_FMT_NONE
- };
- ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
- return 0;
- }
- static av_cold void uninit(AVFilterContext *ctx)
- {
- TInterlaceContext *tinterlace = ctx->priv;
- av_frame_free(&tinterlace->cur );
- av_frame_free(&tinterlace->next);
- av_freep(&tinterlace->black_data[0]);
- }
- static int config_out_props(AVFilterLink *outlink)
- {
- AVFilterContext *ctx = outlink->src;
- AVFilterLink *inlink = outlink->src->inputs[0];
- const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
- TInterlaceContext *tinterlace = ctx->priv;
- tinterlace->vsub = desc->log2_chroma_h;
- outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
- outlink->w = inlink->w;
- outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
- inlink->h*2 : inlink->h;
- if (tinterlace->mode == MODE_PAD) {
- uint8_t black[4] = { 16, 128, 128, 16 };
- int i, ret;
- if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
- black[0] = black[3] = 0;
- ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
- outlink->w, outlink->h, outlink->format, 1);
- if (ret < 0)
- return ret;
-
- for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
- int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
- memset(tinterlace->black_data[i], black[i],
- tinterlace->black_linesize[i] * 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;
- }
- if (tinterlace->mode == MODE_INTERLACEX2) {
- outlink->time_base.num = inlink->time_base.num;
- outlink->time_base.den = inlink->time_base.den * 2;
- outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
- }
- 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;
- }
- #define FIELD_UPPER 0
- #define FIELD_LOWER 1
- #define FIELD_UPPER_AND_LOWER 2
- 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 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 ? FF_CEIL_RSHIFT(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];
- if (linesize < 0)
- return;
- lines = (lines + (src_field == FIELD_UPPER)) / k;
- if (src_field == FIELD_LOWER)
- srcp += src_linesize[plane];
- if (interleave && dst_field == FIELD_LOWER)
- dstp += dst_linesize[plane];
- if (flags & TINTERLACE_FLAG_VLPF) {
-
-
-
- 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;
- if (h == 1) srcp_below = srcp;
- for (i = 0; i < linesize; i++) {
-
-
-
- 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 filter_frame(AVFilterLink *inlink, AVFrame *picref)
- {
- AVFilterContext *ctx = inlink->dst;
- AVFilterLink *outlink = ctx->outputs[0];
- TInterlaceContext *tinterlace = ctx->priv;
- AVFrame *cur, *next, *out;
- int field, tff, ret;
- av_frame_free(&tinterlace->cur);
- tinterlace->cur = tinterlace->next;
- tinterlace->next = picref;
- cur = tinterlace->cur;
- next = tinterlace->next;
-
- if (!tinterlace->cur)
- return 0;
- switch (tinterlace->mode) {
- case MODE_MERGE:
- out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
- if (!out)
- return AVERROR(ENOMEM);
- av_frame_copy_props(out, cur);
- out->height = outlink->h;
- out->interlaced_frame = 1;
- out->top_field_first = 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, tinterlace->flags);
-
- 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, tinterlace->flags);
- av_frame_free(&tinterlace->next);
- break;
- case MODE_DROP_ODD:
- case MODE_DROP_EVEN:
- out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
- if (!out)
- return AVERROR(ENOMEM);
- av_frame_free(&tinterlace->next);
- break;
- case MODE_PAD:
- out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
- if (!out)
- return AVERROR(ENOMEM);
- av_frame_copy_props(out, cur);
- out->height = outlink->h;
- field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
-
- 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, tinterlace->flags);
-
- 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, tinterlace->flags);
- break;
-
- case MODE_INTERLEAVE_TOP:
- case MODE_INTERLEAVE_BOTTOM:
- tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
- out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
- if (!out)
- return AVERROR(ENOMEM);
- av_frame_copy_props(out, cur);
- out->interlaced_frame = 1;
- out->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,
- tinterlace->flags);
-
- 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,
- tinterlace->flags);
- av_frame_free(&tinterlace->next);
- break;
- case MODE_INTERLACEX2:
-
- out = av_frame_clone(cur);
- if (!out)
- return AVERROR(ENOMEM);
- out->interlaced_frame = 1;
- if (cur->pts != AV_NOPTS_VALUE)
- out->pts = cur->pts*2;
- if ((ret = ff_filter_frame(outlink, out)) < 0)
- return ret;
-
- tff = next->top_field_first;
- out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
- if (!out)
- return AVERROR(ENOMEM);
- av_frame_copy_props(out, next);
- out->interlaced_frame = 1;
- if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
- out->pts = cur->pts + next->pts;
- else
- out->pts = AV_NOPTS_VALUE;
-
- 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,
- tinterlace->flags);
-
- 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,
- tinterlace->flags);
- break;
- default:
- av_assert0(0);
- }
- ret = ff_filter_frame(outlink, out);
- tinterlace->frame++;
- return ret;
- }
- static const AVFilterPad tinterlace_inputs[] = {
- {
- .name = "default",
- .type = AVMEDIA_TYPE_VIDEO,
- .filter_frame = filter_frame,
- },
- { NULL }
- };
- static const AVFilterPad tinterlace_outputs[] = {
- {
- .name = "default",
- .type = AVMEDIA_TYPE_VIDEO,
- .config_props = config_out_props,
- },
- { NULL }
- };
- AVFilter ff_vf_tinterlace = {
- .name = "tinterlace",
- .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
- .priv_size = sizeof(TInterlaceContext),
- .uninit = uninit,
- .query_formats = query_formats,
- .inputs = tinterlace_inputs,
- .outputs = tinterlace_outputs,
- .priv_class = &tinterlace_class,
- };
|