vf_pad.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright (c) 2008 vmrsss
  3. * Copyright (c) 2009 Stefano Sabatini
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. "PI",
  41. "PHI",
  42. "E",
  43. "in_w", "iw",
  44. "in_h", "ih",
  45. "out_w", "ow",
  46. "out_h", "oh",
  47. "x",
  48. "y",
  49. "a",
  50. "hsub",
  51. "vsub",
  52. NULL
  53. };
  54. enum var_name {
  55. VAR_PI,
  56. VAR_PHI,
  57. VAR_E,
  58. VAR_IN_W, VAR_IW,
  59. VAR_IN_H, VAR_IH,
  60. VAR_OUT_W, VAR_OW,
  61. VAR_OUT_H, VAR_OH,
  62. VAR_X,
  63. VAR_Y,
  64. VAR_A,
  65. VAR_HSUB,
  66. VAR_VSUB,
  67. VARS_NB
  68. };
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. static const enum AVPixelFormat pix_fmts[] = {
  72. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  73. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  74. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  75. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  76. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  77. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  78. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  79. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  80. AV_PIX_FMT_YUVA420P,
  81. AV_PIX_FMT_NONE
  82. };
  83. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  84. return 0;
  85. }
  86. typedef struct PadContext {
  87. const AVClass *class;
  88. int w, h; ///< output dimensions, a value of 0 will result in the input size
  89. int x, y; ///< offsets of the input area with respect to the padded area
  90. 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
  91. char *w_expr; ///< width expression string
  92. char *h_expr; ///< height expression string
  93. char *x_expr; ///< width expression string
  94. char *y_expr; ///< height expression string
  95. char *color_str;
  96. uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  97. uint8_t *line[4];
  98. int line_step[4];
  99. int hsub, vsub; ///< chroma subsampling values
  100. } PadContext;
  101. static av_cold int init(AVFilterContext *ctx)
  102. {
  103. PadContext *s = ctx->priv;
  104. if (av_parse_color(s->color, s->color_str, -1, ctx) < 0)
  105. return AVERROR(EINVAL);
  106. return 0;
  107. }
  108. static av_cold void uninit(AVFilterContext *ctx)
  109. {
  110. PadContext *s = ctx->priv;
  111. int i;
  112. for (i = 0; i < 4; i++) {
  113. av_freep(&s->line[i]);
  114. s->line_step[i] = 0;
  115. }
  116. }
  117. static int config_input(AVFilterLink *inlink)
  118. {
  119. AVFilterContext *ctx = inlink->dst;
  120. PadContext *s = ctx->priv;
  121. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  122. uint8_t rgba_color[4];
  123. int ret, is_packed_rgba;
  124. double var_values[VARS_NB], res;
  125. char *expr;
  126. s->hsub = pix_desc->log2_chroma_w;
  127. s->vsub = pix_desc->log2_chroma_h;
  128. var_values[VAR_PI] = M_PI;
  129. var_values[VAR_PHI] = M_PHI;
  130. var_values[VAR_E] = M_E;
  131. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  132. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  133. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  134. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  135. var_values[VAR_A] = (double) inlink->w / inlink->h;
  136. var_values[VAR_HSUB] = 1<<s->hsub;
  137. var_values[VAR_VSUB] = 1<<s->vsub;
  138. /* evaluate width and height */
  139. av_expr_parse_and_eval(&res, (expr = s->w_expr),
  140. var_names, var_values,
  141. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  142. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  143. if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  144. var_names, var_values,
  145. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  146. goto eval_fail;
  147. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  148. if (!s->h)
  149. var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
  150. /* evaluate the width again, as it may depend on the evaluated output height */
  151. if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  152. var_names, var_values,
  153. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  154. goto eval_fail;
  155. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  156. if (!s->w)
  157. var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
  158. /* evaluate x and y */
  159. av_expr_parse_and_eval(&res, (expr = s->x_expr),
  160. var_names, var_values,
  161. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  162. s->x = var_values[VAR_X] = res;
  163. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  164. var_names, var_values,
  165. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  166. goto eval_fail;
  167. s->y = var_values[VAR_Y] = res;
  168. /* evaluate x again, as it may depend on the evaluated y value */
  169. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  170. var_names, var_values,
  171. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  172. goto eval_fail;
  173. s->x = var_values[VAR_X] = res;
  174. /* sanity check params */
  175. if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
  176. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  177. return AVERROR(EINVAL);
  178. }
  179. s->w &= ~((1 << s->hsub) - 1);
  180. s->h &= ~((1 << s->vsub) - 1);
  181. s->x &= ~((1 << s->hsub) - 1);
  182. s->y &= ~((1 << s->vsub) - 1);
  183. s->in_w = inlink->w & ~((1 << s->hsub) - 1);
  184. s->in_h = inlink->h & ~((1 << s->vsub) - 1);
  185. memcpy(rgba_color, s->color, sizeof(rgba_color));
  186. ff_fill_line_with_color(s->line, s->line_step, s->w, s->color,
  187. inlink->format, rgba_color, &is_packed_rgba, NULL);
  188. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
  189. inlink->w, inlink->h, s->w, s->h, s->x, s->y,
  190. s->color[0], s->color[1], s->color[2], s->color[3],
  191. is_packed_rgba ? "rgba" : "yuva");
  192. if (s->x < 0 || s->y < 0 ||
  193. s->w <= 0 || s->h <= 0 ||
  194. (unsigned)s->x + (unsigned)inlink->w > s->w ||
  195. (unsigned)s->y + (unsigned)inlink->h > s->h) {
  196. av_log(ctx, AV_LOG_ERROR,
  197. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  198. s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
  199. return AVERROR(EINVAL);
  200. }
  201. return 0;
  202. eval_fail:
  203. av_log(NULL, AV_LOG_ERROR,
  204. "Error when evaluating the expression '%s'\n", expr);
  205. return ret;
  206. }
  207. static int config_output(AVFilterLink *outlink)
  208. {
  209. PadContext *s = outlink->src->priv;
  210. outlink->w = s->w;
  211. outlink->h = s->h;
  212. return 0;
  213. }
  214. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  215. {
  216. PadContext *s = inlink->dst->priv;
  217. AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
  218. w + (s->w - s->in_w),
  219. h + (s->h - s->in_h));
  220. int plane;
  221. if (!frame)
  222. return NULL;
  223. frame->width = w;
  224. frame->height = h;
  225. for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
  226. int hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
  227. int vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
  228. frame->data[plane] += (s->x >> hsub) * s->line_step[plane] +
  229. (s->y >> vsub) * frame->linesize[plane];
  230. }
  231. return frame;
  232. }
  233. /* check whether each plane in this buffer can be padded without copying */
  234. static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
  235. {
  236. int planes[4] = { -1, -1, -1, -1}, *p = planes;
  237. int i, j;
  238. /* get all planes in this buffer */
  239. for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
  240. if (av_frame_get_plane_buffer(frame, i) == buf)
  241. *p++ = i;
  242. }
  243. /* for each plane in this buffer, check that it can be padded without
  244. * going over buffer bounds or other planes */
  245. for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
  246. int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
  247. int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
  248. uint8_t *start = frame->data[planes[i]];
  249. uint8_t *end = start + (frame->height >> hsub) *
  250. frame->linesize[planes[i]];
  251. /* amount of free space needed before the start and after the end
  252. * of the plane */
  253. ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
  254. (s->y >> vsub) * frame->linesize[planes[i]];
  255. ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
  256. s->line_step[planes[i]] +
  257. (s->y >> vsub) * frame->linesize[planes[i]];
  258. if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
  259. return 1;
  260. if (start - buf->data < req_start ||
  261. (buf->data + buf->size) - end < req_end)
  262. return 1;
  263. #define SIGN(x) ((x) > 0 ? 1 : -1)
  264. for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  265. int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
  266. uint8_t *start1 = frame->data[planes[j]];
  267. uint8_t *end1 = start1 + (frame->height >> hsub1) *
  268. frame->linesize[planes[j]];
  269. if (i == j)
  270. continue;
  271. if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
  272. SIGN(end - start1) != SIGN(end - start1 + req_end))
  273. return 1;
  274. }
  275. }
  276. return 0;
  277. }
  278. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  279. {
  280. int i;
  281. if (!av_frame_is_writable(frame))
  282. return 1;
  283. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  284. if (buffer_needs_copy(s, frame, frame->buf[i]))
  285. return 1;
  286. return 0;
  287. }
  288. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  289. {
  290. PadContext *s = inlink->dst->priv;
  291. AVFrame *out;
  292. int needs_copy = frame_needs_copy(s, in);
  293. if (needs_copy) {
  294. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  295. out = ff_get_video_buffer(inlink->dst->outputs[0],
  296. FFMAX(inlink->w, s->w),
  297. FFMAX(inlink->h, s->h));
  298. if (!out) {
  299. av_frame_free(&in);
  300. return AVERROR(ENOMEM);
  301. }
  302. av_frame_copy_props(out, in);
  303. } else {
  304. int i;
  305. out = in;
  306. for (i = 0; i < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
  307. int hsub = (i == 1 || i == 2) ? s->hsub : 0;
  308. int vsub = (i == 1 || i == 2) ? s->vsub : 0;
  309. out->data[i] -= (s->x >> hsub) * s->line_step[i] +
  310. (s->y >> vsub) * out->linesize[i];
  311. }
  312. }
  313. /* top bar */
  314. if (s->y) {
  315. ff_draw_rectangle(out->data, out->linesize,
  316. s->line, s->line_step, s->hsub, s->vsub,
  317. 0, 0, s->w, s->y);
  318. }
  319. /* bottom bar */
  320. if (s->h > s->y + s->in_h) {
  321. ff_draw_rectangle(out->data, out->linesize,
  322. s->line, s->line_step, s->hsub, s->vsub,
  323. 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  324. }
  325. /* left border */
  326. ff_draw_rectangle(out->data, out->linesize, s->line, s->line_step,
  327. s->hsub, s->vsub, 0, s->y, s->x, in->height);
  328. if (needs_copy) {
  329. ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
  330. s->line_step, s->hsub, s->vsub,
  331. s->x, s->y, 0, in->width, in->height);
  332. }
  333. /* right border */
  334. ff_draw_rectangle(out->data, out->linesize,
  335. s->line, s->line_step, s->hsub, s->vsub,
  336. s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  337. in->height);
  338. out->width = s->w;
  339. out->height = s->h;
  340. if (in != out)
  341. av_frame_free(&in);
  342. return ff_filter_frame(inlink->dst->outputs[0], out);
  343. }
  344. #define OFFSET(x) offsetof(PadContext, x)
  345. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  346. static const AVOption options[] = {
  347. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
  348. { "height", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
  349. { "x", "Horizontal position of the left edge of the input video in the "
  350. "output video", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  351. { "y", "Vertical position of the top edge of the input video in the "
  352. "output video", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  353. { "color", "Color of the padded area", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, .flags = FLAGS },
  354. { NULL },
  355. };
  356. static const AVClass pad_class = {
  357. .class_name = "pad",
  358. .item_name = av_default_item_name,
  359. .option = options,
  360. .version = LIBAVUTIL_VERSION_INT,
  361. };
  362. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  363. {
  364. .name = "default",
  365. .type = AVMEDIA_TYPE_VIDEO,
  366. .config_props = config_input,
  367. .get_video_buffer = get_video_buffer,
  368. .filter_frame = filter_frame,
  369. },
  370. { NULL }
  371. };
  372. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  373. {
  374. .name = "default",
  375. .type = AVMEDIA_TYPE_VIDEO,
  376. .config_props = config_output,
  377. },
  378. { NULL }
  379. };
  380. AVFilter ff_vf_pad = {
  381. .name = "pad",
  382. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  383. .priv_size = sizeof(PadContext),
  384. .priv_class = &pad_class,
  385. .init = init,
  386. .uninit = uninit,
  387. .query_formats = query_formats,
  388. .inputs = avfilter_vf_pad_inputs,
  389. .outputs = avfilter_vf_pad_outputs,
  390. };