我想通过使用ffmpeg的func ---> sws_scale()来调整图片大小。

有谁知道该怎么做吗?

您有此功能的源代码吗?

最佳答案

首先,您需要创建一个SwsContext(只需执行一次):

struct SwsContext *resize;
resize = sws_getContext(width1, height1, AV_PIX_FMT_YUV420P, width2, height2, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);

您需要两个帧进行转换,frame1是原始帧,您需要显式分配frame2:
AVFrame* frame1 = avcodec_alloc_frame(); // this is your original frame

AVFrame* frame2 = avcodec_alloc_frame();
int num_bytes = avpicture_get_size(AV_PIX_FMT_RGB24, width2, height2);
uint8_t* frame2_buffer = (uint8_t *)av_malloc(num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)frame2, frame2_buffer, AV_PIX_FMT_RGB24, width2, height2);

如果需要调整接收到的每个帧的大小,可以在循环中使用此部分:
// frame1 should be filled by now (eg using avcodec_decode_video)
sws_scale(resize, frame1->data, frame1->linesize, 0, height1, frame2->data, frame2->linesize);

请注意,我也更改了像素格式,但是您可以在两个帧中使用相同的像素格式

10-08 16:26