[svn] / branches / release-1_0-branch / xvidcore / src / prediction / mbprediction.c Repository:
ViewVC logotype

Diff of /branches/release-1_0-branch/xvidcore/src/prediction/mbprediction.c

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

trunk/xvidcore/src/prediction/mbprediction.c revision 851, Sat Feb 15 15:22:19 2003 UTC branches/release-1_0-branch/xvidcore/src/prediction/mbprediction.c revision 1446, Mon May 3 23:28:29 2004 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
2    *                                                                            *   *
3    *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *   *  XVID MPEG-4 VIDEO CODEC
4    *                                                                            *   *  - Prediction module -
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    *   *  Copyright (C) 2001-2003 Michael Militzer <isibaar@xvid.org>
7    *  software module in hardware or software products are advised that its     *   *                2001-2003 Peter Ross <pross@xvid.org>
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        *   *  This program is free software ; you can redistribute it and/or modify
10    *  software module and his/her company, and subsequent editors and their     *   *  it under the terms of the GNU General Public License as published by
11    *  companies, will have no liability for use of this software or             *   *  the Free Software Foundation ; either version 2 of the License, or
12    *  modifications or derivatives thereof.                                     *   *  (at your option) any later version.
13    *                                                                            *   *
14    *  XviD is free software; you can redistribute it and/or modify it           *   *  This program is distributed in the hope that it will be useful,
15    *  under the terms of the GNU General Public License as published by         *   *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    *  (at your option) any later version.                                       *   *  GNU General Public License for more details.
18    *                                                                            *   *
19    *  XviD is distributed in the hope that it will be useful, but               *   *  You should have received a copy of the GNU General Public License
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  along with this program ; if not, write to the Free Software
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22    *  GNU General Public License for more details.                              *   *
23    *                                                                            *   * $Id: mbprediction.c,v 1.14.2.1 2004-05-03 23:28:29 edgomez Exp $
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    *                                                                            *  #include <stdlib.h>
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  mbprediction.c                                                            *  
   *                                                                            *  
   *  Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org>                  *  
   *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *  
   *                                                                            *  
   *  For more information visit the XviD homepage: http://www.xvid.org         *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  Revision history:                                                         *  
   *                                                                            *  
   *  29.06.2002 predict_acdc() bounding                                        *  
   *  12.12.2001 improved calc_acdc_prediction; removed need for memcpy         *  
   *  15.12.2001 moved pmv displacement to motion estimation                    *  
   *  30.11.2001 mmx cbp support                                                *  
   *  17.11.2001 initial version                                                *  
   *                                                                            *  
   ******************************************************************************/  
