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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7.2.1, Sat Jan 4 06:14:33 2003 UTC revision 1.13.2.7, Mon Jun 9 01:22:58 2003 UTC
# Line 50  Line 50 
50    *                                                                            *    *                                                                            *
51    ******************************************************************************/    ******************************************************************************/
52    
53    #include <stdlib.h>
54    
55  #include "../global.h"  #include "../global.h"
56  #include "../encoder.h"  #include "../encoder.h"
57  #include "mbprediction.h"  #include "mbprediction.h"
58  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
59  #include "../bitstream/cbp.h"  #include "../bitstream/cbp.h"
60    #include "../bitstream/mbcoding.h"
61    #include "../bitstream/zigzag.h"
62    
63    
64  static int __inline  static int __inline
# Line 102  Line 106 
106          const int16_t *pTop = default_acdc_values;          const int16_t *pTop = default_acdc_values;
107          const int16_t *pDiag = default_acdc_values;          const int16_t *pDiag = default_acdc_values;
108    
109          uint32_t index = x + y * mb_width;      // current macroblock          uint32_t index = x + y * mb_width;      /* current macroblock */
110          int *acpred_direction = &pMBs[index].acpred_directions[block];          int *acpred_direction = &pMBs[index].acpred_directions[block];
111          uint32_t i;          uint32_t i;
112    
113          left = top = diag = current = 0;          left = top = diag = current = 0;
114    
115          // grab left,top and diag macroblocks          /* grab left,top and diag macroblocks */
116    
117          // left macroblock          /* left macroblock */
118    
119          if (x && mbpos >= bound + 1  &&          if (x && mbpos >= bound + 1  &&
120                  (pMBs[index - 1].mode == MODE_INTRA ||                  (pMBs[index - 1].mode == MODE_INTRA ||
# Line 118  Line 122 
122    
123                  left = pMBs[index - 1].pred_values[0];                  left = pMBs[index - 1].pred_values[0];
124                  left_quant = pMBs[index - 1].quant;                  left_quant = pMBs[index - 1].quant;
                 //DEBUGI("LEFT", *(left+MBPRED_SIZE));  
125          }          }
126          // top macroblock          /* top macroblock */
127    
128          if (mbpos >= bound + (int)mb_width &&          if (mbpos >= bound + (int)mb_width &&
129                  (pMBs[index - mb_width].mode == MODE_INTRA ||                  (pMBs[index - mb_width].mode == MODE_INTRA ||
# Line 129  Line 132 
132                  top = pMBs[index - mb_width].pred_values[0];                  top = pMBs[index - mb_width].pred_values[0];
133                  top_quant = pMBs[index - mb_width].quant;                  top_quant = pMBs[index - mb_width].quant;
134          }          }
135          // diag macroblock          /* diag macroblock */
136    
137          if (x && mbpos >= bound + (int)mb_width + 1 &&          if (x && mbpos >= bound + (int)mb_width + 1 &&
138                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
# Line 140  Line 143 
143    
144          current = pMBs[index].pred_values[0];          current = pMBs[index].pred_values[0];
145    
146          // now grab pLeft, pTop, pDiag _blocks_          /* now grab pLeft, pTop, pDiag _blocks_ */
147    
148          switch (block) {          switch (block) {
149    
# Line 207  Line 210 
210                  break;                  break;
211          }          }
212    
213          //  determine ac prediction direction & ac/dc predictor          /*
214          //  place rescaled ac/dc predictions into predictors[] for later use           * determine ac prediction direction & ac/dc predictor place rescaled ac/dc
215             * predictions into predictors[] for later use
216             */
217    
218          if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {          if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) {
219                  *acpred_direction = 1;  // vertical                  *acpred_direction = 1;  /* vertical */
220                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);
221                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
222                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);
223                  }                  }
224          } else {          } else {
225                  *acpred_direction = 2;  // horizontal                  *acpred_direction = 2;  /* horizontal */
226                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
227                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
228                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);
# Line 242  Line 247 
247          int16_t *pCurrent = pMB->pred_values[block];          int16_t *pCurrent = pMB->pred_values[block];
248          uint32_t i;          uint32_t i;
249    
250          DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);          DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]);
251    
252          dct_codes[0] += predictors[0];  // dc prediction          dct_codes[0] += predictors[0];  /* dc prediction */
253          pCurrent[0] = dct_codes[0] * iDcScaler;          pCurrent[0] = dct_codes[0] * iDcScaler;
254    
255          if (acpred_direction == 1) {          if (acpred_direction == 1) {
256                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
257                          int level = dct_codes[i] + predictors[i];                          int level = dct_codes[i] + predictors[i];
258    
259                          DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]);                          DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i, predictors[i]);
260    
261                          dct_codes[i] = level;                          dct_codes[i] = level;
262                          pCurrent[i] = level;                          pCurrent[i] = level;
# Line 260  Line 265 
265          } else if (acpred_direction == 2) {          } else if (acpred_direction == 2) {
266                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
267                          int level = dct_codes[i * 8] + predictors[i];                          int level = dct_codes[i * 8] + predictors[i];
268                          DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]);                          DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i*8, predictors[i]);
269    
270                          dct_codes[i * 8] = level;                          dct_codes[i * 8] = level;
271                          pCurrent[i + 7] = level;                          pCurrent[i + 7] = level;
# Line 276  Line 281 
281    
282    
283    
284  // ******************************************************************  /*****************************************************************************
285  // ******************************************************************   ****************************************************************************/
286    
287  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
288    
289  todo: perform [-127,127] clamping after prediction  returns sum of coeefficients *saved* if prediction is enabled
 clamping must adjust the coeffs, so dequant is done correctly  
