ffmpeg-utils.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "ffmpeg-utils.hpp"
  15. #include <unordered_map>
  16. #include <unordered_set>
  17. extern "C" {
  18. #include <libavformat/avformat.h>
  19. }
  20. using namespace std;
  21. vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format, bool ignore_compatibility)
  22. {
  23. vector<FFmpegCodec> codecs;
  24. const AVCodec *codec;
  25. void *i = 0;
  26. while ((codec = av_codec_iterate(&i)) != nullptr) {
  27. // Not an encoding codec
  28. if (!av_codec_is_encoder(codec))
  29. continue;
  30. // Skip if not supported and compatibility check not disabled
  31. if (!ignore_compatibility && !av_codec_get_tag(format.codec_tags, codec->id)) {
  32. continue;
  33. }
  34. codecs.emplace_back(codec);
  35. }
  36. return codecs;
  37. }
  38. static bool is_output_device(const AVClass *avclass)
  39. {
  40. if (!avclass)
  41. return false;
  42. switch (avclass->category) {
  43. case AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT:
  44. case AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT:
  45. case AV_CLASS_CATEGORY_DEVICE_OUTPUT:
  46. return true;
  47. default:
  48. return false;
  49. }
  50. }
  51. vector<FFmpegFormat> GetSupportedFormats()
  52. {
  53. vector<FFmpegFormat> formats;
  54. const AVOutputFormat *output_format;
  55. void *i = 0;
  56. while ((output_format = av_muxer_iterate(&i)) != nullptr) {
  57. if (is_output_device(output_format->priv_class))
  58. continue;
  59. formats.emplace_back(output_format);
  60. }
  61. return formats;
  62. }
  63. FFmpegCodec FFmpegFormat::GetDefaultEncoder(FFmpegCodecType codec_type) const
  64. {
  65. const AVCodecID codec_id = codec_type == VIDEO ? video_codec : audio_codec;
  66. if (codec_type == UNKNOWN || codec_id == AV_CODEC_ID_NONE)
  67. return {};
  68. if (auto codec = avcodec_find_encoder(codec_id))
  69. return {codec};
  70. /* Fall back to using the format name as the encoder,
  71. * this works for some formats such as FLV. */
  72. return FFmpegCodec{name, codec_id, codec_type};
  73. }
  74. bool FFCodecAndFormatCompatible(const char *codec, const char *format)
  75. {
  76. if (!codec || !format)
  77. return false;
  78. const AVOutputFormat *output_format = av_guess_format(format, nullptr, nullptr);
  79. if (!output_format)
  80. return false;
  81. const AVCodecDescriptor *codec_desc = avcodec_descriptor_get_by_name(codec);
  82. if (!codec_desc)
  83. return false;
  84. return avformat_query_codec(output_format, codec_desc->id, FF_COMPLIANCE_NORMAL) == 1;
  85. }
  86. static const unordered_set<string> builtin_codecs = {
  87. "h264", "hevc", "av1", "prores", "aac", "opus", "alac", "flac", "pcm_s16le", "pcm_s24le", "pcm_f32le",
  88. };
  89. bool IsBuiltinCodec(const char *codec)
  90. {
  91. return builtin_codecs.count(codec) > 0;
  92. }
  93. static const unordered_map<string, unordered_set<string>> codec_compat = {
  94. // Technically our muxer supports HEVC and AV1 as well, but nothing else does
  95. {"flv",
  96. {
  97. "h264",
  98. "aac",
  99. }},
  100. {"mpegts",
  101. {
  102. "h264",
  103. "hevc",
  104. "aac",
  105. "opus",
  106. }},
  107. {"hls",
  108. // Also using MPEG-TS in our case, but no Opus support
  109. {
  110. "h264",
  111. "hevc",
  112. "aac",
  113. }},
  114. {"mp4",
  115. {
  116. "h264",
  117. "hevc",
  118. "av1",
  119. "aac",
  120. "opus",
  121. "alac",
  122. "flac",
  123. "pcm_s16le",
  124. "pcm_s24le",
  125. "pcm_f32le",
  126. }},
  127. {"fragmented_mp4",
  128. {
  129. "h264",
  130. "hevc",
  131. "av1",
  132. "aac",
  133. "opus",
  134. "alac",
  135. "flac",
  136. "pcm_s16le",
  137. "pcm_s24le",
  138. "pcm_f32le",
  139. }},
  140. // Not part of FFmpeg, see obs-outputs module
  141. {"hybrid_mp4",
  142. {
  143. "h264",
  144. "hevc",
  145. "av1",
  146. "aac",
  147. "opus",
  148. "alac",
  149. "flac",
  150. "pcm_s16le",
  151. "pcm_s24le",
  152. "pcm_f32le",
  153. }},
  154. {"mov",
  155. {
  156. "h264",
  157. "hevc",
  158. "prores",
  159. "aac",
  160. "alac",
  161. "pcm_s16le",
  162. "pcm_s24le",
  163. "pcm_f32le",
  164. }},
  165. {"fragmented_mov",
  166. {
  167. "h264",
  168. "hevc",
  169. "prores",
  170. "aac",
  171. "alac",
  172. "pcm_s16le",
  173. "pcm_s24le",
  174. "pcm_f32le",
  175. }},
  176. // MKV supports everything
  177. {"mkv", {}},
  178. };
  179. bool ContainerSupportsCodec(const string &container, const string &codec)
  180. {
  181. auto iter = codec_compat.find(container);
  182. if (iter == codec_compat.end())
  183. return false;
  184. auto codecs = iter->second;
  185. // Assume everything is supported
  186. if (codecs.empty())
  187. return true;
  188. return codecs.count(codec) > 0;
  189. }