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.

318 lines
12KB

  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/avstring.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/eval.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "drawutils.h"
  33. #include "internal.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #define R 0
  38. #define G 1
  39. #define B 2
  40. #define A 3
  41. #define Y 0
  42. #define U 1
  43. #define V 2
  44. #define FADE_IN 0
  45. #define FADE_OUT 1
  46. typedef struct {
  47. const AVClass *class;
  48. int type;
  49. int factor, fade_per_frame;
  50. int start_frame, nb_frames;
  51. unsigned int frame_index;
  52. int hsub, vsub, bpp;
  53. unsigned int black_level, black_level_scaled;
  54. uint8_t is_packed_rgb;
  55. uint8_t rgba_map[4];
  56. int alpha;
  57. uint64_t start_time, duration;
  58. enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
  59. } FadeContext;
  60. static av_cold int init(AVFilterContext *ctx)
  61. {
  62. FadeContext *fade = ctx->priv;
  63. fade->fade_per_frame = (1 << 16) / fade->nb_frames;
  64. fade->fade_state = VF_FADE_WAITING;
  65. if (fade->duration != 0) {
  66. // If duration (seconds) is non-zero, assume that we are not fading based on frames
  67. fade->nb_frames = 0; // Mostly to clean up logging
  68. }
  69. // Choose what to log. If both time-based and frame-based options, both lines will be in the log
  70. if (fade->start_frame || fade->nb_frames) {
  71. av_log(ctx, AV_LOG_VERBOSE,
  72. "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
  73. fade->type == FADE_IN ? "in" : "out", fade->start_frame,
  74. fade->nb_frames,fade->alpha);
  75. }
  76. if (fade->start_time || fade->duration) {
  77. av_log(ctx, AV_LOG_VERBOSE,
  78. "type:%s start_time:%f duration:%f alpha:%d\n",
  79. fade->type == FADE_IN ? "in" : "out", (fade->start_time / (double)AV_TIME_BASE),
  80. (fade->duration / (double)AV_TIME_BASE),fade->alpha);
  81. }
  82. return 0;
  83. }
  84. static int query_formats(AVFilterContext *ctx)
  85. {
  86. static const enum AVPixelFormat pix_fmts[] = {
  87. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  88. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  89. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  90. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  91. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  92. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  93. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  94. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  95. AV_PIX_FMT_NONE
  96. };
  97. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  98. return 0;
  99. }
  100. const static enum AVPixelFormat studio_level_pix_fmts[] = {
  101. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  102. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  103. AV_PIX_FMT_YUV440P,
  104. AV_PIX_FMT_NONE
  105. };
  106. static int config_props(AVFilterLink *inlink)
  107. {
  108. FadeContext *fade = inlink->dst->priv;
  109. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  110. fade->hsub = pixdesc->log2_chroma_w;
  111. fade->vsub = pixdesc->log2_chroma_h;
  112. fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  113. fade->alpha &= pixdesc->flags & PIX_FMT_ALPHA;
  114. fade->is_packed_rgb = ff_fill_rgba_map(fade->rgba_map, inlink->format) >= 0;
  115. /* use CCIR601/709 black level for studio-level pixel non-alpha components */
  116. fade->black_level =
  117. ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !fade->alpha ? 16 : 0;
  118. /* 32768 = 1 << 15, it is an integer representation
  119. * of 0.5 and is for rounding. */
  120. fade->black_level_scaled = (fade->black_level << 16) + 32768;
  121. return 0;
  122. }
  123. static void fade_plane(int y, int h, int w,
  124. int fade_factor, int black_level, int black_level_scaled,
  125. uint8_t offset, uint8_t step, int bytes_per_plane,
  126. uint8_t *data, int line_size)
  127. {
  128. uint8_t *p;
  129. int i, j;
  130. /* luma, alpha or rgb plane */
  131. for (i = 0; i < h; i++) {
  132. p = data + offset + (y+i) * line_size;
  133. for (j = 0; j < w * bytes_per_plane; j++) {
  134. /* fade->factor is using 16 lower-order bits for decimal places. */
  135. *p = ((*p - black_level) * fade_factor + black_level_scaled) >> 16;
  136. p+=step;
  137. }
  138. }
  139. }
  140. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  141. {
  142. FadeContext *fade = inlink->dst->priv;
  143. uint8_t *p;
  144. int i, j, plane;
  145. double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
  146. // Calculate Fade assuming this is a Fade In
  147. if (fade->fade_state == VF_FADE_WAITING) {
  148. fade->factor=0;
  149. if ((frame_timestamp >= (fade->start_time/(double)AV_TIME_BASE))
  150. && (fade->frame_index >= fade->start_frame)) {
  151. // Time to start fading
  152. fade->fade_state = VF_FADE_FADING;
  153. // Save start time in case we are starting based on frames and fading based on time
  154. if ((fade->start_time == 0) && (fade->start_frame != 0)) {
  155. fade->start_time = frame_timestamp*(double)AV_TIME_BASE;
  156. }
  157. // Save start frame in case we are starting based on time and fading based on frames
  158. if ((fade->start_time != 0) && (fade->start_frame == 0)) {
  159. fade->start_frame = fade->frame_index;
  160. }
  161. }
  162. }
  163. if (fade->fade_state == VF_FADE_FADING) {
  164. if (fade->duration == 0) {
  165. // Fading based on frame count
  166. fade->factor = (fade->frame_index - fade->start_frame) * fade->fade_per_frame;
  167. if (fade->frame_index > (fade->start_frame + fade->nb_frames)) {
  168. fade->fade_state = VF_FADE_DONE;
  169. }
  170. } else {
  171. // Fading based on duration
  172. fade->factor = (frame_timestamp - (fade->start_time/(double)AV_TIME_BASE))
  173. * (float) UINT16_MAX / (fade->duration/(double)AV_TIME_BASE);
  174. if (frame_timestamp > ((fade->start_time/(double)AV_TIME_BASE)
  175. + (fade->duration/(double)AV_TIME_BASE))) {
  176. fade->fade_state = VF_FADE_DONE;
  177. }
  178. }
  179. }
  180. if (fade->fade_state == VF_FADE_DONE) {
  181. fade->factor=UINT16_MAX;
  182. }
  183. fade->factor = av_clip_uint16(fade->factor);
  184. // Invert fade_factor if Fading Out
  185. if (fade->type == 1) {
  186. fade->factor=UINT16_MAX-fade->factor;
  187. }
  188. if (fade->factor < UINT16_MAX) {
  189. if (fade->alpha) {
  190. // alpha only
  191. plane = fade->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats
  192. // or plane 3 for planar formats
  193. fade_plane(0, frame->height, inlink->w,
  194. fade->factor, fade->black_level, fade->black_level_scaled,
  195. fade->is_packed_rgb ? fade->rgba_map[A] : 0, // alpha offset for packed formats
  196. fade->is_packed_rgb ? 4 : 1, // pixstep for 8 bit packed formats
  197. 1, frame->data[plane], frame->linesize[plane]);
  198. } else {
  199. /* luma or rgb plane */
  200. fade_plane(0, frame->height, inlink->w,
  201. fade->factor, fade->black_level, fade->black_level_scaled,
  202. 0, 1, // offset & pixstep for Y plane or RGB packed format
  203. fade->bpp, frame->data[0], frame->linesize[0]);
  204. if (frame->data[1] && frame->data[2]) {
  205. /* chroma planes */
  206. for (plane = 1; plane < 3; plane++) {
  207. for (i = 0; i < frame->height; i++) {
  208. const int width = FF_CEIL_RSHIFT(inlink->w, fade->hsub);
  209. p = frame->data[plane] + (i >> fade->vsub) * frame->linesize[plane];
  210. for (j = 0; j < width; j++) {
  211. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  212. * representation of 128.5. The .5 is for rounding
  213. * purposes. */
  214. *p = ((*p - 128) * fade->factor + 8421367) >> 16;
  215. p++;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. fade->frame_index++;
  223. return ff_filter_frame(inlink->dst->outputs[0], frame);
  224. }
  225. #define OFFSET(x) offsetof(FadeContext, x)
  226. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  227. static const AVOption fade_options[] = {
  228. { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  229. { "t", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  230. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
  231. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
  232. { "start_frame", "Number of the first frame to which to apply the effect.",
  233. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  234. { "s", "Number of the first frame to which to apply the effect.",
  235. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  236. { "nb_frames", "Number of frames to which the effect should be applied.",
  237. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  238. { "n", "Number of frames to which the effect should be applied.",
  239. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  240. { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS },
  241. { "start_time", "Number of seconds of the beginning of the effect.",
  242. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  243. { "st", "Number of seconds of the beginning of the effect.",
  244. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  245. { "duration", "Duration of the effect in seconds.",
  246. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  247. { "d", "Duration of the effect in seconds.",
  248. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  249. { NULL },
  250. };
  251. AVFILTER_DEFINE_CLASS(fade);
  252. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  253. {
  254. .name = "default",
  255. .type = AVMEDIA_TYPE_VIDEO,
  256. .config_props = config_props,
  257. .get_video_buffer = ff_null_get_video_buffer,
  258. .filter_frame = filter_frame,
  259. .needs_writable = 1,
  260. },
  261. { NULL }
  262. };
  263. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  264. {
  265. .name = "default",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. },
  268. { NULL }
  269. };
  270. AVFilter avfilter_vf_fade = {
  271. .name = "fade",
  272. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
  273. .init = init,
  274. .priv_size = sizeof(FadeContext),
  275. .priv_class = &fade_class,
  276. .query_formats = query_formats,
  277. .inputs = avfilter_vf_fade_inputs,
  278. .outputs = avfilter_vf_fade_outputs,
  279. };