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. First thing to do as part of setup.
26   //
27   // Params:
28   //  - aMFTClsID the clsid used by CoCreateInstance to instantiate the
29   //    decoder MFT.
30   HRESULT Create(const GUID& aMFTClsID);
31 
32   // Sets the input and output media types. Call after Init().
33   //
34   // Params:
35   //  - aInputType needs at least major and minor types set.
36   //  - aOutputType needs at least major and minor types set.
37   //    This is used to select the matching output type out
38   //    of all the available output types of the MFT.
39   HRESULT SetMediaTypes(
40       IMFMediaType* aInputType, IMFMediaType* aOutputType,
41       std::function<HRESULT(IMFMediaType*)>&& aCallback =
42           [](IMFMediaType* aOutput) { return S_OK; });
43 
44   // Returns the MFT's IMFAttributes object.
45   already_AddRefed<IMFAttributes> GetAttributes();
46 
47   // Retrieves the media type being output. This may not be valid until
48   //  the first sample is decoded.
49   HRESULT GetOutputMediaType(RefPtr<IMFMediaType>& aMediaType);
GetOutputMediaSubType()50   const GUID& GetOutputMediaSubType() const { return mOutputSubType; }
51 
52   // Submits data into the MFT for processing.
53   //
54   // Returns:
55   //  - MF_E_NOTACCEPTING if the decoder can't accept input. The data
56   //    must be resubmitted after Output() stops producing output.
57   HRESULT Input(const uint8_t* aData, uint32_t aDataSize,
58                 int64_t aTimestampUsecs, int64_t aDurationUsecs);
59   HRESULT Input(IMFSample* aSample);
60 
61   HRESULT CreateInputSample(const uint8_t* aData, uint32_t aDataSize,
62                             int64_t aTimestampUsecs, int64_t aDurationUsecs,
63                             RefPtr<IMFSample>* aOutSample);
64 
65   // Retrieves output from the MFT. Call this once Input() returns
66   // MF_E_NOTACCEPTING. Some MFTs with hardware acceleration (the H.264
67   // decoder MFT in particular) can't handle it if clients hold onto
68   // references to the output IMFSample, so don't do that.
69   //
70   // Returns:
71   //  - MF_E_TRANSFORM_STREAM_CHANGE if the underlying stream output
72   //    type changed. Retrieve the output media type and reconfig client,
73   //    else you may misinterpret the MFT's output.
74   //  - MF_E_TRANSFORM_NEED_MORE_INPUT if no output can be produced
75   //    due to lack of input.
76   //  - S_OK if an output frame is produced.
77   HRESULT Output(RefPtr<IMFSample>* aOutput);
78 
79   // Sends a flush message to the MFT. This causes it to discard all
80   // input data. Use before seeking.
81   HRESULT Flush();
82 
83   // Sends a message to the MFT.
84   HRESULT SendMFTMessage(MFT_MESSAGE_TYPE aMsg, ULONG_PTR aData);
85 
86   HRESULT FindDecoderOutputTypeWithSubtype(const GUID& aSubType);
87   HRESULT FindDecoderOutputType();
88 
89  private:
90   // Will search a suitable MediaType using aTypeToUse if set, if not will
91   // use the current mOutputType.
92   HRESULT SetDecoderOutputType(
93       const GUID& aSubType, IMFMediaType* aTypeToUse,
94       std::function<HRESULT(IMFMediaType*)>&& aCallback);
95   HRESULT CreateOutputSample(RefPtr<IMFSample>* aOutSample);
96 
97   MFT_INPUT_STREAM_INFO mInputStreamInfo;
98   MFT_OUTPUT_STREAM_INFO mOutputStreamInfo;
99 
100   RefPtr<IMFTransform> mDecoder;
101 
102   RefPtr<IMFMediaType> mOutputType;
103   GUID mOutputSubType;
104 
105   // True if the IMFTransform allocates the samples that it returns.
106   bool mMFTProvidesOutputSamples = false;
107 
108   // True if we need to mark the next sample as a discontinuity.
109   bool mDiscontinuity = true;
110 };
111 
112 }  // namespace mozilla
113 
114 #endif
115