2b1e6ff53e8d284ab87c1e19027e8e9194846022
[ffmpeg.git] / libavfilter / vf_xbr.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * Copyright (c) 2011, 2012 Hyllian/Jararaca <sergiogdb@gmail.com>
5  * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * XBR Filter is used for depixelization of image.
25  * This is based on Hyllian's xBR shader.
26  *
27  * @see http://www.libretro.com/forums/viewtopic.php?f=6&t=134
28  * @see https://github.com/yoyofr/iFBA/blob/master/fba_src/src/intf/video/scalers/xbr.cpp
29  */
30
31 #include "libavutil/opt.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/pixdesc.h"
34 #include "internal.h"
35
36 #define RGB_MASK      0x00FFFFFF
37 #define LB_MASK       0x00FEFEFE
38 #define RED_BLUE_MASK 0x00FF00FF
39 #define GREEN_MASK    0x0000FF00
40
41 typedef int (*xbrfunc_t)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
42
43 typedef struct {
44     const AVClass *class;
45     int n;
46     xbrfunc_t func;
47     uint32_t rgbtoyuv[1<<24];
48 } XBRContext;
49
50 typedef struct ThreadData {
51     AVFrame *in, *out;
52     const uint32_t *rgbtoyuv;
53 } ThreadData;
54
55 #define OFFSET(x) offsetof(XBRContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57 static const AVOption xbr_options[] = {
58     { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 4, .flags = FLAGS },
59     { NULL }
60 };
61
62 AVFILTER_DEFINE_CLASS(xbr);
63
64 static uint32_t pixel_diff(uint32_t x, uint32_t y, const uint32_t *r2y)
65 {
66 #define YMASK 0xff0000
67 #define UMASK 0x00ff00
68 #define VMASK 0x0000ff
69
70     uint32_t yuv1 = r2y[x & 0xffffff];
71     uint32_t yuv2 = r2y[y & 0xffffff];
72
73     return (abs((yuv1 & YMASK) - (yuv2 & YMASK)) >> 16) +
74            (abs((yuv1 & UMASK) - (yuv2 & UMASK)) >>  8) +
75            abs((yuv1 & VMASK) - (yuv2 & VMASK));
76 }
77
78 #define ALPHA_BLEND_128_W(dst, src) dst = ((src & LB_MASK) >> 1) + ((dst & LB_MASK) >> 1)
79 #define ALPHA_BLEND_BASE(a, b, m, s) (  (RED_BLUE_MASK & (((a) & RED_BLUE_MASK) + (((((b) & RED_BLUE_MASK) - ((a) & RED_BLUE_MASK)) * (m)) >> (s)))) \
80                                       | (GREEN_MASK    & (((a) & GREEN_MASK)    + (((((b) & GREEN_MASK)    - ((a) & GREEN_MASK))    * (m)) >> (s)))))
81 #define ALPHA_BLEND_32_W(dst, src)  dst = ALPHA_BLEND_BASE(dst, src, 1, 3)
82 #define ALPHA_BLEND_64_W(dst, src)  dst = ALPHA_BLEND_BASE(dst, src, 1, 2)
83 #define ALPHA_BLEND_192_W(dst, src) dst = ALPHA_BLEND_BASE(dst, src, 3, 2)
84 #define ALPHA_BLEND_224_W(dst, src) dst = ALPHA_BLEND_BASE(dst, src, 7, 3)
85
86 #define df(A, B) pixel_diff(A, B, r2y)
87 #define eq(A, B) (df(A, B) < 155)
88
89 #define FILT2(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1,   \
90               N0, N1, N2, N3) do {                                                                  \
91     if (PE != PH && PE != PF) {                                                                     \
92         const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2);          \
93         const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2);          \
94         if (e < i && (!eq(PF,PB) && !eq(PH,PD) || eq(PE,PI)                                         \
95                       && (!eq(PF,I4) && !eq(PH,I5))                                                 \
96                       || eq(PE,PG) || eq(PE,PC))) {                                                 \
97             const unsigned ke = df(PF,PG);                                                          \
98             const unsigned ki = df(PH,PC);                                                          \
99             const int left    = ke<<1 <= ki && PE != PG && PD != PG;                                \
100             const int up      = ke >= ki<<1 && PE != PC && PB != PC;                                \
101             const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH;                                   \
102             if (left && up) {                                                                       \
103                 ALPHA_BLEND_224_W(E[N3], px);                                                       \
104                 ALPHA_BLEND_64_W( E[N2], px);                                                       \
105                 E[N1] = E[N2];                                                                      \
106             } else if (left) {                                                                      \
107                 ALPHA_BLEND_192_W(E[N3], px);                                                       \
108                 ALPHA_BLEND_64_W( E[N2], px);                                                       \
109             } else if (up) {                                                                        \
110                 ALPHA_BLEND_192_W(E[N3], px);                                                       \
111                 ALPHA_BLEND_64_W( E[N1], px);                                                       \
112             } else { /* diagonal */                                                                 \
113                 ALPHA_BLEND_128_W(E[N3], px);                                                       \
114             }                                                                                       \
115         } else if (e <= i) {                                                                        \
116             ALPHA_BLEND_128_W( E[N3], ((df(PE,PF) <= df(PE,PH)) ? PF : PH));                        \
117         }                                                                                           \
118     }                                                                                               \
119 } while (0)
120
121 #define FILT3(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1,   \
122               N0, N1, N2, N3, N4, N5, N6, N7, N8) do {                                              \
123     if (PE != PH && PE != PF) {                                                                     \
124         const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2);          \
125         const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2);          \
126         if (e < i && (!eq(PF,PB) && !eq(PF,PC) || !eq(PH,PD) && !eq(PH,PG) || eq(PE,PI)             \
127                       && (!eq(PF,F4) && !eq(PF,I4) || !eq(PH,H5) && !eq(PH,I5))                     \
128                       || eq(PE,PG) || eq(PE,PC))) {                                                 \
129             const unsigned ke = df(PF,PG);                                                          \
130             const unsigned ki = df(PH,PC);                                                          \
131             const int left    = ke<<1 <= ki && PE != PG && PD != PG;                                \
132             const int up      = ke >= ki<<1 && PE != PC && PB != PC;                                \
133             const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH;                                   \
134             if (left && up) {                                                                       \
135                 ALPHA_BLEND_192_W(E[N7], px);                                                       \
136                 ALPHA_BLEND_64_W( E[N6], px);                                                       \
137                 E[N5] = E[N7];                                                                      \
138                 E[N2] = E[N6];                                                                      \
139                 E[N8] = px;                                                                         \
140             } else if (left) {                                                                      \
141                 ALPHA_BLEND_192_W(E[N7], px);                                                       \
142                 ALPHA_BLEND_64_W( E[N5], px);                                                       \
143                 ALPHA_BLEND_64_W( E[N6], px);                                                       \
144                 E[N8] = px;                                                                         \
145             } else if (up) {                                                                        \
146                 ALPHA_BLEND_192_W(E[N5], px);                                                       \
147                 ALPHA_BLEND_64_W( E[N7], px);                                                       \
148                 ALPHA_BLEND_64_W( E[N2], px);                                                       \
149                 E[N8] = px;                                                                         \
150             } else { /* diagonal */                                                                 \
151                 ALPHA_BLEND_224_W(E[N8], px);                                                       \
152                 ALPHA_BLEND_32_W( E[N5], px);                                                       \
153                 ALPHA_BLEND_32_W( E[N7], px);                                                       \
154             }                                                                                       \
155         } else if (e <= i) {                                                                        \
156             ALPHA_BLEND_128_W(E[N8], ((df(PE,PF) <= df(PE,PH)) ? PF : PH));                         \
157         }                                                                                           \
158     }                                                                                               \
159 } while (0)
160
161 #define FILT4(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1,   \
162               N15, N14, N11, N3, N7, N10, N13, N12, N9, N6, N2, N1, N5, N8, N4, N0) do {            \
163     if (PE != PH && PE != PF) {                                                                     \
164         const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2);          \
165         const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2);          \
166         if (e < i && (!eq(PF,PB) && !eq(PH,PD) || eq(PE,PI)                                         \
167                       && (!eq(PF,I4) && !eq(PH,I5))                                                 \
168                       || eq(PE,PG) || eq(PE,PC))) {                                                 \
169             const unsigned ke = df(PF,PG);                                                          \
170             const unsigned ki = df(PH,PC);                                                          \
171             const int left    = ke<<1 <= ki && PE != PG && PD != PG;                                \
172             const int up      = ke >= ki<<1 && PE != PC && PB != PC;                                \
173             const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH;                                   \
174             if (left && up) {                                                                       \
175                 ALPHA_BLEND_192_W(E[N13], px);                                                      \
176                 ALPHA_BLEND_64_W( E[N12], px);                                                      \
177                 E[N15] = E[N14] = E[N11] = px;                                                      \
178                 E[N10] = E[N3]  = E[N12];                                                           \
179                 E[N7]  = E[N13];                                                                    \
180             } else if (left) {                                                                      \
181                 ALPHA_BLEND_192_W(E[N11], px);                                                      \
182                 ALPHA_BLEND_192_W(E[N13], px);                                                      \
183                 ALPHA_BLEND_64_W( E[N10], px);                                                      \
184                 ALPHA_BLEND_64_W( E[N12], px);                                                      \
185                 E[N14] = px;                                                                        \
186                 E[N15] = px;                                                                        \
187             } else if (up) {                                                                        \
188                 ALPHA_BLEND_192_W(E[N14], px);                                                      \
189                 ALPHA_BLEND_192_W(E[N7 ], px);                                                      \
190                 ALPHA_BLEND_64_W( E[N10], px);                                                      \
191                 ALPHA_BLEND_64_W( E[N3 ], px);                                                      \
192                 E[N11] = px;                                                                        \
193                 E[N15] = px;                                                                        \
194             } else { /* diagonal */                                                                 \
195                 ALPHA_BLEND_128_W(E[N11], px);                                                      \
196                 ALPHA_BLEND_128_W(E[N14], px);                                                      \
197                 E[N15] = px;                                                                        \
198             }                                                                                       \
199         } else if (e <= i) {                                                                        \
200             ALPHA_BLEND_128_W( E[N15], ((df(PE,PF) <= df(PE,PH)) ? PF : PH));                       \
201         }                                                                                           \
202     }                                                                                               \
203 } while (0)
204
205 static av_always_inline void xbr_filter(const ThreadData *td, int jobnr, int nb_jobs, int n)
206 {
207     int x, y;
208     const AVFrame *input = td->in;
209     AVFrame *output = td->out;
210     const uint32_t *r2y = td->rgbtoyuv;
211     const int slice_start = (input->height *  jobnr   ) / nb_jobs;
212     const int slice_end   = (input->height * (jobnr+1)) / nb_jobs;
213     const int nl = output->linesize[0] >> 2;
214     const int nl1 = nl + nl;
215     const int nl2 = nl1 + nl;
216
217     for (y = slice_start; y < slice_end; y++) {
218
219         uint32_t *E = (uint32_t *)(output->data[0] + y * output->linesize[0] * n);
220         const uint32_t *sa2 = (uint32_t *)(input->data[0] + y * input->linesize[0] - 8); /* center */
221         const uint32_t *sa1 = sa2 - (input->linesize[0]>>2); /* up x1 */
222         const uint32_t *sa0 = sa1 - (input->linesize[0]>>2); /* up x2 */
223         const uint32_t *sa3 = sa2 + (input->linesize[0]>>2); /* down x1 */
224         const uint32_t *sa4 = sa3 + (input->linesize[0]>>2); /* down x2 */
225
226         if (y <= 1) {
227             sa0 = sa1;
228             if (y == 0) {
229                 sa0 = sa1 = sa2;
230             }
231         }
232
233         if (y >= input->height - 2) {
234             sa4 = sa3;
235             if (y == input->height - 1) {
236                 sa4 = sa3 = sa2;
237             }
238         }
239
240         for (x = 0; x < input->width; x++) {
241             const uint32_t B1 = sa0[2];
242             const uint32_t PB = sa1[2];
243             const uint32_t PE = sa2[2];
244             const uint32_t PH = sa3[2];
245             const uint32_t H5 = sa4[2];
246
247             const int pprev = 2 - (x > 0);
248             const uint32_t A1 = sa0[pprev];
249             const uint32_t PA = sa1[pprev];
250             const uint32_t PD = sa2[pprev];
251             const uint32_t PG = sa3[pprev];
252             const uint32_t G5 = sa4[pprev];
253
254             const int pprev2 = pprev - (x > 1);
255             const uint32_t A0 = sa1[pprev2];
256             const uint32_t D0 = sa2[pprev2];
257             const uint32_t G0 = sa3[pprev2];
258
259             const int pnext = 3 - (x == input->width - 1);
260             const uint32_t C1 = sa0[pnext];
261             const uint32_t PC = sa1[pnext];
262             const uint32_t PF = sa2[pnext];
263             const uint32_t PI = sa3[pnext];
264             const uint32_t I5 = sa4[pnext];
265
266             const int pnext2 = pnext + 1 - (x >= input->width - 2);
267             const uint32_t C4 = sa1[pnext2];
268             const uint32_t F4 = sa2[pnext2];
269             const uint32_t I4 = sa3[pnext2];
270
271             if (n == 2) {
272                 E[0]  = E[1]      =     // 0, 1
273                 E[nl] = E[nl + 1] = PE; // 2, 3
274
275                 FILT2(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, 0, 1, nl, nl+1);
276                 FILT2(PE, PC, PF, PB, PI, PA, PH, PD, PG, I4, A1, I5, H5, A0, D0, B1, C1, F4, C4, G5, G0, nl, 0, nl+1, 1);
277                 FILT2(PE, PA, PB, PD, PC, PG, PF, PH, PI, C1, G0, C4, F4, G5, H5, D0, A0, B1, A1, I4, I5, nl+1, nl, 1, 0);
278                 FILT2(PE, PG, PD, PH, PA, PI, PB, PF, PC, A0, I5, A1, B1, I4, F4, H5, G5, D0, G0, C1, C4, 1, nl+1, 0, nl);
279             } else if (n == 3) {
280                 E[0]   = E[1]     = E[2]     =     // 0, 1, 2
281                 E[nl]  = E[nl+1]  = E[nl+2]  =     // 3, 4, 5
282                 E[nl1] = E[nl1+1] = E[nl1+2] = PE; // 6, 7, 8
283
284                 FILT3(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, 0, 1, 2, nl, nl+1, nl+2, nl1, nl1+1, nl1+2);
285                 FILT3(PE, PC, PF, PB, PI, PA, PH, PD, PG, I4, A1, I5, H5, A0, D0, B1, C1, F4, C4, G5, G0, nl1, nl, 0, nl1+1, nl+1, 1, nl1+2, nl+2, 2);
286                 FILT3(PE, PA, PB, PD, PC, PG, PF, PH, PI, C1, G0, C4, F4, G5, H5, D0, A0, B1, A1, I4, I5, nl1+2, nl1+1, nl1, nl+2, nl+1, nl, 2, 1, 0);
287                 FILT3(PE, PG, PD, PH, PA, PI, PB, PF, PC, A0, I5, A1, B1, I4, F4, H5, G5, D0, G0, C1, C4, 2, nl+2, nl1+2, 1, nl+1, nl1+1, 0, nl, nl1);
288             } else if (n == 4) {
289                 E[0]   = E[1]     = E[2]     = E[3]     =     //  0,  1,  2,  3
290                 E[nl]  = E[nl+1]  = E[nl+2]  = E[nl+3]  =     //  4,  5,  6,  7
291                 E[nl1] = E[nl1+1] = E[nl1+2] = E[nl1+3] =     //  8,  9, 10, 11
292                 E[nl2] = E[nl2+1] = E[nl2+2] = E[nl2+3] = PE; // 12, 13, 14, 15
293
294                 FILT4(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, nl2+3, nl2+2, nl1+3, 3, nl+3, nl1+2, nl2+1, nl2, nl1+1, nl+2, 2, 1, nl+1, nl1, nl, 0);
295                 FILT4(PE, PC, PF, PB, PI, PA, PH, PD, PG, I4, A1, I5, H5, A0, D0, B1, C1, F4, C4, G5, G0, 3, nl+3, 2, 0, 1, nl+2, nl1+3, nl2+3, nl1+2, nl+1, nl, nl1, nl1+1, nl2+2, nl2+1, nl2);
296                 FILT4(PE, PA, PB, PD, PC, PG, PF, PH, PI, C1, G0, C4, F4, G5, H5, D0, A0, B1, A1, I4, I5, 0, 1, nl, nl2, nl1, nl+1, 2, 3, nl+2, nl1+1, nl2+1, nl2+2, nl1+2, nl+3, nl1+3, nl2+3);
297                 FILT4(PE, PG, PD, PH, PA, PI, PB, PF, PC, A0, I5, A1, B1, I4, F4, H5, G5, D0, G0, C1, C4, nl2, nl1, nl2+1, nl2+3, nl2+2, nl1+1, nl, 0, nl+1, nl1+2, nl1+3, nl+3, nl+2, 1, 2, 3);
298             }
299
300             sa0 += 1;
301             sa1 += 1;
302             sa2 += 1;
303             sa3 += 1;
304             sa4 += 1;
305
306             E += n;
307         }
308     }
309 }
310
311 #define XBR_FUNC(size) \
312 static int xbr##size##x(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
313 { \
314     xbr_filter(arg, jobnr, nb_jobs, size); \
315     return 0; \
316 }
317
318 XBR_FUNC(2)
319 XBR_FUNC(3)
320 XBR_FUNC(4)
321
322
323 static int config_output(AVFilterLink *outlink)
324 {
325     AVFilterContext *ctx = outlink->src;
326     XBRContext *xbr = ctx->priv;
327     AVFilterLink *inlink = ctx->inputs[0];
328
329     outlink->w = inlink->w * xbr->n;
330     outlink->h = inlink->h * xbr->n;
331     return 0;
332 }
333
334 static int query_formats(AVFilterContext *ctx)
335 {
336     static const enum AVPixelFormat pix_fmts[] = {
337         AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE,
338     };
339
340     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
341     return 0;
342 }
343
344 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
345 {
346     AVFilterContext *ctx = inlink->dst;
347     AVFilterLink *outlink = ctx->outputs[0];
348     XBRContext *xbr = ctx->priv;
349     ThreadData td;
350
351     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
352     if (!out) {
353         av_frame_free(&in);
354         return AVERROR(ENOMEM);
355     }
356
357     av_frame_copy_props(out, in);
358
359     td.in = in;
360     td.out = out;
361     td.rgbtoyuv = xbr->rgbtoyuv;
362     ctx->internal->execute(ctx, xbr->func, &td, NULL, FFMIN(inlink->h, ctx->graph->nb_threads));
363
364     out->width  = outlink->w;
365     out->height = outlink->h;
366
367     av_frame_free(&in);
368     return ff_filter_frame(outlink, out);
369 }
370
371 static int init(AVFilterContext *ctx)
372 {
373     XBRContext *xbr = ctx->priv;
374     static const xbrfunc_t xbrfuncs[] = {xbr2x, xbr3x, xbr4x};
375
376     uint32_t c;
377     int bg, rg, g;
378
379     for (bg = -255; bg < 256; bg++) {
380         for (rg = -255; rg < 256; rg++) {
381             const uint32_t u = (uint32_t)((-169*rg + 500*bg)/1000) + 128;
382             const uint32_t v = (uint32_t)(( 500*rg -  81*bg)/1000) + 128;
383             int startg = FFMAX3(-bg, -rg, 0);
384             int endg = FFMIN3(255-bg, 255-rg, 255);
385             uint32_t y = (uint32_t)(( 299*rg + 1000*startg + 114*bg)/1000);
386             c = bg + (rg<<16) + 0x010101 * startg;
387             for (g = startg; g <= endg; g++) {
388                 xbr->rgbtoyuv[c] = ((y++) << 16) + (u << 8) + v;
389                 c+= 0x010101;
390             }
391         }
392     }
393
394     xbr->func = xbrfuncs[xbr->n - 2];
395     return 0;
396 }
397
398 static const AVFilterPad xbr_inputs[] = {
399     {
400         .name         = "default",
401         .type         = AVMEDIA_TYPE_VIDEO,
402         .filter_frame = filter_frame,
403     },
404     { NULL }
405 };
406
407 static const AVFilterPad xbr_outputs[] = {
408     {
409         .name         = "default",
410         .type         = AVMEDIA_TYPE_VIDEO,
411         .config_props = config_output,
412     },
413     { NULL }
414 };
415
416 AVFilter ff_vf_xbr = {
417     .name          = "xbr",
418     .description   = NULL_IF_CONFIG_SMALL("Scale the input using xBR algorithm."),
419     .inputs        = xbr_inputs,
420     .outputs       = xbr_outputs,
421     .query_formats = query_formats,
422     .priv_size     = sizeof(XBRContext),
423     .priv_class    = &xbr_class,
424     .init          = init,
425     .flags         = AVFILTER_FLAG_SLICE_THREADS,
426 };