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

d3d11va.cpp « decoders « LAVVideo « decoder - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3107deab6be6cb4c8746748df9c701c61db42c0a (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
/*
*      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.
*/

#include "stdafx.h"
#include "d3d11va.h"
#include "d3d11/ID3DVideoMemoryConfiguration.h"
#include "dxva2/dxva_common.h"

ILAVDecoder *CreateDecoderD3D11()
{
  return new CDecD3D11();
}

////////////////////////////////////////////////////////////////////////////////
// D3D11 decoder implementation
////////////////////////////////////////////////////////////////////////////////

static const D3D_FEATURE_LEVEL s_D3D11Levels[] =
{
  D3D_FEATURE_LEVEL_11_1,
  D3D_FEATURE_LEVEL_11_0,
  D3D_FEATURE_LEVEL_10_1,
  D3D_FEATURE_LEVEL_10_0,
  D3D_FEATURE_LEVEL_9_3,
  D3D_FEATURE_LEVEL_9_2,
  D3D_FEATURE_LEVEL_9_1,
};

CDecD3D11::CDecD3D11(void)
  : CDecAvcodec()
{
  ZeroMemory(&m_FrameQueue, sizeof(m_FrameQueue));
}


CDecD3D11::~CDecD3D11(void)
{
  DestroyDecoder(true);

  if (m_pAllocator)
    m_pAllocator->DecoderDestruct();
  SafeRelease(&m_pAllocator);
}

STDMETHODIMP CDecD3D11::DestroyDecoder(bool bFull, bool bNoAVCodec)
{
  for (int i = 0; i < D3D11_QUEUE_SURFACES; i++) {
    ReleaseFrame(&m_FrameQueue[i]);
  }

  if (m_pOutputViews)
  {
    for (int i = 0; i < m_nOutputViews; i++)
    {
      SafeRelease(&m_pOutputViews[i]);
    }
    av_freep(&m_pOutputViews);
    m_nOutputViews = 0;
  }

  SafeRelease(&m_pDecoder);
  SafeRelease(&m_pD3D11StagingTexture);
  av_buffer_unref(&m_pFramesCtx);

  if (!bNoAVCodec) {
    CDecAvcodec::DestroyDecoder();
  }

  if (bFull) {
    av_buffer_unref(&m_pDevCtx);

    if (dx.d3d11lib)
    {
      FreeLibrary(dx.d3d11lib);
      dx.d3d11lib = nullptr;
    }

    if (dx.dxgilib)
    {
      FreeLibrary(dx.dxgilib);
      dx.dxgilib = nullptr;
    }
  }

  return S_OK;
}

// ILAVDecoder
STDMETHODIMP CDecD3D11::Init()
{
  dx.d3d11lib = LoadLibrary(L"d3d11.dll");
  if (dx.d3d11lib == nullptr)
  {
    DbgLog((LOG_TRACE, 10, L"Cannot open d3d11.dll"));
    return E_FAIL;
  }

  dx.mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(dx.d3d11lib, "D3D11CreateDevice");
  if (dx.mD3D11CreateDevice == nullptr)
  {
    DbgLog((LOG_TRACE, 10, L"D3D11CreateDevice not available"));
    return E_FAIL;
  }

  dx.dxgilib = LoadLibrary(L"dxgi.dll");
  if (dx.dxgilib == nullptr)
  {
    DbgLog((LOG_TRACE, 10, L"Cannot open dxgi.dll"));
    return E_FAIL;
  }

  dx.mCreateDXGIFactory1 = (PFN_CREATE_DXGI_FACTORY1)GetProcAddress(dx.dxgilib, "CreateDXGIFactory1");
  if (dx.mCreateDXGIFactory1 == nullptr)
  {
    DbgLog((LOG_TRACE, 10, L"CreateDXGIFactory1 not available"));
    return E_FAIL;
  }

  return S_OK;
}

STDMETHODIMP CDecD3D11::Check()
{
  // attempt creating a hardware device with video support
  // by passing nullptr to the device parameter, no actual device will be created and only support will be checked
  HRESULT hr = dx.mD3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_VIDEO_SUPPORT, s_D3D11Levels, countof(s_D3D11Levels), D3D11_SDK_VERSION, nullptr, nullptr, nullptr);
  return hr;
}

STDMETHODIMP CDecD3D11::InitAllocator(IMemAllocator **ppAlloc)
{
  HRESULT hr = S_OK;
  if (m_bReadBackFallback)
    return E_NOTIMPL;

  if (m_pAllocator == nullptr)
  {
    m_pAllocator = new CD3D11SurfaceAllocator(this, &hr);
    if (!m_pAllocator) {
      return E_OUTOFMEMORY;
    }
    if (FAILED(hr)) {
      SAFE_DELETE(m_pAllocator);
      return hr;
    }

    // Hold a reference on the allocator
    m_pAllocator->AddRef();
  }

  // return the proper interface
  return m_pAllocator->QueryInterface(__uuidof(IMemAllocator), (void **)ppAlloc);
}

