-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuickSync.cpp
1497 lines (1250 loc) · 50 KB
/
QuickSync.cpp
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
/*
* Copyright (c) 2013, INTEL CORPORATION
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* Neither the name of INTEL CORPORATION nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include "IQuickSyncDecoder.h"
#include "QuickSync_defs.h"
#include "CodecInfo.h"
#include "TimeManager.h"
#include "QuickSyncUtils.h"
#include "frame_constructors.h"
#include "QuickSyncDecoder.h"
#include "QuickSyncVPP.h"
#include "QuickSync.h"
#define PAGE_MASK 4095
EXTERN_GUID(WMMEDIASUBTYPE_WVC1,
0x31435657, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71);
EXTERN_GUID(WMMEDIASUBTYPE_WMV3,
0x33564D57, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71);
DEFINE_GUID(WMMEDIASUBTYPE_WVC1_PDVD,
0xD979F77B, 0xDBEA, 0x4BF6, 0x9E, 0x6D, 0x1D, 0x7E, 0x57, 0xFB, 0xAD, 0x53);
////////////////////////////////////////////////////////////////////
// CQuickSync
////////////////////////////////////////////////////////////////////
CQuickSync::CQuickSync() :
m_bInitialized(false),
m_pVPP(NULL),
m_nPitch(0),
m_pFrameConstructor(NULL),
m_nSegmentFrameCount(0),
m_bFlushing(false),
m_bNeedToFlush(false),
m_bDvdDecoding(false),
m_PicStruct(0),
m_SurfaceType(QS_SURFACE_SYSTEM),
m_ProcessedFrame(new QsFrameData, new CQsAlignedBuffer(0)
)
{
MSDK_TRACE("QsDecoder: Constructor\n");
strcpy_s(m_CodecName, "Intel\xae QuickSync Decoder");
mfxStatus sts = MFX_ERR_NONE;
MSDK_ZERO_VAR(m_DecVideoParams);
//m_DecVideoParams.AsyncDepth = 0; // Causes issues when 1 - in old drivers
m_DecVideoParams.mfx.ExtendedPicStruct = 1;
//
// Set default configuration - override what's not zero/false
//
m_Config.bTimeStampCorrection = true;
m_Config.nOutputQueueLength = 8;
m_Config.bEnableH264 = true;
m_Config.bEnableMPEG2 = true;
m_Config.bEnableVC1 = true;
m_Config.bEnableWMV9 = true;
m_Config.bEnableMultithreading = true;
m_Config.bEnableMtCopy = m_Config.bEnableMultithreading;
// For testing purposes only - menus are not handled well
// m_Config.bEnableSwEmulation = true;
// Currently not working well - menu decoding :(
// m_Config.bEnableDvdDecoding = true;
// Force field order
// m_Config.bForceFieldOrder = true;
// m_Config.eFieldOrder = QS_FIELD_TFF;
// VPP - Video Post Processing
m_Config.bEnableVideoProcessing = true;
m_Config.bVppEnableDeinterlacing = true;
m_Config.bVppEnableFullRateDI = true;
m_Config.bVppEnableDITimeStampsInterpolation = true;
// m_Config.bVppEnableForcedDeinterlacing = true;
// m_Config.nVppDetailStrength = 16; // 0-64
// m_Config.nVppDenoiseStrength = 16; // 0-64
// m_Config.bDropDuplicateFrames = true;
DWORD dwVersion = GetVersion();
DWORD dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
DWORD dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
// No QS support for Windows version older than Vista
if (dwMajorVersion < 6)
{
sts = MFX_ERR_UNSUPPORTED;
return;
}
// D3D11 decode was introduced with Win8 (Windows 6.2)
// Window Vista and 7 can use D3D9
else if ((dwMajorVersion == 6 && dwMinorVersion >= 2) || (dwMajorVersion > 6))
{
#if MFX_D3D11_SUPPORT
m_Config.bEnableD3D11 = true;
// m_Config.bDefaultToD3D11 = true;
#endif
}
m_pDecoder = new CQuickSyncDecoder(m_Config, sts);
m_OK = MSDK_SUCCEEDED(sts);
}
CQuickSync::~CQuickSync()
{
MSDK_TRACE("QsDecoder: Destructor\n");
// This will quicken the exit
BeginFlush();
CQsAutoLock cObjectLock(&m_csLock);
delete m_ProcessedFrame.first;
delete m_ProcessedFrame.second;
MSDK_SAFE_DELETE(m_pFrameConstructor);
MSDK_SAFE_DELETE(m_pDecoder);
}
HRESULT CQuickSync::HandleSubType(const AM_MEDIA_TYPE* mtIn, FOURCC fourCC, mfxVideoParam& videoParams, CFrameConstructor*& pFrameConstructor)
{
const GUID& subType = mtIn->subtype;
const GUID& guidFormat = mtIn->formattype;
MSDK_SAFE_DELETE(pFrameConstructor);
// MPEG2
if (subType == MEDIASUBTYPE_MPEG2_VIDEO)
{
if (!m_Config.bEnableMPEG2)
return VFW_E_INVALIDMEDIATYPE;
videoParams.mfx.CodecId = MFX_CODEC_MPEG2;
pFrameConstructor = new CFrameConstructor(&m_TimeManager);
}
// VC1 or WMV3
else if ((fourCC == FOURCC_VC1) || (fourCC == FOURCC_WMV3))
{
videoParams.mfx.CodecId = MFX_CODEC_VC1;
if (subType == WMMEDIASUBTYPE_WMV3)
{
videoParams.mfx.CodecProfile = MFX_PROFILE_VC1_SIMPLE;
if (!m_Config.bEnableWMV9)
return VFW_E_INVALIDMEDIATYPE;
}
else if (fourCC == FOURCC_VC1)
{
videoParams.mfx.CodecProfile = MFX_PROFILE_VC1_ADVANCED;
if (!m_Config.bEnableVC1)
return VFW_E_INVALIDMEDIATYPE;
}
else
{
return VFW_E_INVALIDMEDIATYPE;
}
pFrameConstructor = new CVC1FrameConstructor(&m_TimeManager);
}
// H264
else if ((fourCC == FOURCC_H264) || (fourCC == FOURCC_X264) || (fourCC == FOURCC_h264) ||
(fourCC == FOURCC_avc1) || (fourCC == FOURCC_VSSH) || (fourCC == FOURCC_DAVC) ||
(fourCC == FOURCC_PAVC) || (fourCC == FOURCC_AVC1) || (fourCC == FOURCC_CCV1))
{
if (!m_Config.bEnableH264)
return VFW_E_INVALIDMEDIATYPE;
videoParams.mfx.CodecId = MFX_CODEC_AVC;
// Note: CAVCFrameConstructor can handle both H264 stream types but it can't handle fragment streams (e.g. live TV)
pFrameConstructor = ((fourCC == FOURCC_avc1) || (fourCC == FOURCC_AVC1) || (fourCC == FOURCC_CCV1)) ?
new CAVCFrameConstructor(&m_TimeManager) :
new CFrameConstructor(&m_TimeManager);
// pFrameConstructor = new CAVCFrameConstructor(&m_TimeManager);
}
else
{
return E_NOTIMPL;
}
// Check if codec profile and level are supported before calling DecodeHeader
mfxInfoMFX& mfx = videoParams.mfx;
if (FORMAT_MPEG2_VIDEO == guidFormat)
{
MPEG2VIDEOINFO* mp2 = (MPEG2VIDEOINFO*)(mtIn->pbFormat);
HRESULT hr = CheckCodecProfileSupport(mfx.CodecId, mp2->dwProfile);
if (hr != S_OK)
{
MSDK_TRACE("QsDecoder::InitDecoder - failed due to unsupported codec (%s), profile (%s), level (%i) combination\n",
::GetCodecName(mfx.CodecId), GetProfileName(mfx.CodecId, mp2->dwProfile), mp2->dwLevel);
return E_NOTIMPL;
}
else
{
MSDK_TRACE("QsDecoder::InitDecoder - codec (%s), profile (%s), level (%i)\n",
::GetCodecName(mfx.CodecId), GetProfileName(mfx.CodecId, mp2->dwProfile), mp2->dwLevel);
}
}
else
{
MSDK_TRACE("QsDecoder::InitDecoder - codec (%s)\n", ::GetCodecName(mfx.CodecId));
}
return S_OK;
}
HRESULT CQuickSync::TestMediaType(const AM_MEDIA_TYPE* mtIn, FOURCC fourCC)
{
MSDK_TRACE("QsDecoder: TestMediaType\n");
if (!m_OK)
{
MSDK_TRACE("QsDecoder: TestMediaType was called on an invalid object!\n");
return E_FAIL;
}
// Parameter check
MSDK_CHECK_POINTER(mtIn, E_POINTER);
MSDK_CHECK_POINTER(mtIn->pbFormat, E_UNEXPECTED);
if (mtIn->majortype != MEDIATYPE_Video && mtIn->majortype != MEDIATYPE_DVD_ENCRYPTED_PACK)
{
MSDK_TRACE("QsDecoder: Invalid majortype GUID!\n");
return E_FAIL;
}
VIDEOINFOHEADER2* vih2 = NULL;
size_t nSampleSize = 0, nVideoInfoSize = 0;
mfxVideoParam videoParams;
MSDK_ZERO_VAR(videoParams);
CFrameConstructor* pFrameConstructor = NULL;
HRESULT hr = DecodeHeader(mtIn, fourCC, pFrameConstructor, vih2, nSampleSize, nVideoInfoSize, videoParams);
MSDK_SAFE_DELETE(pFrameConstructor);
MSDK_CHECK_RESULT_P_RET(hr, S_OK);
// If SW emulation is disabled - check if HW acceleration is supported
if (!m_Config.bEnableSwEmulation)
{
mfxStatus sts = m_pDecoder->CheckHwAcceleration(&videoParams);
if (MSDK_FAILED(sts))
{
MSDK_TRACE("HW accelration is not supported. Aborting!\n");
hr = E_FAIL;
}
}
delete[] (mfxU8*)vih2;
MSDK_TRACE("QsDecoder: TestMediaType finished: %s\n", (SUCCEEDED(hr)) ? "success" : "failure");
return hr;
}
HRESULT CQuickSync::CopyMediaTypeToVIDEOINFOHEADER2(const AM_MEDIA_TYPE* mtIn, VIDEOINFOHEADER2*& vih2, size_t& nVideoInfoSize, size_t& nSampleSize)
{
vih2 = NULL;
const GUID& guidFormat = mtIn->formattype;
nVideoInfoSize = sizeof(VIDEOINFOHEADER2);
if (FORMAT_VideoInfo2 == guidFormat || FORMAT_MPEG2_VIDEO == guidFormat)
{
if (FORMAT_MPEG2_VIDEO == guidFormat)
{
nVideoInfoSize = sizeof(MPEG2VIDEOINFO);
}
nSampleSize = mtIn->cbFormat;
vih2 = (VIDEOINFOHEADER2*) new mfxU8[nSampleSize];
// Copy the sample as is
memcpy(vih2, mtIn->pbFormat, nSampleSize);
}
// Translate VIDEOINFOHEADER to VIDEOINFOHEADER2 (easier to work with down the road)
else if (FORMAT_VideoInfo == guidFormat)
{
size_t extraDataLen = mtIn->cbFormat - sizeof(VIDEOINFOHEADER);
nSampleSize = sizeof(VIDEOINFOHEADER2) + extraDataLen;
vih2 = (VIDEOINFOHEADER2*) new mfxU8[nSampleSize];
MSDK_ZERO_MEMORY(vih2, sizeof(VIDEOINFOHEADER2));
// Initialize the VIDEOINFOHEADER2 structure
VIDEOINFOHEADER* pvi = (VIDEOINFOHEADER*)(mtIn->pbFormat);
vih2->rcSource = pvi->rcSource;
vih2->rcTarget = pvi->rcTarget;
vih2->dwBitRate = pvi->dwBitRate;
vih2->dwBitErrorRate = pvi->dwBitErrorRate;
vih2->bmiHeader = pvi->bmiHeader;
vih2->AvgTimePerFrame = pvi->AvgTimePerFrame;
vih2->dwPictAspectRatioX = pvi->bmiHeader.biWidth;
vih2->dwPictAspectRatioY = pvi->bmiHeader.biHeight;
// Copy the out of band data (data past the VIDEOINFOHEADER structure.
// Note: copy past the size of vih2
if (extraDataLen)
{
memcpy(((mfxU8*)vih2) + nVideoInfoSize, mtIn->pbFormat + sizeof(VIDEOINFOHEADER), extraDataLen);
}
}
else
{
return VFW_E_INVALIDMEDIATYPE;
}
return S_OK;
}
HRESULT CQuickSync::DecodeHeader(
const AM_MEDIA_TYPE* mtIn,
FOURCC fourCC,
CFrameConstructor*& pFrameConstructor,
VIDEOINFOHEADER2*& vih2,
size_t nSampleSize,
size_t& nVideoInfoSize,
mfxVideoParam& videoParams)
{
mfxInfoMFX& mfx = videoParams.mfx;
const GUID& guidFormat = mtIn->formattype;
HRESULT hr = S_OK;
mfxStatus sts = MFX_ERR_NONE;
hr = HandleSubType(mtIn, fourCC, videoParams, pFrameConstructor);
MSDK_CHECK_RESULT_P_RET(hr, S_OK);
hr = CopyMediaTypeToVIDEOINFOHEADER2(mtIn, vih2, nVideoInfoSize, nSampleSize);
MSDK_CHECK_RESULT_P_RET(hr, S_OK);
if (MEDIATYPE_DVD_ENCRYPTED_PACK == mtIn->majortype)
{
if (!m_Config.bEnableDvdDecoding)
return VFW_E_INVALIDMEDIATYPE;
pFrameConstructor->SetDvdPacketStripping(true);
m_bDvdDecoding = true;
}
if (!vih2->bmiHeader.biWidth || !vih2->bmiHeader.biHeight)
{
return VFW_E_INVALIDMEDIATYPE;
}
// Construct sequence header and decode the sequence header.
if (nVideoInfoSize < nSampleSize)
{
sts = pFrameConstructor->ConstructHeaders(vih2, guidFormat, nSampleSize, nVideoInfoSize);
if (MSDK_SUCCEEDED(sts))
{
sts = m_pDecoder->DecodeHeader(&pFrameConstructor->GetHeaders(), &videoParams);
if (MSDK_FAILED(sts))
{
MSDK_TRACE("QsDecoder: Warning DecodeHeader failed!\n");
// ASSERT(MSDK_SUCCEEDED(sts));
}
}
}
// Simple info header (without extra data) or DecodeHeader failed (usually not critical, in many cases header will appear later in the stream)
if (nVideoInfoSize == nSampleSize || MSDK_FAILED(sts))
{
mfx.FrameInfo.CropW = (mfxU16)vih2->bmiHeader.biWidth;
mfx.FrameInfo.CropH = (mfxU16)vih2->bmiHeader.biHeight;
mfx.FrameInfo.Width = (mfxU16)MSDK_ALIGN16(mfx.FrameInfo.CropW);
mfx.FrameInfo.Height = (mfxU16)MSDK_ALIGN16(mfx.FrameInfo.CropH);
mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
ConvertFrameRate((vih2->AvgTimePerFrame) ? 1e7 / vih2->AvgTimePerFrame : 0,
mfx.FrameInfo.FrameRateExtN,
mfx.FrameInfo.FrameRateExtD);
// Get codec/profile from MPEG2VIDEOINFO header
if (nVideoInfoSize == sizeof(MPEG2VIDEOINFO))
{
if (mfx.CodecId == MFX_CODEC_AVC)
{
MPEG2VIDEOINFO* mp2 = (MPEG2VIDEOINFO*)(vih2);
mfx.CodecProfile = (mfxU16)mp2->dwProfile;
mfx.CodecLevel = (mfxU16)mp2->dwLevel;
}
}
sts = MFX_ERR_NONE;
}
hr = (MSDK_SUCCEEDED(sts)) ? S_OK : E_FAIL;
if (FAILED(hr))
{
delete[] (mfxU8*)vih2;
vih2 = NULL;
}
return hr;
}
HRESULT CQuickSync::InitDecoder(const AM_MEDIA_TYPE* mtIn, FOURCC fourCC)
{
MSDK_TRACE("QsDecoder: InitDecoder\n");
CQsAutoLock cObjectLock(&m_csLock);
// 2nd initialization - empty queues and kill VPP
if (m_bInitialized)
{
OnSeek(0);
MSDK_ZERO_VAR(m_DecVideoParams.mfx.FrameInfo);
}
m_bNeedToFlush = true;
m_bInitialized = true;
VIDEOINFOHEADER2* vih2 = NULL;
mfxStatus sts = MFX_ERR_NONE;
mfxInfoMFX& mfx = m_DecVideoParams.mfx;
HRESULT hr;
MSDK_CHECK_POINTER(mtIn, E_POINTER);
MSDK_CHECK_POINTER(mtIn->pbFormat, E_UNEXPECTED);
bool bIsFields = false;
size_t nSampleSize = 0, nVideoInfoSize = 0;
if (!(mtIn->majortype == MEDIATYPE_DVD_ENCRYPTED_PACK || mtIn->majortype == MEDIATYPE_Video))
return VFW_E_INVALIDMEDIATYPE;
// Delete frame constructor from previous run
MSDK_SAFE_DELETE(m_pFrameConstructor);
hr = DecodeHeader(mtIn, fourCC, m_pFrameConstructor, vih2, nSampleSize, nVideoInfoSize, m_DecVideoParams);
// Setup frame rate from either the media type or the decoded header
bIsFields = (vih2->dwInterlaceFlags & AMINTERLACE_IsInterlaced) &&
(vih2->dwInterlaceFlags & AMINTERLACE_1FieldPerSample);
// Try getting the frame rate from the decoded headers
if (0 < vih2->AvgTimePerFrame)
{
m_TimeManager.SetFrameRate(1e7 / vih2->AvgTimePerFrame, bIsFields);
// Workaround for buggy SDK frame manipulation when H264 SPS header contains a zero frame rate
if (0 == mfx.FrameInfo.FrameRateExtN * mfx.FrameInfo.FrameRateExtD)
{
ConvertFrameRate((vih2->AvgTimePerFrame) ? 1e7 / vih2->AvgTimePerFrame : 0,
mfx.FrameInfo.FrameRateExtN,
mfx.FrameInfo.FrameRateExtD);
}
}
// In case we don't have a frame rate
else if (mfx.FrameInfo.FrameRateExtN * mfx.FrameInfo.FrameRateExtD != 0)
{
double frameRate = (double)mfx.FrameInfo.FrameRateExtN / (double)mfx.FrameInfo.FrameRateExtD;
m_TimeManager.SetFrameRate(frameRate, bIsFields);
}
// We might decode well even if DecodeHeader failed with MFX_ERR_MORE_DATA
MSDK_IGNORE_MFX_STS(sts, MFX_ERR_MORE_DATA);
m_nPitch = (mfxU16)MSDK_ALIGN32(mfx.FrameInfo.Width);
mfx.FrameInfo.Height = (mfxU16)MSDK_ALIGN32(mfx.FrameInfo.Height);
SetAspectRatio(*vih2, mfx.FrameInfo);
// Disable MT features if main flag is off
m_Config.bEnableMtCopy = m_Config.bEnableMtCopy && m_Config.bEnableMultithreading;
// Video processing
if (m_Config.bEnableVideoProcessing)
{
// If forced DI is enabled, DI is enabled.
// DI is auto enabled when detail/denoise are on as these work on progressive frames.
if (m_Config.bVppEnableForcedDeinterlacing || m_Config.nVppDetailStrength > 0 || m_Config.nVppDenoiseStrength> 0)
{
m_Config.bVppEnableDeinterlacing = true;
}
if (m_Config.bVppEnableForcedDeinterlacing)
{
MSDK_TRACE("QsDecoder: forced deinterlacing is active\n");
}
else if (m_Config.bVppEnableDeinterlacing)
{
MSDK_TRACE("QsDecoder: auto deinterlacing is active\n");
}
if (m_Config.nVppDetailStrength > 0)
{
MSDK_TRACE("QsDecoder: detail filter is at %i%%\n", (32 + 100 * m_Config.nVppDetailStrength) / 64);
}
if (m_Config.nVppDenoiseStrength > 0)
{
MSDK_TRACE("QsDecoder: denoise filter is at %i%%\n", (32 + 100 * m_Config.nVppDenoiseStrength) / 64);
}
}
// Make sure all VPP features are disabled
else
{
m_Config.vpp = 0;
}
if (m_Config.bForceFieldOrder)
{
switch (m_Config.eFieldOrder)
{
case QS_FIELD_AUTO:
MSDK_TRACE("QsDecoder: field order is set to AUTO\n");
break;
case QS_FIELD_TFF:
MSDK_TRACE("QsDecoder: field order is set to TFF\n");
break;
case QS_FIELD_BFF:
MSDK_TRACE("QsDecoder: field order is set to BFF\n");
break;
default:
MSDK_TRACE("QsDecoder: invalid field order parameter reverting to AUTO!\n");
m_Config.eFieldOrder = QS_FIELD_AUTO;
}
}
// Initialization of Media SDK decoder is done in OnSeek to allow late initialization needed
// by full screen exclusive (FSE) mode since D3D devices can't be created. The DS filter must send
// the D3D device manager to this decoder for surface allocation.
if (MSDK_SUCCEEDED(sts))
{
m_pDecoder->SetConfig(m_Config);
size_t surfaceCount = max(8, m_Config.nOutputQueueLength);
if (m_Config.bVppEnableDeinterlacing)
{
surfaceCount += 5;
}
m_pDecoder->SetAuxFramesCount(surfaceCount);
}
m_TimeManager.Enabled() = m_Config.bTimeStampCorrection;
_snprintf_s(m_CodecName, MSDK_ARRAY_LEN(m_CodecName), MSDK_ARRAY_LEN(m_CodecName)-1,
"Intel\xae QuickSync Decoder (%s) - %s",
::GetCodecName(m_DecVideoParams.mfx.CodecId),
(m_pDecoder->IsHwAccelerated()) ? (m_pDecoder->IsD3D11Alloc() ? "HW D3D11" : "HW D3D9" ) : "SW"
);
delete[] (mfxU8*)vih2;
m_OK = MSDK_SUCCEEDED(sts);
return (m_OK) ? S_OK : E_FAIL;
}
void CQuickSync::SetAspectRatio(VIDEOINFOHEADER2& vih2, mfxFrameInfo& frameInfo)
{
// Fix small aspect ratio errors
if (MSDK_ALIGN16(vih2.dwPictAspectRatioX) == frameInfo.CropW)
{
vih2.dwPictAspectRatioX = frameInfo.CropW;
}
// AR is not always contained in the header (it's optional for MFX decoder initialization though)
if (frameInfo.AspectRatioW * frameInfo.AspectRatioH == 0)
{
// Convert display aspect ratio (is in VIDEOINFOHEADER2) to pixel aspect ratio (is accepted by MediaSDK components)
mfxStatus sts = DARtoPAR(vih2.dwPictAspectRatioX, vih2.dwPictAspectRatioY,
frameInfo.CropW, frameInfo.CropH,
frameInfo.AspectRatioW, frameInfo.AspectRatioH);
if (MSDK_FAILED(sts))
{
frameInfo.AspectRatioW = frameInfo.AspectRatioH = 1;
}
}
}
HRESULT CQuickSync::Decode(IMediaSample* pSample)
{
// DEBUG
#ifdef _DEBUG
static bool isSetThreadName = false;
if (!isSetThreadName)
{
isSetThreadName = true;
SetThreadName("*** Decoder ***");
}
#endif
// Input samples should be discarded - we are in the middle of a flush:
// between BeginFlush and EndFlush events.
if (m_bFlushing)
return S_FALSE;
CQsAutoLock cObjectLock(&m_csLock);
MSDK_VTRACE("QsDecoder: Decode\n");
// We haven't flushed since the last BeginFlush call - probably a DVD playback scenario
// where NewSegment/OnSeek isn't issued.
if (m_bNeedToFlush)
{
if (FAILED(OnSeek(0)))
return E_FAIL;
}
MSDK_CHECK_NOT_EQUAL(m_OK, true, E_UNEXPECTED);
HRESULT hr = S_OK;
mfxStatus sts = MFX_ERR_NONE;
mfxFrameSurface1* pSurfaceOut = NULL;
mfxBitstream mfxBS;
MSDK_ZERO_VAR(mfxBS);
// Check error(s)
if (NULL == pSample)
{
return S_OK;
}
if (0 == pSample->GetActualDataLength())
{
return S_FALSE;
}
// Manipulate bitstream for decoder
sts = m_pFrameConstructor->ConstructFrame(pSample, &mfxBS);
ASSERT(mfxBS.DataLength <= mfxBS.MaxLength);
MSDK_CHECK_ERROR(sts, MFX_ERR_MORE_DATA, S_OK); // not an error
MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, E_FAIL);
bool flushed = false;
// Decode mfxBitstream until all data is taken by decoder
while (mfxBS.DataLength > 0 && !m_bNeedToFlush)
{
// Decode the bitstream
sts = m_pDecoder->Decode(&mfxBS, pSurfaceOut);
if (MSDK_SUCCEEDED(sts))
{
// Sync decode - pSurfaceOut holds decoded surface
if (NULL != pSurfaceOut)
{
// Queue the frame for processing
ProcessDecodedFrame(pSurfaceOut);
}
continue;
}
else if (MFX_ERR_MORE_DATA == sts)
{
break; // Need to receive more data from upstream filter
}
else if (MFX_WRN_VIDEO_PARAM_CHANGED == sts)
{
MSDK_TRACE("QsDecoder: Decode MFX_WRN_VIDEO_PARAM_CHANGED\n");
// Need to handle a possible change in the stream parameters
if (!flushed)
{
sts = OnVideoParamsChanged();
flushed = true;
}
continue; // Just continue processing
}
// Need another work surface
else if (MFX_ERR_MORE_SURFACE == sts)
{
// Just continue, new work surface will be found in m_pDecoder->Decode
continue;
}
else if (MFX_ERR_NOT_ENOUGH_BUFFER == sts)
{
MSDK_TRACE("QsDecoder: Error - ran out of work buffers!\n");
// This is a system malfunction!
break;
}
else if (MFX_ERR_INCOMPATIBLE_VIDEO_PARAM == sts)
{
MSDK_TRACE("QsDecoder: Decode MFX_ERR_INCOMPATIBLE_VIDEO_PARAM\n");
// Flush existing frames
Flush(true);
// Destroy VPP object
MSDK_SAFE_DELETE(m_pVPP);
// Retrieve new parameters
mfxVideoParam VideoParams;
MSDK_ZERO_VAR(VideoParams);
VideoParams.mfx.CodecId = m_DecVideoParams.mfx.CodecId;
sts = m_pDecoder->DecodeHeader(&mfxBS, &VideoParams);
if (MFX_ERR_MORE_DATA == sts)
{
break;
}
// Save IOPattern and update parameters
VideoParams.IOPattern = m_DecVideoParams.IOPattern;
memcpy(&m_DecVideoParams, &VideoParams, sizeof(mfxVideoParam));
m_nPitch = MSDK_ALIGN32(m_DecVideoParams.mfx.FrameInfo.Width);
sts = m_pDecoder->Reset(&m_DecVideoParams, m_nPitch);
if (MSDK_SUCCEEDED(sts))
{
continue;
}
MSDK_TRACE("QsDecoder: Decode didn't recover from MFX_ERR_INCOMPATIBLE_VIDEO_PARAM\n");
}
else
{
MSDK_TRACE("QsDecoder: Device failed!\n");
}
// The decoder has returned with an error
hr = E_FAIL;
break;
}
if (SUCCEEDED(hr) && !m_bNeedToFlush)
m_pFrameConstructor->SaveResidualData(&mfxBS);
MSDK_SAFE_DELETE_ARRAY(mfxBS.Data);
return hr;
}
void CQuickSync::DeliverSurface(mfxFrameSurface1* pSurface, int duplicates)
{
MSDK_VTRACE("QsDecoder: DeliverSurface\n");
MSDK_CHECK_POINTER_NO_RET(pSurface);
duplicates = min(1, duplicates);
QsFrameData& outFrameData = *m_ProcessedFrame.first;
CQsAlignedBuffer*& pOutBuffer = m_ProcessedFrame.second;
// Clear the outFrameData
MSDK_ZERO_VAR(outFrameData);
outFrameData.bCorrupted = pSurface->Data.Corrupted != 0;
UpdateAspectRatio(pSurface, outFrameData);
outFrameData.fourCC = pSurface->Info.FourCC;
// Setup interlacing info
PicStructToDsFlags(pSurface->Info.PicStruct, outFrameData.dwInterlaceFlags, outFrameData.frameStructure);
outFrameData.bFilm = 0 != (outFrameData.dwInterlaceFlags & AM_VIDEO_FLAG_REPEAT_FIELD);
// Time stamp
outFrameData.rtStart = m_TimeManager.ConvertMFXTime2ReferenceTime(pSurface->Data.TimeStamp);
outFrameData.rtStop = (outFrameData.rtStart == INVALID_REFTIME) ? INVALID_REFTIME : (outFrameData.rtStart + 1);
// TODO: find actual frame type I/P/B
// Media sample isn't reliable as it referes to an older frame!
outFrameData.frameType = QsFrameData::I;
// Obtain surface data and copy it to temp buffer.
mfxFrameData frameData;
m_pDecoder->LockFrame(pSurface, &frameData);
size_t height = pSurface->Info.CropH; // Cropped image height
// Fill image size
outFrameData.rcFull.top = outFrameData.rcFull.left = 0;
outFrameData.rcFull.bottom = (LONG)height - 1;
outFrameData.rcFull.right = MSDK_ALIGN16(pSurface->Info.CropW + pSurface->Info.CropX) - 1;
outFrameData.rcClip.top = 0;
outFrameData.rcClip.bottom = (LONG)height - 1; // Height is not padded in output buffer
// Note that we always crop the height
outFrameData.rcClip.left = pSurface->Info.CropX;
outFrameData.rcClip.right = pSurface->Info.CropW + pSurface->Info.CropX - 1;
outFrameData.dwStride = frameData.Pitch;
if (m_bNeedToFlush) return;
if (m_Config.bDropDuplicateFrames) duplicates = 1;
for (int i = 0; i < duplicates && !m_bNeedToFlush; ++i)
{
// Fix time stamps for duplicated frames
if (outFrameData.rtStart != INVALID_REFTIME && pSurface->Info.FrameRateExtN > 0)
{
outFrameData.rtStart += (REFERENCE_TIME)(0.5 + (1e7 * i) * (double)pSurface->Info.FrameRateExtD / (double)pSurface->Info.FrameRateExtN);
outFrameData.rtStop = outFrameData.rtStart + 1;
}
switch (m_SurfaceType)
{
case QS_SURFACE_GPU:
CopyFramePointers(pSurface, outFrameData, frameData);
break;
case QS_SURFACE_SYSTEM:
default:
// Copy to output surface and write metadata
CopyFrame(pSurface, outFrameData, pOutBuffer, frameData);
}
if (!m_bNeedToFlush)
{
// Send the surface out - return code from dshow filter is ignored.
MSDK_VTRACE("QsDecoder: DeliverSurfaceCallback (%I64d)\n", outFrameData.rtStart);
m_DeliverSurfaceCallback(m_ObjParent, &outFrameData);
}
}
// Unlock the frame
m_pDecoder->UnlockFrame(pSurface, &frameData);
}
void CQuickSync::PicStructToDsFlags(mfxU32 picStruct, DWORD& flags, QsFrameData::QsFrameStructure& frameStructure)
{
// Note: MSDK will never output fields
frameStructure = (picStruct & MFX_PICSTRUCT_PROGRESSIVE) ?
QsFrameData::fsProgressiveFrame :
QsFrameData::fsInterlacedFrame;
if (m_TimeManager.GetInverseTelecine())
{
flags = AM_VIDEO_FLAG_WEAVE;
return;
}
// Progressive frame - note that sometimes interlaced content has the MFX_PICSTRUCT_PROGRESSIVE in combination with other flags
// Frame doubling/tripling implies progressive source
if (picStruct == MFX_PICSTRUCT_PROGRESSIVE || picStruct & MFX_PICSTRUCT_FRAME_DOUBLING || picStruct & MFX_PICSTRUCT_FRAME_TRIPLING)
{
flags = AM_VIDEO_FLAG_WEAVE;
return;
}
// Interlaced
flags = 0;
// Top field first
if (picStruct & MFX_PICSTRUCT_FIELD_TFF)
{
flags |= AM_VIDEO_FLAG_FIELD1FIRST;
}
// frame is progressive.
if (picStruct & MFX_PICSTRUCT_PROGRESSIVE)
{
flags |= AM_VIDEO_FLAG_WEAVE;
}
// Telecine flag
if (picStruct & MFX_PICSTRUCT_FIELD_REPEATED)
{
flags |= AM_VIDEO_FLAG_REPEAT_FIELD;
}
}
mfxStatus CQuickSync::ConvertFrameRate(mfxF64 dFrameRate, mfxU32& nFrameRateExtN, mfxU32& nFrameRateExtD)
{
mfxU32 fr = (mfxU32)(dFrameRate + .5);
if (fabs(fr - dFrameRate) < 0.0001)
{
nFrameRateExtN = fr;
nFrameRateExtD = (nFrameRateExtN) ? 1 : 0;
return MFX_ERR_NONE;
}
fr = (mfxU32)(dFrameRate * 1.001 + .5);
if (fabs(fr * 1000 - dFrameRate * 1001) < 10)
{
nFrameRateExtN = fr * 1000;
nFrameRateExtD = 1001;
return MFX_ERR_NONE;
}
nFrameRateExtN = (mfxU32)(dFrameRate * 10000 + .5);
nFrameRateExtD = 10000;
return MFX_ERR_NONE;
}
HRESULT CQuickSync::Flush(bool deliverFrames)
{
MSDK_TRACE("QsDecoder: Flush\n");
CQsAutoLock cObjectLock(&m_csLock);
HRESULT hr = S_OK;
mfxStatus sts = MFX_ERR_NONE;
// Recieved a BeginFlush that wasn't handled for some reason.
// This overrides the request to output the remaining frames
m_bNeedToFlush = m_bNeedToFlush || !deliverFrames;
// Flush internal queue
FlushOutputQueue();
// Flush HW decoder by sending NULL bitstreams.
while (MSDK_SUCCEEDED(sts) || MFX_ERR_MORE_SURFACE == sts)
{
mfxFrameSurface1* pSurf = NULL;
sts = m_pDecoder->Decode(NULL, pSurf);
if (MSDK_SUCCEEDED(sts) && !m_bNeedToFlush)
{
ProcessDecodedFrame(pSurf);
}
}
// Flush internal queue again
FlushOutputQueue();
// If VPP is active, we may need to flush it
FlushVPP();
m_TimeManager.Reset();
// All data has been flushed
m_bNeedToFlush = false;
MSDK_TRACE("QsDecoder: Flush ended\n");
return hr;
}
void CQuickSync::FlushOutputQueue()
{
MSDK_TRACE("QsDecoder: FlushOutputQueue (deliverFrames=%s)\n", (!m_bNeedToFlush) ? "TRUE" : "FALSE");
// Note that m_bNeedToFlush can be changed by another thread at any time
// Make sure worker thread has completed all it's tasks
// Empty the decoded output queue
for (size_t i = m_pDecoder->OutputQueueSize(); i > 0; --i)
{
ProcessDecodedFrame(NULL);
}
// Clear the decoded output queue - either failure in flushing or no need to deliver the frames.
for (size_t i = m_pDecoder->OutputQueueSize(); i > 0; --i)
{
// Surface is not needed anymore
m_pDecoder->UnlockSurface(PopSurface());
}
ASSERT(m_pDecoder->OutputQueueEmpty());
}
HRESULT CQuickSync::OnSeek(REFERENCE_TIME /* segmentStart */)
{
MSDK_TRACE("QsDecoder: OnSeek\n");
MSDK_CHECK_POINTER(m_pDecoder, E_UNEXPECTED);
m_bNeedToFlush = true;
CQsAutoLock cObjectLock(&m_csLock);
mfxStatus sts = MFX_ERR_NONE;
m_nSegmentFrameCount = 0;
m_PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
// Make sure the worker thread is idle and released all resources
FlushOutputQueue();
m_TimeManager.Reset();
if (m_pVPP)
{
// If VPP is active, we may need to flush it
FlushVPP();
MSDK_SAFE_DELETE(m_pVPP);
}
sts = m_pDecoder->Reset(&m_DecVideoParams, m_nPitch);
if (MSDK_FAILED(sts))
{
MSDK_TRACE("QsDecoder: reset failed!\n");
return E_FAIL;
}
m_pFrameConstructor->Reset();
FlushOutputQueue();
m_bNeedToFlush = false;
MSDK_TRACE("QsDecoder: OnSeek complete\n");
return (MSDK_SUCCEEDED(sts)) ? S_OK : E_FAIL;
}
bool CQuickSync::SetTimeStamp(mfxFrameSurface1* pSurface, REFERENCE_TIME& rtStart)
{
if (!m_TimeManager.Enabled()) {