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.

419 lines
14KB

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