我正在尝试将图像裁剪CMSampleBufferRef为特定大小。我正在做 5 个步骤 - 1.PixelBuffer从SampleBuffer2. 转换PixelBuffer为CIImage3. 裁剪CIImage4. 渲染CIImage回PixelBuffer5. 附加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 …Run Code Online (Sandbox Code Playgroud) CMSampleBufferRef您好,我需要通过网络发送。然后客户端CMSampleBufferRef通过音频队列服务播放该音频。我见过一些关于堆栈溢出的例子,但大多数只是发送缓冲区。但随后一些信息就会丢失。我发现它[AVAssetReaderOutput copyNextSampleBuffer]返回一个结构体的引用opaqueCMSampleBuffer。我知道如何获取 的内存地址opaqueCMSampleBuffer,但是如何将地址的内容复制到数组中以便可以通过网络发送它?或者有没有更优雅的方法通过CMSampleBuffer网络发送。或者我什至可以以某种方式访问opaqueCMSampleBuffer?
感谢您的时间和帮助
objective-c avfoundation audioqueueservices cmsamplebufferref
我想用AVSampleBufferDisplayLayer显示一些CMSampleBuffer,但是在显示第一个样本后冻结。
我从AVCaptureVideoDataOutputSampleBuffer委托获取了samplebuffers:
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CFRetain(sampleBuffer);
[self imageToBuffer:sampleBuffer];
CFRelease(sampleBuffer);
}
Run Code Online (Sandbox Code Playgroud)
将它们放入向量
-(void) imageToBuffer: (CMSampleBufferRef )source{
//buffers is defined as: std::vector<CMSampleBufferRef> buffers;
CMSampleBufferRef newRef;
CMSampleBufferCreateCopy(kCFAllocatorDefault, source, &newRef);
buffers.push_back(newRef);
}
Run Code Online (Sandbox Code Playgroud)
然后尝试通过AVSampleBufferDisplayLayer(在另一个ViewController中)显示它们
AVSampleBufferDisplayLayer * displayLayer = [[AVSampleBufferDisplayLayer alloc] init];
displayLayer.bounds = self.view.bounds;
displayLayer.position = CGPointMake(CGRectGetMidX(self.displayOnMe.bounds), CGRectGetMidY(self.displayOnMe.bounds));
displayLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
displayLayer.backgroundColor = [[UIColor greenColor] CGColor];
[self.view.layer addSublayer:displayLayer];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
dispatch_queue_t queue = dispatch_queue_create("My queue", DISPATCH_QUEUE_SERIAL);
[displayLayer setNeedsDisplay];
[displayLayer requestMediaDataWhenReadyOnQueue:queue
usingBlock:^{
while ([displayLayer isReadyForMoreMediaData]) {
if (samplesKey < buffers.size()) …Run Code Online (Sandbox Code Playgroud) 我正在使用captureOutput:didOutputSampleBuffer:fromConnection:它来跟踪帧.对于我的用例,我只需要存储最后一帧并在应用程序转到后台时使用它.
这是我的代码中的一个示例:
@property (nonatomic, strong) AVCaptureVideoDataOutput *videoDataOutput;
@property (atomic) CMSampleBufferRef currentBuffer;
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码段将CVPixelBufferRef转换为UIImage:
UIImage *image = nil;
CMSampleBufferRef sampleBuffer = (CMSampleBufferRef)CMBufferQueueDequeueAndRetain(_queue);
if (sampleBuffer)
{
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
NSUInteger width = CVPixelBufferGetWidth(pixelBuffer);
NSUInteger height = CVPixelBufferGetHeight(pixelBuffer);
CIImage *coreImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:nil];
CGImageRef imageRef = [_context createCGImage:coreImage fromRect:CGRectMake(0, 0, width, height)];
image = [UIImage imageWithCGImage:imageRef];
CFRelease(sampleBuffer);
CFRelease(imageRef);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我在设备上运行代码但在模拟器上运行时无法渲染时,它工作正常,控制台输出以下内容:
渲染失败,因为不支持像素格式YCC420f
有任何想法吗?
我试图使用AVFoundation框架快速捕获来自AVCaptureStillImageOutput的"系列"静止图像,就像某些相机中的连拍模式一样.我想使用完成处理程序,
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
Run Code Online (Sandbox Code Playgroud)
并将imageSampleBuffer传递给NSOperation对象以供以后处理.但是我找不到在NSOperation类中保留缓冲区的方法.
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
//Add to queue
SaveImageDataOperation *saveOperation = [[SaveImageDataOperation alloc] initWithImageBuffer:imageSampleBuffer];
[_saveDataQueue addOperation:saveOperation];
[saveOperation release];
//Continue
[self captureCompleted];
}];
Run Code Online (Sandbox Code Playgroud)
有谁知道我在这里做错了什么?有没有更好的方法来做到这一点?
我正在尝试使用AVFoundation的AVCaptureVideoDataOutput在我正在录制的视频上添加水印/徽标.我的类被设置为sampleBufferDelegate并接收CMSamplebufferRefs.我已经将一些效果应用于CMSampleBufferRefs CVPixelBuffer并将其传递回AVAssetWriter.
左上角的徽标使用透明PNG传送.我遇到的问题是,一旦写入视频,UIImage的透明部分就是黑色.任何人都知道我做错了什么或者可能会忘记?
代码片段如下:
//somewhere in the init of the class;
_eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_ciContext = [CIContext contextWithEAGLContext:_eaglContext
options: @{ kCIContextWorkingColorSpace : [NSNull null] }];
//samplebufferdelegate method:
- (void) captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
....
UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
CIImage *renderImage = [[CIImage alloc] initWithCGImage:logoImage.CGImage];
CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();
[_ciContext render:renderImage
toCVPixelBuffer:pixelBuffer
bounds: [renderImage extent]
colorSpace:cSpace];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CGColorSpaceRelease(cSpace);
....
}
Run Code Online (Sandbox Code Playgroud)
看起来CIContext没有绘制CIImages alpha.有任何想法吗?
我试图了解CMPixelBuffer使用 Metal操作 Video output( )的正确方法是什么。
据我了解有MTKView。CMPixelBuffer来自视频输出的每个都被分配给了Metal Texture. 所以最终的预览是来自MTKView?
当我在屏幕上看到最终结果时,是不是:
1)CMSampleBuffer->Metal->CMSampleBuffer
Run Code Online (Sandbox Code Playgroud)
或者
2)CMSampleBuffer->Metal->MTKView
Run Code Online (Sandbox Code Playgroud)
很困惑。有人可以把东西放在现场吗?
这可能是一个愚蠢的问题,但我刚开始学习媒体格式和AVFoundation,所以请耐心等待.
我一直试图弄清楚AVCaptureVideoDataOutput中的CMSampleBuffer是否可以包含多个帧.然而,从文档中可以看出,我看到的大多数示例代码似乎与CMSampleBuffer相似,就好像它是单帧一样.我只是误解了这个吗?
如果每个缓冲区可以有多个帧,是否可以获取各个帧并确定它是否是关键帧?
谢谢
我需要从CMSampleBufferRef 获取UIImage来自未压缩的图像数据.我正在使用代码:
captureStillImageOutput captureStillImageAsynchronouslyFromConnection:connection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
// that famous function from Apple docs found on a lot of websites
// does NOT work for still images
UIImage *capturedImage = [self imageFromSampleBuffer:imageSampleBuffer];
}
Run Code Online (Sandbox Code Playgroud)
http://developer.apple.com/library/ios/#qa/qa1702/_index.html是imageFromSampleBuffer功能链接.
但它不能正常工作.:(
有一种jpegStillImageNSDataRepresentation:imageSampleBuffer方法,但它提供压缩数据(好吧,因为JPEG).
UIImage捕获静止图像后,如何使用最原始的非压缩数据创建?
也许,我应该为视频输出指定一些设置?我目前正在使用这些:
captureStillImageOutput = [[AVCaptureStillImageOutput alloc] init];
captureStillImageOutput.outputSettings = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
Run Code Online (Sandbox Code Playgroud)
我注意到,输出有一个默认值AVVideoCodecKey,即AVVideoCodecJPEG.是否可以以任何方式避免,或者甚至在拍摄静止图像时是否重要?
我在那里找到了一些东西:来自相机的原始图像数据,如"645 PRO",但我只需要一个UIImage,而不使用OpenCV或OGLES或其他第三方.
我试图从CMSampleBufferRef中检索CVPixelBufferRef,以便改变CVPixelBufferRef以动态覆盖水印.
我正在使用CMSampleBufferGetImageBuffer(sampleBuffer)以实现这一目标.我正在打印返回的CVPixelBufferRef的结果,但它始终为null.
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
NSLog(@"PixelBuffer %@",pixelBuffer);
...
}
Run Code Online (Sandbox Code Playgroud)
我有什么我想念的吗?
我尝试编写ios相机,然后从apple中获取了部分代码:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// Create a UIImage from the sample buffer data
UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
< Add your code here that uses the image >
}
Run Code Online (Sandbox Code Playgroud)
我需要在程序的任何位置调用此函数。但是因为它需要创建一个对象类型(CMSampleBufferRef)。怎么做?
我试图写类似:
buf1 = [[CMSampleBufferRef alloc]init]
Run Code Online (Sandbox Code Playgroud)
但这是错误的方式。
我正在编写代码来解压本机附件 B H.264 流,我正在完成解析流的过程,从 SPS/PPS NALU 创建 CMVideoFormatDescription,并包装我从流中提取的其他 NALU在 CMSampleBuffers 中。
我在如何处理解码器的 CMBlockBuffer 和 CMSampleBuffer 内存方面遇到了心理障碍。我相信我的问题更多是缺乏对 CF 如何处理内存的透彻理解,所以我的问题更多的是关于这一点,但我希望上下文有帮助。
如果我像这样创建一个 CMBlockBuffer:
CMBlockBufferRef blockBuffer;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(NULL,
memoryBlock,
blockBufferLength,
kCFAllocatorNull,
NULL,
0,
blockBufferLength,
kCMBlockBufferAlwaysCopyDataFlag | kCMBlockBufferAssureMemoryNowFlag,
&blockBuffer);
Run Code Online (Sandbox Code Playgroud)
并将其添加到 CMSampleBuffer 中,如下所示:
CMSampleBufferRef sampleBuffer;
status = CMSampleBufferCreate(kCFAllocatorDefault,
blockBuffer,
true,
NULL,
NULL,
formatDescription,
1,
0,
NULL,
1,
&sampleSize,
&sampleBuffer);
Run Code Online (Sandbox Code Playgroud)
我应该如何处理块缓冲区?SampleBuffer 是否保留块缓冲区的内存,或者我是否需要做一些事情来确保它没有被释放?
另外,关于异步解码过程,是否有一种明智的方法可以知道解码器何时使用 CMSampleBuffer 完成以便我可以处理它?
我的直觉告诉我 CMSampleBuffer 会保留 CMBlockBuffer,而 VTDecodeSession 会保留 CMSampleBuffer 直到它完成解码,但这是一个未记录的领域,我正在寻找一些方向。我得到的结果暗示我的直觉可能是错误的,所以我需要排除内存管理作为一个问题来保持我的理智......
ios ×8
avfoundation ×6
objective-c ×6
core-image ×2
camera ×1
ciimage ×1
h.264 ×1
macos ×1
metal ×1
rendering ×1
swift ×1