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.

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