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"
36 typedef struct Super2xSaIContext {
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 typedef struct ThreadData {
53 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
55 #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
57 #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) \
58 + ((((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)
60 static int super2xsai(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
62 Super2xSaIContext *s = ctx->priv;
65 AVFrame *out = td->out;
66 const uint8_t *src = in->data[0];
67 uint8_t *dst = out->data[0];
68 const int src_linesize = in->linesize[0];
69 const int dst_linesize = out->linesize[0];
70 const int width = in->width;
71 const int height = in->height;
74 const uint8_t *src_line[4];
75 const int bpp = s->bpp;
76 const uint32_t hi_pixel_mask = s->hi_pixel_mask;
77 const uint32_t lo_pixel_mask = s->lo_pixel_mask;
78 const uint32_t q_hi_pixel_mask = s->q_hi_pixel_mask;
79 const uint32_t q_lo_pixel_mask = s->q_lo_pixel_mask;
80 const int slice_start = (height * jobnr) / nb_jobs;
81 const int slice_end = (height * (jobnr+1)) / nb_jobs;
83 /* Point to the first 4 lines, first line is duplicated */
84 src_line[0] = src + src_linesize*FFMAX(slice_start - 1, 0);
85 src_line[1] = src + src_linesize*slice_start;
86 src_line[2] = src + src_linesize*FFMIN(slice_start + 1, height-1);
87 src_line[3] = src + src_linesize*FFMIN(slice_start + 2, height-1);
89 #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
90 #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
91 #define READ_COLOR2(dst, src_line, off) dst = s->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
93 for (y = slice_start; y < slice_end; y++) {
96 dst_line[0] = dst + dst_linesize*2*y;
97 dst_line[1] = dst + dst_linesize*(2*y+1);
101 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);
102 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);
103 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);
104 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);
107 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);
108 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);
109 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);
110 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);
113 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);
114 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);
115 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);
116 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);
119 for (x = 0; x < width; x++) {
120 uint32_t product1a, product1b, product2a, product2b;
122 //--------------------------------------- B0 B1 B2 B3 0 1 2 3
123 // 4 5* 6 S2 -> 4 5* 6 7
124 // 1 2 3 S1 8 9 10 11
125 // A0 A1 A2 A3 12 13 14 15
126 //--------------------------------------
127 if (color[2][1] == color[1][2] && color[1][1] != color[2][2]) {
128 product2b = color[2][1];
129 product1b = product2b;
130 } else if (color[1][1] == color[2][2] && color[2][1] != color[1][2]) {
131 product2b = color[1][1];
132 product1b = product2b;
133 } else if (color[1][1] == color[2][2] && color[2][1] == color[1][2]) {
136 r += GET_RESULT(color[1][2], color[1][1], color[1][0], color[3][1]);
137 r += GET_RESULT(color[1][2], color[1][1], color[2][0], color[0][1]);
138 r += GET_RESULT(color[1][2], color[1][1], color[3][2], color[2][3]);
139 r += GET_RESULT(color[1][2], color[1][1], color[0][2], color[1][3]);
142 product1b = color[1][2];
144 product1b = color[1][1];
146 product1b = INTERPOLATE(color[1][1], color[1][2]);
148 product2b = product1b;
150 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])
151 product2b = Q_INTERPOLATE(color[2][2], color[2][2], color[2][2], color[2][1]);
152 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])
153 product2b = Q_INTERPOLATE(color[2][1], color[2][1], color[2][1], color[2][2]);
155 product2b = INTERPOLATE(color[2][1], color[2][2]);
157 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])
158 product1b = Q_INTERPOLATE(color[1][2], color[1][2], color[1][2], color[1][1]);
159 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])
160 product1b = Q_INTERPOLATE(color[1][2], color[1][1], color[1][1], color[1][1]);
162 product1b = INTERPOLATE(color[1][1], color[1][2]);
165 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])
166 product2a = INTERPOLATE(color[2][1], color[1][1]);
167 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])
168 product2a = INTERPOLATE(color[2][1], color[1][1]);
170 product2a = color[2][1];
172 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])
173 product1a = INTERPOLATE(color[2][1], color[1][1]);
174 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])
175 product1a = INTERPOLATE(color[2][1], color[1][1]);
177 product1a = color[1][1];
179 /* Set the calculated pixels */
182 AV_WN32A(dst_line[0] + x * 8, product1a);
183 AV_WN32A(dst_line[0] + x * 8 + 4, product1b);
184 AV_WN32A(dst_line[1] + x * 8, product2a);
185 AV_WN32A(dst_line[1] + x * 8 + 4, product2b);
188 AV_WL24(dst_line[0] + x * 6, product1a);
189 AV_WL24(dst_line[0] + x * 6 + 3, product1b);
190 AV_WL24(dst_line[1] + x * 6, product2a);
191 AV_WL24(dst_line[1] + x * 6 + 3, product2b);
195 AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
196 AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
198 AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
199 AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
203 /* Move color matrix forward */
204 color[0][0] = color[0][1]; color[0][1] = color[0][2]; color[0][2] = color[0][3];
205 color[1][0] = color[1][1]; color[1][1] = color[1][2]; color[1][2] = color[1][3];
206 color[2][0] = color[2][1]; color[2][1] = color[2][2]; color[2][2] = color[2][3];
207 color[3][0] = color[3][1]; color[3][1] = color[3][2]; color[3][2] = color[3][3];
213 READ_COLOR4(color[0][3], src_line[0], x);
214 READ_COLOR4(color[1][3], src_line[1], x);
215 READ_COLOR4(color[2][3], src_line[2], x);
216 READ_COLOR4(color[3][3], src_line[3], x);
219 READ_COLOR3(color[0][3], src_line[0], x);
220 READ_COLOR3(color[1][3], src_line[1], x);
221 READ_COLOR3(color[2][3], src_line[2], x);
222 READ_COLOR3(color[3][3], src_line[3], x);
224 default: /* case 2 */
225 READ_COLOR2(color[0][3], src_line[0], x);
226 READ_COLOR2(color[1][3], src_line[1], x);
227 READ_COLOR2(color[2][3], src_line[2], x);
228 READ_COLOR2(color[3][3], src_line[3], x);
234 /* We're done with one line, so we shift the source lines up */
235 src_line[0] = src_line[1];
236 src_line[1] = src_line[2];
237 src_line[2] = src_line[3];
240 src_line[3] = src_line[2];
242 src_line[3] += src_linesize;
248 static int query_formats(AVFilterContext *ctx)
250 static const enum AVPixelFormat pix_fmts[] = {
251 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
252 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
253 AV_PIX_FMT_RGB565BE, AV_PIX_FMT_BGR565BE, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_BGR555BE,
254 AV_PIX_FMT_RGB565LE, AV_PIX_FMT_BGR565LE, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_BGR555LE,
258 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
260 return AVERROR(ENOMEM);
261 return ff_set_common_formats(ctx, fmts_list);
264 static int config_input(AVFilterLink *inlink)
266 Super2xSaIContext *s = inlink->dst->priv;
268 s->hi_pixel_mask = 0xFEFEFEFE;
269 s->lo_pixel_mask = 0x01010101;
270 s->q_hi_pixel_mask = 0xFCFCFCFC;
271 s->q_lo_pixel_mask = 0x03030303;
274 switch (inlink->format) {
275 case AV_PIX_FMT_RGB24:
276 case AV_PIX_FMT_BGR24:
280 case AV_PIX_FMT_RGB565BE:
281 case AV_PIX_FMT_BGR565BE:
283 case AV_PIX_FMT_RGB565LE:
284 case AV_PIX_FMT_BGR565LE:
285 s->hi_pixel_mask = 0xF7DEF7DE;
286 s->lo_pixel_mask = 0x08210821;
287 s->q_hi_pixel_mask = 0xE79CE79C;
288 s->q_lo_pixel_mask = 0x18631863;
292 case AV_PIX_FMT_BGR555BE:
293 case AV_PIX_FMT_RGB555BE:
295 case AV_PIX_FMT_BGR555LE:
296 case AV_PIX_FMT_RGB555LE:
297 s->hi_pixel_mask = 0x7BDE7BDE;
298 s->lo_pixel_mask = 0x04210421;
299 s->q_hi_pixel_mask = 0x739C739C;
300 s->q_lo_pixel_mask = 0x0C630C63;
308 static int config_output(AVFilterLink *outlink)
310 AVFilterLink *inlink = outlink->src->inputs[0];
312 outlink->w = inlink->w*2;
313 outlink->h = inlink->h*2;
315 av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
316 av_get_pix_fmt_name(inlink->format),
317 inlink->w, inlink->h, outlink->w, outlink->h);
322 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
324 AVFilterContext *ctx = inlink->dst;
325 AVFilterLink *outlink = ctx->outputs[0];
327 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
330 return AVERROR(ENOMEM);
332 av_frame_copy_props(out, in);
333 out->width = outlink->w;
334 out->height = outlink->h;
336 td.in = in, td.out = out;
337 ctx->internal->execute(ctx, super2xsai, &td, NULL, FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
340 return ff_filter_frame(outlink, out);
343 static const AVFilterPad super2xsai_inputs[] = {
346 .type = AVMEDIA_TYPE_VIDEO,
347 .config_props = config_input,
348 .filter_frame = filter_frame,
353 static const AVFilterPad super2xsai_outputs[] = {
356 .type = AVMEDIA_TYPE_VIDEO,
357 .config_props = config_output,
362 AVFilter ff_vf_super2xsai = {
363 .name = "super2xsai",
364 .description = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
365 .priv_size = sizeof(Super2xSaIContext),
366 .query_formats = query_formats,
367 .inputs = super2xsai_inputs,
368 .outputs = super2xsai_outputs,
369 .flags = AVFILTER_FLAG_SLICE_THREADS,