vf_pad.c 14 KB

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