[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.4 - (view) (download)

1 : edgomez 1.2 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Postprocessing functions -
5 :     *
6 : syskin 1.4 * Copyright(C) 2003-2004 Michael Militzer <isibaar@xvid.org>
7 :     * 2004 Marc Fauconneau
8 : edgomez 1.2 *
9 :     * 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
11 :     * the Free Software Foundation ; either version 2 of the License, or
12 :     * (at your option) any later version.
13 :     *
14 :     * This program is distributed in the hope that it will be useful,
15 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
16 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 :     * GNU General Public License for more details.
18 :     *
19 :     * You should have received a copy of the GNU General Public License
20 :     * along with this program ; if not, write to the Free Software
21 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 :     *
23 : syskin 1.4 * $Id: postprocessing.c,v 1.3 2004/04/01 11:11:28 suxen_drol Exp $
24 : edgomez 1.2 *
25 :     ****************************************************************************/
26 :    
27 :     #include <stdlib.h>
28 :     #include <string.h>
29 :     #include <math.h>
30 :    
31 :     #include "../portab.h"
32 :     #include "../global.h"
33 :     #include "image.h"
34 :     #include "../utils/emms.h"
35 :     #include "postprocessing.h"
36 :    
37 : suxen_drol 1.3 /* function pointers */
38 :     IMAGEBRIGHTNESS_PTR image_brightness;
39 :    
40 :    
41 : edgomez 1.2 /* 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(XVID_POSTPROC *tbls)
51 :     {
52 :     init_deblock(tbls);
53 :     init_noise(tbls);
54 :     }
55 :    
56 :     void
57 :     image_postproc(XVID_POSTPROC *tbls, IMAGE * img, int edged_width,
58 :     const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
59 : suxen_drol 1.3 int flags, int brightness, int frame_num, int bvop)
60 : edgomez 1.2 {
61 :     const int edged_width2 = edged_width /2;
62 :     int i,j;
63 :     int quant;
64 : syskin 1.4 int dering = flags & XVID_DERINGY;
65 : edgomez 1.2
66 :     /* luma: j,i in block units */
67 :     if ((flags & XVID_DEBLOCKY))
68 :     {
69 :     for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */
70 :     for (i = 0; i < mb_width*2; i++)
71 :     {
72 :     quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
73 : syskin 1.4 deblock8x8_h(tbls, img->y + j*8*edged_width + i*8, edged_width, quant, dering);
74 : edgomez 1.2 }
75 :    
76 :     for (j = 0; j < mb_height*2; j++) /* vertical deblocking */
77 :     for (i = 1; i < mb_width*2; i++)
78 :     {
79 :     quant = mbs[(j+0)/2*mb_stride + (i/2)].quant;
80 : syskin 1.4 deblock8x8_v(tbls, img->y + j*8*edged_width + i*8, edged_width, quant, dering);
81 : edgomez 1.2 }
82 :     }
83 :    
84 :    
85 :     /* chroma */
86 :     if ((flags & XVID_DEBLOCKUV))
87 :     {
88 : syskin 1.4 dering = flags & XVID_DERINGUV;
89 :    
90 : edgomez 1.2 for (j = 1; j < mb_height; j++) /* horizontal deblocking */
91 :     for (i = 0; i < mb_width; i++)
92 :     {
93 :     quant = mbs[(j+0)*mb_stride + i].quant;
94 : syskin 1.4 deblock8x8_h(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant, dering);
95 :     deblock8x8_h(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant, dering);
96 : edgomez 1.2 }
97 :    
98 :     for (j = 0; j < mb_height; j++) /* vertical deblocking */
99 :     for (i = 1; i < mb_width; i++)
100 :     {
101 :     quant = mbs[(j+0)*mb_stride + i].quant;
102 : syskin 1.4 deblock8x8_v(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant, dering);
103 :     deblock8x8_v(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant, dering);
104 : edgomez 1.2 }
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 : suxen_drol 1.3
116 :     if (brightness != 0) {
117 :     image_brightness(img->y, edged_width, mb_width*16, mb_height*16, brightness);
118 :     }
119 : edgomez 1.2 }
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) \
136 :     /* Load pixel addresses and data for filtering */ \
137 :     s[0] = *(v[0] = img - 5*stride + x); \
138 :     s[1] = *(v[1] = img - 4*stride + x); \
139 :     s[2] = *(v[2] = img - 3*stride + x); \
140 :     s[3] = *(v[3] = img - 2*stride + x); \
141 :     s[4] = *(v[4] = img - 1*stride + x); \
142 :     s[5] = *(v[5] = img + 0*stride + x); \
143 :     s[6] = *(v[6] = img + 1*stride + x); \
144 :     s[7] = *(v[7] = img + 2*stride + x); \
145 :     s[8] = *(v[8] = img + 3*stride + x); \
146 :     s[9] = *(v[9] = img + 4*stride + x);
147 :    
148 :     #define LOAD_DATA_VER(x) \
149 :     /* Load pixel addresses and data for filtering */ \
150 :     s[0] = *(v[0] = img + x*stride - 5); \
151 :     s[1] = *(v[1] = img + x*stride - 4); \
152 :     s[2] = *(v[2] = img + x*stride - 3); \
153 :     s[3] = *(v[3] = img + x*stride - 2); \
154 :     s[4] = *(v[4] = img + x*stride - 1); \
155 :     s[5] = *(v[5] = img + x*stride + 0); \
156 :     s[6] = *(v[6] = img + x*stride + 1); \
157 :     s[7] = *(v[7] = img + x*stride + 2); \
158 :     s[8] = *(v[8] = img + x*stride + 3); \
159 :     s[9] = *(v[9] = img + x*stride + 4);
160 :    
161 : syskin 1.4 #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 : edgomez 1.2 #define APPLY_FILTER_CORE \
171 :     /* First, decide whether to use default or DC-offset mode */ \
172 :     \
173 :     eq_cnt = 0; \
174 :     \
175 :     eq_cnt += tbls->xvid_thresh_tbl[s[0] - s[1] + 255]; \
176 :     eq_cnt += tbls->xvid_thresh_tbl[s[1] - s[2] + 255]; \
177 :     eq_cnt += tbls->xvid_thresh_tbl[s[2] - s[3] + 255]; \
178 :     eq_cnt += tbls->xvid_thresh_tbl[s[3] - s[4] + 255]; \
179 :     eq_cnt += tbls->xvid_thresh_tbl[s[4] - s[5] + 255]; \
180 :     eq_cnt += tbls->xvid_thresh_tbl[s[5] - s[6] + 255]; \
181 :     eq_cnt += tbls->xvid_thresh_tbl[s[6] - s[7] + 255]; \
182 :     eq_cnt += tbls->xvid_thresh_tbl[s[7] - s[8] + 255]; \
183 :     \
184 :     if(eq_cnt < THR2) { /* Default mode */ \
185 :     int a30, a31, a32; \
186 :     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)); \
190 :     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)); \
192 :     \
193 :     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; \
195 :     \
196 :     if (limit > 0) \
197 :     diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff); \
198 :     else \
199 :     diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff); \
200 :     \
201 :     *v[4] -= diff; \
202 :     *v[5] += diff; \
203 :     } \
204 : syskin 1.4 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 : edgomez 1.2 } \
235 :     else { /* DC-offset mode */ \
236 :     uint8_t p0, p9; \
237 :     int min, max; \
238 :     \
239 :     /* Now decide whether to apply smoothing filter or not */ \
240 :     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]))))))); \
241 :     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]))))))); \
242 :     \
243 :     if(((max-min)) < 2*quant) { \
244 :     \
245 :     /* Choose edge pixels */ \
246 :     p0 = (tbls->xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \
247 :     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); \
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); \
251 :     *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); \
252 :     *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); \
253 :     *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); \
254 :     *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); \
255 :     *v[7] = (uint8_t) ((s[3] + s[4] + (s[5]<<1) + (s[6]<<1) + (s[7]<<2) + (s[8]<<1) + (p9<<2) + 8) >> 4); \
256 :     *v[8] = (uint8_t) ((s[4] + s[5] + (s[6]<<1) + (s[7]<<1) + (s[8]<<2) + 6*p9 + 8) >> 4); \
257 :     } \
258 :     }
259 :    
260 : syskin 1.4 void deblock8x8_h(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant, int dering)
261 : edgomez 1.2 {
262 :     int eq_cnt;
263 :     uint8_t *v[10];
264 : syskin 1.4 int s[10];
265 :     int e[10];
266 : edgomez 1.2
267 :     LOAD_DATA_HOR(0)
268 :     APPLY_FILTER_CORE
269 :    
270 :     LOAD_DATA_HOR(1)
271 :     APPLY_FILTER_CORE
272 :    
273 :     LOAD_DATA_HOR(2)
274 :     APPLY_FILTER_CORE
275 :    
276 :     LOAD_DATA_HOR(3)
277 :     APPLY_FILTER_CORE
278 :    
279 :     LOAD_DATA_HOR(4)
280 :     APPLY_FILTER_CORE
281 :    
282 :     LOAD_DATA_HOR(5)
283 :     APPLY_FILTER_CORE
284 :    
285 :     LOAD_DATA_HOR(6)
286 :     APPLY_FILTER_CORE
287 :    
288 :     LOAD_DATA_HOR(7)
289 :     APPLY_FILTER_CORE
290 :     }
291 :    
292 :    
293 : syskin 1.4 void deblock8x8_v(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant, int dering)
294 : edgomez 1.2 {
295 :     int eq_cnt;
296 :     uint8_t *v[10];
297 :     int s[10];
298 : syskin 1.4 int e[10];
299 : edgomez 1.2
300 :     LOAD_DATA_VER(0)
301 :     APPLY_FILTER_CORE
302 :    
303 :     LOAD_DATA_VER(1)
304 :     APPLY_FILTER_CORE
305 :    
306 :     LOAD_DATA_VER(2)
307 :     APPLY_FILTER_CORE
308 :    
309 :     LOAD_DATA_VER(3)
310 :     APPLY_FILTER_CORE
311 :    
312 :     LOAD_DATA_VER(4)
313 :     APPLY_FILTER_CORE
314 :    
315 :     LOAD_DATA_VER(5)
316 :     APPLY_FILTER_CORE
317 :    
318 :     LOAD_DATA_VER(6)
319 :     APPLY_FILTER_CORE
320 :    
321 :     LOAD_DATA_VER(7)
322 :     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 : suxen_drol 1.3 }
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 :     dst[y*stride + x] = CLIP( dst[y*stride + x] + offset, 0, 255);
437 :     }
438 : edgomez 1.2 }
439 :     }

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