[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 496, Fri Sep 20 20:17:22 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.9 2002-09-20 20:17:22 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                    free(quant);
130                    return(-1);
131            }
132    
133          for (k = 0; k < mb_height; k++) {          for (k = 0; k < mb_height; k++) {
134                  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 142 
142    
143                          for (i = 0; i < 16; i++)                          for (i = 0; i < 16; i++)
144                                  for (j = 0; j < 16; j++)                                  for (j = 0; j < 16; j++)
                                 {  
                                         if( ptr[i * stride + j] < LowestVal )  
                                                 ptr[i * stride + j] = 0;  
145                                          val[k * mb_width + l] += ptr[i * stride + j];                                          val[k * mb_width + l] += ptr[i * stride + j];
                                 }  
146                          val[k * mb_width + l] /= 256.;                          val[k * mb_width + l] /= 256.;
147                          global += val[k * mb_width + l];                          global += val[k * mb_width + l];
148                          if( val[k * mb_width + l] > maxval )  
149                                  maxval = val[k * mb_width + l];                          if ((val[k * mb_width + l] > LowerLimit) &&
150                                    (val[k * mb_width + l] < UpperLimit))
151                                    mid_range++;
152                  }                  }
153          }          }
154    
155          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;  
156    
157            if (((global <GlobalBrightThres) &&(global >GlobalDarkThres))
158                    || (mid_range < MidRangeThres)) {
159          for (k = 0; k < mb_height; k++) {          for (k = 0; k < mb_height; k++) {
160                  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
161                  {                  {
                         val[k * mb_width + l] /= global;  
162                          if (val[k * mb_width + l] < DarkThres)                          if (val[k * mb_width + l] < DarkThres)
163                                  quant[k * mb_width + l] += global_quant +                                          quant[k * mb_width + l] +=
164                                          DarkAmpl * (DarkThres - val[k * mb_width + l]) / DarkThres;                                                  DarkAmpl * (DarkThres -
165                                                                            val[k * mb_width + l]) / DarkThres;
166                          else if (val[k * mb_width + l] > BrightThres)                          else if (val[k * mb_width + l] > BrightThres)
167                                  quant[k * mb_width + l] += global_quant +                                          quant[k * mb_width + l] +=
168                                          BrightAmpl * (val[k * mb_width + l] - BrightThres) / (maxval - BrightThres);                                                  BrightAmpl * (val[k * mb_width + l] -
169                                                                              BrightThres) / (255 - BrightThres);
170                  }                  }
171          }          }
172          free(val);          }
173          return normalize_quantizer_field(quant, intquant, mb_width * mb_height,  
174            i = normalize_quantizer_field(quant, intquant,
175                                                                      mb_width * mb_height,
176                                                                           min_quant, max_quant);                                                                           min_quant, max_quant);
177    
178            free(val);
179            free(quant);
180    
181            return(i);
182    
183  }  }

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

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