[cvs] / xvidcore / src / image / postprocessing.c Repository:
ViewVC logotype

Diff of /xvidcore/src/image/postprocessing.c

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

revision 1.1.4.1, Wed Dec 10 01:02:40 2003 UTC revision 1.5, Thu Nov 27 00:47:03 2008 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Postprocessing  functions -   *  - Postprocessing  functions -
5   *   *
6   *  Copyright(C) 2003 Michael Militzer <isibaar@xvid.org>   *  Copyright(C) 2003-2004 Michael Militzer <isibaar@xvid.org>
7     *                    2004 Marc Fauconneau
8   *   *
9   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
10   *  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 19  Line 20 
20   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
23     * $Id$
24     *
25   ****************************************************************************/   ****************************************************************************/
26    
27  #include <stdlib.h>  #include <stdlib.h>
# Line 28  Line 31 
31  #include "../portab.h"  #include "../portab.h"
32  #include "../global.h"  #include "../global.h"
33  #include "image.h"  #include "image.h"
34    #include "../utils/emms.h"
35  #include "postprocessing.h"  #include "postprocessing.h"
36    
37  /* Filtering thresholds */  /* function pointers */
38    IMAGEBRIGHTNESS_PTR image_brightness;
39    
 #define THR1 2  
 #define THR2 6  
