[cvs] / xvidcore / src / prediction / mbprediction.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/prediction/mbprediction.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13.2.1 - (view) (download)

1 : edgomez 1.13 /******************************************************************************
2 :     * *
3 :     * This file is part of XviD, a free MPEG-4 video encoder/decoder *
4 :     * *
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 *
7 :     * software module in hardware or software products are advised that its *
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 *
10 :     * software module and his/her company, and subsequent editors and their *
11 :     * companies, will have no liability for use of this software or *
12 :     * modifications or derivatives thereof. *
13 :     * *
14 :     * XviD is free software; you can redistribute it and/or modify it *
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 *
17 :     * (at your option) any later version. *
18 :     * *
19 :     * XviD is distributed in the hope that it will be useful, but *
20 :     * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 :     * GNU General Public License for more details. *
23 :     * *
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 *
26 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
27 :     * *
28 :     ******************************************************************************/
29 :    
30 :     /******************************************************************************
31 :     * *
32 :     * mbprediction.c *
33 :     * *
34 :     * Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> *
35 :     * Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> *
36 :     * *
37 :     * For more information visit the XviD homepage: http://www.xvid.org *
38 :     * *
39 :     ******************************************************************************/
40 :    
41 :     /******************************************************************************
42 :     * *
43 :     * Revision history: *
44 :     * *
45 :     * 29.06.2002 predict_acdc() bounding *
46 :     * 12.12.2001 improved calc_acdc_prediction; removed need for memcpy *
47 :     * 15.12.2001 moved pmv displacement to motion estimation *
48 :     * 30.11.2001 mmx cbp support *
49 :     * 17.11.2001 initial version *
50 :     * *
51 :     ******************************************************************************/
52 : Isibaar 1.1
53 : edgomez 1.13 #include "../global.h"
54 : Isibaar 1.1 #include "../encoder.h"
55 :     #include "mbprediction.h"
56 :     #include "../utils/mbfunctions.h"
57 :     #include "../bitstream/cbp.h"
58 : edgomez 1.13 #include "../bitstream/mbcoding.h"
59 :     #include "../bitstream/zigzag.h"
60 : Isibaar 1.1
61 :    
62 : edgomez 1.4 static int __inline
63 :     rescale(int predict_quant,
64 :     int current_quant,
65 :     int coeff)
66 : Isibaar 1.1 {
67 : edgomez 1.4 return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant),
68 :     (current_quant)) : 0;
69 : Isibaar 1.1 }
70 :    
71 :    
72 : edgomez 1.4 static const int16_t default_acdc_values[15] = {
73 : Isibaar 1.1 1024,
74 : edgomez 1.2 0, 0, 0, 0, 0, 0, 0,
75 :     0, 0, 0, 0, 0, 0, 0
76 : Isibaar 1.1 };
77 : edgomez 1.8
78 :    
79 : Isibaar 1.1 /* get dc/ac prediction direction for a single block and place
80 :     predictor values into MB->pred_values[j][..]
81 :     */
82 :    
83 :    
84 : edgomez 1.4 void
85 :     predict_acdc(MACROBLOCK * pMBs,
86 :     uint32_t x,
87 :     uint32_t y,
88 :     uint32_t mb_width,
89 :     uint32_t block,
90 :     int16_t qcoeff[64],
91 :     uint32_t current_quant,
92 :     int32_t iDcScaler,
93 : suxen_drol 1.5 int16_t predictors[8],
94 : suxen_drol 1.6 const int bound)
95 : suxen_drol 1.5
96 : Isibaar 1.1 {
97 : suxen_drol 1.6 const int mbpos = (y * mb_width) + x;
98 : edgomez 1.2 int16_t *left, *top, *diag, *current;
99 : Isibaar 1.1
100 : edgomez 1.2 int32_t left_quant = current_quant;
101 :     int32_t top_quant = current_quant;
102 : Isibaar 1.1
103 : edgomez 1.2 const int16_t *pLeft = default_acdc_values;
104 :     const int16_t *pTop = default_acdc_values;
105 :     const int16_t *pDiag = default_acdc_values;
106 : Isibaar 1.1
107 : edgomez 1.13 uint32_t index = x + y * mb_width; // current macroblock
108 : edgomez 1.4 int *acpred_direction = &pMBs[index].acpred_directions[block];
109 : Isibaar 1.1 uint32_t i;
110 :    
111 :     left = top = diag = current = 0;
112 :    
113 : edgomez 1.13 // grab left,top and diag macroblocks
114 : Isibaar 1.1
115 : edgomez 1.13 // left macroblock
116 : Isibaar 1.1
117 : suxen_drol 1.5 if (x && mbpos >= bound + 1 &&
118 : edgomez 1.4 (pMBs[index - 1].mode == MODE_INTRA ||
119 :     pMBs[index - 1].mode == MODE_INTRA_Q)) {
120 : Isibaar 1.1
121 :     left = pMBs[index - 1].pred_values[0];
122 :     left_quant = pMBs[index - 1].quant;
123 : edgomez 1.13 //DEBUGI("LEFT", *(left+MBPRED_SIZE));
124 : Isibaar 1.1 }
125 : edgomez 1.13 // top macroblock
126 : edgomez 1.4
127 : suxen_drol 1.6 if (mbpos >= bound + (int)mb_width &&
128 : edgomez 1.4 (pMBs[index - mb_width].mode == MODE_INTRA ||
129 :     pMBs[index - mb_width].mode == MODE_INTRA_Q)) {
130 : Isibaar 1.1
131 :     top = pMBs[index - mb_width].pred_values[0];
132 :     top_quant = pMBs[index - mb_width].quant;
133 : edgomez 1.2 }
134 : edgomez 1.13 // diag macroblock
135 : edgomez 1.4
136 : suxen_drol 1.6 if (x && mbpos >= bound + (int)mb_width + 1 &&
137 : edgomez 1.4 (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
138 :     pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) {
139 : Isibaar 1.1
140 :     diag = pMBs[index - 1 - mb_width].pred_values[0];
141 :     }
142 :    
143 : edgomez 1.2 current = pMBs[index].pred_values[0];
144 : Isibaar 1.1
145 : edgomez 1.13 // now grab pLeft, pTop, pDiag _blocks_
146 : edgomez 1.4
147 : Isibaar 1.1 switch (block) {
148 : edgomez 1.4
149 :     case 0:
150 :     if (left)
151 : Isibaar 1.1 pLeft = left + MBPRED_SIZE;
152 : edgomez 1.4
153 :     if (top)
154 : Isibaar 1.1 pTop = top + (MBPRED_SIZE << 1);
155 : edgomez 1.4
156 :     if (diag)
157 : Isibaar 1.1 pDiag = diag + 3 * MBPRED_SIZE;
158 : edgomez 1.4
159 : Isibaar 1.1 break;
160 : edgomez 1.4
161 : Isibaar 1.1 case 1:
162 :     pLeft = current;
163 :     left_quant = current_quant;
164 : edgomez 1.4
165 :     if (top) {
166 : Isibaar 1.1 pTop = top + 3 * MBPRED_SIZE;
167 :     pDiag = top + (MBPRED_SIZE << 1);
168 :     }
169 :     break;
170 : edgomez 1.4
171 : Isibaar 1.1 case 2:
172 : edgomez 1.4 if (left) {
173 : Isibaar 1.1 pLeft = left + 3 * MBPRED_SIZE;
174 :     pDiag = left + MBPRED_SIZE;
175 :     }
176 : edgomez 1.4
177 : Isibaar 1.1 pTop = current;
178 :     top_quant = current_quant;
179 :    
180 :     break;
181 : edgomez 1.4
182 : Isibaar 1.1 case 3:
183 :     pLeft = current + (MBPRED_SIZE << 1);
184 :     left_quant = current_quant;
185 : edgomez 1.4
186 : Isibaar 1.1 pTop = current + MBPRED_SIZE;
187 :     top_quant = current_quant;
188 : edgomez 1.4
189 : Isibaar 1.1 pDiag = current;
190 : edgomez 1.4
191 : Isibaar 1.1 break;
192 : edgomez 1.4
193 : Isibaar 1.1 case 4:
194 : edgomez 1.4 if (left)
195 : Isibaar 1.1 pLeft = left + (MBPRED_SIZE << 2);
196 : edgomez 1.4 if (top)
197 : Isibaar 1.1 pTop = top + (MBPRED_SIZE << 2);
198 : edgomez 1.4 if (diag)
199 : Isibaar 1.1 pDiag = diag + (MBPRED_SIZE << 2);
200 :     break;
201 : edgomez 1.4
202 : Isibaar 1.1 case 5:
203 : edgomez 1.4 if (left)
204 : Isibaar 1.1 pLeft = left + 5 * MBPRED_SIZE;
205 : edgomez 1.4 if (top)
206 : Isibaar 1.1 pTop = top + 5 * MBPRED_SIZE;
207 : edgomez 1.4 if (diag)
208 : Isibaar 1.1 pDiag = diag + 5 * MBPRED_SIZE;
209 :     break;
210 :     }
211 :    
212 : edgomez 1.13 // determine ac prediction direction & ac/dc predictor
213 :     // place rescaled ac/dc predictions into predictors[] for later use
214 : Isibaar 1.1
215 : edgomez 1.4 if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {
216 : edgomez 1.13 *acpred_direction = 1; // vertical
217 :     predictors[0] = DIV_DIV(pTop[0], iDcScaler);
218 : edgomez 1.4 for (i = 1; i < 8; i++) {
219 : Isibaar 1.1 predictors[i] = rescale(top_quant, current_quant, pTop[i]);
220 :     }
221 : edgomez 1.4 } else {
222 : edgomez 1.13 *acpred_direction = 2; // horizontal
223 :     predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
224 : edgomez 1.4 for (i = 1; i < 8; i++) {
225 : Isibaar 1.1 predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);
226 :     }
227 :     }
228 :     }
229 :    
230 :    
231 :     /* decoder: add predictors to dct_codes[] and
232 :     store current coeffs to pred_values[] for future prediction
233 :     */
234 :    
235 :    
236 : edgomez 1.4 void
237 :     add_acdc(MACROBLOCK * pMB,
238 :     uint32_t block,
239 :     int16_t dct_codes[64],
240 :     uint32_t iDcScaler,
241 :     int16_t predictors[8])
242 : Isibaar 1.1 {
243 :     uint8_t acpred_direction = pMB->acpred_directions[block];
244 : edgomez 1.4 int16_t *pCurrent = pMB->pred_values[block];
245 : Isibaar 1.1 uint32_t i;
246 :    
247 : suxen_drol 1.6 DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);
248 :    
249 : edgomez 1.13 dct_codes[0] += predictors[0]; // dc prediction
250 :     pCurrent[0] = dct_codes[0] * iDcScaler;
251 : Isibaar 1.1
252 : edgomez 1.4 if (acpred_direction == 1) {
253 :     for (i = 1; i < 8; i++) {
254 : Isibaar 1.1 int level = dct_codes[i] + predictors[i];
255 : edgomez 1.4
256 : suxen_drol 1.6 DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]);
257 :    
258 : Isibaar 1.1 dct_codes[i] = level;
259 :     pCurrent[i] = level;
260 : edgomez 1.4 pCurrent[i + 7] = dct_codes[i * 8];
261 : Isibaar 1.1 }
262 : edgomez 1.4 } else if (acpred_direction == 2) {
263 :     for (i = 1; i < 8; i++) {
264 :     int level = dct_codes[i * 8] + predictors[i];
265 : suxen_drol 1.6 DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]);
266 : edgomez 1.4
267 :     dct_codes[i * 8] = level;
268 :     pCurrent[i + 7] = level;
269 : Isibaar 1.1 pCurrent[i] = dct_codes[i];
270 :     }
271 : edgomez 1.4 } else {
272 :     for (i = 1; i < 8; i++) {
273 : Isibaar 1.1 pCurrent[i] = dct_codes[i];
274 : edgomez 1.4 pCurrent[i + 7] = dct_codes[i * 8];
275 : Isibaar 1.1 }
276 :     }
277 :     }
278 :    
279 :    
280 :    
281 : edgomez 1.13 // ******************************************************************
282 :     // ******************************************************************
283 : Isibaar 1.1
284 :     /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
285 :    
286 : edgomez 1.13 returns sum of coeefficients *saved* if prediction is enabled
287 :    
288 : edgomez 1.2 S1 = sum of all (qcoeff - prediction)
289 :     S2 = sum of all qcoeff
290 :     */
291 : Isibaar 1.1
292 : edgomez 1.13 int
293 :     calc_acdc_coeff(MACROBLOCK * pMB,
294 : edgomez 1.4 uint32_t block,
295 :     int16_t qcoeff[64],
296 :     uint32_t iDcScaler,
297 :     int16_t predictors[8])
298 : Isibaar 1.1 {
299 : edgomez 1.4 int16_t *pCurrent = pMB->pred_values[block];
300 : Isibaar 1.1 uint32_t i;
301 : edgomez 1.13 int S1 = 0, S2 = 0;
302 : Isibaar 1.1
303 :    
304 :     /* store current coeffs to pred_values[] for future prediction */
305 :    
306 : edgomez 1.13 pCurrent[0] = qcoeff[0] * iDcScaler;
307 : edgomez 1.4 for (i = 1; i < 8; i++) {
308 : Isibaar 1.1 pCurrent[i] = qcoeff[i];
309 :     pCurrent[i + 7] = qcoeff[i * 8];
310 : edgomez 1.2 }
311 : Isibaar 1.1
312 :     /* subtract predictors and store back in predictors[] */
313 :    
314 :     qcoeff[0] = qcoeff[0] - predictors[0];
315 :    
316 : edgomez 1.4 if (pMB->acpred_directions[block] == 1) {
317 :     for (i = 1; i < 8; i++) {
318 : Isibaar 1.1 int16_t level;
319 :    
320 :     level = qcoeff[i];
321 :     S2 += ABS(level);
322 :     level -= predictors[i];
323 :     S1 += ABS(level);
324 :     predictors[i] = level;
325 :     }
326 : edgomez 1.13 } else // acpred_direction == 2
327 : Isibaar 1.1 {
328 : edgomez 1.4 for (i = 1; i < 8; i++) {
329 : Isibaar 1.1 int16_t level;
330 :    
331 : edgomez 1.4 level = qcoeff[i * 8];
332 : Isibaar 1.1 S2 += ABS(level);
333 :     level -= predictors[i];
334 :     S1 += ABS(level);
335 :     predictors[i] = level;
336 :     }
337 :    
338 : edgomez 1.2 }
339 : Isibaar 1.1
340 : edgomez 1.4
341 : edgomez 1.2 return S2 - S1;
342 : Isibaar 1.1 }
343 :    
344 :    
345 : edgomez 1.13
346 :     /* returns the bits *saved* if prediction is enabled */
347 :    
348 :     int
349 :     calc_acdc_bits(MACROBLOCK * pMB,
350 :     uint32_t block,
351 :     int16_t qcoeff[64],
352 :     uint32_t iDcScaler,
353 :     int16_t predictors[8])
354 :     {
355 :     const int direction = pMB->acpred_directions[block];
356 :     int16_t *pCurrent = pMB->pred_values[block];
357 :     int16_t tmp[8];
358 :     unsigned int i;
359 :     int Z1, Z2;
360 :    
361 :     /* store current coeffs to pred_values[] for future prediction */
362 :     pCurrent[0] = qcoeff[0] * iDcScaler;
363 :     for (i = 1; i < 8; i++) {
364 :     pCurrent[i] = qcoeff[i];
365 :     pCurrent[i + 7] = qcoeff[i * 8];
366 :     }
367 :    
368 :    
369 :     /* dc prediction */
370 :     qcoeff[0] = qcoeff[0] - predictors[0];
371 :    
372 :     /* calc cost before ac prediction */
373 :     #ifdef BIGLUT
374 :     Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1);
375 :     #else
376 :     Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]);
377 :     #endif
378 :    
379 :     /* apply ac prediction & calc cost*/
380 :     if (direction == 1) {
381 :     for (i = 1; i < 8; i++) {
382 :     tmp[i] = qcoeff[i];
383 :     qcoeff[i] -= predictors[i];
384 :     predictors[i] = qcoeff[i];
385 :     }
386 :     }else{ // acpred_direction == 2
387 :     for (i = 1; i < 8; i++) {
388 :     tmp[i] = qcoeff[i*8];
389 :     qcoeff[i*8] -= predictors[i];
390 :     predictors[i] = qcoeff[i*8];
391 :     }
392 :     }
393 :    
394 :     #ifdef BIGLUT
395 :     Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1);
396 :     #else
397 :     Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]);
398 :     #endif
399 :    
400 :     /* undo prediction */
401 :     if (direction == 1) {
402 :     for (i = 1; i < 8; i++)
403 :     qcoeff[i] = tmp[i];
404 :     }else{ // acpred_direction == 2
405 :     for (i = 1; i < 8; i++)
406 :     qcoeff[i*8] = tmp[i];
407 :     }
408 :    
409 :     return Z2-Z1;
410 :     }
411 :    
412 : Isibaar 1.1 /* apply predictors[] to qcoeff */
413 :    
414 : edgomez 1.4 void
415 :     apply_acdc(MACROBLOCK * pMB,
416 :     uint32_t block,
417 :     int16_t qcoeff[64],
418 :     int16_t predictors[8])
419 : Isibaar 1.1 {
420 : edgomez 1.13 unsigned int i;
421 : Isibaar 1.1
422 : edgomez 1.4 if (pMB->acpred_directions[block] == 1) {
423 : edgomez 1.13 for (i = 1; i < 8; i++)
424 : Isibaar 1.1 qcoeff[i] = predictors[i];
425 : edgomez 1.4 } else {
426 : edgomez 1.13 for (i = 1; i < 8; i++)
427 : edgomez 1.4 qcoeff[i * 8] = predictors[i];
428 : edgomez 1.2 }
429 : Isibaar 1.1 }
430 :    
431 :    
432 : edgomez 1.4 void
433 :     MBPrediction(FRAMEINFO * frame,
434 :     uint32_t x,
435 :     uint32_t y,
436 :     uint32_t mb_width,
437 :     int16_t qcoeff[6 * 64])
438 : Isibaar 1.1 {
439 : edgomez 1.2
440 :     int32_t j;
441 : suxen_drol 1.3 int32_t iDcScaler, iQuant = frame->quant;
442 : edgomez 1.13 int S = 0;
443 : Isibaar 1.1 int16_t predictors[6][8];
444 :    
445 : suxen_drol 1.3 MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];
446 : Isibaar 1.1
447 : edgomez 1.2 if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {
448 : edgomez 1.4
449 :     for (j = 0; j < 6; j++) {
450 : edgomez 1.13 iDcScaler = get_dc_scaler(iQuant, j<4);
451 : Isibaar 1.1
452 : edgomez 1.4 predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],
453 : suxen_drol 1.6 iQuant, iDcScaler, predictors[j], 0);
454 : edgomez 1.4
455 : suxen_drol 1.13.2.1 if ((frame->vop_flags & XVID_HQACPRED))
456 : edgomez 1.13 S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
457 :     else
458 :     S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
459 : edgomez 1.2
460 : Isibaar 1.1 }
461 :    
462 : edgomez 1.13 if (S<=0) { // dont predict
463 :     for (j = 0; j < 6; j++)
464 : Isibaar 1.1 pMB->acpred_directions[j] = 0;
465 : edgomez 1.13 }else{
466 :     for (j = 0; j < 6; j++)
467 : edgomez 1.4 apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]);
468 : Isibaar 1.1 }
469 : edgomez 1.13
470 : Isibaar 1.1 pMB->cbp = calc_cbp(qcoeff);
471 :     }
472 : edgomez 1.2
473 : suxen_drol 1.6 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4