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.

415 lines
16KB

  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. AVFilterFormats *fmts_list;
  107. if (s->black_fade)
  108. fmts_list = ff_make_format_list(pix_fmts);
  109. else
  110. fmts_list = ff_make_format_list(pix_fmts_rgb);
  111. if (!fmts_list)
  112. return AVERROR(ENOMEM);
  113. return ff_set_common_formats(ctx, fmts_list);
  114. }
  115. const static enum AVPixelFormat studio_level_pix_fmts[] = {
  116. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  117. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  118. AV_PIX_FMT_YUV440P,
  119. AV_PIX_FMT_NONE
  120. };
  121. static int config_props(AVFilterLink *inlink)
  122. {
  123. FadeContext *s = inlink->dst->priv;
  124. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  125. s->hsub = pixdesc->log2_chroma_w;
  126. s->vsub = pixdesc->log2_chroma_h;
  127. s->bpp = pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR ?
  128. 1 :
  129. av_get_bits_per_pixel(pixdesc) >> 3;
  130. s->alpha &= !!(pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA);
  131. s->is_packed_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  132. /* use CCIR601/709 black level for studio-level pixel non-alpha components */
  133. s->black_level =
  134. ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 : 0;
  135. /* 32768 = 1 << 15, it is an integer representation
  136. * of 0.5 and is for rounding. */
  137. s->black_level_scaled = (s->black_level << 16) + 32768;
  138. return 0;
  139. }
  140. static av_always_inline void filter_rgb(FadeContext *s, const AVFrame *frame,
  141. int slice_start, int slice_end,
  142. int do_alpha, int step)
  143. {
  144. int i, j;
  145. const uint8_t r_idx = s->rgba_map[R];
  146. const uint8_t g_idx = s->rgba_map[G];
  147. const uint8_t b_idx = s->rgba_map[B];
  148. const uint8_t a_idx = s->rgba_map[A];
  149. const uint8_t *c = s->color_rgba;
  150. for (i = slice_start; i < slice_end; i++) {
  151. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  152. for (j = 0; j < frame->width; j++) {
  153. #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)
  154. p[r_idx] = INTERP(r_idx, 0);
  155. p[g_idx] = INTERP(g_idx, 1);
  156. p[b_idx] = INTERP(b_idx, 2);
  157. if (do_alpha)
  158. p[a_idx] = INTERP(a_idx, 3);
  159. p += step;
  160. }
  161. }
  162. }
  163. static int filter_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr,
  164. int nb_jobs)
  165. {
  166. FadeContext *s = ctx->priv;
  167. AVFrame *frame = arg;
  168. int slice_start = (frame->height * jobnr ) / nb_jobs;
  169. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  170. if (s->alpha) filter_rgb(s, frame, slice_start, slice_end, 1, 4);
  171. else if (s->bpp == 3) filter_rgb(s, frame, slice_start, slice_end, 0, 3);
  172. else if (s->bpp == 4) filter_rgb(s, frame, slice_start, slice_end, 0, 4);
  173. else av_assert0(0);
  174. return 0;
  175. }
  176. static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
  177. int nb_jobs)
  178. {
  179. FadeContext *s = ctx->priv;
  180. AVFrame *frame = arg;
  181. int slice_start = (frame->height * jobnr ) / nb_jobs;
  182. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  183. int i, j;
  184. for (i = slice_start; i < slice_end; i++) {
  185. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  186. for (j = 0; j < frame->width * s->bpp; j++) {
  187. /* s->factor is using 16 lower-order bits for decimal
  188. * places. 32768 = 1 << 15, it is an integer representation
  189. * of 0.5 and is for rounding. */
  190. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  191. p++;
  192. }
  193. }
  194. return 0;
  195. }
  196. static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
  197. int nb_jobs)
  198. {
  199. FadeContext *s = ctx->priv;
  200. AVFrame *frame = arg;
  201. int i, j, plane;
  202. const int width = FF_CEIL_RSHIFT(frame->width, s->hsub);
  203. const int height= FF_CEIL_RSHIFT(frame->height, s->vsub);
  204. int slice_start = (height * jobnr ) / nb_jobs;
  205. int slice_end = (height * (jobnr+1)) / nb_jobs;
  206. for (plane = 1; plane < 3; plane++) {
  207. for (i = slice_start; i < slice_end; i++) {
  208. uint8_t *p = frame->data[plane] + i * frame->linesize[plane];
  209. for (j = 0; j < width; j++) {
  210. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  211. * representation of 128.5. The .5 is for rounding
  212. * purposes. */
  213. *p = ((*p - 128) * s->factor + 8421367) >> 16;
  214. p++;
  215. }
  216. }
  217. }
  218. return 0;
  219. }
  220. static int filter_slice_alpha(AVFilterContext *ctx, void *arg, int jobnr,
  221. int nb_jobs)
  222. {
  223. FadeContext *s = ctx->priv;
  224. AVFrame *frame = arg;
  225. int plane = s->is_packed_rgb ? 0 : A;
  226. int slice_start = (frame->height * jobnr ) / nb_jobs;
  227. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  228. int i, j;
  229. for (i = slice_start; i < slice_end; i++) {
  230. uint8_t *p = frame->data[plane] + i * frame->linesize[plane] + s->is_packed_rgb*s->rgba_map[A];
  231. int step = s->is_packed_rgb ? 4 : 1;
  232. for (j = 0; j < frame->width; j++) {
  233. /* s->factor is using 16 lower-order bits for decimal
  234. * places. 32768 = 1 << 15, it is an integer representation
  235. * of 0.5 and is for rounding. */
  236. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  237. p += step;
  238. }
  239. }
  240. return 0;
  241. }
  242. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  243. {
  244. AVFilterContext *ctx = inlink->dst;
  245. FadeContext *s = ctx->priv;
  246. double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
  247. // Calculate Fade assuming this is a Fade In
  248. if (s->fade_state == VF_FADE_WAITING) {
  249. s->factor=0;
  250. if (frame_timestamp >= s->start_time/(double)AV_TIME_BASE
  251. && inlink->frame_count >= s->start_frame) {
  252. // Time to start fading
  253. s->fade_state = VF_FADE_FADING;
  254. // Save start time in case we are starting based on frames and fading based on time
  255. if (s->start_time == 0 && s->start_frame != 0) {
  256. s->start_time = frame_timestamp*(double)AV_TIME_BASE;
  257. }
  258. // Save start frame in case we are starting based on time and fading based on frames
  259. if (s->start_time != 0 && s->start_frame == 0) {
  260. s->start_frame = inlink->frame_count;
  261. }
  262. }
  263. }
  264. if (s->fade_state == VF_FADE_FADING) {
  265. if (s->duration == 0) {
  266. // Fading based on frame count
  267. s->factor = (inlink->frame_count - s->start_frame) * s->fade_per_frame;
  268. if (inlink->frame_count > s->start_frame + s->nb_frames) {
  269. s->fade_state = VF_FADE_DONE;
  270. }
  271. } else {
  272. // Fading based on duration
  273. s->factor = (frame_timestamp - s->start_time/(double)AV_TIME_BASE)
  274. * (float) UINT16_MAX / (s->duration/(double)AV_TIME_BASE);
  275. if (frame_timestamp > s->start_time/(double)AV_TIME_BASE
  276. + s->duration/(double)AV_TIME_BASE) {
  277. s->fade_state = VF_FADE_DONE;
  278. }
  279. }
  280. }
  281. if (s->fade_state == VF_FADE_DONE) {
  282. s->factor=UINT16_MAX;
  283. }
  284. s->factor = av_clip_uint16(s->factor);
  285. // Invert fade_factor if Fading Out
  286. if (s->type == FADE_OUT) {
  287. s->factor=UINT16_MAX-s->factor;
  288. }
  289. if (s->factor < UINT16_MAX) {
  290. if (s->alpha) {
  291. ctx->internal->execute(ctx, filter_slice_alpha, frame, NULL,
  292. FFMIN(frame->height, ctx->graph->nb_threads));
  293. } else if (s->is_packed_rgb && !s->black_fade) {
  294. ctx->internal->execute(ctx, filter_slice_rgb, frame, NULL,
  295. FFMIN(frame->height, ctx->graph->nb_threads));
  296. } else {
  297. /* luma, or rgb plane in case of black */
  298. ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
  299. FFMIN(frame->height, ctx->graph->nb_threads));
  300. if (frame->data[1] && frame->data[2]) {
  301. /* chroma planes */
  302. ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
  303. FFMIN(frame->height, ctx->graph->nb_threads));
  304. }
  305. }
  306. }
  307. return ff_filter_frame(inlink->dst->outputs[0], frame);
  308. }
  309. #define OFFSET(x) offsetof(FadeContext, x)
  310. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  311. static const AVOption fade_options[] = {
  312. { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  313. { "t", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  314. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
  315. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
  316. { "start_frame", "Number of the first frame to which to apply the effect.",
  317. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  318. { "s", "Number of the first frame to which to apply the effect.",
  319. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  320. { "nb_frames", "Number of frames to which the effect should be applied.",
  321. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  322. { "n", "Number of frames to which the effect should be applied.",
  323. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  324. { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS },
  325. { "start_time", "Number of seconds of the beginning of the effect.",
  326. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  327. { "st", "Number of seconds of the beginning of the effect.",
  328. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  329. { "duration", "Duration of the effect in seconds.",
  330. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  331. { "d", "Duration of the effect in seconds.",
  332. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  333. { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  334. { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  335. { NULL }
  336. };
  337. AVFILTER_DEFINE_CLASS(fade);
  338. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  339. {
  340. .name = "default",
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. .config_props = config_props,
  343. .filter_frame = filter_frame,
  344. .needs_writable = 1,
  345. },
  346. { NULL }
  347. };
  348. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  349. {
  350. .name = "default",
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. },
  353. { NULL }
  354. };
  355. AVFilter ff_vf_fade = {
  356. .name = "fade",
  357. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
  358. .init = init,
  359. .priv_size = sizeof(FadeContext),
  360. .priv_class = &fade_class,
  361. .query_formats = query_formats,
  362. .inputs = avfilter_vf_fade_inputs,
  363. .outputs = avfilter_vf_fade_outputs,
  364. .flags = AVFILTER_FLAG_SLICE_THREADS,
  365. };