1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Prediction functions - |
* - Prediction module - |
5 |
* |
* |
6 |
* Copyright(C) 2001-2002 - Michael Militzer <isibaar@xvid.org> |
* Copyright (C) 2001-2003 Michael Militzer <isibaar@xvid.org> |
7 |
* Copyright(C) 2001-2002 - Peter Ross <pross@xvid.org> |
* 2001-2003 Peter Ross <pross@xvid.org> |
|
* |
|
|
* This program is an implementation of a part of one or more MPEG-4 |
|
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
|
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
10 |
* 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 |
20 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: mbprediction.c,v 1.9 2002-09-21 03:07:56 suxen_drol Exp $ |
* $Id: mbprediction.c,v 1.13.2.8 2003-09-10 22:19:00 edgomez Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
27 |
|
#include <stdlib.h> |
28 |
|
|
29 |
|
#include "../global.h" |
30 |
#include "../encoder.h" |
#include "../encoder.h" |
31 |
#include "mbprediction.h" |
#include "mbprediction.h" |
32 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
33 |
#include "../bitstream/cbp.h" |
#include "../bitstream/cbp.h" |
34 |
|
#include "../bitstream/mbcoding.h" |
35 |
|
#include "../bitstream/zigzag.h" |
36 |
|
|
37 |
|
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
|
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
|
|
|
|
|
|
|
|
/***************************************************************************** |
|
|
* Local inlined function |
|
|
****************************************************************************/ |
|
|
|
|
38 |
static int __inline |
static int __inline |
39 |
rescale(int predict_quant, |
rescale(int predict_quant, |
40 |
int current_quant, |
int current_quant, |
45 |
} |
} |
46 |
|
|
47 |
|
|
|
/***************************************************************************** |
|
|
* Local data |
|
|
****************************************************************************/ |
|
|
|
|
48 |
static const int16_t default_acdc_values[15] = { |
static const int16_t default_acdc_values[15] = { |
49 |
1024, |
1024, |
50 |
0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, |
52 |
}; |
}; |
53 |
|
|
54 |
|
|
|
/***************************************************************************** |
|
|
* Functions |
|
|
****************************************************************************/ |
|
|
|
|
|
|
|
55 |
/* get dc/ac prediction direction for a single block and place |
/* get dc/ac prediction direction for a single block and place |
56 |
predictor values into MB->pred_values[j][..] |
predictor values into MB->pred_values[j][..] |
57 |
*/ |
*/ |
80 |
const int16_t *pTop = default_acdc_values; |
const int16_t *pTop = default_acdc_values; |
81 |
const int16_t *pDiag = default_acdc_values; |
const int16_t *pDiag = default_acdc_values; |
82 |
|
|
83 |
uint32_t index = x + y * mb_width; // current macroblock |
uint32_t index = x + y * mb_width; /* current macroblock */ |
84 |
int *acpred_direction = &pMBs[index].acpred_directions[block]; |
int *acpred_direction = &pMBs[index].acpred_directions[block]; |
85 |
uint32_t i; |
uint32_t i; |
86 |
|
|
87 |
left = top = diag = current = 0; |
left = top = diag = current = 0; |
88 |
|
|
89 |
// grab left,top and diag macroblocks |
/* grab left,top and diag macroblocks */ |
90 |
|
|
91 |
// left macroblock |
/* left macroblock */ |
92 |
|
|
93 |
if (x && mbpos >= bound + 1 && |
if (x && mbpos >= bound + 1 && |
94 |
(pMBs[index - 1].mode == MODE_INTRA || |
(pMBs[index - 1].mode == MODE_INTRA || |
96 |
|
|
97 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
98 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
|
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
|
99 |
} |
} |
100 |
// top macroblock |
/* top macroblock */ |
101 |
|
|
102 |
if (mbpos >= bound + (int)mb_width && |
if (mbpos >= bound + (int)mb_width && |
103 |
(pMBs[index - mb_width].mode == MODE_INTRA || |
(pMBs[index - mb_width].mode == MODE_INTRA || |
106 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
107 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
108 |
} |
} |
109 |
// diag macroblock |
/* diag macroblock */ |
110 |
|
|
111 |
if (x && mbpos >= bound + (int)mb_width + 1 && |
if (x && mbpos >= bound + (int)mb_width + 1 && |
112 |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
117 |
|
|
118 |
current = pMBs[index].pred_values[0]; |
current = pMBs[index].pred_values[0]; |
119 |
|
|
120 |
// now grab pLeft, pTop, pDiag _blocks_ |
/* now grab pLeft, pTop, pDiag _blocks_ */ |
121 |
|
|
122 |
switch (block) { |
switch (block) { |
123 |
|
|
184 |
break; |
break; |
185 |
} |
} |
186 |
|
|
187 |
// determine ac prediction direction & ac/dc predictor |
/* |
188 |
// place rescaled ac/dc predictions into predictors[] for later use |
* determine ac prediction direction & ac/dc predictor place rescaled ac/dc |
189 |
|
* predictions into predictors[] for later use |
190 |
|
*/ |
191 |
|
|
192 |
if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) { |
193 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; /* vertical */ |
194 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
195 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
196 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
197 |
} |
} |
198 |
} else { |
} else { |
199 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; /* horizontal */ |
200 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
201 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
202 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
221 |
int16_t *pCurrent = pMB->pred_values[block]; |
int16_t *pCurrent = pMB->pred_values[block]; |
222 |
uint32_t i; |
uint32_t i; |
223 |
|
|
224 |
DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]); |
DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]); |
225 |
|
|
226 |
dct_codes[0] += predictors[0]; // dc prediction |
dct_codes[0] += predictors[0]; /* dc prediction */ |
227 |
pCurrent[0] = dct_codes[0] * iDcScaler; |
pCurrent[0] = dct_codes[0] * iDcScaler; |
228 |
|
|
229 |
if (acpred_direction == 1) { |
if (acpred_direction == 1) { |
230 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
231 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
232 |
|
|
233 |
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]); |
DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i, predictors[i]); |
234 |
|
|
235 |
dct_codes[i] = level; |
dct_codes[i] = level; |
236 |
pCurrent[i] = level; |
pCurrent[i] = level; |
239 |
} else if (acpred_direction == 2) { |
} else if (acpred_direction == 2) { |
240 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
241 |
int level = dct_codes[i * 8] + predictors[i]; |
int level = dct_codes[i * 8] + predictors[i]; |
242 |
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]); |
DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i*8, predictors[i]); |
243 |
|
|
244 |
dct_codes[i * 8] = level; |
dct_codes[i * 8] = level; |
245 |
pCurrent[i + 7] = level; |
pCurrent[i + 7] = level; |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
// ****************************************************************** |
/***************************************************************************** |
259 |
// ****************************************************************** |
****************************************************************************/ |
260 |
|
|
261 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
262 |
|
|
263 |
todo: perform [-127,127] clamping after prediction |
returns sum of coeefficients *saved* if prediction is enabled |
|
clamping must adjust the coeffs, so dequant is done correctly |
|
264 |
|
|
|
S1/S2 are used to determine if its worth predicting for AC |
|
265 |
S1 = sum of all (qcoeff - prediction) |
S1 = sum of all (qcoeff - prediction) |
266 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
267 |
*/ |
*/ |
268 |
|
|
269 |
uint32_t |
int |
270 |
calc_acdc(MACROBLOCK * pMB, |
calc_acdc_coeff(MACROBLOCK * pMB, |
271 |
uint32_t block, |
uint32_t block, |
272 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
273 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
275 |
{ |
{ |
276 |
int16_t *pCurrent = pMB->pred_values[block]; |
int16_t *pCurrent = pMB->pred_values[block]; |
277 |
uint32_t i; |
uint32_t i; |
278 |
uint32_t S1 = 0, S2 = 0; |
int S1 = 0, S2 = 0; |
279 |
|
|
280 |
|
|
281 |
/* store current coeffs to pred_values[] for future prediction */ |
/* store current coeffs to pred_values[] for future prediction */ |
295 |
int16_t level; |
int16_t level; |
296 |
|
|
297 |
level = qcoeff[i]; |
level = qcoeff[i]; |
298 |
S2 += ABS(level); |
S2 += abs(level); |
299 |
level -= predictors[i]; |
level -= predictors[i]; |
300 |
S1 += ABS(level); |
S1 += abs(level); |
301 |
predictors[i] = level; |
predictors[i] = level; |
302 |
} |
} |
303 |
} else // acpred_direction == 2 |
} else /* acpred_direction == 2 */ |
304 |
{ |
{ |
305 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
306 |
int16_t level; |
int16_t level; |
307 |
|
|
308 |
level = qcoeff[i * 8]; |
level = qcoeff[i * 8]; |
309 |
S2 += ABS(level); |
S2 += abs(level); |
310 |
level -= predictors[i]; |
level -= predictors[i]; |
311 |
S1 += ABS(level); |
S1 += abs(level); |
312 |
predictors[i] = level; |
predictors[i] = level; |
313 |
} |
} |
314 |
|
|
319 |
} |
} |
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
/* returns the bits *saved* if prediction is enabled */ |
324 |
|
|
325 |
|
int |
326 |
|
calc_acdc_bits(MACROBLOCK * pMB, |
327 |
|
uint32_t block, |
328 |
|
int16_t qcoeff[64], |
329 |
|
uint32_t iDcScaler, |
330 |
|
int16_t predictors[8]) |
331 |
|
{ |
332 |
|
const int direction = pMB->acpred_directions[block]; |
333 |
|
int16_t *pCurrent = pMB->pred_values[block]; |
334 |
|
int16_t tmp[8]; |
335 |
|
unsigned int i; |
336 |
|
int Z1, Z2; |
337 |
|
|
338 |
|
/* store current coeffs to pred_values[] for future prediction */ |
339 |
|
pCurrent[0] = qcoeff[0] * iDcScaler; |
340 |
|
for (i = 1; i < 8; i++) { |
341 |
|
pCurrent[i] = qcoeff[i]; |
342 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
343 |
|
} |
344 |
|
|
345 |
|
|
346 |
|
/* dc prediction */ |
347 |
|
qcoeff[0] = qcoeff[0] - predictors[0]; |
348 |
|
|
349 |
|
/* calc cost before ac prediction */ |
350 |
|
#ifdef BIGLUT |
351 |
|
Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1); |
352 |
|
#else |
353 |
|
Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
354 |
|
#endif |
355 |
|
|
356 |
|
/* apply ac prediction & calc cost*/ |
357 |
|
if (direction == 1) { |
358 |
|
for (i = 1; i < 8; i++) { |
359 |
|
tmp[i] = qcoeff[i]; |
360 |
|
qcoeff[i] -= predictors[i]; |
361 |
|
predictors[i] = qcoeff[i]; |
362 |
|
} |
363 |
|
}else{ /* acpred_direction == 2 */ |
364 |
|
for (i = 1; i < 8; i++) { |
365 |
|
tmp[i] = qcoeff[i*8]; |
366 |
|
qcoeff[i*8] -= predictors[i]; |
367 |
|
predictors[i] = qcoeff[i*8]; |
368 |
|
} |
369 |
|
} |
370 |
|
|
371 |
|
#ifdef BIGLUT |
372 |
|
Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1); |
373 |
|
#else |
374 |
|
Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
375 |
|
#endif |
376 |
|
|
377 |
|
/* undo prediction */ |
378 |
|
if (direction == 1) { |
379 |
|
for (i = 1; i < 8; i++) |
380 |
|
qcoeff[i] = tmp[i]; |
381 |
|
}else{ /* acpred_direction == 2 */ |
382 |
|
for (i = 1; i < 8; i++) |
383 |
|
qcoeff[i*8] = tmp[i]; |
384 |
|
} |
385 |
|
|
386 |
|
return Z2-Z1; |
387 |
|
} |
388 |
|
|
389 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
390 |
|
|
391 |
void |
void |
394 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
395 |
int16_t predictors[8]) |
int16_t predictors[8]) |
396 |
{ |
{ |
397 |
uint32_t i; |
unsigned int i; |
398 |
|
|
399 |
if (pMB->acpred_directions[block] == 1) { |
if (pMB->acpred_directions[block] == 1) { |
400 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) |
401 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
|
} |
|
402 |
} else { |
} else { |
403 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) |
404 |
qcoeff[i * 8] = predictors[i]; |
qcoeff[i * 8] = predictors[i]; |
405 |
} |
} |
406 |
} |
} |
|
} |
|
407 |
|
|
408 |
|
|
409 |
void |
void |
415 |
{ |
{ |
416 |
|
|
417 |
int32_t j; |
int32_t j; |
418 |
int32_t iDcScaler, iQuant = frame->quant; |
int32_t iDcScaler, iQuant; |
419 |
int32_t S = 0; |
int S = 0; |
420 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
421 |
|
|
422 |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
423 |
|
iQuant = pMB->quant; |
424 |
|
|
425 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
426 |
|
|
427 |
for (j = 0; j < 6; j++) { |
for (j = 0; j < 6; j++) { |
428 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, j<4); |
429 |
|
|
430 |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
431 |
iQuant, iDcScaler, predictors[j], 0); |
iQuant, iDcScaler, predictors[j], 0); |
432 |
|
|
433 |
S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
if ((frame->vop_flags & XVID_VOP_HQACPRED)) |
434 |
|
S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
435 |
|
else |
436 |
|
S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
437 |
|
|
438 |
} |
} |
439 |
|
|
440 |
if (S < 0) // dont predict |
if (S<=0) { /* dont predict */ |
441 |
{ |
for (j = 0; j < 6; j++) |
|
for (j = 0; j < 6; j++) { |
|
442 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
|
} |
|
443 |
} else { |
} else { |
444 |
for (j = 0; j < 6; j++) { |
for (j = 0; j < 6; j++) |
445 |
apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]); |
446 |
} |
} |
447 |
} |
|
448 |
pMB->cbp = calc_cbp(qcoeff); |
pMB->cbp = calc_cbp(qcoeff); |
449 |
} |
} |
450 |
|
} |
451 |
|
|
452 |
|
static const VECTOR zeroMV = { 0, 0 }; |
453 |
|
|
454 |
|
VECTOR |
455 |
|
get_pmv2(const MACROBLOCK * const mbs, |
456 |
|
const int mb_width, |
457 |
|
const int bound, |
458 |
|
const int x, |
459 |
|
const int y, |
460 |
|
const int block) |
461 |
|
{ |
462 |
|
int lx, ly, lz; /* left */ |
463 |
|
int tx, ty, tz; /* top */ |
464 |
|
int rx, ry, rz; /* top-right */ |
465 |
|
int lpos, tpos, rpos; |
466 |
|
int num_cand = 0, last_cand = 1; |
467 |
|
|
468 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
469 |
|
|
470 |
|
switch (block) { |
471 |
|
case 0: |
472 |
|
lx = x - 1; ly = y; lz = 1; |
473 |
|
tx = x; ty = y - 1; tz = 2; |
474 |
|
rx = x + 1; ry = y - 1; rz = 2; |
475 |
|
break; |
476 |
|
case 1: |
477 |
|
lx = x; ly = y; lz = 0; |
478 |
|
tx = x; ty = y - 1; tz = 3; |
479 |
|
rx = x + 1; ry = y - 1; rz = 2; |
480 |
|
break; |
481 |
|
case 2: |
482 |
|
lx = x - 1; ly = y; lz = 3; |
483 |
|
tx = x; ty = y; tz = 0; |
484 |
|
rx = x; ry = y; rz = 1; |
485 |
|
break; |
486 |
|
default: |
487 |
|
lx = x; ly = y; lz = 2; |
488 |
|
tx = x; ty = y; tz = 0; |
489 |
|
rx = x; ry = y; rz = 1; |
490 |
|
} |
491 |
|
|
492 |
|
lpos = lx + ly * mb_width; |
493 |
|
rpos = rx + ry * mb_width; |
494 |
|
tpos = tx + ty * mb_width; |
495 |
|
|
496 |
|
if (lpos >= bound && lx >= 0) { |
497 |
|
num_cand++; |
498 |
|
pmv[1] = mbs[lpos].mvs[lz]; |
499 |
|
} else pmv[1] = zeroMV; |
500 |
|
|
501 |
|
if (tpos >= bound) { |
502 |
|
num_cand++; |
503 |
|
last_cand = 2; |
504 |
|
pmv[2] = mbs[tpos].mvs[tz]; |
505 |
|
} else pmv[2] = zeroMV; |
506 |
|
|
507 |
|
if (rpos >= bound && rx < mb_width) { |
508 |
|
num_cand++; |
509 |
|
last_cand = 3; |
510 |
|
pmv[3] = mbs[rpos].mvs[rz]; |
511 |
|
} else pmv[3] = zeroMV; |
512 |
|
|
513 |
|
/* If there're more than one candidate, we return the median vector */ |
514 |
|
|
515 |
|
if (num_cand > 1) { |
516 |
|
/* set median */ |
517 |
|
pmv[0].x = |
518 |
|
MIN(MAX(pmv[1].x, pmv[2].x), |
519 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
520 |
|
pmv[0].y = |
521 |
|
MIN(MAX(pmv[1].y, pmv[2].y), |
522 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
523 |
|
return pmv[0]; |
524 |
|
} |
525 |
|
|
526 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
527 |
|
} |
528 |
|
|
529 |
|
VECTOR |
530 |
|
get_qpmv2(const MACROBLOCK * const mbs, |
531 |
|
const int mb_width, |
532 |
|
const int bound, |
533 |
|
const int x, |
534 |
|
const int y, |
535 |
|
const int block) |
536 |
|
{ |
537 |
|
int lx, ly, lz; /* left */ |
538 |
|
int tx, ty, tz; /* top */ |
539 |
|
int rx, ry, rz; /* top-right */ |
540 |
|
int lpos, tpos, rpos; |
541 |
|
int num_cand = 0, last_cand = 1; |
542 |
|
|
543 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
544 |
|
|
545 |
|
switch (block) { |
546 |
|
case 0: |
547 |
|
lx = x - 1; ly = y; lz = 1; |
548 |
|
tx = x; ty = y - 1; tz = 2; |
549 |
|
rx = x + 1; ry = y - 1; rz = 2; |
550 |
|
break; |
551 |
|
case 1: |
552 |
|
lx = x; ly = y; lz = 0; |
553 |
|
tx = x; ty = y - 1; tz = 3; |
554 |
|
rx = x + 1; ry = y - 1; rz = 2; |
555 |
|
break; |
556 |
|
case 2: |
557 |
|
lx = x - 1; ly = y; lz = 3; |
558 |
|
tx = x; ty = y; tz = 0; |
559 |
|
rx = x; ry = y; rz = 1; |
560 |
|
break; |
561 |
|
default: |
562 |
|
lx = x; ly = y; lz = 2; |
563 |
|
tx = x; ty = y; tz = 0; |
564 |
|
rx = x; ry = y; rz = 1; |
565 |
|
} |
566 |
|
|
567 |
|
lpos = lx + ly * mb_width; |
568 |
|
rpos = rx + ry * mb_width; |
569 |
|
tpos = tx + ty * mb_width; |
570 |
|
|
571 |
|
if (lpos >= bound && lx >= 0) { |
572 |
|
num_cand++; |
573 |
|
pmv[1] = mbs[lpos].qmvs[lz]; |
574 |
|
} else pmv[1] = zeroMV; |
575 |
|
|
576 |
|
if (tpos >= bound) { |
577 |
|
num_cand++; |
578 |
|
last_cand = 2; |
579 |
|
pmv[2] = mbs[tpos].qmvs[tz]; |
580 |
|
} else pmv[2] = zeroMV; |
581 |
|
|
582 |
|
if (rpos >= bound && rx < mb_width) { |
583 |
|
num_cand++; |
584 |
|
last_cand = 3; |
585 |
|
pmv[3] = mbs[rpos].qmvs[rz]; |
586 |
|
} else pmv[3] = zeroMV; |
587 |
|
|
588 |
|
/* If there're more than one candidate, we return the median vector */ |
589 |
|
|
590 |
|
if (num_cand > 1) { |
591 |
|
/* set median */ |
592 |
|
pmv[0].x = |
593 |
|
MIN(MAX(pmv[1].x, pmv[2].x), |
594 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
595 |
|
pmv[0].y = |
596 |
|
MIN(MAX(pmv[1].y, pmv[2].y), |
597 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
598 |
|
return pmv[0]; |
599 |
|
} |
600 |
|
|
601 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
602 |
} |
} |