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.

288 lines
8.7KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/opt.h"
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/pixdesc.h"
  21. #include "internal.h"
  22. typedef struct EPXContext {
  23. const AVClass *class;
  24. int n;
  25. int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  26. } EPXContext;
  27. typedef struct ThreadData {
  28. AVFrame *in, *out;
  29. } ThreadData;
  30. #define OFFSET(x) offsetof(EPXContext, x)
  31. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  32. static const AVOption epx_options[] = {
  33. { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS },
  34. { NULL }
  35. };
  36. AVFILTER_DEFINE_CLASS(epx);
  37. static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  38. {
  39. ThreadData *td = arg;
  40. const AVFrame *in = td->in;
  41. AVFrame *out = td->out;
  42. const int slice_start = (in->height * jobnr ) / nb_jobs;
  43. const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
  44. for (int p = 0; p < 1; p++) {
  45. const int width = in->width;
  46. const int height = in->height;
  47. const int src_linesize = in->linesize[p] / 4;
  48. const int dst_linesize = out->linesize[p] / 4;
  49. const uint32_t *src = (const uint32_t *)in->data[p];
  50. uint32_t *dst = (uint32_t *)out->data[p];
  51. const uint32_t *src_line[3];
  52. src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
  53. src_line[1] = src + src_linesize * slice_start;
  54. src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
  55. for (int y = slice_start; y < slice_end; y++) {
  56. uint32_t *dst_line[2];
  57. dst_line[0] = dst + dst_linesize*2*y;
  58. dst_line[1] = dst + dst_linesize*(2*y+1);
  59. for (int x = 0; x < width; x++) {
  60. uint32_t E0, E1, E2, E3;
  61. uint32_t B, D, E, F, H;
  62. B = src_line[0][x];
  63. D = src_line[1][FFMAX(x-1, 0)];
  64. E = src_line[1][x];
  65. F = src_line[1][FFMIN(x+1, width - 1)];
  66. H = src_line[2][x];
  67. if (B != H && D != F) {
  68. E0 = D == B ? D : E;
  69. E1 = B == F ? F : E;
  70. E2 = D == H ? D : E;
  71. E3 = H == F ? F : E;
  72. } else {
  73. E0 = E;
  74. E1 = E;
  75. E2 = E;
  76. E3 = E;
  77. }
  78. dst_line[0][x*2] = E0;
  79. dst_line[0][x*2+1] = E1;
  80. dst_line[1][x*2] = E2;
  81. dst_line[1][x*2+1] = E3;
  82. }
  83. src_line[0] = src_line[1];
  84. src_line[1] = src_line[2];
  85. src_line[2] = src_line[1];
  86. if (y < height - 1)
  87. src_line[2] += src_linesize;
  88. }
  89. }
  90. return 0;
  91. }
  92. static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  93. {
  94. ThreadData *td = arg;
  95. const AVFrame *in = td->in;
  96. AVFrame *out = td->out;
  97. const int slice_start = (in->height * jobnr ) / nb_jobs;
  98. const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
  99. for (int p = 0; p < 1; p++) {
  100. const int width = in->width;
  101. const int height = in->height;
  102. const int src_linesize = in->linesize[p] / 4;
  103. const int dst_linesize = out->linesize[p] / 4;
  104. const uint32_t *src = (const uint32_t *)in->data[p];
  105. uint32_t *dst = (uint32_t *)out->data[p];
  106. const uint32_t *src_line[3];
  107. src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
  108. src_line[1] = src + src_linesize * slice_start;
  109. src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
  110. for (int y = slice_start; y < slice_end; y++) {
  111. uint32_t *dst_line[3];
  112. dst_line[0] = dst + dst_linesize*3*y;
  113. dst_line[1] = dst + dst_linesize*(3*y+1);
  114. dst_line[2] = dst + dst_linesize*(3*y+2);
  115. for (int x = 0; x < width; x++) {
  116. uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8;
  117. uint32_t A, B, C, D, E, F, G, H, I;
  118. A = src_line[0][FFMAX(x-1, 0)];
  119. B = src_line[0][x];
  120. C = src_line[0][FFMIN(x+1, width - 1)];
  121. D = src_line[1][FFMAX(x-1, 0)];
  122. E = src_line[1][x];
  123. F = src_line[1][FFMIN(x+1, width - 1)];
  124. G = src_line[2][FFMAX(x-1, 0)];
  125. H = src_line[2][x];
  126. I = src_line[2][FFMIN(x+1, width - 1)];
  127. if (B != H && D != F) {
  128. E0 = D == B ? D : E;
  129. E1 = (D == B && E != C) || (B == F && E != A) ? B : E;
  130. E2 = B == F ? F : E;
  131. E3 = (D == B && E != G) || (D == H && E != A) ? D : E;
  132. E4 = E;
  133. E5 = (B == F && E != I) || (H == F && E != C) ? F : E;
  134. E6 = D == H ? D : E;
  135. E7 = (D == H && E != I) || (H == F && E != G) ? H : E;
  136. E8 = H == F ? F : E;
  137. } else {
  138. E0 = E;
  139. E1 = E;
  140. E2 = E;
  141. E3 = E;
  142. E4 = E;
  143. E5 = E;
  144. E6 = E;
  145. E7 = E;
  146. E8 = E;
  147. }
  148. dst_line[0][x*3] = E0;
  149. dst_line[0][x*3+1] = E1;
  150. dst_line[0][x*3+2] = E2;
  151. dst_line[1][x*3] = E3;
  152. dst_line[1][x*3+1] = E4;
  153. dst_line[1][x*3+2] = E5;
  154. dst_line[2][x*3] = E6;
  155. dst_line[2][x*3+1] = E7;
  156. dst_line[2][x*3+2] = E8;
  157. }
  158. src_line[0] = src_line[1];
  159. src_line[1] = src_line[2];
  160. src_line[2] = src_line[1];
  161. if (y < height - 1)
  162. src_line[2] += src_linesize;
  163. }
  164. }
  165. return 0;
  166. }
  167. static int config_output(AVFilterLink *outlink)
  168. {
  169. AVFilterContext *ctx = outlink->src;
  170. EPXContext *s = ctx->priv;
  171. AVFilterLink *inlink = ctx->inputs[0];
  172. const AVPixFmtDescriptor *desc;
  173. desc = av_pix_fmt_desc_get(outlink->format);
  174. if (!desc)
  175. return AVERROR_BUG;
  176. outlink->w = inlink->w * s->n;
  177. outlink->h = inlink->h * s->n;
  178. switch (s->n) {
  179. case 2:
  180. s->epx_slice = epx2_slice;
  181. break;
  182. case 3:
  183. s->epx_slice = epx3_slice;
  184. break;
  185. }
  186. return 0;
  187. }
  188. static int query_formats(AVFilterContext *ctx)
  189. {
  190. static const enum AVPixelFormat pix_fmts[] = {
  191. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  192. AV_PIX_FMT_NONE,
  193. };
  194. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  195. if (!fmts_list)
  196. return AVERROR(ENOMEM);
  197. return ff_set_common_formats(ctx, fmts_list);
  198. }
  199. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  200. {
  201. AVFilterContext *ctx = inlink->dst;
  202. AVFilterLink *outlink = ctx->outputs[0];
  203. EPXContext *s = ctx->priv;
  204. ThreadData td;
  205. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  206. if (!out) {
  207. av_frame_free(&in);
  208. return AVERROR(ENOMEM);
  209. }
  210. av_frame_copy_props(out, in);
  211. td.in = in, td.out = out;
  212. ctx->internal->execute(ctx, s->epx_slice, &td, NULL, FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
  213. av_frame_free(&in);
  214. return ff_filter_frame(outlink, out);
  215. }
  216. static const AVFilterPad inputs[] = {
  217. {
  218. .name = "default",
  219. .type = AVMEDIA_TYPE_VIDEO,
  220. .filter_frame = filter_frame,
  221. },
  222. { NULL }
  223. };
  224. static const AVFilterPad outputs[] = {
  225. {
  226. .name = "default",
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .config_props = config_output,
  229. },
  230. { NULL }
  231. };
  232. AVFilter ff_vf_epx = {
  233. .name = "epx",
  234. .description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."),
  235. .inputs = inputs,
  236. .outputs = outputs,
  237. .query_formats = query_formats,
  238. .priv_size = sizeof(EPXContext),
  239. .priv_class = &epx_class,
  240. .flags = AVFILTER_FLAG_SLICE_THREADS,
  241. };