gpt4 book ai didi

ios - 裁剪 CMSampleBufferRef

转载 作者:可可西里 更新时间:2023-11-01 05:41:39 27 4
gpt4 key购买 nike

我正在尝试将 CMSampleBufferRef 中的图像裁剪为特定大小。我正在执行 5 个步骤 - 1. 从 SampleBuffer 获取 PixelBuffer 2. 将 PixelBuffer 转换为 CIImage 3. 裁剪 CIImage 4. 将 CIImage 渲染回 PixelBuffer 5. 将 PixelBuffer 附加到 SampleBuffer。到目前为止,我在第 4 步时遇到问题 - 将图像渲染回 PixelBuffer(不能检查超出这一点)没有渲染到缓冲区(我使用相同的 CIImage imageWithCVPixelBuffer 检查它并得到 NULL 作为返回)。非常感谢任何提示或帮助。

CGRect cropRect = CGRectMake(0, 0, 640, 480);

CIImage *ciImage = [CIImage imageWithCVPixelBuffer:(CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer)]; //options: [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]];
ciImage = [ciImage imageByCroppingToRect:cropRect];

CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(kCFAllocatorSystemDefault, 640, 480, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

CIContext * ciContext = [CIContext contextWithOptions: nil];
[ciContext render:ciImage toCVPixelBuffer:pixelBuffer];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

CMSampleTimingInfo sampleTime = {
.duration = CMSampleBufferGetDuration(sampleBuffer),
.presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer),
.decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
};

CMVideoFormatDescriptionRef videoInfo = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);

CMSampleBufferRef oBuf;
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);

最佳答案

所以我找到了我遇到问题的原因。显然,我必须在初始化像素缓冲区时通过创建字典并将其传递给初始化程序来添加 kCVPixelBufferIOSurfacePropertiesKey 键。如果有人偶然发现这个问题,这里有一个代码:

CFDictionaryRef empty; // empty value for attr value.
CFMutableDictionaryRef attrs;
empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary
NULL,
NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attrs,
kCVPixelBufferIOSurfacePropertiesKey,
empty);

然后你只需要将这个字典作为参数传递给 CVPixelBufferCreate

我在这里找到了这个解决方案:http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/

关于ios - 裁剪 CMSampleBufferRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com