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.

142 lines
4.0KB

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