2 * This file is part of FFmpeg.
4 * Copyright (c) 2011, 2012 Hyllian/Jararaca <sergiogdb@gmail.com>
5 * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
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.
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.
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
24 * XBR Filter is used for depixelization of image.
25 * This is based on Hyllian's xBR shader.
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
30 * @todo add threading and FATE test
33 #include "libavutil/opt.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/pixdesc.h"
38 #define RGB_MASK 0x00FFFFFF
39 #define LB_MASK 0x00FEFEFE
40 #define RED_BLUE_MASK 0x00FF00FF
41 #define GREEN_MASK 0x0000FF00
46 uint32_t rgbtoyuv[1<<24];
49 #define OFFSET(x) offsetof(XBRContext, x)
50 static const AVOption xbr_options[] = {
51 { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 4, },
55 AVFILTER_DEFINE_CLASS(xbr);
57 static uint32_t pixel_diff(uint32_t x, uint32_t y, const uint32_t *r2y)
59 #define YMASK 0xff0000
60 #define UMASK 0x00ff00
61 #define VMASK 0x0000ff
63 uint32_t yuv1 = r2y[x & 0xffffff];
64 uint32_t yuv2 = r2y[y & 0xffffff];
66 return (abs((yuv1 & YMASK) - (yuv2 & YMASK)) >> 16) +
67 (abs((yuv1 & UMASK) - (yuv2 & UMASK)) >> 8) +
68 abs((yuv1 & VMASK) - (yuv2 & VMASK));
71 #define ALPHA_BLEND_128_W(dst, src) dst = ((src & LB_MASK) >> 1) + ((dst & LB_MASK) >> 1)
73 #define ALPHA_BLEND_32_W(dst, src) \
74 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
75 (dst & RED_BLUE_MASK))) >> 3))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
76 ((((src & GREEN_MASK) - (dst & GREEN_MASK))) >> 3))))
78 #define ALPHA_BLEND_64_W(dst, src) \
79 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
80 (dst & RED_BLUE_MASK))) >> 2))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
81 ((((src & GREEN_MASK) - (dst & GREEN_MASK))) >> 2))))
83 #define ALPHA_BLEND_192_W(dst, src) \
84 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
85 (dst & RED_BLUE_MASK)) * 3) >> 2))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
86 ((((src & GREEN_MASK) - (dst & GREEN_MASK)) * 3) >> 2))))
88 #define ALPHA_BLEND_224_W(dst, src) \
89 dst = ((RED_BLUE_MASK & ((dst & RED_BLUE_MASK) + ((((src & RED_BLUE_MASK) - \
90 (dst & RED_BLUE_MASK)) * 7) >> 3))) | (GREEN_MASK & ((dst & GREEN_MASK) + \
91 ((((src & GREEN_MASK) - (dst & GREEN_MASK)) * 7) >> 3))))
93 #define df(A, B) pixel_diff(A, B, r2y)
94 #define eq(A, B) (df(A, B) < 155)
96 #define INIT_SRC_DST_POINTERS(level) \
97 uint32_t *E = (uint32_t *)(output->data[0] + y * output->linesize[0] * (level)); \
98 const uint32_t *sa2 = (uint32_t *)(input->data[0] + y * input->linesize[0] - 8); /* center */ \
99 const uint32_t *sa1 = sa2 - (input->linesize[0]>>2); /* up x1 */ \
100 const uint32_t *sa0 = sa1 - (input->linesize[0]>>2); /* up x2 */ \
101 const uint32_t *sa3 = sa2 + (input->linesize[0]>>2); /* down x1 */ \
102 const uint32_t *sa4 = sa3 + (input->linesize[0]>>2); /* down x2 */ \
111 if (y >= input->height - 2) { \
113 if (y == input->height - 1) { \
118 #define INIT_21_PIXELS \
119 const uint32_t B1 = sa0[2]; \
120 const uint32_t PB = sa1[2]; \
121 const uint32_t PE = sa2[2]; \
122 const uint32_t PH = sa3[2]; \
123 const uint32_t H5 = sa4[2]; \
125 const int pprev = 2 - (x > 0); \
126 const uint32_t A1 = sa0[pprev]; \
127 const uint32_t PA = sa1[pprev]; \
128 const uint32_t PD = sa2[pprev]; \
129 const uint32_t PG = sa3[pprev]; \
130 const uint32_t G5 = sa4[pprev]; \
132 const int pprev2 = pprev - (x > 1); \
133 const uint32_t A0 = sa1[pprev2]; \
134 const uint32_t D0 = sa2[pprev2]; \
135 const uint32_t G0 = sa3[pprev2]; \
137 const int pnext = 3 - (x == input->width - 1); \
138 const uint32_t C1 = sa0[pnext]; \
139 const uint32_t PC = sa1[pnext]; \
140 const uint32_t PF = sa2[pnext]; \
141 const uint32_t PI = sa3[pnext]; \
142 const uint32_t I5 = sa4[pnext]; \
144 const int pnext2 = pnext + 1 - (x >= input->width - 2); \
145 const uint32_t C4 = sa1[pnext2]; \
146 const uint32_t F4 = sa2[pnext2]; \
147 const uint32_t I4 = sa3[pnext2];
149 #define FILT2(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, \
150 N0, N1, N2, N3) do { \
151 if (PE != PH && PE != PF) { \
152 const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2); \
153 const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2); \
154 if (e < i && (!eq(PF,PB) && !eq(PH,PD) || eq(PE,PI) \
155 && (!eq(PF,I4) && !eq(PH,I5)) \
156 || eq(PE,PG) || eq(PE,PC))) { \
157 const unsigned ke = df(PF,PG); \
158 const unsigned ki = df(PH,PC); \
159 const int left = ke<<1 <= ki && PE != PG && PD != PG; \
160 const int up = ke >= ki<<1 && PE != PC && PB != PC; \
161 const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH; \
163 ALPHA_BLEND_224_W(E[N3], px); \
164 ALPHA_BLEND_64_W( E[N2], px); \
167 ALPHA_BLEND_192_W(E[N3], px); \
168 ALPHA_BLEND_64_W( E[N2], px); \
170 ALPHA_BLEND_192_W(E[N3], px); \
171 ALPHA_BLEND_64_W( E[N1], px); \
172 } else { /* diagonal */ \
173 ALPHA_BLEND_128_W(E[N3], px); \
175 } else if (e <= i) { \
176 ALPHA_BLEND_128_W( E[N3], ((df(PE,PF) <= df(PE,PH)) ? PF : PH)); \
181 static void xbr2x(AVFrame * input, AVFrame * output, const uint32_t * r2y)
184 const int nl = output->linesize[0] >> 2;
186 for (y = 0; y < input->height; y++) {
187 INIT_SRC_DST_POINTERS(2)
189 for (x = 0; x < input->width; x++) {
192 E[0] = E[1] = E[nl] = E[nl + 1] = PE; // 0, 1, 2, 3
194 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);
195 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);
196 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);
197 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);
210 #define FILT3(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, \
211 N0, N1, N2, N3, N4, N5, N6, N7, N8) do { \
212 if (PE != PH && PE != PF) { \
213 const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2); \
214 const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2); \
215 if (e < i && (!eq(PF,PB) && !eq(PF,PC) || !eq(PH,PD) && !eq(PH,PG) || eq(PE,PI) \
216 && (!eq(PF,F4) && !eq(PF,I4) || !eq(PH,H5) && !eq(PH,I5)) \
217 || eq(PE,PG) || eq(PE,PC))) { \
218 const unsigned ke = df(PF,PG); \
219 const unsigned ki = df(PH,PC); \
220 const int left = ke<<1 <= ki && PE != PG && PD != PG; \
221 const int up = ke >= ki<<1 && PE != PC && PB != PC; \
222 const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH; \
224 ALPHA_BLEND_192_W(E[N7], px); \
225 ALPHA_BLEND_64_W( E[N6], px); \
230 ALPHA_BLEND_192_W(E[N7], px); \
231 ALPHA_BLEND_64_W( E[N5], px); \
232 ALPHA_BLEND_64_W( E[N6], px); \
235 ALPHA_BLEND_192_W(E[N5], px); \
236 ALPHA_BLEND_64_W( E[N7], px); \
237 ALPHA_BLEND_64_W( E[N2], px); \
239 } else { /* diagonal */ \
240 ALPHA_BLEND_224_W(E[N8], px); \
241 ALPHA_BLEND_32_W( E[N5], px); \
242 ALPHA_BLEND_32_W( E[N7], px); \
244 } else if (e <= i) { \
245 ALPHA_BLEND_128_W(E[N8], ((df(PE,PF) <= df(PE,PH)) ? PF : PH)); \
250 static void xbr3x(AVFrame *input, AVFrame *output, const uint32_t *r2y)
253 const int nl = output->linesize[0] >> 2;
254 const int nl1 = nl + nl;
256 for (y = 0; y < input->height; y++) {
257 INIT_SRC_DST_POINTERS(3)
259 for (x = 0; x < input->width; x++) {
262 E[0] = E[1] = E[2] = PE;
263 E[nl] = E[nl+1] = E[nl+2] = PE; // 3, 4, 5
264 E[nl1] = E[nl1+1] = E[nl1+2] = PE; // 6, 7, 8
266 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);
267 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);
268 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);
269 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);
282 #define FILT4(PE, PI, PH, PF, PG, PC, PD, PB, PA, G5, C4, G0, D0, C1, B1, F4, I4, H5, I5, A0, A1, \
283 N15, N14, N11, N3, N7, N10, N13, N12, N9, N6, N2, N1, N5, N8, N4, N0) do { \
284 if (PE != PH && PE != PF) { \
285 const unsigned e = df(PE,PC) + df(PE,PG) + df(PI,H5) + df(PI,F4) + (df(PH,PF)<<2); \
286 const unsigned i = df(PH,PD) + df(PH,I5) + df(PF,I4) + df(PF,PB) + (df(PE,PI)<<2); \
287 if (e < i && (!eq(PF,PB) && !eq(PH,PD) || eq(PE,PI) \
288 && (!eq(PF,I4) && !eq(PH,I5)) \
289 || eq(PE,PG) || eq(PE,PC))) { \
290 const unsigned ke = df(PF,PG); \
291 const unsigned ki = df(PH,PC); \
292 const int left = ke<<1 <= ki && PE != PG && PD != PG; \
293 const int up = ke >= ki<<1 && PE != PC && PB != PC; \
294 const unsigned px = df(PE,PF) <= df(PE,PH) ? PF : PH; \
296 ALPHA_BLEND_192_W(E[N13], px); \
297 ALPHA_BLEND_64_W( E[N12], px); \
298 E[N15] = E[N14] = E[N11] = px; \
299 E[N10] = E[N3] = E[N12]; \
302 ALPHA_BLEND_192_W(E[N11], px); \
303 ALPHA_BLEND_192_W(E[N13], px); \
304 ALPHA_BLEND_64_W( E[N10], px); \
305 ALPHA_BLEND_64_W( E[N12], px); \
309 ALPHA_BLEND_192_W(E[N14], px); \
310 ALPHA_BLEND_192_W(E[N7 ], px); \
311 ALPHA_BLEND_64_W( E[N10], px); \
312 ALPHA_BLEND_64_W( E[N3 ], px); \
315 } else { /* diagonal */ \
316 ALPHA_BLEND_128_W(E[N11], px); \
317 ALPHA_BLEND_128_W(E[N14], px); \
320 } else if (e <= i) { \
321 ALPHA_BLEND_128_W( E[N15], ((df(PE,PF) <= df(PE,PH)) ? PF : PH)); \
326 static void xbr4x(AVFrame *input, AVFrame *output, const uint32_t *r2y)
329 const int nl = output->linesize[0] >> 2;
330 const int nl1 = nl + nl;
331 const int nl2 = nl1 + nl;
333 for (y = 0; y < input->height; y++) {
334 INIT_SRC_DST_POINTERS(4)
336 for (x = 0; x < input->width; x++) {
339 E[0] = E[1] = E[2] = E[3] = PE;
340 E[nl] = E[nl+1] = E[nl+2] = E[nl+3] = PE; // 4, 5, 6, 7
341 E[nl1] = E[nl1+1] = E[nl1+2] = E[nl1+3] = PE; // 8, 9, 10, 11
342 E[nl2] = E[nl2+1] = E[nl2+2] = E[nl2+3] = PE; // 12, 13, 14, 15
344 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);
345 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);
346 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);
347 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);
360 static int config_output(AVFilterLink *outlink)
362 AVFilterContext *ctx = outlink->src;
363 XBRContext *xbr = ctx->priv;
364 AVFilterLink *inlink = ctx->inputs[0];
366 outlink->w = inlink->w * xbr->n;
367 outlink->h = inlink->h * xbr->n;
371 static int query_formats(AVFilterContext *ctx)
373 static const enum AVPixelFormat pix_fmts[] = {
374 AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE,
377 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
381 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
383 AVFilterContext *ctx = inlink->dst;
384 AVFilterLink *outlink = ctx->outputs[0];
385 XBRContext *xbr = ctx->priv;
386 const uint32_t *r2y = xbr->rgbtoyuv;
388 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
391 return AVERROR(ENOMEM);
394 av_frame_copy_props(out, in);
397 else if (xbr->n == 3)
402 out->width = outlink->w;
403 out->height = outlink->h;
406 return ff_filter_frame(outlink, out);
409 static int init(AVFilterContext *ctx)
411 XBRContext *xbr = ctx->priv;
415 for (bg = -255; bg < 256; bg++) {
416 for (rg = -255; rg < 256; rg++) {
417 const uint32_t u = (uint32_t)((-169*rg + 500*bg)/1000) + 128;
418 const uint32_t v = (uint32_t)(( 500*rg - 81*bg)/1000) + 128;
419 int startg = FFMAX3(-bg, -rg, 0);
420 int endg = FFMIN3(255-bg, 255-rg, 255);
421 uint32_t y = (uint32_t)(( 299*rg + 1000*startg + 114*bg)/1000);
422 c = bg + (rg<<16) + 0x010101 * startg;
423 for (g = startg; g <= endg; g++) {
424 xbr->rgbtoyuv[c] = ((y++) << 16) + (u << 8) + v;
433 static const AVFilterPad xbr_inputs[] = {
436 .type = AVMEDIA_TYPE_VIDEO,
437 .filter_frame = filter_frame,
442 static const AVFilterPad xbr_outputs[] = {
445 .type = AVMEDIA_TYPE_VIDEO,
446 .config_props = config_output,
451 AVFilter ff_vf_xbr = {
453 .description = NULL_IF_CONFIG_SMALL("Scale the input using xBR algorithm."),
454 .inputs = xbr_inputs,
455 .outputs = xbr_outputs,
456 .query_formats = query_formats,
457 .priv_size = sizeof(XBRContext),
458 .priv_class = &xbr_class,