Yoohoo Yoohoo - 3 months ago 35
C Question

ffmpeg: how to repalce img_convert() with sws_scale()

I have an old version FFmpeg code like this :

int ret = img_convert((AVPicture *)picture, c->pix_fmt,
(AVPicture *)input_picture, (PixelFormat)input_pix_fmt,
width, height);
if (ret < 0)
return false;


I noticed that in new ffmpeg, the img_convert() has been replaced by sws_scale(), I followed the format and change like this:

int w = width;
int h = height;

static struct SwsContext *img_convert_ctx
= sws_getContext(w, h, (PixelFormat)input_pix_fmt,
w, h, c->pix_fmt,
SWS_BICUBIC, NULL, NULL, NULL);

int ret = sws_scale(img_convert_ctx,
input_picture->data,
input_picture->linesize,
0,
height,
(AVPicture *)picture,
(AVPicture *)picture);

if (ret < 0) {
return false;
}

sws_freeContext(img_convert_ctx);


but it gives the error:

error: cannot convert ‘AVPicture*’ to ⏎
‘uint8_t* const* {aka unsigned char* const*}’ for argument ‘6’ to ⏎
‘int sws_scale(SwsContext*, const uint8_t* const*, ⏎
const int*, int, int, uint8_t* const*, const int*)’


It seems the error falls in the last two parameters of sws_scale. How should I modify it?

And is it caused by c and c++ incompatible problem? I am now run FFmpeg in C, and use extern C to include the headers.

Answer

sws_scale doesn't have a concept of AVPicture. Rather you pass the data lines and linesize lines. Which is

sws_scale(
    img_convert_ctx,
    input_picture->data, 
    input_picture->linesize,
    0,
    height, 
    picture->data,
    picture->linesize
);

By the way you can copy the AVPicture structure around so there is no reason to use pointers.