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.

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