STDMETHODIMP CDecD3D11::CreateD3D11Device(UINT nDeviceIndex, ID3D11Device **ppDevice, DXGI_ADAPTER_DESC *pDesc)
{
  ID3D11Device *pD3D11Device = nullptr;

  // create DXGI factory
  IDXGIAdapter *pDXGIAdapter = nullptr;
  IDXGIFactory1 *pDXGIFactory = nullptr;
  HRESULT hr = dx.mCreateDXGIFactory1(IID_IDXGIFactory1, (void **)&pDXGIFactory);
  if (FAILED(hr))
  {
    DbgLog((LOG_ERROR, 10, L"-> DXGIFactory creation failed"));
    goto fail;
  }

  // find the adapter
enum_adapter:
  hr = pDXGIFactory->EnumAdapters(nDeviceIndex, &pDXGIAdapter);
  if (FAILED(hr))
  {
    if (nDeviceIndex != 0)
    {
      DbgLog((LOG_ERROR, 10, L"-> Requested DXGI device %d not available, falling back to default", nDeviceIndex));
      nDeviceIndex = 0;
      hr = pDXGIFactory->EnumAdapters(0, &pDXGIAdapter);
    }

    if (FAILED(hr))
    {
      DbgLog((LOG_ERROR, 10, L"-> Failed to enumerate a valid DXGI device"));
      goto fail;
    }
  }

  D3D_FEATURE_LEVEL d3dFeatureLevel;
  hr = dx.mD3D11CreateDevice(pDXGIAdapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, D3D11_CREATE_DEVICE_VIDEO_SUPPORT, s_D3D11Levels, countof(s_D3D11Levels), D3D11_SDK_VERSION, &pD3D11Device, &d3dFeatureLevel, nullptr);
  if (FAILED(hr))
  {
    if (nDeviceIndex != 0)
    {
      DbgLog((LOG_ERROR, 10, L"-> Failed to create a D3D11 device with video support on requested device %d, re-trying with default", nDeviceIndex));
      SafeRelease(&pDXGIAdapter);
      nDeviceIndex = 0;
      goto enum_adapter;
    }

    DbgLog((LOG_ERROR, 10, L"-> Failed to create a D3D11 device with video support"));
    goto fail;
  }

  DbgLog((LOG_TRACE, 10, L"-> Created D3D11 device with feature level %d.%d", d3dFeatureLevel >> 12, (d3dFeatureLevel >> 8) & 0xF));

  // enable multithreaded protection
  ID3D10Multithread *pMultithread = nullptr;
  hr = pD3D11Device->QueryInterface(&pMultithread);
  if (SUCCEEDED(hr)) {
    pMultithread->SetMultithreadProtected(TRUE);
    SafeRelease(&pMultithread);
  }

  // store adapter info
  if (pDesc)
  {
    ZeroMemory(pDesc, sizeof(*pDesc));
    pDXGIAdapter->GetDesc(pDesc);
  }

  // return device
  *ppDevice = pD3D11Device;

fail:
  SafeRelease(&pDXGIFactory);
  SafeRelease(&pDXGIAdapter);
  return hr;
}

