vf_pad.c 15 KB

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