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.

407 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] && frame->linesize[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. for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  228. int vsub1 = s->draw.vsub[planes[j]];
  229. uint8_t *start1 = frame->data[planes[j]];
  230. uint8_t *end1 = start1 + (frame->height >> vsub1) *
  231. frame->linesize[planes[j]];
  232. if (i == j)
  233. continue;
  234. if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) ||
  235. FFSIGN(end - start1) != FFSIGN(end - start1 + req_end))
  236. return 1;
  237. }
  238. }
  239. return 0;
  240. }
  241. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  242. {
  243. int i;
  244. if (!av_frame_is_writable(frame))
  245. return 1;
  246. for (i = 0; i < 4 && frame->buf[i]; i++)
  247. if (buffer_needs_copy(s, frame, frame->buf[i]))
  248. return 1;
  249. return 0;
  250. }
  251. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  252. {
  253. PadContext *s = inlink->dst->priv;
  254. AVFrame *out;
  255. int needs_copy = frame_needs_copy(s, in);
  256. if (needs_copy) {
  257. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  258. out = ff_get_video_buffer(inlink->dst->outputs[0],
  259. FFMAX(inlink->w, s->w),
  260. FFMAX(inlink->h, s->h));
  261. if (!out) {
  262. av_frame_free(&in);
  263. return AVERROR(ENOMEM);
  264. }
  265. av_frame_copy_props(out, in);
  266. } else {
  267. int i;
  268. out = in;
  269. for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) {
  270. int hsub = s->draw.hsub[i];
  271. int vsub = s->draw.vsub[i];
  272. out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
  273. (s->y >> vsub) * out->linesize[i];
  274. }
  275. }
  276. /* top bar */
  277. if (s->y) {
  278. ff_fill_rectangle(&s->draw, &s->color,
  279. out->data, out->linesize,
  280. 0, 0, s->w, s->y);
  281. }
  282. /* bottom bar */
  283. if (s->h > s->y + s->in_h) {
  284. ff_fill_rectangle(&s->draw, &s->color,
  285. out->data, out->linesize,
  286. 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  287. }
  288. /* left border */
  289. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  290. 0, s->y, s->x, in->height);
  291. if (needs_copy) {
  292. ff_copy_rectangle2(&s->draw,
  293. out->data, out->linesize, in->data, in->linesize,
  294. s->x, s->y, 0, 0, in->width, in->height);
  295. }
  296. /* right border */
  297. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  298. s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  299. in->height);
  300. out->width = s->w;
  301. out->height = s->h;
  302. if (in != out)
  303. av_frame_free(&in);
  304. return ff_filter_frame(inlink->dst->outputs[0], out);
  305. }
  306. #define OFFSET(x) offsetof(PadContext, x)
  307. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  308. static const AVOption pad_options[] = {
  309. { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  310. { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  311. { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  312. { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  313. { "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 },
  314. { "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 },
  315. { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  316. { NULL },
  317. };
  318. AVFILTER_DEFINE_CLASS(pad);
  319. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  320. {
  321. .name = "default",
  322. .type = AVMEDIA_TYPE_VIDEO,
  323. .config_props = config_input,
  324. .get_video_buffer = get_video_buffer,
  325. .filter_frame = filter_frame,
  326. },
  327. { NULL }
  328. };
  329. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  330. {
  331. .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .config_props = config_output,
  334. },
  335. { NULL }
  336. };
  337. AVFilter avfilter_vf_pad = {
  338. .name = "pad",
  339. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  340. .priv_size = sizeof(PadContext),
  341. .priv_class = &pad_class,
  342. .query_formats = query_formats,
  343. .inputs = avfilter_vf_pad_inputs,
  344. .outputs = avfilter_vf_pad_outputs,
  345. };