Browse code

avfilter: add hqx filter (hq2x, hq3x, hq4x)

Partially fixes Ticket #3404 (xbr filter remaining)

Clément Bœsch authored on 2013/06/07 17:57:29
Showing 11 changed files
... ...
@@ -29,6 +29,7 @@ version <next>:
29 29
 - showcqt multimedia filter
30 30
 - zoompan filter
31 31
 - signalstats filter
32
+- hqx filter (hq2x, hq3x, hq4x)
32 33
 
33 34
 
34 35
 version 2.2:
... ...
@@ -350,6 +350,7 @@ Filters:
350 350
   vf_drawbox.c/drawgrid                 Andrey Utkin
351 351
   vf_extractplanes.c                    Paul B Mahol
352 352
   vf_histogram.c                        Paul B Mahol
353
+  vf_hqx.c                              Clément Bœsch
353 354
   vf_il.c                               Paul B Mahol
354 355
   vf_mergeplanes.c                      Paul B Mahol
355 356
   vf_psnr.c                             Paul B Mahol
... ...
@@ -5203,6 +5203,20 @@ A floating point number which specifies chroma temporal strength. It defaults to
5203 5203
 @var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}.
5204 5204
 @end table
5205 5205
 
5206
+@section hqx
5207
+
5208
+Apply a high-quality magnification filter designed for pixel art. This filter
5209
+was originally created by Maxim Stepin.
5210
+
5211
+It accepts the following option:
5212
+
5213
+@table @option
5214
+@item n
5215
+Set the scaling dimension: @code{2} for @code{hq2x}, @code{3} for
5216
+@code{hq3x} and @code{4} for @code{hq4x}.
5217
+Default is @code{3}.
5218
+@end table
5219
+
5206 5220
 @section hue
5207 5221
 
5208 5222
 Modify the hue and/or the saturation of the input.
... ...
@@ -130,6 +130,7 @@ OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
130 130
 OBJS-$(CONFIG_HISTEQ_FILTER)                 += vf_histeq.o
131 131
 OBJS-$(CONFIG_HISTOGRAM_FILTER)              += vf_histogram.o
132 132
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
133
+OBJS-$(CONFIG_HQX_FILTER)                    += vf_hqx.o
133 134
 OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
134 135
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
135 136
 OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
... ...
@@ -148,6 +148,7 @@ void avfilter_register_all(void)
148 148
     REGISTER_FILTER(HISTEQ,         histeq,         vf);
149 149
     REGISTER_FILTER(HISTOGRAM,      histogram,      vf);
150 150
     REGISTER_FILTER(HQDN3D,         hqdn3d,         vf);
151
+    REGISTER_FILTER(HQX,            hqx,            vf);
151 152
     REGISTER_FILTER(HUE,            hue,            vf);
152 153
     REGISTER_FILTER(IDET,           idet,           vf);
