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.

466 lines
15KB

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