vf_format.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. typedef struct FormatContext {
  34. const AVClass *class;
  35. char *pix_fmts;
  36. /**
  37. * pix_fmts parsed into AVPixelFormats and terminated with
  38. * AV_PIX_FMT_NONE
  39. */
  40. enum AVPixelFormat *formats;
  41. } FormatContext;
  42. static av_cold void uninit(AVFilterContext *ctx)
  43. {
  44. FormatContext *s = ctx->priv;
  45. av_freep(&s->formats);
  46. }
  47. static av_cold int init(AVFilterContext *ctx)
  48. {
  49. FormatContext *s = ctx->priv;
  50. char *cur, *sep;
  51. int nb_formats = 1;
  52. int i;
  53. int ret;
  54. if (!s->pix_fmts) {
  55. av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
  56. return AVERROR(EINVAL);
  57. }
  58. /* count the formats */
  59. cur = s->pix_fmts;
  60. while ((cur = strchr(cur, '|'))) {
  61. nb_formats++;
  62. if (*cur)
  63. cur++;
  64. }
  65. s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
  66. if (!s->formats)
  67. return AVERROR(ENOMEM);
  68. /* parse the list of formats */
  69. cur = s->pix_fmts;
  70. for (i = 0; i < nb_formats; i++) {
  71. sep = strchr(cur, '|');
  72. if (sep)
  73. *sep++ = 0;
  74. if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0)
  75. return ret;
  76. cur = sep;
  77. }
  78. s->formats[nb_formats] = AV_PIX_FMT_NONE;
  79. if (!strcmp(ctx->filter->name, "noformat")) {
  80. const AVPixFmtDescriptor *desc = NULL;
  81. enum AVPixelFormat *formats_allowed;
  82. int nb_formats_lavu = 0, nb_formats_allowed = 0;
  83. /* count the formats known to lavu */
  84. while ((desc = av_pix_fmt_desc_next(desc)))
  85. nb_formats_lavu++;
  86. formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
  87. if (!formats_allowed)
  88. return AVERROR(ENOMEM);
  89. /* for each format known to lavu, check if it's in the list of
  90. * forbidden formats */
  91. while ((desc = av_pix_fmt_desc_next(desc))) {
  92. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
  93. for (i = 0; i < nb_formats; i++) {
  94. if (s->formats[i] == pix_fmt)
  95. break;
  96. }
  97. if (i < nb_formats)
  98. continue;
  99. formats_allowed[nb_formats_allowed++] = pix_fmt;
  100. }
  101. formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
  102. av_freep(&s->formats);
  103. s->formats = formats_allowed;
  104. }
  105. return 0;
  106. }
  107. static int query_formats(AVFilterContext *ctx)
  108. {
  109. FormatContext *s = ctx->priv;
  110. AVFilterFormats *formats = ff_make_format_list(s->formats);
  111. if (!formats)
  112. return AVERROR(ENOMEM);
  113. return ff_set_common_formats(ctx, formats);
  114. }
  115. #define OFFSET(x) offsetof(FormatContext, x)
  116. static const AVOption options[] = {
  117. { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
  118. { NULL }
  119. };
  120. #if CONFIG_FORMAT_FILTER
  121. #define format_options options
  122. AVFILTER_DEFINE_CLASS(format);
  123. static const AVFilterPad avfilter_vf_format_inputs[] = {
  124. {
  125. .name = "default",
  126. .type = AVMEDIA_TYPE_VIDEO,
  127. .get_video_buffer = ff_null_get_video_buffer,
  128. },
  129. { NULL }
  130. };
  131. static const AVFilterPad avfilter_vf_format_outputs[] = {
  132. {
  133. .name = "default",
  134. .type = AVMEDIA_TYPE_VIDEO
  135. },
  136. { NULL }
  137. };
  138. AVFilter ff_vf_format = {
  139. .name = "format",
  140. .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
  141. .init = init,
  142. .uninit = uninit,
  143. .query_formats = query_formats,
  144. .priv_size = sizeof(FormatContext),
  145. .priv_class = &format_class,
  146. .inputs = avfilter_vf_format_inputs,
  147. .outputs = avfilter_vf_format_outputs,
  148. };
  149. #endif /* CONFIG_FORMAT_FILTER */
  150. #if CONFIG_NOFORMAT_FILTER
  151. #define noformat_options options
  152. AVFILTER_DEFINE_CLASS(noformat);
  153. static const AVFilterPad avfilter_vf_noformat_inputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_VIDEO,
  157. .get_video_buffer = ff_null_get_video_buffer,
  158. },
  159. { NULL }
  160. };
  161. static const AVFilterPad avfilter_vf_noformat_outputs[] = {
  162. {
  163. .name = "default",
  164. .type = AVMEDIA_TYPE_VIDEO
  165. },
  166. { NULL }
  167. };
  168. AVFilter ff_vf_noformat = {
  169. .name = "noformat",
  170. .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
  171. .init = init,
  172. .uninit = uninit,
  173. .query_formats = query_formats,
  174. .priv_size = sizeof(FormatContext),
  175. .priv_class = &noformat_class,
  176. .inputs = avfilter_vf_noformat_inputs,
  177. .outputs = avfilter_vf_noformat_outputs,
  178. };
  179. #endif /* CONFIG_NOFORMAT_FILTER */