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.

209 lines
6.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/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. outlink->frame_rate = av_mul_q(inlink->frame_rate,
  75. (AVRational){ 1, tile->w * tile->h });
  76. ff_draw_init(&tile->draw, inlink->format, 0);
  77. /* TODO make the color an option, or find an unified way of choosing it */
  78. ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
  79. return 0;
  80. }
  81. /* Note: direct rendering is not possible since there is no guarantee that
  82. * buffers are fed to start_frame in the order they were obtained from
  83. * get_buffer (think B-frames). */
  84. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  85. {
  86. AVFilterContext *ctx = inlink->dst;
  87. TileContext *tile = ctx->priv;
  88. AVFilterLink *outlink = ctx->outputs[0];
  89. if (tile->current)
  90. return;
  91. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  92. outlink->w, outlink->h);
  93. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  94. outlink->out_buf->video->w = outlink->w;
  95. outlink->out_buf->video->h = outlink->h;
  96. avfilter_start_frame(outlink, outlink->out_buf);
  97. }
  98. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. TileContext *tile = ctx->priv;
  102. AVFilterLink *outlink = ctx->outputs[0];
  103. unsigned x0 = inlink->w * (tile->current % tile->w);
  104. unsigned y0 = inlink->h * (tile->current / tile->w);
  105. ff_copy_rectangle2(&tile->draw,
  106. outlink->out_buf->data, outlink->out_buf->linesize,
  107. inlink ->cur_buf->data, inlink ->cur_buf->linesize,
  108. x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
  109. /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
  110. * immediately. */
  111. }
  112. static void draw_blank_frame(AVFilterContext *ctx)
  113. {
  114. TileContext *tile = ctx->priv;
  115. AVFilterLink *inlink = ctx->inputs[0];
  116. AVFilterLink *outlink = ctx->outputs[0];
  117. unsigned x0 = inlink->w * (tile->current % tile->w);
  118. unsigned y0 = inlink->h * (tile->current / tile->w);
  119. ff_fill_rectangle(&tile->draw, &tile->blank,
  120. outlink->out_buf->data, outlink->out_buf->linesize,
  121. x0, y0, inlink->w, inlink->h);
  122. tile->current++;
  123. }
  124. static void end_last_frame(AVFilterContext *ctx)
  125. {
  126. TileContext *tile = ctx->priv;
  127. AVFilterLink *outlink = ctx->outputs[0];
  128. while (tile->current < tile->w * tile->h)
  129. draw_blank_frame(ctx);
  130. avfilter_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
  131. avfilter_end_frame(outlink);
  132. tile->current = 0;
  133. }
  134. static void end_frame(AVFilterLink *inlink)
  135. {
  136. AVFilterContext *ctx = inlink->dst;
  137. TileContext *tile = ctx->priv;
  138. avfilter_unref_buffer(inlink->cur_buf);
  139. if (++tile->current == tile->w * tile->h)
  140. end_last_frame(ctx);
  141. }
  142. static int request_frame(AVFilterLink *outlink)
  143. {
  144. AVFilterContext *ctx = outlink->src;
  145. TileContext *tile = ctx->priv;
  146. AVFilterLink *inlink = ctx->inputs[0];
  147. int r;
  148. while (1) {
  149. r = avfilter_request_frame(inlink);
  150. if (r < 0) {
  151. if (r == AVERROR_EOF && tile->current)
  152. end_last_frame(ctx);
  153. else
  154. return r;
  155. break;
  156. }
  157. if (!tile->current) /* done */
  158. break;
  159. }
  160. return 0;
  161. }
  162. AVFilter avfilter_vf_tile = {
  163. .name = "tile",
  164. .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
  165. .init = init,
  166. .query_formats = query_formats,
  167. .priv_size = sizeof(TileContext),
  168. .inputs = (const AVFilterPad[]) {
  169. { .name = "default",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .start_frame = start_frame,
  172. .draw_slice = draw_slice,
  173. .end_frame = end_frame,
  174. .min_perms = AV_PERM_READ, },
  175. { .name = NULL }
  176. },
  177. .outputs = (const AVFilterPad[]) {
  178. { .name = "default",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .config_props = config_props,
  181. .request_frame = request_frame },
  182. { .name = NULL }
  183. },
  184. };