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