1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Prediction functions - |
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-2002 - Michael Militzer <isibaar@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* Copyright(C) 2001-2002 - 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 an implementation of a part of one or more MPEG-4 |
10 |
* software module and his/her company, and subsequent editors and their * |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
11 |
* companies, will have no liability for use of this software or * |
* to use this software module in hardware or software products are |
12 |
* modifications or derivatives thereof. * |
* advised that its use may infringe existing patents or copyrights, and |
13 |
* * |
* any such use would be at such party's own risk. The original |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* developer of this software module and his/her company, and subsequent |
15 |
* under the terms of the GNU General Public License as published by * |
* editors and their companies, will have no liability for use of this |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* software or modifications or derivatives thereof. |
17 |
* (at your option) any later version. * |
* |
18 |
* * |
* This program is free software; you can redistribute it and/or modify |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* it under the terms of the GNU General Public License as published by |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* the Free Software Foundation; either version 2 of the License, or |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* (at your option) any later version. |
22 |
* GNU General Public License for more details. * |
* |
23 |
* * |
* This program is distributed in the hope that it will be useful, |
24 |
* You should have received a copy of the GNU General Public License * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
* along with this program; if not, write to the Free Software * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* GNU General Public License for more details. |
27 |
* * |
* |
28 |
******************************************************************************/ |
* You should have received a copy of the GNU General Public License |
29 |
|
* along with this program; if not, write to the Free Software |
30 |
/****************************************************************************** |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
31 |
* * |
* |
32 |
* mbprediction.c * |
* $Id: mbprediction.c,v 1.9 2002-09-21 03:07:56 suxen_drol Exp $ |
33 |
* * |
* |
34 |
* 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 * |
|
|
* * |
|
|
******************************************************************************/ |
|
35 |
|
|
36 |
#include "../encoder.h" |
#include "../encoder.h" |
37 |
#include "mbprediction.h" |
#include "mbprediction.h" |
43 |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
44 |
|
|
45 |
|
|
46 |
static int __inline rescale(int predict_quant, int current_quant, int coeff) |
/***************************************************************************** |
47 |
|
* Local inlined function |
48 |
|
****************************************************************************/ |
49 |
|
|
50 |
|
static int __inline |
51 |
|
rescale(int predict_quant, |
52 |
|
int current_quant, |
53 |
|
int coeff) |
54 |
{ |
{ |
55 |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0; |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), |
56 |
|
(current_quant)) : 0; |
57 |
} |
} |
58 |
|
|
59 |
|
|
60 |
|
/***************************************************************************** |
61 |
|
* Local data |
62 |
|
****************************************************************************/ |
63 |
|
|
64 |
static const int16_t default_acdc_values[15] = { |
static const int16_t default_acdc_values[15] = { |
65 |
1024, |
1024, |
66 |
0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, |
68 |
}; |
}; |
69 |
|
|
70 |
|
|
71 |
|
/***************************************************************************** |
72 |
|
* Functions |
73 |
|
****************************************************************************/ |
74 |
|
|
75 |
|
|
76 |
/* get dc/ac prediction direction for a single block and place |
/* get dc/ac prediction direction for a single block and place |
77 |
predictor values into MB->pred_values[j][..] |
predictor values into MB->pred_values[j][..] |
78 |
*/ |
*/ |
79 |
|
|
80 |
|
|
81 |
void predict_acdc(MACROBLOCK *pMBs, |
void |
82 |
uint32_t x, uint32_t y, uint32_t mb_width, |
predict_acdc(MACROBLOCK * pMBs, |
83 |
|
uint32_t x, |
84 |
|
uint32_t y, |
85 |
|
uint32_t mb_width, |
86 |
uint32_t block, |
uint32_t block, |
87 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
88 |
uint32_t current_quant, |
uint32_t current_quant, |
89 |
int32_t iDcScaler, |
int32_t iDcScaler, |
90 |
int16_t predictors[8]) |
int16_t predictors[8], |
91 |
|
const int bound) |
92 |
|
|
93 |
{ |
{ |
94 |
|
const int mbpos = (y * mb_width) + x; |
95 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
96 |
|
|
97 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
111 |
|
|
112 |
// left macroblock |
// left macroblock |
113 |
|
|
114 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
115 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
116 |
|
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
117 |
|
|
118 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
119 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
120 |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
121 |
} |
} |
|
|
|
122 |
// top macroblock |
// top macroblock |
123 |
|
|
124 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
if (mbpos >= bound + (int)mb_width && |
125 |
|| pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - mb_width].mode == MODE_INTRA || |
126 |
|
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
127 |
|
|
128 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
129 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
130 |
} |
} |
|
|
|
131 |
// diag macroblock |
// diag macroblock |
132 |
|
|
133 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
if (x && mbpos >= bound + (int)mb_width + 1 && |
134 |
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
135 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
136 |
|
|
137 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
138 |
} |
} |
212 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
213 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; // vertical |
214 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
215 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
216 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
217 |
} |
} |
218 |
} |
} else { |
|
else |
|
|
{ |
|
219 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; // horizontal |
220 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
221 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
222 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
223 |
} |
} |
224 |
} |
} |
230 |
*/ |
*/ |
231 |
|
|
232 |
|
|
233 |
void add_acdc(MACROBLOCK *pMB, |
void |
234 |
|
add_acdc(MACROBLOCK * pMB, |
235 |
uint32_t block, |
uint32_t block, |
236 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
237 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
241 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
242 |
uint32_t i; |
uint32_t i; |
243 |
|
|
244 |
|
DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]); |
245 |
|
|
246 |
dct_codes[0] += predictors[0]; // dc prediction |
dct_codes[0] += predictors[0]; // dc prediction |
247 |
pCurrent[0] = dct_codes[0] * iDcScaler; |
pCurrent[0] = dct_codes[0] * iDcScaler; |
248 |
|
|
249 |
if (acpred_direction == 1) |
if (acpred_direction == 1) { |
250 |
{ |
for (i = 1; i < 8; i++) { |
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
251 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
252 |
|
|
253 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]); |
254 |
|
|
255 |
dct_codes[i] = level; |
dct_codes[i] = level; |
256 |
pCurrent[i] = level; |
pCurrent[i] = level; |
257 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
258 |
} |
} |
259 |
} |
} else if (acpred_direction == 2) { |
260 |
else if (acpred_direction == 2) |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
261 |
int level = dct_codes[i*8] + predictors[i]; |
int level = dct_codes[i*8] + predictors[i]; |
262 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]); |
263 |
|
|
264 |
dct_codes[i*8] = level; |
dct_codes[i*8] = level; |
265 |
pCurrent[i+7] = level; |
pCurrent[i+7] = level; |
266 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
267 |
} |
} |
268 |
} |
} else { |
269 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
270 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
271 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
272 |
} |
} |
288 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
289 |
*/ |
*/ |
290 |
|
|
291 |
uint32_t calc_acdc(MACROBLOCK *pMB, |
uint32_t |
292 |
|
calc_acdc(MACROBLOCK * pMB, |
293 |
uint32_t block, |
uint32_t block, |
294 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
295 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
312 |
|
|
313 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
314 |
|
|
315 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
|
{ |
|
316 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
317 |
int16_t level; |
int16_t level; |
318 |
|
|
322 |
S1 += ABS(level); |
S1 += ABS(level); |
323 |
predictors[i] = level; |
predictors[i] = level; |
324 |
} |
} |
325 |
} |
} else // acpred_direction == 2 |
|
else // acpred_direction == 2 |
|
326 |
{ |
{ |
327 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
328 |
int16_t level; |
int16_t level; |
343 |
|
|
344 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
345 |
|
|
346 |
void apply_acdc(MACROBLOCK *pMB, |
void |
347 |
|
apply_acdc(MACROBLOCK * pMB, |
348 |
uint32_t block, |
uint32_t block, |
349 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
350 |
int16_t predictors[8]) |
int16_t predictors[8]) |
351 |
{ |
{ |
352 |
uint32_t i; |
uint32_t i; |
353 |
|
|
354 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
355 |
{ |
for (i = 1; i < 8; i++) { |
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
356 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
357 |
} |
} |
358 |
} |
} else { |
359 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
360 |
qcoeff[i*8] = predictors[i]; |
qcoeff[i*8] = predictors[i]; |
361 |
} |
} |
362 |
} |
} |
363 |
} |
} |
364 |
|
|
365 |
|
|
366 |
void MBPrediction(MBParam *pParam, |
void |
367 |
|
MBPrediction(FRAMEINFO * frame, |
368 |
uint32_t x, |
uint32_t x, |
369 |
uint32_t y, |
uint32_t y, |
370 |
uint32_t mb_width, |
uint32_t mb_width, |
371 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6 * 64]) |
|
MACROBLOCK *mbs) |
|
372 |
{ |
{ |
373 |
|
|
374 |
int32_t j; |
int32_t j; |
375 |
int32_t iDcScaler, iQuant = pParam->quant; |
int32_t iDcScaler, iQuant = frame->quant; |
376 |
int32_t S = 0; |
int32_t S = 0; |
377 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
378 |
|
|
379 |
MACROBLOCK *pMB = &mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
380 |
|
|
381 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
382 |
|
|
383 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
384 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
385 |
|
|
386 |
predict_acdc(mbs, |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
387 |
x, |
iQuant, iDcScaler, predictors[j], 0); |
388 |
y, |
|
389 |
mb_width, |
S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
|
j, |
|
|
&qcoeff[j*64], |
|
|
iQuant, |
|
|
iDcScaler, |
|
|
predictors[j]); |
|
|
|
|
|
S += calc_acdc(pMB, |
|
|
j, |
|
|
&qcoeff[j*64], |
|
|
iDcScaler, |
|
|
predictors[j]); |
|
390 |
|
|
391 |
} |
} |
392 |
|
|
393 |
if (S < 0) // dont predict |
if (S < 0) // dont predict |
394 |
{ |
{ |
395 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
396 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
397 |
} |
} |
398 |
} |
} else { |
399 |
else |
for (j = 0; j < 6; j++) { |
|
{ |
|
|
for(j = 0; j < 6; j++) |
|
|
{ |
|
400 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
401 |
} |
} |
402 |
} |
} |