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.

302 lines
10KB

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