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.

330 lines
11KB

  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
  24. */
  25. #include "avfilter.h"
  26. #include "parseutils.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/colorspace.h"
  29. typedef struct {
  30. int w, h; ///< output dimensions, a value of 0 will result in the input size
  31. int x, y; ///< offsets of the input area with respect to the padded area
  32. 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
  33. uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
  34. uint8_t *line[4];
  35. int line_step[4];
  36. int hsub, vsub; ///< chroma subsampling values
  37. } PadContext;
  38. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  39. {
  40. PadContext *pad = ctx->priv;
  41. char color_string[128] = "black";
  42. if (args)
  43. sscanf(args, "%d:%d:%d:%d:%s", &pad->w, &pad->h, &pad->x, &pad->y, color_string);
  44. if (av_parse_color(pad->color, color_string, ctx) < 0)
  45. return AVERROR(EINVAL);
  46. /* sanity check params */
  47. if (pad->w < 0 || pad->h < 0) {
  48. av_log(ctx, AV_LOG_ERROR, "Negative size values are not acceptable.\n");
  49. return AVERROR(EINVAL);
  50. }
  51. return 0;
  52. }
  53. static av_cold void uninit(AVFilterContext *ctx)
  54. {
  55. PadContext *pad = ctx->priv;
  56. int i;
  57. for (i = 0; i < 4; i++) {
  58. av_freep(&pad->line[i]);
  59. pad->line_step[i] = 0;
  60. }
  61. }
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. static const enum PixelFormat pix_fmts[] = {
  65. PIX_FMT_ARGB, PIX_FMT_RGBA,
  66. PIX_FMT_ABGR, PIX_FMT_BGRA,
  67. PIX_FMT_RGB24, PIX_FMT_BGR24,
  68. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  69. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  70. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  71. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  72. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
  73. PIX_FMT_YUVA420P,
  74. PIX_FMT_NONE
  75. };
  76. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  77. return 0;
  78. }
  79. enum { RED = 0, GREEN, BLUE, ALPHA };
  80. static int config_input(AVFilterLink *inlink)
  81. {
  82. AVFilterContext *ctx = inlink->dst;
  83. PadContext *pad = ctx->priv;
  84. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  85. uint8_t rgba_color[4];
  86. uint8_t rgba_map[4] = {0};
  87. int i, is_packed_rgb = 1;
  88. switch (inlink->format) {
  89. case PIX_FMT_ARGB:
  90. rgba_map[ALPHA] = 0; rgba_map[RED] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE] = 3;
  91. break;
  92. case PIX_FMT_ABGR:
  93. rgba_map[ALPHA] = 0; rgba_map[BLUE] = 1; rgba_map[GREEN] = 2; rgba_map[RED] = 3;
  94. break;
  95. case PIX_FMT_RGBA:
  96. case PIX_FMT_RGB24:
  97. rgba_map[RED] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE] = 2; rgba_map[ALPHA] = 3;
  98. break;
  99. case PIX_FMT_BGRA:
  100. case PIX_FMT_BGR24:
  101. rgba_map[BLUE] = 0; rgba_map[GREEN] = 1; rgba_map[RED] = 2; rgba_map[ALPHA] = 3;
  102. break;
  103. default:
  104. is_packed_rgb = 0;
  105. }
  106. pad->hsub = pix_desc->log2_chroma_w;
  107. pad->vsub = pix_desc->log2_chroma_h;
  108. if (!pad->w)
  109. pad->w = inlink->w;
  110. if (!pad->h)
  111. pad->h = inlink->h;
  112. pad->w &= ~((1 << pad->hsub) - 1);
  113. pad->h &= ~((1 << pad->vsub) - 1);
  114. pad->x &= ~((1 << pad->hsub) - 1);
  115. pad->y &= ~((1 << pad->vsub) - 1);
  116. pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
  117. pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
  118. memcpy(rgba_color, pad->color, sizeof(rgba_color));
  119. if (is_packed_rgb) {
  120. pad->line_step[0] = (av_get_bits_per_pixel(&av_pix_fmt_descriptors[inlink->format]))>>3;
  121. for (i = 0; i < 4; i++)
  122. pad->color[rgba_map[i]] = rgba_color[i];
  123. pad->line[0] = av_malloc(pad->w * pad->line_step[0]);
  124. for (i = 0; i < pad->w; i++)
  125. memcpy(pad->line[0] + i * pad->line_step[0], pad->color, pad->line_step[0]);
  126. } else {
  127. int plane;
  128. pad->color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  129. pad->color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  130. pad->color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  131. pad->color[3] = rgba_color[3];
  132. for (plane = 0; plane < 4; plane++) {
  133. int line_size;
  134. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  135. pad->line_step[plane] = 1;
  136. line_size = (pad->w >> hsub) * pad->line_step[plane];
  137. pad->line[plane] = av_malloc(line_size);
  138. memset(pad->line[plane], pad->color[plane], line_size);
  139. }
  140. }
  141. av_log(ctx, AV_LOG_INFO, "w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
  142. pad->w, pad->h, pad->x, pad->y,
  143. pad->color[0], pad->color[1], pad->color[2], pad->color[3],
  144. is_packed_rgb ? "rgba" : "yuva");
  145. if (pad->x < 0 || pad->y < 0 ||
  146. pad->w <= 0 || pad->h <= 0 ||
  147. (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
  148. (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
  149. av_log(ctx, AV_LOG_ERROR,
  150. "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  151. pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
  152. return AVERROR(EINVAL);
  153. }
  154. return 0;
  155. }
  156. static int config_output(AVFilterLink *outlink)
  157. {
  158. PadContext *pad = outlink->src->priv;
  159. outlink->w = pad->w;
  160. outlink->h = pad->h;
  161. return 0;
  162. }
  163. static AVFilterPicRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  164. {
  165. PadContext *pad = inlink->dst->priv;
  166. AVFilterPicRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
  167. w + (pad->w - pad->in_w),
  168. h + (pad->h - pad->in_h));
  169. int plane;
  170. for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
  171. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  172. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  173. picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
  174. (pad->y >> vsub) * picref->linesize[plane];
  175. }
  176. return picref;
  177. }
  178. static void start_frame(AVFilterLink *inlink, AVFilterPicRef *inpicref)
  179. {
  180. PadContext *pad = inlink->dst->priv;
  181. AVFilterPicRef *outpicref = avfilter_ref_pic(inpicref, ~0);
  182. int plane;
  183. inlink->dst->outputs[0]->outpic = outpicref;
  184. for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
  185. int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
  186. int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
  187. outpicref->data[plane] -= (pad->x >> hsub) * pad->line_step[plane] +
  188. (pad->y >> vsub) * outpicref->linesize[plane];
  189. }
  190. avfilter_start_frame(inlink->dst->outputs[0], outpicref);
  191. }
  192. static void end_frame(AVFilterLink *link)
  193. {
  194. avfilter_end_frame(link->dst->outputs[0]);
  195. avfilter_unref_pic(link->cur_pic);
  196. }
  197. static void draw_rectangle(AVFilterPicRef *outpic, uint8_t *line[4], int line_step[4],
  198. int hsub, int vsub, int x, int y, int w, int h)
  199. {
  200. int i, plane;
  201. uint8_t *p;
  202. for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
  203. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  204. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  205. p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
  206. for (i = 0; i < (h >> vsub1); i++) {
  207. memcpy(p + (x >> hsub1) * line_step[plane], line[plane], (w >> hsub1) * line_step[plane]);
  208. p += outpic->linesize[plane];
  209. }
  210. }
  211. }
  212. static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
  213. {
  214. PadContext *pad = link->dst->priv;
  215. int bar_y, bar_h = 0;
  216. if (slice_dir * before_slice == 1 && y == pad->y) {
  217. /* top bar */
  218. bar_y = 0;
  219. bar_h = pad->y;
  220. } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
  221. /* bottom bar */
  222. bar_y = pad->y + pad->in_h;
  223. bar_h = pad->h - pad->in_h - pad->y;
  224. }
  225. if (bar_h) {
  226. draw_rectangle(link->dst->outputs[0]->outpic,
  227. pad->line, pad->line_step, pad->hsub, pad->vsub,
  228. 0, bar_y, pad->w, bar_h);
  229. avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
  230. }
  231. }
  232. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  233. {
  234. PadContext *pad = link->dst->priv;
  235. AVFilterPicRef *outpic = link->dst->outputs[0]->outpic;
  236. y += pad->y;
  237. y &= ~((1 << pad->vsub) - 1);
  238. h &= ~((1 << pad->vsub) - 1);
  239. if (!h)
  240. return;
  241. draw_send_bar_slice(link, y, h, slice_dir, 1);
  242. /* left border */
  243. draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
  244. 0, y, pad->x, h);
  245. /* right border */
  246. draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
  247. pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
  248. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  249. draw_send_bar_slice(link, y, h, slice_dir, -1);
  250. }
  251. AVFilter avfilter_vf_pad = {
  252. .name = "pad",
  253. .description = NULL_IF_CONFIG_SMALL("Add paddings to the input image."),
  254. .priv_size = sizeof(PadContext),
  255. .init = init,
  256. .uninit = uninit,
  257. .query_formats = query_formats,
  258. .inputs = (AVFilterPad[]) {{ .name = "default",
  259. .type = AVMEDIA_TYPE_VIDEO,
  260. .config_props = config_input,
  261. .get_video_buffer = get_video_buffer,
  262. .start_frame = start_frame,
  263. .draw_slice = draw_slice,
  264. .end_frame = end_frame, },
  265. { .name = NULL}},
  266. .outputs = (AVFilterPad[]) {{ .name = "default",
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. .config_props = config_output, },
  269. { .name = NULL}},
  270. };