Statistics
| Branch: | Revision:

ffmpeg / libavfilter / libmpcodecs / vf_2xsai.c @ e4852fb3

History | View | Annotate | Download (14.8 KB)

1
/*
2
 * This file is part of MPlayer.
3
 *
4
 * MPlayer is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * MPlayer is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License along
15
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 */
18

    
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <inttypes.h>
23

    
24
#include "config.h"
25
#include "mp_msg.h"
26

    
27
#include "img_format.h"
28
#include "mp_image.h"
29
#include "vf.h"
30

    
31
//===========================================================================//
32

    
33
/* FIXME: these all belong in the context, not as globals! */
34

    
35
static uint32_t colorMask = 0xF7DEF7DE;
36
static uint32_t lowPixelMask = 0x08210821;
37
static uint32_t qcolorMask = 0xE79CE79C;
38
static uint32_t qlowpixelMask = 0x18631863;
39
static uint32_t redblueMask = 0xF81F;
40
static uint32_t greenMask = 0x7E0;
41
static int PixelsPerMask = 2;
42

    
43
#define makecol(r,g,b) (r+(g<<8)+(b<<16))
44
#define makecol_depth(d,r,g,b) (r+(g<<8)+(b<<16))
45

    
46
static int Init_2xSaI(int d)
47
{
48

    
49
        int minr = 0, ming = 0, minb = 0;
50
        int i;
51

    
52
//        if (d != 15 && d != 16 && d != 24 && d != 32)
53
//                return -1;
54

    
55
        /* Get lowest color bit */
56
        for (i = 0; i < 255; i++) {
57
                if (!minr)
58
                        minr = makecol(i, 0, 0);
59
                if (!ming)
60
                        ming = makecol(0, i, 0);
61
                if (!minb)
62
                        minb = makecol(0, 0, i);
63
        }
64

    
65
        colorMask = (makecol_depth(d, 255, 0, 0) - minr) | (makecol_depth(d, 0, 255, 0) - ming) | (makecol_depth(d, 0, 0, 255) - minb);
66
        lowPixelMask = minr | ming | minb;
67
        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);
68
        qlowpixelMask = (minr * 3) | (ming * 3) | (minb * 3);
69
        redblueMask = makecol_depth(d, 255, 0, 255);
70
        greenMask = makecol_depth(d, 0, 255, 0);
71

    
72
        PixelsPerMask = (d <= 16) ? 2 : 1;
73

    
74
        if (PixelsPerMask == 2) {
75
                colorMask |= (colorMask << 16);
76
                qcolorMask |= (qcolorMask << 16);
77
                lowPixelMask |= (lowPixelMask << 16);
78
                qlowpixelMask |= (qlowpixelMask << 16);
79
        }
80

    
81
//        TRACE("Color Mask:       0x%lX\n", colorMask);
82
//        TRACE("Low Pixel Mask:   0x%lX\n", lowPixelMask);
83
//        TRACE("QColor Mask:      0x%lX\n", qcolorMask);
84
//        TRACE("QLow Pixel Mask:  0x%lX\n", qlowpixelMask);
85

    
86
        return 0;
87
}
88

    
89

    
90
#define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
91

    
92
#define INTERPOLATE(A, B) (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + (A & B & lowPixelMask))
93

    
94
#define Q_INTERPOLATE(A, B, C, D) ((A & qcolorMask) >> 2) + ((B & qcolorMask) >> 2) + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2) \
95
        + ((((A & qlowpixelMask) + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask)) >> 2) & qlowpixelMask)
96

    
97

    
98
static void Super2xSaI_ex(uint8_t *src, uint32_t src_pitch,
99
                          uint8_t *dst, uint32_t dst_pitch,
100
                          uint32_t width, uint32_t height, int sbpp)
