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
248 static const enum AVPixelFormat pix_fmts[] = {
249 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
250 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
251 AV_PIX_FMT_RGB565BE, AV_PIX_FMT_BGR565BE, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_BGR555BE,
252 AV_PIX_FMT_RGB565LE, AV_PIX_FMT_BGR565LE, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_BGR555LE,
253 AV_PIX_FMT_NONE
254 };
255
config_input(AVFilterLink * inlink)256 static int config_input(AVFilterLink *inlink)
257 {
258 Super2xSaIContext *s = inlink->dst->priv;
259
260 s->hi_pixel_mask = 0xFEFEFEFE;
261 s->lo_pixel_mask = 0x01010101;
262 s->q_hi_pixel_mask = 0xFCFCFCFC;
263 s->q_lo_pixel_mask = 0x03030303;
264 s->bpp = 4;
265
266 switch (inlink->format) {
267 case AV_PIX_FMT_RGB24:
268 case AV_PIX_FMT_BGR24:
269 s->bpp = 3;
270 break;
271
272 case AV_PIX_FMT_RGB565BE:
273 case AV_PIX_FMT_BGR565BE:
274 s->is_be = 1;
275 case AV_PIX_FMT_RGB565LE:
276 case AV_PIX_FMT_BGR565LE:
277 s->hi_pixel_mask = 0xF7DEF7DE;
278 s->lo_pixel_mask = 0x08210821;
279 s->q_hi_pixel_mask = 0xE79CE79C;
280 s->q_lo_pixel_mask = 0x18631863;
281 s->bpp = 2;
282 break;
283
284 case AV_PIX_FMT_BGR555BE:
285 case AV_PIX_FMT_RGB555BE:
286 s->is_be = 1;
287 case AV_PIX_FMT_BGR555LE:
288 case AV_PIX_FMT_RGB555LE:
289 s->hi_pixel_mask = 0x7BDE7BDE;
290 s->lo_pixel_mask = 0x04210421;
291 s->q_hi_pixel_mask = 0x739C739C;
292 s->q_lo_pixel_mask = 0x0C630C63;
293 s->bpp = 2;
294 break;
295 }
296
297 return 0;
298 }
299
config_output(AVFilterLink * outlink)300 static int config_output(AVFilterLink *outlink)
301 {
302 AVFilterLink *inlink = outlink->src->inputs[0];
303
304 outlink->w = inlink->w*2;
305 outlink->h = inlink->h*2;
306
307 av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
308 av_get_pix_fmt_name(inlink->format),
309 inlink->w, inlink->h, outlink->w, outlink->h);
310
311 return 0;
312 }
313
filter_frame(AVFilterLink * inlink,AVFrame * in)314 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
315 {
316 AVFilterContext *ctx = inlink->dst;
317 AVFilterLink *outlink = ctx->outputs[0];
318 ThreadData td;
319 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
320 if (!out) {
321 av_frame_free(&in);
322 return AVERROR(ENOMEM);
323 }
324 av_frame_copy_props(out, in);
325 out->width = outlink->w;
326 out->height = outlink->h;
327
328 td.in = in, td.out = out;
329 ff_filter_execute(ctx, super2xsai, &td, NULL,
330 FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
331
332 av_frame_free(&in);
333 return ff_filter_frame(outlink, out);
334 }
335
336 static const AVFilterPad super2xsai_inputs[] = {
337 {
338 .name = "default",
339 .type = AVMEDIA_TYPE_VIDEO,
340 .config_props = config_input,
341 .filter_frame = filter_frame,
342 },
343 };
344
345 static const AVFilterPad super2xsai_outputs[] = {
346 {
347 .name = "default",
348 .type = AVMEDIA_TYPE_VIDEO,
349 .config_props = config_output,
350 },
351 };
352
353 const AVFilter ff_vf_super2xsai = {
354 .name = "super2xsai",
355 .description = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
356 .priv_size = sizeof(Super2xSaIContext),
357 FILTER_INPUTS(super2xsai_inputs),
358 FILTER_OUTPUTS(super2xsai_outputs),
359 FILTER_PIXFMTS_ARRAY(pix_fmts),
360 .flags = AVFILTER_FLAG_SLICE_THREADS,
361 };
362