vf_alphamerge.c 6.5 KB

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