如何使用ffmpeg的sws_scale()调整图片大小? [英] How to resize a picture using ffmpeg's sws_scale()?
问题描述
我想通过使用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: / p>
AVFrame * frame1 = avcodec_alloc_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应该现在填写(例如使用avcodec_decode_video)
sws_scale(resize,frame1-> data,frame1- > linesize,0,height1,frame2> data,frame2> linesize);
请注意,我也更改了像素格式,但您可以对两个框架使用相同的像素格式
I wanna resize a picture by using the ffmpeg's func--->sws_scale().
Is there any one knows how to do it?
Do you have the source code for this function?
First you need to create a SwsContext
(you need to do this only once) :
struct SwsContext *resize;
resize = sws_getContext(width1, height1, AV_PIX_FMT_YUV420P, width2, height2, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
You need two frames for conversion, frame1 is the original frame, you need to explicitly allocate 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);
You may use this part inside a loop if you need to resize each frame you receive :
// frame1 should be filled by now (eg using avcodec_decode_video)
sws_scale(resize, frame1->data, frame1->linesize, 0, height1, frame2->data, frame2->linesize);
Note that I also changed pixel format, but you can use the same pixel format for both frames
这篇关于如何使用ffmpeg的sws_scale()调整图片大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!