28    
29  #include "../global.h"  #include "../global.h"
30  #include "../encoder.h"  #include "../encoder.h"
# Line 91  Line 67 
67                           uint32_t current_quant,                           uint32_t current_quant,
68                           int32_t iDcScaler,                           int32_t iDcScaler,
69                           int16_t predictors[8],                           int16_t predictors[8],
70                          const int bound)                           const int bound,
71                             const int bsversion)
72    
73  {  {
74          const int mbpos = (y * mb_width) + x;          const int mbpos = (y * mb_width) + x;
# Line 104  Line 81 
81          const int16_t *pTop = default_acdc_values;          const int16_t *pTop = default_acdc_values;
82          const int16_t *pDiag = default_acdc_values;          const int16_t *pDiag = default_acdc_values;
83    
84          uint32_t index = x + y * mb_width;      // current macroblock          uint32_t index = x + y * mb_width;      /* current macroblock */
85          int *acpred_direction = &pMBs[index].acpred_directions[block];          int *acpred_direction = &pMBs[index].acpred_directions[block];
86          uint32_t i;          uint32_t i;
87    
88          left = top = diag = current = 0;          left = top = diag = current = 0;
89    
90          // grab left,top and diag macroblocks          /* grab left,top and diag macroblocks */
91    
92          // left macroblock          /* left macroblock */
93    
94          if (x && mbpos >= bound + 1  &&          if (x && mbpos >= bound + 1  &&
95                  (pMBs[index - 1].mode == MODE_INTRA ||                  (pMBs[index - 1].mode == MODE_INTRA ||
# Line 120  Line 97 
97    
98                  left = pMBs[index - 1].pred_values[0];                  left = pMBs[index - 1].pred_values[0];
99                  left_quant = pMBs[index - 1].quant;                  left_quant = pMBs[index - 1].quant;
                 //DEBUGI("LEFT", *(left+MBPRED_SIZE));  
100          }          }
101          // top macroblock          /* top macroblock */
102    
103          if (mbpos >= bound + (int)mb_width &&          if (mbpos >= bound + (int)mb_width &&
104                  (pMBs[index - mb_width].mode == MODE_INTRA ||                  (pMBs[index - mb_width].mode == MODE_INTRA ||
# Line 131  Line 107 
107                  top = pMBs[index - mb_width].pred_values[0];                  top = pMBs[index - mb_width].pred_values[0];
108                  top_quant = pMBs[index - mb_width].quant;                  top_quant = pMBs[index - mb_width].quant;
109          }          }
110          // diag macroblock          /* diag macroblock */
111    
112          if (x && mbpos >= bound + (int)mb_width + 1 &&          if (x && mbpos >= bound + (int)mb_width + 1 &&
113                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
# Line 142  Line 118 
118    
119          current = pMBs[index].pred_values[0];          current = pMBs[index].pred_values[0];
120    
121          // now grab pLeft, pTop, pDiag _blocks_          /* now grab pLeft, pTop, pDiag _blocks_ */
122    
123          switch (block) {          switch (block) {
124    
# Line 209  Line 185 
185                  break;                  break;
186          }          }
187    
188          //  determine ac prediction direction & ac/dc predictor          /* determine ac prediction direction & ac/dc predictor place rescaled ac/dc
189          //  place rescaled ac/dc predictions into predictors[] for later use           * predictions into predictors[] for later use */
190    
191          if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {          /* Workaround: Bitstream versions <= 32 used to have a wrong predictor
192                  *acpred_direction = 1;  // vertical           * stored as it wasn't clipped to the [-2048, 2047] range. We only
193             * use the right predictors for bs versions > 32 */
194    #define BUGGY_CLIPPING_BS_VERSION 32
195            if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) {
196                    *acpred_direction = 1;  /* vertical */
197                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);
198                    if (bsversion == 0 || bsversion > BUGGY_CLIPPING_BS_VERSION)
199                            predictors[0] = CLIP(predictors[0], -2048, 2047);
200                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
201                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);
202                  }                  }
203          } else {          } else {
204                  *acpred_direction = 2;  // horizontal                  *acpred_direction = 2;  /* horizontal */
205                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
206                    if (bsversion == 0 || bsversion > BUGGY_CLIPPING_BS_VERSION)
207                            predictors[0] = CLIP(predictors[0], -2048, 2047);
208                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
209                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);
210                  }                  }
# Line 244  Line 228 
228          int16_t *pCurrent = pMB->pred_values[block];          int16_t *pCurrent = pMB->pred_values[block];
229          uint32_t i;          uint32_t i;
230    
231          DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);          DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]);
232    
233          dct_codes[0] += predictors[0];  // dc prediction          dct_codes[0] += predictors[0];  /* dc prediction */
234          pCurrent[0] = dct_codes[0] * iDcScaler;          pCurrent[0] = dct_codes[0] * iDcScaler;
235    
236          if (acpred_direction == 1) {          if (acpred_direction == 1) {
237                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
238                          int level = dct_codes[i] + predictors[i];                          int level = dct_codes[i] + predictors[i];
239    
240                          DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]);                          DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i, predictors[i]);
241    
242                          dct_codes[i] = level;                          dct_codes[i] = level;
243                          pCurrent[i] = level;                          pCurrent[i] = level;
# Line 262  Line 246 
246          } else if (acpred_direction == 2) {          } else if (acpred_direction == 2) {
247                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
248                          int level = dct_codes[i * 8] + predictors[i];                          int level = dct_codes[i * 8] + predictors[i];
249                          DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]);                          DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i*8, predictors[i]);
250    
251                          dct_codes[i * 8] = level;                          dct_codes[i * 8] = level;
252                          pCurrent[i + 7] = level;                          pCurrent[i + 7] = level;
# Line 278  Line 262 
262    
263    
264    
265  // ******************************************************************  /*****************************************************************************
266  // ******************************************************************   ****************************************************************************/
267    
268  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
269    
# Line 318  Line 302 
302                          int16_t level;                          int16_t level;
303    
304                          level = qcoeff[i];                          level = qcoeff[i];
305                          S2 += ABS(level);                          S2 += abs(level);
306                          level -= predictors[i];                          level -= predictors[i];
307                          S1 += ABS(level);                          S1 += abs(level);
308                          predictors[i] = level;                          predictors[i] = level;
309                  }                  }
310          } else                                          // acpred_direction == 2          } else                                          /* acpred_direction == 2 */
311          {          {
312                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
313                          int16_t level;                          int16_t level;
314    
315                          level = qcoeff[i * 8];                          level = qcoeff[i * 8];
316                          S2 += ABS(level);                          S2 += abs(level);
317                          level -= predictors[i];                          level -= predictors[i];
318                          S1 += ABS(level);                          S1 += abs(level);
319                          predictors[i] = level;                          predictors[i] = level;
320                  }                  }
321    
# Line 370  Line 354 
354          qcoeff[0] = qcoeff[0] - predictors[0];          qcoeff[0] = qcoeff[0] - predictors[0];
355    
356          /* calc cost before ac prediction */          /* calc cost before ac prediction */
 #ifdef BIGLUT  
         Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1);  
 #else  