40    
41  /* Some useful (and fast) macros  /* Some useful (and fast) macros
42     Note that the MIN/MAX macros assume signed shift - if your compiler     Note that the MIN/MAX macros assume signed shift - if your compiler
# Line 44  Line 47 
47  #define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31)  #define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31)
48  #define ABS(X)    (((X)>0)?(X):-(X))  #define ABS(X)    (((X)>0)?(X):-(X))
49    
50  static int8_t xvid_thresh_tbl[510];  void init_postproc(XVID_POSTPROC *tbls)
 static int8_t xvid_abs_tbl[510];  
   
 void init_postproc(void)  
51  {  {
52          int i;          init_deblock(tbls);
53            init_noise(tbls);
         for(i = -255; i < 256; i++) {  
                 xvid_thresh_tbl[i + 255] = 0;  
                 if(ABS(i) < THR1)  
                         xvid_thresh_tbl[i + 255] = 1;  
                 xvid_abs_tbl[i + 255] = ABS(i);  
         }  
54  }  }
55    
56  void  void
57  image_deblock(IMAGE * img, int edged_width,  image_postproc(XVID_POSTPROC *tbls, IMAGE * img, int edged_width,
58                                  const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,                                  const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
59                                  int flags)                                  int flags, int brightness, int frame_num, int bvop)
60  {  {
61          const int edged_width2 = edged_width /2;          const int edged_width2 = edged_width /2;
62          int i,j;          int i,j;
63          int quant;          int quant;
64            int dering = flags & XVID_DERINGY;
65    
66          /* luma: j,i in block units */          /* luma: j,i in block units */
67          if ((flags & XVID_DEBLOCKY))          if ((flags & XVID_DEBLOCKY))
# Line 75  Line 70 
70                  for (i = 0; i < mb_width*2; i++)                  for (i = 0; i < mb_width*2; i++)
71                  {                  {
72                          quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;                          quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
73                          deblock8x8_h(img->y + j*8*edged_width + i*8, edged_width, quant);                          deblock8x8_h(tbls, img->y + j*8*edged_width + i*8, edged_width, quant, dering);
74                  }                  }
75    
76                  for (j = 0; j < mb_height*2; j++)               /* vertical deblocking */                  for (j = 0; j < mb_height*2; j++)               /* vertical deblocking */
77                  for (i = 1; i < mb_width*2; i++)                  for (i = 1; i < mb_width*2; i++)
78                  {                  {
79                          quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;                          quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
80                          deblock8x8_v(img->y + j*8*edged_width + i*8, edged_width, quant);                          deblock8x8_v(tbls, img->y + j*8*edged_width + i*8, edged_width, quant, dering);
81                  }                  }
82          }          }
83    
# Line 90  Line 85 
85          /* chroma */          /* chroma */
86          if ((flags & XVID_DEBLOCKUV))          if ((flags & XVID_DEBLOCKUV))
87          {          {
88                    dering = flags & XVID_DERINGUV;
89    
90                  for (j = 1; j < mb_height; j++)         /* horizontal deblocking */                  for (j = 1; j < mb_height; j++)         /* horizontal deblocking */
91                  for (i = 0; i < mb_width; i++)                  for (i = 0; i < mb_width; i++)
92                  {                  {
93                          quant = mbs[(j+0)*mb_stride + i].quant;                          quant = mbs[(j+0)*mb_stride + i].quant;
94                          deblock8x8_h(img->u + j*8*edged_width2 + i*8, edged_width2, quant);                          deblock8x8_h(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant, dering);
95                          deblock8x8_h(img->v + j*8*edged_width2 + i*8, edged_width2, quant);                          deblock8x8_h(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant, dering);
96                  }                  }
97    
98                  for (j = 0; j < mb_height; j++)         /* vertical deblocking */                  for (j = 0; j < mb_height; j++)         /* vertical deblocking */
99                  for (i = 1; i < mb_width; i++)                  for (i = 1; i < mb_width; i++)
100                  {                  {
101                          quant = mbs[(j+0)*mb_stride + i].quant;                          quant = mbs[(j+0)*mb_stride + i].quant;
102                          deblock8x8_v(img->u + j*8*edged_width2 + i*8, edged_width2, quant);                          deblock8x8_v(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant, dering);
103                          deblock8x8_v(img->v + j*8*edged_width2 + i*8, edged_width2, quant);                          deblock8x8_v(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant, dering);
104                  }                  }
105          }          }
106    
107            if (!bvop)
108                    tbls->prev_quant = mbs->quant;
109    
110            if ((flags & XVID_FILMEFFECT))
111            {
112                    add_noise(tbls, img->y, img->y, edged_width, mb_width*16,
113                                      mb_height*16, frame_num % 3, tbls->prev_quant);
114            }
115    
116            if (brightness != 0) {
117                    image_brightness(img->y, edged_width, mb_width*16, mb_height*16, brightness);
118            }
119    }
120    
121    /******************************************************************************/
122    
123    void init_deblock(XVID_POSTPROC *tbls)
124    {
125            int i;
126    
127            for(i = -255; i < 256; i++) {
128                    tbls->xvid_thresh_tbl[i + 255] = 0;
129                    if(ABS(i) < THR1)
130                            tbls->xvid_thresh_tbl[i + 255] = 1;
131                    tbls->xvid_abs_tbl[i + 255] = ABS(i);
132            }
133  }  }
134    
135  #define LOAD_DATA_HOR(x) \  #define LOAD_DATA_HOR(x) \
# Line 134  Line 158 
158                  s[8] = *(v[8] = img + x*stride + 3); \                  s[8] = *(v[8] = img + x*stride + 3); \
159                  s[9] = *(v[9] = img + x*stride + 4);                  s[9] = *(v[9] = img + x*stride + 4);
160    
161    #define APPLY_DERING(x) \
162                    *v[x] = (e[x] == 0) ? (                 \
163                            (e[x-1] == 0) ? (                       \
164                            (e[x+1] == 0) ?                         \
165                            ((s[x-1]+s[x]*2+s[x+1])>>2)     \
166                            : ((s[x-1]+s[x])>>1) )          \
167                            : ((s[x]+s[x+1])>>1) )          \
168                            : s[x];
169    
170  #define APPLY_FILTER_CORE \  #define APPLY_FILTER_CORE \
171                  /* First, decide whether to use default or DC-offset mode */ \                  /* First, decide whether to use default or DC-offset mode */ \
172                  \                  \
173                  eq_cnt = 0; \                  eq_cnt = 0; \
174                  \                  \
175                  eq_cnt += xvid_thresh_tbl[s[0] - s[1] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[0] - s[1] + 255]; \
176                  eq_cnt += xvid_thresh_tbl[s[1] - s[2] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[1] - s[2] + 255]; \
177                  eq_cnt += xvid_thresh_tbl[s[2] - s[3] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[2] - s[3] + 255]; \
178                  eq_cnt += xvid_thresh_tbl[s[3] - s[4] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[3] - s[4] + 255]; \
179                  eq_cnt += xvid_thresh_tbl[s[4] - s[5] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[4] - s[5] + 255]; \
180                  eq_cnt += xvid_thresh_tbl[s[5] - s[6] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[5] - s[6] + 255]; \
181                  eq_cnt += xvid_thresh_tbl[s[6] - s[7] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[6] - s[7] + 255]; \
182                  eq_cnt += xvid_thresh_tbl[s[7] - s[8] + 255]; \                  eq_cnt += tbls->xvid_thresh_tbl[s[7] - s[8] + 255]; \
183                  \                  \
184                  if(eq_cnt < THR2) { /* Default mode */  \                  if(eq_cnt < THR2) { /* Default mode */  \
185                          int a30, a31, a32;                                      \                          int a30, a31, a32;                                      \
186                          int diff, limit;                                        \                          int diff, limit;                                        \
187                                                                                                  \                                                                                                  \
188                            if(tbls->xvid_abs_tbl[(s[4] - s[5]) + 255] < quant) {                   \
189                          a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1));    \                          a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1));    \
                                                                                                                                         \  
                         if(xvid_abs_tbl[a30 + 255] < 8*quant) {                                                         \  
190                                  a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1));    \                                  a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1));    \
191                                  a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1));    \                                  a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1));    \
192                                                                                                                                                  \                                                                                                                                                  \
193                                  diff = (5 * ((SIGN(a30) * MIN(xvid_abs_tbl[a30 + 255], MIN(xvid_abs_tbl[a31 + 255], xvid_abs_tbl[a32 + 255]))) - a30) + 32) >> 6;       \                                  diff = (5 * ((SIGN(a30) * MIN(FAST_ABS(a30), MIN(FAST_ABS(a31), FAST_ABS(a32)))) - a30) + 32) >> 6;     \
194                                  limit = (s[4] - s[5]) / 2;      \                                  limit = (s[4] - s[5]) / 2;      \
195                                  \                                  \
196                                  if (limit > 0)                          \                                  if (limit > 0)                          \
# Line 169  Line 201 
201                                  *v[4] -= diff;  \                                  *v[4] -= diff;  \
202                                  *v[5] += diff;  \                                  *v[5] += diff;  \
203                          }       \                          }       \
204                            if (dering) {   \
205                                    e[0] = (tbls->xvid_abs_tbl[(s[0] - s[1]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
206                                    e[1] = (tbls->xvid_abs_tbl[(s[1] - s[2]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
207                                    e[2] = (tbls->xvid_abs_tbl[(s[2] - s[3]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
208                                    e[3] = (tbls->xvid_abs_tbl[(s[3] - s[4]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
209                                    e[4] = (tbls->xvid_abs_tbl[(s[4] - s[5]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
210                                    e[5] = (tbls->xvid_abs_tbl[(s[5] - s[6]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
211                                    e[6] = (tbls->xvid_abs_tbl[(s[6] - s[7]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
212                                    e[7] = (tbls->xvid_abs_tbl[(s[7] - s[8]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
213                                    e[8] = (tbls->xvid_abs_tbl[(s[8] - s[9]) + 255] > quant + DERING_STRENGTH) ? 1 : 0;     \
214                                    \
215                                    e[1] |= e[0];   \
216                                    e[2] |= e[1];   \
217                                    e[3] |= e[2];   \
218                                    e[4] |= e[3];   \
219                                    e[5] |= e[4];   \
220                                    e[6] |= e[5];   \
221                                    e[7] |= e[6];   \
222                                    e[8] |= e[7];   \
223                                    e[9]  = e[8];   \
224                                    \
225                                    APPLY_DERING(1) \
226                                    APPLY_DERING(2) \
227                                    APPLY_DERING(3) \
228                                    APPLY_DERING(4) \
229                                    APPLY_DERING(5) \
230                                    APPLY_DERING(6) \
231                                    APPLY_DERING(7) \
232                                    APPLY_DERING(8) \
233                            }       \
234                  }       \                  }       \
235                  else {  /* DC-offset mode */    \                  else {  /* DC-offset mode */    \
236                          uint8_t p0, p9; \                          uint8_t p0, p9; \
# Line 181  Line 243 
243                          if(((max-min)) < 2*quant) {     \                          if(((max-min)) < 2*quant) {     \
244                                                                                  \                                                                                  \
245                                  /* Choose edge pixels */        \                                  /* Choose edge pixels */        \
246                                  p0 = (xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \                                  p0 = (tbls->xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1];   \
247                                  p9 = (xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8]; \                                  p9 = (tbls->xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8];   \
248                                                                                                                                  \                                                                                                                                  \
249                                  *v[1] = (uint8_t) ((6*p0 + (s[1]<<2) + (s[2]<<1) + (s[3]<<1) + s[4] + s[5] + 8) >> 4);  \                                  *v[1] = (uint8_t) ((6*p0 + (s[1]<<2) + (s[2]<<1) + (s[3]<<1) + s[4] + s[5] + 8) >> 4);  \
250                                  *v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4);   \                                  *v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4);   \
# Line 195  Line 257 
257                          }       \                          }       \
258                  }                  }
259    
260  void deblock8x8_h(uint8_t *img, int stride, int quant)  void deblock8x8_h(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant, int dering)
261  {  {
262          int eq_cnt;          int eq_cnt;
263          uint8_t *v[10];          uint8_t *v[10];
264          int32_t s[10];          int s[10];
265            int e[10];
266    
267          LOAD_DATA_HOR(0)          LOAD_DATA_HOR(0)
268          APPLY_FILTER_CORE          APPLY_FILTER_CORE
# Line 227  Line 290 
290  }  }
291    
292    
293  void deblock8x8_v(uint8_t *img, int stride, int quant)  void deblock8x8_v(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant, int dering)
294  {  {
295          int eq_cnt;          int eq_cnt;
296          uint8_t *v[10];          uint8_t *v[10];
297          int s[10];          int s[10];
298            int e[10];
299    
300          LOAD_DATA_VER(0)          LOAD_DATA_VER(0)
301          APPLY_FILTER_CORE          APPLY_FILTER_CORE
# Line 257  Line 321 
321          LOAD_DATA_VER(7)          LOAD_DATA_VER(7)
322          APPLY_FILTER_CORE          APPLY_FILTER_CORE
323  }  }
324    
325    /******************************************************************************
326     *                                                                            *
327     *  Noise code below taken from MPlayer: http://www.mplayerhq.hu/             *
328     *  Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>                 *
329     *                                                                                                                                                        *
330     ******************************************************************************/
331    
332    #define RAND_N(range) ((int) ((double)range * rand() / (RAND_MAX + 1.0)))
333    #define STRENGTH1 12
334    #define STRENGTH2 8
335    
336    void init_noise(XVID_POSTPROC *tbls)
337    {
338            int i, j;
339            int patt[4] = { -1,0,1,0 };
340    
341            emms();
342    
343            srand(123457);
344    
345            for(i = 0, j = 0; i < MAX_NOISE; i++, j++)
346            {
347                    double x1, x2, w, y1, y2;
348    
349                    do {
350                            x1 = 2.0 * rand() / (float) RAND_MAX - 1.0;
351                            x2 = 2.0 * rand() / (float) RAND_MAX - 1.0;
352                            w = x1 * x1 + x2 * x2;
353                    } while (w >= 1.0);
354    
355                    w = sqrt((-2.0 * log(w)) / w);
356                    y1 = x1 * w;
357                    y2 = x1 * w;
358    
359                    y1 *= STRENGTH1 / sqrt(3.0);
360                    y2 *= STRENGTH2 / sqrt(3.0);
361    
362                y1 /= 2;
363                    y2 /= 2;
364                y1 += patt[j%4] * STRENGTH1 * 0.35;
365                    y2 += patt[j%4] * STRENGTH2 * 0.35;
366    
367                    if (y1 < -128) {
368                            y1=-128;
369                    }
370                    else if (y1 > 127) {
371                            y1= 127;
372                    }
373    
374                    if (y2 < -128) {
375                            y2=-128;
376                    }
377                    else if (y2 > 127) {
378                            y2= 127;
379                    }
380    
381                    y1 /= 3.0;
382                    y2 /= 3.0;
383                    tbls->xvid_noise1[i] = (int) y1;
384                    tbls->xvid_noise2[i] = (int) y2;
385    
386                    if (RAND_N(6) == 0) {
387                            j--;
388                    }
389            }
390    
391            for (i = 0; i < MAX_RES; i++)
392                    for (j = 0; j < 3; j++) {
393                            tbls->xvid_prev_shift[i][j] = tbls->xvid_noise1 + (rand() & (MAX_SHIFT - 1));
394                            tbls->xvid_prev_shift[i][3 + j] = tbls->xvid_noise2 + (rand() & (MAX_SHIFT - 1));
395                    }
396    }
397    
398    void add_noise(XVID_POSTPROC *tbls, uint8_t *dst, uint8_t *src, int stride, int width, int height, int shiftptr, int quant)
399    {
400            int x, y;
401            int shift = 0;
402            int add = (quant < 5) ? 3 : 0;
403            int8_t *noise = (quant < 5) ? tbls->xvid_noise2 : tbls->xvid_noise1;
404    
405            for(y = 0; y < height; y++)
406            {
407            int8_t *src2 = (int8_t *) src;
408    
409                    shift = rand() & (MAX_SHIFT - 1);
410    
411                    shift &= ~7;
412                    for(x = 0; x < width; x++)
413                    {
414                            const int n = tbls->xvid_prev_shift[y][0 + add][x] + tbls->xvid_prev_shift[y][1 + add][x] +
415                                              tbls->xvid_prev_shift[y][2 + add][x];
416    
417                            dst[x] = src2[x] + ((n * src2[x]) >> 7);
418                    }
419    
420                    tbls->xvid_prev_shift[y][shiftptr + add] = noise + shift;
421    
422                    dst += stride;
423                    src += stride;
424            }
425    }
426    
427    
428    void image_brightness_c(uint8_t *dst, int stride, int width, int height, int offset)
429    {
430            int x,y;
431    
432            for(y = 0; y < height; y++)
433            {
434                    for(x = 0; x < width; x++)
435                    {
436                            int p = dst[y*stride + x];
437                            dst[y*stride + x] = CLIP( p + offset, 0, 255);
438                    }
439            }
440    }

Legend:
Removed from v.1.1.4.1  
changed lines
  Added in v.1.5

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