00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include "libavutil/pixdesc.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "avfilter.h"
00032 #include "formats.h"
00033 #include "video.h"
00034
00035 typedef struct {
00036
00037 uint32_t hi_pixel_mask;
00038 uint32_t lo_pixel_mask;
00039
00040
00041 uint32_t q_hi_pixel_mask;
00042 uint32_t q_lo_pixel_mask;
00043
00044 int bpp;
00045 int is_be;
00046 } Super2xSaIContext;
00047
00048 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
00049
00050 #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
00051
00052 #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) \
00053 + ((((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)
00054
00055 static void super2xsai(AVFilterContext *ctx,
00056 uint8_t *src, int src_linesize,
00057 uint8_t *dst, int dst_linesize,
00058 int width, int height)
00059 {
00060 Super2xSaIContext *sai = ctx->priv;
00061 unsigned int x, y;
00062 uint32_t color[4][4];
00063 unsigned char *src_line[4];
00064 const int bpp = sai->bpp;
00065 const uint32_t hi_pixel_mask = sai->hi_pixel_mask;
00066 const uint32_t lo_pixel_mask = sai->lo_pixel_mask;
00067 const uint32_t q_hi_pixel_mask = sai->q_hi_pixel_mask;
00068 const uint32_t q_lo_pixel_mask = sai->q_lo_pixel_mask;
00069
00070
00071 src_line[0] = src;
00072 src_line[1] = src;
00073 src_line[2] = src + src_linesize*FFMIN(1, height-1);
00074 src_line[3] = src + src_linesize*FFMIN(2, height-1);
00075
00076 #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
00077 #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
00078 #define READ_COLOR2(dst, src_line, off) dst = sai->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
00079
00080 for (y = 0; y < height; y++) {
00081 uint8_t *dst_line[2];
00082
00083 dst_line[0] = dst + dst_linesize*2*y;
00084 dst_line[1] = dst + dst_linesize*(2*y+1);
00085
00086 switch (bpp) {
00087 case 4:
00088 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);
00089 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);
00090 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);
00091 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);
00092 break;
00093 case 3:
00094 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);
00095 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);
00096 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);
00097 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);
00098 break;
00099 default:
00100 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);
00101 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);
00102 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);
00103 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);
00104 }
00105
00106 for (x = 0; x < width; x++) {
00107 uint32_t product1a, product1b, product2a, product2b;
00108
00109
00110
00111
00112
00113
00114 if (color[2][1] == color[1][2] && color[1][1] != color[2][2]) {
00115 product2b = color[2][1];
00116 product1b = product2b;
00117 } else if (color[1][1] == color[2][2] && color[2][1] != color[1][2]) {
00118 product2b = color[1][1];
00119 product1b = product2b;
00120 } else if (color[1][1] == color[2][2] && color[2][1] == color[1][2]) {
00121 int r = 0;
00122
00123 r += GET_RESULT(color[1][2], color[1][1], color[1][0], color[3][1]);
00124 r += GET_RESULT(color[1][2], color[1][1], color[2][0], color[0][1]);
00125 r += GET_RESULT(color[1][2], color[1][1], color[3][2], color[2][3]);
00126 r += GET_RESULT(color[1][2], color[1][1], color[0][2], color[1][3]);
00127
00128 if (r > 0)
00129 product1b = color[1][2];
00130 else if (r < 0)
00131 product1b = color[1][1];
00132 else
00133 product1b = INTERPOLATE(color[1][1], color[1][2]);
00134
00135 product2b = product1b;
00136 } else {
00137 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])
00138 product2b = Q_INTERPOLATE(color[2][2], color[2][2], color[2][2], color[2][1]);
00139 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])
00140 product2b = Q_INTERPOLATE(color[2][1], color[2][1], color[2][1], color[2][2]);
00141 else
00142 product2b = INTERPOLATE(color[2][1], color[2][2]);
00143
00144 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])
00145 product1b = Q_INTERPOLATE(color[1][2], color[1][2], color[1][2], color[1][1]);
00146 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])
00147 product1b = Q_INTERPOLATE(color[1][2], color[1][1], color[1][1], color[1][1]);
00148 else
00149 product1b = INTERPOLATE(color[1][1], color[1][2]);
00150 }
00151
00152 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])
00153 product2a = INTERPOLATE(color[2][1], color[1][1]);
00154 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])
00155 product2a = INTERPOLATE(color[2][1], color[1][1]);
00156 else
00157 product2a = color[2][1];
00158
00159 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])
00160 product1a = INTERPOLATE(color[2][1], color[1][1]);
00161 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])
00162 product1a = INTERPOLATE(color[2][1], color[1][1]);
00163 else
00164 product1a = color[1][1];
00165
00166
00167 switch (bpp) {
00168 case 4:
00169 AV_WN32A(dst_line[0] + x * 8, product1a);
00170 AV_WN32A(dst_line[0] + x * 8 + 4, product1b);
00171 AV_WN32A(dst_line[1] + x * 8, product2a);
00172 AV_WN32A(dst_line[1] + x * 8 + 4, product2b);
00173 break;
00174 case 3:
00175 AV_WL24(dst_line[0] + x * 6, product1a);
00176 AV_WL24(dst_line[0] + x * 6 + 3, product1b);
00177 AV_WL24(dst_line[1] + x * 6, product2a);
00178 AV_WL24(dst_line[1] + x * 6 + 3, product2b);
00179 break;
00180 default:
00181 if (sai->is_be) {
00182 AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
00183 AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
00184 } else {
00185 AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
00186 AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
00187 }
00188 }
00189
00190
00191 color[0][0] = color[0][1]; color[0][1] = color[0][2]; color[0][2] = color[0][3];
00192 color[1][0] = color[1][1]; color[1][1] = color[1][2]; color[1][2] = color[1][3];
00193 color[2][0] = color[2][1]; color[2][1] = color[2][2]; color[2][2] = color[2][3];
00194 color[3][0] = color[3][1]; color[3][1] = color[3][2]; color[3][2] = color[3][3];
00195
00196 if (x < width - 3) {
00197 x += 3;
00198 switch (bpp) {
00199 case 4:
00200 READ_COLOR4(color[0][3], src_line[0], x);
00201 READ_COLOR4(color[1][3], src_line[1], x);
00202 READ_COLOR4(color[2][3], src_line[2], x);
00203 READ_COLOR4(color[3][3], src_line[3], x);
00204 break;
00205 case 3:
00206 READ_COLOR3(color[0][3], src_line[0], x);
00207 READ_COLOR3(color[1][3], src_line[1], x);
00208 READ_COLOR3(color[2][3], src_line[2], x);
00209 READ_COLOR3(color[3][3], src_line[3], x);
00210 break;
00211 default:
00212 READ_COLOR2(color[0][3], src_line[0], x);
00213 READ_COLOR2(color[1][3], src_line[1], x);
00214 READ_COLOR2(color[2][3], src_line[2], x);
00215 READ_COLOR2(color[3][3], src_line[3], x);
00216 }
00217 x -= 3;
00218 }
00219 }
00220
00221
00222 src_line[0] = src_line[1];
00223 src_line[1] = src_line[2];
00224 src_line[2] = src_line[3];
00225
00226
00227 src_line[3] = src_line[2];
00228 if (y < height - 3)
00229 src_line[3] += src_linesize;
00230 }
00231 }
00232
00233 static int query_formats(AVFilterContext *ctx)
00234 {
00235 static const enum PixelFormat pix_fmts[] = {
00236 PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
00237 PIX_FMT_RGB24, PIX_FMT_BGR24,
00238 PIX_FMT_RGB565BE, PIX_FMT_BGR565BE, PIX_FMT_RGB555BE, PIX_FMT_BGR555BE,
00239 PIX_FMT_RGB565LE, PIX_FMT_BGR565LE, PIX_FMT_RGB555LE, PIX_FMT_BGR555LE,
00240 PIX_FMT_NONE
00241 };
00242
00243 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00244 return 0;
00245 }
00246
00247 static int config_input(AVFilterLink *inlink)
00248 {
00249 Super2xSaIContext *sai = inlink->dst->priv;
00250
00251 sai->hi_pixel_mask = 0xFEFEFEFE;
00252 sai->lo_pixel_mask = 0x01010101;
00253 sai->q_hi_pixel_mask = 0xFCFCFCFC;
00254 sai->q_lo_pixel_mask = 0x03030303;
00255 sai->bpp = 4;
00256
00257 switch (inlink->format) {
00258 case PIX_FMT_RGB24:
00259 case PIX_FMT_BGR24:
00260 sai->bpp = 3;
00261 break;
00262
00263 case PIX_FMT_RGB565BE:
00264 case PIX_FMT_BGR565BE:
00265 sai->is_be = 1;
00266 case PIX_FMT_RGB565LE:
00267 case PIX_FMT_BGR565LE:
00268 sai->hi_pixel_mask = 0xF7DEF7DE;
00269 sai->lo_pixel_mask = 0x08210821;
00270 sai->q_hi_pixel_mask = 0xE79CE79C;
00271 sai->q_lo_pixel_mask = 0x18631863;
00272 sai->bpp = 2;
00273 break;
00274
00275 case PIX_FMT_BGR555BE:
00276 case PIX_FMT_RGB555BE:
00277 sai->is_be = 1;
00278 case PIX_FMT_BGR555LE:
00279 case PIX_FMT_RGB555LE:
00280 sai->hi_pixel_mask = 0x7BDE7BDE;
00281 sai->lo_pixel_mask = 0x04210421;
00282 sai->q_hi_pixel_mask = 0x739C739C;
00283 sai->q_lo_pixel_mask = 0x0C630C63;
00284 sai->bpp = 2;
00285 break;
00286 }
00287
00288 return 0;
00289 }
00290
00291 static int config_output(AVFilterLink *outlink)
00292 {
00293 AVFilterLink *inlink = outlink->src->inputs[0];
00294
00295 outlink->w = inlink->w*2;
00296 outlink->h = inlink->h*2;
00297
00298 av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
00299 av_get_pix_fmt_name(inlink->format),
00300 inlink->w, inlink->h, outlink->w, outlink->h);
00301
00302 return 0;
00303 }
00304
00305 static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
00306
00307 static int end_frame(AVFilterLink *inlink)
00308 {
00309 AVFilterLink *outlink = inlink->dst->outputs[0];
00310 AVFilterBufferRef *inpicref = inlink->cur_buf;
00311 AVFilterBufferRef *outpicref = outlink->out_buf;
00312
00313 super2xsai(inlink->dst, inpicref->data[0], inpicref->linesize[0],
00314 outpicref->data[0], outpicref->linesize[0],
00315 inlink->w, inlink->h);
00316
00317 ff_draw_slice(outlink, 0, outlink->h, 1);
00318 return ff_end_frame(outlink);
00319 }
00320
00321 AVFilter avfilter_vf_super2xsai = {
00322 .name = "super2xsai",
00323 .description = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
00324 .priv_size = sizeof(Super2xSaIContext),
00325 .query_formats = query_formats,
00326
00327 .inputs = (const AVFilterPad[]) {
00328 { .name = "default",
00329 .type = AVMEDIA_TYPE_VIDEO,
00330 .config_props = config_input,
00331 .draw_slice = null_draw_slice,
00332 .end_frame = end_frame,
00333 .min_perms = AV_PERM_READ },
00334 { .name = NULL }
00335 },
00336 .outputs = (const AVFilterPad[]) {
00337 { .name = "default",
00338 .type = AVMEDIA_TYPE_VIDEO,
00339 .config_props = config_output },
00340 { .name = NULL }
00341 },
00342 };