vf_fade.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/eval.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "drawutils.h"
  32. #include "internal.h"
  33. #define R 0
  34. #define G 1
  35. #define B 2
  36. #define A 3
  37. #define Y 0
  38. #define U 1
  39. #define V 2
  40. typedef struct {
  41. const AVClass *class;
  42. int factor, fade_per_frame;
  43. unsigned int frame_index, start_frame, stop_frame, nb_frames;
  44. int hsub, vsub, bpp;
  45. unsigned int black_level, black_level_scaled;
  46. uint8_t is_packed_rgb;
  47. uint8_t rgba_map[4];
  48. int alpha;
  49. char *type;
  50. } FadeContext;
  51. #define OFFSET(x) offsetof(FadeContext, x)
  52. static const AVOption fade_options[] = {
  53. { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX },
  54. { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX },
  55. { "start_frame", "set expression of frame to start fading", OFFSET(start_frame), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX },
  56. { "s", "set expression of frame to start fading", OFFSET(start_frame), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX },
  57. { "nb_frames", "set expression for fade duration in frames", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.dbl = 25 }, 0, INT_MAX },
  58. { "n", "set expression for fade duration in frames", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.dbl = 25 }, 0, INT_MAX },
  59. { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, 1 },
  60. {NULL},
  61. };
  62. static const char *fade_get_name(void *ctx)
  63. {
  64. return "fade";
  65. }
  66. static const AVClass fade_class = {
  67. "FadeContext",
  68. fade_get_name,
  69. fade_options
  70. };
  71. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  72. {
  73. FadeContext *fade = ctx->priv;
  74. int ret = 0;
  75. char *args1, *expr, *bufptr = NULL;
  76. fade->class = &fade_class;
  77. av_opt_set_defaults(fade);
  78. if (!(args1 = av_strdup(args))) {
  79. ret = AVERROR(ENOMEM);
  80. goto end;
  81. }
  82. if (expr = av_strtok(args1, ":", &bufptr)) {
  83. if (!(fade->type = av_strdup(expr))) {
  84. ret = AVERROR(ENOMEM);
  85. goto end;
  86. }
  87. }
  88. if (expr = av_strtok(NULL, ":", &bufptr)) {
  89. if ((ret = av_opt_set(fade, "start_frame", expr, 0)) < 0) {
  90. av_log(ctx, AV_LOG_ERROR,
  91. "Invalid value '%s' for start_frame option\n", expr);
  92. goto end;
  93. }
  94. }
  95. if (expr = av_strtok(NULL, ":", &bufptr)) {
  96. if ((ret = av_opt_set(fade, "nb_frames", expr, 0)) < 0) {
  97. av_log(ctx, AV_LOG_ERROR,
  98. "Invalid value '%s' for nb_frames option\n", expr);
  99. goto end;
  100. }
  101. }
  102. if (bufptr && (ret = av_set_options_string(fade, bufptr, "=", ":")) < 0)
  103. goto end;
  104. fade->fade_per_frame = (1 << 16) / fade->nb_frames;
  105. if (!strcmp(fade->type, "in"))
  106. fade->factor = 0;
  107. else if (!strcmp(fade->type, "out")) {
  108. fade->fade_per_frame = -fade->fade_per_frame;
  109. fade->factor = (1 << 16);
  110. } else {
  111. av_log(ctx, AV_LOG_ERROR,
  112. "Type argument must be 'in' or 'out' but '%s' was specified\n", fade->type);
  113. ret = AVERROR(EINVAL);
  114. goto end;
  115. }
  116. fade->stop_frame = fade->start_frame + fade->nb_frames;
  117. av_log(ctx, AV_LOG_INFO,
  118. "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
  119. fade->type, fade->start_frame, fade->nb_frames, fade->alpha);
  120. end:
  121. av_free(args1);
  122. return ret;
  123. }
  124. static av_cold void uninit(AVFilterContext *ctx)
  125. {
  126. FadeContext *fade = ctx->priv;
  127. av_freep(&fade->type);
  128. }
  129. static int query_formats(AVFilterContext *ctx)
  130. {
  131. static const enum PixelFormat pix_fmts[] = {
  132. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  133. PIX_FMT_YUV411P, PIX_FMT_YUV410P,
  134. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
  135. PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
  136. PIX_FMT_YUVA420P,
  137. PIX_FMT_RGB24, PIX_FMT_BGR24,
  138. PIX_FMT_ARGB, PIX_FMT_ABGR,
  139. PIX_FMT_RGBA, PIX_FMT_BGRA,
  140. PIX_FMT_NONE
  141. };
  142. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  143. return 0;
  144. }
  145. const static enum PixelFormat studio_level_pix_fmts[] = {
  146. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  147. PIX_FMT_YUV411P, PIX_FMT_YUV410P,
  148. PIX_FMT_YUV440P,
  149. PIX_FMT_NONE
  150. };
  151. static enum PixelFormat alpha_pix_fmts[] = {
  152. PIX_FMT_YUVA420P,
  153. PIX_FMT_ARGB, PIX_FMT_ABGR,
  154. PIX_FMT_RGBA, PIX_FMT_BGRA,
  155. PIX_FMT_NONE
  156. };
  157. static int config_props(AVFilterLink *inlink)
  158. {
  159. FadeContext *fade = inlink->dst->priv;
  160. const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
  161. fade->hsub = pixdesc->log2_chroma_w;
  162. fade->vsub = pixdesc->log2_chroma_h;
  163. fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
  164. fade->alpha = fade->alpha ? ff_fmt_is_in(inlink->format, alpha_pix_fmts) : 0;
  165. fade->is_packed_rgb = ff_fill_rgba_map(fade->rgba_map, inlink->format) >= 0;
  166. /* use CCIR601/709 black level for studio-level pixel non-alpha components */
  167. fade->black_level =
  168. ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !fade->alpha ? 16 : 0;
  169. /* 32768 = 1 << 15, it is an integer representation
  170. * of 0.5 and is for rounding. */
  171. fade->black_level_scaled = (fade->black_level << 16) + 32768;
  172. return 0;
  173. }
  174. static void fade_plane(int y, int h, int w,
  175. int fade_factor, int black_level, int black_level_scaled,
  176. uint8_t offset, uint8_t step, int bytes_per_plane,
  177. uint8_t *data, int line_size)
  178. {
  179. uint8_t *p;
  180. int i, j;
  181. /* luma, alpha or rgb plane */
  182. for (i = 0; i < h; i++) {
  183. p = data + offset + (y+i) * line_size;
  184. for (j = 0; j < w * bytes_per_plane; j++) {
  185. /* fade->factor is using 16 lower-order bits for decimal places. */
  186. *p = ((*p - black_level) * fade_factor + black_level_scaled) >> 16;
  187. p+=step;
  188. }
  189. }
  190. }
  191. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  192. {
  193. FadeContext *fade = inlink->dst->priv;
  194. AVFilterBufferRef *outpic = inlink->cur_buf;
  195. uint8_t *p;
  196. int i, j, plane;
  197. if (fade->factor < UINT16_MAX) {
  198. if (fade->alpha) {
  199. // alpha only
  200. plane = fade->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats
  201. // or plane 3 for planar formats
  202. fade_plane(y, h, inlink->w,
  203. fade->factor, fade->black_level, fade->black_level_scaled,
  204. fade->is_packed_rgb ? fade->rgba_map[A] : 0, // alpha offset for packed formats
  205. fade->is_packed_rgb ? 4 : 1, // pixstep for 8 bit packed formats
  206. 1, outpic->data[plane], outpic->linesize[plane]);
  207. } else {
  208. /* luma or rgb plane */
  209. fade_plane(y, h, inlink->w,
  210. fade->factor, fade->black_level, fade->black_level_scaled,
  211. 0, 1, // offset & pixstep for Y plane or RGB packed format
  212. fade->bpp, outpic->data[0], outpic->linesize[0]);
  213. if (outpic->data[1] && outpic->data[2]) {
  214. /* chroma planes */
  215. for (plane = 1; plane < 3; plane++) {
  216. for (i = 0; i < h; i++) {
  217. p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
  218. for (j = 0; j < inlink->w >> fade->hsub; j++) {
  219. /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
  220. * representation of 128.5. The .5 is for rounding
  221. * purposes. */
  222. *p = ((*p - 128) * fade->factor + 8421367) >> 16;
  223. p++;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  231. }
  232. static void end_frame(AVFilterLink *inlink)
  233. {
  234. FadeContext *fade = inlink->dst->priv;
  235. avfilter_end_frame(inlink->dst->outputs[0]);
  236. if (fade->frame_index >= fade->start_frame &&
  237. fade->frame_index <= fade->stop_frame)
  238. fade->factor += fade->fade_per_frame;
  239. fade->factor = av_clip_uint16(fade->factor);
  240. fade->frame_index++;
  241. }
  242. AVFilter avfilter_vf_fade = {
  243. .name = "fade",
  244. .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
  245. .init = init,
  246. .uninit = uninit,
  247. .priv_size = sizeof(FadeContext),
  248. .query_formats = query_formats,
  249. .inputs = (const AVFilterPad[]) {{ .name = "default",
  250. .type = AVMEDIA_TYPE_VIDEO,
  251. .config_props = config_props,
  252. .get_video_buffer = avfilter_null_get_video_buffer,
  253. .start_frame = avfilter_null_start_frame,
  254. .draw_slice = draw_slice,
  255. .end_frame = end_frame,
  256. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  257. .rej_perms = AV_PERM_PRESERVE, },
  258. { .name = NULL}},
  259. .outputs = (const AVFilterPad[]) {{ .name = "default",
  260. .type = AVMEDIA_TYPE_VIDEO, },
  261. { .name = NULL}},
  262. };