vf_pad.c 15 KB

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