ffmpeg-utils.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 FFmpegCodec;
  41. struct FFmpegFormat {
  42. const char *name;
  43. const char *long_name;
  44. const char *mime_type;
  45. const char *extensions;
  46. AVCodecID audio_codec;
  47. AVCodecID video_codec;
  48. const AVCodecTag *const *codec_tags;
  49. FFmpegFormat() = default;
  50. FFmpegFormat(const char *name, const char *mime_type)
  51. : name(name),
  52. long_name(nullptr),
  53. mime_type(mime_type),
  54. extensions(nullptr),
  55. audio_codec(AV_CODEC_ID_NONE),
  56. video_codec(AV_CODEC_ID_NONE),
  57. codec_tags(nullptr)
  58. {
  59. }
  60. FFmpegFormat(const AVOutputFormat *av_format)
  61. : name(av_format->name),
  62. long_name(av_format->long_name),
  63. mime_type(av_format->mime_type),
  64. extensions(av_format->extensions),
  65. audio_codec(av_format->audio_codec),
  66. video_codec(av_format->video_codec),
  67. codec_tags(av_format->codec_tag)
  68. {
  69. }
  70. FFmpegCodec GetDefaultEncoder(FFmpegCodecType codec_type) const;
  71. bool HasAudio() const { return audio_codec != AV_CODEC_ID_NONE; }
  72. bool HasVideo() const { return video_codec != AV_CODEC_ID_NONE; }
  73. bool operator==(const FFmpegFormat &format) const
  74. {
  75. if (!strequal(name, format.name))
  76. return false;
  77. return strequal(mime_type, format.mime_type);
  78. }
  79. };
  80. Q_DECLARE_METATYPE(FFmpegFormat)
  81. struct FFmpegCodec {
  82. const char *name;
  83. const char *long_name;
  84. int id;
  85. FFmpegCodecType type;
  86. FFmpegCodec() = default;
  87. FFmpegCodec(const char *name, int id, FFmpegCodecType type = UNKNOWN)
  88. : name(name),
  89. long_name(nullptr),
  90. id(id),
  91. type(type)
  92. {
  93. }
  94. FFmpegCodec(const AVCodec *codec) : name(codec->name), long_name(codec->long_name), id(codec->id)
  95. {
  96. switch (codec->type) {
  97. case AVMEDIA_TYPE_AUDIO:
  98. type = AUDIO;
  99. break;
  100. case AVMEDIA_TYPE_VIDEO:
  101. type = VIDEO;
  102. break;
  103. default:
  104. type = UNKNOWN;
  105. }
  106. }
  107. bool operator==(const FFmpegCodec &codec) const
  108. {
  109. if (id != codec.id)
  110. return false;
  111. return strequal(name, codec.name);
  112. }
  113. };
  114. Q_DECLARE_METATYPE(FFmpegCodec)
  115. std::vector<FFmpegFormat> GetSupportedFormats();
  116. std::vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format, bool ignore_compatibility);
  117. bool FFCodecAndFormatCompatible(const char *codec, const char *format);
  118. bool IsBuiltinCodec(const char *codec);
  119. bool ContainerSupportsCodec(const std::string &container, const std::string &codec);