vf_pad.c 15 KB

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