【问题标题】:How to Convert CMSampleBuffer/UIImage into ffmpeg's AVPicture?如何将 CMSampleBuffer/UIImage 转换为 ffmpeg 的 AVPicture?
【发布时间】:2011-05-28 19:15:24
【问题描述】:

我正在尝试使用 ffmpeg 的 libav* 库将 iPhone 的相机帧编码为 H.264 视频。我发现在这个 Apple's article如何将CMSampleBuffer转为UIImage,但是如何转为ffmpeg的AVPicture呢?

谢谢。

【问题讨论】:

    标签: iphone ios4 avfoundation video-encoding libav


    【解决方案1】:

    回答我自己的问题:

    CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    
    // access the data
    int width = CVPixelBufferGetWidth(pixelBuffer);
    int height = CVPixelBufferGetHeight(pixelBuffer);
    unsigned char *rawPixelBase = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);
    
    // Do something with the raw pixels here
    // ...
    
    // Fill in the AVFrame
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    
    AVFrame *pFrame;
    pFrame = avcodec_alloc_frame();
    
    avpicture_fill((AVPicture*)pFrame, rawPixelBase, PIX_FMT_RGB32, width, height);
    

    现在pFrame填入了sample buffer的内容,使用的是像素格式kCVPixelFormatType_32BGRA

    这解决了我的问题。谢谢。

    【讨论】:

    • 是否应该在调用 CVPixelBufferUnlockBaseAddress() 之前调用 avpicture_fill(),因为前者访问 CVImageBufferRef 中的原始像素数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多