You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.3KB

  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, *alpha_formats;
  54. int ret;
  55. if (!(main_formats = ff_make_format_list(main_fmts)) ||
  56. !(alpha_formats = ff_make_format_list(alpha_fmts)))
  57. return AVERROR(ENOMEM);
  58. if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
  59. (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  60. (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
  61. return ret;
  62. return 0;
  63. }
  64. static int config_input_main(AVFilterLink *inlink)
  65. {
  66. AlphaMergeContext *merge = inlink->dst->priv;
  67. merge->is_packed_rgb =
  68. ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
  69. return 0;
  70. }
  71. static int config_output(AVFilterLink *outlink)
  72. {
  73. AVFilterContext *ctx = outlink->src;
  74. AVFilterLink *mainlink = ctx->inputs[0];
  75. AVFilterLink *alphalink = ctx->inputs[1];
  76. if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
  77. av_log(ctx, AV_LOG_ERROR,
  78. "Input frame sizes do not match (%dx%d vs %dx%d).\n",
  79. mainlink->w, mainlink->h,
  80. alphalink->w, alphalink->h);
  81. return AVERROR(EINVAL);
  82. }
  83. outlink->w = mainlink->w;
  84. outlink->h = mainlink->h;
  85. outlink->time_base = mainlink->time_base;
  86. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  87. outlink->frame_rate = mainlink->frame_rate;
  88. return 0;
  89. }
  90. static void draw_frame(AVFilterContext *ctx,
  91. AVFrame *main_buf,
  92. AVFrame *alpha_buf)
  93. {
  94. AlphaMergeContext *merge = ctx->priv;
  95. int h = main_buf->height;
  96. if (merge->is_packed_rgb) {
  97. int x, y;
  98. uint8_t *pin, *pout;
  99. for (y = 0; y < h; y++) {
  100. pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
  101. pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
  102. for (x = 0; x < main_buf->width; x++) {
  103. *pout = *pin;
  104. pin += 1;
  105. pout += 4;
  106. }
  107. }
  108. } else {
  109. int y;
  110. const int main_linesize = main_buf->linesize[A];
  111. const int alpha_linesize = alpha_buf->linesize[Y];
  112. for (y = 0; y < h && y < alpha_buf->height; y++) {
  113. memcpy(main_buf->data[A] + y * main_linesize,
  114. alpha_buf->data[Y] + y * alpha_linesize,
  115. FFMIN(main_linesize, alpha_linesize));
  116. }
  117. }
  118. }
  119. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. AlphaMergeContext *merge = ctx->priv;
  123. int ret = 0;
  124. int is_alpha = (inlink == ctx->inputs[1]);
  125. struct FFBufQueue *queue =
  126. (is_alpha ? &merge->queue_alpha : &merge->queue_main);
  127. ff_bufqueue_add(ctx, queue, buf);
  128. do {
  129. AVFrame *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. draw_frame(ctx, main_buf, alpha_buf);
  135. ret = ff_filter_frame(ctx->outputs[0], main_buf);
  136. av_frame_free(&alpha_buf);
  137. } while (ret >= 0);
  138. return ret;
  139. }
  140. static int request_frame(AVFilterLink *outlink)
  141. {
  142. AVFilterContext *ctx = outlink->src;
  143. AlphaMergeContext *merge = ctx->priv;
  144. int in, ret;
  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. 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. .filter_frame = filter_frame,
  157. .needs_writable = 1,
  158. },{
  159. .name = "alpha",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .filter_frame = filter_frame,
  162. },
  163. { NULL }
  164. };
  165. static const AVFilterPad alphamerge_outputs[] = {
  166. {
  167. .name = "default",
  168. .type = AVMEDIA_TYPE_VIDEO,
  169. .config_props = config_output,
  170. .request_frame = request_frame,
  171. },
  172. { NULL }
  173. };
  174. AVFilter ff_vf_alphamerge = {
  175. .name = "alphamerge",
  176. .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
  177. "input into the alpha channel of the first input."),
  178. .uninit = uninit,
  179. .priv_size = sizeof(AlphaMergeContext),
  180. .query_formats = query_formats,
  181. .inputs = alphamerge_inputs,
  182. .outputs = alphamerge_outputs,
  183. };