YUV420P的格式以及转换为RGB565的代码(Android摄像头的输出一般为YUV420P)

122 篇文章 0 订阅
92 篇文章 1 订阅

YUV420P的格式

 

static void cvt_420p_to_rgb565(int width,int height,constunsignedchar*src,unsignedshort*dst)
{
  int line, col, linewidth;
  int y, u, v, yy, vr, ug, vg, ub;
  int r, g, b;
  const unsigned char *py,*pu,*pv;

  linewidth = width >> 1;
  py = src;
  pu = py + (width * height);
  pv = pu + (width * height)/ 4;

  y = *py++;
  yy = y << 8;
  u = *pu - 128;
  ug = 88 * u;
  ub = 454 * u;
  v = *pv - 128;
  vg = 183 * v;
  vr = 359 * v;

  for (line = 0;line< height;line++){
    for (col = 0; col < width; col++){
      r = (yy + vr) >> 8;
      g = (yy - ug - vg)>> 8;
      b = (yy + ub ) >> 8;

      if (r < 0) r = 0;
      if (r > 255) r = 255;
      if (g < 0) g = 0;
      if (g > 255) g = 255;
      if (b < 0) b = 0;
      if (b > 255) b = 255;
      *dst++ = (((unsignedshort)r>>3)<<11)|(((unsignedshort)g>>2)<<5)|(((unsignedshort)b>>3)<<0);
 
      y = *py++;
      yy = y << 8;
      if (col & 1) {
    pu++;
    pv++;

    u = *pu - 128;
    ug = 88 * u;
    ub = 454 * u;
    v = *pv - 128;
    vg = 183 * v;
    vr = 359 * v;
      }
    }
    if ((line & 1)== 0){
      pu -= linewidth;
      pv -= linewidth;
    }
  }
}

 

 http://www.elautoctrl.info/wordpress/archives/921

http://blog.csdn.net/dotphoenix/article/details/6431351

 

 

extra:

 

Android手机你应该用little endian:
pSwsContext = sws_getContext(nWidth, nHeight, PIX_FMT_YUV420P, nWidth, nHeight
            , PIX_FMT_RGB565LE, SWS_BICUBIC, NULL, NULL, NULL);

刚好手头有一个例子,就顺便给你一个例子吧:
SwsContext *img_convert_ctx  = sws_getContext(codecContext->width, codecContext->height,

PIX_FMT_YUV420P,
mReqWidth,mReqHeight,
PIX_FMT_RGB565LE,
SWS_BICUBIC, NULL, NULL, NULL);
LOGD("sws_getContext  return =%d",img_convert_ctx);






if(img_convert_ctx){

AVFrame *picture;

avcodec_get_frame_defaults(picture);

picture->data[0] = (uint8_t*)mRGB565Data;

picture->data[1] = 0;

picture->data[2] = 0;

picture->linesize[0] = mReqWidth;

picture->linesize[1] = 0;

picture->linesize[2] = 0;



sws_scale(img_convert_ctx, frame.data, frame.linesize,

0, codecContext->height, picture->data,

picture->linesize);

result = 0;

    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值