MFTDecoder.h (6167B)
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 #if !defined(MFTDecoder_h_) 8 # define MFTDecoder_h_ 9 10 # include "WMF.h" 11 # include "mozilla/ReentrantMonitor.h" 12 # include "mozilla/RefPtr.h" 13 # include "nsIThread.h" 14 15 namespace mozilla { 16 17 class MFTDecoder final { 18 ~MFTDecoder(); 19 20 public: 21 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MFTDecoder) 22 23 MFTDecoder(); 24 25 // Creates the MFT by COM class ID. 26 // 27 // Params: 28 // - aCLSID The COM class ID of the decoder. 29 HRESULT Create(const GUID& aCLSID); 30 31 // Creates the MFT by querying a category and media subtype. 32 // First thing to do as part of setup. 33 // 34 // Params: 35 // - aCategory the GUID of the MFT category to use. 36 // - aInSubType the GUID of the input MFT media type to use. 37 // GUID_NULL may be used as a wildcard. 38 // - aOutSubType the GUID of the output MFT media type to use. 39 // GUID_NULL may be used as a wildcard. 40 HRESULT Create(const GUID& aCategory, const GUID& aInSubtype, 41 const GUID& aOutSubtype); 42 43 // Sets the input and output media types. Call after Init(). 44 // 45 // Params: 46 // - aInputType needs at least major and minor types set. 47 // - aOutputType needs at least major and minor types set. 48 // This is used to select the matching output type out 49 // of all the available output types of the MFT. 50 // - aFallbackSubType is a preferred subtype to fall back to if the currently 51 // selected subtype in aOutputType is unavailable, if this is GUID_NULL 52 // then no attempt to fallback will occur, if it is not GUID_NULL then it 53 // will be searched for as a preferred fallback, and if not found the last 54 // subtype available will be chosen as a final fallback. 55 HRESULT SetMediaTypes( 56 IMFMediaType* aInputType, IMFMediaType* aOutputType, 57 const GUID& aFallbackSubType, 58 std::function<HRESULT(IMFMediaType*)>&& aCallback = 59 [](IMFMediaType* aOutput) { return S_OK; }); 60 61 // Returns the MFT's global IMFAttributes object. 62 already_AddRefed<IMFAttributes> GetAttributes(); 63 64 // Returns the MFT's IMFAttributes object for an output stream. 65 already_AddRefed<IMFAttributes> GetOutputStreamAttributes(); 66 67 // Retrieves the media type being input. 68 HRESULT GetInputMediaType(RefPtr<IMFMediaType>& aMediaType); 69 70 // Retrieves the media type being output. This may not be valid until 71 // the first sample is decoded. 72 HRESULT GetOutputMediaType(RefPtr<IMFMediaType>& aMediaType); 73 const GUID& GetOutputMediaSubType() const { return mOutputSubType; } 74 75 // Submits data into the MFT for processing. 76 // 77 // Returns: 78 // - MF_E_NOTACCEPTING if the decoder can't accept input. The data 79 // must be resubmitted after Output() stops producing output. 80 HRESULT Input(const uint8_t* aData, uint32_t aDataSize, 81 int64_t aTimestampUsecs, int64_t aDurationUsecs); 82 HRESULT Input(IMFSample* aSample); 83 84 HRESULT CreateInputSample(const uint8_t* aData, uint32_t aDataSize, 85 int64_t aTimestampUsecs, int64_t aDurationUsecs, 86 RefPtr<IMFSample>* aOutSample); 87 88 // Retrieves output from the MFT. Call this once Input() returns 89 // MF_E_NOTACCEPTING. Some MFTs with hardware acceleration (the H.264 90 // decoder MFT in particular) can't handle it if clients hold onto 91 // references to the output IMFSample, so don't do that. 92 // 93 // Returns: 94 // - MF_E_TRANSFORM_STREAM_CHANGE if the underlying stream output 95 // type changed. Retrieve the output media type and reconfig client, 96 // else you may misinterpret the MFT's output. 97 // - MF_E_TRANSFORM_NEED_MORE_INPUT if no output can be produced 98 // due to lack of input. 99 // - S_OK if an output frame is produced. 100 HRESULT Output(RefPtr<IMFSample>* aOutput); 101 102 // Sends a flush message to the MFT. This causes it to discard all 103 // input data. Use before seeking. 104 HRESULT Flush(); 105 106 // Sends a message to the MFT. 107 HRESULT SendMFTMessage(MFT_MESSAGE_TYPE aMsg, ULONG_PTR aData); 108 109 // This method first attempts to find the provided aSubType in the compatible 110 // list reported by the decoder, if found it will be set up, otherwise it will 111 // search for the preferred subtype aFallbackSubType, and if that is also not 112 // found the last available subtype is set up. 113 // 114 // aFallbackSubType can be GUID_NULL to cause this to return E_FAIL when 115 // aSubType is not found, avoiding fallback behaviors. 116 HRESULT FindDecoderOutputTypeWithSubtype(const GUID& aSubType, 117 const GUID& aFallbackSubType); 118 HRESULT FindDecoderOutputType(const GUID& aFallbackSubType); 119 120 private: 121 // Will search a suitable MediaType using aTypeToUse if set, if not will 122 // use the current mOutputType. 123 // 124 // When aSubType (or the current mOutputType) is not found, it will search for 125 // aFallbackSubType instead, and if not is not found it will use the last 126 // available compatible type reported by the decoder. 127 // 128 // aFallbackSubType can be GUID_NULL to cause this to return E_FAIL when 129 // aSubType (or the current mOutputType) is not found, avoiding fallbacks. 130 HRESULT SetDecoderOutputType( 131 const GUID& aSubType, const GUID& aFallbackSubType, 132 IMFMediaType* aTypeToUse, 133 std::function<HRESULT(IMFMediaType*)>&& aCallback); 134 HRESULT CreateOutputSample(RefPtr<IMFSample>* aOutSample); 135 136 MFT_INPUT_STREAM_INFO mInputStreamInfo; 137 MFT_OUTPUT_STREAM_INFO mOutputStreamInfo; 138 139 RefPtr<IMFActivate> mActivate; 140 RefPtr<IMFTransform> mDecoder; 141 142 RefPtr<IMFMediaType> mOutputType; 143 GUID mOutputSubType; 144 145 // Either MFMediaType_Audio or MFMediaType_Video. 146 GUID mMajorType; 147 148 // True if the IMFTransform allocates the samples that it returns. 149 bool mMFTProvidesOutputSamples = false; 150 151 // True if we need to mark the next sample as a discontinuity. 152 bool mDiscontinuity = true; 153 }; 154 155 } // namespace mozilla 156 157 #endif