vf_pad.c 14 KB

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