vf_crop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * video crop filter
  23. */
  24. #include <stdio.h>
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #include "libavutil/eval.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/libm.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/opt.h"
  36. static const char *const var_names[] = {
  37. "E",
  38. "PHI",
  39. "PI",
  40. "in_w", "iw", ///< width of the input video
  41. "in_h", "ih", ///< height of the input video
  42. "out_w", "ow", ///< width of the cropped video
  43. "out_h", "oh", ///< height of the cropped video
  44. "x",
  45. "y",
  46. "n", ///< number of frame
  47. "pos", ///< position in the file
  48. "t", ///< timestamp expressed in seconds
  49. NULL
  50. };
  51. enum var_name {
  52. VAR_E,
  53. VAR_PHI,
  54. VAR_PI,
  55. VAR_IN_W, VAR_IW,
  56. VAR_IN_H, VAR_IH,
  57. VAR_OUT_W, VAR_OW,
  58. VAR_OUT_H, VAR_OH,
  59. VAR_X,
  60. VAR_Y,
  61. VAR_N,
  62. VAR_T,
  63. VAR_VARS_NB
  64. };
  65. typedef struct CropContext {
  66. const AVClass *class;
  67. int x; ///< x offset of the non-cropped area with respect to the input area
  68. int y; ///< y offset of the non-cropped area with respect to the input area
  69. int w; ///< width of the cropped area
  70. int h; ///< height of the cropped area
  71. int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
  72. int hsub, vsub; ///< chroma subsampling
  73. char *x_expr, *y_expr, *ow_expr, *oh_expr;
  74. AVExpr *x_pexpr, *y_pexpr; /* parsed expressions for x and y */
  75. double var_values[VAR_VARS_NB];
  76. } CropContext;
  77. static int query_formats(AVFilterContext *ctx)
  78. {
  79. static const enum AVPixelFormat pix_fmts[] = {
  80. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGB48LE,
  81. AV_PIX_FMT_BGR48BE, AV_PIX_FMT_BGR48LE,
  82. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  83. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  84. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  85. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
  86. AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
  87. AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
  88. AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
  89. AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
  90. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
  91. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
  92. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
  93. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  94. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  95. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  96. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  97. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  98. AV_PIX_FMT_YUVA420P,
  99. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
  100. AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  101. AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
  102. AV_PIX_FMT_NONE
  103. };
  104. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  105. return 0;
  106. }
  107. static av_cold void uninit(AVFilterContext *ctx)
  108. {
  109. CropContext *s = ctx->priv;
  110. av_expr_free(s->x_pexpr);
  111. s->x_pexpr = NULL;
  112. av_expr_free(s->y_pexpr);
  113. s->y_pexpr = NULL;
  114. }
  115. static inline int normalize_double(int *n, double d)
  116. {
  117. int ret = 0;
  118. if (isnan(d)) {
  119. ret = AVERROR(EINVAL);
  120. } else if (d > INT_MAX || d < INT_MIN) {
  121. *n = d > INT_MAX ? INT_MAX : INT_MIN;
  122. ret = AVERROR(EINVAL);
  123. } else
  124. *n = round(d);
  125. return ret;
  126. }
  127. static int config_input(AVFilterLink *link)
  128. {
  129. AVFilterContext *ctx = link->dst;
  130. CropContext *s = ctx->priv;
  131. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(link->format);
  132. int ret;
  133. const char *expr;
  134. double res;
  135. s->var_values[VAR_E] = M_E;
  136. s->var_values[VAR_PHI] = M_PHI;
  137. s->var_values[VAR_PI] = M_PI;
  138. s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = ctx->inputs[0]->w;
  139. s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = ctx->inputs[0]->h;
  140. s->var_values[VAR_X] = NAN;
  141. s->var_values[VAR_Y] = NAN;
  142. s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = NAN;
  143. s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = NAN;
  144. s->var_values[VAR_N] = 0;
  145. s->var_values[VAR_T] = NAN;
  146. av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
  147. s->hsub = pix_desc->log2_chroma_w;
  148. s->vsub = pix_desc->log2_chroma_h;
  149. if ((ret = av_expr_parse_and_eval(&res, (expr = s->ow_expr),
  150. var_names, s->var_values,
  151. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  152. goto fail_expr;
  153. s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = res;
  154. if ((ret = av_expr_parse_and_eval(&res, (expr = s->oh_expr),
  155. var_names, s->var_values,
  156. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  157. goto fail_expr;
  158. s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = res;
  159. /* evaluate again ow as it may depend on oh */
  160. if ((ret = av_expr_parse_and_eval(&res, (expr = s->ow_expr),
  161. var_names, s->var_values,
  162. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  163. goto fail_expr;
  164. s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = res;
  165. if (normalize_double(&s->w, s->var_values[VAR_OUT_W]) < 0 ||
  166. normalize_double(&s->h, s->var_values[VAR_OUT_H]) < 0) {
  167. av_log(ctx, AV_LOG_ERROR,
  168. "Too big value or invalid expression for out_w/ow or out_h/oh. "
  169. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  170. s->ow_expr, s->oh_expr);
  171. return AVERROR(EINVAL);
  172. }
  173. s->w &= ~((1 << s->hsub) - 1);
  174. s->h &= ~((1 << s->vsub) - 1);
  175. av_expr_free(s->x_pexpr);
  176. av_expr_free(s->y_pexpr);
  177. s->x_pexpr = s->y_pexpr = NULL;
  178. if ((ret = av_expr_parse(&s->x_pexpr, s->x_expr, var_names,
  179. NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
  180. (ret = av_expr_parse(&s->y_pexpr, s->y_expr, var_names,
  181. NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  182. return AVERROR(EINVAL);
  183. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
  184. link->w, link->h, s->w, s->h);
  185. if (s->w <= 0 || s->h <= 0 ||
  186. s->w > link->w || s->h > link->h) {
  187. av_log(ctx, AV_LOG_ERROR,
  188. "Invalid too big or non positive size for width '%d' or height '%d'\n",
  189. s->w, s->h);
  190. return AVERROR(EINVAL);
  191. }
  192. /* set default, required in the case the first computed value for x/y is NAN */
  193. s->x = (link->w - s->w) / 2;
  194. s->y = (link->h - s->h) / 2;
  195. s->x &= ~((1 << s->hsub) - 1);
  196. s->y &= ~((1 << s->vsub) - 1);
  197. return 0;
  198. fail_expr:
  199. av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
  200. return ret;
  201. }
  202. static int config_output(AVFilterLink *link)
  203. {
  204. CropContext *s = link->src->priv;
  205. link->w = s->w;
  206. link->h = s->h;
  207. return 0;
  208. }
  209. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  210. {
  211. AVFilterContext *ctx = link->dst;
  212. CropContext *s = ctx->priv;
  213. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  214. int i;
  215. frame->width = s->w;
  216. frame->height = s->h;
  217. s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
  218. NAN : frame->pts * av_q2d(link->time_base);
  219. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  220. s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
  221. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  222. normalize_double(&s->x, s->var_values[VAR_X]);
  223. normalize_double(&s->y, s->var_values[VAR_Y]);
  224. if (s->x < 0)
  225. s->x = 0;
  226. if (s->y < 0)
  227. s->y = 0;
  228. if ((unsigned)s->x + (unsigned)s->w > link->w)
  229. s->x = link->w - s->w;
  230. if ((unsigned)s->y + (unsigned)s->h > link->h)
  231. s->y = link->h - s->h;
  232. s->x &= ~((1 << s->hsub) - 1);
  233. s->y &= ~((1 << s->vsub) - 1);
  234. av_log(ctx, AV_LOG_TRACE, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
  235. (int)s->var_values[VAR_N], s->var_values[VAR_T], s->x,
  236. s->y, s->x+s->w, s->y+s->h);
  237. frame->data[0] += s->y * frame->linesize[0];
  238. frame->data[0] += s->x * s->max_step[0];
  239. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
  240. for (i = 1; i < 3; i ++) {
  241. if (frame->data[i]) {
  242. frame->data[i] += (s->y >> s->vsub) * frame->linesize[i];
  243. frame->data[i] += (s->x * s->max_step[i]) >> s->hsub;
  244. }
  245. }
  246. }
  247. /* alpha plane */
  248. if (frame->data[3]) {
  249. frame->data[3] += s->y * frame->linesize[3];
  250. frame->data[3] += s->x * s->max_step[3];
  251. }
  252. s->var_values[VAR_N] += 1.0;
  253. return ff_filter_frame(link->dst->outputs[0], frame);
  254. }
  255. #define OFFSET(x) offsetof(CropContext, x)
  256. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  257. static const AVOption options[] = {
  258. { "out_w", "Output video width", OFFSET(ow_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
  259. { "out_h", "Output video height", OFFSET(oh_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
  260. { "x", "Horizontal position in the input video of the left edge of the cropped output video",
  261. OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "(in_w - out_w) / 2" }, .flags = FLAGS },
  262. { "y", "Vertical position in the input video of the top edge of the cropped output video",
  263. OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "(in_h - out_h) / 2" }, .flags = FLAGS },
  264. { NULL },
  265. };
  266. static const AVClass crop_class = {
  267. .class_name = "crop",
  268. .item_name = av_default_item_name,
  269. .option = options,
  270. .version = LIBAVUTIL_VERSION_INT,
  271. };
  272. static const AVFilterPad avfilter_vf_crop_inputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_VIDEO,
  276. .filter_frame = filter_frame,
  277. .get_video_buffer = ff_null_get_video_buffer,
  278. .config_props = config_input,
  279. },
  280. { NULL }
  281. };
  282. static const AVFilterPad avfilter_vf_crop_outputs[] = {
  283. {
  284. .name = "default",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .config_props = config_output,
  287. },
  288. { NULL }
  289. };
  290. AVFilter ff_vf_crop = {
  291. .name = "crop",
  292. .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
  293. .priv_size = sizeof(CropContext),
  294. .priv_class = &crop_class,
  295. .query_formats = query_formats,
  296. .uninit = uninit,
  297. .inputs = avfilter_vf_crop_inputs,
  298. .outputs = avfilter_vf_crop_outputs,
  299. };