Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MPCVideoDecFilter.h « MPCVideoDec « transform « filters « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9784bd999dd0f4fd0357076c68ab99dc89ac05c4 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * (C) 2007-2013 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#pragma once

#include <d3dx9.h>
#include <videoacc.h>   // DXVA1
#include <dxva.h>
#include <dxva2api.h>   // DXVA2
#include "../BaseVideoFilter/BaseVideoFilter.h"

#include "IMPCVideoDecFilter.h"
#include "MPCVideoDecSettingsWnd.h"
#include "../../../DeCSS/DeCSSInputPin.h"
#include "DXVADecoder.h"
#include "TlibavcodecExt.h"

#include "H264RandomAccess.h"
#include <atlpath.h>

// TODO: remove this when it's fixed in MSVC
// Work around warning C4005: 'XXXX' : macro redefinition
#pragma warning(push)
#pragma warning(disable: 4005)
#include <stdint.h>
#pragma warning(pop)


#define MPCVideoDecName L"MPC Video Decoder"

#define MAX_BUFF_TIME   20

#define CHECK_HR_TRACE(x)                   \
    hr = ##x;                               \
    if (FAILED(hr)) {                       \
        TRACE(_T("Error : 0x%08x\n"), hr);  \
        ASSERT(hr == VFW_E_NOT_COMMITTED);  \
        return hr;                          \
    }

struct AVCodec;
struct AVCodecContext;
struct AVFrame;
struct SwsContext;

class CCpuId;

typedef enum {
    MODE_SOFTWARE,
    MODE_DXVA1,
    MODE_DXVA2
} DXVA_MODE;

typedef struct {
    REFERENCE_TIME rtStart;
    REFERENCE_TIME rtStop;
} B_FRAME;

typedef struct {
    REFERENCE_TIME rtStart;
    REFERENCE_TIME rtStop;
    int nBuffPos;
} BUFFER_TIME;

typedef struct {
    bool    video_after_seek;
    int     kf_pts;     ///< timestamp of next video keyframe
    int64_t kf_base;    ///< timestamp of the previous video keyframe
} RMDemuxContext;

