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.

225 lines
7.7KB

  1. /*
  2. * Copyright (c) 2019 Paul B Mahol
  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. #include "libavutil/common.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. typedef struct ScrollContext {
  27. const AVClass *class;
  28. float h_speed, v_speed;
  29. float h_pos, v_pos;
  30. float h_ipos, v_ipos;
  31. int pos_h[4], pos_v[4];
  32. const AVPixFmtDescriptor *desc;
  33. int nb_planes;
  34. int bytes;
  35. int planewidth[4];
  36. int planeheight[4];
  37. } ScrollContext;
  38. static int query_formats(AVFilterContext *ctx)
  39. {
  40. static const enum AVPixelFormat pix_fmts[] = {
  41. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  42. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  43. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  44. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  45. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  46. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  47. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  48. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  49. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  50. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  51. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  52. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  53. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  54. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  55. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  56. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  57. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  58. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  59. AV_PIX_FMT_NONE
  60. };
  61. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  62. if (!fmts_list)
  63. return AVERROR(ENOMEM);
  64. return ff_set_common_formats(ctx, fmts_list);
  65. }
  66. typedef struct ThreadData {
  67. AVFrame *in, *out;
  68. } ThreadData;
  69. static int scroll_slice(AVFilterContext *ctx, void *arg, int jobnr,
  70. int nb_jobs)
  71. {
  72. ScrollContext *s = ctx->priv;
  73. ThreadData *td = arg;
  74. AVFrame *in = td->in;
  75. AVFrame *out = td->out;
  76. for (int p = 0; p < s->nb_planes; p++) {
  77. const uint8_t *src = in->data[p];
  78. const int h = s->planeheight[p];
  79. const int w = s->planewidth[p] * s->bytes;
  80. const int slice_start = (h * jobnr) / nb_jobs;
  81. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  82. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  83. for (int y = slice_start; y < slice_end; y++) {
  84. int yy = (y + s->pos_v[p]) % h;
  85. const uint8_t *ssrc = src + yy * in->linesize[p];
  86. if (s->pos_h[p] < w)
  87. memcpy(dst, ssrc + s->pos_h[p], w - s->pos_h[p]);
  88. if (s->pos_h[p] > 0)
  89. memcpy(dst + w - s->pos_h[p], ssrc, s->pos_h[p]);
  90. dst += out->linesize[p];
  91. }
  92. }
  93. return 0;
  94. }
  95. static void scroll(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
  96. {
  97. ScrollContext *s = ctx->priv;
  98. ThreadData td;
  99. int h_pos, v_pos;
  100. s->h_pos = fmodf(s->h_pos, in->width);
  101. s->v_pos = fmodf(s->v_pos, in->height);
  102. h_pos = s->h_pos;
  103. v_pos = s->v_pos;
  104. if (h_pos < 0)
  105. h_pos += in->width;
  106. if (v_pos < 0)
  107. v_pos += in->height;
  108. s->pos_v[1] = s->pos_v[2] = AV_CEIL_RSHIFT(v_pos, s->desc->log2_chroma_h);
  109. s->pos_v[0] = s->pos_v[3] = v_pos;
  110. s->pos_h[1] = s->pos_h[2] = AV_CEIL_RSHIFT(h_pos, s->desc->log2_chroma_w) * s->bytes;
  111. s->pos_h[0] = s->pos_h[3] = h_pos * s->bytes;
  112. td.in = in; td.out = out;
  113. ctx->internal->execute(ctx, scroll_slice, &td, NULL,
  114. FFMIN(out->height, ff_filter_get_nb_threads(ctx)));
  115. s->h_pos += s->h_speed * in->width;
  116. s->v_pos += s->v_speed * in->height;
  117. }
  118. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  119. {
  120. AVFilterContext *ctx = inlink->dst;
  121. AVFilterLink *outlink = ctx->outputs[0];
  122. AVFrame *out;
  123. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  124. if (!out) {
  125. av_frame_free(&in);
  126. return AVERROR(ENOMEM);
  127. }
  128. av_frame_copy_props(out, in);
  129. scroll(ctx, in, out);
  130. av_frame_free(&in);
  131. return ff_filter_frame(outlink, out);
  132. }
  133. static int config_input(AVFilterLink *inlink)
  134. {
  135. AVFilterContext *ctx = inlink->dst;
  136. ScrollContext *s = ctx->priv;
  137. s->desc = av_pix_fmt_desc_get(inlink->format);
  138. s->nb_planes = s->desc->nb_components;
  139. s->bytes = (s->desc->comp[0].depth + 7) >> 3;
  140. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  141. s->planeheight[0] = s->planeheight[3] = inlink->h;
  142. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  143. s->planewidth[0] = s->planewidth[3] = inlink->w;
  144. s->h_pos = (1.f - s->h_ipos) * inlink->w;
  145. s->v_pos = (1.f - s->v_ipos) * inlink->h;
  146. return 0;
  147. }
  148. #define OFFSET(x) offsetof(ScrollContext, x)
  149. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  150. #define VFT AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  151. static const AVOption scroll_options[] = {
  152. { "horizontal", "set the horizontal scrolling speed", OFFSET(h_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
  153. { "h", "set the horizontal scrolling speed", OFFSET(h_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
  154. { "vertical", "set the vertical scrolling speed", OFFSET(v_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
  155. { "v", "set the vertical scrolling speed", OFFSET(v_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
  156. { "hpos", "set initial horizontal position", OFFSET(h_ipos), AV_OPT_TYPE_FLOAT, {.dbl=0.}, 0, 1., FLAGS },
  157. { "vpos", "set initial vertical position", OFFSET(v_ipos), AV_OPT_TYPE_FLOAT, {.dbl=0.}, 0, 1., FLAGS },
  158. { NULL }
  159. };
  160. AVFILTER_DEFINE_CLASS(scroll);
  161. static const AVFilterPad scroll_inputs[] = {
  162. {
  163. .name = "default",
  164. .type = AVMEDIA_TYPE_VIDEO,
  165. .config_props = config_input,
  166. .filter_frame = filter_frame,
  167. },
  168. { NULL }
  169. };
  170. static const AVFilterPad scroll_outputs[] = {
  171. {
  172. .name = "default",
  173. .type = AVMEDIA_TYPE_VIDEO,
  174. },
  175. { NULL }
  176. };
  177. AVFilter ff_vf_scroll = {
  178. .name = "scroll",
  179. .description = NULL_IF_CONFIG_SMALL("Scroll input video."),
  180. .priv_size = sizeof(ScrollContext),
  181. .priv_class = &scroll_class,
  182. .query_formats = query_formats,
  183. .inputs = scroll_inputs,
  184. .outputs = scroll_outputs,
  185. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  186. .process_command = ff_filter_process_command,
  187. };