ffmpeg-utils.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /******************************************************************************
  2. Copyright (C) 2023 by Dennis Sädtler <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <qmetatype.h>
  16. #include <string>
  17. #include <vector>
  18. extern "C" {
  19. #include <libavcodec/avcodec.h>
  20. #include <libavdevice/avdevice.h>
  21. }
  22. enum FFmpegCodecType { AUDIO, VIDEO, UNKNOWN };
  23. /* This needs to handle a few special cases due to how the format is used in the UI:
  24. * - strequal(nullptr, "") must be true
  25. * - strequal("", nullptr) must be true
  26. * - strequal(nullptr, nullptr) must be true
  27. */
  28. static bool strequal(const char *a, const char *b)
  29. {
  30. if (!a && !b)
  31. return true;
  32. if (!a && *b == 0)
  33. return true;
  34. if (!b && *a == 0)
  35. return true;
  36. if (!a || !b)
  37. return false;
  38. return strcmp(a, b) == 0;
  39. }
  40. struct FFmpegFormat {
  41. const char *name;
  42. const char *long_name;
  43. const char *mime_type;
  44. const char *extensions;
  45. AVCodecID audio_codec;
  46. AVCodecID video_codec;
  47. const AVCodecTag *const *codec_tags;
  48. FFmpegFormat() = default;
  49. FFmpegFormat(const char *name, const char *mime_type)
  50. : name(name),
  51. mime_type(mime_type)
  52. {
  53. }
  54. FFmpegFormat(const AVOutputFormat *av_format)
  55. : name(av_format->name),
  56. long_name(av_format->long_name),
  57. mime_type(av_format->mime_type),
  58. extensions(av_format->extensions),
  59. audio_codec(av_format->audio_codec),
  60. video_codec(av_format->video_codec),
  61. codec_tags(av_format->codec_tag)
  62. {
  63. }
  64. const char *GetDefaultName(FFmpegCodecType codec_type) const;
  65. bool HasAudio() const { return audio_codec != AV_CODEC_ID_NONE; }
  66. bool HasVideo() const { return video_codec != AV_CODEC_ID_NONE; }
  67. bool operator==(const FFmpegFormat &format) const
  68. {
  69. if (!strequal(name, format.name))
  70. return false;
  71. return strequal(mime_type, format.mime_type);
  72. }
  73. };
  74. Q_DECLARE_METATYPE(FFmpegFormat)
  75. struct FFmpegCodec {
  76. const char *name;
  77. const char *long_name;
  78. int id;
  79. FFmpegCodecType type;
  80. FFmpegCodec() = default;
  81. FFmpegCodec(const char *name, int id, FFmpegCodecType type = UNKNOWN)
  82. : name(name),
  83. id(id),
  84. type(type)
  85. {
  86. }
  87. FFmpegCodec(const AVCodec *codec)
  88. : name(codec->name),
  89. long_name(codec->long_name),
  90. id(codec->id)
  91. {
  92. switch (codec->type) {
  93. case AVMEDIA_TYPE_AUDIO:
  94. type = AUDIO;
  95. break;
  96. case AVMEDIA_TYPE_VIDEO:
  97. type = VIDEO;
  98. break;
  99. default:
  100. type = UNKNOWN;
  101. }
  102. }
  103. bool operator==(const FFmpegCodec &codec) const
  104. {
  105. if (id != codec.id)
  106. return false;
  107. return strequal(name, codec.name);
  108. }
  109. };
  110. Q_DECLARE_METATYPE(FFmpegCodec)
  111. std::vector<FFmpegFormat> GetSupportedFormats();
  112. std::vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format,
  113. bool ignore_compatibility);
  114. bool FFCodecAndFormatCompatible(const char *codec, const char *format);
  115. bool IsBuiltinCodec(const char *codec);
  116. bool ContainerSupportsCodec(const std::string &container,
  117. const std::string &codec);