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 |
|
****************************************************************************/ |
23 |
|
|
24 |
#include <stdlib.h> |
#include <stdlib.h> |
25 |
#include <string.h> |
#include <string.h> |
26 |
#include <math.h> |
#include <math.h> |
30 |
#include "image.h" |
#include "image.h" |
31 |
#include "postprocessing.h" |
#include "postprocessing.h" |
32 |
|
|
33 |
|
/* Filtering thresholds */ |
34 |
|
|
35 |
|
#define THR1 2 |
36 |
|
#define THR2 6 |
37 |
|
|
38 |
|
/* Some useful (and fast) macros |
39 |
|
Note that the MIN/MAX macros assume signed shift - if your compiler |
40 |
|
doesn't do signed shifts, use the default MIN/MAX macros from global.h */ |
41 |
|
|
42 |
|
#define FAST_MAX(x,y) ((x) - ((((x) - (y))>>(32 - 1)) & ((x) - (y)))) |
43 |
|
#define FAST_MIN(x,y) ((x) + ((((y) - (x))>>(32 - 1)) & ((y) - (x)))) |
44 |
|
#define FAST_ABS(x) ((((int)(x)) >> 31) ^ ((int)(x))) - (((int)(x)) >> 31) |
45 |
|
#define ABS(X) (((X)>0)?(X):-(X)) |
46 |
|
|
47 |
|
static int8_t xvid_thresh_tbl[510]; |
48 |
|
static int8_t xvid_abs_tbl[510]; |
49 |
|
|
50 |
|
void init_postproc(void) |
51 |
|
{ |
52 |
|
int i; |
53 |
|
|
54 |
|
for(i = -255; i < 256; i++) { |
55 |
|
xvid_thresh_tbl[i + 255] = 0; |
56 |
|
if(ABS(i) < THR1) |
57 |
|
xvid_thresh_tbl[i + 255] = 1; |
58 |
|
xvid_abs_tbl[i + 255] = ABS(i); |
59 |
|
} |
60 |
|
} |
61 |
|
|
62 |
void |
void |
63 |
image_deblock(IMAGE * img, int edged_width, |
image_deblock(IMAGE * img, int edged_width, |
64 |
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
69 |
int quant; |
int quant; |
70 |
|
|
71 |
/* luma: j,i in block units */ |
/* luma: j,i in block units */ |
72 |
if ((flags & XVID_DEC_DEBLOCKY)) |
if ((flags & XVID_DEBLOCKY)) |
73 |
{ |
{ |
74 |
for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */ |
for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */ |
75 |
for (i = 0; i < mb_width*2; i++) |
for (i = 0; i < mb_width*2; i++) |
88 |
|
|
89 |
|
|
90 |
/* chroma */ |
/* chroma */ |
91 |
if ((flags & XVID_DEC_DEBLOCKUV)) |
if ((flags & XVID_DEBLOCKUV)) |
92 |
{ |
{ |
93 |
for (j = 1; j < mb_height; j++) /* horizontal deblocking */ |
for (j = 1; j < mb_height; j++) /* horizontal deblocking */ |
94 |
for (i = 0; i < mb_width; i++) |
for (i = 0; i < mb_width; i++) |
108 |
} |
} |
109 |
} |
} |
110 |
|
|
111 |
#define THR1 2 |
#define LOAD_DATA_HOR(x) \ |
112 |
#define THR2 6 |
/* Load pixel addresses and data for filtering */ \ |
113 |
|
s[0] = *(v[0] = img - 5*stride + x); \ |
114 |
|
s[1] = *(v[1] = img - 4*stride + x); \ |
115 |
|
s[2] = *(v[2] = img - 3*stride + x); \ |
116 |
|
s[3] = *(v[3] = img - 2*stride + x); \ |
117 |
|
s[4] = *(v[4] = img - 1*stride + x); \ |
118 |
|
s[5] = *(v[5] = img + 0*stride + x); \ |
119 |
|
s[6] = *(v[6] = img + 1*stride + x); \ |
120 |
|
s[7] = *(v[7] = img + 2*stride + x); \ |
121 |
|
s[8] = *(v[8] = img + 3*stride + x); \ |
122 |
|
s[9] = *(v[9] = img + 4*stride + x); |
123 |
|
|
124 |
|
#define LOAD_DATA_VER(x) \ |
125 |
|
/* Load pixel addresses and data for filtering */ \ |
126 |
|
s[0] = *(v[0] = img + x*stride - 5); \ |
127 |
|
s[1] = *(v[1] = img + x*stride - 4); \ |
128 |
|
s[2] = *(v[2] = img + x*stride - 3); \ |
129 |
|
s[3] = *(v[3] = img + x*stride - 2); \ |
130 |
|
s[4] = *(v[4] = img + x*stride - 1); \ |
131 |
|
s[5] = *(v[5] = img + x*stride + 0); \ |
132 |
|
s[6] = *(v[6] = img + x*stride + 1); \ |
133 |
|
s[7] = *(v[7] = img + x*stride + 2); \ |
134 |
|
s[8] = *(v[8] = img + x*stride + 3); \ |
135 |
|
s[9] = *(v[9] = img + x*stride + 4); |
136 |
|
|
137 |
|
#define APPLY_FILTER_CORE \ |
138 |
|
/* First, decide whether to use default or DC-offset mode */ \ |
139 |
|
\ |
140 |
|
eq_cnt = 0; \ |
141 |
|
\ |
142 |
|
eq_cnt += xvid_thresh_tbl[s[0] - s[1] + 255]; \ |
143 |
|
eq_cnt += xvid_thresh_tbl[s[1] - s[2] + 255]; \ |
144 |
|
eq_cnt += xvid_thresh_tbl[s[2] - s[3] + 255]; \ |
145 |
|
eq_cnt += xvid_thresh_tbl[s[3] - s[4] + 255]; \ |
146 |
|
eq_cnt += xvid_thresh_tbl[s[4] - s[5] + 255]; \ |
147 |
|
eq_cnt += xvid_thresh_tbl[s[5] - s[6] + 255]; \ |
148 |
|
eq_cnt += xvid_thresh_tbl[s[6] - s[7] + 255]; \ |
149 |
|
eq_cnt += xvid_thresh_tbl[s[7] - s[8] + 255]; \ |
150 |
|
\ |
151 |
|
if(eq_cnt < THR2) { /* Default mode */ \ |
152 |
|
int a30, a31, a32; \ |
153 |
|
int diff, limit; \ |
154 |
|
\ |
155 |
|
a30 = ((s[3]<<1) - s[4] * 5 + s[5] * 5 - (s[6]<<1)); \ |
156 |
|
\ |
157 |
|
if(xvid_abs_tbl[a30 + 255] < 8*quant) { \ |
158 |
|
a31 = ((s[1]<<1) - s[2] * 5 + s[3] * 5 - (s[4]<<1)); \ |
159 |
|
a32 = ((s[5]<<1) - s[6] * 5 + s[7] * 5 - (s[8]<<1)); \ |
160 |
|
\ |
161 |
|
diff = (5 * ((SIGN(a30) * MIN(xvid_abs_tbl[a30 + 255], MIN(xvid_abs_tbl[a31 + 255], xvid_abs_tbl[a32 + 255]))) - a30) + 32) >> 6; \ |
162 |
|
limit = (s[4] - s[5]) / 2; \ |
163 |
|
\ |
164 |
|
if (limit > 0) \ |
165 |
|
diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff); \ |
166 |
|
else \ |
167 |
|
diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff); \ |
168 |
|
\ |
169 |
|
*v[4] -= diff; \ |
170 |
|
*v[5] += diff; \ |
171 |
|
} \ |
172 |
|
} \ |
173 |
|
else { /* DC-offset mode */ \ |
174 |
|
uint8_t p0, p9; \ |
175 |
|
int min, max; \ |
176 |
|
\ |
177 |
|
/* Now decide whether to apply smoothing filter or not */ \ |
178 |
|
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]))))))); \ |
179 |
|
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]))))))); \ |
180 |
|
\ |
181 |
|
if(((max-min)) < 2*quant) { \ |
182 |
|
\ |
183 |
|
/* Choose edge pixels */ \ |
184 |
|
p0 = (xvid_abs_tbl[(s[1] - s[0]) + 255] < quant) ? s[0] : s[1]; \ |
185 |
|
p9 = (xvid_abs_tbl[(s[8] - s[9]) + 255] < quant) ? s[9] : s[8]; \ |
186 |
|
\ |
187 |
|
*v[1] = (uint8_t) ((6*p0 + (s[1]<<2) + (s[2]<<1) + (s[3]<<1) + s[4] + s[5] + 8) >> 4); \ |
188 |
|
*v[2] = (uint8_t) (((p0<<2) + (s[1]<<1) + (s[2]<<2) + (s[3]<<1) + (s[4]<<1) + s[5] + s[6] + 8) >> 4); \ |
189 |
|
*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); \ |
190 |
|
*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); \ |
191 |
|
*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); \ |
192 |
|
*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); \ |
193 |
|
*v[7] = (uint8_t) ((s[3] + s[4] + (s[5]<<1) + (s[6]<<1) + (s[7]<<2) + (s[8]<<1) + (p9<<2) + 8) >> 4); \ |
194 |
|
*v[8] = (uint8_t) ((s[4] + s[5] + (s[6]<<1) + (s[7]<<1) + (s[8]<<2) + 6*p9 + 8) >> 4); \ |
195 |
|
} \ |
196 |
|
} |
197 |
|
|
198 |
void deblock8x8_h(uint8_t *img, int stride, int quant) |
void deblock8x8_h(uint8_t *img, int stride, int quant) |
199 |
{ |
{ |
|
int i, j; |
|
200 |
int eq_cnt; |
int eq_cnt; |
201 |
uint8_t *v[10], p0, p9; |
uint8_t *v[10]; |
202 |
int min, max; |
int32_t s[10]; |
|
int a30, a31, a32; |
|
|
int diff, limit; |
|
|
|
|
|
for(j = 0; j < 8; j++) { |
|
|
eq_cnt = 0; |
|
|
|
|
|
/* Load pixel addresses for filtering */ |
|
|
|
|
|
for(i = 0; i < 10; i++) |
|
|
v[i] = img + (i-5)*stride + j; |
|
|
|
|
|
/* First, decide whether to use default or DC-offset mode */ |
|
|
|
|
|
for(i = 0; i < 9; i++) |
|
|
{ |
|
|
if(ABS(*v[i] - *v[(i+1)]) < THR1) |
|
|
eq_cnt++; |
|
|
} |
|
|
|
|
|
if(eq_cnt < THR2) { /* Default mode */ |
|
|
|
|
|
a30 = (*v[3] * 2 - *v[4] * 5 + *v[5] * 5 - *v[6] * 2); |
|
203 |
|
|
204 |
if(ABS(a30) < 8*quant) { |
LOAD_DATA_HOR(0) |
205 |
a31 = (*v[1] * 2 - *v[2] * 5 + *v[3] * 5 - *v[4] * 2); |
APPLY_FILTER_CORE |
|
a32 = (*v[5] * 2 - *v[6] * 5 + *v[7] * 5 - *v[8] * 2); |
|
206 |
|
|
207 |
diff = (5 * ((SIGN(a30) * MIN(ABS(a30), MIN(ABS(a31), ABS(a32)))) - a30) + 32) >> 6; |
LOAD_DATA_HOR(1) |
208 |
limit = (*v[4] - *v[5]) / 2; |
APPLY_FILTER_CORE |
209 |
|
|
210 |
if (limit > 0) |
LOAD_DATA_HOR(2) |
211 |
diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff); |
APPLY_FILTER_CORE |
|
else |
|
|
diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff); |
|
212 |
|
|
213 |
*v[4] -= diff; |
LOAD_DATA_HOR(3) |
214 |
*v[5] += diff; |
APPLY_FILTER_CORE |
|
} |
|
|
} |
|
|
else { /* DC-offset mode */ |
|
215 |
|
|
216 |
/* Now decide whether to apply smoothing filter or not */ |
LOAD_DATA_HOR(4) |
217 |
max = MAX(*v[1], MAX(*v[2], MAX(*v[3], MAX(*v[4], MAX(*v[5], MAX(*v[6], MAX(*v[7], *v[8]))))))); |
APPLY_FILTER_CORE |
|
min = MIN(*v[1], MIN(*v[2], MIN(*v[3], MIN(*v[4], MIN(*v[5], MIN(*v[6], MIN(*v[7], *v[8]))))))); |
|
218 |
|
|
219 |
if((max-min) < 2*quant) { |
LOAD_DATA_HOR(5) |
220 |
|
APPLY_FILTER_CORE |
221 |
|
|
222 |
/* Choose edge pixels */ |
LOAD_DATA_HOR(6) |
223 |
p0 = (ABS(*v[1]-*v[0]) < quant) ? *v[0] : *v[1]; |
APPLY_FILTER_CORE |
|
p9 = (ABS(*v[8]-*v[9]) < quant) ? *v[9] : *v[8]; |
|
224 |
|
|
225 |
*v[1] = (6*p0 + (*v[1]<<2) + (*v[2]<<1) + (*v[3]<<1) + *v[4] + *v[5] + 8) >> 4; |
LOAD_DATA_HOR(7) |
226 |
*v[2] = ((p0<<2) + (*v[1]<<1) + (*v[2]<<2) + (*v[3]<<1) + (*v[4]<<1) + *v[5] + *v[6] + 8) >> 4; |
APPLY_FILTER_CORE |
|
*v[3] = ((p0<<1) + (*v[1]<<1) + (*v[2]<<1) + (*v[3]<<2) + (*v[4]<<1) + (*v[5]<<1) + *v[6] + *v[7] + 8) >> 4; |
|
|
*v[4] = (p0 + *v[1] + (*v[2]<<1) + (*v[3]<<1) + (*v[4]<<2) + (*v[5]<<1) + (*v[6]<<1) + *v[7] + *v[8] + 8) >> 4; |
|
|
*v[5] = (*v[1] + *v[2] + (*v[3]<<1) + (*v[4]<<1) + (*v[5]<<2) + (*v[6]<<1) + (*v[7]<<1) + *v[8] + p9 + 8) >> 4; |
|
|
*v[6] = (*v[2] + *v[3] + (*v[4]<<1) + (*v[5]<<1) + (*v[6]<<2) + (*v[7]<<1) + (*v[8]<<1) + (p9<<1) + 8) >> 4; |
|
|
*v[7] = (*v[3] + *v[4] + (*v[5]<<1) + (*v[6]<<1) + (*v[7]<<2) + (*v[8]<<1) + (p9<<2) + 8) >> 4; |
|
|
*v[8] = (*v[4] + *v[5] + (*v[6]<<1) + (*v[7]<<1) + (*v[8]<<2) + 6*p9 + 8) >> 4; |
|
|
} |
|
227 |
} |
} |
|
} |
|
|
} |
|
|
|
|
228 |
|
|
229 |
|
|
230 |
void deblock8x8_v(uint8_t *img, int stride, int quant) |
void deblock8x8_v(uint8_t *img, int stride, int quant) |
231 |
{ |
{ |
|
int i, j; |
|
232 |
int eq_cnt; |
int eq_cnt; |
233 |
uint8_t *v[10], p0, p9; |
uint8_t *v[10]; |
234 |
int min, max; |
int s[10]; |
|
int a30, a31, a32; |
|
|
int diff, limit; |
|
|
|
|
|
for(j = 0; j < 8; j++) { |
|
|
eq_cnt = 0; |
|
|
|
|
|
/* Load pixel addresses for filtering */ |
|
235 |
|
|
236 |
for(i = 0; i < 10; i++) |
LOAD_DATA_VER(0) |
237 |
v[i] = img + j*stride + (i-5); |
APPLY_FILTER_CORE |
238 |
|
|
239 |
/* First, decide whether to use default or DC-offset mode */ |
LOAD_DATA_VER(1) |
240 |
|
APPLY_FILTER_CORE |
241 |
|
|
242 |
for(i = 0; i < 9; i++) |
LOAD_DATA_VER(2) |
243 |
{ |
APPLY_FILTER_CORE |
|
if(ABS(*v[i] - *v[i+1]) < THR1) |
|
|
eq_cnt++; |
|
|
} |
|
244 |
|
|
245 |
if(eq_cnt < THR2) { /* Default mode */ |
LOAD_DATA_VER(3) |
246 |
|
APPLY_FILTER_CORE |
247 |
|
|
248 |
a30 = (*v[3] * 2 - *v[4] * 5 + *v[5] * 5 - *v[6] * 2); |
LOAD_DATA_VER(4) |
249 |
|
APPLY_FILTER_CORE |
250 |
|
|
251 |
if(ABS(a30) < 8*quant) { |
LOAD_DATA_VER(5) |
252 |
a31 = (*v[1] * 2 - *v[2] * 5 + *v[3] * 5 - *v[4] * 2); |
APPLY_FILTER_CORE |
|
a32 = (*v[5] * 2 - *v[6] * 5 + *v[7] * 5 - *v[8] * 2); |
|
253 |
|
|
254 |
diff = (5 * ((SIGN(a30) * MIN(ABS(a30), MIN(ABS(a31), ABS(a32)))) - a30) + 32) >> 6; |
LOAD_DATA_VER(6) |
255 |
limit = (*v[4] - *v[5]) / 2; |
APPLY_FILTER_CORE |
256 |
|
|
257 |
if (limit > 0) |
LOAD_DATA_VER(7) |
258 |
diff = (diff < 0) ? 0 : ((diff > limit) ? limit : diff); |
APPLY_FILTER_CORE |
|
else |
|
|
diff = (diff > 0) ? 0 : ((diff < limit) ? limit : diff); |
|
|
|
|
|
*v[4] -= diff; |
|
|
*v[5] += diff; |
|
|
} |
|
|
} |
|
|
else { /* DC-offset mode */ |
|
|
|
|
|
/* Now decide whether to apply smoothing filter or not */ |
|
|
max = MAX(*v[1], MAX(*v[2], MAX(*v[3], MAX(*v[4], MAX(*v[5], MAX(*v[6], MAX(*v[7], *v[8]))))))); |
|
|
min = MIN(*v[1], MIN(*v[2], MIN(*v[3], MIN(*v[4], MIN(*v[5], MIN(*v[6], MIN(*v[7], *v[8]))))))); |
|
|
|
|
|
if((max-min) < 2*quant) { |
|
|
|
|
|
/* Choose edge pixels */ |
|
|
p0 = (ABS(*v[1]-*v[0]) < quant) ? *v[0] : *v[1]; |
|
|
p9 = (ABS(*v[8]-*v[9]) < quant) ? *v[9] : *v[8]; |
|
|
|
|
|
*v[1] = (6*p0 + (*v[1]<<2) + (*v[2]<<1) + (*v[3]<<1) + *v[4] + *v[5] + 8) >> 4; |
|
|
*v[2] = ((p0<<2) + (*v[1]<<1) + (*v[2]<<2) + (*v[3]<<1) + (*v[4]<<1) + *v[5] + *v[6] + 8) >> 4; |
|
|
*v[3] = ((p0<<1) + (*v[1]<<1) + (*v[2]<<1) + (*v[3]<<2) + (*v[4]<<1) + (*v[5]<<1) + *v[6] + *v[7] + 8) >> 4; |
|
|
*v[4] = (p0 + *v[1] + (*v[2]<<1) + (*v[3]<<1) + (*v[4]<<2) + (*v[5]<<1) + (*v[6]<<1) + *v[7] + *v[8] + 8) >> 4; |
|
|
*v[5] = (*v[1] + *v[2] + (*v[3]<<1) + (*v[4]<<1) + (*v[5]<<2) + (*v[6]<<1) + (*v[7]<<1) + *v[8] + p9 + 8) >> 4; |
|
|
*v[6] = (*v[2] + *v[3] + (*v[4]<<1) + (*v[5]<<1) + (*v[6]<<2) + (*v[7]<<1) + (*v[8]<<1) + (p9<<1) + 8) >> 4; |
|
|
*v[7] = (*v[3] + *v[4] + (*v[5]<<1) + (*v[6]<<1) + (*v[7]<<2) + (*v[8]<<1) + (p9<<2) + 8) >> 4; |
|
|
*v[8] = (*v[4] + *v[5] + (*v[6]<<1) + (*v[7]<<1) + (*v[8]<<2) + 6*p9 + 8) >> 4; |
|
|
} |
|
|
} |
|
|
} |
|
259 |
} |
} |