File: transfrm.c

package info (click to toggle)
vtk 5.0.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 51,080 kB
  • ctags: 67,442
  • sloc: cpp: 522,627; ansic: 221,292; tcl: 43,377; python: 14,072; perl: 3,102; java: 1,436; yacc: 1,033; sh: 469; lex: 248; makefile: 181; asm: 154
file content (279 lines) | stat: -rw-r--r-- 8,080 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* transfrm.c,  forward / inverse transformation                            */

/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */

/*
 * Disclaimer of Warranty
 *
 * These software programs are available to the user without any license fee or
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
 * any and all warranties, whether express, implied, or statuary, including any
 * implied warranties or merchantability or of fitness for a particular
 * purpose.  In no event shall the copyright-holder be liable for any
 * incidental, punitive, or consequential damages of any kind whatsoever
 * arising from the use of these programs.
 *
 * This disclaimer of warranty extends to the user of these programs and user's
 * customers, employees, agents, transferees, successors, and assigns.
 *
 * The MPEG Software Simulation Group does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any third-party
 * patents.
 *
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
 * are subject to royalty fees to patent holders.  Many of these patents are
 * general enough such that they are unavoidable regardless of implementation
 * design.
 *
 */

#include <stdio.h>
#include <math.h>
#include "mpeg2enc_config.h"
#include "mpeg2enc_global.h"

/* private prototypes*/
static void add_pred _ANSI_ARGS_((unsigned char *pred, unsigned char *cur,
  int lx, short *blk,struct MPEG2_structure *mpeg2_struct));
static void sub_pred _ANSI_ARGS_((unsigned char *pred, unsigned char *cur,
  int lx, short *blk));

/* subtract prediction and transform prediction error */
void MPEG2_transform( unsigned char *pred[], unsigned char *cur[],
  struct mbinfo *mbi,
  short blocks[][64],
  struct MPEG2_structure *mpeg2_struct)
{
  int i, j, i1, j1, k, n, cc, offs, lx;

  k = 0;

  for (j=0; j<mpeg2_struct->height2; j+=16)
    for (i=0; i<mpeg2_struct->width; i+=16)
    {
      for (n=0; n<mpeg2_struct->block_count; n++)
      {
        cc = (n<4) ? 0 : (n&1)+1; /* color component index */
        if (cc==0)
        {
          /* luminance */
          if ((mpeg2_struct->pict_struct==FRAME_PICTURE) && mbi[k].dct_type)
          {
            /* field DCT */
            offs = i + ((n&1)<<3) + mpeg2_struct->width*(j+((n&2)>>1));
            lx = mpeg2_struct->width<<1;
          }
          else
          {
            /* frame DCT */
            offs = i + ((n&1)<<3) + mpeg2_struct->width2*(j+((n&2)<<2));
            lx = mpeg2_struct->width2;
          }

          if (mpeg2_struct->pict_struct==BOTTOM_FIELD)
            offs += mpeg2_struct->width;
        }
        else
        {
          /* chrominance */

          /* scale coordinates */
          i1 = (mpeg2_struct->chroma_format==CHROMA444) ? i : i>>1;
          j1 = (mpeg2_struct->chroma_format!=CHROMA420) ? j : j>>1;

          if ((mpeg2_struct->pict_struct==FRAME_PICTURE) && mbi[k].dct_type
              && (mpeg2_struct->chroma_format!=CHROMA420))
          {
            /* field DCT */
            offs = i1 + (n&8) + mpeg2_struct->chrom_width*(j1+((n&2)>>1));
            lx = mpeg2_struct->chrom_width<<1;
          }
          else
          {
            /* frame DCT */
            offs = i1 + (n&8) + mpeg2_struct->chrom_width2*(j1+((n&2)<<2));
            lx = mpeg2_struct->chrom_width2;
          }

          if (mpeg2_struct->pict_struct==BOTTOM_FIELD)
            offs += mpeg2_struct->chrom_width;
        }

        sub_pred(pred[cc]+offs,cur[cc]+offs,lx,blocks[k*mpeg2_struct->block_count+n]);
        MPEG2_fdct(blocks[k*mpeg2_struct->block_count+n]);
      }

      k++;
    }
}

/* inverse transform prediction error and add prediction */
void MPEG2_itransform( unsigned char *pred[], unsigned char *cur[],
  struct mbinfo *mbi,
  short blocks[][64],
  struct MPEG2_structure *mpeg2_struct)
{
  int i, j, i1, j1, k, n, cc, offs, lx;

  k = 0;

