vf_format.c 5.0 KB

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