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.

434 lines
15KB

  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 "libavutil/avstring.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/colorspace.h"
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/parseutils.h"
  33. #include "libavutil/mathematics.h"
  34. #include "drawutils.h"
  35. static const char *var_names[] = {
  36. "PI",
  37. "PHI",
  38. "E",
  39. "in_w", "iw",
  40. "in_h", "ih",
  41. "out_w", "ow",
  42. "out_h", "oh",
  43. "x",
  44. "y",
  45. "a", "dar",
  46. "sar",
  47. "hsub",
  48. "vsub",
  49. NULL
  50. };
  51. enum var_name {
  52. VAR_PI,
  53. VAR_PHI,
  54. VAR_E,
  55. VAR_IN_W, VAR_IW,
  56. VAR_IN_H, VAR_IH,
  57. VAR_OUT_W, VAR_OW,
  58. VAR_OUT_H, VAR_OH,
  59. VAR_X,
  60. VAR_Y,
  61. VAR_A, VAR_DAR,
  62. VAR_SAR,
  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. avfilter_set_common_pixel_formats(ctx, avfilter_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_DAR] = var_values[VAR_A] = (float) inlink->w / inlink->h;
  141. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  142. (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  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_INFO, "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 = avfilter_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. picref->video->w = w;
  229. picref->video->h = h;
  230. for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
  231. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  232. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  233. picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
  234. (pad->y >> vsub) * picref->linesize[plane];
  235. }
  236. return picref;
  237. }
  238. static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
  239. {
  240. int64_t x_in_buf, y_in_buf;
  241. x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
  242. + (x >> hsub) * pad ->line_step[plane]
  243. + (y >> vsub) * outpicref->linesize [plane];
  244. if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
  245. return 1;
  246. x_in_buf /= pad->line_step[plane];
  247. av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
  248. y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
  249. x_in_buf %= outpicref->buf->linesize[plane];
  250. if( y_in_buf<<vsub >= outpicref->buf->h
  251. || x_in_buf<<hsub >= outpicref->buf->w)
  252. return 1;
  253. return 0;
  254. }
  255. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  256. {
  257. PadContext *pad = inlink->dst->priv;
  258. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  259. int plane;
  260. for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
  261. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  262. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  263. av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
  264. if(outpicref->format != outpicref->buf->format) //unsupported currently
  265. break;
  266. outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
  267. + (pad->y >> vsub) * outpicref->linesize [plane];
  268. if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
  269. || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
  270. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
  271. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
  272. )
  273. break;
  274. }
  275. pad->needs_copy= plane < 4 && outpicref->data[plane];
  276. if(pad->needs_copy){
  277. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  278. avfilter_unref_buffer(outpicref);
  279. outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
  280. FFMAX(inlink->w, pad->w),
  281. FFMAX(inlink->h, pad->h));
  282. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  283. }
  284. inlink->dst->outputs[0]->out_buf = outpicref;
  285. outpicref->video->w = pad->w;
  286. outpicref->video->h = pad->h;
  287. avfilter_start_frame(inlink->dst->outputs[0], outpicref);
  288. }
  289. static void end_frame(AVFilterLink *link)
  290. {
  291. avfilter_end_frame(link->dst->outputs[0]);
  292. avfilter_unref_buffer(link->cur_buf);
  293. }
  294. static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  295. {
  296. PadContext *pad = link->dst->priv;
  297. int bar_y, bar_h = 0;
  298. if (slice_dir * before_slice == 1 && y == pad->y) {
  299. /* top bar */
  300. bar_y = 0;
  301. bar_h = pad->y;
  302. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  303. /* bottom bar */
  304. bar_y = pad->y + pad->in_h;
  305. bar_h = pad->h - pad->in_h - pad->y;
  306. }
  307. if (bar_h) {
  308. ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
  309. link->dst->outputs[0]->out_buf->linesize,
  310. pad->line, pad->line_step, pad->hsub, pad->vsub,
  311. 0, bar_y, pad->w, bar_h);
  312. avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  313. }
  314. }
  315. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  316. {
  317. PadContext *pad = link->dst->priv;
  318. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  319. AVFilterBufferRef *inpic = link->cur_buf;
  320. y += pad->y;
  321. y &= ~((1 << pad->vsub) - 1);
  322. h &= ~((1 << pad->vsub) - 1);
  323. if (!h)
  324. return;
  325. draw_send_bar_slice(link, y, h, slice_dir, 1);
  326. /* left border */
  327. ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
  328. pad->hsub, pad->vsub, 0, y, pad->x, h);
  329. if(pad->needs_copy){
  330. ff_copy_rectangle(outpic->data, outpic->linesize,
  331. inpic->data, inpic->linesize, pad->line_step,
  332. pad->hsub, pad->vsub,
  333. pad->x, y, y-pad->y, inpic->video->w, h);
  334. }
  335. /* right border */
  336. ff_draw_rectangle(outpic->data, outpic->linesize,
  337. pad->line, pad->line_step, pad->hsub, pad->vsub,
  338. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  339. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  340. draw_send_bar_slice(link, y, h, slice_dir, -1);
  341. }
  342. AVFilter avfilter_vf_pad = {
  343. .name = "pad",
  344. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  345. .priv_size = sizeof(PadContext),
  346. .init = init,
  347. .uninit = uninit,
  348. .query_formats = query_formats,
  349. .inputs = (AVFilterPad[]) {{ .name = "default",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. .config_props = config_input,
  352. .get_video_buffer = get_video_buffer,
  353. .start_frame = start_frame,
  354. .draw_slice = draw_slice,
  355. .end_frame = end_frame, },
  356. { .name = NULL}},
  357. .outputs = (AVFilterPad[]) {{ .name = "default",
  358. .type = AVMEDIA_TYPE_VIDEO,
  359. .config_props = config_output, },
  360. { .name = NULL}},
  361. };