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.

288 lines
9.2KB

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