FFmpegCodec.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "FFmpegCodec.hpp"
  15. #include "FFmpegFormat.hpp"
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. using namespace std;
  19. vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format, bool ignore_compatibility)
  20. {
  21. vector<FFmpegCodec> codecs;
  22. const AVCodec *codec;
  23. void *i = 0;
  24. while ((codec = av_codec_iterate(&i)) != nullptr) {
  25. // Not an encoding codec
  26. if (!av_codec_is_encoder(codec))
  27. continue;
  28. // Skip if not supported and compatibility check not disabled
  29. if (!ignore_compatibility && !av_codec_get_tag(format.codec_tags, codec->id)) {
  30. continue;
  31. }
  32. codecs.emplace_back(codec);
  33. }
  34. return codecs;
  35. }
  36. bool FFCodecAndFormatCompatible(const char *codec, const char *format)
  37. {
  38. if (!codec || !format)
  39. return false;
  40. const AVOutputFormat *output_format = av_guess_format(format, nullptr, nullptr);
  41. if (!output_format)
  42. return false;
  43. const AVCodecDescriptor *codec_desc = avcodec_descriptor_get_by_name(codec);
  44. if (!codec_desc)
  45. return false;
  46. return avformat_query_codec(output_format, codec_desc->id, FF_COMPLIANCE_NORMAL) == 1;
  47. }
  48. static const unordered_set<string> builtin_codecs = {
  49. "h264", "hevc", "av1", "prores", "aac", "opus", "alac", "flac", "pcm_s16le", "pcm_s24le", "pcm_f32le",
  50. };
  51. bool IsBuiltinCodec(const char *codec)
  52. {
  53. return builtin_codecs.count(codec) > 0;
  54. }
  55. static const unordered_map<string, unordered_set<string>> codec_compat = {
  56. // Technically our muxer supports HEVC and AV1 as well, but nothing else does
  57. {"flv",
  58. {
  59. "h264",
  60. "aac",
  61. }},
  62. {"mpegts",
  63. {
  64. "h264",
  65. "hevc",
  66. "aac",
  67. "opus",
  68. }},
  69. {"hls",
  70. // Also using MPEG-TS in our case, but no Opus support
  71. {
  72. "h264",
  73. "hevc",
  74. "aac",
  75. }},
  76. {"mp4",
  77. {
  78. "h264",
  79. "hevc",
  80. "av1",
  81. "aac",
  82. "opus",
  83. "alac",
  84. "flac",
  85. "pcm_s16le",
  86. "pcm_s24le",
  87. "pcm_f32le",
  88. }},
  89. {"fragmented_mp4",
  90. {
  91. "h264",
  92. "hevc",
  93. "av1",
  94. "aac",
  95. "opus",
  96. "alac",
  97. "flac",
  98. "pcm_s16le",
  99. "pcm_s24le",
  100. "pcm_f32le",
  101. }},
  102. // Not part of FFmpeg, see obs-outputs module
  103. {"hybrid_mp4",
  104. {
  105. "h264",
  106. "hevc",
  107. "av1",
  108. "aac",
  109. "opus",
  110. "alac",
  111. "flac",
  112. "pcm_s16le",
  113. "pcm_s24le",
  114. "pcm_f32le",
  115. }},
  116. // Not part of FFmpeg, see obs-outputs module
  117. {"hybrid_mov",
  118. {
  119. "h264",
  120. "hevc",
  121. "prores",
  122. "aac",
  123. "alac",
  124. "pcm_s16le",
  125. "pcm_s24le",
  126. "pcm_f32le",
  127. }},
  128. {"mov",
  129. {
  130. "h264",
  131. "hevc",
  132. "prores",
  133. "aac",
  134. "alac",
  135. "pcm_s16le",
  136. "pcm_s24le",
  137. "pcm_f32le",
  138. }},
  139. {"fragmented_mov",
  140. {
  141. "h264",
  142. "hevc",
  143. "prores",
  144. "aac",
  145. "alac",
  146. "pcm_s16le",
  147. "pcm_s24le",
  148. "pcm_f32le",
  149. }},
  150. // MKV supports everything
  151. {"mkv", {}},
  152. };
  153. bool ContainerSupportsCodec(const string &container, const string &codec)
  154. {
  155. auto iter = codec_compat.find(container);
  156. if (iter == codec_compat.end())
  157. return false;
  158. auto codecs = iter->second;
  159. // Assume everything is supported
  160. if (codecs.empty())
  161. return true;
  162. return codecs.count(codec) > 0;
  163. }