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.

174 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 "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct {
  32. int factor, fade_per_frame;
  33. unsigned int frame_index, start_frame, stop_frame;
  34. int hsub, vsub, bpp;
  35. } FadeContext;
  36. static av_cold int init(AVFilterContext *ctx, const char *args)
  37. {
  38. FadeContext *fade = ctx->priv;
  39. unsigned int nb_frames;
  40. char in_out[4];
  41. if (!args ||
  42. sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
  43. av_log(ctx, AV_LOG_ERROR,
  44. "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
  45. return AVERROR(EINVAL);
  46. }
  47. nb_frames = nb_frames ? nb_frames : 1;
  48. fade->fade_per_frame = (1 << 16) / nb_frames;
  49. if (!strcmp(in_out, "in"))
  50. fade->factor = 0;
  51. else if (!strcmp(in_out, "out")) {
  52. fade->fade_per_frame = -fade->fade_per_frame;
  53. fade->factor = (1 << 16);
  54. } else {
  55. av_log(ctx, AV_LOG_ERROR,
  56. "first argument must be 'in' or 'out':'%s'\n", in_out);
  57. return AVERROR(EINVAL);
  58. }
  59. fade->stop_frame = fade->start_frame + nb_frames;
  60. av_log(ctx, AV_LOG_VERBOSE,
  61. "type:%s start_frame:%d nb_frames:%d\n",
  62. in_out, fade->start_frame, nb_frames);
  63. return 0;
  64. }
  65. static int query_formats(AVFilterContext *ctx)
  66. {
  67. static const enum PixelFormat pix_fmts[] = {
  68. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  69. PIX_FMT_YUV411P, PIX_FMT_YUV410P,
  70. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
  71. PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
  72. PIX_FMT_RGB24, PIX_FMT_BGR24,
  73. PIX_FMT_NONE
  74. };
  75. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  76. return 0;
  77. }
  78. static int config_props(AVFilterLink *inlink)
  79. {
  80. FadeContext *fade = inlink->dst->priv;
  81. const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
  82. fade->hsub = pixdesc->log2_chroma_w;
  83. fade->vsub = pixdesc->log2_chroma_h;
  84. fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  85. return 0;
  86. }
  87. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  88. {
  89. FadeContext *fade = inlink->dst->priv;
  90. AVFilterBufferRef *outpic = inlink->cur_buf;
  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 < h; i++) {
  96. p = outpic->data[0] + (y+i) * outpic->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 (outpic->data[1] && outpic->data[2]) {
  106. /* chroma planes */
  107. for (plane = 1; plane < 3; plane++) {
  108. for (i = 0; i < h; i++) {
  109. p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->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. ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  122. }
  123. static void end_frame(AVFilterLink *inlink)
  124. {
  125. FadeContext *fade = inlink->dst->priv;
  126. ff_end_frame(inlink->dst->outputs[0]);
  127. if (fade->frame_index >= fade->start_frame &&
  128. fade->frame_index <= fade->stop_frame)
  129. fade->factor += fade->fade_per_frame;
  130. fade->factor = av_clip_uint16(fade->factor);
  131. fade->frame_index++;
  132. }
  133. AVFilter avfilter_vf_fade = {
  134. .name = "fade",
  135. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
  136. .init = init,
  137. .priv_size = sizeof(FadeContext),
  138. .query_formats = query_formats,
  139. .inputs = (AVFilterPad[]) {{ .name = "default",
  140. .type = AVMEDIA_TYPE_VIDEO,
  141. .config_props = config_props,
  142. .get_video_buffer = ff_null_get_video_buffer,
  143. .start_frame = ff_null_start_frame,
  144. .draw_slice = draw_slice,
  145. .end_frame = end_frame,
  146. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  147. .rej_perms = AV_PERM_PRESERVE, },
  148. { .name = NULL}},
  149. .outputs = (AVFilterPad[]) {{ .name = "default",
  150. .type = AVMEDIA_TYPE_VIDEO, },
  151. { .name = NULL}},
  152. };