~ubuntu-branches/ubuntu/gutsy/vtk/gutsy

« back to all changes in this revision

Viewing changes to Utilities/vtkmpeg2encode/idct.c

  • Committer: Bazaar Package Importer
  • Author(s): Michele Angrisano
  • Date: 2007-06-30 22:39:48 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070630223948-6tn51upaurwfrcz8
Tags: 5.0.3-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add export SHELL=/bin/bash in debian/rules.
  - Update maintainer in field debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* idct.c, inverse fast discrete cosine transform                           */
2
 
 
3
 
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
 
 
5
 
/*
6
 
 * Disclaimer of Warranty
7
 
 *
8
 
 * These software programs are available to the user without any license fee or
9
 
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
10
 
 * any and all warranties, whether express, implied, or statuary, including any
11
 
 * implied warranties or merchantability or of fitness for a particular
12
 
 * purpose.  In no event shall the copyright-holder be liable for any
13
 
 * incidental, punitive, or consequential damages of any kind whatsoever
14
 
 * arising from the use of these programs.
15
 
 *
16
 
 * This disclaimer of warranty extends to the user of these programs and user's
17
 
 * customers, employees, agents, transferees, successors, and assigns.
18
 
 *
19
 
 * The MPEG Software Simulation Group does not represent or warrant that the
20
 
 * programs furnished hereunder are free of infringement of any third-party
21
 
 * patents.
22
 
 *
23
 
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24
 
 * are subject to royalty fees to patent holders.  Many of these patents are
25
 
 * general enough such that they are unavoidable regardless of implementation
26
 
 * design.
27
 
 *
28
 
 */
29
 
 
30
 
/**********************************************************/
31
 
/* inverse two dimensional DCT, Chen-Wang algorithm       */
32
 
/* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984)             */
33
 
/* 32-bit integer arithmetic (8 bit coefficients)         */
34
 
/* 11 mults, 29 adds per DCT                              */
35
 
/*                                      sE, 18.8.91       */
36
 
/**********************************************************/
37
 
/* coefficients extended to 12 bit for IEEE1180-1990      */
38
 
/* compliance                           sE,  2.1.94       */
39
 
/**********************************************************/
40
 
 
41
 
/* this code assumes >> to be a two's-complement arithmetic */
42
 
/* right shift: (-2)>>1 == -1 , (-3)>>1 == -2               */
43
 
 
44
 
#include <stdio.h>
45
 
#include "mpeg2enc_config.h"
46
 
#include "mpeg2enc_global.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
 
/* global declarations */
56
 
void MPEG2_idct _ANSI_ARGS_((short *block));
57
 
 
58
 
/* private data */
59
 
static short iclip[1024]; /* clipping table */
60
 
static short *iclp;
61
 
 
62
 
/* private prototypes */
63
 
static void MPEG2_idctrow _ANSI_ARGS_((short *blk));
64
 
static void MPEG2_idctcol _ANSI_ARGS_((short *blk));
65
 
 
66
 
/* row (horizontal) IDCT
67
 
 *
68
 
 *           7                       pi         1
69
 
 * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
70
 
 *          l=0                      8          2
71
 
 *
72
 
 * where: c[0]    = 128
73
 
 *        c[1..7] = 128*sqrt(2)
74
 
 */
75
 
 
76
 
static void MPEG2_idctrow(blk)
77
 
short *blk;
78
 
