vf_format.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 libavfilter/vf_format.c
  22. * format and noformat video filters
  23. */
  24. #include "avfilter.h"
  25. typedef struct {
  26. /**
  27. * List of flags telling if a given image format has been listed
  28. * as argument to the filter.
  29. */
  30. int listed_pix_fmt_flags[PIX_FMT_NB];
  31. } FormatContext;
  32. #define PIX_FMT_NAME_MAXSIZE 32
  33. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  34. {
  35. FormatContext *format = ctx->priv;
  36. const char *cur, *sep;
  37. char pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
  38. int pix_fmt_name_len;
  39. enum PixelFormat pix_fmt;
  40. /* parse the list of formats */
  41. for (cur = args; cur; cur = sep ? sep+1 : NULL) {
  42. if (!(sep = strchr(cur, ':')))
  43. pix_fmt_name_len = strlen(cur);
  44. else
  45. pix_fmt_name_len = sep - cur;
  46. if (pix_fmt_name_len >= PIX_FMT_NAME_MAXSIZE) {
  47. av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
  48. return -1;
  49. }
  50. memcpy(pix_fmt_name, cur, pix_fmt_name_len);
  51. pix_fmt_name[pix_fmt_name_len] = 0;
  52. pix_fmt = avcodec_get_pix_fmt(pix_fmt_name);
  53. if (pix_fmt == PIX_FMT_NONE) {
  54. av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
  55. return -1;
  56. }
  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. static int query_formats_format(AVFilterContext *ctx)
  73. {
  74. avfilter_set_common_formats(ctx, make_format_list(ctx->priv, 1));
  75. return 0;
  76. }
  77. static int query_formats_noformat(AVFilterContext *ctx)
  78. {
  79. avfilter_set_common_formats(ctx, make_format_list(ctx->priv, 0));
  80. return 0;
  81. }
  82. static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
  83. int w, int h)
  84. {
  85. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  86. }
  87. static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  88. {
  89. avfilter_start_frame(link->dst->outputs[0], picref);
  90. }
  91. static void end_frame(AVFilterLink *link)
  92. {
  93. avfilter_end_frame(link->dst->outputs[0]);
  94. }
  95. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  96. {
  97. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  98. }
  99. AVFilter avfilter_vf_format = {
  100. .name = "format",
  101. .description = "Convert the input video to one of the specified pixel formats.",
  102. .init = init,
  103. .query_formats = query_formats_format,
  104. .priv_size = sizeof(FormatContext),
  105. .inputs = (AVFilterPad[]) {{ .name = "default",
  106. .type = CODEC_TYPE_VIDEO,
  107. .get_video_buffer= get_video_buffer,
  108. .start_frame = start_frame,
  109. .draw_slice = draw_slice,
  110. .end_frame = end_frame, },
  111. { .name = NULL}},
  112. .outputs = (AVFilterPad[]) {{ .name = "default",
  113. .type = CODEC_TYPE_VIDEO },
  114. { .name = NULL}},
  115. };
  116. AVFilter avfilter_vf_noformat = {
  117. .name = "noformat",
  118. .description = "Force libavfilter not to use any of the specified pixel formats for the input to the next filter.",
  119. .init = init,
  120. .query_formats = query_formats_noformat,
  121. .priv_size = sizeof(FormatContext),
  122. .inputs = (AVFilterPad[]) {{ .name = "default",
  123. .type = CODEC_TYPE_VIDEO,
  124. .get_video_buffer= get_video_buffer,
  125. .start_frame = start_frame,
  126. .draw_slice = draw_slice,
  127. .end_frame = end_frame, },
  128. { .name = NULL}},
  129. .outputs = (AVFilterPad[]) {{ .name = "default",
  130. .type = CODEC_TYPE_VIDEO },
  131. { .name = NULL}},
  132. };