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.

464 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. av_expr_parse_and_eval(&res, (expr = s->w_expr),
  141. var_names, var_values,
  142. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  143. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  144. if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  145. var_names, var_values,
  146. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  147. goto eval_fail;
  148. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  149. if (!s->h)
  150. var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
  151. /* evaluate the width again, as it may depend on the evaluated output height */
  152. if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  153. var_names, var_values,
  154. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  155. goto eval_fail;
  156. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  157. if (!s->w)
  158. var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
  159. /* evaluate x and y */
  160. av_expr_parse_and_eval(&res, (expr = s->x_expr),
  161. var_names, var_values,
  162. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  163. s->x = var_values[VAR_X] = res;
  164. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  165. var_names, var_values,
  166. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  167. goto eval_fail;
  168. s->y = var_values[VAR_Y] = res;
  169. /* evaluate x again, as it may depend on the evaluated y value */
  170. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  171. var_names, var_values,
  172. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  173. goto eval_fail;
  174. s->x = var_values[VAR_X] = res;
  175. /* sanity check params */
  176. if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
  177. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  178. return AVERROR(EINVAL);
  179. }
  180. s->w &= ~((1 << s->hsub) - 1);
  181. s->h &= ~((1 << s->vsub) - 1);
  182. s->x &= ~((1 << s->hsub) - 1);
  183. s->y &= ~((1 << s->vsub) - 1);
  184. s->in_w = inlink->w & ~((1 << s->hsub) - 1);
  185. s->in_h = inlink->h & ~((1 << s->vsub) - 1);
  186. memcpy(rgba_color, s->color, sizeof(rgba_color));
  187. ff_fill_line_with_color(s->line, s->line_step, s->w, s->color,
  188. inlink->format, rgba_color, &is_packed_rgba, NULL);
  189. 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",
  190. inlink->w, inlink->h, s->w, s->h, s->x, s->y,
  191. s->color[0], s->color[1], s->color[2], s->color[3],
  192. is_packed_rgba ? "rgba" : "yuva");
  193. if (s->x < 0 || s->y < 0 ||
  194. s->w <= 0 || s->h <= 0 ||
  195. (unsigned)s->x + (unsigned)inlink->w > s->w ||
  196. (unsigned)s->y + (unsigned)inlink->h > s->h) {
  197. av_log(ctx, AV_LOG_ERROR,
  198. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  199. s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
  200. return AVERROR(EINVAL);
  201. }
  202. return 0;
  203. eval_fail:
  204. av_log(NULL, AV_LOG_ERROR,
  205. "Error when evaluating the expression '%s'\n", expr);
  206. return ret;
  207. }
  208. static int config_output(AVFilterLink *outlink)
  209. {
  210. PadContext *s = outlink->src->priv;
  211. outlink->w = s->w;
  212. outlink->h = s->h;
  213. return 0;
  214. }
  215. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  216. {
  217. PadContext *s = inlink->dst->priv;
  218. AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
  219. w + (s->w - s->in_w),
  220. h + (s->h - s->in_h));
  221. int plane;
  222. if (!frame)
  223. return NULL;
  224. frame->width = w;
  225. frame->height = h;
  226. for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
  227. int hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
  228. int vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
  229. frame->data[plane] += (s->x >> hsub) * s->line_step[plane] +
  230. (s->y >> vsub) * frame->linesize[plane];
  231. }
  232. return frame;
  233. }
  234. /* check whether each plane in this buffer can be padded without copying */
  235. static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
  236. {
  237. int planes[4] = { -1, -1, -1, -1}, *p = planes;
  238. int i, j;
  239. /* get all planes in this buffer */
  240. for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
  241. if (av_frame_get_plane_buffer(frame, i) == buf)
  242. *p++ = i;
  243. }
  244. /* for each plane in this buffer, check that it can be padded without
  245. * going over buffer bounds or other planes */
  246. for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
  247. int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
  248. int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
  249. uint8_t *start = frame->data[planes[i]];
  250. uint8_t *end = start + (frame->height >> hsub) *
  251. frame->linesize[planes[i]];
  252. /* amount of free space needed before the start and after the end
  253. * of the plane */
  254. ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
  255. (s->y >> vsub) * frame->linesize[planes[i]];
  256. ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
  257. s->line_step[planes[i]] +
  258. (s->y >> vsub) * frame->linesize[planes[i]];
  259. if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
  260. return 1;
  261. if (start - buf->data < req_start ||
  262. (buf->data + buf->size) - end < req_end)
  263. return 1;
  264. #define SIGN(x) ((x) > 0 ? 1 : -1)
  265. for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  266. int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
  267. uint8_t *start1 = frame->data[planes[j]];
  268. uint8_t *end1 = start1 + (frame->height >> hsub1) *
  269. frame->linesize[planes[j]];
  270. if (i == j)
  271. continue;
  272. if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
  273. SIGN(end - start1) != SIGN(end - start1 + req_end))
  274. return 1;
  275. }
  276. }
  277. return 0;
  278. }
  279. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  280. {
  281. int i;
  282. if (!av_frame_is_writable(frame))
  283. return 1;
  284. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  285. if (buffer_needs_copy(s, frame, frame->buf[i]))
  286. return 1;
  287. return 0;
  288. }
  289. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  290. {
  291. PadContext *s = inlink->dst->priv;
  292. AVFrame *out;
  293. int needs_copy = frame_needs_copy(s, in);
  294. if (needs_copy) {
  295. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  296. out = ff_get_video_buffer(inlink->dst->outputs[0],
  297. FFMAX(inlink->w, s->w),
  298. FFMAX(inlink->h, s->h));
  299. if (!out) {
  300. av_frame_free(&in);
  301. return AVERROR(ENOMEM);
  302. }
  303. av_frame_copy_props(out, in);
  304. } else {
  305. int i;
  306. out = in;
  307. for (i = 0; i < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
  308. int hsub = (i == 1 || i == 2) ? s->hsub : 0;
  309. int vsub = (i == 1 || i == 2) ? s->vsub : 0;
  310. out->data[i] -= (s->x >> hsub) * s->line_step[i] +
  311. (s->y >> vsub) * out->linesize[i];
  312. }
  313. }
  314. /* top bar */
  315. if (s->y) {
  316. ff_draw_rectangle(out->data, out->linesize,
  317. s->line, s->line_step, s->hsub, s->vsub,
  318. 0, 0, s->w, s->y);
  319. }
  320. /* bottom bar */
  321. if (s->h > s->y + s->in_h) {
  322. ff_draw_rectangle(out->data, out->linesize,
  323. s->line, s->line_step, s->hsub, s->vsub,
  324. 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  325. }
  326. /* left border */
  327. ff_draw_rectangle(out->data, out->linesize, s->line, s->line_step,
  328. s->hsub, s->vsub, 0, s->y, s->x, in->height);
  329. if (needs_copy) {
  330. ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
  331. s->line_step, s->hsub, s->vsub,
  332. s->x, s->y, 0, in->width, in->height);
  333. }
  334. /* right border */
  335. ff_draw_rectangle(out->data, out->linesize,
  336. s->line, s->line_step, s->hsub, s->vsub,
  337. s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  338. in->height);
  339. out->width = s->w;
  340. out->height = s->h;
  341. if (in != out)
  342. av_frame_free(&in);
  343. return ff_filter_frame(inlink->dst->outputs[0], out);
  344. }
  345. #define OFFSET(x) offsetof(PadContext, x)
  346. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  347. static const AVOption options[] = {
  348. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
  349. { "height", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
  350. { "x", "Horizontal position of the left edge of the input video in the "
  351. "output video", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  352. { "y", "Vertical position of the top edge of the input video in the "
  353. "output video", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  354. { "color", "Color of the padded area", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, .flags = FLAGS },
  355. { NULL },
  356. };
  357. static const AVClass pad_class = {
  358. .class_name = "pad",
  359. .item_name = av_default_item_name,
  360. .option = options,
  361. .version = LIBAVUTIL_VERSION_INT,
  362. };
  363. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  364. {
  365. .name = "default",
  366. .type = AVMEDIA_TYPE_VIDEO,
  367. .config_props = config_input,
  368. .get_video_buffer = get_video_buffer,
  369. .filter_frame = filter_frame,
  370. },
  371. { NULL }
  372. };
  373. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  374. {
  375. .name = "default",
  376. .type = AVMEDIA_TYPE_VIDEO,
  377. .config_props = config_output,
  378. },
  379. { NULL }
  380. };
  381. AVFilter ff_vf_pad = {
  382. .name = "pad",
  383. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  384. .priv_size = sizeof(PadContext),
  385. .priv_class = &pad_class,
  386. .init = init,
  387. .uninit = uninit,
  388. .query_formats = query_formats,
  389. .inputs = avfilter_vf_pad_inputs,
  390. .outputs = avfilter_vf_pad_outputs,
  391. };