栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

QT 使用FFmpeg4将bgra的Qimage转换成YUV422P

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

QT 使用FFmpeg4将bgra的Qimage转换成YUV422P

前言
用FFmpeg4实现一个非常简单的argb转换yuv422p的函数
1.通过QPixmap获取QLabel显示界面并转成image,在传入转换函数
QPixmap pixmap(subtitleLabel->size());
pixmap.fill(Qt::transparent);//设置pixmap背景透明
subtitleLabel->render(&pixmap);
QImage image = pixmap.toImage();
unsigned char *imgBuf = image.bits();
argbToyuv(image.width(),image.height(),imgBuf);
2.创建rgbBuffer并填充image的rgbBuf数据,创建yuvBuffer,创建SwsContext转换上下文,使用sws_scale进行转换
#define HI_ALIGN_UP(x, a)           ( ( ((x) + ((a) - 1) ) / (a) ) * (a) )
int argbToyuv(int width, int height, unsigned char *rgbBuf)
{
    int ret, numBytes = 0;
    uint8_t *src_data[4], *dst_data[4];
    int src_linesize[4], dst_linesize[4];
    int src_w = width, src_h = height, dst_w, dst_h;
    dst_w = HI_ALIGN_UP(src_w, 16);//像素对齐
    dst_h = HI_ALIGN_UP(src_h, 16);
    enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_ARGB, dst_pix_fmt = AV_PIX_FMT_YUV422P;
    SwsContext *sws_ctx = NULL;

	
    numBytes = av_image_get_buffer_size(src_pix_fmt, src_w, src_h, 1);//1920*1080*4
    uint8_t *bgraBuffer = NULL;
    (uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
    av_image_fill_arrays(src_data, src_linesize, bgraBuffer, src_pix_fmt, src_w, src_h, 1);
    src_data[0] = rgbBuf;
    src_data[1] = src_data[0]+(width*height);
    src_data[2] = src_data[1]+(width*2*height);

//    FILE *output_rgb=fopen("/opt/vw30/bin/subtitle_rgb","ab+");
//    fwrite(rgbBuf,(src_w*src_h)*3,1,output_rgb);
//    fclose(output_rgb);

	
    numBytes = av_image_get_buffer_size(dst_pix_fmt, dst_w, dst_h, 1);
    uint8_t *yuvBuffer = NULL;
    yuvBuffer = (uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
    av_image_fill_arrays(dst_data, dst_linesize, yuvBuffer ,dst_pix_fmt, dst_w, dst_h, 1);

    
    sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
                             dst_w, dst_h, dst_pix_fmt,
                             SWS_BILINEAR, NULL, NULL, NULL);
    if (!sws_ctx) {
        fprintf(stderr,
                "Impossible to create scale context for the conversion "
                "fmt:%s s:%dx%d -> fmt:%s s:%dx%dn",
                av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
                av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
        ret = AVERROR(EINVAL);
        goto end;
    }
    
    ret = sws_scale(sws_ctx, src_data, src_linesize, 0, src_h, dst_data, dst_linesize);
    if (ret < 0){
        qDebug()<<"sws_scale error";
        goto end;
    }

end:
    if(yuvBuffer){
        av_free(yuvBuffer);
    }
    if(bgraBuffer){
       av_free(bgraBuffer);
    }
    if(sws_ctx){
       sws_freeContext(sws_ctx);
    }

//    FILE *output=fopen("/opt/vw30/bin/subtitle.yuv","ab+");
//    fwrite(dst_data[0],dst_w*dst_h,1,output);
//    fwrite(dst_data[1],dst_w*dst_h/4,1,output);
//    fwrite(dst_data[2],dst_w*dst_h/4,1,output);
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005108.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号