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 file is part of XviD, a free MPEG-4 video encoder/decoder |
10 |
* software module and his/her company, and subsequent editors and their * |
* |
11 |
* companies, will have no liability for use of this software or * |
* XviD is free software; you can redistribute it and/or modify it |
12 |
* modifications or derivatives thereof. * |
* under the terms of the GNU General Public License as published by |
13 |
* * |
* the Free Software Foundation; either version 2 of the License, or |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* (at your option) any later version. |
15 |
* under the terms of the GNU General Public License as published by * |
* |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* This program is distributed in the hope that it will be useful, |
17 |
* (at your option) any later version. * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
* * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* GNU General Public License for more details. |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* You should have received a copy of the GNU General Public License |
22 |
* GNU General Public License for more details. * |
* along with this program; if not, write to the Free Software |
23 |
* * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
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 * |
* Under section 8 of the GNU General Public License, the copyright |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* holders of XVID explicitly forbid distribution in the following |
27 |
* * |
* countries: |
28 |
******************************************************************************/ |
* |
29 |
|
* - Japan |
30 |
/****************************************************************************** |
* - United States of America |
31 |
* * |
* |
32 |
* mbprediction.c * |
* Linking XviD statically or dynamically with other modules is making a |
33 |
* * |
* combined work based on XviD. Thus, the terms and conditions of the |
34 |
* Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> * |
* GNU General Public License cover the whole combination. |
35 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* |
36 |
* * |
* As a special exception, the copyright holders of XviD give you |
37 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* permission to link XviD with independent modules that communicate with |
38 |
* * |
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
39 |
******************************************************************************/ |
* license terms of these independent modules, and to copy and distribute |
40 |
|
* the resulting combined work under terms of your choice, provided that |
41 |
/****************************************************************************** |
* every copy of the combined work is accompanied by a complete copy of |
42 |
* * |
* the source code of XviD (the version of XviD used to produce the |
43 |
* Revision history: * |
* combined work), being distributed under the terms of the GNU General |
44 |
* * |
* Public License plus this exception. An independent module is a module |
45 |
* 12.12.2001 improved calc_acdc_prediction; removed need for memcpy * |
* which is not derived from or based on XviD. |
46 |
* 15.12.2001 moved pmv displacement to motion estimation * |
* |
47 |
* 30.11.2001 mmx cbp support * |
* Note that people who make modified versions of XviD are not obligated |
48 |
* 17.11.2001 initial version * |
* to grant this special exception for their modified versions; it is |
49 |
* * |
* their choice whether to do so. The GNU General Public License gives |
50 |
******************************************************************************/ |
* permission to release a modified version without this exception; this |
51 |
|
* exception also makes it possible to release a modified version which |
52 |
|
* carries forward this exception. |
53 |
|
* |
54 |
|
* $Id$ |
55 |
|
* |
56 |
|
****************************************************************************/ |
57 |
|
|
58 |
#include "../encoder.h" |
#include "../encoder.h" |
59 |
#include "mbprediction.h" |
#include "mbprediction.h" |
65 |
#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) ) |
66 |
|
|
67 |
|
|
68 |
static int __inline rescale(int predict_quant, int current_quant, int coeff) |
/***************************************************************************** |
69 |
|
* Local inlined function |
70 |
|
****************************************************************************/ |
71 |
|
|
72 |
|
static int __inline |
73 |
|
rescale(int predict_quant, |
74 |
|
int current_quant, |
75 |
|
int coeff) |
76 |
{ |
{ |
77 |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0; |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), |
78 |
|
(current_quant)) : 0; |
79 |
} |
} |
80 |
|
|
81 |
|
|
82 |
|
/***************************************************************************** |
83 |
|
* Local data |
84 |
|
****************************************************************************/ |
85 |
|
|
86 |
static const int16_t default_acdc_values[15] = { |
static const int16_t default_acdc_values[15] = { |
87 |
1024, |
1024, |
88 |
0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, |
90 |
}; |
}; |
91 |
|
|
92 |
|
|
93 |
|
/***************************************************************************** |
94 |
|
* Functions |
95 |
|
****************************************************************************/ |
96 |
|
|
97 |
|
|
98 |
/* get dc/ac prediction direction for a single block and place |
/* get dc/ac prediction direction for a single block and place |
99 |
predictor values into MB->pred_values[j][..] |
predictor values into MB->pred_values[j][..] |
100 |
*/ |
*/ |
101 |
|
|
102 |
|
|
103 |
void predict_acdc(MACROBLOCK *pMBs, |
void |
104 |
uint32_t x, uint32_t y, uint32_t mb_width, |
predict_acdc(MACROBLOCK * pMBs, |
105 |
|
uint32_t x, |
106 |
|
uint32_t y, |
107 |
|
uint32_t mb_width, |
108 |
uint32_t block, |
uint32_t block, |
109 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
110 |
uint32_t current_quant, |
uint32_t current_quant, |
111 |
int32_t iDcScaler, |
int32_t iDcScaler, |
112 |
int16_t predictors[8]) |
int16_t predictors[8], |
113 |
|
const int bound) |
114 |
|
|
115 |
{ |
{ |
116 |
|
const int mbpos = (y * mb_width) + x; |
117 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
118 |
|
|
119 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
133 |
|
|
134 |
// left macroblock |
// left macroblock |
135 |
|
|
136 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
137 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
138 |
|
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
139 |
|
|
140 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
141 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
142 |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
143 |
} |
} |
|
|
|
144 |
// top macroblock |
// top macroblock |
145 |
|
|
146 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
if (mbpos >= bound + (int)mb_width && |
147 |
|| pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - mb_width].mode == MODE_INTRA || |
148 |
|
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
149 |
|
|
150 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
151 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
152 |
} |
} |
|
|
|
153 |
// diag macroblock |
// diag macroblock |
154 |
|
|
155 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
if (x && mbpos >= bound + (int)mb_width + 1 && |
156 |
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
157 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
158 |
|
|
159 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
160 |
} |
} |
234 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
235 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; // vertical |
236 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
237 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
238 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
239 |
} |
} |
240 |
} |
} else { |
|
else |
|
|
{ |
|
241 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; // horizontal |
242 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
243 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
244 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
245 |
} |
} |
246 |
} |
} |
252 |
*/ |
*/ |
253 |
|
|
254 |
|
|
255 |
void add_acdc(MACROBLOCK *pMB, |
void |
256 |
|
add_acdc(MACROBLOCK * pMB, |
257 |
uint32_t block, |
uint32_t block, |
258 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
259 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
263 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
264 |
uint32_t i; |
uint32_t i; |
265 |
|
|
266 |
|
DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]); |
267 |
|
|
268 |
dct_codes[0] += predictors[0]; // dc prediction |
dct_codes[0] += predictors[0]; // dc prediction |
269 |
pCurrent[0] = dct_codes[0] * iDcScaler; |
pCurrent[0] = dct_codes[0] * iDcScaler; |
270 |
|
|
271 |
if (acpred_direction == 1) |
if (acpred_direction == 1) { |
272 |
{ |
for (i = 1; i < 8; i++) { |
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
273 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
274 |
|
|
275 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]); |
276 |
|
|
277 |
dct_codes[i] = level; |
dct_codes[i] = level; |
278 |
pCurrent[i] = level; |
pCurrent[i] = level; |
279 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
280 |
} |
} |
281 |
} |
} else if (acpred_direction == 2) { |
282 |
else if (acpred_direction == 2) |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
283 |
int level = dct_codes[i*8] + predictors[i]; |
int level = dct_codes[i*8] + predictors[i]; |
284 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]); |
285 |
|
|
286 |
dct_codes[i*8] = level; |
dct_codes[i*8] = level; |
287 |
pCurrent[i+7] = level; |
pCurrent[i+7] = level; |
288 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
289 |
} |
} |
290 |
} |
} else { |
291 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
292 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
293 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
294 |
} |
} |
310 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
311 |
*/ |
*/ |
312 |
|
|
313 |
uint32_t calc_acdc(MACROBLOCK *pMB, |
uint32_t |
314 |
|
calc_acdc(MACROBLOCK * pMB, |
315 |
uint32_t block, |
uint32_t block, |
316 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
317 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
334 |
|
|
335 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
336 |
|
|
337 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
|
{ |
|
338 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
339 |
int16_t level; |
int16_t level; |
340 |
|
|
344 |
S1 += ABS(level); |
S1 += ABS(level); |
345 |
predictors[i] = level; |
predictors[i] = level; |
346 |
} |
} |
347 |
} |
} else // acpred_direction == 2 |
|
else // acpred_direction == 2 |
|
348 |
{ |
{ |
349 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
350 |
int16_t level; |
int16_t level; |
365 |
|
|
366 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
367 |
|
|
368 |
void apply_acdc(MACROBLOCK *pMB, |
void |
369 |
|
apply_acdc(MACROBLOCK * pMB, |
370 |
uint32_t block, |
uint32_t block, |
371 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
372 |
int16_t predictors[8]) |
int16_t predictors[8]) |
373 |
{ |
{ |
374 |
uint32_t i; |
uint32_t i; |
375 |
|
|
376 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
377 |
{ |
for (i = 1; i < 8; i++) { |
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
378 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
379 |
} |
} |
380 |
} |
} else { |
381 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
382 |
qcoeff[i*8] = predictors[i]; |
qcoeff[i*8] = predictors[i]; |
383 |
} |
} |
384 |
} |
} |
385 |
} |
} |
386 |
|
|
387 |
|
|
388 |
void MBPrediction(FRAMEINFO *frame, |
void |
389 |
|
MBPrediction(FRAMEINFO * frame, |
390 |
uint32_t x, |
uint32_t x, |
391 |
uint32_t y, |
uint32_t y, |
392 |
uint32_t mb_width, |
uint32_t mb_width, |
402 |
|
|
403 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
404 |
|
|
405 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
406 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
407 |
|
|
408 |
predict_acdc(frame->mbs, |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
409 |
x, |
iQuant, iDcScaler, predictors[j], 0); |
410 |
y, |
|
411 |
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]); |
|
412 |
|
|
413 |
} |
} |
414 |
|
|
415 |
if (S < 0) // dont predict |
if (S < 0) // dont predict |
416 |
{ |
{ |
417 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
418 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
419 |
} |
} |
420 |
} |
} else { |
421 |
else |
for (j = 0; j < 6; j++) { |
|
{ |
|
|
for(j = 0; j < 6; j++) |
|
|
{ |
|
422 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
423 |
} |
} |
424 |
} |
} |