如何调试Mac OS未使用硬件H264编码器的原因

时间:2015-10-29 01:23:08

标签: macos encoding h.264 video-toolbox

我尝试使用H264对某些视频流进行编码,并且我愿意使用硬件编码器来比较硬件和CPU编码之间的质量和资源消耗。问题是我无法强迫操作系统使用硬件编码器。

这是我用来创建VTCompressionSession的代码:

var status: OSStatus

let encoderSpecifications: CFDictionary? = [
    kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder as String: true,
    kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder as String: true,
    kVTVideoEncoderSpecification_EncoderID as String: "com.apple.videotoolbox.videoencoder.24rgb" // Tried without this paramenter so the system can decide what encoder ID should be using but doesn't work anyway.
]

let pixelBufferOptions: CFDictionary? = [
    kCVPixelBufferWidthKey as String: Int(width),
    kCVPixelBufferHeightKey as String: Int(height),
    kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_24RGB) // Tried commenting this in case that there was a pixelformat constraint but didn't change anything
];

status = VTCompressionSessionCreate(kCFAllocatorDefault, width, height, CMVideoCodecType(kCMVideoCodecType_H264), encoderSpecifications, pixelBufferOptions, nil, { (outputCallbackRefCon: UnsafeMutablePointer<Void>, sourceFrameRefCon: UnsafeMutablePointer<Void>, status: OSStatus, infoFlags: VTEncodeInfoFlags, sampleBuffer: CMSampleBuffer?) -> Void in
    ...
}, unsafeBitCast(self, UnsafeMutablePointer<Void>.self), &compressionSession)

我打开了控制台,这是我尝试创建会话时唯一相关的消息:

10/28/15 22:06:27.711 Dupla-Mac[87762]: <<<< VTVideoEncoderSelection >>>> VTSelectAndCreateVideoEncoderInstanceInternal: no video encoder found for 'avc1'

这是我使用EncoderID时获得的状态代码:

2015-10-28 22:17:13.480 Dupla-Mac[87895:5578917] Couldn't create compression session :( -12908

当我不使用EncoderID时,这是我得到的:

2015-10-28 22:18:16.695 Dupla-Mac[87996:5581914] Couldn't create compression session :( -12915

两者都与资源缺乏有关,但无法找到任何差异。我已经检查过可能使用硬件编码器的最常用功能已关闭,但我不知道如何检查这一点。 AirPlay关闭,QuickTime关闭,没有任何应用访问相机,等等。

TL; DR:有没有办法强迫知道操作系统用来启用硬件编码器的策略是什么,并最终知道为什么它随时都无法使用?

提前致谢!

2 个答案:

答案 0 :(得分:1)

我猜你已经解决了这个问题,但对于其他人来说 - macOS上唯一可用的硬件加速编码器(所有macs 2012+的10.8-10.12)/ iOS(8-10)是com.apple.videotoolbox.videoencoder.h264.gva以下是完整列表:https://gist.github.com/vade/06ace2e33561a79cc240

答案 1 :(得分:0)

获取编解码器列表

    CFArrayRef encoder_list;
    VTCopyVideoEncoderList(NULL, &encoder_list);
    CFIndex size = CFArrayGetCount(encoder_list);
    for(CFIndex i = 0; i < size; i++) {
        CFDictionaryRef encoder_dict = CFArrayGetValueAtIndex(encoder_list, i);
        CFStringRef type = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_CodecType);
        CFStringRef  encoderID = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_EncoderID);
        CFStringRef codecName = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_CodecName);
        CFStringRef encoderName = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_EncoderName);
        CFStringRef display_name = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_DisplayName);
        NSLog(@"  %@  %@ %@ %@ %@", type, encoderID, codecName, encoderName, display_name);
    }