101
{
102

    
103
        unsigned int x, y;
104
        uint32_t color[16];
105
        unsigned char *src_line[4];
106

    
107
        /* Point to the first 3 lines. */
108
        src_line[0] = src;
109
        src_line[1] = src;
110
        src_line[2] = src + src_pitch;
111
        src_line[3] = src + src_pitch * 2;
112

    
113
        x = 0, y = 0;
114

    
115
        if (PixelsPerMask == 2) {
116
                unsigned short *sbp;
117
                sbp = (unsigned short*)src_line[0];
118
                color[0] = *sbp;       color[1] = color[0];   color[2] = color[0];    color[3] = color[0];
119
                color[4] = color[0];   color[5] = color[0];   color[6] = *(sbp + 1);  color[7] = *(sbp + 2);
120
                sbp = (unsigned short*)src_line[2];
121
                color[8] = *sbp;     color[9] = color[8];     color[10] = *(sbp + 1); color[11] = *(sbp + 2);
122
                sbp = (unsigned short*)src_line[3];
123
                color[12] = *sbp;    color[13] = color[12];   color[14] = *(sbp + 1); color[15] = *(sbp + 2);
124
        }
125
        else {
126
                uint32_t *lbp;
127
                lbp = (uint32_t*)src_line[0];
128
                color[0] = *lbp;       color[1] = color[0];   color[2] = color[0];    color[3] = color[0];
129
                color[4] = color[0];   color[5] = color[0];   color[6] = *(lbp + 1);  color[7] = *(lbp + 2);
130
                lbp = (uint32_t*)src_line[2];
131
                color[8] = *lbp;     color[9] = color[8];     color[10] = *(lbp + 1); color[11] = *(lbp + 2);
132
                lbp = (uint32_t*)src_line[3];
133
                color[12] = *lbp;    color[13] = color[12];   color[14] = *(lbp + 1); color[15] = *(lbp + 2);
134
        }
135

    
136
        for (y = 0; y < height; y++) {
137
                unsigned char *dst_line[2];
138

    
139
                dst_line[0] = dst + dst_pitch*2*y;
140
                dst_line[1] = dst + dst_pitch*(2*y+1);
141

    
142
                /* Todo: x = width - 2, x = width - 1 */
143

    
144
                for (x = 0; x < width; x++) {
145
                        uint32_t product1a, product1b, product2a, product2b;
146

    
147
//---------------------------------------  B0 B1 B2 B3    0  1  2  3
148
//                                         4  5* 6  S2 -> 4  5* 6  7
149
//                                         1  2  3  S1    8  9 10 11
150
//                                         A0 A1 A2 A3   12 13 14 15
151
//--------------------------------------
152
                        if (color[9] == color[6] && color[5] != color[10]) {
153
                                product2b = color[9];
154
                                product1b = product2b;
155
                        }
156
                        else if (color[5] == color[10] && color[9] != color[6]) {
157
                                product2b = color[5];
158
                                product1b = product2b;
159
                        }
160
                        else if (color[5] == color[10] && color[9] == color[6]) {
161
                                int r = 0;
162

    
163
                                r += GET_RESULT(color[6], color[5], color[8], color[13]);
164
                                r += GET_RESULT(color[6], color[5], color[4], color[1]);
165
                                r += GET_RESULT(color[6], color[5], color[14], color[11]);
166
                                r += GET_RESULT(color[6], color[5], color[2], color[7]);
167

    
168
                                if (r > 0)
169
                                        product1b = color[6];
170
                                else if (r < 0)
171
                                        product1b = color[5];
172
                                else
173
                                        product1b = INTERPOLATE(color[5], color[6]);
174

    
175
                                product2b = product1b;
176

    
177
                        }
178
                        else {
179
                                if (color[6] == color[10] && color[10] == color[13] && color[9] != color[14] && color[10] != color[12])
180
                                        product2b = Q_INTERPOLATE(color[10], color[10], color[10], color[9]);
181
                                else if (color[5] == color[9] && color[9] == color[14] && color[13] != color[10] && color[9] != color[15])
182
                                        product2b = Q_INTERPOLATE(color[9], color[9], color[9], color[10]);
183
                                else
184
                                        product2b = INTERPOLATE(color[9], color[10]);
185

    
186
                                if (color[6] == color[10] && color[6] == color[1] && color[5] != color[2] && color[6] != color[0])
187
                                        product1b = Q_INTERPOLATE(color[6], color[6], color[6], color[5]);
188
                                else if (color[5] == color[9] && color[5] == color[2] && color[1] != color[6] && color[5] != color[3])
189
                                        product1b = Q_INTERPOLATE(color[6], color[5], color[5], color[5]);
190
                                else
191
                                        product1b = INTERPOLATE(color[5], color[6]);
192
                        }
193

    
194
                        if (color[5] == color[10] && color[9] != color[6] && color[4] == color[5] && color[5] != color[14])
195
                                product2a = INTERPOLATE(color[9], color[5]);
196
                        else if (color[5] == color[8] && color[6] == color[5] && color[4] != color[9] && color[5] != color[12])
197
                                product2a = INTERPOLATE(color[9], color[5]);
198
                        else
199
                                product2a = color[9];
200

    
201
                        if (color[9] == color[6] && color[5] != color[10] && color[8] == color[9] && color[9] != color[2])
202
                                product1a = INTERPOLATE(color[9], color[5]);
203
                        else if (color[4] == color[9] && color[10] == color[9] && color[8] != color[5] && color[9] != color[0])
204
                                product1a = INTERPOLATE(color[9], color[5]);
205
                        else
206
                                product1a = color[5];
207

    
208
                        if (PixelsPerMask == 2) {
209
                                *((uint32_t *) (&dst_line[0][x * 4])) = product1a | (product1b << 16);
210
                                *((uint32_t *) (&dst_line[1][x * 4])) = product2a | (product2b << 16);
211
                        }
212
                        else {
213
                                *((uint32_t *) (&dst_line[0][x * 8])) = product1a;
214
                                *((uint32_t *) (&dst_line[0][x * 8 + 4])) = product1b;
215
                                *((uint32_t *) (&dst_line[1][x * 8])) = product2a;
216
                                *((uint32_t *) (&dst_line[1][x * 8 + 4])) = product2b;
217
                        }
218

    
219
                        /* Move color matrix forward */
220
                        color[0] = color[1]; color[4] = color[5]; color[8] = color[9];   color[12] = color[13];
221
                        color[1] = color[2]; color[5] = color[6]; color[9] = color[10];  color[13] = color[14];
222
                        color[2] = color[3]; color[6] = color[7]; color[10] = color[11]; color[14] = color[15];
223

    
224
                        if (x < width - 3) {
225
                                x += 3;
226
                                if (PixelsPerMask == 2) {
227
                                        color[3] = *(((unsigned short*)src_line[0]) + x);
228
                                        color[7] = *(((unsigned short*)src_line[1]) + x);
229
                                        color[11] = *(((unsigned short*)src_line[2]) + x);
230
                                        color[15] = *(((unsigned short*)src_line[3]) + x);
231
                                }
232
                                else {
233
                                        color[3] = *(((uint32_t*)src_line[0]) + x);
234
                                        color[7] = *(((uint32_t*)src_line[1]) + x);
235
                                        color[11] = *(((uint32_t*)src_line[2]) + x);
236
                                        color[15] = *(((uint32_t*)src_line[3]) + x);
237
                                }
238
                                x -= 3;
239
                        }
240
                }
241

    
242
                /* We're done with one line, so we shift the source lines up */
243
                src_line[0] = src_line[1];
244
                src_line[1] = src_line[2];
245
                src_line[2] = src_line[3];
246

    
247
                /* Read next line */
248
                if (y + 3 >= height)
249
                        src_line[3] = src_line[2];
250
                else
251
                        src_line[3] = src_line[2] + src_pitch;
252

    
253
                /* Then shift the color matrix up */
254
                if (PixelsPerMask == 2) {
255
                        unsigned short *sbp;
256
                        sbp = (unsigned short*)src_line[0];
257
                        color[0] = *sbp;     color[1] = color[0];    color[2] = *(sbp + 1);  color[3] = *(sbp + 2);
258
                        sbp = (unsigned short*)src_line[1];
259
                        color[4] = *sbp;     color[5] = color[4];    color[6] = *(sbp + 1);  color[7] = *(sbp + 2);
260
                        sbp = (unsigned short*)src_line[2];
261
                        color[8] = *sbp;     color[9] = color[9];    color[10] = *(sbp + 1); color[11] = *(sbp + 2);
262
                        sbp = (unsigned short*)src_line[3];
263
                        color[12] = *sbp;    color[13] = color[12];  color[14] = *(sbp + 1); color[15] = *(sbp + 2);
264
                }
265
                else {
266
                        uint32_t *lbp;
267
                        lbp = (uint32_t*)src_line[0];
268
                        color[0] = *lbp;     color[1] = color[0];    color[2] = *(lbp + 1);  color[3] = *(lbp + 2);
269
                        lbp = (uint32_t*)src_line[1];
270
                        color[4] = *lbp;     color[5] = color[4];    color[6] = *(lbp + 1);  color[7] = *(lbp + 2);
271
                        lbp = (uint32_t*)src_line[2];
272
                        color[8] = *lbp;     color[9] = color[9];    color[10] = *(lbp + 1); color[11] = *(lbp + 2);
273
                        lbp = (uint32_t*)src_line[3];
274
                        color[12] = *lbp;    color[13] = color[12];  color[14] = *(lbp + 1); color[15] = *(lbp + 2);
275
                }
276

    
277
        } // y loop
278

    
279
}
280

    
281

    
282
//===========================================================================//
283

    
284
static int config(struct vf_instance *vf,
285
        int width, int height, int d_width, int d_height,
286
        unsigned int flags, unsigned int outfmt){
287

    
288
    Init_2xSaI(outfmt&255);
289

    
290
    return vf_next_config(vf,2*width,2*height,2*d_width,2*d_height,flags,outfmt);
291
}
292

    
293
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
294
    mp_image_t *dmpi;