STDMETHODIMP CDecD3D11::PostConnect(IPin *pPin)
{
  DbgLog((LOG_TRACE, 10, L"CDecD3D11::PostConnect()"));
  HRESULT hr = S_OK;

  ID3D11DecoderConfiguration *pD3D11DecoderConfiguration = nullptr;
  hr = pPin->QueryInterface(&pD3D11DecoderConfiguration);
  if (FAILED(hr)) {
    DbgLog((LOG_ERROR, 10, L"-> ID3D11DecoderConfiguration not available, using fallback mode"));
  }

  // Release old D3D resources, we're about to re-init
  m_pCallback->ReleaseAllDXVAResources();

  // free the decoder to force a re-init down the line
  SafeRelease(&m_pDecoder);

  // and the old device
  av_buffer_unref(&m_pDevCtx);

  // device id
  UINT nDevice = pD3D11DecoderConfiguration ? pD3D11DecoderConfiguration->GetD3D11AdapterIndex() : 0;

  // create the device
  ID3D11Device *pD3D11Device = nullptr;
  hr = CreateD3D11Device(nDevice, &pD3D11Device, &m_AdapterDesc);
  if (FAILED(hr))
  {
    goto fail;
  }

  // allocate and fill device context
  m_pDevCtx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA);
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;
  pDeviceContext->device = pD3D11Device;

  // finalize the context
  int ret = av_hwdevice_ctx_init(m_pDevCtx);
  if (ret < 0)
  {
    av_buffer_unref(&m_pDevCtx);
    goto fail;
  }

  // check if the connection supports native mode
  {
    CMediaType mt = m_pCallback->GetOutputMediaType();
    if ((m_SurfaceFormat == DXGI_FORMAT_NV12 && mt.subtype != MEDIASUBTYPE_NV12)
      || (m_SurfaceFormat == DXGI_FORMAT_P010 && mt.subtype != MEDIASUBTYPE_P010)
      || (m_SurfaceFormat == DXGI_FORMAT_P016 && mt.subtype != MEDIASUBTYPE_P016)) {
      DbgLog((LOG_ERROR, 10, L"-> Connection is not the appropriate pixel format for D3D11 Native"));

      m_bReadBackFallback = false;
      SafeRelease(&pD3D11DecoderConfiguration);
    }
  }

  // verify hardware support
  {
    GUID guidConversion = GUID_NULL;
    hr = FindVideoServiceConversion(m_pAVCtx->codec_id, m_pAVCtx->profile, m_SurfaceFormat, &guidConversion);
    if (FAILED(hr))
    {
      goto fail;
    }

    // get decoder configuration
    D3D11_VIDEO_DECODER_DESC desc = { 0 };
    desc.Guid = guidConversion;
    desc.OutputFormat = m_SurfaceFormat;
    desc.SampleWidth = m_pAVCtx->coded_width;
    desc.SampleHeight = m_pAVCtx->coded_height;

    D3D11_VIDEO_DECODER_CONFIG decoder_config = { 0 };
    hr = FindDecoderConfiguration(&desc, &decoder_config);
    if (FAILED(hr))
    {
      goto fail;
    }
  }

  // Notice the connected pin that we're sending D3D11 textures
  if (pD3D11DecoderConfiguration)
  {
    hr = pD3D11DecoderConfiguration->ActivateD3D11Decoding(pDeviceContext->device, pDeviceContext->device_context, pDeviceContext->lock_ctx, 0);
    SafeRelease(&pD3D11DecoderConfiguration);

    m_bReadBackFallback = FAILED(hr);
  }
  else
  {
    m_bReadBackFallback = true;
  }

  return S_OK;

fail:
  SafeRelease(&pD3D11DecoderConfiguration);
  return E_FAIL;
}

STDMETHODIMP CDecD3D11::InitDecoder(AVCodecID codec, const CMediaType *pmt)
{
  HRESULT hr = S_OK;
  DbgLog((LOG_TRACE, 10, L"CDecD3D11::InitDecoder(): Initializing D3D11 decoder"));

  // Destroy old decoder
  DestroyDecoder(false);

  // reset stream compatibility
  m_bFailHWDecode = false;

  m_DisplayDelay = D3D11_QUEUE_SURFACES;

  // Reduce display delay for DVD decoding for lower decode latency
  if (m_pCallback->GetDecodeFlags() & LAV_VIDEO_DEC_FLAG_DVD)
    m_DisplayDelay /= 2;

  // Initialize ffmpeg
  hr = CDecAvcodec::InitDecoder(codec, pmt);
  if (FAILED(hr))
    return hr;

  if (check_dxva_codec_profile(m_pAVCtx->codec_id, m_pAVCtx->pix_fmt, m_pAVCtx->profile, AV_PIX_FMT_D3D11))
  {
    DbgLog((LOG_TRACE, 10, L"-> Incompatible profile detected, falling back to software decoding"));
    return E_FAIL;
  }

  // initialize surface format to ensure the default media type is set properly
  bool bHighBitdepth = (m_pAVCtx->codec_id == AV_CODEC_ID_HEVC && (m_pAVCtx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 || m_pAVCtx->profile == FF_PROFILE_HEVC_MAIN_10))
                    || (m_pAVCtx->codec_id == AV_CODEC_ID_VP9  && (m_pAVCtx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 || m_pAVCtx->profile == FF_PROFILE_VP9_2));
  if (bHighBitdepth)
    m_SurfaceFormat = DXGI_FORMAT_P010;
  else
    m_SurfaceFormat = DXGI_FORMAT_NV12;

  m_dwSurfaceWidth = dxva_align_dimensions(m_pAVCtx->codec_id, m_pAVCtx->coded_width);
  m_dwSurfaceHeight = dxva_align_dimensions(m_pAVCtx->codec_id, m_pAVCtx->coded_height);

  return S_OK;
}

HRESULT CDecD3D11::AdditionaDecoderInit()
{
  AVD3D11VAContext *ctx = av_d3d11va_alloc_context();

  if (m_pDecoder) {
    FillHWContext(ctx);
  }

  m_pAVCtx->thread_count = 1;
  m_pAVCtx->hwaccel_context = ctx;
  m_pAVCtx->get_format = get_d3d11_format;
  m_pAVCtx->get_buffer2 = get_d3d11_buffer;
  m_pAVCtx->opaque = this;

  m_pAVCtx->slice_flags |= SLICE_FLAG_ALLOW_FIELD;

  // disable error concealment in hwaccel mode, it doesn't work either way
  m_pAVCtx->error_concealment = 0;
  av_opt_set_int(m_pAVCtx, "enable_er", 0, AV_OPT_SEARCH_CHILDREN);

  return S_OK;
}

