vf_alphaextract.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "formats.h"
  29. #include "video.h"
  30. enum { Y, U, V, A };
  31. typedef struct {
  32. int is_packed_rgb;
  33. uint8_t rgba_map[4];
  34. } AlphaExtractContext;
  35. static int query_formats(AVFilterContext *ctx)
  36. {
  37. enum PixelFormat in_fmts[] = {
  38. PIX_FMT_YUVA444P, PIX_FMT_YUVA422P, PIX_FMT_YUVA420P,
  39. PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
  40. PIX_FMT_NONE
  41. };
  42. enum PixelFormat out_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
  43. ff_formats_ref(ff_make_format_list(in_fmts), &ctx->inputs[0]->out_formats);
  44. ff_formats_ref(ff_make_format_list(out_fmts), &ctx->outputs[0]->in_formats);
  45. return 0;
  46. }
  47. static int config_input(AVFilterLink *inlink)
  48. {
  49. AlphaExtractContext *extract = inlink->dst->priv;
  50. extract->is_packed_rgb =
  51. ff_fill_rgba_map(extract->rgba_map, inlink->format) >= 0;
  52. return 0;
  53. }
  54. static int draw_slice(AVFilterLink *inlink, int y0, int h, int slice_dir)
  55. {
  56. AlphaExtractContext *extract = inlink->dst->priv;
  57. AVFilterBufferRef *cur_buf = inlink->cur_buf;
  58. AVFilterBufferRef *out_buf = inlink->dst->outputs[0]->out_buf;
  59. if (extract->is_packed_rgb) {
  60. int x, y;
  61. uint8_t *pin, *pout;
  62. for (y = y0; y < (y0 + h); y++) {
  63. pin = cur_buf->data[0] + y * cur_buf->linesize[0] + extract->rgba_map[A];
  64. pout = out_buf->data[0] + y * out_buf->linesize[0];
  65. for (x = 0; x < out_buf->video->w; x++) {
  66. *pout = *pin;
  67. pout += 1;
  68. pin += 4;
  69. }
  70. }
  71. } else if (cur_buf->linesize[A] == out_buf->linesize[Y]) {
  72. const int linesize = cur_buf->linesize[A];
  73. memcpy(out_buf->data[Y] + y0 * linesize,
  74. cur_buf->data[A] + y0 * linesize,
  75. linesize * h);
  76. } else {
  77. const int linesize = FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]);
  78. int y;
  79. for (y = y0; y < (y0 + h); y++) {
  80. memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
  81. cur_buf->data[A] + y * cur_buf->linesize[A],
  82. linesize);
  83. }
  84. }
  85. return ff_draw_slice(inlink->dst->outputs[0], y0, h, slice_dir);
  86. }
  87. AVFilter avfilter_vf_alphaextract = {
  88. .name = "alphaextract",
  89. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  90. "grayscale image component."),
  91. .priv_size = sizeof(AlphaExtractContext),
  92. .query_formats = query_formats,
  93. .inputs = (const AVFilterPad[]) {
  94. { .name = "default",
  95. .type = AVMEDIA_TYPE_VIDEO,
  96. .config_props = config_input,
  97. .draw_slice = draw_slice,
  98. .min_perms = AV_PERM_READ },
  99. { .name = NULL }
  100. },
  101. .outputs = (const AVFilterPad[]) {
  102. { .name = "default",
  103. .type = AVMEDIA_TYPE_VIDEO, },
  104. { .name = NULL }
  105. },
  106. };