vf_pad.c 14 KB

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