idct.c
上传用户:aoeyumen
上传日期:2007-01-06
资源大小:3329k
文件大小:6k
源码类别:

DVD

开发平台:

Unix_Linux

  1. /*
  2.  *  idct.c
  3.  *
  4.  *  Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999
  5.  *
  6.  *  Portions of this code are from the MPEG software simulation group
  7.  *  idct implementation. This code will be replaced with a new
  8.  *  implementation soon.
  9.  *
  10.  *  This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
  11.  *
  12.  *  mpeg2dec is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2, or (at your option)
  15.  *  any later version.
  16.  *   
  17.  *  mpeg2dec is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *   
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with GNU Make; see the file COPYING.  If not, write to
  24.  *  the Free Software Foundation, 
  25.  *
  26.  */
  27. /**********************************************************/
  28. /* inverse two dimensional DCT, Chen-Wang algorithm       */
  29. /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984)             */
  30. /* 32-bit integer arithmetic (8 bit coefficients)         */
  31. /* 11 mults, 29 adds per DCT                              */
  32. /*                                      sE, 18.8.91       */
  33. /**********************************************************/
  34. /* coefficients extended to 12 bit for IEEE1180-1990      */
  35. /* compliance                           sE,  2.1.94       */
  36. /**********************************************************/
  37. /* this code assumes >> to be a two's-complement arithmetic */
  38. /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2               */
  39. #include <stdio.h>
  40. #include "config.h"
  41. #include "mpeg2.h"
  42. #include "mpeg2_internal.h"
  43. #include "mb_buffer.h"
  44. #include "idct.h"
  45. #include "idct_mmx.h"
  46. #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
  47. #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
  48. #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
  49. #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
  50. #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
  51. #define W7 565  /* 2048*sqrt(2)*cos(7*pi/16) */
  52. // idct main entry point 
  53. void (*idct)(mb_buffer_t *mb_buffer);
  54. // private prototypes 
  55. static void idct_row(sint_16 *blk);
  56. static void idct_col(sint_16 *blk);
  57. static void idct_c(mb_buffer_t *mb_buffer);
  58. // Clamp to [-256,255]
  59. static sint_16 clip_tbl[1024]; /* clipping table */
  60. static sint_16 *clip;
  61. void 
  62. idct_init(void)
  63. {
  64.   sint_32 i;
  65.   clip = clip_tbl + 512;
  66.   for (i= -512; i< 512; i++)
  67.     clip[i] = (i < -256) ? -256 : ((i > 255) ? 255 : i);
  68. #ifdef __i386__
  69. if(config.flags & MPEG2_MMX_ENABLE)
  70. idct = idct_mmx;
  71. else
  72. #endif
  73. idct = idct_c;
  74. }
  75. /* row (horizontal) IDCT
  76.  *
  77.  *           7                       pi         1
  78.  * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
  79.  *          l=0                      8          2
  80.  *
  81.  * where: c[0]    = 128
  82.  *        c[1..7] = 128*sqrt(2)
  83.  */
  84. static void idct_row(sint_16 *blk)
  85. {
  86.   sint_32 x0, x1, x2, x3, x4, x5, x6, x7, x8;
  87.   x1 = blk[4]<<11; 
  88. x2 = blk[6]; 
  89. x3 = blk[2];
  90.   x4 = blk[1]; 
  91. x5 = blk[7]; 
  92. x6 = blk[5]; 
  93. x7 = blk[3];
  94. #if 0
  95.   /* shortcut */
  96.   if (!(x1 | x2 | x3 | x4 | x5 | x6 | x7 ))
  97.   {
  98.     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
  99.     return;
  100.   }
  101. #endif
  102.   x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
  103.   /* first stage */
  104.   x8 = W7*(x4+x5);
  105.   x4 = x8 + (W1-W7)*x4;
  106.   x5 = x8 - (W1+W7)*x5;
  107.   x8 = W3*(x6+x7);
  108.   x6 = x8 - (W3-W5)*x6;
  109.   x7 = x8 - (W3+W5)*x7;
  110.   
  111.   /* second stage */
  112.   x8 = x0 + x1;
  113.   x0 -= x1;
  114.   x1 = W6*(x3+x2);
  115.   x2 = x1 - (W2+W6)*x2;
  116.   x3 = x1 + (W2-W6)*x3;
  117.   x1 = x4 + x6;
  118.   x4 -= x6;
  119.   x6 = x5 + x7;
  120.   x5 -= x7;
  121.   
  122.   /* third stage */
  123.   x7 = x8 + x3;
  124.   x8 -= x3;
  125.   x3 = x0 + x2;
  126.   x0 -= x2;
  127.   x2 = (181*(x4+x5)+128)>>8;
  128.   x4 = (181*(x4-x5)+128)>>8;
  129.   
  130.   /* fourth stage */
  131.   blk[0] = (x7+x1)>>8;
  132.   blk[1] = (x3+x2)>>8;
  133.   blk[2] = (x0+x4)>>8;
  134.   blk[3] = (x8+x6)>>8;
  135.   blk[4] = (x8-x6)>>8;
  136.   blk[5] = (x0-x4)>>8;
  137.   blk[6] = (x3-x2)>>8;
  138.   blk[7] = (x7-x1)>>8;
  139. }
  140. /* column (vertical) IDCT
  141.  *
  142.  *             7                         pi         1
  143.  * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
  144.  *            l=0                        8          2
  145.  *
  146.  * where: c[0]    = 1/1024
  147.  *        c[1..7] = (1/1024)*sqrt(2)
  148.  */
  149. /* FIXME something odd is going on with inlining this 
  150.  * procedure. Things break if it isn't inlined */
  151. static void idct_col(sint_16 *blk)
  152. {
  153.   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  154.   /* shortcut */
  155.   x1 = (blk[8*4]<<8); 
  156. x2 = blk[8*6]; 
  157. x3 = blk[8*2];
  158.   x4 = blk[8*1];
  159. x5 = blk[8*7]; 
  160. x6 = blk[8*5];
  161. x7 = blk[8*3];
  162. #if 0
  163.   if (!(x1  | x2 | x3 | x4 | x5 | x6 | x7 ))
  164.   {
  165.     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
  166.       clip[(blk[8*0]+32)>>6];
  167.     return;
  168.   }
  169. #endif
  170.   x0 = (blk[8*0]<<8) + 8192;
  171.   /* first stage */
  172.   x8 = W7*(x4+x5) + 4;
  173.   x4 = (x8+(W1-W7)*x4)>>3;
  174.   x5 = (x8-(W1+W7)*x5)>>3;
  175.   x8 = W3*(x6+x7) + 4;
  176.   x6 = (x8-(W3-W5)*x6)>>3;
  177.   x7 = (x8-(W3+W5)*x7)>>3;
  178.   
  179.   /* second stage */
  180.   x8 = x0 + x1;
  181.   x0 -= x1;
  182.   x1 = W6*(x3+x2) + 4;
  183.   x2 = (x1-(W2+W6)*x2)>>3;
  184.   x3 = (x1+(W2-W6)*x3)>>3;
  185.   x1 = x4 + x6;
  186.   x4 -= x6;
  187.   x6 = x5 + x7;
  188.   x5 -= x7;
  189.   
  190.   /* third stage */
  191.   x7 = x8 + x3;
  192.   x8 -= x3;
  193.   x3 = x0 + x2;
  194.   x0 -= x2;
  195.   x2 = (181*(x4+x5)+128)>>8;
  196.   x4 = (181*(x4-x5)+128)>>8;
  197.   
  198.   /* fourth stage */
  199.   blk[8*0] = clip[(x7+x1)>>14];
  200.   blk[8*1] = clip[(x3+x2)>>14];
  201.   blk[8*2] = clip[(x0+x4)>>14];
  202.   blk[8*3] = clip[(x8+x6)>>14];
  203.   blk[8*4] = clip[(x8-x6)>>14];
  204.   blk[8*5] = clip[(x0-x4)>>14];
  205.   blk[8*6] = clip[(x3-x2)>>14];
  206.   blk[8*7] = clip[(x7-x1)>>14];
  207. }
  208.  
  209. void
  210. idct_c(mb_buffer_t *mb_buffer)
  211. {
  212. uint_32 i,j,k;
  213. sint_16 *blk;
  214. macroblock_t *mb = mb_buffer->macroblocks;
  215. uint_32 num_blocks = mb_buffer->num_blocks;
  216. for(k=0;k<num_blocks;k++)
  217. {
  218. if(mb[k].skipped)
  219. continue;
  220. //XXX only 4:2:0 supported here
  221. for(i=0;i<4;i++)
  222. {
  223. blk = mb[k].y_blocks + 64*i; 
  224. if(mb[k].coded_block_pattern & (0x20 >> i))
  225. {
  226. for (j=0; j<8; j++)
  227. idct_row(blk + 8*j);
  228. for (j=0; j<8; j++)
  229. idct_col(blk + j);
  230. }
  231. }
  232. if(mb[k].coded_block_pattern & 0x2)
  233. {
  234. blk = mb[k].cr_blocks; 
  235. for (j=0; j<8; j++)
  236. idct_row(blk + 8*j);
  237. for (j=0; j<8; j++)
  238. idct_col(blk + j);
  239. }
  240. if(mb[k].coded_block_pattern & 0x1)
  241. {
  242. blk = mb[k].cb_blocks; 
  243. for (j=0; j<8; j++)
  244. idct_row(blk + 8*j);
  245. for (j=0; j<8; j++)
  246. idct_col(blk + j);
  247. }
  248. }
  249. }