Add support for HEVC codec types in Media Capabilities
[WebKit-https.git] / Source / WebCore / platform / graphics / cocoa / HEVCUtilitiesCocoa.cpp
1 /*
2  * Copyright (C) 2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "HEVCUtilitiesCocoa.h"
28
29 #if PLATFORM(COCOA)
30
31 #include "HEVCUtilities.h"
32 #include "MediaCapabilitiesInfo.h"
33
34 #include "VideoToolboxSoftLink.h"
35
36 namespace WebCore {
37
38 bool validateHEVCParameters(HEVCParameterSet& parameters, MediaCapabilitiesInfo& info)
39 {
40     if (!canLoad_VideoToolbox_VTCopyHEVCDecoderCapabilitiesDictionary()
41         || !canLoad_VideoToolbox_kVTHEVCDecoderCapability_SupportedProfiles()
42         || !canLoad_VideoToolbox_kVTHEVCDecoderCapability_PerProfileSupport()
43         || !canLoad_VideoToolbox_kVTHEVCDecoderProfileCapability_IsHardwareAccelerated()
44         || !canLoad_VideoToolbox_kVTHEVCDecoderProfileCapability_MaxDecodeLevel()
45         || !canLoad_VideoToolbox_kVTHEVCDecoderProfileCapability_MaxPlaybackLevel())
46         return false;
47
48     RetainPtr<CFDictionaryRef> capabilities = adoptCF(VTCopyHEVCDecoderCapabilitiesDictionary());
49     if (!capabilities)
50         return false;
51
52     auto supportedProfiles = (CFArrayRef)CFDictionaryGetValue(capabilities.get(), kVTHEVCDecoderCapability_SupportedProfiles);
53     if (!supportedProfiles || CFGetTypeID(supportedProfiles) != CFArrayGetTypeID())
54         return false;
55
56     int16_t generalProfileIDC = parameters.generalProfileIDC;
57     auto cfGeneralProfileIDC = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &generalProfileIDC));
58     auto searchRange = CFRangeMake(0, CFArrayGetCount(supportedProfiles));
59     if (!CFArrayContainsValue(supportedProfiles, searchRange, cfGeneralProfileIDC.get()))
60         return false;
61
62     auto perProfileSupport = (CFDictionaryRef)CFDictionaryGetValue(capabilities.get(), kVTHEVCDecoderCapability_PerProfileSupport);
63     if (!perProfileSupport || CFGetTypeID(perProfileSupport) != CFDictionaryGetTypeID())
64         return false;
65
66     auto generalProfileIDCString = String::number(generalProfileIDC).createCFString();
67     auto profileSupport = (CFDictionaryRef)CFDictionaryGetValue(perProfileSupport, generalProfileIDCString.get());
68     if (!profileSupport || CFGetTypeID(profileSupport) != CFDictionaryGetTypeID())
69         return false;
70
71     auto isHardwareAccelerated = (CFBooleanRef)CFDictionaryGetValue(profileSupport, kVTHEVCDecoderProfileCapability_IsHardwareAccelerated);
72     if (isHardwareAccelerated && CFGetTypeID(isHardwareAccelerated) == CFBooleanGetTypeID())
73         info.powerEfficient = CFBooleanGetValue(isHardwareAccelerated);
74
75     auto cfMaxDecodeLevel = (CFNumberRef)CFDictionaryGetValue(profileSupport, kVTHEVCDecoderProfileCapability_MaxDecodeLevel);
76     if (cfMaxDecodeLevel && CFGetTypeID(cfMaxDecodeLevel) == CFNumberGetTypeID()) {
77         int16_t maxDecodeLevel = 0;
78         if (!CFNumberGetValue(cfMaxDecodeLevel, kCFNumberSInt16Type, &maxDecodeLevel))
79             return false;
80
81         if (parameters.generalLevelIDC > maxDecodeLevel)
82             return false;
83
84         info.supported = true;
85     }
86
87     auto cfMaxPlaybackLevel = (CFNumberRef)CFDictionaryGetValue(profileSupport, kVTHEVCDecoderProfileCapability_MaxPlaybackLevel);
88     if (cfMaxPlaybackLevel && CFGetTypeID(cfMaxPlaybackLevel) == CFNumberGetTypeID()) {
89         int16_t maxPlaybackLevel = 0;
90         if (!CFNumberGetValue(cfMaxPlaybackLevel, kCFNumberSInt16Type, &maxPlaybackLevel))
91             return false;
92
93         info.smooth = parameters.generalLevelIDC <= maxPlaybackLevel;
94     }
95
96     return true;
97 }
98
99 }
100
101 #endif // PLATFORM(COCOA)