vf_tile.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "drawutils.h"
  28. #include "formats.h"
  29. #include "video.h"
  30. #include "internal.h"
  31. typedef struct {
  32. const AVClass *class;
  33. unsigned w, h;
  34. unsigned margin;
  35. unsigned padding;
  36. unsigned current;
  37. unsigned nb_frames;
  38. FFDrawContext draw;
  39. FFDrawColor blank;
  40. AVFrame *out_ref;
  41. uint8_t rgba_color[4];
  42. } TileContext;
  43. #define REASONABLE_SIZE 1024
  44. #define OFFSET(x) offsetof(TileContext, x)
  45. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption tile_options[] = {
  47. { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
  48. {.str = "6x5"}, 0, 0, FLAGS },
  49. { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
  50. AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  51. { "margin", "set outer border margin in pixels", OFFSET(margin),
  52. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
  53. { "padding", "set inner border thickness in pixels", OFFSET(padding),
  54. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
  55. { "color", "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  56. { NULL }
  57. };
  58. AVFILTER_DEFINE_CLASS(tile);
  59. static av_cold int init(AVFilterContext *ctx)
  60. {
  61. TileContext *tile = ctx->priv;
  62. if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
  63. av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
  64. tile->w, tile->h);
  65. return AVERROR(EINVAL);
  66. }
  67. if (tile->nb_frames == 0) {
  68. tile->nb_frames = tile->w * tile->h;
  69. } else if (tile->nb_frames > tile->w * tile->h) {
  70. av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
  71. tile->w, tile->h, tile->w * tile->h);
  72. return AVERROR(EINVAL);
  73. }
  74. return 0;
  75. }
  76. static int query_formats(AVFilterContext *ctx)
  77. {
  78. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  79. return 0;
  80. }
  81. static int config_props(AVFilterLink *outlink)
  82. {
  83. AVFilterContext *ctx = outlink->src;
  84. TileContext *tile = ctx->priv;
  85. AVFilterLink *inlink = ctx->inputs[0];
  86. const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
  87. const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
  88. if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
  89. av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
  90. tile->w, inlink->w);
  91. return AVERROR(EINVAL);
  92. }
  93. if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
  94. av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
  95. tile->h, inlink->h);
  96. return AVERROR(EINVAL);
  97. }
  98. outlink->w = tile->w * inlink->w + total_margin_w;
  99. outlink->h = tile->h * inlink->h + total_margin_h;
  100. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  101. outlink->frame_rate = av_mul_q(inlink->frame_rate,
  102. av_make_q(1, tile->nb_frames));
  103. ff_draw_init(&tile->draw, inlink->format, 0);
  104. ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
  105. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  106. return 0;
  107. }
  108. static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
  109. {
  110. TileContext *tile = ctx->priv;
  111. AVFilterLink *inlink = ctx->inputs[0];
  112. const unsigned tx = tile->current % tile->w;
  113. const unsigned ty = tile->current / tile->w;
  114. *x = tile->margin + (inlink->w + tile->padding) * tx;
  115. *y = tile->margin + (inlink->h + tile->padding) * ty;
  116. }
  117. static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
  118. {
  119. TileContext *tile = ctx->priv;
  120. AVFilterLink *inlink = ctx->inputs[0];
  121. unsigned x0, y0;
  122. get_current_tile_pos(ctx, &x0, &y0);
  123. ff_fill_rectangle(&tile->draw, &tile->blank,
  124. out_buf->data, out_buf->linesize,
  125. x0, y0, inlink->w, inlink->h);
  126. tile->current++;
  127. }
  128. static int end_last_frame(AVFilterContext *ctx)
  129. {
  130. TileContext *tile = ctx->priv;
  131. AVFilterLink *outlink = ctx->outputs[0];
  132. AVFrame *out_buf = tile->out_ref;
  133. int ret;
  134. while (tile->current < tile->nb_frames)
  135. draw_blank_frame(ctx, out_buf);
  136. ret = ff_filter_frame(outlink, out_buf);
  137. tile->current = 0;
  138. return ret;
  139. }
  140. /* Note: direct rendering is not possible since there is no guarantee that
  141. * buffers are fed to filter_frame in the order they were obtained from
  142. * get_buffer (think B-frames). */
  143. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  144. {
  145. AVFilterContext *ctx = inlink->dst;
  146. TileContext *tile = ctx->priv;
  147. AVFilterLink *outlink = ctx->outputs[0];
  148. unsigned x0, y0;
  149. if (!tile->current) {
  150. tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  151. if (!tile->out_ref) {
  152. av_frame_free(&picref);
  153. return AVERROR(ENOMEM);
  154. }
  155. av_frame_copy_props(tile->out_ref, picref);
  156. tile->out_ref->width = outlink->w;
  157. tile->out_ref->height = outlink->h;
  158. /* fill surface once for margin/padding */
  159. if (tile->margin || tile->padding)
  160. ff_fill_rectangle(&tile->draw, &tile->blank,
  161. tile->out_ref->data,
  162. tile->out_ref->linesize,
  163. 0, 0, outlink->w, outlink->h);
  164. }
  165. get_current_tile_pos(ctx, &x0, &y0);
  166. ff_copy_rectangle2(&tile->draw,
  167. tile->out_ref->data, tile->out_ref->linesize,
  168. picref->data, picref->linesize,
  169. x0, y0, 0, 0, inlink->w, inlink->h);
  170. av_frame_free(&picref);
  171. if (++tile->current == tile->nb_frames)
  172. return end_last_frame(ctx);
  173. return 0;
  174. }
  175. static int request_frame(AVFilterLink *outlink)
  176. {
  177. AVFilterContext *ctx = outlink->src;
  178. TileContext *tile = ctx->priv;
  179. AVFilterLink *inlink = ctx->inputs[0];
  180. int r;
  181. r = ff_request_frame(inlink);
  182. if (r == AVERROR_EOF && tile->current)
  183. r = end_last_frame(ctx);
  184. return r;
  185. }
  186. static const AVFilterPad tile_inputs[] = {
  187. {
  188. .name = "default",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .filter_frame = filter_frame,
  191. },
  192. { NULL }
  193. };
  194. static const AVFilterPad tile_outputs[] = {
  195. {
  196. .name = "default",
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. .config_props = config_props,
  199. .request_frame = request_frame,
  200. },
  201. { NULL }
  202. };
  203. AVFilter ff_vf_tile = {
  204. .name = "tile",
  205. .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
  206. .init = init,
  207. .query_formats = query_formats,
  208. .priv_size = sizeof(TileContext),
  209. .inputs = tile_inputs,
  210. .outputs = tile_outputs,
  211. .priv_class = &tile_class,
  212. };