vf_format.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ff_set_common_formats(ctx, formats);
  114. return 0;
  115. }
  116. #define OFFSET(x) offsetof(FormatContext, x)
  117. static const AVOption options[] = {
  118. { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
  119. { NULL }
  120. };
  121. #if CONFIG_FORMAT_FILTER
  122. #define format_options options
  123. AVFILTER_DEFINE_CLASS(format);
  124. static const AVFilterPad avfilter_vf_format_inputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_VIDEO,
  128. .get_video_buffer = ff_null_get_video_buffer,
  129. },
  130. { NULL }
  131. };
  132. static const AVFilterPad avfilter_vf_format_outputs[] = {
  133. {
  134. .name = "default",
  135. .type = AVMEDIA_TYPE_VIDEO
  136. },
  137. { NULL }
  138. };
  139. AVFilter ff_vf_format = {
  140. .name = "format",
  141. .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
  142. .init = init,
  143. .uninit = uninit,
  144. .query_formats = query_formats,
  145. .priv_size = sizeof(FormatContext),
  146. .priv_class = &format_class,
  147. .inputs = avfilter_vf_format_inputs,
  148. .outputs = avfilter_vf_format_outputs,
  149. };
  150. #endif /* CONFIG_FORMAT_FILTER */
  151. #if CONFIG_NOFORMAT_FILTER
  152. #define noformat_options options
  153. AVFILTER_DEFINE_CLASS(noformat);
  154. static const AVFilterPad avfilter_vf_noformat_inputs[] = {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_VIDEO,
  158. .get_video_buffer = ff_null_get_video_buffer,
  159. },
  160. { NULL }
  161. };
  162. static const AVFilterPad avfilter_vf_noformat_outputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO
  166. },
  167. { NULL }
  168. };
  169. AVFilter ff_vf_noformat = {
  170. .name = "noformat",
  171. .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
  172. .init = init,
  173. .uninit = uninit,
  174. .query_formats = query_formats,
  175. .priv_size = sizeof(FormatContext),
  176. .priv_class = &noformat_class,
  177. .inputs = avfilter_vf_noformat_inputs,
  178. .outputs = avfilter_vf_noformat_outputs,
  179. };
  180. #endif /* CONFIG_NOFORMAT_FILTER */