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.

224 lines
7.1KB

  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 FadeContext {
  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 *s = ctx->priv;
  46. s->fade_per_frame = (1 << 16) / s->nb_frames;
  47. if (s->type == FADE_IN) {
  48. s->factor = 0;
  49. } else if (s->type == FADE_OUT) {
  50. s->fade_per_frame = -s->fade_per_frame;
  51. s->factor = (1 << 16);
  52. }
  53. s->stop_frame = s->start_frame + s->nb_frames;
  54. av_log(ctx, AV_LOG_VERBOSE,
  55. "type:%s start_frame:%d nb_frames:%d\n",
  56. s->type == FADE_IN ? "in" : "out", s->start_frame,
  57. s->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 *s = inlink->dst->priv;
  76. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  77. s->hsub = pixdesc->log2_chroma_w;
  78. s->vsub = pixdesc->log2_chroma_h;
  79. s->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  80. return 0;
  81. }
  82. static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
  83. int nb_jobs)
  84. {
  85. FadeContext *s = ctx->priv;
  86. AVFrame *frame = arg;
  87. int slice_h = frame->height / nb_jobs;
  88. int slice_start = jobnr * slice_h;
  89. int slice_end = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h;
  90. int i, j;
  91. for (i = slice_start; i < slice_end; i++) {
  92. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  93. for (j = 0; j < frame->width * s->bpp; j++) {
  94. /* s->factor is using 16 lower-order bits for decimal
  95. * places. 32768 = 1 << 15, it is an integer representation
  96. * of 0.5 and is for rounding. */
  97. *p = (*p * s->factor + 32768) >> 16;
  98. p++;
  99. }
  100. }
  101. return 0;
  102. }
  103. static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
  104. int nb_jobs)
  105. {
  106. FadeContext *s = ctx->priv;
  107. AVFrame *frame = arg;
  108. int slice_h = FFALIGN(frame->height / nb_jobs, 1 << s->vsub);
  109. int slice_start = jobnr * slice_h;
  110. int slice_end = (jobnr == nb_jobs - 1) ? frame->height :
  111. FFMIN((jobnr + 1) * slice_h, frame->height);
  112. int i, j, plane;
  113. for (plane = 1; plane < 3; plane++) {
  114. for (i = slice_start; i < slice_end; i++) {
  115. uint8_t *p = frame->data[plane] + (i >> s->vsub) * frame->linesize[plane];
  116. for (j = 0; j < frame->width >> s->hsub; j++) {
  117. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  118. * representation of 128.5. The .5 is for rounding
  119. * purposes. */
  120. *p = ((*p - 128) * s->factor + 8421367) >> 16;
  121. p++;
  122. }
  123. }
  124. }
  125. return 0;
  126. }
  127. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  128. {
  129. AVFilterContext *ctx = inlink->dst;
  130. FadeContext *s = ctx->priv;
  131. if (s->factor < UINT16_MAX) {
  132. /* luma or rgb plane */
  133. ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
  134. FFMIN(frame->height, ctx->graph->nb_threads));
  135. if (frame->data[1] && frame->data[2]) {
  136. /* chroma planes */
  137. ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
  138. FFMIN(frame->height, ctx->graph->nb_threads));
  139. }
  140. }
  141. if (s->frame_index >= s->start_frame &&
  142. s->frame_index <= s->stop_frame)
  143. s->factor += s->fade_per_frame;
  144. s->factor = av_clip_uint16(s->factor);
  145. s->frame_index++;
  146. return ff_filter_frame(inlink->dst->outputs[0], frame);
  147. }
  148. #define OFFSET(x) offsetof(FadeContext, x)
  149. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  150. static const AVOption options[] = {
  151. { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  152. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
  153. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
  154. { "start_frame", "Number of the first frame to which to apply the effect.",
  155. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  156. { "nb_frames", "Number of frames to which the effect should be applied.",
  157. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
  158. { NULL },
  159. };
  160. static const AVClass fade_class = {
  161. .class_name = "fade",
  162. .item_name = av_default_item_name,
  163. .option = options,
  164. .version = LIBAVUTIL_VERSION_INT,
  165. };
  166. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  167. {
  168. .name = "default",
  169. .type = AVMEDIA_TYPE_VIDEO,
  170. .config_props = config_props,
  171. .get_video_buffer = ff_null_get_video_buffer,
  172. .filter_frame = filter_frame,
  173. .needs_writable = 1,
  174. },
  175. { NULL }
  176. };
  177. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  178. {
  179. .name = "default",
  180. .type = AVMEDIA_TYPE_VIDEO,
  181. },
  182. { NULL }
  183. };
  184. AVFilter ff_vf_fade = {
  185. .name = "fade",
  186. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
  187. .init = init,
  188. .priv_size = sizeof(FadeContext),
  189. .priv_class = &fade_class,
  190. .query_formats = query_formats,
  191. .inputs = avfilter_vf_fade_inputs,
  192. .outputs = avfilter_vf_fade_outputs,
  193. .flags = AVFILTER_FLAG_SLICE_THREADS,
  194. };