vf_fade.c 11 KB

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