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.

258 lines
8.0KB

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