3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Postprocessing functions - |
* - Postprocessing functions - |
5 |
* |
* |
6 |
* Copyright(C) 2003 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2003-2010 Michael Militzer <isibaar@xvid.org> |
7 |
|
* 2004 Marc Fauconneau |
8 |
* |
* |
9 |
* This program is free software ; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
34 |
#include "../utils/emms.h" |
#include "../utils/emms.h" |
35 |
#include "postprocessing.h" |
#include "postprocessing.h" |
36 |
|
|
37 |
|
/* function pointers */ |
38 |
|
IMAGEBRIGHTNESS_PTR image_brightness; |
39 |
|
|
40 |
|
|
41 |
/* Some useful (and fast) macros |
/* Some useful (and fast) macros |
42 |
Note that the MIN/MAX macros assume signed shift - if your compiler |
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 */ |
doesn't do signed shifts, use the default MIN/MAX macros from global.h */ |
54 |
} |
} |
55 |
|
|
56 |
void |
void |
57 |
image_postproc(XVID_POSTPROC *tbls, IMAGE * img, int edged_width, |
stripe_deblock_h(SMPDeblock *h) |
|
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
|
|
int flags, int frame_num, int bvop) |
|
58 |
{ |
{ |
59 |
const int edged_width2 = edged_width /2; |
const int stride = h->stride; |
60 |
|
const int stride2 = stride /2; |
61 |
|
|
62 |
int i,j; |
int i,j; |
63 |
int quant; |
int quant; |
64 |
|
|
65 |
/* luma: j,i in block units */ |
/* luma: j,i in block units */ |
66 |
if ((flags & XVID_DEBLOCKY)) |
if ((h->flags & XVID_DEBLOCKY)) |
67 |
{ |
{ |
68 |
for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */ |
int dering = h->flags & XVID_DERINGY; |
69 |
for (i = 0; i < mb_width*2; i++) |
|
70 |
|
for (j = 1; j < h->stop_y; j++) /* horizontal luma deblocking */ |
71 |
|
for (i = h->start_x; i < h->stop_x; i++) |
72 |
{ |
{ |
73 |
quant = mbs[(j+0)/2*mb_stride + (i/2)].quant; |
quant = h->mbs[(j+0)/2*h->mb_stride + (i/2)].quant; |
74 |
deblock8x8_h(tbls, img->y + j*8*edged_width + i*8, edged_width, quant); |
deblock8x8_h(h->tbls, h->img->y + j*8*stride + i*8, stride, quant, dering); |
75 |
} |
} |
76 |
|
} |
77 |
|
|
78 |
|
/* chroma */ |
79 |
|
if ((h->flags & XVID_DEBLOCKUV)) |
80 |
|
{ |
81 |
|
int dering = h->flags & XVID_DERINGUV; |
82 |
|
|
83 |
for (j = 0; j < mb_height*2; j++) /* vertical deblocking */ |
for (j = 1; j < h->stop_y/2; j++) /* horizontal deblocking */ |
84 |
for (i = 1; i < mb_width*2; i++) |
for (i = h->start_x/2; i < h->stop_x/2; i++) |
85 |
{ |
{ |
86 |
quant = mbs[(j+0)/2*mb_stride + (i/2)].quant; |
quant = h->mbs[(j+0)*h->mb_stride + i].quant; |
87 |
deblock8x8_v(tbls, img->y + j*8*edged_width + i*8, edged_width, quant); |
deblock8x8_h(h->tbls, h->img->u + j*8*stride2 + i*8, stride2, quant, dering); |
88 |
|
deblock8x8_h(h->tbls, h->img->v + j*8*stride2 + i*8, stride2, quant, dering); |
89 |
|
} |
90 |
} |
} |
91 |
} |
} |
92 |
|
|
93 |
|
void |
94 |
|
stripe_deblock_v(SMPDeblock *h) |
95 |
|
{ |
96 |
|
const int stride = h->stride; |
97 |
|
const int stride2 = stride /2; |
98 |
|
|
99 |
|
int i,j; |
100 |
|
int quant; |
101 |
|
|
102 |
/* chroma */ |
/* luma: j,i in block units */ |
103 |
if ((flags & XVID_DEBLOCKUV)) |
if ((h->flags & XVID_DEBLOCKY)) |
104 |
{ |
{ |
105 |
for (j = 1; j < mb_height; j++) /* horizontal deblocking */ |
int dering = h->flags & XVID_DERINGY; |
106 |
for (i = 0; i < mb_width; i++) |
|
107 |
|
for (j = h->start_y; j < h->stop_y; j++) /* vertical deblocking */ |
108 |
|
for (i = 1; i < h->stop_x; i++) |
109 |
{ |
{ |
110 |
quant = mbs[(j+0)*mb_stride + i].quant; |
quant = h->mbs[(j+0)/2*h->mb_stride + (i/2)].quant; |
111 |
deblock8x8_h(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant); |
deblock8x8_v(h->tbls, h->img->y + j*8*stride + i*8, stride, quant, dering); |
112 |
deblock8x8_h(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant); |
} |
113 |
} |
} |
114 |
|
|
115 |
for (j = 0; j < mb_height; j++) /* vertical deblocking */ |
/* chroma */ |
116 |
for (i = 1; i < mb_width; i++) |
if ((h->flags & XVID_DEBLOCKUV)) |
117 |
|
{ |
118 |
|
int dering = h->flags & XVID_DERINGUV; |
119 |
|
|
120 |
|
for (j = h->start_y/2; j < h->stop_y/2; j++) /* vertical deblocking */ |
121 |
|
for (i = 1; i < h->stop_x/2; i++) |
122 |
{ |
{ |
123 |
quant = mbs[(j+0)*mb_stride + i].quant; |
quant = h->mbs[(j+0)*h->mb_stride + i].quant; |
124 |
deblock8x8_v(tbls, img->u + j*8*edged_width2 + i*8, edged_width2, quant); |
deblock8x8_v(h->tbls, h->img->u + j*8*stride2 + i*8, stride2, quant, dering); |
125 |
deblock8x8_v(tbls, img->v + j*8*edged_width2 + i*8, edged_width2, quant); |
deblock8x8_v(h->tbls, h->img->v + j*8*stride2 + i*8, stride2, quant, dering); |
126 |
|
} |
127 |
} |
} |
128 |
} |
} |
129 |
|
|
130 |
|
void |
131 |
|
image_postproc(XVID_POSTPROC *tbls, IMAGE * img, int edged_width, |
132 |
|
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
133 |
|
int flags, int brightness, int frame_num, int bvop, int threads) |
134 |
|
{ |
135 |
|
int k; |
136 |
|
#ifndef HAVE_PTHREAD |
137 |
|
int num_threads = 1; |
138 |
|
#else |
139 |
|
int num_threads = MAX(1, MIN(threads, 4)); |
140 |
|
void *status = NULL; |
141 |
|
#endif |
142 |
|
SMPDeblock data[4]; |
143 |
|
|
144 |
|
/* horizontal deblocking, dispatch threads */ |
145 |
|
for (k = 0; k < num_threads; k++) { |
146 |
|
data[k].flags = flags; |
147 |
|
data[k].img = img; |
148 |
|
data[k].mb_stride = mb_stride; |
149 |
|
data[k].mbs = mbs; |
150 |
|
data[k].stride = edged_width; |
151 |
|
data[k].tbls = tbls; |
152 |
|
|
153 |
|
data[k].start_x = (k*mb_width / num_threads)*2; |
154 |
|
data[k].stop_x = ((k+1)*mb_width / num_threads)*2; |
155 |
|
|
156 |
|
data[k].stop_y = mb_height*2; |
157 |
|
} |
158 |
|
#ifdef HAVE_PTHREAD |
159 |
|
/* create threads */ |
160 |
|
for (k = 1; k < num_threads; k++) { |
161 |
|
pthread_create(&data[k].handle, NULL, |
162 |
|
(void*)stripe_deblock_h, (void*)&data[k]); |
163 |
|
} |
164 |
|
#endif |
165 |
|
stripe_deblock_h(&data[0]); |
166 |
|
|
167 |
|
#ifdef HAVE_PTHREAD |
168 |
|
/* wait until all threads are finished */ |
169 |
|
for (k = 1; k < num_threads; k++) { |
170 |
|
pthread_join(data[k].handle, &status); |
171 |
|
} |
172 |
|
#endif |
173 |
|
|
174 |
|
/* vertical deblocking, dispatch threads */ |
175 |
|
for (k = 0; k < num_threads; k++) { |
176 |
|
data[k].start_y = (k*mb_height / num_threads)*2; |
177 |
|
data[k].stop_y = ((k+1)*mb_height / num_threads)*2; |
178 |
|
data[k].stop_x = mb_width*2; |
179 |
|
} |
180 |
|
|
181 |
|
#ifdef HAVE_PTHREAD |
182 |
|
/* create threads */ |
183 |
|
for (k = 1; k < num_threads; k++) { |
184 |
|
pthread_create(&data[k].handle, NULL, |
185 |
|
(void*)stripe_deblock_v, (void*)&data[k]); |
186 |
|
} |
187 |
|
#endif |
188 |
|
stripe_deblock_v(&data[0]); |
189 |
|
|
190 |
|
#ifdef HAVE_PTHREAD |
191 |
|
/* wait until all threads are finished */ |
192 |
|
for (k = 1; k < num_threads; k++) { |
193 |
|
pthread_join(data[k].handle, &status); |
194 |
|
} |
195 |
|
#endif |
196 |
|
|
197 |
if (!bvop) |
if (!bvop) |
198 |
tbls->prev_quant = mbs->quant; |
tbls->prev_quant = mbs->quant; |
199 |
|
|
202 |
add_noise(tbls, img->y, img->y, edged_width, mb_width*16, |
add_noise(tbls, img->y, img->y, edged_width, mb_width*16, |
203 |
mb_height*16, frame_num % 3, tbls->prev_quant); |
mb_height*16, frame_num % 3, tbls->prev_quant); |
204 |
} |
} |
205 |
|
|
206 |
|
if (brightness != 0) { |
207 |
|
image_brightness(img->y, edged_width, mb_width*16, mb_height*16, brightness); |
208 |
|
} |
209 |
} |
} |
210 |
|
|
211 |
/******************************************************************************/ |
/******************************************************************************/ |
248 |
s[8] = *(v[8] = img + x*stride + 3); \ |
s[8] = *(v[8] = img + x*stride + 3); \ |
249 |
s[9] = *(v[9] = img + x*stride + 4); |
s[9] = *(v[9] = img + x*stride + 4); |
250 |
|
|
251 |
|
#define APPLY_DERING(x) \ |
252 |
|
*v[x] = (e[x] == 0) ? ( \ |
253 |
|
(e[x-1] == 0) ? ( \ |
254 |
|
(e[x+1] == 0) ? \ |
255 |
|
((s[x-1]+s[x]*2+s[x+1])>>2) \ |
256 |
|
: ((s[x-1]+s[x])>>1) ) \ |
257 |
|
: ((s[x]+s[x+1])>>1) ) \ |
258 |
|
: s[x]; |
259 |
|
|
260 |
#define APPLY_FILTER_CORE \ |
#define APPLY_FILTER_CORE \ |
261 |
/* First, decide whether to use default or DC-offset mode */ \ |
/* First, decide whether to use default or DC-offset mode */ \ |
262 |
\ |
\ |
291 |
*v[4] -= diff; \ |
*v[4] -= diff; \ |
292 |
*v[5] += diff; \ |
*v[5] += diff; \ |
293 |
} \ |
} \ |
294 |
|
if (dering) { \ |
295 |
|
e[0] = (tbls->xvid_abs_tbl[(s[0] - s[1]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
296 |
|
e[1] = (tbls->xvid_abs_tbl[(s[1] - s[2]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
297 |
|
e[2] = (tbls->xvid_abs_tbl[(s[2] - s[3]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
298 |
|
e[3] = (tbls->xvid_abs_tbl[(s[3] - s[4]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
299 |
|
e[4] = (tbls->xvid_abs_tbl[(s[4] - s[5]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
300 |
|
e[5] = (tbls->xvid_abs_tbl[(s[5] - s[6]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
301 |
|
e[6] = (tbls->xvid_abs_tbl[(s[6] - s[7]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
302 |
|
e[7] = (tbls->xvid_abs_tbl[(s[7] - s[8]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
303 |
|
e[8] = (tbls->xvid_abs_tbl[(s[8] - s[9]) + 255] > quant + DERING_STRENGTH) ? 1 : 0; \ |
304 |
|
\ |
305 |
|
e[1] |= e[0]; \ |
306 |
|
e[2] |= e[1]; \ |
307 |
|
e[3] |= e[2]; \ |
308 |
|
e[4] |= e[3]; \ |
309 |
|
e[5] |= e[4]; \ |
310 |
|
e[6] |= e[5]; \ |
311 |
|
e[7] |= e[6]; \ |
312 |
|
e[8] |= e[7]; \ |
313 |
|
e[9] = e[8]; \ |
314 |
|
\ |
315 |
|
APPLY_DERING(1) \ |
316 |
|
APPLY_DERING(2) \ |
317 |
|
APPLY_DERING(3) \ |
318 |
|
APPLY_DERING(4) \ |
319 |
|
APPLY_DERING(5) \ |
320 |
|
APPLY_DERING(6) \ |
321 |
|
APPLY_DERING(7) \ |
322 |
|
APPLY_DERING(8) \ |
323 |
|
} \ |
324 |
} \ |
} \ |
325 |
else { /* DC-offset mode */ \ |
else { /* DC-offset mode */ \ |
326 |
uint8_t p0, p9; \ |
uint8_t p0, p9; \ |
347 |
} \ |
} \ |
348 |
} |
} |
349 |
|
|
350 |
void deblock8x8_h(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant) |
void deblock8x8_h(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant, int dering) |
351 |
{ |
{ |
352 |
int eq_cnt; |
int eq_cnt; |
353 |
uint8_t *v[10]; |
uint8_t *v[10]; |
354 |
int32_t s[10]; |
int s[10]; |
355 |
|
int e[10]; |
356 |
|
|
357 |
LOAD_DATA_HOR(0) |
LOAD_DATA_HOR(0) |
358 |
APPLY_FILTER_CORE |
APPLY_FILTER_CORE |
380 |
} |
} |
381 |
|
|
382 |
|
|
383 |
void deblock8x8_v(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant) |
void deblock8x8_v(XVID_POSTPROC *tbls, uint8_t *img, int stride, int quant, int dering) |
384 |
{ |
{ |
385 |
int eq_cnt; |
int eq_cnt; |
386 |
uint8_t *v[10]; |
uint8_t *v[10]; |
387 |
int s[10]; |
int s[10]; |
388 |
|
int e[10]; |
389 |
|
|
390 |
LOAD_DATA_VER(0) |
LOAD_DATA_VER(0) |
391 |
APPLY_FILTER_CORE |
APPLY_FILTER_CORE |
513 |
src += stride; |
src += stride; |
514 |
} |
} |
515 |
} |
} |
516 |
|
|
517 |
|
|
518 |
|
void image_brightness_c(uint8_t *dst, int stride, int width, int height, int offset) |
519 |
|
{ |
520 |
|
int x,y; |
521 |
|
|
522 |
|
for(y = 0; y < height; y++) |
523 |
|
{ |
524 |
|
for(x = 0; x < width; x++) |
525 |
|
{ |
526 |
|
int p = dst[y*stride + x]; |
527 |
|
dst[y*stride + x] = CLIP( p + offset, 0, 255); |
528 |
|
} |
529 |
|
} |
530 |
|
} |