保存YUV数据到文件
-
保存AvFrame到文件
void SaveAvFrame(AVFrame* avFrame)
{
FILE* fDump = fopen(“avFrame”, “ab”);uint32_t pitchY = avFrame->linesize[0];
uint32_t pitchU = avFrame->linesize[1];
uint32_t pitchV = avFrame->linesize[2];uint8_t* avY = avFrame->data[0];
uint8_t* avU = avFrame->data[1];
uint8_t* avV = avFrame->data[2];
uint32_t i = 0;
printf("%s %d Begin\n", FUNCTION, LINE);
for (i = 0; i < avFrame->height; i++) {
fwrite(avY, avFrame->width, 1, fDump);
avY += pitchY;
}for ( i = 0; i < avFrame->height / 2; i++) {
fwrite(avU, avFrame->width / 2, 1, fDump);
avU += pitchU;
}for ( i = 0; i < avFrame->height / 2; i++) {
fwrite(avV, avFrame->width / 2, 1, fDump);
avV += pitchV;
}
printf("%s %d end\n", FUNCTION, LINE);
fclose(fDump);
}
2.保存YUV数据到文件
int saveYUVFrameToFile(AVFrame* frame, int width, int height,int flag)
{
FILE* fileHandle;
int y, writeError;
char filename[32];
static int frameNumber = 0;
printf("%s %d YUV flag = %d width = %d ,height = %d A\n", FUNCTION, LINE, flag, width, height);
if (flag == 0)
{//src
sprintf(filename, “./YUVS/frameS%d.yuv”, frameNumber);
}
else if (flag == 1)
{//dst
sprintf(filename, “./YUVD/frameD%d.yuv”, frameNumber);
}
fileHandle = fopen(filename, “wb”);
if (fileHandle == NULL)
{
printf(“Unable to open %s…\n”, filename);
return 0;
}
/*Writing Y plane data to file.*/
for (y = 0; y < height; y++)
{
writeError = fwrite(frame->data[0] + y * frame->linesize[0], 1, width, fileHandle);
if (writeError != width)
{
printf("Unable to write Y plane data!\n");
return 0;
}
}
/*Dividing by 2.*/
height >>= 1;
width >>= 1;
/*Writing U plane data to file.*/
for (y = 0; y < height; y++)
{
writeError = fwrite(frame->data[1] + y * frame->linesize[1], 1, width, fileHandle);
if (writeError != width)
{
printf("Unable to write U plane data!\n");
return 0;
}
}
/*Writing V plane data to file.*/
for (y = 0; y < height; y++)
{
writeError = fwrite(frame->data[2] + y * frame->linesize[2], 1, width, fileHandle);
if (writeError != width)
{
printf("Unable to write V plane data!\n");
return 0;
}
}
fclose(fileHandle);
frameNumber++;
printf("%s %d Save YUV End\n", __FUNCTION__, __LINE__);
return 1;
}
用途:
在解码后获取YUV数据,保存到文件,检测YUV数据是否正常
void decode(AVPacket* packet,AVCodecContext* codecContext)
int ret = 0;
AVFrame* pSrcFrame = av_frame_alloc();
AVFrame *filtFrame = av_frame_alloc();
if (!codecContext || !codecContext->codec)
{
goto error;
}
ret = avcodec_send_packet(codecContext, packet);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{
goto error;
}
ret = avcodec_receive_frame(codecContext, pSrcFrame);
if (ret < 0 && ret != AVERROR_EOF)
{
goto error;
}
saveYUVFrameToFile(pSrcFrame, pSrcFrame->width, pSrcFrame->height);
保存的YUV 图像正常
}
//保存的文件可以使用YUV工具播放