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.

462 lines
16KB

  1. /*
  2. * Copyright (c) 2008 vmrsss
  3. * Copyright (c) 2009 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * video padding filter
  24. */
  25. #include <float.h> /* DBL_MAX */
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/common.h"
  32. #include "libavutil/eval.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/colorspace.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavutil/parseutils.h"
  37. #include "libavutil/mathematics.h"
  38. #include "libavutil/opt.h"
  39. #include "drawutils.h"
  40. static const char *const var_names[] = {
  41. "in_w", "iw",
  42. "in_h", "ih",
  43. "out_w", "ow",
  44. "out_h", "oh",
  45. "x",
  46. "y",
  47. "a",
  48. "sar",
  49. "dar",
  50. "hsub",
  51. "vsub",
  52. NULL
  53. };
  54. enum var_name {
  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_A,
  62. VAR_SAR,
  63. VAR_DAR,
  64. VAR_HSUB,
  65. VAR_VSUB,
  66. VARS_NB
  67. };
  68. static int query_formats(AVFilterContext *ctx)
  69. {
  70. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  71. }
  72. enum EvalMode {
  73. EVAL_MODE_INIT,
  74. EVAL_MODE_FRAME,
  75. EVAL_MODE_NB
  76. };
  77. typedef struct PadContext {
  78. const AVClass *class;
  79. int w, h; ///< output dimensions, a value of 0 will result in the input size
  80. int x, y; ///< offsets of the input area with respect to the padded area
  81. int in_w, in_h; ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
  82. int inlink_w, inlink_h;
  83. AVRational aspect;
  84. char *w_expr; ///< width expression string
  85. char *h_expr; ///< height expression string
  86. char *x_expr; ///< width expression string
  87. char *y_expr; ///< height expression string
  88. uint8_t rgba_color[4]; ///< color for the padding area
  89. FFDrawContext draw;
  90. FFDrawColor color;
  91. int eval_mode; ///< expression evaluation mode
  92. } PadContext;
  93. static int config_input(AVFilterLink *inlink)
  94. {
  95. AVFilterContext *ctx = inlink->dst;
  96. PadContext *s = ctx->priv;
  97. AVRational adjusted_aspect = s->aspect;
  98. int ret;
  99. double var_values[VARS_NB], res;
  100. char *expr;
  101. ff_draw_init(&s->draw, inlink->format, 0);
  102. ff_draw_color(&s->draw, &s->color, s->rgba_color);
  103. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  104. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  105. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  106. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  107. var_values[VAR_A] = (double) inlink->w / inlink->h;
  108. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  109. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  110. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  111. var_values[VAR_HSUB] = 1 << s->draw.hsub_max;
  112. var_values[VAR_VSUB] = 1 << s->draw.vsub_max;
  113. /* evaluate width and height */
  114. av_expr_parse_and_eval(&res, (expr = s->w_expr),
  115. var_names, var_values,
  116. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  117. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  118. if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  119. var_names, var_values,
  120. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  121. goto eval_fail;
  122. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  123. if (!s->h)
  124. var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
  125. /* evaluate the width again, as it may depend on the evaluated output height */
  126. if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  127. var_names, var_values,
  128. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  129. goto eval_fail;
  130. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  131. if (!s->w)
  132. var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
  133. if (adjusted_aspect.num && adjusted_aspect.den) {
  134. adjusted_aspect = av_div_q(adjusted_aspect, inlink->sample_aspect_ratio);
  135. if (s->h < av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num)) {
  136. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num);
  137. } else {
  138. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = av_rescale(s->h, adjusted_aspect.num, adjusted_aspect.den);
  139. }
  140. }
  141. /* evaluate x and y */
  142. av_expr_parse_and_eval(&res, (expr = s->x_expr),
  143. var_names, var_values,
  144. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  145. s->x = var_values[VAR_X] = res;
  146. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  147. var_names, var_values,
  148. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  149. goto eval_fail;
  150. s->y = var_values[VAR_Y] = res;
  151. /* evaluate x again, as it may depend on the evaluated y value */
  152. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  153. var_names, var_values,
  154. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  155. goto eval_fail;
  156. s->x = var_values[VAR_X] = res;
  157. if (s->x < 0 || s->x + inlink->w > s->w)
  158. s->x = var_values[VAR_X] = (s->w - inlink->w) / 2;
  159. if (s->y < 0 || s->y + inlink->h > s->h)
  160. s->y = var_values[VAR_Y] = (s->h - inlink->h) / 2;
  161. /* sanity check params */
  162. if (s->w < 0 || s->h < 0) {
  163. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  164. return AVERROR(EINVAL);
  165. }
  166. s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
  167. s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
  168. s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
  169. s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
  170. s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
  171. s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
  172. s->inlink_w = inlink->w;
  173. s->inlink_h = inlink->h;
  174. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
  175. inlink->w, inlink->h, s->w, s->h, s->x, s->y,
  176. s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
  177. if (s->w <= 0 || s->h <= 0) {
  178. av_log(ctx, AV_LOG_ERROR,
  179. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  180. s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
  181. return AVERROR(EINVAL);
  182. }
  183. return 0;
  184. eval_fail:
  185. av_log(NULL, AV_LOG_ERROR,
  186. "Error when evaluating the expression '%s'\n", expr);
  187. return ret;
  188. }
  189. static int config_output(AVFilterLink *outlink)
  190. {
  191. PadContext *s = outlink->src->priv;
  192. outlink->w = s->w;
  193. outlink->h = s->h;
  194. return 0;
  195. }
  196. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  197. {
  198. PadContext *s = inlink->dst->priv;
  199. AVFrame *frame;
  200. int plane;
  201. if (s->inlink_w <= 0)
  202. return NULL;
  203. frame = ff_get_video_buffer(inlink->dst->outputs[0],
  204. w + (s->w - s->in_w),
  205. h + (s->h - s->in_h) + (s->x > 0));
  206. if (!frame)
  207. return NULL;
  208. frame->width = w;
  209. frame->height = h;
  210. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  211. int hsub = s->draw.hsub[plane];
  212. int vsub = s->draw.vsub[plane];
  213. frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
  214. (s->y >> vsub) * frame->linesize[plane];
  215. }
  216. return frame;
  217. }
  218. /* check whether each plane in this buffer can be padded without copying */
  219. static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
  220. {
  221. int planes[4] = { -1, -1, -1, -1}, *p = planes;
  222. int i, j;
  223. /* get all planes in this buffer */
  224. for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
  225. if (av_frame_get_plane_buffer(frame, i) == buf)
  226. *p++ = i;
  227. }
  228. /* for each plane in this buffer, check that it can be padded without
  229. * going over buffer bounds or other planes */
  230. for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
  231. int hsub = s->draw.hsub[planes[i]];
  232. int vsub = s->draw.vsub[planes[i]];
  233. uint8_t *start = frame->data[planes[i]];
  234. uint8_t *end = start + (frame->height >> vsub) *
  235. frame->linesize[planes[i]];
  236. /* amount of free space needed before the start and after the end
  237. * of the plane */
  238. ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
  239. (s->y >> vsub) * frame->linesize[planes[i]];
  240. ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
  241. s->draw.pixelstep[planes[i]] +
  242. ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]];
  243. if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
  244. return 1;
  245. if (start - buf->data < req_start ||
  246. (buf->data + buf->size) - end < req_end)
  247. return 1;
  248. for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  249. int vsub1 = s->draw.vsub[planes[j]];
  250. uint8_t *start1 = frame->data[planes[j]];
  251. uint8_t *end1 = start1 + (frame->height >> vsub1) *
  252. frame->linesize[planes[j]];
  253. if (i == j)
  254. continue;
  255. if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) ||
  256. FFSIGN(end - start1) != FFSIGN(end - start1 + req_end))
  257. return 1;
  258. }
  259. }
  260. return 0;
  261. }
  262. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  263. {
  264. int i;
  265. if (!av_frame_is_writable(frame))
  266. return 1;
  267. for (i = 0; i < 4 && frame->buf[i]; i++)
  268. if (buffer_needs_copy(s, frame, frame->buf[i]))
  269. return 1;
  270. return 0;
  271. }
  272. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  273. {
  274. PadContext *s = inlink->dst->priv;
  275. AVFilterLink *outlink = inlink->dst->outputs[0];
  276. AVFrame *out;
  277. int needs_copy;
  278. if(s->eval_mode == EVAL_MODE_FRAME && (
  279. in->width != s->inlink_w
  280. || in->height != s->inlink_h
  281. || in->format != outlink->format
  282. || in->sample_aspect_ratio.den != outlink->sample_aspect_ratio.den || in->sample_aspect_ratio.num != outlink->sample_aspect_ratio.num)) {
  283. int ret;
  284. inlink->dst->inputs[0]->format = in->format;
  285. inlink->dst->inputs[0]->w = in->width;
  286. inlink->dst->inputs[0]->h = in->height;
  287. inlink->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
  288. inlink->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
  289. if ((ret = config_input(inlink)) < 0) {
  290. s->inlink_w = -1;
  291. return ret;
  292. }
  293. if ((ret = config_output(outlink)) < 0) {
  294. s->inlink_w = -1;
  295. return ret;
  296. }
  297. }
  298. needs_copy = frame_needs_copy(s, in);
  299. if (needs_copy) {
  300. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  301. out = ff_get_video_buffer(inlink->dst->outputs[0],
  302. FFMAX(inlink->w, s->w),
  303. FFMAX(inlink->h, s->h));
  304. if (!out) {
  305. av_frame_free(&in);
  306. return AVERROR(ENOMEM);
  307. }
  308. av_frame_copy_props(out, in);
  309. } else {
  310. int i;
  311. out = in;
  312. for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) {
  313. int hsub = s->draw.hsub[i];
  314. int vsub = s->draw.vsub[i];
  315. out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
  316. (s->y >> vsub) * out->linesize[i];
  317. }
  318. }
  319. /* top bar */
  320. if (s->y) {
  321. ff_fill_rectangle(&s->draw, &s->color,
  322. out->data, out->linesize,
  323. 0, 0, s->w, s->y);
  324. }
  325. /* bottom bar */
  326. if (s->h > s->y + s->in_h) {
  327. ff_fill_rectangle(&s->draw, &s->color,
  328. out->data, out->linesize,
  329. 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  330. }
  331. /* left border */
  332. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  333. 0, s->y, s->x, in->height);
  334. if (needs_copy) {
  335. ff_copy_rectangle2(&s->draw,
  336. out->data, out->linesize, in->data, in->linesize,
  337. s->x, s->y, 0, 0, in->width, in->height);
  338. }
  339. /* right border */
  340. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  341. s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  342. in->height);
  343. out->width = s->w;
  344. out->height = s->h;
  345. if (in != out)
  346. av_frame_free(&in);
  347. return ff_filter_frame(inlink->dst->outputs[0], out);
  348. }
  349. #define OFFSET(x) offsetof(PadContext, x)
  350. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  351. static const AVOption pad_options[] = {
  352. { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  353. { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  354. { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  355. { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  356. { "x", "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  357. { "y", "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  358. { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  359. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  360. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  361. { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  362. { "aspect", "pad to fit an aspect instead of a resolution", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, DBL_MAX, FLAGS },
  363. { NULL }
  364. };
  365. AVFILTER_DEFINE_CLASS(pad);
  366. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  367. {
  368. .name = "default",
  369. .type = AVMEDIA_TYPE_VIDEO,
  370. .config_props = config_input,
  371. .get_video_buffer = get_video_buffer,
  372. .filter_frame = filter_frame,
  373. },
  374. { NULL }
  375. };
  376. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  377. {
  378. .name = "default",
  379. .type = AVMEDIA_TYPE_VIDEO,
  380. .config_props = config_output,
  381. },
  382. { NULL }
  383. };
  384. AVFilter ff_vf_pad = {
  385. .name = "pad",
  386. .description = NULL_IF_CONFIG_SMALL("Pad the input video."),
  387. .priv_size = sizeof(PadContext),
  388. .priv_class = &pad_class,
  389. .query_formats = query_formats,
  390. .inputs = avfilter_vf_pad_inputs,
  391. .outputs = avfilter_vf_pad_outputs,
  392. };