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.

177 lines
5.5KB

  1. /*
  2. * Copyright (c) 2010 Brandon Mintern
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * video fade filter
  24. * based heavily on vf_negate.c by Bobby Bingham
  25. */
  26. #include "libavutil/common.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct {
  33. int factor, fade_per_frame;
  34. unsigned int frame_index, start_frame, stop_frame;
  35. int hsub, vsub, bpp;
  36. } FadeContext;
  37. static av_cold int init(AVFilterContext *ctx, const char *args)
  38. {
  39. FadeContext *fade = ctx->priv;
  40. unsigned int nb_frames;
  41. char in_out[4];
  42. if (!args ||
  43. sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
  44. av_log(ctx, AV_LOG_ERROR,
  45. "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
  46. return AVERROR(EINVAL);
  47. }
  48. nb_frames = nb_frames ? nb_frames : 1;
  49. fade->fade_per_frame = (1 << 16) / nb_frames;
  50. if (!strcmp(in_out, "in"))
  51. fade->factor = 0;
  52. else if (!strcmp(in_out, "out")) {
  53. fade->fade_per_frame = -fade->fade_per_frame;
  54. fade->factor = (1 << 16);
  55. } else {
  56. av_log(ctx, AV_LOG_ERROR,
  57. "first argument must be 'in' or 'out':'%s'\n", in_out);
  58. return AVERROR(EINVAL);
  59. }
  60. fade->stop_frame = fade->start_frame + nb_frames;
  61. av_log(ctx, AV_LOG_VERBOSE,
  62. "type:%s start_frame:%d nb_frames:%d\n",
  63. in_out, fade->start_frame, nb_frames);
  64. return 0;
  65. }
  66. static int query_formats(AVFilterContext *ctx)
  67. {
  68. static const enum AVPixelFormat pix_fmts[] = {
  69. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  70. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  71. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  72. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  73. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  74. AV_PIX_FMT_NONE
  75. };
  76. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  77. return 0;
  78. }
  79. static int config_props(AVFilterLink *inlink)
  80. {
  81. FadeContext *fade = inlink->dst->priv;
  82. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  83. fade->hsub = pixdesc->log2_chroma_w;
  84. fade->vsub = pixdesc->log2_chroma_h;
  85. fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  86. return 0;
  87. }
  88. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  89. {
  90. FadeContext *fade = inlink->dst->priv;
  91. uint8_t *p;
  92. int i, j, plane;
  93. if (fade->factor < UINT16_MAX) {
  94. /* luma or rgb plane */
  95. for (i = 0; i < frame->video->h; i++) {
  96. p = frame->data[0] + i * frame->linesize[0];
  97. for (j = 0; j < inlink->w * fade->bpp; j++) {
  98. /* fade->factor is using 16 lower-order bits for decimal
  99. * places. 32768 = 1 << 15, it is an integer representation
  100. * of 0.5 and is for rounding. */
  101. *p = (*p * fade->factor + 32768) >> 16;
  102. p++;
  103. }
  104. }
  105. if (frame->data[1] && frame->data[2]) {
  106. /* chroma planes */
  107. for (plane = 1; plane < 3; plane++) {
  108. for (i = 0; i < frame->video->h; i++) {
  109. p = frame->data[plane] + (i >> fade->vsub) * frame->linesize[plane];
  110. for (j = 0; j < inlink->w >> fade->hsub; j++) {
  111. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  112. * representation of 128.5. The .5 is for rounding
  113. * purposes. */
  114. *p = ((*p - 128) * fade->factor + 8421367) >> 16;
  115. p++;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. if (fade->frame_index >= fade->start_frame &&
  122. fade->frame_index <= fade->stop_frame)
  123. fade->factor += fade->fade_per_frame;
  124. fade->factor = av_clip_uint16(fade->factor);
  125. fade->frame_index++;
  126. return ff_filter_frame(inlink->dst->outputs[0], frame);
  127. }
  128. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  129. {
  130. .name = "default",
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. .config_props = config_props,
  133. .get_video_buffer = ff_null_get_video_buffer,
  134. .filter_frame = filter_frame,
  135. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  136. .rej_perms = AV_PERM_PRESERVE,
  137. },
  138. { NULL }
  139. };
  140. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  141. {
  142. .name = "default",
  143. .type = AVMEDIA_TYPE_VIDEO,
  144. },
  145. { NULL }
  146. };
  147. AVFilter avfilter_vf_fade = {
  148. .name = "fade",
  149. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
  150. .init = init,
  151. .priv_size = sizeof(FadeContext),
  152. .query_formats = query_formats,
  153. .inputs = avfilter_vf_fade_inputs,
  154. .outputs = avfilter_vf_fade_outputs,
  155. };