STDMETHODIMP CDecD3D11::FillHWContext(AVD3D11VAContext *ctx)
{
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;

  ctx->decoder       = m_pDecoder;
  ctx->video_context = pDeviceContext->video_context;
  ctx->cfg           = &m_DecoderConfig;
  ctx->surface_count = m_nOutputViews;
  ctx->surface       = m_pOutputViews;

  ctx->context_mutex = pDeviceContext->lock_ctx;

  ctx->workaround    = 0;

  return S_OK;
}

STDMETHODIMP_(long) CDecD3D11::GetBufferCount()
{
  long buffers = 0;

  // Native decoding should use 16 buffers to enable seamless codec changes
  if (!m_bReadBackFallback)
    buffers = 16;
  else {
    // Buffers based on max ref frames
    if (m_nCodecId == AV_CODEC_ID_H264 || m_nCodecId == AV_CODEC_ID_HEVC)
      buffers = 16;
    else
      buffers = 2;
  }

  // 4 extra buffers for handling and safety
  buffers += 4;

  if (m_bReadBackFallback) {
    buffers += m_DisplayDelay;
  }

  if (m_pCallback->GetDecodeFlags() & LAV_VIDEO_DEC_FLAG_DVD) {
    buffers += 4;
  }
  return buffers;
}

STDMETHODIMP CDecD3D11::FlushDisplayQueue(BOOL bDeliver)
{
  for (int i = 0; i < m_DisplayDelay; ++i) {
    if (m_FrameQueue[m_FrameQueuePosition]) {
      if (bDeliver) {
        DeliverD3D11Frame(m_FrameQueue[m_FrameQueuePosition]);
        m_FrameQueue[m_FrameQueuePosition] = nullptr;
      }
      else {
        ReleaseFrame(&m_FrameQueue[m_FrameQueuePosition]);
      }
    }
    m_FrameQueuePosition = (m_FrameQueuePosition + 1) % m_DisplayDelay;
  }

  return S_OK;
}

STDMETHODIMP CDecD3D11::Flush()
{
  CDecAvcodec::Flush();

  // Flush display queue
  FlushDisplayQueue(FALSE);

  return S_OK;
}

STDMETHODIMP CDecD3D11::EndOfStream()
{
  CDecAvcodec::EndOfStream();

  // Flush display queue
  FlushDisplayQueue(TRUE);

  return S_OK;
}

HRESULT CDecD3D11::PostDecode()
{
  if (m_bFailHWDecode) {
    DbgLog((LOG_TRACE, 10, L"::PostDecode(): HW Decoder failed, falling back to software decoding"));
    return E_FAIL;
  }
  return S_OK;
}

enum AVPixelFormat CDecD3D11::get_d3d11_format(struct AVCodecContext *s, const enum AVPixelFormat * pix_fmts)
{
  CDecD3D11 *pDec = (CDecD3D11 *)s->opaque;
  const enum AVPixelFormat *p;
  for (p = pix_fmts; *p != -1; p++) {
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);

    if (!desc || !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
      break;

    if (*p == AV_PIX_FMT_D3D11)
    {
      HRESULT hr = pDec->ReInitD3D11Decoder(s);
      if (FAILED(hr))
      {
        pDec->m_bFailHWDecode = TRUE;
        continue;
      }
      else
      {
        break;
      }
    }
  }
  return *p;
}

int CDecD3D11::get_d3d11_buffer(struct AVCodecContext *c, AVFrame *frame, int flags)
{
  CDecD3D11 *pDec = (CDecD3D11 *)c->opaque;
  HRESULT hr = S_OK;

  if (frame->format != AV_PIX_FMT_D3D11) {
    DbgLog((LOG_ERROR, 10, L"D3D11 buffer request, but not D3D11 pixfmt"));
    pDec->m_bFailHWDecode = TRUE;
    return -1;
  }

  hr = pDec->ReInitD3D11Decoder(c);
  if (FAILED(hr)) {
    pDec->m_bFailHWDecode = TRUE;
    return -1;
  }

  if (pDec->m_bReadBackFallback && pDec->m_pFramesCtx)
  {
    int ret = av_hwframe_get_buffer(pDec->m_pFramesCtx, frame, 0);
    frame->width = c->coded_width;
    frame->height = c->coded_height;
    return ret;
  }
  else if (pDec->m_bReadBackFallback == false && pDec->m_pAllocator)
  {
    IMediaSample *pSample = nullptr;
    hr = pDec->m_pAllocator->GetBuffer(&pSample, nullptr, nullptr, 0);
    if (SUCCEEDED(hr))
    {
      CD3D11MediaSample *pD3D11Sample = dynamic_cast<CD3D11MediaSample *>(pSample);

      // fill the frame from the sample, including a reference to the sample
      pD3D11Sample->GetAVFrameBuffer(frame);

      frame->width = c->coded_width;
      frame->height = c->coded_height;

      // the frame holds the sample now, can release the direct interface
      pD3D11Sample->Release();
      return 0;
    }
  }

  return -1;
}

