http://bbs.chinavideo.org/forum.php?mod=forumdisplay&fid=10
转自:http://bbs.chinavideo.org/forum.php?mod=viewthread&tid=1971
void YV12ToAVFrame(BYTE * pData,int nLen,int nWidth,int nHeight,AVFrame * pFrame)
{
avcodec_get_frame_defaults(pFrame);
int size=avpicture_get_size(PIX_FMT_YUV420P,nWidth ,nHeight );
ASSERT(size==nLen);
avpicture_fill( (AVPicture*) pFrame,pData,PIX_FMT_YUV420P , nWidth ,nHeight );
BYTE * ptmp=pFrame->data [1];//U,V互换
pFrame->data [1]=pFrame->data [2];
pFrame->data [2]=ptmp;
}
void AVFrameToYV12(AVFrame * pFrame,int nWidth,int nHeight,BYTE * pData,int * npLen)
{
BYTE * ptmp=pFrame->data [1];//U,V互换
pFrame->data [1]=pFrame->data [2];
pFrame->data [2]=ptmp;
AVPicture pic;
int size=avpicture_get_size(PIX_FMT_YUV420P,nWidth ,nHeight );
if(*npLen<size)
return;//缓冲区太小
avpicture_fill(&pic, pData, PIX_FMT_YUV420P,
nWidth, nHeight);
*npLen=size;
img_copy(&pic,(AVPicture *)pFrame,PIX_FMT_YUV420P,nWidth,nHeight);
ptmp=pFrame->data [1];//恢复AVFrame
pFrame->data [1]=pFrame->data [2];
pFrame->data [2]=ptmp;
}