2

How do you resize an AVFrame? I

Here's what I'm currently doing:

AVFrame* frame = /*...*/;

int width = 600, height = 400;
AVFrame* resizedFrame = av_frame_alloc();

auto format = AVPixelFormat(frame->format);

auto buffer = av_malloc(avpicture_get_size(format, width, height) * sizeof(uint8_t));

avpicture_fill((AVPicture *)resizedFrame, (uint8_t*)buffer, format, width, height);

struct SwsContext* swsContext = sws_getContext(frame->width, frame->height, format,
                                               width,         height,         format,
                                               SWS_BILINEAR,  nullptr,        nullptr,        nullptr);

sws_scale(swsContext, frame->data, frame->linesize, 0, frame->height, resizedFrame->data, resizedFrame->linesize);

But after this resizedFrames->widthand height are still 0, the contents of the AVFrame look like garbage, and I get an warning that data is unaligned when I call sws_scale. Note: I don't want to change the pixel format, and I don't want to hard code what it is.

David
  • 25,830
  • 16
  • 80
  • 130
  • How about this: http://stackoverflow.com/questions/12831761/how-to-resize-a-picture-using-ffmpegs-sws-scale – jim mcnamara Jul 06 '15 at 18:59
  • @jimmcnamara well I appear to be doing everything the same, except having hardcoded pixel formats. So is there something wrong with the way I'm passing pixel formats? Maybe sws_scale isn't supposed to fill out the new `width` and `height` in AVFrame? – David Jul 06 '15 at 19:02
  • I remember copying and using the code, but it has been several years. I do not know more. – jim mcnamara Jul 06 '15 at 19:06

1 Answers1

5

So, there's a few things going on.

I think if you follow these two suggestions, you'll find that the rescaling works as expected, gives no warnings and has correct output. The quality may not be great because you're trying to do bilinear scaling. Most people use bicubic scaling (SWS_BICUBIC).

Ronald S. Bultje
  • 9,576
  • 22
  • 43