[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.6, Sun Jun 30 10:46:29 2002 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"
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"
 #define ABS(X) (((X)>0)?(X):-(X))  
 #define DIV_DIV(A,B)    ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )  
62    
63    
64  static int __inline  static int __inline
# Line 105  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 121  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 132  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 143  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 210  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 245  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 263  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 279  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 301  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 321  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 345  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 353  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 376  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                          }                          }
                 }  
                 pMB->cbp = calc_cbp(qcoeff);  
         }  
   
 }  
   
   
   
   
 /*  
   get_pmvdata2: get_pmvdata with bounding  
 */  
 #define OFFSET(x,y,stride)   ((x)+((y)*(stride)))  
   
 int  
 get_pmvdata2(const MACROBLOCK * const pMBs,  
                         const uint32_t x,  
                         const uint32_t y,  
                         const uint32_t x_dim,  
                         const uint32_t block,  
                         VECTOR * const pmv,  
                         int32_t * const psad,  
                         const int bound)  
 {  
         const int mbpos = OFFSET(x, y ,x_dim);  
   
         /*  
          * pmv are filled with:  
          *  [0]: Median (or whatever is correct in a special case)  
          *  [1]: left neighbour  
          *  [2]: top neighbour  
          *  [3]: topright neighbour  
          * psad are filled with:  
          *  [0]: minimum of [1] to [3]  
          *  [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed)  
          *  [2]: top neighbour's SAD  
          *  [3]: topright neighbour's SAD  
          */  
   
         int xin1, xin2, xin3;  
         int yin1, yin2, yin3;  
         int vec1, vec2, vec3;  
   
         int pos1, pos2, pos3;  
         int num_cand = 0;               // number of candidates  
         int last_cand;                  // last candidate  
   
         uint32_t index = x + y * x_dim;  
         const VECTOR zeroMV = { 0, 0 };  
   
         /*  
          * MODE_INTER, vm18 page 48  
          * MODE_INTER4V vm18 page 51  
          *  
          *  (x,y-1)      (x+1,y-1)  
          *  [   |   ]    [   |   ]  
          *  [ 2 | 3 ]    [ 2 |   ]  
          *  
          *  (x-1,y)      (x,y)        (x+1,y)  
          *  [   | 1 ]    [ 0 | 1 ]    [ 0 |   ]  
          *  [   | 3 ]    [ 2 | 3 ]    [   |   ]  
          */  
   
         switch (block) {  
         case 0:  
                 xin1 = x - 1;  
                 yin1 = y;  
                 vec1 = 1;                               /* left */  
                 xin2 = x;  
                 yin2 = y - 1;  
                 vec2 = 2;                               /* top */  
                 xin3 = x + 1;  
                 yin3 = y - 1;  
                 vec3 = 2;                               /* top right */  
                 break;  
         case 1:  
                 xin1 = x;  
                 yin1 = y;  
                 vec1 = 0;  
                 xin2 = x;  
                 yin2 = y - 1;  
                 vec2 = 3;  
                 xin3 = x + 1;  
                 yin3 = y - 1;  
                 vec3 = 2;  
                 break;  
         case 2:  
                 xin1 = x - 1;  
                 yin1 = y;  
                 vec1 = 3;  
                 xin2 = x;  
                 yin2 = y;  
                 vec2 = 0;  
                 xin3 = x;  
                 yin3 = y;  
                 vec3 = 1;  
                 break;  
         default:  
                 xin1 = x;  
                 yin1 = y;  
                 vec1 = 2;  
                 xin2 = x;  
                 yin2 = y;  
                 vec2 = 0;  
                 xin3 = x;  
                 yin3 = y;  
                 vec3 = 1;  
         }  
   
         pos1 = OFFSET(xin1, yin1, x_dim);  
         pos2 = OFFSET(xin2, yin2, x_dim);  
         pos3 = OFFSET(xin3, yin3, x_dim);  
   
         // left  
         if (xin1 < 0 || pos1 < bound) {  
                 pmv[1] = zeroMV;  
                 psad[1] = MV_MAX_ERROR;  
         } else {  
                 pmv[1] = pMBs[xin1 + yin1 * x_dim].mvs[vec1];  
                 psad[1] = pMBs[xin1 + yin1 * x_dim].sad8[vec1];  
                 num_cand++;  
                 last_cand = 1;  
         }  
   
         // top  
         if (yin2 < 0 || pos2 < bound) {  
                 pmv[2] = zeroMV;  
                 psad[2] = MV_MAX_ERROR;  
         } else {  
                 pmv[2] = pMBs[xin2 + yin2 * x_dim].mvs[vec2];  
                 psad[2] = pMBs[xin2 + yin2 * x_dim].sad8[vec2];  
                 num_cand++;  
                 last_cand = 2;  
         }  
   
473    
474          // top right                  pMB->cbp = calc_cbp(qcoeff);
         if (yin3 < 0 || pos3 < bound || xin3 >= (int)x_dim) {  
                 pmv[3] = zeroMV;  
                 psad[3] = MV_MAX_ERROR;  
                 //DPRINTF(DPRINTF_MV, "top-right");  
         } else {  
                 pmv[3] = pMBs[xin3 + yin3 * x_dim].mvs[vec3];  
                 psad[3] = pMBs[xin2 + yin2 * x_dim].sad8[vec3];  
                 num_cand++;  
                 last_cand = 3;  
         }  
   
         if (num_cand == 1)  
         {  
                 /* DPRINTF(DPRINTF_MV,"cand0=(%i,%i), cand1=(%i,%i) cand2=(%i,%i) last=%i",  
                         pmv[1].x, pmv[1].y,  
                         pmv[2].x, pmv[2].y,  
                         pmv[3].x, pmv[3].y, last_cand - 1);  
                 */  
   
                 pmv[0] = pmv[last_cand];  
                 psad[0] = psad[last_cand];  
                 return 0;  
         }  
   
         /* DPRINTF(DPRINTF_MV,"cand0=(%i,%i), cand1=(%i,%i) cand2=(%i,%i)",  
                 pmv[1].x, pmv[1].y,  
                 pmv[2].x, pmv[2].y,  
                 pmv[3].x, pmv[3].y);*/  
   
         if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {  
                 pmv[0] = pmv[1];  
                 psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);  
                 return 1;  
475          }          }
476    
         /* median,minimum */  
   
         pmv[0].x =  
                 MIN(MAX(pmv[1].x, pmv[2].x),  
                         MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));  
         pmv[0].y =  
                 MIN(MAX(pmv[1].y, pmv[2].y),  
                         MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));  
         psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);  
   
         return 0;  
477  }  }

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

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