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 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct Super2xSaIContext {
37 /* masks used for two pixels interpolation */
38 uint32_t hi_pixel_mask;
39 uint32_t lo_pixel_mask;
40
41 /* masks used for four pixels interpolation */
42 uint32_t q_hi_pixel_mask;
43 uint32_t q_lo_pixel_mask;
44
45 int bpp; ///< bytes per pixel, pixel stride for each (packed) pixel
46 int is_be;
47 } Super2xSaIContext;
48
49 typedef struct ThreadData {
50 AVFrame *in, *out;
51 } ThreadData;
52
53 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
54
55 #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
56
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)
59
super2xsai(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)60 static int super2xsai(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
61 {
62 Super2xSaIContext *s = ctx->priv;
63 ThreadData *td = arg;
64 AVFrame *in = td->in;
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;
72 unsigned int x, y;
73 uint32_t color[4][4];
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;
82
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);
88
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)
92
93 for (y = slice_start; y < slice_end; y++) {
94 uint8_t *dst_line[2];
95
96 dst_line[0] = dst + dst_linesize*2*y;
97 dst_line[1] = dst + dst_linesize*(2*y+1);
98
99 switch (bpp) {
100 case 4:
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);
105 break;
106 case 3:
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);
111 break;
112 default:
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);
117 }
118
119 for (x = 0; x < width; x++) {
120 uint32_t product1a, product1b, product2a, product2b;
121
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]) {
134 int r = 0;
135
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]);
140
141 if (r > 0)
142 product1b = color[1][2];
143 else if (r < 0)
144 product1b = color[1][1];
145 else
146 product1b = INTERPOLATE(color[1][1], color[1][2]);
147
148 product2b = product1b;
149 } else {
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]);
154 else
155 product2b = INTERPOLATE(color[2][1], color[2][2]);
156
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]);
161 else
162 product1b = INTERPOLATE(color[1][1], color[1][2]);
163 }
164
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]);
169 else
170 product2a = color[2][1];
171
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]);
176 else
177 product1a = color[1][1];
178
179 /* Set the calculated pixels */
180 switch (bpp) {
181 case 4:
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);
186 break;
187 case 3:
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);
192 break;
193 default: // bpp = 2
194 if (s->is_be) {
195 AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
196 AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
197 } else {
198 AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
199 AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
200 }
201 }
202
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];
208
209 if (x < width - 3) {
210 x += 3;
211 switch (bpp) {
212 case 4:
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);
217 break;
218 case 3:
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);
223 break;
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);
229 }
230 x -= 3;
231 }
232 }
233
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];
238
239 /* Read next line */
240 src_line[3] = src_line[2];
241 if (y < height - 3)
242 src_line[3] += src_linesize;
243 } // y loop
244
245 return 0;
246 }
247
query_formats(AVFilterContext * ctx)248 static int query_formats(AVFilterContext *ctx)
249 {
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,
255 AV_PIX_FMT_NONE
256 };
257
258 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
259 if (!fmts_list)
260 return AVERROR(ENOMEM);
261 return ff_set_common_formats(ctx, fmts_list);
262 }
263
config_input(AVFilterLink * inlink)264 static int config_input(AVFilterLink *inlink)
265 {
266 Super2xSaIContext *s = inlink->dst->priv;
267
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;
272 s->bpp = 4;
273
274 switch (inlink->format) {
275 case AV_PIX_FMT_RGB24:
276 case AV_PIX_FMT_BGR24:
277 s->bpp = 3;
278 break;
279
280 case AV_PIX_FMT_RGB565BE:
281 case AV_PIX_FMT_BGR565BE:
282 s->is_be = 1;
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;
289 s->bpp = 2;
290 break;
291
292 case AV_PIX_FMT_BGR555BE:
293 case AV_PIX_FMT_RGB555BE:
294 s->is_be = 1;
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;
301 s->bpp = 2;
302 break;
303 }
304
305 return 0;
306 }
307
config_output(AVFilterLink * outlink)308 static int config_output(AVFilterLink *outlink)
309 {
310 AVFilterLink *inlink = outlink->src->inputs[0];
311
312 outlink->w = inlink->w*2;
313 outlink->h = inlink->h*2;
314
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);
318
319 return 0;
320 }
321
filter_frame(AVFilterLink * inlink,AVFrame * in)322 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
323 {
324 AVFilterContext *ctx = inlink->dst;
325 AVFilterLink *outlink = ctx->outputs[0];
326 ThreadData td;
327 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
328 if (!out) {
329 av_frame_free(&in);
330 return AVERROR(ENOMEM);
331 }
332 av_frame_copy_props(out, in);
333 out->width = outlink->w;
334 out->height = outlink->h;
335
336 td.in = in, td.out = out;
337 ctx->internal->execute(ctx, super2xsai, &td, NULL, FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
338
339 av_frame_free(&in);
340 return ff_filter_frame(outlink, out);
341 }
342
343 static const AVFilterPad super2xsai_inputs[] = {
344 {
345 .name = "default",
346 .type = AVMEDIA_TYPE_VIDEO,
347 .config_props = config_input,
348 .filter_frame = filter_frame,
349 },
350 { NULL }
351 };
352
353 static const AVFilterPad super2xsai_outputs[] = {
354 {
355 .name = "default",
356 .type = AVMEDIA_TYPE_VIDEO,
357 .config_props = config_output,
358 },
359 { NULL }
360 };
361
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,
370 };
371