summaryrefslogtreecommitdiffstats
path: root/libhb/platform/macosx/vt_common.m
blob: 65bf4deaabd18a8d3884c4019a5a0a0a9b9549cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* vt_common.c

   Copyright (c) 2003-2021 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */

#include "vt_common.h"

#include <VideoToolbox/VideoToolbox.h>
#include <CoreMedia/CoreMedia.h>
#include <CoreVideo/CoreVideo.h>

static const CFStringRef encoder_id_h264 = CFSTR("com.apple.videotoolbox.videoencoder.h264.gva");
static const CFStringRef encoder_id_h265 = CFSTR("com.apple.videotoolbox.videoencoder.hevc.gva");

static int encvt_available(CFStringRef encoder)
{
    CFArrayRef encoder_list;
    VTCopyVideoEncoderList(NULL, &encoder_list);
    CFIndex size = CFArrayGetCount(encoder_list);

    for (CFIndex i = 0; i < size; i++ )
    {
        CFDictionaryRef encoder_dict = CFArrayGetValueAtIndex(encoder_list, i);
        CFStringRef encoder_id = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderSpecification_EncoderID);
        if (CFEqual(encoder_id, encoder))
        {
            CFRelease(encoder_list);
            return 1;
        }
    }
    CFRelease(encoder_list);
    return 0;
}

static OSStatus encoder_properties(CMVideoCodecType codecType, CFStringRef *encoderIDOut, CFDictionaryRef *supportedPropertiesOut) API_AVAILABLE(macosx(10.13), ios(11.0), tvos(11.0))
{
    const void *keys[1] = { kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder };
    const void *values[1] = { kCFBooleanTrue };
    CFDictionaryRef specification = CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    OSStatus err = VTCopySupportedPropertyDictionaryForEncoder(1920, 1080,
                                                               codecType, specification,
                                                               encoderIDOut,
                                                               supportedPropertiesOut);
    CFRelease(specification);
    return err;
}

static int hb_vt_is_hardware_encoder_available(CMVideoCodecType codecType)
{
#if defined(__MAC_11_0)
    if (@available (macOS 11, *))
    {
        CFStringRef encoderIDOut;
        CFDictionaryRef supportedPropertiesOut;

        OSStatus err = encoder_properties(codecType, &encoderIDOut, &supportedPropertiesOut);

        if (err == noErr) {
            CFRelease(encoderIDOut);
            CFRelease(supportedPropertiesOut);
            return 1;
        }
        return 0;
    }
    else
    {
#endif
        CFStringRef encoder_id;

        switch (codecType) {
            case kCMVideoCodecType_H264:
                encoder_id = encoder_id_h264;
                break;
            case kCMVideoCodecType_HEVC:
                encoder_id = encoder_id_h265;
                break;
            default:
                return 0;
        }

        return encvt_available(encoder_id);
#if defined(__MAC_11_0)
    }
#endif
}

static int hb_vt_is_constant_quality_available(CMVideoCodecType codecType)
{
#if defined(__MAC_11_0) && defined(__aarch64__)
    if (@available (macOS 11, *))
    {
        CFStringRef encoderIDOut;
        CFDictionaryRef supportedPropertiesOut;

        OSStatus err = encoder_properties(codecType, &encoderIDOut, &supportedPropertiesOut);

        if (err == noErr) {
            Boolean keyExists;
            CFBooleanRef value = kCFBooleanFalse;
            keyExists = CFDictionaryGetValueIfPresent(supportedPropertiesOut,
                                                      kVTCompressionPropertyKey_Quality,
                                                      (const void **)&value);

            CFRelease(encoderIDOut);
            CFRelease(supportedPropertiesOut);

            if (keyExists) {
                return 1;
            }
        }
    }
#endif
    return 0;
}

static int vt_h264_available;

int hb_vt_h264_is_available()
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vt_h264_available = hb_vt_is_hardware_encoder_available(kCMVideoCodecType_H264);
    });
    return vt_h264_available;
}

static int vt_h265_available;

int hb_vt_h265_is_available()
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vt_h265_available = hb_vt_is_hardware_encoder_available(kCMVideoCodecType_HEVC);
    });
    return vt_h265_available;
}

static int vt_h264_constant_quality;

int hb_vt_h264_is_constant_quality_available()
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vt_h264_constant_quality = hb_vt_is_constant_quality_available(kCMVideoCodecType_H264);
    });
    return vt_h264_constant_quality;
}

static int vt_h265_constant_quality;

int hb_vt_h265_is_constant_quality_available()
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vt_h265_constant_quality = hb_vt_is_constant_quality_available(kCMVideoCodecType_HEVC);
    });
    return vt_h265_constant_quality;
}