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.

191 lines
6.2KB

  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/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. #define FADE_IN 0
  34. #define FADE_OUT 1
  35. typedef struct {
  36. const AVClass *class;
  37. int type;
  38. int factor, fade_per_frame;
  39. int start_frame, nb_frames;
  40. unsigned int frame_index, stop_frame;
  41. int hsub, vsub, bpp;
  42. } FadeContext;
  43. static av_cold int init(AVFilterContext *ctx)
  44. {
  45. FadeContext *fade = ctx->priv;
  46. fade->fade_per_frame = (1 << 16) / fade->nb_frames;
  47. if (fade->type == FADE_IN) {
  48. fade->factor = 0;
  49. } else if (fade->type == FADE_OUT) {
  50. fade->fade_per_frame = -fade->fade_per_frame;
  51. fade->factor = (1 << 16);
  52. }
  53. fade->stop_frame = fade->start_frame + fade->nb_frames;
  54. av_log(ctx, AV_LOG_VERBOSE,
  55. "type:%s start_frame:%d nb_frames:%d\n",
  56. fade->type == FADE_IN ? "in" : "out", fade->start_frame,
  57. fade->nb_frames);
  58. return 0;
  59. }
  60. static int query_formats(AVFilterContext *ctx)
  61. {
  62. static const enum AVPixelFormat pix_fmts[] = {
  63. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  64. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  65. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  66. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  67. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  68. AV_PIX_FMT_NONE
  69. };
  70. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  71. return 0;
  72. }
  73. static int config_props(AVFilterLink *inlink)
  74. {
  75. FadeContext *fade = inlink->dst->priv;
  76. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  77. fade->hsub = pixdesc->log2_chroma_w;
  78. fade->vsub = pixdesc->log2_chroma_h;
  79. fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  80. return 0;
  81. }
  82. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  83. {
  84. FadeContext *fade = inlink->dst->priv;
  85. uint8_t *p;
  86. int i, j, plane;
  87. if (fade->factor < UINT16_MAX) {
  88. /* luma or rgb plane */
  89. for (i = 0; i < frame->height; i++) {
  90. p = frame->data[0] + i * frame->linesize[0];
  91. for (j = 0; j < inlink->w * fade->bpp; j++) {
  92. /* fade->factor is using 16 lower-order bits for decimal
  93. * places. 32768 = 1 << 15, it is an integer representation
  94. * of 0.5 and is for rounding. */
  95. *p = (*p * fade->factor + 32768) >> 16;
  96. p++;
  97. }
  98. }
  99. if (frame->data[1] && frame->data[2]) {
  100. /* chroma planes */
  101. for (plane = 1; plane < 3; plane++) {
  102. for (i = 0; i < frame->height; i++) {
  103. p = frame->data[plane] + (i >> fade->vsub) * frame->linesize[plane];
  104. for (j = 0; j < inlink->w >> fade->hsub; j++) {
  105. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  106. * representation of 128.5. The .5 is for rounding
  107. * purposes. */
  108. *p = ((*p - 128) * fade->factor + 8421367) >> 16;
  109. p++;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. if (fade->frame_index >= fade->start_frame &&
  116. fade->frame_index <= fade->stop_frame)
  117. fade->factor += fade->fade_per_frame;
  118. fade->factor = av_clip_uint16(fade->factor);
  119. fade->frame_index++;
  120. return ff_filter_frame(inlink->dst->outputs[0], frame);
  121. }
  122. #define OFFSET(x) offsetof(FadeContext, x)
  123. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  124. static const AVOption options[] = {
  125. { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  126. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
  127. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
  128. { "start_frame", "Number of the first frame to which to apply the effect.",
  129. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  130. { "nb_frames", "Number of frames to which the effect should be applied.",
  131. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
  132. { NULL },
  133. };
  134. static const AVClass fade_class = {
  135. .class_name = "fade",
  136. .item_name = av_default_item_name,
  137. .option = options,
  138. .version = LIBAVUTIL_VERSION_INT,
  139. };
  140. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  141. {
  142. .name = "default",
  143. .type = AVMEDIA_TYPE_VIDEO,
  144. .config_props = config_props,
  145. .get_video_buffer = ff_null_get_video_buffer,
  146. .filter_frame = filter_frame,
  147. .needs_writable = 1,
  148. },
  149. { NULL }
  150. };
  151. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  152. {
  153. .name = "default",
  154. .type = AVMEDIA_TYPE_VIDEO,
  155. },
  156. { NULL }
  157. };
  158. AVFilter avfilter_vf_fade = {
  159. .name = "fade",
  160. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
  161. .init = init,
  162. .priv_size = sizeof(FadeContext),
  163. .priv_class = &fade_class,
  164. .query_formats = query_formats,
  165. .inputs = avfilter_vf_fade_inputs,
  166. .outputs = avfilter_vf_fade_outputs,
  167. };