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

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

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