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

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

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

trunk/xvidcore/src/prediction/mbprediction.c revision 78, Thu Mar 28 20:57:25 2002 UTC branches/dev-api-4/xvidcore/src/prediction/mbprediction.c revision 1053, Mon Jun 9 01:25:19 2003 UTC
# Line 42  Line 42 
42    *                                                                            *    *                                                                            *
43    *  Revision history:                                                         *    *  Revision history:                                                         *
44    *                                                                            *    *                                                                            *
45      *  29.06.2002 predict_acdc() bounding                                        *
46    *  12.12.2001 improved calc_acdc_prediction; removed need for memcpy         *    *  12.12.2001 improved calc_acdc_prediction; removed need for memcpy         *
47    *  15.12.2001 moved pmv displacement to motion estimation                    *    *  15.12.2001 moved pmv displacement to motion estimation                    *
48    *  30.11.2001 mmx cbp support                                                *    *  30.11.2001 mmx cbp support                                                *
# Line 49  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"
62    
63    
64  #define ABS(X) (((X)>0)?(X):-(X))  static int __inline
65  #define DIV_DIV(A,B)    ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )  rescale(int predict_quant,
66                    int current_quant,
67                    int coeff)
 static int __inline rescale(int predict_quant, int current_quant, int coeff)  
68  {  {
69          return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0;          return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant),
70                                                                      (current_quant)) : 0;
71  }  }
72    
73    
# Line 77  Line 83 
83  */  */
84    
85    
86  void predict_acdc(MACROBLOCK *pMBs,  void
87                    uint32_t x, uint32_t y,       uint32_t mb_width,  predict_acdc(MACROBLOCK * pMBs,
88                             uint32_t x,
89                             uint32_t y,
90                             uint32_t mb_width,
91                    uint32_t block,                    uint32_t block,
92                    int16_t qcoeff[64],                    int16_t qcoeff[64],
93                    uint32_t current_quant,                    uint32_t current_quant,
94                    int32_t iDcScaler,                    int32_t iDcScaler,
95                    int16_t predictors[8])                           int16_t predictors[8],
96                            const int bound)
97    
98  {  {
99            const int mbpos = (y * mb_width) + x;
100          int16_t *left, *top, *diag, *current;          int16_t *left, *top, *diag, *current;
101    
102          int32_t left_quant = current_quant;          int32_t left_quant = current_quant;
# Line 94  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 && (pMBs[index - 1].mode == MODE_INTRA          if (x && mbpos >= bound + 1  &&
120                   || pMBs[index - 1].mode == MODE_INTRA_Q)) {                  (pMBs[index - 1].mode == MODE_INTRA ||
121                     pMBs[index - 1].mode == MODE_INTRA_Q)) {
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 */
127    
128          // top macroblock          if (mbpos >= bound + (int)mb_width &&
129                    (pMBs[index - mb_width].mode == MODE_INTRA ||
130          if(y && (pMBs[index - mb_width].mode == MODE_INTRA                   pMBs[index - mb_width].mode == MODE_INTRA_Q)) {
                  || pMBs[index - mb_width].mode == MODE_INTRA_Q)) {  
131    
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 */
136    
137          // diag macroblock          if (x && mbpos >= bound + (int)mb_width + 1 &&
138                    (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
139          if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA                   pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) {
                       || pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) {  
140    
141                  diag = pMBs[index - 1 - mb_width].pred_values[0];                  diag = pMBs[index - 1 - mb_width].pred_values[0];
142          }          }
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 198  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 {
225          else                  *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]);
229                  }                  }
230          }          }
# Line 226  Line 236 
236  */  */
237    
238    
239  void add_acdc(MACROBLOCK *pMB,  void
240    add_acdc(MACROBLOCK * pMB,
241                uint32_t block,                uint32_t block,
242                int16_t dct_codes[64],                int16_t dct_codes[64],
243                uint32_t iDcScaler,                uint32_t iDcScaler,
# Line 236  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          dct_codes[0] += predictors[0];  // dc prediction          DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]);
251    
252            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(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;
263                          pCurrent[i+7] = dct_codes[i*8];                          pCurrent[i+7] = dct_codes[i*8];
264                  }                  }
265          }          } else if (acpred_direction == 2) {
266          else if (acpred_direction == 2)                  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(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;
272                          pCurrent[i] = dct_codes[i];                          pCurrent[i] = dct_codes[i];
273                  }                  }
274          }          } else {
275          else                  for (i = 1; i < 8; i++) {
         {  
                 for (i = 1; i < 8; i++)  
                 {  
276                          pCurrent[i] = dct_codes[i];                          pCurrent[i] = dct_codes[i];
277                          pCurrent[i+7] = dct_codes[i*8];                          pCurrent[i+7] = dct_codes[i*8];
278                  }                  }
# Line 271  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 calc_acdc(MACROBLOCK *pMB,  int
296    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 292  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 307  Line 316 
316    
317          qcoeff[0] = qcoeff[0] - predictors[0];          qcoeff[0] = qcoeff[0] - predictors[0];
318    
319          if (pMB->acpred_directions[block] == 1)          if (pMB->acpred_directions[block] == 1) {
         {  
320                  for(i = 1; i < 8; i++) {                  for(i = 1; i < 8; i++) {
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 338  Line 345 
345  }  }
346    
347    
 /* apply predictors[] to qcoeff */  
348    
349  void apply_acdc(MACROBLOCK *pMB,  /* returns the bits *saved* if prediction is enabled */
350    
351    int
352    calc_acdc_bits(MACROBLOCK * pMB,
353                  uint32_t block,                  uint32_t block,
354                  int16_t qcoeff[64],                  int16_t qcoeff[64],
355                      uint32_t iDcScaler,
356                  int16_t predictors[8])                  int16_t predictors[8])
357  {  {
358          uint32_t i;          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          if (pMB->acpred_directions[block] == 1)          /* 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++)                  for(i = 1; i < 8; i++)
406                  {                          qcoeff[i] = tmp[i];
407                          qcoeff[i] = predictors[i];          }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          else  
415    /* apply predictors[] to qcoeff */
416    
417    void
418    apply_acdc(MACROBLOCK * pMB,
419                       uint32_t block,
420                       int16_t qcoeff[64],
421                       int16_t predictors[8])
422          {          {
423            unsigned int i;
424    
425            if (pMB->acpred_directions[block] == 1) {
426                    for (i = 1; i < 8; i++)
427                            qcoeff[i] = predictors[i];
428            } 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 MBPrediction(MBParam *pParam,  void
436    MBPrediction(FRAMEINFO * frame,
437                    uint32_t x,                    uint32_t x,
438                    uint32_t y,                    uint32_t y,
439                    uint32_t mb_width,                    uint32_t mb_width,
440                    int16_t qcoeff[6*64],                           int16_t qcoeff[6 * 64])
                   MACROBLOCK *mbs)  
441  {  {
442    
443          int32_t j;          int32_t j;
444          int32_t iDcScaler, iQuant = pParam->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 = &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);
                         iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0);  
455    
456                          predict_acdc(mbs,                          predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],
457                                       x,                                                   iQuant, iDcScaler, predictors[j], 0);
458                                       y,  
459                                       mb_width,                          if ((frame->vop_flags & XVID_VOP_HQACPRED))
460                                       j,                                  S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
461                                       &qcoeff[j*64],                          else
462                                       iQuant,                                  S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
                                      iDcScaler,  
                                      predictors[j]);  
   
                         S += calc_acdc(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.78  
changed lines
  Added in v.1053

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