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.

583 lines
23KB

  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, depth;
  52. unsigned int black_level, black_level_scaled;
  53. uint8_t is_rgb;
  54. uint8_t is_packed_rgb;
  55. uint8_t rgba_map[4];
  56. int alpha;
  57. int is_planar;
  58. uint64_t start_time, duration;
  59. uint64_t start_time_pts, duration_pts;
  60. enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
  61. uint8_t color_rgba[4]; ///< fade color
  62. int black_fade; ///< if color_rgba is black
  63. int (*filter_slice_luma)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  64. int (*filter_slice_chroma)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  65. int (*filter_slice_alpha)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  66. } FadeContext;
  67. static av_cold int init(AVFilterContext *ctx)
  68. {
  69. FadeContext *s = ctx->priv;
  70. s->fade_per_frame = (1 << 16) / s->nb_frames;
  71. s->fade_state = VF_FADE_WAITING;
  72. if (s->duration != 0) {
  73. // If duration (seconds) is non-zero, assume that we are not fading based on frames
  74. s->nb_frames = 0; // Mostly to clean up logging
  75. }
  76. // Choose what to log. If both time-based and frame-based options, both lines will be in the log
  77. if (s->start_frame || s->nb_frames) {
  78. av_log(ctx, AV_LOG_VERBOSE,
  79. "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
  80. s->type == FADE_IN ? "in" : "out", s->start_frame,
  81. s->nb_frames,s->alpha);
  82. }
  83. if (s->start_time || s->duration) {
  84. av_log(ctx, AV_LOG_VERBOSE,
  85. "type:%s start_time:%f duration:%f alpha:%d\n",
  86. s->type == FADE_IN ? "in" : "out", (s->start_time / (double)AV_TIME_BASE),
  87. (s->duration / (double)AV_TIME_BASE),s->alpha);
  88. }
  89. s->black_fade = !memcmp(s->color_rgba, "\x00\x00\x00\xff", 4);
  90. return 0;
  91. }
  92. static int query_formats(AVFilterContext *ctx)
  93. {
  94. const FadeContext *s = ctx->priv;
  95. static const enum AVPixelFormat pix_fmts[] = {
  96. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  97. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  98. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  99. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  100. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  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_GBRP, AV_PIX_FMT_GBRAP,
  105. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  106. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  107. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  108. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  109. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  110. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  111. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  112. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  113. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  114. AV_PIX_FMT_NONE
  115. };
  116. static const enum AVPixelFormat pix_fmts_rgb[] = {
  117. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  118. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  119. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  120. AV_PIX_FMT_GBRP,
  121. AV_PIX_FMT_NONE
  122. };
  123. static const enum AVPixelFormat pix_fmts_alpha[] = {
  124. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  125. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  126. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  127. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  128. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  129. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  130. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  131. AV_PIX_FMT_GBRAP,
  132. AV_PIX_FMT_NONE
  133. };
  134. static const enum AVPixelFormat pix_fmts_rgba[] = {
  135. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  136. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  137. AV_PIX_FMT_GBRAP,
  138. AV_PIX_FMT_NONE
  139. };
  140. AVFilterFormats *fmts_list;
  141. if (s->alpha) {
  142. if (s->black_fade)
  143. fmts_list = ff_make_format_list(pix_fmts_alpha);
  144. else
  145. fmts_list = ff_make_format_list(pix_fmts_rgba);
  146. } else {
  147. if (s->black_fade)
  148. fmts_list = ff_make_format_list(pix_fmts);
  149. else
  150. fmts_list = ff_make_format_list(pix_fmts_rgb);
  151. }
  152. if (!fmts_list)
  153. return AVERROR(ENOMEM);
  154. return ff_set_common_formats(ctx, fmts_list);
  155. }
  156. const static enum AVPixelFormat studio_level_pix_fmts[] = {
  157. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  158. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  159. AV_PIX_FMT_YUV440P,
  160. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  161. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  162. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  163. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  164. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  165. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  166. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  167. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  168. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  169. AV_PIX_FMT_NONE
  170. };
  171. static av_always_inline void filter_rgb(FadeContext *s, const AVFrame *frame,
  172. int slice_start, int slice_end,
  173. int do_alpha, int step)
  174. {
  175. int i, j;
  176. const uint8_t r_idx = s->rgba_map[R];
  177. const uint8_t g_idx = s->rgba_map[G];
  178. const uint8_t b_idx = s->rgba_map[B];
  179. const uint8_t a_idx = s->rgba_map[A];
  180. const uint8_t *c = s->color_rgba;
  181. for (i = slice_start; i < slice_end; i++) {
  182. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  183. for (j = 0; j < frame->width; j++) {
  184. #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)
  185. p[r_idx] = INTERP(r_idx, 0);
  186. p[g_idx] = INTERP(g_idx, 1);
  187. p[b_idx] = INTERP(b_idx, 2);
  188. if (do_alpha)
  189. p[a_idx] = INTERP(a_idx, 3);
  190. p += step;
  191. }
  192. }
  193. }
  194. static av_always_inline void filter_rgb_planar(FadeContext *s, const AVFrame *frame,
  195. int slice_start, int slice_end,
  196. int do_alpha)
  197. {
  198. int i, j;
  199. const uint8_t *c = s->color_rgba;
  200. for (i = slice_start; i < slice_end; i++) {
  201. uint8_t *pg = frame->data[0] + i * frame->linesize[0];
  202. uint8_t *pb = frame->data[1] + i * frame->linesize[1];
  203. uint8_t *pr = frame->data[2] + i * frame->linesize[2];
  204. uint8_t *pa = frame->data[3] + i * frame->linesize[3];
  205. for (j = 0; j < frame->width; j++) {
  206. #define INTERPP(c_name, c_idx) av_clip_uint8(((c[c_idx]<<16) + ((int)c_name - (int)c[c_idx]) * s->factor + (1<<15)) >> 16)
  207. pr[j] = INTERPP(pr[j], 0);
  208. pg[j] = INTERPP(pg[j], 1);
  209. pb[j] = INTERPP(pb[j], 2);
  210. if (do_alpha)
  211. pa[j] = INTERPP(pa[j], 3);
  212. }
  213. }
  214. }
  215. static int filter_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr,
  216. int nb_jobs)
  217. {
  218. FadeContext *s = ctx->priv;
  219. AVFrame *frame = arg;
  220. int slice_start = (frame->height * jobnr ) / nb_jobs;
  221. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  222. if (s->is_planar && s->alpha)
  223. filter_rgb_planar(s, frame, slice_start, slice_end, 1);
  224. else if (s->is_planar)
  225. filter_rgb_planar(s, frame, slice_start, slice_end, 0);
  226. else if (s->alpha) filter_rgb(s, frame, slice_start, slice_end, 1, 4);
  227. else if (s->bpp == 3) filter_rgb(s, frame, slice_start, slice_end, 0, 3);
  228. else if (s->bpp == 4) filter_rgb(s, frame, slice_start, slice_end, 0, 4);
  229. else av_assert0(0);
  230. return 0;
  231. }
  232. static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
  233. int nb_jobs)
  234. {
  235. FadeContext *s = ctx->priv;
  236. AVFrame *frame = arg;
  237. int slice_start = (frame->height * jobnr ) / nb_jobs;
  238. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  239. int i, j;
  240. for (int k = 0; k < 1 + 2 * (s->is_planar && s->is_rgb); k++) {
  241. for (i = slice_start; i < slice_end; i++) {
  242. uint8_t *p = frame->data[k] + i * frame->linesize[k];
  243. for (j = 0; j < frame->width * s->bpp; j++) {
  244. /* s->factor is using 16 lower-order bits for decimal
  245. * places. 32768 = 1 << 15, it is an integer representation
  246. * of 0.5 and is for rounding. */
  247. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  248. p++;
  249. }
  250. }
  251. }
  252. return 0;
  253. }
  254. static int filter_slice_luma16(AVFilterContext *ctx, void *arg, int jobnr,
  255. int nb_jobs)
  256. {
  257. FadeContext *s = ctx->priv;
  258. AVFrame *frame = arg;
  259. int slice_start = (frame->height * jobnr ) / nb_jobs;
  260. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  261. int i, j;
  262. for (int k = 0; k < 1 + 2 * (s->is_planar && s->is_rgb); k++) {
  263. for (i = slice_start; i < slice_end; i++) {
  264. uint16_t *p = (uint16_t *)(frame->data[k] + i * frame->linesize[k]);
  265. for (j = 0; j < frame->width * s->bpp; j++) {
  266. /* s->factor is using 16 lower-order bits for decimal
  267. * places. 32768 = 1 << 15, it is an integer representation
  268. * of 0.5 and is for rounding. */
  269. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  270. p++;
  271. }
  272. }
  273. }
  274. return 0;
  275. }
  276. static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
  277. int nb_jobs)
  278. {
  279. FadeContext *s = ctx->priv;
  280. AVFrame *frame = arg;
  281. int i, j, plane;
  282. const int width = AV_CEIL_RSHIFT(frame->width, s->hsub);
  283. const int height= AV_CEIL_RSHIFT(frame->height, s->vsub);
  284. int slice_start = (height * jobnr ) / nb_jobs;
  285. int slice_end = FFMIN(((height * (jobnr+1)) / nb_jobs), frame->height);
  286. for (plane = 1; plane < 3; plane++) {
  287. for (i = slice_start; i < slice_end; i++) {
  288. uint8_t *p = frame->data[plane] + i * frame->linesize[plane];
  289. for (j = 0; j < width; j++) {
  290. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  291. * representation of 128.5. The .5 is for rounding
  292. * purposes. */
  293. *p = ((*p - 128) * s->factor + 8421367) >> 16;
  294. p++;
  295. }
  296. }
  297. }
  298. return 0;
  299. }
  300. static int filter_slice_chroma16(AVFilterContext *ctx, void *arg, int jobnr,
  301. int nb_jobs)
  302. {
  303. FadeContext *s = ctx->priv;
  304. AVFrame *frame = arg;
  305. int i, j, plane;
  306. const int width = AV_CEIL_RSHIFT(frame->width, s->hsub);
  307. const int height= AV_CEIL_RSHIFT(frame->height, s->vsub);
  308. const int mid = 1 << (s->depth - 1);
  309. const int add = ((mid << 1) + 1) << 15;
  310. int slice_start = (height * jobnr ) / nb_jobs;
  311. int slice_end = FFMIN(((height * (jobnr+1)) / nb_jobs), frame->height);
  312. for (plane = 1; plane < 3; plane++) {
  313. for (i = slice_start; i < slice_end; i++) {
  314. uint16_t *p = (uint16_t *)(frame->data[plane] + i * frame->linesize[plane]);
  315. for (j = 0; j < width; j++) {
  316. *p = ((*p - mid) * s->factor + add) >> 16;
  317. p++;
  318. }
  319. }
  320. }
  321. return 0;
  322. }
  323. static int filter_slice_alpha(AVFilterContext *ctx, void *arg, int jobnr,
  324. int nb_jobs)
  325. {
  326. FadeContext *s = ctx->priv;
  327. AVFrame *frame = arg;
  328. int plane = s->is_packed_rgb ? 0 : A;
  329. int slice_start = (frame->height * jobnr ) / nb_jobs;
  330. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  331. int i, j;
  332. for (i = slice_start; i < slice_end; i++) {
  333. uint8_t *p = frame->data[plane] + i * frame->linesize[plane] + s->is_packed_rgb*s->rgba_map[A];
  334. int step = s->is_packed_rgb ? 4 : 1;
  335. for (j = 0; j < frame->width; j++) {
  336. /* s->factor is using 16 lower-order bits for decimal
  337. * places. 32768 = 1 << 15, it is an integer representation
  338. * of 0.5 and is for rounding. */
  339. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  340. p += step;
  341. }
  342. }
  343. return 0;
  344. }
  345. static int filter_slice_alpha16(AVFilterContext *ctx, void *arg, int jobnr,
  346. int nb_jobs)
  347. {
  348. FadeContext *s = ctx->priv;
  349. AVFrame *frame = arg;
  350. int plane = s->is_packed_rgb ? 0 : A;
  351. int slice_start = (frame->height * jobnr ) / nb_jobs;
  352. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  353. int i, j;
  354. for (i = slice_start; i < slice_end; i++) {
  355. uint16_t *p = (uint16_t *)(frame->data[plane] + i * frame->linesize[plane]) + s->is_packed_rgb*s->rgba_map[A];
  356. int step = s->is_packed_rgb ? 4 : 1;
  357. for (j = 0; j < frame->width; j++) {
  358. /* s->factor is using 16 lower-order bits for decimal
  359. * places. 32768 = 1 << 15, it is an integer representation
  360. * of 0.5 and is for rounding. */
  361. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  362. p += step;
  363. }
  364. }
  365. return 0;
  366. }
  367. static int config_input(AVFilterLink *inlink)
  368. {
  369. FadeContext *s = inlink->dst->priv;
  370. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  371. s->hsub = pixdesc->log2_chroma_w;
  372. s->vsub = pixdesc->log2_chroma_h;
  373. ff_fill_rgba_map(s->rgba_map, inlink->format);
  374. s->depth = pixdesc->comp[0].depth;
  375. s->bpp = pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR ?
  376. 1 :
  377. av_get_bits_per_pixel(pixdesc) >> 3;
  378. s->alpha &= !!(pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA);
  379. s->is_planar = pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR;
  380. s->is_rgb = pixdesc->flags & AV_PIX_FMT_FLAG_RGB;
  381. s->is_packed_rgb = !s->is_planar && s->is_rgb;
  382. if (s->duration)
  383. s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, inlink->time_base);
  384. if (s->start_time)
  385. s->start_time_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, inlink->time_base);
  386. /* use CCIR601/709 black level for studio-level pixel non-alpha components */
  387. s->black_level =
  388. ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 * (1 << (s->depth - 8)): 0;
  389. /* 32768 = 1 << 15, it is an integer representation
  390. * of 0.5 and is for rounding. */
  391. s->black_level_scaled = (s->black_level << 16) + 32768;
  392. s->filter_slice_luma = s->depth <= 8 ? filter_slice_luma : filter_slice_luma16;
  393. s->filter_slice_chroma = s->depth <= 8 ? filter_slice_chroma : filter_slice_chroma16;
  394. s->filter_slice_alpha = s->depth <= 8 ? filter_slice_alpha : filter_slice_alpha16;
  395. return 0;
  396. }
  397. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  398. {
  399. AVFilterContext *ctx = inlink->dst;
  400. FadeContext *s = ctx->priv;
  401. // Calculate Fade assuming this is a Fade In
  402. if (s->fade_state == VF_FADE_WAITING) {
  403. s->factor=0;
  404. if (frame->pts >= s->start_time_pts
  405. && inlink->frame_count_out >= s->start_frame) {
  406. // Time to start fading
  407. s->fade_state = VF_FADE_FADING;
  408. // Save start time in case we are starting based on frames and fading based on time
  409. if (s->start_time_pts == 0 && s->start_frame != 0) {
  410. s->start_time_pts = frame->pts;
  411. }
  412. // Save start frame in case we are starting based on time and fading based on frames
  413. if (s->start_time_pts != 0 && s->start_frame == 0) {
  414. s->start_frame = inlink->frame_count_out;
  415. }
  416. }
  417. }
  418. if (s->fade_state == VF_FADE_FADING) {
  419. if (s->duration_pts == 0) {
  420. // Fading based on frame count
  421. s->factor = (inlink->frame_count_out - s->start_frame) * s->fade_per_frame;
  422. if (inlink->frame_count_out > s->start_frame + s->nb_frames) {
  423. s->fade_state = VF_FADE_DONE;
  424. }
  425. } else {
  426. // Fading based on duration
  427. s->factor = (frame->pts - s->start_time_pts) * UINT16_MAX / s->duration_pts;
  428. if (frame->pts > s->start_time_pts + s->duration_pts) {
  429. s->fade_state = VF_FADE_DONE;
  430. }
  431. }
  432. }
  433. if (s->fade_state == VF_FADE_DONE) {
  434. s->factor=UINT16_MAX;
  435. }
  436. s->factor = av_clip_uint16(s->factor);
  437. // Invert fade_factor if Fading Out
  438. if (s->type == FADE_OUT) {
  439. s->factor=UINT16_MAX-s->factor;
  440. }
  441. if (s->factor < UINT16_MAX) {
  442. if (s->alpha) {
  443. ctx->internal->execute(ctx, s->filter_slice_alpha, frame, NULL,
  444. FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
  445. } else if (s->is_rgb && !s->black_fade) {
  446. ctx->internal->execute(ctx, filter_slice_rgb, frame, NULL,
  447. FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
  448. } else {
  449. /* luma, or rgb plane in case of black */
  450. ctx->internal->execute(ctx, s->filter_slice_luma, frame, NULL,
  451. FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
  452. if (frame->data[1] && frame->data[2] && !s->is_rgb) {
  453. /* chroma planes */
  454. ctx->internal->execute(ctx, s->filter_slice_chroma, frame, NULL,
  455. FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
  456. }
  457. }
  458. }
  459. return ff_filter_frame(inlink->dst->outputs[0], frame);
  460. }
  461. #define OFFSET(x) offsetof(FadeContext, x)
  462. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  463. static const AVOption fade_options[] = {
  464. { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  465. { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  466. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .flags = FLAGS, .unit = "type" },
  467. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .flags = FLAGS, .unit = "type" },
  468. { "start_frame", "Number of the first frame to which to apply the effect.",
  469. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  470. { "s", "Number of the first frame to which to apply the effect.",
  471. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  472. { "nb_frames", "Number of frames to which the effect should be applied.",
  473. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 1, INT_MAX, FLAGS },
  474. { "n", "Number of frames to which the effect should be applied.",
  475. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 1, INT_MAX, FLAGS },
  476. { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS },
  477. { "start_time", "Number of seconds of the beginning of the effect.",
  478. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
  479. { "st", "Number of seconds of the beginning of the effect.",
  480. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
  481. { "duration", "Duration of the effect in seconds.",
  482. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
  483. { "d", "Duration of the effect in seconds.",
  484. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
  485. { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, 0, 0, FLAGS },
  486. { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, 0, 0, FLAGS },
  487. { NULL }
  488. };
  489. AVFILTER_DEFINE_CLASS(fade);
  490. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  491. {
  492. .name = "default",
  493. .type = AVMEDIA_TYPE_VIDEO,
  494. .config_props = config_input,
  495. .filter_frame = filter_frame,
  496. .needs_writable = 1,
  497. },
  498. { NULL }
  499. };
  500. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  501. {
  502. .name = "default",
  503. .type = AVMEDIA_TYPE_VIDEO,
  504. },
  505. { NULL }
  506. };
  507. AVFilter ff_vf_fade = {
  508. .name = "fade",
  509. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
  510. .init = init,
  511. .priv_size = sizeof(FadeContext),
  512. .priv_class = &fade_class,
  513. .query_formats = query_formats,
  514. .inputs = avfilter_vf_fade_inputs,
  515. .outputs = avfilter_vf_fade_outputs,
  516. .flags = AVFILTER_FLAG_SLICE_THREADS |
  517. AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  518. };