使用sws_scale函数实现各种原始颜色格式互转

[原始链接:https://bbs.huaweicloud.com/blogs/315396]

/*
      YUYV转QImage格式
      */
      QImage YUYV422_TO_QImage(uint8_t *yuyv422,int image_width,int image_height)
      {
         uint8_t *out_buffer= nullptr;
          AVFrame *Input_pFrame= nullptr;
          AVFrame *Output_pFrame = nullptr;
         struct SwsContext *img_convert_ctx=nullptr;  //用于解码后的视频格式转换
         /*1. 申请空间*/
          Output_pFrame = av_frame_alloc();  //存放RGB数据的缓冲区
          Input_pFrame = av_frame_alloc();//存放YUV数据的缓冲区
         /*2.设置转码参数*/
          img_convert_ctx=sws_getContext(image_width, image_height,AV_PIX_FMT_YUYV422,
                                         image_width, image_height,AV_PIX_FMT_RGB24,
                                         SWS_BICUBIC, nullptr, nullptr, nullptr);
         /*3. 申请转码需要空间*/
         //获取转码后数据需要的内存空间大小
         int numBytes = avpicture_get_size(AV_PIX_FMT_RGB24,image_width,image_height);
         //申请空间
          out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
         /*4. 设置转码的源数据地址*/
         avpicture_fill((AVPicture *) Output_pFrame, out_buffer, AV_PIX_FMT_RGB24,image_width, image_height);
         avpicture_fill((AVPicture *) Input_pFrame, yuyv422, AV_PIX_FMT_YUYV422,image_width, image_height);
         //转格式
         sws_scale(img_convert_ctx,
           (uint8_t const **) Input_pFrame->data,
           Input_pFrame->linesize, 0, image_height, Output_pFrame->data,
           Output_pFrame->linesize);
         //加载图片数据
         QImage image(out_buffer,image_width,image_height,QImage::Format_RGB888);
         //释放空间
         if(Input_pFrame)av_free(Input_pFrame);
         if(Output_pFrame)av_free(Output_pFrame);
         if(out_buffer) av_free(out_buffer);
         if(img_convert_ctx)sws_freeContext(img_convert_ctx);
         return image.copy();
      }
      /*
      YUYV转YUV420P格式
      */
      void YUYV422_TO_YUV420P(uint8_t *yuyv422,uint8_t *yuv420p,int video_width,int video_height)
      {
          AVFrame *Input_pFrame= nullptr;
          AVFrame *Output_pFrame = nullptr;
         struct SwsContext *img_convert_ctx=nullptr; //用于解码后的格式转换
         /*1. 申请空间*/
          Input_pFrame = av_frame_alloc();
          Output_pFrame = av_frame_alloc();
         /*2.设置转码参数*/
          img_convert_ctx=sws_getContext(video_width, video_height,AV_PIX_FMT_YUYV422, //输入
                                         video_width, video_height,AV_PIX_FMT_YUV420P,   //输出
                                         SWS_BICUBIC, nullptr, nullptr, nullptr);
         /*3. 申请转码需要空间*/
         /*4. 设置转码的源数据地址*/
         avpicture_fill((AVPicture *) Input_pFrame, yuyv422, AV_PIX_FMT_YUYV422,video_width, video_height);
         avpicture_fill((AVPicture *) Output_pFrame, yuv420p, AV_PIX_FMT_YUV420P,video_width, video_height);
         //转格式
         sws_scale(img_convert_ctx,
           (uint8_t const **) Input_pFrame->data,Input_pFrame->linesize,
           0, video_height, Output_pFrame->data,Output_pFrame->linesize);
         //释放空间
         if(Input_pFrame)av_free(Input_pFrame);
         if(Output_pFrame)av_free(Output_pFrame);
         if(img_convert_ctx)sws_freeContext(img_convert_ctx);
      }
      /*
      YUYV422转RGB888格式
      */
      void YUYV422_TO_RGB888(uint8_t *yuyv422,uint8_t *rgb888,int image_width,int image_height)
      {
          AVFrame *Input_pFrame= nullptr;
          AVFrame *Output_pFrame = nullptr;
         struct SwsContext *img_convert_ctx=nullptr;  //用于解码后的视频格式转换
         /*1. 申请空间*/
          Output_pFrame = av_frame_alloc();  //存放RGB数据的缓冲区
          Input_pFrame = av_frame_alloc();//存放YUV数据的缓冲区
         /*2.设置转码参数*/
          img_convert_ctx=sws_getContext(image_width, image_height,AV_PIX_FMT_YUYV422,
                                         image_width, image_height,AV_PIX_FMT_RGB24,
                                         SWS_BICUBIC, nullptr, nullptr, nullptr);
         /*4. 设置转码的源数据地址*/
         avpicture_fill((AVPicture *) Output_pFrame, rgb888, AV_PIX_FMT_RGB24,image_width, image_height);
         avpicture_fill((AVPicture *) Input_pFrame, yuyv422, AV_PIX_FMT_YUYV422,image_width, image_height);
         //转格式
         sws_scale(img_convert_ctx,
           (uint8_t const **) Input_pFrame->data,
           Input_pFrame->linesize, 0, image_height, Output_pFrame->data,
           Output_pFrame->linesize);
         //释放空间
         if(Input_pFrame)av_free(Input_pFrame);
         if(Output_pFrame)av_free(Output_pFrame);
         if(img_convert_ctx)sws_freeContext(img_convert_ctx);
      }
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值