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.

366 lines
13KB

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