vf_pad.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <float.h> /* DBL_MAX */
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/common.h"
  32. #include "libavutil/eval.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/colorspace.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. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  71. }
  72. enum EvalMode {
  73. EVAL_MODE_INIT,
  74. EVAL_MODE_FRAME,
  75. EVAL_MODE_NB
  76. };
  77. typedef struct PadContext {
  78. const AVClass *class;
  79. int w, h; ///< output dimensions, a value of 0 will result in the input size
  80. int x, y; ///< offsets of the input area with respect to the padded area
  81. 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
  82. int inlink_w, inlink_h;
  83. AVRational aspect;
  84. char *w_expr; ///< width expression string
  85. char *h_expr; ///< height expression string
  86. char *x_expr; ///< width expression string
  87. char *y_expr; ///< height expression string
  88. uint8_t rgba_color[4]; ///< color for the padding area
  89. FFDrawContext draw;
  90. FFDrawColor color;
  91. int eval_mode; ///< expression evaluation mode
  92. } PadContext;
  93. static int config_input(AVFilterLink *inlink)
  94. {
  95. AVFilterContext *ctx = inlink->dst;
  96. PadContext *s = ctx->priv;
  97. AVRational adjusted_aspect = s->aspect;
  98. int ret;
  99. double var_values[VARS_NB], res;
  100. char *expr;
  101. ff_draw_init(&s->draw, inlink->format, 0);
  102. ff_draw_color(&s->draw, &s->color, s->rgba_color);
  103. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  104. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  105. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  106. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  107. var_values[VAR_A] = (double) inlink->w / inlink->h;
  108. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  109. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  110. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  111. var_values[VAR_HSUB] = 1 << s->draw.hsub_max;
  112. var_values[VAR_VSUB] = 1 << s->draw.vsub_max;
  113. /* evaluate width and height */
  114. av_expr_parse_and_eval(&res, (expr = s->w_expr),
  115. var_names, var_values,
  116. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  117. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  118. if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  119. var_names, var_values,
  120. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  121. goto eval_fail;
  122. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  123. if (!s->h)
  124. var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
  125. /* evaluate the width again, as it may depend on the evaluated output height */
  126. if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  127. var_names, var_values,
  128. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  129. goto eval_fail;
  130. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  131. if (!s->w)
  132. var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
  133. if (adjusted_aspect.num && adjusted_aspect.den) {
  134. adjusted_aspect = av_div_q(adjusted_aspect, inlink->sample_aspect_ratio);
  135. if (s->h < av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num)) {
  136. s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num);
  137. } else {
  138. s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = av_rescale(s->h, adjusted_aspect.num, adjusted_aspect.den);
  139. }
  140. }
  141. /* evaluate x and y */
  142. av_expr_parse_and_eval(&res, (expr = s->x_expr),
  143. var_names, var_values,
  144. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  145. s->x = var_values[VAR_X] = res;
  146. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  147. var_names, var_values,
  148. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  149. goto eval_fail;
  150. s->y = var_values[VAR_Y] = res;
  151. /* evaluate x again, as it may depend on the evaluated y value */
  152. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  153. var_names, var_values,
  154. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  155. goto eval_fail;
  156. s->x = var_values[VAR_X] = res;
  157. if (s->x < 0 || s->x + inlink->w > s->w)
  158. s->x = var_values[VAR_X] = (s->w - inlink->w) / 2;
  159. if (s->y < 0 || s->y + inlink->h > s->h)
  160. s->y = var_values[VAR_Y] = (s->h - inlink->h) / 2;
  161. /* sanity check params */
  162. if (s->w < 0 || s->h < 0) {
  163. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  164. return AVERROR(EINVAL);
  165. }
  166. s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
  167. s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
  168. s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
  169. s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
  170. s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
  171. s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
  172. s->inlink_w = inlink->w;
  173. s->inlink_h = inlink->h;
  174. 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",
  175. inlink->w, inlink->h, s->w, s->h, s->x, s->y,
  176. s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
  177. if (s->x < 0 || s->y < 0 ||
  178. s->w <= 0 || s->h <= 0 ||
  179. (unsigned)s->x + (unsigned)inlink->w > s->w ||
  180. (unsigned)s->y + (unsigned)inlink->h > s->h) {
  181. av_log(ctx, AV_LOG_ERROR,
  182. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  183. s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
  184. return AVERROR(EINVAL);
  185. }
  186. return 0;
  187. eval_fail:
  188. av_log(NULL, AV_LOG_ERROR,
  189. "Error when evaluating the expression '%s'\n", expr);
  190. return ret;
  191. }
  192. static int config_output(AVFilterLink *outlink)
  193. {
  194. PadContext *s = outlink->src->priv;
  195. outlink->w = s->w;
  196. outlink->h = s->h;
  197. return 0;
  198. }
  199. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  200. {
  201. PadContext *s = inlink->dst->priv;
  202. AVFrame *frame;
  203. int plane;
  204. if (s->inlink_w <= 0)
  205. return NULL;
  206. frame = ff_get_video_buffer(inlink->dst->outputs[0],
  207. w + (s->w - s->in_w),
  208. h + (s->h - s->in_h) + (s->x > 0));
  209. if (!frame)
  210. return NULL;
  211. frame->width = w;
  212. frame->height = h;
  213. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  214. int hsub = s->draw.hsub[plane];
  215. int vsub = s->draw.vsub[plane];
  216. frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
  217. (s->y >> vsub) * frame->linesize[plane];
  218. }
  219. return frame;
  220. }
  221. /* check whether each plane in this buffer can be padded without copying */
  222. static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
  223. {
  224. int planes[4] = { -1, -1, -1, -1}, *p = planes;
  225. int i, j;
  226. /* get all planes in this buffer */
  227. for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
  228. if (av_frame_get_plane_buffer(frame, i) == buf)
  229. *p++ = i;
  230. }
  231. /* for each plane in this buffer, check that it can be padded without
  232. * going over buffer bounds or other planes */
  233. for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
  234. int hsub = s->draw.hsub[planes[i]];
  235. int vsub = s->draw.vsub[planes[i]];
  236. uint8_t *start = frame->data[planes[i]];
  237. uint8_t *end = start + (frame->height >> vsub) *
  238. frame->linesize[planes[i]];
  239. /* amount of free space needed before the start and after the end
  240. * of the plane */
  241. ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
  242. (s->y >> vsub) * frame->linesize[planes[i]];
  243. ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
  244. s->draw.pixelstep[planes[i]] +
  245. ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]];
  246. if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
  247. return 1;
  248. if (start - buf->data < req_start ||
  249. (buf->data + buf->size) - end < req_end)
  250. return 1;
  251. for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  252. int vsub1 = s->draw.vsub[planes[j]];
  253. uint8_t *start1 = frame->data[planes[j]];
  254. uint8_t *end1 = start1 + (frame->height >> vsub1) *
  255. frame->linesize[planes[j]];
  256. if (i == j)
  257. continue;
  258. if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) ||
  259. FFSIGN(end - start1) != FFSIGN(end - start1 + req_end))
  260. return 1;
  261. }
  262. }
  263. return 0;
  264. }
  265. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  266. {
  267. int i;
  268. if (!av_frame_is_writable(frame))
  269. return 1;
  270. for (i = 0; i < 4 && frame->buf[i]; i++)
  271. if (buffer_needs_copy(s, frame, frame->buf[i]))
  272. return 1;
  273. return 0;
  274. }
  275. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  276. {
  277. PadContext *s = inlink->dst->priv;
  278. AVFilterLink *outlink = inlink->dst->outputs[0];
  279. AVFrame *out;
  280. int needs_copy;
  281. if(s->eval_mode == EVAL_MODE_FRAME && (
  282. in->width != s->inlink_w
  283. || in->height != s->inlink_h
  284. || in->format != outlink->format
  285. || in->sample_aspect_ratio.den != outlink->sample_aspect_ratio.den || in->sample_aspect_ratio.num != outlink->sample_aspect_ratio.num)) {
  286. int ret;
  287. inlink->dst->inputs[0]->format = in->format;
  288. inlink->dst->inputs[0]->w = in->width;
  289. inlink->dst->inputs[0]->h = in->height;
  290. inlink->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
  291. inlink->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
  292. if ((ret = config_input(inlink)) < 0) {
  293. s->inlink_w = -1;
  294. return ret;
  295. }
  296. if ((ret = config_output(outlink)) < 0) {
  297. s->inlink_w = -1;
  298. return ret;
  299. }
  300. }
  301. needs_copy = frame_needs_copy(s, in);
  302. if (needs_copy) {
  303. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  304. out = ff_get_video_buffer(inlink->dst->outputs[0],
  305. FFMAX(inlink->w, s->w),
  306. FFMAX(inlink->h, s->h));
  307. if (!out) {
  308. av_frame_free(&in);
  309. return AVERROR(ENOMEM);
  310. }
  311. av_frame_copy_props(out, in);
  312. } else {
  313. int i;
  314. out = in;
  315. for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) {
  316. int hsub = s->draw.hsub[i];
  317. int vsub = s->draw.vsub[i];
  318. out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
  319. (s->y >> vsub) * out->linesize[i];
  320. }
  321. }
  322. /* top bar */
  323. if (s->y) {
  324. ff_fill_rectangle(&s->draw, &s->color,
  325. out->data, out->linesize,
  326. 0, 0, s->w, s->y);
  327. }
  328. /* bottom bar */
  329. if (s->h > s->y + s->in_h) {
  330. ff_fill_rectangle(&s->draw, &s->color,
  331. out->data, out->linesize,
  332. 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  333. }
  334. /* left border */
  335. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  336. 0, s->y, s->x, in->height);
  337. if (needs_copy) {
  338. ff_copy_rectangle2(&s->draw,
  339. out->data, out->linesize, in->data, in->linesize,
  340. s->x, s->y, 0, 0, in->width, in->height);
  341. }
  342. /* right border */
  343. ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  344. s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  345. in->height);
  346. out->width = s->w;
  347. out->height = s->h;
  348. if (in != out)
  349. av_frame_free(&in);
  350. return ff_filter_frame(inlink->dst->outputs[0], out);
  351. }
  352. #define OFFSET(x) offsetof(PadContext, x)
  353. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  354. static const AVOption pad_options[] = {
  355. { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  356. { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  357. { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  358. { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  359. { "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 },
  360. { "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 },
  361. { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  362. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  363. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  364. { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  365. { "aspect", "pad to fit an aspect instead of a resolution", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, DBL_MAX, FLAGS },
  366. { NULL }
  367. };
  368. AVFILTER_DEFINE_CLASS(pad);
  369. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  370. {
  371. .name = "default",
  372. .type = AVMEDIA_TYPE_VIDEO,
  373. .config_props = config_input,
  374. .get_video_buffer = get_video_buffer,
  375. .filter_frame = filter_frame,
  376. },
  377. { NULL }
  378. };
  379. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  380. {
  381. .name = "default",
  382. .type = AVMEDIA_TYPE_VIDEO,
  383. .config_props = config_output,
  384. },
  385. { NULL }
  386. };
  387. AVFilter ff_vf_pad = {
  388. .name = "pad",
  389. .description = NULL_IF_CONFIG_SMALL("Pad the input video."),
  390. .priv_size = sizeof(PadContext),
  391. .priv_class = &pad_class,
  392. .query_formats = query_formats,
  393. .inputs = avfilter_vf_pad_inputs,
  394. .outputs = avfilter_vf_pad_outputs,
  395. };