7919
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4 #include <inttypes.h>
|
|
5
|
|
6 #include "../config.h"
|
|
7 #include "../mp_msg.h"
|
|
8
|
|
9 #include "img_format.h"
|
|
10 #include "mp_image.h"
|
|
11 #include "vf.h"
|
|
12
|
|
13 //===========================================================================//
|
|
14
|
|
15 #define uint32 unsigned long
|
|
16 #define uint16 unsigned short
|
|
17 #define uint8 unsigned char
|
|
18
|
|
19 static uint32 colorMask = 0xF7DEF7DE;
|
|
20 static uint32 lowPixelMask = 0x08210821;
|
|
21 static uint32 qcolorMask = 0xE79CE79C;
|
|
22 static uint32 qlowpixelMask = 0x18631863;
|
|
23 static uint32 redblueMask = 0xF81F;
|
|
24 static uint32 greenMask = 0x7E0;
|
|
25 static int PixelsPerMask = 2;
|
|
26 static int xsai_depth = 0;
|
|
27
|
|
28 #define makecol(r,g,b) (r+(g<<8)+(b<<16))
|
|
29 #define makecol_depth(d,r,g,b) (r+(g<<8)+(b<<16))
|
|
30
|
|
31 int Init_2xSaI(int d)
|
|
32 {
|
|
33
|
|
34 int minr = 0, ming = 0, minb = 0;
|
|
35 int i;
|
|
36
|
|
37 // if (d != 15 && d != 16 && d != 24 && d != 32)
|
|
38 // return -1;
|
|
39
|
|
40 /* Get lowest color bit */
|
|
41 for (i = 0; i < 255; i++) {
|
|
42 if (!minr)
|
|
43 minr = makecol(i, 0, 0);
|
|
44 if (!ming)
|
|
45 ming = makecol(0, i, 0);
|
|
46 if (!minb)
|
|
47 minb = makecol(0, 0, i);
|
|
48 }
|
|
49
|
|
50 colorMask = (makecol_depth(d, 255, 0, 0) - minr) | (makecol_depth(d, 0, 255, 0) - ming) | (makecol_depth(d, 0, 0, 255) - minb);
|
|
51 lowPixelMask = minr | ming | minb;
|
|
52 qcolorMask = (makecol_depth(d, 255, 0, 0) - 3 * minr) | (makecol_depth(d, 0, 255, 0) - 3 * ming) | (makecol_depth(d, 0, 0, 255) - 3 * minb);
|
|
53 qlowpixelMask = (minr * 3) | (ming * 3) | (minb * 3);
|
|
54 redblueMask = makecol_depth(d, 255, 0, 255);
|
|
55 greenMask = makecol_depth(d, 0, 255, 0);
|
|
56
|
|
57 PixelsPerMask = (d <= 16) ? 2 : 1;
|
|
58
|
|
59 if (PixelsPerMask == 2) {
|
|
60 colorMask |= (colorMask << 16);
|
|
61 qcolorMask |= (qcolorMask << 16);
|
|
62 lowPixelMask |= (lowPixelMask << 16);
|
|
63 qlowpixelMask |= (qlowpixelMask << 16);
|
|
64 }
|
|
65
|
|
66 // TRACE("Color Mask: 0x%lX\n", colorMask);
|
|
67 // TRACE("Low Pixel Mask: 0x%lX\n", lowPixelMask);
|
|
68 // TRACE("QColor Mask: 0x%lX\n", qcolorMask);
|
|
69 // TRACE("QLow Pixel Mask: 0x%lX\n", qlowpixelMask);
|
|
70
|
|
71 xsai_depth = d;
|
|
72
|
|
73 return 0;
|
|
74 }
|
|
75
|
|
76
|
|
77 static int GetResult1(uint32 A, uint32 B, uint32 C, uint32 D)
|
|
78 {
|
|
79 int x = 0;
|
|
80 int y = 0;
|
|
81 int r = 0;
|
|
82 if (A == C)
|
|
83 x += 1;
|
|
84 else if (B == C)
|
|
85 y += 1;
|
|
86 if (A == D)
|
|
87 x += 1;
|
|
88 else if (B == D)
|
|
89 y += 1;
|
|
90 if (x <= 1)
|
|
91 r += 1;
|
|
92 if (y <= 1)
|
|
93 r -= 1;
|
|
94 return r;
|
|
95 }
|
|
96
|
|
97 static int GetResult2(uint32 A, uint32 B, uint32 C, uint32 D, uint32 E)
|
|
98 {
|
|
99 int x = 0;
|
|
100 int y = 0;
|
|
101 int r = 0;
|
|
102 if (A == C)
|
|
103 x += 1;
|
|
104 else if (B == C)
|
|
105 y += 1;
|
|
106 if (A == D)
|
|
107 x += 1;
|
|
108 else if (B == D)
|
|
109 y += 1;
|
|
110 if (x <= 1)
|
|
111 r -= 1;
|
|
112 if (y <= 1)
|
|
113 r += 1;
|
|
114 return r;
|
|
115 }
|
|
116
|
|
117
|
|
118 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
|
|
119
|
|
120 #define INTERPOLATE(A, B) (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + (A & B & lowPixelMask))
|
|
121
|
|
122 #define Q_INTERPOLATE(A, B, C, D) ((A & qcolorMask) >> 2) + ((B & qcolorMask) >> 2) + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2) \
|
|
123 + ((((A & qlowpixelMask) + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask)) >> 2) & qlowpixelMask)
|
|
124
|
|
125
|
|
126 static unsigned char *src_line[4];
|
|
127 static unsigned char *dst_line[2];
|
|
128
|
|
129 void Super2xSaI_ex(uint8 *src, uint32 src_pitch,
|
|
130 uint8 *dst, uint32 dst_pitch,
|
|
131 uint32 width, uint32 height, int sbpp) {
|
|
132
|
|
133 int j;
|
|
134 unsigned int x, y;
|
|
135 unsigned long color[16];
|
|
136
|
|
137 /* Point to the first 3 lines. */
|
|
138 src_line[0] = src;
|
|
139 src_line[1] = src;
|
|
140 src_line[2] = src + src_pitch;
|
|
141 src_line[3] = src + src_pitch * 2;
|
|
142
|
|
143 x = 0, y = 0;
|
|
144
|
|
145 if (PixelsPerMask == 2) {
|
|
146 unsigned short *sbp;
|
|
147 sbp = (unsigned short*)src_line[0];
|
|
148 color[0] = *sbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
|
|
149 color[4] = color[0]; color[5] = color[0]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
|
|
150 sbp = (unsigned short*)src_line[2];
|
|
151 color[8] = *sbp; color[9] = color[8]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
|
|
152 sbp = (unsigned short*)src_line[3];
|
|
153 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
|
|
154 }
|
|
155 else {
|
|
156 unsigned long *lbp;
|
|
157 lbp = (unsigned long*)src_line[0];
|
|
158 color[0] = *lbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
|
|
159 color[4] = color[0]; color[5] = color[0]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
|
|
160 lbp = (unsigned long*)src_line[2];
|
|
161 color[8] = *lbp; color[9] = color[8]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
|
|
162 lbp = (unsigned long*)src_line[3];
|
|
163 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
|
|
164 }
|
|
165
|
|
166 for (y = 0; y < height; y++) {
|
|
167
|
|
168 dst_line[0] = dst + dst_pitch*2*y;
|
|
169 dst_line[1] = dst + dst_pitch*(2*y+1);
|
|
170
|
|
171 /* Todo: x = width - 2, x = width - 1 */
|
|
172
|
|
173 for (x = 0; x < width; x++) {
|
|
174 unsigned long product1a, product1b, product2a, product2b;
|
|
175
|
|
176 //--------------------------------------- B0 B1 B2 B3 0 1 2 3
|
|
177 // 4 5* 6 S2 -> 4 5* 6 7
|
|
178 // 1 2 3 S1 8 9 10 11
|
|
179 // A0 A1 A2 A3 12 13 14 15
|
|
180 //--------------------------------------
|
|
181 if (color[9] == color[6] && color[5] != color[10]) {
|
|
182 product2b = color[9];
|
|
183 product1b = product2b;
|
|
184 }
|
|
185 else if (color[5] == color[10] && color[9] != color[6]) {
|
|
186 product2b = color[5];
|
|
187 product1b = product2b;
|
|
188 }
|
|
189 else if (color[5] == color[10] && color[9] == color[6]) {
|
|
190 int r = 0;
|
|
191
|
|
192 r += GET_RESULT(color[6], color[5], color[8], color[13]);
|
|
193 r += GET_RESULT(color[6], color[5], color[4], color[1]);
|
|
194 r += GET_RESULT(color[6], color[5], color[14], color[11]);
|
|
195 r += GET_RESULT(color[6], color[5], color[2], color[7]);
|
|
196
|
|
197 if (r > 0)
|
|
198 product1b = color[6];
|
|
199 else if (r < 0)
|
|
200 product1b = color[5];
|
|
201 else
|
|
202 product1b = INTERPOLATE(color[5], color[6]);
|
|
203
|
|
204 product2b = product1b;
|
|
205
|
|
206 }
|
|
207 else {
|
|
208 if (color[6] == color[10] && color[10] == color[13] && color[9] != color[14] && color[10] != color[12])
|
|
209 product2b = Q_INTERPOLATE(color[10], color[10], color[10], color[9]);
|
|
210 else if (color[5] == color[9] && color[9] == color[14] && color[13] != color[10] && color[9] != color[15])
|
|
211 product2b = Q_INTERPOLATE(color[9], color[9], color[9], color[10]);
|
|
212 else
|
|
213 product2b = INTERPOLATE(color[9], color[10]);
|
|
214
|
|
215 if (color[6] == color[10] && color[6] == color[1] && color[5] != color[2] && color[6] != color[0])
|
|
216 product1b = Q_INTERPOLATE(color[6], color[6], color[6], color[5]);
|
|
217 else if (color[5] == color[9] && color[5] == color[2] && color[1] != color[6] && color[5] != color[3])
|
|
218 product1b = Q_INTERPOLATE(color[6], color[5], color[5], color[5]);
|
|
219 else
|
|
220 product1b = INTERPOLATE(color[5], color[6]);
|
|
221 }
|
|
222
|
|
223 if (color[5] == color[10] && color[9] != color[6] && color[4] == color[5] && color[5] != color[14])
|
|
224 product2a = INTERPOLATE(color[9], color[5]);
|
|
225 else if (color[5] == color[8] && color[6] == color[5] && color[4] != color[9] && color[5] != color[12])
|
|
226 product2a = INTERPOLATE(color[9], color[5]);
|
|
227 else
|
|
228 product2a = color[9];
|
|
229
|
|
230 if (color[9] == color[6] && color[5] != color[10] && color[8] == color[9] && color[9] != color[2])
|
|
231 product1a = INTERPOLATE(color[9], color[5]);
|
|
232 else if (color[4] == color[9] && color[10] == color[9] && color[8] != color[5] && color[9] != color[0])
|
|
233 product1a = INTERPOLATE(color[9], color[5]);
|
|
234 else
|
|
235 product1a = color[5];
|
|
236
|
|
237 if (PixelsPerMask == 2) {
|
|
238 *((unsigned long *) (&dst_line[0][x * 4])) = product1a | (product1b << 16);
|
|
239 *((unsigned long *) (&dst_line[1][x * 4])) = product2a | (product2b << 16);
|
|
240 }
|
|
241 else {
|
|
242 *((unsigned long *) (&dst_line[0][x * 8])) = product1a;
|
|
243 *((unsigned long *) (&dst_line[0][x * 8 + 4])) = product1b;
|
|
244 *((unsigned long *) (&dst_line[1][x * 8])) = product2a;
|
|
245 *((unsigned long *) (&dst_line[1][x * 8 + 4])) = product2b;
|
|
246 }
|
|
247
|
|
248 /* Move color matrix forward */
|
|
249 color[0] = color[1]; color[4] = color[5]; color[8] = color[9]; color[12] = color[13];
|
|
250 color[1] = color[2]; color[5] = color[6]; color[9] = color[10]; color[13] = color[14];
|
|
251 color[2] = color[3]; color[6] = color[7]; color[10] = color[11]; color[14] = color[15];
|
|
252
|
|
253 if (x < width - 3) {
|
|
254 x += 3;
|
|
255 if (PixelsPerMask == 2) {
|
|
256 color[3] = *(((unsigned short*)src_line[0]) + x);
|
|
257 color[7] = *(((unsigned short*)src_line[1]) + x);
|
|
258 color[11] = *(((unsigned short*)src_line[2]) + x);
|
|
259 color[15] = *(((unsigned short*)src_line[3]) + x);
|
|
260 }
|
|
261 else {
|
|
262 color[3] = *(((unsigned long*)src_line[0]) + x);
|
|
263 color[7] = *(((unsigned long*)src_line[1]) + x);
|
|
264 color[11] = *(((unsigned long*)src_line[2]) + x);
|
|
265 color[15] = *(((unsigned long*)src_line[3]) + x);
|
|
266 }
|
|
267 x -= 3;
|
|
268 }
|
|
269 }
|
|
270
|
|
271 /* We're done with one line, so we shift the source lines up */
|
|
272 src_line[0] = src_line[1];
|
|
273 src_line[1] = src_line[2];
|
|
274 src_line[2] = src_line[3];
|
|
275
|
|
276 /* Read next line */
|
|
277 if (y + 3 >= height)
|
|
278 src_line[3] = src_line[2];
|
|
279 else
|
|
280 src_line[3] = src_line[2] + src_pitch;
|
|
281
|
|
282 /* Then shift the color matrix up */
|
|
283 if (PixelsPerMask == 2) {
|
|
284 unsigned short *sbp;
|
|
285 sbp = (unsigned short*)src_line[0];
|
|
286 color[0] = *sbp; color[1] = color[0]; color[2] = *(sbp + 1); color[3] = *(sbp + 2);
|
|
287 sbp = (unsigned short*)src_line[1];
|
|
288 color[4] = *sbp; color[5] = color[4]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
|
|
289 sbp = (unsigned short*)src_line[2];
|
|
290 color[8] = *sbp; color[9] = color[9]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
|
|
291 sbp = (unsigned short*)src_line[3];
|
|
292 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
|
|
293 }
|
|
294 else {
|
|
295 unsigned long *lbp;
|
|
296 lbp = (unsigned long*)src_line[0];
|
|
297 color[0] = *lbp; color[1] = color[0]; color[2] = *(lbp + 1); color[3] = *(lbp + 2);
|
|
298 lbp = (unsigned long*)src_line[1];
|
|
299 color[4] = *lbp; color[5] = color[4]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
|
|
300 lbp = (unsigned long*)src_line[2];
|
|
301 color[8] = *lbp; color[9] = color[9]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
|
|
302 lbp = (unsigned long*)src_line[3];
|
|
303 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
|
|
304 }
|
|
305
|
|
306 } // y loop
|
|
307
|
|
308 }
|
|
309
|
|
310
|
|
311 //===========================================================================//
|
|
312
|
|
313 static int config(struct vf_instance_s* vf,
|
|
314 int width, int height, int d_width, int d_height,
|
|
315 unsigned int flags, unsigned int outfmt){
|
|
316
|
|
317 Init_2xSaI(outfmt&255);
|
|
318
|
|
319 return vf_next_config(vf,2*width,2*height,2*d_width,2*d_height,flags,outfmt);
|
|
320 }
|
|
321
|
|
322 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
323 mp_image_t *dmpi;
|
|
324
|
|
325 // hope we'll get DR buffer:
|
|
326 dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
327 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
|
328 2*mpi->w, 2*mpi->h);
|
|
329
|
|
330 Super2xSaI_ex(mpi->planes[0], mpi->stride[0],
|
|
331 dmpi->planes[0], dmpi->stride[0],
|
|
332 mpi->w, mpi->h, mpi->bpp/8);
|
|
333
|
|
334 return vf_next_put_image(vf,dmpi);
|
|
335 }
|
|
336
|
|
337 //===========================================================================//
|
|
338
|
|
339 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
340 switch(fmt){
|
|
341 // case IMGFMT_BGR15:
|
|
342 // case IMGFMT_BGR16:
|
|
343 case IMGFMT_BGR32:
|
|
344 return vf_next_query_format(vf,fmt);
|
|
345 }
|
|
346 return 0;
|
|
347 }
|
|
348
|
|
349 static int open(vf_instance_t *vf, char* args){
|
|
350 vf->config=config;
|
|
351 vf->put_image=put_image;
|
|
352 vf->query_format=query_format;
|
|
353 return 1;
|
|
354 }
|
|
355
|
|
356 vf_info_t vf_info_2xsai = {
|
|
357 "2xSai BGR bitmap 2x scaler",
|
|
358 "2xsai",
|
|
359 "A'rpi",
|
|
360 "http://elektron.its.tudelft.nl/~dalikifa/",
|
|
361 open
|
|
362 };
|
|
363
|
|
364 //===========================================================================//
|