STDMETHODIMP CDecD3D11::ReInitD3D11Decoder(AVCodecContext *c)
{
  HRESULT hr = S_OK;

  // Don't allow decoder creation during first init
  if (m_bInInit)
    return S_FALSE;

  // sanity check that we have a device
  if (m_pDevCtx == nullptr)
    return E_FAIL;

  // we need an allocator at this point
  if (m_bReadBackFallback == false && m_pAllocator == nullptr)
    return E_FAIL;

  if (m_pDecoder == nullptr || m_dwSurfaceWidth != dxva_align_dimensions(c->codec_id, c->coded_width) || m_dwSurfaceHeight != dxva_align_dimensions(c->codec_id, c->coded_height) || m_DecodePixelFormat != c->sw_pix_fmt)
  {
    AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;
    DbgLog((LOG_TRACE, 10, L"No D3D11 Decoder or image dimensions changed -> Re-Allocating resources"));

    // if we're not in readback mode, we need to flush all the frames
    if (m_bReadBackFallback == false)
      avcodec_flush_buffers(c);
    else
      FlushDisplayQueue(TRUE);

    pDeviceContext->lock(pDeviceContext->lock_ctx);
    hr = CreateD3D11Decoder();
    pDeviceContext->unlock(pDeviceContext->lock_ctx);
    if (FAILED(hr))
      return hr;

    // Update the frames context in the allocator
    if (m_bReadBackFallback == false)
    {
      // decommit the allocator
      m_pAllocator->Decommit();

      // verify we were able to decommit all its resources
      if (m_pAllocator->DecommitInProgress()) {
        DbgLog((LOG_TRACE, 10, L"WARNING! D3D11 Allocator is still busy, trying to flush downstream"));
        m_pCallback->ReleaseAllDXVAResources();
        m_pCallback->GetOutputPin()->GetConnected()->BeginFlush();
        m_pCallback->GetOutputPin()->GetConnected()->EndFlush();
        if (m_pAllocator->DecommitInProgress()) {
          DbgLog((LOG_TRACE, 10, L"WARNING! Flush had no effect, decommit of the allocator still not complete"));
        }
        else {
          DbgLog((LOG_TRACE, 10, L"Flush was successfull, decommit completed!"));
        }
      }

      // re-commit it to update its frame reference
      m_pAllocator->Commit();
    }
  }

  return S_OK;
}

STDMETHODIMP CDecD3D11::FindVideoServiceConversion(AVCodecID codec, int profile, DXGI_FORMAT surface_format, GUID *input )
{
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;
  HRESULT hr = S_OK;

  UINT nProfiles = pDeviceContext->video_device->GetVideoDecoderProfileCount();
  GUID *guid_list = (GUID *)av_malloc_array(nProfiles, sizeof(*guid_list));

  DbgLog((LOG_TRACE, 10, L"-> Enumerating supported D3D11 modes (count: %d)", nProfiles));
  for (UINT i = 0; i < nProfiles; i++)
  {
    hr = pDeviceContext->video_device->GetVideoDecoderProfile(i, &guid_list[i]);
    if (FAILED(hr)) {
      DbgLog((LOG_ERROR, 10, L"Error retrieving decoder profile"));
      av_free(guid_list);
      return hr;
    }

#ifdef DEBUG
    const dxva_mode_t *mode = get_dxva_mode_from_guid(&guid_list[i]);
    if (mode) {
      DbgLog((LOG_TRACE, 10, L"  -> %S", mode->name));
    }
    else {
      DbgLog((LOG_TRACE, 10, L"  -> Unknown GUID (%s)", WStringFromGUID(guid_list[i]).c_str()));
    }
#endif
  }

  /* Iterate over our priority list */
  for (unsigned i = 0; dxva_modes[i].name; i++) {
    const dxva_mode_t *mode = &dxva_modes[i];
    if (!check_dxva_mode_compatibility(mode, codec, profile))
      continue;

    BOOL supported = FALSE;
    for (UINT g = 0; !supported && g < nProfiles; g++) {
      supported = IsEqualGUID(*mode->guid, guid_list[g]);
    }
    if (!supported)
      continue;

    DbgLog((LOG_TRACE, 10, L"-> Trying to use '%S'", mode->name));
    hr = pDeviceContext->video_device->CheckVideoDecoderFormat(mode->guid, surface_format, &supported);
    if (SUCCEEDED(hr) && supported) {
      *input = *mode->guid;

      av_free(guid_list);
      return S_OK;
    }
  }

  av_free(guid_list);
  return E_FAIL;
}

