使用libavformat和libswscale提取视频帧图像可以提供翻转图像

时间:2015-11-18 21:20:13

标签: c ffmpeg libavcodec libavformat

我使用以下代码将avcodec_decode_video2()输出的图像帧提取到BMP文件中:

// 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;
    }
}

但结果是翻转图像:

Resulting BMP image

有谁能帮助我理解,我在这里做错了什么?

视频流信息:

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.

我有使用libopencv的替代代码,它从同一个视频文件中提供正确的图像,但我需要直接使用ffmpeg libs达到正确的效果。

Correct image established from the same video file via libopencv

1 个答案:

答案 0 :(得分:2)

所以这是工作代码:

// 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;
    }
}