]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2enc/transfrm.c
avoid segfault when unaware user sets SplitOnly to true, and the asks for ImageDataVector
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2enc / transfrm.c
1 /* transfrm.c,  forward / inverse transformation                            */
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 <math.h>
32 #include "config.h"
33 #include "global.h"
34
35 /* private prototypes*/
36 static void add_pred _ANSI_ARGS_((unsigned char *pred, unsigned char *cur,
37   int lx, short *blk));
38 static void sub_pred _ANSI_ARGS_((unsigned char *pred, unsigned char *cur,
39   int lx, short *blk));
40
41 /* subtract prediction and transform prediction error */
42 void transform(pred,cur,mbi,blocks)
43 unsigned char *pred[], *cur[];
44 struct mbinfo *mbi;
45 short blocks[][64];
46 {
47   int i, j, i1, j1, k, n, cc, offs, lx;
48
49   k = 0;
50
51   for (j=0; j<height2; j+=16)
52     for (i=0; i<width; i+=16)
53     {
54       for (n=0; n<block_count; n++)
55       {
56         cc = (n<4) ? 0 : (n&1)+1; /* color component index */
57         if (cc==0)
58         {
59           /* luminance */
60           if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type)
61           {
62             /* field DCT */
63             offs = i + ((n&1)<<3) + width*(j+((n&2)>>1));
64             lx = width<<1;
65           }
66           else
67           {
68             /* frame DCT */
69             offs = i + ((n&1)<<3) + width2*(j+((n&2)<<2));
70             lx = width2;
71           }
72
73           if (pict_struct==BOTTOM_FIELD)
74             offs += width;
75         }
76         else
77         {
78           /* chrominance */
79
80           /* scale coordinates */
81           i1 = (chroma_format==CHROMA444) ? i : i>>1;
82           j1 = (chroma_format!=CHROMA420) ? j : j>>1;
83
84           if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type
85               && (chroma_format!=CHROMA420))
86           {
87             /* field DCT */
88             offs = i1 + (n&8) + chrom_width*(j1+((n&2)>>1));
89             lx = chrom_width<<1;
90           }
91           else
92           {
93             /* frame DCT */
94             offs = i1 + (n&8) + chrom_width2*(j1+((n&2)<<2));
95             lx = chrom_width2;
96           }
97
98           if (pict_struct==BOTTOM_FIELD)
99             offs += chrom_width;
100         }
101
102         sub_pred(pred[cc]+offs,cur[cc]+offs,lx,blocks[k*block_count+n]);
103         fdct(blocks[k*block_count+n]);
104       }
105
106       k++;
107     }
108 }
109
110 /* inverse transform prediction error and add prediction */
111 void itransform(pred,cur,mbi,blocks)
112 unsigned char *pred[],*cur[];
113 struct mbinfo *mbi;
114 short blocks[][64];
115 {
116   int i, j, i1, j1, k, n, cc, offs, lx;
117
118   k = 0;
119
120   for (j=0; j<height2; j+=16)
121     for (i=0; i<width; i+=16)
122     {
123       for (n=0; n<block_count; n++)
124       {
125         cc = (n<4) ? 0 : (n&1)+1; /* color component index */
126
127         if (cc==0)
128         {
129           /* luminance */
130           if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type)
131           {
132             /* field DCT */
133             offs = i + ((n&1)<<3) + width*(j+((n&2)>>1));
134             lx = width<<1;
135           }
136           else
137           {
138             /* frame DCT */
139             offs = i + ((n&1)<<3) + width2*(j+((n&2)<<2));
140             lx = width2;
141           }
142
143           if (pict_struct==BOTTOM_FIELD)
144             offs += width;
145         }
146         else
147         {
148           /* chrominance */
149
150           /* scale coordinates */
151           i1 = (chroma_format==CHROMA444) ? i : i>>1;
152           j1 = (chroma_format!=CHROMA420) ? j : j>>1;
153
154           if ((pict_struct==FRAME_PICTURE) && mbi[k].dct_type
155               && (chroma_format!=CHROMA420))
156           {
157             /* field DCT */
158             offs = i1 + (n&8) + chrom_width*(j1+((n&2)>>1));
159             lx = chrom_width<<1;
160           }
161           else
162           {
163             /* frame DCT */
164             offs = i1 + (n&8) + chrom_width2*(j1+((n&2)<<2));
165             lx = chrom_width2;
166           }
167
168           if (pict_struct==BOTTOM_FIELD)
169             offs += chrom_width;
170         }
171
172         idct(blocks[k*block_count+n]);
173         add_pred(pred[cc]+offs,cur[cc]+offs,lx,blocks[k*block_count+n]);
174       }
175
176       k++;
177     }
178 }
179
180 /* add prediction and prediction error, saturate to 0...255 */
181 static void add_pred(pred,cur,lx,blk)
182 unsigned char *pred, *cur;
183 int lx;
184 short *blk;
185 {
186   int i, j;
187
188   for (j=0; j<8; j++)
189   {
190     for (i=0; i<8; i++)
191       cur[i] = clp[blk[i] + pred[i]];
192     blk+= 8;
193     cur+= lx;
194     pred+= lx;
195   }
196 }
197
198 /* subtract prediction from block data */
199 static void sub_pred(pred,cur,lx,blk)
200 unsigned char *pred, *cur;
201 int lx;
202 short *blk;
203 {
204   int i, j;
205
206   for (j=0; j<8; j++)
207   {
208     for (i=0; i<8; i++)
209       blk[i] = cur[i] - pred[i];
210     blk+= 8;
211     cur+= lx;
212     pred+= lx;
213   }
214 }
215
216 /*
217  * select between frame and field DCT
218  *
219  * preliminary version: based on inter-field correlation
220  */
221 void dct_type_estimation(pred,cur,mbi)
222 unsigned char *pred,*cur;
223 struct mbinfo *mbi;
224 {
225   short blk0[128], blk1[128];
226   int i, j, i0, j0, k, offs, s0, s1, sq0, sq1, s01;
227   double d, r;
228
229   k = 0;
230
231   for (j0=0; j0<height2; j0+=16)
232     for (i0=0; i0<width; i0+=16)
233     {
234       if (frame_pred_dct || pict_struct!=FRAME_PICTURE)
235         mbi[k].dct_type = 0;
236       else
237       {
238         /* interlaced frame picture */
239         /*
240          * calculate prediction error (cur-pred) for top (blk0)
241          * and bottom field (blk1)
242          */
243         for (j=0; j<8; j++)
244         {
245           offs = width*((j<<1)+j0) + i0;
246           for (i=0; i<16; i++)
247           {
248             blk0[16*j+i] = cur[offs] - pred[offs];
249             blk1[16*j+i] = cur[offs+width] - pred[offs+width];
250             offs++;
251           }
252         }
253         /* correlate fields */
254         s0=s1=sq0=sq1=s01=0;
255
256         for (i=0; i<128; i++)
257         {
258           s0+= blk0[i];
259           sq0+= blk0[i]*blk0[i];
260           s1+= blk1[i];
261           sq1+= blk1[i]*blk1[i];
262           s01+= blk0[i]*blk1[i];
263         }
264
265         d = (sq0-(s0*s0)/128.0)*(sq1-(s1*s1)/128.0);
266
267         if (d>0.0)
268         {
269           r = (s01-(s0*s1)/128.0)/sqrt(d);
270           if (r>0.5)
271             mbi[k].dct_type = 0; /* frame DCT */
272           else
273             mbi[k].dct_type = 1; /* field DCT */
274         }
275         else
276           mbi[k].dct_type = 1; /* field DCT */
277       }
278       k++;
279     }
280 }