small compilation fix
[mplayer/glamo.git] / libmpcodecs / vf_2xsai.c
blob21a8f48ab902002158d36061c8d11dca7cf79c70
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "../config.h"
7 #include "../mp_msg.h"
9 #include "img_format.h"
10 #include "mp_image.h"
11 #include "vf.h"
13 //===========================================================================//
15 #define uint32 unsigned long
16 #define uint16 unsigned short
17 #define uint8 unsigned char
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;
28 #define makecol(r,g,b) (r+(g<<8)+(b<<16))
29 #define makecol_depth(d,r,g,b) (r+(g<<8)+(b<<16))
31 int Init_2xSaI(int d)
34 int minr = 0, ming = 0, minb = 0;
35 int i;
37 // if (d != 15 && d != 16 && d != 24 && d != 32)
38 // return -1;
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);
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);
57 PixelsPerMask = (d <= 16) ? 2 : 1;
59 if (PixelsPerMask == 2) {
60 colorMask |= (colorMask << 16);
61 qcolorMask |= (qcolorMask << 16);
62 lowPixelMask |= (lowPixelMask << 16);
63 qlowpixelMask |= (qlowpixelMask << 16);
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);
71 xsai_depth = d;
73 return 0;
77 static int GetResult1(uint32 A, uint32 B, uint32 C, uint32 D)
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;
97 static int GetResult2(uint32 A, uint32 B, uint32 C, uint32 D, uint32 E)
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;
118 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
120 #define INTERPOLATE(A, B) (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + (A & B & lowPixelMask))
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)
126 static unsigned char *src_line[4];
127 static unsigned char *dst_line[2];
129 void Super2xSaI_ex(uint8 *src, uint32 src_pitch,
130 uint8 *dst, uint32 dst_pitch,
131 uint32 width, uint32 height, int sbpp) {
133 unsigned int x, y;
134 unsigned long color[16];
136 /* Point to the first 3 lines. */
137 src_line[0] = src;
138 src_line[1] = src;
139 src_line[2] = src + src_pitch;
140 src_line[3] = src + src_pitch * 2;
142 x = 0, y = 0;
144 if (PixelsPerMask == 2) {
145 unsigned short *sbp;
146 sbp = (unsigned short*)src_line[0];
147 color[0] = *sbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
148 color[4] = color[0]; color[5] = color[0]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
149 sbp = (unsigned short*)src_line[2];
150 color[8] = *sbp; color[9] = color[8]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
151 sbp = (unsigned short*)src_line[3];
152 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
154 else {
155 unsigned long *lbp;
156 lbp = (unsigned long*)src_line[0];
157 color[0] = *lbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
158 color[4] = color[0]; color[5] = color[0]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
159 lbp = (unsigned long*)src_line[2];
160 color[8] = *lbp; color[9] = color[8]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
161 lbp = (unsigned long*)src_line[3];
162 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
165 for (y = 0; y < height; y++) {
167 dst_line[0] = dst + dst_pitch*2*y;
168 dst_line[1] = dst + dst_pitch*(2*y+1);
170 /* Todo: x = width - 2, x = width - 1 */
172 for (x = 0; x < width; x++) {
173 unsigned long product1a, product1b, product2a, product2b;
175 //--------------------------------------- B0 B1 B2 B3 0 1 2 3
176 // 4 5* 6 S2 -> 4 5* 6 7
177 // 1 2 3 S1 8 9 10 11
178 // A0 A1 A2 A3 12 13 14 15
179 //--------------------------------------
180 if (color[9] == color[6] && color[5] != color[10]) {
181 product2b = color[9];
182 product1b = product2b;
184 else if (color[5] == color[10] && color[9] != color[6]) {
185 product2b = color[5];
186 product1b = product2b;
188 else if (color[5] == color[10] && color[9] == color[6]) {
189 int r = 0;
191 r += GET_RESULT(color[6], color[5], color[8], color[13]);
192 r += GET_RESULT(color[6], color[5], color[4], color[1]);
193 r += GET_RESULT(color[6], color[5], color[14], color[11]);
194 r += GET_RESULT(color[6], color[5], color[2], color[7]);
196 if (r > 0)
197 product1b = color[6];
198 else if (r < 0)
199 product1b = color[5];
200 else
201 product1b = INTERPOLATE(color[5], color[6]);
203 product2b = product1b;
206 else {
207 if (color[6] == color[10] && color[10] == color[13] && color[9] != color[14] && color[10] != color[12])
208 product2b = Q_INTERPOLATE(color[10], color[10], color[10], color[9]);
209 else if (color[5] == color[9] && color[9] == color[14] && color[13] != color[10] && color[9] != color[15])
210 product2b = Q_INTERPOLATE(color[9], color[9], color[9], color[10]);
211 else
212 product2b = INTERPOLATE(color[9], color[10]);
214 if (color[6] == color[10] && color[6] == color[1] && color[5] != color[2] && color[6] != color[0])
215 product1b = Q_INTERPOLATE(color[6], color[6], color[6], color[5]);
216 else if (color[5] == color[9] && color[5] == color[2] && color[1] != color[6] && color[5] != color[3])
217 product1b = Q_INTERPOLATE(color[6], color[5], color[5], color[5]);
218 else
219 product1b = INTERPOLATE(color[5], color[6]);
222 if (color[5] == color[10] && color[9] != color[6] && color[4] == color[5] && color[5] != color[14])
223 product2a = INTERPOLATE(color[9], color[5]);
224 else if (color[5] == color[8] && color[6] == color[5] && color[4] != color[9] && color[5] != color[12])
225 product2a = INTERPOLATE(color[9], color[5]);
226 else
227 product2a = color[9];
229 if (color[9] == color[6] && color[5] != color[10] && color[8] == color[9] && color[9] != color[2])
230 product1a = INTERPOLATE(color[9], color[5]);
231 else if (color[4] == color[9] && color[10] == color[9] && color[8] != color[5] && color[9] != color[0])
232 product1a = INTERPOLATE(color[9], color[5]);
233 else
234 product1a = color[5];
236 if (PixelsPerMask == 2) {
237 *((unsigned long *) (&dst_line[0][x * 4])) = product1a | (product1b << 16);
238 *((unsigned long *) (&dst_line[1][x * 4])) = product2a | (product2b << 16);
240 else {
241 *((unsigned long *) (&dst_line[0][x * 8])) = product1a;
242 *((unsigned long *) (&dst_line[0][x * 8 + 4])) = product1b;
243 *((unsigned long *) (&dst_line[1][x * 8])) = product2a;
244 *((unsigned long *) (&dst_line[1][x * 8 + 4])) = product2b;
247 /* Move color matrix forward */
248 color[0] = color[1]; color[4] = color[5]; color[8] = color[9]; color[12] = color[13];
249 color[1] = color[2]; color[5] = color[6]; color[9] = color[10]; color[13] = color[14];
250 color[2] = color[3]; color[6] = color[7]; color[10] = color[11]; color[14] = color[15];
252 if (x < width - 3) {
253 x += 3;
254 if (PixelsPerMask == 2) {
255 color[3] = *(((unsigned short*)src_line[0]) + x);
256 color[7] = *(((unsigned short*)src_line[1]) + x);
257 color[11] = *(((unsigned short*)src_line[2]) + x);
258 color[15] = *(((unsigned short*)src_line[3]) + x);
260 else {
261 color[3] = *(((unsigned long*)src_line[0]) + x);
262 color[7] = *(((unsigned long*)src_line[1]) + x);
263 color[11] = *(((unsigned long*)src_line[2]) + x);
264 color[15] = *(((unsigned long*)src_line[3]) + x);
266 x -= 3;
270 /* We're done with one line, so we shift the source lines up */
271 src_line[0] = src_line[1];
272 src_line[1] = src_line[2];
273 src_line[2] = src_line[3];
275 /* Read next line */
276 if (y + 3 >= height)
277 src_line[3] = src_line[2];
278 else
279 src_line[3] = src_line[2] + src_pitch;
281 /* Then shift the color matrix up */
282 if (PixelsPerMask == 2) {
283 unsigned short *sbp;
284 sbp = (unsigned short*)src_line[0];
285 color[0] = *sbp; color[1] = color[0]; color[2] = *(sbp + 1); color[3] = *(sbp + 2);
286 sbp = (unsigned short*)src_line[1];
287 color[4] = *sbp; color[5] = color[4]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
288 sbp = (unsigned short*)src_line[2];
289 color[8] = *sbp; color[9] = color[9]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
290 sbp = (unsigned short*)src_line[3];
291 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
293 else {
294 unsigned long *lbp;
295 lbp = (unsigned long*)src_line[0];
296 color[0] = *lbp; color[1] = color[0]; color[2] = *(lbp + 1); color[3] = *(lbp + 2);
297 lbp = (unsigned long*)src_line[1];
298 color[4] = *lbp; color[5] = color[4]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
299 lbp = (unsigned long*)src_line[2];
300 color[8] = *lbp; color[9] = color[9]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
301 lbp = (unsigned long*)src_line[3];
302 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
305 } // y loop
310 //===========================================================================//
312 static int config(struct vf_instance_s* vf,
313 int width, int height, int d_width, int d_height,
314 unsigned int flags, unsigned int outfmt){
316 Init_2xSaI(outfmt&255);
318 return vf_next_config(vf,2*width,2*height,2*d_width,2*d_height,flags,outfmt);
321 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
322 mp_image_t *dmpi;
324 // hope we'll get DR buffer:
325 dmpi=vf_get_image(vf->next,mpi->imgfmt,
326 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
327 2*mpi->w, 2*mpi->h);
329 Super2xSaI_ex(mpi->planes[0], mpi->stride[0],
330 dmpi->planes[0], dmpi->stride[0],
331 mpi->w, mpi->h, mpi->bpp/8);
333 return vf_next_put_image(vf,dmpi);
336 //===========================================================================//
338 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
339 switch(fmt){
340 // case IMGFMT_BGR15:
341 // case IMGFMT_BGR16:
342 case IMGFMT_BGR32:
343 return vf_next_query_format(vf,fmt);
345 return 0;
348 static int open(vf_instance_t *vf, char* args){
349 vf->config=config;
350 vf->put_image=put_image;
351 vf->query_format=query_format;
352 return 1;
355 vf_info_t vf_info_2xsai = {
356 "2xSai BGR bitmap 2x scaler",
357 "2xsai",
358 "A'rpi",
359 "http://elektron.its.tudelft.nl/~dalikifa/",
360 open,
361 NULL
364 //===========================================================================//