vf_alphamerge.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. enum PixelFormat main_fmts[] = {
  49. PIX_FMT_YUVA444P, PIX_FMT_YUVA422P, PIX_FMT_YUVA420P,
  50. PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
  51. PIX_FMT_NONE
  52. };
  53. enum PixelFormat alpha_fmts[] = { PIX_FMT_GRAY8, 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 int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) {return 0;}
  88. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) {return 0;}
  89. static void draw_frame(AVFilterContext *ctx,
  90. AVFilterBufferRef *main_buf,
  91. AVFilterBufferRef *alpha_buf)
  92. {
  93. AlphaMergeContext *merge = ctx->priv;
  94. int h = main_buf->video->h;
  95. if (merge->is_packed_rgb) {
  96. int x, y;
  97. uint8_t *pin, *pout;
  98. for (y = 0; y < h; y++) {
  99. pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
  100. pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
  101. for (x = 0; x < main_buf->video->w; x++) {
  102. *pout = *pin;
  103. pin += 1;
  104. pout += 4;
  105. }
  106. }
  107. } else {
  108. int y;
  109. const int main_linesize = main_buf->linesize[A];
  110. const int alpha_linesize = alpha_buf->linesize[Y];
  111. for (y = 0; y < h && y < alpha_buf->video->h; y++) {
  112. memcpy(main_buf->data[A] + y * main_linesize,
  113. alpha_buf->data[Y] + y * alpha_linesize,
  114. FFMIN(main_linesize, alpha_linesize));
  115. }
  116. }
  117. ff_draw_slice(ctx->outputs[0], 0, h, 1);
  118. }
  119. static int end_frame(AVFilterLink *inlink)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. AlphaMergeContext *merge = ctx->priv;
  123. int is_alpha = (inlink == ctx->inputs[1]);
  124. struct FFBufQueue *queue =
  125. (is_alpha ? &merge->queue_alpha : &merge->queue_main);
  126. ff_bufqueue_add(ctx, queue, inlink->cur_buf);
  127. inlink->cur_buf = NULL;
  128. while (1) {
  129. AVFilterBufferRef *main_buf, *alpha_buf;
  130. if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
  131. !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
  132. main_buf = ff_bufqueue_get(&merge->queue_main);
  133. alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
  134. ctx->outputs[0]->out_buf = main_buf;
  135. ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(main_buf, ~0));
  136. merge->frame_requested = 0;
  137. draw_frame(ctx, main_buf, alpha_buf);
  138. ff_end_frame(ctx->outputs[0]);
  139. avfilter_unref_buffer(alpha_buf);
  140. }
  141. return 0;
  142. }
  143. static int request_frame(AVFilterLink *outlink)
  144. {
  145. AVFilterContext *ctx = outlink->src;
  146. AlphaMergeContext *merge = ctx->priv;
  147. int in, ret;
  148. merge->frame_requested = 1;
  149. while (merge->frame_requested) {
  150. in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
  151. ret = ff_request_frame(ctx->inputs[in]);
  152. if (ret < 0)
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. AVFilter avfilter_vf_alphamerge = {
  158. .name = "alphamerge",
  159. .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
  160. "input into the alpha channel of the first input."),
  161. .uninit = uninit,
  162. .priv_size = sizeof(AlphaMergeContext),
  163. .query_formats = query_formats,
  164. .inputs = (const AVFilterPad[]) {
  165. { .name = "main",
  166. .type = AVMEDIA_TYPE_VIDEO,
  167. .config_props = config_input_main,
  168. .get_video_buffer = ff_null_get_video_buffer,
  169. .start_frame = start_frame,
  170. .draw_slice = draw_slice,
  171. .end_frame = end_frame,
  172. .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE },
  173. { .name = "alpha",
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. .start_frame = start_frame,
  176. .draw_slice = draw_slice,
  177. .end_frame = end_frame,
  178. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE },
  179. { .name = NULL }
  180. },
  181. .outputs = (const AVFilterPad[]) {
  182. { .name = "default",
  183. .type = AVMEDIA_TYPE_VIDEO,
  184. .config_props = config_output,
  185. .request_frame = request_frame },
  186. { .name = NULL }
  187. },
  188. };