1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Prediction module - |
5 |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
* |
6 |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
* Copyright (C) 2001-2003 Michael Militzer <isibaar@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* 2001-2003 Peter Ross <pross@xvid.org> |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* |
9 |
* would be at such party's own risk. The original developer of this * |
* This program is free software ; you can redistribute it and/or modify |
10 |
* software module and his/her company, and subsequent editors and their * |
* it under the terms of the GNU General Public License as published by |
11 |
* companies, will have no liability for use of this software or * |
* the Free Software Foundation ; either version 2 of the License, or |
12 |
* modifications or derivatives thereof. * |
* (at your option) any later version. |
13 |
* * |
* |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* This program is distributed in the hope that it will be useful, |
15 |
* under the terms of the GNU General Public License as published by * |
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* (at your option) any later version. * |
* GNU General Public License for more details. |
18 |
* * |
* |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* You should have received a copy of the GNU General Public License |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* along with this program ; if not, write to the Free Software |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* GNU General Public License for more details. * |
* |
23 |
* * |
* $Id$ |
24 |
* You should have received a copy of the GNU General Public License * |
* |
25 |
* along with this program; if not, write to the Free Software * |
****************************************************************************/ |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* mbprediction.c * |
|
|
* * |
|
|
* Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> * |
|
|
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
|
|
* * |
|
|
* For more information visit the XviD homepage: http://www.xvid.org * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* Revision history: * |
|
|
* * |
|
|
* 12.12.2001 improved calc_acdc_prediction; removed need for memcpy * |
|
|
* 15.12.2001 moved pmv displacement to motion estimation * |
|
|
* 30.11.2001 mmx cbp support * |
|
|
* 17.11.2001 initial version * |
|
|
* * |
|
|
******************************************************************************/ |
|
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 |
|
|
38 |
#define ABS(X) (((X)>0)?(X):-(X)) |
static int __inline |
39 |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
rescale(int predict_quant, |
40 |
|
int current_quant, |
41 |
|
int coeff) |
|
static int __inline rescale(int predict_quant, int current_quant, int coeff) |
|
42 |
{ |
{ |
43 |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0; |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), |
44 |
|
(current_quant)) : 0; |
45 |
} |
} |
46 |
|
|
47 |
|
|
57 |
*/ |
*/ |
58 |
|
|
59 |
|
|
60 |
void predict_acdc(MACROBLOCK *pMBs, |
void |
61 |
uint32_t x, uint32_t y, uint32_t mb_width, |
predict_acdc(MACROBLOCK * pMBs, |
62 |
|
uint32_t x, |
63 |
|
uint32_t y, |
64 |
|
uint32_t mb_width, |
65 |
uint32_t block, |
uint32_t block, |
66 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
67 |
uint32_t current_quant, |
uint32_t current_quant, |
68 |
int32_t iDcScaler, |
int32_t iDcScaler, |
69 |
int16_t predictors[8]) |
int16_t predictors[8], |
70 |
|
const int bound) |
71 |
|
|
72 |
{ |
{ |
73 |
|
const int mbpos = (y * mb_width) + x; |
74 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
75 |
|
|
76 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
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 = NULL; |
88 |
|
|
89 |
// grab left,top and diag macroblocks |
/* grab left,top and diag macroblocks */ |
90 |
|
|
91 |
// left macroblock |
/* left macroblock */ |
92 |
|
|
93 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
94 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
95 |
|
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 */ |
101 |
|
|
102 |
// top macroblock |
if (mbpos >= bound + (int)mb_width && |
103 |
|
(pMBs[index - mb_width].mode == MODE_INTRA || |
104 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
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 */ |
110 |
|
|
111 |
// diag macroblock |
if (x && mbpos >= bound + (int)mb_width + 1 && |
112 |
|
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
113 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
114 |
|
|
115 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
diag = (int16_t*)pMBs[index - 1 - mb_width].pred_values[0]; |
|
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
|
|
|
|
|
diag = 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 |
|
|
122 |
switch (block) { |
switch (block) { |
123 |
|
|
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])) { |
190 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
*acpred_direction = 1; /* vertical */ |
|
*acpred_direction = 1; // vertical |
|
191 |
predictors[0] = 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 { |
196 |
else |
*acpred_direction = 2; /* horizontal */ |
|
{ |
|
|
*acpred_direction = 2; // horizontal |
|
197 |
predictors[0] = 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 |
} |
} |
201 |
} |
} |
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 add_acdc(MACROBLOCK *pMB, |
void |
214 |
|
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 |
dct_codes[0] += predictors[0]; // dc prediction |
DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]); |
226 |
|
|
227 |
|
dct_codes[0] += predictors[0]; /* dc prediction */ |
228 |
pCurrent[0] = 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(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; |
241 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
242 |
} |
} |
243 |
} |
} else if (acpred_direction == 2) { |
244 |
else if (acpred_direction == 2) |
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(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; |
250 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
251 |
} |
} |
252 |
} |
} else { |
253 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
254 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
255 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
256 |
} |
} |
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 calc_acdc(MACROBLOCK *pMB, |
static int |
274 |
|
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] = 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]; |
295 |
|
|
296 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
297 |
|
|
298 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
|
{ |
|
299 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
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 |
|
309 |
{ |
{ |
310 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
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 |
|
|
|
/* apply predictors[] to qcoeff */ |
|
327 |
|
|
328 |
void apply_acdc(MACROBLOCK *pMB, |
/* returns the bits *saved* if prediction is enabled */ |
329 |
|
|
330 |
|
static int |
331 |
|
calc_acdc_bits(MACROBLOCK * pMB, |
332 |
uint32_t block, |
uint32_t block, |
333 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
334 |
|
uint32_t iDcScaler, |
335 |
int16_t predictors[8]) |
int16_t predictors[8]) |
336 |
{ |
{ |
337 |
uint32_t i; |
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 |
if (pMB->acpred_directions[block] == 1) |
/* 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++) |
for(i = 1; i < 8; i++) |
378 |
{ |
qcoeff[i] = tmp[i]; |
379 |
qcoeff[i] = predictors[i]; |
}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 |
else |
|
387 |
|
/* apply predictors[] to qcoeff */ |
388 |
|
|
389 |
|
static void |
390 |
|
apply_acdc(MACROBLOCK * pMB, |
391 |
|
uint32_t block, |
392 |
|
int16_t qcoeff[64], |
393 |
|
int16_t predictors[8]) |
394 |
{ |
{ |
395 |
|
unsigned int i; |
396 |
|
|
397 |
|
if (pMB->acpred_directions[block] == 1) { |
398 |
|
for (i = 1; i < 8; i++) |
399 |
|
qcoeff[i] = predictors[i]; |
400 |
|
} 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 MBPrediction(MBParam *pParam, |
void |
408 |
|
MBPrediction(FRAMEINFO * frame, |
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]) |
|
MACROBLOCK *mbs) |
|
413 |
{ |
{ |
414 |
|
|
415 |
int32_t j; |
int32_t j; |
416 |
int32_t iDcScaler, iQuant = pParam->quant; |
int32_t iDcScaler, iQuant; |
417 |
int32_t S = 0; |
int S = 0; |
418 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
419 |
|
|
420 |
MACROBLOCK *pMB = &mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
421 |
|
iQuant = pMB->quant; |
422 |
|
|
423 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
424 |
|
|
425 |
|
for (j = 0; j < 6; j++) { |
426 |
|
iDcScaler = get_dc_scaler(iQuant, j<4); |
427 |
|
|
428 |
|
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
429 |
|
iQuant, iDcScaler, predictors[j], 0); |
430 |
|
|
431 |
|
if ((frame->vop_flags & XVID_VOP_HQACPRED)) |
432 |
|
S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
433 |
|
else |
434 |
|
S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
435 |
|
|
436 |
|
} |
437 |
|
|
438 |
|
if (S<=0) { /* dont predict */ |
439 |
for(j = 0; j < 6; j++) |
for(j = 0; j < 6; j++) |
440 |
{ |
pMB->acpred_directions[j] = 0; |
441 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
}else{ |
442 |
|
for (j = 0; j < 6; j++) |
443 |
|
apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]); |
444 |
|
} |
445 |
|
|
446 |
predict_acdc(mbs, |
pMB->cbp = calc_cbp(qcoeff); |
447 |
x, |
} |
448 |
y, |
} |
449 |
mb_width, |
|
450 |
j, |
static const VECTOR zeroMV = { 0, 0 }; |
451 |
&qcoeff[j*64], |
|
452 |
iQuant, |
VECTOR |
453 |
iDcScaler, |
get_pmv2(const MACROBLOCK * const mbs, |
454 |
predictors[j]); |
const int mb_width, |
455 |
|
const int bound, |
456 |
S += calc_acdc(pMB, |
const int x, |
457 |
j, |
const int y, |
458 |
&qcoeff[j*64], |
const int block) |
459 |
iDcScaler, |
{ |
460 |
predictors[j]); |
int lx, ly, lz; /* left */ |
461 |
|
int tx, ty, tz; /* top */ |
462 |
|
int rx, ry, rz; /* top-right */ |
463 |
|
int lpos, tpos, rpos; |
464 |
|
int num_cand = 0, last_cand = 1; |
465 |
|
|
466 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
467 |
|
|
468 |
|
switch (block) { |
469 |
|
case 0: |
470 |
|
lx = x - 1; ly = y; lz = 1; |
471 |
|
tx = x; ty = y - 1; tz = 2; |
472 |
|
rx = x + 1; ry = y - 1; rz = 2; |
473 |
|
break; |
474 |
|
case 1: |
475 |
|
lx = x; ly = y; lz = 0; |
476 |
|
tx = x; ty = y - 1; tz = 3; |
477 |
|
rx = x + 1; ry = y - 1; rz = 2; |
478 |
|
break; |
479 |
|
case 2: |
480 |
|
lx = x - 1; ly = y; lz = 3; |
481 |
|
tx = x; ty = y; tz = 0; |
482 |
|
rx = x; ry = y; rz = 1; |
483 |
|
break; |
484 |
|
default: |
485 |
|
lx = x; ly = y; lz = 2; |
486 |
|
tx = x; ty = y; tz = 0; |
487 |
|
rx = x; ry = y; rz = 1; |
488 |
|
} |
489 |
|
|
490 |
|
lpos = lx + ly * mb_width; |
491 |
|
rpos = rx + ry * mb_width; |
492 |
|
tpos = tx + ty * mb_width; |
493 |
|
|
494 |
|
if (lpos >= bound && lx >= 0) { |
495 |
|
num_cand++; |
496 |
|
pmv[1] = mbs[lpos].mvs[lz]; |
497 |
|
} else pmv[1] = zeroMV; |
498 |
|
|
499 |
|
if (tpos >= bound) { |
500 |
|
num_cand++; |
501 |
|
last_cand = 2; |
502 |
|
pmv[2] = mbs[tpos].mvs[tz]; |
503 |
|
} else pmv[2] = zeroMV; |
504 |
|
|
505 |
|
if (rpos >= bound && rx < mb_width) { |
506 |
|
num_cand++; |
507 |
|
last_cand = 3; |
508 |
|
pmv[3] = mbs[rpos].mvs[rz]; |
509 |
|
} else pmv[3] = zeroMV; |
510 |
|
|
511 |
|
/* If there're more than one candidate, we return the median vector */ |
512 |
|
|
513 |
|
if (num_cand > 1) { |
514 |
|
/* set median */ |
515 |
|
pmv[0].x = |
516 |
|
MIN(MAX(pmv[1].x, pmv[2].x), |
517 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
518 |
|
pmv[0].y = |
519 |
|
MIN(MAX(pmv[1].y, pmv[2].y), |
520 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
521 |
|
return pmv[0]; |
522 |
|
} |
523 |
|
|
524 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
525 |
|
} |
526 |
|
|
527 |
|
VECTOR get_pmv2_interlaced(const MACROBLOCK * const mbs, |
528 |
|
const int mb_width, |
529 |
|
const int bound, |
530 |
|
const int x, |
531 |
|
const int y, |
532 |
|
const int block) |
533 |
|
{ |
534 |
|
int lx, ly, lz; /* left */ |
535 |
|
int tx, ty, tz; /* top */ |
536 |
|
int rx, ry, rz; /* top-right */ |
537 |
|
int lpos, tpos, rpos; |
538 |
|
int num_cand = 0, last_cand = 1; |
539 |
|
|
540 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
541 |
|
|
542 |
|
lx=x-1; ly=y; lz=1; |
543 |
|
tx=x; ty=y-1; tz=2; |
544 |
|
rx=x+1; ry=y-1; rz=2; |
545 |
|
|
546 |
|
lpos=lx+ly*mb_width; |
547 |
|
rpos=rx+ry*mb_width; |
548 |
|
tpos=tx+ty*mb_width; |
549 |
|
|
550 |
|
if(lx>=0 && lpos>=bound) |
551 |
|
{ |
552 |
|
num_cand++; |
553 |
|
if(mbs[lpos].field_pred) |
554 |
|
pmv[1] = mbs[lpos].mvs_avg; |
555 |
|
else |
556 |
|
pmv[1] = mbs[lpos].mvs[lz]; |
557 |
|
} |
558 |
|
else |
559 |
|
{ |
560 |
|
pmv[1] = zeroMV; |
561 |
} |
} |
562 |
|
|
563 |
if (S < 0) // dont predict |
if(tpos>=bound) |
564 |
{ |
{ |
565 |
for(j = 0; j < 6; j++) |
num_cand++; |
566 |
|
last_cand=2; |
567 |
|
if(mbs[tpos].field_pred) |
568 |
|
pmv[2] = mbs[tpos].mvs_avg; |
569 |
|
else |
570 |
|
pmv[2] = mbs[tpos].mvs[tz]; |
571 |
|
} |
572 |
|
else |
573 |
{ |
{ |
574 |
pMB->acpred_directions[j] = 0; |
pmv[2] = zeroMV; |
575 |
} |
} |
576 |
|
|
577 |
|
if(rx<mb_width && rpos>=bound) |
578 |
|
{ |
579 |
|
num_cand++; |
580 |
|
last_cand = 3; |
581 |
|
if(mbs[rpos].field_pred) |
582 |
|
pmv[3] = mbs[rpos].mvs_avg; |
583 |
|
else |
584 |
|
pmv[3] = mbs[rpos].mvs[rz]; |
585 |
} |
} |
586 |
else |
else |
587 |
{ |
{ |
588 |
for(j = 0; j < 6; j++) |
pmv[3] = zeroMV; |
589 |
|
} |
590 |
|
|
591 |
|
/* If there're more than one candidate, we return the median vector */ |
592 |
|
if(num_cand>1) |
593 |
{ |
{ |
594 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
/* set median */ |
595 |
|
pmv[0].x = MIN(MAX(pmv[1].x, pmv[2].x), |
596 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
597 |
|
pmv[0].y = MIN(MAX(pmv[1].y, pmv[2].y), |
598 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
599 |
|
|
600 |
|
return pmv[0]; |
601 |
} |
} |
602 |
|
|
603 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
604 |
} |
} |
605 |
pMB->cbp = calc_cbp(qcoeff); |
|
606 |
|
VECTOR |
607 |
|
get_qpmv2(const MACROBLOCK * const mbs, |
608 |
|
const int mb_width, |
609 |
|
const int bound, |
610 |
|
const int x, |
611 |
|
const int y, |
612 |
|
const int block) |
613 |
|
{ |
614 |
|
int lx, ly, lz; /* left */ |
615 |
|
int tx, ty, tz; /* top */ |
616 |
|
int rx, ry, rz; /* top-right */ |
617 |
|
int lpos, tpos, rpos; |
618 |
|
int num_cand = 0, last_cand = 1; |
619 |
|
|
620 |
|
VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ |
621 |
|
|
622 |
|
switch (block) { |
623 |
|
case 0: |
624 |
|
lx = x - 1; ly = y; lz = 1; |
625 |
|
tx = x; ty = y - 1; tz = 2; |
626 |
|
rx = x + 1; ry = y - 1; rz = 2; |
627 |
|
break; |
628 |
|
case 1: |
629 |
|
lx = x; ly = y; lz = 0; |
630 |
|
tx = x; ty = y - 1; tz = 3; |
631 |
|
rx = x + 1; ry = y - 1; rz = 2; |
632 |
|
break; |
633 |
|
case 2: |
634 |
|
lx = x - 1; ly = y; lz = 3; |
635 |
|
tx = x; ty = y; tz = 0; |
636 |
|
rx = x; ry = y; rz = 1; |
637 |
|
break; |
638 |
|
default: |
639 |
|
lx = x; ly = y; lz = 2; |
640 |
|
tx = x; ty = y; tz = 0; |
641 |
|
rx = x; ry = y; rz = 1; |
642 |
|
} |
643 |
|
|
644 |
|
lpos = lx + ly * mb_width; |
645 |
|
rpos = rx + ry * mb_width; |
646 |
|
tpos = tx + ty * mb_width; |
647 |
|
|
648 |
|
if (lpos >= bound && lx >= 0) { |
649 |
|
num_cand++; |
650 |
|
pmv[1] = mbs[lpos].qmvs[lz]; |
651 |
|
} else pmv[1] = zeroMV; |
652 |
|
|
653 |
|
if (tpos >= bound) { |
654 |
|
num_cand++; |
655 |
|
last_cand = 2; |
656 |
|
pmv[2] = mbs[tpos].qmvs[tz]; |
657 |
|
} else pmv[2] = zeroMV; |
658 |
|
|
659 |
|
if (rpos >= bound && rx < mb_width) { |
660 |
|
num_cand++; |
661 |
|
last_cand = 3; |
662 |
|
pmv[3] = mbs[rpos].qmvs[rz]; |
663 |
|
} else pmv[3] = zeroMV; |
664 |
|
|
665 |
|
/* If there're more than one candidate, we return the median vector */ |
666 |
|
|
667 |
|
if (num_cand > 1) { |
668 |
|
/* set median */ |
669 |
|
pmv[0].x = |
670 |
|
MIN(MAX(pmv[1].x, pmv[2].x), |
671 |
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
672 |
|
pmv[0].y = |
673 |
|
MIN(MAX(pmv[1].y, pmv[2].y), |
674 |
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
675 |
|
return pmv[0]; |
676 |
} |
} |
677 |
|
|
678 |
|
return pmv[last_cand]; /* no point calculating median mv */ |
679 |
} |
} |