summaryrefslogtreecommitdiff
path: root/media/gmp-clearkey/0.1/WMFUtils.h
blob: 51d46d70d48e309c5089fbe552955b06cfbdd774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright 2013, Mozilla Foundation and contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __WMFUtils_h__
#define __WMFUtils_h__

#include <cstdint>
#include <string>

#include <assert.h>
#include <mfapi.h>
#include <mferror.h>
#include <mfobjects.h>
#include <mftransform.h>
#include <wmcodecdsp.h>
#include "VideoLimits.h"

#include "gmp-platform.h"

void LOG(const char* format, ...);

#ifdef LOG_SAMPLE_DECODE
#define SAMPLE_LOG LOG
#else
#define SAMPLE_LOG(...)
#endif

#ifndef CLSID_CMSAACDecMFT
#define WMF_MUST_DEFINE_AAC_MFT_CLSID
extern "C" const CLSID CLSID_CMSAACDecMFT;
#endif

extern "C" const CLSID CLSID_CMSH264DecMFT;

namespace wmf {

// Reimplementation of CComPtr to reduce dependence on system
// shared libraries.
template<class T>
class CComPtr {
public:
  CComPtr(CComPtr&& aOther) : mPtr(aOther.Detach()) { }
  CComPtr& operator=(CComPtr&& other) { mPtr = other.Detach(); }

  CComPtr(const CComPtr& aOther) : mPtr(nullptr) { Set(aOther.Get()); }
  CComPtr() : mPtr(nullptr) { }
  CComPtr(T* const & aPtr) : mPtr(nullptr) { Set(aPtr); }
  CComPtr(const std::nullptr_t& aNullPtr) : mPtr(aNullPtr) { }
  T** operator&() { return &mPtr; }
  T* operator->(){ return mPtr; }
  operator T*() { return mPtr; }
  T* operator=(T* const & aPtr) { return Set(aPtr); }
  T* operator=(const std::nullptr_t& aPtr) { return mPtr = aPtr; }

  T* Get() const { return mPtr; }

  T* Detach() {
    T* tmp = mPtr;
    mPtr = nullptr;
    return tmp;
  }

  ~CComPtr() {
    if (mPtr) {
      mPtr->Release();
    }
    mPtr = nullptr;
  }

private:

  T* Set(T* aPtr) {
    if (mPtr == aPtr) {
      return aPtr;
    }
    if (mPtr) {
      mPtr->Release();
    }
    mPtr = aPtr;
    if (mPtr) {
      mPtr->AddRef();
    }
    return mPtr;
  }

  T* mPtr;
};

class IntRect {
public:
  IntRect(int32_t _x, int32_t _y, int32_t _w, int32_t _h)
    : x(_x), y(_y), width(_w), height(_h) {}
  IntRect()
    : x(0), y(0), width(0), height(0) {}
  int32_t x;
  int32_t y;
  int32_t width;
  int32_t height;
};

typedef int64_t Microseconds;

#ifdef ENSURE
#undef ENSURE
#endif

#define ENSURE(condition, ret) \
{ if (!(condition)) { LOG("##condition## FAILED %S:%d\n", __FILE__, __LINE__); return ret; } }

#define GMP_SUCCEEDED(x) ((x) == GMPNoErr)
#define GMP_FAILED(x) ((x) != GMPNoErr)

#define MFPLAT_FUNC(_func, _dllname) \
  extern decltype(::_func)* _func;
#include "WMFSymbols.h"
#undef MFPLAT_FUNC

bool
EnsureLibs();

HRESULT
GetPictureRegion(IMFMediaType* aMediaType, IntRect& aOutPictureRegion);

HRESULT
GetDefaultStride(IMFMediaType *aType, uint32_t* aOutStride);

// Converts from microseconds to hundreds of nanoseconds.
// We use microseconds for our timestamps, whereas WMF uses
// hundreds of nanoseconds.
inline int64_t
UsecsToHNs(int64_t aUsecs) {
  return aUsecs * 10;
}

// Converts from hundreds of nanoseconds to microseconds.
// We use microseconds for our timestamps, whereas WMF uses
// hundreds of nanoseconds.
inline int64_t
HNsToUsecs(int64_t hNanoSecs) {
  return hNanoSecs / 10;
}

inline std::string narrow(std::wstring &wide) {
  std::string ns(wide.begin(), wide.end());
  return ns;
}

inline std::wstring widen(std::string &narrow) {
  std::wstring ws(narrow.begin(), narrow.end());
  return ws;
}

#define ARRAY_LENGTH(array_) \
  (sizeof(array_)/sizeof(array_[0]))

template<class Type>
class AutoPtr {
public:
  AutoPtr()
    : mPtr(nullptr)
  {
  }

  AutoPtr(AutoPtr<Type>& aPtr)
    : mPtr(aPtr.Forget())
  {
  }

  AutoPtr(Type* aPtr)
    : mPtr(aPtr)
  {
  }

  ~AutoPtr() {
    if (mPtr) {
      delete mPtr;
    }
  }

  Type* Forget() {
    Type* rv = mPtr;
    mPtr = nullptr;
    return rv;
  }

  AutoPtr<Type>& operator=(Type* aOther) {
    Assign(aOther);
    return *this;
  }

  AutoPtr<Type>& operator=(AutoPtr<Type>& aOther) {
    Assign(aOther.Forget());
    return *this;
  }

  Type* Get() const {
    return mPtr;
  }

  Type* operator->() const {
    assert(mPtr);
    return Get();
  }

  operator Type*() const {
    return Get();
  }

  Type** Receive() {
    return &mPtr;
  }
private:

  void Assign(Type* aPtr) {
    if (mPtr) {
      delete mPtr;
    }
    mPtr = aPtr;
  }

  Type* mPtr;
};

// Video frame microseconds are (currently) in 90kHz units, as used by RTP.
// Use this to convert to microseconds...
inline Microseconds RTPTimeToMicroseconds(int64_t rtptime) {
  return (rtptime * 1000000) / 90000;
}

inline uint32_t MicrosecondsToRTPTime(Microseconds us) {
  return uint32_t(0xffffffff & (us * 90000) / 1000000);
}

void dump(const uint8_t* data, uint32_t len, const char* filename);

HRESULT
CreateMFT(const CLSID& clsid,
          const char* aDllName,
          CComPtr<IMFTransform>& aOutMFT);

enum CodecType {
  H264,
  AAC,
};

// Returns the name of the DLL that is needed to decode H.264 or AAC on
// the given windows version we're running on.
const char* WMFDecoderDllNameFor(CodecType aCodec);

// Returns the maximum number of threads we want WMF to use for decoding
// given the number of logical processors available.
int32_t GetNumThreads(int32_t aCoreCount);

} // namespace wmf

#endif // __WMFUtils_h__