Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to properly initialize AVFrame for sws_scale conversion

Tags:

ffmpeg

opengl

I'm decoding video using FFMpeg, and want to edit the decoded frames using OpenGL, but in order to do that I need to convert the data in AVFrame from YUV to RGB.

In order to do that I create a new AVFrame:

AVFrame *inputFrame = av_frame_alloc();
AVFrame *outputFrame = av_frame_alloc();
av_image_alloc(outputFrame->data, outputFrame->linesize, width, height, AV_PIX_FMT_RGB24, 1);
av_image_fill_arrays(outputFrame->data, outputFrame->linesize, NULL, AV_PIX_FMT_RGB24, width, height, 1);

Create a conversion context:

struct SwsContext *img_convert_ctx = sws_getContext(width, height, AV_PIX_FMT_YUV420P,
                                                  width, height, AV_PIX_FMT_RGB24,
                                                  0, NULL, NULL, NULL);

And then try to convert it to RGB:

sws_scale(img_convert_ctx, (const uint8_t *const *)&inputFrame->data, inputFrame->linesize, 0, inputFrame->height, outputFrame->data, outputFrame->linesize);

But this causes an "[swscaler @ 0x123f15000] bad dst image pointers" error during run time. When I went over FFMpeg's source I found out that the reason is that outputFrame's data wasn't initialized, but I don't understand how it should be.

All existing answers or tutorials that I found (see example) seem to use deprecated APIs, and it's unclear how to use the new APIs. I'd appreciate any help.

like image 205
nihohit Avatar asked Oct 26 '25 09:10

nihohit


1 Answers

Here's how I call sws_scale:

image buf2((buf.w + 15)/16*16, buf.h, 3);
sws_scale(sws_ctx, (const uint8_t * const *)frame->data, frame->linesize, 0, c->height, (uint8_t * const *)buf2.c, &buf2.ys);

There are two differences here:

  1. You pass &inputFrame->data but it shall be inputFrame->data without the address-of operator.

  2. You don't have to allocate a second frame structure. The sws_scale doesn't care about it. It just needs a chunk of memory of the proper size (and maybe alignment).

like image 185
Yakov Galka Avatar answered Oct 29 '25 02:10

Yakov Galka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!