MPP硬解码得到的MppFrame为NV12格式,如何将该MppFrame转换成YUV420P格式的AVFrame,具体实现如下(示例中的frame代表MppFrame,mpp解码的视频帧大小为1920*1080,编码方式为H265):
AVFrame *avFrame = av_frame_alloc();
int width = mpp_frame_get_width(frame);
int height = mpp_frame_get_height(frame);
avFrame->width = 1920;
avFrame->height = 1080;
avFrame->format = AV_PIX_FMT_YUV420P;
int a = av_frame_get_buffer(avFrame,0);
MppFrameFormat format = mpp_frame_get_fmt(frame);
MppBuffer buff = mpp_frame_get_buffer(frame);
int stride = mpp_frame_get_hor_stride(frame);
int vStride = mpp_frame_get_ver_stride(frame);
uint8_t* data_y = (uint8_t*)mpp_buffer_get_ptr(buff);
uint8_t* data_uv = data_y + stride*height;
uint8_t* yPlane = avFrame->data[0];
uint8_t* uPlane = avFrame->data[1];
uint8_t* vPlane = avFrame->data[2];
int yPlaneSize = avFrame->linesize[0];
int uPlaneSize = avFrame->linesize[1];
int vPlaneSize = avFrame->linesize[2];
for (int i = 0; i < height; ++i) {
memcpy(yPlane + i * yPlaneSize, data_y + i * 2304, 1920);
}
//使用YUV420P格式
for (int i = 0; i < 540; ++i) { //i < height/2
for(int j = 0; j < 960; j++) { //j < width/2
memcpy(uPlane + i * uPlaneSize + j, data_uv + i*2304 + 2*j, 1);
memcpy(vPlane + i * vPlaneSize + j, data_uv + i*2304 + 2*j + 1, 1);
}
}