STDMETHODIMP CDecD3D11::FindDecoderConfiguration(const D3D11_VIDEO_DECODER_DESC *desc, D3D11_VIDEO_DECODER_CONFIG *pConfig)
{
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;
  HRESULT hr = S_OK;

  UINT nConfig = 0;
  hr = pDeviceContext->video_device->GetVideoDecoderConfigCount(desc, &nConfig);
  if (FAILED(hr)) {
    DbgLog((LOG_ERROR, 10, L"Unable to retreive decoder configuration count"));
    return E_FAIL;
  }

  int best_score = 0;
  D3D11_VIDEO_DECODER_CONFIG best_config;
  for (UINT i = 0; i < nConfig; i++)
  {
    D3D11_VIDEO_DECODER_CONFIG config = { 0 };
    hr = pDeviceContext->video_device->GetVideoDecoderConfig(desc, i, &config);
    if (FAILED(hr))
      continue;

    int score;
    if (config.ConfigBitstreamRaw == 1)
      score = 1;
    else if (m_pAVCtx->codec_id == AV_CODEC_ID_H264 && config.ConfigBitstreamRaw == 2)
      score = 2;
    else
      continue;
    if (IsEqualGUID(config.guidConfigBitstreamEncryption, DXVA2_NoEncrypt))
      score += 16;
    if (score > best_score) {
      best_score = score;
      best_config = config;
    }
  }

  if (best_score <= 0) {
    DbgLog((LOG_TRACE, 10, L"-> No matching configuration available"));
    return E_FAIL;
  }

  *pConfig = best_config;
  return S_OK;
}

static DXGI_FORMAT d3d11va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt)
{
  switch (pix_fmt) {
  case AV_PIX_FMT_YUV420P10:
  case AV_PIX_FMT_P010:       return DXGI_FORMAT_P010;
  case AV_PIX_FMT_NV12:
  default:                    return DXGI_FORMAT_NV12;
  }
}

STDMETHODIMP CDecD3D11::CreateD3D11Decoder()
{
  HRESULT hr = S_OK;
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;

  // release the old decoder, it needs to be re-created
  SafeRelease(&m_pDecoder);

  // find a decoder configuration
  GUID profileGUID = GUID_NULL;
  DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(m_pAVCtx->sw_pix_fmt);
  hr = FindVideoServiceConversion(m_pAVCtx->codec_id, m_pAVCtx->profile, surface_format, &profileGUID);
  if (FAILED(hr))
  {
    DbgLog((LOG_ERROR, 10, L"-> No video service profile found"));
    return hr;
  }

  // get decoder configuration
  D3D11_VIDEO_DECODER_DESC desc = { 0 };
  desc.Guid = profileGUID;
  desc.OutputFormat = surface_format;
  desc.SampleWidth = m_pAVCtx->coded_width;
  desc.SampleHeight = m_pAVCtx->coded_height;

  D3D11_VIDEO_DECODER_CONFIG decoder_config = { 0 };
  hr = FindDecoderConfiguration(&desc, &decoder_config);
  if (FAILED(hr))
  {
    DbgLog((LOG_ERROR, 10, L"-> No valid video decoder configuration found"));
    return hr;
  }

  m_DecoderConfig = decoder_config;

  // update surface properties
  m_dwSurfaceWidth = dxva_align_dimensions(m_pAVCtx->codec_id, m_pAVCtx->coded_width);
  m_dwSurfaceHeight = dxva_align_dimensions(m_pAVCtx->codec_id, m_pAVCtx->coded_height);
  m_DecodePixelFormat = m_pAVCtx->sw_pix_fmt;
  m_SurfaceFormat = surface_format;

  if (m_bReadBackFallback == false && m_pAllocator)
  {
    ALLOCATOR_PROPERTIES properties;
    hr = m_pAllocator->GetProperties(&properties);
    if (FAILED(hr))
      return hr;

    m_dwSurfaceCount = properties.cBuffers;
  }
  else
  {
    m_dwSurfaceCount = GetBufferCount();
  }

  // allocate a new frames context for the dimensions and format
  hr = AllocateFramesContext(m_dwSurfaceWidth, m_dwSurfaceHeight, m_DecodePixelFormat, m_dwSurfaceCount, &m_pFramesCtx);
  if (FAILED(hr))
  {
    DbgLog((LOG_ERROR, 10, L"-> Error allocating frames context"));
    return hr;
  }

  // release any old output views and allocate memory for the new ones
  if (m_pOutputViews)
  {
    for (int i = 0; i < m_nOutputViews; i++)
    {
      SafeRelease(&m_pOutputViews[i]);
    }
    av_freep(&m_pOutputViews);
  }

  m_pOutputViews = (ID3D11VideoDecoderOutputView **)av_mallocz_array(m_dwSurfaceCount, sizeof(*m_pOutputViews));
  m_nOutputViews = m_dwSurfaceCount;

  // allocate output views for the frames
  AVD3D11VAFramesContext *pFramesContext = (AVD3D11VAFramesContext *)((AVHWFramesContext *)m_pFramesCtx->data)->hwctx;
  for (int i = 0; i < m_nOutputViews; i++)
  {
    D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = { 0 };
    viewDesc.DecodeProfile = profileGUID;
    viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
    viewDesc.Texture2D.ArraySlice = i;

    hr = pDeviceContext->video_device->CreateVideoDecoderOutputView(pFramesContext->texture, &viewDesc, &m_pOutputViews[i]);
    if (FAILED(hr))
    {
      DbgLog((LOG_ERROR, 10, L"-> Failed to create video decoder output views"));
      return E_FAIL;
    }
  }

  // create the decoder
  hr = pDeviceContext->video_device->CreateVideoDecoder(&desc, &decoder_config, &m_pDecoder);
  if (FAILED(hr))
  {
    DbgLog((LOG_ERROR, 10, L"-> Failed to create video decoder object"));
    return E_FAIL;
  }

  FillHWContext((AVD3D11VAContext *)m_pAVCtx->hwaccel_context);

  return S_OK;
}

