You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
15KB

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