<div dir="ltr"><font face="monospace">Trying to read .mkv file file and write it to .bmp, but resulting .bmp is black-and-white and consists of multiple mini-images of what supposed to be written:<br></font><div><font face="monospace"><br>int main()<br>{<br>    av_register_all();<br>    avformat_network_init();<br>    avfilter_register_all();<br><br>    //crashes on -Ofast without =NULL initialization:<br>    AVFormatContext * format = NULL;<br>    if ( avformat_open_input( & format, VIDEO_FILE, NULL, NULL ) != 0 ) {<br>        cerr << "Could not open file " << VIDEO_FILE << endl;<br>        return -1;<br>    }<br><br>    // Retrieve stream information<br>    if ( avformat_find_stream_info( format, NULL ) < 0) {<br>        cerr << "avformat_find_stream_info() failed." << endl;<br>        return -1;<br>    }<br>    av_dump_format( format, 0, VIDEO_FILE, false );<br>    <br>    AVCodec * video_dec = (AVCodec*)1;<br>    AVCodec * audio_dec = (AVCodec*)1;<br>    const auto video_stream_index = av_find_best_stream( format, AVMEDIA_TYPE_VIDEO, -1, -1, & video_dec, 0 );<br>    const auto audio_stream_index = av_find_best_stream( format, AVMEDIA_TYPE_AUDIO, -1, -1, & audio_dec, 0 );<br>    if ( video_stream_index < 0 ) {<br>        cerr << "Failed to find video stream." << endl;<br>        return -1;<br>    }<br>    if ( audio_stream_index < 0 ) {<br>        cerr << "Failed to find audio stream." << endl;<br>        return -1;<br>    }<br><br>    AVCodecParameters * videoParams = format->streams[ video_stream_index ]->codecpar;<br>    cout << "Having " << videoParams->width << " | " << videoParams->height << " video." << endl;<br>    <br>    av_read_play( format );<br><br>    // create decoding context<br>    AVCodecContext * video_ctx = avcodec_alloc_context3( video_dec );<br>    AVCodecContext * audio_ctx = avcodec_alloc_context3( audio_dec );<br>    if ( ! video_ctx || ! audio_ctx ) {<br>        cerr << "Failed to avcodec_alloc_context3()" << endl;<br>        return -1;<br>    }<br>    if ( video_dec->capabilities & AV_CODEC_CAP_TRUNCATED ) video_ctx->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames<br>    <br>    /* For some codecs, such as msmpeg4 and mpeg4, width and height<br>       MUST be initialized there because this information is not<br>       available in the bitstream. */<br>  <br>    avcodec_parameters_to_context( video_ctx, format->streams[ video_stream_index ]->codecpar );<br>    avcodec_parameters_to_context( audio_ctx, format->streams[ audio_stream_index ]->codecpar );<br>    if ( avcodec_open2( video_ctx, video_dec, NULL ) < 0 ) {<br>        cout << "Failed to open video decoder." << endl;<br>        return -1;<br>    }<br>    if ( avcodec_open2( audio_ctx, audio_dec, NULL ) < 0 ) {<br>        cout << "Failed to open audio decoder." << endl;<br>        return -1;<br>    }<br>    <br>    uint8_t* picture_buffer = (uint8_t*) (av_malloc( avpicture_get_size( AV_PIX_FMT_RGB24 , videoParams->width, videoParams->height ) ));<br>    AVFrame* picture = av_frame_alloc();<br>    avpicture_fill( (AVPicture *) picture, picture_buffer, AV_PIX_FMT_RGB24, video_ctx->width, video_ctx->height );<br>    <br>    AVPacket packet;<br>    av_init_packet( & packet );<br>    <br>    int cnt = 0;<br>    while ( av_read_frame( format, & packet ) >= 0 && cnt < 10 ) {<br>        if ( packet.stream_index == video_stream_index ) {<br>            int check;<br>            const auto result = avcodec_decode_video2( video_ctx, picture, & check, & packet );<br>            cout << "Bytes decoded " << result << " check " << check << endl;<br>            <br>            std::string name = "debug/av/";<br>            name += std::to_string( cnt ) + ".bmp";<br>            cout << "Writing frame " << name << " with linesize " << picture->linesize[0] << " ..." << endl;<br>            write_bmp( (uint8_t*) picture->data, videoParams->width, videoParams->height, name.c_str() );<br>            <br>            av_frame_unref( picture );<br>            <br>            ++ cnt;<br>        }<br>        else if ( packet.stream_index == audio_stream_index ) {<br>            cout << "Sound packet" << endl;<br>        }<br>        av_free_packet( & packet );<br>        av_init_packet( & packet );<br></font><div><font face="monospace">    }</font></div><div><font face="monospace">}<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">How can I fix it? Thanks in advance.<br></font></div></div></div>