STDMETHODIMP CDecD3D11::AllocateFramesContext(int width, int height, AVPixelFormat format, int nSurfaces, AVBufferRef **ppFramesCtx)
{
  ASSERT(m_pAVCtx);
  ASSERT(m_pDevCtx);
  ASSERT(ppFramesCtx);

  // unref any old buffer
  av_buffer_unref(ppFramesCtx);
  SafeRelease(&m_pD3D11StagingTexture);

  // allocate a new frames context for the device context
  *ppFramesCtx = av_hwframe_ctx_alloc(m_pDevCtx);
  if (*ppFramesCtx == nullptr)
    return E_OUTOFMEMORY;

  AVHWFramesContext *pFrames = (AVHWFramesContext *)(*ppFramesCtx)->data;
  pFrames->format = AV_PIX_FMT_D3D11;
  pFrames->sw_format = (format == AV_PIX_FMT_YUV420P10) ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
  pFrames->width = width;
  pFrames->height = height;
  pFrames->initial_pool_size = nSurfaces;

  AVD3D11VAFramesContext *pFramesHWContext = (AVD3D11VAFramesContext *)pFrames->hwctx;
  pFramesHWContext->BindFlags |= D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE;
  pFramesHWContext->MiscFlags |= D3D11_RESOURCE_MISC_SHARED;

  int ret = av_hwframe_ctx_init(*ppFramesCtx);
  if (ret < 0)
  {
    av_buffer_unref(ppFramesCtx);
    return E_FAIL;
  }

  return S_OK;
}

HRESULT CDecD3D11::HandleDXVA2Frame(LAVFrame *pFrame)
{
  ASSERT(pFrame->format == LAVPixFmt_D3D11);

  if (pFrame->flags & LAV_FRAME_FLAG_FLUSH) {
    if (m_bReadBackFallback) {
      FlushDisplayQueue(TRUE);
    }
    DeliverD3D11Frame(pFrame);
    return S_OK;
  }

  if (m_bReadBackFallback == false || m_DisplayDelay == 0)
  {
    DeliverD3D11Frame(pFrame);
  }
  else
  {
    LAVFrame *pQueuedFrame = m_FrameQueue[m_FrameQueuePosition];
    m_FrameQueue[m_FrameQueuePosition] = pFrame;

    m_FrameQueuePosition = (m_FrameQueuePosition + 1) % m_DisplayDelay;

    if (pQueuedFrame) {
      DeliverD3D11Frame(pQueuedFrame);
    }
  }

  return S_OK;
}

HRESULT CDecD3D11::DeliverD3D11Frame(LAVFrame *pFrame)
{
  if (m_bReadBackFallback)
  {
    if (m_bDirect)
      DeliverD3D11ReadbackDirect(pFrame);
    else
      DeliverD3D11Readback(pFrame);
  }
  else
  {
    AVFrame *pAVFrame = (AVFrame *)pFrame->priv_data;
    pFrame->data[0] = pAVFrame->data[3];
    pFrame->data[1] = pFrame->data[2] = pFrame->data[3] = nullptr;

    GetPixelFormat(&pFrame->format, &pFrame->bpp);

    Deliver(pFrame);
  }

  return S_OK;
}

HRESULT CDecD3D11::DeliverD3D11Readback(LAVFrame *pFrame)
{
  AVFrame *src = (AVFrame *)pFrame->priv_data;
  AVFrame *dst = av_frame_alloc();

  int ret = av_hwframe_transfer_data(dst, src, 0);
  if (ret < 0)
  {
    ReleaseFrame(&pFrame);
    av_frame_free(&dst);
    return E_FAIL;
  }

  // free the source frame
  av_frame_free(&src);

  // and store the dst frame in LAVFrame
  pFrame->priv_data = dst;
  GetPixelFormat(&pFrame->format, &pFrame->bpp);

  ASSERT((dst->format == AV_PIX_FMT_NV12 && pFrame->format == LAVPixFmt_NV12) || (dst->format == AV_PIX_FMT_P010 && pFrame->format == LAVPixFmt_P016));

  for (int i = 0; i < 4; i++) {
    pFrame->data[i] = dst->data[i];
    pFrame->stride[i] = dst->linesize[i];
  }

  return Deliver(pFrame);
}

