From b1eb9dbeac22a63a19bd16251a21b60a35627405 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <vital.had@gmail.com>
Date: Sun, 22 Feb 2026 18:55:29 +0800
Subject: [PATCH] Disable vtb

diff --git a/contrib/ffmpeg/module.defs b/contrib/ffmpeg/module.defs
index aedf2cb2a..785993cf0 100644
--- a/contrib/ffmpeg/module.defs
+++ b/contrib/ffmpeg/module.defs
@@ -117,7 +117,15 @@ FFMPEG.CONFIGURE.extra += \
     --enable-muxer=sup
 
 ifeq (darwin,$(HOST.system))
-    FFMPEG.CONFIGURE.extra += --disable-audiotoolbox --disable-coreimage --enable-hwaccel=h264_videotoolbox --enable-hwaccel=hevc_videotoolbox --enable-hwaccel=prores_videotoolbox --enable-hwaccel=av1_videotoolbox
+    FFMPEG.CONFIGURE.extra += --disable-audiotoolbox --disable-coreimage
+
+    ifeq (1,$(FEATURE.vt))
+        FFMPEG.CONFIGURE.extra += \
+            --enable-hwaccel=av1_videotoolbox \
+            --enable-hwaccel=h264_videotoolbox \
+            --enable-hwaccel=hevc_videotoolbox \
+            --enable-hwaccel=prores_videotoolbox
+    endif
 
     ifeq (x86_64,$(HOST.arch))
         FFMPEG.CONFIGURE.extra += --arch=x86_64
diff --git a/gtk/meson.build b/gtk/meson.build
index 0217154a8..d862f2817 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -82,6 +82,20 @@ endif
 
 # OS-specific dependencies
 
+if host_machine.system() == 'darwin'
+  darwin_frameworks = ['AudioToolbox', 'CoreAudio', 'DiskArbitration', 'Foundation', 'IOKit']
+  foreach framework: darwin_frameworks
+    ghb_deps += dependency('appleframeworks', modules: framework)
+  endforeach
+  
+  if get_option('vt').enabled()
+    vt_frameworks = ['CoreMedia', 'CoreVideo', 'VideoToolbox']
+    foreach framework: vt_frameworks
+      ghb_deps += dependency('appleframeworks', modules: framework)
+    endforeach
+  endif
+endif
+
 if host_machine.system() == 'windows'
   add_project_link_arguments('-mwindows', language: ['c', 'cpp'])
   win_dlls = ['bcrypt', 'ntdll', 'ole32', 'regex', 'userenv', 'uuid', 'ws2_32']
diff --git a/gtk/meson_options.txt b/gtk/meson_options.txt
index 4c06e4a22..c4593d67e 100644
--- a/gtk/meson_options.txt
+++ b/gtk/meson_options.txt
@@ -33,6 +33,11 @@ option('qsv',
        value: 'disabled',
        description: 'Enable QSV encoder')
 
