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