iPhone rgbaからargb
-
08-10-2019 - |
質問
GlreadPixelsを使用してOpenGLシーンのスクリーンショットをつかみ、iOS 4のAvassetWriterを使用してビデオに変換します。私の問題は、Alphaチャンネルをkcvpixelformattype_32Argbとglreadpixelsを受け入れるビデオに渡す必要があることです。したがって、基本的には、RGBAをArgbに変換する方法が必要です。つまり、Alphaバイトを最初に置きます。
int depth = 4;
unsigned char buffer[width * height * depth];
glReadPixels(0,0,width, height, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer), width*height*depth, NULL );
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGImageRef image = CGImageCreate(width, height, 8, 32, width*depth, CGColorSpaceCreateDeviceRGB(), bitmapInfo, ref, NULL, true, kCGRenderingIntentDefault);
UIWindow* parentWindow = [self window];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess);
NSParameterAssert(pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, width, height, 8, depth*width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, parentWindow.transform);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer; // chuck pixel buffer into AVAssetWriter
他の誰かを助けるかもしれないので、私はコード全体を投稿すると思いました。
乾杯
解決
注:チャネルごとに8ビットを想定しています。そうでない場合は、それに応じて調整します。
アルファビットを最後に移動するには、実行する必要があります 回転. 。これは通常、ビットシフトによって最も簡単に表現されます。
この場合、RGBビット8ビットを右に移動し、Aビット24ビットを残します。これらの2つの値はビットワイズを使用してまとめてください。 argb = (rgba >> 8) | (rgba << 24)
.
他のヒント
さらに良いことに、Argbを使用してビデオをエンコードしないでください。AvassetWriterBgraフレームを送信してください。私が説明しているように この答え, 、そうすることで、iPhone 4の30 fpsで640x480ビデオを、720pビデオで最大20 fpsのビデオをエンコードできます。 iPhone 4は、これを使用して30 fpsで最大1080pのビデオを進むことができます。
また、毎回ピクセルバッファーを再現する代わりに、ピクセルバッファープールを使用することを確認する必要があります。その回答からコードをコピーすると、これを使用してavassetwriterを構成します。
NSError *error = nil;
assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL fileType:AVFileTypeAppleM4V error:&error];
if (error != nil)
{
NSLog(@"Error: %@", error);
}
NSMutableDictionary * outputSettings = [[NSMutableDictionary alloc] init];
[outputSettings setObject: AVVideoCodecH264 forKey: AVVideoCodecKey];
[outputSettings setObject: [NSNumber numberWithInt: videoSize.width] forKey: AVVideoWidthKey];
[outputSettings setObject: [NSNumber numberWithInt: videoSize.height] forKey: AVVideoHeightKey];
assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
assetWriterVideoInput.expectsMediaDataInRealTime = YES;
// You need to use BGRA for the video in order to get realtime encoding. I use a color-swizzling shader to line up glReadPixels' normal RGBA output with the movie input's BGRA.
NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
[NSNumber numberWithInt:videoSize.width], kCVPixelBufferWidthKey,
[NSNumber numberWithInt:videoSize.height], kCVPixelBufferHeightKey,
nil];
assetWriterPixelBufferInput = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:assetWriterVideoInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
[assetWriter addInput:assetWriterVideoInput];
次に、このコードを使用して、レンダリングされた各フレームを使用して取得します glReadPixels()
:
CVPixelBufferRef pixel_buffer = NULL;
CVReturn status = CVPixelBufferPoolCreatePixelBuffer (NULL, [assetWriterPixelBufferInput pixelBufferPool], &pixel_buffer);
if ((pixel_buffer == NULL) || (status != kCVReturnSuccess))
{
return;
}
else
{
CVPixelBufferLockBaseAddress(pixel_buffer, 0);
GLubyte *pixelBufferData = (GLubyte *)CVPixelBufferGetBaseAddress(pixel_buffer);
glReadPixels(0, 0, videoSize.width, videoSize.height, GL_RGBA, GL_UNSIGNED_BYTE, pixelBufferData);
}
// May need to add a check here, because if two consecutive times with the same value are added to the movie, it aborts recording
CMTime currentTime = CMTimeMakeWithSeconds([[NSDate date] timeIntervalSinceDate:startTime],120);
if(![assetWriterPixelBufferInput appendPixelBuffer:pixel_buffer withPresentationTime:currentTime])
{
NSLog(@"Problem appending pixel buffer at time: %lld", currentTime.value);
}
else
{
// NSLog(@"Recorded pixel buffer at time: %lld", currentTime.value);
}
CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);
CVPixelBufferRelease(pixel_buffer);
使用するとき glReadPixels()
, 、あなたはあなたのフレームの色をスイズする必要があるので、私はこれを行うために次のコードでオフスクリーンFBOとフラグメントシェーダーを使用しました:
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
void main()
{
gl_FragColor = texture2D(inputImageTexture, textureCoordinate).bgra;
}
ただし、iOS 5.0にはさらに高速なルートがあり、OpenGL ESコンテンツをつかむよりも glReadPixels()
, 、私が説明しています この答え. 。そのプロセスの良いところは、テクスチャがすでにBGRAピクセル形式でコンテンツを保存していることです。そのため、カプセル化するピクセルバッファーを色変換せずにAvassetWriterに直接供給しても、優れたエンコーディング速度を見ることができます。
この質問に答えられたことはわかりますが、私は人々がVimage、Accelerate Frameworkの一部、iOSとOSXで利用可能であることを確認したかったのです。私の理解では、VimageはコアグラフィックスがビットマップでCPUバウンドベクター操作を行うために使用されます。
ArgbをRGBAに変換するために必要な特定のAPIは、vimagepermutechannels_argb8888です。また、RGBをARGB/XRGBに変換し、画像を裏返し、チャネルを上書きするAPIもあります。それは一種の隠された宝石です!
更新:ブラッドラーソンは本質的に同じ質問に素晴らしい答えを書きました ここ.
うん、チャンネルあたり8ビットなので、次のようなものです。
int depth = 4;
int width = 320;
int height = 480;
unsigned char buffer[width * height * depth];
glReadPixels(0,0,width, height, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
buffer[i*j] = (buffer[i*j] >> 8) | (buffer[i*j] << 24);
}
}
私はそれを機能させることができないようです
アルファ値は無視できると確信しています。だからあなたはただできる memcpy
ピクセルバッファーアレイが1つのバイトでシフトした状態で:
void *buffer = malloc(width*height*4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);
…
memcpy(pxdata + 1, buffer, width*height*4 - 1);
+ (UIImage *) createARGBImageFromRGBAImage: (UIImage *)image {
CGSize dimensions = [image size];
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * dimensions.width;
NSUInteger bitsPerComponent = 8;
unsigned char *rgba = malloc(bytesPerPixel * dimensions.width * dimensions.height);
unsigned char *argb = malloc(bytesPerPixel * dimensions.width * dimensions.height);
CGColorSpaceRef colorSpace = NULL;
CGContextRef context = NULL;
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(rgba, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
CGContextDrawImage(context, CGRectMake(0, 0, dimensions.width, dimensions.height), [image CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
for (int x = 0; x < dimensions.width; x++) {
for (int y = 0; y < dimensions.height; y++) {
NSUInteger offset = ((dimensions.width * y) + x) * bytesPerPixel;
argb[offset + 0] = rgba[offset + 3];
argb[offset + 1] = rgba[offset + 0];
argb[offset + 2] = rgba[offset + 1];
argb[offset + 3] = rgba[offset + 2];
}
}
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(argb, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
CGImageRef imageRef = CGBitmapContextCreateImage(context);
image = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(rgba);
free(argb);
return image;
}