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.

243 lines
7.5KB

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