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.

411 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/eval.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/colorspace.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/mathematics.h"
  37. #include "drawutils.h"
  38. static const char *const var_names[] = {
  39. "in_w", "iw",
  40. "in_h", "ih",
  41. "out_w", "ow",
  42. "out_h", "oh",
  43. "x",
  44. "y",
  45. "a",
  46. "sar",
  47. "dar",
  48. "hsub",
  49. "vsub",
  50. NULL
  51. };
  52. enum var_name {
  53. VAR_IN_W, VAR_IW,
  54. VAR_IN_H, VAR_IH,
  55. VAR_OUT_W, VAR_OW,
  56. VAR_OUT_H, VAR_OH,
  57. VAR_X,
  58. VAR_Y,
  59. VAR_A,
  60. VAR_SAR,
  61. VAR_DAR,
  62. VAR_HSUB,
  63. VAR_VSUB,
  64. VARS_NB
  65. };
  66. static int query_formats(AVFilterContext *ctx)
  67. {
  68. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  69. return 0;
  70. }
  71. typedef struct {
  72. int w, h; ///< output dimensions, a value of 0 will result in the input size
  73. int x, y; ///< offsets of the input area with respect to the padded area
  74. 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
  75. char w_expr[256]; ///< width expression string
  76. char h_expr[256]; ///< height expression string
  77. char x_expr[256]; ///< width expression string
  78. char y_expr[256]; ///< height expression string
  79. uint8_t rgba_color[4]; ///< color for the padding area
  80. FFDrawContext draw;
  81. FFDrawColor color;
  82. int needs_copy;
  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] = (float) inlink->w / inlink->h;
  113. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  114. (float) 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 start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  225. {
  226. PadContext *pad = inlink->dst->priv;
  227. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  228. AVFilterBufferRef *for_next_filter;
  229. int plane, ret = 0;
  230. if (!outpicref)
  231. return AVERROR(ENOMEM);
  232. for (plane = 0; plane < 4 && outpicref->data[plane] && pad->draw.pixelstep[plane]; plane++) {
  233. int hsub = pad->draw.hsub[plane];
  234. int vsub = pad->draw.vsub[plane];
  235. av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
  236. if(outpicref->format != outpicref->buf->format) //unsupported currently
  237. break;
  238. outpicref->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane]
  239. + (pad->y >> vsub) * outpicref->linesize[plane];
  240. if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
  241. || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
  242. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
  243. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
  244. )
  245. break;
  246. }
  247. pad->needs_copy= plane < 4 && outpicref->data[plane] || !(outpicref->perms & AV_PERM_WRITE);
  248. if(pad->needs_copy){
  249. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  250. avfilter_unref_buffer(outpicref);
  251. outpicref = 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 (!outpicref)
  255. return AVERROR(ENOMEM);
  256. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  257. }
  258. outpicref->video->w = pad->w;
  259. outpicref->video->h = pad->h;
  260. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  261. if (!for_next_filter) {
  262. ret = AVERROR(ENOMEM);
  263. goto fail;
  264. }
  265. ret = ff_start_frame(inlink->dst->outputs[0], for_next_filter);
  266. if (ret < 0)
  267. goto fail;
  268. inlink->dst->outputs[0]->out_buf = outpicref;
  269. return 0;
  270. fail:
  271. avfilter_unref_bufferp(&outpicref);
  272. return ret;
  273. }
  274. static int draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  275. {
  276. PadContext *pad = link->dst->priv;
  277. int bar_y, bar_h = 0, ret = 0;
  278. if (slice_dir * before_slice == 1 && y == pad->y) {
  279. /* top bar */
  280. bar_y = 0;
  281. bar_h = pad->y;
  282. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  283. /* bottom bar */
  284. bar_y = pad->y + pad->in_h;
  285. bar_h = pad->h - pad->in_h - pad->y;
  286. }
  287. if (bar_h) {
  288. ff_fill_rectangle(&pad->draw, &pad->color,
  289. link->dst->outputs[0]->out_buf->data,
  290. link->dst->outputs[0]->out_buf->linesize,
  291. 0, bar_y, pad->w, bar_h);
  292. ret = ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  293. }
  294. return ret;
  295. }
  296. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  297. {
  298. PadContext *pad = link->dst->priv;
  299. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  300. AVFilterBufferRef *inpic = link->cur_buf;
  301. int ret;
  302. y += pad->y;
  303. y = ff_draw_round_to_sub(&pad->draw, 1, -1, y);
  304. h = ff_draw_round_to_sub(&pad->draw, 1, -1, h);
  305. if (!h)
  306. return 0;
  307. draw_send_bar_slice(link, y, h, slice_dir, 1);
  308. /* left border */
  309. ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
  310. 0, y, pad->x, h);
  311. if(pad->needs_copy){
  312. ff_copy_rectangle2(&pad->draw,
  313. outpic->data, outpic->linesize,
  314. inpic ->data, inpic ->linesize,
  315. pad->x, y, 0, y - pad->y, inpic->video->w, h);
  316. }
  317. /* right border */
  318. ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
  319. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  320. ret = ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  321. if (ret < 0)
  322. return ret;
  323. return draw_send_bar_slice(link, y, h, slice_dir, -1);
  324. }
  325. AVFilter avfilter_vf_pad = {
  326. .name = "pad",
  327. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  328. .priv_size = sizeof(PadContext),
  329. .init = init,
  330. .query_formats = query_formats,
  331. .inputs = (const AVFilterPad[]) {{ .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .config_props = config_input,
  334. .get_video_buffer = get_video_buffer,
  335. .start_frame = start_frame,
  336. .draw_slice = draw_slice, },
  337. { .name = NULL}},
  338. .outputs = (const AVFilterPad[]) {{ .name = "default",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .config_props = config_output, },
  341. { .name = NULL}},
  342. };