PR #26276 opened by superuser404
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26276
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26276.patch
This adds `bwdif_videotoolbox`, a Metal port of bwdif for VideoToolbox frames
next to `yadif_videotoolbox`. Its output is bit-identical to the CPU `bwdif`
filter.
We use it in a tvOS player for 1080i broadcast TV (HDHomeRun tuners, IPTV).
Frames come out of VideoToolbox, and today the choice is `yadif_videotoolbox`
on the GPU or a download for CPU `bwdif`. classicjazz wrote the kernel and the
first version of the wrapper. It is the default deinterlacer in our current
Apple TV beta, where classicjazz has been watching 1080i sports with it on an
Apple TV 4K.
### The kernel
It does the integer arithmetic of `bwdifdsp.c`, held in floats. GPUs run float
math faster than 32-bit integer math, and a float holds every integer up to
2^24 exactly.
- Each texel is read back as the integer sample the CPU filter sees (`scale` is
255 or 65535, `shift` is 6 for P010).
- For samples of up to 10 bits the largest intermediate value is about 15.6
million, so nothing is rounded. Each `x >> n` becomes `floor(x * 2^-n)`, which
matches C on negative values too.
- Rows follow `vf_bwdif.c`: the kept field is copied, the last field of the
stream goes through `filter_intra`, the top and bottom four rows through
`filter_edge`, all others through `filter_line`.
- Formats deeper than 10 bits would leave the exact range, so they are refused.
Kernel GPU time alone for both planes of a 1080 frame, measured by classicjazz
on an M1 Max: 0.138 ms NV12 / 0.147 ms P010, against 0.113 / 0.125 ms for the
yadif kernel. Plain integer math in the same kernel took 0.178 / 0.185 ms.
### The wrapper
It is `vf_yadif_videotoolbox.m` with bwdif's options and defaults (`mode`
defaults to `send_field`, like CPU bwdif). Two differences:
- All planes of a frame go into one command buffer, with one commit and one
wait, and the parameters go by value through `setBytes`. This is the same
change #26244 proposes for `yadif_videotoolbox`; a failed command buffer
returns `AVERROR_EXTERNAL`.
- Formats and plane sizes the kernel cannot run (not planar, more than two
components per plane, deeper than 10 bits, planes under 3x4) are refused in
`config_output`, before the output pool is allocated, instead of failing on
every frame.
### Testing (macOS arm64, M1)
framemd5 of these two chains, compared frame by frame:
```
CPU: format=yuv420p,bwdif=<opts>,format=yuv420p
GPU:
format=yuv420p,format=nv12,hwupload,bwdif_videotoolbox=<opts>,hwdownload,format=nv12,format=yuv420p
```
(and the same with `yuv420p10le` / `p010le`), with `-init_hw_device
videotoolbox=vt -filter_hw_device vt`.
| Input | Variants | Result |
|---|---|---|
| testsrc2 1920x1080, `setfield=tff` / `bff` | NV12 and P010, send_field and
send_frame, parity auto and forced | identical (480 / 240 frames) |
| testsrc2 38x10 | NV12 and P010, both modes | identical (200 / 100 frames) |
| `mpeg2/xdcam8mp2-1s_small.ts` | both modes | identical |
| `mpeg2/matrixbench_mpeg2.lq1.mpg` | both modes | identical |
| `mpeg2/mpeg2_field_encoding.ts`, `deint=interlaced` | both modes | identical
except the frames built from the last picture, see below |
Control: `yadif_videotoolbox` against CPU `bwdif` differs on all 480 frames, so
the comparison does catch differences.
The last picture of `mpeg2_field_encoding.ts` is damaged (`ac-tex damaged at 0
22`), and the decoder leaves its concealed area different from run to run
depending on the filter chain. Two plain decodes, `format=yuv420p` against
`format=yuv420p,format=nv12,format=yuv420p`, already give different hashes for
that picture, so bwdif gets different input there.
Before this branch, classicjazz checked the kernel alone against `bwdifdsp.c`
built from n8.1.3: 1,280 cases (8 and 10 bit, one and two channels, sizes from
37x4 to 960x540, both parities and field orders, the end-of-stream field), 167
million samples, 0 differences. The same whole-filter comparison matched on a
31 s off-air ATSC 1080i capture.
On an M1, a 1080p59.94 field costs 2.19 ms in `bwdif_videotoolbox` against 1.90
ms in `yadif_videotoolbox` (with #26244) in the same run. Against a progressive
original, interlaced and deinterlaced at field rate, bwdif scored 0.8 and 1.2
dB higher luma PSNR on two film scenes.
`fate-source` passes. I could not build the docs here (no `makeinfo`).
### Not in this PR
With `deint=interlaced`, a frame not marked interlaced is passed through by
`yadif_common.c` with the input's `hw_frames_ctx`, and a following `hwdownload`
rejects it ("Input frame is not the in the configured hwframe context").
`yadif_videotoolbox` on master does the same, so I left it for a separate patch.
While reading the other GPU ports, classicjazz noticed that `bwdif_vulkan`'s
shader never loads `next2[4]` when `field_parity` is false although the
high-frequency term reads it, and that `bwdif_cuda` takes the temporal average
`d` from `prev` and `next` in every field, where the CPU and Vulkan versions
use the frames that hold the row's field. Neither is built here, so neither is
tested.
>From 96ed538b56ca5a59bb31cbb149817f516c4dc44a Mon Sep 17 00:00:00 2001
From: Vincent Herbst <[email protected]>
Date: Sun, 22 Nov 2026 10:37:50 +0100
Subject: [PATCH] avfilter: add bwdif_videotoolbox
A Metal port of bwdif for VideoToolbox frames, next to
yadif_videotoolbox, so a VideoToolbox pipeline can use bwdif without
leaving the GPU.
The kernel does the integer arithmetic of bwdifdsp.c, held in floats.
GPUs run float math faster than 32-bit integer math, and a float holds
every integer up to 2^24 exactly. For samples of up to 10 bits the
largest intermediate value is about 15.6 million, so nothing is rounded,
and each x >> n becomes floor(x * 2^-n), which matches C on negative
values too. The rows follow vf_bwdif.c: the kept field is copied, the
last field of the stream is interpolated spatially only (filter_intra),
the top and bottom four rows take the filter_edge path, and all other
rows the full filter_line. Formats deeper than 10 bits would leave the
exact range, so they are refused.
The wrapper is vf_yadif_videotoolbox.m with bwdif's options and
defaults. All planes of a frame go into one command buffer with one
commit and one wait, and formats or plane sizes the kernel cannot run
are refused in config_output, before the output pool is allocated.
The output is identical to the CPU filter: framemd5 of CPU bwdif and
bwdif_videotoolbox match on NV12 and P010, send_field and send_frame,
TFF, BFF and forced parity, testsrc2 at 1920x1080 and 38x10, and the
MPEG-2 samples mpeg2/xdcam8mp2-1s_small.ts and
mpeg2/matrixbench_mpeg2.lq1.mpg. On an M1 a 1080p59.94 field takes
2.19 ms, against 1.90 ms for yadif_videotoolbox.
Co-authored-by: classicjazz <[email protected]>
Signed-off-by: Vincent Herbst <[email protected]>
---
Changelog | 1 +
configure | 1 +
doc/filters.texi | 71 +++
libavfilter/Makefile | 4 +
libavfilter/allfilters.c | 1 +
libavfilter/metal/vf_bwdif_videotoolbox.metal | 182 +++++++
libavfilter/version.h | 2 +-
libavfilter/vf_bwdif_videotoolbox.m | 499 ++++++++++++++++++
8 files changed, 760 insertions(+), 1 deletion(-)
create mode 100644 libavfilter/metal/vf_bwdif_videotoolbox.metal
create mode 100644 libavfilter/vf_bwdif_videotoolbox.m
diff --git a/Changelog b/Changelog
index 8731d005142..fb76b3cffbc 100644
--- a/Changelog
+++ b/Changelog
@@ -9,6 +9,7 @@ version <next>:
- PCM PAMF decoder
- Super Audio CD (SACD) demuxer
- DSS variable rate SP (format 1) support
+- bwdif_videotoolbox filter
version 9.1:
- ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0
muxers and demuxers
diff --git a/configure b/configure
index 5f1bcebf5b6..7147f8f190c 100755
--- a/configure
+++ b/configure
@@ -4155,6 +4155,7 @@ boxblur_opencl_filter_deps="opencl gpl"
bs2b_filter_deps="libbs2b"
bwdif_cuda_filter_deps="ffnvcodec"
bwdif_cuda_filter_deps_any="cuda_nvcc cuda_llvm"
+bwdif_videotoolbox_filter_deps="metal corevideo videotoolbox"
bwdif_vulkan_filter_deps="vulkan spirv_compiler"
chromaber_vulkan_filter_deps="vulkan spirv_compiler"
color_vulkan_filter_deps="vulkan spirv_compiler"
diff --git a/doc/filters.texi b/doc/filters.texi
index 84993c59f3c..926e07ec574 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -29954,6 +29954,77 @@ VideoToolbox filter depends on VideoToolbox framework,
and is auto detected
when building ffmpeg for an Apple platform such as macOS. Add
@code{--enable-videotoolbox} to configure if autodetect is disabled.
+@section bwdif_videotoolbox
+
+Deinterlace the input video using the @ref{bwdif} algorithm, implemented as a
+Metal compute kernel, so that it can work as part of a GPU accelerated
+pipeline with VideoToolbox decoding and encoding. The output is identical to
+the CPU @ref{bwdif} filter.
+
+The kernel accepts planar formats with up to two components per plane and up
+to 10 bits per sample, such as @code{nv12} and @code{p010}. Deeper formats
+are refused.
+
+It accepts the following parameters:
+
+@table @option
+@item mode
+The interlacing mode to adopt. It accepts one of the following values:
+
+@table @option
+@item 0, send_frame
+Output one frame for each frame.
+@item 1, send_field
+Output one frame for each field.
+@end table
+
+The default value is @code{send_field}.
+
+@item parity
+The picture field parity assumed for the input interlaced video. It accepts one
+of the following values:
+
+@table @option
+@item 0, tff
+Assume the top field is first.
+@item 1, bff
+Assume the bottom field is first.
+@item -1, auto
+Enable automatic detection of field parity.
+@end table
+
+The default value is @code{auto}.
+If the interlacing is unknown or the decoder does not export this information,
+top field first will be assumed.
+
+@item deint
+Specify which frames to deinterlace. Accepts one of the following
+values:
+
+@table @option
+@item 0, all
+Deinterlace all frames.
+@item 1, interlaced
+Only deinterlace frames marked as interlaced.
+@end table
+
+The default value is @code{all}.
+@end table
+
+@subsection Example
+
+@itemize
+@item
+Decode with VideoToolbox, deinterlace at field rate and encode with
+VideoToolbox:
+@example
+ffmpeg -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld \
+ -i interlaced.ts \
+ -vf bwdif_videotoolbox=mode=send_field:deint=interlaced \
+ -c:v hevc_videotoolbox -b:v 8M out.mp4
+@end example
+@end itemize
+
@section scale_vt
Scale and convert the color parameters using VTPixelTransferSession.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 8f3c64459cb..390cec26f86 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -223,6 +223,10 @@ OBJS-$(CONFIG_BOXBLUR_OPENCL_FILTER) +=
vf_avgblur_opencl.o opencl.o \
OBJS-$(CONFIG_BWDIF_FILTER) += vf_bwdif.o bwdifdsp.o
yadif_common.o
OBJS-$(CONFIG_BWDIF_CUDA_FILTER) += vf_bwdif_cuda.o
vf_bwdif_cuda.ptx.o \
yadif_common.o
cuda/load_helper.o
+OBJS-$(CONFIG_BWDIF_VIDEOTOOLBOX_FILTER) += vf_bwdif_videotoolbox.o \
+
metal/vf_bwdif_videotoolbox.metallib.o \
+ metal/utils.o \
+ yadif_common.o
OBJS-$(CONFIG_BWDIF_VULKAN_FILTER) += vf_bwdif_vulkan.o
yadif_common.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_CAS_FILTER) += vf_cas.o
OBJS-$(CONFIG_CCREPACK_FILTER) += vf_ccrepack.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 9f07bf83dce..5607dc8fd39 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -204,6 +204,7 @@ extern const FFFilter ff_vf_boxblur;
extern const FFFilter ff_vf_boxblur_opencl;
extern const FFFilter ff_vf_bwdif;
extern const FFFilter ff_vf_bwdif_cuda;
+extern const FFFilter ff_vf_bwdif_videotoolbox;
extern const FFFilter ff_vf_bwdif_vulkan;
extern const FFFilter ff_vf_cas;
extern const FFFilter ff_vf_ccrepack;
diff --git a/libavfilter/metal/vf_bwdif_videotoolbox.metal
b/libavfilter/metal/vf_bwdif_videotoolbox.metal
new file mode 100644
index 00000000000..6a41590dabe
--- /dev/null
+++ b/libavfilter/metal/vf_bwdif_videotoolbox.metal
@@ -0,0 +1,182 @@
+/*
+ * BWDIF for VideoToolbox frames, as a Metal compute kernel.
+ *
+ * The arithmetic is the integer arithmetic of libavfilter/bwdifdsp.c, done on
+ * the sample values the CPU filter sees, so the output matches the CPU bwdif
+ * filter bit for bit, edge rows and the end-of-stream field included.
+ * The arithmetic and coefficients are those of bwdifdsp.c (Thomas Mundt),
+ * which implements the BBC R&D Weston 3 field deinterlacer on top of yadif.
+ * The texture plumbing follows vf_yadif_videotoolbox.metal (Philip Langdale,
+ * Aman Karmani, Stefan Dyulgerov).
+ *
+ * The integer values are held in floats, which GPUs run faster than 32-bit
+ * integer math. A float holds every integer up to 2^24 exactly; for samples of
+ * up to 10 bits every sum and product here stays below that (the largest is
+ * about 15.6 million, in the high-frequency term), and each right shift
+ * x >> n is floor(x * 2^-n), which matches C's >> on negative values too.
+ * Deeper samples would overflow that range, so the filter refuses them.
+ *
+ * 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 <metal_stdlib>
+#include <metal_integer>
+#include <metal_texture>
+
+using namespace metal;
+
+/*
+ * Parameters for the deint kernel. Must match struct mtlBwdifParams in
+ * vf_bwdif_videotoolbox.m.
+ *
+ * parity: the field kept as is (rows with y % 2 == parity are copied).
+ * tff: top field first.
+ * field_end: the last field of the stream, interpolated spatially only.
+ * shift: how far the sample sits above bit 0 in its container (6 for
p010).
+ * scale: the largest container value (255 or 65535), to read unorm texels
+ * back as integers.
+ * clip_max: the largest sample value, (1 << depth) - 1.
+ */
+struct deintParams {
+ uint channels;
+ uint parity;
+ uint tff;
+ uint field_end;
+ uint shift;
+ uint scale;
+ int clip_max;
+};
+
+/* BBC PH-2071 (Weston 3 field deinterlacer) coefficients, as in bwdifdsp.c. */
+constant float coef_lf[2] = { 4309.0f, 213.0f };
+constant float coef_hf[3] = { 5570.0f, 3801.0f, 1016.0f };
+constant float coef_sp[2] = { 5077.0f, 981.0f };
+
+typedef texture2d<float, access::read> src_tex;
+
+/* One texel as integer samples; .y is 0 on a single-channel plane. */
+static inline float2 load(src_tex tex, uint x, int y, constant deintParams& p)
+{
+ float2 v = tex.read(uint2(x, uint(y))).xy;
+ return floor(rint(v * float(p.scale)) * (1.0f / float(1u << p.shift)));
+}
+
+static inline float2 store_value(float2 v, constant deintParams& p)
+{
+ return v * float(1u << p.shift) / float(p.scale);
+}
+
+static inline float2 bw_max3(float2 a, float2 b, float2 c) { return max(a,
max(b, c)); }
+static inline float2 bw_min3(float2 a, float2 b, float2 c) { return min(a,
min(b, c)); }
+
+kernel void deint(
+ texture2d<float, access::write> dst [[texture(0)]],
+ src_tex prev [[texture(1)]],
+ src_tex cur [[texture(2)]],
+ src_tex next [[texture(3)]],
+ constant deintParams& p [[buffer(4)]],
+ uint2 pos [[thread_position_in_grid]])
+{
+ const int w = int(dst.get_width());
+ const int h = int(dst.get_height());
+ const uint x = pos.x;
+ const int y = int(pos.y);
+
+ if (int(x) >= w || y >= h)
+ return;
+
+ // The kept field passes through unchanged.
+ if (uint(y) % 2 == p.parity) {
+ dst.write(cur.read(pos), pos);
+ return;
+ }
+
+ const float2 zero = float2(0.0f);
+ const float2 cmax = float2(float(p.clip_max));
+
+ // Neighbouring rows, mirrored at the frame edges exactly as vf_bwdif.c
does.
+ const int yp1 = (y + 1) < h ? y + 1 : y - 1;
+ const int ym1 = y > 0 ? y - 1 : y + 1;
+
+ if (p.field_end) {
+ const int yp3 = (y + 3) < h ? y + 3 : y - 1;
+ const int ym3 = y > 2 ? y - 3 : y + 1;
+ float2 interpol = floor((coef_sp[0] * (load(cur, x, ym1, p) +
load(cur, x, yp1, p))
+ - coef_sp[1] * (load(cur, x, ym3, p) + load(cur, x,
yp3, p))) * (1.0f / 8192.0f));
+ dst.write(store_value(clamp(interpol, zero, cmax), p).xyyy, pos);
+ return;
+ }
+
+ // In bwdifdsp.c's names, prev2 / next2 are the frames holding this row's
+ // field, before and after, and prev / next are the frames either side of
cur.
+ const bool prev2_is_prev = (p.parity ^ p.tff) != 0;
+ src_tex prev2 = prev2_is_prev ? prev : cur;
+ src_tex next2 = prev2_is_prev ? cur : next;
+
+ const float2 c = load(cur, x, ym1, p);
+ const float2 e = load(cur, x, yp1, p);
+ const float2 p2_0 = load(prev2, x, y, p);
+ const float2 n2_0 = load(next2, x, y, p);
+ const float2 d = floor((p2_0 + n2_0) * 0.5f);
+
+ const float2 temporal_diff0 = abs(p2_0 - n2_0);
+ const float2 temporal_diff1 = floor((abs(load(prev, x, ym1, p) - c) +
abs(load(prev, x, yp1, p) - e)) * 0.5f);
+ const float2 temporal_diff2 = floor((abs(load(next, x, ym1, p) - c) +
abs(load(next, x, yp1, p) - e)) * 0.5f);
+ float2 diff = bw_max3(floor(temporal_diff0 * 0.5f), temporal_diff1,
temporal_diff2);
+ // bwdifdsp.c returns the temporal average before the spatial check when
+ // nothing moved, so this test uses diff as it stands here.
+ const bool2 still = diff == float2(0.0f);
+
+ const bool edge = (y < 4) || ((y + 5) > h);
+ // filter_edge runs the spatial check only away from the outermost rows.
+ const bool spat = !edge || !((y < 2) || ((y + 3) > h));
+
+ float2 interpol;
+ if (spat) {
+ const float2 p2_m2 = load(prev2, x, y - 2, p);
+ const float2 n2_m2 = load(next2, x, y - 2, p);
+ const float2 p2_p2 = load(prev2, x, y + 2, p);
+ const float2 n2_p2 = load(next2, x, y + 2, p);
+ const float2 b = floor((p2_m2 + n2_m2) * 0.5f) - c;
+ const float2 f = floor((p2_p2 + n2_p2) * 0.5f) - e;
+ const float2 dc = d - c;
+ const float2 de = d - e;
+ const float2 mx = bw_max3(de, dc, min(b, f));
+ const float2 mn = bw_min3(de, dc, max(b, f));
+ diff = bw_max3(diff, mn, -mx);
+
+ if (!edge) {
+ const float2 c3 = load(cur, x, y - 3, p) + load(cur, x, y + 3, p);
+ const float2 hf = floor((coef_hf[0] * (p2_0 + n2_0)
+ - coef_hf[1] * (p2_m2 + n2_m2 + p2_p2 + n2_p2)
+ + coef_hf[2] * (load(prev2, x, y - 4, p) +
load(next2, x, y - 4, p)
+ + load(prev2, x, y + 4, p) +
load(next2, x, y + 4, p))) * 0.25f);
+ const float2 temporal = floor((hf + coef_lf[0] * (c + e) -
coef_lf[1] * c3) * (1.0f / 8192.0f));
+ const float2 spatial = floor((coef_sp[0] * (c + e) - coef_sp[1] *
c3) * (1.0f / 8192.0f));
+ interpol = select(spatial, temporal, abs(c - e) > temporal_diff0);
+ } else {
+ interpol = floor((c + e) * 0.5f);
+ }
+ } else {
+ interpol = floor((c + e) * 0.5f);
+ }
+
+ interpol = clamp(interpol, d - diff, d + diff);
+ interpol = clamp(interpol, zero, cmax);
+ const float2 result = select(interpol, d, still);
+ dst.write(store_value(result, p).xyyy, pos);
+}
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 64cd692ab67..537df129cd3 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
#include "version_major.h"
-#define LIBAVFILTER_VERSION_MINOR 12
+#define LIBAVFILTER_VERSION_MINOR 13
#define LIBAVFILTER_VERSION_MICRO 100
diff --git a/libavfilter/vf_bwdif_videotoolbox.m
b/libavfilter/vf_bwdif_videotoolbox.m
new file mode 100644
index 00000000000..9b2970e28cf
--- /dev/null
+++ b/libavfilter/vf_bwdif_videotoolbox.m
@@ -0,0 +1,499 @@
+/*
+ * Copyright (C) 2018 Philip Langdale <[email protected]>
+ * 2020 Aman Karmani <[email protected]>
+ *
+ * BWDIF for VideoToolbox frames using Metal compute.
+ *
+ * Based on vf_yadif_videotoolbox.m; the frame handling is shared with every
+ * yadif-family filter through yadif_common.c, and the kernel reproduces
+ * bwdifdsp.c.
+ *
+ * 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 "filters.h"
+#include "metal/utils.h"
+#include "yadif.h"
+#include "libavutil/avassert.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_videotoolbox.h"
+#include "libavutil/objc.h"
+
+#include <assert.h>
+
+extern char ff_vf_bwdif_videotoolbox_metallib_data[];
+extern unsigned int ff_vf_bwdif_videotoolbox_metallib_len;
+
+typedef struct API_AVAILABLE(macos(10.11), ios(8.0)) BWDIFVTContext {
+ YADIFContext yadif;
+
+ AVBufferRef *device_ref;
+ AVBufferRef *input_frames_ref;
+ AVHWFramesContext *input_frames;
+
+ id<MTLDevice> mtlDevice;
+ id<MTLLibrary> mtlLibrary;
+ id<MTLCommandQueue> mtlQueue;
+ id<MTLComputePipelineState> mtlPipeline;
+ id<MTLFunction> mtlFunction;
+
+ CVMetalTextureCacheRef textureCache;
+} BWDIFVTContext API_AVAILABLE(macos(10.11), ios(8.0));
+
+// Using sizeof(BWDIFVTContext) outside of an availability check will error
+// if we're targeting an older OS version, so we need to calculate the size
ourselves
+// (we'll statically verify it's correct in bwdif_videotoolbox_init behind a
check)
+#define BWDIF_VT_CTX_SIZE (sizeof(YADIFContext) + sizeof(void*) * 9)
+
+// Must match struct deintParams in metal/vf_bwdif_videotoolbox.metal.
+struct mtlBwdifParams {
+ uint channels;
+ uint parity;
+ uint tff;
+ uint field_end;
+ uint shift;
+ uint scale;
+ int clip_max;
+};
+
+// All planes of a frame are encoded into one command buffer, so the frame
+// goes to the GPU once and waits once.
+static void encode_plane(AVFilterContext *ctx,
+ id<MTLComputeCommandEncoder> encoder,
+ id<MTLTexture> dst,
+ id<MTLTexture> prev,
+ id<MTLTexture> cur,
+ id<MTLTexture> next,
+ int channels,
+ int pixel_size,
+ const AVComponentDescriptor *comp,
+ int parity,
+ int tff) API_AVAILABLE(macos(10.11), ios(8.0))
+{
+ BWDIFVTContext *s = ctx->priv;
+ struct mtlBwdifParams params = {
+ .channels = channels,
+ .parity = parity,
+ .tff = tff,
+ .field_end = s->yadif.current_field == YADIF_FIELD_END,
+ .shift = comp->shift,
+ .scale = pixel_size == 1 ? 0xFF : 0xFFFF,
+ .clip_max = (1 << comp->depth) - 1,
+ };
+
+ [encoder setTexture:dst atIndex:0];
+ [encoder setTexture:prev atIndex:1];
+ [encoder setTexture:cur atIndex:2];
+ [encoder setTexture:next atIndex:3];
+ [encoder setBytes:¶ms length:sizeof(params) atIndex:4];
+ ff_metal_compute_encoder_dispatch(s->mtlDevice, s->mtlPipeline, encoder,
dst.width, dst.height);
+}
+
+static int filter(AVFilterContext *ctx, AVFrame *dst,
+ int parity, int tff) API_AVAILABLE(macos(10.11), ios(8.0))
+{
+ BWDIFVTContext *s = ctx->priv;
+ YADIFContext *y = &s->yadif;
+ // Four textures per plane, at most four planes. The GPU reads them until
+ // the command buffer completes, so they are released after the wait.
+ CVMetalTextureRef textures[16] = { NULL };
+ int nb_textures = 0, ret = 0;
+
+ @autoreleasepool {
+ id<MTLCommandBuffer> buffer = s->mtlQueue.commandBuffer;
+ id<MTLComputeCommandEncoder> encoder = buffer.computeCommandEncoder;
+
+ for (int i = 0; i < y->csp->nb_components; i++) {
+ int pixel_size, channels;
+ const AVComponentDescriptor *comp = &y->csp->comp[i];
+ CVMetalTextureRef *tex = &textures[nb_textures];
+ MTLPixelFormat format;
+
+ if (comp->plane < i) {
+ // We process planes as a whole, so don't reprocess
+ // them for additional components
+ continue;
+ }
+
+ pixel_size = (comp->depth + comp->shift) / 8;
+ channels = comp->step / pixel_size;
+ if (pixel_size > 2 || channels > 2) {
+ av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
y->csp->name);
+ ret = AVERROR(EINVAL);
+ break;
+ }
+ switch (pixel_size) {
+ case 1:
+ format = channels == 1 ? MTLPixelFormatR8Unorm :
MTLPixelFormatRG8Unorm;
+ break;
+ case 2:
+ format = channels == 1 ? MTLPixelFormatR16Unorm :
MTLPixelFormatRG16Unorm;
+ break;
+ default:
+ av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
y->csp->name);
+ ret = AVERROR(EINVAL);
+ break;
+ }
+ if (ret < 0)
+ break;
+ av_log(ctx, AV_LOG_TRACE,
+ "Deinterlacing plane %d: pixel_size: %d channels: %d\n",
+ comp->plane, pixel_size, channels);
+
+ tex[0] = ff_metal_texture_from_pixbuf(ctx, s->textureCache,
(CVPixelBufferRef)y->prev->data[3], i, format);
+ tex[1] = ff_metal_texture_from_pixbuf(ctx, s->textureCache,
(CVPixelBufferRef)y->cur->data[3], i, format);
+ tex[2] = ff_metal_texture_from_pixbuf(ctx, s->textureCache,
(CVPixelBufferRef)y->next->data[3], i, format);
+ tex[3] = ff_metal_texture_from_pixbuf(ctx, s->textureCache,
(CVPixelBufferRef)dst->data[3], i, format);
+ nb_textures += 4;
+
+ if (!tex[0] || !tex[1] || !tex[2] || !tex[3]) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create Metal texture for
plane %d\n", i);
+ ret = AVERROR_EXTERNAL;
+ break;
+ }
+
+ encode_plane(ctx, encoder,
+ CVMetalTextureGetTexture(tex[3]),
+ CVMetalTextureGetTexture(tex[0]),
+ CVMetalTextureGetTexture(tex[1]),
+ CVMetalTextureGetTexture(tex[2]),
+ channels, pixel_size, comp, parity, tff);
+ }
+
+ [encoder endEncoding];
+ if (ret >= 0) {
+ [buffer commit];
+ [buffer waitUntilCompleted];
+ if (buffer.status == MTLCommandBufferStatusError) {
+ av_log(ctx, AV_LOG_ERROR, "Metal command buffer failed: %s\n",
+ buffer.error.localizedDescription.UTF8String);
+ ret = AVERROR_EXTERNAL;
+ }
+ }
+ }
+
+ for (int i = 0; i < nb_textures; i++) {
+ if (textures[i])
+ CFRelease(textures[i]);
+ }
+ if (ret < 0)
+ return ret;
+
+ CVBufferPropagateAttachments((CVPixelBufferRef)y->cur->data[3],
(CVPixelBufferRef)dst->data[3]);
+
+ if (y->current_field == YADIF_FIELD_END) {
+ y->current_field = YADIF_FIELD_NORMAL;
+ }
+
+ return 0;
+}
+
+static av_cold void do_uninit(AVFilterContext *ctx)
API_AVAILABLE(macos(10.11), ios(8.0))
+{
+ BWDIFVTContext *s = ctx->priv;
+
+ ff_yadif_uninit(ctx);
+
+ av_buffer_unref(&s->device_ref);
+ av_buffer_unref(&s->input_frames_ref);
+ s->input_frames = NULL;
+
+ ff_objc_release(&s->mtlFunction);
+ ff_objc_release(&s->mtlPipeline);
+ ff_objc_release(&s->mtlQueue);
+ ff_objc_release(&s->mtlLibrary);
+ ff_objc_release(&s->mtlDevice);
+
+ if (s->textureCache) {
+ CFRelease(s->textureCache);
+ s->textureCache = NULL;
+ }
+}
+
+
+static av_cold void bwdif_videotoolbox_uninit(AVFilterContext *ctx)
+{
+ if (@available(macOS 10.11, iOS 8.0, *)) {
+ do_uninit(ctx);
+ }
+}
+
+static av_cold int do_init(AVFilterContext *ctx) API_AVAILABLE(macos(10.11),
ios(8.0))
+{
+ BWDIFVTContext *s = ctx->priv;
+ NSError *err = nil;
+ CVReturn ret;
+
+ s->mtlDevice = MTLCreateSystemDefaultDevice();
+ if (!s->mtlDevice) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to find Metal device\n");
+ goto fail;
+ }
+
+ av_log(ctx, AV_LOG_INFO, "Using Metal device: %s\n",
s->mtlDevice.name.UTF8String);
+
+ dispatch_data_t libData = dispatch_data_create(
+ ff_vf_bwdif_videotoolbox_metallib_data,
+ ff_vf_bwdif_videotoolbox_metallib_len,
+ nil,
+ nil);
+ s->mtlLibrary = [s->mtlDevice newLibraryWithData:libData error:&err];
+ dispatch_release(libData);
+ libData = nil;
+ if (err) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to load Metal library: %s\n",
err.description.UTF8String);
+ goto fail;
+ }
+
+ s->mtlFunction = [s->mtlLibrary newFunctionWithName:@"deint"];
+ if (!s->mtlFunction) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create Metal function!\n");
+ goto fail;
+ }
+
+ s->mtlQueue = s->mtlDevice.newCommandQueue;
+ if (!s->mtlQueue) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create Metal command queue!\n");
+ goto fail;
+ }
+
+ s->mtlPipeline = [s->mtlDevice
+ newComputePipelineStateWithFunction:s->mtlFunction
+ error:&err];
+ if (err) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create Metal compute pipeline:
%s\n", err.description.UTF8String);
+ goto fail;
+ }
+
+ ret = CVMetalTextureCacheCreate(
+ NULL,
+ NULL,
+ s->mtlDevice,
+ NULL,
+ &s->textureCache
+ );
+ if (ret != kCVReturnSuccess) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create CVMetalTextureCache:
%d\n", ret);
+ goto fail;
+ }
+
+ return 0;
+fail:
+ bwdif_videotoolbox_uninit(ctx);
+ return AVERROR_EXTERNAL;
+}
+
+static av_cold int bwdif_videotoolbox_init(AVFilterContext *ctx)
+{
+ if (@available(macOS 10.11, iOS 8.0, *)) {
+ // Ensure we calculated BWDIF_VT_CTX_SIZE correctly
+ static_assert(BWDIF_VT_CTX_SIZE == sizeof(BWDIFVTContext), "Incorrect
BWDIF_VT_CTX_SIZE value!");
+ return do_init(ctx);
+ } else {
+ av_log(ctx, AV_LOG_ERROR, "Metal is not available on this OS
version\n");
+ return AVERROR(ENOSYS);
+ }
+}
+
+static int do_config_input(AVFilterLink *inlink) API_AVAILABLE(macos(10.11),
ios(8.0))
+{
+ FilterLink *l = ff_filter_link(inlink);
+ AVFilterContext *ctx = inlink->dst;
+ BWDIFVTContext *s = ctx->priv;
+
+ if (!l->hw_frames_ctx) {
+ av_log(ctx, AV_LOG_ERROR, "A hardware frames reference is "
+ "required to associate the processing device.\n");
+ return AVERROR(EINVAL);
+ }
+
+ s->input_frames_ref = av_buffer_ref(l->hw_frames_ctx);
+ if (!s->input_frames_ref) {
+ av_log(ctx, AV_LOG_ERROR, "A input frames reference create "
+ "failed.\n");
+ return AVERROR(ENOMEM);
+ }
+ s->input_frames = (AVHWFramesContext*)s->input_frames_ref->data;
+
+ return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ if (@available(macOS 10.11, iOS 8.0, *)) {
+ return do_config_input(inlink);
+ } else {
+ av_log(ctx, AV_LOG_ERROR, "Metal is not available on this OS
version\n");
+ return AVERROR(ENOSYS);
+ }
+}
+
+static int do_config_output(AVFilterLink *link) API_AVAILABLE(macos(10.11),
ios(8.0))
+{
+ FilterLink *l = ff_filter_link(link);
+ FilterLink *il = ff_filter_link(link->src->inputs[0]);
+ AVHWFramesContext *output_frames, *input_frames;
+ AVFilterContext *ctx = link->src;
+ BWDIFVTContext *s = ctx->priv;
+ YADIFContext *y = &s->yadif;
+ const AVPixFmtDescriptor *desc;
+ int ret = 0;
+
+ av_assert0(s->input_frames);
+
+ // Refuse what the kernel cannot run before an output pool is allocated,
so the
+ // graph fails at configuration instead of on every frame.
+ desc = av_pix_fmt_desc_get(s->input_frames->sw_format);
+ if (!desc || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) {
+ av_log(ctx, AV_LOG_ERROR, "Pixel format %s is not planar, which is not
supported\n",
+ desc ? desc->name : "unknown");
+ return AVERROR(ENOSYS);
+ }
+ for (int i = 0; i < desc->nb_components; i++) {
+ const AVComponentDescriptor *comp = &desc->comp[i];
+ int pixel_size = (comp->depth + comp->shift) / 8;
+ // The kernel does its integer math in floats, which is exact up to
10-bit samples.
+ if (comp->depth > 10) {
+ av_log(ctx, AV_LOG_ERROR, "Pixel format %s is deeper than 10 bits,
which is not supported\n",
+ desc->name);
+ return AVERROR(ENOSYS);
+ }
+ if (pixel_size < 1 || pixel_size > 2 || comp->step / pixel_size > 2) {
+ av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
desc->name);
+ return AVERROR(ENOSYS);
+ }
+ }
+ // The kernel reads up to four rows away and mirrors at most three, as
vf_bwdif.c does.
+ if (AV_CEIL_RSHIFT(ctx->inputs[0]->w, desc->log2_chroma_w) < 3 ||
+ AV_CEIL_RSHIFT(ctx->inputs[0]->h, desc->log2_chroma_h) < 4) {
+ av_log(ctx, AV_LOG_ERROR, "Video with planes less than 3 columns or 4
lines is not supported\n");
+ return AVERROR(EINVAL);
+ }
+
+ s->device_ref = av_buffer_ref(s->input_frames->device_ref);
+ if (!s->device_ref) {
+ av_log(ctx, AV_LOG_ERROR, "A device reference create "
+ "failed.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ l->hw_frames_ctx = av_hwframe_ctx_alloc(s->device_ref);
+ if (!l->hw_frames_ctx) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
+ "for output.\n");
+ ret = AVERROR(ENOMEM);
+ goto exit;
+ }
+
+ input_frames = (AVHWFramesContext*)il->hw_frames_ctx->data;
+ output_frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
+
+ output_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
+ output_frames->sw_format = s->input_frames->sw_format;
+ output_frames->width = ctx->inputs[0]->w;
+ output_frames->height = ctx->inputs[0]->h;
+ ((AVVTFramesContext *)output_frames->hwctx)->color_range =
((AVVTFramesContext *)input_frames->hwctx)->color_range;
+
+ ret = ff_filter_init_hw_frames(ctx, link, 10);
+ if (ret < 0)
+ goto exit;
+
+ ret = av_hwframe_ctx_init(l->hw_frames_ctx);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to initialise VideoToolbox frame "
+ "context for output: %d\n", ret);
+ goto exit;
+ }
+
+ ret = ff_yadif_config_output_common(link);
+ if (ret < 0)
+ goto exit;
+
+ y->csp = av_pix_fmt_desc_get(output_frames->sw_format);
+ y->filter = filter;
+
+exit:
+ return ret;
+}
+
+static int config_output(AVFilterLink *link)
+{
+ AVFilterContext *ctx = link->src;
+ if (@available(macOS 10.11, iOS 8.0, *)) {
+ return do_config_output(link);
+ } else {
+ av_log(ctx, AV_LOG_ERROR, "Metal is not available on this OS
version\n");
+ return AVERROR(ENOSYS);
+ }
+}
+
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST,
{.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
+
+static const AVOption bwdif_videotoolbox_options[] = {
+ #define OFFSET(x) offsetof(YADIFContext, x)
+ { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT,
{.i64=YADIF_MODE_SEND_FIELD}, 0, 1, FLAGS, .unit = "mode"},
+ CONST("send_frame", "send one frame for each frame",
YADIF_MODE_SEND_FRAME, "mode"),
+ CONST("send_field", "send one frame for each field",
YADIF_MODE_SEND_FIELD, "mode"),
+
+ { "parity", "specify the assumed picture field parity", OFFSET(parity),
AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
+ CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
+ CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
+ CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
+
+ { "deint", "specify which frames to deinterlace", OFFSET(deint),
AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
+ CONST("all", "deinterlace all frames",
YADIF_DEINT_ALL, "deint"),
+ CONST("interlaced", "only deinterlace frames marked as interlaced",
YADIF_DEINT_INTERLACED, "deint"),
+ #undef OFFSET
+
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(bwdif_videotoolbox);
+
+static const AVFilterPad bwdif_videotoolbox_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = ff_yadif_filter_frame,
+ .config_props = config_input,
+ },
+};
+
+static const AVFilterPad bwdif_videotoolbox_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .request_frame = ff_yadif_request_frame,
+ .config_props = config_output,
+ },
+};
+
+const FFFilter ff_vf_bwdif_videotoolbox = {
+ .p.name = "bwdif_videotoolbox",
+ .p.description = NULL_IF_CONFIG_SMALL("BWDIF for VideoToolbox frames
using Metal compute"),
+ .p.priv_class = &bwdif_videotoolbox_class,
+ .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
+ .priv_size = BWDIF_VT_CTX_SIZE,
+ .init = bwdif_videotoolbox_init,
+ .uninit = bwdif_videotoolbox_uninit,
+ FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VIDEOTOOLBOX),
+ FILTER_INPUTS(bwdif_videotoolbox_inputs),
+ FILTER_OUTPUTS(bwdif_videotoolbox_outputs),
+ .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};
--
2.54.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]