1
/*  This file is part of the KDE project.
2
3
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
5
This library is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 2.1 or 3 of the License.
8
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU Lesser General Public License for more details.
13
14
You should have received a copy of the GNU Lesser General Public License
15
along with this library.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
19
#include "videorenderer_evr.h"
20
#include "qevr9.h"
21
22
#ifndef QT_NO_PHONON_VIDEO
23
24
#include <QtGui/QWidget>
25
#include <QtGui/QPainter>
26
27
QT_BEGIN_NAMESPACE
28
29
namespace Phonon
30
{
31
    namespace DS9
32
    {
33
        //we have to define them here because not all compilers/sdk have them
34
        static const GUID MR_VIDEO_RENDER_SERVICE =     {0x1092a86c, 0xab1a, 0x459a, {0xa3, 0x36, 0x83, 0x1f, 0xbc, 0x4d, 0x11, 0xff} };
35
        static const GUID MR_VIDEO_MIXER_SERVICE =      { 0x73cd2fc, 0x6cf4, 0x40b7, {0x88, 0x59, 0xe8, 0x95, 0x52, 0xc8, 0x41, 0xf8} };
36
        static const IID IID_IMFVideoDisplayControl =   {0xa490b1e4, 0xab84, 0x4d31, {0xa1, 0xb2, 0x18, 0x1e, 0x03, 0xb1, 0x07, 0x7a} };
37
        static const IID IID_IMFVideoMixerControl =     {0xA5C6C53F, 0xC202, 0x4aa5, {0x96, 0x95, 0x17, 0x5B, 0xA8, 0xC5, 0x08, 0xA5} };
38
        static const IID IID_IMFVideoProcessor =        {0x6AB0000C, 0xFECE, 0x4d1f, {0xA2, 0xAC, 0xA9, 0x57, 0x35, 0x30, 0x65, 0x6E} };
39
        static const IID IID_IMFGetService =            {0xFA993888, 0x4383, 0x415A, {0xA9, 0x30, 0xDD, 0x47, 0x2A, 0x8C, 0xF6, 0xF7} };
40
        static const GUID CLSID_EnhancedVideoRenderer = {0xfa10746c, 0x9b63, 0x4b6c, {0xbc, 0x49, 0xfc, 0x30,  0xe, 0xa5, 0xf2, 0x56} };
41
42
        template <typename T> ComPointer<T> getService(const Filter &filter, REFGUID guidService, REFIID riid)
43
        {
44
            //normally we should use IID_IMFGetService but this introduces another dependency
45
            //so here we simply define our own IId with the same value
46
            ComPointer<T> ret;
47
            ComPointer<IMFGetService> getService(filter, IID_IMFGetService);
48
            if (getService) {
49
                getService->GetService(guidService, riid, reinterpret_cast<void**>(ret.pparam()));
50
            }
51
            return ret;
52
        }
53
54
        VideoRendererEVR::~VideoRendererEVR()
55
        {
56
        }
57
58
        bool VideoRendererEVR::isNative() const
59
        {
60
            return true;
61
        }
62
63
        VideoRendererEVR::VideoRendererEVR(QWidget *target) : m_target(target)
64
        {
65
            if (QSysInfo::WindowsVersion < QSysInfo::WV_VISTA)
66
                return;
67
            m_filter = Filter(CLSID_EnhancedVideoRenderer, IID_IBaseFilter);
68
            if (!m_filter) {
69
                return;
70
            }
71
72
            ComPointer<IMFVideoDisplayControl> filterControl = getService<IMFVideoDisplayControl>(m_filter, MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl);
73
            if (!filterControl ||
74
                FAILED(filterControl->SetVideoWindow(reinterpret_cast<HWND>(target->winId()))) ||
75
                FAILED(filterControl->SetAspectRatioMode(MFVideoARMode_None)) ||        // We're in control of the size
76
                !getService<IMFVideoMixerControl>(m_filter, MR_VIDEO_MIXER_SERVICE, IID_IMFVideoMixerControl) ||
77
                !getService<IMFVideoProcessor>(m_filter, MR_VIDEO_MIXER_SERVICE, IID_IMFVideoProcessor))  {
78
                m_filter = Filter(); //will release the interface
79
            }
80
        }
81
82
        QImage VideoRendererEVR::snapshot() const
83
        {
84
            // This will always capture black areas where no video is drawn, if any are present.
85
            // Due to the hack in notifyResize()
86
            ComPointer<IMFVideoDisplayControl> filterControl = getService<IMFVideoDisplayControl>(m_filter, MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl);
87
            if (filterControl) {
88
                BITMAPINFOHEADER bmi;
89
                BYTE *buffer = 0;
90
                DWORD bufferSize;
91
                LONGLONG timeStamp;
92
93
                bmi.biSize = sizeof(BITMAPINFOHEADER);
94
95
                HRESULT hr = filterControl->GetCurrentImage(&bmi, &buffer, &bufferSize, &timeStamp);
96
                if (SUCCEEDED(hr)) {
97
98
                    const int w = qAbs(bmi.biWidth),
99
                        h = qAbs(bmi.biHeight);
100
101
                    // Create image and copy data into image.
102
                    QImage ret(w, h, QImage::Format_RGB32);
103
104
                    if (!ret.isNull()) {
105
                        uchar *data = buffer;
106
                        const int bytes_per_line = w * sizeof(QRgb);
107
                        for (int y = h - 1; y >= 0; --y) {
108
                            qMemCopy(ret.scanLine(y), //destination
109
                                data,     //source
110
                                bytes_per_line);
111
                            data += bytes_per_line;
112
                        }
113
                    }
114
                    ::CoTaskMemFree(buffer);
115
                    return ret;
116
                }
117
            }
118
            return QImage();
119
        }
120
121
        QSize VideoRendererEVR::videoSize() const
122
        {
123
            SIZE nativeSize;
124
            SIZE aspectRatioSize;
125
126
            ComPointer<IMFVideoDisplayControl> filterControl = getService<IMFVideoDisplayControl>(m_filter, MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl);
127
128
            filterControl->GetNativeVideoSize(&nativeSize, &aspectRatioSize);
129
130
            return QSize(nativeSize.cx, nativeSize.cy);
131
        }
132
       
133
        void VideoRendererEVR::repaintCurrentFrame(QWidget *target, const QRect &rect)
134
        {
135
            // repaint the video
136
            ComPointer<IMFVideoDisplayControl> filterControl = getService<IMFVideoDisplayControl>(m_filter, MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl);
137
            // All failed results can be safely ignored
138
            filterControl->RepaintVideo();
139
        }
140
141
        void VideoRendererEVR::notifyResize(const QSize &size, Phonon::VideoWidget::AspectRatio aspectRatio,
142
            Phonon::VideoWidget::ScaleMode scaleMode)
143
        {
144
            if (!isActive()) {
145
                RECT dummyRect = { 0, 0, 0, 0};
146
                ComPointer<IMFVideoDisplayControl> filterControl = getService<IMFVideoDisplayControl>(m_filter, MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl);
147
                filterControl->SetVideoPosition(0, &dummyRect);
148
                return;
149
            }
150
151
            const QSize vsize = videoSize();
152
            internalNotifyResize(size, vsize, aspectRatio, scaleMode);
153
154
            RECT dstRectWin = { 0, 0, size.width(), size.height()};
155
156
            // Resize the Stream output rect instead of the destination rect.
157
            // Hacky workaround for flicker in the areas outside of the destination rect
158
            // This way these areas don't exist
159
            MFVideoNormalizedRect streamOutputRect = { float(m_dstX) / float(size.width()), float(m_dstY) / float(size.height()),
160
                                                       float(m_dstWidth + m_dstX) / float(size.width()), float(m_dstHeight + m_dstY) / float(size.height())};
161
162
            ComPointer<IMFVideoMixerControl> filterMixer = getService<IMFVideoMixerControl>(m_filter, MR_VIDEO_MIXER_SERVICE, IID_IMFVideoMixerControl);
163
            ComPointer<IMFVideoDisplayControl> filterControl = getService<IMFVideoDisplayControl>(m_filter, MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl);
164
165
            filterMixer->SetStreamOutputRect(0, &streamOutputRect);
166
            filterControl->SetVideoPosition(0, &dstRectWin);
167
        }
168
169
        void VideoRendererEVR::applyMixerSettings(qreal brightness, qreal contrast, qreal hue, qreal saturation)
170
        {
171
            InputPin sink = BackendNode::pins(m_filter, PINDIR_INPUT).first();
172
            OutputPin source;
173
            if (FAILED(sink->ConnectedTo(source.pparam()))) {
174
                return; //it must be connected to work
175
            }
176
177
            // Get the "Video Processor" (used for brightness/contrast/saturation/hue)
178
            ComPointer<IMFVideoProcessor> processor = getService<IMFVideoProcessor>(m_filter, MR_VIDEO_MIXER_SERVICE, IID_IMFVideoProcessor);
179
            Q_ASSERT(processor);
180
181
            DXVA2_ValueRange contrastRange;
182
            DXVA2_ValueRange brightnessRange;
183
            DXVA2_ValueRange saturationRange;
184
            DXVA2_ValueRange hueRange;
185
186
            if (FAILED(processor->GetProcAmpRange(DXVA2_ProcAmp_Contrast, &contrastRange)))
187
                return;
188
            if (FAILED(processor->GetProcAmpRange(DXVA2_ProcAmp_Brightness, &brightnessRange)))
189
                return;
190
            if (FAILED(processor->GetProcAmpRange(DXVA2_ProcAmp_Saturation, &saturationRange)))
191
                return;
192
            if (FAILED(processor->GetProcAmpRange(DXVA2_ProcAmp_Hue, &hueRange)))
193
                return;
194
195
            DXVA2_ProcAmpValues values;
196
197
            values.Contrast = DXVA2FloatToFixed(((contrast < 0
198
                                ? DXVA2FixedToFloat(contrastRange.MinValue) : DXVA2FixedToFloat(contrastRange.MaxValue))
199
                               - DXVA2FixedToFloat(contrastRange.DefaultValue)) * qAbs(contrast) + DXVA2FixedToFloat(contrastRange.DefaultValue));
200
            values.Brightness = DXVA2FloatToFixed(((brightness < 0
201
                                ? DXVA2FixedToFloat(brightnessRange.MinValue) : DXVA2FixedToFloat(brightnessRange.MaxValue))
202
                               - DXVA2FixedToFloat(brightnessRange.DefaultValue)) * qAbs(brightness) + DXVA2FixedToFloat(brightnessRange.DefaultValue));
203
            values.Saturation = DXVA2FloatToFixed(((saturation < 0
204
                                ? DXVA2FixedToFloat(saturationRange.MinValue) : DXVA2FixedToFloat(saturationRange.MaxValue))
205
                               - DXVA2FixedToFloat(saturationRange.DefaultValue)) * qAbs(saturation) + DXVA2FixedToFloat(saturationRange.DefaultValue));
206
            values.Hue = DXVA2FloatToFixed(((hue < 0
207
                                ? DXVA2FixedToFloat(hueRange.MinValue) : DXVA2FixedToFloat(hueRange.MaxValue))
208
                               - DXVA2FixedToFloat(hueRange.DefaultValue)) * qAbs(hue) + DXVA2FixedToFloat(hueRange.DefaultValue));
209
210
            //finally set the settings
211
            processor->SetProcAmpValues(DXVA2_ProcAmp_Contrast | DXVA2_ProcAmp_Brightness | DXVA2_ProcAmp_Saturation | DXVA2_ProcAmp_Hue, &values);
212
213
        }
214
    }
215
}
216
217
QT_END_NAMESPACE
218
219
#endif //QT_NO_PHONON_VIDEO