[svn] / branches / dev-api-4 / xvidcore / src / quant / adapt_quant.c Repository:
ViewVC logotype

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

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

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