vf_rotate.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. #include <float.h>
  36. static const char * const var_names[] = {
  37. "in_w" , "iw", ///< width of the input video
  38. "in_h" , "ih", ///< height of the input video
  39. "out_w", "ow", ///< width of the input video
  40. "out_h", "oh", ///< height of the input video
  41. "hsub", "vsub",
  42. "n", ///< number of frame
  43. "t", ///< timestamp expressed in seconds
  44. NULL
  45. };
  46. enum var_name {
  47. VAR_IN_W , VAR_IW,
  48. VAR_IN_H , VAR_IH,
  49. VAR_OUT_W, VAR_OW,
  50. VAR_OUT_H, VAR_OH,
  51. VAR_HSUB, VAR_VSUB,
  52. VAR_N,
  53. VAR_T,
  54. VAR_VARS_NB
  55. };
  56. typedef struct RotContext {
  57. const AVClass *class;
  58. double angle;
  59. char *angle_expr_str; ///< expression for the angle
  60. AVExpr *angle_expr; ///< parsed expression for the angle
  61. char *outw_expr_str, *outh_expr_str;
  62. int outh, outw;
  63. uint8_t fillcolor[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  64. char *fillcolor_str;
  65. int fillcolor_enable;
  66. int hsub, vsub;
  67. int nb_planes;
  68. int use_bilinear;
  69. float sinx, cosx;
  70. double var_values[VAR_VARS_NB];
  71. FFDrawContext draw;
  72. FFDrawColor color;
  73. uint8_t *(*interpolate_bilinear)(uint8_t *dst_color,
  74. const uint8_t *src, int src_linesize, int src_linestep,
  75. int x, int y, int max_x, int max_y);
  76. } RotContext;
  77. typedef struct ThreadData {
  78. AVFrame *in, *out;
  79. int inw, inh;
  80. int outw, outh;
  81. int plane;
  82. int xi, yi;
  83. int xprime, yprime;
  84. int c, s;
  85. } ThreadData;
  86. #define OFFSET(x) offsetof(RotContext, x)
  87. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  88. static const AVOption rotate_options[] = {
  89. { "angle", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  90. { "a", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  91. { "out_w", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  92. { "ow", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  93. { "out_h", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  94. { "oh", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  95. { "fillcolor", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  96. { "c", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
  97. { "bilinear", "use bilinear interpolation", OFFSET(use_bilinear), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, .flags=FLAGS },
  98. { NULL }
  99. };
  100. AVFILTER_DEFINE_CLASS(rotate);
  101. static av_cold int init(AVFilterContext *ctx)
  102. {
  103. RotContext *rot = ctx->priv;
  104. if (!strcmp(rot->fillcolor_str, "none"))
  105. rot->fillcolor_enable = 0;
  106. else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
  107. rot->fillcolor_enable = 1;
  108. else
  109. return AVERROR(EINVAL);
  110. return 0;
  111. }
  112. static av_cold void uninit(AVFilterContext *ctx)
  113. {
  114. RotContext *rot = ctx->priv;
  115. av_expr_free(rot->angle_expr);
  116. rot->angle_expr = NULL;
  117. }
  118. static int query_formats(AVFilterContext *ctx)
  119. {
  120. static const enum AVPixelFormat pix_fmts[] = {
  121. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  122. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  123. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  124. AV_PIX_FMT_0RGB, AV_PIX_FMT_RGB0,
  125. AV_PIX_FMT_0BGR, AV_PIX_FMT_BGR0,
  126. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  127. AV_PIX_FMT_GRAY8,
  128. AV_PIX_FMT_YUV410P,
  129. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  130. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  131. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
  132. AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
  133. AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
  134. AV_PIX_FMT_YUV420P12LE,
  135. AV_PIX_FMT_YUV444P12LE,
  136. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
  137. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
  138. AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
  139. AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
  140. AV_PIX_FMT_NONE
  141. };
  142. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  143. if (!fmts_list)
  144. return AVERROR(ENOMEM);
  145. return ff_set_common_formats(ctx, fmts_list);
  146. }
  147. static double get_rotated_w(void *opaque, double angle)
  148. {
  149. RotContext *rot = opaque;
  150. double inw = rot->var_values[VAR_IN_W];
  151. double inh = rot->var_values[VAR_IN_H];
  152. float sinx = sin(angle);
  153. float cosx = cos(angle);
  154. return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
  155. FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
  156. }
  157. static double get_rotated_h(void *opaque, double angle)
  158. {
  159. RotContext *rot = opaque;
  160. double inw = rot->var_values[VAR_IN_W];
  161. double inh = rot->var_values[VAR_IN_H];
  162. float sinx = sin(angle);
  163. float cosx = cos(angle);
  164. return FFMAX(0, -inh * cosx) + FFMAX(0, -inw * sinx) +
  165. FFMAX(0, inh * cosx) + FFMAX(0, inw * sinx);
  166. }
  167. static double (* const func1[])(void *, double) = {
  168. get_rotated_w,
  169. get_rotated_h,
  170. NULL
  171. };
  172. static const char * const func1_names[] = {
  173. "rotw",
  174. "roth",
  175. NULL
  176. };
  177. #define FIXP (1<<16)
  178. #define FIXP2 (1<<20)
  179. #define INT_PI 3294199 //(M_PI * FIXP2)
  180. /**
  181. * Compute the sin of a using integer values.
  182. * Input is scaled by FIXP2 and output values are scaled by FIXP.
  183. */
  184. static int64_t int_sin(int64_t a)
  185. {
  186. int64_t a2, res = 0;
  187. int i;
  188. if (a < 0) a = INT_PI-a; // 0..inf
  189. a %= 2 * INT_PI; // 0..2PI
  190. if (a >= INT_PI*3/2) a -= 2*INT_PI; // -PI/2 .. 3PI/2
  191. if (a >= INT_PI/2 ) a = INT_PI - a; // -PI/2 .. PI/2
  192. /* compute sin using Taylor series approximated to the fifth term */
  193. a2 = (a*a)/(FIXP2);
  194. for (i = 2; i < 11; i += 2) {
  195. res += a;
  196. a = -a*a2 / (FIXP2*i*(i+1));
  197. }
  198. return (res + 8)>>4;
  199. }
  200. /**
  201. * Interpolate the color in src at position x and y using bilinear
  202. * interpolation.
  203. */
  204. static uint8_t *interpolate_bilinear8(uint8_t *dst_color,
  205. const uint8_t *src, int src_linesize, int src_linestep,
  206. int x, int y, int max_x, int max_y)
  207. {
  208. int int_x = av_clip(x>>16, 0, max_x);
  209. int int_y = av_clip(y>>16, 0, max_y);
  210. int frac_x = x&0xFFFF;
  211. int frac_y = y&0xFFFF;
  212. int i;
  213. int int_x1 = FFMIN(int_x+1, max_x);
  214. int int_y1 = FFMIN(int_y+1, max_y);
  215. for (i = 0; i < src_linestep; i++) {
  216. int s00 = src[src_linestep * int_x + i + src_linesize * int_y ];
  217. int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
  218. int s10 = src[src_linestep * int_x + i + src_linesize * int_y1];
  219. int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
  220. int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
  221. int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
  222. dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
  223. }
  224. return dst_color;
  225. }
  226. /**
  227. * Interpolate the color in src at position x and y using bilinear
  228. * interpolation.
  229. */
  230. static uint8_t *interpolate_bilinear16(uint8_t *dst_color,
  231. const uint8_t *src, int src_linesize, int src_linestep,
  232. int x, int y, int max_x, int max_y)
  233. {
  234. int int_x = av_clip(x>>16, 0, max_x);
  235. int int_y = av_clip(y>>16, 0, max_y);
  236. int frac_x = x&0xFFFF;
  237. int frac_y = y&0xFFFF;
  238. int i;
  239. int int_x1 = FFMIN(int_x+1, max_x);
  240. int int_y1 = FFMIN(int_y+1, max_y);
  241. for (i = 0; i < src_linestep; i+=2) {
  242. int s00 = AV_RL16(&src[src_linestep * int_x + i + src_linesize * int_y ]);
  243. int s01 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y ]);
  244. int s10 = AV_RL16(&src[src_linestep * int_x + i + src_linesize * int_y1]);
  245. int s11 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y1]);
  246. int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
  247. int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
  248. AV_WL16(&dst_color[i], ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32);
  249. }
  250. return dst_color;
  251. }
  252. static int config_props(AVFilterLink *outlink)
  253. {
  254. AVFilterContext *ctx = outlink->src;
  255. RotContext *rot = ctx->priv;
  256. AVFilterLink *inlink = ctx->inputs[0];
  257. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  258. int ret;
  259. double res;
  260. char *expr;
  261. ff_draw_init(&rot->draw, inlink->format, 0);
  262. ff_draw_color(&rot->draw, &rot->color, rot->fillcolor);
  263. rot->hsub = pixdesc->log2_chroma_w;
  264. rot->vsub = pixdesc->log2_chroma_h;
  265. if (pixdesc->comp[0].depth == 8)
  266. rot->interpolate_bilinear = interpolate_bilinear8;
  267. else
  268. rot->interpolate_bilinear = interpolate_bilinear16;
  269. rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
  270. rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
  271. rot->var_values[VAR_HSUB] = 1<<rot->hsub;
  272. rot->var_values[VAR_VSUB] = 1<<rot->vsub;
  273. rot->var_values[VAR_N] = NAN;
  274. rot->var_values[VAR_T] = NAN;
  275. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
  276. rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
  277. av_expr_free(rot->angle_expr);
  278. rot->angle_expr = NULL;
  279. if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
  280. func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
  281. av_log(ctx, AV_LOG_ERROR,
  282. "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
  283. return ret;
  284. }
  285. #define SET_SIZE_EXPR(name, opt_name) do { \
  286. ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str, \
  287. var_names, rot->var_values, \
  288. func1_names, func1, NULL, NULL, rot, 0, ctx); \
  289. if (ret < 0 || isnan(res) || isinf(res) || res <= 0) { \
  290. av_log(ctx, AV_LOG_ERROR, \
  291. "Error parsing or evaluating expression for option %s: " \
  292. "invalid expression '%s' or non-positive or indefinite value %f\n", \
  293. opt_name, expr, res); \
  294. return ret; \
  295. } \
  296. } while (0)
  297. /* evaluate width and height */
  298. av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
  299. func1_names, func1, NULL, NULL, rot, 0, ctx);
  300. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
  301. rot->outw = res + 0.5;
  302. SET_SIZE_EXPR(outh, "out_h");
  303. rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
  304. rot->outh = res + 0.5;
  305. /* evaluate the width again, as it may depend on the evaluated output height */
  306. SET_SIZE_EXPR(outw, "out_w");
  307. rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
  308. rot->outw = res + 0.5;
  309. /* compute number of planes */
  310. rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
  311. outlink->w = rot->outw;
  312. outlink->h = rot->outh;
  313. return 0;
  314. }
  315. static av_always_inline void copy_elem(uint8_t *pout, const uint8_t *pin, int elem_size)
  316. {
  317. int v;
  318. switch (elem_size) {
  319. case 1:
  320. *pout = *pin;
  321. break;
  322. case 2:
  323. *((uint16_t *)pout) = *((uint16_t *)pin);
  324. break;
  325. case 3:
  326. v = AV_RB24(pin);
  327. AV_WB24(pout, v);
  328. break;
  329. case 4:
  330. *((uint32_t *)pout) = *((uint32_t *)pin);
  331. break;
  332. default:
  333. memcpy(pout, pin, elem_size);
  334. break;
  335. }
  336. }
  337. static av_always_inline void simple_rotate_internal(uint8_t *dst, const uint8_t *src, int src_linesize, int angle, int elem_size, int len)
  338. {
  339. int i;
  340. switch(angle) {
  341. case 0:
  342. memcpy(dst, src, elem_size * len);
  343. break;
  344. case 1:
  345. for (i = 0; i<len; i++)
  346. copy_elem(dst + i*elem_size, src + (len-i-1)*src_linesize, elem_size);
  347. break;
  348. case 2:
  349. for (i = 0; i<len; i++)
  350. copy_elem(dst + i*elem_size, src + (len-i-1)*elem_size, elem_size);
  351. break;
  352. case 3:
  353. for (i = 0; i<len; i++)
  354. copy_elem(dst + i*elem_size, src + i*src_linesize, elem_size);
  355. break;
  356. }
  357. }
  358. static av_always_inline void simple_rotate(uint8_t *dst, const uint8_t *src, int src_linesize, int angle, int elem_size, int len)
  359. {
  360. switch(elem_size) {
  361. case 1 : simple_rotate_internal(dst, src, src_linesize, angle, 1, len); break;
  362. case 2 : simple_rotate_internal(dst, src, src_linesize, angle, 2, len); break;
  363. case 3 : simple_rotate_internal(dst, src, src_linesize, angle, 3, len); break;
  364. case 4 : simple_rotate_internal(dst, src, src_linesize, angle, 4, len); break;
  365. default: simple_rotate_internal(dst, src, src_linesize, angle, elem_size, len); break;
  366. }
  367. }
  368. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  369. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  370. {
  371. ThreadData *td = arg;
  372. AVFrame *in = td->in;
  373. AVFrame *out = td->out;
  374. RotContext *rot = ctx->priv;
  375. const int outw = td->outw, outh = td->outh;
  376. const int inw = td->inw, inh = td->inh;
  377. const int plane = td->plane;
  378. const int xi = td->xi, yi = td->yi;
  379. const int c = td->c, s = td->s;
  380. const int start = (outh * job ) / nb_jobs;
  381. const int end = (outh * (job+1)) / nb_jobs;
  382. int xprime = td->xprime + start * s;
  383. int yprime = td->yprime + start * c;
  384. int i, j, x, y;
  385. for (j = start; j < end; j++) {
  386. x = xprime + xi + FIXP*(inw-1)/2;
  387. y = yprime + yi + FIXP*(inh-1)/2;
  388. if (fabs(rot->angle - 0) < FLT_EPSILON && outw == inw && outh == inh) {
  389. simple_rotate(out->data[plane] + j * out->linesize[plane],
  390. in->data[plane] + j * in->linesize[plane],
  391. in->linesize[plane], 0, rot->draw.pixelstep[plane], outw);
  392. } else if (fabs(rot->angle - M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
  393. simple_rotate(out->data[plane] + j * out->linesize[plane],
  394. in->data[plane] + j * rot->draw.pixelstep[plane],
  395. in->linesize[plane], 1, rot->draw.pixelstep[plane], outw);
  396. } else if (fabs(rot->angle - M_PI) < FLT_EPSILON && outw == inw && outh == inh) {
  397. simple_rotate(out->data[plane] + j * out->linesize[plane],
  398. in->data[plane] + (outh-j-1) * in->linesize[plane],
  399. in->linesize[plane], 2, rot->draw.pixelstep[plane], outw);
  400. } else if (fabs(rot->angle - 3*M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
  401. simple_rotate(out->data[plane] + j * out->linesize[plane],
  402. in->data[plane] + (outh-j-1) * rot->draw.pixelstep[plane],
  403. in->linesize[plane], 3, rot->draw.pixelstep[plane], outw);
  404. } else {
  405. for (i = 0; i < outw; i++) {
  406. int32_t v;
  407. int x1, y1;
  408. uint8_t *pin, *pout;
  409. x1 = x>>16;
  410. y1 = y>>16;
  411. /* the out-of-range values avoid border artifacts */
  412. if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
  413. uint8_t inp_inv[4]; /* interpolated input value */
  414. pout = out->data[plane] + j * out->linesize[plane] + i * rot->draw.pixelstep[plane];
  415. if (rot->use_bilinear) {
  416. pin = rot->interpolate_bilinear(inp_inv,
  417. in->data[plane], in->linesize[plane], rot->draw.pixelstep[plane],
  418. x, y, inw-1, inh-1);
  419. } else {
  420. int x2 = av_clip(x1, 0, inw-1);
  421. int y2 = av_clip(y1, 0, inh-1);
  422. pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->draw.pixelstep[plane];
  423. }
  424. switch (rot->draw.pixelstep[plane]) {
  425. case 1:
  426. *pout = *pin;
  427. break;
  428. case 2:
  429. v = AV_RL16(pin);
  430. AV_WL16(pout, v);
  431. break;
  432. case 3:
  433. v = AV_RB24(pin);
  434. AV_WB24(pout, v);
  435. break;
  436. case 4:
  437. *((uint32_t *)pout) = *((uint32_t *)pin);
  438. break;
  439. default:
  440. memcpy(pout, pin, rot->draw.pixelstep[plane]);
  441. break;
  442. }
  443. }
  444. x += c;
  445. y -= s;
  446. }
  447. }
  448. xprime += s;
  449. yprime += c;
  450. }
  451. return 0;
  452. }
  453. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  454. {
  455. AVFilterContext *ctx = inlink->dst;
  456. AVFilterLink *outlink = ctx->outputs[0];
  457. AVFrame *out;
  458. RotContext *rot = ctx->priv;
  459. int angle_int, s, c, plane;
  460. double res;
  461. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  462. if (!out) {
  463. av_frame_free(&in);
  464. return AVERROR(ENOMEM);
  465. }
  466. av_frame_copy_props(out, in);
  467. rot->var_values[VAR_N] = inlink->frame_count_out;
  468. rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
  469. rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
  470. av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
  471. rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
  472. angle_int = res * FIXP * 16;
  473. s = int_sin(angle_int);
  474. c = int_sin(angle_int + INT_PI/2);
  475. /* fill background */
  476. if (rot->fillcolor_enable)
  477. ff_fill_rectangle(&rot->draw, &rot->color, out->data, out->linesize,
  478. 0, 0, outlink->w, outlink->h);
  479. for (plane = 0; plane < rot->nb_planes; plane++) {
  480. int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
  481. int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
  482. const int outw = AV_CEIL_RSHIFT(outlink->w, hsub);
  483. const int outh = AV_CEIL_RSHIFT(outlink->h, vsub);
  484. ThreadData td = { .in = in, .out = out,
  485. .inw = AV_CEIL_RSHIFT(inlink->w, hsub),
  486. .inh = AV_CEIL_RSHIFT(inlink->h, vsub),
  487. .outh = outh, .outw = outw,
  488. .xi = -(outw-1) * c / 2, .yi = (outw-1) * s / 2,
  489. .xprime = -(outh-1) * s / 2,
  490. .yprime = -(outh-1) * c / 2,
  491. .plane = plane, .c = c, .s = s };
  492. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ff_filter_get_nb_threads(ctx)));
  493. }
  494. av_frame_free(&in);
  495. return ff_filter_frame(outlink, out);
  496. }
  497. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  498. char *res, int res_len, int flags)
  499. {
  500. RotContext *rot = ctx->priv;
  501. int ret;
  502. if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
  503. AVExpr *old = rot->angle_expr;
  504. ret = av_expr_parse(&rot->angle_expr, args, var_names,
  505. NULL, NULL, NULL, NULL, 0, ctx);
  506. if (ret < 0) {
  507. av_log(ctx, AV_LOG_ERROR,
  508. "Error when parsing the expression '%s' for angle command\n", args);
  509. rot->angle_expr = old;
  510. return ret;
  511. }
  512. av_expr_free(old);
  513. } else
  514. ret = AVERROR(ENOSYS);
  515. return ret;
  516. }
  517. static const AVFilterPad rotate_inputs[] = {
  518. {
  519. .name = "default",
  520. .type = AVMEDIA_TYPE_VIDEO,
  521. .filter_frame = filter_frame,
  522. },
  523. { NULL }
  524. };
  525. static const AVFilterPad rotate_outputs[] = {
  526. {
  527. .name = "default",
  528. .type = AVMEDIA_TYPE_VIDEO,
  529. .config_props = config_props,
  530. },
  531. { NULL }
  532. };
  533. AVFilter ff_vf_rotate = {
  534. .name = "rotate",
  535. .description = NULL_IF_CONFIG_SMALL("Rotate the input image."),
  536. .priv_size = sizeof(RotContext),
  537. .init = init,
  538. .uninit = uninit,
  539. .query_formats = query_formats,
  540. .process_command = process_command,
  541. .inputs = rotate_inputs,
  542. .outputs = rotate_outputs,
  543. .priv_class = &rotate_class,
  544. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  545. };