Use Objective-C types in VideoToolbox encoder / decoder

The goal of this CL is to make the CFDictionaryRef not dependent
on a fixed number of properties, which will facilitate future work.

No behavior change intended.

Bug: None
Change-Id: I32261d81eaa9b77380cecbdaefcbaeafde300f9a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/257920
Auto-Submit: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
Cr-Commit-Position: refs/heads/main@{#36449}
This commit is contained in:
Byoungchan Lee
2022-04-05 22:32:01 +09:00
committed by WebRTC LUCI CQ
parent 64380ce385
commit f8750fcf67
2 changed files with 38 additions and 77 deletions

View File

@ -202,48 +202,30 @@ void decompressionOutputCallback(void *decoderRef,
// CVPixelBuffers directly to the renderer. // CVPixelBuffers directly to the renderer.
// TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that // TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that
// we can pass CVPixelBuffers as native handles in decoder output. // we can pass CVPixelBuffers as native handles in decoder output.
#if TARGET_OS_SIMULATOR NSDictionary *attributes = @{
static size_t const attributesSize = 2;
#else
static size_t const attributesSize = 3;
#endif
CFTypeRef keys[attributesSize] = {
#if defined(WEBRTC_IOS) && (TARGET_OS_MACCATALYST || TARGET_OS_SIMULATOR) #if defined(WEBRTC_IOS) && (TARGET_OS_MACCATALYST || TARGET_OS_SIMULATOR)
kCVPixelBufferMetalCompatibilityKey, (NSString *)kCVPixelBufferMetalCompatibilityKey : @(YES),
#elif defined(WEBRTC_IOS) #elif defined(WEBRTC_IOS)
kCVPixelBufferOpenGLESCompatibilityKey, (NSString *)kCVPixelBufferOpenGLESCompatibilityKey : @(YES),
#elif defined(WEBRTC_MAC) #elif defined(WEBRTC_MAC)
kCVPixelBufferOpenGLCompatibilityKey, (NSString *)kCVPixelBufferOpenGLCompatibilityKey : @(YES),
#endif #endif
#if !(TARGET_OS_SIMULATOR) #if !(TARGET_OS_SIMULATOR)
kCVPixelBufferIOSurfacePropertiesKey, (NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{},
#endif
kCVPixelBufferPixelFormatTypeKey};
CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0);
int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
#if TARGET_OS_SIMULATOR
CFTypeRef values[attributesSize] = {kCFBooleanTrue, pixelFormat};
#else
CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelFormat};
#endif #endif
(NSString *)
kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange),
};
CFDictionaryRef attributes = CreateCFTypeDictionary(keys, values, attributesSize);
if (ioSurfaceValue) {
CFRelease(ioSurfaceValue);
ioSurfaceValue = nullptr;
}
if (pixelFormat) {
CFRelease(pixelFormat);
pixelFormat = nullptr;
}
VTDecompressionOutputCallbackRecord record = { VTDecompressionOutputCallbackRecord record = {
decompressionOutputCallback, (__bridge void *)self, decompressionOutputCallback, (__bridge void *)self,
}; };
OSStatus status = VTDecompressionSessionCreate( OSStatus status = VTDecompressionSessionCreate(nullptr,
nullptr, _videoFormat, nullptr, attributes, &record, &_decompressionSession); _videoFormat,
CFRelease(attributes); nullptr,
(__bridge CFDictionaryRef)attributes,
&record,
&_decompressionSession);
if (status != noErr) { if (status != noErr) {
RTC_LOG(LS_ERROR) << "Failed to create decompression session: " << status; RTC_LOG(LS_ERROR) << "Failed to create decompression session: " << status;
[self destroyDecompressionSession]; [self destroyDecompressionSession];

View File

@ -605,59 +605,38 @@ NSUInteger GetMaxSampleRate(const webrtc::H264ProfileLevelId &profile_level_id)
// Set source image buffer attributes. These attributes will be present on // Set source image buffer attributes. These attributes will be present on
// buffers retrieved from the encoder's pixel buffer pool. // buffers retrieved from the encoder's pixel buffer pool.
const size_t attributesSize = 3; NSDictionary *sourceAttributes = @{
CFTypeRef keys[attributesSize] = {
#if defined(WEBRTC_IOS) && (TARGET_OS_MACCATALYST || TARGET_OS_SIMULATOR) #if defined(WEBRTC_IOS) && (TARGET_OS_MACCATALYST || TARGET_OS_SIMULATOR)
kCVPixelBufferMetalCompatibilityKey, (NSString *)kCVPixelBufferMetalCompatibilityKey : @(YES),
#elif defined(WEBRTC_IOS) #elif defined(WEBRTC_IOS)
kCVPixelBufferOpenGLESCompatibilityKey, (NSString *)kCVPixelBufferOpenGLESCompatibilityKey : @(YES),
#elif defined(WEBRTC_MAC) #elif defined(WEBRTC_MAC)
kCVPixelBufferOpenGLCompatibilityKey, (NSString *)kCVPixelBufferOpenGLCompatibilityKey : @(YES),
#endif #endif
kCVPixelBufferIOSurfacePropertiesKey, (NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{},
kCVPixelBufferPixelFormatTypeKey}; (NSString *)kCVPixelBufferPixelFormatTypeKey : @(framePixelFormat),
CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0); };
int64_t pixelFormatType = framePixelFormat;
CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &pixelFormatType); NSDictionary *encoder_specs;
CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelFormat};
CFDictionaryRef sourceAttributes = CreateCFTypeDictionary(keys, values, attributesSize);
if (ioSurfaceValue) {
CFRelease(ioSurfaceValue);
ioSurfaceValue = nullptr;
}
if (pixelFormat) {
CFRelease(pixelFormat);
pixelFormat = nullptr;
}
CFMutableDictionaryRef encoder_specs = nullptr;
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
// Currently hw accl is supported above 360p on mac, below 360p // Currently hw accl is supported above 360p on mac, below 360p
// the compression session will be created with hw accl disabled. // the compression session will be created with hw accl disabled.
encoder_specs = CFDictionaryCreateMutable( encoder_specs = @{
nullptr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); (NSString *)kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder : @(YES),
CFDictionarySetValue(encoder_specs, };
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
kCFBooleanTrue);
#endif #endif
OSStatus status = OSStatus status = VTCompressionSessionCreate(
VTCompressionSessionCreate(nullptr, // use default allocator nullptr, // use default allocator
_width, _width,
_height, _height,
kCMVideoCodecType_H264, kCMVideoCodecType_H264,
encoder_specs, // use hardware accelerated encoder if available (__bridge CFDictionaryRef)encoder_specs, // use hardware accelerated encoder if available
sourceAttributes, (__bridge CFDictionaryRef)sourceAttributes,
nullptr, // use default compressed data allocator nullptr, // use default compressed data allocator
compressionOutputCallback, compressionOutputCallback,
nullptr, nullptr,
&_compressionSession); &_compressionSession);
if (sourceAttributes) {
CFRelease(sourceAttributes);
sourceAttributes = nullptr;
}
if (encoder_specs) {
CFRelease(encoder_specs);
encoder_specs = nullptr;
}
if (status != noErr) { if (status != noErr) {
RTC_LOG(LS_ERROR) << "Failed to create compression session: " << status; RTC_LOG(LS_ERROR) << "Failed to create compression session: " << status;
return WEBRTC_VIDEO_CODEC_ERROR; return WEBRTC_VIDEO_CODEC_ERROR;