【发布时间】:2014-03-23 05:14:05
【问题描述】:
我正在尝试在 ffmpeg/libav 中将 RGB 帧转换为 YUV420P 格式。以下是转换代码以及转换前后的图像。转换后的图像会丢失所有颜色信息,并且比例也会发生显着变化。有人知道如何处理吗?我对 ffmpeg/libav 完全陌生!
// Did we get a video frame?
if(frameFinished)
{
i++;
sws_scale(img_convert_ctx, (const uint8_t * const *)pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);
//==============================================================
AVFrame *pFrameYUV = avcodec_alloc_frame();
// Determine required buffer size and allocate buffer
int numBytes2 = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
uint8_t *buffer = (uint8_t *)av_malloc(numBytes2*sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);
rgb_to_yuv_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
PIX_FMT_RGB24,
pCodecCtx->width,pCodecCtx->height,
PIX_FMT_RGB24,
SWS_BICUBIC, NULL,NULL,NULL);
sws_scale(rgb_to_yuv_ctx, pFrameRGB->data, pFrameRGB->linesize, 0,
pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
sws_freeContext(rgb_to_yuv_ctx);
SaveFrame(pFrameYUV, pCodecCtx->width, pCodecCtx->height, i);
av_free(buffer);
av_free(pFrameYUV);
}
【问题讨论】:
-
这些图像与我的相似,我正在做 rgb2yuv 并手动返回,黑白图像正是我在 GIMP 中打开时看到的,但数据是正确的,它与我的相机发送的相同出,所以它可能只是一个预览问题?你能用另一种方式测试数据吗?
标签: ffmpeg video-encoding libavcodec libav libavformat