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> |
8 |
* |
* |
9 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* 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 |
|
* XviD is free software; you can redistribute it and/or modify it |
|
|
* under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
12 |
* (at your option) any later version. |
* (at your option) any later version. |
13 |
* |
* |
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 |
* |
* |
|
* Under section 8 of the GNU General Public License, the copyright |
|
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
23 |
* $Id$ |
* $Id$ |
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 |
*/ |
*/ |
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 = NULL; |
88 |
|
|
89 |
/* grab left,top and diag macroblocks */ |
/* grab left,top and diag macroblocks */ |
90 |
|
|
94 |
(pMBs[index - 1].mode == MODE_INTRA || |
(pMBs[index - 1].mode == MODE_INTRA || |
95 |
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
96 |
|
|
97 |
left = pMBs[index - 1].pred_values[0]; |
left = (int16_t*)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 |
|
|
103 |
(pMBs[index - mb_width].mode == MODE_INTRA || |
(pMBs[index - mb_width].mode == MODE_INTRA || |
104 |
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
105 |
|
|
106 |
top = pMBs[index - mb_width].pred_values[0]; |
top = (int16_t*)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 */ |
112 |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
113 |
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
114 |
|
|
115 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = (int16_t*)pMBs[index - 1 - mb_width].pred_values[0]; |
116 |
} |
} |
117 |
|
|
118 |
current = pMBs[index].pred_values[0]; |
current = (int16_t*)pMBs[index].pred_values[0]; |
119 |
|
|
120 |
/* now grab pLeft, pTop, pDiag _blocks_ */ |
/* now grab pLeft, pTop, pDiag _blocks_ */ |
121 |
|
|
184 |
break; |
break; |
185 |
} |
} |
186 |
|
|
187 |
/* determine ac prediction direction & ac/dc predictor */ |
/* determine ac prediction direction & ac/dc predictor place rescaled ac/dc |
188 |
/* place rescaled ac/dc predictions into predictors[] for later use */ |
* predictions into predictors[] for later use */ |
189 |
|
if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) { |
|
if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
|
190 |
*acpred_direction = 1; /* vertical */ |
*acpred_direction = 1; /* vertical */ |
191 |
predictors[0] = (int16_t)(DIV_DIV(pTop[0], iDcScaler)); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
192 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
193 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
194 |
} |
} |
195 |
} else { |
} else { |
196 |
*acpred_direction = 2; /* horizontal */ |
*acpred_direction = 2; /* horizontal */ |
197 |
predictors[0] = (int16_t)(DIV_DIV(pLeft[0], iDcScaler)); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
198 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
199 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
200 |
} |
} |
206 |
store current coeffs to pred_values[] for future prediction |
store current coeffs to pred_values[] for future prediction |
207 |
*/ |
*/ |
208 |
|
|
209 |
|
/* Up to this version, no DC clipping was performed, so we try to be backward |
210 |
|
* compatible to avoid artifacts */ |
211 |
|
#define BS_VERSION_BUGGY_DC_CLIPPING 34 |
212 |
|
|
213 |
void |
void |
214 |
add_acdc(MACROBLOCK * pMB, |
add_acdc(MACROBLOCK * pMB, |
215 |
uint32_t block, |
uint32_t block, |
216 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
217 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
218 |
int16_t predictors[8]) |
int16_t predictors[8], |
219 |
|
const int bsversion) |
220 |
{ |
{ |
221 |
uint8_t acpred_direction = pMB->acpred_directions[block]; |
uint8_t acpred_direction = pMB->acpred_directions[block]; |
222 |
int16_t *pCurrent = pMB->pred_values[block]; |
int16_t *pCurrent = (int16_t*)pMB->pred_values[block]; |
223 |
uint32_t i; |
uint32_t i; |
224 |
|
|
225 |
DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]); |
DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]); |
226 |
|
|
227 |
dct_codes[0] += predictors[0]; /* dc prediction */ |
dct_codes[0] += predictors[0]; /* dc prediction */ |
228 |
pCurrent[0] = (int16_t)(dct_codes[0] * iDcScaler); |
pCurrent[0] = dct_codes[0]*iDcScaler; |
229 |
|
if (bsversion > BS_VERSION_BUGGY_DC_CLIPPING) { |
230 |
|
pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); |
231 |
|
} |
232 |
|
|
233 |
if (acpred_direction == 1) { |
if (acpred_direction == 1) { |
234 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
235 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
236 |
|
|
237 |
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]); |
DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i, predictors[i]); |
238 |
|
|
239 |
dct_codes[i] = level; |
dct_codes[i] = level; |
240 |
pCurrent[i] = level; |
pCurrent[i] = level; |
243 |
} else if (acpred_direction == 2) { |
} else if (acpred_direction == 2) { |
244 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
245 |
int level = dct_codes[i * 8] + predictors[i]; |
int level = dct_codes[i * 8] + predictors[i]; |
246 |
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]); |
DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i*8, predictors[i]); |
247 |
|
|
248 |
dct_codes[i * 8] = level; |
dct_codes[i * 8] = level; |
249 |
pCurrent[i + 7] = level; |
pCurrent[i + 7] = level; |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
/* ****************************************************************** */ |
/***************************************************************************** |
263 |
/* ****************************************************************** */ |
****************************************************************************/ |
264 |
|
|
265 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
266 |
|
|
267 |
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 |
|
268 |
|
|
|
S1/S2 are used to determine if its worth predicting for AC |
|
269 |
S1 = sum of all (qcoeff - prediction) |
S1 = sum of all (qcoeff - prediction) |
270 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
271 |
*/ |
*/ |
272 |
|
|
273 |
uint32_t |
static int |
274 |
calc_acdc(MACROBLOCK * pMB, |
calc_acdc_coeff(MACROBLOCK * pMB, |
275 |
uint32_t block, |
uint32_t block, |
276 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
277 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
278 |
int16_t predictors[8]) |
int16_t predictors[8]) |
279 |
{ |
{ |
280 |
int16_t *pCurrent = pMB->pred_values[block]; |
int16_t *pCurrent = (int16_t*)pMB->pred_values[block]; |
281 |
uint32_t i; |
uint32_t i; |
282 |
uint32_t S1 = 0, S2 = 0; |
int S1 = 0, S2 = 0; |
283 |
|
|
284 |
|
|
285 |
/* store current coeffs to pred_values[] for future prediction */ |
/* store current coeffs to pred_values[] for future prediction */ |
286 |
|
|
287 |
pCurrent[0] = (int16_t)(qcoeff[0] * iDcScaler); |
pCurrent[0] = qcoeff[0] * iDcScaler; |
288 |
|
pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); |
289 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) { |
290 |
pCurrent[i] = qcoeff[i]; |
pCurrent[i] = qcoeff[i]; |
291 |
pCurrent[i + 7] = qcoeff[i * 8]; |
pCurrent[i + 7] = qcoeff[i * 8]; |
300 |
int16_t level; |
int16_t level; |
301 |
|
|
302 |
level = qcoeff[i]; |
level = qcoeff[i]; |
303 |
S2 += ABS(level); |
S2 += abs(level); |
304 |
level -= predictors[i]; |
level -= predictors[i]; |
305 |
S1 += ABS(level); |
S1 += abs(level); |
306 |
predictors[i] = level; |
predictors[i] = level; |
307 |
} |
} |
308 |
} else /* acpred_direction == 2 */ |
} else /* acpred_direction == 2 */ |
311 |
int16_t level; |
int16_t level; |
312 |
|
|
313 |
level = qcoeff[i * 8]; |
level = qcoeff[i * 8]; |
314 |
S2 += ABS(level); |
S2 += abs(level); |
315 |
level -= predictors[i]; |
level -= predictors[i]; |
316 |
S1 += ABS(level); |
S1 += abs(level); |
317 |
predictors[i] = level; |
predictors[i] = level; |
318 |
} |
} |
319 |
|
|
324 |
} |
} |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
/* returns the bits *saved* if prediction is enabled */ |
329 |
|
|
330 |
|
static int |
331 |
|
calc_acdc_bits(MACROBLOCK * pMB, |
332 |
|
uint32_t block, |
333 |
|
int16_t qcoeff[64], |
334 |
|
uint32_t iDcScaler, |
335 |
|
int16_t predictors[8]) |
336 |
|
{ |
337 |
|
const int direction = pMB->acpred_directions[block]; |
338 |
|
int16_t *pCurrent = (int16_t*)pMB->pred_values[block]; |
339 |
|
int16_t tmp[8]; |
340 |
|
unsigned int i; |
341 |
|
int Z1, Z2; |
342 |
|
|
343 |
|
/* store current coeffs to pred_values[] for future prediction */ |
344 |
|
pCurrent[0] = qcoeff[0] * iDcScaler; |
345 |
|
pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); |
346 |
|
for (i = 1; i < 8; i++) { |
347 |
|
pCurrent[i] = qcoeff[i]; |
348 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
349 |
|
} |
350 |
|
|
351 |
|
|
352 |
|
/* dc prediction */ |
353 |
|
qcoeff[0] = qcoeff[0] - predictors[0]; |
354 |
|
|
355 |
|
/* calc cost before ac prediction */ |
356 |
|
Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
357 |
|
|
358 |
|
/* apply ac prediction & calc cost*/ |
359 |
|
if (direction == 1) { |
360 |
|
for (i = 1; i < 8; i++) { |
361 |
|
tmp[i] = qcoeff[i]; |
362 |
|
qcoeff[i] -= predictors[i]; |
363 |
|
predictors[i] = qcoeff[i]; |
364 |
|
} |
365 |
|
}else{ /* acpred_direction == 2 */ |
366 |
|
for (i = 1; i < 8; i++) { |
367 |
|
tmp[i] = qcoeff[i*8]; |
368 |
|
qcoeff[i*8] -= predictors[i]; |
369 |
|
predictors[i] = qcoeff[i*8]; |
370 |
|
} |
371 |
|
} |
372 |
|
|
373 |
|
Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
374 |
|
|
375 |
|
/* undo prediction */ |
376 |
|
if (direction == 1) { |
377 |
|
for (i = 1; i < 8; i++) |
378 |
|
qcoeff[i] = tmp[i]; |
379 |
|
}else{ /* acpred_direction == 2 */ |
380 |
|
for (i = 1; i < 8; i++) |
381 |
|
qcoeff[i*8] = tmp[i]; |
382 |
|
} |
383 |
|
|
384 |
|
return Z2-Z1; |
385 |
|
} |
386 |
|
|
387 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
388 |
|
|
389 |
void |
static void |
390 |
apply_acdc(MACROBLOCK * pMB, |
apply_acdc(MACROBLOCK * pMB, |
391 |
uint32_t block, |
uint32_t block, |
392 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
393 |
int16_t predictors[8]) |
int16_t predictors[8]) |
394 |
{ |
{ |
395 |
uint32_t i; |
unsigned int i; |
396 |
|
|
397 |
if (pMB->acpred_directions[block] == 1) { |
if (pMB->acpred_directions[block] == 1) { |
398 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) |
399 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
|
} |
|
400 |
} else { |
} else { |
401 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) |
402 |
qcoeff[i * 8] = predictors[i]; |
qcoeff[i * 8] = predictors[i]; |
403 |
} |
} |
404 |
} |
} |
|
} |
|
405 |
|
|
406 |
|
|
407 |
void |
void |
409 |
uint32_t x, |
uint32_t x, |
410 |
uint32_t y, |
uint32_t y, |
411 |
uint32_t mb_width, |
uint32_t mb_width, |
412 |
int16_t qcoeff[6 * 64]) |
int16_t qcoeff[6 * 64], |
413 |
|
const int bound) |
414 |
{ |
{ |
415 |
|
|
416 |
int32_t j; |
int32_t j; |
417 |
int32_t iDcScaler, iQuant = frame->quant; |
int32_t iDcScaler, iQuant; |
418 |
int32_t S = 0; |
int S = 0; |
419 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
420 |
|
|
421 |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
422 |
|
iQuant = pMB->quant; |
423 |
|
|
424 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
425 |
|
|
426 |
for (j = 0; j < 6; j++) { |
for (j = 0; j < 6; j++) { |
427 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, j<4); |
428 |
|
|
429 |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
430 |
iQuant, iDcScaler, predictors[j], 0); |
iQuant, iDcScaler, predictors[j], bound); |
431 |
|
|
432 |
S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
if ((frame->vop_flags & XVID_VOP_HQACPRED)) |
433 |
|
S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
434 |
|
else |
435 |
|
S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
436 |
|
|
437 |
} |
} |
438 |
|
|
439 |
if (S < 0) /* dont predict */ |
if (S<=0) { /* dont predict */ |
440 |
{ |
for (j = 0; j < 6; j++) |
|
for (j = 0; j < 6; j++) { |
|
441 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
|
} |
|
442 |
} else { |
} else { |
443 |
for (j = 0; j < 6; j++) { |
for (j = 0; j < 6; j++) |
444 |
apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]); |
445 |
} |
} |
446 |
} |
|
447 |
pMB->cbp = calc_cbp(qcoeff); |
pMB->cbp = calc_cbp(qcoeff); |
448 |
} |
} |
449 |
|
} |
450 |
|
|
451 |
|
static const VECTOR zeroMV = { 0, 0 }; |
452 |
|
|
453 |
|
VECTOR |
454 |
|
get_pmv2(const MACROBLOCK * const mbs, |
455 |
|
const int mb_width, |
456 |
|
const int bound, |
457 |
|
const int x, |
458 |
|
const int y, |
459 |
|
const int block) |
460 |
|
{ |
461 |
|
int lx, ly, lz; /* left */ |
462 |
|
int tx, ty, tz; /* top */ |
463 |
|
int rx, ry, rz; /* top-right */ |
464 |
|
int lpos, tpos, rpos; |
465 |
|
int num_cand = 0, last_cand = 1; |
466 |
|
|
467 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
468 |
|
|
469 |
|
switch (block) { |
470 |
|
case 0: |
471 |
|
lx = x - 1; ly = y; lz = 1; |
472 |
|
tx = x; ty = y - 1; tz = 2; |
473 |
|
rx = x + 1; ry = y - 1; rz = 2; |
474 |
|
break; |
475 |
|
case 1: |
476 |
|
lx = x; ly = y; lz = 0; |
477 |
|
tx = x; ty = y - 1; tz = 3; |
478 |
|
rx = x + 1; ry = y - 1; rz = 2; |
479 |
|
break; |
480 |
|
case 2: |
481 |
|
lx = x - 1; ly = y; lz = 3; |
482 |
|
tx = x; ty = y; tz = 0; |
483 |
|
rx = x; ry = y; rz = 1; |
484 |
|
break; |
485 |
|
default: |
486 |
|
lx = x; ly = y; lz = 2; |
487 |
|
tx = x; ty = y; tz = 0; |
488 |
|
rx = x; ry = y; rz = 1; |
489 |
|
} |
490 |
|
|
491 |
|
lpos = lx + ly * mb_width; |
492 |
|
rpos = rx + ry * mb_width; |
493 |
|
tpos = tx + ty * mb_width; |
494 |
|
|
495 |
|
if (lpos >= bound && lx >= 0) { |
496 |
|
num_cand++; |
497 |
|
pmv[1] = mbs[lpos].mvs[lz]; |
498 |
|
} else pmv[1] = zeroMV; |
499 |
|
|
500 |
|
if (tpos >= bound) { |
501 |
|
num_cand++; |
502 |
|
last_cand = 2; |
503 |
|
pmv[2] = mbs[tpos].mvs[tz]; |
504 |
|
} else pmv[2] = zeroMV; |
505 |
|
|
506 |
|
if (rpos >= bound && rx < mb_width) { |
507 |
|
num_cand++; |
508 |
|
last_cand = 3; |
509 |
|
pmv[3] = mbs[rpos].mvs[rz]; |
510 |
|
} else pmv[3] = zeroMV; |
511 |
|
|
512 |
|
/* If there're more than one candidate, we return the median vector */ |
513 |
|
|
514 |
|
if (num_cand > 1) { |
515 |
|
/* set median */ |
516 |
|
pmv[0].x = |
517 |
|
MIN(MAX(pmv[1].x, pmv[2].x), |
518 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
519 |
|
pmv[0].y = |
520 |
|
MIN(MAX(pmv[1].y, pmv[2].y), |
521 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
522 |
|
return pmv[0]; |
523 |
|
} |
524 |
|
|
525 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
526 |
|
} |
527 |
|
|
528 |
|
VECTOR get_pmv2_interlaced(const MACROBLOCK * const mbs, |
529 |
|
const int mb_width, |
530 |
|
const int bound, |
531 |
|
const int x, |
532 |
|
const int y, |
533 |
|
const int block) |
534 |
|
{ |
535 |
|
int lx, ly, lz; /* left */ |
536 |
|
int tx, ty, tz; /* top */ |
537 |
|
int rx, ry, rz; /* top-right */ |
538 |
|
int lpos, tpos, rpos; |
539 |
|
int num_cand = 0, last_cand = 1; |
540 |
|
|
541 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
542 |
|
|
543 |
|
lx=x-1; ly=y; lz=1; |
544 |
|
tx=x; ty=y-1; tz=2; |
545 |
|
rx=x+1; ry=y-1; rz=2; |
546 |
|
|
547 |
|
lpos=lx+ly*mb_width; |
548 |
|
rpos=rx+ry*mb_width; |
549 |
|
tpos=tx+ty*mb_width; |
550 |
|
|
551 |
|
if(lx>=0 && lpos>=bound) |
552 |
|
{ |
553 |
|
num_cand++; |
554 |
|
if(mbs[lpos].field_pred) |
555 |
|
pmv[1] = mbs[lpos].mvs_avg; |
556 |
|
else |
557 |
|
pmv[1] = mbs[lpos].mvs[lz]; |
558 |
|
} |
559 |
|
else |
560 |
|
{ |
561 |
|
pmv[1] = zeroMV; |
562 |
|
} |
563 |
|
|
564 |
|
if(tpos>=bound) |
565 |
|
{ |
566 |
|
num_cand++; |
567 |
|
last_cand=2; |
568 |
|
if(mbs[tpos].field_pred) |
569 |
|
pmv[2] = mbs[tpos].mvs_avg; |
570 |
|
else |
571 |
|
pmv[2] = mbs[tpos].mvs[tz]; |
572 |
|
} |
573 |
|
else |
574 |
|
{ |
575 |
|
pmv[2] = zeroMV; |
576 |
|
} |
577 |
|
|
578 |
|
if(rx<mb_width && rpos>=bound) |
579 |
|
{ |
580 |
|
num_cand++; |
581 |
|
last_cand = 3; |
582 |
|
if(mbs[rpos].field_pred) |
583 |
|
pmv[3] = mbs[rpos].mvs_avg; |
584 |
|
else |
585 |
|
pmv[3] = mbs[rpos].mvs[rz]; |
586 |
|
} |
587 |
|
else |
588 |
|
{ |
589 |
|
pmv[3] = zeroMV; |
590 |
|
} |
591 |
|
|
592 |
|
/* If there're more than one candidate, we return the median vector */ |
593 |
|
if(num_cand>1) |
594 |
|
{ |
595 |
|
/* set median */ |
596 |
|
pmv[0].x = MIN(MAX(pmv[1].x, pmv[2].x), |
597 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
598 |
|
pmv[0].y = MIN(MAX(pmv[1].y, pmv[2].y), |
599 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
600 |
|
|
601 |
|
return pmv[0]; |
602 |
|
} |
603 |
|
|
604 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
605 |
|
} |
606 |
|
|
607 |
|
VECTOR |
608 |
|
get_qpmv2(const MACROBLOCK * const mbs, |
609 |
|
const int mb_width, |
610 |
|
const int bound, |
611 |
|
const int x, |
612 |
|
const int y, |
613 |
|
const int block) |
614 |
|
{ |
615 |
|
int lx, ly, lz; /* left */ |
616 |
|
int tx, ty, tz; /* top */ |
617 |
|
int rx, ry, rz; /* top-right */ |
618 |
|
int lpos, tpos, rpos; |
619 |
|
int num_cand = 0, last_cand = 1; |
620 |
|
|
621 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
622 |
|
|
623 |
|
switch (block) { |
624 |
|
case 0: |
625 |
|
lx = x - 1; ly = y; lz = 1; |
626 |
|
tx = x; ty = y - 1; tz = 2; |
627 |
|
rx = x + 1; ry = y - 1; rz = 2; |
628 |
|
break; |
629 |
|
case 1: |
630 |
|
lx = x; ly = y; lz = 0; |
631 |
|
tx = x; ty = y - 1; tz = 3; |
632 |
|
rx = x + 1; ry = y - 1; rz = 2; |
633 |
|
break; |
634 |
|
case 2: |
635 |
|
lx = x - 1; ly = y; lz = 3; |
636 |
|
tx = x; ty = y; tz = 0; |
637 |
|
rx = x; ry = y; rz = 1; |
638 |
|
break; |
639 |
|
default: |
640 |
|
lx = x; ly = y; lz = 2; |
641 |
|
tx = x; ty = y; tz = 0; |
642 |
|
rx = x; ry = y; rz = 1; |
643 |
|
} |
644 |
|
|
645 |
|
lpos = lx + ly * mb_width; |
646 |
|
rpos = rx + ry * mb_width; |
647 |
|
tpos = tx + ty * mb_width; |
648 |
|
|
649 |
|
if (lpos >= bound && lx >= 0) { |
650 |
|
num_cand++; |
651 |
|
pmv[1] = mbs[lpos].qmvs[lz]; |
652 |
|
} else pmv[1] = zeroMV; |
653 |
|
|
654 |
|
if (tpos >= bound) { |
655 |
|
num_cand++; |
656 |
|
last_cand = 2; |
657 |
|
pmv[2] = mbs[tpos].qmvs[tz]; |
658 |
|
} else pmv[2] = zeroMV; |
659 |
|
|
660 |
|
if (rpos >= bound && rx < mb_width) { |
661 |
|
num_cand++; |
662 |
|
last_cand = 3; |
663 |
|
pmv[3] = mbs[rpos].qmvs[rz]; |
664 |
|
} else pmv[3] = zeroMV; |
665 |
|
|
666 |
|
/* If there're more than one candidate, we return the median vector */ |
667 |
|
|
668 |
|
if (num_cand > 1) { |
669 |
|
/* set median */ |
670 |
|
pmv[0].x = |
671 |
|
MIN(MAX(pmv[1].x, pmv[2].x), |
672 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
673 |
|
pmv[0].y = |
674 |
|
MIN(MAX(pmv[1].y, pmv[2].y), |
675 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
676 |
|
return pmv[0]; |
677 |
|
} |
678 |
|
|
679 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
680 |
} |
} |