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.

408 lines
14KB

  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 "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/eval.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/colorspace.h"
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/parseutils.h"
  38. #include "libavutil/mathematics.h"
  39. #include "libavutil/opt.h"
  40. #include "drawutils.h"
  41. static const char *const var_names[] = {
  42. "in_w", "iw",
  43. "in_h", "ih",
  44. "out_w", "ow",
  45. "out_h", "oh",
  46. "x",
  47. "y",
  48. "a",
  49. "sar",
  50. "dar",
  51. "hsub",
  52. "vsub",
  53. NULL
  54. };
  55. enum var_name {
  56. VAR_IN_W, VAR_IW,
  57. VAR_IN_H, VAR_IH,
  58. VAR_OUT_W, VAR_OW,
  59. VAR_OUT_H, VAR_OH,
  60. VAR_X,
  61. VAR_Y,
  62. VAR_A,
  63. VAR_SAR,
  64. VAR_DAR,
  65. VAR_HSUB,
  66. VAR_VSUB,
  67. VARS_NB
  68. };
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  72. return 0;
  73. }
  74. typedef struct {
  75. const AVClass *class;
  76. int w, h; ///< output dimensions, a value of 0 will result in the input size
  77. int x, y; ///< offsets of the input area with respect to the padded area
  78. 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
  79. char *w_expr; ///< width expression string
  80. char *h_expr; ///< height expression string
  81. char *x_expr; ///< width expression string
  82. char *y_expr; ///< height expression string
  83. uint8_t rgba_color[4]; ///< color for the padding area
  84. FFDrawContext draw;
  85. FFDrawColor color;
  86. } PadContext;
  87. static int config_input(AVFilterLink *inlink)
  88. {
  89. AVFilterContext *ctx = inlink->dst;
  90. PadContext *s = ctx->priv;
  91. int ret;
  92. double var_values[VARS_NB], res;
  93. char *expr;
  94. ff_draw_init(&s->draw, inlink->format, 0);
  95. ff_draw_color(&s->draw, &s->color, s->rgba_color);
  96. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  97. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  98. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  99. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  100. var_values[VAR_A] = (double) inlink->w / inlink->h;
  101. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  102. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  103. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  104. var_values[VAR_HSUB] = 1 << s->draw.hsub_max;
  105. var_values[VAR_VSUB] = 1 << s->draw.vsub_max;
  106. /* evaluate width and height */
  107. av_expr_parse_and_eval(&res, (expr = s->w_expr),
  108. var_names, var_values,
  109. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  110. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  111. if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  112. var_names, var_values,
  113. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  114. goto eval_fail;
  115. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  116. /* evaluate the width again, as it may depend on the evaluated output height */
  117. if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  118. var_names, var_values,
  119. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  120. goto eval_fail;
  121. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  122. /* evaluate x and y */
  123. av_expr_parse_and_eval(&res, (expr = s->x_expr),
  124. var_names, var_values,
  125. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  126. s->x = var_values[VAR_X] = res;
  127. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  128. var_names, var_values,
  129. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  130. goto eval_fail;
  131. s->y = var_values[VAR_Y] = res;
  132. /* evaluate x again, as it may depend on the evaluated y value */
  133. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  134. var_names, var_values,
  135. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  136. goto eval_fail;
  137. s->x = var_values[VAR_X] = res;
  138. /* sanity check params */
  139. if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
  140. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  141. return AVERROR(EINVAL);
  142. }
  143. if (!s->w)
  144. s->w = inlink->w;
  145. if (!s->h)
  146. s->h = inlink->h;
  147. s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
  148. s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
  149. s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
  150. s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
  151. s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
  152. s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
  153. 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",
  154. inlink->w, inlink->h, s->w, s->h, s->x, s->y,
  155. s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
  156. if (s->x < 0 || s->y < 0 ||
  157. s->w <= 0 || s->h <= 0 ||
  158. (unsigned)s->x + (unsigned)inlink->w > s->w ||
  159. (unsigned)s->y + (unsigned)inlink->h > s->h) {
  160. av_log(ctx, AV_LOG_ERROR,
  161. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  162. s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
  163. return AVERROR(EINVAL);
  164. }
  165. return 0;
  166. eval_fail:
  167. av_log(NULL, AV_LOG_ERROR,
  168. "Error when evaluating the expression '%s'\n", expr);
  169. return ret;
  170. }
  171. static int config_output(AVFilterLink *outlink)
  172. {
  173. PadContext *s = outlink->src->priv;
  174. outlink->w = s->w;
  175. outlink->h = s->h;
  176. return 0;
  177. }
  178. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  179. {
  180. PadContext *s = inlink->dst->priv;
  181. AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
  182. w + (s->w - s->in_w),
  183. h + (s->h - s->in_h));
  184. int plane;
  185. if (!frame)
  186. return NULL;
  187. frame->width = w;
  188. frame->height = h;
  189. for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
  190. int hsub = s->draw.hsub[plane];
  191. int vsub = s->draw.vsub[plane];
  192. frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
  193. (s->y >> vsub) * frame->linesize[plane];
  194. }
  195. return frame;
  196. }
  197. /* check whether each plane in this buffer can be padded without copying */
  198. static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
  199. {
  200. int planes[4] = { -1, -1, -1, -1}, *p = planes;
  201. int i, j;
  202. /* get all planes in this buffer */
  203. for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
  204. if (av_frame_get_plane_buffer(frame, i) == buf)
  205. *p++ = i;
  206. }
  207. /* for each plane in this buffer, check that it can be padded without
  208. * going over buffer bounds or other planes */
  209. for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
  210. int hsub = s->draw.hsub[planes[i]];
  211. int vsub = s->draw.vsub[planes[i]];
  212. uint8_t *start = frame->data[planes[i]];
  213. uint8_t *end = start + (frame->height >> vsub) *
  214. frame->linesize[planes[i]];
  215. /* amount of free space needed before the start and after the end
  216. * of the plane */
  217. ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
  218. (s->y >> vsub) * frame->linesize[planes[i]];
  219. ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
  220. s->draw.pixelstep[planes[i]] +
  221. (s->y >> vsub) * frame->linesize[planes[i]];
  222. if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
  223. return 1;
  224. if (start - buf->data < req_start ||
  225. (buf->data + buf->size) - end < req_end)
  226. return 1;
  227. #define SIGN(x) ((x) > 0 ? 1 : -1)
  228. for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  229. int vsub1 = s->draw.vsub[planes[j]];
  230. uint8_t *start1 = frame->data[planes[j]];
  231. uint8_t *end1 = start1 + (frame->height >> vsub1) *
  232. frame->linesize[planes[j]];
  233. if (i == j)
  234. continue;
  235. if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
  236. SIGN(end - start1) != SIGN(end - start1 + req_end))
  237. return 1;
  238. }
  239. }
  240. return 0;
  241. }
  242. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  243. {
  244. int i;
  245. if (!av_frame_is_writable(frame))
  246. return 1;
  247. for (i = 0; i < 4 && frame->buf[i]; i++)
  248. if (buffer_needs_copy(s, frame, frame->buf[i]))
  249. return 1;
  250. return 0;
  251. }
  252. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  253. {
  254. PadContext *s = inlink->dst->priv;
  255. AVFrame *out;
  256. int needs_copy = frame_needs_copy(s, in);
  257. if (needs_copy) {
  258. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  259. out = ff_get_video_buffer(inlink->dst->outputs[0],
  260. FFMAX(inlink->w, s->w),
  261. FFMAX(inlink->h, s->h));
  262. if (!out) {
  263. av_frame_free(&in);
  264. return AVERROR(ENOMEM);
  265. }
  266. av_frame_copy_props(out, in);
  267. } else {
  268. int i;
  269. out = in;
  270. for (i = 0; i < 4 && out->data[i]; i++) {
  271. int hsub = s->draw.hsub[i];
  272. int vsub = s->draw.vsub[i];
  273. out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
  274. (s->y >> vsub) * out->linesize[i];
  275. }
  276. }
  277. /* top bar */
  278. if (s->y) {
  279. ff_fill_rectangle(&s->draw, &s->color,
  280. out->data, out->linesize,
  281. 0, 0, s->w, s->y);
  282. }
  283. /* bottom bar */
  284. if (s->h > s->y + s->in_h) {
  285. ff_fill_rectangle(&s->draw, &s->color,
  286. out->data, out->linesize,
  287. 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  288. }
  289. /* left border */
  290. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  291. 0, s->y, s->x, in->height);
  292. if (needs_copy) {
  293. ff_copy_rectangle2(&s->draw,
  294. out->data, out->linesize, in->data, in->linesize,
  295. s->x, s->y, 0, 0, in->width, in->height);
  296. }
  297. /* right border */
  298. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  299. s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  300. in->height);
  301. out->width = s->w;
  302. out->height = s->h;
  303. if (in != out)
  304. av_frame_free(&in);
  305. return ff_filter_frame(inlink->dst->outputs[0], out);
  306. }
  307. #define OFFSET(x) offsetof(PadContext, x)
  308. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  309. static const AVOption pad_options[] = {
  310. { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  311. { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  312. { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  313. { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  314. { "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 },
  315. { "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 },
  316. { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  317. { NULL },
  318. };
  319. AVFILTER_DEFINE_CLASS(pad);
  320. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  321. {
  322. .name = "default",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .config_props = config_input,
  325. .get_video_buffer = get_video_buffer,
  326. .filter_frame = filter_frame,
  327. },
  328. { NULL }
  329. };
  330. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  331. {
  332. .name = "default",
  333. .type = AVMEDIA_TYPE_VIDEO,
  334. .config_props = config_output,
  335. },
  336. { NULL }
  337. };
  338. AVFilter avfilter_vf_pad = {
  339. .name = "pad",
  340. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  341. .priv_size = sizeof(PadContext),
  342. .priv_class = &pad_class,
  343. .query_formats = query_formats,
  344. .inputs = avfilter_vf_pad_inputs,
  345. .outputs = avfilter_vf_pad_outputs,
  346. };