vf_alphaextract.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * simple channel-swapping filter to get at the alpha component
  23. */
  24. #include <string.h>
  25. #include "libavutil/pixfmt.h"
  26. #include "avfilter.h"
  27. #include "drawutils.h"
  28. #include "internal.h"
  29. #include "formats.h"
  30. #include "video.h"
  31. enum { Y, U, V, A };
  32. typedef struct {
  33. int is_packed_rgb;
  34. uint8_t rgba_map[4];
  35. } AlphaExtractContext;
  36. static int query_formats(AVFilterContext *ctx)
  37. {
  38. static const enum AVPixelFormat in_fmts[] = {
  39. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  40. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  41. AV_PIX_FMT_NONE
  42. };
  43. static const enum AVPixelFormat out_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  44. ff_formats_ref(ff_make_format_list(in_fmts), &ctx->inputs[0]->out_formats);
  45. ff_formats_ref(ff_make_format_list(out_fmts), &ctx->outputs[0]->in_formats);
  46. return 0;
  47. }
  48. static int config_input(AVFilterLink *inlink)
  49. {
  50. AlphaExtractContext *extract = inlink->dst->priv;
  51. extract->is_packed_rgb =
  52. ff_fill_rgba_map(extract->rgba_map, inlink->format) >= 0;
  53. return 0;
  54. }
  55. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *cur_buf)
  56. {
  57. AlphaExtractContext *extract = inlink->dst->priv;
  58. AVFilterLink *outlink = inlink->dst->outputs[0];
  59. AVFilterBufferRef *out_buf =
  60. ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  61. int ret;
  62. if (!out_buf) {
  63. ret = AVERROR(ENOMEM);
  64. goto end;
  65. }
  66. avfilter_copy_buffer_ref_props(out_buf, cur_buf);
  67. if (extract->is_packed_rgb) {
  68. int x, y;
  69. uint8_t *pcur, *pout;
  70. for (y = 0; y < outlink->h; y++) {
  71. pcur = cur_buf->data[0] + y * cur_buf->linesize[0] + extract->rgba_map[A];
  72. pout = out_buf->data[0] + y * out_buf->linesize[0];
  73. for (x = 0; x < outlink->w; x++) {
  74. *pout = *pcur;
  75. pout += 1;
  76. pcur += 4;
  77. }
  78. }
  79. } else {
  80. const int linesize = abs(FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]));
  81. int y;
  82. for (y = 0; y < outlink->h; y++) {
  83. memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
  84. cur_buf->data[A] + y * cur_buf->linesize[A],
  85. linesize);
  86. }
  87. }
  88. ret = ff_filter_frame(outlink, out_buf);
  89. end:
  90. avfilter_unref_buffer(cur_buf);
  91. return ret;
  92. }
  93. static const AVFilterPad alphaextract_inputs[] = {
  94. {
  95. .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .config_props = config_input,
  98. .filter_frame = filter_frame,
  99. .min_perms = AV_PERM_READ,
  100. },
  101. { NULL }
  102. };
  103. static const AVFilterPad alphaextract_outputs[] = {
  104. {
  105. .name = "default",
  106. .type = AVMEDIA_TYPE_VIDEO,
  107. },
  108. { NULL }
  109. };
  110. AVFilter avfilter_vf_alphaextract = {
  111. .name = "alphaextract",
  112. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  113. "grayscale image component."),
  114. .priv_size = sizeof(AlphaExtractContext),
  115. .query_formats = query_formats,
  116. .inputs = alphaextract_inputs,
  117. .outputs = alphaextract_outputs,
  118. };