vf_pad.c 13 KB

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