struct D3D11DirectPrivate
{
  AVBufferRef *pDeviceContex;
  ID3D11Texture2D *pStagingTexture;
};

static bool d3d11_direct_lock(LAVFrame * pFrame, LAVDirectBuffer *pBuffer)
{
  D3D11DirectPrivate *c = (D3D11DirectPrivate *)pFrame->priv_data;
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)c->pDeviceContex->data)->hwctx;
  D3D11_TEXTURE2D_DESC desc;
  D3D11_MAPPED_SUBRESOURCE map;

  ASSERT(pFrame && pBuffer);

  // lock the device context
  pDeviceContext->lock(pDeviceContext->lock_ctx);

  c->pStagingTexture->GetDesc(&desc);

  // map
  HRESULT hr = pDeviceContext->device_context->Map(c->pStagingTexture, 0, D3D11_MAP_READ, 0, &map);
  if (FAILED(hr))
  {
    pDeviceContext->unlock(pDeviceContext->lock_ctx);
    return false;
  }

  pBuffer->data[0] = (BYTE *)map.pData;
  pBuffer->data[1] = pBuffer->data[0] + desc.Height * map.RowPitch;

  pBuffer->stride[0] = map.RowPitch;
  pBuffer->stride[1] = map.RowPitch;

  return true;
}

static void d3d11_direct_unlock(LAVFrame * pFrame)
{
  D3D11DirectPrivate *c = (D3D11DirectPrivate *)pFrame->priv_data;
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)c->pDeviceContex->data)->hwctx;

  pDeviceContext->device_context->Unmap(c->pStagingTexture, 0);
  pDeviceContext->unlock(pDeviceContext->lock_ctx);
}

static void d3d11_direct_free(LAVFrame * pFrame)
{
  D3D11DirectPrivate *c = (D3D11DirectPrivate *)pFrame->priv_data;
  av_buffer_unref(&c->pDeviceContex);
  c->pStagingTexture->Release();
  delete c;
}

HRESULT CDecD3D11::DeliverD3D11ReadbackDirect(LAVFrame *pFrame)
{
  AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx;
  AVFrame *src = (AVFrame *)pFrame->priv_data;

  if (m_pD3D11StagingTexture == nullptr)
  {
    D3D11_TEXTURE2D_DESC texDesc = { 0 };
    ((ID3D11Texture2D *)src->data[0])->GetDesc(&texDesc);

    texDesc.ArraySize = 1;
    texDesc.Usage = D3D11_USAGE_STAGING;
    texDesc.BindFlags = 0;
    texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;

    HRESULT hr = pDeviceContext->device->CreateTexture2D(&texDesc, nullptr, &m_pD3D11StagingTexture);
    if (FAILED(hr))
    {
      ReleaseFrame(&pFrame);
      return E_FAIL;
    }
  }

  pDeviceContext->lock(pDeviceContext->lock_ctx);
  pDeviceContext->device_context->CopySubresourceRegion(m_pD3D11StagingTexture, 0, 0, 0, 0, (ID3D11Texture2D *)src->data[0], (UINT)(intptr_t)src->data[1], nullptr);
  pDeviceContext->unlock(pDeviceContext->lock_ctx);

  av_frame_free(&src);

  D3D11DirectPrivate *c = new D3D11DirectPrivate;
  c->pDeviceContex = av_buffer_ref(m_pDevCtx);
  c->pStagingTexture = m_pD3D11StagingTexture;
  m_pD3D11StagingTexture->AddRef();

  pFrame->priv_data = c;
  pFrame->destruct = d3d11_direct_free;

  GetPixelFormat(&pFrame->format, &pFrame->bpp);

  pFrame->direct = true;
  pFrame->direct_lock = d3d11_direct_lock;
  pFrame->direct_unlock = d3d11_direct_unlock;

  return Deliver(pFrame);
}

STDMETHODIMP CDecD3D11::GetPixelFormat(LAVPixelFormat *pPix, int *pBpp)
{
  // Output is always NV12 or P010
  if (pPix)
    *pPix = m_bReadBackFallback == false ? LAVPixFmt_D3D11 : ((m_SurfaceFormat == DXGI_FORMAT_P010 || m_SurfaceFormat == DXGI_FORMAT_P016) ? LAVPixFmt_P016 : LAVPixFmt_NV12);

  if (pBpp)
    *pBpp = (m_SurfaceFormat == DXGI_FORMAT_P016) ? 16 : (m_SurfaceFormat == DXGI_FORMAT_P010 ? 10 : 8);

  return S_OK;
}

STDMETHODIMP CDecD3D11::GetHWAccelActiveDevice(BSTR *pstrDeviceName)
{
  CheckPointer(pstrDeviceName, E_POINTER);

  if (m_AdapterDesc.Description[0] == 0)
    return E_UNEXPECTED;

  *pstrDeviceName = SysAllocString(m_AdapterDesc.Description);
  return S_OK;
}