我正在尝试使用ffmpeg将RGB帧(ppm格式)转换为YUV420P格式。为了确保我的代码C ++是好的,我将输出与此命令创建的输出进行了比较(相同的文件管理器BILINEAR): ffmpeg -start_number 1 -i data / test512x512%d.ppm -sws_flags' bilinear' -pix_fmt yuv420p data / test-yuv420p.yuv
我的代码:
static unsigned char *readPPM(int i)
{
FILE *pF;
unsigned char *imgRGB;
unsigned char *imgBGR;
int w,h;
int c;
int bit;
char buff[16];
char *filename;
asprintf(&filename,"test512x512%d.ppm",i);
pF = fopen(filename,"rb");
free(filename);
if (pF) {
if (!fgets(buff, sizeof(buff), pF)) {
return nullptr;
}
if (buff[0] != 'P' || buff[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
return nullptr;
}
c = getc(pF);
while (c == '#') {
while (getc(pF) != '\n') ;
c = getc(pF);
}
ungetc(c, pF);
// read size
if (fscanf(pF, "%d %d", &w, &h) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
return nullptr;
}
//read bit
if (fscanf(pF, "%d", &bit) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}
imgRGB =(unsigned char*) malloc(3*h*w);
imgBGR =(unsigned char*) malloc(3*h*w);
//read pixel data from file
int length = fread(imgBGR, sizeof(unsigned char)*3, w*h, pF) ;
if (length != w*h) {
fprintf(stderr, "Error loading image '%s'\n", filename);
return nullptr;
}
int start=0;
for (i=0; i < HEIGHT*WIDTH;i++) {
imgRGB[start] = imgBGR[start];
imgRGB[start+2]= imgBGR[start+2];
imgRGB[start+1]= imgBGR[start+1];
start+=3;
}
fclose(pF);
free(imgBGR);
return imgRGB;
}
else {
return nullptr;
}
}
void Test_FFMPEG::FillFrame (uint8_t* pic, int index)
{
avpicture_fill((AVPicture*)RGBFrame, pic, AV_PIX_FMT_RGB24, encodeContext->width, encodeContext->height);
struct SwsContext* fooContext = sws_getContext(encodeContext->width, encodeContext->height,
PIX_FMT_RGB24,
encodeContext->width, encodeContext->height,
PIX_FMT_YUV420P,
SWS_BILINEAR , nullptr, nullptr, nullptr);
sws_scale(fooContext, RGBFrame->data, RGBFrame->linesize, 0, encodeContext->height, OrgFrame->data, OrgFrame->linesize);
OrgFrame->pts = index;
}
比较结果不好。在Y和V中略有不同但在U中很多。我无法发布我的图像,但有一部分Y在U图像中。它会使颜色发生变化。
你能告诉我我的错误在哪里吗?谢谢你
答案 0 :(得分:0)
我对ffmpeg默认值不肯定,但使用SWS_BILINEAR | SWS_ACCURATE_RND
会产生更好的结果。