30 |
#include "../portab.h" |
#include "../portab.h" |
31 |
#include "../global.h" |
#include "../global.h" |
32 |
#include "image.h" |
#include "image.h" |
33 |
|
#include "../utils/emms.h" |
34 |
#include "postprocessing.h" |
#include "postprocessing.h" |
35 |
|
|
36 |
/* Filtering thresholds */ |
/* function pointers */ |
37 |
|
IMAGEBRIGHTNESS_PTR image_brightness; |
38 |
|
|
|
#define THR1 2 |
|
|
#define THR2 6 |
|
39 |
|
|
40 |
/* Some useful (and fast) macros |
/* Some useful (and fast) macros |
41 |
Note that the MIN/MAX macros assume signed shift - if your compiler |
Note that the MIN/MAX macros assume signed shift - if your compiler |
46 |
#define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31) |
#define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31) |
47 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
48 |
|
|
49 |
static int8_t xvid_thresh_tbl[510]; |
void init_postproc(XVID_POSTPROC *tbls) |
|
static int8_t xvid_abs_tbl[510]; |
|
|
|
|
|
void init_postproc(void) |
|
50 |
{ |
{ |
51 |
int i; |
init_deblock(tbls); |
52 |
|
init_noise(tbls); |
|
for(i = -255; i < 256; i++) { |
|
|
xvid_thresh_tbl[i + 255] = 0; |
|
|
if(ABS(i) < THR1) |
|
|
xvid_thresh_tbl[i + 255] = 1; |
|
|
xvid_abs_tbl[i + 255] = ABS(i); |
|
|
} |
|
53 |
} |
} |
54 |
|
|
55 |
void |
void |
56 |
image_deblock(IMAGE * img, int edged_width, |
image_postproc(XVID_POSTPROC *tbls, IMAGE * img, int edged_width, |
57 |
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
58 |
int flags) |
int flags, int brightness, int frame_num, int bvop) |
59 |
{ |
{ |
60 |
const int edged_width2 = edged_width /2; |
const int edged_width2 = edged_width /2; |
61 |
int i,j; |
int i,j; |
68 |
for (i = 0; i < mb_width*2; i++) |
for (i = 0; i < mb_width*2; i++) |
69 |
{ |
{ |
70 |
quant = mbs[(j+0)/2*mb_stride + (i/2)].quant; |
quant = mbs[(j+0)/2*mb_stride + (i/2)].quant; |
71 |
deblock8x8_h(img->y + j*8*edged_width + i*8, edged_width, quant); |
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 */ |
for (j = 0; j < mb_height*2; j++) /* vertical deblocking */ |
75 |
for (i = 1; i < mb_width*2; i++) |
for (i = 1; i < mb_width*2; i++) |
76 |
{ |
{ |
77 |
quant = mbs[(j+0)/2*mb_stride + (i/2)].quant; |
quant = mbs[(j+0)/2*mb_stride + (i/2)].quant; |
78 |
deblock8x8_v(img->y + j*8*edged_width + i*8, edged_width, quant); |
deblock8x8_v(tbls, img->y + j*8*edged_width + i*8, edged_width, quant); |
79 |
} |
} |
80 |
} |
} |
81 |
|
|
87 |
for (i = 0; i < mb_width; i++) |
for (i = 0; i < mb_width; i++) |
88 |
{ |
{ |
89 |
quant = mbs[(j+0)*mb_stride + i].quant; |
quant = mbs[(j+0)*mb_stride + i].quant; |
90 |
deblock8x8_h(img->u + j*8*edged_width2 + i*8, edged_width2, quant); |
deblock8x8_h(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant); |
91 |
deblock8x8_h(img->v + j*8*edged_width2 + i*8, edged_width2, quant); |
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 */ |
for (j = 0; j < mb_height; j++) /* vertical deblocking */ |
95 |
for (i = 1; i < mb_width; i++) |
for (i = 1; i < mb_width; i++) |
96 |
{ |
{ |
97 |
quant = mbs[(j+0)*mb_stride + i].quant; |
quant = mbs[(j+0)*mb_stride + i].quant; |
98 |
deblock8x8_v(img->u + j*8*edged_width2 + i*8, edged_width2, quant); |
deblock8x8_v(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant); |
99 |
deblock8x8_v(img->v + j*8*edged_width2 + i*8, edged_width2, quant); |
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 |
|
|
112 |
|
if (brightness != 0) { |
113 |
|
image_brightness(img->y, edged_width, mb_width*16, mb_height*16, brightness); |
114 |
|
} |
115 |
|
} |
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 |
|
|
159 |
\ |
\ |
160 |
eq_cnt = 0; \ |
eq_cnt = 0; \ |
161 |
\ |
\ |
162 |
eq_cnt += xvid_thresh_tbl[s[0] - s[1] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[0] - s[1] + 255]; \ |
163 |
eq_cnt += xvid_thresh_tbl[s[1] - s[2] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[1] - s[2] + 255]; \ |
164 |
eq_cnt += xvid_thresh_tbl[s[2] - s[3] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[2] - s[3] + 255]; \ |
165 |
eq_cnt += xvid_thresh_tbl[s[3] - s[4] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[3] - s[4] + 255]; \ |
166 |
eq_cnt += xvid_thresh_tbl[s[4] - s[5] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[4] - s[5] + 255]; \ |
167 |
eq_cnt += xvid_thresh_tbl[s[5] - s[6] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[5] - s[6] + 255]; \ |
168 |
eq_cnt += xvid_thresh_tbl[s[6] - s[7] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[6] - s[7] + 255]; \ |
169 |
eq_cnt += xvid_thresh_tbl[s[7] - s[8] + 255]; \ |
eq_cnt += tbls->xvid_thresh_tbl[s[7] - s[8] + 255]; \ |
170 |
\ |
\ |
171 |
if(eq_cnt < THR2) { /* Default mode */ \ |
if(eq_cnt < THR2) { /* Default mode */ \ |
172 |
int a30, a31, a32; \ |
int a30, a31, a32; \ |
173 |
int diff, limit; \ |
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)); \ |
a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1)); \ |
|
\ |
|
|
if(xvid_abs_tbl[a30 + 255] < 8*quant) { \ |
|
177 |
a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1)); \ |
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)); \ |
a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1)); \ |
179 |
\ |
\ |
180 |
diff = (5 * ((SIGN(a30) * MIN(xvid_abs_tbl[a30 + 255], MIN(xvid_abs_tbl[a31 + 255], xvid_abs_tbl[a32 + 255]))) - a30) + 32) >> 6; \ |
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; \ |
limit = (s[4] - s[5]) / 2; \ |
182 |
\ |
\ |
183 |
if (limit > 0) \ |
if (limit > 0) \ |
200 |
if(((max-min)) < 2*quant) { \ |
if(((max-min)) < 2*quant) { \ |
201 |
\ |
\ |
202 |
/* Choose edge pixels */ \ |
/* Choose edge pixels */ \ |
203 |
p0 = (xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \ |
p0 = (tbls->xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \ |
204 |
p9 = (xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8]; \ |
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); \ |
*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); \ |
*v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4); \ |
214 |
} \ |
} \ |
215 |
} |
} |
216 |
|
|
217 |
void deblock8x8_h(uint8_t *img, int stride, int quant) |
void deblock8x8_h(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant) |
218 |
{ |
{ |
219 |
int eq_cnt; |
int eq_cnt; |
220 |
uint8_t *v[10]; |
uint8_t *v[10]; |
246 |
} |
} |
247 |
|
|
248 |
|
|
249 |
void deblock8x8_v(uint8_t *img, int stride, int quant) |
void deblock8x8_v(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant) |
250 |
{ |
{ |
251 |
int eq_cnt; |
int eq_cnt; |
252 |
uint8_t *v[10]; |
uint8_t *v[10]; |
276 |
LOAD_DATA_VER(7) |
LOAD_DATA_VER(7) |
277 |
APPLY_FILTER_CORE |
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 |
|
|
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 |
|
} |