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.8KB

  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. { "margin", "set outer border margin in pixels", OFFSET(margin),
  49. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
  50. { "padding", "set inner border thickness in pixels", OFFSET(padding),
  51. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, 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. {NULL},
  55. };
  56. AVFILTER_DEFINE_CLASS(tile);
  57. static av_cold int init(AVFilterContext *ctx, const char *args)
  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. return 0;
  105. }
  106. static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
  107. {
  108. TileContext *tile = ctx->priv;
  109. AVFilterLink *inlink = ctx->inputs[0];
  110. const unsigned tx = tile->current % tile->w;
  111. const unsigned ty = tile->current / tile->w;
  112. *x = tile->margin + (inlink->w + tile->padding) * tx;
  113. *y = tile->margin + (inlink->h + tile->padding) * ty;
  114. }
  115. static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
  116. {
  117. TileContext *tile = ctx->priv;
  118. AVFilterLink *inlink = ctx->inputs[0];
  119. unsigned x0, y0;
  120. get_current_tile_pos(ctx, &x0, &y0);
  121. ff_fill_rectangle(&tile->draw, &tile->blank,
  122. out_buf->data, out_buf->linesize,
  123. x0, y0, inlink->w, inlink->h);
  124. tile->current++;
  125. }
  126. static int end_last_frame(AVFilterContext *ctx)
  127. {
  128. TileContext *tile = ctx->priv;
  129. AVFilterLink *outlink = ctx->outputs[0];
  130. AVFrame *out_buf = tile->out_ref;
  131. int ret;
  132. while (tile->current < tile->nb_frames)
  133. draw_blank_frame(ctx, out_buf);
  134. ret = ff_filter_frame(outlink, out_buf);
  135. tile->current = 0;
  136. return ret;
  137. }
  138. /* Note: direct rendering is not possible since there is no guarantee that
  139. * buffers are fed to filter_frame in the order they were obtained from
  140. * get_buffer (think B-frames). */
  141. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  142. {
  143. AVFilterContext *ctx = inlink->dst;
  144. TileContext *tile = ctx->priv;
  145. AVFilterLink *outlink = ctx->outputs[0];
  146. unsigned x0, y0;
  147. if (!tile->current) {
  148. tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  149. if (!tile->out_ref)
  150. return AVERROR(ENOMEM);
  151. av_frame_copy_props(tile->out_ref, picref);
  152. tile->out_ref->width = outlink->w;
  153. tile->out_ref->height = outlink->h;
  154. /* fill surface once for margin/padding */
  155. if (tile->margin || tile->padding)
  156. ff_fill_rectangle(&tile->draw, &tile->blank,
  157. tile->out_ref->data,
  158. tile->out_ref->linesize,
  159. 0, 0, outlink->w, outlink->h);
  160. }
  161. get_current_tile_pos(ctx, &x0, &y0);
  162. ff_copy_rectangle2(&tile->draw,
  163. tile->out_ref->data, tile->out_ref->linesize,
  164. picref->data, picref->linesize,
  165. x0, y0, 0, 0, inlink->w, inlink->h);
  166. av_frame_free(&picref);
  167. if (++tile->current == tile->nb_frames)
  168. return end_last_frame(ctx);
  169. return 0;
  170. }
  171. static int request_frame(AVFilterLink *outlink)
  172. {
  173. AVFilterContext *ctx = outlink->src;
  174. TileContext *tile = ctx->priv;
  175. AVFilterLink *inlink = ctx->inputs[0];
  176. int r;
  177. while (1) {
  178. r = ff_request_frame(inlink);
  179. if (r < 0) {
  180. if (r == AVERROR_EOF && tile->current)
  181. r = end_last_frame(ctx);
  182. break;
  183. }
  184. if (!tile->current) /* done */
  185. break;
  186. }
  187. return r;
  188. }
  189. static const AVFilterPad tile_inputs[] = {
  190. {
  191. .name = "default",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .filter_frame = filter_frame,
  194. },
  195. { NULL }
  196. };
  197. static const AVFilterPad tile_outputs[] = {
  198. {
  199. .name = "default",
  200. .type = AVMEDIA_TYPE_VIDEO,
  201. .config_props = config_props,
  202. .request_frame = request_frame,
  203. },
  204. { NULL }
  205. };
  206. static const char *const shorthand[] =
  207. { "layout", "nb_frames", "margin", "padding", NULL };
  208. AVFilter avfilter_vf_tile = {
  209. .name = "tile",
  210. .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
  211. .init = init,
  212. .query_formats = query_formats,
  213. .priv_size = sizeof(TileContext),
  214. .inputs = tile_inputs,
  215. .outputs = tile_outputs,
  216. .priv_class = &tile_class,
  217. .shorthand = shorthand,
  218. };