1 /* transfrm.c, forward / inverse transformation */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
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.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
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
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
35 /* private prototypes*/
36 static void add_pred _ANSI_ARGS_((unsigned char *pred, unsigned char *cur,
38 static void sub_pred _ANSI_ARGS_((unsigned char *pred, unsigned char *cur,
41 /* subtract prediction and transform prediction error */
42 void transform(pred,cur,mbi,blocks)
43 unsigned char *pred[], *cur[];
47 int i, j, i1, j1, k, n, cc, offs, lx;
51 for (j=0; j<height2; j+=16)
52 for (i=0; i<width; i+=16)
54 for (n=0; n<block_count; n++)
56 cc = (n<4) ? 0 : (n&1)+1; /* color component index */
60 if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type)
63 offs = i + ((n&1)<<3) + width*(j+((n&2)>>1));
69 offs = i + ((n&1)<<3) + width2*(j+((n&2)<<2));
73 if (pict_struct==BOTTOM_FIELD)
80 /* scale coordinates */
81 i1 = (chroma_format==CHROMA444) ? i : i>>1;
82 j1 = (chroma_format!=CHROMA420) ? j : j>>1;
84 if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type
85 && (chroma_format!=CHROMA420))
88 offs = i1 + (n&8) + chrom_width*(j1+((n&2)>>1));
94 offs = i1 + (n&8) + chrom_width2*(j1+((n&2)<<2));
98 if (pict_struct==BOTTOM_FIELD)
102 sub_pred(pred[cc]+offs,cur[cc]+offs,lx,blocks[k*block_count+n]);
103 fdct(blocks[k*block_count+n]);
110 /* inverse transform prediction error and add prediction */
111 void itransform(pred,cur,mbi,blocks)
112 unsigned char *pred[],*cur[];
116 int i, j, i1, j1, k, n, cc, offs, lx;
120 for (j=0; j<height2; j+=16)
121 for (i=0; i<width; i+=16)
123 for (n=0; n<block_count; n++)
125 cc = (n<4) ? 0 : (n&1)+1; /* color component index */
130 if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type)
133 offs = i + ((n&1)<<3) + width*(j+((n&2)>>1));
139 offs = i + ((n&1)<<3) + width2*(j+((n&2)<<2));
143 if (pict_struct==BOTTOM_FIELD)
150 /* scale coordinates */
151 i1 = (chroma_format==CHROMA444) ? i : i>>1;
152 j1 = (chroma_format!=CHROMA420) ? j : j>>1;
154 if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type
155 && (chroma_format!=CHROMA420))
158 offs = i1 + (n&8) + chrom_width*(j1+((n&2)>>1));
164 offs = i1 + (n&8) + chrom_width2*(j1+((n&2)<<2));
168 if (pict_struct==BOTTOM_FIELD)
172 idct(blocks[k*block_count+n]);
173 add_pred(pred[cc]+offs,cur[cc]+offs,lx,blocks[k*block_count+n]);
180 /* add prediction and prediction error, saturate to 0...255 */
181 static void add_pred(pred,cur,lx,blk)
182 unsigned char *pred, *cur;
191 cur[i] = clp[blk[i] + pred[i]];
198 /* subtract prediction from block data */
199 static void sub_pred(pred,cur,lx,blk)
200 unsigned char *pred, *cur;
209 blk[i] = cur[i] - pred[i];
217 * select between frame and field DCT
219 * preliminary version: based on inter-field correlation
221 void dct_type_estimation(pred,cur,mbi)
222 unsigned char *pred,*cur;
225 short blk0[128], blk1[128];
226 int i, j, i0, j0, k, offs, s0, s1, sq0, sq1, s01;
231 for (j0=0; j0<height2; j0+=16)
232 for (i0=0; i0<width; i0+=16)
234 if (frame_pred_dct || pict_struct!=FRAME_PICTURE)
238 /* interlaced frame picture */
240 * calculate prediction error (cur-pred) for top (blk0)
241 * and bottom field (blk1)
245 offs = width*((j<<1)+j0) + i0;
248 blk0[16*j+i] = cur[offs] - pred[offs];
249 blk1[16*j+i] = cur[offs+width] - pred[offs+width];
253 /* correlate fields */
256 for (i=0; i<128; i++)
259 sq0+= blk0[i]*blk0[i];
261 sq1+= blk1[i]*blk1[i];
262 s01+= blk0[i]*blk1[i];
265 d = (sq0-(s0*s0)/128.0)*(sq1-(s1*s1)/128.0);
269 r = (s01-(s0*s1)/128.0)/sqrt(d);
271 mbi[k].dct_type = 0; /* frame DCT */
273 mbi[k].dct_type = 1; /* field DCT */
276 mbi[k].dct_type = 1; /* field DCT */