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.

468 lines
15KB

  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. avfilter_start_frame(inlink->dst->outputs[0], outpicref);
  213. }
  214. static void end_frame(AVFilterLink *link)
  215. {
  216. avfilter_end_frame(link->dst->outputs[0]);
  217. avfilter_unref_buffer(link->cur_buf);
  218. }
  219. static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  220. {
  221. PadContext *pad = link->dst->priv;
  222. int bar_y, bar_h = 0;
  223. if (slice_dir * before_slice == 1 && y == pad->y) {
  224. /* top bar */
  225. bar_y = 0;
  226. bar_h = pad->y;
  227. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  228. /* bottom bar */
  229. bar_y = pad->y + pad->in_h;
  230. bar_h = pad->h - pad->in_h - pad->y;
  231. }
  232. if (bar_h) {
  233. draw_rectangle(link->dst->outputs[0]->out_buf,
  234. pad->line, pad->line_step, pad->hsub, pad->vsub,
  235. 0, bar_y, pad->w, bar_h);
  236. avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  237. }
  238. }
  239. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  240. {
  241. PadContext *pad = link->dst->priv;
  242. AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
  243. y += pad->y;
  244. y &= ~((1 << pad->vsub) - 1);
  245. h &= ~((1 << pad->vsub) - 1);
  246. if (!h)
  247. return;
  248. draw_send_bar_slice(link, y, h, slice_dir, 1);
  249. /* left border */
  250. draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
  251. 0, y, pad->x, h);
  252. /* right border */
  253. draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
  254. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  255. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  256. draw_send_bar_slice(link, y, h, slice_dir, -1);
  257. }
  258. AVFilter avfilter_vf_pad = {
  259. .name = "pad",
  260. .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
  261. .priv_size = sizeof(PadContext),
  262. .init = init,
  263. .uninit = uninit,
  264. .query_formats = query_formats,
  265. .inputs = (AVFilterPad[]) {{ .name = "default",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. .config_props = config_input,
  268. .get_video_buffer = get_video_buffer,
  269. .start_frame = start_frame,
  270. .draw_slice = draw_slice,
  271. .end_frame = end_frame, },
  272. { .name = NULL}},
  273. .outputs = (AVFilterPad[]) {{ .name = "default",
  274. .type = AVMEDIA_TYPE_VIDEO,
  275. .config_props = config_output, },
  276. { .name = NULL}},
  277. };
  278. #endif /* CONFIG_PAD_FILTER */
  279. #if CONFIG_COLOR_FILTER
  280. typedef struct {
  281. int w, h;
  282. uint8_t color[4];
  283. AVRational time_base;
  284. uint8_t *line[4];
  285. int line_step[4];
  286. int hsub, vsub; ///< chroma subsampling values
  287. uint64_t pts;
  288. } ColorContext;
  289. static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
  290. {
  291. ColorContext *color = ctx->priv;
  292. char color_string[128] = "black";
  293. char frame_size [128] = "320x240";
  294. char frame_rate [128] = "25";
  295. AVRational frame_rate_q;
  296. int ret;
  297. if (args)
  298. sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
  299. if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
  300. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
  301. return AVERROR(EINVAL);
  302. }
  303. if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
  304. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  305. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
  306. return AVERROR(EINVAL);
  307. }
  308. color->time_base.num = frame_rate_q.den;
  309. color->time_base.den = frame_rate_q.num;
  310. if ((ret = av_parse_color(color->color, color_string, -1, ctx)) < 0)
  311. return ret;
  312. return 0;
  313. }
  314. static av_cold void color_uninit(AVFilterContext *ctx)
  315. {
  316. ColorContext *color = ctx->priv;
  317. int i;
  318. for (i = 0; i < 4; i++) {
  319. av_freep(&color->line[i]);
  320. color->line_step[i] = 0;
  321. }
  322. }
  323. static int color_config_props(AVFilterLink *inlink)
  324. {
  325. AVFilterContext *ctx = inlink->src;
  326. ColorContext *color = ctx->priv;
  327. uint8_t rgba_color[4];
  328. int is_packed_rgba;
  329. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  330. color->hsub = pix_desc->log2_chroma_w;
  331. color->vsub = pix_desc->log2_chroma_h;
  332. color->w &= ~((1 << color->hsub) - 1);
  333. color->h &= ~((1 << color->vsub) - 1);
  334. if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
  335. return AVERROR(EINVAL);
  336. memcpy(rgba_color, color->color, sizeof(rgba_color));
  337. fill_line_with_color(color->line, color->line_step, color->w, color->color,
  338. inlink->format, rgba_color, &is_packed_rgba);
  339. av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x[%s]\n",
  340. color->w, color->h, color->time_base.den, color->time_base.num,
  341. color->color[0], color->color[1], color->color[2], color->color[3],
  342. is_packed_rgba ? "rgba" : "yuva");
  343. inlink->w = color->w;
  344. inlink->h = color->h;
  345. return 0;
  346. }
  347. static int color_request_frame(AVFilterLink *link)
  348. {
  349. ColorContext *color = link->src->priv;
  350. AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
  351. picref->video->pixel_aspect = (AVRational) {1, 1};
  352. picref->pts = av_rescale_q(color->pts++, color->time_base, AV_TIME_BASE_Q);
  353. picref->pos = 0;
  354. avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
  355. draw_rectangle(picref,
  356. color->line, color->line_step, color->hsub, color->vsub,
  357. 0, 0, color->w, color->h);
  358. avfilter_draw_slice(link, 0, color->h, 1);
  359. avfilter_end_frame(link);
  360. avfilter_unref_buffer(picref);
  361. return 0;
  362. }
  363. AVFilter avfilter_vsrc_color = {
  364. .name = "color",
  365. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]"),
  366. .priv_size = sizeof(ColorContext),
  367. .init = color_init,
  368. .uninit = color_uninit,
  369. .query_formats = query_formats,
  370. .inputs = (AVFilterPad[]) {{ .name = NULL}},
  371. .outputs = (AVFilterPad[]) {{ .name = "default",
  372. .type = AVMEDIA_TYPE_VIDEO,
  373. .request_frame = color_request_frame,
  374. .config_props = color_config_props },
  375. { .name = NULL}},
  376. };
  377. #endif /* CONFIG_COLOR_FILTER */