vf_rotate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2013 Stefano Sabatini
  3. * Copyright (c) 2008 Vitor Sessak
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * rotation filter, partially based on the tests/rotozoom.c program
  24. */
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "drawutils.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. static const char *var_names[] = {
  36. "in_w" , "iw", ///< width of the input video
  37. "in_h" , "ih", ///< height of the input video
  38. "out_w", "ow", ///< width of the input video
  39. "out_h", "oh", ///< height of the input video
  40. "hsub", "vsub",
  41. "n", ///< number of frame
  42. "t", ///< timestamp expressed in seconds
  43. NULL
  44. };
  45. enum var_name {
  46. VAR_IN_W , VAR_IW,
  47. VAR_IN_H , VAR_IH,
  48. VAR_OUT_W, VAR_OW,
  49. VAR_OUT_H, VAR_OH,
  50. VAR_HSUB, VAR_VSUB,
  51. VAR_N,
  52. VAR_T,
  53. VAR_VARS_NB
  54. };
  55. typedef struct {
  56. const AVClass *class;
  57. double angle;
  58. char *angle_expr_str; ///< expression for the angle
  59. AVExpr *angle_expr; ///< parsed expression for the angle
  60. char *outw_expr_str, *outh_expr_str;
  61. int outh, outw;
  62. uint8_t fillcolor[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  63. char *fillcolor_str;
  64. int fillcolor_enable;
  65. int hsub, vsub;
  66. int nb_planes;
  67. int use_bilinear;
  68. float sinx, cosx;
  69. double var_values[VAR_VARS_NB];
  70. FFDrawContext draw;
  71. FFDrawColor color;
  72. } RotContext;
  73. typedef struct ThreadData {
  74. AVFrame *in, *out;
  75. int inw, inh;
  76. int outw, outh;
  77. int plane;
  78. int xi, yi;
  79. int xprime, yprime;
  80. int c, s;
  81. } ThreadData;
  82. #define OFFSET(x) offsetof(RotContext, x)
  83. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  84. static const AVOption rotate_options[] = {
  85. { "angle", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  86. { "a", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  87. { "out_w", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  88. { "ow", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  89. { "out_h", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  90. { "oh", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  91. { "fillcolor", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  92. { "c", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  93. { "bilinear", "use bilinear interpolation", OFFSET(use_bilinear), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags=FLAGS },
  94. { NULL }
  95. };
  96. AVFILTER_DEFINE_CLASS(rotate);
  97. static av_cold int init(AVFilterContext *ctx)
  98. {
  99. RotContext *rot = ctx->priv;
  100. if (!strcmp(rot->fillcolor_str, "none"))
  101. rot->fillcolor_enable = 0;
  102. else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
  103. rot->fillcolor_enable = 1;
  104. else
  105. return AVERROR(EINVAL);
  106. return 0;
  107. }
  108. static av_cold void uninit(AVFilterContext *ctx)
  109. {
  110. RotContext *rot = ctx->priv;
  111. av_expr_free(rot->angle_expr);
  112. rot->angle_expr = NULL;
  113. }
  114. static int query_formats(AVFilterContext *ctx)
  115. {
  116. static enum PixelFormat pix_fmts[] = {
  117. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  118. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  119. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  120. AV_PIX_FMT_0RGB, AV_PIX_FMT_RGB0,
  121. AV_PIX_FMT_0BGR, AV_PIX_FMT_BGR0,
  122. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  123. AV_PIX_FMT_GRAY8,
  124. AV_PIX_FMT_YUV410P,
  125. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  126. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  127. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
  128. AV_PIX_FMT_NONE
  129. };
  130. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  131. return 0;
  132. }
  133. static double get_rotated_w(void *opaque, double angle)
  134. {
  135. RotContext *rot = opaque;
  136. double inw = rot->var_values[VAR_IN_W];
  137. double inh = rot->var_values[VAR_IN_H];
  138. float sinx = sin(angle);
  139. float cosx = cos(angle);
  140. return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
  141. FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
  142. }
  143. static double get_rotated_h(void *opaque, double angle)
  144. {
  145. RotContext *rot = opaque;
  146. double inw = rot->var_values[VAR_IN_W];
  147. double inh = rot->var_values[VAR_IN_H];
  148. float sinx = sin(angle);
  149. float cosx = cos(angle);
  150. return FFMAX(0, -inh * cosx) + FFMAX(0, -inw * sinx) +
  151. FFMAX(0, inh * cosx) + FFMAX(0, inw * sinx);
  152. }
  153. static double (* const func1[])(void *, double) = {
  154. get_rotated_w,
  155. get_rotated_h,
  156. NULL
  157. };
  158. static const char * const func1_names[] = {
  159. "rotw",
  160. "roth",
  161. NULL
  162. };
  163. static int config_props(AVFilterLink *outlink)
  164. {
  165. AVFilterContext *ctx = outlink->src;
  166. RotContext *rot = ctx->priv;
  167. AVFilterLink *inlink = ctx->inputs[0];
  168. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  169. int ret;
  170. double res;
  171. char *expr;
  172. ff_draw_init(&rot->draw, inlink->format, 0);
  173. ff_draw_color(&rot->draw, &rot->color, rot->fillcolor);
  174. rot->hsub = pixdesc->log2_chroma_w;
  175. rot->vsub = pixdesc->log2_chroma_h;
  176. rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
  177. rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
  178. rot->var_values[VAR_HSUB] = 1<<rot->hsub;
  179. rot->var_values[VAR_VSUB] = 1<<rot->vsub;
  180. rot->var_values[VAR_N] = NAN;
  181. rot->var_values[VAR_T] = NAN;
  182. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
  183. rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
  184. av_expr_free(rot->angle_expr);
  185. rot->angle_expr = NULL;
  186. if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
  187. func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
  188. av_log(ctx, AV_LOG_ERROR,
  189. "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
  190. return ret;
  191. }
  192. #define SET_SIZE_EXPR(name, opt_name) do { \
  193. ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str, \
  194. var_names, rot->var_values, \
  195. func1_names, func1, NULL, NULL, rot, 0, ctx); \
  196. if (ret < 0 || isnan(res) || isinf(res) || res <= 0) { \
  197. av_log(ctx, AV_LOG_ERROR, \
  198. "Error parsing or evaluating expression for option %s: " \
  199. "invalid expression '%s' or non-positive or indefinite value %f\n", \
  200. opt_name, expr, res); \
  201. return ret; \
  202. } \
  203. } while (0)
  204. /* evaluate width and height */
  205. av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
  206. func1_names, func1, NULL, NULL, rot, 0, ctx);
  207. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
  208. rot->outw = res + 0.5;
  209. SET_SIZE_EXPR(outh, "out_w");
  210. rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
  211. rot->outh = res + 0.5;
  212. /* evaluate the width again, as it may depend on the evaluated output height */
  213. SET_SIZE_EXPR(outw, "out_h");
  214. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
  215. rot->outw = res + 0.5;
  216. /* compute number of planes */
  217. rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
  218. outlink->w = rot->outw;
  219. outlink->h = rot->outh;
  220. return 0;
  221. }
  222. #define FIXP (1<<16)
  223. #define INT_PI 205887 //(M_PI * FIXP)
  224. /**
  225. * Compute the sin of a using integer values.
  226. * Input and output values are scaled by FIXP.
  227. */
  228. static int64_t int_sin(int64_t a)
  229. {
  230. int64_t a2, res = 0;
  231. int i;
  232. if (a < 0) a = INT_PI-a; // 0..inf
  233. a %= 2 * INT_PI; // 0..2PI
  234. if (a >= INT_PI*3/2) a -= 2*INT_PI; // -PI/2 .. 3PI/2
  235. if (a >= INT_PI/2 ) a = INT_PI - a; // -PI/2 .. PI/2
  236. /* compute sin using Taylor series approximated to the third term */
  237. a2 = (a*a)/FIXP;
  238. for (i = 2; i < 7; i += 2) {
  239. res += a;
  240. a = -a*a2 / (FIXP*i*(i+1));
  241. }
  242. return res;
  243. }
  244. /**
  245. * Interpolate the color in src at position x and y using bilinear
  246. * interpolation.
  247. */
  248. static uint8_t *interpolate_bilinear(uint8_t *dst_color,
  249. const uint8_t *src, int src_linesize, int src_linestep,
  250. int x, int y, int max_x, int max_y)
  251. {
  252. int int_x = av_clip(x>>16, 0, max_x);
  253. int int_y = av_clip(y>>16, 0, max_y);
  254. int frac_x = x&0xFFFF;
  255. int frac_y = y&0xFFFF;
  256. int i;
  257. int int_x1 = FFMIN(int_x+1, max_x);
  258. int int_y1 = FFMIN(int_y+1, max_y);
  259. for (i = 0; i < src_linestep; i++) {
  260. int s00 = src[src_linestep * int_x + i + src_linesize * int_y ];
  261. int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
  262. int s10 = src[src_linestep * int_x + i + src_linesize * int_y1];
  263. int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
  264. int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
  265. int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
  266. dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
  267. }
  268. return dst_color;
  269. }
  270. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  271. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  272. {
  273. ThreadData *td = arg;
  274. AVFrame *in = td->in;
  275. AVFrame *out = td->out;
  276. RotContext *rot = ctx->priv;
  277. const int outw = td->outw, outh = td->outh;
  278. const int inw = td->inw, inh = td->inh;
  279. const int plane = td->plane;
  280. const int xi = td->xi, yi = td->yi;
  281. const int c = td->c, s = td->s;
  282. const int start = (outh * job ) / nb_jobs;
  283. const int end = (outh * (job+1)) / nb_jobs;
  284. int xprime = td->xprime + start * s;
  285. int yprime = td->yprime + start * c;
  286. int i, j, x, y;
  287. for (j = start; j < end; j++) {
  288. x = xprime + xi + FIXP*inw/2;
  289. y = yprime + yi + FIXP*inh/2;
  290. for (i = 0; i < outw; i++) {
  291. int32_t v;
  292. int x1, y1;
  293. uint8_t *pin, *pout;
  294. x += c;
  295. y -= s;
  296. x1 = x>>16;
  297. y1 = y>>16;
  298. /* the out-of-range values avoid border artifacts */
  299. if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
  300. uint8_t inp_inv[4]; /* interpolated input value */
  301. pout = out->data[plane] + j * out->linesize[plane] + i * rot->draw.pixelstep[plane];
  302. if (rot->use_bilinear) {
  303. pin = interpolate_bilinear(inp_inv,
  304. in->data[plane], in->linesize[plane], rot->draw.pixelstep[plane],
  305. x, y, inw-1, inh-1);
  306. } else {
  307. int x2 = av_clip(x1, 0, inw-1);
  308. int y2 = av_clip(y1, 0, inh-1);
  309. pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->draw.pixelstep[plane];
  310. }
  311. switch (rot->draw.pixelstep[plane]) {
  312. case 1:
  313. *pout = *pin;
  314. break;
  315. case 2:
  316. *((uint16_t *)pout) = *((uint16_t *)pin);
  317. break;
  318. case 3:
  319. v = AV_RB24(pin);
  320. AV_WB24(pout, v);
  321. break;
  322. case 4:
  323. *((uint32_t *)pout) = *((uint32_t *)pin);
  324. break;
  325. default:
  326. memcpy(pout, pin, rot->draw.pixelstep[plane]);
  327. break;
  328. }
  329. }
  330. }
  331. xprime += s;
  332. yprime += c;
  333. }
  334. return 0;
  335. }
  336. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  337. {
  338. AVFilterContext *ctx = inlink->dst;
  339. AVFilterLink *outlink = ctx->outputs[0];
  340. AVFrame *out;
  341. RotContext *rot = ctx->priv;
  342. int angle_int, s, c, plane;
  343. double res;
  344. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  345. if (!out) {
  346. av_frame_free(&in);
  347. return AVERROR(ENOMEM);
  348. }
  349. av_frame_copy_props(out, in);
  350. rot->var_values[VAR_N] = inlink->frame_count;
  351. rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
  352. rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
  353. av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
  354. rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
  355. angle_int = res * FIXP;
  356. s = int_sin(angle_int);
  357. c = int_sin(angle_int + INT_PI/2);
  358. /* fill background */
  359. if (rot->fillcolor_enable)
  360. ff_fill_rectangle(&rot->draw, &rot->color, out->data, out->linesize,
  361. 0, 0, outlink->w, outlink->h);
  362. for (plane = 0; plane < rot->nb_planes; plane++) {
  363. int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
  364. int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
  365. const int outw = FF_CEIL_RSHIFT(outlink->w, hsub);
  366. const int outh = FF_CEIL_RSHIFT(outlink->h, vsub);
  367. ThreadData td = { .in = in, .out = out,
  368. .inw = FF_CEIL_RSHIFT(inlink->w, hsub),
  369. .inh = FF_CEIL_RSHIFT(inlink->h, vsub),
  370. .outh = outh, .outw = outw,
  371. .xi = -outw/2 * c, .yi = outw/2 * s,
  372. .xprime = -outh/2 * s,
  373. .yprime = -outh/2 * c,
  374. .plane = plane, .c = c, .s = s };
  375. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ctx->graph->nb_threads));
  376. }
  377. av_frame_free(&in);
  378. return ff_filter_frame(outlink, out);
  379. }
  380. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  381. char *res, int res_len, int flags)
  382. {
  383. RotContext *rot = ctx->priv;
  384. int ret;
  385. if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
  386. AVExpr *old = rot->angle_expr;
  387. ret = av_expr_parse(&rot->angle_expr, args, var_names,
  388. NULL, NULL, NULL, NULL, 0, ctx);
  389. if (ret < 0) {
  390. av_log(ctx, AV_LOG_ERROR,
  391. "Error when parsing the expression '%s' for angle command\n", args);
  392. rot->angle_expr = old;
  393. return ret;
  394. }
  395. av_expr_free(old);
  396. } else
  397. ret = AVERROR(ENOSYS);
  398. return ret;
  399. }
  400. static const AVFilterPad rotate_inputs[] = {
  401. {
  402. .name = "default",
  403. .type = AVMEDIA_TYPE_VIDEO,
  404. .filter_frame = filter_frame,
  405. },
  406. { NULL }
  407. };
  408. static const AVFilterPad rotate_outputs[] = {
  409. {
  410. .name = "default",
  411. .type = AVMEDIA_TYPE_VIDEO,
  412. .config_props = config_props,
  413. },
  414. { NULL }
  415. };
  416. AVFilter ff_vf_rotate = {
  417. .name = "rotate",
  418. .description = NULL_IF_CONFIG_SMALL("Rotate the input image."),
  419. .priv_size = sizeof(RotContext),
  420. .init = init,
  421. .uninit = uninit,
  422. .query_formats = query_formats,
  423. .process_command = process_command,
  424. .inputs = rotate_inputs,
  425. .outputs = rotate_outputs,
  426. .priv_class = &rotate_class,
  427. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  428. };