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.

471 lines
16KB

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