290    
 S1/S2 are used  to determine if its worth predicting for AC  
291  S1 = sum of all (qcoeff - prediction)  S1 = sum of all (qcoeff - prediction)
292  S2 = sum of all qcoeff  S2 = sum of all qcoeff
293  */  */
294    
295  uint32_t  int
296  calc_acdc(MACROBLOCK * pMB,  calc_acdc_coeff(MACROBLOCK * pMB,
297                    uint32_t block,                    uint32_t block,
298                    int16_t qcoeff[64],                    int16_t qcoeff[64],
299                    uint32_t iDcScaler,                    uint32_t iDcScaler,
# Line 298  Line 301 
301  {  {
302          int16_t *pCurrent = pMB->pred_values[block];          int16_t *pCurrent = pMB->pred_values[block];
303          uint32_t i;          uint32_t i;
304          uint32_t S1 = 0, S2 = 0;          int S1 = 0, S2 = 0;
305    
306    
307          /* store current coeffs to pred_values[] for future prediction */          /* store current coeffs to pred_values[] for future prediction */
# Line 318  Line 321 
321                          int16_t level;                          int16_t level;
322    
323                          level = qcoeff[i];                          level = qcoeff[i];
324                          S2 += ABS(level);                          S2 += abs(level);
325                          level -= predictors[i];                          level -= predictors[i];
326                          S1 += ABS(level);                          S1 += abs(level);
327                          predictors[i] = level;                          predictors[i] = level;
328                  }                  }
329          } else                                          // acpred_direction == 2          } else                                          /* acpred_direction == 2 */
330          {          {
331                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
332                          int16_t level;                          int16_t level;
333    
334                          level = qcoeff[i * 8];                          level = qcoeff[i * 8];
335                          S2 += ABS(level);                          S2 += abs(level);
336                          level -= predictors[i];                          level -= predictors[i];
337                          S1 += ABS(level);                          S1 += abs(level);
338                          predictors[i] = level;                          predictors[i] = level;
339                  }                  }
340    
# Line 342  Line 345 
345  }  }
346    
347    
348    
349    /* returns the bits *saved* if prediction is enabled */
350    
351    int
352    calc_acdc_bits(MACROBLOCK * pMB,
353                      uint32_t block,
354                      int16_t qcoeff[64],
355                      uint32_t iDcScaler,
356                      int16_t predictors[8])
357    {
358            const int direction = pMB->acpred_directions[block];
359            int16_t *pCurrent = pMB->pred_values[block];
360            int16_t tmp[8];
361            unsigned int i;
362            int Z1, Z2;
363    
364            /* store current coeffs to pred_values[] for future prediction */
365            pCurrent[0] = qcoeff[0] * iDcScaler;
366            for (i = 1; i < 8; i++) {
367                    pCurrent[i] = qcoeff[i];
368                    pCurrent[i + 7] = qcoeff[i * 8];
369            }
370    
371    
372            /* dc prediction */
373            qcoeff[0] = qcoeff[0] - predictors[0];
374    
375            /* calc cost before ac prediction */
376    #ifdef BIGLUT
377            Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1);
378    #else
379            Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]);
380    #endif
381    
382            /* apply ac prediction & calc cost*/
383            if (direction == 1) {
384                    for (i = 1; i < 8; i++) {
385                            tmp[i] = qcoeff[i];
386                            qcoeff[i] -= predictors[i];
387                            predictors[i] = qcoeff[i];
388                    }
389            }else{                                          /* acpred_direction == 2 */
390                    for (i = 1; i < 8; i++) {
391                            tmp[i] = qcoeff[i*8];
392                            qcoeff[i*8] -= predictors[i];
393                            predictors[i] = qcoeff[i*8];
394                    }
395            }
396    
397    #ifdef BIGLUT
398            Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1);
399    #else
400            Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]);
401    #endif
402    
403            /* undo prediction */
404            if (direction == 1) {
405                    for (i = 1; i < 8; i++)
406                            qcoeff[i] = tmp[i];
407            }else{                                          /* acpred_direction == 2 */
408                    for (i = 1; i < 8; i++)
409                            qcoeff[i*8] = tmp[i];
410            }
411    
412            return Z2-Z1;
413    }
414    
415  /* apply predictors[] to qcoeff */  /* apply predictors[] to qcoeff */
416    
417  void  void
# Line 350  Line 420 
420                     int16_t qcoeff[64],                     int16_t qcoeff[64],
421                     int16_t predictors[8])                     int16_t predictors[8])
422  {  {
423          uint32_t i;          unsigned int i;
424    
425          if (pMB->acpred_directions[block] == 1) {          if (pMB->acpred_directions[block] == 1) {
426                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++)
427                          qcoeff[i] = predictors[i];                          qcoeff[i] = predictors[i];
                 }  
428          } else {          } else {
429                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++)
430                          qcoeff[i * 8] = predictors[i];                          qcoeff[i * 8] = predictors[i];
431                  }                  }
432          }          }
 }  
