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.

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