图像处理之基础---2个YUV视频 拼接技术

  1. /************************************************* 
  2. * 主要功能:两路 YUV4:2:0拼接一路左右半宽格式YUV视频 
  3. 参考资料:http://www.pudn.com/downloads271/sourcecode/multimedia/vfw/detail1237363.html 
  4. U_size=V_size=Y_size/2 
  5. *************************************************/  
  6. #include<stdio.h>  
  7. #include<stdlib.h>  
  8. #include<string.h>  
  9. #define PREWEIGHT 1920  
  10. #define PREHEIGHT 1080  
  11. #define RESWEIGHT 3840  
  12. #define RESHEIGHT 1080  
  13. #define PREYSIZE ((PREWEIGHT)*(PREHEIGHT))  
  14. #define PREUSIZE ((PREWEIGHT/2)*(PREHEIGHT/2))  
  15. #define PREVSIZE ((PREWEIGHT/2)*(PREHEIGHT/2))  
  16. #define RESYSIZE ((RESWEIGHT)*(RESHEIGHT))  
  17. #define RESUSIZE ((RESWEIGHT/2)*(RESHEIGHT/2))  
  18. #define RESVSIZE ((RESWEIGHT/2)*(RESHEIGHT/2))  
  19. #define PRESIZE ((PREYSIZE)+(PREUSIZE)+(PREVSIZE))  
  20. #define RESSIZE ((RESYSIZE)+(RESUSIZE)+(RESVSIZE))  
  21.   
  22. int GetFrameNum(const char *File)  
  23. {  
  24.     FILE *fp;  
  25.     int size=0;  
  26.     if (!(fp=fopen(File,"rb")))  
  27.     {  
  28.         printf("Open %s error !",File);  
  29.         exit(1);  
  30.     }   
  31.     else  
  32.     {  
  33.         fseek(fp,0,SEEK_END);/*将文件指针移到YUV文件的末尾*/    
  34.         size=ftell(fp);/*计算文件的总大小*/  
  35.     }  
  36.     return (size/PRESIZE);  
  37. }  
  38. void ReadYUV(char *ResBuf,char *PreBuf,int resstart,int prestart,int resoffset,int preoffset,int size,int height)  
  39. {  
  40.     int k;  
  41.     for (k=0;k<height;k++)  
  42.     {  
  43.         memmove(ResBuf+resstart+k*(resoffset),PreBuf+prestart+k*(preoffset),size);//注意这里用memmov不用strncpy  
  44.     }  
  45. }  
  46. int main(int argc,char *argv[])  
  47. {  
  48.     const char *FileName[]={"e:\BMX_L_1920x1080_240frms.yuv","e:\BMX_R_1920x1080_240frms.yuv"};/*两路YUV文件名*/  
  49.     FILE *FileResult;/*输出文件名*/  
  50.     FILE** fp_combine=(FILE**)malloc(sizeof(FILE *)*3);/*申请文件指针*/  
  51.     int *FileFrameNum=(int *)malloc(sizeof(int)*3);/*每个YUV的帧数*/  
  52.     char *PreBuf=(char *)malloc(sizeof(char)*(PRESIZE+1));/*处理前每一帧图像的大小*/  
  53.     char *ResBuf=(char*)malloc(sizeof(char)*(RESSIZE+1));/*处理后每一帧图像的大小*/  
  54.     int Y_start_section=0;/*预处理图片Y分量存入目标区域的起始区域*/  
  55.     int U_start_section = 0;/*预处理图片U分量存入目标区域的起始区域*/  
  56.     int V_start_section = 0;/*预处理图片V分量存入目标区域的起始区域*/  
  57.     int File_offset = 0;/*预处理文件偏移值*/      
  58.     int i_combine=0,j_combine=0,k_combine=0;/*控制循环*/  
  59.     /*判断申请内存是否成功*/  
  60.     if (!((fp_combine)&&(FileFrameNum)&&(PreBuf)&&(ResBuf)))  
  61.     {  
  62.         printf("Allocate memeroy Faile !");  
  63.         exit(1);  
  64.     }   
  65.     /*初始化申请空间*/  
  66.     memset(fp_combine,0,sizeof(FILE *)*2);  
  67.     memset(FileFrameNum,0,sizeof(int)*2);  
  68.     memset(PreBuf,0,sizeof(char)*PRESIZE);  
  69.     memset(ResBuf,0,sizeof(char)*RESSIZE);  
  70.     if (!(FileResult=fopen("hua_result.YUV","wb")))/*创建输出文件*/  
  71.     {  
  72.         printf("Creat File faile !");  
  73.         exit(1);  
  74.     }   
  75.     for (i_combine=0;i_combine<2;i_combine++)  
  76.     {  
  77.         if(!(fp_combine[i_combine]=fopen(FileName[i_combine],"rb")))/*打开输入文件*/  
  78.         {  
  79.             printf("Open File %s Faile !",FileName[i_combine]);  
  80.             exit(1);  
  81.         }  
  82.         else  
  83.         {  
  84.             FileFrameNum[i_combine]=GetFrameNum(FileName[i_combine]);/*存储每一个视频的帧数*/  
  85.         }  
  86.     }  
  87.     i_combine=0;  
  88.     k_combine=FileFrameNum[i_combine];  
  89.     while (i_combine<k_combine)  
  90.     {  
  91.         File_offset = i_combine*PRESIZE;  
  92.         j_combine=0;  
  93.         while (j_combine<2)  
  94.         {  
  95.             fseek(fp_combine[j_combine],File_offset,SEEK_SET);/*移动文件指针至需要处理的数据的位置*/  
  96.             fread(PreBuf,1,PRESIZE,fp_combine[j_combine]);/*读取一幅图像*/  
  97.             if (j_combine==0)  
  98.             {  
  99.                 /*把读取预处理图片Y/U/V分量的起始位置放置目标对应位置*/  
  100.                 Y_start_section=0;  
  101.                 U_start_section=RESYSIZE;  
  102.                 V_start_section=RESYSIZE+RESUSIZE;  
  103.             }   
  104.             else  
  105.             {  
  106.                 /*把读取预处理图片Y/U/V分量的起始位置放置目标对应位置*/  
  107.                 Y_start_section=PREWEIGHT;  
  108.                 U_start_section=RESYSIZE+PREWEIGHT/2;  
  109.                 V_start_section=RESYSIZE+RESUSIZE+PREWEIGHT/2;  
  110.             }  
  111.             /*分别读Y、U、V*/  
  112.             ReadYUV(ResBuf,PreBuf,Y_start_section,0,                 RESWEIGHT,PREWEIGHT,PREWEIGHT,PREHEIGHT);  
  113.             ReadYUV(ResBuf,PreBuf,U_start_section,PREYSIZE,          RESWEIGHT/2,PREWEIGHT/2,PREWEIGHT/2,PREHEIGHT/2);  
  114.             ReadYUV(ResBuf,PreBuf,V_start_section,PREYSIZE+PREUSIZE, RESWEIGHT/2,PREWEIGHT/2,PREWEIGHT/2,PREHEIGHT/2);  
  115.             j_combine++;  
  116.         }  
  117.         fwrite(ResBuf,1,RESSIZE,FileResult);  
  118.         fflush(FileResult);  
  119.         i_combine++;  
  120.     }  
  121.     fclose(fp_combine[0]);  
  122.     fclose(fp_combine[1]);  
  123.     fclose(FileResult);  
  124.     return 0;  
  125. }  

http://blog.csdn.net/huahuahailang/article/details/9040847

posted @ 2014-08-29 11:57  midu  阅读(927)  评论(0编辑  收藏  举报