[cvs] / xvidcore / src / plugins / plugin_lumimasking.c Repository:
ViewVC logotype

Diff of /xvidcore/src/plugins/plugin_lumimasking.c

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

revision 1.3, Fri Apr 2 22:20:31 2004 UTC revision 1.7, Wed May 27 15:52:05 2009 UTC
# Line 5  Line 5 
5   *   *
6   *  Copyright(C) 2002-2003 Peter Ross <pross@xvid.org>   *  Copyright(C) 2002-2003 Peter Ross <pross@xvid.org>
7   *               2002      Christoph Lampert <gruel@web.de>   *               2002      Christoph Lampert <gruel@web.de>
8     *               2008      Jason Garrett-Glaser <darkshikari@gmail.com>
9   *   *
10   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 25  Line 26 
26   ****************************************************************************/   ****************************************************************************/
27    
28  #include <stdlib.h>  #include <stdlib.h>
29    #include <stdio.h>
30    #include <math.h>
31    
32  #include "../xvid.h"  #include "../xvid.h"
33    #include "../global.h"
34  #include "../portab.h"  #include "../portab.h"
35  #include "../utils/emms.h"  #include "../utils/emms.h"
36    
# Line 38  Line 42 
42  {  {
43          float *quant;          float *quant;
44          float *val;          float *val;
45            int method;
46  } lumi_data_t;  } lumi_data_t;
47    
48  /*****************************************************************************  /*****************************************************************************
# Line 97  Line 102 
102  lumi_plg_create(xvid_plg_create_t *create, lumi_data_t **handle)  lumi_plg_create(xvid_plg_create_t *create, lumi_data_t **handle)
103  {  {
104          lumi_data_t *lumi;          lumi_data_t *lumi;
105            xvid_plugin_lumimasking_t *param = (xvid_plugin_lumimasking_t *) create->param;
106    
107          if ((lumi = (lumi_data_t*)malloc(sizeof(lumi_data_t))) == NULL)          if ((lumi = (lumi_data_t*)malloc(sizeof(lumi_data_t))) == NULL)
108                  return(XVID_ERR_MEMORY);                  return(XVID_ERR_MEMORY);
109    
110            lumi->method = 0;
111          lumi->quant = (float*)malloc(create->mb_width*create->mb_height*sizeof(float));          lumi->quant = (float*)malloc(create->mb_width*create->mb_height*sizeof(float));
112          if (lumi->quant == NULL) {          if (lumi->quant == NULL) {
113                  free(lumi);                  free(lumi);
# Line 114  Line 121 
121                  return(XVID_ERR_MEMORY);                  return(XVID_ERR_MEMORY);
122          }          }
123    
124            if (param != NULL)
125                    lumi->method = param->method;
126    
127          /* Bind the data structure to the handle */          /* Bind the data structure to the handle */
128          *handle = lumi;          *handle = lumi;
129    
# Line 171  Line 181 
181          const float GlobalDarkThres = 60;          const float GlobalDarkThres = 60;
182          const float GlobalBrightThres = 170;          const float GlobalBrightThres = 170;
183    
184            /* Arbitrary centerpoint for variance-based AQ.  Roughly the same as used in x264. */
185            float center = 14000.f;
186            /* Arbitrary strength for variance-based AQ. */
187            float strength = 0.2f;
188    
189          if (data->type == XVID_TYPE_BVOP) return 0;          if (data->type == XVID_TYPE_BVOP) return 0;
190    
191          /* Do this for all macroblocks individually  */          /* Do this for all macroblocks individually  */
192          for (j = 0; j < data->mb_height; j++) {          for (j = 0; j < data->mb_height; j++) {
193                  for (i = 0; i < data->mb_width; i++) {                  for (i = 0; i < data->mb_width; i++) {
194                          int k, l, sum = 0;                          int k, l, sum = 0, sum_of_squares = 0;
195                          unsigned char *ptr;                          unsigned char *ptr;
196    
197                          /* Initialize the current quant value to the frame quant */                          /* Initialize the current quant value to the frame quant */
# Line 188  Line 203 
203                          ptr  = data->current.plane[0];                          ptr  = data->current.plane[0];
204                          ptr += 16*j*data->current.stride[0] + 16*i;                          ptr += 16*j*data->current.stride[0] + 16*i;
205    
206                            if (handle->method) { /* Variance masking mode */
207                                    /* Accumulate sum and sum of squares over the MB */
208                                    for (k = 0; k < 16; k++) {
209                                            for (l = 0; l < 16; l++) {
210                                                    int val = ptr[k*data->current.stride[0] + l];
211                                                    sum += val;
212                                                    sum_of_squares += val * val;
213                                            }
214                                    }
215                                    /* Variance = SSD - SAD^2 / (numpixels) */
216                                    int variance = sum_of_squares - sum * sum / 256;
217                                    handle->val[j*data->mb_width + i] = (float)variance;
218                            }
219                            else { /* Luminance masking mode */
220                          /* Accumulate luminance */                          /* Accumulate luminance */
221                          for (k = 0; k < 16; k++)                          for (k = 0; k < 16; k++)
222                                  for (l = 0; l < 16; l++)                                  for (l = 0; l < 16; l++)
# Line 199  Line 228 
228                          global += (float)sum/256.0f;                          global += (float)sum/256.0f;
229                  }                  }
230          }          }
231            }
232    
233            if (handle->method) { /* Variance masking */
234                    /* Apply the variance masking formula to all MBs */
235                    for (i = 0; i < data->mb_height; i++)
236                    {
237                            for (j = 0; j < data->mb_width; j++)
238                            {
239                                    float value = handle->val[i*data->mb_width + j];
240                                    float qscale_diff = strength * logf(value / center);
241                                    handle->quant[i*data->mb_width + j] *= (1.0f + qscale_diff);
242                            }
243                    }
244            }
245            else { /* Luminance masking */
246          /* Normalize the global luminance accumulator */          /* Normalize the global luminance accumulator */
247          global /= data->mb_width*data->mb_height;          global /= data->mb_width*data->mb_height;
248    
# Line 221  Line 264 
264                          }                          }
265                  }                  }
266          }          }
267            }
268    
269           /* Normalize the quantizer field */           /* Normalize the quantizer field */
270           data->quant = normalize_quantizer_field(handle->quant,           data->quant = normalize_quantizer_field(handle->quant,
271                                                                                           data->dquant,                                                                                           data->dquant,
272                                                                                           data->mb_width*data->mb_height,                                                                                           data->mb_width*data->mb_height,
273                                                                                           data->quant,                                                                                           data->quant,
274                                                                                           data->quant + data->quant/2);                                                                                           MAX(2,data->quant + data->quant/2));
275    
276           /* Plugin job finished */           /* Plugin job finished */
277           return(0);           return(0);

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.7

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