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.

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