[svn] / branches / dev-api-4 / xvidcore / src / prediction / mbprediction.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/prediction/mbprediction.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 926 - (view) (download)

1 : edgomez 851 /******************************************************************************
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 : Isibaar 3
30 : edgomez 851 /******************************************************************************
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 :    
53 :     #include "../global.h"
54 : Isibaar 3 #include "../encoder.h"
55 :     #include "mbprediction.h"
56 :     #include "../utils/mbfunctions.h"
57 :     #include "../bitstream/cbp.h"
58 : edgomez 851 #include "../bitstream/mbcoding.h"
59 :     #include "../bitstream/zigzag.h"
60 : Isibaar 3
61 :    
62 : edgomez 195 static int __inline
63 :     rescale(int predict_quant,
64 :     int current_quant,
65 :     int coeff)
66 : Isibaar 3 {
67 : edgomez 195 return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant),
68 :     (current_quant)) : 0;
69 : Isibaar 3 }
70 :    
71 :    
72 : edgomez 195 static const int16_t default_acdc_values[15] = {
73 : Isibaar 3 1024,
74 : edgomez 78 0, 0, 0, 0, 0, 0, 0,
75 :     0, 0, 0, 0, 0, 0, 0
76 : Isibaar 3 };
77 :    
78 :    
79 :     /* get dc/ac prediction direction for a single block and place
80 :     predictor values into MB->pred_values[j][..]
81 :     */
82 :    
83 :    
84 : edgomez 195 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 248 int16_t predictors[8],
94 : suxen_drol 252 const int bound)
95 : suxen_drol 248
96 : Isibaar 3 {
97 : suxen_drol 252 const int mbpos = (y * mb_width) + x;
98 : edgomez 78 int16_t *left, *top, *diag, *current;
99 : Isibaar 3
100 : edgomez 78 int32_t left_quant = current_quant;
101 :     int32_t top_quant = current_quant;
102 : Isibaar 3
103 : edgomez 78 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 3
107 : edgomez 851 uint32_t index = x + y * mb_width; // current macroblock
108 : edgomez 195 int *acpred_direction = &pMBs[index].acpred_directions[block];
109 : Isibaar 3 uint32_t i;
110 :    
111 :     left = top = diag = current = 0;
112 :    
113 : edgomez 851 // grab left,top and diag macroblocks
114 : Isibaar 3
115 : edgomez 851 // left macroblock
116 : Isibaar 3
117 : suxen_drol 248 if (x && mbpos >= bound + 1 &&
118 : edgomez 195 (pMBs[index - 1].mode == MODE_INTRA ||
119 :     pMBs[index - 1].mode == MODE_INTRA_Q)) {
120 : Isibaar 3
121 :     left = pMBs[index - 1].pred_values[0];
122 :     left_quant = pMBs[index - 1].quant;
123 : edgomez 851 //DEBUGI("LEFT", *(left+MBPRED_SIZE));
124 : Isibaar 3 }
125 : edgomez 851 // top macroblock
126 : Isibaar 3
127 : suxen_drol 252 if (mbpos >= bound + (int)mb_width &&
128 : edgomez 195 (pMBs[index - mb_width].mode == MODE_INTRA ||
129 :     pMBs[index - mb_width].mode == MODE_INTRA_Q)) {
130 :    
131 : Isibaar 3 top = pMBs[index - mb_width].pred_values[0];
132 :     top_quant = pMBs[index - mb_width].quant;
133 : edgomez 78 }
134 : edgomez 851 // diag macroblock
135 : Isibaar 3
136 : suxen_drol 252 if (x && mbpos >= bound + (int)mb_width + 1 &&
137 : edgomez 195 (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
138 :     pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) {
139 :    
140 : Isibaar 3 diag = pMBs[index - 1 - mb_width].pred_values[0];
141 :     }
142 :    
143 : edgomez 78 current = pMBs[index].pred_values[0];
144 : Isibaar 3
145 : edgomez 851 // now grab pLeft, pTop, pDiag _blocks_
146 : edgomez 195
147 : Isibaar 3 switch (block) {
148 : edgomez 195
149 :     case 0:
150 :     if (left)
151 : Isibaar 3 pLeft = left + MBPRED_SIZE;
152 : edgomez 195
153 :     if (top)
154 : Isibaar 3 pTop = top + (MBPRED_SIZE << 1);
155 : edgomez 195
156 :     if (diag)
157 : Isibaar 3 pDiag = diag + 3 * MBPRED_SIZE;
158 : edgomez 195
159 : Isibaar 3 break;
160 : edgomez 195
161 : Isibaar 3 case 1:
162 :     pLeft = current;
163 :     left_quant = current_quant;
164 : edgomez 195
165 :     if (top) {
166 : Isibaar 3 pTop = top + 3 * MBPRED_SIZE;
167 :     pDiag = top + (MBPRED_SIZE << 1);
168 :     }
169 :     break;
170 : edgomez 195
171 : Isibaar 3 case 2:
172 : edgomez 195 if (left) {
173 : Isibaar 3 pLeft = left + 3 * MBPRED_SIZE;
174 :     pDiag = left + MBPRED_SIZE;
175 :     }
176 : edgomez 195
177 : Isibaar 3 pTop = current;
178 :     top_quant = current_quant;
179 :    
180 :     break;
181 : edgomez 195
182 : Isibaar 3 case 3:
183 :     pLeft = current + (MBPRED_SIZE << 1);
184 :     left_quant = current_quant;
185 : edgomez 195
186 : Isibaar 3 pTop = current + MBPRED_SIZE;
187 :     top_quant = current_quant;
188 : edgomez 195
189 : Isibaar 3 pDiag = current;
190 : edgomez 195
191 : Isibaar 3 break;
192 : edgomez 195
193 : Isibaar 3 case 4:
194 : edgomez 195 if (left)
195 : Isibaar 3 pLeft = left + (MBPRED_SIZE << 2);
196 : edgomez 195 if (top)
197 : Isibaar 3 pTop = top + (MBPRED_SIZE << 2);
198 : edgomez 195 if (diag)
199 : Isibaar 3 pDiag = diag + (MBPRED_SIZE << 2);
200 :     break;
201 : edgomez 195
202 : Isibaar 3 case 5:
203 : edgomez 195 if (left)
204 : Isibaar 3 pLeft = left + 5 * MBPRED_SIZE;
205 : edgomez 195 if (top)
206 : Isibaar 3 pTop = top + 5 * MBPRED_SIZE;
207 : edgomez 195 if (diag)
208 : Isibaar 3 pDiag = diag + 5 * MBPRED_SIZE;
209 :     break;
210 :     }
211 :    
212 : edgomez 851 // determine ac prediction direction & ac/dc predictor
213 :     // place rescaled ac/dc predictions into predictors[] for later use
214 : Isibaar 3
215 : edgomez 195 if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {
216 : edgomez 851 *acpred_direction = 1; // vertical
217 :     predictors[0] = DIV_DIV(pTop[0], iDcScaler);
218 : edgomez 195 for (i = 1; i < 8; i++) {
219 : Isibaar 3 predictors[i] = rescale(top_quant, current_quant, pTop[i]);
220 :     }
221 : edgomez 195 } else {
222 : edgomez 851 *acpred_direction = 2; // horizontal
223 :     predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
224 : edgomez 195 for (i = 1; i < 8; i++) {
225 : Isibaar 3 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 195 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 3 {
243 :     uint8_t acpred_direction = pMB->acpred_directions[block];
244 : edgomez 195 int16_t *pCurrent = pMB->pred_values[block];
245 : Isibaar 3 uint32_t i;
246 :    
247 : suxen_drol 252 DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);
248 :    
249 : edgomez 851 dct_codes[0] += predictors[0]; // dc prediction
250 :     pCurrent[0] = dct_codes[0] * iDcScaler;
251 : Isibaar 3
252 : edgomez 195 if (acpred_direction == 1) {
253 :     for (i = 1; i < 8; i++) {
254 : Isibaar 3 int level = dct_codes[i] + predictors[i];
255 : edgomez 195
256 : suxen_drol 252 DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]);
257 :    
258 : Isibaar 3 dct_codes[i] = level;
259 :     pCurrent[i] = level;
260 : edgomez 195 pCurrent[i + 7] = dct_codes[i * 8];
261 : Isibaar 3 }
262 : edgomez 195 } else if (acpred_direction == 2) {
263 :     for (i = 1; i < 8; i++) {
264 :     int level = dct_codes[i * 8] + predictors[i];
265 : suxen_drol 252 DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]);
266 : edgomez 195
267 :     dct_codes[i * 8] = level;
268 :     pCurrent[i + 7] = level;
269 : Isibaar 3 pCurrent[i] = dct_codes[i];
270 :     }
271 : edgomez 195 } else {
272 :     for (i = 1; i < 8; i++) {
273 : Isibaar 3 pCurrent[i] = dct_codes[i];
274 : edgomez 195 pCurrent[i + 7] = dct_codes[i * 8];
275 : Isibaar 3 }
276 :     }
277 :     }
278 :    
279 :    
280 :    
281 : edgomez 851 // ******************************************************************
282 :     // ******************************************************************
283 : Isibaar 3
284 :     /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
285 :    
286 : edgomez 851 returns sum of coeefficients *saved* if prediction is enabled
287 :    
288 : edgomez 78 S1 = sum of all (qcoeff - prediction)
289 :     S2 = sum of all qcoeff
290 :     */
291 : Isibaar 3
292 : edgomez 851 int
293 :     calc_acdc_coeff(MACROBLOCK * pMB,
294 : edgomez 195 uint32_t block,
295 :     int16_t qcoeff[64],
296 :     uint32_t iDcScaler,
297 :     int16_t predictors[8])
298 : Isibaar 3 {
299 : edgomez 195 int16_t *pCurrent = pMB->pred_values[block];
300 : Isibaar 3 uint32_t i;
301 : edgomez 851 int S1 = 0, S2 = 0;
302 : Isibaar 3
303 :    
304 :     /* store current coeffs to pred_values[] for future prediction */
305 :    
306 : edgomez 851 pCurrent[0] = qcoeff[0] * iDcScaler;
307 : edgomez 195 for (i = 1; i < 8; i++) {
308 : Isibaar 3 pCurrent[i] = qcoeff[i];
309 :     pCurrent[i + 7] = qcoeff[i * 8];
310 : edgomez 78 }
311 : Isibaar 3
312 :     /* subtract predictors and store back in predictors[] */
313 :    
314 :     qcoeff[0] = qcoeff[0] - predictors[0];
315 :    
316 : edgomez 195 if (pMB->acpred_directions[block] == 1) {
317 :     for (i = 1; i < 8; i++) {
318 : Isibaar 3 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 851 } else // acpred_direction == 2
327 : Isibaar 3 {
328 : edgomez 195 for (i = 1; i < 8; i++) {
329 : Isibaar 3 int16_t level;
330 :    
331 : edgomez 195 level = qcoeff[i * 8];
332 : Isibaar 3 S2 += ABS(level);
333 :     level -= predictors[i];
334 :     S1 += ABS(level);
335 :     predictors[i] = level;
336 :     }
337 :    
338 : edgomez 78 }
339 : Isibaar 3
340 : edgomez 195
341 : edgomez 78 return S2 - S1;
342 : Isibaar 3 }
343 :    
344 :    
345 : edgomez 851
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 3 /* apply predictors[] to qcoeff */
413 :    
414 : edgomez 195 void
415 :     apply_acdc(MACROBLOCK * pMB,
416 :     uint32_t block,
417 :     int16_t qcoeff[64],
418 :     int16_t predictors[8])
419 : Isibaar 3 {
420 : edgomez 851 unsigned int i;
421 : Isibaar 3
422 : edgomez 195 if (pMB->acpred_directions[block] == 1) {
423 : edgomez 851 for (i = 1; i < 8; i++)
424 : Isibaar 3 qcoeff[i] = predictors[i];
425 : edgomez 195 } else {
426 : edgomez 851 for (i = 1; i < 8; i++)
427 : edgomez 195 qcoeff[i * 8] = predictors[i];
428 : edgomez 78 }
429 : Isibaar 3 }
430 :    
431 :    
432 : edgomez 195 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 3 {
439 : edgomez 78
440 :     int32_t j;
441 : suxen_drol 926 int32_t iDcScaler, iQuant;
442 : edgomez 851 int S = 0;
443 : Isibaar 3 int16_t predictors[6][8];
444 :    
445 : suxen_drol 136 MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];
446 : suxen_drol 926 iQuant = pMB->quant;
447 : Isibaar 3
448 : edgomez 78 if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {
449 : edgomez 195
450 :     for (j = 0; j < 6; j++) {
451 : edgomez 851 iDcScaler = get_dc_scaler(iQuant, j<4);
452 : Isibaar 3
453 : edgomez 195 predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],
454 : suxen_drol 252 iQuant, iDcScaler, predictors[j], 0);
455 : edgomez 78
456 : suxen_drol 890 if ((frame->vop_flags & XVID_HQACPRED))
457 : edgomez 851 S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
458 :     else
459 :     S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
460 : edgomez 78
461 : Isibaar 3 }
462 :    
463 : edgomez 851 if (S<=0) { // dont predict
464 :     for (j = 0; j < 6; j++)
465 : Isibaar 3 pMB->acpred_directions[j] = 0;
466 : edgomez 851 }else{
467 :     for (j = 0; j < 6; j++)
468 : edgomez 195 apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]);
469 : Isibaar 3 }
470 : edgomez 851
471 : Isibaar 3 pMB->cbp = calc_cbp(qcoeff);
472 :     }
473 : edgomez 78
474 : Isibaar 3 }

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