[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, Sat May 3 23:26:55 2003 UTC revision 1.1.4.4, Sun Dec 21 19:42:07 2003 UTC
# Line 0  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Postprocessing  functions -
5     *
6     *  Copyright(C) 2003 Michael Militzer <isibaar@xvid.org>
7     *
8     *  This program is free software ; you can redistribute it and/or modify
9     *  it under the terms of the GNU General Public License as published by
10     *  the Free Software Foundation ; either version 2 of the License, or
11     *  (at your option) any later version.
12     *
13     *  This program is distributed in the hope that it will be useful,
14     *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
15     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     *  GNU General Public License for more details.
17     *
18     *  You should have received a copy of the GNU General Public License
19     *  along with this program ; if not, write to the Free Software
20     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21     *
22     * $Id$
23     *
24     ****************************************************************************/
25    
26    #include <stdlib.h>
27    #include <string.h>
28    #include <math.h>
29    
30    #include "../portab.h"
31    #include "../global.h"
32    #include "image.h"
33    #include "../utils/emms.h"
34    #include "postprocessing.h"
35    
36    /* Some useful (and fast) macros
37       Note that the MIN/MAX macros assume signed shift - if your compiler
38       doesn't do signed shifts, use the default MIN/MAX macros from global.h */
39    
40    #define FAST_MAX(x,y) ((x) - ((((x) - (y))>>(32 - 1)) & ((x) - (y))))
41    #define FAST_MIN(x,y) ((x) + ((((y) - (x))>>(32 - 1)) & ((y) - (x))))
42    #define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31)
43    #define ABS(X)    (((X)>0)?(X):-(X))
44    
45    void init_postproc(XVID_POSTPROC *tbls)
46    {
47            init_deblock(tbls);
48            init_noise(tbls);
49    }
50    
51    void
52    image_postproc(XVID_POSTPROC *tbls, IMAGE * img, int edged_width,
53                                    const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
54                                    int flags, int frame_num, int bvop)
55    {
56            const int edged_width2 = edged_width /2;
57            int i,j;
58            int quant;
59    
60            /* luma: j,i in block units */
61            if ((flags & XVID_DEBLOCKY))
62            {
63                    for (j = 1; j < mb_height*2; j++)               /* horizontal deblocking */
64                    for (i = 0; i < mb_width*2; i++)
65                    {
66                            quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
67                            deblock8x8_h(tbls, img->y + j*8*edged_width + i*8, edged_width, quant);
68                    }
69    
70                    for (j = 0; j < mb_height*2; j++)               /* vertical deblocking */
71                    for (i = 1; i < mb_width*2; i++)
72                    {
73                            quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
74                            deblock8x8_v(tbls, img->y + j*8*edged_width + i*8, edged_width, quant);
75                    }
76            }
77    
78    
79            /* chroma */
80            if ((flags & XVID_DEBLOCKUV))
81            {
82                    for (j = 1; j < mb_height; j++)         /* horizontal deblocking */
83                    for (i = 0; i < mb_width; i++)
84                    {
85                            quant = mbs[(j+0)*mb_stride + i].quant;
86                            deblock8x8_h(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant);
87                            deblock8x8_h(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant);
88                    }
89    
90                    for (j = 0; j < mb_height; j++)         /* vertical deblocking */
91                    for (i = 1; i < mb_width; i++)
92                    {
93                            quant = mbs[(j+0)*mb_stride + i].quant;
94                            deblock8x8_v(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant);
95                            deblock8x8_v(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant);
96                    }
97            }
98    
99            if (!bvop)
100                    tbls->prev_quant = mbs->quant;
101    
102            if ((flags & XVID_FILMEFFECT))
103            {
104                    add_noise(tbls, img->y, img->y, edged_width, mb_width*16,
105                                      mb_height*16, frame_num % 3, tbls->prev_quant);
106            }
107    }
108    
109    /******************************************************************************/
110    
111    void init_deblock(XVID_POSTPROC *tbls)
112    {
113            int i;
114    
115            for(i = -255; i < 256; i++) {
116                    tbls->xvid_thresh_tbl[i + 255] = 0;
117                    if(ABS(i) < THR1)
118                            tbls->xvid_thresh_tbl[i + 255] = 1;
119                    tbls->xvid_abs_tbl[i + 255] = ABS(i);
120            }
121    }
122    
123    #define LOAD_DATA_HOR(x) \
124                    /* Load pixel addresses and data for filtering */ \
125                s[0] = *(v[0] = img - 5*stride + x); \
126                    s[1] = *(v[1] = img - 4*stride + x); \
127                    s[2] = *(v[2] = img - 3*stride + x); \
128                    s[3] = *(v[3] = img - 2*stride + x); \
129                    s[4] = *(v[4] = img - 1*stride + x); \
130                    s[5] = *(v[5] = img + 0*stride + x); \
131                    s[6] = *(v[6] = img + 1*stride + x); \
132                    s[7] = *(v[7] = img + 2*stride + x); \
133                    s[8] = *(v[8] = img + 3*stride + x); \
134                    s[9] = *(v[9] = img + 4*stride + x);
135    
136    #define LOAD_DATA_VER(x) \
137                    /* Load pixel addresses and data for filtering */ \
138                    s[0] = *(v[0] = img + x*stride - 5); \
139                    s[1] = *(v[1] = img + x*stride - 4); \
140                    s[2] = *(v[2] = img + x*stride - 3); \
141                    s[3] = *(v[3] = img + x*stride - 2); \
142                    s[4] = *(v[4] = img + x*stride - 1); \
143                    s[5] = *(v[5] = img + x*stride + 0); \
144                    s[6] = *(v[6] = img + x*stride + 1); \
145                    s[7] = *(v[7] = img + x*stride + 2); \
146                    s[8] = *(v[8] = img + x*stride + 3); \
147                    s[9] = *(v[9] = img + x*stride + 4);
148    
149    #define APPLY_FILTER_CORE \
150                    /* First, decide whether to use default or DC-offset mode */ \
151                    \
152                    eq_cnt = 0; \
153                    \
154                    eq_cnt += tbls->xvid_thresh_tbl[s[0] - s[1] + 255]; \
155                    eq_cnt += tbls->xvid_thresh_tbl[s[1] - s[2] + 255]; \
156                    eq_cnt += tbls->xvid_thresh_tbl[s[2] - s[3] + 255]; \
157                    eq_cnt += tbls->xvid_thresh_tbl[s[3] - s[4] + 255]; \
158                    eq_cnt += tbls->xvid_thresh_tbl[s[4] - s[5] + 255]; \
159                    eq_cnt += tbls->xvid_thresh_tbl[s[5] - s[6] + 255]; \
160                    eq_cnt += tbls->xvid_thresh_tbl[s[6] - s[7] + 255]; \
161                    eq_cnt += tbls->xvid_thresh_tbl[s[7] - s[8] + 255]; \
162                    \
163                    if(eq_cnt < THR2) { /* Default mode */  \
164                            int a30, a31, a32;                                      \
165                            int diff, limit;                                        \
166                                                                                                    \
167                            if(tbls->xvid_abs_tbl[(s[4] - s[5]) + 255] < quant) {                   \
168                                    a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1));    \
169                                    a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1));    \
170                                    a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1));    \
171                                                                                                                                                    \
172                                    diff = (5 * ((SIGN(a30) * MIN(tbls->xvid_abs_tbl[a30 + 255], MIN(tbls->xvid_abs_tbl[a31 + 255], tbls->xvid_abs_tbl[a32 + 255]))) - a30) + 32) >> 6;     \
173                                    limit = (s[4] - s[5]) / 2;      \
174                                    \
175                                    if (limit > 0)                          \
176                                            diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff);        \
177                                    else    \
178                                            diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff);        \
179                                                                                                                                                                    \
180                                    *v[4] -= diff;  \
181                                    *v[5] += diff;  \
182                            }       \
183                    }       \
184                    else {  /* DC-offset mode */    \
185                            uint8_t p0, p9; \
186                            int min, max;   \
187                                                            \
188                            /* Now decide whether to apply smoothing filter or not */       \
189                            max = FAST_MAX(s[1], FAST_MAX(s[2], FAST_MAX(s[3], FAST_MAX(s[4], FAST_MAX(s[5], FAST_MAX(s[6], FAST_MAX(s[7], s[8])))))));     \
190                            min = FAST_MIN(s[1], FAST_MIN(s[2], FAST_MIN(s[3], FAST_MIN(s[4], FAST_MIN(s[5], FAST_MIN(s[6], FAST_MIN(s[7], s[8])))))));     \
191                            \
192                            if(((max-min)) < 2*quant) {     \
193                                                                                    \
194                                    /* Choose edge pixels */        \
195                                    p0 = (tbls->xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1];   \
196                                    p9 = (tbls->xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8];   \
197                                                                                                                                    \
198                                    *v[1] = (uint8_t) ((6*p0 + (s[1]<<2) + (s[2]<<1) + (s[3]<<1) + s[4] + s[5] + 8) >> 4);  \
199                                    *v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4);   \
200                                    *v[3] = (uint8_t) (((p0<<1) + (s[1]<<1) + (s[2]<<1) + (s[3]<<2) + (s[4]<<1) + (s[5]<<1) + s[6] + s[7] + 8) >> 4);       \
201                                    *v[4] = (uint8_t) ((p0 + s[1] + (s[2]<<1) + (s[3]<<1) + (s[4]<<2) + (s[5]<<1) + (s[6]<<1) + s[7] + s[8] + 8) >> 4);     \
202                                    *v[5] = (uint8_t) ((s[1] + s[2] + (s[3]<<1) + (s[4]<<1) + (s[5]<<2) + (s[6]<<1) + (s[7]<<1) + s[8] + p9 + 8) >> 4);     \
203                                    *v[6] = (uint8_t) ((s[2] + s[3] + (s[4]<<1) + (s[5]<<1) + (s[6]<<2) + (s[7]<<1) + (s[8]<<1) + (p9<<1) + 8) >> 4);       \
204                                    *v[7] = (uint8_t) ((s[3] + s[4] + (s[5]<<1) + (s[6]<<1) + (s[7]<<2) + (s[8]<<1) + (p9<<2) + 8) >> 4);   \
205                                    *v[8] = (uint8_t) ((s[4] + s[5] + (s[6]<<1) + (s[7]<<1) + (s[8]<<2) + 6*p9 + 8) >> 4);  \
206                            }       \
207                    }
208    
209    void deblock8x8_h(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant)
210    {
211            int eq_cnt;
212            uint8_t *v[10];
213            int32_t s[10];
214    
215            LOAD_DATA_HOR(0)
216            APPLY_FILTER_CORE
217    
218            LOAD_DATA_HOR(1)
219            APPLY_FILTER_CORE
220    
221            LOAD_DATA_HOR(2)
222            APPLY_FILTER_CORE
223    
224            LOAD_DATA_HOR(3)
225            APPLY_FILTER_CORE
226    
227            LOAD_DATA_HOR(4)
228            APPLY_FILTER_CORE
229    
230            LOAD_DATA_HOR(5)
231            APPLY_FILTER_CORE
232    
233            LOAD_DATA_HOR(6)
234            APPLY_FILTER_CORE
235    
236            LOAD_DATA_HOR(7)
237            APPLY_FILTER_CORE
238    }
239    
240    
241    void deblock8x8_v(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant)
242    {
243            int eq_cnt;
244            uint8_t *v[10];
245            int s[10];
246    
247            LOAD_DATA_VER(0)
248            APPLY_FILTER_CORE
249    
250            LOAD_DATA_VER(1)
251            APPLY_FILTER_CORE
252    
253            LOAD_DATA_VER(2)
254            APPLY_FILTER_CORE
255    
256            LOAD_DATA_VER(3)
257            APPLY_FILTER_CORE
258    
259            LOAD_DATA_VER(4)
260            APPLY_FILTER_CORE
261    
262            LOAD_DATA_VER(5)
263            APPLY_FILTER_CORE
264    
265            LOAD_DATA_VER(6)
266            APPLY_FILTER_CORE
267    
268            LOAD_DATA_VER(7)
269            APPLY_FILTER_CORE
270    }
271    
272    /******************************************************************************
273     *                                                                            *
274     *  Noise code below taken from MPlayer: http://www.mplayerhq.hu/             *
275     *  Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>                 *
276     *                                                                                                                                                        *
277     ******************************************************************************/
278    
279    #define RAND_N(range) ((int) ((double)range * rand() / (RAND_MAX + 1.0)))
280    #define STRENGTH1 12
281    #define STRENGTH2 8
282    
283    void init_noise(XVID_POSTPROC *tbls)
284    {
285            int i, j;
286            int patt[4] = { -1,0,1,0 };
287    
288            emms();
289    
290            srand(123457);
291    
292            for(i = 0, j = 0; i < MAX_NOISE; i++, j++)
293            {
294                    double x1, x2, w, y1, y2;
295    
296                    do {
297                            x1 = 2.0 * rand() / (float) RAND_MAX - 1.0;
298                            x2 = 2.0 * rand() / (float) RAND_MAX - 1.0;
299                            w = x1 * x1 + x2 * x2;
300                    } while (w >= 1.0);
301    
302                    w = sqrt((-2.0 * log(w)) / w);
303                    y1 = x1 * w;
304                    y2 = x1 * w;
305    
306                    y1 *= STRENGTH1 / sqrt(3.0);
307                    y2 *= STRENGTH2 / sqrt(3.0);
308    
309                y1 /= 2;
310                    y2 /= 2;
311                y1 += patt[j%4] * STRENGTH1 * 0.35;
312                    y2 += patt[j%4] * STRENGTH2 * 0.35;
313    
314                    if (y1 < -128) {
315                            y1=-128;
316                    }
317                    else if (y1 > 127) {
318                            y1= 127;
319                    }
320    
321                    if (y2 < -128) {
322                            y2=-128;
323                    }
324                    else if (y2 > 127) {
325                            y2= 127;
326                    }
327    
328                    y1 /= 3.0;
329                    y2 /= 3.0;
330                    tbls->xvid_noise1[i] = (int) y1;
331                    tbls->xvid_noise2[i] = (int) y2;
332    
333                    if (RAND_N(6) == 0) {
334                            j--;
335                    }
336            }
337    
338            for (i = 0; i < MAX_RES; i++)
339                    for (j = 0; j < 3; j++) {
340                            tbls->xvid_prev_shift[i][j] = tbls->xvid_noise1 + (rand() & (MAX_SHIFT - 1));
341                            tbls->xvid_prev_shift[i][3 + j] = tbls->xvid_noise2 + (rand() & (MAX_SHIFT - 1));
342                    }
343    }
344    
345    void add_noise(XVID_POSTPROC *tbls, uint8_t *dst, uint8_t *src, int stride, int width, int height, int shiftptr, int quant)
346    {
347            int x, y;
348            int shift = 0;
349            int add = (quant < 5) ? 3 : 0;
350            int8_t *noise = (quant < 5) ? tbls->xvid_noise2 : tbls->xvid_noise1;
351    
352            for(y = 0; y < height; y++)
353            {
354            int8_t *src2 = (int8_t *) src;
355    
356                    shift = rand() & (MAX_SHIFT - 1);
357    
358                    shift &= ~7;
359                    for(x = 0; x < width; x++)
360                    {
361                            const int n = tbls->xvid_prev_shift[y][0 + add][x] + tbls->xvid_prev_shift[y][1 + add][x] +
362                                              tbls->xvid_prev_shift[y][2 + add][x];
363    
364                            dst[x] = src2[x] + ((n * src2[x]) >> 7);
365                    }
366    
367                    tbls->xvid_prev_shift[y][shiftptr + add] = noise + shift;
368    
369                    dst += stride;
370                    src += stride;
371            }
372    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.4.4

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