home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / dctm.lzh / DCTM / source.lzh / source / uvtempbuffers.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-02-08  |  5.4 KB  |  215 lines

  1. /*************************************************************************
  2.     uvtempbuffer.cpp
  3.  
  4.     03/02/08    Xiaohong
  5. *************************************************************************/
  6. #include "uvtempbuffers.h"
  7. #include "macros.h"
  8. /**********************************************************
  9.     UVTEMPBUFFERS é╠âüâôâoè╓Éöé╠ÉΘî╛
  10. **********************************************************/
  11. UVTEMPBUFFERS::UVTEMPBUFFERS()
  12. {
  13.     pUBuffer = NULL;
  14.     pVBuffer = NULL;
  15.     pUBuffer422 = NULL;
  16.     pVBuffer422 = NULL;
  17.     pTempFilterBuffer = NULL;
  18.     m_nSize = 0;
  19. }
  20.  
  21. UVTEMPBUFFERS::~UVTEMPBUFFERS()
  22. {
  23.     Clear();
  24. }
  25.  
  26. bool UVTEMPBUFFERS::Alloc(int size,bool filter)
  27. {
  28.     Clear();
  29.     if (!(pUBuffer = (unsigned char *)_malloc(size)))
  30.         return false;
  31.  
  32.     if (!(pVBuffer = (unsigned char *)_malloc(size)))
  33.         return false;
  34.  
  35.     if (!(pUBuffer422 = (unsigned char *)_malloc(size)))
  36.         return false;
  37.  
  38.     if (!(pVBuffer422 = (unsigned char *)_malloc(size)))
  39.         return false;
  40.  
  41.     if(filter)
  42.         if (!(pTempFilterBuffer = (unsigned char *)_malloc(size)))
  43.             return false;
  44.  
  45.     m_nSize = size;
  46.  
  47.     return true;
  48. }
  49. void UVTEMPBUFFERS::Clear(void)
  50. {
  51.     if(pUBuffer!=NULL)
  52.     {
  53.         _free(pUBuffer);
  54.     }
  55.     if(pVBuffer!=NULL)
  56.     {
  57.         _free(pVBuffer);
  58.     }
  59.     if(pUBuffer422!=NULL)
  60.     {
  61.         _free(pUBuffer422);
  62.     }
  63.     if(pVBuffer422!=NULL)
  64.     {
  65.         _free(pVBuffer422);
  66.     }
  67.     if(pTempFilterBuffer!=NULL)
  68.     {
  69.         _free(pTempFilterBuffer);
  70.     }
  71.     m_nSize = 0;
  72. }
  73.  
  74. void UVTEMPBUFFERS::conv444to422(const unsigned char* src,
  75.                                  unsigned char* dst,
  76.                                  const PICTURESTRUCT* pPictureStruct,
  77.                                  const SEQUENCEDATA* pSequenceData)
  78. {
  79.     const int width = pSequenceData->stHeader.nWidth;
  80.     const int height = pSequenceData->stHeader.nHeight;
  81.  
  82.     int i, j, im5, im4, im3, im2, im1, ip1, ip2, ip3, ip4, ip5, ip6;
  83.  
  84.  
  85.     for (j=0; j<height; j++)
  86.     {
  87.         for (i=0; i<width; i+=2)
  88.         {
  89.             im5 = (i<5) ? 0 : i-5;
  90.             im4 = (i<4) ? 0 : i-4;
  91.             im3 = (i<3) ? 0 : i-3;
  92.             im2 = (i<2) ? 0 : i-2;
  93.             im1 = (i<1) ? 0 : i-1;
  94.             ip1 = (i<width-1) ? i+1 : width-1;
  95.             ip2 = (i<width-2) ? i+2 : width-1;
  96.             ip3 = (i<width-3) ? i+3 : width-1;
  97.             ip4 = (i<width-4) ? i+4 : width-1;
  98.             ip5 = (i<width-5) ? i+5 : width-1;
  99.             ip6 = (i<width-5) ? i+6 : width-1;
  100.  
  101.             // FIR filter with 0.5 sample interval phase shift
  102.             dst[i>>1] = pPictureStruct->pClippingTable[(int)(228*(src[i]+src[ip1])
  103.                              +70*(src[im1]+src[ip2])
  104.                              -37*(src[im2]+src[ip3])
  105.                              -21*(src[im3]+src[ip4])
  106.                              +11*(src[im4]+src[ip5])
  107.                              + 5*(src[im5]+src[ip6])+256)>>9];
  108.         }
  109.         src+= width;
  110.         dst+= width>>1;
  111.     }
  112. }
  113.  
  114. void UVTEMPBUFFERS::conv422to420(unsigned char* src,
  115.                                  unsigned char* dst,
  116.                                  const PICTURESTRUCT* pPictureStruct,
  117.                                  const SEQUENCEDATA* pSequenceData)
  118. {
  119.     const int width = pSequenceData->stHeader.nWidth;
  120.     const int height = pSequenceData->stHeader.nHeight;
  121.     int w, i, j, jm5, jm4, jm3, jm2, jm1;
  122.     int jp1, jp2, jp3, jp4, jp5, jp6;
  123.     int height_div2 = height >> 1;
  124.  
  125.     w = width>>1;
  126.  
  127.     for (i=0; i<w; i++)
  128.     {
  129.         for (j=0; j<height; j+=2)
  130.         {
  131.             jm5 = (j<5) ? 0 : j-5;
  132.             jm4 = (j<4) ? 0 : j-4;
  133.             jm3 = (j<3) ? 0 : j-3;
  134.             jm2 = (j<2) ? 0 : j-2;
  135.             jm1 = (j<1) ? 0 : j-1;
  136.             jp1 = (j<height-1) ? j+1 : height-1;
  137.             jp2 = (j<height-2) ? j+2 : height-1;
  138.             jp3 = (j<height-3) ? j+3 : height-1;
  139.             jp4 = (j<height-4) ? j+4 : height-1;
  140.             jp5 = (j<height-5) ? j+5 : height-1;
  141.             jp6 = (j<height-5) ? j+6 : height-1;
  142.  
  143.             // FIR filter with 0.5 sample interval phase shift
  144.             dst[w*(j>>1)] = pPictureStruct->pClippingTable[(int)(228*(src[w*j]+src[w*jp1])
  145.                              +70*(src[w*jm1]+src[w*jp2])
  146.                              -37*(src[w*jm2]+src[w*jp3])
  147.                              -21*(src[w*jm3]+src[w*jp4])
  148.                              +11*(src[w*jm4]+src[w*jp5])
  149.                              + 5*(src[w*jm5]+src[w*jp6])+256)>>9];
  150.         }
  151.         src++;
  152.         dst++;
  153.     }
  154. }
  155. void UVTEMPBUFFERS::SoftFilter(unsigned char* frame[],const int width,const int height)
  156. {
  157.     if(pTempFilterBuffer != NULL)
  158.     {
  159.         SoftFilter(pTempFilterBuffer, frame[0], width, height);
  160.         memcpy(frame[0], pTempFilterBuffer, width*height);
  161.  
  162.         SoftFilter(pTempFilterBuffer, frame[1], width/2, height/2);
  163.         memcpy(frame[1], pTempFilterBuffer, width*height/4);
  164.  
  165.         SoftFilter(pTempFilterBuffer, frame[2], width/2, height/2);
  166.         memcpy(frame[2], pTempFilterBuffer, width*height/4);
  167.     }
  168. }
  169. /*---------------------------------------------------------
  170.     âtâBâïâ^é≡é⌐é»é▄é╖
  171. ---------------------------------------------------------*/
  172. void UVTEMPBUFFERS::SoftFilter(unsigned char* dest, unsigned char* src,const int width,const int height)
  173. {
  174.     const int Tolerance  = 10;
  175.     const int Filtersize = 6;
  176.  
  177.     int refval, aktval, upperval, lowerval, numvalues, sum, rowindex;
  178.     int x, y, fx, fy, fy1, fy2, fx1, fx2;
  179.  
  180.     for(y = 0; y < height; y++)
  181.     {
  182.         for(x = 0; x < width; x++)
  183.         {
  184.             refval    = src[x+y*width];
  185.             upperval  = refval + Tolerance;
  186.             lowerval  = refval - Tolerance;
  187.  
  188.             numvalues = 1;
  189.             sum       = refval;
  190.  
  191.             fy1 = MAX(y-Filtersize,   0);
  192.             fy2 = MIN(y+Filtersize+1, height);
  193.  
  194.             for (fy = fy1; fy<fy2; fy++)
  195.             {
  196.                 rowindex = fy*width;
  197.                 fx1      = MAX(x-Filtersize,   0)     + rowindex;
  198.                 fx2      = MIN(x+Filtersize+1, width) + rowindex;
  199.  
  200.                 for (fx = fx1; fx<fx2; fx++)
  201.                 {
  202.                     aktval = src[fx];
  203.                     if ((lowerval-aktval)*(upperval-aktval)<0)
  204.                     {
  205.                         numvalues ++;
  206.                         sum += aktval;
  207.                     }
  208.                 } //for fx
  209.             } //for fy
  210.  
  211.             dest[x+y*width] = sum/numvalues;
  212.         }
  213.     }
  214. }
  215.