vf_format.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  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. * format and noformat video filters
  23. */
  24. #include <string.h>
  25. #include "libavutil/internal.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. typedef struct {
  34. /**
  35. * List of flags telling if a given image format has been listed
  36. * as argument to the filter.
  37. */
  38. int listed_pix_fmt_flags[PIX_FMT_NB];
  39. } FormatContext;
  40. #define PIX_FMT_NAME_MAXSIZE 32
  41. static av_cold int init(AVFilterContext *ctx, const char *args)
  42. {
  43. FormatContext *format = ctx->priv;
  44. const char *cur, *sep;
  45. char pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
  46. int pix_fmt_name_len, ret;
  47. enum PixelFormat pix_fmt;
  48. /* parse the list of formats */
  49. for (cur = args; cur; cur = sep ? sep+1 : NULL) {
  50. if (!(sep = strchr(cur, ':')))
  51. pix_fmt_name_len = strlen(cur);
  52. else
  53. pix_fmt_name_len = sep - cur;
  54. if (pix_fmt_name_len >= PIX_FMT_NAME_MAXSIZE) {
  55. av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
  56. return -1;
  57. }
  58. memcpy(pix_fmt_name, cur, pix_fmt_name_len);
  59. pix_fmt_name[pix_fmt_name_len] = 0;
  60. if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
  61. return ret;
  62. format->listed_pix_fmt_flags[pix_fmt] = 1;
  63. }
  64. return 0;
  65. }
  66. static AVFilterFormats *make_format_list(FormatContext *format, int flag)
  67. {
  68. AVFilterFormats *formats;
  69. enum PixelFormat pix_fmt;
  70. formats = av_mallocz(sizeof(AVFilterFormats));
  71. formats->formats = av_malloc(sizeof(enum PixelFormat) * PIX_FMT_NB);
  72. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  73. if (format->listed_pix_fmt_flags[pix_fmt] == flag)
  74. formats->formats[formats->format_count++] = pix_fmt;
  75. return formats;
  76. }
  77. #if CONFIG_FORMAT_FILTER
  78. static int query_formats_format(AVFilterContext *ctx)
  79. {
  80. ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
  81. return 0;
  82. }
  83. AVFilter avfilter_vf_format = {
  84. .name = "format",
  85. .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
  86. .init = init,
  87. .query_formats = query_formats_format,
  88. .priv_size = sizeof(FormatContext),
  89. .inputs = (const AVFilterPad[]) {{ .name = "default",
  90. .type = AVMEDIA_TYPE_VIDEO,
  91. .get_video_buffer= ff_null_get_video_buffer,
  92. .start_frame = ff_null_start_frame,
  93. .draw_slice = ff_null_draw_slice,
  94. .end_frame = ff_null_end_frame, },
  95. { .name = NULL}},
  96. .outputs = (const AVFilterPad[]) {{ .name = "default",
  97. .type = AVMEDIA_TYPE_VIDEO },
  98. { .name = NULL}},
  99. };
  100. #endif /* CONFIG_FORMAT_FILTER */
  101. #if CONFIG_NOFORMAT_FILTER
  102. static int query_formats_noformat(AVFilterContext *ctx)
  103. {
  104. ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
  105. return 0;
  106. }
  107. AVFilter avfilter_vf_noformat = {
  108. .name = "noformat",
  109. .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
  110. .init = init,
  111. .query_formats = query_formats_noformat,
  112. .priv_size = sizeof(FormatContext),
  113. .inputs = (const AVFilterPad[]) {{ .name = "default",
  114. .type = AVMEDIA_TYPE_VIDEO,
  115. .get_video_buffer= ff_null_get_video_buffer,
  116. .start_frame = ff_null_start_frame,
  117. .draw_slice = ff_null_draw_slice,
  118. .end_frame = ff_null_end_frame, },
  119. { .name = NULL}},
  120. .outputs = (const AVFilterPad[]) {{ .name = "default",
  121. .type = AVMEDIA_TYPE_VIDEO },
  122. { .name = NULL}},
  123. };
  124. #endif /* CONFIG_NOFORMAT_FILTER */