PR #25935 opened by kierank URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25935 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25935.patch
Progressive to interlaced conversion with the perceptually optimal interlacing filter of BBC R&D White Paper WHP 315 >From 0c4c7b746f0fdddcebd2f9268b7a254ce5f50985 Mon Sep 17 00:00:00 2001 From: Phillip Blucas <[email protected]> Date: Wed, 11 Nov 2026 01:56:24 +0000 Subject: [PATCH 1/3] avfilter: add vtinterlace filter Progressive to interlaced conversion with the perceptually optimal interlacing filter of BBC R&D White Paper WHP 315 (K.C. Noland, "Optimal Interlacing using Human Sensitivity Measurements", 2016): a 19-line by 5-frame linear vertical-temporal low-pass applied to the progressive sequence before alternate lines are discarded, with a pass-band shaped by the measured human sensitivity to interlacing aliases (WHP 230). Output behaviour has been validated against the EBU TECH 3384 test patterns and measurement methodology. 2N progressive frames become N woven frames at half rate. Output frame n uses input frames 2n-2..2n+3 (repeated at the stream edges); each field line is the VT-filtered progressive line of the frame supplying that field. Vertically subsampled chroma is resampled onto the interlaced siting grid inside the same convolution (fractionally delayed variants of the kernel), honouring the input chroma location. The same filter is applied to all planes, including alpha. Ported from https://github.com/ifb/vapoursynth-interlace 41bb21b (relicensed to LGPL-2.1-or-later in f69ff68). --- Changelog | 1 + doc/filters.texi | 57 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_vtinterlace.c | 565 +++++++++++++++++++++++++++++++ libavfilter/vtinterlace_coeffs.h | 111 ++++++ libavfilter/vtinterlacedsp.h | 67 ++++ 7 files changed, 803 insertions(+) create mode 100644 libavfilter/vf_vtinterlace.c create mode 100644 libavfilter/vtinterlace_coeffs.h create mode 100644 libavfilter/vtinterlacedsp.h diff --git a/Changelog b/Changelog index bce79b181f7..ae2cbd10525 100644 --- a/Changelog +++ b/Changelog @@ -6,6 +6,7 @@ version <next>: - LCEVC raw demuxer and muxer - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder - PCM PAMF decoder +- vtinterlace filter version 9.1: - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 muxers and demuxers diff --git a/doc/filters.texi b/doc/filters.texi index ec23628067a..2a224a64d35 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -26724,6 +26724,63 @@ If set to 1, force the output to terminate when the shortest input terminates. Default value is 0. @end table +@section vtinterlace + +Convert progressive video to interlaced video, halving the frame rate. + +Each pair of progressive input frames is woven into one interlaced output +frame. Before the alternate lines are discarded, the progressive sequence is +passed through the perceptually optimal vertical-temporal low-pass filter of +BBC R&D White Paper WHP 315 (K.C. Noland, "Optimal Interlacing using Human +Sensitivity Measurements", 2016): a 19-line by 5-frame filter whose pass-band +is shaped by the measured human sensitivity to interlacing aliases. Output has +been validated against the EBU TECH 3384 test patterns and measurement +methodology. + +The same filter is applied to all planes. Vertically subsampled chroma +(e.g. 4:2:0) is resampled onto the interlaced chroma siting grid within the +same filter, honouring the input frame's chroma location (left is assumed +when it is unspecified); the output chroma location is set accordingly. +Vertical chroma subsampling greater than 2x is not supported and all plane +heights must be even. + +An odd trailing input frame is dropped. Output frames take the timestamp of +the first frame of each pair and twice its duration. + +Interlace last: do all scaling, colour conversion and tone mapping on the +progressive video first, and never resize after interlacing. For example, +convert 2160p50 to 1080i25 by scaling to 1080p50 and then applying this filter. + +The filter accepts the following options: + +@table @option +@item tff +Set the field order of the output. If set to 1, the temporally first frame +of each pair supplies the top field and the output is top field first; +otherwise it supplies the bottom field. Default value is 1. + +@item filter +If set to 1, apply the vertical-temporal prefilter. If set to 0, fields are +woven without any filtering, which aliases; only useful as a worst-case +reference. Default value is 1. +@end table + +@subsection Examples + +@itemize +@item +Convert 1080p50 to 1080i25 top field first in 4:2:2 10-bit: +@example +format=yuv422p10,vtinterlace +@end example + +@item +Convert 2160p50 to 1080i25: +@example +scale=1920:1080:flags=lanczos,vtinterlace +@end example +@end itemize + @section w3fdif Deinterlace the input video ("w3fdif" stands for "Weston 3 Field diff --git a/libavfilter/Makefile b/libavfilter/Makefile index dc5c083f013..1393e8fd8bd 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -581,6 +581,7 @@ OBJS-$(CONFIG_FRC_AMF_FILTER) += vf_frc_amf.o vf_amf_common.o OBJS-$(CONFIG_VQE_AMF_FILTER) += vf_vqe_amf.o vf_amf_common.o OBJS-$(CONFIG_VPP_QSV_FILTER) += vf_vpp_qsv.o OBJS-$(CONFIG_VSTACK_FILTER) += vf_stack.o framesync.o +OBJS-$(CONFIG_VTINTERLACE_FILTER) += vf_vtinterlace.o OBJS-$(CONFIG_W3FDIF_FILTER) += vf_w3fdif.o OBJS-$(CONFIG_WAVEFORM_FILTER) += vf_waveform.o OBJS-$(CONFIG_WEAVE_FILTER) += vf_weave.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 9f07bf83dce..24d4462be3a 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -543,6 +543,7 @@ extern const FFFilter ff_vf_vignette; extern const FFFilter ff_vf_vmafmotion; extern const FFFilter ff_vf_vpp_qsv; extern const FFFilter ff_vf_vstack; +extern const FFFilter ff_vf_vtinterlace; extern const FFFilter ff_vf_w3fdif; extern const FFFilter ff_vf_waveform; extern const FFFilter ff_vf_weave; diff --git a/libavfilter/vf_vtinterlace.c b/libavfilter/vf_vtinterlace.c new file mode 100644 index 00000000000..e9773e76718 --- /dev/null +++ b/libavfilter/vf_vtinterlace.c @@ -0,0 +1,565 @@ +/* + * Copyright (c) 2026 Phillip Blucas + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Progressive to interlaced conversion with the vertical-temporal + * prefilter of BBC R&D White Paper WHP 315 (19 lines x 5 frames). + */ + +#include <stdint.h> +#include <string.h> + +#include "config.h" + +#include "libavutil/common.h" +#include "libavutil/intfloat.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" + +#include "avfilter.h" +#include "filters.h" +#include "formats.h" +#include "video.h" +#include "vtinterlace_coeffs.h" +#include "vtinterlacedsp.h" + +/* input frames needed per output frame: dt -2..+2 around both field centers */ +#define WINDOW 6 + +typedef void (*filter_line_fn)(void *dst, const void *const *src, int width, + int peak, const void *coefs); + +typedef struct VTInterlaceContext { + const AVClass *class; + int tff; + int filter; + + int nb_planes; + int planewidth[4]; + int planeheight[4]; + int bps; ///< bytes per sample + int peak; + int chroma_sited; ///< vertically subsampled chroma: correct its siting + enum VTInterlaceKind kind; + VTInterlaceDSPContext dsp; + filter_line_fn filter_line_c; + + AVFrame *frames[WINDOW]; ///< input frame i lives in frames[i % WINDOW] + int64_t nb_in; + int64_t nb_out; + int eof; + int eof_status; + int64_t eof_pts; +} VTInterlaceContext; + +typedef struct ThreadData { + AVFrame *out; + const AVFrame *src[WINDOW]; ///< src[k] = input frame 2n+k-2, edges repeated + const void *chroma_coefs[2]; +} ThreadData; + +#define OFFSET(x) offsetof(VTInterlaceContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption vtinterlace_options[] = { + { "tff", "output top field first", OFFSET(tff), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS }, + { "filter", "apply the vertical-temporal prefilter", OFFSET(filter), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(vtinterlace); + +/* [shift][taps], ordered as enum VTInterlaceShift */ +static const int16_t coefs_i16[VTI_SHIFT_NB][VTI_SRC_PTRS] = { + { VT_COEFS_M375_I16 }, { VT_COEFS_M125_I16 }, { VT_COEFS_I16 }, + { VT_COEFS_P125_I16 }, { VT_COEFS_P375_I16 }, +}; +static const float coefs_f32[VTI_SHIFT_NB][VTI_SRC_PTRS] = { + { VT_COEFS_M375_F32 }, { VT_COEFS_M125_F32 }, { VT_COEFS_F32 }, + { VT_COEFS_P125_F32 }, { VT_COEFS_P375_F32 }, +}; + +const void *ff_vtinterlace_coefs(enum VTInterlaceKind kind, enum VTInterlaceShift shift) +{ + return kind == VTI_KIND_U8 || kind == VTI_KIND_U16 ? (const void *)coefs_i16[shift] + : (const void *)coefs_f32[shift]; +} + +/* interlaced 4:2:0 sites field chroma at 1/4 and 3/4 between the field's luma + * lines; the shift from the progressive position depends on the progressive + * vertical siting class of the chroma location. In chroma rows, + * y[r] = x[r+s]. */ +static const enum VTInterlaceShift chroma_shift[3][2] = { /* [vclass][bottom] */ + { VTI_SHIFT_M125, VTI_SHIFT_P125 }, /* left/center: chroma between lines */ + { VTI_SHIFT_P125, VTI_SHIFT_P375 }, /* topleft/top: co-sited with line 0 */ + { VTI_SHIFT_M375, VTI_SHIFT_M125 }, /* bottomleft/bottom */ +}; + +#define VT_TILE 512 + +#define DEFINE_FILTER_LINE_INT(name, pixel) \ +static void filter_line_##name##_c(void *dstv, const void *const *srcv, \ + int width, int peak, const void *coefsv) \ +{ \ + pixel *dst = dstv; \ + const pixel *const *src = (const pixel *const *)srcv; \ + const int16_t *coefs = coefsv; \ + \ + for (int x0 = 0; x0 < width; x0 += VT_TILE) { \ + const int n = FFMIN(width - x0, VT_TILE); \ + int32_t acc[VT_TILE]; \ + \ + for (int x = 0; x < n; x++) \ + acc[x] = 0; \ + for (int i = 0; i < VT_TAPS_T * VT_TAPS_V; i++) { \ + const pixel *s = src[i] + x0; \ + const int32_t c = coefs[i]; \ + for (int x = 0; x < n; x++) \ + acc[x] += c * s[x]; \ + } \ + for (int x = 0; x < n; x++) { \ + const int32_t v = (acc[x] + (1 << (VT_COEF_BITS - 1))) >> VT_COEF_BITS; \ + dst[x0 + x] = av_clip(v, 0, peak); \ + } \ + } \ +} + +DEFINE_FILTER_LINE_INT(u8, uint8_t) +DEFINE_FILTER_LINE_INT(u16, uint16_t) + +static void filter_line_f32_c(void *dstv, const void *const *srcv, int width, + int peak, const void *coefsv) +{ + float *dst = dstv; + const float *const *src = (const float *const *)srcv; + const float *coefs = coefsv; + + for (int x0 = 0; x0 < width; x0 += VT_TILE) { + const int n = FFMIN(width - x0, VT_TILE); + float acc[VT_TILE]; + + for (int x = 0; x < n; x++) + acc[x] = 0; + for (int i = 0; i < VT_TAPS_T * VT_TAPS_V; i++) { + const float *s = src[i] + x0; + const float c = coefs[i]; + for (int x = 0; x < n; x++) + acc[x] += c * s[x]; + } + for (int x = 0; x < n; x++) + dst[x0 + x] = acc[x]; + } +} + +/* soft binary16 conversion, round-to-nearest-even; matches F16C for finite values */ +static float f16_to_f32(uint16_t h) +{ + const uint32_t sign = (uint32_t)(h & 0x8000) << 16; + const uint32_t em = h & 0x7fff; + uint32_t bits; + + if (em >= 0x7c00) /* inf/nan */ + bits = sign | 0x7f800000 | ((em & 0x3ff) << 13); + else if (em >= 0x400) /* normal */ + bits = sign | ((em + 0x1c000) << 13); + else if (em) { /* subnormal */ + uint32_t m = em; + int shift = 0; + while (!(m & 0x400)) { + m <<= 1; + shift++; + } + bits = sign | ((uint32_t)(113 - shift) << 23) | ((m & 0x3ff) << 13); + } else + bits = sign; + return av_int2float(bits); +} + +static uint16_t f32_to_f16(float f) +{ + const uint32_t bits = av_float2int(f); + const uint32_t sign = (bits >> 16) & 0x8000; + const uint32_t em = bits & 0x7fffffff; + + if (em >= 0x47800000) /* overflow -> inf, nan stays nan */ + return sign | (em > 0x7f800000 ? 0x7e00 : 0x7c00); + if (em >= 0x38800000) { /* normal */ + const uint32_t r = em - 0x38000000; + const uint32_t rem = r & 0x1fff; + uint32_t h = r >> 13; + h += rem > 0x1000 || (rem == 0x1000 && (h & 1)); + return sign | h; + } + if (em >= 0x33000000) { /* subnormal */ + const uint32_t m = (em & 0x7fffff) | 0x800000; + const unsigned s = 126 - (em >> 23); + return sign | ((m + ((1u << (s - 1)) - 1) + ((m >> s) & 1)) >> s); + } + return sign; +} + +static void filter_line_f16_c(void *dstv, const void *const *srcv, int width, + int peak, const void *coefsv) +{ + uint16_t *dst = dstv; + const uint16_t *const *src = (const uint16_t *const *)srcv; + const float *coefs = coefsv; + + for (int x = 0; x < width; x++) { + float sum = 0; + for (int i = 0; i < VT_TAPS_T * VT_TAPS_V; i++) + sum += coefs[i] * f16_to_f32(src[i][x]); + dst[x] = f32_to_f16(sum); + } +} + +static const filter_line_fn filter_line_c_fns[VTI_KIND_NB] = { + [VTI_KIND_U8] = filter_line_u8_c, + [VTI_KIND_U16] = filter_line_u16_c, + [VTI_KIND_F32] = filter_line_f32_c, + [VTI_KIND_F16] = filter_line_f16_c, +}; + +av_cold void ff_vtinterlace_dsp_init(VTInterlaceDSPContext *dsp, enum VTInterlaceKind kind) +{ + dsp->filter_line = filter_line_c_fns[kind]; + dsp->min_width = 1; +} + +static int is_supported(const AVPixFmtDescriptor *desc, int filter) +{ + const unsigned reject = AV_PIX_FMT_FLAG_HWACCEL | AV_PIX_FMT_FLAG_BITSTREAM | + AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_BAYER | + AV_PIX_FMT_FLAG_XYZ; + const int depth = desc->comp[0].depth; + unsigned planes = 0; + int step; + + if (desc->flags & reject) + return 0; + if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { + if (depth != 16 && depth != 32) + return 0; + step = depth / 8; + } else { + if (depth < 8 || depth > 16) + return 0; + step = depth > 8 ? 2 : 1; + } + if (step > 1 && !!(desc->flags & AV_PIX_FMT_FLAG_BE) != HAVE_BIGENDIAN) + return 0; + if (filter && desc->log2_chroma_h > 1) + return 0; + + /* every component in a plane of its own, no packing */ + for (int i = 0; i < desc->nb_components; i++) { + const AVComponentDescriptor *c = &desc->comp[i]; + if (c->depth != depth || c->step != step || c->shift || c->offset || + planes & (1u << c->plane)) + return 0; + planes |= 1u << c->plane; + } + return 1; +} + +static int query_formats(const AVFilterContext *ctx, + AVFilterFormatsConfig **cfg_in, + AVFilterFormatsConfig **cfg_out) +{ + const VTInterlaceContext *s = ctx->priv; + AVFilterFormats *formats = NULL; + const AVPixFmtDescriptor *desc = NULL; + int ret; + + while ((desc = av_pix_fmt_desc_next(desc))) { + if (is_supported(desc, s->filter) && + (ret = ff_add_format(&formats, av_pix_fmt_desc_get_id(desc))) < 0) + return ret; + } + + return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + VTInterlaceContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + const int depth = desc->comp[0].depth; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + s->planewidth[0] = s->planewidth[3] = inlink->w; + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planeheight[0] = s->planeheight[3] = inlink->h; + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + + for (int p = 0; p < s->nb_planes; p++) { + if (s->planeheight[p] & 1) { + av_log(ctx, AV_LOG_ERROR, "All plane heights must be even.\n"); + return AVERROR(EINVAL); + } + } + + if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { + s->kind = depth == 32 ? VTI_KIND_F32 : VTI_KIND_F16; + s->peak = 0; + } else { + s->kind = depth == 8 ? VTI_KIND_U8 : VTI_KIND_U16; + s->peak = (1 << depth) - 1; + } + s->bps = desc->comp[0].step; + s->chroma_sited = s->filter && desc->log2_chroma_h == 1; + + s->filter_line_c = filter_line_c_fns[s->kind]; + ff_vtinterlace_dsp_init(&s->dsp, s->kind); + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + FilterLink *il = ff_filter_link(ctx->inputs[0]); + FilterLink *ol = ff_filter_link(outlink); + + if (il->frame_rate.num > 0 && il->frame_rate.den > 0) + ol->frame_rate = av_mul_q(il->frame_rate, (AVRational){ 1, 2 }); + else + ol->frame_rate = il->frame_rate; + + return 0; +} + +static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs) +{ + VTInterlaceContext *s = ctx->priv; + ThreadData *td = arg; + AVFrame *out = td->out; + const void *center = ff_vtinterlace_coefs(s->kind, VTI_SHIFT_CENTER); + const int top_off = !s->tff; + + for (int p = 0; p < s->nb_planes; p++) { + const int rows = s->planeheight[p]; + const int width = s->planewidth[p]; + const int start = ff_slice_pos(rows, job, nb_jobs); + const int end = ff_slice_pos(rows, job + 1, nb_jobs); + const int sited = s->chroma_sited && (p == 1 || p == 2); + const ptrdiff_t dst_stride = out->linesize[p]; + uint8_t *dst = out->data[p]; + + if (!s->filter) { + /* plain weave: line r comes from the frame supplying that field */ + for (int r = start; r < end; r++) { + const AVFrame *src = td->src[2 + ((r & 1) ^ top_off)]; + memcpy(dst + r * dst_stride, + src->data[p] + r * src->linesize[p], width * s->bps); + } + continue; + } + + /* output line r = progressive line r of the frame supplying that + * field, VT-filtered; line edges repeated */ + for (int r = start; r < end; r++) { + const int o = (r & 1) ^ top_off; + const void *ptrs[VTI_SRC_PTRS]; + + for (int t = 0; t < VT_TAPS_T; t++) { + const AVFrame *src = td->src[o + t]; + const uint8_t *base = src->data[p]; + const ptrdiff_t stride = src->linesize[p]; + for (int v = 0; v < VT_TAPS_V; v++) { + const int line = av_clip(r + v - VT_TAPS_V / 2, 0, rows - 1); + ptrs[t * VT_TAPS_V + v] = base + line * stride; + } + } + ptrs[VTI_SRC_PTRS - 1] = ptrs[VTI_SRC_PTRS - 2]; + + (width >= s->dsp.min_width ? s->dsp.filter_line : s->filter_line_c) + (dst + r * dst_stride, ptrs, width, s->peak, + sited ? td->chroma_coefs[r & 1] : center); + } + } + + return 0; +} + +static int output_pending(const VTInterlaceContext *s) +{ + if (s->eof) /* an odd trailing frame is dropped */ + return s->nb_out < s->nb_in / 2; + return s->nb_in >= 2 * s->nb_out + (s->filter ? 4 : 2); +} + +static int output_frame(AVFilterContext *ctx) +{ + VTInterlaceContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + const int64_t n = s->nb_out; + const AVFrame *first; + ThreadData td = { 0 }; + AVFrame *out; + int ret; + + for (int k = 0; k < WINDOW; k++) { + const int64_t q = av_clip64(2 * n + k - 2, 0, s->nb_in - 1); + td.src[k] = s->frames[q % WINDOW]; + } + first = td.src[2]; + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) + return AVERROR(ENOMEM); + ret = av_frame_copy_props(out, first); + if (ret < 0) { + av_frame_free(&out); + return ret; + } + + if (s->chroma_sited) { + const enum AVChromaLocation loc = first->chroma_location; + int vclass; + + switch (loc) { + case AVCHROMA_LOC_UNSPECIFIED: + case AVCHROMA_LOC_LEFT: + case AVCHROMA_LOC_CENTER: vclass = 0; break; + case AVCHROMA_LOC_TOPLEFT: + case AVCHROMA_LOC_TOP: vclass = 1; break; + case AVCHROMA_LOC_BOTTOMLEFT: + case AVCHROMA_LOC_BOTTOM: vclass = 2; break; + default: + av_log(ctx, AV_LOG_ERROR, "Unsupported chroma location %d.\n", loc); + av_frame_free(&out); + return AVERROR(EINVAL); + } + for (int b = 0; b < 2; b++) + td.chroma_coefs[b] = ff_vtinterlace_coefs(s->kind, chroma_shift[vclass][b]); + + /* corrected to interlaced siting; horizontal siting is kept */ + out->chroma_location = loc == AVCHROMA_LOC_CENTER || + loc == AVCHROMA_LOC_TOP || + loc == AVCHROMA_LOC_BOTTOM ? AVCHROMA_LOC_CENTER + : AVCHROMA_LOC_LEFT; + } + + td.out = out; + ff_filter_execute(ctx, filter_slice, &td, NULL, + FFMIN(outlink->h, ff_filter_get_nb_threads(ctx))); + + out->pts = first->pts; + if (out->duration > 0) + out->duration *= 2; + out->flags |= AV_FRAME_FLAG_INTERLACED; + if (s->tff) + out->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; + else + out->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST; + + /* frames 2n-2 and 2n-1 are not needed for output n+1 */ + for (int64_t q = FFMAX(2 * n - 2, 0); q < 2 * n; q++) + av_frame_free(&s->frames[q % WINDOW]); + s->nb_out++; + + return ff_filter_frame(outlink, out); +} + +static int activate(AVFilterContext *ctx) +{ + VTInterlaceContext *s = ctx->priv; + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + int ret; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + while (!s->eof && !output_pending(s)) { + AVFrame *in; + + ret = ff_inlink_consume_frame(inlink, &in); + if (ret < 0) + return ret; + if (!ret) + break; + av_frame_free(&s->frames[s->nb_in % WINDOW]); + s->frames[s->nb_in % WINDOW] = in; + s->nb_in++; + } + + if (!s->eof && !output_pending(s) && + ff_inlink_acknowledge_status(inlink, &s->eof_status, &s->eof_pts)) + s->eof = 1; + + if (output_pending(s)) { + ret = output_frame(ctx); + if (ret < 0) + return ret; + ff_filter_set_ready(ctx, 100); + return 0; + } + + if (s->eof) { + for (int k = 0; k < WINDOW; k++) + av_frame_free(&s->frames[k]); + ff_outlink_set_status(outlink, s->eof_status, s->eof_pts); + return 0; + } + + FF_FILTER_FORWARD_WANTED(outlink, inlink); + + return FFERROR_NOT_READY; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + VTInterlaceContext *s = ctx->priv; + + for (int k = 0; k < WINDOW; k++) + av_frame_free(&s->frames[k]); +} + +static const AVFilterPad vtinterlace_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, +}; + +static const AVFilterPad vtinterlace_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, +}; + +const FFFilter ff_vf_vtinterlace = { + .p.name = "vtinterlace", + .p.description = NULL_IF_CONFIG_SMALL("Convert progressive video to interlaced with a WHP 315 vertical-temporal prefilter."), + .p.priv_class = &vtinterlace_class, + .p.flags = AVFILTER_FLAG_SLICE_THREADS, + .priv_size = sizeof(VTInterlaceContext), + .uninit = uninit, + .activate = activate, + FILTER_INPUTS(vtinterlace_inputs), + FILTER_OUTPUTS(vtinterlace_outputs), + FILTER_QUERY_FUNC2(query_formats), +}; diff --git a/libavfilter/vtinterlace_coeffs.h b/libavfilter/vtinterlace_coeffs.h new file mode 100644 index 00000000000..d4839f866be --- /dev/null +++ b/libavfilter/vtinterlace_coeffs.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2026 Phillip Blucas + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + * Vertical-temporal interlacing prefilter (19 lines x 5 frames) + * designed per BBC R&D WHP 315 / WHP 230. + * + * Generated by tools/vt_filter_design.py of + * https://github.com/ifb/vapoursynth-interlace; do not edit by hand. + * + * Flat [t][v] initializers; i16 is padded with a 0 to 96 entries + * so SIMD can consume coefficients as pmaddwd pairs. + */ + +#ifndef AVFILTER_VTINTERLACE_COEFFS_H +#define AVFILTER_VTINTERLACE_COEFFS_H +#define VT_TAPS_V 19 +#define VT_TAPS_T 5 +#define VT_COEF_BITS 13 + +#define VT_COEFS_I16 \ + -36, 17, 69, -21, -84, 12, 71, 0, -28, -5, -28, 0, 71, 12, -84, -21, 69, 17, -36, \ + 13, 53, -30, -147, 47, 264, -44, -364, 17, 404, 17, -364, -44, 264, 47, -147, -30, 53, 13, \ + -18, 3, -12, 17, 166, -57, -600, 96, 2498, 3972, 2498, 96, -600, -57, 166, 17, -12, 3, -18, \ + 13, 53, -30, -147, 47, 264, -44, -364, 17, 404, 17, -364, -44, 264, 47, -147, -30, 53, 13, \ + -36, 17, 69, -21, -84, 12, 71, 0, -28, -5, -28, 0, 71, 12, -84, -21, 69, 17, -36, 0 + +#define VT_COEFS_F32 \ + -4.33484284e-03f, 2.11259849e-03f, 8.38493772e-03f, -2.56801756e-03f, -1.02743794e-02f, 1.52030459e-03f, 8.69080325e-03f, -7.26283869e-07f, -3.44080137e-03f, -6.67796956e-04f, -3.44080137e-03f, -7.26283869e-07f, 8.69080325e-03f, 1.52030459e-03f, -1.02743794e-02f, -2.56801756e-03f, 8.38493772e-03f, 2.11259849e-03f, -4.33484284e-03f, \ + 1.59157976e-03f, 6.52960491e-03f, -3.64697718e-03f, -1.79977836e-02f, 5.69511356e-03f, 3.21762447e-02f, -5.32470463e-03f, -4.43917951e-02f, 2.13079717e-03f, 4.93021413e-02f, 2.13079717e-03f, -4.43917951e-02f, -5.32470463e-03f, 3.21762447e-02f, 5.69511356e-03f, -1.79977836e-02f, -3.64697718e-03f, 6.52960491e-03f, 1.59157976e-03f, \ + -2.25323075e-03f, 3.47609524e-04f, -1.42756182e-03f, 2.05349093e-03f, 2.02567924e-02f, -6.94309838e-03f, -7.32626366e-02f, 1.17285784e-02f, 3.04929331e-01f, 4.84464938e-01f, 3.04929331e-01f, 1.17285784e-02f, -7.32626366e-02f, -6.94309838e-03f, 2.02567924e-02f, 2.05349093e-03f, -1.42756182e-03f, 3.47609524e-04f, -2.25323075e-03f, \ + 1.59157976e-03f, 6.52960491e-03f, -3.64697718e-03f, -1.79977836e-02f, 5.69511356e-03f, 3.21762447e-02f, -5.32470463e-03f, -4.43917951e-02f, 2.13079717e-03f, 4.93021413e-02f, 2.13079717e-03f, -4.43917951e-02f, -5.32470463e-03f, 3.21762447e-02f, 5.69511356e-03f, -1.79977836e-02f, -3.64697718e-03f, 6.52960491e-03f, 1.59157976e-03f, \ + -4.33484284e-03f, 2.11259849e-03f, 8.38493772e-03f, -2.56801756e-03f, -1.02743794e-02f, 1.52030459e-03f, 8.69080325e-03f, -7.26283869e-07f, -3.44080137e-03f, -6.67796956e-04f, -3.44080137e-03f, -7.26283869e-07f, 8.69080325e-03f, 1.52030459e-03f, -1.02743794e-02f, -2.56801756e-03f, 8.38493772e-03f, 2.11259849e-03f, -4.33484284e-03f, 0 + +/* fractional vertical delays for interlaced chroma siting, + in chroma rows; y[r] samples x[r + shift] */ + +#define VT_COEFS_M375_I16 \ + -21, 35, 42, -55, -60, 46, 53, -23, -23, -9, -25, 30, 68, -30, -84, 20, 74, -6, -39, \ + 27, 34, -84, -106, 152, 197, -210, -290, 222, 340, -189, -323, 125, 246, -61, -141, 23, 54, -1, \ + -9, -6, -22, 76, 149, -283, -560, 879, 3326, 3757, 1551, -423, -460, 113, 127, -21, 4, 2, -24, \ + 27, 34, -84, -106, 152, 197, -210, -290, 222, 340, -189, -323, 125, 246, -61, -141, 23, 54, -1, \ + -21, 35, 42, -55, -60, 46, 53, -23, -23, -9, -25, 30, 68, -30, -84, 20, 74, -6, -39, 0 + +#define VT_COEFS_M375_F32 \ + -2.52502192e-03f, 4.29360875e-03f, 5.09356433e-03f, -6.72571019e-03f, -7.26936361e-03f, 5.67590464e-03f, 6.42000662e-03f, -2.78750535e-03f, -2.80646147e-03f, -1.14429413e-03f, -3.10961479e-03f, 3.63888623e-03f, 8.27393594e-03f, -3.70992403e-03f, -1.02188790e-02f, 2.47628707e-03f, 8.99924043e-03f, -7.50127381e-04f, -4.81529014e-03f, \ + 3.31150121e-03f, 4.13576892e-03f, -1.02305324e-02f, -1.29689074e-02f, 1.85202223e-02f, 2.40037106e-02f, -2.55838827e-02f, -3.54510280e-02f, 2.70725305e-02f, 4.15164131e-02f, -2.30369859e-02f, -3.93842117e-02f, 1.52476867e-02f, 2.99764959e-02f, -7.48645866e-03f, -1.71717570e-02f, 2.83512210e-03f, 6.58207215e-03f, -1.12259381e-04f, \ + -1.04244499e-03f, -7.00465856e-04f, -2.70206219e-03f, 9.28610361e-03f, 1.81864686e-02f, -3.45593185e-02f, -6.83299974e-02f, 1.07251488e-01f, 4.05955886e-01f, 4.59062422e-01f, 1.89347229e-01f, -5.15901631e-02f, -5.61935210e-02f, 1.37984734e-02f, 1.54580606e-02f, -2.61183055e-03f, 5.38598604e-04f, 2.40770002e-04f, -2.96518211e-03f, \ + 3.31150121e-03f, 4.13576892e-03f, -1.02305324e-02f, -1.29689074e-02f, 1.85202223e-02f, 2.40037106e-02f, -2.55838827e-02f, -3.54510280e-02f, 2.70725305e-02f, 4.15164131e-02f, -2.30369859e-02f, -3.93842117e-02f, 1.52476867e-02f, 2.99764959e-02f, -7.48645866e-03f, -1.71717570e-02f, 2.83512210e-03f, 6.58207215e-03f, -1.12259381e-04f, \ + -2.52502192e-03f, 4.29360875e-03f, 5.09356433e-03f, -6.72571019e-03f, -7.26936361e-03f, 5.67590464e-03f, 6.42000662e-03f, -2.78750535e-03f, -2.80646147e-03f, -1.14429413e-03f, -3.10961479e-03f, 3.63888623e-03f, 8.27393594e-03f, -3.70992403e-03f, -1.02188790e-02f, 2.47628707e-03f, 8.99924043e-03f, -7.50127381e-04f, -4.81529014e-03f, 0 + +#define VT_COEFS_M125_I16 \ + -32, 24, 62, -34, -79, 25, 67, -9, -27, -6, -29, 9, 73, -2, -88, -8, 73, 9, -39, \ + 18, 49, -49, -140, 83, 251, -102, -354, 89, 398, -55, -364, 15, 268, 9, -151, -11, 56, 8, \ + -16, 1, -17, 35, 168, -129, -613, 331, 2804, 3958, 2195, -110, -571, 8, 158, 1, -6, 3, -22, \ + 18, 49, -49, -140, 83, 251, -102, -354, 89, 398, -55, -364, 15, 268, 9, -151, -11, 56, 8, \ + -32, 24, 62, -34, -79, 25, 67, -9, -27, -6, -29, 9, 73, -2, -88, -8, 73, 9, -39, 0 + +#define VT_COEFS_M125_F32 \ + -3.92356879e-03f, 2.91330237e-03f, 7.56278004e-03f, -4.16260162e-03f, -9.66377986e-03f, 3.07001534e-03f, 8.20011744e-03f, -1.11847026e-03f, -3.35357138e-03f, -7.45399275e-04f, -3.51954809e-03f, 1.12757515e-03f, 8.89027209e-03f, -2.09764072e-04f, -1.07084808e-02f, -9.41827623e-04f, 8.92734971e-03f, 1.14944382e-03f, -4.71599002e-03f, \ + 2.15074323e-03f, 5.96958075e-03f, -5.96103092e-03f, -1.70727325e-02f, 1.01800527e-02f, 3.06809673e-02f, -1.24799439e-02f, -4.31581650e-02f, 1.08512941e-02f, 4.85886109e-02f, -6.69599794e-03f, -4.44772257e-02f, 1.82625152e-03f, 3.26862496e-02f, 1.06055659e-03f, -1.84641308e-02f, -1.36838916e-03f, 6.78312384e-03f, 9.40082967e-04f, \ + -1.93676457e-03f, 7.47659502e-05f, -2.01947775e-03f, 4.23273317e-03f, 2.05080684e-02f, -1.57483913e-02f, -7.48620076e-02f, 4.03850556e-02f, 3.42284864e-01f, 4.83284492e-01f, 2.67921735e-01f, -1.34036144e-02f, -6.97069158e-02f, 1.03340965e-03f, 1.92294129e-02f, 8.72371098e-05f, -7.68352133e-04f, 4.05115777e-04f, -2.63686947e-03f, \ + 2.15074323e-03f, 5.96958075e-03f, -5.96103092e-03f, -1.70727325e-02f, 1.01800527e-02f, 3.06809673e-02f, -1.24799439e-02f, -4.31581650e-02f, 1.08512941e-02f, 4.85886109e-02f, -6.69599794e-03f, -4.44772257e-02f, 1.82625152e-03f, 3.26862496e-02f, 1.06055659e-03f, -1.84641308e-02f, -1.36838916e-03f, 6.78312384e-03f, 9.40082967e-04f, \ + -3.92356879e-03f, 2.91330237e-03f, 7.56278004e-03f, -4.16260162e-03f, -9.66377986e-03f, 3.07001534e-03f, 8.20011744e-03f, -1.11847026e-03f, -3.35357138e-03f, -7.45399275e-04f, -3.51954809e-03f, 1.12757515e-03f, 8.89027209e-03f, -2.09764072e-04f, -1.07084808e-02f, -9.41827623e-04f, 8.92734971e-03f, 1.14944382e-03f, -4.71599002e-03f, 0 + +#define VT_COEFS_P125_I16 \ + -39, 9, 73, -8, -88, -2, 73, 9, -29, -6, -27, -9, 67, 25, -79, -34, 62, 24, -32, \ + 8, 56, -11, -151, 9, 268, 15, -364, -55, 398, 89, -354, -102, 251, 83, -140, -49, 49, 18, \ + -22, 3, -6, 1, 158, 8, -571, -110, 2195, 3958, 2804, 331, -613, -129, 168, 35, -17, 1, -16, \ + 8, 56, -11, -151, 9, 268, 15, -364, -55, 398, 89, -354, -102, 251, 83, -140, -49, 49, 18, \ + -39, 9, 73, -8, -88, -2, 73, 9, -29, -6, -27, -9, 67, 25, -79, -34, 62, 24, -32, 0 + +#define VT_COEFS_P125_F32 \ + -4.71599002e-03f, 1.14944382e-03f, 8.92734971e-03f, -9.41827623e-04f, -1.07084808e-02f, -2.09764072e-04f, 8.89027209e-03f, 1.12757515e-03f, -3.51954809e-03f, -7.45399275e-04f, -3.35357138e-03f, -1.11847026e-03f, 8.20011744e-03f, 3.07001534e-03f, -9.66377986e-03f, -4.16260162e-03f, 7.56278004e-03f, 2.91330237e-03f, -3.92356879e-03f, \ + 9.40082967e-04f, 6.78312384e-03f, -1.36838916e-03f, -1.84641308e-02f, 1.06055659e-03f, 3.26862496e-02f, 1.82625152e-03f, -4.44772257e-02f, -6.69599794e-03f, 4.85886109e-02f, 1.08512941e-02f, -4.31581650e-02f, -1.24799439e-02f, 3.06809673e-02f, 1.01800527e-02f, -1.70727325e-02f, -5.96103092e-03f, 5.96958075e-03f, 2.15074323e-03f, \ + -2.63686947e-03f, 4.05115777e-04f, -7.68352133e-04f, 8.72371098e-05f, 1.92294129e-02f, 1.03340965e-03f, -6.97069158e-02f, -1.34036144e-02f, 2.67921735e-01f, 4.83284492e-01f, 3.42284864e-01f, 4.03850556e-02f, -7.48620076e-02f, -1.57483913e-02f, 2.05080684e-02f, 4.23273317e-03f, -2.01947775e-03f, 7.47659502e-05f, -1.93676457e-03f, \ + 9.40082967e-04f, 6.78312384e-03f, -1.36838916e-03f, -1.84641308e-02f, 1.06055659e-03f, 3.26862496e-02f, 1.82625152e-03f, -4.44772257e-02f, -6.69599794e-03f, 4.85886109e-02f, 1.08512941e-02f, -4.31581650e-02f, -1.24799439e-02f, 3.06809673e-02f, 1.01800527e-02f, -1.70727325e-02f, -5.96103092e-03f, 5.96958075e-03f, 2.15074323e-03f, \ + -4.71599002e-03f, 1.14944382e-03f, 8.92734971e-03f, -9.41827623e-04f, -1.07084808e-02f, -2.09764072e-04f, 8.89027209e-03f, 1.12757515e-03f, -3.51954809e-03f, -7.45399275e-04f, -3.35357138e-03f, -1.11847026e-03f, 8.20011744e-03f, 3.07001534e-03f, -9.66377986e-03f, -4.16260162e-03f, 7.56278004e-03f, 2.91330237e-03f, -3.92356879e-03f, 0 + +#define VT_COEFS_P375_I16 \ + -39, -6, 74, 20, -84, -30, 68, 30, -25, -9, -23, -23, 53, 46, -60, -55, 42, 35, -21, \ + -1, 54, 23, -141, -61, 246, 125, -323, -189, 340, 222, -290, -210, 197, 152, -106, -84, 34, 27, \ + -24, 2, 4, -21, 127, 113, -460, -423, 1551, 3757, 3326, 879, -560, -283, 149, 76, -22, -6, -9, \ + -1, 54, 23, -141, -61, 246, 125, -323, -189, 340, 222, -290, -210, 197, 152, -106, -84, 34, 27, \ + -39, -6, 74, 20, -84, -30, 68, 30, -25, -9, -23, -23, 53, 46, -60, -55, 42, 35, -21, 0 + +#define VT_COEFS_P375_F32 \ + -4.81529014e-03f, -7.50127381e-04f, 8.99924043e-03f, 2.47628707e-03f, -1.02188790e-02f, -3.70992403e-03f, 8.27393594e-03f, 3.63888623e-03f, -3.10961479e-03f, -1.14429413e-03f, -2.80646147e-03f, -2.78750535e-03f, 6.42000662e-03f, 5.67590464e-03f, -7.26936361e-03f, -6.72571019e-03f, 5.09356433e-03f, 4.29360875e-03f, -2.52502192e-03f, \ + -1.12259381e-04f, 6.58207215e-03f, 2.83512210e-03f, -1.71717570e-02f, -7.48645866e-03f, 2.99764959e-02f, 1.52476867e-02f, -3.93842117e-02f, -2.30369859e-02f, 4.15164131e-02f, 2.70725305e-02f, -3.54510280e-02f, -2.55838827e-02f, 2.40037106e-02f, 1.85202223e-02f, -1.29689074e-02f, -1.02305324e-02f, 4.13576892e-03f, 3.31150121e-03f, \ + -2.96518211e-03f, 2.40770002e-04f, 5.38598604e-04f, -2.61183055e-03f, 1.54580606e-02f, 1.37984734e-02f, -5.61935210e-02f, -5.15901631e-02f, 1.89347229e-01f, 4.59062422e-01f, 4.05955886e-01f, 1.07251488e-01f, -6.83299974e-02f, -3.45593185e-02f, 1.81864686e-02f, 9.28610361e-03f, -2.70206219e-03f, -7.00465856e-04f, -1.04244499e-03f, \ + -1.12259381e-04f, 6.58207215e-03f, 2.83512210e-03f, -1.71717570e-02f, -7.48645866e-03f, 2.99764959e-02f, 1.52476867e-02f, -3.93842117e-02f, -2.30369859e-02f, 4.15164131e-02f, 2.70725305e-02f, -3.54510280e-02f, -2.55838827e-02f, 2.40037106e-02f, 1.85202223e-02f, -1.29689074e-02f, -1.02305324e-02f, 4.13576892e-03f, 3.31150121e-03f, \ + -4.81529014e-03f, -7.50127381e-04f, 8.99924043e-03f, 2.47628707e-03f, -1.02188790e-02f, -3.70992403e-03f, 8.27393594e-03f, 3.63888623e-03f, -3.10961479e-03f, -1.14429413e-03f, -2.80646147e-03f, -2.78750535e-03f, 6.42000662e-03f, 5.67590464e-03f, -7.26936361e-03f, -6.72571019e-03f, 5.09356433e-03f, 4.29360875e-03f, -2.52502192e-03f, 0 + +#endif /* AVFILTER_VTINTERLACE_COEFFS_H */ diff --git a/libavfilter/vtinterlacedsp.h b/libavfilter/vtinterlacedsp.h new file mode 100644 index 00000000000..1b5ccdbaafa --- /dev/null +++ b/libavfilter/vtinterlacedsp.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2026 Phillip Blucas + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVFILTER_VTINTERLACEDSP_H +#define AVFILTER_VTINTERLACEDSP_H + +enum VTInterlaceKind { + VTI_KIND_U8, + VTI_KIND_U16, + VTI_KIND_F32, + VTI_KIND_F16, + VTI_KIND_NB, +}; + +/* fractional vertical delay of the coefficient set, in chroma rows; + * moves subsampled chroma onto the interlaced siting grid */ +enum VTInterlaceShift { + VTI_SHIFT_M375, + VTI_SHIFT_M125, + VTI_SHIFT_CENTER, + VTI_SHIFT_P125, + VTI_SHIFT_P375, + VTI_SHIFT_NB, +}; + +/* src: 5 x 19 line pointers, [t][v] flattened, edges pre-clamped, plus one + * extra (any valid line) so SIMD can process coefficients in pairs; its + * coefficient is 0. */ +#define VTI_SRC_PTRS (5 * 19 + 1) + +typedef struct VTInterlaceDSPContext { + /** + * Filter one output line. + * + * Reads exactly width samples from each source line and writes exactly + * width samples to dst. width must be >= min_width. + * + * @param peak maximum integer sample value (u16 only) + * @param coefs VTI_SRC_PTRS int16_t (u8, u16) or float (f32, f16) + */ + void (*filter_line)(void *dst, const void *const *src, int width, int peak, + const void *coefs); + int min_width; +} VTInterlaceDSPContext; + +void ff_vtinterlace_dsp_init(VTInterlaceDSPContext *dsp, enum VTInterlaceKind kind); + +const void *ff_vtinterlace_coefs(enum VTInterlaceKind kind, enum VTInterlaceShift shift); + +#endif /* AVFILTER_VTINTERLACEDSP_H */ -- 2.54.0 >From c36946b3ed7b168473c0ea944a8b2bc3643c10cb Mon Sep 17 00:00:00 2001 From: Phillip Blucas <[email protected]> Date: Wed, 11 Nov 2026 02:51:33 +0000 Subject: [PATCH 2/3] avfilter/x86: add SIMD for vtinterlace SSE2, AVX2 and AVX-512 versions of the vertical-temporal line kernel for 8-bit, 9..16-bit and single precision samples, plus AVX2 and AVX-512 for half precision (F16C conversion on load, f32 accumulation, round to nearest even on store; F16C has no CPU flag of its own in FFmpeg and is implied by every AVX2 implementation). Integer kernels accumulate with pmaddwd over coefficient pairs and are bit-exact with C; 9..16-bit samples are biased to signed 16 bit, which cancels exactly since every coefficient set sums to 1 << 13. Float kernels use FMA from AVX2 up, so their output may differ from C by a few ulp; SSE2 f32 is bit-exact. Unlike the original, no kernel reads or writes outside [0, width): the original stores a whole vector past width at SSE2/AVX2 (AVX-512 masked the store) and reads whole vectors past width at every level, which is not safe for arbitrary frames (user-allocated buffers, frames handed out by a downstream filter's get_buffer). Instead the last vector of a line is moved back to end exactly at width and recomputes a few samples already written, bit-identically. Lines narrower than one vector use the C version; the DSP context carries the minimum width. Ported from https://github.com/ifb/vapoursynth-interlace 41bb21b (relicensed to LGPL-2.1-or-later in f69ff68). --- libavfilter/vf_vtinterlace.c | 4 + libavfilter/vtinterlacedsp.h | 1 + libavfilter/x86/Makefile | 2 + libavfilter/x86/vf_vtinterlace.asm | 235 ++++++++++++++++++++++++++ libavfilter/x86/vf_vtinterlace_init.c | 91 ++++++++++ 5 files changed, 333 insertions(+) create mode 100644 libavfilter/x86/vf_vtinterlace.asm create mode 100644 libavfilter/x86/vf_vtinterlace_init.c diff --git a/libavfilter/vf_vtinterlace.c b/libavfilter/vf_vtinterlace.c index e9773e76718..e4ce5aaa478 100644 --- a/libavfilter/vf_vtinterlace.c +++ b/libavfilter/vf_vtinterlace.c @@ -242,6 +242,10 @@ av_cold void ff_vtinterlace_dsp_init(VTInterlaceDSPContext *dsp, enum VTInterlac { dsp->filter_line = filter_line_c_fns[kind]; dsp->min_width = 1; + +#if ARCH_X86 && HAVE_X86ASM + ff_vtinterlace_init_x86(dsp, kind); +#endif } static int is_supported(const AVPixFmtDescriptor *desc, int filter) diff --git a/libavfilter/vtinterlacedsp.h b/libavfilter/vtinterlacedsp.h index 1b5ccdbaafa..afa2c4e9eb6 100644 --- a/libavfilter/vtinterlacedsp.h +++ b/libavfilter/vtinterlacedsp.h @@ -61,6 +61,7 @@ typedef struct VTInterlaceDSPContext { } VTInterlaceDSPContext; void ff_vtinterlace_dsp_init(VTInterlaceDSPContext *dsp, enum VTInterlaceKind kind); +void ff_vtinterlace_init_x86(VTInterlaceDSPContext *dsp, enum VTInterlaceKind kind); const void *ff_vtinterlace_coefs(enum VTInterlaceKind kind, enum VTInterlaceShift shift); diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile index cfbed4317c8..3bab1fa7841 100644 --- a/libavfilter/x86/Makefile +++ b/libavfilter/x86/Makefile @@ -70,6 +70,8 @@ X86ASM-OBJS-$(CONFIG_TRANSPOSE_FILTER) += x86/vf_transpose.o \ x86/vf_transpose_init.o X86ASM-OBJS-$(CONFIG_VOLUME_FILTER) += x86/af_volume.o x86/af_volume_init.o X86ASM-OBJS-$(CONFIG_V360_FILTER) += x86/vf_v360.o x86/vf_v360_init.o +X86ASM-OBJS-$(CONFIG_VTINTERLACE_FILTER) += x86/vf_vtinterlace.o \ + x86/vf_vtinterlace_init.o X86ASM-OBJS-$(CONFIG_W3FDIF_FILTER) += x86/vf_w3fdif.o x86/vf_w3fdif_init.o X86ASM-OBJS-$(CONFIG_XPSNR_FILTER) += x86/vf_psnr.o x86/vf_psnr_init.o X86ASM-OBJS-$(CONFIG_YADIF_FILTER) += x86/vf_yadif.o x86/yadif-16.o \ diff --git a/libavfilter/x86/vf_vtinterlace.asm b/libavfilter/x86/vf_vtinterlace.asm new file mode 100644 index 00000000000..c6538c3e3cf --- /dev/null +++ b/libavfilter/x86/vf_vtinterlace.asm @@ -0,0 +1,235 @@ +;***************************************************************************** +;* x86-optimized vertical-temporal prefilter for the vtinterlace filter +;* +;* Copyright (c) 2026 Phillip Blucas +;* +;* This file is part of FFmpeg. +;* +;* FFmpeg is free software; you can redistribute it and/or +;* modify it under the terms of the GNU Lesser General Public +;* License as published by the Free Software Foundation; either +;* version 2.1 of the License, or (at your option) any later version. +;* +;* FFmpeg is distributed in the hope that it will be useful, +;* but WITHOUT ANY WARRANTY; without even the implied warranty of +;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;* Lesser General Public License for more details. +;* +;* You should have received a copy of the GNU Lesser General Public +;* License along with FFmpeg; if not, write to the Free Software +;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +;***************************************************************************** + +%include "libavutil/x86/x86util.asm" + +SECTION_RODATA 64 + +pd_4096: times 16 dd 4096 +pw_8000: times 32 dw 0x8000 + +SECTION .text + +; All kernels: +; void ff_vtinterlace_line_<type>_<cpu>(void *dst, const void *const *src, +; int width, int peak, const void *coefs) +; src has 96 line pointers; width must be at least one vector of output. +; Every vector is a whole vector of input and output: the last one is moved +; back to end exactly at width, recomputing a few already written samples +; (bit-identically), so nothing outside [0, width) is read or written. + +%if ARCH_X86_64 + +; broadcast one coefficient pmaddwd pair (dword) +%macro COEF_PD 2 +%if mmsize >= 32 + vpbroadcastd %1, %2 +%else + movd %1, %2 + pshufd %1, %1, 0 +%endif +%endmacro + +; wq = byte offset of the last vector, %1 = bytes stored per vector +%macro NEXT_X 1 + cmp xq, wq + je .end + add xq, %1 + cmp xq, wq + jle .xloop + mov xq, wq + jmp .xloop +.end: + RET +%endmacro + +; coefficients consumed as 48 pmaddwd pairs; peak is implied (packuswb) +%macro VT_U8 0 +cglobal vtinterlace_line_u8, 5, 9, 12, dst, src, w, peak, coef, x, k, ptra, ptrb + movsxdifnidn wq, wd + sub wq, mmsize + pxor m9, m9 + xor xd, xd +.xloop: + mova m0, [pd_4096] + mova m1, m0 + mova m2, m0 + mova m3, m0 + xor kd, kd +.kloop: + mov ptraq, [srcq + kq*8] + mov ptrbq, [srcq + kq*8 + 8] + COEF_PD m6, [coefq + kq*2] + movu m4, [ptraq + xq] + movu m5, [ptrbq + xq] + punpckhbw m7, m4, m9 + punpcklbw m4, m9 + punpckhbw m8, m5, m9 + punpcklbw m5, m9 + punpckhwd m10, m4, m5 + punpcklwd m4, m5 + punpckhwd m11, m7, m8 + punpcklwd m7, m8 + pmaddwd m4, m6 + pmaddwd m10, m6 + pmaddwd m7, m6 + pmaddwd m11, m6 + paddd m0, m4 + paddd m1, m10 + paddd m2, m7 + paddd m3, m11 + add kd, 2 + cmp kd, 96 + jl .kloop + psrad m0, 13 + psrad m1, 13 + psrad m2, 13 + psrad m3, 13 + packssdw m0, m1 + packssdw m2, m3 + packuswb m0, m2 + movu [dstq + xq], m0 + NEXT_X mmsize +%endmacro + +; pixels are biased to i16 for pmaddwd; sum(coefs) = 1<<13 makes the bias +; correction cancel against the post-shift unbias, so only rounding remains +%macro VT_U16 0 +cglobal vtinterlace_line_u16, 5, 9, 9, dst, src, w, peak, coef, x, k, ptra, ptrb + mova m6, [pw_8000] + sub peakd, 32768 + movd xm8, peakd +%if mmsize >= 32 + vpbroadcastw m8, xm8 +%else + punpcklwd m8, m8 + pshufd m8, m8, 0 +%endif + movsxdifnidn wq, wd + add wq, wq + sub wq, mmsize + xor xd, xd +.xloop: + mova m0, [pd_4096] + mova m1, m0 + xor kd, kd +.kloop: + mov ptraq, [srcq + kq*8] + mov ptrbq, [srcq + kq*8 + 8] + COEF_PD m5, [coefq + kq*2] + movu m2, [ptraq + xq] + movu m3, [ptrbq + xq] + pxor m2, m6 + pxor m3, m6 + punpckhwd m4, m2, m3 + punpcklwd m2, m3 + pmaddwd m2, m5 + pmaddwd m4, m5 + paddd m0, m2 + paddd m1, m4 + add kd, 2 + cmp kd, 96 + jl .kloop + psrad m0, 13 + psrad m1, 13 + packssdw m0, m1 + pminsw m0, m8 + pxor m0, m6 + movu [dstq + xq], m0 + NEXT_X mmsize +%endmacro + +; ascending tap order; AVX2 up uses FMA (one rounding fewer per tap, so a few +; ulp from the C sum and slightly more accurate), SSE2 matches C bit-exactly +%macro VT_F32 0 +cglobal vtinterlace_line_f32, 5, 8, 3, dst, src, w, peak, coef, x, k, ptra + movsxdifnidn wq, wd + shl wq, 2 + sub wq, mmsize + xor xd, xd +.xloop: + xorps m0, m0 + xor kd, kd +.kloop: + mov ptraq, [srcq + kq*8] +%if mmsize >= 32 + vbroadcastss m1, [coefq + kq*4] + vfmadd231ps m0, m1, [ptraq + xq] +%else + movss m1, [coefq + kq*4] + shufps m1, m1, 0 + movu m2, [ptraq + xq] + mulps m2, m1 + addps m0, m2 +%endif + inc kd + cmp kd, 95 + jl .kloop + movu [dstq + xq], m0 + NEXT_X mmsize +%endmacro + +; F16C convert on load, f32 FMA accumulation in scalar tap order, +; round-to-nearest-even store. AVX2 up only; the C version covers the rest. +%macro VT_F16 0 +cglobal vtinterlace_line_f16, 5, 8, 3, dst, src, w, peak, coef, x, k, ptra + movsxdifnidn wq, wd + add wq, wq + sub wq, mmsize/2 + xor xd, xd +.xloop: + xorps m0, m0 + xor kd, kd +.kloop: + mov ptraq, [srcq + kq*8] + vbroadcastss m1, [coefq + kq*4] + vcvtph2ps m2, [ptraq + xq] + vfmadd231ps m0, m1, m2 + inc kd + cmp kd, 95 + jl .kloop + vcvtps2ph [dstq + xq], m0, 4 + NEXT_X mmsize/2 +%endmacro + +INIT_XMM sse2 +VT_U8 +VT_U16 +VT_F32 + +%if HAVE_AVX2_EXTERNAL +INIT_YMM avx2 +VT_U8 +VT_U16 +VT_F32 +VT_F16 +%endif + +%if HAVE_AVX512_EXTERNAL +INIT_ZMM avx512 +VT_U8 +VT_U16 +VT_F32 +VT_F16 +%endif + +%endif ; ARCH_X86_64 diff --git a/libavfilter/x86/vf_vtinterlace_init.c b/libavfilter/x86/vf_vtinterlace_init.c new file mode 100644 index 00000000000..a1afa06ff3f --- /dev/null +++ b/libavfilter/x86/vf_vtinterlace_init.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2026 Phillip Blucas + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/attributes.h" +#include "libavutil/cpu.h" +#include "libavutil/x86/cpu.h" +#include "libavfilter/vtinterlacedsp.h" + +#define DECL_KERNEL(type, cpu) \ +void ff_vtinterlace_line_##type##_##cpu(void *dst, const void *const *src, \ + int width, int peak, const void *coefs); + +DECL_KERNEL(u8, sse2) +DECL_KERNEL(u16, sse2) +DECL_KERNEL(f32, sse2) +DECL_KERNEL(u8, avx2) +DECL_KERNEL(u16, avx2) +DECL_KERNEL(f32, avx2) +DECL_KERNEL(f16, avx2) +DECL_KERNEL(u8, avx512) +DECL_KERNEL(u16, avx512) +DECL_KERNEL(f32, avx512) +DECL_KERNEL(f16, avx512) + +/* min_width: output samples per vector */ +#define SET(type, cpu, pixels) \ + do { \ + dsp->filter_line = ff_vtinterlace_line_##type##_##cpu; \ + dsp->min_width = pixels; \ + } while (0) + +av_cold void ff_vtinterlace_init_x86(VTInterlaceDSPContext *dsp, enum VTInterlaceKind kind) +{ +#if ARCH_X86_64 + int cpu_flags = av_get_cpu_flags(); + + switch (kind) { + case VTI_KIND_U8: + if (EXTERNAL_SSE2(cpu_flags)) + SET(u8, sse2, 16); + if (EXTERNAL_AVX2_FAST(cpu_flags)) + SET(u8, avx2, 32); + if (EXTERNAL_AVX512(cpu_flags)) + SET(u8, avx512, 64); + break; + case VTI_KIND_U16: + if (EXTERNAL_SSE2(cpu_flags)) + SET(u16, sse2, 8); + if (EXTERNAL_AVX2_FAST(cpu_flags)) + SET(u16, avx2, 16); + if (EXTERNAL_AVX512(cpu_flags)) + SET(u16, avx512, 32); + break; + case VTI_KIND_F32: + if (EXTERNAL_SSE2(cpu_flags)) + SET(f32, sse2, 4); + if (EXTERNAL_AVX2_FAST(cpu_flags) && EXTERNAL_FMA3(cpu_flags)) + SET(f32, avx2, 8); + if (EXTERNAL_AVX512(cpu_flags)) + SET(f32, avx512, 16); + break; + case VTI_KIND_F16: + /* F16C has no CPU flag of its own; every AVX2 CPU implements it. + * There is no SSE2 version, the C one covers it. */ + if (EXTERNAL_AVX2_FAST(cpu_flags) && EXTERNAL_FMA3(cpu_flags)) + SET(f16, avx2, 8); + if (EXTERNAL_AVX512(cpu_flags)) + SET(f16, avx512, 16); + break; + default: + break; + } +#endif +} -- 2.54.0 >From 753ea27b43d2c4162ad9d8e3329e85c642e08a37 Mon Sep 17 00:00:00 2001 From: Phillip Blucas <[email protected]> Date: Wed, 11 Nov 2026 02:51:33 +0000 Subject: [PATCH 3/3] checkasm: add vtinterlace test Tests the vertical-temporal line kernel of the vtinterlace filter for u8, u16, f32 and f16 samples over widths from 1 to 1920 (each SIMD version from its minimum width), all five coefficient sets, several peaks, misaligned source lines and no writes past width. Integer output must be bit-exact; float output is compared with a small ulp budget, as FMA accumulation rounds once per tap less than the C sum. Ported from https://github.com/ifb/vapoursynth-interlace 41bb21b (relicensed to LGPL-2.1-or-later in f69ff68). --- tests/checkasm/Makefile | 1 + tests/checkasm/checkasm.c | 3 + tests/checkasm/checkasm.h | 1 + tests/checkasm/vf_vtinterlace.c | 170 ++++++++++++++++++++++++++++++++ tests/fate/checkasm.mak | 1 + 5 files changed, 176 insertions(+) create mode 100644 tests/checkasm/vf_vtinterlace.c diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile index ab1cc17b56c..118a360a91e 100644 --- a/tests/checkasm/Makefile +++ b/tests/checkasm/Makefile @@ -86,6 +86,7 @@ AVFILTEROBJS-$(CONFIG_PP7_FILTER) += vf_pp7.o AVFILTEROBJS-$(CONFIG_THRESHOLD_FILTER) += vf_threshold.o AVFILTEROBJS-$(CONFIG_NLMEANS_FILTER) += vf_nlmeans.o AVFILTEROBJS-$(CONFIG_SOBEL_FILTER) += vf_convolution.o +AVFILTEROBJS-$(CONFIG_VTINTERLACE_FILTER) += vf_vtinterlace.o CHECKASMOBJS-$(CONFIG_AVFILTER) += $(AVFILTEROBJS-yes) diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c index e50d2885a3a..e76a0c78c3d 100644 --- a/tests/checkasm/checkasm.c +++ b/tests/checkasm/checkasm.c @@ -300,6 +300,9 @@ static const CheckasmTest tests[] = { #if CONFIG_SOBEL_FILTER { "vf_sobel", checkasm_check_vf_sobel }, #endif + #if CONFIG_VTINTERLACE_FILTER + { "vf_vtinterlace", checkasm_check_vf_vtinterlace }, + #endif #endif #if CONFIG_SWSCALE { "sw_gbrp", checkasm_check_sw_gbrp }, diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h index 0fc6823c4fb..bdef55568f9 100644 --- a/tests/checkasm/checkasm.h +++ b/tests/checkasm/checkasm.h @@ -123,6 +123,7 @@ void checkasm_check_vf_limiter(void); void checkasm_check_vf_pp7(void); void checkasm_check_vf_threshold(void); void checkasm_check_vf_sobel(void); +void checkasm_check_vf_vtinterlace(void); void checkasm_check_vp3dsp(void); void checkasm_check_vp6dsp(void); void checkasm_check_vp8dsp(void); diff --git a/tests/checkasm/vf_vtinterlace.c b/tests/checkasm/vf_vtinterlace.c new file mode 100644 index 00000000000..619bc23645f --- /dev/null +++ b/tests/checkasm/vf_vtinterlace.c @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2026 Phillip Blucas + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <math.h> +#include <stdint.h> +#include <string.h> + +#include "checkasm.h" +#include "libavfilter/vtinterlacedsp.h" + +#define MAXW 1920 +#define PAD 64 +#define ROWB ((MAXW + 128) * 4) /* bytes per source row, any sample type */ + +static CHECKASM_ALIGN(uint8_t src_buf[VTI_SRC_PTRS][ROWB]); +static CHECKASM_ALIGN(uint8_t dst_c[(MAXW + PAD) * 4]); +static CHECKASM_ALIGN(uint8_t dst_a[(MAXW + PAD) * 4]); + +static const int widths[] = { + 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 255, + 359, 360, 719, 720, 960, 1279, 1919, 1920, +}; + +static const char *const kind_names[VTI_KIND_NB] = { "u8", "u16", "f32", "f16" }; +static const int kind_bytes[VTI_KIND_NB] = { 1, 2, 4, 2 }; + +/* deterministic sample-aligned misalignment per row and run */ +static void setup_ptrs(const void *ptrs[VTI_SRC_PTRS], int run, int bytes) +{ + for (int i = 0; i < VTI_SRC_PTRS - 1; i++) + ptrs[i] = src_buf[i] + ((i * 7 + run * 13) % 32) * bytes; + ptrs[VTI_SRC_PTRS - 1] = ptrs[VTI_SRC_PTRS - 2]; +} + +static void init_src(enum VTInterlaceKind kind, int phase) +{ + INITIALIZE_BUF(src_buf); + if (kind == VTI_KIND_F32) { + /* finite floats: random sign/mantissa, exponent pinned to [0.5,1) */ + uint32_t *bits = (uint32_t *)src_buf; + for (size_t i = 0; i < sizeof(src_buf) / 4; i++) + bits[i] = (bits[i] & 0x807fffffu) | 0x3f000000u; + } else if (kind == VTI_KIND_F16) { + uint16_t *h = (uint16_t *)src_buf; + for (size_t i = 0; i < sizeof(src_buf) / 2; i++) { + if (phase) /* +/-[0.5,1): tight fused/unfused bound */ + h[i] = (h[i] & 0x83ff) | 0x3800; + else if ((h[i] & 0x7c00) == 0x7c00) /* finite full range */ + h[i] ^= 0x0400; + } + } + /* integer buffers stay full-range: every peak must clamp like C */ +} + +static float f16_value(uint16_t h) +{ + int e = (h >> 10) & 0x1f; + float m = (float)((h & 0x3ff) | (e ? 0x400 : 0)); + float v = ldexpf(m, (e ? e : 1) - 25); + return h & 0x8000 ? -v : v; +} + +static void check_dst(enum VTInterlaceKind kind, int w, int phase) +{ + const int bytes = kind_bytes[kind]; + + switch (kind) { + case VTI_KIND_U8: + checkasm_check1d(uint8_t, dst_c, dst_a, w, "dst"); + break; + case VTI_KIND_U16: + checkasm_check1d(uint16_t, (uint16_t *)dst_c, (uint16_t *)dst_a, w, "dst"); + break; + case VTI_KIND_F32: { + /* AVX2 up accumulates with FMA: one rounding fewer per tap than + * the C sum, so compare with a small ULP budget */ + const float *fc = (const float *)dst_c, *fa = (const float *)dst_a; + for (int j = 0; j < w; j++) { + if (!checkasm_float_near_abs_eps_ulp(fc[j], fa[j], 2e-6f, 64)) { + checkasm_fail_func("dst[%d]: %.9g vs %.9g", j, fc[j], fa[j]); + return; + } + } + break; + } + case VTI_KIND_F16: { + /* FMA vs the unfused C sum differs by up to one f32 rounding per + * tap, which cancellation can magnify past the f16 ulp of the + * result; accept one f16 step or the fused/unfused bound */ + const uint16_t *hc = (const uint16_t *)dst_c, *ha = (const uint16_t *)dst_a; + const float eps = phase ? 3e-5f : 2.0f; + for (int j = 0; j < w; j++) { + int ka = ha[j] & 0x8000 ? -(ha[j] & 0x7fff) : ha[j]; + int kc = hc[j] & 0x8000 ? -(hc[j] & 0x7fff) : hc[j]; + float dv = f16_value(ha[j]) - f16_value(hc[j]); + if ((ka - kc > 1 || kc - ka > 1) && (dv > eps || dv < -eps)) { + checkasm_fail_func("dst[%d]: 0x%04x vs 0x%04x", j, hc[j], ha[j]); + return; + } + } + break; + } + default: + return; + } + /* nothing may be written past width */ + if (memcmp(dst_c + w * bytes, dst_a + w * bytes, PAD * bytes)) + checkasm_fail_func("write past width %d", w); +} + +static void check_kind(enum VTInterlaceKind kind) +{ + static const int peaks[] = { 511, 1023, 4095, 65535 }; + const int bytes = kind_bytes[kind]; + const int nb_phases = kind == VTI_KIND_F16 ? 2 : 1; + const void *ptrs[VTI_SRC_PTRS]; + VTInterlaceDSPContext dsp; + + checkasm_declare(void, void *, const void *const *, int, int, const void *); + + ff_vtinterlace_dsp_init(&dsp, kind); + + if (checkasm_check_func(dsp.filter_line, "vtinterlace_line_%s", kind_names[kind])) { + for (int phase = 0; phase < nb_phases; phase++) { + init_src(kind, phase); + for (int i = 0; i < FF_ARRAY_ELEMS(widths); i++) { + const int w = widths[i]; + const int peak = kind == VTI_KIND_U8 ? 255 : + kind == VTI_KIND_U16 ? peaks[i % FF_ARRAY_ELEMS(peaks)] : 0; + const void *coefs = ff_vtinterlace_coefs(kind, i % VTI_SHIFT_NB); + + if (w < dsp.min_width) + continue; + setup_ptrs(ptrs, i, bytes); + memset(dst_c, 0xaa, sizeof(dst_c)); + memset(dst_a, 0xaa, sizeof(dst_a)); + checkasm_call_ref(dst_c, ptrs, w, peak, coefs); + checkasm_call_new(dst_a, ptrs, w, peak, coefs); + check_dst(kind, w, phase); + } + } + setup_ptrs(ptrs, 0, bytes); + checkasm_bench_new(dst_a, ptrs, MAXW, kind == VTI_KIND_U8 ? 255 : 1023, + ff_vtinterlace_coefs(kind, VTI_SHIFT_CENTER)); + } + checkasm_report("vtinterlace_line_%s", kind_names[kind]); +} + +void checkasm_check_vf_vtinterlace(void) +{ + for (int kind = 0; kind < VTI_KIND_NB; kind++) + check_kind(kind); +} diff --git a/tests/fate/checkasm.mak b/tests/fate/checkasm.mak index 19bbe7e571a..76c806b8e53 100644 --- a/tests/fate/checkasm.mak +++ b/tests/fate/checkasm.mak @@ -92,6 +92,7 @@ FATE_CHECKASM = fate-checkasm-aacencdsp \ fate-checkasm-vf_pp7 \ fate-checkasm-vf_threshold \ fate-checkasm-vf_sobel \ + fate-checkasm-vf_vtinterlace \ fate-checkasm-videodsp \ fate-checkasm-vorbisdsp \ fate-checkasm-vp3dsp \ -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
