向 NSDictionary 添加结构

Adding a structure to a NSDictionary

我正在使用以下代码凭空创建一个 CMVideoFormatDescriptionRef

  CMVideoDimensions dimensions = {
    .width = width,
    .height = height
  };

  CMVideoFormatDescriptionRef videoInfo = NULL;

  NSDictionary *options2 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @(YES), kCVPixelBufferCGImageCompatibilityKey,
                           @(YES), kCVPixelBufferCGBitmapContextCompatibilityKey,
                           dimensions, kCVImageBufferDisplayDimensionsKey,
                            nil];
  CFDictionaryRef dictOptionsRef = (__bridge CFDictionaryRef)options2;

  CMFormatDescriptionCreate(kCFAllocatorDefault, kCMMediaType_Video, 'brga', dictOptionsRef, &videoInfo);

我需要将该字典传递给 CMFormatDescriptionCreate

最后一行有问题

dimensions, kCVImageBufferDisplayDimensionsKey,

这不是向该字典添加 dimensions 的正确方法。它使应用程序崩溃。

我尝试使用以下方法将整个内容包装在 NSValue 上:

NSValue *miValue = [NSValue value: &dimensions
                     withObjCType:@encode(CMVideoDimensions)];

理论上它可以工作,没有崩溃,但是 videoInfo 没有正确创建 (OSStatus -12731)。如果我从字典中删除最后一行,则会创建 videoInfo 但没有尺寸定义,假定为 0x0 像素或 codecType。见下文:

<CMVideoFormatDescription 0x17044c270 [0x1b0330bb8]> {
    mediaType:'vide' 
    mediaSubType:'brga' 
    mediaSpecific: {
        codecType: 0        dimensions: 0 x 0    <--HERE!!!
    } 
    extensions: {<CFBasicHash 0x17086dcc0 [0x1b0330bb8]>{type = immutable dict, count = 4,
entries =>
    0 : <CFString 0x1aa9427c8 [0x1b0330bb8]>{contents = "CVImageBufferYCbCrMatrix"} = <CFString 0x1aa942808 [0x1b0330bb8]>{contents = "ITU_R_601_4"}
    3 : <CFString 0x1aa942c68 [0x1b0330bb8]>{contents = "CGImageCompatibility"} = <CFBoolean 0x1b0331110 [0x1b0330bb8]>{value = true}
    5 : <CFString 0x1aa9428a8 [0x1b0330bb8]>{contents = "CVImageBufferColorPrimaries"} = <CFString 0x1aa9427e8 [0x1b0330bb8]>{contents = "ITU_R_709_2"}
    6 : <CFString 0x1aa942c48 [0x1b0330bb8]>{contents = "CGBitmapContextCompatibility"} = <CFBoolean 0x1b0331110 [0x1b0330bb8]>{value = true}
}
}
}

来自常规 CMSampleBufferRefCMVideoFormatDescriptionRefcodecType 显示为 BGRA 和框架的常规尺寸,例如 1920x1080。

我该怎么做?

kCVImageBufferDisplayDimensionsKey 头文件说键值是

// CFDictionary with the following two keys

kCVImageBufferDisplayWidthKeykCVImageBufferDisplayHeightKey,都是CFNumber

您传递的是 CMVideoDimensions,因此请将其更改为

NSDictionary *dimensions = [NSDictionary dictionaryWithObjectsAndKeys:@(width), kCVImageBufferDisplayWidthKey, @(height), kCVImageBufferDisplayHeightKey];