vf_fade.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2010 Brandon Mintern
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/pixdesc.h"
  27. #include "avfilter.h"
  28. typedef struct {
  29. int factor, fade_per_frame;
  30. unsigned int frame_index, start_frame, stop_frame;
  31. int hsub, vsub, bpp;
  32. } FadeContext;
  33. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  34. {
  35. FadeContext *fade = ctx->priv;
  36. unsigned int nb_frames;
  37. char in_out[4];
  38. if (!args ||
  39. sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
  40. av_log(ctx, AV_LOG_ERROR,
  41. "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
  42. return AVERROR(EINVAL);
  43. }
  44. nb_frames = nb_frames ? nb_frames : 1;
  45. fade->fade_per_frame = (1 << 16) / nb_frames;
  46. if (!strcmp(in_out, "in"))
  47. fade->factor = 0;
  48. else if (!strcmp(in_out, "out")) {
  49. fade->fade_per_frame = -fade->fade_per_frame;
  50. fade->factor = (1 << 16);
  51. } else {
  52. av_log(ctx, AV_LOG_ERROR,
  53. "first argument must be 'in' or 'out':'%s'\n", in_out);
  54. return AVERROR(EINVAL);
  55. }
  56. fade->stop_frame = fade->start_frame + nb_frames;
  57. av_log(ctx, AV_LOG_INFO,
  58. "type:%s start_frame:%d nb_frames:%d\n",
  59. in_out, fade->start_frame, nb_frames);
  60. return 0;
  61. }
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. const static enum PixelFormat pix_fmts[] = {
  65. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  66. PIX_FMT_YUV411P, PIX_FMT_YUV410P,
  67. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
  68. PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
  69. PIX_FMT_RGB24, PIX_FMT_BGR24,
  70. PIX_FMT_NONE
  71. };
  72. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  73. return 0;
  74. }
  75. static int config_props(AVFilterLink *inlink)
  76. {
  77. FadeContext *fade = inlink->dst->priv;
  78. const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
  79. fade->hsub = pixdesc->log2_chroma_w;
  80. fade->vsub = pixdesc->log2_chroma_h;
  81. fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  82. return 0;
  83. }
  84. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  85. {
  86. FadeContext *fade = inlink->dst->priv;
  87. AVFilterBufferRef *outpic = inlink->cur_buf;
  88. uint8_t *p;
  89. int i, j, plane;
  90. if (fade->factor < UINT16_MAX) {
  91. /* luma or rgb plane */
  92. for (i = 0; i < h; i++) {
  93. p = outpic->data[0] + (y+i) * outpic->linesize[0];
  94. for (j = 0; j < inlink->w * fade->bpp; j++) {
  95. /* fade->factor is using 16 lower-order bits for decimal
  96. * places. 32768 = 1 << 15, it is an integer representation
  97. * of 0.5 and is for rounding. */
  98. *p = (*p * fade->factor + 32768) >> 16;
  99. p++;
  100. }
  101. }
  102. if (outpic->data[1] && outpic->data[2]) {
  103. /* chroma planes */
  104. for (plane = 1; plane < 3; plane++) {
  105. for (i = 0; i < h; i++) {
  106. p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
  107. for (j = 0; j < inlink->w >> fade->hsub; j++) {
  108. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  109. * representation of 128.5. The .5 is for rounding
  110. * purposes. */
  111. *p = ((*p - 128) * fade->factor + 8421367) >> 16;
  112. p++;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  119. }
  120. static void end_frame(AVFilterLink *inlink)
  121. {
  122. FadeContext *fade = inlink->dst->priv;
  123. avfilter_end_frame(inlink->dst->outputs[0]);
  124. if (fade->frame_index >= fade->start_frame &&
  125. fade->frame_index <= fade->stop_frame)
  126. fade->factor += fade->fade_per_frame;
  127. fade->factor = av_clip_uint16(fade->factor);
  128. fade->frame_index++;
  129. }
  130. AVFilter avfilter_vf_fade = {
  131. .name = "fade",
  132. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
  133. .init = init,
  134. .priv_size = sizeof(FadeContext),
  135. .query_formats = query_formats,
  136. .inputs = (AVFilterPad[]) {{ .name = "default",
  137. .type = AVMEDIA_TYPE_VIDEO,
  138. .config_props = config_props,
  139. .get_video_buffer = avfilter_null_get_video_buffer,
  140. .start_frame = avfilter_null_start_frame,
  141. .draw_slice = draw_slice,
  142. .end_frame = end_frame,
  143. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  144. .rej_perms = AV_PERM_PRESERVE, },
  145. { .name = NULL}},
  146. .outputs = (AVFilterPad[]) {{ .name = "default",
  147. .type = AVMEDIA_TYPE_VIDEO, },
  148. { .name = NULL}},
  149. };