vf_super2xsai.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2010 Niel van der Westhuizen <nielkie@gmail.com>
  3. * Copyright (c) 2002 A'rpi
  4. * Copyright (c) 1997-2001 ZSNES Team ( zsknight@zsnes.com / _demo_@zsnes.com )
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  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
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * Super 2xSaI video filter
  25. * Ported from MPlayer libmpcodecs/vf_2xsai.c.
  26. */
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "video.h"
  32. typedef struct {
  33. /* masks used for two pixels interpolation */
  34. uint32_t hi_pixel_mask;
  35. uint32_t lo_pixel_mask;
  36. /* masks used for four pixels interpolation */
  37. uint32_t q_hi_pixel_mask;
  38. uint32_t q_lo_pixel_mask;
  39. int bpp; ///< bytes per pixel, pixel stride for each (packed) pixel
  40. int is_be;
  41. } Super2xSaIContext;
  42. #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
  43. #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
  44. #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) \
  45. + ((((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)
  46. static void super2xsai(AVFilterContext *ctx,
  47. uint8_t *src, int src_linesize,
  48. uint8_t *dst, int dst_linesize,
  49. int width, int height)
  50. {
  51. Super2xSaIContext *sai = ctx->priv;
  52. unsigned int x, y;
  53. uint32_t color[4][4];
  54. unsigned char *src_line[4];
  55. const int bpp = sai->bpp;
  56. const uint32_t hi_pixel_mask = sai->hi_pixel_mask;
  57. const uint32_t lo_pixel_mask = sai->lo_pixel_mask;
  58. const uint32_t q_hi_pixel_mask = sai->q_hi_pixel_mask;
  59. const uint32_t q_lo_pixel_mask = sai->q_lo_pixel_mask;
  60. /* Point to the first 4 lines, first line is duplicated */
  61. src_line[0] = src;
  62. src_line[1] = src;
  63. src_line[2] = src + src_linesize*FFMIN(1, height-1);
  64. src_line[3] = src + src_linesize*FFMIN(2, height-1);
  65. #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
  66. #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
  67. #define READ_COLOR2(dst, src_line, off) dst = sai->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
  68. for (y = 0; y < height; y++) {
  69. uint8_t *dst_line[2];
  70. dst_line[0] = dst + dst_linesize*2*y;
  71. dst_line[1] = dst + dst_linesize*(2*y+1);
  72. switch (bpp) {
  73. case 4:
  74. 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);
  75. 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);
  76. 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);
  77. 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);
  78. break;
  79. case 3:
  80. 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);
  81. 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);
  82. 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);
  83. 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);
  84. break;
  85. default:
  86. 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);
  87. 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);
  88. 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);
  89. 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);
  90. }
  91. for (x = 0; x < width; x++) {
  92. uint32_t product1a, product1b, product2a, product2b;
  93. //--------------------------------------- B0 B1 B2 B3 0 1 2 3
  94. // 4 5* 6 S2 -> 4 5* 6 7
  95. // 1 2 3 S1 8 9 10 11
  96. // A0 A1 A2 A3 12 13 14 15
  97. //--------------------------------------
  98. if (color[2][1] == color[1][2] && color[1][1] != color[2][2]) {
  99. product2b = color[2][1];
  100. product1b = product2b;
  101. } else if (color[1][1] == color[2][2] && color[2][1] != color[1][2]) {
  102. product2b = color[1][1];
  103. product1b = product2b;
  104. } else if (color[1][1] == color[2][2] && color[2][1] == color[1][2]) {
  105. int r = 0;
  106. r += GET_RESULT(color[1][2], color[1][1], color[1][0], color[3][1]);
  107. r += GET_RESULT(color[1][2], color[1][1], color[2][0], color[0][1]);
  108. r += GET_RESULT(color[1][2], color[1][1], color[3][2], color[2][3]);
  109. r += GET_RESULT(color[1][2], color[1][1], color[0][2], color[1][3]);
  110. if (r > 0)
  111. product1b = color[1][2];
  112. else if (r < 0)
  113. product1b = color[1][1];
  114. else
  115. product1b = INTERPOLATE(color[1][1], color[1][2]);
  116. product2b = product1b;
  117. } else {
  118. 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])
  119. product2b = Q_INTERPOLATE(color[2][2], color[2][2], color[2][2], color[2][1]);
  120. 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])
  121. product2b = Q_INTERPOLATE(color[2][1], color[2][1], color[2][1], color[2][2]);
  122. else
  123. product2b = INTERPOLATE(color[2][1], color[2][2]);
  124. 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])
  125. product1b = Q_INTERPOLATE(color[1][2], color[1][2], color[1][2], color[1][1]);
  126. 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])
  127. product1b = Q_INTERPOLATE(color[1][2], color[1][1], color[1][1], color[1][1]);
  128. else
  129. product1b = INTERPOLATE(color[1][1], color[1][2]);
  130. }
  131. 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])
  132. product2a = INTERPOLATE(color[2][1], color[1][1]);
  133. 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])
  134. product2a = INTERPOLATE(color[2][1], color[1][1]);
  135. else
  136. product2a = color[2][1];
  137. 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])
  138. product1a = INTERPOLATE(color[2][1], color[1][1]);
  139. 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])
  140. product1a = INTERPOLATE(color[2][1], color[1][1]);
  141. else
  142. product1a = color[1][1];
  143. /* Set the calculated pixels */
  144. switch (bpp) {
  145. case 4:
  146. AV_WN32A(dst_line[0] + x * 8, product1a);
  147. AV_WN32A(dst_line[0] + x * 8 + 4, product1b);
  148. AV_WN32A(dst_line[1] + x * 8, product2a);
  149. AV_WN32A(dst_line[1] + x * 8 + 4, product2b);
  150. break;
  151. case 3:
  152. AV_WL24(dst_line[0] + x * 6, product1a);
  153. AV_WL24(dst_line[0] + x * 6 + 3, product1b);
  154. AV_WL24(dst_line[1] + x * 6, product2a);
  155. AV_WL24(dst_line[1] + x * 6 + 3, product2b);
  156. break;
  157. default: // bpp = 2
  158. if (sai->is_be) {
  159. AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
  160. AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
  161. } else {
  162. AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
  163. AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
  164. }
  165. }
  166. /* Move color matrix forward */
  167. color[0][0] = color[0][1]; color[0][1] = color[0][2]; color[0][2] = color[0][3];
  168. color[1][0] = color[1][1]; color[1][1] = color[1][2]; color[1][2] = color[1][3];
  169. color[2][0] = color[2][1]; color[2][1] = color[2][2]; color[2][2] = color[2][3];
  170. color[3][0] = color[3][1]; color[3][1] = color[3][2]; color[3][2] = color[3][3];
  171. if (x < width - 3) {
  172. x += 3;
  173. switch (bpp) {
  174. case 4:
  175. READ_COLOR4(color[0][3], src_line[0], x);
  176. READ_COLOR4(color[1][3], src_line[1], x);
  177. READ_COLOR4(color[2][3], src_line[2], x);
  178. READ_COLOR4(color[3][3], src_line[3], x);
  179. break;
  180. case 3:
  181. READ_COLOR3(color[0][3], src_line[0], x);
  182. READ_COLOR3(color[1][3], src_line[1], x);
  183. READ_COLOR3(color[2][3], src_line[2], x);
  184. READ_COLOR3(color[3][3], src_line[3], x);
  185. break;
  186. default: /* case 2 */
  187. READ_COLOR2(color[0][3], src_line[0], x);
  188. READ_COLOR2(color[1][3], src_line[1], x);
  189. READ_COLOR2(color[2][3], src_line[2], x);
  190. READ_COLOR2(color[3][3], src_line[3], x);
  191. }
  192. x -= 3;
  193. }
  194. }
  195. /* We're done with one line, so we shift the source lines up */
  196. src_line[0] = src_line[1];
  197. src_line[1] = src_line[2];
  198. src_line[2] = src_line[3];
  199. /* Read next line */
  200. src_line[3] = src_line[2];
  201. if (y < height - 3)
  202. src_line[3] += src_linesize;
  203. } // y loop
  204. }
  205. static int query_formats(AVFilterContext *ctx)
  206. {
  207. static const enum PixelFormat pix_fmts[] = {
  208. PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
  209. PIX_FMT_RGB24, PIX_FMT_BGR24,
  210. PIX_FMT_RGB565BE, PIX_FMT_BGR565BE, PIX_FMT_RGB555BE, PIX_FMT_BGR555BE,
  211. PIX_FMT_RGB565LE, PIX_FMT_BGR565LE, PIX_FMT_RGB555LE, PIX_FMT_BGR555LE,
  212. PIX_FMT_NONE
  213. };
  214. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  215. return 0;
  216. }
  217. static int config_input(AVFilterLink *inlink)
  218. {
  219. Super2xSaIContext *sai = inlink->dst->priv;
  220. sai->hi_pixel_mask = 0xFEFEFEFE;
  221. sai->lo_pixel_mask = 0x01010101;
  222. sai->q_hi_pixel_mask = 0xFCFCFCFC;
  223. sai->q_lo_pixel_mask = 0x03030303;
  224. sai->bpp = 4;
  225. switch (inlink->format) {
  226. case PIX_FMT_RGB24:
  227. case PIX_FMT_BGR24:
  228. sai->bpp = 3;
  229. break;
  230. case PIX_FMT_RGB565BE:
  231. case PIX_FMT_BGR565BE:
  232. sai->is_be = 1;
  233. case PIX_FMT_RGB565LE:
  234. case PIX_FMT_BGR565LE:
  235. sai->hi_pixel_mask = 0xF7DEF7DE;
  236. sai->lo_pixel_mask = 0x08210821;
  237. sai->q_hi_pixel_mask = 0xE79CE79C;
  238. sai->q_lo_pixel_mask = 0x18631863;
  239. sai->bpp = 2;
  240. break;
  241. case PIX_FMT_BGR555BE:
  242. case PIX_FMT_RGB555BE:
  243. sai->is_be = 1;
  244. case PIX_FMT_BGR555LE:
  245. case PIX_FMT_RGB555LE:
  246. sai->hi_pixel_mask = 0x7BDE7BDE;
  247. sai->lo_pixel_mask = 0x04210421;
  248. sai->q_hi_pixel_mask = 0x739C739C;
  249. sai->q_lo_pixel_mask = 0x0C630C63;
  250. sai->bpp = 2;
  251. break;
  252. }
  253. return 0;
  254. }
  255. static int config_output(AVFilterLink *outlink)
  256. {
  257. AVFilterLink *inlink = outlink->src->inputs[0];
  258. outlink->w = inlink->w*2;
  259. outlink->h = inlink->h*2;
  260. av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
  261. av_get_pix_fmt_name(inlink->format),
  262. inlink->w, inlink->h, outlink->w, outlink->h);
  263. return 0;
  264. }
  265. static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
  266. static int end_frame(AVFilterLink *inlink)
  267. {
  268. AVFilterLink *outlink = inlink->dst->outputs[0];
  269. AVFilterBufferRef *inpicref = inlink->cur_buf;
  270. AVFilterBufferRef *outpicref = outlink->out_buf;
  271. super2xsai(inlink->dst, inpicref->data[0], inpicref->linesize[0],
  272. outpicref->data[0], outpicref->linesize[0],
  273. inlink->w, inlink->h);
  274. ff_draw_slice(outlink, 0, outlink->h, 1);
  275. return ff_end_frame(outlink);
  276. }
  277. AVFilter avfilter_vf_super2xsai = {
  278. .name = "super2xsai",
  279. .description = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
  280. .priv_size = sizeof(Super2xSaIContext),
  281. .query_formats = query_formats,
  282. .inputs = (const AVFilterPad[]) {
  283. { .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .config_props = config_input,
  286. .draw_slice = null_draw_slice,
  287. .end_frame = end_frame,
  288. .min_perms = AV_PERM_READ },
  289. { .name = NULL }
  290. },
  291. .outputs = (const AVFilterPad[]) {
  292. { .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .config_props = config_output },
  295. { .name = NULL }
  296. },
  297. };