ffmpeg-utils.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. const char *GetDefaultName(FFmpegCodecType codec_type) const;
  50. bool HasAudio() const { return audio_codec != AV_CODEC_ID_NONE; }
  51. bool HasVideo() const { return video_codec != AV_CODEC_ID_NONE; }
  52. bool operator==(const FFmpegFormat &format) const
  53. {
  54. if (!strequal(name, format.name))
  55. return false;
  56. return strequal(mime_type, format.mime_type);
  57. }
  58. };
  59. Q_DECLARE_METATYPE(FFmpegFormat)
  60. struct FFmpegCodec {
  61. const char *name;
  62. const char *long_name;
  63. int id;
  64. bool alias;
  65. const char *base_name;
  66. FFmpegCodecType type;
  67. FFmpegCodec() = default;
  68. bool operator==(const FFmpegCodec &codec) const
  69. {
  70. if (id != codec.id)
  71. return false;
  72. return strequal(name, codec.name);
  73. }
  74. };
  75. Q_DECLARE_METATYPE(FFmpegCodec)
  76. std::vector<FFmpegFormat> GetSupportedFormats();
  77. std::vector<FFmpegCodec> GetFormatCodecs(const FFmpegFormat &format,
  78. bool ignore_compatibility);
  79. bool FFCodecAndFormatCompatible(const char *codec, const char *format);
  80. bool IsBuiltinCodec(const char *codec);
  81. bool ContainerSupportsCodec(const std::string &container,
  82. const std::string &codec);