[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.13.2.6, Thu May 22 16:36:07 2003 UTC revision 1.14.2.2, Fri Jun 4 11:54:42 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$
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               *   ****************************************************************************/
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  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                                                *  
   *                                                                            *  
   ******************************************************************************/  
26    
27  #include <stdlib.h>  #include <stdlib.h>
28    
# Line 106  Line 80 
80          const int16_t *pTop = default_acdc_values;          const int16_t *pTop = default_acdc_values;
81          const int16_t *pDiag = default_acdc_values;          const int16_t *pDiag = default_acdc_values;
82    
83          uint32_t index = x + y * mb_width;      // current macroblock          uint32_t index = x + y * mb_width;      /* current macroblock */
84          int *acpred_direction = &pMBs[index].acpred_directions[block];          int *acpred_direction = &pMBs[index].acpred_directions[block];
85          uint32_t i;          uint32_t i;
86    
87          left = top = diag = current = 0;          left = top = diag = current = 0;
88    
89          // grab left,top and diag macroblocks          /* grab left,top and diag macroblocks */
90    
91          // left macroblock          /* left macroblock */
92    
93          if (x && mbpos >= bound + 1  &&          if (x && mbpos >= bound + 1  &&
94                  (pMBs[index - 1].mode == MODE_INTRA ||                  (pMBs[index - 1].mode == MODE_INTRA ||
# Line 122  Line 96 
96    
97                  left = pMBs[index - 1].pred_values[0];                  left = pMBs[index - 1].pred_values[0];
98                  left_quant = pMBs[index - 1].quant;                  left_quant = pMBs[index - 1].quant;
                 //DEBUGI("LEFT", *(left+MBPRED_SIZE));  
99          }          }
100          // top macroblock          /* top macroblock */
101    
102          if (mbpos >= bound + (int)mb_width &&          if (mbpos >= bound + (int)mb_width &&
103                  (pMBs[index - mb_width].mode == MODE_INTRA ||                  (pMBs[index - mb_width].mode == MODE_INTRA ||
# Line 133  Line 106 
106                  top = pMBs[index - mb_width].pred_values[0];                  top = pMBs[index - mb_width].pred_values[0];
107                  top_quant = pMBs[index - mb_width].quant;                  top_quant = pMBs[index - mb_width].quant;
108          }          }
109          // diag macroblock          /* diag macroblock */
110    
111          if (x && mbpos >= bound + (int)mb_width + 1 &&          if (x && mbpos >= bound + (int)mb_width + 1 &&
112                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
# Line 144  Line 117 
117    
118          current = pMBs[index].pred_values[0];          current = pMBs[index].pred_values[0];
119    
120          // now grab pLeft, pTop, pDiag _blocks_          /* now grab pLeft, pTop, pDiag _blocks_ */
121    
122          switch (block) {          switch (block) {
123    
# Line 211  Line 184 
184                  break;                  break;
185          }          }
186    
187          //  determine ac prediction direction & ac/dc predictor          /* determine ac prediction direction & ac/dc predictor place rescaled ac/dc
188          //  place rescaled ac/dc predictions into predictors[] for later use           * predictions into predictors[] for later use */
   
189          if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) {          if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) {
190                  *acpred_direction = 1;  // vertical                  *acpred_direction = 1;  /* vertical */
191                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);
192                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
193                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);
194                  }                  }
195          } else {          } else {
196                  *acpred_direction = 2;  // horizontal                  *acpred_direction = 2;  /* horizontal */
197                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
198                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
199                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);
# Line 234  Line 206 
206     store current coeffs to pred_values[] for future prediction     store current coeffs to pred_values[] for future prediction
207  */  */
208    
209    /* Up to this version, no DC clipping was performed, so we try to be backward
210     * compatible to avoid artifacts */
211    #define BS_VERSION_BUGGY_DC_CLIPPING 34
212    
213  void  void
214  add_acdc(MACROBLOCK * pMB,  add_acdc(MACROBLOCK * pMB,
215                   uint32_t block,                   uint32_t block,
216                   int16_t dct_codes[64],                   int16_t dct_codes[64],
217                   uint32_t iDcScaler,                   uint32_t iDcScaler,
218                   int16_t predictors[8])                   int16_t predictors[8],
219                     const int bsversion)
220  {  {
221          uint8_t acpred_direction = pMB->acpred_directions[block];          uint8_t acpred_direction = pMB->acpred_directions[block];
222          int16_t *pCurrent = pMB->pred_values[block];          int16_t *pCurrent = pMB->pred_values[block];
# Line 248  Line 224 
224    
225          DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]);          DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]);
226    
227          dct_codes[0] += predictors[0];  // dc prediction          dct_codes[0] += predictors[0];  /* dc prediction */
228          pCurrent[0] = dct_codes[0] * iDcScaler;          pCurrent[0] = dct_codes[0] * iDcScaler;
229            if (!bsversion || bsversion > BS_VERSION_BUGGY_DC_CLIPPING) {
230                    pCurrent[0] = CLIP(pCurrent[0], -2048, 2047);
231            }
232    
233          if (acpred_direction == 1) {          if (acpred_direction == 1) {
234                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
# Line 280  Line 259 
259    
260    
261    
262  // ******************************************************************  /*****************************************************************************
263  // ******************************************************************   ****************************************************************************/
264    
265  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
266    
# Line 306  Line 285 
285          /* store current coeffs to pred_values[] for future prediction */          /* store current coeffs to pred_values[] for future prediction */
286    
287          pCurrent[0] = qcoeff[0] * iDcScaler;          pCurrent[0] = qcoeff[0] * iDcScaler;
288            pCurrent[0] = CLIP(pCurrent[0], -2048, 2047);
289          for (i = 1; i < 8; i++) {          for (i = 1; i < 8; i++) {
290                  pCurrent[i] = qcoeff[i];                  pCurrent[i] = qcoeff[i];
291                  pCurrent[i + 7] = qcoeff[i * 8];                  pCurrent[i + 7] = qcoeff[i * 8];
# Line 325  Line 305 
305                          S1 += abs(level);                          S1 += abs(level);
306                          predictors[i] = level;                          predictors[i] = level;
307                  }                  }
308          } else                                          // acpred_direction == 2          } else                                          /* acpred_direction == 2 */
309          {          {
310                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
311                          int16_t level;                          int16_t level;
# Line 362  Line 342 
342    
343          /* store current coeffs to pred_values[] for future prediction */          /* store current coeffs to pred_values[] for future prediction */
344          pCurrent[0] = qcoeff[0] * iDcScaler;          pCurrent[0] = qcoeff[0] * iDcScaler;
345            pCurrent[0] = CLIP(pCurrent[0], -2048, 2047);
346          for (i = 1; i < 8; i++) {          for (i = 1; i < 8; i++) {
347                  pCurrent[i] = qcoeff[i];                  pCurrent[i] = qcoeff[i];
348                  pCurrent[i + 7] = qcoeff[i * 8];                  pCurrent[i + 7] = qcoeff[i * 8];
# Line 372  Line 353 
353          qcoeff[0] = qcoeff[0] - predictors[0];          qcoeff[0] = qcoeff[0] - predictors[0];
354    
355          /* calc cost before ac prediction */          /* calc cost before ac prediction */
 #ifdef BIGLUT  
         Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1);  
 #else  
356          Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]);          Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]);
 #endif  