433    
434    
435  void  void
# Line 373  Line 441 
441  {  {
442    
443          int32_t j;          int32_t j;
444          int32_t iDcScaler, iQuant = frame->quant;          int32_t iDcScaler, iQuant;
445          int32_t S = 0;          int S = 0;
446          int16_t predictors[6][8];          int16_t predictors[6][8];
447    
448          MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];          MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];
449        iQuant = pMB->quant;
450    
451          if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {          if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {
452    
453                  for (j = 0; j < 6; j++) {                  for (j = 0; j < 6; j++) {
454                          iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0);                          iDcScaler = get_dc_scaler(iQuant, j<4);
455    
456                          predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],                          predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],
457                                                   iQuant, iDcScaler, predictors[j], 0);                                                   iQuant, iDcScaler, predictors[j], 0);
458    
459                          S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);                          if ((frame->vop_flags & XVID_VOP_HQACPRED))
460                                    S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
461                            else
462                                    S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
463    
464                  }                  }
465    
466                  if (S < 0)                              // dont predict                  if (S<=0) {                             /* dont predict */
467                  {                          for (j = 0; j < 6; j++)
                         for (j = 0; j < 6; j++) {  
468                                  pMB->acpred_directions[j] = 0;                                  pMB->acpred_directions[j] = 0;
                         }  
469                  } else {                  } else {
470                          for (j = 0; j < 6; j++) {                          for (j = 0; j < 6; j++)
471                                  apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]);                                  apply_acdc(pMB, j, &qcoeff[j * 64], predictors[j]);
472                          }                          }
473                  }  
474                  pMB->cbp = calc_cbp(qcoeff);                  pMB->cbp = calc_cbp(qcoeff);
475          }          }
476    

Legend:
Removed from v.1.7.2.1  
changed lines
  Added in v.1.13.2.7

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