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.

218 lines
6.6KB

  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/imgutils.h"
  26. #include "libavutil/pixfmt.h"
  27. #include "avfilter.h"
  28. #include "bufferqueue.h"
  29. #include "drawutils.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. enum { Y, U, V, A };
  34. typedef struct AlphaMergeContext {
  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_GBRAP,
  51. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  52. AV_PIX_FMT_NONE
  53. };
  54. static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  55. AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
  56. int ret;
  57. if (!(main_formats = ff_make_format_list(main_fmts)) ||
  58. !(alpha_formats = ff_make_format_list(alpha_fmts))) {
  59. ret = AVERROR(ENOMEM);
  60. goto fail;
  61. }
  62. if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
  63. (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  64. (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
  65. goto fail;
  66. return 0;
  67. fail:
  68. if (main_formats)
  69. av_freep(&main_formats->formats);
  70. av_freep(&main_formats);
  71. if (alpha_formats)
  72. av_freep(&alpha_formats->formats);
  73. av_freep(&alpha_formats);
  74. return ret;
  75. }
  76. static int config_input_main(AVFilterLink *inlink)
  77. {
  78. AlphaMergeContext *merge = inlink->dst->priv;
  79. merge->is_packed_rgb =
  80. ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0 &&
  81. inlink->format != AV_PIX_FMT_GBRAP;
  82. return 0;
  83. }
  84. static int config_output(AVFilterLink *outlink)
  85. {
  86. AVFilterContext *ctx = outlink->src;
  87. AVFilterLink *mainlink = ctx->inputs[0];
  88. AVFilterLink *alphalink = ctx->inputs[1];
  89. if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
  90. av_log(ctx, AV_LOG_ERROR,
  91. "Input frame sizes do not match (%dx%d vs %dx%d).\n",
  92. mainlink->w, mainlink->h,
  93. alphalink->w, alphalink->h);
  94. return AVERROR(EINVAL);
  95. }
  96. outlink->w = mainlink->w;
  97. outlink->h = mainlink->h;
  98. outlink->time_base = mainlink->time_base;
  99. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  100. outlink->frame_rate = mainlink->frame_rate;
  101. return 0;
  102. }
  103. static void draw_frame(AVFilterContext *ctx,
  104. AVFrame *main_buf,
  105. AVFrame *alpha_buf)
  106. {
  107. AlphaMergeContext *merge = ctx->priv;
  108. int h = main_buf->height;
  109. if (merge->is_packed_rgb) {
  110. int x, y;
  111. uint8_t *pin, *pout;
  112. for (y = 0; y < h; y++) {
  113. pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
  114. pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
  115. for (x = 0; x < main_buf->width; x++) {
  116. *pout = *pin;
  117. pin += 1;
  118. pout += 4;
  119. }
  120. }
  121. } else {
  122. const int main_linesize = main_buf->linesize[A];
  123. const int alpha_linesize = alpha_buf->linesize[Y];
  124. av_image_copy_plane(main_buf->data[A], main_linesize,
  125. alpha_buf->data[Y], alpha_linesize,
  126. FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
  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. };