简体   繁体   English

核心图像 - 在CMSampleBufferRef上渲染透明图像会导致其周围出现黑框

[英]Core Image - rendering a transparent image on CMSampleBufferRef result in black box around it

I'm trying to add a watermark/logo on a video that I'm recording using AVFoundation's AVCaptureVideoDataOutput. 我正在尝试使用AVFoundation的AVCaptureVideoDataOutput在我正在录制的视频上添加水印/徽标。 My class is set as the sampleBufferDelegate and receives the CMSamplebufferRefs. 我的类被设置为sampleBufferDelegate并接收CMSamplebufferRefs。 I already apply some effects to the CMSampleBufferRefs CVPixelBuffer and pass it back to the AVAssetWriter. 我已经将一些效果应用于CMSampleBufferRefs CVPixelBuffer并将其传递回AVAssetWriter。

The logo in the top left corner is delivered using a transparent PNG. 左上角的徽标使用透明PNG传送。 The problem I'm having is that the transparent parts of the UIImage are black once written to the video. 我遇到的问题是,一旦写入视频,UIImage的透明部分就是黑色。 Anyone have an idea of what I'm doing wrong or could be forgetting? 任何人都知道我做错了什么或者可能会忘记?

Code snippets below: 代码片段如下:

//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);

....
}

It looks like the CIContext does not draw the CIImages alpha. 看起来CIContext没有绘制CIImages alpha。 Any ideas? 有任何想法吗?

For developers who've encountered the same issue: 对于遇到相同问题的开发人员:

It appears anything rendered on the GPU and written to the video ends up making a black hole in the video. 看起来在GPU上呈现的任何内容都会写入视频,最终会在视频中形成黑洞。 Instead, I removed the above code, created a CGContextRef, like you would do when editing images, and drew on that context. 相反,我删除了上面的代码,创建了一个CGContextRef,就像编辑图像时一样,并绘制了上下文。

Code: 码:

....
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(pixelBuffer),
                                             CVPixelBufferGetWidth(pixelBuffer),
                                             CVPixelBufferGetHeight(pixelBuffer),
                                             8,
                                             CVPixelBufferGetBytesPerRow(pixelBuffer),
                                             CGColorSpaceCreateDeviceRGB(),
                                             (CGBitmapInfo)
                                             kCGBitmapByteOrder32Little |
                                             kCGImageAlphaPremultipliedFirst);

CGRect renderBounds = ...
CGContextDrawImage(context, renderBounds, [overlayImage CGImage]);

CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CGColorSpaceRelease(cSpace);
....

And off course, the global EAGLContext and the CIContext are not needed anymore. 当然,不再需要全局EAGLContextCIContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM