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.

206 lines
5.9KB

  1. /*
  2. * Copyright (c) 2012 Steven Robertson
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * copy an alpha component from another video's luma
  23. */
  24. #include <string.h>
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixfmt.h"
  28. #include "avfilter.h"
  29. #include "drawutils.h"
  30. #include "formats.h"
  31. #include "filters.h"
  32. #include "framesync.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. enum { Y, U, V, A };
  36. typedef struct AlphaMergeContext {
  37. const AVClass *class;
  38. int is_packed_rgb;
  39. uint8_t rgba_map[4];
  40. FFFrameSync fs;
  41. } AlphaMergeContext;
  42. static int do_alphamerge(FFFrameSync *fs)
  43. {
  44. AVFilterContext *ctx = fs->parent;
  45. AlphaMergeContext *s = ctx->priv;
  46. AVFrame *main_buf, *alpha_buf;
  47. int ret;
  48. ret = ff_framesync_dualinput_get_writable(fs, &main_buf, &alpha_buf);
  49. if (ret < 0)
  50. return ret;
  51. if (!alpha_buf)
  52. return ff_filter_frame(ctx->outputs[0], main_buf);
  53. if (s->is_packed_rgb) {
  54. int x, y;
  55. uint8_t *pin, *pout;
  56. for (y = 0; y < main_buf->height; y++) {
  57. pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
  58. pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
  59. for (x = 0; x < main_buf->width; x++) {
  60. *pout = *pin;
  61. pin += 1;
  62. pout += 4;
  63. }
  64. }
  65. } else {
  66. const int main_linesize = main_buf->linesize[A];
  67. const int alpha_linesize = alpha_buf->linesize[Y];
  68. av_image_copy_plane(main_buf->data[A], main_linesize,
  69. alpha_buf->data[Y], alpha_linesize,
  70. FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
  71. }
  72. return ff_filter_frame(ctx->outputs[0], main_buf);
  73. }
  74. static av_cold int init(AVFilterContext *ctx)
  75. {
  76. AlphaMergeContext *s = ctx->priv;
  77. s->fs.on_event = do_alphamerge;
  78. return 0;
  79. }
  80. static int query_formats(AVFilterContext *ctx)
  81. {
  82. static const enum AVPixelFormat main_fmts[] = {
  83. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  84. AV_PIX_FMT_GBRAP,
  85. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  86. AV_PIX_FMT_NONE
  87. };
  88. static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  89. AVFilterFormats *main_formats = ff_make_format_list(main_fmts);
  90. int ret;
  91. if ((ret = ff_formats_ref(main_formats, &ctx->inputs[0]->outcfg.formats)) < 0 ||
  92. (ret = ff_formats_ref(main_formats, &ctx->outputs[0]->incfg.formats)) < 0)
  93. return ret;
  94. return ff_formats_ref(ff_make_format_list(alpha_fmts),
  95. &ctx->inputs[1]->outcfg.formats);
  96. }
  97. static int config_input_main(AVFilterLink *inlink)
  98. {
  99. AlphaMergeContext *s = inlink->dst->priv;
  100. s->is_packed_rgb =
  101. ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
  102. inlink->format != AV_PIX_FMT_GBRAP;
  103. return 0;
  104. }
  105. static int config_output(AVFilterLink *outlink)
  106. {
  107. AVFilterContext *ctx = outlink->src;
  108. AlphaMergeContext *s = ctx->priv;
  109. AVFilterLink *mainlink = ctx->inputs[0];
  110. AVFilterLink *alphalink = ctx->inputs[1];
  111. int ret;
  112. if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
  113. av_log(ctx, AV_LOG_ERROR,
  114. "Input frame sizes do not match (%dx%d vs %dx%d).\n",
  115. mainlink->w, mainlink->h,
  116. alphalink->w, alphalink->h);
  117. return AVERROR(EINVAL);
  118. }
  119. if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
  120. return ret;
  121. outlink->w = mainlink->w;
  122. outlink->h = mainlink->h;
  123. outlink->time_base = mainlink->time_base;
  124. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  125. outlink->frame_rate = mainlink->frame_rate;
  126. return ff_framesync_configure(&s->fs);
  127. }
  128. static int activate(AVFilterContext *ctx)
  129. {
  130. AlphaMergeContext *s = ctx->priv;
  131. return ff_framesync_activate(&s->fs);
  132. }
  133. static av_cold void uninit(AVFilterContext *ctx)
  134. {
  135. AlphaMergeContext *s = ctx->priv;
  136. ff_framesync_uninit(&s->fs);
  137. }
  138. static const AVFilterPad alphamerge_inputs[] = {
  139. {
  140. .name = "main",
  141. .type = AVMEDIA_TYPE_VIDEO,
  142. .config_props = config_input_main,
  143. },{
  144. .name = "alpha",
  145. .type = AVMEDIA_TYPE_VIDEO,
  146. },
  147. { NULL }
  148. };
  149. static const AVFilterPad alphamerge_outputs[] = {
  150. {
  151. .name = "default",
  152. .type = AVMEDIA_TYPE_VIDEO,
  153. .config_props = config_output,
  154. },
  155. { NULL }
  156. };
  157. static const AVOption alphamerge_options[] = {
  158. { NULL }
  159. };
  160. FRAMESYNC_DEFINE_CLASS(alphamerge, AlphaMergeContext, fs);
  161. AVFilter ff_vf_alphamerge = {
  162. .name = "alphamerge",
  163. .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
  164. "input into the alpha channel of the first input."),
  165. .preinit = alphamerge_framesync_preinit,
  166. .priv_size = sizeof(AlphaMergeContext),
  167. .priv_class = &alphamerge_class,
  168. .init = init,
  169. .query_formats = query_formats,
  170. .inputs = alphamerge_inputs,
  171. .outputs = alphamerge_outputs,
  172. .uninit = uninit,
  173. .activate = activate,
  174. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  175. };