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.

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