home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wvis0626.zip / warpvision_20020626.zip / libmpeg2 / idct.c < prev    next >
C/C++ Source or Header  |  2002-06-19  |  8KB  |  289 lines

  1. /*
  2.  * idct.c
  3.  * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  4.  *
  5.  * Portions of this code are from the MPEG software simulation group
  6.  * idct implementation. This code will be replaced with a new
  7.  * implementation soon.
  8.  *
  9.  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
  10.  *
  11.  * mpeg2dec is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * mpeg2dec is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24.  */
  25.  
  26. /**********************************************************/
  27. /* inverse two dimensional DCT, Chen-Wang algorithm */
  28. /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
  29. /* 32-bit integer arithmetic (8 bit coefficients) */
  30. /* 11 mults, 29 adds per DCT */
  31. /* sE, 18.8.91 */
  32. /**********************************************************/
  33. /* coefficients extended to 12 bit for IEEE1180-1990 */
  34. /* compliance sE, 2.1.94 */
  35. /**********************************************************/
  36.  
  37. /* this code assumes >> to be a two's-complement arithmetic */
  38. /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2 */
  39.  
  40. #include "config.h"
  41.  
  42. #include <stdio.h>
  43. #include <inttypes.h>
  44.  
  45. #include "mpeg2_internal.h"
  46. #include "mm_accel.h"
  47.  
  48. #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
  49. #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
  50. #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
  51. #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
  52. #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
  53. #define W7 565  /* 2048*sqrt (2)*cos (7*pi/16) */
  54.  
  55. /* idct main entry point  */
  56. void (*idct_block_copy) (int16_t * block, uint8_t * dest, int stride);
  57. void (*idct_block_add) (int16_t * block, uint8_t * dest, int stride);
  58.  
  59. static void idct_block_copy_c (int16_t *block, uint8_t * dest, int stride);
  60. static void idct_block_add_c (int16_t *block, uint8_t * dest, int stride);
  61.  
  62. static uint8_t clip_lut[1024];
  63. #define CLIP(i) ((clip_lut+384)[ (i)])
  64.  
  65. void idct_init (void)
  66. {
  67. #ifdef ARCH_X86
  68.     if (config.flags & MM_ACCEL_X86_MMXEXT) {
  69.     fprintf (stderr, "Using MMXEXT for IDCT transform\n");
  70.     idct_block_copy = idct_block_copy_mmxext;
  71.     idct_block_add = idct_block_add_mmxext;
  72.     idct_mmx_init ();
  73.     } else if (config.flags & MM_ACCEL_X86_MMX) {
  74.     fprintf (stderr, "Using MMX for IDCT transform\n");
  75.     idct_block_copy = idct_block_copy_mmx;
  76.     idct_block_add = idct_block_add_mmx;
  77.     idct_mmx_init ();
  78.     } else
  79. #endif
  80. #ifdef LIBMPEG2_MLIB
  81.     if (config.flags & MM_ACCEL_MLIB) {
  82.     fprintf (stderr, "Using mlib for IDCT transform\n");
  83.     idct_block_copy = idct_block_copy_mlib;
  84.     idct_block_add = idct_block_add_mlib;
  85.     } else
  86. #endif
  87.     {
  88.     int i;
  89.  
  90.     fprintf (stderr, "No accelerated IDCT transform found\n");
  91.     idct_block_copy = idct_block_copy_c;
  92.     idct_block_add = idct_block_add_c;
  93.     for (i = -384; i < 640; i++)
  94.         clip_lut[i+384] = (i < 0) ? 0 : ((i > 255) ? 255 : i);
  95.     }
  96. }
  97.  
  98. /* row (horizontal) IDCT
  99.  *
  100.  * 7 pi 1
  101.  * dst[k] = sum c[l] * src[l] * cos ( -- * ( k + - ) * l )
  102.  * l=0 8 2
  103.  *
  104.  * where: c[0] = 128
  105.  * c[1..7] = 128*sqrt (2)
  106.  */
  107.  
  108. static void inline idct_row (int16_t * block)
  109. {
  110.     int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  111.  
  112.     x1 = block[4] << 11;
  113.     x2 = block[6];
  114.     x3 = block[2];
  115.     x4 = block[1];
  116.     x5 = block[7];
  117.     x6 = block[5];
  118.     x7 = block[3];
  119.  
  120.     /* shortcut */
  121.     if (! (x1 | x2 | x3 | x4 | x5 | x6 | x7 )) {
  122.     block[0] = block[1] = block[2] = block[3] = block[4] =
  123.         block[5] = block[6] = block[7] = block[0]<<3;
  124.     return;
  125.     }
  126.  
  127.     x0 = (block[0] << 11) + 128; /* for proper rounding in the fourth stage */
  128.  
  129.     /* first stage */
  130.     x8 = W7 * (x4 + x5);
  131.     x4 = x8 + (W1 - W7) * x4;
  132.     x5 = x8 - (W1 + W7) * x5;
  133.     x8 = W3 * (x6 + x7);
  134.     x6 = x8 - (W3 - W5) * x6;
  135.     x7 = x8 - (W3 + W5) * x7;
  136.  
  137.     /* second stage */
  138.     x8 = x0 + x1;
  139.     x0 -= x1;
  140.     x1 = W6 * (x3 + x2);
  141.     x2 = x1 - (W2 + W6) * x2;
  142.     x3 = x1 + (W2 - W6) * x3;
  143.     x1 = x4 + x6;
  144.     x4 -= x6;
  145.     x6 = x5 + x7;
  146.     x5 -= x7;
  147.  
  148.     /* third stage */
  149.     x7 = x8 + x3;
  150.     x8 -= x3;
  151.     x3 = x0 + x2;
  152.     x0 -= x2;
  153.     x2 = (181 * (x4 + x5) + 128) >> 8;
  154.     x4 = (181 * (x4 - x5) + 128) >> 8;
  155.  
  156.     /* fourth stage */
  157.     block[0] = (x7 + x1) >> 8;
  158.     block[1] = (x3 + x2) >> 8;
  159.     block[2] = (x0 + x4) >> 8;
  160.     block[3] = (x8 + x6) >> 8;
  161.     block[4] = (x8 - x6) >> 8;
  162.     block[5] = (x0 - x4) >> 8;
  163.     block[6] = (x3 - x2) >> 8;
  164.     block[7] = (x7 - x1) >> 8;
  165. }
  166.  
  167. /* column (vertical) IDCT
  168.  *
  169.  * 7 pi 1
  170.  * dst[8*k] = sum c[l] * src[8*l] * cos ( -- * ( k + - ) * l )
  171.  * l=0 8 2
  172.  *
  173.  * where: c[0] = 1/1024
  174.  * c[1..7] = (1/1024)*sqrt (2)
  175.  */
  176.  
  177. static void inline idct_col (int16_t *block)
  178. {
  179.     int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  180.  
  181.     /* shortcut */
  182.     x1 = block [8*4] << 8;
  183.     x2 = block [8*6];
  184.     x3 = block [8*2];
  185.     x4 = block [8*1];
  186.     x5 = block [8*7];
  187.     x6 = block [8*5];
  188.     x7 = block [8*3];
  189.  
  190. #if 0
  191.     if (! (x1 | x2 | x3 | x4 | x5 | x6 | x7 )) {
  192.     block[8*0] = block[8*1] = block[8*2] = block[8*3] = block[8*4] =
  193.         block[8*5] = block[8*6] = block[8*7] = (block[8*0] + 32) >> 6;
  194.     return;
  195.     }
  196. #endif
  197.  
  198.     x0 = (block[8*0] << 8) + 8192;
  199.  
  200.     /* first stage */
  201.     x8 = W7 * (x4 + x5) + 4;
  202.     x4 = (x8 + (W1 - W7) * x4) >> 3;
  203.     x5 = (x8 - (W1 + W7) * x5) >> 3;
  204.     x8 = W3 * (x6 + x7) + 4;
  205.     x6 = (x8 - (W3 - W5) * x6) >> 3;
  206.     x7 = (x8 - (W3 + W5) * x7) >> 3;
  207.  
  208.     /* second stage */
  209.     x8 = x0 + x1;
  210.     x0 -= x1;
  211.     x1 = W6 * (x3 + x2) + 4;
  212.     x2 = (x1 - (W2 + W6) * x2) >> 3;
  213.     x3 = (x1 + (W2 - W6) * x3) >> 3;
  214.     x1 = x4 + x6;
  215.     x4 -= x6;
  216.     x6 = x5 + x7;
  217.     x5 -= x7;
  218.  
  219.     /* third stage */
  220.     x7 = x8 + x3;
  221.     x8 -= x3;
  222.     x3 = x0 + x2;
  223.     x0 -= x2;
  224.     x2 = (181 * (x4 + x5) + 128) >> 8;
  225.     x4 = (181 * (x4 - x5) + 128) >> 8;
  226.  
  227.     /* fourth stage */
  228.     block[8*0] = (x7 + x1) >> 14;
  229.     block[8*1] = (x3 + x2) >> 14;
  230.     block[8*2] = (x0 + x4) >> 14;
  231.     block[8*3] = (x8 + x6) >> 14;
  232.     block[8*4] = (x8 - x6) >> 14;
  233.     block[8*5] = (x0 - x4) >> 14;
  234.     block[8*6] = (x3 - x2) >> 14;
  235.     block[8*7] = (x7 - x1) >> 14;
  236. }
  237.  
  238. void idct_block_copy_c (int16_t * block, uint8_t * dest, int stride)
  239. {
  240.     int i;
  241.  
  242.     for (i = 0; i < 8; i++)
  243.     idct_row (block + 8 * i);
  244.  
  245.     for (i = 0; i < 8; i++)
  246.     idct_col (block + i);
  247.  
  248.     i = 8;
  249.     do {
  250.     dest[0] = CLIP (block[0]);
  251.     dest[1] = CLIP (block[1]);
  252.     dest[2] = CLIP (block[2]);
  253.     dest[3] = CLIP (block[3]);
  254.     dest[4] = CLIP (block[4]);
  255.     dest[5] = CLIP (block[5]);
  256.     dest[6] = CLIP (block[6]);
  257.     dest[7] = CLIP (block[7]);
  258.  
  259.     dest += stride;
  260.     block += 8;
  261.     } while (--i);
  262. }
  263.  
  264. void idct_block_add_c (int16_t * block, uint8_t * dest, int stride)
  265. {
  266.     int i;
  267.  
  268.     for (i = 0; i < 8; i++)
  269.     idct_row (block + 8 * i);
  270.  
  271.     for (i = 0; i < 8; i++)
  272.     idct_col (block + i);
  273.  
  274.     i = 8;
  275.     do {
  276.     dest[0] = CLIP (block[0] + dest[0]);
  277.     dest[1] = CLIP (block[1] + dest[1]);
  278.     dest[2] = CLIP (block[2] + dest[2]);
  279.     dest[3] = CLIP (block[3] + dest[3]);
  280.     dest[4] = CLIP (block[4] + dest[4]);
  281.     dest[5] = CLIP (block[5] + dest[5]);
  282.     dest[6] = CLIP (block[6] + dest[6]);
  283.     dest[7] = CLIP (block[7] + dest[7]);
  284.  
  285.     dest += stride;
  286.     block += 8;
  287.     } while (--i);
  288. }
  289.