1

我想尽快将 opengl 帧缓冲区数据传输到 AVCodec。

我已经使用着色器将 RGB 转换为 YUV 并使用 glReadPixels 读取它

我仍然需要手动填充 AVFrame 数据。有没有更好的办法?

AVFrame *frame;
// Y
frame->data[0][y*frame->linesize[0]+x] = data[i*3];
// U
frame->data[1][y*frame->linesize[1]+x] = data[i*3+1];
// V
frame->data[2][y*frame->linesize[2]+x] = data[i*3+2];
4

1 回答 1

0

您可以使用sws_scale.

事实上,你不需要着色器来转换 RGB->YUV。相信我,它不会有非常不同的表现。

swsContext = sws_getContext(WIDTH, HEIGHT, AV_PIX_FMT_RGBA, WIDTH, HEIGHT, AV_PIX_FMT_YUV, SWS_BICUBIC, 0, 0, 0 );
sws_scale(swsContext, (const uint8_t * const *)sourcePictureRGB.data, sourcePictureRGB.linesize, 0, codecContext->height, destinyPictureYUV.data, destinyPictureYUV.linesize);

中的数据destinyPictureYUV将准备好进入编解码器。

在此示例中,AVFrame您要填写的正是命运图片YUV。尝试这样设置:

AVFrame * frame;
AVPicture destinyPictureYUV;

avpicture_alloc(&destinyPictureYUV, codecContext->pix_fmt, newCodecContext->width, newCodecContext->height);

// THIS is what you want probably
*reinterpret_cast<AVPicture *>(frame) = destinyPictureYUV;

通过此设置,您还可以根据需要在 GPU 中填充已转换为 YUV 的数据……您可以选择所需的方式。

于 2015-09-21T18:33:50.673 回答