class __declspec(uuid("008BAC12-FBAF-497b-9670-BC6F6FBAE2C4"))
    CMPCVideoDecFilter
    : public CBaseVideoFilter
    , public TlibavcodecExt
    , public ISpecifyPropertyPages2
    , public IMPCVideoDecFilter2
{
protected:

    // === FFmpeg callbacks
    static void LogLibavcodec(void* par, int level, const char* fmt, va_list valist);
    virtual void OnGetBuffer(AVFrame* pic);

    friend class CVideoDecDXVAAllocator;

    CCpuId* m_pCpuId;
    CCritSec m_csProps;

    CAutoPtr<bool> m_DXVAFilters;
    CAutoPtr<bool> m_FFmpegFilters;

    // === Persistants parameters (registry)
    int m_nThreadNumber;
    int m_nDiscardMode;
    bool m_bDXVACompatible;
    int m_nActiveCodecs;
    int m_nARMode;
    int m_nDXVACheckCompatibility;
    int m_nDXVA_SD;
    MPCVD_INTERLACED_FLAG m_interlacedFlag;

    FF_FIELD_TYPE m_nFrameType;

    // === FFmpeg variables
    AVCodec* m_pAVCodec;
    AVCodecContext* m_pAVCtx;
    AVFrame* m_pFrame;
    int m_nCodecNb;
    enum AVCodecID m_nCodecId;
    int m_nWorkaroundBug;
    int m_nErrorConcealment;
    REFERENCE_TIME m_rtAvrTimePerFrame;
    bool m_bReorderBFrame;
    B_FRAME m_BFrames[2];
    int m_nPosB;
    int m_nWidth;       // Frame width given to input pin
    int m_nHeight;      // Frame height given to input pin

    bool m_bTheoraMTSupport;
    bool m_bIsEVO;

    // Buffer management for truncated stream (store stream chunks & reference time sent by splitter)
    BYTE* m_pFFBuffer;
    int m_nFFBufferSize;
    BYTE* m_pAlignedFFBuffer;
    int m_nAlignedFFBufferSize;

    int m_nFFBufferPos;
    int m_nFFPicEnd;
    BUFFER_TIME m_FFBufferTime[MAX_BUFF_TIME];

    REFERENCE_TIME m_rtLastStart;   // rtStart for last delivered frame
    int m_nCountEstimated;          // Number of rtStart estimated since last rtStart received
    double m_dRate;
    REFERENCE_TIME m_rtPrevStop;
    bool m_bFrame_repeat_pict;

    bool m_bUseDXVA;
    bool m_bUseFFmpeg;
    CSize m_par;
    SwsContext* m_pSwsContext;
    unsigned __int64 m_nOutCsp;
    CSize m_pOutSize;               // Picture size on output pin
    int m_nSwOutBpp;

    // === DXVA common variables
    VIDEO_OUTPUT_FORMATS* m_pVideoOutputFormat;
    int m_nVideoOutputCount;
    DXVA_MODE m_nDXVAMode;
    CDXVADecoder* m_pDXVADecoder;
    GUID m_DXVADecoderGUID;

    DWORD m_nPCIVendor;
    DWORD m_nPCIDevice;
    LARGE_INTEGER m_VideoDriverVersion;
    CString m_strDeviceDescription;

    // === DXVA1 variables
    DDPIXELFORMAT m_PixelFormat;

    // === DXVA2 variables
    CComPtr<IDirect3DDeviceManager9> m_pDeviceManager;
    CComPtr<IDirectXVideoDecoderService> m_pDecoderService;
    CComPtr<IDirect3DSurface9> m_pDecoderRenderTarget;
    DXVA2_ConfigPictureDecode m_DXVA2Config;
    HANDLE m_hDevice;
    DXVA2_VideoDesc m_VideoDesc;

    CH264RandomAccess m_h264RandomAccess;

    BOOL m_bWaitingForKeyFrame;

    RMDemuxContext rm;
    REFERENCE_TIME m_rtStart;

    // === Private functions
    void Cleanup();
    int  FindCodec(const CMediaType* mtIn);
    void AllocExtradata(AVCodecContext* pAVCtx, const CMediaType* mt);
    bool IsMultiThreadSupported(enum AVCodecID nCodec);
    void GetOutputFormats(int& nNumber, VIDEO_OUTPUT_FORMATS** ppFormats);
    void CalcAvgTimePerFrame();
    void DetectVideoCard(HWND hWnd);
    unsigned __int64 GetCspFromMediaType(GUID& subtype);
    void InitSwscale();

    void SetTypeSpecificFlags(IMediaSample* pMS);
    HRESULT SoftwareDecode(IMediaSample* pIn, BYTE* pDataIn, int nSize, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
    bool AppendBuffer(BYTE* pDataIn, int nSize, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop);
    bool FindPicture(int nIndex, int nStartCode);
    void ShrinkBuffer();
    void ResetBuffer();
    void PushBufferTime(int nPos, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
    void PopBufferTime(int nPos);

public:

    const static AMOVIESETUP_MEDIATYPE sudPinTypesIn[];
    const static int sudPinTypesInCount;
    const static AMOVIESETUP_MEDIATYPE sudPinTypesOut[];
    const static int sudPinTypesOutCount;

    CMPCVideoDecFilter(LPUNKNOWN lpunk, HRESULT* phr);
    virtual ~CMPCVideoDecFilter();

    DECLARE_IUNKNOWN
    STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
    virtual bool IsVideoInterlaced();
    virtual void GetOutputSize(int& w, int& h, int& arx, int& ary, int& RealWidth, int& RealHeight);
    CTransformOutputPin* GetOutputPin() { return m_pOutput; }

    void UpdateFrameTime(REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop, bool b_repeat_pict = false);
    CString GetFileExtension();

    void SetDXVAFilters(CAutoPtr<bool> DXVAFilters) { m_DXVAFilters = DXVAFilters; };
    void SetFFmpegFilters(CAutoPtr<bool> FFmpegFilters) {  m_FFmpegFilters = FFmpegFilters; };

    // === Overriden DirectShow functions
    HRESULT SetMediaType(PIN_DIRECTION direction, const CMediaType* pmt);
    HRESULT CheckInputType(const CMediaType* mtIn);
    HRESULT Transform(IMediaSample* pIn);
    HRESULT CompleteConnect(PIN_DIRECTION direction, IPin* pReceivePin);
    HRESULT DecideBufferSize(IMemAllocator* pAllocator, ALLOCATOR_PROPERTIES* pProperties);
    HRESULT BeginFlush();
    HRESULT EndFlush();
    HRESULT NewSegment(REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, double dRate);
    HRESULT EndOfStream();

    HRESULT BreakConnect(PIN_DIRECTION dir);


    // === ISpecifyPropertyPages2

    STDMETHODIMP GetPages(CAUUID* pPages);
    STDMETHODIMP CreatePage(const GUID& guid, IPropertyPage** ppPage);

    // === IMPCVideoDecFilter
    STDMETHODIMP Apply();
    STDMETHODIMP SetThreadNumber(int nValue);
    STDMETHODIMP_(int) GetThreadNumber();
    STDMETHOD(SetDiscardMode(int nValue));
    STDMETHOD_(int, GetDiscardMode());
    STDMETHOD_(GUID*, GetDXVADecoderGuid());
    STDMETHOD(SetActiveCodecs(MPC_VIDEO_CODEC nValue));
    STDMETHOD_(MPC_VIDEO_CODEC, GetActiveCodecs());
    STDMETHODIMP_(LPCTSTR) GetVideoCardDescription();

    STDMETHOD(SetARMode(int nValue));
    STDMETHOD_(int, GetARMode());

    STDMETHOD(SetDXVACheckCompatibility(int nValue));
    STDMETHOD_(int, GetDXVACheckCompatibility());

    STDMETHOD(SetDXVA_SD(int nValue));
    STDMETHOD_(int, GetDXVA_SD());

    // === IMPCVideoDecFilter2
    STDMETHOD_(int, GetFrameType());

    STDMETHOD(SetInterlacedFlag(MPCVD_INTERLACED_FLAG interlacedFlag));
    STDMETHOD_(MPCVD_INTERLACED_FLAG, GetInterlacedFlag());

    // === DXVA common functions
    BOOL IsSupportedDecoderConfig(const D3DFORMAT nD3DFormat, const DXVA2_ConfigPictureDecode& config, bool& bIsPrefered);
    BOOL IsSupportedDecoderMode(const GUID& mode);
    void BuildDXVAOutputFormat();
    int  GetPicEntryNumber();
    int  PictWidth();
    int  PictHeight();
    int  PictWidthRounded();
    int  PictHeightRounded();

    inline bool UseDXVA2() { return (m_nDXVAMode == MODE_DXVA2); };
    inline AVCodecContext* GetAVCtx() { return m_pAVCtx; };
    inline AVFrame* GetFrame() { return m_pFrame; };
    inline enum AVCodecID GetCodec() { return m_nCodecId; };
    inline bool IsReorderBFrame() { return m_bReorderBFrame; };
    inline bool IsEvo() { return m_bIsEVO; };
    inline DWORD GetPCIVendor() { return m_nPCIVendor; };
    inline REFERENCE_TIME GetAvrTimePerFrame() { return m_rtAvrTimePerFrame; };
    inline double GetRate() { return m_dRate; };
    bool IsDXVASupported();
    void UpdateAspectRatio();
    void ReorderBFrames(REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
    void FlushDXVADecoder()  {
        if (m_pDXVADecoder) {
            m_pDXVADecoder->Flush();
        }
    }

    void SetFrameType(FF_FIELD_TYPE nFrameType);

    // === DXVA1 functions
    DDPIXELFORMAT* GetPixelFormat() { return &m_PixelFormat; }
    HRESULT FindDXVA1DecoderConfiguration(IAMVideoAccelerator* pAMVideoAccelerator, const GUID* guidDecoder, DDPIXELFORMAT* pPixelFormat);
    HRESULT CheckDXVA1Decoder(const GUID* pGuid);
    void SetDXVA1Params(const GUID* pGuid, DDPIXELFORMAT* pPixelFormat);
    WORD GetDXVA1RestrictedMode();
    HRESULT CreateDXVA1Decoder(IAMVideoAccelerator* pAMVideoAccelerator, const GUID* pDecoderGuid, DWORD dwSurfaceCount);


    // === DXVA2 functions
    void FillInVideoDescription(DXVA2_VideoDesc* pDesc);
    DXVA2_ConfigPictureDecode* GetDXVA2Config() { return &m_DXVA2Config; };
    HRESULT ConfigureDXVA2(IPin* pPin);
    HRESULT SetEVRForDXVA2(IPin* pPin);
    HRESULT FindDXVA2DecoderConfiguration(IDirectXVideoDecoderService* pDecoderService,
                                          const GUID& guidDecoder,
                                          DXVA2_ConfigPictureDecode* pSelectedConfig,
                                          BOOL* pbFoundDXVA2Configuration);
    HRESULT CreateDXVA2Decoder(UINT nNumRenderTargets, IDirect3DSurface9** pDecoderRenderTargets);
};