软解码使用的是 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; }
|