vf_alphamerge.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/pixfmt.h"
  27. #include "avfilter.h"
  28. #include "drawutils.h"
  29. #include "formats.h"
  30. #include "filters.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. enum { Y, U, V, A };
  34. typedef struct AlphaMergeContext {
  35. int is_packed_rgb;
  36. uint8_t rgba_map[4];
  37. AVFrame *main_frame;
  38. AVFrame *alpha_frame;
  39. } AlphaMergeContext;
  40. static int query_formats(AVFilterContext *ctx)
  41. {
  42. static const enum AVPixelFormat main_fmts[] = {
  43. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  44. AV_PIX_FMT_GBRAP,
  45. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  46. AV_PIX_FMT_NONE
  47. };
  48. static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  49. AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
  50. int ret;
  51. if (!(main_formats = ff_make_format_list(main_fmts)) ||
  52. !(alpha_formats = ff_make_format_list(alpha_fmts))) {
  53. ret = AVERROR(ENOMEM);
  54. goto fail;
  55. }
  56. if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
  57. (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  58. (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
  59. goto fail;
  60. return 0;
  61. fail:
  62. if (main_formats)
  63. av_freep(&main_formats->formats);
  64. av_freep(&main_formats);
  65. if (alpha_formats)
  66. av_freep(&alpha_formats->formats);
  67. av_freep(&alpha_formats);
  68. return ret;
  69. }
  70. static int config_input_main(AVFilterLink *inlink)
  71. {
  72. AlphaMergeContext *merge = inlink->dst->priv;
  73. merge->is_packed_rgb =
  74. ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0 &&
  75. inlink->format != AV_PIX_FMT_GBRAP;
  76. return 0;
  77. }
  78. static int config_output(AVFilterLink *outlink)
  79. {
  80. AVFilterContext *ctx = outlink->src;
  81. AVFilterLink *mainlink = ctx->inputs[0];
  82. AVFilterLink *alphalink = ctx->inputs[1];
  83. if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
  84. av_log(ctx, AV_LOG_ERROR,
  85. "Input frame sizes do not match (%dx%d vs %dx%d).\n",
  86. mainlink->w, mainlink->h,
  87. alphalink->w, alphalink->h);
  88. return AVERROR(EINVAL);
  89. }
  90. outlink->w = mainlink->w;
  91. outlink->h = mainlink->h;
  92. outlink->time_base = mainlink->time_base;
  93. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  94. outlink->frame_rate = mainlink->frame_rate;
  95. return 0;
  96. }
  97. static void draw_frame(AVFilterContext *ctx,
  98. AVFrame *main_buf,
  99. AVFrame *alpha_buf)
  100. {
  101. AlphaMergeContext *merge = ctx->priv;
  102. int h = main_buf->height;
  103. if (merge->is_packed_rgb) {
  104. int x, y;
  105. uint8_t *pin, *pout;
  106. for (y = 0; y < h; y++) {
  107. pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
  108. pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
  109. for (x = 0; x < main_buf->width; x++) {
  110. *pout = *pin;
  111. pin += 1;
  112. pout += 4;
  113. }
  114. }
  115. } else {
  116. const int main_linesize = main_buf->linesize[A];
  117. const int alpha_linesize = alpha_buf->linesize[Y];
  118. av_image_copy_plane(main_buf->data[A], main_linesize,
  119. alpha_buf->data[Y], alpha_linesize,
  120. FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
  121. }
  122. }
  123. static int activate(AVFilterContext *ctx)
  124. {
  125. AVFilterLink *outlink = ctx->outputs[0];
  126. AlphaMergeContext *s = ctx->priv;
  127. int ret;
  128. FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
  129. if (!s->main_frame) {
  130. ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
  131. if (ret < 0)
  132. return ret;
  133. }
  134. if (!s->alpha_frame) {
  135. ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
  136. if (ret < 0)
  137. return ret;
  138. }
  139. if (s->main_frame && s->alpha_frame) {
  140. draw_frame(ctx, s->main_frame, s->alpha_frame);
  141. ret = ff_filter_frame(outlink, s->main_frame);
  142. av_frame_free(&s->alpha_frame);
  143. s->main_frame = NULL;
  144. return ret;
  145. }
  146. FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
  147. FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
  148. if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
  149. !ff_outlink_get_status(ctx->inputs[0]) &&
  150. !s->main_frame) {
  151. ff_inlink_request_frame(ctx->inputs[0]);
  152. return 0;
  153. }
  154. if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
  155. !ff_outlink_get_status(ctx->inputs[1]) &&
  156. !s->alpha_frame) {
  157. ff_inlink_request_frame(ctx->inputs[1]);
  158. return 0;
  159. }
  160. return FFERROR_NOT_READY;
  161. }
  162. static const AVFilterPad alphamerge_inputs[] = {
  163. {
  164. .name = "main",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .config_props = config_input_main,
  167. .needs_writable = 1,
  168. },{
  169. .name = "alpha",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. },
  172. { NULL }
  173. };
  174. static const AVFilterPad alphamerge_outputs[] = {
  175. {
  176. .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .config_props = config_output,
  179. },
  180. { NULL }
  181. };
  182. AVFilter ff_vf_alphamerge = {
  183. .name = "alphamerge",
  184. .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
  185. "input into the alpha channel of the first input."),
  186. .priv_size = sizeof(AlphaMergeContext),
  187. .query_formats = query_formats,
  188. .inputs = alphamerge_inputs,
  189. .outputs = alphamerge_outputs,
  190. .activate = activate,
  191. };