1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* Reduced-Resolution utilities |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Pascal Massimino <skal@planet-d.net> |
7 |
|
* |
8 |
|
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
9 |
|
* |
10 |
|
* XviD is free software; you can redistribute it and/or modify it |
11 |
|
* under the terms of the GNU General Public License as published by |
12 |
|
* the Free Software Foundation; either version 2 of the License, or |
13 |
|
* (at your option) any later version. |
14 |
|
* |
15 |
|
* This program is distributed in the hope that it will be useful, |
16 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
* GNU General Public License for more details. |
19 |
|
* |
20 |
|
* You should have received a copy of the GNU General Public License |
21 |
|
* along with this program; if not, write to the Free Software |
22 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
|
* |
24 |
|
* Under section 8 of the GNU General Public License, the copyright |
25 |
|
* holders of XVID explicitly forbid distribution in the following |
26 |
|
* countries: |
27 |
|
* |
28 |
|
* - Japan |
29 |
|
* - United States of America |
30 |
|
* |
31 |
|
* Linking XviD statically or dynamically with other modules is making a |
32 |
|
* combined work based on XviD. Thus, the terms and conditions of the |
33 |
|
* GNU General Public License cover the whole combination. |
34 |
|
* |
35 |
|
* As a special exception, the copyright holders of XviD give you |
36 |
|
* permission to link XviD with independent modules that communicate with |
37 |
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
38 |
|
* license terms of these independent modules, and to copy and distribute |
39 |
|
* the resulting combined work under terms of your choice, provided that |
40 |
|
* every copy of the combined work is accompanied by a complete copy of |
41 |
|
* the source code of XviD (the version of XviD used to produce the |
42 |
|
* combined work), being distributed under the terms of the GNU General |
43 |
|
* Public License plus this exception. An independent module is a module |
44 |
|
* which is not derived from or based on XviD. |
45 |
|
* |
46 |
|
* Note that people who make modified versions of XviD are not obligated |
47 |
|
* to grant this special exception for their modified versions; it is |
48 |
|
* their choice whether to do so. The GNU General Public License gives |
49 |
|
* permission to release a modified version without this exception; this |
50 |
|
* exception also makes it possible to release a modified version which |
51 |
|
* carries forward this exception. |
52 |
|
* |
53 |
|
* $Id$ |
54 |
|
* |
55 |
|
****************************************************************************/ |
56 |
|
|
57 |
|
#include "../portab.h" |
58 |
|
#include "reduced.h" |
59 |
|
|
60 |
|
// function pointers |
61 |
|
COPY_UPSAMPLED_8X8_16TO8 * copy_upsampled_8x8_16to8; |
62 |
|
ADD_UPSAMPLED_8X8_16TO8 * add_upsampled_8x8_16to8; |
63 |
|
VFILTER_31 * vfilter_31; |
64 |
|
HFILTER_31 * hfilter_31; |
65 |
|
FILTER_18X18_TO_8X8 * filter_18x18_to_8x8; |
66 |
|
FILTER_DIFF_18X18_TO_8X8 * filter_diff_18x18_to_8x8; |
67 |
|
|
68 |
|
////////////////////////////////////////////////////////// |
69 |
|
// Upsampling (1/3/3/1) filter |
70 |
|
|
71 |
|
#define CLIP(x) ((x)<0 ? 0 : (x)>255 ? 255 : (x)) |
72 |
|
#define ADD(dst,src) (dst) = CLIP((dst)+(src)) |
73 |
|
|
74 |
|
static __inline void Filter_31(uint8_t *Dst1, uint8_t *Dst2, |
75 |
|
const int16_t *Src1, const int16_t *Src2) |
76 |
|
{ |
77 |
|
/* Src[] is assumed to be >=0. So we can use ">>2" instead of "/2" */ |
78 |
|
int16_t a = (3*Src1[0]+ Src2[0]+2) >> 2; |
79 |
|
int16_t b = ( Src1[0]+3*Src2[0]+2) >> 2; |
80 |
|
Dst1[0] = CLIP(a); |
81 |
|
Dst2[0] = CLIP(b); |
82 |
|
} |
83 |
|
|
84 |
|
static __inline void Filter_9331(uint8_t *Dst1, uint8_t *Dst2, |
85 |
|
const int16_t *Src1, const int16_t *Src2) |
86 |
|
{ |
87 |
|
/* Src[] is assumed to be >=0. So we can use ">>4" instead of "/16" */ |
88 |
|
int16_t a = (9*Src1[0]+ 3*Src1[1]+ 3*Src2[0] + 1*Src2[1] + 8) >> 4; |
89 |
|
int16_t b = (3*Src1[0]+ 9*Src1[1]+ 1*Src2[0] + 3*Src2[1] + 8) >> 4; |
90 |
|
int16_t c = (3*Src1[0]+ 1*Src1[1]+ 9*Src2[0] + 3*Src2[1] + 8) >> 4; |
91 |
|
int16_t d = (1*Src1[0]+ 3*Src1[1]+ 3*Src2[0] + 9*Src2[1] + 8) >> 4; |
92 |
|
Dst1[0] = CLIP(a); |
93 |
|
Dst1[1] = CLIP(b); |
94 |
|
Dst2[0] = CLIP(c); |
95 |
|
Dst2[1] = CLIP(d); |
96 |
|
} |
97 |
|
|
98 |
|
void xvid_Copy_Upsampled_8x8_16To8_C(uint8_t *Dst, const int16_t *Src, const int BpS) |
99 |
|
{ |
100 |
|
int x, y; |
101 |
|
|
102 |
|
Dst[0] = CLIP(Src[0]); |
103 |
|
for(x=0; x<7; ++x) Filter_31(Dst+2*x+1, Dst+2*x+2, Src+x, Src+x+1); |
104 |
|
Dst[15] = CLIP(Src[7]); |
105 |
|
Dst += BpS; |
106 |
|
for(y=0; y<7; ++y) { |
107 |
|
uint8_t *const Dst2 = Dst + BpS; |
108 |
|
Filter_31(Dst, Dst2, Src, Src+8); |
109 |
|
for(x=0; x<7; ++x) |
110 |
|
Filter_9331(Dst+2*x+1, Dst2+2*x+1, Src+x, Src+x+8); |
111 |
|
Filter_31(Dst+15, Dst2+15, Src+7, Src+7+8); |
112 |
|
Src += 8; |
113 |
|
Dst += 2*BpS; |
114 |
|
} |
115 |
|
Dst[0] = CLIP(Src[0]); |
116 |
|
for(x=0; x<7; ++x) Filter_31(Dst+2*x+1, Dst+2*x+2, Src+x, Src+x+1); |
117 |
|
Dst[15] = CLIP(Src[7]); |
118 |
|
} |
119 |
|
|
120 |
|
static __inline void Filter_Add_31(uint8_t *Dst1, uint8_t *Dst2, |
121 |
|
const int16_t *Src1, const int16_t *Src2) |
122 |
|
{ |
123 |
|
/* Here, we must use "/4", since Src[] is in [-256, 255] */ |
124 |
|
int16_t a = (3*Src1[0]+ Src2[0] + 2) / 4; |
125 |
|
int16_t b = ( Src1[0]+3*Src2[0] + 2) / 4; |
126 |
|
ADD(Dst1[0], a); |
127 |
|
ADD(Dst2[0], b); |
128 |
|
} |
129 |
|
|
130 |
|
static __inline void Filter_Add_9331(uint8_t *Dst1, uint8_t *Dst2, |
131 |
|
const int16_t *Src1, const int16_t *Src2) |
132 |
|
{ |
133 |
|
int16_t a = (9*Src1[0]+ 3*Src1[1]+ 3*Src2[0] + 1*Src2[1] + 8) / 16; |
134 |
|
int16_t b = (3*Src1[0]+ 9*Src1[1]+ 1*Src2[0] + 3*Src2[1] + 8) / 16; |
135 |
|
int16_t c = (3*Src1[0]+ 1*Src1[1]+ 9*Src2[0] + 3*Src2[1] + 8) / 16; |
136 |
|
int16_t d = (1*Src1[0]+ 3*Src1[1]+ 3*Src2[0] + 9*Src2[1] + 8) / 16; |
137 |
|
ADD(Dst1[0], a); |
138 |
|
ADD(Dst1[1], b); |
139 |
|
ADD(Dst2[0], c); |
140 |
|
ADD(Dst2[1], d); |
141 |
|
} |
142 |
|
|
143 |
|
void xvid_Add_Upsampled_8x8_16To8_C(uint8_t *Dst, const int16_t *Src, const int BpS) |
144 |
|
{ |
145 |
|
int x, y; |
146 |
|
|
147 |
|
ADD(Dst[0], Src[0]); |
148 |
|
for(x=0; x<7; ++x) Filter_Add_31(Dst+2*x+1, Dst+2*x+2, Src+x, Src+x+1); |
149 |
|
ADD(Dst[15], Src[7]); |
150 |
|
Dst += BpS; |
151 |
|
for(y=0; y<7; ++y) { |
152 |
|
uint8_t *const Dst2 = Dst + BpS; |
153 |
|
Filter_Add_31(Dst, Dst2, Src, Src+8); |
154 |
|
for(x=0; x<7; ++x) |
155 |
|
Filter_Add_9331(Dst+2*x+1, Dst2+2*x+1, Src+x, Src+x+8); |
156 |
|
Filter_Add_31(Dst+15, Dst2+15, Src+7, Src+7+8); |
157 |
|
Src += 8; |
158 |
|
Dst += 2*BpS; |
159 |
|
} |
160 |
|
ADD(Dst[0], Src[0]); |
161 |
|
for(x=0; x<7; ++x) Filter_Add_31(Dst+2*x+1, Dst+2*x+2, Src+x, Src+x+1); |
162 |
|
ADD(Dst[15], Src[7]); |
163 |
|
} |
164 |
|
#undef CLIP |
165 |
|
#undef ADD |
166 |
|
|
167 |
|
////////////////////////////////////////////////////////// |
168 |
|
// horizontal and vertical deblocking |
169 |
|
|
170 |
|
void xvid_HFilter_31_C(uint8_t *Src1, uint8_t *Src2, int Nb_Blks) |
171 |
|
{ |
172 |
|
Nb_Blks *= 8; |
173 |
|
while(Nb_Blks-->0) { |
174 |
|
uint8_t a = ( 3*Src1[0] + 1*Src2[0] + 2 ) >> 2; |
175 |
|
uint8_t b = ( 1*Src1[0] + 3*Src2[0] + 2 ) >> 2; |
176 |
|
*Src1++ = a; |
177 |
|
*Src2++ = b; |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
void xvid_VFilter_31_C(uint8_t *Src1, uint8_t *Src2, const int BpS, int Nb_Blks) |
182 |
|
{ |
183 |
|
Nb_Blks *= 8; |
184 |
|
while(Nb_Blks-->0) { |
185 |
|
uint8_t a = ( 3*Src1[0] + 1*Src2[0] + 2 ) >> 2; |
186 |
|
uint8_t b = ( 1*Src1[0] + 3*Src2[0] + 2 ) >> 2; |
187 |
|
*Src1 = a; |
188 |
|
*Src2 = b; |
189 |
|
Src1 += BpS; |
190 |
|
Src2 += BpS; |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
////////////////////////////////////////////////////////// |
195 |
|
// 16x16 -> 8x8 (1/3/3/1) downsampling |
196 |
|
// |
197 |
|
// Warning! These read 1 pixel outside of the input 16x16 block! |
198 |
|
// |
199 |
|
////////////////////////////////////////////////////////// |
200 |
|
|
201 |
|
void xvid_Filter_18x18_To_8x8_C(int16_t *Dst, const uint8_t *Src, const int BpS) |
202 |
|
{ |
203 |
|
int16_t *T, Tmp[18*8]; |
204 |
|
int i, j; |
205 |
|
|
206 |
|
T = Tmp; |
207 |
|
Src -= BpS; |
208 |
|
for(j=-1; j<17; j++) { |
209 |
|
for(i=0; i<8; ++i) |
210 |
|
T[i] = Src[2*i-1] + 3*Src[2*i+0] + 3*Src[2*i+1] + Src[2*i+2]; |
211 |
|
T += 8; |
212 |
|
Src += BpS; |
213 |
|
} |
214 |
|
T = Tmp + 8; |
215 |
|
for(j=0; j<8; j++) { |
216 |
|
for(i=0; i<8; ++i) |
217 |
|
Dst[i] = ( T[-8+i] + 3*T[0+i] + 3*T[8+i] + T[16+i] + 32 ) / 64; |
218 |
|
Dst += 8; |
219 |
|
T += 16; |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
|
void xvid_Filter_Diff_18x18_To_8x8_C(int16_t *Dst, const uint8_t *Src, const int BpS) |
224 |
|
{ |
225 |
|
int16_t *T, Tmp[18*8]; |
226 |
|
int i, j; |
227 |
|
|
228 |
|
T = Tmp; |
229 |
|
Src -= BpS; |
230 |
|
for(j=-1; j<17; j++) { |
231 |
|
for(i=0; i<8; ++i) |
232 |
|
T[i] = Src[2*i-1] + 3*Src[2*i+0] + 3*Src[2*i+1] + Src[2*i+2]; |
233 |
|
T += 8; |
234 |
|
Src += BpS; |
235 |
|
} |
236 |
|
T = Tmp; |
237 |
|
for(j=0; j<8; j++) { |
238 |
|
for(i=0; i<8; ++i) |
239 |
|
Dst[i] -= ( T[i] + 3*T[8+i] + 3*T[16+i] + T[24+i] + 32 ) / 64; |
240 |
|
Dst += 8; |
241 |
|
T += 16; |
242 |
|
} |
243 |
|
} |
244 |
|
|
245 |
|
////////////////////////////////////////////////////////// |