vf_pad.c 18 KB

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