]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_super2xsai.c
lavfi/super2xsai: fix fate test on bigendian
[ffmpeg] / libavfilter / vf_super2xsai.c
1 /*
2  * Copyright (c) 2010 Niel van der Westhuizen <nielkie@gmail.com>
3  * Copyright (c) 2002 A'rpi
4  * Copyright (c) 1997-2001 ZSNES Team ( zsknight@zsnes.com / _demo_@zsnes.com )
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 along
19  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * @file
25  * Super 2xSaI video filter
26  * Ported from MPlayer libmpcodecs/vf_2xsai.c.
27  */
28
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/intreadwrite.h"
31 #include "avfilter.h"
32
33 typedef struct {
34     /* masks used for two pixels interpolation */
35     uint32_t hi_pixel_mask;
36     uint32_t lo_pixel_mask;
37
38     /* masks used for four pixels interpolation */
39     uint32_t q_hi_pixel_mask;
40     uint32_t q_lo_pixel_mask;
41
42     int bpp; ///< bytes per pixel, pixel stride for each (packed) pixel
43     int is_be;
44 } Super2xSaIContext;
45
46 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
47
48 #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
49
50 #define Q_INTERPOLATE(A, B, C, D) ((A & q_hi_pixel_mask) >> 2) + ((B & q_hi_pixel_mask) >> 2) + ((C & q_hi_pixel_mask) >> 2) + ((D & q_hi_pixel_mask) >> 2) \
51     + ((((A & q_lo_pixel_mask) + (B & q_lo_pixel_mask) + (C & q_lo_pixel_mask) + (D & q_lo_pixel_mask)) >> 2) & q_lo_pixel_mask)
52
53 static void super2xsai(AVFilterContext *ctx,
54                        uint8_t *src, int src_linesize,
55                        uint8_t *dst, int dst_linesize,
56                        int width, int height)
57 {
58     Super2xSaIContext *sai = ctx->priv;
59     unsigned int x, y;
60     uint32_t color[4][4];
61     unsigned char *src_line[4];
62     const int bpp = sai->bpp;
63     const uint32_t hi_pixel_mask = sai->hi_pixel_mask;
64     const uint32_t lo_pixel_mask = sai->lo_pixel_mask;
65     const uint32_t q_hi_pixel_mask = sai->q_hi_pixel_mask;
66     const uint32_t q_lo_pixel_mask = sai->q_lo_pixel_mask;
67
68     /* Point to the first 4 lines, first line is duplicated */
69     src_line[0] = src;
70     src_line[1] = src;
71     src_line[2] = src + src_linesize*FFMIN(1, height-1);
72     src_line[3] = src + src_linesize*FFMIN(2, height-1);
73
74 #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
75 #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
76 #define READ_COLOR2(dst, src_line, off) dst = sai->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
77
78     for (y = 0; y < height; y++) {
79         uint8_t *dst_line[2];
80
81         dst_line[0] = dst + dst_linesize*2*y;
82         dst_line[1] = dst + dst_linesize*(2*y+1);
83
84         switch (bpp) {
85         case 4:
86             READ_COLOR4(color[0][0], src_line[0], 0); color[0][1] = color[0][0]; READ_COLOR4(color[0][2], src_line[0], 1); READ_COLOR4(color[0][3], src_line[0], 2);
87             READ_COLOR4(color[1][0], src_line[1], 0); color[1][1] = color[1][0]; READ_COLOR4(color[1][2], src_line[1], 1); READ_COLOR4(color[1][3], src_line[1], 2);
88             READ_COLOR4(color[2][0], src_line[2], 0); color[2][1] = color[2][0]; READ_COLOR4(color[2][2], src_line[2], 1); READ_COLOR4(color[2][3], src_line[2], 2);
89             READ_COLOR4(color[3][0], src_line[3], 0); color[3][1] = color[3][0]; READ_COLOR4(color[3][2], src_line[3], 1); READ_COLOR4(color[3][3], src_line[3], 2);
90             break;
91         case 3:
92             READ_COLOR3(color[0][0], src_line[0], 0); color[0][1] = color[0][0]; READ_COLOR3(color[0][2], src_line[0], 1); READ_COLOR3(color[0][3], src_line[0], 2);
93             READ_COLOR3(color[1][0], src_line[1], 0); color[1][1] = color[1][0]; READ_COLOR3(color[1][2], src_line[1], 1); READ_COLOR3(color[1][3], src_line[1], 2);
94             READ_COLOR3(color[2][0], src_line[2], 0); color[2][1] = color[2][0]; READ_COLOR3(color[2][2], src_line[2], 1); READ_COLOR3(color[2][3], src_line[2], 2);
95             READ_COLOR3(color[3][0], src_line[3], 0); color[3][1] = color[3][0]; READ_COLOR3(color[3][2], src_line[3], 1); READ_COLOR3(color[3][3], src_line[3], 2);
96             break;
97         default:
98             READ_COLOR2(color[0][0], src_line[0], 0); color[0][1] = color[0][0]; READ_COLOR2(color[0][2], src_line[0], 1); READ_COLOR2(color[0][3], src_line[0], 2);
99             READ_COLOR2(color[1][0], src_line[1], 0); color[1][1] = color[1][0]; READ_COLOR2(color[1][2], src_line[1], 1); READ_COLOR2(color[1][3], src_line[1], 2);
100             READ_COLOR2(color[2][0], src_line[2], 0); color[2][1] = color[2][0]; READ_COLOR2(color[2][2], src_line[2], 1); READ_COLOR2(color[2][3], src_line[2], 2);
101             READ_COLOR2(color[3][0], src_line[3], 0); color[3][1] = color[3][0]; READ_COLOR2(color[3][2], src_line[3], 1); READ_COLOR2(color[3][3], src_line[3], 2);
102         }
103
104         for (x = 0; x < width; x++) {
105             uint32_t product1a, product1b, product2a, product2b;
106
107 //---------------------------------------  B0 B1 B2 B3    0  1  2  3
108 //                                         4  5* 6  S2 -> 4  5* 6  7
109 //                                         1  2  3  S1    8  9 10 11
110 //                                         A0 A1 A2 A3   12 13 14 15
111 //--------------------------------------
112             if (color[2][1] == color[1][2] && color[1][1] != color[2][2]) {
113                 product2b = color[2][1];
114                 product1b = product2b;
115             } else if (color[1][1] == color[2][2] && color[2][1] != color[1][2]) {
116                 product2b = color[1][1];
117                 product1b = product2b;
118             } else if (color[1][1] == color[2][2] && color[2][1] == color[1][2]) {
119                 int r = 0;
120
121                 r += GET_RESULT(color[1][2], color[1][1], color[1][0], color[3][1]);
122                 r += GET_RESULT(color[1][2], color[1][1], color[2][0], color[0][1]);
123                 r += GET_RESULT(color[1][2], color[1][1], color[3][2], color[2][3]);
124                 r += GET_RESULT(color[1][2], color[1][1], color[0][2], color[1][3]);
125
126                 if (r > 0)
127                     product1b = color[1][2];
128                 else if (r < 0)
129                     product1b = color[1][1];
130                 else
131                     product1b = INTERPOLATE(color[1][1], color[1][2]);
132
133                 product2b = product1b;
134             } else {
135                 if (color[1][2] == color[2][2] && color[2][2] == color[3][1] && color[2][1] != color[3][2] && color[2][2] != color[3][0])
136                     product2b = Q_INTERPOLATE(color[2][2], color[2][2], color[2][2], color[2][1]);
137                 else if (color[1][1] == color[2][1] && color[2][1] == color[3][2] && color[3][1] != color[2][2] && color[2][1] != color[3][3])
138                     product2b = Q_INTERPOLATE(color[2][1], color[2][1], color[2][1], color[2][2]);
139                 else
140                     product2b = INTERPOLATE(color[2][1], color[2][2]);
141
142                 if (color[1][2] == color[2][2] && color[1][2] == color[0][1] && color[1][1] != color[0][2] && color[1][2] != color[0][0])
143                     product1b = Q_INTERPOLATE(color[1][2], color[1][2], color[1][2], color[1][1]);
144                 else if (color[1][1] == color[2][1] && color[1][1] == color[0][2] && color[0][1] != color[1][2] && color[1][1] != color[0][3])
145                     product1b = Q_INTERPOLATE(color[1][2], color[1][1], color[1][1], color[1][1]);
146                 else
147                     product1b = INTERPOLATE(color[1][1], color[1][2]);
148             }
149
150             if (color[1][1] == color[2][2] && color[2][1] != color[1][2] && color[1][0] == color[1][1] && color[1][1] != color[3][2])
151                 product2a = INTERPOLATE(color[2][1], color[1][1]);
152             else if (color[1][1] == color[2][0] && color[1][2] == color[1][1] && color[1][0] != color[2][1] && color[1][1] != color[3][0])
153                 product2a = INTERPOLATE(color[2][1], color[1][1]);
154             else
155                 product2a = color[2][1];
156
157             if (color[2][1] == color[1][2] && color[1][1] != color[2][2] && color[2][0] == color[2][1] && color[2][1] != color[0][2])
158                 product1a = INTERPOLATE(color[2][1], color[1][1]);
159             else if (color[1][0] == color[2][1] && color[2][2] == color[2][1] && color[2][0] != color[1][1] && color[2][1] != color[0][0])
160                 product1a = INTERPOLATE(color[2][1], color[1][1]);
161             else
162                 product1a = color[1][1];
163
164             /* Set the calculated pixels */
165             switch (bpp) {
166             case 4:
167                 AV_WN32A(dst_line[0] + x * 8,     product1a);
168                 AV_WN32A(dst_line[0] + x * 8 + 4, product1b);
169                 AV_WN32A(dst_line[1] + x * 8,     product2a);
170                 AV_WN32A(dst_line[1] + x * 8 + 4, product2b);
171                 break;
172             case 3:
173                 AV_WL24(dst_line[0] + x * 6,     product1a);
174                 AV_WL24(dst_line[0] + x * 6 + 3, product1b);
175                 AV_WL24(dst_line[1] + x * 6,     product2a);
176                 AV_WL24(dst_line[1] + x * 6 + 3, product2b);
177                 break;
178             default: // bpp = 2
179                 if (sai->is_be) {
180                     AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
181                     AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
182                 } else {
183                     AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
184                     AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
185                 }
186             }
187
188             /* Move color matrix forward */
189             color[0][0] = color[0][1]; color[0][1] = color[0][2]; color[0][2] = color[0][3];
190             color[1][0] = color[1][1]; color[1][1] = color[1][2]; color[1][2] = color[1][3];
191             color[2][0] = color[2][1]; color[2][1] = color[2][2]; color[2][2] = color[2][3];
192             color[3][0] = color[3][1]; color[3][1] = color[3][2]; color[3][2] = color[3][3];
193
194             if (x < width - 3) {
195                 x += 3;
196                 switch (bpp) {
197                 case 4:
198                     READ_COLOR4(color[0][3], src_line[0], x);
199                     READ_COLOR4(color[1][3], src_line[1], x);
200                     READ_COLOR4(color[2][3], src_line[2], x);
201                     READ_COLOR4(color[3][3], src_line[3], x);
202                     break;
203                 case 3:
204                     READ_COLOR3(color[0][3], src_line[0], x);
205                     READ_COLOR3(color[1][3], src_line[1], x);
206                     READ_COLOR3(color[2][3], src_line[2], x);
207                     READ_COLOR3(color[3][3], src_line[3], x);
208                     break;
209                 default:        /* case 2 */
210                     READ_COLOR2(color[0][3], src_line[0], x);
211                     READ_COLOR2(color[1][3], src_line[1], x);
212                     READ_COLOR2(color[2][3], src_line[2], x);
213                     READ_COLOR2(color[3][3], src_line[3], x);
214                 }
215                 x -= 3;
216             }
217         }
218
219         /* We're done with one line, so we shift the source lines up */
220         src_line[0] = src_line[1];
221         src_line[1] = src_line[2];
222         src_line[2] = src_line[3];
223
224         /* Read next line */
225         src_line[3] = src_line[2];
226         if (y < height - 3)
227             src_line[3] += src_linesize;
228     } // y loop
229 }
230
231 static int query_formats(AVFilterContext *ctx)
232 {
233     static const enum PixelFormat pix_fmts[] = {
234         PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
235         PIX_FMT_RGB24, PIX_FMT_BGR24,
236         PIX_FMT_RGB565BE, PIX_FMT_BGR565BE, PIX_FMT_RGB555BE, PIX_FMT_BGR555BE,
237         PIX_FMT_RGB565LE, PIX_FMT_BGR565LE, PIX_FMT_RGB555LE, PIX_FMT_BGR555LE,
238         PIX_FMT_NONE
239     };
240
241     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
242     return 0;
243 }
244
245 static int config_input(AVFilterLink *inlink)
246 {
247     Super2xSaIContext *sai = inlink->dst->priv;
248
249     sai->hi_pixel_mask   = 0xFEFEFEFE;
250     sai->lo_pixel_mask   = 0x01010101;
251     sai->q_hi_pixel_mask = 0xFCFCFCFC;
252     sai->q_lo_pixel_mask = 0x03030303;
253     sai->bpp  = 4;
254
255     switch (inlink->format) {
256     case PIX_FMT_RGB24:
257     case PIX_FMT_BGR24:
258         sai->bpp = 3;
259         break;
260
261     case PIX_FMT_RGB565BE:
262     case PIX_FMT_BGR565BE:
263         sai->is_be = 1;
264     case PIX_FMT_RGB565LE:
265     case PIX_FMT_BGR565LE:
266         sai->hi_pixel_mask   = 0xF7DEF7DE;
267         sai->lo_pixel_mask   = 0x08210821;
268         sai->q_hi_pixel_mask = 0xE79CE79C;
269         sai->q_lo_pixel_mask = 0x18631863;
270         sai->bpp = 2;
271         break;
272
273     case PIX_FMT_BGR555BE:
274     case PIX_FMT_RGB555BE:
275         sai->is_be = 1;
276     case PIX_FMT_BGR555LE:
277     case PIX_FMT_RGB555LE:
278         sai->hi_pixel_mask   = 0x7BDE7BDE;
279         sai->lo_pixel_mask   = 0x04210421;
280         sai->q_hi_pixel_mask = 0x739C739C;
281         sai->q_lo_pixel_mask = 0x0C630C63;
282         sai->bpp = 2;
283         break;
284     }
285
286     return 0;
287 }
288
289 static int config_output(AVFilterLink *outlink)
290 {
291     AVFilterLink *inlink = outlink->src->inputs[0];
292
293     outlink->w = inlink->w*2;
294     outlink->h = inlink->h*2;
295
296     av_log(inlink->dst, AV_LOG_INFO, "fmt:%s size:%dx%d -> size:%dx%d\n",
297            av_get_pix_fmt_name(inlink->format),
298            inlink->w, inlink->h, outlink->w, outlink->h);
299
300     return 0;
301 }
302
303 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
304
305 static void end_frame(AVFilterLink *inlink)
306 {
307     AVFilterLink *outlink = inlink->dst->outputs[0];
308     AVFilterBufferRef  *inpicref =  inlink->cur_buf;
309     AVFilterBufferRef *outpicref = outlink->out_buf;
310
311     super2xsai(inlink->dst, inpicref->data[0], inpicref->linesize[0],
312                outpicref->data[0], outpicref->linesize[0],
313                inlink->w, inlink->h);
314
315     avfilter_unref_buffer(inpicref);
316     avfilter_draw_slice(outlink, 0, outlink->h, 1);
317     avfilter_end_frame(outlink);
318     avfilter_unref_buffer(outpicref);
319 }
320
321 AVFilter avfilter_vf_super2xsai = {
322     .name        = "super2xsai",
323     .description = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
324     .priv_size   = sizeof(Super2xSaIContext),
325     .query_formats = query_formats,
326
327     .inputs = (const AVFilterPad[]) {
328         { .name             = "default",
329           .type             = AVMEDIA_TYPE_VIDEO,
330           .config_props     = config_input,
331           .draw_slice       = null_draw_slice,
332           .end_frame        = end_frame,
333           .min_perms        = AV_PERM_READ },
334         { .name = NULL }
335     },
336     .outputs = (const AVFilterPad[]) {
337         { .name             = "default",
338           .type             = AVMEDIA_TYPE_VIDEO,
339           .config_props     = config_output },
340         { .name = NULL }
341     },
342 };