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.

433 lines
15KB

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