vf_tile.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "drawutils.h"
  27. typedef struct {
  28. unsigned w, h;
  29. unsigned current;
  30. FFDrawContext draw;
  31. FFDrawColor blank;
  32. } TileContext;
  33. #define REASONABLE_SIZE 1024
  34. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  35. {
  36. TileContext *tile = ctx->priv;
  37. int r;
  38. char dummy;
  39. if (!args)
  40. args = "6x5";
  41. r = sscanf(args, "%ux%u%c", &tile->w, &tile->h, &dummy);
  42. if (r != 2 || !tile->w || !tile->h)
  43. return AVERROR(EINVAL);
  44. if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
  45. av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
  46. tile->w, tile->h);
  47. return AVERROR(EINVAL);
  48. }
  49. return 0;
  50. }
  51. static int query_formats(AVFilterContext *ctx)
  52. {
  53. avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
  54. return 0;
  55. }
  56. static int config_props(AVFilterLink *outlink)
  57. {
  58. AVFilterContext *ctx = outlink->src;
  59. TileContext *tile = ctx->priv;
  60. AVFilterLink *inlink = ctx->inputs[0];
  61. if (inlink->w > INT_MAX / tile->w) {
  62. av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
  63. tile->w, inlink->w);
  64. return AVERROR(EINVAL);
  65. }
  66. if (inlink->h > INT_MAX / tile->h) {
  67. av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
  68. tile->h, inlink->h);
  69. return AVERROR(EINVAL);
  70. }
  71. outlink->w = tile->w * inlink->w;
  72. outlink->h = tile->h * inlink->h;
  73. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  74. ff_draw_init(&tile->draw, inlink->format, 0);
  75. /* TODO make the color an option, or find an unified way of choosing it */
  76. ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
  77. return 0;
  78. }
  79. /* Note: direct rendering is not possible since there is no guarantee that
  80. * buffers are fed to start_frame in the order they were obtained from
  81. * get_buffer (think B-frames). */
  82. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  83. {
  84. AVFilterContext *ctx = inlink->dst;
  85. TileContext *tile = ctx->priv;
  86. AVFilterLink *outlink = ctx->outputs[0];
  87. if (tile->current)
  88. return;
  89. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  90. outlink->w, outlink->h);
  91. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  92. outlink->out_buf->video->w = outlink->w;
  93. outlink->out_buf->video->h = outlink->h;
  94. avfilter_start_frame(outlink, outlink->out_buf);
  95. }
  96. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  97. {
  98. AVFilterContext *ctx = inlink->dst;
  99. TileContext *tile = ctx->priv;
  100. AVFilterLink *outlink = ctx->outputs[0];
  101. unsigned x0 = inlink->w * (tile->current % tile->w);
  102. unsigned y0 = inlink->h * (tile->current / tile->w);
  103. ff_copy_rectangle2(&tile->draw,
  104. outlink->out_buf->data, outlink->out_buf->linesize,
  105. inlink ->cur_buf->data, inlink ->cur_buf->linesize,
  106. x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
  107. /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
  108. * immediately. */
  109. }
  110. static void draw_blank_frame(AVFilterContext *ctx)
  111. {
  112. TileContext *tile = ctx->priv;
  113. AVFilterLink *inlink = ctx->inputs[0];
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. unsigned x0 = inlink->w * (tile->current % tile->w);
  116. unsigned y0 = inlink->h * (tile->current / tile->w);
  117. ff_fill_rectangle(&tile->draw, &tile->blank,
  118. outlink->out_buf->data, outlink->out_buf->linesize,
  119. x0, y0, inlink->w, inlink->h);
  120. tile->current++;
  121. }
  122. static void end_last_frame(AVFilterContext *ctx)
  123. {
  124. TileContext *tile = ctx->priv;
  125. AVFilterLink *outlink = ctx->outputs[0];
  126. while (tile->current < tile->w * tile->h)
  127. draw_blank_frame(ctx);
  128. avfilter_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
  129. avfilter_end_frame(outlink);
  130. tile->current = 0;
  131. }
  132. static void end_frame(AVFilterLink *inlink)
  133. {
  134. AVFilterContext *ctx = inlink->dst;
  135. TileContext *tile = ctx->priv;
  136. avfilter_unref_buffer(inlink->cur_buf);
  137. if (++tile->current == tile->w * tile->h)
  138. end_last_frame(ctx);
  139. }
  140. static int request_frame(AVFilterLink *outlink)
  141. {
  142. AVFilterContext *ctx = outlink->src;
  143. TileContext *tile = ctx->priv;
  144. AVFilterLink *inlink = ctx->inputs[0];
  145. int r;
  146. while (1) {
  147. r = avfilter_request_frame(inlink);
  148. if (r < 0) {
  149. if (r == AVERROR_EOF && tile->current)
  150. end_last_frame(ctx);
  151. else
  152. return r;
  153. break;
  154. }
  155. if (!tile->current) /* done */
  156. break;
  157. }
  158. return 0;
  159. }
  160. AVFilter avfilter_vf_tile = {
  161. .name = "tile",
  162. .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
  163. .init = init,
  164. .query_formats = query_formats,
  165. .priv_size = sizeof(TileContext),
  166. .inputs = (const AVFilterPad[]) {
  167. { .name = "default",
  168. .type = AVMEDIA_TYPE_VIDEO,
  169. .start_frame = start_frame,
  170. .draw_slice = draw_slice,
  171. .end_frame = end_frame,
  172. .min_perms = AV_PERM_READ, },
  173. { .name = NULL }
  174. },
  175. .outputs = (const AVFilterPad[]) {
  176. { .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .config_props = config_props,
  179. .request_frame = request_frame },
  180. { .name = NULL }
  181. },
  182. };