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.

331 lines
15KB

  1. /*
  2. * Copyright (c) 2021 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "drawutils.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #include <float.h>
  30. typedef struct ShearContext {
  31. const AVClass *class;
  32. float shx, shy;
  33. int interp;
  34. uint8_t fillcolor[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  35. char *fillcolor_str;
  36. int fillcolor_enable;
  37. int nb_planes;
  38. int depth;
  39. FFDrawContext draw;
  40. FFDrawColor color;
  41. int hsub, vsub;
  42. int planewidth[4];
  43. int planeheight[4];
  44. int (*filter_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  45. } ShearContext;
  46. typedef struct ThreadData {
  47. AVFrame *in, *out;
  48. } ThreadData;
  49. #define OFFSET(x) offsetof(ShearContext, x)
  50. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  51. static const AVOption shear_options[] = {
  52. { "shx", "set x shear factor", OFFSET(shx), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -2, 2, .flags=FLAGS },
  53. { "shy", "set y shear factor", OFFSET(shy), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -2, 2, .flags=FLAGS },
  54. { "fillcolor", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, 0, 0, .flags=FLAGS },
  55. { "c", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, 0, 0, .flags=FLAGS },
  56. { "interp", "set interpolation", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags=FLAGS, "interp" },
  57. { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags=FLAGS, "interp" },
  58. { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags=FLAGS, "interp" },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(shear);
  62. static av_cold int init(AVFilterContext *ctx)
  63. {
  64. ShearContext *s = ctx->priv;
  65. if (!strcmp(s->fillcolor_str, "none"))
  66. s->fillcolor_enable = 0;
  67. else if (av_parse_color(s->fillcolor, s->fillcolor_str, -1, ctx) >= 0)
  68. s->fillcolor_enable = 1;
  69. else
  70. return AVERROR(EINVAL);
  71. return 0;
  72. }
  73. static int query_formats(AVFilterContext *ctx)
  74. {
  75. static const enum AVPixelFormat pix_fmts[] = {
  76. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  77. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
  78. AV_PIX_FMT_GRAY16,
  79. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  80. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  81. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  82. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  83. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  84. AV_PIX_FMT_YUVJ411P,
  85. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  86. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  87. AV_PIX_FMT_YUV440P10,
  88. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  89. AV_PIX_FMT_YUV440P12,
  90. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  91. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  92. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  93. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  94. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  95. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  96. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  97. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  98. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  99. AV_PIX_FMT_NONE
  100. };
  101. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  102. if (!fmts_list)
  103. return AVERROR(ENOMEM);
  104. return ff_set_common_formats(ctx, fmts_list);
  105. }
  106. #define NN(type, name) \
  107. static int filter_slice_nn##name(AVFilterContext *ctx, void *arg, int jobnr, \
  108. int nb_jobs) \
  109. { \
  110. ThreadData *td = arg; \
  111. AVFrame *in = td->in; \
  112. AVFrame *out = td->out; \
  113. ShearContext *s = ctx->priv; \
  114. const float shx = s->shx; \
  115. const float shy = s->shy; \
  116. \
  117. for (int p = 0; p < s->nb_planes; p++) { \
  118. const int hsub = (p == 1 || p == 2) ? s->hsub: 1; \
  119. const int vsub = (p == 1 || p == 2) ? s->vsub: 1; \
  120. const int width = s->planewidth[p]; \
  121. const int height = s->planeheight[p]; \
  122. const int wx = vsub * shx * height * 0.5f / hsub; \
  123. const int wy = hsub * shy * width * 0.5f / vsub; \
  124. const int slice_start = (height * jobnr) / nb_jobs; \
  125. const int slice_end = (height * (jobnr+1)) / nb_jobs; \
  126. const int src_linesize = in->linesize[p] / sizeof(type); \
  127. const int dst_linesize = out->linesize[p] / sizeof(type); \
  128. const type *src = (const type *)in->data[p]; \
  129. type *dst = (type *)out->data[p] + slice_start * dst_linesize; \
  130. \
  131. for (int y = slice_start; y < slice_end; y++) { \
  132. for (int x = 0; x < width; x++) { \
  133. int sx = x + vsub * shx * y / hsub - wx; \
  134. int sy = y + hsub * shy * x / vsub - wy; \
  135. \
  136. if (sx >= 0 && sx < width - 1 && \
  137. sy >= 0 && sy < height - 1) { \
  138. dst[x] = src[sy * src_linesize + sx]; \
  139. } \
  140. } \
  141. \
  142. dst += dst_linesize; \
  143. } \
  144. } \
  145. \
  146. return 0; \
  147. }
  148. NN(uint8_t, 8)
  149. NN(uint16_t, 16)
  150. #define BL(type, name) \
  151. static int filter_slice_bl##name(AVFilterContext *ctx, void *arg, int jobnr, \
  152. int nb_jobs) \
  153. { \
  154. ThreadData *td = arg; \
  155. AVFrame *in = td->in; \
  156. AVFrame *out = td->out; \
  157. ShearContext *s = ctx->priv; \
  158. const int depth = s->depth; \
  159. const float shx = s->shx; \
  160. const float shy = s->shy; \
  161. \
  162. for (int p = 0; p < s->nb_planes; p++) { \
  163. const int hsub = (p == 1 || p == 2) ? s->hsub: 1; \
  164. const int vsub = (p == 1 || p == 2) ? s->vsub: 1; \
  165. const int width = s->planewidth[p]; \
  166. const int height = s->planeheight[p]; \
  167. const float wx = vsub * shx * height * 0.5f / hsub; \
  168. const float wy = hsub * shy * width * 0.5f / vsub; \
  169. const int slice_start = (height * jobnr) / nb_jobs; \
  170. const int slice_end = (height * (jobnr+1)) / nb_jobs; \
  171. const int src_linesize = in->linesize[p] / sizeof(type); \
  172. const int dst_linesize = out->linesize[p] / sizeof(type); \
  173. const type *src = (const type *)in->data[p]; \
  174. type *dst = (type *)out->data[p] + slice_start * dst_linesize; \
  175. \
  176. for (int y = slice_start; y < slice_end; y++) { \
  177. for (int x = 0; x < width; x++) { \
  178. const float sx = x + vsub * shx * y / hsub - wx; \
  179. const float sy = y + hsub * shy * x / vsub - wy; \
  180. \
  181. if (sx >= 0 && sx < width - 1 && \
  182. sy >= 0 && sy < height - 1) { \
  183. float sum = 0.f; \
  184. int ax = floorf(sx); \
  185. int ay = floorf(sy); \
  186. float du = sx - ax; \
  187. float dv = sy - ay; \
  188. int bx = FFMIN(ax + 1, width - 1); \
  189. int by = FFMIN(ay + 1, height - 1); \
  190. \
  191. sum += (1.f - du) * (1.f - dv) * src[ay * src_linesize + ax];\
  192. sum += ( du) * (1.f - dv) * src[ay * src_linesize + bx];\
  193. sum += (1.f - du) * ( dv) * src[by * src_linesize + ax];\
  194. sum += ( du) * ( dv) * src[by * src_linesize + bx];\
  195. dst[x] = av_clip_uintp2_c(lrintf(sum), depth); \
  196. } \
  197. } \
  198. \
  199. dst += dst_linesize; \
  200. } \
  201. } \
  202. \
  203. return 0; \
  204. }
  205. BL(uint8_t, 8)
  206. BL(uint16_t, 16)
  207. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  208. {
  209. AVFilterContext *ctx = inlink->dst;
  210. ShearContext *s = ctx->priv;
  211. AVFilterLink *outlink = ctx->outputs[0];
  212. ThreadData td;
  213. AVFrame *out;
  214. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  215. if (!out) {
  216. av_frame_free(&in);
  217. return AVERROR(ENOMEM);
  218. }
  219. av_frame_copy_props(out, in);
  220. /* fill background */
  221. if (s->fillcolor_enable)
  222. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  223. 0, 0, outlink->w, outlink->h);
  224. td.in = in, td.out = out;
  225. ctx->internal->execute(ctx, s->filter_slice[s->interp], &td, NULL, FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
  226. av_frame_free(&in);
  227. return ff_filter_frame(outlink, out);
  228. }
  229. static int config_output(AVFilterLink *outlink)
  230. {
  231. AVFilterContext *ctx = outlink->src;
  232. ShearContext *s = ctx->priv;
  233. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  234. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  235. s->depth = desc->comp[0].depth;
  236. s->hsub = 1 << desc->log2_chroma_w;
  237. s->vsub = 1 << desc->log2_chroma_h;
  238. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(ctx->inputs[0]->w, desc->log2_chroma_w);
  239. s->planewidth[0] = s->planewidth[3] = ctx->inputs[0]->w;
  240. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(ctx->inputs[0]->h, desc->log2_chroma_h);
  241. s->planeheight[0] = s->planeheight[3] = ctx->inputs[0]->h;
  242. ff_draw_init(&s->draw, outlink->format, 0);
  243. ff_draw_color(&s->draw, &s->color, s->fillcolor);
  244. s->filter_slice[0] = s->depth <= 8 ? filter_slice_nn8 : filter_slice_nn16;
  245. s->filter_slice[1] = s->depth <= 8 ? filter_slice_bl8 : filter_slice_bl16;
  246. return 0;
  247. }
  248. static int process_command(AVFilterContext *ctx,
  249. const char *cmd,
  250. const char *arg,
  251. char *res,
  252. int res_len,
  253. int flags)
  254. {
  255. ShearContext *s = ctx->priv;
  256. int ret;
  257. ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
  258. if (ret < 0)
  259. return ret;
  260. ret = init(ctx);
  261. if (ret < 0)
  262. return ret;
  263. ff_draw_color(&s->draw, &s->color, s->fillcolor);
  264. return 0;
  265. }
  266. static const AVFilterPad inputs[] = {
  267. {
  268. .name = "default",
  269. .type = AVMEDIA_TYPE_VIDEO,
  270. .filter_frame = filter_frame,
  271. },
  272. { NULL }
  273. };
  274. static const AVFilterPad outputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .config_props = config_output,
  279. },
  280. { NULL }
  281. };
  282. AVFilter ff_vf_shear = {
  283. .name = "shear",
  284. .description = NULL_IF_CONFIG_SMALL("Shear transform the input image."),
  285. .priv_size = sizeof(ShearContext),
  286. .init = init,
  287. .query_formats = query_formats,
  288. .inputs = inputs,
  289. .outputs = outputs,
  290. .priv_class = &shear_class,
  291. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  292. .process_command = process_command,
  293. };