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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1397 - (view) (download)

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

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