vf_crop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 "avfilter.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/libm.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/mathematics.h"
  31. static const char *var_names[] = {
  32. "E",
  33. "PHI",
  34. "PI",
  35. "in_w", "iw", ///< width of the input video
  36. "in_h", "ih", ///< height of the input video
  37. "out_w", "ow", ///< width of the cropped video
  38. "out_h", "oh", ///< height of the cropped video
  39. "x",
  40. "y",
  41. "n", ///< number of frame
  42. "pos", ///< position in the file
  43. "t", ///< timestamp expressed in seconds
  44. NULL
  45. };
  46. enum var_name {
  47. VAR_E,
  48. VAR_PHI,
  49. VAR_PI,
  50. VAR_IN_W, VAR_IW,
  51. VAR_IN_H, VAR_IH,
  52. VAR_OUT_W, VAR_OW,
  53. VAR_OUT_H, VAR_OH,
  54. VAR_X,
  55. VAR_Y,
  56. VAR_N,
  57. VAR_POS,
  58. VAR_T,
  59. VAR_VARS_NB
  60. };
  61. typedef struct {
  62. int x; ///< x offset of the non-cropped area with respect to the input area
  63. int y; ///< y offset of the non-cropped area with respect to the input area
  64. int w; ///< width of the cropped area
  65. int h; ///< height of the cropped area
  66. int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
  67. int hsub, vsub; ///< chroma subsampling
  68. char x_expr[256], y_expr[256], ow_expr[256], oh_expr[256];
  69. AVExpr *x_pexpr, *y_pexpr; /* parsed expressions for x and y */
  70. double var_values[VAR_VARS_NB];
  71. } CropContext;
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. static const enum PixelFormat pix_fmts[] = {
  75. PIX_FMT_RGB48BE, PIX_FMT_RGB48LE,
  76. PIX_FMT_BGR48BE, PIX_FMT_BGR48LE,
  77. PIX_FMT_ARGB, PIX_FMT_RGBA,
  78. PIX_FMT_ABGR, PIX_FMT_BGRA,
  79. PIX_FMT_RGB24, PIX_FMT_BGR24,
  80. PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
  81. PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
  82. PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
  83. PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
  84. PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
  85. PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
  86. PIX_FMT_YUV422P16LE, PIX_FMT_YUV422P16BE,
  87. PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
  88. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  89. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  90. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  91. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  92. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
  93. PIX_FMT_YUVA420P,
  94. PIX_FMT_RGB8, PIX_FMT_BGR8,
  95. PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
  96. PIX_FMT_PAL8, PIX_FMT_GRAY8,
  97. PIX_FMT_NONE
  98. };
  99. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  100. return 0;
  101. }
  102. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  103. {
  104. CropContext *crop = ctx->priv;
  105. av_strlcpy(crop->ow_expr, "iw", sizeof(crop->ow_expr));
  106. av_strlcpy(crop->oh_expr, "ih", sizeof(crop->oh_expr));
  107. av_strlcpy(crop->x_expr, "(in_w-out_w)/2", sizeof(crop->x_expr));
  108. av_strlcpy(crop->y_expr, "(in_h-out_h)/2", sizeof(crop->y_expr));
  109. if (args)
  110. sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->ow_expr, crop->oh_expr, crop->x_expr, crop->y_expr);
  111. return 0;
  112. }
  113. static av_cold void uninit(AVFilterContext *ctx)
  114. {
  115. CropContext *crop = ctx->priv;
  116. av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
  117. av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
  118. }
  119. static inline int normalize_double(int *n, double d)
  120. {
  121. int ret = 0;
  122. if (isnan(d)) {
  123. ret = AVERROR(EINVAL);
  124. } else if (d > INT_MAX || d < INT_MIN) {
  125. *n = d > INT_MAX ? INT_MAX : INT_MIN;
  126. ret = AVERROR(EINVAL);
  127. } else
  128. *n = round(d);
  129. return ret;
  130. }
  131. static int config_input(AVFilterLink *link)
  132. {
  133. AVFilterContext *ctx = link->dst;
  134. CropContext *crop = ctx->priv;
  135. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
  136. int ret;
  137. const char *expr;
  138. double res;
  139. crop->var_values[VAR_E] = M_E;
  140. crop->var_values[VAR_PHI] = M_PHI;
  141. crop->var_values[VAR_PI] = M_PI;
  142. crop->var_values[VAR_IN_W] = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
  143. crop->var_values[VAR_IN_H] = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
  144. crop->var_values[VAR_X] = NAN;
  145. crop->var_values[VAR_Y] = NAN;
  146. crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
  147. crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
  148. crop->var_values[VAR_N] = 0;
  149. crop->var_values[VAR_T] = NAN;
  150. crop->var_values[VAR_POS] = NAN;
  151. av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
  152. crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  153. crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  154. if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
  155. var_names, crop->var_values,
  156. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
  157. crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
  158. if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
  159. var_names, crop->var_values,
  160. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
  161. crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
  162. /* evaluate again ow as it may depend on oh */
  163. if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
  164. var_names, crop->var_values,
  165. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
  166. crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
  167. if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
  168. normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
  169. av_log(ctx, AV_LOG_ERROR,
  170. "Too big value or invalid expression for out_w/ow or out_h/oh. "
  171. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  172. crop->ow_expr, crop->oh_expr);
  173. return AVERROR(EINVAL);
  174. }
  175. crop->w &= ~((1 << crop->hsub) - 1);
  176. crop->h &= ~((1 << crop->vsub) - 1);
  177. if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
  178. NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
  179. (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
  180. NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  181. return AVERROR(EINVAL);
  182. av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d\n",
  183. link->w, link->h, crop->w, crop->h);
  184. if (crop->w <= 0 || crop->h <= 0 ||
  185. crop->w > link->w || crop->h > link->h) {
  186. av_log(ctx, AV_LOG_ERROR,
  187. "Invalid too big or non positive size for width '%d' or height '%d'\n",
  188. crop->w, crop->h);
  189. return AVERROR(EINVAL);
  190. }
  191. /* set default, required in the case the first computed value for x/y is NAN */
  192. crop->x = (link->w - crop->w) / 2;
  193. crop->y = (link->h - crop->h) / 2;
  194. crop->x &= ~((1 << crop->hsub) - 1);
  195. crop->y &= ~((1 << crop->vsub) - 1);
  196. return 0;
  197. fail_expr:
  198. av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
  199. return ret;
  200. }
  201. static int config_output(AVFilterLink *link)
  202. {
  203. CropContext *crop = link->src->priv;
  204. link->w = crop->w;
  205. link->h = crop->h;
  206. return 0;
  207. }
  208. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  209. {
  210. AVFilterContext *ctx = link->dst;
  211. CropContext *crop = ctx->priv;
  212. AVFilterBufferRef *ref2;
  213. int i;
  214. ref2 = avfilter_ref_buffer(picref, ~0);
  215. ref2->video->w = crop->w;
  216. ref2->video->h = crop->h;
  217. crop->var_values[VAR_T] = picref->pts == AV_NOPTS_VALUE ?
  218. NAN : picref->pts * av_q2d(link->time_base);
  219. crop->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  220. crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
  221. crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
  222. crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
  223. normalize_double(&crop->x, crop->var_values[VAR_X]);
  224. normalize_double(&crop->y, crop->var_values[VAR_Y]);
  225. if (crop->x < 0) crop->x = 0;
  226. if (crop->y < 0) crop->y = 0;
  227. if ((unsigned)crop->x + (unsigned)crop->w > link->w) crop->x = link->w - crop->w;
  228. if ((unsigned)crop->y + (unsigned)crop->h > link->h) crop->y = link->h - crop->h;
  229. crop->x &= ~((1 << crop->hsub) - 1);
  230. crop->y &= ~((1 << crop->vsub) - 1);
  231. av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
  232. (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->x,
  233. crop->y, crop->x+crop->w, crop->y+crop->h);
  234. ref2->data[0] += crop->y * ref2->linesize[0];
  235. ref2->data[0] += crop->x * crop->max_step[0];
  236. if (!(av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)) {
  237. for (i = 1; i < 3; i ++) {
  238. if (ref2->data[i]) {
  239. ref2->data[i] += (crop->y >> crop->vsub) * ref2->linesize[i];
  240. ref2->data[i] += (crop->x * crop->max_step[i]) >> crop->hsub;
  241. }
  242. }
  243. }
  244. /* alpha plane */
  245. if (ref2->data[3]) {
  246. ref2->data[3] += crop->y * ref2->linesize[3];
  247. ref2->data[3] += crop->x * crop->max_step[3];
  248. }
  249. avfilter_start_frame(link->dst->outputs[0], ref2);
  250. }
  251. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  252. {
  253. AVFilterContext *ctx = link->dst;
  254. CropContext *crop = ctx->priv;
  255. if (y >= crop->y + crop->h || y + h <= crop->y)
  256. return;
  257. if (y < crop->y) {
  258. h -= crop->y - y;
  259. y = crop->y;
  260. }
  261. if (y + h > crop->y + crop->h)
  262. h = crop->y + crop->h - y;
  263. avfilter_draw_slice(ctx->outputs[0], y - crop->y, h, slice_dir);
  264. }
  265. static void end_frame(AVFilterLink *link)
  266. {
  267. CropContext *crop = link->dst->priv;
  268. crop->var_values[VAR_N] += 1.0;
  269. avfilter_unref_buffer(link->cur_buf);
  270. avfilter_end_frame(link->dst->outputs[0]);
  271. }
  272. AVFilter avfilter_vf_crop = {
  273. .name = "crop",
  274. .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
  275. .priv_size = sizeof(CropContext),
  276. .query_formats = query_formats,
  277. .init = init,
  278. .uninit = uninit,
  279. .inputs = (AVFilterPad[]) {{ .name = "default",
  280. .type = AVMEDIA_TYPE_VIDEO,
  281. .start_frame = start_frame,
  282. .draw_slice = draw_slice,
  283. .end_frame = end_frame,
  284. .get_video_buffer = avfilter_null_get_video_buffer,
  285. .config_props = config_input, },
  286. { .name = NULL}},
  287. .outputs = (AVFilterPad[]) {{ .name = "default",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .config_props = config_output, },
  290. { .name = NULL}},
  291. };