1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "evrhelpers.h"
41 
42 #ifndef D3DFMT_YV12
43 #define D3DFMT_YV12 (D3DFORMAT)MAKEFOURCC ('Y', 'V', '1', '2')
44 #endif
45 #ifndef D3DFMT_NV12
46 #define D3DFMT_NV12 (D3DFORMAT)MAKEFOURCC ('N', 'V', '1', '2')
47 #endif
48 
49 QT_BEGIN_NAMESPACE
50 
qt_evr_getFourCC(IMFMediaType * type,DWORD * fourCC)51 HRESULT qt_evr_getFourCC(IMFMediaType *type, DWORD *fourCC)
52 {
53     if (!fourCC)
54         return E_POINTER;
55 
56     HRESULT hr = S_OK;
57     GUID guidSubType = GUID_NULL;
58 
59     if (SUCCEEDED(hr))
60         hr = type->GetGUID(MF_MT_SUBTYPE, &guidSubType);
61 
62     if (SUCCEEDED(hr))
63         *fourCC = guidSubType.Data1;
64 
65     return hr;
66 }
67 
qt_evr_areMediaTypesEqual(IMFMediaType * type1,IMFMediaType * type2)68 bool qt_evr_areMediaTypesEqual(IMFMediaType *type1, IMFMediaType *type2)
69 {
70     if (!type1 && !type2)
71         return true;
72     if (!type1 || !type2)
73         return false;
74 
75     DWORD dwFlags = 0;
76     HRESULT hr = type1->IsEqual(type2, &dwFlags);
77 
78     return (hr == S_OK);
79 }
80 
qt_evr_validateVideoArea(const MFVideoArea & area,UINT32 width,UINT32 height)81 HRESULT qt_evr_validateVideoArea(const MFVideoArea& area, UINT32 width, UINT32 height)
82 {
83     float fOffsetX = qt_evr_MFOffsetToFloat(area.OffsetX);
84     float fOffsetY = qt_evr_MFOffsetToFloat(area.OffsetY);
85 
86     if ( ((LONG)fOffsetX + area.Area.cx > (LONG)width) ||
87          ((LONG)fOffsetY + area.Area.cy > (LONG)height) ) {
88         return MF_E_INVALIDMEDIATYPE;
89     }
90     return S_OK;
91 }
92 
qt_evr_isSampleTimePassed(IMFClock * clock,IMFSample * sample)93 bool qt_evr_isSampleTimePassed(IMFClock *clock, IMFSample *sample)
94 {
95     if (!sample || !clock)
96         return false;
97 
98     HRESULT hr = S_OK;
99     MFTIME hnsTimeNow = 0;
100     MFTIME hnsSystemTime = 0;
101     MFTIME hnsSampleStart = 0;
102     MFTIME hnsSampleDuration = 0;
103 
104     hr = clock->GetCorrelatedTime(0, &hnsTimeNow, &hnsSystemTime);
105 
106     if (SUCCEEDED(hr))
107         hr = sample->GetSampleTime(&hnsSampleStart);
108 
109     if (SUCCEEDED(hr))
110         hr = sample->GetSampleDuration(&hnsSampleDuration);
111 
112     if (SUCCEEDED(hr)) {
113         if (hnsSampleStart + hnsSampleDuration < hnsTimeNow)
114             return true;
115     }
116 
117     return false;
118 }
119 
qt_evr_pixelFormatFromD3DFormat(DWORD format)120 QVideoFrame::PixelFormat qt_evr_pixelFormatFromD3DFormat(DWORD format)
121 {
122     switch (format) {
123     case D3DFMT_R8G8B8:
124         return QVideoFrame::Format_RGB24;
125     case D3DFMT_A8R8G8B8:
126         return QVideoFrame::Format_ARGB32;
127     case D3DFMT_X8R8G8B8:
128         return QVideoFrame::Format_RGB32;
129     case D3DFMT_R5G6B5:
130         return QVideoFrame::Format_RGB565;
131     case D3DFMT_X1R5G5B5:
132         return QVideoFrame::Format_RGB555;
133     case D3DFMT_A8:
134         return QVideoFrame::Format_Y8;
135     case D3DFMT_A8B8G8R8:
136         return QVideoFrame::Format_BGRA32;
137     case D3DFMT_X8B8G8R8:
138         return QVideoFrame::Format_BGR32;
139     case D3DFMT_UYVY:
140         return QVideoFrame::Format_UYVY;
141     case D3DFMT_YUY2:
142         return QVideoFrame::Format_YUYV;
143     case D3DFMT_NV12:
144         return QVideoFrame::Format_NV12;
145     case D3DFMT_YV12:
146         return QVideoFrame::Format_YV12;
147     case D3DFMT_UNKNOWN:
148     default:
149         return QVideoFrame::Format_Invalid;
150     }
151 }
152 
qt_evr_D3DFormatFromPixelFormat(QVideoFrame::PixelFormat format)153 D3DFORMAT qt_evr_D3DFormatFromPixelFormat(QVideoFrame::PixelFormat format)
154 {
155     switch (format) {
156     case QVideoFrame::Format_RGB24:
157         return D3DFMT_R8G8B8;
158     case QVideoFrame::Format_ARGB32:
159         return D3DFMT_A8R8G8B8;
160     case QVideoFrame::Format_RGB32:
161         return D3DFMT_X8R8G8B8;
162     case QVideoFrame::Format_RGB565:
163         return D3DFMT_R5G6B5;
164     case QVideoFrame::Format_RGB555:
165         return D3DFMT_X1R5G5B5;
166     case QVideoFrame::Format_Y8:
167         return D3DFMT_A8;
168     case QVideoFrame::Format_BGRA32:
169         return D3DFMT_A8B8G8R8;
170     case QVideoFrame::Format_BGR32:
171         return D3DFMT_X8B8G8R8;
172     case QVideoFrame::Format_UYVY:
173         return D3DFMT_UYVY;
174     case QVideoFrame::Format_YUYV:
175         return D3DFMT_YUY2;
176     case QVideoFrame::Format_NV12:
177         return D3DFMT_NV12;
178     case QVideoFrame::Format_YV12:
179         return D3DFMT_YV12;
180     case QVideoFrame::Format_Invalid:
181     default:
182         return D3DFMT_UNKNOWN;
183     }
184 }
185 
186 QT_END_NAMESPACE
187