vf_super2xsai.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "internal.h"
  32. #include "video.h"
  33. typedef struct Super2xSaIContext {
  34. /* masks used for two pixels interpolation */
  35. uint32_t hi_pixel_mask;
  36. uint32_t lo_pixel_mask;
  37. /* masks used for four pixels interpolation */
  38. uint32_t q_hi_pixel_mask;
  39. uint32_t q_lo_pixel_mask;
  40. int bpp; ///< bytes per pixel, pixel stride for each (packed) pixel
  41. int is_be;
  42. } Super2xSaIContext;
  43. #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
  44. #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
  45. #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) \
  46. + ((((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)
  47. static void super2xsai(AVFilterContext *ctx,
  48. uint8_t *src, int src_linesize,
  49. uint8_t *dst, int dst_linesize,
  50. int width, int height)
  51. {
  52. Super2xSaIContext *s = ctx->priv;
  53. unsigned int x, y;
  54. uint32_t color[4][4];
  55. unsigned char *src_line[4];
  56. const int bpp = s->bpp;
  57. const uint32_t hi_pixel_mask = s->hi_pixel_mask;
  58. const uint32_t lo_pixel_mask = s->lo_pixel_mask;
  59. const uint32_t q_hi_pixel_mask = s->q_hi_pixel_mask;
  60. const uint32_t q_lo_pixel_mask = s->q_lo_pixel_mask;
  61. /* Point to the first 4 lines, first line is duplicated */
  62. src_line[0] = src;
  63. src_line[1] = src;
  64. src_line[2] = src + src_linesize*FFMIN(1, height-1);
  65. src_line[3] = src + src_linesize*FFMIN(2, height-1);
  66. #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
  67. #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
  68. #define READ_COLOR2(dst, src_line, off) dst = s->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
  69. for (y = 0; y < height; y++) {
  70. uint8_t *dst_line[2];
  71. dst_line[0] = dst + dst_linesize*2*y;
  72. dst_line[1] = dst + dst_linesize*(2*y+1);
  73. switch (bpp) {
  74. case 4:
  75. 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);
  76. 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);
  77. 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);
  78. 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);
  79. break;
  80. case 3:
  81. 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);
  82. 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);
  83. 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);
  84. 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);
  85. break;
  86. default:
  87. 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);
  88. 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);
  89. 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);
  90. 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);
  91. }
  92. for (x = 0; x < width; x++) {
  93. uint32_t product1a, product1b, product2a, product2b;
  94. //--------------------------------------- B0 B1 B2 B3 0 1 2 3
  95. // 4 5* 6 S2 -> 4 5* 6 7
  96. // 1 2 3 S1 8 9 10 11
  97. // A0 A1 A2 A3 12 13 14 15
  98. //--------------------------------------
  99. if (color[2][1] == color[1][2] && color[1][1] != color[2][2]) {
  100. product2b = color[2][1];
  101. product1b = product2b;
  102. } else if (color[1][1] == color[2][2] && color[2][1] != color[1][2]) {
  103. product2b = color[1][1];
  104. product1b = product2b;
  105. } else if (color[1][1] == color[2][2] && color[2][1] == color[1][2]) {
  106. int r = 0;
  107. r += GET_RESULT(color[1][2], color[1][1], color[1][0], color[3][1]);
  108. r += GET_RESULT(color[1][2], color[1][1], color[2][0], color[0][1]);
  109. r += GET_RESULT(color[1][2], color[1][1], color[3][2], color[2][3]);
  110. r += GET_RESULT(color[1][2], color[1][1], color[0][2], color[1][3]);
  111. if (r > 0)
  112. product1b = color[1][2];
  113. else if (r < 0)
  114. product1b = color[1][1];
  115. else
  116. product1b = INTERPOLATE(color[1][1], color[1][2]);
  117. product2b = product1b;
  118. } else {
  119. 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])
  120. product2b = Q_INTERPOLATE(color[2][2], color[2][2], color[2][2], color[2][1]);
  121. 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])
  122. product2b = Q_INTERPOLATE(color[2][1], color[2][1], color[2][1], color[2][2]);
  123. else
  124. product2b = INTERPOLATE(color[2][1], color[2][2]);
  125. 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])
  126. product1b = Q_INTERPOLATE(color[1][2], color[1][2], color[1][2], color[1][1]);
  127. 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])
  128. product1b = Q_INTERPOLATE(color[1][2], color[1][1], color[1][1], color[1][1]);
  129. else
  130. product1b = INTERPOLATE(color[1][1], color[1][2]);
  131. }
  132. 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])
  133. product2a = INTERPOLATE(color[2][1], color[1][1]);
  134. 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])
  135. product2a = INTERPOLATE(color[2][1], color[1][1]);
  136. else
  137. product2a = color[2][1];
  138. 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])
  139. product1a = INTERPOLATE(color[2][1], color[1][1]);
  140. 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])
  141. product1a = INTERPOLATE(color[2][1], color[1][1]);
  142. else
  143. product1a = color[1][1];
  144. /* Set the calculated pixels */
  145. switch (bpp) {
  146. case 4:
  147. AV_WN32A(dst_line[0] + x * 8, product1a);
  148. AV_WN32A(dst_line[0] + x * 8 + 4, product1b);
  149. AV_WN32A(dst_line[1] + x * 8, product2a);
  150. AV_WN32A(dst_line[1] + x * 8 + 4, product2b);
  151. break;
  152. case 3:
  153. AV_WL24(dst_line[0] + x * 6, product1a);
  154. AV_WL24(dst_line[0] + x * 6 + 3, product1b);
  155. AV_WL24(dst_line[1] + x * 6, product2a);
  156. AV_WL24(dst_line[1] + x * 6 + 3, product2b);
  157. break;
  158. default: // bpp = 2
  159. if (s->is_be) {
  160. AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
  161. AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
  162. } else {
  163. AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
  164. AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
  165. }
  166. }
  167. /* Move color matrix forward */
  168. color[0][0] = color[0][1]; color[0][1] = color[0][2]; color[0][2] = color[0][3];
  169. color[1][0] = color[1][1]; color[1][1] = color[1][2]; color[1][2] = color[1][3];
  170. color[2][0] = color[2][1]; color[2][1] = color[2][2]; color[2][2] = color[2][3];
  171. color[3][0] = color[3][1]; color[3][1] = color[3][2]; color[3][2] = color[3][3];
  172. if (x < width - 3) {
  173. x += 3;
  174. switch (bpp) {
  175. case 4:
  176. READ_COLOR4(color[0][3], src_line[0], x);
  177. READ_COLOR4(color[1][3], src_line[1], x);
  178. READ_COLOR4(color[2][3], src_line[2], x);
  179. READ_COLOR4(color[3][3], src_line[3], x);
  180. break;
  181. case 3:
  182. READ_COLOR3(color[0][3], src_line[0], x);
  183. READ_COLOR3(color[1][3], src_line[1], x);
  184. READ_COLOR3(color[2][3], src_line[2], x);
  185. READ_COLOR3(color[3][3], src_line[3], x);
  186. break;
  187. default: /* case 2 */
  188. READ_COLOR2(color[0][3], src_line[0], x);
  189. READ_COLOR2(color[1][3], src_line[1], x);
  190. READ_COLOR2(color[2][3], src_line[2], x);
  191. READ_COLOR2(color[3][3], src_line[3], x);
  192. }
  193. x -= 3;
  194. }
  195. }
  196. /* We're done with one line, so we shift the source lines up */
  197. src_line[0] = src_line[1];
  198. src_line[1] = src_line[2];
  199. src_line[2] = src_line[3];
  200. /* Read next line */
  201. src_line[3] = src_line[2];
  202. if (y < height - 3)
  203. src_line[3] += src_linesize;
  204. } // y loop
  205. }
  206. static int query_formats(AVFilterContext *ctx)
  207. {
  208. static const enum AVPixelFormat pix_fmts[] = {
  209. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  210. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  211. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_BGR565BE, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_BGR555BE,
  212. AV_PIX_FMT_RGB565LE, AV_PIX_FMT_BGR565LE, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_BGR555LE,
  213. AV_PIX_FMT_NONE
  214. };
  215. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  216. if (!fmts_list)
  217. return AVERROR(ENOMEM);
  218. return ff_set_common_formats(ctx, fmts_list);
  219. }
  220. static int config_input(AVFilterLink *inlink)
  221. {
  222. Super2xSaIContext *s = inlink->dst->priv;
  223. s->hi_pixel_mask = 0xFEFEFEFE;
  224. s->lo_pixel_mask = 0x01010101;
  225. s->q_hi_pixel_mask = 0xFCFCFCFC;
  226. s->q_lo_pixel_mask = 0x03030303;
  227. s->bpp = 4;
  228. switch (inlink->format) {
  229. case AV_PIX_FMT_RGB24:
  230. case AV_PIX_FMT_BGR24:
  231. s->bpp = 3;
  232. break;
  233. case AV_PIX_FMT_RGB565BE:
  234. case AV_PIX_FMT_BGR565BE:
  235. s->is_be = 1;
  236. case AV_PIX_FMT_RGB565LE:
  237. case AV_PIX_FMT_BGR565LE:
  238. s->hi_pixel_mask = 0xF7DEF7DE;
  239. s->lo_pixel_mask = 0x08210821;
  240. s->q_hi_pixel_mask = 0xE79CE79C;
  241. s->q_lo_pixel_mask = 0x18631863;
  242. s->bpp = 2;
  243. break;
  244. case AV_PIX_FMT_BGR555BE:
  245. case AV_PIX_FMT_RGB555BE:
  246. s->is_be = 1;
  247. case AV_PIX_FMT_BGR555LE:
  248. case AV_PIX_FMT_RGB555LE:
  249. s->hi_pixel_mask = 0x7BDE7BDE;
  250. s->lo_pixel_mask = 0x04210421;
  251. s->q_hi_pixel_mask = 0x739C739C;
  252. s->q_lo_pixel_mask = 0x0C630C63;
  253. s->bpp = 2;
  254. break;
  255. }
  256. return 0;
  257. }
  258. static int config_output(AVFilterLink *outlink)
  259. {
  260. AVFilterLink *inlink = outlink->src->inputs[0];
  261. outlink->w = inlink->w*2;
  262. outlink->h = inlink->h*2;
  263. av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
  264. av_get_pix_fmt_name(inlink->format),
  265. inlink->w, inlink->h, outlink->w, outlink->h);
  266. return 0;
  267. }
  268. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  269. {
  270. AVFilterLink *outlink = inlink->dst->outputs[0];
  271. AVFrame *outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  272. if (!outpicref) {
  273. av_frame_free(&inpicref);
  274. return AVERROR(ENOMEM);
  275. }
  276. av_frame_copy_props(outpicref, inpicref);
  277. outpicref->width = outlink->w;
  278. outpicref->height = outlink->h;
  279. super2xsai(inlink->dst, inpicref->data[0], inpicref->linesize[0],
  280. outpicref->data[0], outpicref->linesize[0],
  281. inlink->w, inlink->h);
  282. av_frame_free(&inpicref);
  283. return ff_filter_frame(outlink, outpicref);
  284. }
  285. static const AVFilterPad super2xsai_inputs[] = {
  286. {
  287. .name = "default",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .config_props = config_input,
  290. .filter_frame = filter_frame,
  291. },
  292. { NULL }
  293. };
  294. static const AVFilterPad super2xsai_outputs[] = {
  295. {
  296. .name = "default",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .config_props = config_output,
  299. },
  300. { NULL }
  301. };
  302. AVFilter ff_vf_super2xsai = {
  303. .name = "super2xsai",
  304. .description = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
  305. .priv_size = sizeof(Super2xSaIContext),
  306. .query_formats = query_formats,
  307. .inputs = super2xsai_inputs,
  308. .outputs = super2xsai_outputs,
  309. };