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.

402 lines
14KB

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