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.

204 lines
6.2KB

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