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.

466 lines
16KB

  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. uint8_t *line[4];
  69. int linestep[4];
  70. float sinx, cosx;
  71. double var_values[VAR_VARS_NB];
  72. } RotContext;
  73. #define OFFSET(x) offsetof(RotContext, x)
  74. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  75. static const AVOption rotate_options[] = {
  76. { "angle", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  77. { "a", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  78. { "out_w", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  79. { "ow", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  80. { "out_h", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  81. { "oh", "set output width expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  82. { "fillcolor", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  83. { "c", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  84. { "bilinear", "use bilinear interpolation", OFFSET(use_bilinear), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags=FLAGS },
  85. { NULL }
  86. };
  87. AVFILTER_DEFINE_CLASS(rotate);
  88. static av_cold int init(AVFilterContext *ctx)
  89. {
  90. RotContext *rot = ctx->priv;
  91. if (!strcmp(rot->fillcolor_str, "none"))
  92. rot->fillcolor_enable = 0;
  93. else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
  94. rot->fillcolor_enable = 1;
  95. else
  96. return AVERROR(EINVAL);
  97. return 0;
  98. }
  99. static av_cold void uninit(AVFilterContext *ctx)
  100. {
  101. RotContext *rot = ctx->priv;
  102. int i;
  103. for (i = 0; i < 4; i++)
  104. av_freep(&rot->line[i]);
  105. av_expr_free(rot->angle_expr);
  106. rot->angle_expr = NULL;
  107. }
  108. static int query_formats(AVFilterContext *ctx)
  109. {
  110. static enum PixelFormat pix_fmts[] = {
  111. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  112. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  113. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  114. AV_PIX_FMT_GRAY8,
  115. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  116. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  117. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
  118. AV_PIX_FMT_NONE
  119. };
  120. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  121. return 0;
  122. }
  123. static double get_rotated_w(void *opaque, double angle)
  124. {
  125. RotContext *rot = opaque;
  126. double inw = rot->var_values[VAR_IN_W];
  127. double inh = rot->var_values[VAR_IN_H];
  128. float sinx = sin(angle);
  129. float cosx = cos(angle);
  130. return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
  131. FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
  132. }
  133. static double get_rotated_h(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 * cosx) + FFMAX(0, -inw * sinx) +
  141. FFMAX(0, inh * cosx) + FFMAX(0, inw * sinx);
  142. }
  143. static double (* const func1[])(void *, double) = {
  144. get_rotated_w,
  145. get_rotated_h,
  146. NULL
  147. };
  148. static const char * const func1_names[] = {
  149. "rotw",
  150. "roth",
  151. NULL
  152. };
  153. static int config_props(AVFilterLink *outlink)
  154. {
  155. AVFilterContext *ctx = outlink->src;
  156. RotContext *rot = ctx->priv;
  157. AVFilterLink *inlink = ctx->inputs[0];
  158. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  159. uint8_t rgba_color[4];
  160. int is_packed_rgba, ret;
  161. double res;
  162. char *expr;
  163. rot->hsub = pixdesc->log2_chroma_w;
  164. rot->vsub = pixdesc->log2_chroma_h;
  165. rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
  166. rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
  167. rot->var_values[VAR_HSUB] = 1<<rot->hsub;
  168. rot->var_values[VAR_VSUB] = 1<<rot->vsub;
  169. rot->var_values[VAR_N] = NAN;
  170. rot->var_values[VAR_T] = NAN;
  171. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
  172. rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
  173. av_expr_free(rot->angle_expr);
  174. rot->angle_expr = NULL;
  175. if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
  176. func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
  177. av_log(ctx, AV_LOG_ERROR,
  178. "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
  179. return ret;
  180. }
  181. #define SET_SIZE_EXPR(name, opt_name) do { \
  182. ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str, \
  183. var_names, rot->var_values, \
  184. func1_names, func1, NULL, NULL, rot, 0, ctx); \
  185. if (ret < 0 || isnan(res) || isinf(res) || res <= 0) { \
  186. av_log(ctx, AV_LOG_ERROR, \
  187. "Error parsing or evaluating expression for option %s: " \
  188. "invalid expression '%s' or non-positive or indefinite value %f\n", \
  189. opt_name, expr, res); \
  190. return ret; \
  191. } \
  192. } while (0)
  193. /* evaluate width and height */
  194. av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
  195. func1_names, func1, NULL, NULL, rot, 0, ctx);
  196. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
  197. rot->outw = res + 0.5;
  198. SET_SIZE_EXPR(outh, "out_w");
  199. rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
  200. rot->outh = res + 0.5;
  201. /* evaluate the width again, as it may depend on the evaluated output height */
  202. SET_SIZE_EXPR(outw, "out_h");
  203. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
  204. rot->outw = res + 0.5;
  205. /* compute number of planes */
  206. rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
  207. outlink->w = rot->outw;
  208. outlink->h = rot->outh;
  209. memcpy(rgba_color, rot->fillcolor, sizeof(rgba_color));
  210. ff_fill_line_with_color(rot->line, rot->linestep, outlink->w, rot->fillcolor,
  211. outlink->format, rgba_color, &is_packed_rgba, NULL);
  212. av_log(ctx, AV_LOG_INFO,
  213. "w:%d h:%d -> w:%d h:%d bgcolor:0x%02X%02X%02X%02X[%s]\n",
  214. inlink->w, inlink->h, outlink->w, outlink->h,
  215. rot->fillcolor[0], rot->fillcolor[1], rot->fillcolor[2], rot->fillcolor[3],
  216. is_packed_rgba ? "rgba" : "yuva");
  217. return 0;
  218. }
  219. #define FIXP (1<<16)
  220. #define INT_PI 205887 //(M_PI * FIXP)
  221. /**
  222. * Compute the sin of a using integer values.
  223. * Input and output values are scaled by FIXP.
  224. */
  225. static int64_t int_sin(int64_t a)
  226. {
  227. int64_t a2, res = 0;
  228. int i;
  229. if (a < 0) a = INT_PI-a; // 0..inf
  230. a %= 2 * INT_PI; // 0..2PI
  231. if (a >= INT_PI*3/2) a -= 2*INT_PI; // -PI/2 .. 3PI/2
  232. if (a >= INT_PI/2 ) a = INT_PI - a; // -PI/2 .. PI/2
  233. /* compute sin using Taylor series approximated to the third term */
  234. a2 = (a*a)/FIXP;
  235. for (i = 2; i < 7; i += 2) {
  236. res += a;
  237. a = -a*a2 / (FIXP*i*(i+1));
  238. }
  239. return res;
  240. }
  241. /**
  242. * Interpolate the color in src at position x and y using bilinear
  243. * interpolation.
  244. */
  245. static uint8_t *interpolate_bilinear(uint8_t *dst_color,
  246. const uint8_t *src, int src_linesize, int src_linestep,
  247. int x, int y, int max_x, int max_y)
  248. {
  249. int int_x = av_clip(x>>16, 0, max_x);
  250. int int_y = av_clip(y>>16, 0, max_y);
  251. int frac_x = x&0xFFFF;
  252. int frac_y = y&0xFFFF;
  253. int i;
  254. int int_x1 = FFMIN(int_x+1, max_x);
  255. int int_y1 = FFMIN(int_y+1, max_y);
  256. for (i = 0; i < src_linestep; i++) {
  257. int s00 = src[src_linestep * int_x + i + src_linesize * int_y ];
  258. int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
  259. int s10 = src[src_linestep * int_x + i + src_linesize * int_y1];
  260. int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
  261. int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
  262. int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
  263. dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
  264. }
  265. return dst_color;
  266. }
  267. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  268. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  269. {
  270. AVFilterContext *ctx = inlink->dst;
  271. AVFilterLink *outlink = ctx->outputs[0];
  272. AVFrame *out;
  273. RotContext *rot = ctx->priv;
  274. int angle_int, s, c, plane;
  275. double res;
  276. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  277. if (!out) {
  278. av_frame_free(&in);
  279. return AVERROR(ENOMEM);
  280. }
  281. av_frame_copy_props(out, in);
  282. rot->var_values[VAR_N] = inlink->frame_count;
  283. rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
  284. rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
  285. av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
  286. rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
  287. angle_int = res * FIXP;
  288. s = int_sin(angle_int);
  289. c = int_sin(angle_int + INT_PI/2);
  290. /* fill background */
  291. if (rot->fillcolor_enable)
  292. ff_draw_rectangle(out->data, out->linesize,
  293. rot->line, rot->linestep, rot->hsub, rot->vsub,
  294. 0, 0, outlink->w, outlink->h);
  295. for (plane = 0; plane < rot->nb_planes; plane++) {
  296. int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
  297. int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
  298. int inw = FF_CEIL_RSHIFT(inlink->w, hsub);
  299. int inh = FF_CEIL_RSHIFT(inlink->h, vsub);
  300. int outw = FF_CEIL_RSHIFT(outlink->w, hsub);
  301. int outh = FF_CEIL_RSHIFT(outlink->h, hsub);
  302. const int xi = -outw/2 * c;
  303. const int yi = outw/2 * s;
  304. int xprime = -outh/2 * s;
  305. int yprime = -outh/2 * c;
  306. int i, j, x, y;
  307. for (j = 0; j < outh; j++) {
  308. x = xprime + xi + FIXP*inw/2;
  309. y = yprime + yi + FIXP*inh/2;
  310. for (i = 0; i < outw; i++) {
  311. int32_t v;
  312. int x1, y1;
  313. uint8_t *pin, *pout;
  314. x += c;
  315. y -= s;
  316. x1 = x>>16;
  317. y1 = y>>16;
  318. /* the out-of-range values avoid border artifacts */
  319. if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
  320. uint8_t inp_inv[4]; /* interpolated input value */
  321. pout = out->data[plane] + j * out->linesize[plane] + i * rot->linestep[plane];
  322. if (rot->use_bilinear) {
  323. pin = interpolate_bilinear(inp_inv,
  324. in->data[plane], in->linesize[plane], rot->linestep[plane],
  325. x, y, inw-1, inh-1);
  326. } else {
  327. int x2 = av_clip(x1, 0, inw-1);
  328. int y2 = av_clip(y1, 0, inh-1);
  329. pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->linestep[plane];
  330. }
  331. switch (rot->linestep[plane]) {
  332. case 1:
  333. *pout = *pin;
  334. break;
  335. case 2:
  336. *((uint16_t *)pout) = *((uint16_t *)pin);
  337. break;
  338. case 3:
  339. v = AV_RB24(pin);
  340. AV_WB24(pout, v);
  341. break;
  342. case 4:
  343. *((uint32_t *)pout) = *((uint32_t *)pin);
  344. break;
  345. default:
  346. memcpy(pout, pin, rot->linestep[plane]);
  347. break;
  348. }
  349. }
  350. }
  351. xprime += s;
  352. yprime += c;
  353. }
  354. }
  355. av_frame_free(&in);
  356. return ff_filter_frame(outlink, out);
  357. }
  358. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  359. char *res, int res_len, int flags)
  360. {
  361. RotContext *rot = ctx->priv;
  362. int ret;
  363. if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
  364. AVExpr *old = rot->angle_expr;
  365. ret = av_expr_parse(&rot->angle_expr, args, var_names,
  366. NULL, NULL, NULL, NULL, 0, ctx);
  367. if (ret < 0) {
  368. av_log(ctx, AV_LOG_ERROR,
  369. "Error when parsing the expression '%s' for angle command\n", args);
  370. rot->angle_expr = old;
  371. return ret;
  372. }
  373. av_expr_free(old);
  374. } else
  375. ret = AVERROR(ENOSYS);
  376. return ret;
  377. }
  378. static const AVFilterPad rotate_inputs[] = {
  379. {
  380. .name = "default",
  381. .type = AVMEDIA_TYPE_VIDEO,
  382. .filter_frame = filter_frame,
  383. },
  384. { NULL }
  385. };
  386. static const AVFilterPad rotate_outputs[] = {
  387. {
  388. .name = "default",
  389. .type = AVMEDIA_TYPE_VIDEO,
  390. .config_props = config_props,
  391. },
  392. { NULL }
  393. };
  394. AVFilter avfilter_vf_rotate = {
  395. .name = "rotate",
  396. .description = NULL_IF_CONFIG_SMALL("Rotate the input image."),
  397. .priv_size = sizeof(RotContext),
  398. .init = init,
  399. .uninit = uninit,
  400. .query_formats = query_formats,
  401. .process_command = process_command,
  402. .inputs = rotate_inputs,
  403. .outputs = rotate_outputs,
  404. .priv_class = &rotate_class,
  405. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  406. };