dsputil.c
上传用户:jxp0626
上传日期:2007-01-08
资源大小:102k
文件大小:11k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Unix_Linux

  1. /*
  2.  * DSP utils
  3.  * Copyright (c) 2000 Gerard Lantau.
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "mpegvideo.h"
  24. #define avg2(a,b) ((a+b+1)>>1)
  25. #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
  26. static UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
  27. UINT32 squareTbl[512];
  28. void dsputil_init(void)
  29. {
  30.     int i;
  31.     for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
  32.     for(i=0;i<MAX_NEG_CROP;i++) {
  33.         cropTbl[i] = 0;
  34.         cropTbl[i + MAX_NEG_CROP + 256] = 255;
  35.     }
  36.     for(i=0;i<512;i++) {
  37.         squareTbl[i] = (i - 256) * (i - 256);
  38.     }
  39. }
  40. void get_pixels(DCTELEM *block, const UINT8 *pixels, int line_size)
  41. {
  42.     DCTELEM *p;
  43.     const UINT8 *pix;
  44.     int i;
  45.     /* read the pixels */
  46.     p = block;
  47.     pix = pixels;
  48.     for(i=0;i<8;i++) {
  49.         p[0] = pix[0];
  50.         p[1] = pix[1];
  51.         p[2] = pix[2];
  52.         p[3] = pix[3];
  53.         p[4] = pix[4];
  54.         p[5] = pix[5];
  55.         p[6] = pix[6];
  56.         p[7] = pix[7];
  57.         pix += line_size;
  58.         p += 8;
  59.     }
  60. }
  61. void put_pixels(const DCTELEM *block, UINT8 *pixels, int line_size)
  62. {
  63.     const DCTELEM *p;
  64.     UINT8 *pix;
  65.     int i;
  66.     UINT8 *cm = cropTbl + MAX_NEG_CROP;
  67.     
  68.     /* read the pixels */
  69.     p = block;
  70.     pix = pixels;
  71.     for(i=0;i<8;i++) {
  72.         pix[0] = cm[p[0]];
  73.         pix[1] = cm[p[1]];
  74.         pix[2] = cm[p[2]];
  75.         pix[3] = cm[p[3]];
  76.         pix[4] = cm[p[4]];
  77.         pix[5] = cm[p[5]];
  78.         pix[6] = cm[p[6]];
  79.         pix[7] = cm[p[7]];
  80.         pix += line_size;
  81.         p += 8;
  82.     }
  83. }
  84. #define PIXOP(OPNAME, OP)                                                                
  85.                                                                                          
  86. static void OPNAME ## _pixels(DCTELEM *block, const UINT8 *pixels, int line_size)        
  87. {                                                                                        
  88.     DCTELEM *p;                                                                          
  89.     const UINT8 *pix;                                                                    
  90.     int i;                                                                               
  91.                                                                                          
  92.     p = block;                                                                           
  93.     pix = pixels;                                                                        
  94.     for(i=0;i<8;i++) {                                                                   
  95.         p[0] OP pix[0];                                                                  
  96.         p[1] OP pix[1];                                                                  
  97.         p[2] OP pix[2];                                                                  
  98.         p[3] OP pix[3];                                                                  
  99.         p[4] OP pix[4];                                                                  
  100.         p[5] OP pix[5];                                                                  
  101.         p[6] OP pix[6];                                                                  
  102.         p[7] OP pix[7];                                                                  
  103.         pix += line_size;                                                                
  104.         p += 8;                                                                          
  105.     }                                                                                    
  106. }                                                                                        
  107.                                                                                          
  108. static void OPNAME ## _pixels_x2(DCTELEM *block, const UINT8 *pixels, int line_size)     
  109. {                                                                                        
  110.     DCTELEM *p;                                                                          
  111.     const UINT8 *pix;                                                                    
  112.     int i;                                                                               
  113.                                                                                          
  114.     p = block;                                                                           
  115.     pix = pixels;                                                                        
  116.     for(i=0;i<8;i++) {                                                                   
  117.         p[0] OP avg2(pix[0], pix[1]);                                                    
  118.         p[1] OP avg2(pix[1], pix[2]);                                                    
  119.         p[2] OP avg2(pix[2], pix[3]);                                                    
  120.         p[3] OP avg2(pix[3], pix[4]);                                                    
  121.         p[4] OP avg2(pix[4], pix[5]);                                                    
  122.         p[5] OP avg2(pix[5], pix[6]);                                                    
  123.         p[6] OP avg2(pix[6], pix[7]);                                                    
  124.         p[7] OP avg2(pix[7], pix[8]);                                                    
  125.         pix += line_size;                                                                
  126.         p += 8;                                                                          
  127.     }                                                                                    
  128. }                                                                                        
  129.                                                                                          
  130. static void OPNAME ## _pixels_y2(DCTELEM *block, const UINT8 *pixels, int line_size)     
  131. {                                                                                        
  132.     DCTELEM *p;                                                                          
  133.     const UINT8 *pix;                                                                    
  134.     const UINT8 *pix1;                                                                   
  135.     int i;                                                                               
  136.                                                                                          
  137.     p = block;                                                                           
  138.     pix = pixels;                                                                        
  139.     pix1 = pixels + line_size;                                                           
  140.     for(i=0;i<8;i++) {                                                                   
  141.         p[0] OP avg2(pix[0], pix1[0]);                                                   
  142.         p[1] OP avg2(pix[1], pix1[1]);                                                   
  143.         p[2] OP avg2(pix[2], pix1[2]);                                                   
  144.         p[3] OP avg2(pix[3], pix1[3]);                                                   
  145.         p[4] OP avg2(pix[4], pix1[4]);                                                   
  146.         p[5] OP avg2(pix[5], pix1[5]);                                                   
  147.         p[6] OP avg2(pix[6], pix1[6]);                                                   
  148.         p[7] OP avg2(pix[7], pix1[7]);                                                   
  149.         pix += line_size;                                                                
  150.         pix1 += line_size;                                                               
  151.         p += 8;                                                                          
  152.     }                                                                                    
  153. }                                                                                        
  154.                                                                                          
  155. static void OPNAME ## _pixels_xy2(DCTELEM *block, const UINT8 *pixels, int line_size)    
  156. {                                                                                        
  157.     DCTELEM *p;                                                                          
  158.     const UINT8 *pix;                                                                    
  159.     const UINT8 *pix1;                                                                   
  160.     int i;                                                                               
  161.                                                                                          
  162.     p = block;                                                                           
  163.     pix = pixels;                                                                        
  164.     pix1 = pixels + line_size;                                                           
  165.     for(i=0;i<8;i++) {                                                                   
  166.         p[0] OP avg4(pix[0], pix[1], pix1[0], pix1[1]);                                  
  167.         p[1] OP avg4(pix[1], pix[2], pix1[1], pix1[2]);                                  
  168.         p[2] OP avg4(pix[2], pix[3], pix1[2], pix1[3]);                                  
  169.         p[3] OP avg4(pix[3], pix[4], pix1[3], pix1[4]);                                  
  170.         p[4] OP avg4(pix[4], pix[5], pix1[4], pix1[5]);                                  
  171.         p[5] OP avg4(pix[5], pix[6], pix1[5], pix1[6]);                                  
  172.         p[6] OP avg4(pix[6], pix[7], pix1[6], pix1[7]);                                  
  173.         p[7] OP avg4(pix[7], pix[8], pix1[7], pix1[8]);                                  
  174.         pix += line_size;                                                                
  175.         pix1 += line_size;                                                               
  176.         p += 8;                                                                          
  177.     }                                                                                    
  178. }                                                                                        
  179.                                                                                          
  180. void (*OPNAME ## _pixels_tab[4])(DCTELEM *block, const UINT8 *pixels, int line_size) = { 
  181.     OPNAME ## _pixels,                                                                   
  182.     OPNAME ## _pixels_x2,                                                                
  183.     OPNAME ## _pixels_y2,                                                                
  184.     OPNAME ## _pixels_xy2,                                                               
  185. };
  186. PIXOP(add, +=)
  187. PIXOP(sub, -=)