软解码使用的是 CPU 解码,CPU 进行大量的矩阵计算是非常的占用性能,导致手机功耗增加,电量耗尽更快。
iPhone 6 搭载的是 A8 芯片,iPhone 6s 搭载的是 A9 芯片,iPhone 6s 之前的苹果手机都不支持 HEVC 硬解码。iOS 上使用 VideoToolbox 框架解码视频时,iPhone 6 进行软解码时 CPU 占用将增加大约 30%,而使用 iPhone 14 使用硬解码 CPU 占用增加不到 10%。
那么如何验证 iPhone 是否支持硬解码呢?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// 检查是否启用硬解-(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;}