Search code examples
ffmpeglibavlibavcodec

Allocate AVFrame for sws_scale()


Trying to write program which uses libav to extract raw pixel data (~BMP) from arbitrary video. Everything goes well except sws_scale() failing to convert AVFrame to RGB24.

I formulated minimal example of it where AVFrame is being created and initialized with 4 different methods found on internet: https://github.com/SlavMFM/libav_bmp_example - all of them fail in different ways. How can I fix it so sws_scale() does the convertion?


Solution

  • First, don't use avcodec_decode_video2. Use avcodec_send_packet and avcodec_receive_frame

    second, Don't call av_frame_get_buffer on source Just allocate it with av_frame_alloc, avcodec_receive_frame will set up the rest

    Then allocate a destination frame frame like:

    AVFrame* frame = av_frame_alloc();
    frame->format = whatever;
    frame->width = w;
    frame->height = h;
    av_frame_get_buffer(frame, 32);