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.

330 lines
11KB

  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. /* #define DEBUG */
  25. #include <stdio.h>
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "libavutil/eval.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/libm.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/mathematics.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 {
  66. int x; ///< x offset of the non-cropped area with respect to the input area
  67. int y; ///< y offset of the non-cropped area with respect to the input area
  68. int w; ///< width of the cropped area
  69. int h; ///< height of the cropped area
  70. int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
  71. int hsub, vsub; ///< chroma subsampling
  72. char x_expr[256], y_expr[256], ow_expr[256], oh_expr[256];
  73. AVExpr *x_pexpr, *y_pexpr; /* parsed expressions for x and y */
  74. double var_values[VAR_VARS_NB];
  75. } CropContext;
  76. static int query_formats(AVFilterContext *ctx)
  77. {
  78. static const enum AVPixelFormat pix_fmts[] = {
  79. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGB48LE,
  80. AV_PIX_FMT_BGR48BE, AV_PIX_FMT_BGR48LE,
  81. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  82. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  83. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  84. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
  85. AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
  86. AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
  87. AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
  88. AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
  89. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
  90. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
  91. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
  92. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  93. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  94. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  95. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  96. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  97. AV_PIX_FMT_YUVA420P,
  98. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
  99. AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  100. AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
  101. AV_PIX_FMT_NONE
  102. };
  103. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  104. return 0;
  105. }
  106. static av_cold int init(AVFilterContext *ctx, const char *args)
  107. {
  108. CropContext *crop = ctx->priv;
  109. av_strlcpy(crop->ow_expr, "iw", sizeof(crop->ow_expr));
  110. av_strlcpy(crop->oh_expr, "ih", sizeof(crop->oh_expr));
  111. av_strlcpy(crop->x_expr, "(in_w-out_w)/2", sizeof(crop->x_expr));
  112. av_strlcpy(crop->y_expr, "(in_h-out_h)/2", sizeof(crop->y_expr));
  113. if (args)
  114. sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->ow_expr, crop->oh_expr, crop->x_expr, crop->y_expr);
  115. return 0;
  116. }
  117. static av_cold void uninit(AVFilterContext *ctx)
  118. {
  119. CropContext *crop = ctx->priv;
  120. av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
  121. av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
  122. }
  123. static inline int normalize_double(int *n, double d)
  124. {
  125. int ret = 0;
  126. if (isnan(d)) {
  127. ret = AVERROR(EINVAL);
  128. } else if (d > INT_MAX || d < INT_MIN) {
  129. *n = d > INT_MAX ? INT_MAX : INT_MIN;
  130. ret = AVERROR(EINVAL);
  131. } else
  132. *n = round(d);
  133. return ret;
  134. }
  135. static int config_input(AVFilterLink *link)
  136. {
  137. AVFilterContext *ctx = link->dst;
  138. CropContext *crop = ctx->priv;
  139. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(link->format);
  140. int ret;
  141. const char *expr;
  142. double res;
  143. crop->var_values[VAR_E] = M_E;
  144. crop->var_values[VAR_PHI] = M_PHI;
  145. crop->var_values[VAR_PI] = M_PI;
  146. crop->var_values[VAR_IN_W] = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
  147. crop->var_values[VAR_IN_H] = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
  148. crop->var_values[VAR_X] = NAN;
  149. crop->var_values[VAR_Y] = NAN;
  150. crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
  151. crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
  152. crop->var_values[VAR_N] = 0;
  153. crop->var_values[VAR_T] = NAN;
  154. av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
  155. crop->hsub = pix_desc->log2_chroma_w;
  156. crop->vsub = pix_desc->log2_chroma_h;
  157. if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
  158. var_names, crop->var_values,
  159. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
  160. crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
  161. if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
  162. var_names, crop->var_values,
  163. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
  164. crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
  165. /* evaluate again ow as it may depend on oh */
  166. if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
  167. var_names, crop->var_values,
  168. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
  169. crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
  170. if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
  171. normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
  172. av_log(ctx, AV_LOG_ERROR,
  173. "Too big value or invalid expression for out_w/ow or out_h/oh. "
  174. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  175. crop->ow_expr, crop->oh_expr);
  176. return AVERROR(EINVAL);
  177. }
  178. crop->w &= ~((1 << crop->hsub) - 1);
  179. crop->h &= ~((1 << crop->vsub) - 1);
  180. if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
  181. NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
  182. (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
  183. NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  184. return AVERROR(EINVAL);
  185. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
  186. link->w, link->h, crop->w, crop->h);
  187. if (crop->w <= 0 || crop->h <= 0 ||
  188. crop->w > link->w || crop->h > link->h) {
  189. av_log(ctx, AV_LOG_ERROR,
  190. "Invalid too big or non positive size for width '%d' or height '%d'\n",
  191. crop->w, crop->h);
  192. return AVERROR(EINVAL);
  193. }
  194. /* set default, required in the case the first computed value for x/y is NAN */
  195. crop->x = (link->w - crop->w) / 2;
  196. crop->y = (link->h - crop->h) / 2;
  197. crop->x &= ~((1 << crop->hsub) - 1);
  198. crop->y &= ~((1 << crop->vsub) - 1);
  199. return 0;
  200. fail_expr:
  201. av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
  202. return ret;
  203. }
  204. static int config_output(AVFilterLink *link)
  205. {
  206. CropContext *crop = link->src->priv;
  207. link->w = crop->w;
  208. link->h = crop->h;
  209. return 0;
  210. }
  211. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  212. {
  213. AVFilterContext *ctx = link->dst;
  214. CropContext *crop = ctx->priv;
  215. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  216. int i;
  217. frame->width = crop->w;
  218. frame->height = crop->h;
  219. crop->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
  220. NAN : frame->pts * av_q2d(link->time_base);
  221. crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
  222. crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
  223. crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
  224. normalize_double(&crop->x, crop->var_values[VAR_X]);
  225. normalize_double(&crop->y, crop->var_values[VAR_Y]);
  226. if (crop->x < 0) crop->x = 0;
  227. if (crop->y < 0) crop->y = 0;
  228. if ((unsigned)crop->x + (unsigned)crop->w > link->w) crop->x = link->w - crop->w;
  229. if ((unsigned)crop->y + (unsigned)crop->h > link->h) crop->y = link->h - crop->h;
  230. crop->x &= ~((1 << crop->hsub) - 1);
  231. crop->y &= ~((1 << crop->vsub) - 1);
  232. av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
  233. (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->x,
  234. crop->y, crop->x+crop->w, crop->y+crop->h);
  235. frame->data[0] += crop->y * frame->linesize[0];
  236. frame->data[0] += crop->x * crop->max_step[0];
  237. if (!(desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)) {
  238. for (i = 1; i < 3; i ++) {
  239. if (frame->data[i]) {
  240. frame->data[i] += (crop->y >> crop->vsub) * frame->linesize[i];
  241. frame->data[i] += (crop->x * crop->max_step[i]) >> crop->hsub;
  242. }
  243. }
  244. }
  245. /* alpha plane */
  246. if (frame->data[3]) {
  247. frame->data[3] += crop->y * frame->linesize[3];
  248. frame->data[3] += crop->x * crop->max_step[3];
  249. }
  250. crop->var_values[VAR_N] += 1.0;
  251. return ff_filter_frame(link->dst->outputs[0], frame);
  252. }
  253. static const AVFilterPad avfilter_vf_crop_inputs[] = {
  254. {
  255. .name = "default",
  256. .type = AVMEDIA_TYPE_VIDEO,
  257. .filter_frame = filter_frame,
  258. .get_video_buffer = ff_null_get_video_buffer,
  259. .config_props = config_input,
  260. },
  261. { NULL }
  262. };
  263. static const AVFilterPad avfilter_vf_crop_outputs[] = {
  264. {
  265. .name = "default",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. .config_props = config_output,
  268. },
  269. { NULL }
  270. };
  271. AVFilter avfilter_vf_crop = {
  272. .name = "crop",
  273. .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
  274. .priv_size = sizeof(CropContext),
  275. .query_formats = query_formats,
  276. .init = init,
  277. .uninit = uninit,
  278. .inputs = avfilter_vf_crop_inputs,
  279. .outputs = avfilter_vf_crop_outputs,
  280. };