c++ - ffmpeg sws_scale 得到了从 YUV420P 到 RGB24 的失真图像

标签 c++ ffmpeg

尝试使用 将 YUV420p 转换为 RGB24 时,图像失真 sws_scale.

代码:

ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
if (ret < 0) {
    fprintf(stderr, "Error decoding video frame\n");
    return ret;
}
if (*got_frame) 
{
    printf("video_frame%s n:%d coded_n:%d pts:%s\n",
               cached ? "(cached)" : "",
               video_frame_count++, frame->coded_picture_number,
               "#"/*av_ts2timestr(frame->pts, &video_dec_ctx->time_base)*/);
    /* copy decoded frame to destination buffer:
     * this is required since rawvideo expects non aligned data */
    av_image_copy(video_dst_data, video_dst_linesize,
                  (const uint8_t **)(frame->data), frame->linesize,
                  video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
    /* write to rawvideo file */
    fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);

    AVPicture pic;
    avpicture_alloc( &pic, AV_PIX_FMT_RGB24, frame->width, frame->height);
    SwsContext *ctxt = sws_getContext(frame->width, frame->height, static_cast<AVPixelFormat>(frame->format),
        frame->width, frame->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
    if ( NULL == ctxt )
    {
       //Log("failed to get sws context");
    }

    if ( 0 < sws_scale(ctxt, frame->data, frame->linesize, 0, frame->height, pic.data, pic.linesize))
    {
        char szPic[256] = { 0 };
        sprintf( szPic, "decoded/%d.bmp", video_frame_count );
        FILE *pf = fopen(szPic,"w");
        if ( NULL != pf )
        {
            BITMAPFILEHEADER bmpFileHeader = {0};
            bmpFileHeader.bfReserved1 = 0;
            bmpFileHeader.bfReserved2 = 0;
            bmpFileHeader.bfType = 0x4D42;
            bmpFileHeader.bfSize = sizeof(bmpFileHeader) + sizeof(BITMAPINFOHEADER) + pic.linesize[0] * frame->height;
            bmpFileHeader.bfOffBits = sizeof(bmpFileHeader) + sizeof(BITMAPINFOHEADER);
            BITMAPINFOHEADER bmiHeader = { 0 };
            bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            bmiHeader.biWidth = frame->width;
            bmiHeader.biHeight = 0 - frame->height;
            bmiHeader.biPlanes = 1;
            bmiHeader.biBitCount = 24;
            bmiHeader.biCompression = BI_RGB;
            bmiHeader.biSizeImage = pic.linesize[0] * frame->height;
            bmiHeader.biXPelsPerMeter = 0;
            bmiHeader.biYPelsPerMeter = 0;
            bmiHeader.biClrUsed = 0;
            bmiHeader.biClrImportant = 0;
            fwrite( &bmpFileHeader, 1, sizeof(bmpFileHeader), pf );
            fwrite( &bmiHeader, 1, sizeof(bmiHeader), pf );
            fwrite( pic.data[0], 1, pic.linesize[0] * frame->height, pf );
            fclose( pf );
        }
    }
    // pic.data[0] now contains the image data in RGB format (3 bytes)
    // and pic.linesize[0] is the pitch of the data (ie. size of a row in memory, which can be larger than width*sizeof(pixel))


    avpicture_free(&pic);
    sws_freeContext(ctxt);
}

上面只解码帧,然后将其转换为 RGB24,然后写入位图。 像这样的原始视频帧, original video 但转换后的图像, converted image 是否缺少某些代码或某些代码错误?

提前致谢。

最佳答案

fwrite( pic.data[0], 1, pic.linesize[0] * frame->height, pf );

对于例如的图像1280x720,线条尺寸通常较大,例如1312,所以如果你写 linesize*height,你将写入比图像大小更多的数据。你想写(在一个循环中)由 linesize 字节偏移的宽度像素:

uint8_t *ptr = pic.data[0];
for (int y = 0; y < frame->height; y++) {
    fwrite(ptr, 1, frame->width, pf);
    ptr += pic.linesize[0];
}

然后它应该可以正常工作。

关于c++ - ffmpeg sws_scale 得到了从 YUV420P 到 RGB24 的失真图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20186502/

相关文章:

c++ - 类型转换 boost::fusion::vector

Android记录屏幕并流式传输

FFMPEG:如何只保留图像的上半部分?

c++ - C++ 中不寻常的 typedef 使用

c++ - PyImport_AppendInittab 怎么会失败?

c++ - C++ 中的 Visual Studio 调试错误

c++ - 将 QString 日期(RFC 822 格式)转换为另一种基于文化的 QString 格式

c++ - FFMPEG TS空包传输

audio - 如何使用FFmpeg连接多个具有不同流顺序的视频文件?

ffmpeg - Python ffmpeg 在缩放文件大小方面的差异