YUY2到YUV420的转换

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <LIMITS.H>

//src_fileName out_fileName src_width src_height frameNo
int main(int argc, char* argv[])
{
char src_fileName[255];
char out_fileName[255];

int src_width ,src_height;
src_width = src_height = 0;

int frameNum = INT_MAX;

int src_size, out_size, tem_size;
src_size = out_size = tem_size = 0;

unsigned char * src_buf, *out_buf, *tem_buf;

FILE* in_file, *out_file;

unsigned char *Y, *U, *V;
unsigned char * Y2, *U2, *V2;

unsigned char * p=NULL;

if(argc>5)
{
   strcpy(src_fileName, argv[1]);
   strcpy(out_fileName, argv[2]);
   src_width = atoi(argv[3]);
   src_height = atoi(argv[4]);
   frameNum = atoi(argv[5]);
}
else
{
   strcpy(src_fileName, argv[1]);
   strcpy(out_fileName, argv[2]);
   src_width = atoi(argv[3]);
   src_height = atoi(argv[4]);
}

src_size = (src_width * src_height) << 1 ;//对于YUY2 4:2:2
src_buf = (unsigned char *)malloc(src_size*sizeof(char));
memset(src_buf, 0, src_size);


tem_size = (src_width * src_height) << 1; ///对于YUV 4:2:2
tem_buf = (unsigned char *)malloc(tem_size*sizeof(char));
memset(tem_buf, 0, tem_size);

out_size = src_width*src_height * 1.5;//对于YUV 4:2:0
out_buf = (unsigned char *)malloc(out_size*sizeof(char));
memset(out_buf, 0, out_size);

in_file = fopen(src_fileName, "rb");
if (!in_file)
    {
        printf("cannot open input file.");
        return 0;
    }

out_file = fopen(out_fileName, "wb");
    if (!out_file)
    {
        printf("cannot write 264 file./n");
        return 0;
    }

while(frameNum>0 && !feof(in_file))
{
   //读出一帧数据
   if (fread(src_buf, src_size, 1, in_file) <= 0)
    printf("cannot read from input file.");
   p = src_buf;
  
   Y = tem_buf;
   U = Y + src_width*src_height;
   V = U + (src_width*src_height>>1);
  
   Y2 = out_buf;
   U2 = Y2 + src_width*src_height;
   V2 = U2 + (src_width*src_height>>2);
  
   /*由打包YUYV变成平板YUV*/
   for(int k=0; k<src_height; ++k)
   {
    for(int j=0; j<(src_width>>1); ++j)
    {    
     Y[j*2] = p[4*j];    
     U[j] = p[4*j+1];
     Y[j*2+1] = p[4*j+2];
     V[j] = p[4*j+3];
    }
    p = p + src_width*2;
   
    Y = Y + src_width;
    U = U + (src_width>>1);
    V = V + (src_width>>1);
   }
  
   //复位
   Y = tem_buf;
   U = Y + src_width*src_height;
   V = U + (src_width*src_height>>1);
  
   for(int l=0; l<src_height/2; ++l)
   {
    memcpy(U2, U, src_width>>1);
    memcpy(V2, V, src_width>>1);
   
    U2 = U2 + (src_width>>1);
    V2 = V2 + (src_width>>1);
   
    U = U + (src_width);
    V = V + (src_width);
   }
  
   memcpy(Y2, Y, src_width*src_height);
  
   fwrite(out_buf, sizeof(char), out_size, out_file);
   printf(".");

   frameNum--;
}

fflush(out_file);

free(src_buf); src_buf=NULL;
free(tem_buf); tem_buf=NULL;
free(out_buf); out_buf=NULL;

fclose(in_file);
fclose(out_file);

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值