+option('vt',
+       type: 'feature',
+       value: 'auto',
+       description: 'Enable VideoToolbox/Metal hardware acceleration support')
+
 option('x265',
        type: 'feature',
        value: 'disabled',
diff --git a/gtk/module.defs b/gtk/module.defs
index 6fa587623..f02abda00 100644
--- a/gtk/module.defs
+++ b/gtk/module.defs
@@ -78,6 +78,13 @@ else
     GTK.CONFIGURE.extra += -Dlibdovi=disabled
 endif
 
+ifeq (1,$(FEATURE.vt))
+    GTK.CONFIGURE.extra += -Dvt=enabled
+else
+    GTK.CONFIGURE.extra += -Dvt=disabled
+    GTK.GCC.D += HB_DISABLE_VT
+endif
+
 ifeq (1,$(FEATURE.flatpak))
     GTK.CONFIGURE.extra += -Dflatpak=true
 else
diff --git a/libhb/common.c b/libhb/common.c
index c5c2cfb8b..c53175c5f 100644
--- a/libhb/common.c
+++ b/libhb/common.c
@@ -368,7 +368,7 @@
                 return hb_nvenc_av1_available();
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
             case HB_VCODEC_VT_H264:
             case HB_VCODEC_VT_H265:
             case HB_VCODEC_VT_H265_10BIT:
@@ -591,7 +591,7 @@
 
 static void hb_common_global_hw_init()
 {
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
     hb_register_hwaccel(&hb_hwaccel_videotoolbox);
 #endif
 #if HB_PROJECT_FEATURE_NVDEC
@@ -1814,7 +1814,7 @@
 {
     switch (codec)
     {
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H264:
         case HB_VCODEC_VT_H265:
         case HB_VCODEC_VT_H265_10BIT:
@@ -1846,7 +1846,7 @@
 {
     switch (codec)
     {
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H264:
         case HB_VCODEC_VT_H265:
         case HB_VCODEC_VT_H265_10BIT:
@@ -1964,7 +1964,7 @@
         case HB_VCODEC_FFMPEG_QSV_H265_10BIT:
         case HB_VCODEC_FFMPEG_QSV_AV1_10BIT:
 #endif
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H265_10BIT:
 #endif
         case HB_VCODEC_X264_10BIT:
@@ -2010,7 +2010,7 @@
         case HB_VCODEC_X265_16BIT:
             return x265_preset_names;
 #endif
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H264:
         case HB_VCODEC_VT_H265:
         case HB_VCODEC_VT_H265_10BIT:
@@ -2099,7 +2099,7 @@
         case HB_VCODEC_FFMPEG_VCE_AV1_10BIT:
             return hb_vce_av1_10bit_profile_names;
 #endif
-#if __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H264:
         case HB_VCODEC_VT_H265:
         case HB_VCODEC_VT_H265_10BIT:
@@ -2158,7 +2158,7 @@
         case HB_VCODEC_X265_16BIT:
             return hb_h265_level_names;
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H264:
         case HB_VCODEC_VT_H265:
         case HB_VCODEC_VT_H265_10BIT:
@@ -2312,7 +2312,7 @@
             }
         }
 #endif
-#if __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_VCODEC_VT_H264:
         case HB_VCODEC_VT_H265:
         case HB_VCODEC_VT_H265_10BIT:
@@ -5147,7 +5147,7 @@
             filter = &hb_filter_mt_frame;
             break;
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case HB_FILTER_ADAPTER_VT:
             filter = &hb_filter_adapter_vt;
             break;
diff --git a/libhb/fifo.c b/libhb/fifo.c
old mode 100644
new mode 100755
index bf21f2dcc..505c4dec8
--- a/libhb/fifo.c
+++ b/libhb/fifo.c
@@ -12,7 +12,7 @@
 
 #include "handbrake/handbrake.h"
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
 #include <CoreMedia/CoreMedia.h>
 #include "platform/macosx/vt_common.h"
 #include "platform/macosx/cv_utils.h"
@@ -629,7 +629,7 @@ int hb_buffer_is_writable(const hb_buffer_t *buf)
             return av_frame_is_writable((AVFrame *)buf->storage);
         case STANDARD:
             return 1;
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case COREMEDIA:
             return hb_cv_get_io_surface_usage_count(buf) == 1;
 #endif
@@ -755,7 +755,7 @@ hb_buffer_t * hb_buffer_dup(const hb_buffer_t *src)
         // into another hardware AVFrame.
         if (frame->hw_frames_ctx)
         {
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
             if (frame->format == AV_PIX_FMT_VIDEOTOOLBOX)
             {
                 buf = hb_vt_buffer_dup(src);
@@ -790,7 +790,7 @@ hb_buffer_t * hb_buffer_dup(const hb_buffer_t *src)
             }
         }
     }
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
     else if (src->storage_type == COREMEDIA)
     {
         buf = hb_vt_buffer_dup(src);
@@ -1020,7 +1020,7 @@ static void free_buffer_resources(hb_buffer_t *b)
         av_frame_unref((AVFrame *)b->storage);
         av_frame_free((AVFrame **)&b->storage);
     }
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
     else if (b->storage_type == COREMEDIA && b->storage != NULL)
     {
         CFRelease((CMSampleBufferRef)b->storage);
diff --git a/libhb/handbrake/common.h b/libhb/handbrake/common.h
index 4940734d3..5cd881017 100644
--- a/libhb/handbrake/common.h
+++ b/libhb/handbrake/common.h
@@ -1511,7 +1511,9 @@ extern hb_work_object_t hb_decavsub;
 extern hb_work_object_t hb_encavsub;
 extern hb_work_object_t hb_encavcodec;
 extern hb_work_object_t hb_encqsv;
+#ifndef HB_DISABLE_VT
 extern hb_work_object_t hb_encvt;
+#endif
 extern hb_work_object_t hb_encx264;
 extern hb_work_object_t hb_enctheora;
 extern hb_work_object_t hb_encx265;
diff --git a/libhb/handbrake/internal.h b/libhb/handbrake/internal.h
index 15a4e5df1..47c285f96 100644
--- a/libhb/handbrake/internal.h
+++ b/libhb/handbrake/internal.h
@@ -461,7 +461,7 @@
 extern hb_filter_object_t hb_filter_colorspace;
 extern hb_filter_object_t hb_filter_format;
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
 extern hb_filter_object_t hb_filter_adapter_vt;
 extern hb_filter_object_t hb_filter_comb_detect_vt;
 extern hb_filter_object_t hb_filter_yadif_vt;
@@ -478,7 +478,7 @@
 extern hb_motion_metric_object_t hb_motion_metric;
 extern hb_blend_object_t hb_blend;
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
 extern hb_motion_metric_object_t hb_motion_metric_vt;
 extern hb_blend_object_t hb_blend_vt;
 #endif
diff --git a/libhb/hb.c b/libhb/hb.c
index 3d16b0c8c..3844cde89 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -2174,7 +2174,9 @@ int hb_global_init()
     hb_register(&hb_encavcodec);
     hb_register(&hb_encavcodeca);
 #ifdef __APPLE__
+#ifndef HB_DISABLE_VT
     hb_register(&hb_encvt);
+#endif
     hb_register(&hb_encca_aac);
     hb_register(&hb_encca_haac);
 #endif
diff --git a/libhb/module.defs b/libhb/module.defs
index 5fbb4750d..7d381ac56 100644
--- a/libhb/module.defs
+++ b/libhb/module.defs
@@ -67,9 +67,19 @@ ifeq ($(HOST.system),cygwin)
     LIBHB.GCC.D += SYS_CYGWIN
 else ifeq ($(HOST.system),darwin)
     LIBHB.GCC.D += SYS_DARWIN
-    LIBHB.c += $(wildcard $(LIBHB.src/)platform/macosx/*.c)
-    LIBHB.m += $(wildcard $(LIBHB.src/)platform/macosx/*.m)
-    LIBHB.metal += $(wildcard $(LIBHB.src/)platform/macosx/shaders/*.metal)
+    LIBHB.GCC.f += AudioToolbox CoreAudio DiskArbitration Foundation IOKit
+
+    ifeq (1,$(FEATURE.vt))
+        # Modern macOS, default.
+        LIBHB.GCC.f += CoreMedia CoreVideo VideoToolbox
+        LIBHB.c += $(wildcard $(LIBHB.src/)platform/macosx/*.c)
+        LIBHB.m += $(wildcard $(LIBHB.src/)platform/macosx/*.m)
+        LIBHB.metal += $(wildcard $(LIBHB.src/)platform/macosx/shaders/*.metal)
+    else
+        LIBHB.GCC.D += HB_DISABLE_VT
+        LIBHB.c += $(LIBHB.src/)platform/macosx/encca_aac.c
+        LIBHB.m += $(LIBHB.src/)platform/macosx/config.m
+    endif
 else ifeq ($(HOST.system),linux)
     LIBHB.GCC.D += SYS_LINUX _LARGEFILE_SOURCE _FILE_OFFSET_BITS=64
 else ifeq ($(HOST.system),freebsd)
diff --git a/libhb/ports.c b/libhb/ports.c
old mode 100644
new mode 100755
index a92b52076..4cf6237c4
--- a/libhb/ports.c
+++ b/libhb/ports.c
@@ -72,6 +72,12 @@
 #ifdef __APPLE__
 #include <CoreServices/CoreServices.h>
 #include <IOKit/pwr_mgt/IOPMLib.h>
+#ifndef kIOPMAssertPreventUserIdleSystemSleep
+#define kIOPMAssertPreventUserIdleSystemSleep CFSTR("PreventUserIdleSystemSleep")
+#endif
+#ifndef kIOPMAssertionLevelOn
+#define kIOPMAssertionLevelOn 255
+#endif
 #endif
 
 #include <stddef.h>
diff --git a/libhb/rendersub.c b/libhb/rendersub.c
index 830468d13..ce0b6e467 100644
--- a/libhb/rendersub.c
+++ b/libhb/rendersub.c
@@ -1130,7 +1130,7 @@ static hb_blend_object_t * hb_blend_init(hb_filter_init_t init, int sub_pix_fmt)
     hb_blend_object_t *blend;
     switch (init.hw_pix_fmt)
     {
-#if defined(__APPLE__)
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case AV_PIX_FMT_VIDEOTOOLBOX:
             blend = &hb_blend_vt;
             break;
diff --git a/libhb/vfr.c b/libhb/vfr.c
index 0a1536607..10ce647aa 100644
--- a/libhb/vfr.c
+++ b/libhb/vfr.c
@@ -77,7 +77,7 @@ static hb_motion_metric_object_t * hb_motion_metric_init(hb_filter_init_t *init)
     hb_motion_metric_object_t *metric;
     switch (init->hw_pix_fmt)
     {
-#if defined(__APPLE__)
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
         case AV_PIX_FMT_VIDEOTOOLBOX:
             metric = &hb_motion_metric_vt;
             break;
diff --git a/libhb/work.c b/libhb/work.c
index 41d01530a..3bdb294d0 100644
--- a/libhb/work.c
+++ b/libhb/work.c
@@ -20,7 +20,7 @@
 #include "handbrake/qsv_common.h"
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
 #include "platform/macosx/vt_common.h"
 #endif
 
@@ -1447,7 +1447,7 @@ static enum AVPixelFormat match_pix_fmt(enum AVPixelFormat pix_fmt,
 
 static void sanitize_filter_list_post(hb_job_t *job)
 {
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(HB_DISABLE_VT)
     if (job->hw_pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX)
     {
         hb_vt_setup_hw_filters(job);
diff --git a/make/configure.py b/make/configure.py
index 25640d993..e16fb2691 100644
--- a/make/configure.py
+++ b/make/configure.py
@@ -1452,6 +1452,10 @@ def createCLI( cross = None ):
     grp.add_argument( '--enable-libdovi', dest="enable_libdovi", default=not Tools.cargo.fail and not Tools.cargoc.fail, action='store_true', help=(( 'enable %s' %h ) if h != argparse.SUPPRESS else h) )
     grp.add_argument( '--disable-libdovi', dest="enable_libdovi", action='store_false', help=(( 'disable %s' %h ) if h != argparse.SUPPRESS else h) )
 
+    # Facilitate compat build for legacy macOS versions.
+    h = 'VideoToolbox/Metal hardware acceleration' if host_tuple.match( '*-*-darwin*' ) else argparse.SUPPRESS
+    grp.add_argument( '--enable-vt', dest="enable_vt", default=True, action='store_true', help=(( 'enable %s' %h ) if h != argparse.SUPPRESS else h) )
+    grp.add_argument( '--disable-vt', dest="enable_vt", action='store_false', help=(( 'disable %s' %h ) if h != argparse.SUPPRESS else h) )
 
     cli.add_argument_group( grp )
 
@@ -2098,7 +2102,8 @@ int main()
     doc.add( 'FEATURE.libdovi',    int( options.enable_libdovi ))
 
     if build_tuple.match( '*-*-darwin*' ) and options.cross is None:
-        doc.add( 'FEATURE.xcode',      int( not (Tools.xcodebuild.fail or options.disable_xcode) ))
+        doc.add( 'FEATURE.vt',      int( options.enable_vt ))
+        doc.add( 'FEATURE.xcode',   int( not (Tools.xcodebuild.fail or options.disable_xcode) ))
         if not Tools.xcodebuild.fail and not options.disable_xcode:
             doc.addBlank()
             doc.add( 'XCODE.prefix',  cfg.xcode_prefix_final )
diff --git a/test/module.defs b/test/module.defs
index 91c762e51..f88651cd7 100644
--- a/test/module.defs
+++ b/test/module.defs
@@ -54,7 +54,10 @@ endif
 TEST.GCC.I += $(LIBHB.GCC.I)
 
 ifeq ($(HOST.system),darwin)
-    TEST.GCC.f += IOKit CoreServices CoreText CoreGraphics AudioToolbox VideoToolbox CoreMedia CoreVideo IOSurface Foundation DiskArbitration Metal
+    TEST.GCC.f += AudioToolbox CoreServices DiskArbitration Foundation IOKit
+ifeq (1,$(FEATURE.vt))
+    TEST.GCC.f += CoreGraphics CoreMedia CoreText CoreVideo IOSurface Metal VideoToolbox
+endif
     TEST.GCC.l += iconv
 else ifeq ($(HOST.system),linux)
     TEST.GCC.l += pthread dl m
diff --git a/test/test.c b/test/test.c
index 20a1e61c7..69ea1a37b 100644
--- a/test/test.c
+++ b/test/test.c
@@ -3280,7 +3280,7 @@ static int ParseOptions( int argc, char ** argv )
                     }
                     else if (!strcmp(optarg, "videotoolbox"))
                     {
-#if defined( __APPLE_CC__ )
+#if defined(__APPLE__) && defined(__clang__)
                         if (__builtin_available(macOS 13, *))
                         {
                             hw_decode = HB_DECODE_VIDEOTOOLBOX;
