1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="HandBrakeHardwareEncoderHelper.cs" company="HandBrake Project (https://handbrake.fr)">
3 //   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
4 // </copyright>
5 // <summary>
6 //   The System Information.
7 // </summary>
8 // --------------------------------------------------------------------------------------------------------------------
9 
10 namespace HandBrake.Interop.Interop
11 {
12     using System;
13     using System.Diagnostics;
14     using System.Runtime.InteropServices;
15 
16     using HandBrake.Interop.Interop.HbLib;
17 
18     public class HandBrakeHardwareEncoderHelper
19     {
20         private static bool? isNvencH264Available; // Local cache to prevent log spam.
21         private static bool? isNvencH265Available;
22 
23         public static bool IsSafeMode
24         {
25             get
26             {
27                 try
28                 {
29                     if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
30                     {
31                         return false;
32                     }
33 
34                     return (HBFunctions.hb_qsv_available() + HBFunctions.hb_vce_h264_available()
35                                                            + HBFunctions.hb_nvenc_h264_available()) == -3;
36                 }
37                 catch (Exception)
38                 {
39                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
40                     return false;
41                 }
42             }
43         }
44 
45         /* QuickSync Support */
46 
47         public static bool IsQsvAvailable
48         {
49             get
50             {
51                 try
52                 {
53                     return HBFunctions.hb_qsv_available() > 0;
54                 }
55                 catch (Exception)
56                 {
57                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
58                     return false;
59                 }
60             }
61         }
62 
63         public static bool IsQsvAvailableH264
64         {
65             get
66             {
67                 try
68                 {
69                     return (HBFunctions.hb_qsv_available() & NativeConstants.HB_VCODEC_QSV_H264) > 0;
70                 }
71                 catch (Exception)
72                 {
73                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
74                     return false;
75                 }
76             }
77         }
78 
79         public static bool IsQsvAvailableH265
80         {
81             get
82             {
83                 try
84                 {
85                     return (HBFunctions.hb_qsv_available() & NativeConstants.HB_VCODEC_QSV_H265) > 0;
86                 }
87                 catch (Exception)
88                 {
89                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
90                     return false;
91                 }
92             }
93         }
94 
95         public static int QsvHardwareGeneration
96         {
97             get
98             {
99                 try
100                 {
101                     int adapter_index = HBFunctions.hb_qsv_get_adapter_index();
102                     int qsv_platform = HBFunctions.hb_qsv_get_platform(adapter_index);
103                     int hardware = HBFunctions.hb_qsv_hardware_generation(qsv_platform);
104                     return hardware;
105                 }
106                 catch (Exception exc)
107                 {
108                     // Silent failure. -1 means unsupported.
109                     Debug.WriteLine(exc);
110                     return -1;
111                 }
112             }
113         }
114 
115         public static bool IsQsvAvailableH26510bit
116         {
117             get
118             {
119                 try
120                 {
121                     return (HBFunctions.hb_qsv_available() & NativeConstants.HB_VCODEC_QSV_H265_10BIT) > 0;
122                 }
123                 catch (Exception)
124                 {
125                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
126                     return false;
127                 }
128             }
129         }
130 
131         /* AMD VCE Support */
132 
133         public static bool IsVceH264Available
134         {
135             get
136             {
137                 try
138                 {
139                     return HBFunctions.hb_vce_h264_available() > 0;
140                 }
141                 catch (Exception)
142                 {
143                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
144                     return false;
145                 }
146             }
147         }
148 
149         public static bool IsVceH265Available
150         {
151             get
152             {
153                 try
154                 {
155                     return HBFunctions.hb_vce_h265_available() > 0;
156                 }
157                 catch (Exception)
158                 {
159                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
160                     return false;
161                 }
162             }
163         }
164 
165         /* Nvidia NVEnc Support */
166 
167         public static bool IsNVEncH264Available
168         {
169             get
170             {
171                 try
172                 {
173                     if (isNvencH264Available == null)
174                     {
175                         isNvencH264Available = HBFunctions.hb_nvenc_h264_available() > 0;
176                     }
177 
178                     return isNvencH264Available.Value;
179                 }
180                 catch (Exception)
181                 {
182                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
183                     return false;
184                 }
185             }
186         }
187 
188         public static bool IsNVEncH265Available
189         {
190             get
191             {
192                 try
193                 {
194                     if (!IsNVEncH264Available)
195                     {
196                         return false;
197                     }
198 
199                     if (isNvencH265Available == null)
200                     {
201                         isNvencH265Available = HBFunctions.hb_nvenc_h265_available() > 0;
202                     }
203 
204                     return isNvencH265Available.Value;
205                 }
206                 catch (Exception)
207                 {
208                     // Silent failure. Typically this means the dll hasn't been built with --enable-qsv
209                     return false;
210                 }
211             }
212         }
213     }
214 }
215