[svn] / branches / dev-api-4 / xvidcore / src / motion / gmc.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/motion/gmc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1117 - (view) (download)

1 : chl 1077 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * GMC interpolation module
5 :     *
6 :     * This program is free software; you can redistribute it and/or modify
7 :     * it under the terms of the GNU General Public License as published by
8 :     * the Free Software Foundation; either version 2 of the License, or
9 :     * (at your option) any later version.
10 :     *
11 :     * This program is distributed in the hope that it will be useful,
12 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : syskin 1085 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : chl 1077 * GNU General Public License for more details.
15 :     *
16 :     * You should have received a copy of the GNU General Public License
17 :     * along with this program; if not, write to the Free Software
18 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 :     *
20 :     *************************************************************************/
21 :    
22 :     #include "../portab.h"
23 :     #include "../global.h"
24 :     #include "../encoder.h"
25 :     #include "gmc.h"
26 :    
27 :     #include <stdio.h>
28 :    
29 :     /* These are mainly the new GMC routines by -Skal- (C) 2003 */
30 :    
31 :     //////////////////////////////////////////////////////////
32 : syskin 1085 // Pts = 2 or 3
33 : chl 1077
34 : syskin 1085 // Warning! *src is the global frame pointer (that is: adress
35 :     // of pixel 0,0), not the macroblock one.
36 :     // Conversely, *dst is the macroblock top-left adress.
37 : chl 1077
38 :     void Predict_16x16_C(const NEW_GMC_DATA * const This,
39 : syskin 1085 uint8_t *dst, const uint8_t *src,
40 :     int dststride, int srcstride, int x, int y, int rounding)
41 : chl 1077 {
42 : syskin 1085 const int W = This->sW;
43 :     const int H = This->sH;
44 :     const int rho = 3 - This->accuracy;
45 :     const int Rounder = ( (1<<7) - (rounding<<(2*rho)) ) << 16;
46 : chl 1077
47 : syskin 1085 const int dUx = This->dU[0];
48 :     const int dVx = This->dV[0];
49 :     const int dUy = This->dU[1];
50 :     const int dVy = This->dV[1];
51 : chl 1077
52 : syskin 1085 int Uo = This->Uo + 16*(dUy*y + dUx*x);
53 :     int Vo = This->Vo + 16*(dVy*y + dVx*x);
54 : chl 1077
55 : syskin 1085 int i, j;
56 : chl 1077
57 : syskin 1085 dst += 16;
58 : syskin 1117 for (j=16; j>0; --j) {
59 :     int U = Uo, V = Vo;
60 :     Uo += dUy; Vo += dVy;
61 :     for (i=-16; i<0; ++i) {
62 :     unsigned int f0, f1, ri = 16, rj = 16;
63 :     int Offset;
64 :     int u = ( U >> 16 ) << rho;
65 :     int v = ( V >> 16 ) << rho;
66 : chl 1077
67 : syskin 1117 U += dUx; V += dVx;
68 : chl 1077
69 : syskin 1117 if (u > 0 && u <= W) { ri = MTab[u&15]; Offset = u>>4; }
70 :     else if (u > W) Offset = W>>4;
71 :     else Offset = -1;
72 :    
73 :     if (v > 0 && v <= H) { rj = MTab[v&15]; Offset += (v>>4)*srcstride; }
74 :     else if (v > H) Offset += (H>>4)*srcstride;
75 :     else Offset -= srcstride;
76 : chl 1077
77 : syskin 1117 f0 = src[Offset + 0];
78 :     f0 |= src[Offset + 1] << 16;
79 :     f1 = src[Offset + srcstride + 0];
80 :     f1 |= src[Offset + srcstride + 1] << 16;
81 :     f0 = (ri*f0)>>16;
82 :     f1 = (ri*f1) & 0x0fff0000;
83 :     f0 |= f1;
84 :     f0 = (rj*f0 + Rounder) >> 24;
85 : chl 1077
86 : syskin 1117 dst[i] = (uint8_t)f0;
87 :     }
88 :     dst += dststride;
89 : syskin 1085 }
90 : chl 1077 }
91 :    
92 :     void Predict_8x8_C(const NEW_GMC_DATA * const This,
93 : syskin 1085 uint8_t *uDst, const uint8_t *uSrc,
94 :     uint8_t *vDst, const uint8_t *vSrc,
95 :     int dststride, int srcstride, int x, int y, int rounding)
96 : chl 1077 {
97 : syskin 1085 const int W = This->sW >> 1;
98 :     const int H = This->sH >> 1;
99 :     const int rho = 3-This->accuracy;
100 :     const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
101 : chl 1077
102 : syskin 1085 const int32_t dUx = This->dU[0];
103 :     const int32_t dVx = This->dV[0];
104 :     const int32_t dUy = This->dU[1];
105 :     const int32_t dVy = This->dV[1];
106 : chl 1077
107 : syskin 1085 int32_t Uo = This->Uco + 8*(dUy*y + dUx*x);
108 :     int32_t Vo = This->Vco + 8*(dVy*y + dVx*x);
109 : chl 1077
110 : syskin 1085 int i, j;
111 : chl 1077
112 : syskin 1085 uDst += 8;
113 :     vDst += 8;
114 : syskin 1117 for (j=8; j>0; --j) {
115 :     int32_t U = Uo, V = Vo;
116 :     Uo += dUy; Vo += dVy;
117 : chl 1077
118 : syskin 1117 for (i=-8; i<0; ++i) {
119 :     int Offset;
120 :     uint32_t f0, f1, ri, rj;
121 :     int32_t u, v;
122 : chl 1077
123 : syskin 1117 u = ( U >> 16 ) << rho;
124 :     v = ( V >> 16 ) << rho;
125 :     U += dUx; V += dVx;
126 : chl 1077
127 : syskin 1117 if (u > 0 && u <= W) {
128 :     ri = MTab[u&15];
129 :     Offset = u>>4;
130 :     } else {
131 :     ri = 16;
132 :     if (u>W) Offset = W>>4;
133 :     else Offset = -1;
134 :     }
135 :    
136 :     if (v > 0 && v <= H) {
137 :     rj = MTab[v&15];
138 :     Offset += (v>>4)*srcstride;
139 :     } else {
140 :     rj = 16;
141 :     if (v>H) Offset += (H>>4)*srcstride;
142 :     else Offset -= srcstride;
143 :     }
144 : chl 1077
145 : syskin 1117 f0 = uSrc[Offset + 0];
146 :     f0 |= uSrc[Offset + 1] << 16;
147 :     f1 = uSrc[Offset + srcstride + 0];
148 :     f1 |= uSrc[Offset + srcstride + 1] << 16;
149 :     f0 = (ri*f0)>>16;
150 :     f1 = (ri*f1) & 0x0fff0000;
151 :     f0 |= f1;
152 :     f0 = (rj*f0 + Rounder) >> 24;
153 : chl 1077
154 : syskin 1117 uDst[i] = (uint8_t)f0;
155 : chl 1077
156 : syskin 1117 f0 = vSrc[Offset + 0];
157 :     f0 |= vSrc[Offset + 1] << 16;
158 :     f1 = vSrc[Offset + srcstride + 0];
159 :     f1 |= vSrc[Offset + srcstride + 1] << 16;
160 :     f0 = (ri*f0)>>16;
161 :     f1 = (ri*f1) & 0x0fff0000;
162 :     f0 |= f1;
163 :     f0 = (rj*f0 + Rounder) >> 24;
164 : chl 1077
165 : syskin 1117 vDst[i] = (uint8_t)f0;
166 :     }
167 :     uDst += dststride;
168 :     vDst += dststride;
169 : syskin 1085 }
170 : chl 1077 }
171 :    
172 : syskin 1085 void get_average_mv_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
173 :     int x, int y, int qpel)
174 : chl 1077 {
175 : syskin 1085 int i, j;
176 :     int vx = 0, vy = 0;
177 :     int32_t uo = Dsp->Uo + 16*(Dsp->dU[1]*y + Dsp->dU[0]*x);
178 :     int32_t vo = Dsp->Vo + 16*(Dsp->dV[1]*y + Dsp->dV[0]*x);
179 :     for (j=16; j>0; --j)
180 :     {
181 :     int32_t U, V;
182 :     U = uo; uo += Dsp->dU[1];
183 :     V = vo; vo += Dsp->dV[1];
184 :     for (i=16; i>0; --i)
185 :     {
186 :     int32_t u,v;
187 :     u = U >> 16; U += Dsp->dU[0]; vx += u;
188 :     v = V >> 16; V += Dsp->dV[0]; vy += v;
189 :     }
190 :     }
191 :     vx -= (256*x+120) << (5+Dsp->accuracy); // 120 = 15*16/2
192 :     vy -= (256*y+120) << (5+Dsp->accuracy);
193 : chl 1077
194 : syskin 1085 mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel );
195 :     mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel );
196 : chl 1077 }
197 :    
198 :     //////////////////////////////////////////////////////////
199 :     // simplified version for 1 warp point
200 :    
201 :     void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This,
202 : syskin 1085 uint8_t *Dst, const uint8_t *Src,
203 :     int dststride, int srcstride, int x, int y, int rounding)
204 : chl 1077 {
205 : syskin 1085 const int W = This->sW;
206 :     const int H = This->sH;
207 :     const int rho = 3-This->accuracy;
208 :     const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
209 : chl 1077
210 :    
211 : syskin 1085 int32_t uo = This->Uo + (x<<8); // ((16*x)<<4)
212 :     int32_t vo = This->Vo + (y<<8);
213 :     const uint32_t ri = MTab[uo & 15];
214 :     const uint32_t rj = MTab[vo & 15];
215 :     int i, j;
216 : chl 1077
217 : syskin 1085 int32_t Offset;
218 :     if ((uint32_t)vo<=(uint32_t)H) Offset = (vo>>4)*srcstride;
219 :     else if (vo>H) Offset = ( H>>4)*srcstride;
220 :     else Offset =-16*srcstride;
221 :     if ((uint32_t)uo<=(uint32_t)W) Offset += (uo>>4);
222 :     else if (uo>W) Offset += ( W>>4);
223 :     else Offset -= 16;
224 : chl 1077
225 : syskin 1085 Dst += 16;
226 : chl 1077
227 : syskin 1085 for(j=16; j>0; --j, Offset+=srcstride-16)
228 :     {
229 :     for(i=-16; i<0; ++i, ++Offset)
230 :     {
231 :     uint32_t f0, f1;
232 :     f0 = Src[ Offset +0 ];
233 :     f0 |= Src[ Offset +1 ] << 16;
234 :     f1 = Src[ Offset+srcstride +0 ];
235 :     f1 |= Src[ Offset+srcstride +1 ] << 16;
236 :     f0 = (ri*f0)>>16;
237 :     f1 = (ri*f1) & 0x0fff0000;
238 :     f0 |= f1;
239 :     f0 = ( rj*f0 + Rounder ) >> 24;
240 :     Dst[i] = (uint8_t)f0;
241 :     }
242 :     Dst += dststride;
243 :     }
244 :     }
245 : chl 1077
246 :     void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This,
247 : syskin 1085 uint8_t *uDst, const uint8_t *uSrc,
248 :     uint8_t *vDst, const uint8_t *vSrc,
249 :     int dststride, int srcstride, int x, int y, int rounding)
250 : chl 1077 {
251 : syskin 1085 const int W = This->sW >> 1;
252 :     const int H = This->sH >> 1;
253 :     const int rho = 3-This->accuracy;
254 :     const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
255 : chl 1077
256 : syskin 1085 int32_t uo = This->Uco + (x<<7);
257 :     int32_t vo = This->Vco + (y<<7);
258 :     const uint32_t rri = MTab[uo & 15];
259 :     const uint32_t rrj = MTab[vo & 15];
260 :     int i, j;
261 : chl 1077
262 : syskin 1085 int32_t Offset;
263 :     if ((uint32_t)vo<=(uint32_t)H) Offset = (vo>>4)*srcstride;
264 :     else if (vo>H) Offset = ( H>>4)*srcstride;
265 :     else Offset =-8*srcstride;
266 :     if ((uint32_t)uo<=(uint32_t)W) Offset += (uo>>4);
267 :     else if (uo>W) Offset += (W>>4);
268 :     else Offset -= 8;
269 : chl 1077
270 : syskin 1085 uDst += 8;
271 :     vDst += 8;
272 :     for(j=8; j>0; --j, Offset+=srcstride-8)
273 :     {
274 :     for(i=-8; i<0; ++i, Offset++)
275 :     {
276 :     uint32_t f0, f1;
277 :     f0 = uSrc[ Offset + 0 ];
278 :     f0 |= uSrc[ Offset + 1 ] << 16;
279 :     f1 = uSrc[ Offset + srcstride + 0 ];
280 :     f1 |= uSrc[ Offset + srcstride + 1 ] << 16;
281 :     f0 = (rri*f0)>>16;
282 :     f1 = (rri*f1) & 0x0fff0000;
283 :     f0 |= f1;
284 :     f0 = ( rrj*f0 + Rounder ) >> 24;
285 :     uDst[i] = (uint8_t)f0;
286 : chl 1077
287 : syskin 1085 f0 = vSrc[ Offset + 0 ];
288 :     f0 |= vSrc[ Offset + 1 ] << 16;
289 :     f1 = vSrc[ Offset + srcstride + 0 ];
290 :     f1 |= vSrc[ Offset + srcstride + 1 ] << 16;
291 :     f0 = (rri*f0)>>16;
292 :     f1 = (rri*f1) & 0x0fff0000;
293 :     f0 |= f1;
294 :     f0 = ( rrj*f0 + Rounder ) >> 24;
295 :     vDst[i] = (uint8_t)f0;
296 :     }
297 :     uDst += dststride;
298 :     vDst += dststride;
299 :     }
300 : chl 1077 }
301 :    
302 : syskin 1085 void get_average_mv_1pt_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
303 :     int x, int y, int qpel)
304 : chl 1077 {
305 : syskin 1085 mv->x = RSHIFT(Dsp->Uo<<qpel, 3);
306 :     mv->y = RSHIFT(Dsp->Vo<<qpel, 3);
307 : chl 1077 }
308 :    
309 :     //////////////////////////////////////////////////////////
310 :    
311 :    
312 : syskin 1085 // Warning! It's Accuracy being passed, not 'resolution'!
313 : chl 1077
314 :     void generate_GMCparameters( int nb_pts, const int accuracy,
315 : syskin 1085 const WARPPOINTS *const pts,
316 :     const int width, const int height,
317 :     NEW_GMC_DATA *const gmc)
318 : chl 1077 {
319 : syskin 1085 gmc->sW = width << 4;
320 :     gmc->sH = height << 4;
321 :     gmc->accuracy = accuracy;
322 :     gmc->num_wp = nb_pts;
323 : chl 1077
324 : syskin 1085 // reduce the number of points, if possible
325 :     if (nb_pts<3 || (pts->duv[2].x==-pts->duv[1].y && pts->duv[2].y==pts->duv[1].x)) {
326 :     if (nb_pts<2 || (pts->duv[1].x==0 && pts->duv[1].y==0)) {
327 :     if (nb_pts<1 || (pts->duv[0].x==0 && pts->duv[0].y==0)) {
328 :     nb_pts = 0;
329 :     }
330 :     else nb_pts = 1;
331 :     }
332 :     else nb_pts = 2;
333 :     }
334 :     else nb_pts = 3;
335 :    
336 :     // now, nb_pts stores the actual number of points required for interpolation
337 : chl 1077
338 : syskin 1085 if (nb_pts<=1)
339 :     {
340 :     if (nb_pts==1) {
341 :     // store as 4b fixed point
342 :     gmc->Uo = pts->duv[0].x << accuracy;
343 :     gmc->Vo = pts->duv[0].y << accuracy;
344 :     gmc->Uco = ((pts->duv[0].x>>1) | (pts->duv[0].x&1)) << accuracy; // DIV2RND()
345 :     gmc->Vco = ((pts->duv[0].y>>1) | (pts->duv[0].y&1)) << accuracy; // DIV2RND()
346 :     }
347 :     else { // zero points?!
348 :     gmc->Uo = gmc->Vo = 0;
349 :     gmc->Uco = gmc->Vco = 0;
350 :     }
351 : chl 1077
352 : syskin 1085 gmc->predict_16x16 = Predict_1pt_16x16_C;
353 :     gmc->predict_8x8 = Predict_1pt_8x8_C;
354 :     gmc->get_average_mv = get_average_mv_1pt_C;
355 :     }
356 :     else { // 2 or 3 points
357 :     const int rho = 3 - accuracy; // = {3,2,1,0} for Acc={0,1,2,3}
358 :     int Alpha = log2bin(width-1);
359 :     int Ws = 1 << Alpha;
360 : chl 1077
361 : syskin 1085 gmc->dU[0] = 16*Ws + RDIV( 8*Ws*pts->duv[1].x, width ); // dU/dx
362 :     gmc->dV[0] = RDIV( 8*Ws*pts->duv[1].y, width ); // dV/dx
363 : chl 1077
364 : syskin 1085 /* disabled, because possibly buggy? */
365 : chl 1077
366 :     /* if (nb_pts==2) {
367 : syskin 1085 gmc->dU[1] = -gmc->dV[0]; // -Sin
368 :     gmc->dV[1] = gmc->dU[0] ; // Cos
369 :     }
370 :     else */
371 : chl 1077 {
372 : syskin 1085 const int Beta = log2bin(height-1);
373 :     const int Hs = 1<<Beta;
374 :     gmc->dU[1] = RDIV( 8*Hs*pts->duv[2].x, height ); // dU/dy
375 :     gmc->dV[1] = 16*Hs + RDIV( 8*Hs*pts->duv[2].y, height ); // dV/dy
376 :     if (Beta>Alpha) {
377 :     gmc->dU[0] <<= (Beta-Alpha);
378 :     gmc->dV[0] <<= (Beta-Alpha);
379 :     Alpha = Beta;
380 :     Ws = Hs;
381 :     }
382 :     else {
383 :     gmc->dU[1] <<= Alpha - Beta;
384 :     gmc->dV[1] <<= Alpha - Beta;
385 :     }
386 :     }
387 :     // upscale to 16b fixed-point
388 :     gmc->dU[0] <<= (16-Alpha - rho);
389 :     gmc->dU[1] <<= (16-Alpha - rho);
390 :     gmc->dV[0] <<= (16-Alpha - rho);
391 :     gmc->dV[1] <<= (16-Alpha - rho);
392 : chl 1077
393 : syskin 1085 gmc->Uo = ( pts->duv[0].x <<(16+ accuracy)) + (1<<15);
394 :     gmc->Vo = ( pts->duv[0].y <<(16+ accuracy)) + (1<<15);
395 :     gmc->Uco = ((pts->duv[0].x-1)<<(17+ accuracy)) + (1<<17);
396 :     gmc->Vco = ((pts->duv[0].y-1)<<(17+ accuracy)) + (1<<17);
397 :     gmc->Uco = (gmc->Uco + gmc->dU[0] + gmc->dU[1])>>2;
398 :     gmc->Vco = (gmc->Vco + gmc->dV[0] + gmc->dV[1])>>2;
399 : chl 1077
400 : syskin 1085 gmc->predict_16x16 = Predict_16x16_C;
401 :     gmc->predict_8x8 = Predict_8x8_C;
402 :     gmc->get_average_mv = get_average_mv_C;
403 :     }
404 : chl 1077 }
405 :    
406 :     //////////////////////////////////////////////////////////
407 :    
408 :     /* quick and dirty routine to generate the full warped image (pGMC != NULL)
409 :     or just all average Motion Vectors (pGMC == NULL) */
410 :    
411 :     void
412 :     generate_GMCimage( const NEW_GMC_DATA *const gmc_data, // [input] precalculated data
413 :     const IMAGE *const pRef, // [input]
414 :     const int mb_width,
415 :     const int mb_height,
416 :     const int stride,
417 :     const int stride2,
418 :     const int fcode, // [input] some parameters...
419 : syskin 1085 const int32_t quarterpel, // [input] for rounding avgMV
420 : chl 1077 const int reduced_resolution, // [input] ignored
421 :     const int32_t rounding, // [input] for rounding image data
422 :     MACROBLOCK *const pMBs, // [output] average motion vectors
423 :     IMAGE *const pGMC) // [output] full warped image
424 :     {
425 :    
426 :     unsigned int mj,mi;
427 :     VECTOR avgMV;
428 :    
429 :     for (mj = 0; mj < (unsigned int)mb_height; mj++)
430 :     for (mi = 0; mi < (unsigned int)mb_width; mi++) {
431 :     const int mbnum = mj*mb_width+mi;
432 :     if (pGMC)
433 :     {
434 :     gmc_data->predict_16x16(gmc_data,
435 :     pGMC->y + mj*16*stride + mi*16, pRef->y,
436 :     stride, stride, mi, mj, rounding);
437 :    
438 :     gmc_data->predict_8x8(gmc_data,
439 :     pGMC->u + mj*8*stride2 + mi*8, pRef->u,
440 :     pGMC->v + mj*8*stride2 + mi*8, pRef->v,
441 :     stride2, stride2, mi, mj, rounding);
442 :     }
443 :     gmc_data->get_average_mv(gmc_data, &avgMV, mi, mj, quarterpel);
444 :    
445 :     pMBs[mbnum].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode);
446 :     pMBs[mbnum].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode);
447 :    
448 :     pMBs[mbnum].mcsel = 0; /* until mode decision */
449 :     }
450 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4