2 * This file is part of FFmpeg.
4 * Copyright (C) 2011, 2012 Hyllian/Jararaca - sergiogdb@gmail.com
6 * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * XBR Filter is used for depixelization of image.
26 * This is based on Hyllian's xBR shader.
28 * @see http://www.libretro.com/forums/viewtopic.php?f=6&t=134
29 * @see https://github.com/yoyofr/iFBA/blob/master/fba_src/src/intf/video/scalers/xbr.cpp
31 * @todo add threading and FATE test
34 #include "libavutil/opt.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/pixdesc.h"
39 #define RGB_MASK 0x00FFFFFF
40 #define LB_MASK 0x00FEFEFE
41 #define RED_BLUE_MASK 0x00FF00FF
42 #define GREEN_MASK 0x0000FF00
47 uint32_t rgbtoyuv[1<<24];
50 #define OFFSET(x) offsetof(XBRContext, x)
51 static const AVOption xbr_options[] = {
52 { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 4, },
56 AVFILTER_DEFINE_CLASS(xbr);
58 static uint32_t pixel_diff(uint32_t x, uint32_t y, const uint32_t *r2y)
60 #define YMASK 0xff0000
61 #define UMASK 0x00ff00
62 #define VMASK 0x0000ff
64 uint32_t yuv1 = r2y[x & 0xffffff];
65 uint32_t yuv2 = r2y[y & 0xffffff];
67 return (abs((yuv1 & YMASK) - (yuv2 & YMASK)) >> 16) +
68 (abs((yuv1 & UMASK) - (yuv2 & UMASK)) >> 8) +
69 abs((yuv1 & VMASK) - (yuv2 & VMASK));
72 #define ALPHA_BLEND_128_W(dst, src) dst = ((src & LB_MASK) >> 1) + ((dst & LB_MASK) >> 1)
74 #define ALPHA_BLEND_32_W(dst, src) \
75 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
76 (dst & RED_BLUE_MASK))) >> 3))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
77 ((((src & GREEN_MASK) - (dst & GREEN_MASK))) >> 3))))
79 #define ALPHA_BLEND_64_W(dst, src) \
80 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
81 (dst & RED_BLUE_MASK))) >> 2))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
82 ((((src & GREEN_MASK) - (dst & GREEN_MASK))) >> 2))))
84 #define ALPHA_BLEND_192_W(dst, src) \
85 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
86 (dst & RED_BLUE_MASK)) * 3) >> 2))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
87 ((((src & GREEN_MASK) - (dst & GREEN_MASK)) * 3) >> 2))))
89 #define ALPHA_BLEND_224_W(dst, src) \
90 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
91 (dst & RED_BLUE_MASK)) * 7) >> 3))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
92 ((((src & GREEN_MASK) - (dst & GREEN_MASK)) * 7) >> 3))))
94 #define df(A, B) pixel_diff(A, B, r2y)
95 #define eq(A, B) (df(A, B) < 155)
97 #define INIT_SRC_DST_POINTERS(level) \
98 uint32_t *E = (uint32_t *)(output->data[0] + y * output->linesize[0] * (level)); \
99 const uint32_t *sa2 = (uint32_t *)(input->data[0] + y * input->linesize[0] - 8); /* center */ \
100 const uint32_t *sa1 = sa2 - (input->linesize[0]>>2); /* up x1 */ \
101 const uint32_t *sa0 = sa1 - (input->linesize[0]>>2); /* up x2 */ \
102 const uint32_t *sa3 = sa2 + (input->linesize[0]>>2); /* down x1 */ \
103 const uint32_t *sa4 = sa3 + (input->linesize[0]>>2); /* down x2 */ \
112 if (y >= input->height - 2) { \
114 if (y == input->height - 1) { \
119 #define INIT_21_PIXELS \
120 const uint32_t B1 = sa0[2]; \
121 const uint32_t PB = sa1[2]; \
122 const uint32_t PE = sa2[2]; \
123 const uint32_t PH = sa3[2]; \
124 const uint32_t H5 = sa4[2]; \
126 const int pprev = 2 - (x > 0); \
127 const uint32_t A1 = sa0[pprev]; \
128 const uint32_t PA = sa1[pprev]; \
129 const uint32_t PD = sa2[pprev]; \
130 const uint32_t PG = sa3[pprev]; \
131 const uint32_t G5 = sa4[pprev]; \
133 const int pprev2 = pprev - (x > 1); \
134 const uint32_t A0 = sa1[pprev2]; \
135 const uint32_t D0 = sa2[pprev2]; \
136 const uint32_t G0 = sa3[pprev2]; \
138 const int pnext = 3 - (x == input->width - 1); \
139 const uint32_t C1 = sa0[pnext]; \
140 const uint32_t PC = sa1[pnext]; \
141 const uint32_t PF = sa2[pnext]; \
142 const uint32_t PI = sa3[pnext]; \
143 const uint32_t I5 = sa4[pnext]; \
145 const int pnext2 = pnext + 1 - (x >= input->width - 2); \
146 const uint32_t C4 = sa1[pnext2]; \
147 const uint32_t F4 = sa2[pnext2]; \
148 const uint32_t I4 = sa3[pnext2];
150 #define FILT2(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, \
151 N0, N1, N2, N3) do { \
152 if (PE != PH && PE != PF) { \
153 const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2); \
154 const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2); \
155 if (e < i && (!eq(PF,PB) && !eq(PH,PD) || eq(PE,PI) \
156 && (!eq(PF,I4) && !eq(PH,I5)) \
157 || eq(PE,PG) || eq(PE,PC))) { \
158 const unsigned ke = df(PF,PG); \
159 const unsigned ki = df(PH,PC); \
160 const int left = ke<<1 <= ki && PE != PG && PD != PG; \
161 const int up = ke >= ki<<1 && PE != PC && PB != PC; \
162 const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH; \
164 ALPHA_BLEND_224_W(E[N3], px); \
165 ALPHA_BLEND_64_W( E[N2], px); \
168 ALPHA_BLEND_192_W(E[N3], px); \
169 ALPHA_BLEND_64_W( E[N2], px); \
171 ALPHA_BLEND_192_W(E[N3], px); \
172 ALPHA_BLEND_64_W( E[N1], px); \
173 } else { /* diagonal */ \
174 ALPHA_BLEND_128_W(E[N3], px); \
176 } else if (e <= i) { \
177 ALPHA_BLEND_128_W( E[N3], ((df(PE,PF) <= df(PE,PH)) ? PF : PH)); \
182 static void xbr2x(AVFrame * input, AVFrame * output, const uint32_t * r2y)
185 const int nl = output->linesize[0] >> 2;
187 for (y = 0; y < input->height; y++) {
188 INIT_SRC_DST_POINTERS(2)
190 for (x = 0; x < input->width; x++) {
193 E[0] = E[1] = E[nl] = E[nl + 1] = PE; // 0, 1, 2, 3
195 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);
196 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);
197 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);
198 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);
211 #define FILT3(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, \
212 N0, N1, N2, N3, N4, N5, N6, N7, N8) do { \
213 if (PE != PH && PE != PF) { \
214 const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2); \
215 const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2); \
216 if (e < i && (!eq(PF,PB) && !eq(PF,PC) || !eq(PH,PD) && !eq(PH,PG) || eq(PE,PI) \
217 && (!eq(PF,F4) && !eq(PF,I4) || !eq(PH,H5) && !eq(PH,I5)) \
218 || eq(PE,PG) || eq(PE,PC))) { \
219 const unsigned ke = df(PF,PG); \
220 const unsigned ki = df(PH,PC); \
221 const int left = ke<<1 <= ki && PE != PG && PD != PG; \
222 const int up = ke >= ki<<1 && PE != PC && PB != PC; \
223 const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH; \
225 ALPHA_BLEND_192_W(E[N7], px); \
226 ALPHA_BLEND_64_W( E[N6], px); \
231 ALPHA_BLEND_192_W(E[N7], px); \
232 ALPHA_BLEND_64_W( E[N5], px); \
233 ALPHA_BLEND_64_W( E[N6], px); \
236 ALPHA_BLEND_192_W(E[N5], px); \
237 ALPHA_BLEND_64_W( E[N7], px); \
238 ALPHA_BLEND_64_W( E[N2], px); \
240 } else { /* diagonal */ \
241 ALPHA_BLEND_224_W(E[N8], px); \
242 ALPHA_BLEND_32_W( E[N5], px); \
243 ALPHA_BLEND_32_W( E[N7], px); \
245 } else if (e <= i) { \
246 ALPHA_BLEND_128_W(E[N8], ((df(PE,PF) <= df(PE,PH)) ? PF : PH)); \
251 static void xbr3x(AVFrame *input, AVFrame *output, const uint32_t *r2y)
254 const int nl = output->linesize[0] >> 2;
255 const int nl1 = nl + nl;
257 for (y = 0; y < input->height; y++) {
258 INIT_SRC_DST_POINTERS(3)
260 for (x = 0; x < input->width; x++) {
263 E[0] = E[1] = E[2] = PE;
264 E[nl] = E[nl+1] = E[nl+2] = PE; // 3, 4, 5
265 E[nl1] = E[nl1+1] = E[nl1+2] = PE; // 6, 7, 8
267 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);
268 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);
269 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);
270 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);
283 #define FILT4(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, \
284 N15, N14, N11, N3, N7, N10, N13, N12, N9, N6, N2, N1, N5, N8, N4, N0) do { \
285 if (PE != PH && PE != PF) { \
286 const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2); \
287 const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2); \
288 if (e < i && (!eq(PF,PB) && !eq(PH,PD) || eq(PE,PI) \
289 && (!eq(PF,I4) && !eq(PH,I5)) \
290 || eq(PE,PG) || eq(PE,PC))) { \
291 const unsigned ke = df(PF,PG); \
292 const unsigned ki = df(PH,PC); \
293 const int left = ke<<1 <= ki && PE != PG && PD != PG; \
294 const int up = ke >= ki<<1 && PE != PC && PB != PC; \
295 const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH; \
297 ALPHA_BLEND_192_W(E[N13], px); \
298 ALPHA_BLEND_64_W( E[N12], px); \
299 E[N15] = E[N14] = E[N11] = px; \
300 E[N10] = E[N3] = E[N12]; \
303 ALPHA_BLEND_192_W(E[N11], px); \
304 ALPHA_BLEND_192_W(E[N13], px); \
305 ALPHA_BLEND_64_W( E[N10], px); \
306 ALPHA_BLEND_64_W( E[N12], px); \
310 ALPHA_BLEND_192_W(E[N14], px); \
311 ALPHA_BLEND_192_W(E[N7 ], px); \
312 ALPHA_BLEND_64_W( E[N10], px); \
313 ALPHA_BLEND_64_W( E[N3 ], px); \
316 } else { /* diagonal */ \
317 ALPHA_BLEND_128_W(E[N11], px); \
318 ALPHA_BLEND_128_W(E[N14], px); \
321 } else if (e <= i) { \
322 ALPHA_BLEND_128_W( E[N15], ((df(PE,PF) <= df(PE,PH)) ? PF : PH)); \
327 static void xbr4x(AVFrame *input, AVFrame *output, const uint32_t *r2y)
330 const int nl = output->linesize[0] >> 2;
331 const int nl1 = nl + nl;
332 const int nl2 = nl1 + nl;
334 for (y = 0; y < input->height; y++) {
335 INIT_SRC_DST_POINTERS(4)
337 for (x = 0; x < input->width; x++) {
340 E[0] = E[1] = E[2] = E[3] = PE;
341 E[nl] = E[nl+1] = E[nl+2] = E[nl+3] = PE; // 4, 5, 6, 7
342 E[nl1] = E[nl1+1] = E[nl1+2] = E[nl1+3] = PE; // 8, 9, 10, 11
343 E[nl2] = E[nl2+1] = E[nl2+2] = E[nl2+3] = PE; // 12, 13, 14, 15
345 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);
346 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);
347 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);
348 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);
361 static int config_output(AVFilterLink *outlink)
363 AVFilterContext *ctx = outlink->src;
364 XBRContext *xbr = ctx->priv;
365 AVFilterLink *inlink = ctx->inputs[0];
367 outlink->w = inlink->w * xbr->n;
368 outlink->h = inlink->h * xbr->n;
372 static int query_formats(AVFilterContext *ctx)
374 static const enum AVPixelFormat pix_fmts[] = {
375 AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE,
378 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
382 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
384 AVFilterContext *ctx = inlink->dst;
385 AVFilterLink *outlink = ctx->outputs[0];
386 XBRContext *xbr = ctx->priv;
387 const uint32_t *r2y = xbr->rgbtoyuv;
389 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
392 return AVERROR(ENOMEM);
395 av_frame_copy_props(out, in);
398 else if (xbr->n == 3)
403 out->width = outlink->w;
404 out->height = outlink->h;
407 return ff_filter_frame(outlink, out);
410 static int init(AVFilterContext *ctx)
412 XBRContext *xbr = ctx->priv;
416 for (bg = -255; bg < 256; bg++) {
417 for (rg = -255; rg < 256; rg++) {
418 const uint32_t u = (uint32_t)((-169*rg + 500*bg)/1000) + 128;
419 const uint32_t v = (uint32_t)(( 500*rg - 81*bg)/1000) + 128;
420 int startg = FFMAX3(-bg, -rg, 0);
421 int endg = FFMIN3(255-bg, 255-rg, 255);
422 uint32_t y = (uint32_t)(( 299*rg + 1000*startg + 114*bg)/1000);
423 c = bg + (rg<<16) + 0x010101 * startg;
424 for (g = startg; g <= endg; g++) {
425 xbr->rgbtoyuv[c] = ((y++) << 16) + (u << 8) + v;
434 static const AVFilterPad xbr_inputs[] = {
437 .type = AVMEDIA_TYPE_VIDEO,
438 .filter_frame = filter_frame,
443 static const AVFilterPad xbr_outputs[] = {
446 .type = AVMEDIA_TYPE_VIDEO,
447 .config_props = config_output,
452 AVFilter ff_vf_xbr = {
454 .description = NULL_IF_CONFIG_SMALL("Scale the input using xBR algorithm."),
455 .inputs = xbr_inputs,
456 .outputs = xbr_outputs,
457 .query_formats = query_formats,
458 .priv_size = sizeof(XBRContext),
459 .priv_class = &xbr_class,