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

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

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

revision 252, Sun Jun 30 10:46:29 2002 UTC revision 677, Tue Nov 26 23:44:11 2002 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 functions -
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-2002 - Michael Militzer <isibaar@xvid.org>
7    *  software module in hardware or software products are advised that its     *   *  Copyright(C) 2001-2002 - 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 file is part of XviD, a free MPEG-4 video encoder/decoder
10    *  software module and his/her company, and subsequent editors and their     *   *
11    *  companies, will have no liability for use of this software or             *   *  XviD is free software; you can redistribute it and/or modify it
12    *  modifications or derivatives thereof.                                     *   *  under the terms of the GNU General Public License as published by
13    *                                                                            *   *  the Free Software Foundation; either version 2 of the License, or
14    *  XviD is free software; you can redistribute it and/or modify it           *   *  (at your option) any later version.
15    *  under the terms of the GNU General Public License as published by         *   *
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  This program is distributed in the hope that it will be useful,
17    *  (at your option) any later version.                                       *   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18    *                                                                            *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    *  XviD is distributed in the hope that it will be useful, but               *   *  GNU General Public License for more details.
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  You should have received a copy of the GNU General Public License
22    *  GNU General Public License for more details.                              *   *  along with this program; if not, write to the Free Software
23    *                                                                            *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
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               *   *  Under section 8 of the GNU General Public License, the copyright
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   *  holders of XVID explicitly forbid distribution in the following
27    *                                                                            *   *  countries:
28    ******************************************************************************/   *
29     *    - Japan
30   /******************************************************************************   *    - United States of America
31    *                                                                            *   *
32    *  mbprediction.c                                                            *   *  Linking XviD statically or dynamically with other modules is making a
33    *                                                                            *   *  combined work based on XviD.  Thus, the terms and conditions of the
34    *  Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org>                  *   *  GNU General Public License cover the whole combination.
35    *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *   *
36    *                                                                            *   *  As a special exception, the copyright holders of XviD give you
37    *  For more information visit the XviD homepage: http://www.xvid.org         *   *  permission to link XviD with independent modules that communicate with
38    *                                                                            *   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
39    ******************************************************************************/   *  license terms of these independent modules, and to copy and distribute
40     *  the resulting combined work under terms of your choice, provided that
41   /******************************************************************************   *  every copy of the combined work is accompanied by a complete copy of
42    *                                                                            *   *  the source code of XviD (the version of XviD used to produce the
43    *  Revision history:                                                         *   *  combined work), being distributed under the terms of the GNU General
44    *                                                                            *   *  Public License plus this exception.  An independent module is a module
45    *  29.06.2002 predict_acdc() bounding                                        *   *  which is not derived from or based on XviD.
46    *  12.12.2001 improved calc_acdc_prediction; removed need for memcpy         *   *
47    *  15.12.2001 moved pmv displacement to motion estimation                    *   *  Note that people who make modified versions of XviD are not obligated
48    *  30.11.2001 mmx cbp support                                                *   *  to grant this special exception for their modified versions; it is
49    *  17.11.2001 initial version                                                *   *  their choice whether to do so.  The GNU General Public License gives
50    *                                                                            *   *  permission to release a modified version without this exception; this
51    ******************************************************************************/   *  exception also makes it possible to release a modified version which
52     *  carries forward this exception.
53     *
54     * $Id: mbprediction.c,v 1.11 2002-11-26 23:44:11 edgomez Exp $
55     *
56     ****************************************************************************/
57    
58  #include "../encoder.h"  #include "../encoder.h"
59  #include "mbprediction.h"  #include "mbprediction.h"
# Line 60  Line 65 
65  #define DIV_DIV(A,B)    ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )  #define DIV_DIV(A,B)    ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )
66    
67    
68    /*****************************************************************************
69     * Local inlined function
70     ****************************************************************************/
71    
72  static int __inline  static int __inline
73  rescale(int predict_quant,  rescale(int predict_quant,
74                  int current_quant,                  int current_quant,
# Line 70  Line 79 
79  }  }
80    
81    
82    /*****************************************************************************
83     * Local data
84     ****************************************************************************/
85    
86  static const int16_t default_acdc_values[15] = {  static const int16_t default_acdc_values[15] = {
87          1024,          1024,
88          0, 0, 0, 0, 0, 0, 0,          0, 0, 0, 0, 0, 0, 0,
# Line 77  Line 90 
90  };  };
91    
92    
93    /*****************************************************************************
94     * Functions
95     ****************************************************************************/
96    
97    
98  /*      get dc/ac prediction direction for a single block and place  /*      get dc/ac prediction direction for a single block and place
99          predictor values into MB->pred_values[j][..]          predictor values into MB->pred_values[j][..]
100  */  */
# Line 105  Line 123 
123          const int16_t *pTop = default_acdc_values;          const int16_t *pTop = default_acdc_values;
124          const int16_t *pDiag = default_acdc_values;          const int16_t *pDiag = default_acdc_values;
125    
126          uint32_t index = x + y * mb_width;      // current macroblock          uint32_t index = x + y * mb_width;      /* current macroblock */
127          int *acpred_direction = &pMBs[index].acpred_directions[block];          int *acpred_direction = &pMBs[index].acpred_directions[block];
128          uint32_t i;          uint32_t i;
129    
130          left = top = diag = current = 0;          left = top = diag = current = 0;
131    
132          // grab left,top and diag macroblocks          /* grab left,top and diag macroblocks */
133    
134          // left macroblock          /* left macroblock  */
135    
136          if (x && mbpos >= bound + 1  &&          if (x && mbpos >= bound + 1  &&
137                  (pMBs[index - 1].mode == MODE_INTRA ||                  (pMBs[index - 1].mode == MODE_INTRA ||
# Line 121  Line 139 
139    
140                  left = pMBs[index - 1].pred_values[0];                  left = pMBs[index - 1].pred_values[0];
141                  left_quant = pMBs[index - 1].quant;                  left_quant = pMBs[index - 1].quant;
142                  //DEBUGI("LEFT", *(left+MBPRED_SIZE));                  /*DEBUGI("LEFT", *(left+MBPRED_SIZE)); */
143          }          }
144          // top macroblock          /* top macroblock */
145    
146          if (mbpos >= bound + (int)mb_width &&          if (mbpos >= bound + (int)mb_width &&
147                  (pMBs[index - mb_width].mode == MODE_INTRA ||                  (pMBs[index - mb_width].mode == MODE_INTRA ||
# Line 132  Line 150 
150                  top = pMBs[index - mb_width].pred_values[0];                  top = pMBs[index - mb_width].pred_values[0];
151                  top_quant = pMBs[index - mb_width].quant;                  top_quant = pMBs[index - mb_width].quant;
152          }          }
153          // diag macroblock          /* diag macroblock  */
154    
155          if (x && mbpos >= bound + (int)mb_width + 1 &&          if (x && mbpos >= bound + (int)mb_width + 1 &&
156                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||                  (pMBs[index - 1 - mb_width].mode == MODE_INTRA ||
# Line 143  Line 161 
161    
162          current = pMBs[index].pred_values[0];          current = pMBs[index].pred_values[0];
163    
164          // now grab pLeft, pTop, pDiag _blocks_          /* now grab pLeft, pTop, pDiag _blocks_  */
165    
166          switch (block) {          switch (block) {
167    
# Line 210  Line 228 
228                  break;                  break;
229          }          }
230    
231          //  determine ac prediction direction & ac/dc predictor          /*  determine ac prediction direction & ac/dc predictor */
232          //  place rescaled ac/dc predictions into predictors[] for later use          /*  place rescaled ac/dc predictions into predictors[] for later use */
233    
234          if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {          if (ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) {
235                  *acpred_direction = 1;  // vertical                  *acpred_direction = 1;  /* vertical */
236                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);                  predictors[0] = DIV_DIV(pTop[0], iDcScaler);
237                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
238                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);                          predictors[i] = rescale(top_quant, current_quant, pTop[i]);
239                  }                  }
240          } else {          } else {
241                  *acpred_direction = 2;  // horizontal                  *acpred_direction = 2;  /* horizontal */
242                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);                  predictors[0] = DIV_DIV(pLeft[0], iDcScaler);
243                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
244                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);                          predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]);
# Line 247  Line 265 
265    
266          DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);          DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]);
267    
268          dct_codes[0] += predictors[0];  // dc prediction          dct_codes[0] += predictors[0];  /* dc prediction */
269          pCurrent[0] = dct_codes[0] * iDcScaler;          pCurrent[0] = dct_codes[0] * iDcScaler;
270    
271          if (acpred_direction == 1) {          if (acpred_direction == 1) {
# Line 279  Line 297 
297    
298    
299    
300  // ******************************************************************  /* ****************************************************************** */
301  // ******************************************************************  /* ****************************************************************** */
302    
303  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2  /* encoder: subtract predictors from qcoeff[] and calculate S1/S2
304    
# Line 326  Line 344 
344                          S1 += ABS(level);                          S1 += ABS(level);
345                          predictors[i] = level;                          predictors[i] = level;
346                  }                  }
347          } else                                          // acpred_direction == 2          } else                                          /* acpred_direction == 2 */
348          {          {
349                  for (i = 1; i < 8; i++) {                  for (i = 1; i < 8; i++) {
350                          int16_t level;                          int16_t level;
# Line 394  Line 412 
412    
413                  }                  }
414    
415                  if (S < 0)                              // dont predict                  if (S < 0)                              /* dont predict */
416                  {                  {
417                          for (j = 0; j < 6; j++) {                          for (j = 0; j < 6; j++) {
418                                  pMB->acpred_directions[j] = 0;                                  pMB->acpred_directions[j] = 0;
# Line 408  Line 426 
426          }          }
427    
428  }  }
   
   
   
   
 /*  
   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;  
         }  
   
   
         // top right  
         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;  
         }  
   
         /* 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;  
 }  

Legend:
Removed from v.252  
changed lines
  Added in v.677

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