如何将CMSampleBuffer/UIImage转换为ffmpeg的AVPicture?

8
我正在尝试使用ffmpeg的libav*库将iPhone相机帧编码为H.264视频。我在这篇苹果文章中找到了如何将CMSampleBuffer转换为UIImage,但是我该如何将其转换为ffmpeg的AVPicture呢?
谢谢。
1个回答

15

回答我自己的问题:

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已经填充了样本缓冲区的内容,该缓冲区使用像素格式kCVPixelFormatType_32BGRA

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


在调用CVPixelBufferUnlockBaseAddress()之前,应该先调用avpicture_fill(),因为前者访问CVImageBufferRef中的原始像素数据。 - seertaak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接