vf_format.c 4.9 KB

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