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.

472 lines
18KB

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