[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.7.2.1 - (view) (download)

1 : Isibaar 1.1 /******************************************************************************
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 : suxen_drol 1.5 * 29.06.2002 predict_acdc() bounding *
46 : Isibaar 1.1 * 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 : suxen_drol 1.7.2.1 #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 :    
59 :    
60 : edgomez 1.4 static int __inline
61 :     rescale(int predict_quant,
62 :     int current_quant,
63 :     int coeff)
64 : Isibaar 1.1 {
65 : edgomez 1.4 return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant),
66 :     (current_quant)) : 0;
67 : Isibaar 1.1 }
68 :    
69 :    
70 : edgomez 1.4 static const int16_t default_acdc_values[15] = {
71 : Isibaar 1.1 1024,
72 : edgomez 1.2 0, 0, 0, 0, 0, 0, 0,
73 :     0, 0, 0, 0, 0, 0, 0
74 : Isibaar 1.1 };
75 :    
76 :    
77 :     /* get dc/ac prediction direction for a single block and place
78 :     predictor values into MB->pred_values[j][..]
79 :     */
80 :    
81 :    
82 : edgomez 1.4 void
83 :     predict_acdc(MACROBLOCK * pMBs,
84 :     uint32_t x,
85 :     uint32_t y,
86 :     uint32_t mb_width,
87 :     uint32_t block,
88 :     int16_t qcoeff[64],
89 :     uint32_t current_quant,
90 :     int32_t iDcScaler,
91 : suxen_drol 1.5 int16_t predictors[8],
92 : suxen_drol 1.6 const int bound)
93 : suxen_drol 1.5
94 : Isibaar 1.1 {
95 : suxen_drol 1.6 const int mbpos = (y * mb_width) + x;
96 : edgomez 1.2 int16_t *left, *top, *diag, *current;
97 : Isibaar 1.1
98 : edgomez 1.2 int32_t left_quant = current_quant;
99 :     int32_t top_quant = current_quant;
100 : Isibaar 1.1
101 : edgomez 1.2 const int16_t *pLeft = default_acdc_values;
102 :     const int16_t *pTop = default_acdc_values;
103 :     const int16_t *pDiag = default_acdc_values;
104 : Isibaar 1.1
105 : edgomez 1.4 uint32_t index = x + y * mb_width; // current macroblock
106 :     int *acpred_direction = &pMBs[index].acpred_directions[block];
107 : Isibaar 1.1 uint32_t i;
108 :    
109 :     left = top = diag = current = 0;
110 :    
111 :     // grab left,top and diag macroblocks
112 :    
113 :     // left macroblock
114 :    
115 : suxen_drol 1.5 if (x && mbpos >= bound + 1 &&
116 : edgomez 1.4 (pMBs[index - 1].mode == MODE_INTRA ||
117 :     pMBs[index - 1].mode == MODE_INTRA_Q)) {
118 : Isibaar 1.1
119 :     left = pMBs[index - 1].pred_values[0];
120 :     left_quant = pMBs[index - 1].quant;
121 :     //DEBUGI("LEFT", *(left+MBPRED_SIZE));
122 :     }
123 :     // top macroblock
124 : edgomez 1.4
125 : suxen_drol 1.6 if (mbpos >= bound + (int)mb_width &&
126 : edgomez 1.4 (pMBs[index - mb_width].mode == MODE_INTRA ||
127 :     pMBs[index - mb_width].mode == MODE_INTRA_Q)) {
128 : Isibaar 1.1
129 :     top = pMBs[index - mb_width].pred_values[0];
130 :     top_quant = pMBs[index - mb_width].quant;
131 : edgomez 1.2 }
132 : Isibaar 1.1 // diag macroblock
133 : edgomez 1.4
134 : suxen_drol 1.6 if (x && mbpos >= bound + (int)mb_width + 1 &&
135 : edgomez 1.4 (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
136 :     pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) {
137 : Isibaar 1.1
138 :     diag = pMBs[index - 1 - mb_width].pred_values[0];
139 :     }
140 :    
141 : edgomez 1.2 current = pMBs[index].pred_values[0];
142 : Isibaar 1.1
143 :     // now grab pLeft, pTop, pDiag _blocks_
144 : edgomez 1.4
145 : Isibaar 1.1 switch (block) {
146 : edgomez 1.4
147 :     case 0:
148 :     if (left)
149 : Isibaar 1.1 pLeft = left + MBPRED_SIZE;
150 : edgomez 1.4
151 :     if (top)
152 : Isibaar 1.1 pTop = top + (MBPRED_SIZE << 1);
153 : edgomez 1.4
154 :     if (diag)
155 : Isibaar 1.1 pDiag = diag + 3 * MBPRED_SIZE;
156 : edgomez 1.4
157 : Isibaar 1.1 break;
158 : edgomez 1.4
159 : Isibaar 1.1 case 1:
160 :     pLeft = current;
161 :     left_quant = current_quant;
162 : edgomez 1.4
163 :     if (top) {
164 : Isibaar 1.1 pTop = top + 3 * MBPRED_SIZE;
165 :     pDiag = top + (MBPRED_SIZE << 1);
166 :     }
167 :     break;
168 : edgomez 1.4
169 : Isibaar 1.1 case 2:
170 : edgomez 1.4 if (left) {
171 : Isibaar 1.1 pLeft = left + 3 * MBPRED_SIZE;
172 :     pDiag = left + MBPRED_SIZE;
173 :     }
174 : edgomez 1.4
175 : Isibaar 1.1 pTop = current;
176 :     top_quant = current_quant;
177 :    
178 :     break;
179 : edgomez 1.4
180 : Isibaar 1.1 case 3:
181 :     pLeft = current + (MBPRED_SIZE << 1);
182 :     left_quant = current_quant;
183 : edgomez 1.4
184 : Isibaar 1.1 pTop = current + MBPRED_SIZE;
185 :     top_quant = current_quant;
186 : edgomez 1.4
187 : Isibaar 1.1 pDiag = current;
188 : edgomez 1.4
189 : Isibaar 1.1 break;
190 : edgomez 1.4
191 : Isibaar 1.1 case 4:
192 : edgomez 1.4 if (left)
193 : Isibaar 1.1 pLeft = left + (MBPRED_SIZE << 2);
194 : edgomez 1.4 if (top)
195 : Isibaar 1.1 pTop = top + (MBPRED_SIZE << 2);
196 : edgomez 1.4 if (diag)
197 : Isibaar 1.1 pDiag = diag + (MBPRED_SIZE << 2);
198 :     break;
199 : edgomez 1.4
200 : Isibaar 1.1 case 5:
201 : edgomez 1.4 if (left)
202 : Isibaar 1.1 pLeft = left + 5 * MBPRED_SIZE;
203 : edgomez 1.4 if (top)
204 : Isibaar 1.1 pTop = top + 5 * MBPRED_SIZE;
205 : edgomez 1.4 if (diag)
206 : Isibaar 1.1 pDiag = diag + 5 * MBPRED_SIZE;
207 :     break;
208 :     }
209 :    
210 : edgomez 1.4 // determine ac prediction direction & ac/dc predictor
211 :     // place rescaled ac/dc predictions into predictors[] for later use
212 : Isibaar 1.1
213 : edgomez 1.4 if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {
214 :     *acpred_direction = 1; // vertical
215 : Isibaar 1.1 predictors[0] = DIV_DIV(pTop[0], iDcScaler);
216 : edgomez 1.4 for (i = 1; i < 8; i++) {
217 : Isibaar 1.1 predictors[i] = rescale(top_quant, current_quant, pTop[i]);
218 :     }
219 : edgomez 1.4 } else {
220 :     *acpred_direction = 2; // horizontal
221 : Isibaar 1.1 predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
222 : edgomez 1.4 for (i = 1; i < 8; i++) {
223 : Isibaar 1.1 predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);
224 :     }
225 :     }
226 :     }
227 :    
228 :    
229 :     /* decoder: add predictors to dct_codes[] and
230 :     store current coeffs to pred_values[] for future prediction
231 :     */
232 :    
233 :    
234 : edgomez 1.4 void
235 :     add_acdc(MACROBLOCK * pMB,
236 :     uint32_t block,
237 :     int16_t dct_codes[64],
238 :     uint32_t iDcScaler,
239 :     int16_t predictors[8])
240 : Isibaar 1.1 {
241 :     uint8_t acpred_direction = pMB->acpred_directions[block];
242 : edgomez 1.4 int16_t *pCurrent = pMB->pred_values[block];
243 : Isibaar 1.1 uint32_t i;
244 :    
245 : suxen_drol 1.6 DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);
246 :    
247 : Isibaar 1.1 dct_codes[0] += predictors[0]; // dc prediction
248 :     pCurrent[0] = dct_codes[0] * iDcScaler;
249 :    
250 : edgomez 1.4 if (acpred_direction == 1) {
251 :     for (i = 1; i < 8; i++) {
252 : Isibaar 1.1 int level = dct_codes[i] + predictors[i];
253 : edgomez 1.4
254 : suxen_drol 1.6 DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]);
255 :    
256 : Isibaar 1.1 dct_codes[i] = level;
257 :     pCurrent[i] = level;
258 : edgomez 1.4 pCurrent[i + 7] = dct_codes[i * 8];
259 : Isibaar 1.1 }
260 : edgomez 1.4 } else if (acpred_direction == 2) {
261 :     for (i = 1; i < 8; i++) {
262 :     int level = dct_codes[i * 8] + predictors[i];
263 : suxen_drol 1.6 DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]);
264 : edgomez 1.4
265 :     dct_codes[i * 8] = level;
266 :     pCurrent[i + 7] = level;
267 : Isibaar 1.1 pCurrent[i] = dct_codes[i];
268 :     }
269 : edgomez 1.4 } else {
270 :     for (i = 1; i < 8; i++) {
271 : Isibaar 1.1 pCurrent[i] = dct_codes[i];
272 : edgomez 1.4 pCurrent[i + 7] = dct_codes[i * 8];
273 : Isibaar 1.1 }
274 :     }
275 :     }
276 :    
277 :    
278 :    
279 :     // ******************************************************************
280 :     // ******************************************************************
281 :    
282 :     /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
283 :    
284 : edgomez 1.2 todo: perform [-127,127] clamping after prediction
285 :     clamping must adjust the coeffs, so dequant is done correctly
286 : Isibaar 1.1
287 : edgomez 1.2 S1/S2 are used to determine if its worth predicting for AC
288 :     S1 = sum of all (qcoeff - prediction)
289 :     S2 = sum of all qcoeff
290 :     */
291 : Isibaar 1.1
292 : edgomez 1.4 uint32_t
293 :     calc_acdc(MACROBLOCK * pMB,
294 :     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 :     uint32_t S1 = 0, S2 = 0;
302 :    
303 :    
304 :     /* store current coeffs to pred_values[] for future prediction */
305 :    
306 :     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.4 } 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 :     /* apply predictors[] to qcoeff */
346 :    
347 : edgomez 1.4 void
348 :     apply_acdc(MACROBLOCK * pMB,
349 :     uint32_t block,
350 :     int16_t qcoeff[64],
351 :     int16_t predictors[8])
352 : Isibaar 1.1 {
353 :     uint32_t i;
354 :    
355 : edgomez 1.4 if (pMB->acpred_directions[block] == 1) {
356 :     for (i = 1; i < 8; i++) {
357 : Isibaar 1.1 qcoeff[i] = predictors[i];
358 :     }
359 : edgomez 1.4 } else {
360 :     for (i = 1; i < 8; i++) {
361 :     qcoeff[i * 8] = predictors[i];
362 : Isibaar 1.1 }
363 : edgomez 1.2 }
364 : Isibaar 1.1 }
365 :    
366 :    
367 : edgomez 1.4 void
368 :     MBPrediction(FRAMEINFO * frame,
369 :     uint32_t x,
370 :     uint32_t y,
371 :     uint32_t mb_width,
372 :     int16_t qcoeff[6 * 64])
373 : Isibaar 1.1 {
374 : edgomez 1.2
375 :     int32_t j;
376 : suxen_drol 1.3 int32_t iDcScaler, iQuant = frame->quant;
377 : Isibaar 1.1 int32_t S = 0;
378 :     int16_t predictors[6][8];
379 :    
380 : suxen_drol 1.3 MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];
381 : Isibaar 1.1
382 : edgomez 1.2 if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {
383 : edgomez 1.4
384 :     for (j = 0; j < 6; j++) {
385 : Isibaar 1.1 iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0);
386 :    
387 : edgomez 1.4 predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],
388 : suxen_drol 1.6 iQuant, iDcScaler, predictors[j], 0);
389 : edgomez 1.4
390 :     S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
391 : edgomez 1.2
392 : Isibaar 1.1 }
393 :    
394 : edgomez 1.4 if (S < 0) // dont predict
395 :     {
396 :     for (j = 0; j < 6; j++) {
397 : Isibaar 1.1 pMB->acpred_directions[j] = 0;
398 :     }
399 : edgomez 1.4 } else {
400 :     for (j = 0; j < 6; j++) {
401 :     apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]);
402 : Isibaar 1.1 }
403 :     }
404 :     pMB->cbp = calc_cbp(qcoeff);
405 :     }
406 : edgomez 1.2
407 : suxen_drol 1.6 }

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