织梦CMS - 轻松建站从此开始!

罗索

YUV420SP to YUV420P

jackyhwei 发布于 2010-06-29 18:15 点击:次 
最近做TI DM365的solution, 发现出来的画面有抖动,于是就想抓RAW data出来看看是sensor有问题还是encode出了问题
TAG:

最近做TI DM365的solution, 发现出来的画面有抖动,于是就想抓RAW data出来看看是sensor有问题还是encode出了问题,sensor出来的数据是RGB的,通过Previewer后就变成了YUV420SP, 抓了RAW data出来之后用YUVtools一看,整个画面都是灰色的,感觉只有Y值work了,UV都没了,于是又去网上download其他的YUV tools, 结果还是一样,后来发现一般的YUV tools只支持普通的YUV420格式,即YUV420P, 于是去找解析YUV420SP的tool, 发现还不太好找,于是就自己写了一个转换工具,将YUV420SP的转成YUV420P的格式,转了之后用YUV tools 一看,颜色全对了。

下面就是源码:
/*
YYYYYYYYYYYYY                                YYYYYYYYYYYYY
YYYYYYYYYYYYY       -->                      YYYYYYYYYYYYY
YYYYYYYYYYYYY                                YYYYYYYYYYYYY
YYYYYYYYYYYYY                                YYYYYYYYYYYYY
CbCrCbCr                                     CbCbCbCb
CbCrCbCr                                     CrCrCrCr

YYYYYYYY...      -->   YYYYYYYY...
CbCrCbCr....     -->   CbCb.....  CrCr......
*/

 

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <errno.h> 
  4. #include <sys/stat.h> 
  5.  
  6. int main(int argc, char* argv[]) 
  7.  int i; 
  8.  int idx; 
  9.  int width; 
  10.  int height; 
  11.  int pixels; 
  12.  int frame_no; 
  13.  FILE *inputfp = NULL; 
  14.  FILE *outputfp = NULL; 
  15.  unsigned char *pBuffer; 
  16.  struct stat state; 
  17.  
  18.  if (argc != 5) 
  19.  { 
  20.   fprintf(stderr, "Usage : %s <input file name> \
  21. <output file name> <width> <height>\n",argv[0]); 
  22.   return -1; 
  23.  } 
  24.  
  25.  width = atoi(argv[3]); 
  26.  height = atoi(argv[4]); 
  27.  
  28.  if(width <= 0 || height <= 0) 
  29.  { 
  30.   fprintf(stderr, "parameter error [width = %d, height=%d]\n",width, height); 
  31.   return -1; 
  32.  } 
  33.  
  34.  if (stat(argv[1], &state) < 0) 
  35.  { 
  36.   fprintf(stderr, "Faile to stat %s\n",argv[1]); 
  37.   return -1; 
  38.  } 
  39.  frame_no = (state.st_size/((width*height/2)*3)); 
  40.  
  41.  inputfp = fopen(argv[1], "rb"); 
  42.  if (!inputfp) 
  43.  { 
  44.   fprintf(stderr, "fopen failed for input file[%s]\n",argv[1]); 
  45.   return -1; 
  46.  } 
  47.  
  48.  outputfp = fopen(argv[2], "wb"); 
  49.  if (!outputfp) 
  50.  { 
  51.   fprintf(stderr, "fopen failed for output file[%s]\n",argv[2]); 
  52.   return -1; 
  53.  } 
  54.  
  55.  pixels = width * height; 
  56.  pBuffer = (unsigned char *)malloc(pixels); 
  57.  
  58.  for (i=0; i<frame_no; i++) 
  59.  { 
  60.   // Read Y 
  61.   fread (pBuffer, pixels, sizeof(unsigned char), inputfp); 
  62.  
  63.   // Write Y 
  64.   fwrite(pBuffer, pixels, sizeof(unsigned char), outputfp); 
  65.  
  66.   // Read Cb Cr 
  67.   fread (pBuffer, (pixels/2), sizeof(unsigned char), inputfp); 
  68.  
  69.   // Write Cb 
  70.   for(idx = 0; idx <  (pixels/2); idx+=2) 
  71.   { 
  72.    fwrite((pBuffer + idx), 1, sizeof(unsigned char), outputfp); 
  73.   } 
  74.  
  75.   // Write Cr 
  76.   for(idx = 1; idx <  (pixels/2); idx+=2) 
  77.   { 
  78.    fwrite((pBuffer + idx), 1, sizeof(unsigned char), outputfp); 
  79.   } 
  80.  } 
  81.   
  82.  free(pBuffer); 
  83.  fclose(inputfp); 
  84.  fclose(outputfp); 
  85.  return 0; 

 

(leesphone)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201006/9752.html]
本文出处:CSDN博客 作者:leesphone
顶一下
(10)
100%
踩一下
(0)
0%
------分隔线----------------------------
相关文章
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容