vf_fade.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/eval.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avfilter.h"
  33. #include "drawutils.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 FadeContext {
  47. const AVClass *class;
  48. int type;
  49. int factor, fade_per_frame;
  50. int start_frame, nb_frames;
  51. int hsub, vsub, bpp;
  52. unsigned int black_level, black_level_scaled;
  53. uint8_t is_packed_rgb;
  54. uint8_t rgba_map[4];
  55. int alpha;
  56. uint64_t start_time, duration;
  57. enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
  58. uint8_t color_rgba[4]; ///< fade color
  59. int black_fade; ///< if color_rgba is black
  60. } FadeContext;
  61. static av_cold int init(AVFilterContext *ctx)
  62. {
  63. FadeContext *s = ctx->priv;
  64. s->fade_per_frame = (1 << 16) / s->nb_frames;
  65. s->fade_state = VF_FADE_WAITING;
  66. if (s->duration != 0) {
  67. // If duration (seconds) is non-zero, assume that we are not fading based on frames
  68. s->nb_frames = 0; // Mostly to clean up logging
  69. }
  70. // Choose what to log. If both time-based and frame-based options, both lines will be in the log
  71. if (s->start_frame || s->nb_frames) {
  72. av_log(ctx, AV_LOG_VERBOSE,
  73. "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
  74. s->type == FADE_IN ? "in" : "out", s->start_frame,
  75. s->nb_frames,s->alpha);
  76. }
  77. if (s->start_time || s->duration) {
  78. av_log(ctx, AV_LOG_VERBOSE,
  79. "type:%s start_time:%f duration:%f alpha:%d\n",
  80. s->type == FADE_IN ? "in" : "out", (s->start_time / (double)AV_TIME_BASE),
  81. (s->duration / (double)AV_TIME_BASE),s->alpha);
  82. }
  83. s->black_fade = !memcmp(s->color_rgba, "\x00\x00\x00\xff", 4);
  84. return 0;
  85. }
  86. static int query_formats(AVFilterContext *ctx)
  87. {
  88. const FadeContext *s = ctx->priv;
  89. static const enum AVPixelFormat pix_fmts[] = {
  90. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  91. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  92. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  93. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  94. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  95. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  96. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  97. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  98. AV_PIX_FMT_NONE
  99. };
  100. static const enum AVPixelFormat pix_fmts_rgb[] = {
  101. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  102. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  103. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  104. AV_PIX_FMT_NONE
  105. };
  106. static const enum AVPixelFormat pix_fmts_alpha[] = {
  107. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  108. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  109. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  110. AV_PIX_FMT_NONE
  111. };
  112. static const enum AVPixelFormat pix_fmts_rgba[] = {
  113. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  114. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  115. AV_PIX_FMT_NONE
  116. };
  117. AVFilterFormats *fmts_list;
  118. if (s->alpha) {
  119. if (s->black_fade)
  120. fmts_list = ff_make_format_list(pix_fmts_alpha);
  121. else
  122. fmts_list = ff_make_format_list(pix_fmts_rgba);
  123. } else {
  124. if (s->black_fade)
  125. fmts_list = ff_make_format_list(pix_fmts);
  126. else
  127. fmts_list = ff_make_format_list(pix_fmts_rgb);
  128. }
  129. if (!fmts_list)
  130. return AVERROR(ENOMEM);
  131. return ff_set_common_formats(ctx, fmts_list);
  132. }
  133. const static enum AVPixelFormat studio_level_pix_fmts[] = {
  134. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  135. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  136. AV_PIX_FMT_YUV440P,
  137. AV_PIX_FMT_NONE
  138. };
  139. static int config_props(AVFilterLink *inlink)
  140. {
  141. FadeContext *s = inlink->dst->priv;
  142. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  143. s->hsub = pixdesc->log2_chroma_w;
  144. s->vsub = pixdesc->log2_chroma_h;
  145. s->bpp = pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR ?
  146. 1 :
  147. av_get_bits_per_pixel(pixdesc) >> 3;
  148. s->alpha &= !!(pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA);
  149. s->is_packed_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  150. /* use CCIR601/709 black level for studio-level pixel non-alpha components */
  151. s->black_level =
  152. ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 : 0;
  153. /* 32768 = 1 << 15, it is an integer representation
  154. * of 0.5 and is for rounding. */
  155. s->black_level_scaled = (s->black_level << 16) + 32768;
  156. return 0;
  157. }
  158. static av_always_inline void filter_rgb(FadeContext *s, const AVFrame *frame,
  159. int slice_start, int slice_end,
  160. int do_alpha, int step)
  161. {
  162. int i, j;
  163. const uint8_t r_idx = s->rgba_map[R];
  164. const uint8_t g_idx = s->rgba_map[G];
  165. const uint8_t b_idx = s->rgba_map[B];
  166. const uint8_t a_idx = s->rgba_map[A];
  167. const uint8_t *c = s->color_rgba;
  168. for (i = slice_start; i < slice_end; i++) {
  169. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  170. for (j = 0; j < frame->width; j++) {
  171. #define INTERP(c_name, c_idx) av_clip_uint8(((c[c_idx]<<16) + ((int)p[c_name] - (int)c[c_idx]) * s->factor + (1<<15)) >> 16)
  172. p[r_idx] = INTERP(r_idx, 0);
  173. p[g_idx] = INTERP(g_idx, 1);
  174. p[b_idx] = INTERP(b_idx, 2);
  175. if (do_alpha)
  176. p[a_idx] = INTERP(a_idx, 3);
  177. p += step;
  178. }
  179. }
  180. }
  181. static int filter_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr,
  182. int nb_jobs)
  183. {
  184. FadeContext *s = ctx->priv;
  185. AVFrame *frame = arg;
  186. int slice_start = (frame->height * jobnr ) / nb_jobs;
  187. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  188. if (s->alpha) filter_rgb(s, frame, slice_start, slice_end, 1, 4);
  189. else if (s->bpp == 3) filter_rgb(s, frame, slice_start, slice_end, 0, 3);
  190. else if (s->bpp == 4) filter_rgb(s, frame, slice_start, slice_end, 0, 4);
  191. else av_assert0(0);
  192. return 0;
  193. }
  194. static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
  195. int nb_jobs)
  196. {
  197. FadeContext *s = ctx->priv;
  198. AVFrame *frame = arg;
  199. int slice_start = (frame->height * jobnr ) / nb_jobs;
  200. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  201. int i, j;
  202. for (i = slice_start; i < slice_end; i++) {
  203. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  204. for (j = 0; j < frame->width * s->bpp; j++) {
  205. /* s->factor is using 16 lower-order bits for decimal
  206. * places. 32768 = 1 << 15, it is an integer representation
  207. * of 0.5 and is for rounding. */
  208. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  209. p++;
  210. }
  211. }
  212. return 0;
  213. }
  214. static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
  215. int nb_jobs)
  216. {
  217. FadeContext *s = ctx->priv;
  218. AVFrame *frame = arg;
  219. int i, j, plane;
  220. const int width = AV_CEIL_RSHIFT(frame->width, s->hsub);
  221. const int height= AV_CEIL_RSHIFT(frame->height, s->vsub);
  222. int slice_start = (height * jobnr ) / nb_jobs;
  223. int slice_end = FFMIN(((height * (jobnr+1)) / nb_jobs), frame->height);
  224. for (plane = 1; plane < 3; plane++) {
  225. for (i = slice_start; i < slice_end; i++) {
  226. uint8_t *p = frame->data[plane] + i * frame->linesize[plane];
  227. for (j = 0; j < width; j++) {
  228. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  229. * representation of 128.5. The .5 is for rounding
  230. * purposes. */
  231. *p = ((*p - 128) * s->factor + 8421367) >> 16;
  232. p++;
  233. }
  234. }
  235. }
  236. return 0;
  237. }
  238. static int filter_slice_alpha(AVFilterContext *ctx, void *arg, int jobnr,
  239. int nb_jobs)
  240. {
  241. FadeContext *s = ctx->priv;
  242. AVFrame *frame = arg;
  243. int plane = s->is_packed_rgb ? 0 : A;
  244. int slice_start = (frame->height * jobnr ) / nb_jobs;
  245. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  246. int i, j;
  247. for (i = slice_start; i < slice_end; i++) {
  248. uint8_t *p = frame->data[plane] + i * frame->linesize[plane] + s->is_packed_rgb*s->rgba_map[A];
  249. int step = s->is_packed_rgb ? 4 : 1;
  250. for (j = 0; j < frame->width; j++) {
  251. /* s->factor is using 16 lower-order bits for decimal
  252. * places. 32768 = 1 << 15, it is an integer representation
  253. * of 0.5 and is for rounding. */
  254. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  255. p += step;
  256. }
  257. }
  258. return 0;
  259. }
  260. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  261. {
  262. AVFilterContext *ctx = inlink->dst;
  263. FadeContext *s = ctx->priv;
  264. double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
  265. // Calculate Fade assuming this is a Fade In
  266. if (s->fade_state == VF_FADE_WAITING) {
  267. s->factor=0;
  268. if (frame_timestamp >= s->start_time/(double)AV_TIME_BASE
  269. && inlink->frame_count >= s->start_frame) {
  270. // Time to start fading
  271. s->fade_state = VF_FADE_FADING;
  272. // Save start time in case we are starting based on frames and fading based on time
  273. if (s->start_time == 0 && s->start_frame != 0) {
  274. s->start_time = frame_timestamp*(double)AV_TIME_BASE;
  275. }
  276. // Save start frame in case we are starting based on time and fading based on frames
  277. if (s->start_time != 0 && s->start_frame == 0) {
  278. s->start_frame = inlink->frame_count;
  279. }
  280. }
  281. }
  282. if (s->fade_state == VF_FADE_FADING) {
  283. if (s->duration == 0) {
  284. // Fading based on frame count
  285. s->factor = (inlink->frame_count - s->start_frame) * s->fade_per_frame;
  286. if (inlink->frame_count > s->start_frame + s->nb_frames) {
  287. s->fade_state = VF_FADE_DONE;
  288. }
  289. } else {
  290. // Fading based on duration
  291. s->factor = (frame_timestamp - s->start_time/(double)AV_TIME_BASE)
  292. * (float) UINT16_MAX / (s->duration/(double)AV_TIME_BASE);
  293. if (frame_timestamp > s->start_time/(double)AV_TIME_BASE
  294. + s->duration/(double)AV_TIME_BASE) {
  295. s->fade_state = VF_FADE_DONE;
  296. }
  297. }
  298. }
  299. if (s->fade_state == VF_FADE_DONE) {
  300. s->factor=UINT16_MAX;
  301. }
  302. s->factor = av_clip_uint16(s->factor);
  303. // Invert fade_factor if Fading Out
  304. if (s->type == FADE_OUT) {
  305. s->factor=UINT16_MAX-s->factor;
  306. }
  307. if (s->factor < UINT16_MAX) {
  308. if (s->alpha) {
  309. ctx->internal->execute(ctx, filter_slice_alpha, frame, NULL,
  310. FFMIN(frame->height, ctx->graph->nb_threads));
  311. } else if (s->is_packed_rgb && !s->black_fade) {
  312. ctx->internal->execute(ctx, filter_slice_rgb, frame, NULL,
  313. FFMIN(frame->height, ctx->graph->nb_threads));
  314. } else {
  315. /* luma, or rgb plane in case of black */
  316. ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
  317. FFMIN(frame->height, ctx->graph->nb_threads));
  318. if (frame->data[1] && frame->data[2]) {
  319. /* chroma planes */
  320. ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
  321. FFMIN(frame->height, ctx->graph->nb_threads));
  322. }
  323. }
  324. }
  325. return ff_filter_frame(inlink->dst->outputs[0], frame);
  326. }
  327. #define OFFSET(x) offsetof(FadeContext, x)
  328. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  329. static const AVOption fade_options[] = {
  330. { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  331. { "t", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  332. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
  333. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
  334. { "start_frame", "Number of the first frame to which to apply the effect.",
  335. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  336. { "s", "Number of the first frame to which to apply the effect.",
  337. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  338. { "nb_frames", "Number of frames to which the effect should be applied.",
  339. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  340. { "n", "Number of frames to which the effect should be applied.",
  341. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  342. { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS },
  343. { "start_time", "Number of seconds of the beginning of the effect.",
  344. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  345. { "st", "Number of seconds of the beginning of the effect.",
  346. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  347. { "duration", "Duration of the effect in seconds.",
  348. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  349. { "d", "Duration of the effect in seconds.",
  350. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  351. { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  352. { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  353. { NULL }
  354. };
  355. AVFILTER_DEFINE_CLASS(fade);
  356. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  357. {
  358. .name = "default",
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. .config_props = config_props,
  361. .filter_frame = filter_frame,
  362. .needs_writable = 1,
  363. },
  364. { NULL }
  365. };
  366. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  367. {
  368. .name = "default",
  369. .type = AVMEDIA_TYPE_VIDEO,
  370. },
  371. { NULL }
  372. };
  373. AVFilter ff_vf_fade = {
  374. .name = "fade",
  375. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
  376. .init = init,
  377. .priv_size = sizeof(FadeContext),
  378. .priv_class = &fade_class,
  379. .query_formats = query_formats,
  380. .inputs = avfilter_vf_fade_inputs,
  381. .outputs = avfilter_vf_fade_outputs,
  382. .flags = AVFILTER_FLAG_SLICE_THREADS,
  383. };