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

d3d11va.h « decoders « LAVVideo « decoder - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b20570fbdc402b56d0f000ad11e23c3af260c65a (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
/*
*      Copyright (C) 2017 Hendrik Leppkes
*      http://www.1f0.de
*
*  This program 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 2 of the License, or
*  (at your option) any later version.
*
*  This program 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, write to the Free Software Foundation, Inc.,
*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once
#include "DecBase.h"
#include "avcodec.h"

#include <d3d11.h>
#include <dxgi.h>

#include "d3d11/D3D11SurfaceAllocator.h"

extern "C" {
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_d3d11va.h"
#include "libavcodec/d3d11va.h"
}

#define D3D11_QUEUE_SURFACES 4

typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY1)(REFIID riid, void **ppFactory);

class CDecD3D11 : public CDecAvcodec
{
public:
  CDecD3D11(void);
  virtual ~CDecD3D11(void);

  // ILAVDecoder
  STDMETHODIMP Check();
  STDMETHODIMP InitDecoder(AVCodecID codec, const CMediaType *pmt);
  STDMETHODIMP GetPixelFormat(LAVPixelFormat *pPix, int *pBpp);
  STDMETHODIMP Flush();
  STDMETHODIMP EndOfStream();

  STDMETHODIMP InitAllocator(IMemAllocator **ppAlloc);
  STDMETHODIMP PostConnect(IPin *pPin);
  STDMETHODIMP_(long) GetBufferCount();
  STDMETHODIMP_(const WCHAR*) GetDecoderName() { return m_bReadBackFallback ? (m_bDirect ? L"d3d11 cb direct" : L"d3d11 cb") : L"d3d11 native"; }
  STDMETHODIMP HasThreadSafeBuffers() { return S_FALSE; }
  STDMETHODIMP SetDirectOutput(BOOL bDirect) { m_bDirect = bDirect; return S_OK; }
  STDMETHODIMP GetHWAccelActiveDevice(BSTR *pstrDeviceName);

  // CDecBase
  STDMETHODIMP Init();

protected:
  HRESULT AdditionaDecoderInit();
  HRESULT PostDecode();

  HRESULT HandleDXVA2Frame(LAVFrame *pFrame);
  HRESULT DeliverD3D11Frame(LAVFrame *pFrame);
  HRESULT DeliverD3D11Readback(LAVFrame *pFrame);
  HRESULT DeliverD3D11ReadbackDirect(LAVFrame *pFrame);

private:
  STDMETHODIMP DestroyDecoder(bool bFull, bool bNoAVCodec = false);

  STDMETHODIMP ReInitD3D11Decoder(AVCodecContext *c);

  STDMETHODIMP CreateD3D11Decoder();
  STDMETHODIMP AllocateFramesContext(int width, int height, AVPixelFormat format, int nSurfaces, AVBufferRef **pFramesCtx);

  STDMETHODIMP FindVideoServiceConversion(AVCodecID codec, int profile, DXGI_FORMAT surface_format, GUID *input);
  STDMETHODIMP FindDecoderConfiguration(const D3D11_VIDEO_DECODER_DESC *desc, D3D11_VIDEO_DECODER_CONFIG *pConfig);

  STDMETHODIMP FillHWContext(AVD3D11VAContext *ctx);

  STDMETHODIMP FlushDisplayQueue(BOOL bDeliver);

  static enum AVPixelFormat get_d3d11_format(struct AVCodecContext *s, const enum AVPixelFormat * pix_fmts);
  static int get_d3d11_buffer(struct AVCodecContext *c, AVFrame *pic, int flags);

private:
  CD3D11SurfaceAllocator *m_pAllocator = nullptr;

  AVBufferRef *m_pDevCtx = nullptr;
  AVBufferRef *m_pFramesCtx = nullptr;

  D3D11_VIDEO_DECODER_CONFIG m_DecoderConfig;
  ID3D11VideoDecoder *m_pDecoder = nullptr;

  int m_nOutputViews = 0;
  ID3D11VideoDecoderOutputView **m_pOutputViews = nullptr;

  DWORD m_dwSurfaceWidth = 0;
  DWORD m_dwSurfaceHeight = 0;
  DWORD m_dwSurfaceCount = 0;
  AVPixelFormat m_DecodePixelFormat = AV_PIX_FMT_NONE;
  DXGI_FORMAT m_SurfaceFormat = DXGI_FORMAT_UNKNOWN;

  BOOL m_bReadBackFallback = FALSE;
  BOOL m_bDirect = FALSE;
  BOOL m_bFailHWDecode = FALSE;

  ID3D11Texture2D *m_pD3D11StagingTexture = nullptr;

  LAVFrame* m_FrameQueue[D3D11_QUEUE_SURFACES];
  int       m_FrameQueuePosition = 0;
  int       m_DisplayDelay = D3D11_QUEUE_SURFACES;

  struct
  {
    HMODULE d3d11lib;
    PFN_D3D11_CREATE_DEVICE mD3D11CreateDevice;

    HMODULE dxgilib;
    PFN_CREATE_DXGI_FACTORY1 mCreateDXGIFactory1;
  } dx = { 0 };

  DXGI_ADAPTER_DESC m_AdapterDesc = { 0 };

  friend class CD3D11SurfaceAllocator;
};