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.

132 lines
3.8KB

  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, AVFrame *cur_buf)
  56. {
  57. AlphaExtractContext *extract = inlink->dst->priv;
  58. AVFilterLink *outlink = inlink->dst->outputs[0];
  59. AVFrame *out_buf = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  60. int ret;
  61. if (!out_buf) {
  62. ret = AVERROR(ENOMEM);
  63. goto end;
  64. }
  65. av_frame_copy_props(out_buf, cur_buf);
  66. if (extract->is_packed_rgb) {
  67. int x, y;
  68. uint8_t *pcur, *pout;
  69. for (y = 0; y < outlink->h; y++) {
  70. pcur = cur_buf->data[0] + y * cur_buf->linesize[0] + extract->rgba_map[A];
  71. pout = out_buf->data[0] + y * out_buf->linesize[0];
  72. for (x = 0; x < outlink->w; x++) {
  73. *pout = *pcur;
  74. pout += 1;
  75. pcur += 4;
  76. }
  77. }
  78. } else {
  79. const int linesize = abs(FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]));
  80. int y;
  81. for (y = 0; y < outlink->h; y++) {
  82. memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
  83. cur_buf->data[A] + y * cur_buf->linesize[A],
  84. linesize);
  85. }
  86. }
  87. ret = ff_filter_frame(outlink, out_buf);
  88. end:
  89. av_frame_free(&cur_buf);
  90. return ret;
  91. }
  92. static const AVFilterPad alphaextract_inputs[] = {
  93. {
  94. .name = "default",
  95. .type = AVMEDIA_TYPE_VIDEO,
  96. .config_props = config_input,
  97. .filter_frame = filter_frame,
  98. },
  99. { NULL }
  100. };
  101. static const AVFilterPad alphaextract_outputs[] = {
  102. {
  103. .name = "default",
  104. .type = AVMEDIA_TYPE_VIDEO,
  105. },
  106. { NULL }
  107. };
  108. AVFilter avfilter_vf_alphaextract = {
  109. .name = "alphaextract",
  110. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  111. "grayscale image component."),
  112. .priv_size = sizeof(AlphaExtractContext),
  113. .query_formats = query_formats,
  114. .inputs = alphaextract_inputs,
  115. .outputs = alphaextract_outputs,
  116. };