vf_pad.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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",
  46. "sar",
  47. "dar",
  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_SAR,
  64. VAR_DAR,
  65. VAR_HSUB,
  66. VAR_VSUB,
  67. VARS_NB
  68. };
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. static const enum PixelFormat pix_fmts[] = {
  72. PIX_FMT_ARGB, PIX_FMT_RGBA,
  73. PIX_FMT_ABGR, PIX_FMT_BGRA,
  74. PIX_FMT_RGB24, PIX_FMT_BGR24,
  75. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  76. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  77. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  78. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  79. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
  80. PIX_FMT_YUVA420P,
  81. PIX_FMT_NONE
  82. };
  83. avfilter_set_common_pixel_formats(ctx, avfilter_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, void *opaque)
  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_descriptors[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] = (float) inlink->w / inlink->h;
  143. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  144. (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  145. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  146. var_values[VAR_HSUB] = 1<<pad->hsub;
  147. var_values[VAR_VSUB] = 1<<pad->vsub;
  148. /* evaluate width and height */
  149. av_expr_parse_and_eval(&res, (expr = pad->w_expr),
  150. var_names, var_values,
  151. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  152. pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  153. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
  154. var_names, var_values,
  155. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  156. goto eval_fail;
  157. pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  158. /* evaluate the width again, as it may depend on the evaluated output height */
  159. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
  160. var_names, var_values,
  161. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  162. goto eval_fail;
  163. pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  164. /* evaluate x and y */
  165. av_expr_parse_and_eval(&res, (expr = pad->x_expr),
  166. var_names, var_values,
  167. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  168. pad->x = var_values[VAR_X] = res;
  169. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
  170. var_names, var_values,
  171. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  172. goto eval_fail;
  173. pad->y = var_values[VAR_Y] = res;
  174. /* evaluate x again, as it may depend on the evaluated y value */
  175. if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
  176. var_names, var_values,
  177. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  178. goto eval_fail;
  179. pad->x = var_values[VAR_X] = res;
  180. /* sanity check params */
  181. if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
  182. av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  183. return AVERROR(EINVAL);
  184. }
  185. if (!pad->w)
  186. pad->w = inlink->w;
  187. if (!pad->h)
  188. pad->h = inlink->h;
  189. pad->w &= ~((1 << pad->hsub) - 1);
  190. pad->h &= ~((1 << pad->vsub) - 1);
  191. pad->x &= ~((1 << pad->hsub) - 1);
  192. pad->y &= ~((1 << pad->vsub) - 1);
  193. pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
  194. pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
  195. memcpy(rgba_color, pad->color, sizeof(rgba_color));
  196. ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
  197. inlink->format, rgba_color, &is_packed_rgba, NULL);
  198. 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",
  199. inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
  200. pad->color[0], pad->color[1], pad->color[2], pad->color[3],
  201. is_packed_rgba ? "rgba" : "yuva");
  202. if (pad->x < 0 || pad->y < 0 ||
  203. pad->w <= 0 || pad->h <= 0 ||
  204. (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
  205. (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
  206. av_log(ctx, AV_LOG_ERROR,
  207. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  208. pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
  209. return AVERROR(EINVAL);
  210. }
  211. return 0;
  212. eval_fail:
  213. av_log(NULL, AV_LOG_ERROR,
  214. "Error when evaluating the expression '%s'\n", expr);
  215. return ret;
  216. }
  217. static int config_output(AVFilterLink *outlink)
  218. {
  219. PadContext *pad = outlink->src->priv;
  220. outlink->w = pad->w;
  221. outlink->h = pad->h;
  222. return 0;
  223. }
  224. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  225. {
  226. PadContext *pad = inlink->dst->priv;
  227. AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
  228. w + (pad->w - pad->in_w),
  229. h + (pad->h - pad->in_h));
  230. int plane;
  231. picref->video->w = w;
  232. picref->video->h = h;
  233. for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
  234. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  235. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  236. picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
  237. (pad->y >> vsub) * picref->linesize[plane];
  238. }
  239. return picref;
  240. }
  241. static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
  242. {
  243. int64_t x_in_buf, y_in_buf;
  244. x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
  245. + (x >> hsub) * pad ->line_step[plane]
  246. + (y >> vsub) * outpicref->linesize [plane];
  247. if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
  248. return 1;
  249. x_in_buf /= pad->line_step[plane];
  250. av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
  251. y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
  252. x_in_buf %= outpicref->buf->linesize[plane];
  253. if( y_in_buf<<vsub >= outpicref->buf->h
  254. || x_in_buf<<hsub >= outpicref->buf->w)
  255. return 1;
  256. return 0;
  257. }
  258. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  259. {
  260. PadContext *pad = inlink->dst->priv;
  261. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  262. int plane;
  263. for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
  264. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  265. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  266. av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
  267. if(outpicref->format != outpicref->buf->format) //unsupported currently
  268. break;
  269. outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
  270. + (pad->y >> vsub) * outpicref->linesize [plane];
  271. if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
  272. || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
  273. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
  274. || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
  275. )
  276. break;
  277. }
  278. pad->needs_copy= plane < 4 && outpicref->data[plane];
  279. if(pad->needs_copy){
  280. av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  281. avfilter_unref_buffer(outpicref);
  282. outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
  283. FFMAX(inlink->w, pad->w),
  284. FFMAX(inlink->h, pad->h));
  285. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  286. }
  287. inlink->dst->outputs[0]->out_buf = outpicref;
  288. outpicref->video->w = pad->w;
  289. outpicref->video->h = pad->h;
  290. avfilter_start_frame(inlink->dst->outputs[0], outpicref);
  291. }
  292. static void end_frame(AVFilterLink *link)
  293. {
  294. avfilter_end_frame(link->dst->outputs[0]);
  295. avfilter_unref_buffer(link->cur_buf);
  296. }
  297. static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  298. {
  299. PadContext *pad = link->dst->priv;
  300. int bar_y, bar_h = 0;
  301. if (slice_dir * before_slice == 1 && y == pad->y) {
  302. /* top bar */
  303. bar_y = 0;
  304. bar_h = pad->y;
  305. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  306. /* bottom bar */
  307. bar_y = pad->y + pad->in_h;
  308. bar_h = pad->h - pad->in_h - pad->y;
  309. }
  310. if (bar_h) {
  311. ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
  312. link->dst->outputs[0]->out_buf->linesize,
  313. pad->line, pad->line_step, pad->hsub, pad->vsub,
  314. 0, bar_y, pad->w, bar_h);
  315. avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  316. }
  317. }
  318. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  319. {
  320. PadContext *pad = link->dst->priv;
  321. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  322. AVFilterBufferRef *inpic = link->cur_buf;
  323. y += pad->y;
  324. y &= ~((1 << pad->vsub) - 1);
  325. h &= ~((1 << pad->vsub) - 1);
  326. if (!h)
  327. return;
  328. draw_send_bar_slice(link, y, h, slice_dir, 1);
  329. /* left border */
  330. ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
  331. pad->hsub, pad->vsub, 0, y, pad->x, h);
  332. if(pad->needs_copy){
  333. ff_copy_rectangle(outpic->data, outpic->linesize,
  334. inpic->data, inpic->linesize, pad->line_step,
  335. pad->hsub, pad->vsub,
  336. pad->x, y, y-pad->y, inpic->video->w, h);
  337. }
  338. /* right border */
  339. ff_draw_rectangle(outpic->data, outpic->linesize,
  340. pad->line, pad->line_step, pad->hsub, pad->vsub,
  341. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  342. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  343. draw_send_bar_slice(link, y, h, slice_dir, -1);
  344. }
  345. AVFilter avfilter_vf_pad = {
  346. .name = "pad",
  347. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  348. .priv_size = sizeof(PadContext),
  349. .init = init,
  350. .uninit = uninit,
  351. .query_formats = query_formats,
  352. .inputs = (AVFilterPad[]) {{ .name = "default",
  353. .type = AVMEDIA_TYPE_VIDEO,
  354. .config_props = config_input,
  355. .get_video_buffer = get_video_buffer,
  356. .start_frame = start_frame,
  357. .draw_slice = draw_slice,
  358. .end_frame = end_frame, },
  359. { .name = NULL}},
  360. .outputs = (AVFilterPad[]) {{ .name = "default",
  361. .type = AVMEDIA_TYPE_VIDEO,
  362. .config_props = config_output, },
  363. { .name = NULL}},
  364. };