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.

252 lines
7.9KB

  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 current;
  38. unsigned nb_frames;
  39. FFDrawContext draw;
  40. FFDrawColor blank;
  41. AVFrame *out_ref;
  42. uint8_t rgba_color[4];
  43. } TileContext;
  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 > UINT_MAX / tile->h) {
  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->padding) {
  68. if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
  69. (tile->h - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding)) {
  70. av_log(ctx, AV_LOG_ERROR, "Combination of Tile size %ux%u, padding %d and margin %d overflows.\n",
  71. tile->w, tile->h, tile->padding, tile->margin);
  72. return AVERROR(EINVAL);
  73. }
  74. }
  75. if (tile->nb_frames == 0) {
  76. tile->nb_frames = tile->w * tile->h;
  77. } else if (tile->nb_frames > tile->w * tile->h) {
  78. av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
  79. tile->w, tile->h, tile->w * tile->h);
  80. return AVERROR(EINVAL);
  81. }
  82. return 0;
  83. }
  84. static int query_formats(AVFilterContext *ctx)
  85. {
  86. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  87. }
  88. static int config_props(AVFilterLink *outlink)
  89. {
  90. AVFilterContext *ctx = outlink->src;
  91. TileContext *tile = ctx->priv;
  92. AVFilterLink *inlink = ctx->inputs[0];
  93. const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
  94. const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
  95. if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
  96. av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
  97. tile->w, inlink->w);
  98. return AVERROR(EINVAL);
  99. }
  100. if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
  101. av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
  102. tile->h, inlink->h);
  103. return AVERROR(EINVAL);
  104. }
  105. outlink->w = tile->w * inlink->w + total_margin_w;
  106. outlink->h = tile->h * inlink->h + total_margin_h;
  107. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  108. outlink->frame_rate = av_mul_q(inlink->frame_rate,
  109. av_make_q(1, tile->nb_frames));
  110. ff_draw_init(&tile->draw, inlink->format, 0);
  111. ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
  112. return 0;
  113. }
  114. static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
  115. {
  116. TileContext *tile = ctx->priv;
  117. AVFilterLink *inlink = ctx->inputs[0];
  118. const unsigned tx = tile->current % tile->w;
  119. const unsigned ty = tile->current / tile->w;
  120. *x = tile->margin + (inlink->w + tile->padding) * tx;
  121. *y = tile->margin + (inlink->h + tile->padding) * ty;
  122. }
  123. static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
  124. {
  125. TileContext *tile = ctx->priv;
  126. AVFilterLink *inlink = ctx->inputs[0];
  127. unsigned x0, y0;
  128. get_current_tile_pos(ctx, &x0, &y0);
  129. ff_fill_rectangle(&tile->draw, &tile->blank,
  130. out_buf->data, out_buf->linesize,
  131. x0, y0, inlink->w, inlink->h);
  132. tile->current++;
  133. }
  134. static int end_last_frame(AVFilterContext *ctx)
  135. {
  136. TileContext *tile = ctx->priv;
  137. AVFilterLink *outlink = ctx->outputs[0];
  138. AVFrame *out_buf = tile->out_ref;
  139. int ret;
  140. while (tile->current < tile->nb_frames)
  141. draw_blank_frame(ctx, out_buf);
  142. ret = ff_filter_frame(outlink, out_buf);
  143. tile->current = 0;
  144. return ret;
  145. }
  146. /* Note: direct rendering is not possible since there is no guarantee that
  147. * buffers are fed to filter_frame in the order they were obtained from
  148. * get_buffer (think B-frames). */
  149. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  150. {
  151. AVFilterContext *ctx = inlink->dst;
  152. TileContext *tile = ctx->priv;
  153. AVFilterLink *outlink = ctx->outputs[0];
  154. unsigned x0, y0;
  155. if (!tile->current) {
  156. tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  157. if (!tile->out_ref) {
  158. av_frame_free(&picref);
  159. return AVERROR(ENOMEM);
  160. }
  161. av_frame_copy_props(tile->out_ref, picref);
  162. tile->out_ref->width = outlink->w;
  163. tile->out_ref->height = outlink->h;
  164. /* fill surface once for margin/padding */
  165. if (tile->margin || tile->padding)
  166. ff_fill_rectangle(&tile->draw, &tile->blank,
  167. tile->out_ref->data,
  168. tile->out_ref->linesize,
  169. 0, 0, outlink->w, outlink->h);
  170. }
  171. get_current_tile_pos(ctx, &x0, &y0);
  172. ff_copy_rectangle2(&tile->draw,
  173. tile->out_ref->data, tile->out_ref->linesize,
  174. picref->data, picref->linesize,
  175. x0, y0, 0, 0, inlink->w, inlink->h);
  176. av_frame_free(&picref);
  177. if (++tile->current == tile->nb_frames)
  178. return end_last_frame(ctx);
  179. return 0;
  180. }
  181. static int request_frame(AVFilterLink *outlink)
  182. {
  183. AVFilterContext *ctx = outlink->src;
  184. TileContext *tile = ctx->priv;
  185. AVFilterLink *inlink = ctx->inputs[0];
  186. int r;
  187. r = ff_request_frame(inlink);
  188. if (r == AVERROR_EOF && tile->current)
  189. r = end_last_frame(ctx);
  190. return r;
  191. }
  192. static const AVFilterPad tile_inputs[] = {
  193. {
  194. .name = "default",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .filter_frame = filter_frame,
  197. },
  198. { NULL }
  199. };
  200. static const AVFilterPad tile_outputs[] = {
  201. {
  202. .name = "default",
  203. .type = AVMEDIA_TYPE_VIDEO,
  204. .config_props = config_props,
  205. .request_frame = request_frame,
  206. },
  207. { NULL }
  208. };
  209. AVFilter ff_vf_tile = {
  210. .name = "tile",
  211. .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
  212. .init = init,
  213. .query_formats = query_formats,
  214. .priv_size = sizeof(TileContext),
  215. .inputs = tile_inputs,
  216. .outputs = tile_outputs,
  217. .priv_class = &tile_class,
  218. };