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