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.

356 lines
13KB

  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 *s = ctx->priv;
  63. s->fade_per_frame = (1 << 16) / s->nb_frames;
  64. s->fade_state = VF_FADE_WAITING;
  65. if (s->duration != 0) {
  66. // If duration (seconds) is non-zero, assume that we are not fading based on frames
  67. s->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 (s->start_frame || s->nb_frames) {
  71. av_log(ctx, AV_LOG_VERBOSE,
  72. "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
  73. s->type == FADE_IN ? "in" : "out", s->start_frame,
  74. s->nb_frames,s->alpha);
  75. }
  76. if (s->start_time || s->duration) {
  77. av_log(ctx, AV_LOG_VERBOSE,
  78. "type:%s start_time:%f duration:%f alpha:%d\n",
  79. s->type == FADE_IN ? "in" : "out", (s->start_time / (double)AV_TIME_BASE),
  80. (s->duration / (double)AV_TIME_BASE),s->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 *s = inlink->dst->priv;
  109. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  110. s->hsub = pixdesc->log2_chroma_w;
  111. s->vsub = pixdesc->log2_chroma_h;
  112. s->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  113. s->alpha &= !!(pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA);
  114. s->is_packed_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  115. /* use CCIR601/709 black level for studio-level pixel non-alpha components */
  116. s->black_level =
  117. ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !s->alpha ? 16 : 0;
  118. /* 32768 = 1 << 15, it is an integer representation
  119. * of 0.5 and is for rounding. */
  120. s->black_level_scaled = (s->black_level << 16) + 32768;
  121. return 0;
  122. }
  123. static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
  124. int nb_jobs)
  125. {
  126. FadeContext *s = ctx->priv;
  127. AVFrame *frame = arg;
  128. int slice_start = (frame->height * jobnr ) / nb_jobs;
  129. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  130. int i, j;
  131. for (i = slice_start; i < slice_end; i++) {
  132. uint8_t *p = frame->data[0] + i * frame->linesize[0];
  133. for (j = 0; j < frame->width * s->bpp; j++) {
  134. /* s->factor is using 16 lower-order bits for decimal
  135. * places. 32768 = 1 << 15, it is an integer representation
  136. * of 0.5 and is for rounding. */
  137. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  138. p++;
  139. }
  140. }
  141. return 0;
  142. }
  143. static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
  144. int nb_jobs)
  145. {
  146. FadeContext *s = ctx->priv;
  147. AVFrame *frame = arg;
  148. int i, j, plane;
  149. const int width = FF_CEIL_RSHIFT(frame->width, s->hsub);
  150. const int height= FF_CEIL_RSHIFT(frame->height, s->vsub);
  151. int slice_start = (height * jobnr ) / nb_jobs;
  152. int slice_end = (height * (jobnr+1)) / nb_jobs;
  153. for (plane = 1; plane < 3; plane++) {
  154. for (i = slice_start; i < slice_end; i++) {
  155. uint8_t *p = frame->data[plane] + i * frame->linesize[plane];
  156. for (j = 0; j < width; j++) {
  157. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  158. * representation of 128.5. The .5 is for rounding
  159. * purposes. */
  160. *p = ((*p - 128) * s->factor + 8421367) >> 16;
  161. p++;
  162. }
  163. }
  164. }
  165. return 0;
  166. }
  167. static int filter_slice_alpha(AVFilterContext *ctx, void *arg, int jobnr,
  168. int nb_jobs)
  169. {
  170. FadeContext *s = ctx->priv;
  171. AVFrame *frame = arg;
  172. int plane = s->is_packed_rgb ? 0 : A;
  173. int slice_start = (frame->height * jobnr ) / nb_jobs;
  174. int slice_end = (frame->height * (jobnr+1)) / nb_jobs;
  175. int i, j;
  176. for (i = slice_start; i < slice_end; i++) {
  177. uint8_t *p = frame->data[plane] + i * frame->linesize[plane] + s->is_packed_rgb*s->rgba_map[A];
  178. int step = s->is_packed_rgb ? 4 : 1;
  179. for (j = 0; j < frame->width; j++) {
  180. /* s->factor is using 16 lower-order bits for decimal
  181. * places. 32768 = 1 << 15, it is an integer representation
  182. * of 0.5 and is for rounding. */
  183. *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16;
  184. p += step;
  185. }
  186. }
  187. return 0;
  188. }
  189. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  190. {
  191. AVFilterContext *ctx = inlink->dst;
  192. FadeContext *s = ctx->priv;
  193. double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
  194. // Calculate Fade assuming this is a Fade In
  195. if (s->fade_state == VF_FADE_WAITING) {
  196. s->factor=0;
  197. if ((frame_timestamp >= (s->start_time/(double)AV_TIME_BASE))
  198. && (s->frame_index >= s->start_frame)) {
  199. // Time to start fading
  200. s->fade_state = VF_FADE_FADING;
  201. // Save start time in case we are starting based on frames and fading based on time
  202. if ((s->start_time == 0) && (s->start_frame != 0)) {
  203. s->start_time = frame_timestamp*(double)AV_TIME_BASE;
  204. }
  205. // Save start frame in case we are starting based on time and fading based on frames
  206. if ((s->start_time != 0) && (s->start_frame == 0)) {
  207. s->start_frame = s->frame_index;
  208. }
  209. }
  210. }
  211. if (s->fade_state == VF_FADE_FADING) {
  212. if (s->duration == 0) {
  213. // Fading based on frame count
  214. s->factor = (s->frame_index - s->start_frame) * s->fade_per_frame;
  215. if (s->frame_index > (s->start_frame + s->nb_frames)) {
  216. s->fade_state = VF_FADE_DONE;
  217. }
  218. } else {
  219. // Fading based on duration
  220. s->factor = (frame_timestamp - (s->start_time/(double)AV_TIME_BASE))
  221. * (float) UINT16_MAX / (s->duration/(double)AV_TIME_BASE);
  222. if (frame_timestamp > ((s->start_time/(double)AV_TIME_BASE)
  223. + (s->duration/(double)AV_TIME_BASE))) {
  224. s->fade_state = VF_FADE_DONE;
  225. }
  226. }
  227. }
  228. if (s->fade_state == VF_FADE_DONE) {
  229. s->factor=UINT16_MAX;
  230. }
  231. s->factor = av_clip_uint16(s->factor);
  232. // Invert fade_factor if Fading Out
  233. if (s->type == 1) {
  234. s->factor=UINT16_MAX-s->factor;
  235. }
  236. if (s->factor < UINT16_MAX) {
  237. if (s->alpha) {
  238. ctx->internal->execute(ctx, filter_slice_alpha, frame, NULL,
  239. FFMIN(frame->height, ctx->graph->nb_threads));
  240. } else {
  241. /* luma or rgb plane */
  242. ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
  243. FFMIN(frame->height, ctx->graph->nb_threads));
  244. if (frame->data[1] && frame->data[2]) {
  245. /* chroma planes */
  246. ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
  247. FFMIN(frame->height, ctx->graph->nb_threads));
  248. }
  249. }
  250. }
  251. s->frame_index++;
  252. return ff_filter_frame(inlink->dst->outputs[0], frame);
  253. }
  254. #define OFFSET(x) offsetof(FadeContext, x)
  255. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  256. static const AVOption fade_options[] = {
  257. { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  258. { "t", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
  259. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
  260. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
  261. { "start_frame", "Number of the first frame to which to apply the effect.",
  262. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  263. { "s", "Number of the first frame to which to apply the effect.",
  264. OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  265. { "nb_frames", "Number of frames to which the effect should be applied.",
  266. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  267. { "n", "Number of frames to which the effect should be applied.",
  268. OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
  269. { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS },
  270. { "start_time", "Number of seconds of the beginning of the effect.",
  271. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  272. { "st", "Number of seconds of the beginning of the effect.",
  273. OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  274. { "duration", "Duration of the effect in seconds.",
  275. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  276. { "d", "Duration of the effect in seconds.",
  277. OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  278. { NULL },
  279. };
  280. AVFILTER_DEFINE_CLASS(fade);
  281. static const AVFilterPad avfilter_vf_fade_inputs[] = {
  282. {
  283. .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .config_props = config_props,
  286. .get_video_buffer = ff_null_get_video_buffer,
  287. .filter_frame = filter_frame,
  288. .needs_writable = 1,
  289. },
  290. { NULL }
  291. };
  292. static const AVFilterPad avfilter_vf_fade_outputs[] = {
  293. {
  294. .name = "default",
  295. .type = AVMEDIA_TYPE_VIDEO,
  296. },
  297. { NULL }
  298. };
  299. AVFilter avfilter_vf_fade = {
  300. .name = "fade",
  301. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
  302. .init = init,
  303. .priv_size = sizeof(FadeContext),
  304. .priv_class = &fade_class,
  305. .query_formats = query_formats,
  306. .inputs = avfilter_vf_fade_inputs,
  307. .outputs = avfilter_vf_fade_outputs,
  308. .flags = AVFILTER_FLAG_SLICE_THREADS,
  309. };