WMFUtils.h (5275B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef WMFUtils_h 8 #define WMFUtils_h 9 10 #include <hstring.h> 11 #include <winstring.h> 12 13 #include "ImageTypes.h" 14 #include "PlatformDecoderModule.h" 15 #include "TimeUnits.h" 16 #include "VideoUtils.h" 17 #include "WMF.h" 18 #include "mozilla/DefineEnum.h" 19 #include "mozilla/gfx/Rect.h" 20 #include "nsString.h" 21 22 // Various utilities shared by WMF backend files. 23 24 namespace mozilla { 25 26 namespace gfx { 27 enum class ColorDepth : uint8_t; 28 } 29 extern LazyLogModule sPDMLog; 30 31 #define LOG_AND_WARNING_PDM(msg, ...) \ 32 do { \ 33 NS_WARNING(nsPrintfCString(msg, rv).get()); \ 34 MOZ_LOG(sPDMLog, LogLevel::Debug, \ 35 ("%s:%d, " msg, __FILE__, __LINE__, ##__VA_ARGS__)); \ 36 } while (false) 37 38 #ifndef RETURN_IF_FAILED 39 # define RETURN_IF_FAILED(x) \ 40 do { \ 41 HRESULT rv = x; \ 42 if (MOZ_UNLIKELY(FAILED(rv))) { \ 43 LOG_AND_WARNING_PDM("(" #x ") failed, rv=%lx", rv); \ 44 return rv; \ 45 } \ 46 } while (false) 47 #endif 48 49 #ifndef RETURN_PARAM_IF_FAILED 50 # define RETURN_PARAM_IF_FAILED(x, defaultOut) \ 51 do { \ 52 HRESULT rv = x; \ 53 if (MOZ_UNLIKELY(FAILED(rv))) { \ 54 LOG_AND_WARNING_PDM("(" #x ") failed, rv=%lx", rv); \ 55 return defaultOut; \ 56 } \ 57 } while (false) 58 #endif 59 60 static const GUID CLSID_MSOpusDecoder = { 61 0x63e17c10, 62 0x2d43, 63 0x4c42, 64 {0x8f, 0xe3, 0x8d, 0x8b, 0x63, 0xe4, 0x6a, 0x6a}}; 65 66 // Media types supported by Media Foundation. 67 MOZ_DEFINE_ENUM_CLASS_WITH_TOSTRING(WMFStreamType, 68 (Unknown, H264, VP8, VP9, AV1, HEVC, MP3, 69 AAC, OPUS, VORBIS, SENTINEL)); 70 71 bool StreamTypeIsVideo(const WMFStreamType& aType); 72 73 bool StreamTypeIsAudio(const WMFStreamType& aType); 74 75 WMFStreamType GetStreamTypeFromMimeType(const nsCString& aMimeType); 76 77 GUID GetOutputSubType(const gfx::ColorDepth& aColorDepth, 78 bool aIsHardwareDecoding); 79 80 nsCString GetSubTypeStr(const GUID& aSubtype); 81 82 // Converts from microseconds to hundreds of nanoseconds. 83 // We use microseconds for our timestamps, whereas WMF uses 84 // hundreds of nanoseconds. 85 inline int64_t UsecsToHNs(int64_t aUsecs) { return aUsecs * 10; } 86 87 // Converts from hundreds of nanoseconds to microseconds. 88 // We use microseconds for our timestamps, whereas WMF uses 89 // hundreds of nanoseconds. 90 inline int64_t HNsToUsecs(int64_t hNanoSecs) { return hNanoSecs / 10; } 91 92 HRESULT HNsToFrames(int64_t aHNs, uint32_t aRate, int64_t* aOutFrames); 93 94 HRESULT 95 GetDefaultStride(IMFMediaType* aType, uint32_t aWidth, uint32_t* aOutStride); 96 97 Maybe<gfx::YUVColorSpace> GetYUVColorSpace(IMFMediaType* aType); 98 99 int32_t MFOffsetToInt32(const MFOffset& aOffset); 100 101 // Gets the sub-region of the video frame that should be displayed. 102 // See: 103 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb530115(v=vs.85).aspx 104 HRESULT 105 GetPictureRegion(IMFMediaType* aMediaType, gfx::IntRect& aOutPictureRegion); 106 107 // Returns the duration of a IMFSample in TimeUnit. 108 // Returns media::TimeUnit::Invalid() on failure. 109 media::TimeUnit GetSampleDuration(IMFSample* aSample); 110 111 // Returns the presentation time of a IMFSample in TimeUnit. 112 // Returns media::TimeUnit::Invalid() on failure. 113 media::TimeUnit GetSampleTime(IMFSample* aSample); 114 115 inline bool IsFlagSet(DWORD flags, DWORD pattern) { 116 return (flags & pattern) == pattern; 117 } 118 119 // Will return %ProgramW6432% value as per: 120 // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx 121 nsString GetProgramW6432Path(); 122 123 const char* MFTMessageTypeToStr(MFT_MESSAGE_TYPE aMsg); 124 125 GUID AudioMimeTypeToMediaFoundationSubtype(const nsACString& aMimeType); 126 127 GUID VideoMimeTypeToMediaFoundationSubtype(const nsACString& aMimeType); 128 129 void AACAudioSpecificConfigToUserData(uint8_t aAACProfileLevelIndication, 130 const uint8_t* aAudioSpecConfig, 131 uint32_t aConfigLength, 132 nsTArray<BYTE>& aOutUserData); 133 134 class ScopedHString final { 135 public: 136 explicit ScopedHString(const nsAString& aStr) { 137 WindowsCreateString(PromiseFlatString(aStr).get(), aStr.Length(), &mString); 138 } 139 explicit ScopedHString(const WCHAR aCharArray[]) { 140 WindowsCreateString(aCharArray, wcslen(aCharArray), &mString); 141 } 142 ~ScopedHString() { WindowsDeleteString(mString); } 143 const HSTRING& Get() { return mString; } 144 145 private: 146 HSTRING mString; 147 }; 148 149 } // namespace mozilla 150 151 #endif