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.

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