153 154
     REGISTER_FILTER(IL,             il,             vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   4
33
-#define LIBAVFILTER_VERSION_MINOR   8
33
+#define LIBAVFILTER_VERSION_MINOR   9
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,560 @@
0
+/*
1
+ * Copyright (c) 2014 Clément Bœsch
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * Permission to use, copy, modify, and/or distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+
18
+/**
19
+ * @file
20
+ * hqx magnification filters (hq2x, hq3x, hq4x)
21
+ *
22
+ * Originally designed by Maxim Stephin.
23
+ *
24
+ * @see http://en.wikipedia.org/wiki/Hqx
25
+ * @see http://web.archive.org/web/20131114143602/http://www.hiend3d.com/hq3x.html
26
+ */
27
+
28
+#include "libavutil/opt.h"
29
+#include "libavutil/avassert.h"
30
+#include "libavutil/pixdesc.h"
31
+#include "internal.h"
32
+
33
+typedef int (*hqxfunc_t)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
34
+
35
+typedef struct {
36
+    const AVClass *class;
37
+    int n;
38
+    hqxfunc_t func;
39
+    uint32_t rgbtoyuv[1<<24];
40
+} HQXContext;
41
+
42
+typedef struct ThreadData {
43
+    AVFrame *in, *out;
44
+    const uint32_t *rgbtoyuv;
45
+} ThreadData;
46
+
47
+#define OFFSET(x) offsetof(HQXContext, x)
48
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49
+static const AVOption hqx_options[] = {
50
+    { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 4, .flags = FLAGS },
51
+    { NULL }
52
+};
53
+
54
+AVFILTER_DEFINE_CLASS(hqx);
55
+
56
+static av_always_inline uint32_t rgb2yuv(const uint32_t *r2y, uint32_t c)
57
+{
58
+    return r2y[c & 0xffffff];
59
+}
60
+
61
+static av_always_inline int yuv_diff(uint32_t yuv1, uint32_t yuv2)
62
+{
63
+#define YMASK 0xff0000
64
+#define UMASK 0x00ff00
65
+#define VMASK 0x0000ff
66
+    return abs((yuv1 & YMASK) - (yuv2 & YMASK)) > (48 << 16) ||
67
+           abs((yuv1 & UMASK) - (yuv2 & UMASK)) > ( 7 <<  8) ||
68
+           abs((yuv1 & VMASK) - (yuv2 & VMASK)) > ( 6 <<  0);
69
+}
70
+
71
+/* (c1*w1 + c2*w2) >> s */
72
+static av_always_inline uint32_t interp_2px(uint32_t c1, int w1, uint32_t c2, int w2, int s)
73
+{
74
+    return (((((c1 & 0xff00ff00) >> 8) * w1 + ((c2 & 0xff00ff00) >> 8) * w2) << (8 - s)) & 0xff00ff00) |
75
+           (((((c1 & 0x00ff00ff)     ) * w1 + ((c2 & 0x00ff00ff)     ) * w2) >>      s ) & 0x00ff00ff);
76
+}
77
+
78
+/* (c1*w1 + c2*w2 + c3*w3) >> s */
79
+static av_always_inline uint32_t interp_3px(uint32_t c1, int w1, uint32_t c2, int w2, uint32_t c3, int w3, int s)
80
+{
81
+    return (((((c1 & 0xff00ff00) >> 8) * w1 + ((c2 & 0xff00ff00) >> 8) * w2 + ((c3 & 0xff00ff00) >> 8) * w3) << (8 - s)) & 0xff00ff00) |
82
+           (((((c1 & 0x00ff00ff)     ) * w1 + ((c2 & 0x00ff00ff)     ) * w2 + ((c3 & 0x00ff00ff)     ) * w3) >>      s ) & 0x00ff00ff);
83
+}
84
+
85
+/* m is the mask of diff with the center pixel that matters in the pattern, and
86
+ * r is the expected result (bit set to 1 if there is difference with the
87
+ * center, 0 otherwise) */
88
+#define P(m, r) ((k_shuffled & (m)) == (r))
89
+
90
+/* adjust 012345678 to 01235678: the mask doesn't contain the (null) diff
91
+ * between the center/current pixel and itself */
92
+#define DROP4(z) ((z) > 4 ? (z)-1 : (z))
93
+
94
+/* shuffle the input mask: move bit n (4-adjusted) to position stored in p<n> */
95
+#define SHF(x, rot, n) (((x) >> ((rot) ? 7-DROP4(n) : DROP4(n)) & 1) << DROP4(p##n))
96
+
97
+/* used to check if there is YUV difference between 2 pixels */
98
+#define WDIFF(c1, c2) yuv_diff(rgb2yuv(r2y, c1), rgb2yuv(r2y, c2))
99
+
100
+/* bootstrap template for every interpolation code. It defines the shuffled
101
+ * masks and surrounding pixels. The rot flag is used to indicate if it's a
102
+ * rotation; its basic effect is to shuffle k using p8..p0 instead of p0..p8 */
103
+#define INTERP_BOOTSTRAP(rot)                                           \
104
+    const int k_shuffled = SHF(k,rot,0) | SHF(k,rot,1) | SHF(k,rot,2)   \
105
+                         | SHF(k,rot,3) |       0      | SHF(k,rot,5)   \
106
+                         | SHF(k,rot,6) | SHF(k,rot,7) | SHF(k,rot,8);  \
107
+                                                                        \
108
+    const uint32_t w0 = w[p0], w1 = w[p1],                              \
109
+                   w3 = w[p3], w4 = w[p4], w5 = w[p5],                  \
110
+                               w7 = w[p7]
111
+
112
+/* Assuming p0..p8 is mapped to pixels 0..8, this function interpolates the
113
+ * top-left pixel in the total of the 2x2 pixels to interpolates. The function
114
+ * is also used for the 3 other pixels */
115
+static av_always_inline uint32_t hq2x_interp_1x1(const uint32_t *r2y, int k,
116
+                                                 const uint32_t *w,
117
+                                                 int p0, int p1, int p2,
118
+                                                 int p3, int p4, int p5,
119
+                                                 int p6, int p7, int p8)
120
+{
121
+    INTERP_BOOTSTRAP(0);
122
+
123
+    if ((P(0xbf,0x37) || P(0xdb,0x13)) && WDIFF(w1, w5))
124
+        return interp_2px(w4, 3, w3, 1, 2);
125
+    if ((P(0xdb,0x49) || P(0xef,0x6d)) && WDIFF(w7, w3))
126
+        return interp_2px(w4, 3, w1, 1, 2);
127
+    if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && WDIFF(w3, w1))
128
+        return w4;
129
+    if ((P(0x6f,0x2a) || P(0x5b,0x0a) || P(0xbf,0x3a) || P(0xdf,0x5a) ||
130
+         P(0x9f,0x8a) || P(0xcf,0x8a) || P(0xef,0x4e) || P(0x3f,0x0e) ||
131
+         P(0xfb,0x5a) || P(0xbb,0x8a) || P(0x7f,0x5a) || P(0xaf,0x8a) ||
132
+         P(0xeb,0x8a)) && WDIFF(w3, w1))
133
+        return interp_2px(w4, 3, w0, 1, 2);
134
+    if (P(0x0b,0x08))
135
+        return interp_3px(w4, 2, w0, 1, w1, 1, 2);
136
+    if (P(0x0b,0x02))
137
+        return interp_3px(w4, 2, w0, 1, w3, 1, 2);
138
+    if (P(0x2f,0x2f))
139
+        return interp_3px(w4, 14, w3, 1, w1, 1, 4);
140
+    if (P(0xbf,0x37) || P(0xdb,0x13))
141
+        return interp_3px(w4, 5, w1, 2, w3, 1, 3);
142
+    if (P(0xdb,0x49) || P(0xef,0x6d))
143
+        return interp_3px(w4, 5, w3, 2, w1, 1, 3);
144
+    if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43))
145
+        return interp_2px(w4, 3, w3, 1, 2);
146
+    if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19))
147
+        return interp_2px(w4, 3, w1, 1, 2);
148
+    if (P(0x7e,0x2a) || P(0xef,0xab) || P(0xbf,0x8f) || P(0x7e,0x0e))
149
+        return interp_3px(w4, 2, w3, 3, w1, 3, 3);
150
+    if (P(0xfb,0x6a) || P(0x6f,0x6e) || P(0x3f,0x3e) || P(0xfb,0xfa) ||
151
+        P(0xdf,0xde) || P(0xdf,0x1e))
152
+        return interp_2px(w4, 3, w0, 1, 2);
153
+    if (P(0x0a,0x00) || P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) ||
154
+        P(0xbe,0x0a) || P(0xee,0x0a) || P(0x7e,0x0a) || P(0xeb,0x4b) ||
155
+        P(0x3b,0x1b))
156
+        return interp_3px(w4, 2, w3, 1, w1, 1, 2);
157
+    return interp_3px(w4, 6, w3, 1, w1, 1, 3);
158
+}
159
+
160
+/* Assuming p0..p8 is mapped to pixels 0..8, this function interpolates the
161
+ * top-left and top-center pixel in the total of the 3x3 pixels to
162
+ * interpolates. The function is also used for the 3 other couples of pixels
163
+ * defining the outline. The center pixel is not defined through this function,
164
+ * since it's just the same as the original value. */
165
+static av_always_inline void hq3x_interp_2x1(uint32_t *dst, int dst_linesize,
166
+                                             const uint32_t *r2y, int k,
167
+                                             const uint32_t *w,
168
+                                             int pos00, int pos01,
169
+                                             int p0, int p1, int p2,
170
+                                             int p3, int p4, int p5,
171
+                                             int p6, int p7, int p8,
172
+                                             int rotate)
173
+{
174
+    INTERP_BOOTSTRAP(rotate);
175
+
176
+    uint32_t *dst00 = &dst[dst_linesize*(pos00>>1) + (pos00&1)];
177
+    uint32_t *dst01 = &dst[dst_linesize*(pos01>>1) + (pos01&1)];
178
+
179
+    if ((P(0xdb,0x49) || P(0xef,0x6d)) && WDIFF(w7, w3))
180
+        *dst00 = interp_2px(w4, 3, w1, 1, 2);
181
+    else if ((P(0xbf,0x37) || P(0xdb,0x13)) && WDIFF(w1, w5))
182
+        *dst00 = interp_2px(w4, 3, w3, 1, 2);
183
+    else if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && WDIFF(w3, w1))
184
+        *dst00 = w4;
185
+    else if ((P(0x6f,0x2a) || P(0x5b,0x0a) || P(0xbf,0x3a) || P(0xdf,0x5a) ||
186
+              P(0x9f,0x8a) || P(0xcf,0x8a) || P(0xef,0x4e) || P(0x3f,0x0e) ||
187
+              P(0xfb,0x5a) || P(0xbb,0x8a) || P(0x7f,0x5a) || P(0xaf,0x8a) ||
188
+              P(0xeb,0x8a)) && WDIFF(w3, w1))
189
+        *dst00 = interp_2px(w4, 3, w0, 1, 2);
190
+    else if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19))
191
+        *dst00 = interp_2px(w4, 3, w1, 1, 2);
192
+    else if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43))
193
+        *dst00 = interp_2px(w4, 3, w3, 1, 2);
194
+    else if (P(0x7e,0x2a) || P(0xef,0xab) || P(0xbf,0x8f) || P(0x7e,0x0e))
195
+        *dst00 = interp_2px(w3, 1, w1, 1, 1);
196
+    else if (P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) || P(0xbe,0x0a) ||
197
+             P(0xee,0x0a) || P(0x7e,0x0a) || P(0xeb,0x4b) || P(0x3b,0x1b))
198
+        *dst00 = interp_3px(w4, 2, w3, 7, w1, 7, 4);
199
+    else if (P(0x0b,0x08) || P(0xf9,0x68) || P(0xf3,0x62) || P(0x6d,0x6c) ||
200
+             P(0x67,0x66) || P(0x3d,0x3c) || P(0x37,0x36) || P(0xf9,0xf8) ||
201
+             P(0xdd,0xdc) || P(0xf3,0xf2) || P(0xd7,0xd6) || P(0xdd,0x1c) ||
202
+             P(0xd7,0x16) || P(0x0b,0x02))
203
+        *dst00 = interp_2px(w4, 3, w0, 1, 2);
204
+    else
205
+        *dst00 = interp_3px(w4, 2, w3, 1, w1, 1, 2);
206
+
207
+    if ((P(0xfe,0xde) || P(0x9e,0x16) || P(0xda,0x12) || P(0x17,0x16) ||
208
+         P(0x5b,0x12) || P(0xbb,0x12)) && WDIFF(w1, w5))
209
+        *dst01 = w4;
210
+    else if ((P(0x0f,0x0b) || P(0x5e,0x0a) || P(0xfb,0x7b) || P(0x3b,0x0b) ||
211
+              P(0xbe,0x0a) || P(0x7a,0x0a)) && WDIFF(w3, w1))
212
+        *dst01 = w4;
213
+    else if (P(0xbf,0x8f) || P(0x7e,0x0e) || P(0xbf,0x37) || P(0xdb,0x13))
214
+        *dst01 = interp_2px(w1, 3, w4, 1, 2);
215
+    else if (P(0x02,0x00) || P(0x7c,0x28) || P(0xed,0xa9) || P(0xf5,0xb4) ||
216
+             P(0xd9,0x90))
217
+        *dst01 = interp_2px(w4, 3, w1, 1, 2);
218
+    else if (P(0x4f,0x4b) || P(0xfb,0x7b) || P(0xfe,0x7e) || P(0x9f,0x1b) ||
219
+             P(0x2f,0x0b) || P(0xbe,0x0a) || P(0x7e,0x0a) || P(0xfb,0x4b) ||
220
+             P(0xfb,0xdb) || P(0xfe,0xde) || P(0xfe,0x56) || P(0x57,0x56) ||
221
+             P(0x97,0x16) || P(0x3f,0x1e) || P(0xdb,0x12) || P(0xbb,0x12))
222
+        *dst01 = interp_2px(w4, 7, w1, 1, 3);
223
+    else
224
+        *dst01 = w4;
225
+}
226
+
227
+/* Assuming p0..p8 is mapped to pixels 0..8, this function interpolates the
228
+ * top-left block of 2x2 pixels in the total of the 4x4 pixels (or 4 blocks) to
229
+ * interpolates. The function is also used for the 3 other blocks of 2x2
230
+ * pixels. */
231
+static av_always_inline void hq4x_interp_2x2(uint32_t *dst, int dst_linesize,
232
+                                             const uint32_t *r2y, int k,
233
+                                             const uint32_t *w,
234
+                                             int pos00, int pos01,
235
+                                             int pos10, int pos11,
236
+                                             int p0, int p1, int p2,
237
+                                             int p3, int p4, int p5,
238
+                                             int p6, int p7, int p8)
239
+{
240
+    INTERP_BOOTSTRAP(0);
241
+
242
+    uint32_t *dst00 = &dst[dst_linesize*(pos00>>1) + (pos00&1)];
243
+    uint32_t *dst01 = &dst[dst_linesize*(pos01>>1) + (pos01&1)];
244
+    uint32_t *dst10 = &dst[dst_linesize*(pos10>>1) + (pos10&1)];
245
+    uint32_t *dst11 = &dst[dst_linesize*(pos11>>1) + (pos11&1)];
246
+
247
+    const int cond00 = (P(0xbf,0x37) || P(0xdb,0x13)) && WDIFF(w1, w5);
248
+    const int cond01 = (P(0xdb,0x49) || P(0xef,0x6d)) && WDIFF(w7, w3);
249
+    const int cond02 = (P(0x6f,0x2a) || P(0x5b,0x0a) || P(0xbf,0x3a) ||
250
+                        P(0xdf,0x5a) || P(0x9f,0x8a) || P(0xcf,0x8a) ||
251
+                        P(0xef,0x4e) || P(0x3f,0x0e) || P(0xfb,0x5a) ||
252
+                        P(0xbb,0x8a) || P(0x7f,0x5a) || P(0xaf,0x8a) ||
253
+                        P(0xeb,0x8a)) && WDIFF(w3, w1);
254
+    const int cond03 = P(0xdb,0x49) || P(0xef,0x6d);
255
+    const int cond04 = P(0xbf,0x37) || P(0xdb,0x13);
256
+    const int cond05 = P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) ||
257
+                       P(0x6b,0x43);
258
+    const int cond06 = P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) ||
259
+                       P(0x3b,0x19);
260
+    const int cond07 = P(0x0b,0x08) || P(0xf9,0x68) || P(0xf3,0x62) ||
261
+                       P(0x6d,0x6c) || P(0x67,0x66) || P(0x3d,0x3c) ||
262
+                       P(0x37,0x36) || P(0xf9,0xf8) || P(0xdd,0xdc) ||
263
+                       P(0xf3,0xf2) || P(0xd7,0xd6) || P(0xdd,0x1c) ||
264
+                       P(0xd7,0x16) || P(0x0b,0x02);
265
+    const int cond08 = (P(0x0f,0x0b) || P(0x2b,0x0b) || P(0xfe,0x4a) ||
266
+                        P(0xfe,0x1a)) && WDIFF(w3, w1);
267
+    const int cond09 = P(0x2f,0x2f);
268
+    const int cond10 = P(0x0a,0x00);
269
+    const int cond11 = P(0x0b,0x09);
270
+    const int cond12 = P(0x7e,0x2a) || P(0xef,0xab);
271
+    const int cond13 = P(0xbf,0x8f) || P(0x7e,0x0e);
272
+    const int cond14 = P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) ||
273
+                       P(0xbe,0x0a) || P(0xee,0x0a) || P(0x7e,0x0a) ||
274
+                       P(0xeb,0x4b) || P(0x3b,0x1b);
275
+    const int cond15 = P(0x0b,0x03);
276
+
277
+    if (cond00)
278
+        *dst00 = interp_2px(w4, 5, w3, 3, 3);
279
+    else if (cond01)
280
+        *dst00 = interp_2px(w4, 5, w1, 3, 3);
281
+    else if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && WDIFF(w3, w1))
282
+        *dst00 = w4;
283
+    else if (cond02)
284
+        *dst00 = interp_2px(w4, 5, w0, 3, 3);
285
+    else if (cond03)
286
+        *dst00 = interp_2px(w4, 3, w3, 1, 2);
287
+    else if (cond04)
288
+        *dst00 = interp_2px(w4, 3, w1, 1, 2);
289
+    else if (cond05)
290
+        *dst00 = interp_2px(w4, 5, w3, 3, 3);
291
+    else if (cond06)
292
+        *dst00 = interp_2px(w4, 5, w1, 3, 3);
293
+    else if (P(0x0f,0x0b) || P(0x5e,0x0a) || P(0x2b,0x0b) || P(0xbe,0x0a) ||
294
+             P(0x7a,0x0a) || P(0xee,0x0a))
295
+        *dst00 = interp_2px(w1, 1, w3, 1, 1);
296
+    else if (cond07)
297
+        *dst00 = interp_2px(w4, 5, w0, 3, 3);
298
+    else
299
+        *dst00 = interp_3px(w4, 2, w1, 1, w3, 1, 2);
300
+
301
+    if (cond00)
302
+        *dst01 = interp_2px(w4, 7, w3, 1, 3);
303
+    else if (cond08)
304
+        *dst01 = w4;
305
+    else if (cond02)
306
+        *dst01 = interp_2px(w4, 3, w0, 1, 2);
307
+    else if (cond09)
308
+        *dst01 = w4;
309
+    else if (cond10)
310
+        *dst01 = interp_3px(w4, 5, w1, 2, w3, 1, 3);
311
+    else if (P(0x0b,0x08))
312
+        *dst01 = interp_3px(w4, 5, w1, 2, w0, 1, 3);
313
+    else if (cond11)
314
+        *dst01 = interp_2px(w4, 5, w1, 3, 3);
315
+    else if (cond04)
316
+        *dst01 = interp_2px(w1, 3, w4, 1, 2);
317
+    else if (cond12)
318
+        *dst01 = interp_3px(w1, 2, w4, 1, w3, 1, 2);
319
+    else if (cond13)
320
+        *dst01 = interp_2px(w1, 5, w3, 3, 3);
321
+    else if (cond05)
322
+        *dst01 = interp_2px(w4, 7, w3, 1, 3);
323
+    else if (P(0xf3,0x62) || P(0x67,0x66) || P(0x37,0x36) || P(0xf3,0xf2) ||
324
+             P(0xd7,0xd6) || P(0xd7,0x16) || P(0x0b,0x02))
325
+        *dst01 = interp_2px(w4, 3, w0, 1, 2);
326
+    else if (cond14)
327
+        *dst01 = interp_2px(w1, 1, w4, 1, 1);
328
+    else
329
+        *dst01 = interp_2px(w4, 3, w1, 1, 2);
330
+
331
+    if (cond01)
332
+        *dst10 = interp_2px(w4, 7, w1, 1, 3);
333
+    else if (cond08)
334
+        *dst10 = w4;
335
+    else if (cond02)
336
+        *dst10 = interp_2px(w4, 3, w0, 1, 2);
337
+    else if (cond09)
338
+        *dst10 = w4;
339
+    else if (cond10)
340
+        *dst10 = interp_3px(w4, 5, w3, 2, w1, 1, 3);
341
+    else if (P(0x0b,0x02))
342
+        *dst10 = interp_3px(w4, 5, w3, 2, w0, 1, 3);
343
+    else if (cond15)
344
+        *dst10 = interp_2px(w4, 5, w3, 3, 3);
345
+    else if (cond03)
346
+        *dst10 = interp_2px(w3, 3, w4, 1, 2);
347
+    else if (cond13)
348
+        *dst10 = interp_3px(w3, 2, w4, 1, w1, 1, 2);
349
+    else if (cond12)
350
+        *dst10 = interp_2px(w3, 5, w1, 3, 3);
351
+    else if (cond06)
352
+        *dst10 = interp_2px(w4, 7, w1, 1, 3);
353
+    else if (P(0x0b,0x08) || P(0xf9,0x68) || P(0x6d,0x6c) || P(0x3d,0x3c) ||
354
+             P(0xf9,0xf8) || P(0xdd,0xdc) || P(0xdd,0x1c))
355
+        *dst10 = interp_2px(w4, 3, w0, 1, 2);
356
+    else if (cond14)
357
+        *dst10 = interp_2px(w3, 1, w4, 1, 1);
358
+    else
359
+        *dst10 = interp_2px(w4, 3, w3, 1, 2);
360
+
361
+    if ((P(0x7f,0x2b) || P(0xef,0xab) || P(0xbf,0x8f) || P(0x7f,0x0f)) &&
362
+         WDIFF(w3, w1))
363
+        *dst11 = w4;
364
+    else if (cond02)
365
+        *dst11 = interp_2px(w4, 7, w0, 1, 3);
366
+    else if (cond15)
367
+        *dst11 = interp_2px(w4, 7, w3, 1, 3);
368
+    else if (cond11)
369
+        *dst11 = interp_2px(w4, 7, w1, 1, 3);
370
+    else if (P(0x0a,0x00) || P(0x7e,0x2a) || P(0xef,0xab) || P(0xbf,0x8f) ||
371
+             P(0x7e,0x0e))
372
+        *dst11 = interp_3px(w4, 6, w3, 1, w1, 1, 3);
373
+    else if (cond07)
374
+        *dst11 = interp_2px(w4, 7, w0, 1, 3);
375
+    else
376
+        *dst11 = w4;
377
+}
378
+
379
+static av_always_inline void hqx_filter(const ThreadData *td, int jobnr, int nb_jobs, int n)
380
+{
381
+    int x, y;
382
+    AVFrame *in = td->in, *out = td->out;
383
+    const uint32_t *r2y = td->rgbtoyuv;
384
+    const int height = in->height;
385
+    const int width  = in->width;
386
+    const int slice_start = (height *  jobnr   ) / nb_jobs;
387
+    const int slice_end   = (height * (jobnr+1)) / nb_jobs;
388
+    const int dst_linesize = out->linesize[0];
389
+    const int src_linesize =  in->linesize[0];
390
+    uint8_t       *dst = out->data[0] + slice_start * dst_linesize * n;
391
+    const uint8_t *src =  in->data[0] + slice_start * src_linesize;
392
+
393
+    const int dst32_linesize = dst_linesize >> 2;
394
+    const int src32_linesize = src_linesize >> 2;
395
+
396
+    for (y = slice_start; y < slice_end; y++) {
397
+        const uint32_t *src32 = (const uint32_t *)src;
398
+        uint32_t       *dst32 = (uint32_t *)dst;
399
+        const int prevline = y > 0          ? -src32_linesize : 0;
400
+        const int nextline = y < height - 1 ?  src32_linesize : 0;
401
+
402
+        for (x = 0; x < width; x++) {
403
+            uint32_t yuv1, yuv2;
404
+            const int prevcol = x > 0        ? -1 : 0;
405
+            const int nextcol = x < width -1 ?  1 : 0;
406
+            int pattern = 0, flag = 1, k;
407
+            const uint32_t w[3*3] = {
408
+                src32[prevcol + prevline], src32[prevline], src32[prevline + nextcol],
409
+                src32[prevcol           ], src32[       0], src32[           nextcol],
410
+                src32[prevcol + nextline], src32[nextline], src32[nextline + nextcol]
411
+            };
412
+
413
+            yuv1 = rgb2yuv(r2y, w[4]);
414
+
415
+            for (k = 0; k < FF_ARRAY_ELEMS(w); k++) {
416
+                if (k == 4)
417
+                    continue;
418
+                if (w[k] != w[4]) {
419
+                    yuv2 = rgb2yuv(r2y, w[k]);
420
+                    if (yuv_diff(yuv1, yuv2))
421
+                        pattern |= flag;
422
+                }
423
+                flag <<= 1;
424
+            }
425
+
426
+            if (n == 2) {
427
+                dst32[dst32_linesize*0 + 0] = hq2x_interp_1x1(r2y, pattern, w, 0,1,2,3,4,5,6,7,8);  // 00
428
+                dst32[dst32_linesize*0 + 1] = hq2x_interp_1x1(r2y, pattern, w, 2,1,0,5,4,3,8,7,6);  // 01 (vert mirrored)
429
+                dst32[dst32_linesize*1 + 0] = hq2x_interp_1x1(r2y, pattern, w, 6,7,8,3,4,5,0,1,2);  // 10 (horiz mirrored)
430
+                dst32[dst32_linesize*1 + 1] = hq2x_interp_1x1(r2y, pattern, w, 8,7,6,5,4,3,2,1,0);  // 11 (center mirrored)
431
+            } else if (n == 3) {
432
+                hq3x_interp_2x1(dst32,                        dst32_linesize, r2y, pattern, w, 0,1, 0,1,2,3,4,5,6,7,8, 0);  // 00 01
433
+                hq3x_interp_2x1(dst32 + 1,                    dst32_linesize, r2y, pattern, w, 1,3, 2,5,8,1,4,7,0,3,6, 1);  // 02 12 (rotated to the right)
434
+                hq3x_interp_2x1(dst32 + 1*dst32_linesize,     dst32_linesize, r2y, pattern, w, 2,0, 6,3,0,7,4,1,8,5,2, 1);  // 20 10 (rotated to the left)
435
+                hq3x_interp_2x1(dst32 + 1*dst32_linesize + 1, dst32_linesize, r2y, pattern, w, 3,2, 8,7,6,5,4,3,2,1,0, 0);  // 22 21 (center mirrored)
436
+                dst32[dst32_linesize + 1] = w[4];                                                                           // 11
437
+            } else if (n == 4) {
438
+                hq4x_interp_2x2(dst32,                        dst32_linesize, r2y, pattern, w, 0,1,2,3, 0,1,2,3,4,5,6,7,8); // 00 01 10 11
439
+                hq4x_interp_2x2(dst32 + 2,                    dst32_linesize, r2y, pattern, w, 1,0,3,2, 2,1,0,5,4,3,8,7,6); // 02 03 12 13 (vert mirrored)
440
+                hq4x_interp_2x2(dst32 + 2*dst32_linesize,     dst32_linesize, r2y, pattern, w, 2,3,0,1, 6,7,8,3,4,5,0,1,2); // 20 21 30 31 (horiz mirrored)
441
+                hq4x_interp_2x2(dst32 + 2*dst32_linesize + 2, dst32_linesize, r2y, pattern, w, 3,2,1,0, 8,7,6,5,4,3,2,1,0); // 22 23 32 33 (center mirrored)
442
+            } else {
443
+                av_assert0(0);
444
+            }
445
+
446
+            src32 += 1;
447
+            dst32 += n;
448
+        }
449
+
450
+        src += src_linesize;
451
+        dst += dst_linesize * n;
452
+    }
453
+}
454
+
455
+#define HQX_FUNC(size) \
456
+static int hq##size##x(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
457
+{ \
458
+    hqx_filter(arg, jobnr, nb_jobs, size); \
459
+    return 0; \
460
+}
461
+
462
+HQX_FUNC(2)
463
+HQX_FUNC(3)
464
+HQX_FUNC(4)
465
+
466
+static int query_formats(AVFilterContext *ctx)
467
+{
468
+    static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE};
469
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
470
+    return 0;
471
+}
472
+
473
+static int config_output(AVFilterLink *outlink)
474
+{
475
+    AVFilterContext *ctx = outlink->src;
476
+    HQXContext *hqx = ctx->priv;
477
+    AVFilterLink *inlink = ctx->inputs[0];
478
+
479
+    outlink->w = inlink->w * hqx->n;
480
+    outlink->h = inlink->h * hqx->n;
481
+    av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
482
+           av_get_pix_fmt_name(inlink->format),
483
+           inlink->w, inlink->h, outlink->w, outlink->h);
484
+    return 0;
485
+}
486
+
487
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
488
+{
489
+    AVFilterContext *ctx = inlink->dst;
490
+    AVFilterLink *outlink = ctx->outputs[0];
491
+    HQXContext *hqx = ctx->priv;
492
+    ThreadData td;
493
+    AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
494
+    if (!out) {
495
+        av_frame_free(&in);
496
+        return AVERROR(ENOMEM);
497
+    }
498
+    av_frame_copy_props(out, in);
499
+    out->width  = outlink->w;
500
+    out->height = outlink->h;
501
+
502
+    td.in = in;
503
+    td.out = out;
504
+    td.rgbtoyuv = hqx->rgbtoyuv;
505
+    ctx->internal->execute(ctx, hqx->func, &td, NULL, FFMIN(inlink->h, ctx->graph->nb_threads));
506
+
507
+    av_frame_free(&in);
508
+    return ff_filter_frame(outlink, out);
509
+}
510
+
511
+static av_cold int init(AVFilterContext *ctx)
512
+{
513
+    HQXContext *hqx = ctx->priv;
514
+    static const hqxfunc_t hqxfuncs[] = {hq2x, hq3x, hq4x};
515
+
516
+    uint32_t c;
517
+    for (c = 0; c < FF_ARRAY_ELEMS(hqx->rgbtoyuv); c++) {
518
+        const uint32_t r = c >> 16 & 0xff;
519
+        const uint32_t g = c >>  8 & 0xff;
520
+        const uint32_t b = c       & 0xff;
521
+        const uint32_t y = (uint32_t)( 0.299*r + 0.587*g + 0.114*b);
522
+        const uint32_t u = (uint32_t)(-0.169*r - 0.331*g +   0.5*b) + 128;
523
+        const uint32_t v = (uint32_t)(   0.5*r - 0.419*g - 0.081*b) + 128;
524
+        hqx->rgbtoyuv[c] = (y << 16) + (u << 8) + v;
525
+    }
526
+
527
+    hqx->func = hqxfuncs[hqx->n - 2];
528
+    return 0;
529
+}
530
+
531
+static const AVFilterPad hqx_inputs[] = {
532
+    {
533
+        .name         = "default",
534
+        .type         = AVMEDIA_TYPE_VIDEO,
535
+        .filter_frame = filter_frame,
536
+    },
537
+    { NULL }
538
+};
539
+
540
+static const AVFilterPad hqx_outputs[] = {
541
+    {
542
+        .name         = "default",
543
+        .type         = AVMEDIA_TYPE_VIDEO,
544
+        .config_props = config_output,
545
+    },
546
+    { NULL }
547
+};
548
+
549
+AVFilter ff_vf_hqx = {
550
+    .name          = "hqx",
551
+    .description   = NULL_IF_CONFIG_SMALL("Scale the input by 2, 3 or 4 using the hq*x magnification algorithm."),
552
+    .priv_size     = sizeof(HQXContext),
553
+    .init          = init,
554
+    .query_formats = query_formats,
555
+    .inputs        = hqx_inputs,
556
+    .outputs       = hqx_outputs,
557
+    .priv_class    = &hqx_class,
558
+    .flags         = AVFILTER_FLAG_SLICE_THREADS,
559
+};
... ...
@@ -146,6 +146,13 @@ FATE_FILTER-$(call ALLYES, SMJPEG_DEMUXER MJPEG_DECODER PERMS_FILTER HQDN3D_FILT
146 146
 fate-filter-hqdn3d-sample: tests/data/filtergraphs/hqdn3d
147 147
 fate-filter-hqdn3d-sample: CMD = framecrc -idct simple -i $(TARGET_SAMPLES)/smjpeg/scenwin.mjpg -filter_complex_script $(TARGET_PATH)/tests/data/filtergraphs/hqdn3d -an
148 148
 
149
+FATE_FILTER_HQX-$(call ALLYES, IMAGE2_DEMUXER PNG_DECODER HQX_FILTER) = fate-filter-hq2x fate-filter-hq3x fate-filter-hq4x
150
+FATE_FILTER-yes += $(FATE_FILTER_HQX-yes)
151
+fate-filter-hq2x: CMD = framecrc -i $(TARGET_SAMPLES)/filter/pixelart%d.png -vf hqx=2
152
+fate-filter-hq3x: CMD = framecrc -i $(TARGET_SAMPLES)/filter/pixelart%d.png -vf hqx=3
153
+fate-filter-hq4x: CMD = framecrc -i $(TARGET_SAMPLES)/filter/pixelart%d.png -vf hqx=4
154
+fate-filter-hqx: $(FATE_FILTER_HQX-yes)
155
+
149 156
 FATE_FILTER-$(call ALLYES, UTVIDEO_DECODER AVI_DEMUXER PERMS_FILTER CURVES_FILTER) += fate-filter-curves
150 157
 fate-filter-curves: CMD = framecrc -i $(TARGET_SAMPLES)/utvideo/utvideo_rgb_median.avi -vf perms=random,curves=vintage
151 158
 
152 159
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+#tb 0: 1/25
1
+0,          0,          0,        1,   877072, 0x9369339e
2
+0,          1,          1,        1,   877072, 0x32d119a1
0 3
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+#tb 0: 1/25
1
+0,          0,          0,        1,  1973412, 0xafc227fa
2
+0,          1,          1,        1,  1973412, 0x93aebf19
0 3
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+#tb 0: 1/25
1
+0,          0,          0,        1,  3508288, 0x034ef75e
2
+0,          1,          1,        1,  3508288, 0x738e9bbb