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.

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