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.

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