vf_format.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. if (!s->pix_fmts) {
  54. av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
  55. return AVERROR(EINVAL);
  56. }
  57. /* count the formats */
  58. cur = s->pix_fmts;
  59. while ((cur = strchr(cur, '|'))) {
  60. nb_formats++;
  61. if (*cur)
  62. cur++;
  63. }
  64. s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
  65. if (!s->formats)
  66. return AVERROR(ENOMEM);
  67. /* parse the list of formats */
  68. cur = s->pix_fmts;
  69. for (i = 0; i < nb_formats; i++) {
  70. sep = strchr(cur, '|');
  71. if (sep)
  72. *sep++ = 0;
  73. s->formats[i] = av_get_pix_fmt(cur);
  74. if (s->formats[i] == AV_PIX_FMT_NONE) {
  75. av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
  76. return AVERROR(EINVAL);
  77. }
  78. cur = sep;
  79. }
  80. s->formats[nb_formats] = AV_PIX_FMT_NONE;
  81. if (!strcmp(ctx->filter->name, "noformat")) {
  82. const AVPixFmtDescriptor *desc = NULL;
  83. enum AVPixelFormat *formats_allowed;
  84. int nb_formats_lavu = 0, nb_formats_allowed = 0;;
  85. /* count the formats known to lavu */
  86. while ((desc = av_pix_fmt_desc_next(desc)))
  87. nb_formats_lavu++;
  88. formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
  89. if (!formats_allowed)
  90. return AVERROR(ENOMEM);
  91. /* for each format known to lavu, check if it's in the list of
  92. * forbidden formats */
  93. while ((desc = av_pix_fmt_desc_next(desc))) {
  94. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
  95. for (i = 0; i < nb_formats; i++) {
  96. if (s->formats[i] == pix_fmt)
  97. break;
  98. }
  99. if (i < nb_formats)
  100. continue;
  101. formats_allowed[nb_formats_allowed++] = pix_fmt;
  102. }
  103. formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
  104. av_freep(&s->formats);
  105. s->formats = formats_allowed;
  106. }
  107. return 0;
  108. }
  109. static int query_formats(AVFilterContext *ctx)
  110. {
  111. FormatContext *s = ctx->priv;
  112. AVFilterFormats *formats = ff_make_format_list(s->formats);
  113. if (!formats)
  114. return AVERROR(ENOMEM);
  115. ff_set_common_formats(ctx, formats);
  116. return 0;
  117. }
  118. #define OFFSET(x) offsetof(FormatContext, x)
  119. static const AVOption options[] = {
  120. { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
  121. { NULL },
  122. };
  123. #if CONFIG_FORMAT_FILTER
  124. static const AVClass format_class = {
  125. .class_name = "format",
  126. .item_name = av_default_item_name,
  127. .option = options,
  128. .version = LIBAVUTIL_VERSION_INT,
  129. };
  130. static const AVFilterPad avfilter_vf_format_inputs[] = {
  131. {
  132. .name = "default",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .get_video_buffer = ff_null_get_video_buffer,
  135. },
  136. { NULL }
  137. };
  138. static const AVFilterPad avfilter_vf_format_outputs[] = {
  139. {
  140. .name = "default",
  141. .type = AVMEDIA_TYPE_VIDEO
  142. },
  143. { NULL }
  144. };
  145. AVFilter ff_vf_format = {
  146. .name = "format",
  147. .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
  148. .init = init,
  149. .uninit = uninit,
  150. .query_formats = query_formats,
  151. .priv_size = sizeof(FormatContext),
  152. .priv_class = &format_class,
  153. .inputs = avfilter_vf_format_inputs,
  154. .outputs = avfilter_vf_format_outputs,
  155. };
  156. #endif /* CONFIG_FORMAT_FILTER */
  157. #if CONFIG_NOFORMAT_FILTER
  158. static const AVClass noformat_class = {
  159. .class_name = "noformat",
  160. .item_name = av_default_item_name,
  161. .option = options,
  162. .version = LIBAVUTIL_VERSION_INT,
  163. };
  164. static const AVFilterPad avfilter_vf_noformat_inputs[] = {
  165. {
  166. .name = "default",
  167. .type = AVMEDIA_TYPE_VIDEO,
  168. .get_video_buffer = ff_null_get_video_buffer,
  169. },
  170. { NULL }
  171. };
  172. static const AVFilterPad avfilter_vf_noformat_outputs[] = {
  173. {
  174. .name = "default",
  175. .type = AVMEDIA_TYPE_VIDEO
  176. },
  177. { NULL }
  178. };
  179. AVFilter ff_vf_noformat = {
  180. .name = "noformat",
  181. .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
  182. .init = init,
  183. .uninit = uninit,
  184. .query_formats = query_formats,
  185. .priv_size = sizeof(FormatContext),
  186. .priv_class = &noformat_class,
  187. .inputs = avfilter_vf_noformat_inputs,
  188. .outputs = avfilter_vf_noformat_outputs,
  189. };
  190. #endif /* CONFIG_NOFORMAT_FILTER */