vf_slicify.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  22. * video slicing filter
  23. */
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "libavutil/pixdesc.h"
  28. typedef struct {
  29. int h; ///< output slice height
  30. int vshift; ///< vertical chroma subsampling shift
  31. uint32_t lcg_state; ///< LCG state used to compute random slice height
  32. int use_random_h; ///< enable the use of random slice height values
  33. } SliceContext;
  34. static av_cold int init(AVFilterContext *ctx, const char *args)
  35. {
  36. SliceContext *slice = ctx->priv;
  37. slice->h = 16;
  38. if (args) {
  39. if (!strcmp(args, "random")) {
  40. slice->use_random_h = 1;
  41. } else {
  42. sscanf(args, "%d", &slice->h);
  43. }
  44. }
  45. return 0;
  46. }
  47. static int config_props(AVFilterLink *link)
  48. {
  49. SliceContext *slice = link->dst->priv;
  50. slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  51. return 0;
  52. }
  53. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  54. {
  55. SliceContext *slice = link->dst->priv;
  56. if (slice->use_random_h) {
  57. slice->lcg_state = slice->lcg_state * 1664525 + 1013904223;
  58. slice->h = 8 + (uint64_t)slice->lcg_state * 25 / UINT32_MAX;
  59. }
  60. /* ensure that slices play nice with chroma subsampling, and enforce
  61. * a reasonable minimum size for the slices */
  62. slice->h = FFMAX(8, slice->h & (-1 << slice->vshift));
  63. av_log(link->dst, AV_LOG_DEBUG, "h:%d\n", slice->h);
  64. link->cur_buf = NULL;
  65. return ff_start_frame(link->dst->outputs[0], picref);
  66. }
  67. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  68. {
  69. SliceContext *slice = link->dst->priv;
  70. int y2, ret = 0;
  71. if (slice_dir == 1) {
  72. for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h) {
  73. ret = ff_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir);
  74. if (ret < 0)
  75. return ret;
  76. }
  77. if (y2 < y + h)
  78. return ff_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir);
  79. } else if (slice_dir == -1) {
  80. for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h) {
  81. ret = ff_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. if (y2 > y)
  86. return ff_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir);
  87. }
  88. return 0;
  89. }
  90. AVFilter avfilter_vf_slicify = {
  91. .name = "slicify",
  92. .description = NULL_IF_CONFIG_SMALL("Pass the images of input video on to next video filter as multiple slices."),
  93. .init = init,
  94. .priv_size = sizeof(SliceContext),
  95. .inputs = (const AVFilterPad[]) {{ .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .get_video_buffer = ff_null_get_video_buffer,
  98. .start_frame = start_frame,
  99. .draw_slice = draw_slice,
  100. .config_props = config_props,
  101. .end_frame = ff_null_end_frame, },
  102. { .name = NULL}},
  103. .outputs = (const AVFilterPad[]) {{ .name = "default",
  104. .type = AVMEDIA_TYPE_VIDEO, },
  105. { .name = NULL}},
  106. };