vf_alphamerge.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. AVFrame *main_buf,
  89. AVFrame *alpha_buf)
  90. {
  91. AlphaMergeContext *merge = ctx->priv;
  92. int h = main_buf->height;
  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->width; 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->height; 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, AVFrame *buf)
  117. {
  118. AVFilterContext *ctx = inlink->dst;
  119. AlphaMergeContext *merge = ctx->priv;
  120. int ret = 0;
  121. int is_alpha = (inlink == ctx->inputs[1]);
  122. struct FFBufQueue *queue =
  123. (is_alpha ? &merge->queue_alpha : &merge->queue_main);
  124. ff_bufqueue_add(ctx, queue, buf);
  125. do {
  126. AVFrame *main_buf, *alpha_buf;
  127. if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
  128. !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
  129. main_buf = ff_bufqueue_get(&merge->queue_main);
  130. alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
  131. merge->frame_requested = 0;
  132. draw_frame(ctx, main_buf, alpha_buf);
  133. ret = ff_filter_frame(ctx->outputs[0], main_buf);
  134. av_frame_free(&alpha_buf);
  135. } while (ret >= 0);
  136. return ret;
  137. }
  138. static int request_frame(AVFilterLink *outlink)
  139. {
  140. AVFilterContext *ctx = outlink->src;
  141. AlphaMergeContext *merge = ctx->priv;
  142. int in, ret;
  143. merge->frame_requested = 1;
  144. while (merge->frame_requested) {
  145. in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
  146. ret = ff_request_frame(ctx->inputs[in]);
  147. if (ret < 0)
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static const AVFilterPad alphamerge_inputs[] = {
  153. {
  154. .name = "main",
  155. .type = AVMEDIA_TYPE_VIDEO,
  156. .config_props = config_input_main,
  157. .filter_frame = filter_frame,
  158. .needs_writable = 1,
  159. },{
  160. .name = "alpha",
  161. .type = AVMEDIA_TYPE_VIDEO,
  162. .filter_frame = filter_frame,
  163. },
  164. { NULL }
  165. };
  166. static const AVFilterPad alphamerge_outputs[] = {
  167. {
  168. .name = "default",
  169. .type = AVMEDIA_TYPE_VIDEO,
  170. .config_props = config_output,
  171. .request_frame = request_frame,
  172. },
  173. { NULL }
  174. };
  175. AVFilter ff_vf_alphamerge = {
  176. .name = "alphamerge",
  177. .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
  178. "input into the alpha channel of the first input."),
  179. .uninit = uninit,
  180. .priv_size = sizeof(AlphaMergeContext),
  181. .query_formats = query_formats,
  182. .inputs = alphamerge_inputs,
  183. .outputs = alphamerge_outputs,
  184. };