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.

472 lines
15KB

  1. /*
  2. * Copyright (c) 2008 vmrsss
  3. * Copyright (c) 2009 Stefano Sabatini
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. "PI",
  41. "PHI",
  42. "E",
  43. "in_w", "iw",
  44. "in_h", "ih",
  45. "out_w", "ow",
  46. "out_h", "oh",
  47. "x",
  48. "y",
  49. "a",
  50. "hsub",
  51. "vsub",
  52. NULL
  53. };
  54. enum var_name {
  55. VAR_PI,
  56. VAR_PHI,
  57. VAR_E,
  58. VAR_IN_W, VAR_IW,
  59. VAR_IN_H, VAR_IH,
  60. VAR_OUT_W, VAR_OW,
  61. VAR_OUT_H, VAR_OH,
  62. VAR_X,
  63. VAR_Y,
  64. VAR_A,
  65. VAR_HSUB,
  66. VAR_VSUB,
  67. VARS_NB
  68. };
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. static const enum AVPixelFormat pix_fmts[] = {
  72. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  73. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  74. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  75. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  76. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  77. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  78. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  79. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  80. AV_PIX_FMT_YUVA420P,
  81. AV_PIX_FMT_NONE
  82. };
  83. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  84. return 0;
  85. }
  86. typedef struct {
  87. int w, h; ///< output dimensions, a value of 0 will result in the input size
  88. int x, y; ///< offsets of the input area with respect to the padded area
  89. 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
  90. char w_expr[256]; ///< width expression string
  91. char h_expr[256]; ///< height expression string
  92. char x_expr[256]; ///< width expression string
  93. char y_expr[256]; ///< height expression string
  94. uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  95. uint8_t *line[4];
  96. int line_step[4];
  97. int hsub, vsub; ///< chroma subsampling values
  98. int needs_copy;
  99. } PadContext;
  100. static av_cold int init(AVFilterContext *ctx, const char *args)
  101. {
  102. PadContext *pad = ctx->priv;
  103. char color_string[128] = "black";
  104. av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
  105. av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
  106. av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
  107. av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
  108. if (args)
  109. sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
  110. pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
  111. if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
  112. return AVERROR(EINVAL);
  113. return 0;
  114. }
  115. static av_cold void uninit(AVFilterContext *ctx)
  116. {
  117. PadContext *pad = ctx->priv;
  118. int i;
  119. for (i = 0; i < 4; i++) {
  120. av_freep(&pad->line[i]);
  121. pad->line_step[i] = 0;
  122. }
  123. }
  124. static int config_input(AVFilterLink *inlink)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. PadContext *pad = ctx->priv;
  128. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  129. uint8_t rgba_color[4];
  130. int ret, is_packed_rgba;
  131. double var_values[VARS_NB], res;
  132. char *expr;
  133. pad->hsub = pix_desc->log2_chroma_w;
  134. pad->vsub = pix_desc->log2_chroma_h;
  135. var_values[VAR_PI] = M_PI;
  136. var_values[VAR_PHI] = M_PHI;
  137. var_values[VAR_E] = M_E;
  138. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  139. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  140. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  141. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  142. var_values[VAR_A] = (double) inlink->w / inlink->h;
  143. var_values[VAR_HSUB] = 1<<pad->hsub;
  144. var_values[VAR_VSUB] = 1<<pad->vsub;
  145. /* evaluate width and height */
  146. av_expr_parse_and_eval(&res, (expr = pad->w_expr),
  147. var_names, var_values,
  148. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  149. pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  150. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
  151. var_names, var_values,
  152. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  153. goto eval_fail;
  154. pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  155. /* evaluate the width again, as it may depend on the evaluated output height */
  156. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
  157. var_names, var_values,
  158. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  159. goto eval_fail;
  160. pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  161. /* evaluate x and y */
  162. av_expr_parse_and_eval(&res, (expr = pad->x_expr),
  163. var_names, var_values,
  164. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  165. pad->x = var_values[VAR_X] = res;
  166. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
  167. var_names, var_values,
  168. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  169. goto eval_fail;
  170. pad->y = var_values[VAR_Y] = res;
  171. /* evaluate x again, as it may depend on the evaluated y value */
  172. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
  173. var_names, var_values,
  174. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  175. goto eval_fail;
  176. pad->x = var_values[VAR_X] = res;
  177. /* sanity check params */
  178. if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
  179. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  180. return AVERROR(EINVAL);
  181. }
  182. if (!pad->w)
  183. pad->w = inlink->w;
  184. if (!pad->h)
  185. pad->h = inlink->h;
  186. pad->w &= ~((1 << pad->hsub) - 1);
  187. pad->h &= ~((1 << pad->vsub) - 1);
  188. pad->x &= ~((1 << pad->hsub) - 1);
  189. pad->y &= ~((1 << pad->vsub) - 1);
  190. pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
  191. pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
  192. memcpy(rgba_color, pad->color, sizeof(rgba_color));
  193. ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
  194. inlink->format, rgba_color, &is_packed_rgba, NULL);
  195. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
  196. inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
  197. pad->color[0], pad->color[1], pad->color[2], pad->color[3],
  198. is_packed_rgba ? "rgba" : "yuva");
  199. if (pad->x < 0 || pad->y < 0 ||
  200. pad->w <= 0 || pad->h <= 0 ||
  201. (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
  202. (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
  203. av_log(ctx, AV_LOG_ERROR,
  204. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  205. pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
  206. return AVERROR(EINVAL);
  207. }
  208. return 0;
  209. eval_fail:
  210. av_log(NULL, AV_LOG_ERROR,
  211. "Error when evaluating the expression '%s'\n", expr);
  212. return ret;
  213. }
  214. static int config_output(AVFilterLink *outlink)
  215. {
  216. PadContext *pad = outlink->src->priv;
  217. outlink->w = pad->w;
  218. outlink->h = pad->h;
  219. return 0;
  220. }
  221. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  222. {
  223. PadContext *pad = inlink->dst->priv;
  224. AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
  225. w + (pad->w - pad->in_w),
  226. h + (pad->h - pad->in_h));
  227. int plane;
  228. if (!picref)
  229. return NULL;
  230. picref->video->w = w;
  231. picref->video->h = h;
  232. for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
  233. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  234. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  235. picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
  236. (pad->y >> vsub) * picref->linesize[plane];
  237. }
  238. return picref;
  239. }
  240. static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
  241. {
  242. int64_t x_in_buf, y_in_buf;
  243. x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
  244. + (x >> hsub) * pad ->line_step[plane]
  245. + (y >> vsub) * outpicref->linesize [plane];
  246. if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
  247. return 1;
  248. x_in_buf /= pad->line_step[plane];
  249. av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
  250. y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
  251. x_in_buf %= outpicref->buf->linesize[plane];
  252. if( y_in_buf<<vsub >= outpicref->buf->h
  253. || x_in_buf<<hsub >= outpicref->buf->w)
  254. return 1;
  255. return 0;
  256. }
  257. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  258. {
  259. PadContext *pad = inlink->dst->priv;
  260. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  261. AVFilterBufferRef *for_next_filter;
  262. int plane, ret = 0;
  263. if (!outpicref)
  264. return AVERROR(ENOMEM);
  265. for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
  266. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  267. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  268. av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
  269. if(outpicref->format != outpicref->buf->format) //unsupported currently
  270. break;
  271. outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
  272. + (pad->y >> vsub) * outpicref->linesize [plane];
  273. if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
  274. || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
  275. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
  276. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
  277. )
  278. break;
  279. }
  280. pad->needs_copy= plane < 4 && outpicref->data[plane];
  281. if(pad->needs_copy){
  282. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  283. avfilter_unref_buffer(outpicref);
  284. outpicref = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
  285. FFMAX(inlink->w, pad->w),
  286. FFMAX(inlink->h, pad->h));
  287. if (!outpicref)
  288. return AVERROR(ENOMEM);
  289. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  290. }
  291. outpicref->video->w = pad->w;
  292. outpicref->video->h = pad->h;
  293. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  294. if (!for_next_filter) {
  295. ret = AVERROR(ENOMEM);
  296. goto fail;
  297. }
  298. ret = ff_start_frame(inlink->dst->outputs[0], for_next_filter);
  299. if (ret < 0)
  300. goto fail;
  301. inlink->dst->outputs[0]->out_buf = outpicref;
  302. return 0;
  303. fail:
  304. avfilter_unref_bufferp(&outpicref);
  305. return ret;
  306. }
  307. static int end_frame(AVFilterLink *link)
  308. {
  309. return ff_end_frame(link->dst->outputs[0]);
  310. }
  311. static int draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  312. {
  313. PadContext *pad = link->dst->priv;
  314. int bar_y, bar_h = 0, ret = 0;
  315. if (slice_dir * before_slice == 1 && y == pad->y) {
  316. /* top bar */
  317. bar_y = 0;
  318. bar_h = pad->y;
  319. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  320. /* bottom bar */
  321. bar_y = pad->y + pad->in_h;
  322. bar_h = pad->h - pad->in_h - pad->y;
  323. }
  324. if (bar_h) {
  325. ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
  326. link->dst->outputs[0]->out_buf->linesize,
  327. pad->line, pad->line_step, pad->hsub, pad->vsub,
  328. 0, bar_y, pad->w, bar_h);
  329. ret = ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  330. }
  331. return ret;
  332. }
  333. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  334. {
  335. PadContext *pad = link->dst->priv;
  336. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  337. AVFilterBufferRef *inpic = link->cur_buf;
  338. int ret;
  339. y += pad->y;
  340. y &= ~((1 << pad->vsub) - 1);
  341. h &= ~((1 << pad->vsub) - 1);
  342. if (!h)
  343. return 0;
  344. draw_send_bar_slice(link, y, h, slice_dir, 1);
  345. /* left border */
  346. ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
  347. pad->hsub, pad->vsub, 0, y, pad->x, h);
  348. if(pad->needs_copy){
  349. ff_copy_rectangle(outpic->data, outpic->linesize,
  350. inpic->data, inpic->linesize, pad->line_step,
  351. pad->hsub, pad->vsub,
  352. pad->x, y, y-pad->y, inpic->video->w, h);
  353. }
  354. /* right border */
  355. ff_draw_rectangle(outpic->data, outpic->linesize,
  356. pad->line, pad->line_step, pad->hsub, pad->vsub,
  357. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  358. ret = ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  359. if (ret < 0)
  360. return ret;
  361. return draw_send_bar_slice(link, y, h, slice_dir, -1);
  362. }
  363. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  364. {
  365. .name = "default",
  366. .type = AVMEDIA_TYPE_VIDEO,
  367. .config_props = config_input,
  368. .get_video_buffer = get_video_buffer,
  369. .start_frame = start_frame,
  370. .draw_slice = draw_slice,
  371. .end_frame = end_frame,
  372. },
  373. { NULL }
  374. };
  375. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  376. {
  377. .name = "default",
  378. .type = AVMEDIA_TYPE_VIDEO,
  379. .config_props = config_output,
  380. },
  381. { NULL }
  382. };
  383. AVFilter avfilter_vf_pad = {
  384. .name = "pad",
  385. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  386. .priv_size = sizeof(PadContext),
  387. .init = init,
  388. .uninit = uninit,
  389. .query_formats = query_formats,
  390. .inputs = avfilter_vf_pad_inputs,
  391. .outputs = avfilter_vf_pad_outputs,
  392. };