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

« back to all changes in this revision

Viewing changes to Utilities/vtkmpeg2encode/quantize.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
 
/* quantize.c, quantization / inverse quantization                          */
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
 
#include <stdio.h>
31
 
#include "mpeg2enc_config.h"
32
 
#include "mpeg2enc_global.h"
33
 
 
34
 
static void iquant1_intra _ANSI_ARGS_((short *src, short *dst,
35
 
  int dc_prec, unsigned char *quant_mat, int mquant));
36
 
static void iquant1_non_intra _ANSI_ARGS_((short *src, short *dst,
37
 
  unsigned char *quant_mat, int mquant));
38
 
 
39
 
/* Test Model 5 quantization
40
 
 *
41
 
 * this quantizer has a bias of 1/8 stepsize towards zero
42
 
 * (except for the DC coefficient)
43
 
 */
44
 
int MPEG2_quant_intra(src,dst,dc_prec,quant_mat,mquant,mpeg2_struct)
45
 
short *src, *dst;
46
 
int dc_prec;
47
 
unsigned char *quant_mat;
48
 
int mquant;
49
 
struct MPEG2_structure *mpeg2_struct;
50
 
{
51
 
  int i;
52
 
  int x, y, d;
53
 
 
54
 
  x = src[0];
55
 
  d = 8>>dc_prec; /* intra_dc_mult */
56
 
  dst[0] = (x>=0) ? (x+(d>>1))/d : -((-x+(d>>1))/d); /* round(x/d) */
57
 
 
58
 
  for (i=1; i<64; i++)
59
 
  {
60
 
    x = src[i];
61
 
    d = quant_mat[i];
62
 
    y = (32*(x>=0 ? x : -x) + (d>>1))/d; /* round(32*x/quant_mat) */
63
 
    d = (3*mquant+2)>>2;
64
 
    y = (y+d)/(2*mquant); /* (y+0.75*mquant) / (2*mquant) */
65
 
 
66
 
    /* clip to syntax limits */
67
 
    if (y > 255)
68
 
    {
69
 
      if (mpeg2_struct->mpeg1)
70
 
        y = 255;
71
 
      else if (y > 2047)
72
 
        y = 2047;
73
 
    }
74
 
 
75
 
    dst[i] = (x>=0) ? y : -y;
76
 
 
77
 
#if 0
78
 
    /* this quantizer is virtually identical to the above */
79
 
    if (x<0)
80
 
      x = -x;
81
 
    d = mquant*quant_mat[i];
82
 
    y = (16*x + ((3*d)>>3)) / d;
83
 
    dst[i] = (src[i]<0) ? -y : y;
84
 
#endif
85
 
  }
86
 
 
87
 
  return 1;
88
 
}
89
 
 
90
 
int MPEG2_quant_non_intra(src,dst,quant_mat,mquant,mpeg2_struct)
91
 
short *src, *dst;
92
 
unsigned char *quant_mat;
93
 
int mquant;
94
 
struct MPEG2_structure *mpeg2_struct;
95
 
{
96
 
  int i;
97
 
  int x, y, d;
98
 
  int nzflag;
99
 
 
100
 
  nzflag = 0;
101
 
 
102
 
  for (i=0; i<64; i++)
103
 
  {
104
 
    x = src[i];
105
 
    d = quant_mat[i];
106
 
    y = (32*(x>=0 ? x : -x) + (d>>1))/d; /* round(32*x/quant_mat) */
107
 
    y /= (2*mquant);
108
 
 
109
 
    /* clip to syntax limits */
110
 
    if (y > 255)
111
 
    {
112
 
      if (mpeg2_struct->mpeg1)
113
 
        y = 255;
114
 
      else if (y > 2047)
115
 
        y = 2047;
116
 
    }
117
 
 
118
 
    if ((dst[i] = (x>=0 ? y : -y)) != 0)
119
 
      nzflag=1;
120
 
  }
121
 
 
122
 
  return nzflag;
123
 
}
124
 
 
125
 
/* MPEG-2 inverse quantization */
126
 
void MPEG2_iquant_intra(src,dst,dc_prec,quant_mat,mquant,mpeg2_struct)
127
 
short *src, *dst;
128
 
int dc_prec;
129
 
unsigned char *quant_mat;
130
 
int mquant;
131
 
struct MPEG2_structure *mpeg2_struct;
132
 
{
133
 
  int i, val, sum;
134
 
 
135
 
  if (mpeg2_struct->mpeg1)
136
 
    iquant1_intra(src,dst,dc_prec,quant_mat,mquant);
137
 
  else
138
 
  {
139
 
    sum = dst[0] = src[0] << (3-dc_prec);
140
 
    for (i=1; i<64; i++)
141
 
    {
142
 
      val = (int)(src[i]*quant_mat[i]*mquant)/16;
143
 
      sum+= dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
144
 
    }
145
 
 
146
 
    /* mismatch control */
147
 
    if ((sum&1)==0)
148
 
      dst[63]^= 1;
149
 
  }
150
 
}
151
 
 
152
 
void MPEG2_iquant_non_intra(src,dst,quant_mat,mquant,mpeg2_struct)
153
 
short *src, *dst;
154
 
unsigned char *quant_mat;
155
 
int mquant;
156
 
struct MPEG2_structure *mpeg2_struct;
157
 
{
158
 
  int i, val, sum;
159
 
 
160
 
  if (mpeg2_struct->mpeg1)
161
 
    iquant1_non_intra(src,dst,quant_mat,mquant);
162
 
  else
163
 
  {
164
 
    sum = 0;
165
 
    for (i=0; i<64; i++)
166
 
    {
167
 
      val = src[i];
168
 
      if (val!=0)
169
 
        val = (int)((2*val+(val>0 ? 1 : -1))*quant_mat[i]*mquant)/32;
170
 
      sum+= dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
171
 
    }
172
 
 
173
 
    /* mismatch control */
174
 
    if ((sum&1)==0)
175
 
      dst[63]^= 1;
176
 
  }
177
 
}
178
 
 
179
 
/* MPEG-1 inverse quantization */
180
 
static void iquant1_intra(src,dst,dc_prec,quant_mat,mquant)
181
 
short *src, *dst;
182
 
int dc_prec;
183
 
unsigned char *quant_mat;
184
 
int mquant;
185
 
{
186
 
  int i, val;
187
 
 
188
 
  dst[0] = src[0] << (3-dc_prec);
189
 
  for (i=1; i<64; i++)
190
 
  {
191
 
    val = (int)(src[i]*quant_mat[i]*mquant)/16;
192
 
 
193
 
    /* mismatch control */
194
 
    if ((val&1)==0 && val!=0)
195
 
      val+= (val>0) ? -1 : 1;
196
 
 
197
 
    /* saturation */
198
 
    dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
199
 
  }
200
 
}
201
 
 
202
 
static void iquant1_non_intra(src,dst,quant_mat,mquant)
203
 
short *src, *dst;
204
 
unsigned char *quant_mat;
205
 
int mquant;
206
 
{
207
 
  int i, val;
208
 
 
209
 
  for (i=0; i<64; i++)
210
 
  {
211
 
    val = src[i];
212
 
    if (val!=0)
213
 
    {
214
 
      val = (int)((2*val+(val>0 ? 1 : -1))*quant_mat[i]*mquant)/32;
215
 
 
216
 
      /* mismatch control */
217
 
      if ((val&1)==0 && val!=0)
218
 
        val+= (val>0) ? -1 : 1;
219
 
    }
220
 
 
221
 
    /* saturation */
222
 
    dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
223
 
  }
224
 
}