vf_alphamerge.c 6.6 KB

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