颜色转换rgb24 to yuv420

  1. void CVideoEncoder::RGB24ToYUV420(int Width,int Height,uint8_t* RgbBuffer,uint8_t*YuvBuffer)  
  2. {   
  3.     uint8_t* yuvBuf=YuvBuffer;//YUV空间  
  4.     int nWidth=Width;  
  5.     int nHeight=Height;  
  6.     /下面转换算法是网上查到的  
  7.     int i, j;   
  8.     uint8_t*bufY = yuvBuf;   
  9.     uint8_t*bufU = yuvBuf + nWidth * nHeight;   
  10.     uint8_t*bufV = bufU + (nWidth* nHeight* 1/4);   
  11.     uint8_t*Y=bufY;  
  12.     uint8_t*U=bufU;  
  13.     uint8_t*V=bufV;  
  14.     uint8_t*bufRGB;  
  15.     unsigned char y, u, v, r, g, b,testu,testv;  
  16.     if (NULL==pRGBBuffer)  
  17.     {  
  18.        return ;  
  19.     }  
  20.     for (j = 0; j<nHeight;j++)  
  21.     {  
  22.         bufRGB = RgbBuffer + nWidth * (nHeight - 1-j) * 3 ;   
  23.         for (i = 0;i<nWidth;i++)  
  24.         {  
  25.             int pos = nWidth * i + j;  
  26.             r= *(bufRGB++);  
  27.             g = *(bufRGB++);  
  28.             b = *(bufRGB++);  
  29.             y =(unsigned char)(( 66 * r + 129 * g +  25 * b + 128) >>8) + 16;//16  
  30.             v = (unsigned char)((-38 * r -  74 * g + 112 * b + 128) >>8) +128  ; //128            
  31.             u = (unsigned char)((112 * r -  94 * g -  18 * b + 128) >> 8) + 128 ;  
  32.             *(bufY++)=max(0,min(y, 255 ));  
  33.   
  34.             if (j%2==0&&i%2 ==0)  
  35.             {  
  36.                 if (u>255)  
  37.                 {  
  38.                     u=255;  
  39.                 }  
  40.                 if (u<0)  
  41.                 {  
  42.                     u = 0;  
  43.                 }  
  44.                 *(bufU++) =u;  
  45.                 //存u分量  
  46.             }  
  47.             else  
  48.             {  
  49.                 //存v分量  
  50.                 if (i%2==0)  
  51.                 {  
  52.                     if (v>255)  
  53.                     {  
  54.                         v = 255;  
  55.                     }  
  56.                     if (v<0)  
  57.                     {  
  58.                         v = 0;  
  59.                     }  
  60.                     *(bufV++) =v;  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //YUV对应到data  
  2.             pVideoEncoder->m_pYUVFrame->data[0]=pVideoEncoder->yuv_buff;  
  3.             pVideoEncoder->m_pYUVFrame->data[1]=pVideoEncoder->yuv_buff+y_size;  
  4.             pVideoEncoder->m_pYUVFrame->data[2]=pVideoEncoder->yuv_buff+(y_size*5)/4;   


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //第二种方法 用FFMPEG  
  2. uint8_t *rgb_src[3]= {rgb, NULL, NULL};  
  3. int rgb_stride[3]={3*biWidth, 0, 0};  
  4. AVFrame picture;  
  5. avpicture_alloc((AVPicture*)(&picture), AV_PIX_FMT_YUV420P,biWidth,biHeight);  
  6. SwsContext *yuvContext=sws_getContext(biWidth,biHeight,AV_PIX_FMT_BGR24,biWidth,  
  7. biHeight,AV_PIX_FMT_YUV420P,SWS_BICUBIC, NULL, NULL, NULL);  
  8. sws_scale(yuvContext,rgb_src, rgb_stride, 0, biHeight, picture.data, picture.linesize);  

//注意 如果转换后出现倒立,请安下面方式更正

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. picture.data[0] += picture.linesize[0] * (biHeight - 1);  
  2. picture.linesize[0] *= -1;                        
  3. picture.data[1] += picture.linesize[1] * (biHeight / 2 - 1);  
  4. picture.linesize[1] *= -1;  
  5. picture.data[2] += picture.linesize[2] * (biHeight/ 2 - 1);  
  6. picture.linesize[2] *= -1;   
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值