[svn] / trunk / xvidcore / src / quant / adapt_quant.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/quant/adapt_quant.c

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

revision 224, Wed Jun 19 08:46:57 2002 UTC revision 495, Fri Sep 20 19:58:36 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Adaptive quantization functions -
5     *
6     *  Copyright(C) 2002 Peter Ross
7     *
8     *  This program is an implementation of a part of one or more MPEG-4
9     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
10     *  to use this software module in hardware or software products are
11     *  advised that its use may infringe existing patents or copyrights, and
12     *  any such use would be at such party's own risk.  The original
13     *  developer of this software module and his/her company, and subsequent
14     *  editors and their companies, will have no liability for use of this
15     *  software or modifications or derivatives thereof.
16     *
17     *  This program is free software ; you can redistribute it and/or modify
18     *  it under the terms of the GNU General Public License as published by
19     *  the Free Software Foundation ; either version 2 of the License, or
20     *  (at your option) any later version.
21     *
22     *  This program is distributed in the hope that it will be useful,
23     *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
24     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25     *  GNU General Public License for more details.
26     *
27     *  You should have received a copy of the GNU General Public License
28     *  along with this program ; if not, write to the Free Software
29     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
30     *
31     *  $Id: adapt_quant.c,v 1.8 2002-09-20 19:58:36 edgomez Exp $
32     *
33     ****************************************************************************/
34    
35  #include "../portab.h"  #include "../portab.h"
36  #include "adapt_quant.h"  #include "adapt_quant.h"
37    
# Line 6  Line 40 
40  #define MAX(a,b)      (((a) > (b)) ? (a) : (b))  #define MAX(a,b)      (((a) > (b)) ? (a) : (b))
41  #define RDIFF(a,b)    ((int)(a+0.5)-(int)(b+0.5))  #define RDIFF(a,b)    ((int)(a+0.5)-(int)(b+0.5))
42    
43    /*****************************************************************************
44     * Functions
45     ****************************************************************************/
46    
47  int  int
48  normalize_quantizer_field(float *in,  normalize_quantizer_field(float *in,
49                                                    int *out,                                                    int *out,
# Line 65  Line 103 
103  {  {
104          int i, j, k, l;          int i, j, k, l;
105    
106          static float *quant;          float *quant;
107          unsigned char *ptr;          unsigned char *ptr;
108          float *val;          float *val;
109          float global = 0., maxval = 0.;          float global = 0.;
110            uint32_t mid_range = 0;
         const float DarkThres = 0.25;  
         const float DarkAmpl = 7.0;  
   
         const float BrightThres = 4.0;  
         const float BrightAmpl = 5.0;  
111    
112          const char LowestVal = 10;          const float DarkAmpl = 14 / 2;
113            const float BrightAmpl = 10 / 2;
114            const float DarkThres = 70;
115            const float BrightThres = 200;
116    
117            const float GlobalDarkThres = 60;
118            const float GlobalBrightThres = 170;
119    
120            const float MidRangeThres = 20;
121            const float UpperLimit = 200;
122            const float LowerLimit = 25;
123    
         const float GlobalBrightThres = 220.0;  
         const float GlobalDarkThres = 20.0;  
         float global_quant = 1.0;  
124    
         if (!quant)  
125                  if (!(quant = (float *) malloc(mb_width * mb_height * sizeof(float))))                  if (!(quant = (float *) malloc(mb_width * mb_height * sizeof(float))))
126                          return -1;                  return(-1);
127    
128          val = (float *) malloc(mb_width * mb_height * sizeof(float));          if(!(val = (float *) malloc(mb_width * mb_height * sizeof(float))))
129                    return(-1);
130    
131          for (k = 0; k < mb_height; k++) {          for (k = 0; k < mb_height; k++) {
132                  for (l = 0; l < mb_width; l++)  // do this for all macroblocks individually                  for (l = 0; l < mb_width; l++)  // do this for all macroblocks individually
# Line 100  Line 140 
140    
141                          for (i = 0; i < 16; i++)                          for (i = 0; i < 16; i++)
142                                  for (j = 0; j < 16; j++)                                  for (j = 0; j < 16; j++)
                                 {  
                                         if( ptr[i * stride + j] < LowestVal )  
                                                 ptr[i * stride + j] = 0;  
143                                          val[k * mb_width + l] += ptr[i * stride + j];                                          val[k * mb_width + l] += ptr[i * stride + j];
                                 }  
144                          val[k * mb_width + l] /= 256.;                          val[k * mb_width + l] /= 256.;
145                          global += val[k * mb_width + l];                          global += val[k * mb_width + l];
146                          if( val[k * mb_width + l] > maxval )  
147                                  maxval = val[k * mb_width + l];                          if ((val[k * mb_width + l] > LowerLimit) &&
148                                    (val[k * mb_width + l] < UpperLimit))
149                                    mid_range++;
150                  }                  }
151          }          }
152    
153          global /= mb_width * mb_height;          global /= mb_width * mb_height;
         maxval /= global;  
         if( global < GlobalDarkThres )  
                 global_quant *= -1.0;  
         else if ( global < GlobalBrightThres )  
                 global_quant = 0.0;  
154    
155            if (((global <GlobalBrightThres) &&(global >GlobalDarkThres))
156                    || (mid_range < MidRangeThres)) {
157          for (k = 0; k < mb_height; k++) {          for (k = 0; k < mb_height; k++) {
158                  for (l = 0; l < mb_width; l++)  // do this for all macroblocks individually                  for (l = 0; l < mb_width; l++)  // do this for all macroblocks individually
159                  {                  {
                         val[k * mb_width + l] /= global;  
160                          if (val[k * mb_width + l] < DarkThres)                          if (val[k * mb_width + l] < DarkThres)
161                                  quant[k * mb_width + l] += global_quant +                                          quant[k * mb_width + l] +=
162                                          DarkAmpl * (DarkThres - val[k * mb_width + l]) / DarkThres;                                                  DarkAmpl * (DarkThres -
163                                                                            val[k * mb_width + l]) / DarkThres;
164                          else if (val[k * mb_width + l] > BrightThres)                          else if (val[k * mb_width + l] > BrightThres)
165                                  quant[k * mb_width + l] += global_quant +                                          quant[k * mb_width + l] +=
166                                          BrightAmpl * (val[k * mb_width + l] - BrightThres) / (maxval - BrightThres);                                                  BrightAmpl * (val[k * mb_width + l] -
167                                                                              BrightThres) / (255 - BrightThres);
168                  }                  }
169          }          }
170          free(val);          }
171          return normalize_quantizer_field(quant, intquant, mb_width * mb_height,  
172            i = normalize_quantizer_field(quant, intquant,
173                                                                      mb_width * mb_height,
174                                                                           min_quant, max_quant);                                                                           min_quant, max_quant);
175    
176            free(val);
177            free(quant);
178    
179            return(i);
180    
181  }  }

Legend:
Removed from v.224  
changed lines
  Added in v.495

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