  for (j=0; j<mpeg2_struct->height2; j+=16)
    for (i=0; i<mpeg2_struct->width; i+=16)
    {
      for (n=0; n<mpeg2_struct->block_count; n++)
      {
        cc = (n<4) ? 0 : (n&1)+1; /* color component index */

        if (cc==0)
        {
          /* luminance */
          if ((mpeg2_struct->pict_struct==FRAME_PICTURE) && mbi[k].dct_type)
          {
            /* field DCT */
            offs = i + ((n&1)<<3) + mpeg2_struct->width*(j+((n&2)>>1));
            lx = mpeg2_struct->width<<1;
          }
          else
          {
            /* frame DCT */
            offs = i + ((n&1)<<3) + mpeg2_struct->width2*(j+((n&2)<<2));
            lx = mpeg2_struct->width2;
          }

          if (mpeg2_struct->pict_struct==BOTTOM_FIELD)
            offs += mpeg2_struct->width;
        }
        else
        {
          /* chrominance */

          /* scale coordinates */
          i1 = (mpeg2_struct->chroma_format==CHROMA444) ? i : i>>1;
          j1 = (mpeg2_struct->chroma_format!=CHROMA420) ? j : j>>1;

          if ((mpeg2_struct->pict_struct==FRAME_PICTURE) && mbi[k].dct_type
              && (mpeg2_struct->chroma_format!=CHROMA420))
          {
            /* field DCT */
            offs = i1 + (n&8) + mpeg2_struct->chrom_width*(j1+((n&2)>>1));
            lx = mpeg2_struct->chrom_width<<1;
          }
          else
          {
            /* frame DCT */
            offs = i1 + (n&8) + mpeg2_struct->chrom_width2*(j1+((n&2)<<2));
            lx = mpeg2_struct->chrom_width2;
          }

          if (mpeg2_struct->pict_struct==BOTTOM_FIELD)
            offs += mpeg2_struct->chrom_width;
        }

        MPEG2_idct(blocks[k*mpeg2_struct->block_count+n]);
        add_pred(pred[cc]+offs,cur[cc]+offs,lx,blocks[k*mpeg2_struct->block_count+n],mpeg2_struct);
      }

      k++;
    }
}

/* add prediction and prediction error, saturate to 0...255 */
static void add_pred( unsigned char *pred, unsigned char *cur,
  int lx,
  short *blk,
  struct MPEG2_structure *mpeg2_struct)
{
  int i, j;

  for (j=0; j<8; j++)
  {
    for (i=0; i<8; i++)
      cur[i] = mpeg2_struct->clp[blk[i] + pred[i]];
    blk+= 8;
    cur+= lx;
    pred+= lx;
  }
}

/* subtract prediction from block data */
static void sub_pred(pred,cur,lx,blk)
unsigned char *pred, *cur;
int lx;
short *blk;
{
  int i, j;

  for (j=0; j<8; j++)
  {
    for (i=0; i<8; i++)
      blk[i] = cur[i] - pred[i];
    blk+= 8;
    cur+= lx;
    pred+= lx;
  }
}

/*
 * select between frame and field DCT
 *
 * preliminary version: based on inter-field correlation
 */
void MPEG2_dct_type_estimation( unsigned char *pred, unsigned char *cur,
  struct mbinfo *mbi,struct MPEG2_structure *mpeg2_struct)
{
  short blk0[128], blk1[128];
  int i, j, i0, j0, k, offs, s0, s1, sq0, sq1, s01;
  double d, r;

  k = 0;

  for (j0=0; j0<mpeg2_struct->height2; j0+=16)
    for (i0=0; i0<mpeg2_struct->width; i0+=16)
    {
      if (mpeg2_struct->frame_pred_dct || mpeg2_struct->pict_struct!=FRAME_PICTURE)
        mbi[k].dct_type = 0;
      else
      {
        /* interlaced frame picture */
        /*
         * calculate prediction error (cur-pred) for top (blk0)
         * and bottom field (blk1)
         */
        for (j=0; j<8; j++)
        {
          offs = mpeg2_struct->width*((j<<1)+j0) + i0;
          for (i=0; i<16; i++)
          {
            blk0[16*j+i] = cur[offs] - pred[offs];
            blk1[16*j+i] = cur[offs+mpeg2_struct->width] - pred[offs+mpeg2_struct->width];
            offs++;
          }
        }
        /* correlate fields */
        s0=s1=sq0=sq1=s01=0;

        for (i=0; i<128; i++)
        {
          s0+= blk0[i];
          sq0+= blk0[i]*blk0[i];
          s1+= blk1[i];
          sq1+= blk1[i]*blk1[i];
          s01+= blk0[i]*blk1[i];
        }

        d = (sq0-(s0*s0)/128.0)*(sq1-(s1*s1)/128.0);

        if (d>0.0)
        {
          r = (s01-(s0*s1)/128.0)/sqrt(d);
          if (r>0.5)
            mbi[k].dct_type = 0; /* frame DCT */
          else
            mbi[k].dct_type = 1; /* field DCT */
        }
        else
          mbi[k].dct_type = 1; /* field DCT */
      }
      k++;
    }
}