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.

352 lines
12KB

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