357          Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]);          Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]);
 #endif  
358    
359          /* apply ac prediction & calc cost*/          /* apply ac prediction & calc cost*/
360          if (direction == 1) {          if (direction == 1) {
# Line 383  Line 363 
363                          qcoeff[i] -= predictors[i];                          qcoeff[i] -= predictors[i];
364                          predictors[i] = qcoeff[i];                          predictors[i] = qcoeff[i];
365                  }                  }
366          }else{                                          // acpred_direction == 2          }else{                                          /* acpred_direction == 2 */
367                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
368                          tmp[i] = qcoeff[i*8];                          tmp[i] = qcoeff[i*8];
369                          qcoeff[i*8] -= predictors[i];                          qcoeff[i*8] -= predictors[i];
# Line 391  Line 371 
371                  }                  }
372          }          }
373    
 #ifdef BIGLUT  
         Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1);  
 #else  
374          Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]);          Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]);
 #endif  
375    
376          /* undo prediction */          /* undo prediction */
377          if (direction == 1) {          if (direction == 1) {
378                  for (i = 1; i < 8; i++)                  for (i = 1; i < 8; i++)
379                          qcoeff[i] = tmp[i];                          qcoeff[i] = tmp[i];
380          }else{                                          // acpred_direction == 2          }else{                                          /* acpred_direction == 2 */
381                  for (i = 1; i < 8; i++)                  for (i = 1; i < 8; i++)
382                          qcoeff[i*8] = tmp[i];                          qcoeff[i*8] = tmp[i];
383          }          }
# Line 438  Line 414 
414  {  {
415    
416          int32_t j;          int32_t j;
417          int32_t iDcScaler, iQuant = frame->quant;          int32_t iDcScaler, iQuant;
418          int S = 0;          int S = 0;
419          int16_t predictors[6][8];          int16_t predictors[6][8];
420    
421          MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];          MACROBLOCK *pMB = &frame->mbs[x + y * mb_width];
422        iQuant = pMB->quant;
423    
424          if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {          if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) {
425    
# Line 450  Line 427 
427                          iDcScaler = get_dc_scaler(iQuant, j<4);                          iDcScaler = get_dc_scaler(iQuant, j<4);
428    
429                          predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],                          predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64],
430                                                   iQuant, iDcScaler, predictors[j], 0);                                                   iQuant, iDcScaler, predictors[j], 0, 0);
431    
432                          if ((frame->global_flags & XVID_HQACPRED))                          if ((frame->vop_flags & XVID_VOP_HQACPRED))
433                                  S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);                                  S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
434                          else                          else
435                                  S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);                                  S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]);
436    
437                  }                  }
438    
439                  if (S<=0) {                             // dont predict                  if (S<=0) {                             /* dont predict */
440                          for (j = 0; j < 6; j++)                          for (j = 0; j < 6; j++)
441                                  pMB->acpred_directions[j] = 0;                                  pMB->acpred_directions[j] = 0;
442                  }else{                  }else{
# Line 469  Line 446 
446    
447                  pMB->cbp = calc_cbp(qcoeff);                  pMB->cbp = calc_cbp(qcoeff);
448          }          }
449    }
450    
451    static const VECTOR zeroMV = { 0, 0 };
452    
453    VECTOR
454    get_pmv2(const MACROBLOCK * const mbs,
455                    const int mb_width,
456                    const int bound,
457                    const int x,
458                    const int y,
459                    const int block)
460    {
461            int lx, ly, lz;         /* left */
462            int tx, ty, tz;         /* top */
463            int rx, ry, rz;         /* top-right */
464            int lpos, tpos, rpos;
465            int num_cand = 0, last_cand = 1;
466    
467            VECTOR pmv[4];  /* left neighbour, top neighbour, top-right neighbour */
468    
469            switch (block) {
470            case 0:
471                    lx = x - 1;     ly = y;         lz = 1;
472                    tx = x;         ty = y - 1;     tz = 2;
473                    rx = x + 1;     ry = y - 1;     rz = 2;
474                    break;
475            case 1:
476                    lx = x;         ly = y;         lz = 0;
477                    tx = x;         ty = y - 1;     tz = 3;
478                    rx = x + 1;     ry = y - 1;     rz = 2;
479                    break;
480            case 2:
481                    lx = x - 1;     ly = y;         lz = 3;
482                    tx = x;         ty = y;         tz = 0;
483                    rx = x;         ry = y;         rz = 1;
484                    break;
485            default:
486                    lx = x;         ly = y;         lz = 2;
487                    tx = x;         ty = y;         tz = 0;
488                    rx = x;         ry = y;         rz = 1;
489            }
490    
491            lpos = lx + ly * mb_width;
492            rpos = rx + ry * mb_width;
493            tpos = tx + ty * mb_width;
494    
495            if (lpos >= bound && lx >= 0) {
496                    num_cand++;
497                    pmv[1] = mbs[lpos].mvs[lz];
498            } else pmv[1] = zeroMV;
499    
500            if (tpos >= bound) {
501                    num_cand++;
502                    last_cand = 2;
503                    pmv[2] = mbs[tpos].mvs[tz];
504            } else pmv[2] = zeroMV;
505    
506            if (rpos >= bound && rx < mb_width) {
507                    num_cand++;
508                    last_cand = 3;
509                    pmv[3] = mbs[rpos].mvs[rz];
510            } else pmv[3] = zeroMV;
511    
512            /* If there're more than one candidate, we return the median vector */
513    
514            if (num_cand > 1) {
515                    /* set median */
516                    pmv[0].x =
517                            MIN(MAX(pmv[1].x, pmv[2].x),
518                                    MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
519                    pmv[0].y =
520                            MIN(MAX(pmv[1].y, pmv[2].y),
521                                    MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
522                    return pmv[0];
523            }
524    
525            return pmv[last_cand];  /* no point calculating median mv */
526    }
527    
528    VECTOR
529    get_qpmv2(const MACROBLOCK * const mbs,
530                    const int mb_width,
531                    const int bound,
532                    const int x,
533                    const int y,
534                    const int block)
535    {
536            int lx, ly, lz;         /* left */
537            int tx, ty, tz;         /* top */
538            int rx, ry, rz;         /* top-right */
539            int lpos, tpos, rpos;
540            int num_cand = 0, last_cand = 1;
541    
542            VECTOR pmv[4];  /* left neighbour, top neighbour, top-right neighbour */
543    
544            switch (block) {
545            case 0:
546                    lx = x - 1;     ly = y;         lz = 1;
547                    tx = x;         ty = y - 1;     tz = 2;
548                    rx = x + 1;     ry = y - 1;     rz = 2;
549                    break;
550            case 1:
551                    lx = x;         ly = y;         lz = 0;
552                    tx = x;         ty = y - 1;     tz = 3;
553                    rx = x + 1;     ry = y - 1;     rz = 2;
554                    break;
555            case 2:
556                    lx = x - 1;     ly = y;         lz = 3;
557                    tx = x;         ty = y;         tz = 0;
558                    rx = x;         ry = y;         rz = 1;
559                    break;
560            default:
561                    lx = x;         ly = y;         lz = 2;
562                    tx = x;         ty = y;         tz = 0;
563                    rx = x;         ry = y;         rz = 1;
564            }
565    
566            lpos = lx + ly * mb_width;
567            rpos = rx + ry * mb_width;
568            tpos = tx + ty * mb_width;
569    
570            if (lpos >= bound && lx >= 0) {
571                    num_cand++;
572                    pmv[1] = mbs[lpos].qmvs[lz];
573            } else pmv[1] = zeroMV;
574    
575            if (tpos >= bound) {
576                    num_cand++;
577                    last_cand = 2;
578                    pmv[2] = mbs[tpos].qmvs[tz];
579            } else pmv[2] = zeroMV;
580    
581            if (rpos >= bound && rx < mb_width) {
582                    num_cand++;
583                    last_cand = 3;
584                    pmv[3] = mbs[rpos].qmvs[rz];
585            } else pmv[3] = zeroMV;
586    
587            /* If there're more than one candidate, we return the median vector */
588    
589            if (num_cand > 1) {
590                    /* set median */
591                    pmv[0].x =
592                            MIN(MAX(pmv[1].x, pmv[2].x),
593                                    MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
594                    pmv[0].y =
595                            MIN(MAX(pmv[1].y, pmv[2].y),
596                                    MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
597                    return pmv[0];
598            }
599    
600            return pmv[last_cand];  /* no point calculating median mv */
601  }  }

Legend:
Removed from v.851  
changed lines
  Added in v.1446

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