357    
358          /* apply ac prediction & calc cost*/          /* apply ac prediction & calc cost*/
359          if (direction == 1) {          if (direction == 1) {
# Line 385  Line 362 
362                          qcoeff[i] -= predictors[i];                          qcoeff[i] -= predictors[i];
363                          predictors[i] = qcoeff[i];                          predictors[i] = qcoeff[i];
364                  }                  }
365          }else{                                          // acpred_direction == 2          }else{                                          /* acpred_direction == 2 */
366                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
367                          tmp[i] = qcoeff[i*8];                          tmp[i] = qcoeff[i*8];
368                          qcoeff[i*8] -= predictors[i];                          qcoeff[i*8] -= predictors[i];
# Line 393  Line 370 
370                  }                  }
371          }          }
372    
 #ifdef BIGLUT  
         Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1);  
 #else  
373          Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]);          Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]);
 #endif  
374    
375          /* undo prediction */          /* undo prediction */
376          if (direction == 1) {          if (direction == 1) {
377                  for (i = 1; i < 8; i++)                  for (i = 1; i < 8; i++)
378                          qcoeff[i] = tmp[i];                          qcoeff[i] = tmp[i];
379          }else{                                          // acpred_direction == 2          }else{                                          /* acpred_direction == 2 */
380                  for (i = 1; i < 8; i++)                  for (i = 1; i < 8; i++)
381                          qcoeff[i*8] = tmp[i];                          qcoeff[i*8] = tmp[i];
382          }          }
# Line 462  Line 435 
435    
436                  }                  }
437    
438                  if (S<=0) {                             // dont predict                  if (S<=0) {                             /* dont predict */
439                          for (j = 0; j < 6; j++)                          for (j = 0; j < 6; j++)
440                                  pMB->acpred_directions[j] = 0;                                  pMB->acpred_directions[j] = 0;
441                  }else{                  }else{
# Line 472  Line 445 
445    
446                  pMB->cbp = calc_cbp(qcoeff);                  pMB->cbp = calc_cbp(qcoeff);
447          }          }
448    }
449    
450    static const VECTOR zeroMV = { 0, 0 };
451    
452    VECTOR
453    get_pmv2(const MACROBLOCK * const mbs,
454                    const int mb_width,
455                    const int bound,
456                    const int x,
457                    const int y,
458                    const int block)
459    {
460            int lx, ly, lz;         /* left */
461            int tx, ty, tz;         /* top */
462            int rx, ry, rz;         /* top-right */
463            int lpos, tpos, rpos;
464            int num_cand = 0, last_cand = 1;
465    
466            VECTOR pmv[4];  /* left neighbour, top neighbour, top-right neighbour */
467    
468            switch (block) {
469            case 0:
470                    lx = x - 1;     ly = y;         lz = 1;
471                    tx = x;         ty = y - 1;     tz = 2;
472                    rx = x + 1;     ry = y - 1;     rz = 2;
473                    break;
474            case 1:
475                    lx = x;         ly = y;         lz = 0;
476                    tx = x;         ty = y - 1;     tz = 3;
477                    rx = x + 1;     ry = y - 1;     rz = 2;
478                    break;
479            case 2:
480                    lx = x - 1;     ly = y;         lz = 3;
481                    tx = x;         ty = y;         tz = 0;
482                    rx = x;         ry = y;         rz = 1;
483                    break;
484            default:
485                    lx = x;         ly = y;         lz = 2;
486                    tx = x;         ty = y;         tz = 0;
487                    rx = x;         ry = y;         rz = 1;
488            }
489    
490            lpos = lx + ly * mb_width;
491            rpos = rx + ry * mb_width;
492            tpos = tx + ty * mb_width;
493    
494            if (lpos >= bound && lx >= 0) {
495                    num_cand++;
496                    pmv[1] = mbs[lpos].mvs[lz];
497            } else pmv[1] = zeroMV;
498    
499            if (tpos >= bound) {
500                    num_cand++;
501                    last_cand = 2;
502                    pmv[2] = mbs[tpos].mvs[tz];
503            } else pmv[2] = zeroMV;
504    
505            if (rpos >= bound && rx < mb_width) {
506                    num_cand++;
507                    last_cand = 3;
508                    pmv[3] = mbs[rpos].mvs[rz];
509            } else pmv[3] = zeroMV;
510    
511            /* If there're more than one candidate, we return the median vector */
512    
513            if (num_cand > 1) {
514                    /* set median */
515                    pmv[0].x =
516                            MIN(MAX(pmv[1].x, pmv[2].x),
517                                    MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
518                    pmv[0].y =
519                            MIN(MAX(pmv[1].y, pmv[2].y),
520                                    MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
521                    return pmv[0];
522            }
523    
524            return pmv[last_cand];  /* no point calculating median mv */
525    }
526    
527    VECTOR
528    get_qpmv2(const MACROBLOCK * const mbs,
529                    const int mb_width,
530                    const int bound,
531                    const int x,
532                    const int y,
533                    const int block)
534    {
535            int lx, ly, lz;         /* left */
536            int tx, ty, tz;         /* top */
537            int rx, ry, rz;         /* top-right */
538            int lpos, tpos, rpos;
539            int num_cand = 0, last_cand = 1;
540    
541            VECTOR pmv[4];  /* left neighbour, top neighbour, top-right neighbour */
542    
543            switch (block) {
544            case 0:
545                    lx = x - 1;     ly = y;         lz = 1;
546                    tx = x;         ty = y - 1;     tz = 2;
547                    rx = x + 1;     ry = y - 1;     rz = 2;
548                    break;
549            case 1:
550                    lx = x;         ly = y;         lz = 0;
551                    tx = x;         ty = y - 1;     tz = 3;
552                    rx = x + 1;     ry = y - 1;     rz = 2;
553                    break;
554            case 2:
555                    lx = x - 1;     ly = y;         lz = 3;
556                    tx = x;         ty = y;         tz = 0;
557                    rx = x;         ry = y;         rz = 1;
558                    break;
559            default:
560                    lx = x;         ly = y;         lz = 2;
561                    tx = x;         ty = y;         tz = 0;
562                    rx = x;         ry = y;         rz = 1;
563            }
564    
565            lpos = lx + ly * mb_width;
566            rpos = rx + ry * mb_width;
567            tpos = tx + ty * mb_width;
568    
569            if (lpos >= bound && lx >= 0) {
570                    num_cand++;
571                    pmv[1] = mbs[lpos].qmvs[lz];
572            } else pmv[1] = zeroMV;
573    
574            if (tpos >= bound) {
575                    num_cand++;
576                    last_cand = 2;
577                    pmv[2] = mbs[tpos].qmvs[tz];
578            } else pmv[2] = zeroMV;
579    
580            if (rpos >= bound && rx < mb_width) {
581                    num_cand++;
582                    last_cand = 3;
583                    pmv[3] = mbs[rpos].qmvs[rz];
584            } else pmv[3] = zeroMV;
585    
586            /* If there're more than one candidate, we return the median vector */
587    
588            if (num_cand > 1) {
589                    /* set median */
590                    pmv[0].x =
591                            MIN(MAX(pmv[1].x, pmv[2].x),
592                                    MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
593                    pmv[0].y =
594                            MIN(MAX(pmv[1].y, pmv[2].y),
595                                    MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
596                    return pmv[0];
597            }
598    
599            return pmv[last_cand];  /* no point calculating median mv */
600  }  }

Legend:
Removed from v.1.13.2.6  
changed lines
  Added in v.1.14.2.2

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