【问题标题】:OpenGL Texture from CALayer (AVPlayerLayer)来自 CALayer (AVPlayerLayer) 的 OpenGL 纹理
【发布时间】:2011-08-12 00:08:33
【问题描述】:

我有一个 AVPlayerLayer,我想用它创建一个 OpenGL 纹理。我对 opengl 纹理很满意,甚至对将 CGImageRef 转换为 opengl 纹理也很满意。在我看来,下面的代码应该可以工作,但我得到的只是纯黑色。我究竟做错了什么?我需要先在 CALayer / AVPlayerLayer 上设置任何属性吗?

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

int width = (int)[layer bounds].size.width;
int height = (int)[layer bounds].size.height;

CGContextRef context = CGBitmapContextCreate(NULL,
                                 width,
                                 height,
                                 8,
                                 width * 4,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedLast);

CGColorSpaceRelease(colorSpace);

if (context== NULL) {
    ofLog(OF_LOG_ERROR, "getTextureFromLayer: failed to create context 1");
    return;
}

[[layer presentationLayer] renderInContext:context];

CGImageRef cgImage = CGBitmapContextCreateImage(context);

int bytesPerPixel   = CGImageGetBitsPerPixel(cgImage)/8;
if(bytesPerPixel == 3) bytesPerPixel = 4;

GLubyte *pixels     = (GLubyte *) malloc(width * height * bytesPerPixel);

CGContextRelease(context);
context = CGBitmapContextCreate(pixels,
                                width,
                                height,
                                CGImageGetBitsPerComponent(cgImage),
                                width * bytesPerPixel,
                                CGImageGetColorSpace(cgImage),
                                kCGImageAlphaPremultipliedLast);

if(context == NULL) {
    ofLog(OF_LOG_ERROR, "getTextureFromLayer: failed to create context 2");
    free(pixels);
    return;
}

CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), cgImage);

int glMode;
switch(bytesPerPixel) {
    case 1:
        glMode = GL_LUMINANCE;
        break;
    case 3: 
        glMode = GL_RGB;
        break;
    case 4: 
    default:
        glMode = GL_RGBA; break;
}

if(texture.bAllocated() == false || texture.getWidth() != width || texture.getHeight() != height) {
    NSLog(@"getTextureFromLayer: allocating texture %i, %i\n", width, height);
    texture.allocate(width, height, glMode, true);
}

// test texture
//        for(int i=0; i<width*height*4; i++) pixels[i] = ofRandomuf() * 255;

texture.loadData(pixels, width, height, glMode);

CGContextRelease(context);
CFRelease(cgImage);
free(pixels);

附:变量“纹理”是一个 C++ opengl(-es 兼容)纹理对象,我知道它可以工作。如果我取消注释“测试纹理”for-loop 用随机噪声填充纹理,我可以看到,所以问题肯定在之前。

更新

为了回应 Nick Weaver 的回复,我尝试了一种不同的方法,现在我总是从 copyNextSampleBuffer 返回 NULL,状态 == 3 (AVAssetReaderStatusFailed)。我错过了什么吗?

变量

AVPlayer                *videoPlayer;
AVPlayerLayer           *videoLayer;
AVAssetReader           *videoReader;
AVAssetReaderTrackOutput*videoOutput;

初始化

    videoPlayer = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:videoPath.c_str()]]];

    if(videoPlayer == nil) {
        NSLog(@"videoPlayer == nil ERROR LOADING %s\n", videoPath.c_str());
    } else {
        NSLog(@"videoPlayer: %@", videoPlayer);
        videoLayer = [[AVPlayerLayer playerLayerWithPlayer:videoPlayer] retain];
        videoLayer.frame = [ThreeDView instance].bounds;
        // [[ThreeDView instance].layer addSublayer:videoLayer]; // test to see if it's loading and running

        AVAsset *asset = videoPlayer.currentItem.asset;
        NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
        NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], (NSString*)kCVPixelBufferPixelFormatTypeKey, nil];

        videoReader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
        videoOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:[tracks objectAtIndex:0] outputSettings:settings];
        [videoReader addOutput:videoOutput];
        [videoReader startReading];
    }

绘制循环

    if(videoPlayer == 0) {
        ofLog(OF_LOG_WARNING, "Shot::drawVideo: videoPlayer == 0");
        return;
    }


    if(videoOutput == 0) {
        ofLog(OF_LOG_WARNING, "Shot::drawVideo: videoOutput == 0");
        return;
    }

    CMSampleBufferRef sampleBuffer = [videoOutput copyNextSampleBuffer];

    if(sampleBuffer == 0) {
        ofLog(OF_LOG_ERROR, "Shot::drawVideo: sampleBuffer == 0, status: %i", videoReader.status);
        return;
    }


    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CFRelease(sampleBuffer);

    CVPixelBufferLockBaseAddress(imageBuffer,0); 

    unsigned char *pixels = ( unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer); 

    int width = CVPixelBufferGetWidth(imageBuffer); 
    int height = CVPixelBufferGetHeight(imageBuffer);

    if(videoTexture.bAllocated() == false || videoTexture.getWidth() != width || videoTexture.getHeight() != height) {
        NSLog(@"Shot::drawVideo() allocating texture %i, %i\n", width, height);
        videoTexture.allocate(width, height, GL_RGBA, true);
    }

    videoTexture.loadData(pixels, width, height, GL_BGRA);

    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

【问题讨论】:

  • 您是否在流式传输受 FairPlay 保护的 HLS 媒体?如果是这样,我认为尝试获取对解密数据的引用在设计上是行不通的:stackoverflow.com/q/42839831/1097106

标签: cocoa-touch ios opengl-es calayer avfoundation


【解决方案1】:

我认为iOS4: how do I use video file as an OpenGL texture? 会对您的问题有所帮助。

【讨论】:

  • 谢谢,这是一种不同的方法,但我现在也尝试过。我总是从 copyNextSampleBuffer 得到 NULL,我在我的问题中添加了新代码。我错过了什么吗?
  • 如果您的视频是 HTTP Live Streaming 格式,那么没有简单的方法可以从中获取单个帧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多