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.

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