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.

172 lines
5.9KB

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