vf_tile.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2012 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * tile video filter
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "drawutils.h"
  29. #include "formats.h"
  30. #include "video.h"
  31. #include "internal.h"
  32. typedef struct TileContext {
  33. const AVClass *class;
  34. unsigned w, h;
  35. unsigned margin;
  36. unsigned padding;
  37. unsigned overlap;
  38. unsigned init_padding;
  39. unsigned current;
  40. unsigned nb_frames;
  41. FFDrawContext draw;
  42. FFDrawColor blank;
  43. AVFrame *out_ref;
  44. AVFrame *prev_out_ref;
  45. uint8_t rgba_color[4];
  46. } TileContext;
  47. #define OFFSET(x) offsetof(TileContext, x)
  48. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. static const AVOption tile_options[] = {
  50. { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
  51. {.str = "6x5"}, 0, 0, FLAGS },
  52. { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
  53. AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  54. { "margin", "set outer border margin in pixels", OFFSET(margin),
  55. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
  56. { "padding", "set inner border thickness in pixels", OFFSET(padding),
  57. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
  58. { "color", "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  59. { "overlap", "set how many frames to overlap for each render", OFFSET(overlap),
  60. AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  61. { "init_padding", " set how many frames to initially pad", OFFSET(init_padding),
  62. AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  63. { NULL }
  64. };
  65. AVFILTER_DEFINE_CLASS(tile);
  66. static av_cold int init(AVFilterContext *ctx)
  67. {
  68. TileContext *tile = ctx->priv;
  69. if (tile->w > UINT_MAX / tile->h) {
  70. av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
  71. tile->w, tile->h);
  72. return AVERROR(EINVAL);
  73. }
  74. if (tile->padding) {
  75. if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
  76. (tile->h - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding)) {
  77. av_log(ctx, AV_LOG_ERROR, "Combination of Tile size %ux%u, padding %d and margin %d overflows.\n",
  78. tile->w, tile->h, tile->padding, tile->margin);
  79. return AVERROR(EINVAL);
  80. }
  81. }
  82. if (tile->nb_frames == 0) {
  83. tile->nb_frames = tile->w * tile->h;
  84. } else if (tile->nb_frames > tile->w * tile->h) {
  85. av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
  86. tile->w, tile->h, tile->w * tile->h);
  87. return AVERROR(EINVAL);
  88. }
  89. if (tile->overlap >= tile->nb_frames) {
  90. av_log(ctx, AV_LOG_WARNING, "overlap must be less than %d\n", tile->nb_frames);
  91. tile->overlap = tile->nb_frames - 1;
  92. }
  93. if (tile->init_padding >= tile->nb_frames) {
  94. av_log(ctx, AV_LOG_WARNING, "init_padding must be less than %d\n", tile->nb_frames);
  95. } else {
  96. tile->current = tile->init_padding;
  97. }
  98. return 0;
  99. }
  100. static int query_formats(AVFilterContext *ctx)
  101. {
  102. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  103. }
  104. static int config_props(AVFilterLink *outlink)
  105. {
  106. AVFilterContext *ctx = outlink->src;
  107. TileContext *tile = ctx->priv;
  108. AVFilterLink *inlink = ctx->inputs[0];
  109. const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
  110. const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
  111. if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
  112. av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
  113. tile->w, inlink->w);
  114. return AVERROR(EINVAL);
  115. }
  116. if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
  117. av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
  118. tile->h, inlink->h);
  119. return AVERROR(EINVAL);
  120. }
  121. outlink->w = tile->w * inlink->w + total_margin_w;
  122. outlink->h = tile->h * inlink->h + total_margin_h;
  123. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  124. outlink->frame_rate = av_mul_q(inlink->frame_rate,
  125. av_make_q(1, tile->nb_frames - tile->overlap));
  126. ff_draw_init(&tile->draw, inlink->format, 0);
  127. ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
  128. return 0;
  129. }
  130. static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
  131. {
  132. TileContext *tile = ctx->priv;
  133. AVFilterLink *inlink = ctx->inputs[0];
  134. const unsigned tx = current % tile->w;
  135. const unsigned ty = current / tile->w;
  136. *x = tile->margin + (inlink->w + tile->padding) * tx;
  137. *y = tile->margin + (inlink->h + tile->padding) * ty;
  138. }
  139. static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
  140. {
  141. TileContext *tile = ctx->priv;
  142. AVFilterLink *inlink = ctx->inputs[0];
  143. unsigned x0, y0;
  144. get_tile_pos(ctx, &x0, &y0, tile->current);
  145. ff_fill_rectangle(&tile->draw, &tile->blank,
  146. out_buf->data, out_buf->linesize,
  147. x0, y0, inlink->w, inlink->h);
  148. tile->current++;
  149. }
  150. static int end_last_frame(AVFilterContext *ctx)
  151. {
  152. TileContext *tile = ctx->priv;
  153. AVFilterLink *outlink = ctx->outputs[0];
  154. AVFrame *out_buf = tile->out_ref;
  155. int ret;
  156. while (tile->current < tile->nb_frames)
  157. draw_blank_frame(ctx, out_buf);
  158. tile->current = tile->overlap;
  159. if (tile->current) {
  160. av_frame_free(&tile->prev_out_ref);
  161. tile->prev_out_ref = av_frame_clone(out_buf);
  162. }
  163. ret = ff_filter_frame(outlink, out_buf);
  164. tile->out_ref = NULL;
  165. return ret;
  166. }
  167. /* Note: direct rendering is not possible since there is no guarantee that
  168. * buffers are fed to filter_frame in the order they were obtained from
  169. * get_buffer (think B-frames). */
  170. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  171. {
  172. AVFilterContext *ctx = inlink->dst;
  173. TileContext *tile = ctx->priv;
  174. AVFilterLink *outlink = ctx->outputs[0];
  175. unsigned x0, y0;
  176. if (!tile->out_ref) {
  177. tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  178. if (!tile->out_ref) {
  179. av_frame_free(&picref);
  180. return AVERROR(ENOMEM);
  181. }
  182. av_frame_copy_props(tile->out_ref, picref);
  183. tile->out_ref->width = outlink->w;
  184. tile->out_ref->height = outlink->h;
  185. /* fill surface once for margin/padding */
  186. if (tile->margin || tile->padding || tile->init_padding)
  187. ff_fill_rectangle(&tile->draw, &tile->blank,
  188. tile->out_ref->data,
  189. tile->out_ref->linesize,
  190. 0, 0, outlink->w, outlink->h);
  191. tile->init_padding = 0;
  192. }
  193. if (tile->prev_out_ref) {
  194. unsigned x1, y1, i;
  195. for (i = tile->nb_frames - tile->overlap; i < tile->nb_frames; i++) {
  196. get_tile_pos(ctx, &x1, &y1, i);
  197. get_tile_pos(ctx, &x0, &y0, i - (tile->nb_frames - tile->overlap));
  198. ff_copy_rectangle2(&tile->draw,
  199. tile->out_ref->data, tile->out_ref->linesize,
  200. tile->prev_out_ref->data, tile->prev_out_ref->linesize,
  201. x0, y0, x1, y1, inlink->w, inlink->h);
  202. }
  203. }
  204. get_tile_pos(ctx, &x0, &y0, tile->current);
  205. ff_copy_rectangle2(&tile->draw,
  206. tile->out_ref->data, tile->out_ref->linesize,
  207. picref->data, picref->linesize,
  208. x0, y0, 0, 0, inlink->w, inlink->h);
  209. av_frame_free(&picref);
  210. if (++tile->current == tile->nb_frames)
  211. return end_last_frame(ctx);
  212. return 0;
  213. }
  214. static int request_frame(AVFilterLink *outlink)
  215. {
  216. AVFilterContext *ctx = outlink->src;
  217. TileContext *tile = ctx->priv;
  218. AVFilterLink *inlink = ctx->inputs[0];
  219. int r;
  220. r = ff_request_frame(inlink);
  221. if (r == AVERROR_EOF && tile->current && tile->out_ref)
  222. r = end_last_frame(ctx);
  223. return r;
  224. }
  225. static av_cold void uninit(AVFilterContext *ctx)
  226. {
  227. TileContext *tile = ctx->priv;
  228. av_frame_free(&tile->prev_out_ref);
  229. }
  230. static const AVFilterPad tile_inputs[] = {
  231. {
  232. .name = "default",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. .filter_frame = filter_frame,
  235. },
  236. { NULL }
  237. };
  238. static const AVFilterPad tile_outputs[] = {
  239. {
  240. .name = "default",
  241. .type = AVMEDIA_TYPE_VIDEO,
  242. .config_props = config_props,
  243. .request_frame = request_frame,
  244. },
  245. { NULL }
  246. };
  247. AVFilter ff_vf_tile = {
  248. .name = "tile",
  249. .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
  250. .init = init,
  251. .uninit = uninit,
  252. .query_formats = query_formats,
  253. .priv_size = sizeof(TileContext),
  254. .inputs = tile_inputs,
  255. .outputs = tile_outputs,
  256. .priv_class = &tile_class,
  257. };