简体   繁体   中英

Extracting video frame image with libavformat and libswscale gives flipped image

I am using following code to extract image frames outputted by avcodec_decode_video2() into BMP file:

// Convert the image from its native format to RGB
int height = sws_scale(state.sws_ctx, 
             (uint8_t const * const *)state.frame->data,
             state.frame->linesize, 
             0, 
             state.video_codec_ctx->height,
             state.picture.data, 
             state.picture.linesize);

// ..... here BMP header initialization goes ...

// Extract pixels
{
    int y = 0;
    size_t frame_line_size = state.picture.linesize[0];
    uint8_t* bmp_data = &bmp->data[0];
    uint8_t* picture_data = state.picture.data[0];
    while(y < height)
    {
        memcpy(bmp_data, picture_data, image_line_size);
        bmp_data += image_line_size_with_padding;
        picture_data += frame_line_size;
        ++y;
    }
}

But it results flipped image:

生成的BMP图像

Can anyone please help me to understand, what I am doing wrong here?

Video stream information:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'testdata/video3.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    title           : video3.mp4
    encoder         : Lavf52.78.3
  Duration: 00:00:07.93, start: 0.000000, bitrate: 1467 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 640x320 [PAR 1:1 DAR 2:1], 1331 kb/s, 29.73 fps, 29.73 tbr, 29734 tbn, 59.47 tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
    Stream #0.1(und): Audio: aac, 48000 Hz, stereo, fltp, 127 kb/s
    Metadata:
      creation_time   : 1970-01-01 00:00:00
Video file 'testdata/video3.mp4' has 236 frames.

I have alternate code that uses libopencv, and it gives correct image from the same video file, but I need to reach the correct effect using directly ffmpeg libs.

通过libopencv从同一视频文件建立的正确图像

So here is the working code:

// Extract pixels
{
    int y = 0;
    size_t frame_line_size = state.picture.linesize[0];
    uint8_t* bmp_data = &bmp->data[0] + (image_line_size_with_padding * (height - 1));
    uint8_t* picture_data = state.picture.data[0];
    while(y < height)
    {
        memcpy(bmp_data, picture_data, image_line_size);
        bmp_data -= image_line_size_with_padding;
        picture_data += frame_line_size;
        ++y;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM