sws_scale YUV --> RGB 扭曲图像

问题描述 投票:0回答:1

我想将

YUV420P
图像(从
H.264
流接收)转换为
RGB
,同时使用
sws_scale
调整其大小。
原始图像的大小为
480 × 800
。只需转换相同的尺寸就可以了。
但是当我尝试更改尺寸时,我得到了一个扭曲的图像,具有以下模式:

  • 更改为
    481 × 800
    将产生扭曲的黑白图像,看起来像是在中间被切割的
  • 482 × 800
    会更加扭曲
  • 483 × 800
    扭曲但有颜色
  • 484 × 800
    没问题(正确缩放)。

现在遵循这种模式 - 只有当两者之间的差异除以 4 时,缩放才会正常工作。

这是我解码和转换图像的方式的示例代码。所有方法均显示“成功”。

int srcX = 480;
int srcY = 800;
int dstX = 481; // or 482, 483 etc
int dstY = 800;

AVFrame* avFrameYUV = avcodec_alloc_frame();
avpicture_fill((AVPicture *)avFrameYUV, decoded_yuv_frame, PIX_FMT_YUV420P, srcX , srcY);

AVFrame *avFrameRGB = avcodec_alloc_frame();

AVPacket avPacket;
av_init_packet(&avPacket);
avPacket.size = read; // size of raw data
avPacket.data = raw_data; // raw data before decoding to YUV

int frame_decoded = 0;
int decoded_length = avcodec_decode_video2(g_avCodecContext, avFrameYUV, &frame_decoded, &avPacket);
int size = dstX * dstY * 3;

struct SwsContext *img_convert_ctx = sws_getContext(srcX, srcY, SOURCE_FORMAT, dstX, dstY, PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);

avpicture_fill((AVPicture *)avFrameRGB, rgb_frame, PIX_FMT_RGB24, dstX, dstY);
sws_scale(img_convert_ctx, avFrameYUV->data, avFrameYUV->linesize, 0, srcY, avFrameRGB->data, avFrameRGB->linesize);

// draws the resulting frame with windows BitBlt
DrawBitmap(hdc, dstX, dstY, rgb_frame, size);

sws_freeContext(img_convert_ctx); 
c++ ffmpeg yuv libavcodec
1个回答
0
投票

制作位图图像时,图像的宽度必须是 4 的倍数。

所以你必须改变宽度,如 480、484、488、492 ...

这里是更改为4的倍数的方法

#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)

void main()
{
    BITMAPFILEHEADER bmFileHeader;
    BITMAPINFOHEADER bmInfoHeader;

    // load image
    // ...

    // when you use the method, put parameter like this.
    int tempWidth = WIDTHBYTES(width * bmInfoHeader.biBitCount);
}

希望你能解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.