我正在尝试使用 FFmpeg 将 H264 流解码为 YUV 数据,然后通过LibYUV将 YUV 数据转换为 RGB 数据,最后绘制它。但是出现了一些奇怪的点(点)。如何解决?
av_init_packet(&m_avpkt);
int cur_size;
uint8_t *cur_ptr;
while (true)
{
memset(outSampleBuffer, 0, IMAGE_BUFFER);
long ret = m_pInputPortInfo->pPacketPool->receivePacket(outSampleBuffer, info);
cur_size = info.size;
cur_ptr = (uint8_t *)outSampleBuffer;
if (m_de_context == NULL)
continue;
while (cur_size > 0)
{
int len = av_parser_parse2(
pCodecParser, m_de_context,
&m_avpkt.data, &m_avpkt.size,
cur_ptr, cur_size,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
cur_ptr += len;
cur_size -= len;
if (m_avpkt.size == 0)
continue;
int got_picture = 0;
int iOutLen = avcodec_decode_video2(m_de_context, m_de_frame, &got_picture, &m_avpkt);
if (got_picture != 0)
{
//YUV to RGB
//...
}
}
为了找出哪一步错了,我在使用前保存了H264数据avcodec_decode_video2
,解码后保存了YUV数据。我发现所有的H264数据都是正确的,而YUV数据是错误的,用P帧解码,I帧解码数据是ok的。这是我保存 YUV 数据的方法:
int got_picture = 0;
int iOutLen = avcodec_decode_video2(m_de_context, m_de_frame, &got_picture, &m_avpkt);
if (got_picture != 0)
{
if (m_de_frame->format == AVPixelFormat::AV_PIX_FMT_YUV420P)
{
int y = m_de_frame->width*m_de_frame->height;
int u = y / 4;
int v = y / 4;
uint8_t* y_ptr = m_de_frame->data[0];
uint8_t* u_ptr = m_de_frame->data[1];
uint8_t* v_ptr = m_de_frame->data[2];
int yuvbufsize = y + u + v;
char *buf = new char[yuvbufsize];
memcpy(buf, y_ptr, y);
memcpy(buf + y, u_ptr, u);
memcpy(buf + y+u, v_ptr, v);
static int count = 0;
char yuvimgPath[MAX_PATH] = { 0 };
sprintf(yuvimgPath, "d:\\images\\de_%d.yuv", count);
FILE *fp1 = fopen(yuvimgPath, "wb");
fwrite(buf, 1, yuvbufsize, fp1);
fclose(fp1);
count++;
delete[]buf;
}
}