1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* GMC interpolation module |
* - GMC interpolation module - |
5 |
|
* |
6 |
|
* Copyright(C) 2002-2003 Pascal Massimino <skal@planet-d.net> |
7 |
* |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
17 |
* |
* |
18 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
|
* |
22 |
|
* $Id$ |
23 |
* |
* |
24 |
*************************************************************************/ |
****************************************************************************/ |
25 |
|
|
26 |
#include "../portab.h" |
#include "../portab.h" |
27 |
#include "../global.h" |
#include "../global.h" |
30 |
|
|
31 |
#include <stdio.h> |
#include <stdio.h> |
32 |
|
|
33 |
/* These are mainly the new GMC routines by -Skal- (C) 2003 */ |
/* ************************************************************ |
34 |
|
* Pts = 2 or 3 |
35 |
////////////////////////////////////////////////////////// |
* |
36 |
// Pts = 2 or 3 |
* Warning! *src is the global frame pointer (that is: adress |
37 |
|
* of pixel 0,0), not the macroblock one. |
38 |
// Warning! *src is the global frame pointer (that is: adress |
* Conversely, *dst is the macroblock top-left adress. |
39 |
// of pixel 0,0), not the macroblock one. |
*/ |
|
// Conversely, *dst is the macroblock top-left adress. |
|
|
|
|
40 |
|
|
41 |
void Predict_16x16_C(const NEW_GMC_DATA * const This, |
void Predict_16x16_C(const NEW_GMC_DATA * const This, |
42 |
uint8_t *dst, const uint8_t *src, |
uint8_t *dst, const uint8_t *src, |
58 |
int i, j; |
int i, j; |
59 |
|
|
60 |
dst += 16; |
dst += 16; |
61 |
for (j=16; j>0; --j) |
for (j=16; j>0; --j) { |
|
{ |
|
62 |
int U = Uo, V = Vo; |
int U = Uo, V = Vo; |
63 |
Uo += dUy; Vo += dVy; |
Uo += dUy; Vo += dVy; |
64 |
for (i=-16; i<0; ++i) |
for (i=-16; i<0; ++i) { |
65 |
{ |
unsigned int f0, f1, ri = 16, rj = 16; |
|
unsigned int f0, f1, ri, rj; |
|
66 |
int Offset; |
int Offset; |
|
|
|
67 |
int u = ( U >> 16 ) << rho; |
int u = ( U >> 16 ) << rho; |
68 |
int v = ( V >> 16 ) << rho; |
int v = ( V >> 16 ) << rho; |
69 |
|
|
70 |
U += dUx; V += dVx; |
U += dUx; V += dVx; |
71 |
|
|
72 |
ri = 16; |
if (u > 0 && u <= W) { ri = MTab[u&15]; Offset = u>>4; } |
|
if ((uint32_t)u<=(uint32_t)W) { ri = MTab[u&15]; Offset = u>>4; } |
|
73 |
else if (u>W) Offset = W>>4; |
else if (u>W) Offset = W>>4; |
74 |
else Offset = -1; |
else Offset = -1; |
75 |
|
|
76 |
rj = 16; |
if (v > 0 && v <= H) { rj = MTab[v&15]; Offset += (v>>4)*srcstride; } |
|
if ((uint32_t)v<=(uint32_t)H) { rj = MTab[v&15]; Offset += (v>>4)*srcstride; } |
|
77 |
else if (v>H) Offset += (H>>4)*srcstride; |
else if (v>H) Offset += (H>>4)*srcstride; |
78 |
else Offset -= srcstride; |
else Offset -= srcstride; |
79 |
|
|
92 |
} |
} |
93 |
} |
} |
94 |
|
|
|
|
|
95 |
void Predict_8x8_C(const NEW_GMC_DATA * const This, |
void Predict_8x8_C(const NEW_GMC_DATA * const This, |
96 |
uint8_t *uDst, const uint8_t *uSrc, |
uint8_t *uDst, const uint8_t *uSrc, |
97 |
uint8_t *vDst, const uint8_t *vSrc, |
uint8_t *vDst, const uint8_t *vSrc, |
114 |
|
|
115 |
uDst += 8; |
uDst += 8; |
116 |
vDst += 8; |
vDst += 8; |
117 |
for (j=8; j>0; --j) |
for (j=8; j>0; --j) { |
|
{ |
|
118 |
int32_t U = Uo, V = Vo; |
int32_t U = Uo, V = Vo; |
119 |
Uo += dUy; Vo += dVy; |
Uo += dUy; Vo += dVy; |
120 |
|
|
121 |
for (i=-8; i<0; ++i) |
for (i=-8; i<0; ++i) { |
|
{ |
|
122 |
int Offset; |
int Offset; |
123 |
uint32_t f0, f1, ri, rj; |
uint32_t f0, f1, ri, rj; |
124 |
int32_t u, v; |
int32_t u, v; |
127 |
v = ( V >> 16 ) << rho; |
v = ( V >> 16 ) << rho; |
128 |
U += dUx; V += dVx; |
U += dUx; V += dVx; |
129 |
|
|
130 |
if ((uint32_t)u<=(uint32_t)W) { |
if (u > 0 && u <= W) { |
131 |
ri = MTab[u&15]; |
ri = MTab[u&15]; |
132 |
Offset = u>>4; |
Offset = u>>4; |
133 |
} |
} else { |
|
else { |
|
134 |
ri = 16; |
ri = 16; |
135 |
if (u>W) Offset = W>>4; |
if (u>W) Offset = W>>4; |
136 |
else Offset = -1; |
else Offset = -1; |
137 |
} |
} |
138 |
if ((uint32_t)v<=(uint32_t)H) { |
|
139 |
|
if (v > 0 && v <= H) { |
140 |
rj = MTab[v&15]; |
rj = MTab[v&15]; |
141 |
Offset += (v>>4)*srcstride; |
Offset += (v>>4)*srcstride; |
142 |
} |
} else { |
|
else { |
|
143 |
rj = 16; |
rj = 16; |
144 |
if (v>H) Offset += (H>>4)*srcstride; |
if (v>H) Offset += (H>>4)*srcstride; |
145 |
else Offset -= srcstride; |
else Offset -= srcstride; |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
|
|
|
175 |
void get_average_mv_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv, |
void get_average_mv_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv, |
176 |
int x, int y, int qpel) |
int x, int y, int qpel) |
177 |
{ |
{ |
191 |
v = V >> 16; V += Dsp->dV[0]; vy += v; |
v = V >> 16; V += Dsp->dV[0]; vy += v; |
192 |
} |
} |
193 |
} |
} |
194 |
vx -= (256*x+120) << (5+Dsp->accuracy); // 120 = 15*16/2 |
vx -= (256*x+120) << (5+Dsp->accuracy); /* 120 = 15*16/2 */ |
195 |
vy -= (256*y+120) << (5+Dsp->accuracy); |
vy -= (256*y+120) << (5+Dsp->accuracy); |
196 |
|
|
197 |
mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel ); |
mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel ); |
198 |
mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel ); |
mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel ); |
199 |
} |
} |
200 |
|
|
201 |
////////////////////////////////////////////////////////// |
/* ************************************************************ |
202 |
// simplified version for 1 warp point |
* simplified version for 1 warp point |
203 |
|
*/ |
204 |
|
|
205 |
void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This, |
void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This, |
206 |
uint8_t *Dst, const uint8_t *Src, |
uint8_t *Dst, const uint8_t *Src, |
212 |
const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16; |
const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16; |
213 |
|
|
214 |
|
|
215 |
int32_t uo = This->Uo + (x<<8); // ((16*x)<<4) |
int32_t uo = This->Uo + (x<<8); /* ((16*x)<<4) */ |
216 |
int32_t vo = This->Vo + (y<<8); |
int32_t vo = This->Vo + (y<<8); |
217 |
const uint32_t ri = MTab[uo & 15]; |
const uint32_t ri = MTab[uo & 15]; |
218 |
const uint32_t rj = MTab[vo & 15]; |
const uint32_t rj = MTab[vo & 15]; |
247 |
} |
} |
248 |
} |
} |
249 |
|
|
|
|
|
250 |
void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This, |
void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This, |
251 |
uint8_t *uDst, const uint8_t *uSrc, |
uint8_t *uDst, const uint8_t *uSrc, |
252 |
uint8_t *vDst, const uint8_t *vSrc, |
uint8_t *vDst, const uint8_t *vSrc, |
303 |
} |
} |
304 |
} |
} |
305 |
|
|
|
|
|
306 |
void get_average_mv_1pt_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv, |
void get_average_mv_1pt_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv, |
307 |
int x, int y, int qpel) |
int x, int y, int qpel) |
308 |
{ |
{ |
310 |
mv->y = RSHIFT(Dsp->Vo<<qpel, 3); |
mv->y = RSHIFT(Dsp->Vo<<qpel, 3); |
311 |
} |
} |
312 |
|
|
313 |
////////////////////////////////////////////////////////// |
/* ************************************************************* |
314 |
|
* Warning! It's Accuracy being passed, not 'resolution'! |
315 |
|
*/ |
|
// Warning! It's Accuracy being passed, not 'resolution'! |
|
316 |
|
|
317 |
void generate_GMCparameters( int nb_pts, const int accuracy, |
void generate_GMCparameters( int nb_pts, const int accuracy, |
318 |
const WARPPOINTS *const pts, |
const WARPPOINTS *const pts, |
324 |
gmc->accuracy = accuracy; |
gmc->accuracy = accuracy; |
325 |
gmc->num_wp = nb_pts; |
gmc->num_wp = nb_pts; |
326 |
|
|
327 |
// reduce the number of points, if possible |
/* reduce the number of points, if possible */ |
328 |
if (nb_pts<3 || (pts->duv[2].x==-pts->duv[1].y && pts->duv[2].y==pts->duv[1].x)) { |
if (nb_pts<3 || (pts->duv[2].x==-pts->duv[1].y && pts->duv[2].y==pts->duv[1].x)) { |
329 |
if (nb_pts<2 || (pts->duv[1].x==0 && pts->duv[1].y==0)) { |
if (nb_pts<2 || (pts->duv[1].x==0 && pts->duv[1].y==0)) { |
330 |
if (nb_pts<1 || (pts->duv[0].x==0 && pts->duv[0].y==0)) { |
if (nb_pts<1 || (pts->duv[0].x==0 && pts->duv[0].y==0)) { |
336 |
} |
} |
337 |
else nb_pts = 3; |
else nb_pts = 3; |
338 |
|
|
339 |
// now, nb_pts stores the actual number of points required for interpolation |
/* now, nb_pts stores the actual number of points required for interpolation */ |
340 |
|
|
341 |
if (nb_pts<=1) |
if (nb_pts<=1) |
342 |
{ |
{ |
343 |
if (nb_pts==1) { |
if (nb_pts==1) { |
344 |
// store as 4b fixed point |
/* store as 4b fixed point */ |
345 |
gmc->Uo = pts->duv[0].x << accuracy; |
gmc->Uo = pts->duv[0].x << accuracy; |
346 |
gmc->Vo = pts->duv[0].y << accuracy; |
gmc->Vo = pts->duv[0].y << accuracy; |
347 |
gmc->Uco = ((pts->duv[0].x>>1) | (pts->duv[0].x&1)) << accuracy; // DIV2RND() |
gmc->Uco = ((pts->duv[0].x>>1) | (pts->duv[0].x&1)) << accuracy; /* DIV2RND() */ |
348 |
gmc->Vco = ((pts->duv[0].y>>1) | (pts->duv[0].y&1)) << accuracy; // DIV2RND() |
gmc->Vco = ((pts->duv[0].y>>1) | (pts->duv[0].y&1)) << accuracy; /* DIV2RND() */ |
349 |
} |
} |
350 |
else { // zero points?! |
else { /* zero points?! */ |
351 |
gmc->Uo = gmc->Vo = 0; |
gmc->Uo = gmc->Vo = 0; |
352 |
gmc->Uco = gmc->Vco = 0; |
gmc->Uco = gmc->Vco = 0; |
353 |
} |
} |
356 |
gmc->predict_8x8 = Predict_1pt_8x8_C; |
gmc->predict_8x8 = Predict_1pt_8x8_C; |
357 |
gmc->get_average_mv = get_average_mv_1pt_C; |
gmc->get_average_mv = get_average_mv_1pt_C; |
358 |
} |
} |
359 |
else { // 2 or 3 points |
else { /* 2 or 3 points */ |
360 |
const int rho = 3 - accuracy; // = {3,2,1,0} for Acc={0,1,2,3} |
const int rho = 3 - accuracy; /* = {3,2,1,0} for Acc={0,1,2,3} */ |
361 |
int Alpha = log2bin(width-1); |
int Alpha = log2bin(width-1); |
362 |
int Ws = 1 << Alpha; |
int Ws = 1 << Alpha; |
363 |
|
|
364 |
gmc->dU[0] = 16*Ws + RDIV( 8*Ws*pts->duv[1].x, width ); // dU/dx |
gmc->dU[0] = 16*Ws + RDIV( 8*Ws*pts->duv[1].x, width ); /* dU/dx */ |
365 |
gmc->dV[0] = RDIV( 8*Ws*pts->duv[1].y, width ); // dV/dx |
gmc->dV[0] = RDIV( 8*Ws*pts->duv[1].y, width ); /* dV/dx */ |
366 |
|
|
367 |
/* disabled, because possibly buggy? */ |
/* disabled, because possibly buggy? */ |
368 |
|
|
369 |
/* if (nb_pts==2) { |
#if 0 |
370 |
gmc->dU[1] = -gmc->dV[0]; // -Sin |
if (nb_pts==2) { |
371 |
gmc->dV[1] = gmc->dU[0] ; // Cos |
gmc->dU[1] = -gmc->dV[0]; /* -Sin */ |
372 |
|
gmc->dV[1] = gmc->dU[0] ; /* Cos */ |
373 |
} |
} |
374 |
else */ |
else |
375 |
|
#endif |
376 |
{ |
{ |
377 |
const int Beta = log2bin(height-1); |
const int Beta = log2bin(height-1); |
378 |
const int Hs = 1<<Beta; |
const int Hs = 1<<Beta; |
379 |
gmc->dU[1] = RDIV( 8*Hs*pts->duv[2].x, height ); // dU/dy |
gmc->dU[1] = RDIV( 8*Hs*pts->duv[2].x, height ); /* dU/dy */ |
380 |
gmc->dV[1] = 16*Hs + RDIV( 8*Hs*pts->duv[2].y, height ); // dV/dy |
gmc->dV[1] = 16*Hs + RDIV( 8*Hs*pts->duv[2].y, height ); /* dV/dy */ |
381 |
if (Beta>Alpha) { |
if (Beta>Alpha) { |
382 |
gmc->dU[0] <<= (Beta-Alpha); |
gmc->dU[0] <<= (Beta-Alpha); |
383 |
gmc->dV[0] <<= (Beta-Alpha); |
gmc->dV[0] <<= (Beta-Alpha); |
389 |
gmc->dV[1] <<= Alpha - Beta; |
gmc->dV[1] <<= Alpha - Beta; |
390 |
} |
} |
391 |
} |
} |
392 |
// upscale to 16b fixed-point |
/* upscale to 16b fixed-point */ |
393 |
gmc->dU[0] <<= (16-Alpha - rho); |
gmc->dU[0] <<= (16-Alpha - rho); |
394 |
gmc->dU[1] <<= (16-Alpha - rho); |
gmc->dU[1] <<= (16-Alpha - rho); |
395 |
gmc->dV[0] <<= (16-Alpha - rho); |
gmc->dV[0] <<= (16-Alpha - rho); |
408 |
} |
} |
409 |
} |
} |
410 |
|
|
411 |
////////////////////////////////////////////////////////// |
/* ******************************************************************* |
412 |
|
* quick and dirty routine to generate the full warped image |
413 |
|
* (pGMC != NULL) or just all average Motion Vectors (pGMC == NULL) */ |
|
/* quick and dirty routine to generate the full warped image (pGMC != NULL) |
|
|
or just all average Motion Vectors (pGMC == NULL) */ |
|
414 |
|
|
415 |
void |
void |
416 |
generate_GMCimage( const NEW_GMC_DATA *const gmc_data, // [input] precalculated data |
generate_GMCimage( const NEW_GMC_DATA *const gmc_data, /* [input] precalculated data */ |
417 |
const IMAGE *const pRef, // [input] |
const IMAGE *const pRef, /* [input] */ |
418 |
const int mb_width, |
const int mb_width, |
419 |
const int mb_height, |
const int mb_height, |
420 |
const int stride, |
const int stride, |
421 |
const int stride2, |
const int stride2, |
422 |
const int fcode, // [input] some parameters... |
const int fcode, /* [input] some parameters... */ |
423 |
const int32_t quarterpel, // [input] for rounding avgMV |
const int32_t quarterpel, /* [input] for rounding avgMV */ |
424 |
const int reduced_resolution, // [input] ignored |
const int reduced_resolution, /* [input] ignored */ |
425 |
const int32_t rounding, // [input] for rounding image data |
const int32_t rounding, /* [input] for rounding image data */ |
426 |
MACROBLOCK *const pMBs, // [output] average motion vectors |
MACROBLOCK *const pMBs, /* [output] average motion vectors */ |
427 |
IMAGE *const pGMC) // [output] full warped image |
IMAGE *const pGMC) /* [output] full warped image */ |
428 |
{ |
{ |
429 |
|
|
430 |
unsigned int mj,mi; |
unsigned int mj,mi; |