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.

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