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.

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