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.

433 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/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. "PI",
  40. "PHI",
  41. "E",
  42. "in_w", "iw",
  43. "in_h", "ih",
  44. "out_w", "ow",
  45. "out_h", "oh",
  46. "x",
  47. "y",
  48. "a",
  49. "hsub",
  50. "vsub",
  51. NULL
  52. };
  53. enum var_name {
  54. VAR_PI,
  55. VAR_PHI,
  56. VAR_E,
  57. VAR_IN_W, VAR_IW,
  58. VAR_IN_H, VAR_IH,
  59. VAR_OUT_W, VAR_OW,
  60. VAR_OUT_H, VAR_OH,
  61. VAR_X,
  62. VAR_Y,
  63. VAR_A,
  64. VAR_HSUB,
  65. VAR_VSUB,
  66. VARS_NB
  67. };
  68. static int query_formats(AVFilterContext *ctx)
  69. {
  70. static const enum PixelFormat pix_fmts[] = {
  71. PIX_FMT_ARGB, PIX_FMT_RGBA,
  72. PIX_FMT_ABGR, PIX_FMT_BGRA,
  73. PIX_FMT_RGB24, PIX_FMT_BGR24,
  74. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  75. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  76. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  77. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  78. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
  79. PIX_FMT_YUVA420P,
  80. PIX_FMT_NONE
  81. };
  82. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  83. return 0;
  84. }
  85. typedef struct {
  86. int w, h; ///< output dimensions, a value of 0 will result in the input size
  87. int x, y; ///< offsets of the input area with respect to the padded area
  88. 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
  89. char w_expr[256]; ///< width expression string
  90. char h_expr[256]; ///< height expression string
  91. char x_expr[256]; ///< width expression string
  92. char y_expr[256]; ///< height expression string
  93. uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  94. uint8_t *line[4];
  95. int line_step[4];
  96. int hsub, vsub; ///< chroma subsampling values
  97. int needs_copy;
  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_descriptors[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] = (float) 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. picref->video->w = w;
  228. picref->video->h = h;
  229. for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
  230. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  231. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  232. picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
  233. (pad->y >> vsub) * picref->linesize[plane];
  234. }
  235. return picref;
  236. }
  237. static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
  238. {
  239. int64_t x_in_buf, y_in_buf;
  240. x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
  241. + (x >> hsub) * pad ->line_step[plane]
  242. + (y >> vsub) * outpicref->linesize [plane];
  243. if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
  244. return 1;
  245. x_in_buf /= pad->line_step[plane];
  246. av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
  247. y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
  248. x_in_buf %= outpicref->buf->linesize[plane];
  249. if( y_in_buf<<vsub >= outpicref->buf->h
  250. || x_in_buf<<hsub >= outpicref->buf->w)
  251. return 1;
  252. return 0;
  253. }
  254. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  255. {
  256. PadContext *pad = inlink->dst->priv;
  257. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  258. int plane;
  259. for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
  260. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  261. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  262. av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
  263. if(outpicref->format != outpicref->buf->format) //unsupported currently
  264. break;
  265. outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
  266. + (pad->y >> vsub) * outpicref->linesize [plane];
  267. if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
  268. || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
  269. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
  270. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
  271. )
  272. break;
  273. }
  274. pad->needs_copy= plane < 4 && outpicref->data[plane];
  275. if(pad->needs_copy){
  276. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  277. avfilter_unref_buffer(outpicref);
  278. outpicref = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
  279. FFMAX(inlink->w, pad->w),
  280. FFMAX(inlink->h, pad->h));
  281. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  282. }
  283. inlink->dst->outputs[0]->out_buf = outpicref;
  284. outpicref->video->w = pad->w;
  285. outpicref->video->h = pad->h;
  286. ff_start_frame(inlink->dst->outputs[0], outpicref);
  287. }
  288. static void end_frame(AVFilterLink *link)
  289. {
  290. ff_end_frame(link->dst->outputs[0]);
  291. avfilter_unref_buffer(link->cur_buf);
  292. }
  293. static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  294. {
  295. PadContext *pad = link->dst->priv;
  296. int bar_y, bar_h = 0;
  297. if (slice_dir * before_slice == 1 && y == pad->y) {
  298. /* top bar */
  299. bar_y = 0;
  300. bar_h = pad->y;
  301. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  302. /* bottom bar */
  303. bar_y = pad->y + pad->in_h;
  304. bar_h = pad->h - pad->in_h - pad->y;
  305. }
  306. if (bar_h) {
  307. ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
  308. link->dst->outputs[0]->out_buf->linesize,
  309. pad->line, pad->line_step, pad->hsub, pad->vsub,
  310. 0, bar_y, pad->w, bar_h);
  311. ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  312. }
  313. }
  314. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  315. {
  316. PadContext *pad = link->dst->priv;
  317. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  318. AVFilterBufferRef *inpic = link->cur_buf;
  319. y += pad->y;
  320. y &= ~((1 << pad->vsub) - 1);
  321. h &= ~((1 << pad->vsub) - 1);
  322. if (!h)
  323. return;
  324. draw_send_bar_slice(link, y, h, slice_dir, 1);
  325. /* left border */
  326. ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
  327. pad->hsub, pad->vsub, 0, y, pad->x, h);
  328. if(pad->needs_copy){
  329. ff_copy_rectangle(outpic->data, outpic->linesize,
  330. inpic->data, inpic->linesize, pad->line_step,
  331. pad->hsub, pad->vsub,
  332. pad->x, y, y-pad->y, inpic->video->w, h);
  333. }
  334. /* right border */
  335. ff_draw_rectangle(outpic->data, outpic->linesize,
  336. pad->line, pad->line_step, pad->hsub, pad->vsub,
  337. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  338. ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  339. draw_send_bar_slice(link, y, h, slice_dir, -1);
  340. }
  341. AVFilter avfilter_vf_pad = {
  342. .name = "pad",
  343. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  344. .priv_size = sizeof(PadContext),
  345. .init = init,
  346. .uninit = uninit,
  347. .query_formats = query_formats,
  348. .inputs = (AVFilterPad[]) {{ .name = "default",
  349. .type = AVMEDIA_TYPE_VIDEO,
  350. .config_props = config_input,
  351. .get_video_buffer = get_video_buffer,
  352. .start_frame = start_frame,
  353. .draw_slice = draw_slice,
  354. .end_frame = end_frame, },
  355. { .name = NULL}},
  356. .outputs = (AVFilterPad[]) {{ .name = "default",
  357. .type = AVMEDIA_TYPE_VIDEO,
  358. .config_props = config_output, },
  359. { .name = NULL}},
  360. };