Store.cpp
上传用户:szklck
上传日期:2007-01-22
资源大小:925k
文件大小:10k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. // Store.cpp: implementation of the CStore class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "视频编解码器.h"
  6. #include "Store.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. #define OBFRSIZE 4096
  16. static unsigned char obfr[OBFRSIZE];
  17. static unsigned char *optr;
  18. static int outfile;
  19. extern unsigned char *clp;
  20. extern int outtype,horizontal_size,coded_picture_width,coded_picture_height;
  21. extern int quiet,vertical_size;
  22. CStore::CStore()
  23. {
  24. }
  25. CStore::~CStore()
  26. {
  27. }
  28. // store a picture as either one frame or two fields
  29. void CStore::storeframe(unsigned char *src[], int frame)
  30. {
  31.     char outname[8]="D:\测试";
  32.     //outname=
  33. store_one(outname,src,0,coded_picture_width,vertical_size);
  34. }
  35. void CStore::store_one(char *outname, unsigned char *src[], 
  36.        int offset, int incr, int height)
  37. {
  38.  switch (outtype)
  39.   {
  40.   case T_BMP:
  41.     store_bmp(outname,src,offset,incr,height);
  42. break;
  43.   case T_YUV:
  44.    // store_yuv(outname,src,offset,incr,height);
  45.     break;
  46.   case T_YUV_CONC:
  47.    // store_yuv_append(outname,src,offset,incr,height);
  48.     break;
  49.   case T_SIF:
  50.    // store_sif(outname,src,offset,incr,height);
  51.     break;
  52.   case T_TGA:
  53.    // store_ppm_tga(outname,src,offset,incr,height,1);
  54.     break;
  55.   case T_PPM:
  56.   //  store_ppm_tga(outname,src,offset,incr,height,0);
  57.     break;
  58. #ifdef DISPLAY
  59.   case T_X11:
  60.     dither(src);
  61.     break;
  62. #endif
  63.   default:
  64.     break;
  65.   }
  66. }
  67. void CStore::store_bmp(char *outname, unsigned char *src[],  
  68.                int offset, int incr, int height)
  69. {
  70.   
  71. }
  72. /*
  73. //没有文件头的Y U V格式文件
  74. void CStore::store_yuv(char *outname, unsigned char *src[], 
  75.        int offset, int incr, int height)
  76. {
  77.   int hsize;
  78.   char tmpname[32];
  79.   hsize = horizontal_size;
  80.   sprintf(tmpname,"%s.Y",outname);
  81.   store_yuv1(tmpname,src[0],offset,incr,hsize,height,0);
  82.   offset>>=1; incr>>=1; hsize>>=1;
  83.   height>>=1;
  84.   sprintf(tmpname,"%s.U",outname);
  85.   store_yuv1(tmpname,src[1],offset,incr,hsize,height,0);
  86.   sprintf(tmpname,"%s.V",outname);
  87.   store_yuv1(tmpname,src[2],offset,incr,hsize,height,0);
  88. }
  89. //有文件头的YUV格式的文件 
  90. void CStore::store_yuv_append(char *outname, unsigned char *src[], int offset, int incr, int height)
  91. {
  92.   int hsize;
  93.   hsize = horizontal_size;
  94.   store_yuv1(outname,src[0],offset,incr,hsize,height,1);
  95.   offset>>=1; incr>>=1; hsize>>=1;
  96.   height>>=1;
  97.   store_yuv1(outname,src[1],offset,incr,hsize,height,1);
  98.   store_yuv1(outname,src[2],offset,incr,hsize,height,1);
  99. }
  100. void CStore::store_sif(char *outname, unsigned char *src[], int offset, int incr, int height)
  101. {
  102.   int i,j;
  103.   unsigned char *py, *pu, *pv;
  104.   static unsigned char *u422, *v422;
  105.   if (!u422) 
  106.   {
  107.     if (!(u422 = (unsigned char *)malloc((coded_picture_width>>1)
  108.  *coded_picture_height)))
  109. {
  110. AfxMessageBox("malloc failed!");
  111. return;
  112. }
  113.     if (!(v422 = (unsigned char *)malloc((coded_picture_width>>1)
  114.  *coded_picture_height)))
  115. {
  116. AfxMessageBox("malloc failed!");
  117. return;
  118. }
  119.   }
  120.   
  121.   conv420to422(src[1],u422);
  122.   conv420to422(src[2],v422);
  123.   strcat(outname,".SIF");
  124.   if (!quiet)
  125.     fprintf(stderr,"saving %sn",outname);
  126.   if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
  127.   {
  128.       AfxMessageBox("Couldn't create outname!");
  129.       
  130.   }
  131.   optr = obfr;
  132.   for (i=0; i<height; i++)
  133.   {
  134.     py = src[0] + offset + incr*i;
  135.     pu = u422 + (offset>>1) + (incr>>1)*i;
  136.     pv = v422 + (offset>>1) + (incr>>1)*i;
  137.     for (j=0; j<horizontal_size; j+=2)
  138.     {
  139.       putbyte(*pu++);
  140.       putbyte(*py++);
  141.       putbyte(*pv++);
  142.       putbyte(*py++);
  143.     }
  144.   }
  145.   if (optr!=obfr)
  146.     write(outfile,obfr,optr-obfr);
  147.   close(outfile);
  148. }
  149. void CStore::store_ppm_tga(char *outname, unsigned char *src[], int offset, int incr, int height, int tgaflag)
  150. {
  151.   int i, j;
  152.   int y, u, v, r, g, b;
  153.   int crv, cbu, cgu, cgv;
  154.   unsigned char *py, *pu, *pv;
  155.   static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
  156.   char header[32];
  157.   static unsigned char *u422, *v422, *u444, *v444;
  158.   if (!u444) {
  159.     if (!(u422 = (unsigned char *)malloc((coded_picture_width>>1)
  160.  *coded_picture_height)))
  161.       error("malloc failed");
  162.     if (!(v422 = (unsigned char *)malloc((coded_picture_width>>1)
  163.  *coded_picture_height)))
  164.       error("malloc failed");
  165.     if (!(u444 = (unsigned char *)malloc(coded_picture_width
  166.  *coded_picture_height)))
  167.       error("malloc failed");
  168.     if (!(v444 = (unsigned char *)malloc(coded_picture_width
  169.  *coded_picture_height)))
  170.       error("malloc failed");
  171.   }
  172.   conv420to422(src[1],u422);
  173.   conv420to422(src[2],v422);
  174.   conv422to444(u422,u444);
  175.   conv422to444(v422,v444);
  176.   strcat(outname,tgaflag ? ".tga" : ".ppm");
  177.   if (!quiet)
  178.     fprintf(stderr,"saving %sn",outname);
  179.   if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
  180.     {
  181.       sprintf(errortext,"Couldn't create %sn",outname);
  182.       error(errortext);
  183.     }
  184.   optr = obfr;
  185.   if (tgaflag) {
  186.     //* TGA header 
  187.     for (i=0; i<12; i++)
  188.       putbyte(tga24[i]);
  189.     putword(horizontal_size); putword(height);
  190.     putbyte(tga24[12]); putbyte(tga24[13]);
  191.   }
  192.   else {
  193.     // PPM header 
  194.     sprintf(header,"P6n%d %dn255n",horizontal_size,height);
  195.     for (i=0; header[i]!=0; i++)
  196.       putbyte(header[i]);
  197.   }
  198.   //* matrix coefficients 
  199.   crv = convmat[matrix_coefficients][0];
  200.   cbu = convmat[matrix_coefficients][1];
  201.   cgu = convmat[matrix_coefficients][2];
  202.   cgv = convmat[matrix_coefficients][3];
  203.   
  204.   for (i=0; i<height; i++)
  205.   {
  206.     py = src[0] + offset + incr*i;
  207.     pu = u444 + offset + incr*i;
  208.     pv = v444 + offset + incr*i;
  209.     for (j=0; j<horizontal_size; j++)
  210. {
  211.       u = *pu++ - 128;
  212.       v = *pv++ - 128;
  213.       y = 76309 * (*py++ - 16); //* (255/219)*65536 
  214.       r = clp[(y + crv*v + 32768)>>16];
  215.       g = clp[(y - cgu*u - cgv*v + 32768)>>16];
  216.       b = clp[(y + cbu*u + 32786)>>16];
  217.       if (tgaflag) 
  218.   {
  219.      putbyte(b);
  220. putbyte(g); 
  221. putbyte(r);
  222.       }
  223.       else 
  224.   {
  225.     putbyte(r);
  226. putbyte(g);
  227. putbyte(b);
  228.       }
  229.     }
  230.   }
  231.   if (optr!=obfr)
  232.     write(outfile,obfr,optr-obfr);
  233.   close(outfile);
  234. }
  235. void CStore::store_yuv1(char *name, unsigned char *src, int offset, int incr, int width, int height, int append)
  236. {
  237.  int i, j;
  238.   unsigned char *p;
  239.   if (append)
  240.   {
  241.     if ((outfile = open(name,O_APPEND|O_WRONLY|O_BINARY,0666))==-1) 
  242. {
  243.       sprintf(errortext,"Couldn't append to %sn",name);
  244.       error(errortext);
  245.     }
  246.   }
  247.   else 
  248.   {
  249.     if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
  250. {
  251.       AfxMessageBox("Couldn't create file name!");
  252.       error(errortext);
  253.     }
  254.   }
  255.   if (!quiet)
  256.     AfxMessageBox("saving file!");
  257.   optr=obfr;
  258.   for (i=0; i<height; i++) 
  259.   {
  260.     p = src + offset + incr*i;
  261.     for (j=0; j<width; j++)
  262.       putbyte(*p++);
  263.   }
  264.   if (optr!=obfr)
  265.     write(outfile,obfr,optr-obfr);
  266.   close(outfile);
  267. }
  268. */
  269. void CStore::putbyte(int c)
  270. {
  271.   *optr++ = c;
  272.   if (optr == obfr+OBFRSIZE)
  273.   {
  274. //    write(outfile,obfr,OBFRSIZE);
  275.     optr = obfr;
  276.   }
  277. }
  278. void CStore::putword(int w)
  279. {
  280.   putbyte(w); 
  281.   putbyte(w>>8);
  282. }
  283. //  将420格式的文件转化为444的文件
  284. //  1.垂直插值   422
  285. //  2.水平插值
  286. // 水平方向的插值 1:2 interpolation filter 
  287. void CStore::conv422to444(unsigned char *src, unsigned char *dst)
  288. {
  289.  int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
  290.   w = coded_picture_width>>1;
  291.   for (j=0; j<coded_picture_height; j++)
  292.   {
  293.     for (i=0; i<w; i++)
  294. {
  295.       i2 = i<<1;
  296.       im3 = (i<3) ? 0 : i-3;  //左右边界
  297.       im2 = (i<2) ? 0 : i-2;
  298.       im1 = (i<1) ? 0 : i-1;
  299.       ip1 = (i<w-1) ? i+1 : w-1;
  300.       ip2 = (i<w-2) ? i+2 : w-1;
  301.       ip3 = (i<w-3) ? i+3 : w-1;
  302.       // FIR filter coefficients (*256): 5 -21 70 228 -37 11 
  303.       dst[i2] =   clp[(int)(  5*src[im3]
  304.       -21*src[im2]
  305.       +70*src[im1]
  306.       +228*src[i]
  307.       -37*src[ip1]
  308.       +11*src[ip2]+128)>>8];
  309.       dst[i2+1] = clp[(int)(  5*src[ip3]
  310.       -21*src[ip2]
  311.       +70*src[ip1]
  312.       +228*src[i]
  313.       -37*src[im1]
  314.       +11*src[im2]+128)>>8];
  315.     }
  316.     src+= w;
  317.     dst+= coded_picture_width;
  318.   }
  319. }
  320. // 垂直方向的插值 1:2 interpolation filter 
  321. void CStore::conv420to422(unsigned char *src, unsigned char *dst)
  322. {
  323.   int w, h, i, j, j2;
  324.   int jm3, jm2, jm1, jp1, jp2, jp3;
  325.   w = coded_picture_width>>1;
  326.   h = coded_picture_height>>1;
  327.   /* intra frame */
  328.   for (i=0; i<w; i++) 
  329.   {
  330.     for (j=0; j<h; j++) 
  331. {
  332.       j2 = j<<1;//
  333.       jm3 = (j<3) ? 0 : j-3;//边界点
  334.       jm2 = (j<2) ? 0 : j-2;
  335.       jm1 = (j<1) ? 0 : j-1;
  336.       jp1 = (j<h-1) ? j+1 : h-1;//边界点
  337.       jp2 = (j<h-2) ? j+2 : h-1;
  338.       jp3 = (j<h-3) ? j+3 : h-1;
  339.        
  340.       // 进行平滑滤波
  341.   //FIR filter coefficients (*256): 5 -21 70 228 -37 11 
  342.       // New FIR filter coefficients (*256): 3 -16 67 227 -32 7 
  343.       dst[w*j2] =clp[(int)(  3*src[w*jm3]
  344.   -16*src[w*jm2]
  345.   +67*src[w*jm1]
  346.   +227*src[w*j]
  347.   -32*src[w*jp1]
  348.   +7*src[w*jp2]+128)>>8];
  349.       dst[w*(j2+1)] = clp[(int)(  3*src[w*jp3]
  350.   -16*src[w*jp2]
  351.   +67*src[w*jp1]
  352.   +227*src[w*j]
  353.   -32*src[w*jm1]
  354.   +7*src[w*jm2]+128)>>8];
  355.     }
  356.     src++;
  357.     dst++;
  358.   }
  359. }