[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.1, Wed Dec 10 01:02:40 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     ****************************************************************************/
23    
24    #include <stdlib.h>
25    #include <string.h>
26    #include <math.h>
27    
28    #include "../portab.h"
29    #include "../global.h"
30    #include "image.h"
31    #include "postprocessing.h"
32    
33    /* Filtering thresholds */
34    
35    #define THR1 2
36    #define THR2 6
37    
38    /* Some useful (and fast) macros
39       Note that the MIN/MAX macros assume signed shift - if your compiler
40       doesn't do signed shifts, use the default MIN/MAX macros from global.h */
41    
42    #define FAST_MAX(x,y) ((x) - ((((x) - (y))>>(32 - 1)) & ((x) - (y))))
43    #define FAST_MIN(x,y) ((x) + ((((y) - (x))>>(32 - 1)) & ((y) - (x))))
44    #define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31)
45    #define ABS(X)    (((X)>0)?(X):-(X))
46    
47    static int8_t xvid_thresh_tbl[510];
48    static int8_t xvid_abs_tbl[510];
49    
50    void init_postproc(void)
51    {
52            int i;
53    
54            for(i = -255; i < 256; i++) {
55                    xvid_thresh_tbl[i + 255] = 0;
56                    if(ABS(i) < THR1)
57                            xvid_thresh_tbl[i + 255] = 1;
58                    xvid_abs_tbl[i + 255] = ABS(i);
59            }
60    }
61    
62    void
63    image_deblock(IMAGE * img, int edged_width,
64                                    const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
65                                    int flags)
66    {
67            const int edged_width2 = edged_width /2;
68            int i,j;
69            int quant;
70    
71            /* luma: j,i in block units */
72            if ((flags & XVID_DEBLOCKY))
73            {
74                    for (j = 1; j < mb_height*2; j++)               /* horizontal deblocking */
75                    for (i = 0; i < mb_width*2; i++)
76                    {
77                            quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
78                            deblock8x8_h(img->y + j*8*edged_width + i*8, edged_width, quant);
79                    }
80    
81                    for (j = 0; j < mb_height*2; j++)               /* vertical deblocking */
82                    for (i = 1; i < mb_width*2; i++)
83                    {
84                            quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
85                            deblock8x8_v(img->y + j*8*edged_width + i*8, edged_width, quant);
86                    }
87            }
88    
89    
90            /* chroma */
91            if ((flags & XVID_DEBLOCKUV))
92            {
93                    for (j = 1; j < mb_height; j++)         /* horizontal deblocking */
94                    for (i = 0; i < mb_width; i++)
95                    {
96                            quant = mbs[(j+0)*mb_stride + i].quant;
97                            deblock8x8_h(img->u + j*8*edged_width2 + i*8, edged_width2, quant);
98                            deblock8x8_h(img->v + j*8*edged_width2 + i*8, edged_width2, quant);
99                    }
100    
101                    for (j = 0; j < mb_height; j++)         /* vertical deblocking */
102                    for (i = 1; i < mb_width; i++)
103                    {
104                            quant = mbs[(j+0)*mb_stride + i].quant;
105                            deblock8x8_v(img->u + j*8*edged_width2 + i*8, edged_width2, quant);
106                            deblock8x8_v(img->v + j*8*edged_width2 + i*8, edged_width2, quant);
107                    }
108            }
109    }
110    
111    #define LOAD_DATA_HOR(x) \
112                    /* Load pixel addresses and data for filtering */ \
113                s[0] = *(v[0] = img - 5*stride + x); \
114                    s[1] = *(v[1] = img - 4*stride + x); \
115                    s[2] = *(v[2] = img - 3*stride + x); \
116                    s[3] = *(v[3] = img - 2*stride + x); \
117                    s[4] = *(v[4] = img - 1*stride + x); \
118                    s[5] = *(v[5] = img + 0*stride + x); \
119                    s[6] = *(v[6] = img + 1*stride + x); \
120                    s[7] = *(v[7] = img + 2*stride + x); \
121                    s[8] = *(v[8] = img + 3*stride + x); \
122                    s[9] = *(v[9] = img + 4*stride + x);
123    
124    #define LOAD_DATA_VER(x) \
125                    /* Load pixel addresses and data for filtering */ \
126                    s[0] = *(v[0] = img + x*stride - 5); \
127                    s[1] = *(v[1] = img + x*stride - 4); \
128                    s[2] = *(v[2] = img + x*stride - 3); \
129                    s[3] = *(v[3] = img + x*stride - 2); \
130                    s[4] = *(v[4] = img + x*stride - 1); \
131                    s[5] = *(v[5] = img + x*stride + 0); \
132                    s[6] = *(v[6] = img + x*stride + 1); \
133                    s[7] = *(v[7] = img + x*stride + 2); \
134                    s[8] = *(v[8] = img + x*stride + 3); \
135                    s[9] = *(v[9] = img + x*stride + 4);
136    
137    #define APPLY_FILTER_CORE \
138                    /* First, decide whether to use default or DC-offset mode */ \
139                    \
140                    eq_cnt = 0; \
141                    \
142                    eq_cnt += xvid_thresh_tbl[s[0] - s[1] + 255]; \
143                    eq_cnt += xvid_thresh_tbl[s[1] - s[2] + 255]; \
144                    eq_cnt += xvid_thresh_tbl[s[2] - s[3] + 255]; \
145                    eq_cnt += xvid_thresh_tbl[s[3] - s[4] + 255]; \
146                    eq_cnt += xvid_thresh_tbl[s[4] - s[5] + 255]; \
147                    eq_cnt += xvid_thresh_tbl[s[5] - s[6] + 255]; \
148                    eq_cnt += xvid_thresh_tbl[s[6] - s[7] + 255]; \
149                    eq_cnt += xvid_thresh_tbl[s[7] - s[8] + 255]; \
150                    \
151                    if(eq_cnt < THR2) { /* Default mode */  \
152                            int a30, a31, a32;                                      \
153                            int diff, limit;                                        \
154                                                                                                    \
155                            a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1));    \
156                                                                                                                                            \
157                            if(xvid_abs_tbl[a30 + 255] < 8*quant) {                                                         \
158                                    a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1));    \
159                                    a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1));    \
160                                                                                                                                                    \
161                                    diff = (5 * ((SIGN(a30) * MIN(xvid_abs_tbl[a30 + 255], MIN(xvid_abs_tbl[a31 + 255], xvid_abs_tbl[a32 + 255]))) - a30) + 32) >> 6;       \
162                                    limit = (s[4] - s[5]) / 2;      \
163                                    \
164                                    if (limit > 0)                          \
165                                            diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff);        \
166                                    else    \
167                                            diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff);        \
168                                                                                                                                                                    \
169                                    *v[4] -= diff;  \
170                                    *v[5] += diff;  \
171                            }       \
172                    }       \
173                    else {  /* DC-offset mode */    \
174                            uint8_t p0, p9; \
175                            int min, max;   \
176                                                            \
177                            /* Now decide whether to apply smoothing filter or not */       \
178                            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])))))));     \
179                            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])))))));     \
180                            \
181                            if(((max-min)) < 2*quant) {     \
182                                                                                    \
183                                    /* Choose edge pixels */        \
184                                    p0 = (xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \
185                                    p9 = (xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8]; \
186                                                                                                                                    \
187                                    *v[1] = (uint8_t) ((6*p0 + (s[1]<<2) + (s[2]<<1) + (s[3]<<1) + s[4] + s[5] + 8) >> 4);  \
188                                    *v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4);   \
189                                    *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);       \
190                                    *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);     \
191                                    *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);     \
192                                    *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);       \
193                                    *v[7] = (uint8_t) ((s[3] + s[4] + (s[5]<<1) + (s[6]<<1) + (s[7]<<2) + (s[8]<<1) + (p9<<2) + 8) >> 4);   \
194                                    *v[8] = (uint8_t) ((s[4] + s[5] + (s[6]<<1) + (s[7]<<1) + (s[8]<<2) + 6*p9 + 8) >> 4);  \
195                            }       \
196                    }
197    
198    void deblock8x8_h(uint8_t *img, int stride, int quant)
199    {
200            int eq_cnt;
201            uint8_t *v[10];
202            int32_t s[10];
203    
204            LOAD_DATA_HOR(0)
205            APPLY_FILTER_CORE
206    
207            LOAD_DATA_HOR(1)
208            APPLY_FILTER_CORE
209    
210            LOAD_DATA_HOR(2)
211            APPLY_FILTER_CORE
212    
213            LOAD_DATA_HOR(3)
214            APPLY_FILTER_CORE
215    
216            LOAD_DATA_HOR(4)
217            APPLY_FILTER_CORE
218    
219            LOAD_DATA_HOR(5)
220            APPLY_FILTER_CORE
221    
222            LOAD_DATA_HOR(6)
223            APPLY_FILTER_CORE
224    
225            LOAD_DATA_HOR(7)
226            APPLY_FILTER_CORE
227    }
228    
229    
230    void deblock8x8_v(uint8_t *img, int stride, int quant)
231    {
232            int eq_cnt;
233            uint8_t *v[10];
234            int s[10];
235    
236            LOAD_DATA_VER(0)
237            APPLY_FILTER_CORE
238    
239            LOAD_DATA_VER(1)
240            APPLY_FILTER_CORE
241    
242            LOAD_DATA_VER(2)
243            APPLY_FILTER_CORE
244    
245            LOAD_DATA_VER(3)
246            APPLY_FILTER_CORE
247    
248            LOAD_DATA_VER(4)
249            APPLY_FILTER_CORE
250    
251            LOAD_DATA_VER(5)
252            APPLY_FILTER_CORE
253    
254            LOAD_DATA_VER(6)
255            APPLY_FILTER_CORE
256    
257            LOAD_DATA_VER(7)
258            APPLY_FILTER_CORE
259    }

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

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