vf_pad.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. int needs_copy;
  84. } PadContext;
  85. static av_cold int init(AVFilterContext *ctx, const char *args)
  86. {
  87. PadContext *pad = ctx->priv;
  88. char color_string[128] = "black";
  89. av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
  90. av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
  91. av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
  92. av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
  93. if (args)
  94. sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
  95. pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
  96. if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0)
  97. return AVERROR(EINVAL);
  98. return 0;
  99. }
  100. static int config_input(AVFilterLink *inlink)
  101. {
  102. AVFilterContext *ctx = inlink->dst;
  103. PadContext *pad = ctx->priv;
  104. int ret;
  105. double var_values[VARS_NB], res;
  106. char *expr;
  107. ff_draw_init(&pad->draw, inlink->format, 0);
  108. ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
  109. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  110. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  111. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  112. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  113. var_values[VAR_A] = (float) inlink->w / inlink->h;
  114. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  115. (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  116. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  117. var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
  118. var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
  119. /* evaluate width and height */
  120. av_expr_parse_and_eval(&res, (expr = pad->w_expr),
  121. var_names, var_values,
  122. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  123. pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  124. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
  125. var_names, var_values,
  126. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  127. goto eval_fail;
  128. pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  129. /* evaluate the width again, as it may depend on the evaluated output height */
  130. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
  131. var_names, var_values,
  132. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  133. goto eval_fail;
  134. pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  135. /* evaluate x and y */
  136. av_expr_parse_and_eval(&res, (expr = pad->x_expr),
  137. var_names, var_values,
  138. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  139. pad->x = var_values[VAR_X] = res;
  140. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
  141. var_names, var_values,
  142. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  143. goto eval_fail;
  144. pad->y = var_values[VAR_Y] = res;
  145. /* evaluate x again, as it may depend on the evaluated y value */
  146. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
  147. var_names, var_values,
  148. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  149. goto eval_fail;
  150. pad->x = var_values[VAR_X] = res;
  151. /* sanity check params */
  152. if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
  153. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  154. return AVERROR(EINVAL);
  155. }
  156. if (!pad->w)
  157. pad->w = inlink->w;
  158. if (!pad->h)
  159. pad->h = inlink->h;
  160. pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
  161. pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
  162. pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
  163. pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
  164. pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
  165. pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
  166. 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",
  167. inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
  168. pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
  169. if (pad->x < 0 || pad->y < 0 ||
  170. pad->w <= 0 || pad->h <= 0 ||
  171. (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
  172. (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
  173. av_log(ctx, AV_LOG_ERROR,
  174. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  175. pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
  176. return AVERROR(EINVAL);
  177. }
  178. return 0;
  179. eval_fail:
  180. av_log(NULL, AV_LOG_ERROR,
  181. "Error when evaluating the expression '%s'\n", expr);
  182. return ret;
  183. }
  184. static int config_output(AVFilterLink *outlink)
  185. {
  186. PadContext *pad = outlink->src->priv;
  187. outlink->w = pad->w;
  188. outlink->h = pad->h;
  189. return 0;
  190. }
  191. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  192. {
  193. PadContext *pad = inlink->dst->priv;
  194. int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
  195. AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
  196. w + (pad->w - pad->in_w) + 4*align,
  197. h + (pad->h - pad->in_h));
  198. int plane;
  199. if (!picref)
  200. return NULL;
  201. picref->video->w = w;
  202. picref->video->h = h;
  203. for (plane = 0; plane < 4 && picref->data[plane]; plane++)
  204. picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
  205. (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane];
  206. return picref;
  207. }
  208. static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
  209. {
  210. int64_t x_in_buf, y_in_buf;
  211. x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
  212. + (x >> hsub) * pad->draw.pixelstep[plane]
  213. + (y >> vsub) * outpicref->linesize[plane];
  214. if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
  215. return 1;
  216. x_in_buf /= pad->draw.pixelstep[plane];
  217. av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
  218. y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
  219. x_in_buf %= outpicref->buf->linesize[plane];
  220. if( y_in_buf<<vsub >= outpicref->buf->h
  221. || x_in_buf<<hsub >= outpicref->buf->w)
  222. return 1;
  223. return 0;
  224. }
  225. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  226. {
  227. PadContext *pad = inlink->dst->priv;
  228. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  229. AVFilterBufferRef *for_next_filter;
  230. int plane, ret = 0;
  231. if (!outpicref)
  232. return AVERROR(ENOMEM);
  233. for (plane = 0; plane < 4 && outpicref->data[plane] && pad->draw.pixelstep[plane]; plane++) {
  234. int hsub = pad->draw.hsub[plane];
  235. int vsub = pad->draw.vsub[plane];
  236. av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
  237. if(outpicref->format != outpicref->buf->format) //unsupported currently
  238. break;
  239. outpicref->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane]
  240. + (pad->y >> vsub) * outpicref->linesize[plane];
  241. if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
  242. || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
  243. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
  244. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
  245. )
  246. break;
  247. }
  248. pad->needs_copy= plane < 4 && outpicref->data[plane] || !(outpicref->perms & AV_PERM_WRITE);
  249. if(pad->needs_copy){
  250. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  251. avfilter_unref_buffer(outpicref);
  252. outpicref = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
  253. FFMAX(inlink->w, pad->w),
  254. FFMAX(inlink->h, pad->h));
  255. if (!outpicref)
  256. return AVERROR(ENOMEM);
  257. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  258. }
  259. outpicref->video->w = pad->w;
  260. outpicref->video->h = pad->h;
  261. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  262. if (!for_next_filter) {
  263. ret = AVERROR(ENOMEM);
  264. goto fail;
  265. }
  266. ret = ff_start_frame(inlink->dst->outputs[0], for_next_filter);
  267. if (ret < 0)
  268. goto fail;
  269. inlink->dst->outputs[0]->out_buf = outpicref;
  270. return 0;
  271. fail:
  272. avfilter_unref_bufferp(&outpicref);
  273. return ret;
  274. }
  275. static int draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  276. {
  277. PadContext *pad = link->dst->priv;
  278. int bar_y, bar_h = 0, ret = 0;
  279. if (slice_dir * before_slice == 1 && y == pad->y) {
  280. /* top bar */
  281. bar_y = 0;
  282. bar_h = pad->y;
  283. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  284. /* bottom bar */
  285. bar_y = pad->y + pad->in_h;
  286. bar_h = pad->h - pad->in_h - pad->y;
  287. }
  288. if (bar_h) {
  289. ff_fill_rectangle(&pad->draw, &pad->color,
  290. link->dst->outputs[0]->out_buf->data,
  291. link->dst->outputs[0]->out_buf->linesize,
  292. 0, bar_y, pad->w, bar_h);
  293. ret = ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  294. }
  295. return ret;
  296. }
  297. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  298. {
  299. PadContext *pad = link->dst->priv;
  300. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  301. AVFilterBufferRef *inpic = link->cur_buf;
  302. int ret;
  303. y += pad->y;
  304. y = ff_draw_round_to_sub(&pad->draw, 1, -1, y);
  305. h = ff_draw_round_to_sub(&pad->draw, 1, -1, h);
  306. if (!h)
  307. return 0;
  308. draw_send_bar_slice(link, y, h, slice_dir, 1);
  309. /* left border */
  310. ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
  311. 0, y, pad->x, h);
  312. if(pad->needs_copy){
  313. ff_copy_rectangle2(&pad->draw,
  314. outpic->data, outpic->linesize,
  315. inpic ->data, inpic ->linesize,
  316. pad->x, y, 0, y - pad->y, inpic->video->w, h);
  317. }
  318. /* right border */
  319. ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
  320. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  321. ret = ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  322. if (ret < 0)
  323. return ret;
  324. return draw_send_bar_slice(link, y, h, slice_dir, -1);
  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. .query_formats = query_formats,
  332. .inputs = (const AVFilterPad[]) {{ .name = "default",
  333. .type = AVMEDIA_TYPE_VIDEO,
  334. .config_props = config_input,
  335. .get_video_buffer = get_video_buffer,
  336. .start_frame = start_frame,
  337. .draw_slice = draw_slice, },
  338. { .name = NULL}},
  339. .outputs = (const AVFilterPad[]) {{ .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. .config_props = config_output, },
  342. { .name = NULL}},
  343. };