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 )
6 * This file is part of FFmpeg.
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.
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.
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.
25 * Super 2xSaI video filter
26 * Ported from MPlayer libmpcodecs/vf_2xsai.c.
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/intreadwrite.h"
37 /* masks used for two pixels interpolation */
38 uint32_t hi_pixel_mask
;
39 uint32_t lo_pixel_mask
;
41 /* masks used for four pixels interpolation */
42 uint32_t q_hi_pixel_mask
;
43 uint32_t q_lo_pixel_mask
;
45 int bpp
; ///< bytes per pixel, pixel stride for each (packed) pixel
49 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
51 #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
53 #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) \
54 + ((((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)
56 static void super2xsai(AVFilterContext
*ctx
,
57 uint8_t *src
, int src_linesize
,
58 uint8_t *dst
, int dst_linesize
,
59 int width
, int height
)
61 Super2xSaIContext
*sai
= ctx
->priv
;
64 unsigned char *src_line
[4];
65 const int bpp
= sai
->bpp
;
66 const uint32_t hi_pixel_mask
= sai
->hi_pixel_mask
;
67 const uint32_t lo_pixel_mask
= sai
->lo_pixel_mask
;
68 const uint32_t q_hi_pixel_mask
= sai
->q_hi_pixel_mask
;
69 const uint32_t q_lo_pixel_mask
= sai
->q_lo_pixel_mask
;
71 /* Point to the first 4 lines, first line is duplicated */
74 src_line
[2] = src
+ src_linesize
*FFMIN(1, height
-1);
75 src_line
[3] = src
+ src_linesize
*FFMIN(2, height
-1);
77 #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
78 #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
79 #define READ_COLOR2(dst, src_line, off) dst = sai->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
81 for (y
= 0; y
< height
; y
++) {
84 dst_line
[0] = dst
+ dst_linesize
*2*y
;
85 dst_line
[1] = dst
+ dst_linesize
*(2*y
+1);
89 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);
90 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);
91 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);
92 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);
95 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);
96 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);
97 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);
98 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);
101 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);
102 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);
103 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);
104 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);
107 for (x
= 0; x
< width
; x
++) {
108 uint32_t product1a
, product1b
, product2a
, product2b
;
110 //--------------------------------------- B0 B1 B2 B3 0 1 2 3
111 // 4 5* 6 S2 -> 4 5* 6 7
112 // 1 2 3 S1 8 9 10 11
113 // A0 A1 A2 A3 12 13 14 15
114 //--------------------------------------
115 if (color
[2][1] == color
[1][2] && color
[1][1] != color
[2][2]) {
116 product2b
= color
[2][1];
117 product1b
= product2b
;
118 } else if (color
[1][1] == color
[2][2] && color
[2][1] != color
[1][2]) {
119 product2b
= color
[1][1];
120 product1b
= product2b
;
121 } else if (color
[1][1] == color
[2][2] && color
[2][1] == color
[1][2]) {
124 r
+= GET_RESULT(color
[1][2], color
[1][1], color
[1][0], color
[3][1]);
125 r
+= GET_RESULT(color
[1][2], color
[1][1], color
[2][0], color
[0][1]);
126 r
+= GET_RESULT(color
[1][2], color
[1][1], color
[3][2], color
[2][3]);
127 r
+= GET_RESULT(color
[1][2], color
[1][1], color
[0][2], color
[1][3]);
130 product1b
= color
[1][2];
132 product1b
= color
[1][1];
134 product1b
= INTERPOLATE(color
[1][1], color
[1][2]);
136 product2b
= product1b
;
138 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])
139 product2b
= Q_INTERPOLATE(color
[2][2], color
[2][2], color
[2][2], color
[2][1]);
140 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])
141 product2b
= Q_INTERPOLATE(color
[2][1], color
[2][1], color
[2][1], color
[2][2]);
143 product2b
= INTERPOLATE(color
[2][1], color
[2][2]);
145 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])
146 product1b
= Q_INTERPOLATE(color
[1][2], color
[1][2], color
[1][2], color
[1][1]);
147 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])
148 product1b
= Q_INTERPOLATE(color
[1][2], color
[1][1], color
[1][1], color
[1][1]);
150 product1b
= INTERPOLATE(color
[1][1], color
[1][2]);
153 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])
154 product2a
= INTERPOLATE(color
[2][1], color
[1][1]);
155 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])
156 product2a
= INTERPOLATE(color
[2][1], color
[1][1]);
158 product2a
= color
[2][1];
160 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])
161 product1a
= INTERPOLATE(color
[2][1], color
[1][1]);
162 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])
163 product1a
= INTERPOLATE(color
[2][1], color
[1][1]);
165 product1a
= color
[1][1];
167 /* Set the calculated pixels */
170 AV_WN32A(dst_line
[0] + x
* 8, product1a
);
171 AV_WN32A(dst_line
[0] + x
* 8 + 4, product1b
);
172 AV_WN32A(dst_line
[1] + x
* 8, product2a
);
173 AV_WN32A(dst_line
[1] + x
* 8 + 4, product2b
);
176 AV_WL24(dst_line
[0] + x
* 6, product1a
);
177 AV_WL24(dst_line
[0] + x
* 6 + 3, product1b
);
178 AV_WL24(dst_line
[1] + x
* 6, product2a
);
179 AV_WL24(dst_line
[1] + x
* 6 + 3, product2b
);
183 AV_WB32(dst_line
[0] + x
* 4, product1a
| (product1b
<< 16));
184 AV_WB32(dst_line
[1] + x
* 4, product2a
| (product2b
<< 16));
186 AV_WL32(dst_line
[0] + x
* 4, product1a
| (product1b
<< 16));
187 AV_WL32(dst_line
[1] + x
* 4, product2a
| (product2b
<< 16));
191 /* Move color matrix forward */
192 color
[0][0] = color
[0][1]; color
[0][1] = color
[0][2]; color
[0][2] = color
[0][3];
193 color
[1][0] = color
[1][1]; color
[1][1] = color
[1][2]; color
[1][2] = color
[1][3];
194 color
[2][0] = color
[2][1]; color
[2][1] = color
[2][2]; color
[2][2] = color
[2][3];
195 color
[3][0] = color
[3][1]; color
[3][1] = color
[3][2]; color
[3][2] = color
[3][3];
201 READ_COLOR4(color
[0][3], src_line
[0], x
);
202 READ_COLOR4(color
[1][3], src_line
[1], x
);
203 READ_COLOR4(color
[2][3], src_line
[2], x
);
204 READ_COLOR4(color
[3][3], src_line
[3], x
);
207 READ_COLOR3(color
[0][3], src_line
[0], x
);
208 READ_COLOR3(color
[1][3], src_line
[1], x
);
209 READ_COLOR3(color
[2][3], src_line
[2], x
);
210 READ_COLOR3(color
[3][3], src_line
[3], x
);
212 default: /* case 2 */
213 READ_COLOR2(color
[0][3], src_line
[0], x
);
214 READ_COLOR2(color
[1][3], src_line
[1], x
);
215 READ_COLOR2(color
[2][3], src_line
[2], x
);
216 READ_COLOR2(color
[3][3], src_line
[3], x
);
222 /* We're done with one line, so we shift the source lines up */
223 src_line
[0] = src_line
[1];
224 src_line
[1] = src_line
[2];
225 src_line
[2] = src_line
[3];
228 src_line
[3] = src_line
[2];
230 src_line
[3] += src_linesize
;
234 static int query_formats(AVFilterContext
*ctx
)
236 static const enum AVPixelFormat pix_fmts
[] = {
237 AV_PIX_FMT_RGBA
, AV_PIX_FMT_BGRA
, AV_PIX_FMT_ARGB
, AV_PIX_FMT_ABGR
,
238 AV_PIX_FMT_RGB24
, AV_PIX_FMT_BGR24
,
239 AV_PIX_FMT_RGB565BE
, AV_PIX_FMT_BGR565BE
, AV_PIX_FMT_RGB555BE
, AV_PIX_FMT_BGR555BE
,
240 AV_PIX_FMT_RGB565LE
, AV_PIX_FMT_BGR565LE
, AV_PIX_FMT_RGB555LE
, AV_PIX_FMT_BGR555LE
,
244 ff_set_common_formats(ctx
, ff_make_format_list(pix_fmts
));
248 static int config_input(AVFilterLink
*inlink
)
250 Super2xSaIContext
*sai
= inlink
->dst
->priv
;
252 sai
->hi_pixel_mask
= 0xFEFEFEFE;
253 sai
->lo_pixel_mask
= 0x01010101;
254 sai
->q_hi_pixel_mask
= 0xFCFCFCFC;
255 sai
->q_lo_pixel_mask
= 0x03030303;
258 switch (inlink
->format
) {
259 case AV_PIX_FMT_RGB24
:
260 case AV_PIX_FMT_BGR24
:
264 case AV_PIX_FMT_RGB565BE
:
265 case AV_PIX_FMT_BGR565BE
:
267 case AV_PIX_FMT_RGB565LE
:
268 case AV_PIX_FMT_BGR565LE
:
269 sai
->hi_pixel_mask
= 0xF7DEF7DE;
270 sai
->lo_pixel_mask
= 0x08210821;
271 sai
->q_hi_pixel_mask
= 0xE79CE79C;
272 sai
->q_lo_pixel_mask
= 0x18631863;
276 case AV_PIX_FMT_BGR555BE
:
277 case AV_PIX_FMT_RGB555BE
:
279 case AV_PIX_FMT_BGR555LE
:
280 case AV_PIX_FMT_RGB555LE
:
281 sai
->hi_pixel_mask
= 0x7BDE7BDE;
282 sai
->lo_pixel_mask
= 0x04210421;
283 sai
->q_hi_pixel_mask
= 0x739C739C;
284 sai
->q_lo_pixel_mask
= 0x0C630C63;
292 static int config_output(AVFilterLink
*outlink
)
294 AVFilterLink
*inlink
= outlink
->src
->inputs
[0];
296 outlink
->w
= inlink
->w
*2;
297 outlink
->h
= inlink
->h
*2;
299 av_log(inlink
->dst
, AV_LOG_VERBOSE
, "fmt:%s size:%dx%d -> size:%dx%d\n",
300 av_get_pix_fmt_name(inlink
->format
),
301 inlink
->w
, inlink
->h
, outlink
->w
, outlink
->h
);
306 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*inpicref
)
308 AVFilterLink
*outlink
= inlink
->dst
->outputs
[0];
309 AVFrame
*outpicref
= ff_get_video_buffer(outlink
, outlink
->w
, outlink
->h
);
311 av_frame_free(&inpicref
);
312 return AVERROR(ENOMEM
);
314 av_frame_copy_props(outpicref
, inpicref
);
315 outpicref
->width
= outlink
->w
;
316 outpicref
->height
= outlink
->h
;
318 super2xsai(inlink
->dst
, inpicref
->data
[0], inpicref
->linesize
[0],
319 outpicref
->data
[0], outpicref
->linesize
[0],
320 inlink
->w
, inlink
->h
);
322 av_frame_free(&inpicref
);
323 return ff_filter_frame(outlink
, outpicref
);
326 static const AVFilterPad super2xsai_inputs
[] = {
329 .type
= AVMEDIA_TYPE_VIDEO
,
330 .config_props
= config_input
,
331 .filter_frame
= filter_frame
,
336 static const AVFilterPad super2xsai_outputs
[] = {
339 .type
= AVMEDIA_TYPE_VIDEO
,
340 .config_props
= config_output
,
345 AVFilter ff_vf_super2xsai
= {
346 .name
= "super2xsai",
347 .description
= NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
348 .priv_size
= sizeof(Super2xSaIContext
),
349 .query_formats
= query_formats
,
350 .inputs
= super2xsai_inputs
,
351 .outputs
= super2xsai_outputs
,