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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.4.3 - (view) (download)

1 : Isibaar 1.1.4.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 : Isibaar 1.1.4.3 * $Id: postprocessing.c,v 1.1.4.2 2003/12/10 15:07:42 edgomez Exp $
23 : edgomez 1.1.4.2 *
24 : Isibaar 1.1.4.1 ****************************************************************************/
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 : Isibaar 1.1.4.3 #include "../utils/emms.h"
34 : Isibaar 1.1.4.1 #include "postprocessing.h"
35 :    
36 :     /* Filtering thresholds */
37 :    
38 :     #define THR1 2
39 :     #define THR2 6
40 :    
41 :     /* Some useful (and fast) macros
42 :     Note that the MIN/MAX macros assume signed shift - if your compiler
43 :     doesn't do signed shifts, use the default MIN/MAX macros from global.h */
44 :    
45 :     #define FAST_MAX(x,y) ((x) - ((((x) - (y))>>(32 - 1)) & ((x) - (y))))
46 :     #define FAST_MIN(x,y) ((x) + ((((y) - (x))>>(32 - 1)) & ((y) - (x))))
47 :     #define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31)
48 :     #define ABS(X) (((X)>0)?(X):-(X))
49 :    
50 :     void init_postproc(void)
51 :     {
52 : Isibaar 1.1.4.3 init_deblock();
53 :     init_noise();
54 : Isibaar 1.1.4.1 }
55 :    
56 :     void
57 : Isibaar 1.1.4.3 image_postproc(IMAGE * img, int edged_width,
58 : Isibaar 1.1.4.1 const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
59 : Isibaar 1.1.4.3 int flags, int frame_num)
60 : Isibaar 1.1.4.1 {
61 :     const int edged_width2 = edged_width /2;
62 :     int i,j;
63 :     int quant;
64 :    
65 :     /* luma: j,i in block units */
66 :     if ((flags & XVID_DEBLOCKY))
67 :     {
68 :     for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */
69 :     for (i = 0; i < mb_width*2; i++)
70 :     {
71 :     quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
72 :     deblock8x8_h(img->y + j*8*edged_width + i*8, edged_width, quant);
73 :     }
74 :    
75 :     for (j = 0; j < mb_height*2; j++) /* vertical deblocking */
76 :     for (i = 1; i < mb_width*2; i++)
77 :     {
78 :     quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
79 :     deblock8x8_v(img->y + j*8*edged_width + i*8, edged_width, quant);
80 :     }
81 :     }
82 :    
83 :    
84 :     /* chroma */
85 :     if ((flags & XVID_DEBLOCKUV))
86 :     {
87 :     for (j = 1; j < mb_height; j++) /* horizontal deblocking */
88 :     for (i = 0; i < mb_width; i++)
89 :     {
90 :     quant = mbs[(j+0)*mb_stride + i].quant;
91 :     deblock8x8_h(img->u + j*8*edged_width2 + i*8, edged_width2, quant);
92 :     deblock8x8_h(img->v + j*8*edged_width2 + i*8, edged_width2, quant);
93 :     }
94 :    
95 :     for (j = 0; j < mb_height; j++) /* vertical deblocking */
96 :     for (i = 1; i < mb_width; i++)
97 :     {
98 :     quant = mbs[(j+0)*mb_stride + i].quant;
99 :     deblock8x8_v(img->u + j*8*edged_width2 + i*8, edged_width2, quant);
100 :     deblock8x8_v(img->v + j*8*edged_width2 + i*8, edged_width2, quant);
101 :     }
102 :     }
103 : Isibaar 1.1.4.3
104 :     if ((flags & XVID_FILMEFFECT))
105 :     {
106 :     add_noise(img->y, img->y, edged_width, mb_width*16, mb_height*16, frame_num % 3);
107 :     }
108 :     }
109 :    
110 :     /******************************************************************************/
111 :    
112 :     static int8_t xvid_thresh_tbl[510];
113 :     static int8_t xvid_abs_tbl[510];
114 :    
115 :     void init_deblock(void)
116 :     {
117 :     int i;
118 :    
119 :     for(i = -255; i < 256; i++) {
120 :     xvid_thresh_tbl[i + 255] = 0;
121 :     if(ABS(i) < THR1)
122 :     xvid_thresh_tbl[i + 255] = 1;
123 :     xvid_abs_tbl[i + 255] = ABS(i);
124 :     }
125 : Isibaar 1.1.4.1 }
126 :    
127 :     #define LOAD_DATA_HOR(x) \
128 :     /* Load pixel addresses and data for filtering */ \
129 :     s[0] = *(v[0] = img - 5*stride + x); \
130 :     s[1] = *(v[1] = img - 4*stride + x); \
131 :     s[2] = *(v[2] = img - 3*stride + x); \
132 :     s[3] = *(v[3] = img - 2*stride + x); \
133 :     s[4] = *(v[4] = img - 1*stride + x); \
134 :     s[5] = *(v[5] = img + 0*stride + x); \
135 :     s[6] = *(v[6] = img + 1*stride + x); \
136 :     s[7] = *(v[7] = img + 2*stride + x); \
137 :     s[8] = *(v[8] = img + 3*stride + x); \
138 :     s[9] = *(v[9] = img + 4*stride + x);
139 :    
140 :     #define LOAD_DATA_VER(x) \
141 :     /* Load pixel addresses and data for filtering */ \
142 :     s[0] = *(v[0] = img + x*stride - 5); \
143 :     s[1] = *(v[1] = img + x*stride - 4); \
144 :     s[2] = *(v[2] = img + x*stride - 3); \
145 :     s[3] = *(v[3] = img + x*stride - 2); \
146 :     s[4] = *(v[4] = img + x*stride - 1); \
147 :     s[5] = *(v[5] = img + x*stride + 0); \
148 :     s[6] = *(v[6] = img + x*stride + 1); \
149 :     s[7] = *(v[7] = img + x*stride + 2); \
150 :     s[8] = *(v[8] = img + x*stride + 3); \
151 :     s[9] = *(v[9] = img + x*stride + 4);
152 :    
153 :     #define APPLY_FILTER_CORE \
154 :     /* First, decide whether to use default or DC-offset mode */ \
155 :     \
156 :     eq_cnt = 0; \
157 :     \
158 :     eq_cnt += xvid_thresh_tbl[s[0] - s[1] + 255]; \
159 :     eq_cnt += xvid_thresh_tbl[s[1] - s[2] + 255]; \
160 :     eq_cnt += xvid_thresh_tbl[s[2] - s[3] + 255]; \
161 :     eq_cnt += xvid_thresh_tbl[s[3] - s[4] + 255]; \
162 :     eq_cnt += xvid_thresh_tbl[s[4] - s[5] + 255]; \
163 :     eq_cnt += xvid_thresh_tbl[s[5] - s[6] + 255]; \
164 :     eq_cnt += xvid_thresh_tbl[s[6] - s[7] + 255]; \
165 :     eq_cnt += xvid_thresh_tbl[s[7] - s[8] + 255]; \
166 :     \
167 :     if(eq_cnt < THR2) { /* Default mode */ \
168 :     int a30, a31, a32; \
169 :     int diff, limit; \
170 :     \
171 : Isibaar 1.1.4.3 if(xvid_abs_tbl[(s[4] - s[5]) + 255] < quant) { \
172 :     a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1)); \
173 : Isibaar 1.1.4.1 a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1)); \
174 :     a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1)); \
175 :     \
176 :     diff = (5 * ((SIGN(a30) * MIN(xvid_abs_tbl[a30 + 255], MIN(xvid_abs_tbl[a31 + 255], xvid_abs_tbl[a32 + 255]))) - a30) + 32) >> 6; \
177 :     limit = (s[4] - s[5]) / 2; \
178 :     \
179 :     if (limit > 0) \
180 :     diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff); \
181 :     else \
182 :     diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff); \
183 :     \
184 :     *v[4] -= diff; \
185 :     *v[5] += diff; \
186 :     } \
187 :     } \
188 :     else { /* DC-offset mode */ \
189 :     uint8_t p0, p9; \
190 :     int min, max; \
191 :     \
192 :     /* Now decide whether to apply smoothing filter or not */ \
193 :     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]))))))); \
194 :     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]))))))); \
195 :     \
196 :     if(((max-min)) < 2*quant) { \
197 :     \
198 :     /* Choose edge pixels */ \
199 :     p0 = (xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \
200 :     p9 = (xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8]; \
201 :     \
202 :     *v[1] = (uint8_t) ((6*p0 + (s[1]<<2) + (s[2]<<1) + (s[3]<<1) + s[4] + s[5] + 8) >> 4); \
203 :     *v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4); \
204 :     *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); \
205 :     *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); \
206 :     *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); \
207 :     *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); \
208 :     *v[7] = (uint8_t) ((s[3] + s[4] + (s[5]<<1) + (s[6]<<1) + (s[7]<<2) + (s[8]<<1) + (p9<<2) + 8) >> 4); \
209 :     *v[8] = (uint8_t) ((s[4] + s[5] + (s[6]<<1) + (s[7]<<1) + (s[8]<<2) + 6*p9 + 8) >> 4); \
210 :     } \
211 :     }
212 :    
213 :     void deblock8x8_h(uint8_t *img, int stride, int quant)
214 :     {
215 :     int eq_cnt;
216 :     uint8_t *v[10];
217 :     int32_t s[10];
218 :    
219 :     LOAD_DATA_HOR(0)
220 :     APPLY_FILTER_CORE
221 :    
222 :     LOAD_DATA_HOR(1)
223 :     APPLY_FILTER_CORE
224 :    
225 :     LOAD_DATA_HOR(2)
226 :     APPLY_FILTER_CORE
227 :    
228 :     LOAD_DATA_HOR(3)
229 :     APPLY_FILTER_CORE
230 :    
231 :     LOAD_DATA_HOR(4)
232 :     APPLY_FILTER_CORE
233 :    
234 :     LOAD_DATA_HOR(5)
235 :     APPLY_FILTER_CORE
236 :    
237 :     LOAD_DATA_HOR(6)
238 :     APPLY_FILTER_CORE
239 :    
240 :     LOAD_DATA_HOR(7)
241 :     APPLY_FILTER_CORE
242 :     }
243 :    
244 :    
245 :     void deblock8x8_v(uint8_t *img, int stride, int quant)
246 :     {
247 :     int eq_cnt;
248 :     uint8_t *v[10];
249 :     int s[10];
250 :    
251 :     LOAD_DATA_VER(0)
252 :     APPLY_FILTER_CORE
253 :    
254 :     LOAD_DATA_VER(1)
255 :     APPLY_FILTER_CORE
256 :    
257 :     LOAD_DATA_VER(2)
258 :     APPLY_FILTER_CORE
259 :    
260 :     LOAD_DATA_VER(3)
261 :     APPLY_FILTER_CORE
262 :    
263 :     LOAD_DATA_VER(4)
264 :     APPLY_FILTER_CORE
265 :    
266 :     LOAD_DATA_VER(5)
267 :     APPLY_FILTER_CORE
268 :    
269 :     LOAD_DATA_VER(6)
270 :     APPLY_FILTER_CORE
271 :    
272 :     LOAD_DATA_VER(7)
273 :     APPLY_FILTER_CORE
274 : Isibaar 1.1.4.3 }
275 :    
276 :     /******************************************************************************
277 :     * *
278 :     * Noise code below taken from MPlayer: http://www.mplayerhq.hu/ *
279 :     * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at> *
280 :     * *
281 :     ******************************************************************************/
282 :    
283 :     #define MAX_NOISE 4096
284 :     #define MAX_SHIFT 1024
285 :     #define MAX_RES (MAX_NOISE - MAX_SHIFT)
286 :    
287 :     #define RAND_N(range) ((int) ((double)range * rand() / (RAND_MAX + 1.0)))
288 :    
289 :     #define STRENGTH 13
290 :    
291 :     static int8_t xvid_noise[MAX_NOISE * sizeof(int8_t)];
292 :     static int8_t *xvid_prev_shift[MAX_RES][3];
293 :    
294 :     void init_noise(void)
295 :     {
296 :     int i, j;
297 :     int patt[4] = { -1,0,1,0 };
298 :    
299 :     emms();
300 :    
301 :     srand(123457);
302 :    
303 :     for(i = 0, j = 0; i < MAX_NOISE; i++, j++)
304 :     {
305 :     double x1, x2, w, y1;
306 :    
307 :     do {
308 :     x1 = 2.0 * rand() / (float) RAND_MAX - 1.0;
309 :     x2 = 2.0 * rand() / (float) RAND_MAX - 1.0;
310 :     w = x1 * x1 + x2 * x2;
311 :     } while (w >= 1.0);
312 :    
313 :     w = sqrt((-2.0 * log(w)) / w);
314 :     y1 = x1 * w;
315 :     y1 *= STRENGTH / sqrt(3.0);
316 :    
317 :     y1 /= 2;
318 :     y1 += patt[j%4] * STRENGTH * 0.35;
319 :    
320 :     if (y1 < -128) {
321 :     y1=-128;
322 :     }
323 :     else if (y1 > 127) {
324 :     y1= 127;
325 :     }
326 :    
327 :     y1 /= 3.0;
328 :     xvid_noise[i] = (int) y1;
329 :    
330 :     if (RAND_N(6) == 0) {
331 :     j--;
332 :     }
333 :     }
334 :    
335 :     for (i = 0; i < MAX_RES; i++)
336 :     for (j = 0; j < 3; j++) {
337 :     xvid_prev_shift[i][j] = xvid_noise + (rand() & (MAX_SHIFT - 1));
338 :     }
339 :     }
340 :    
341 :     void add_noise(uint8_t *dst, uint8_t *src, int stride, int width, int height, int shiftptr)
342 :     {
343 :     int x, y;
344 :     int shift = 0;
345 :    
346 :     for(y = 0; y < height; y++)
347 :     {
348 :     int8_t *src2 = (int8_t *) src;
349 :    
350 :     shift = rand() & (MAX_SHIFT - 1);
351 :    
352 :     shift &= ~7;
353 :     for(x = 0; x < width; x++)
354 :     {
355 :     const int n = xvid_prev_shift[y][0][x] + xvid_prev_shift[y][1][x] +
356 :     xvid_prev_shift[y][2][x];
357 :    
358 :     dst[x] = src2[x] + ((n * src2[x]) >> 7);
359 :     }
360 :    
361 :     xvid_prev_shift[y][shiftptr] = xvid_noise + shift;
362 :    
363 :     dst += stride;
364 :     src += stride;
365 :     }
366 : edgomez 1.1.4.2 }

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