vf_slicify.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * copyright (c) 2007 Bobby Bingham
  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 libavfilter/vf_slicify.c
  22. * video slicing filter
  23. */
  24. #include "avfilter.h"
  25. #include "libavutil/pixdesc.h"
  26. typedef struct {
  27. int h; ///< output slice height
  28. int vshift; ///< vertical chroma subsampling shift
  29. } SliceContext;
  30. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  31. {
  32. SliceContext *slice = ctx->priv;
  33. slice->h = 16;
  34. if (args)
  35. sscanf(args, "%d", &slice->h);
  36. return 0;
  37. }
  38. static int config_props(AVFilterLink *link)
  39. {
  40. SliceContext *slice = link->dst->priv;
  41. slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  42. /* ensure that slices play nice with chroma subsampling, and enforce
  43. * a reasonable minimum size for the slices */
  44. slice->h = FFMAX(8, slice->h & (-1 << slice->vshift));
  45. av_log(link->dst, AV_LOG_INFO, "h:%d\n", slice->h);
  46. return 0;
  47. }
  48. static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
  49. int w, int h)
  50. {
  51. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  52. }
  53. static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  54. {
  55. avfilter_start_frame(link->dst->outputs[0], picref);
  56. }
  57. static void end_frame(AVFilterLink *link)
  58. {
  59. avfilter_end_frame(link->dst->outputs[0]);
  60. }
  61. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  62. {
  63. SliceContext *slice = link->dst->priv;
  64. int y2;
  65. if (slice_dir == 1) {
  66. for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h)
  67. avfilter_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir);
  68. if (y2 < y + h)
  69. avfilter_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir);
  70. } else if (slice_dir == -1) {
  71. for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h)
  72. avfilter_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir);
  73. if (y2 > y)
  74. avfilter_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir);
  75. }
  76. }
  77. AVFilter avfilter_vf_slicify = {
  78. .name = "slicify",
  79. .description = "Pass the images of input video on to next video filter as multiple slices.",
  80. .init = init,
  81. .priv_size = sizeof(SliceContext),
  82. .inputs = (AVFilterPad[]) {{ .name = "default",
  83. .type = CODEC_TYPE_VIDEO,
  84. .get_video_buffer = get_video_buffer,
  85. .start_frame = start_frame,
  86. .draw_slice = draw_slice,
  87. .config_props = config_props,
  88. .end_frame = end_frame, },
  89. { .name = NULL}},
  90. .outputs = (AVFilterPad[]) {{ .name = "default",
  91. .type = CODEC_TYPE_VIDEO, },
  92. { .name = NULL}},
  93. };