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