iOS 如何获取解码视频使用的是软解码还是硬解码

软解码使用的是 CPU 解码,CPU 进行大量的矩阵计算是非常的占用性能,导致手机功耗增加,电量耗尽更快。

iPhone 6 搭载的是 A8 芯片,iPhone 6s 搭载的是 A9 芯片,iPhone 6s 之前的苹果手机都不支持 HEVC 硬解码。
iOS 上使用 VideoToolbox 框架解码视频时,iPhone 6 进行软解码时 CPU 占用将增加大约 30%,而使用 iPhone 14 使用硬解码 CPU 占用增加不到 10%。

那么如何验证 iPhone 是否支持硬解码呢?

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
// 检查是否启用硬解
-(BOOL)isHardwareDecodingEnabledForFormatDescription:(CMFormatDescriptionRef)videoFormatDesc {
VTDecompressionSessionRef decompressionSession = NULL;
NSDictionary *destinationPixelBufferAttributes = @{
(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
};

// 创建解码会话
VTDecompressionOutputCallbackRecord callbackRecord = {0};
OSStatus status = VTDecompressionSessionCreate(
kCFAllocatorDefault,
videoFormatDesc,
NULL, // 使用默认解码器
(__bridge CFDictionaryRef)destinationPixelBufferAttributes,
&callbackRecord,
&decompressionSession
);

if (status != noErr || !decompressionSession) {
NSLog(@"解码会话创建失败: %d", (int)status);
return NO;
}

// 查询是否使用硬件加速
CFBooleanRef isUsingHardwareAcceleration = NULL;
status = VTSessionCopyProperty(
decompressionSession,
kVTDecompressionPropertyKey_UsingHardwareAcceleratedVideoDecoder,
kCFAllocatorDefault,
&isUsingHardwareAcceleration
);

BOOL result = NO;
if (status == noErr && isUsingHardwareAcceleration != NULL) {
result = CFBooleanGetValue(isUsingHardwareAcceleration);
}

// 释放资源
if (decompressionSession) {
VTDecompressionSessionInvalidate(decompressionSession);
CFRelease(decompressionSession);
}
if (isUsingHardwareAcceleration) {
CFRelease(isUsingHardwareAcceleration);
}

return result;
}

iOS 如何获取解码视频使用的是软解码还是硬解码

https://ganzhixiong.com/p/726c30b7/

Author

干志雄

Posted on

2025-04-30

Updated on

2025-04-30

Licensed under

Comments