4

I'm trying to encode some video only stream using H264, and I'm willing to use the hardware encoder in order to compare both quality and resource consumption between hardware and CPU encoding. The thing is that I'm not being able to force the OS to use the hardware encoder.

This is the code I'm using to create the 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)

I opened the Console and this the only relevant message I'm getting when I try to create the session:

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

This is the status code I get when I use the EncoderID:

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

And this is the one I get when I don't use the EncoderID:

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

Both relate to the lack of availability of the resource, but couldn't find any difference. I've checked that the most known functionality that may use the hardware encoder are turned off, but I don't know how to check this for sure. AirPlay is off, QuickTime is off, there's not any app accessing the camera, and so.

TL;DR: is there any way to force or to know what's the strategy the OS is using to enable the Hardware Encoder, and eventually know why it is not available at any moment?

Thanks in advance!

4

2 回答 2

1

I guess you've already resolved the problem but for others - the only HW-accelerated encoder available on macOS (10.8-10.12 for all macs 2012+) / iOS(8-10) is com.apple.videotoolbox.videoencoder.h264.gva and here is the full list: https://gist.github.com/vade/06ace2e33561a79cc240

于 2016-07-14T16:35:26.030 回答
0

get codec list

    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);
    }
于 2016-08-15T11:49:32.860 回答