295

    
296
    // hope we'll get DR buffer:
297
    dmpi=vf_get_image(vf->next,mpi->imgfmt,
298
        MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
299
        2*mpi->w, 2*mpi->h);
300

    
301
    Super2xSaI_ex(mpi->planes[0], mpi->stride[0],
302
                  dmpi->planes[0], dmpi->stride[0],
303
                  mpi->w, mpi->h, mpi->bpp/8);
304

    
305
    return vf_next_put_image(vf,dmpi, pts);
306
}
307

    
308
//===========================================================================//
309

    
310
static int query_format(struct vf_instance *vf, unsigned int fmt){
311
    switch(fmt){
312
//    case IMGFMT_BGR15:
313
//    case IMGFMT_BGR16:
314
    case IMGFMT_BGR32:
315
        return vf_next_query_format(vf,fmt);
316
    }
317
    return 0;
318
}
319

    
320
static int vf_open(vf_instance_t *vf, char *args){
321
    vf->config=config;
322
    vf->put_image=put_image;
323
    vf->query_format=query_format;
324
    return 1;
325
}
326

    
327
const vf_info_t vf_info_2xsai = {
328
    "2xSai BGR bitmap 2x scaler",
329
    "2xsai",
330
    "A'rpi",
331
    "http://elektron.its.tudelft.nl/~dalikifa/",
332
    vf_open,
333
    NULL
334
};
335

    
336
//===========================================================================//