{
79
 
  int x0, x1, x2, x3, x4, x5, x6, x7, x8;
80
 
 
81
 
  /* shortcut */
82
 
  if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
83
 
        (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
84
 
  {
85
 
    blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
86
 
    return;
87
 
  }
88
 
 
89
 
  x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
90
 
 
91
 
  /* first stage */
92
 
  x8 = W7*(x4+x5);
93
 
  x4 = x8 + (W1-W7)*x4;
94
 
  x5 = x8 - (W1+W7)*x5;
95
 
  x8 = W3*(x6+x7);
96
 
  x6 = x8 - (W3-W5)*x6;
97
 
  x7 = x8 - (W3+W5)*x7;
98
 
  
99
 
  /* second stage */
100
 
  x8 = x0 + x1;
101
 
  x0 -= x1;
102
 
  x1 = W6*(x3+x2);
103
 
  x2 = x1 - (W2+W6)*x2;
104
 
  x3 = x1 + (W2-W6)*x3;
105
 
  x1 = x4 + x6;
106
 
  x4 -= x6;
107
 
  x6 = x5 + x7;
108
 
  x5 -= x7;
109
 
  
110
 
  /* third stage */
111
 
  x7 = x8 + x3;
112
 
  x8 -= x3;
113
 
  x3 = x0 + x2;
114
 
  x0 -= x2;
115
 
  x2 = (181*(x4+x5)+128)>>8;
116
 
  x4 = (181*(x4-x5)+128)>>8;
117
 
  
118
 
  /* fourth stage */
119
 
  blk[0] = (x7+x1)>>8;
120
 
  blk[1] = (x3+x2)>>8;
121
 
  blk[2] = (x0+x4)>>8;
122
 
  blk[3] = (x8+x6)>>8;
123
 
  blk[4] = (x8-x6)>>8;
124
 
  blk[5] = (x0-x4)>>8;
125
 
  blk[6] = (x3-x2)>>8;
126
 
  blk[7] = (x7-x1)>>8;
127
 
}
128
 
 
129
 
/* column (vertical) IDCT
130
 
 *
131
 
 *             7                         pi         1
132
 
 * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
133
 
 *            l=0                        8          2
134
 
 *
135
 
 * where: c[0]    = 1/1024
136
 
 *        c[1..7] = (1/1024)*sqrt(2)
137
 
 */
138
 
static void MPEG2_idctcol(blk)
139
 
short *blk;
140
 
{
141
 
  int x0, x1, x2, x3, x4, x5, x6, x7, x8;
142
 
 
143
 
  /* shortcut */
144
 
  if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
145
 
        (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
146
 
  {
147
 
    blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
148
 
      iclp[(blk[8*0]+32)>>6];
149
 
    return;
150
 
  }
151
 
 
152
 
  x0 = (blk[8*0]<<8) + 8192;
153
 
 
154
 
  /* first stage */
155
 
  x8 = W7*(x4+x5) + 4;
156
 
  x4 = (x8+(W1-W7)*x4)>>3;
157
 
  x5 = (x8-(W1+W7)*x5)>>3;
158
 
  x8 = W3*(x6+x7) + 4;
159
 
  x6 = (x8-(W3-W5)*x6)>>3;
160
 
  x7 = (x8-(W3+W5)*x7)>>3;
161
 
  
162
 
  /* second stage */
163
 
  x8 = x0 + x1;
164
 
  x0 -= x1;
165
 
  x1 = W6*(x3+x2) + 4;
166
 
  x2 = (x1-(W2+W6)*x2)>>3;
167
 
  x3 = (x1+(W2-W6)*x3)>>3;
168
 
  x1 = x4 + x6;
169
 
  x4 -= x6;
170
 
  x6 = x5 + x7;
171
 
  x5 -= x7;
172
 
  
173
 
  /* third stage */
174
 
  x7 = x8 + x3;
175
 
  x8 -= x3;
176
 
  x3 = x0 + x2;
177
 
  x0 -= x2;
178
 
  x2 = (181*(x4+x5)+128)>>8;
179
 
  x4 = (181*(x4-x5)+128)>>8;
180
 
  
181
 
  /* fourth stage */
182
 
  blk[8*0] = iclp[(x7+x1)>>14];
183
 
  blk[8*1] = iclp[(x3+x2)>>14];
184
 
  blk[8*2] = iclp[(x0+x4)>>14];
185
 
  blk[8*3] = iclp[(x8+x6)>>14];
186
 
  blk[8*4] = iclp[(x8-x6)>>14];
187
 
  blk[8*5] = iclp[(x0-x4)>>14];
188
 
  blk[8*6] = iclp[(x3-x2)>>14];
189
 
  blk[8*7] = iclp[(x7-x1)>>14];
190
 
}
191
 
 
192
 
/* two dimensional inverse discrete cosine transform */
193
 
void MPEG2_idct(block)
194
 
short *block;
195
 
{
196
 
  int i;
197
 
 
198
 
  for (i=0; i<8; i++)
199
 
    MPEG2_idctrow(block+8*i);
200
 
 
201
 
  for (i=0; i<8; i++)
202
 
    MPEG2_idctcol(block+i);
203
 
}
204
 
 
205
 
VTK_MPEG2ENC_EXPORT void MPEG2_init_idct()
206
 
{
207
 
  int i;
208
 
 
209
 
  iclp = iclip+512;
210
 
  for (i= -512; i<512; i++)
211
 
    iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
212
 
}