-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_encode.cpp
1606 lines (1312 loc) · 50.5 KB
/
pipeline_encode.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
/*********************************************************************************
INTEL CORPORATION PROPRIETARY INFORMATION
This software is supplied under the terms of a license agreement or nondisclosure
agreement with Intel Corporation and may not be copied or disclosed except in
accordance with the terms of that agreement
Copyright(c) 2005-2014 Intel Corporation. All Rights Reserved.
**********************************************************************************/
#include "pipeline_encode.h"
#include "sysmem_allocator.h"
#include "global.h"
#if D3D_SURFACES_SUPPORT
#include "d3d_allocator.h"
#include "d3d11_allocator.h"
#include "d3d_device.h"
#include "d3d11_device.h"
#endif
#ifdef LIBVA_SUPPORT
#include "vaapi_allocator.h"
#include "vaapi_device.h"
#endif
FILE *fpout_v;
FILE *fp_yuv;
static void WipeMfxBitstream(mfxBitstream* pBitstream)
{
MSDK_CHECK_POINTER(pBitstream);
//free allocated memory
MSDK_SAFE_DELETE_ARRAY(pBitstream->Data);
}
CEncTaskPool::CEncTaskPool()
{
m_pTasks = NULL;
m_pMfxSession = NULL;
m_nTaskBufferStart = 0;
m_nPoolSize = 0;
}
CEncTaskPool::~CEncTaskPool()
{
Close();
}
mfxStatus CEncTaskPool::Init(MFXVideoSession* pmfxSession, outudppool* pLoopListBuffer,
mfxU32 nPoolSize, mfxU32 nBufferSize, PSAMPLE pSample )
{
MSDK_CHECK_POINTER(pmfxSession, MFX_ERR_NULL_PTR);
MSDK_CHECK_POINTER(pLoopListBuffer, MFX_ERR_NULL_PTR);
MSDK_CHECK_ERROR(nPoolSize, 0, MFX_ERR_UNDEFINED_BEHAVIOR);
MSDK_CHECK_ERROR(nBufferSize, 0, MFX_ERR_UNDEFINED_BEHAVIOR);
MSDK_CHECK_POINTER(pSample, MFX_ERR_NULL_PTR);
m_pMfxSession = pmfxSession;
m_nPoolSize = nPoolSize;
m_pTasks = new sTask [m_nPoolSize];
MSDK_CHECK_POINTER(m_pTasks, MFX_ERR_MEMORY_ALLOC);
mfxStatus sts = MFX_ERR_NONE;
for ( mfxU32 i = 0; i < m_nPoolSize; i++)
{
sts = m_pTasks[i].Init( nBufferSize, pLoopListBuffer, pSample );
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
}
fpout_v = fopen("transcodeV.264","wb+");
fp_yuv = fopen("tempV.yuv","ab+");
return MFX_ERR_NONE;
}
mfxStatus CEncTaskPool::SynchronizeFirstTask()
{
MSDK_CHECK_POINTER(m_pTasks, MFX_ERR_NOT_INITIALIZED);
MSDK_CHECK_POINTER(m_pMfxSession, MFX_ERR_NOT_INITIALIZED);
mfxStatus sts = MFX_ERR_NONE;
// non-null sync point indicates that task is in execution
if (NULL != m_pTasks[m_nTaskBufferStart].EncSyncP)
{
sts = m_pMfxSession->SyncOperation(m_pTasks[m_nTaskBufferStart].EncSyncP, MSDK_WAIT_INTERVAL);
if (MFX_ERR_NONE == sts)
{
/* 写输出流 */
sts = m_pTasks[m_nTaskBufferStart].WriteBitstream();
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
sts = m_pTasks[m_nTaskBufferStart].Reset();
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
// move task buffer start to the next executing task
// the first transform frame to the right with non zero sync point
for (mfxU32 i = 0; i < m_nPoolSize; i++)
{
m_nTaskBufferStart = (m_nTaskBufferStart + 1) % m_nPoolSize;
if (NULL != m_pTasks[m_nTaskBufferStart].EncSyncP)
{
break;
}
}
}
else if (MFX_ERR_ABORTED == sts)
{
while (!m_pTasks[m_nTaskBufferStart].DependentVppTasks.empty())
{
// find out if the error occurred in a VPP task to perform recovery procedure if applicable
sts = m_pMfxSession->SyncOperation(*m_pTasks[m_nTaskBufferStart].DependentVppTasks.begin(), 0);
if (MFX_ERR_NONE == sts)
{
m_pTasks[m_nTaskBufferStart].DependentVppTasks.pop_front();
sts = MFX_ERR_ABORTED; // save the status of the encode task
continue; // go to next vpp task
}
else
{
break;
}
}
}
return sts;
}
else
{
return MFX_ERR_NOT_FOUND; // no tasks left in task buffer
}
}
mfxU32 CEncTaskPool::GetFreeTaskIndex()
{
mfxU32 off = 0;
if (m_pTasks)
{
for (off = 0; off < m_nPoolSize; off++)
{
if (NULL == m_pTasks[(m_nTaskBufferStart + off) % m_nPoolSize].EncSyncP)
{
break;
}
}
}
if (off >= m_nPoolSize)
return m_nPoolSize;
return (m_nTaskBufferStart + off) % m_nPoolSize;
}
mfxStatus CEncTaskPool::GetFreeTask(sTask **ppTask)
{
MSDK_CHECK_POINTER(ppTask, MFX_ERR_NULL_PTR);
MSDK_CHECK_POINTER(m_pTasks, MFX_ERR_NOT_INITIALIZED);
mfxU32 index = GetFreeTaskIndex();
if (index >= m_nPoolSize)
{
return MFX_ERR_NOT_FOUND;
}
// return the address of the task
*ppTask = &m_pTasks[index];
return MFX_ERR_NONE;
}
void CEncTaskPool::Close()
{
if (m_pTasks)
{
for (mfxU32 i = 0; i < m_nPoolSize; i++)
{
m_pTasks[i].Close();
}
}
MSDK_SAFE_DELETE_ARRAY(m_pTasks);
m_pMfxSession = NULL;
m_nTaskBufferStart = 0;
m_nPoolSize = 0;
}
sTask::sTask()
: EncSyncP(0)
, m_pLoopListBuffer( NULL )
, m_pSample( NULL )
{
MSDK_ZERO_MEMORY(mfxBS);
}
mfxStatus sTask::Init( mfxU32 nBufferSize, outudppool* pLoopListBuffer, PSAMPLE pSample )
{
Close();
m_pLoopListBuffer = pLoopListBuffer;
m_pSample = pSample;
mfxStatus sts = Reset();
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
sts = InitMfxBitstream(&mfxBS, nBufferSize);
MSDK_CHECK_RESULT_SAFE(sts, MFX_ERR_NONE, sts, WipeMfxBitstream(&mfxBS));
return sts;
}
mfxStatus sTask::Close()
{
WipeMfxBitstream(&mfxBS);
EncSyncP = 0;
DependentVppTasks.clear();
return MFX_ERR_NONE;
}
mfxStatus sTask::WriteBitstream()
{
if ( !m_pLoopListBuffer || !m_pSample )
return MFX_ERR_NOT_INITIALIZED;
// write buffer
memcpy( &(m_pSample->abySample[0]), mfxBS.Data + mfxBS.DataOffset, mfxBS.DataLength );
m_pSample->lSampleLength = mfxBS.DataLength;
m_pSample->lTimeStamp = mfxBS.TimeStamp;
m_pSample->lDecodeTimeStamp = mfxBS.DecodeTimeStamp;
// if(m_pLoopListBuffer->fpVideo)
fwrite(m_pSample->abySample,m_pSample->lSampleLength,1,fpout_v);
//写输出
// m_pLoopListBuffer->Write( m_pSample, bVIDEO);
// mark that we don't need bit stream data any more
mfxBS.DataLength = 0;
return MFX_ERR_NONE;
}
mfxStatus sTask::Reset()
{
// mark sync point as free
EncSyncP = NULL;
// prepare bit stream
mfxBS.DataOffset = 0;
mfxBS.DataLength = 0;
mfxBS.TimeStamp = 0;
DependentVppTasks.clear();
return MFX_ERR_NONE;
}
mfxStatus sTask::InitMfxBitstream(mfxBitstream* pBitstream, mfxU32 nSize)
{
//check input params
MSDK_CHECK_POINTER(pBitstream, MFX_ERR_NULL_PTR);
MSDK_CHECK_ERROR(nSize, 0, MFX_ERR_NOT_INITIALIZED);
//prepare pBitstream
WipeMfxBitstream(pBitstream);
//prepare buffer
pBitstream->Data = new mfxU8[nSize];
MSDK_CHECK_POINTER(pBitstream->Data, MFX_ERR_MEMORY_ALLOC);
pBitstream->MaxLength = nSize;
return MFX_ERR_NONE;
}
mfxStatus CEncodingPipeline::AllocAndInitMVCSeqDesc()
{
// a simple example of mfxExtMVCSeqDesc structure filling
// actually equal to the "Default dependency mode" - when the structure fields are left 0,
// but we show how to properly allocate and fill the fields
mfxU32 i;
// mfxMVCViewDependency array
m_MVCSeqDesc.NumView = m_nNumView;
m_MVCSeqDesc.NumViewAlloc = m_nNumView;
m_MVCSeqDesc.View = new mfxMVCViewDependency[m_MVCSeqDesc.NumViewAlloc];
MSDK_CHECK_POINTER(m_MVCSeqDesc.View, MFX_ERR_MEMORY_ALLOC);
for (i = 0; i < m_MVCSeqDesc.NumViewAlloc; ++i)
{
MSDK_ZERO_MEMORY(m_MVCSeqDesc.View[i]);
m_MVCSeqDesc.View[i].ViewId = (mfxU16) i; // set view number as view id
}
// set up dependency for second view
m_MVCSeqDesc.View[1].NumAnchorRefsL0 = 1;
m_MVCSeqDesc.View[1].AnchorRefL0[0] = 0; // ViewId 0 - base view
m_MVCSeqDesc.View[1].NumNonAnchorRefsL0 = 1;
m_MVCSeqDesc.View[1].NonAnchorRefL0[0] = 0; // ViewId 0 - base view
// viewId array
m_MVCSeqDesc.NumViewId = m_nNumView;
m_MVCSeqDesc.NumViewIdAlloc = m_nNumView;
m_MVCSeqDesc.ViewId = new mfxU16[m_MVCSeqDesc.NumViewIdAlloc];
MSDK_CHECK_POINTER(m_MVCSeqDesc.ViewId, MFX_ERR_MEMORY_ALLOC);
for (i = 0; i < m_MVCSeqDesc.NumViewIdAlloc; ++i)
{
m_MVCSeqDesc.ViewId[i] = (mfxU16) i;
}
// create a single operation point containing all views
m_MVCSeqDesc.NumOP = 1;
m_MVCSeqDesc.NumOPAlloc = 1;
m_MVCSeqDesc.OP = new mfxMVCOperationPoint[m_MVCSeqDesc.NumOPAlloc];
MSDK_CHECK_POINTER(m_MVCSeqDesc.OP, MFX_ERR_MEMORY_ALLOC);
for (i = 0; i < m_MVCSeqDesc.NumOPAlloc; ++i)
{
MSDK_ZERO_MEMORY(m_MVCSeqDesc.OP[i]);
m_MVCSeqDesc.OP[i].NumViews = (mfxU16) m_nNumView;
m_MVCSeqDesc.OP[i].NumTargetViews = (mfxU16) m_nNumView;
m_MVCSeqDesc.OP[i].TargetViewId = m_MVCSeqDesc.ViewId; // points to mfxExtMVCSeqDesc::ViewId
}
return MFX_ERR_NONE;
}
mfxStatus CEncodingPipeline::AllocAndInitVppDoNotUse()
{
m_VppDoNotUse.NumAlg = 4;
m_VppDoNotUse.AlgList = new mfxU32 [m_VppDoNotUse.NumAlg];
MSDK_CHECK_POINTER(m_VppDoNotUse.AlgList, MFX_ERR_MEMORY_ALLOC);
m_VppDoNotUse.AlgList[0] = MFX_EXTBUFF_VPP_DENOISE; // turn off denoising (on by default)
m_VppDoNotUse.AlgList[1] = MFX_EXTBUFF_VPP_SCENE_ANALYSIS; // turn off scene analysis (on by default)
m_VppDoNotUse.AlgList[2] = MFX_EXTBUFF_VPP_DETAIL; // turn off detail enhancement (on by default)
m_VppDoNotUse.AlgList[3] = MFX_EXTBUFF_VPP_PROCAMP; // turn off processing amplified (on by default)
return MFX_ERR_NONE;
} // CEncodingPipeline::AllocAndInitVppDoNotUse()
void CEncodingPipeline::FreeMVCSeqDesc()
{
MSDK_SAFE_DELETE_ARRAY(m_MVCSeqDesc.View);
MSDK_SAFE_DELETE_ARRAY(m_MVCSeqDesc.ViewId);
MSDK_SAFE_DELETE_ARRAY(m_MVCSeqDesc.OP);
}
void CEncodingPipeline::FreeVppDoNotUse()
{
MSDK_SAFE_DELETE_ARRAY(m_VppDoNotUse.AlgList);
}
#include <QDebug>
mfxStatus CEncodingPipeline::InitMfxEncParams(sParams *pInParams)
{
m_MfxEncParams.mfx.CodecLevel = pInParams->nCodecLevel;
m_MfxEncParams.mfx.CodecProfile = pInParams->nCodecProfile;
m_MfxEncParams.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;// pInParams->nPicStruct;
m_MfxEncParams.mfx.RateControlMethod = pInParams->nRateControlMethod;
m_MfxEncParams.mfx.GopPicSize = pInParams->nGopPicSize;
m_MfxEncParams.mfx.GopRefDist = pInParams->nGopRefDist;
// //transcode add
// m_MfxEncParams.mfx.GopOptFlag = MFX_GOP_STRICT;
// m_MfxEncParams.mfx.RateControlMethod = MFX_RATECONTROL_CQP;
m_MfxEncParams.mfx.IdrInterval = 0;
if ( m_MfxEncParams.mfx.RateControlMethod == MFX_RATECONTROL_CBR )
{
m_MfxEncParams.mfx.BufferSizeInKB = pInParams->BufferSizeInKB;
m_MfxEncParams.mfx.MaxKbps = pInParams->nBitRate;
m_MfxEncParams.mfx.TargetKbps = pInParams->nBitRate;
}
else if( m_MfxEncParams.mfx.RateControlMethod == MFX_RATECONTROL_AVBR )
{
m_MfxEncParams.mfx.BufferSizeInKB = 3*pInParams->nBitRate;
m_MfxEncParams.mfx.TargetKbps = pInParams->nBitRate;
m_MfxEncParams.mfx.Accuracy = 300;
m_MfxEncParams.mfx.Convergence = 2500;
}
else if( m_MfxEncParams.mfx.RateControlMethod == MFX_RATECONTROL_ICQ ||
m_MfxEncParams.mfx.RateControlMethod == MFX_RATECONTROL_LA_ICQ )
{
//m_MfxEncParams.mfx.BufferSizeInKB = pInParams->BufferSizeInKB;
//m_MfxEncParams.mfx.MaxKbps = pInParams->nBitRate;
if( pInParams->nICQQuality > 51 )
pInParams->nICQQuality = 51;
else if( pInParams->nICQQuality <= 0 )
pInParams->nICQQuality = 1;
m_MfxEncParams.mfx.ICQQuality = pInParams->nICQQuality;
}
else if ( m_MfxEncParams.mfx.RateControlMethod == MFX_RATECONTROL_CQP )
{
m_MfxEncParams.mfx.QPI = pInParams->nQPI;
m_MfxEncParams.mfx.QPP = pInParams->nQPP+5;
m_MfxEncParams.mfx.QPB = pInParams->nQPB+12;
}
else if( m_MfxEncParams.mfx.RateControlMethod == MFX_RATECONTROL_LA )
{
m_MfxEncParams.mfx.TargetKbps = pInParams->nBitRate;
}
else
{
// for other BitRate
m_MfxEncParams.mfx.BufferSizeInKB = pInParams->BufferSizeInKB;
m_MfxEncParams.mfx.MaxKbps = pInParams->nBitRate;
m_MfxEncParams.mfx.TargetKbps = pInParams->nBitRate;
}
m_MfxEncParams.mfx.NumSlice = pInParams->nNumSlice;
m_MfxEncParams.mfx.TargetUsage = pInParams->nTargetUsage; // trade-off between quality and speed
m_MfxEncParams.mfx.GopOptFlag = pInParams->nGopOptFlag;
m_MfxEncParams.mfx.CodecId = pInParams->CodecId;
ConvertFrameRate(pInParams->dFrameRate, &m_MfxEncParams.mfx.FrameInfo.FrameRateExtN, &m_MfxEncParams.mfx.FrameInfo.FrameRateExtD);
m_MfxEncParams.mfx.EncodedOrder = 0; // binary flag, 0 signals encoder to take frames in display order
// specify memory type
m_MfxEncParams.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
// frame info parameters
m_MfxEncParams.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
// m_MfxEncParams.mfx.FrameInfo.FourCC = MFX_FOURCC_YV12;
m_MfxEncParams.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
// set frame size and crops
// width must be a multiple of 16
// height must be a multiple of 16 in case of frame picture and a multiple of 32 in case of field picture
m_MfxEncParams.mfx.FrameInfo.Width = MSDK_ALIGN16(pInParams->nDstWidth);
m_MfxEncParams.mfx.FrameInfo.Height = (MFX_PICSTRUCT_PROGRESSIVE == m_MfxEncParams.mfx.FrameInfo.PicStruct)?
MSDK_ALIGN16(pInParams->nDstHeight) : MSDK_ALIGN32(pInParams->nDstHeight);
m_MfxEncParams.mfx.FrameInfo.CropX = 0;
m_MfxEncParams.mfx.FrameInfo.CropY = 0;
m_MfxEncParams.mfx.FrameInfo.CropW = pInParams->nDstWidth;
m_MfxEncParams.mfx.FrameInfo.CropH = pInParams->nDstHeight;
// we don't specify profile and level and let the encoder choose those basing on parameters
// we must specify profile only for MVC codec
if (MVC_ENABLED & m_MVCflags)
{
m_MfxEncParams.mfx.CodecProfile = MFX_PROFILE_AVC_STEREO_HIGH;
}
// configure and attach external parameters
if (MVC_ENABLED & pInParams->MVC_flags)
m_EncExtParams.push_back((mfxExtBuffer *)&m_MVCSeqDesc);
if (MVC_VIEWOUTPUT & pInParams->MVC_flags)
{
// ViewOuput option requested
m_CodingOption.ViewOutput = MFX_CODINGOPTION_ON;
m_EncExtParams.push_back((mfxExtBuffer *)&m_CodingOption);
}
// configure the depth of the look ahead BRC if specified in command line
if (pInParams->nLADepth || pInParams->nMaxSliceSize)
{
// MSDK_ZERO_MEMORY(m_ExtLAControl);
// m_ExtLAControl.Header.BufferId = MFX_EXTBUFF_LOOKAHEAD_CTRL;
// m_ExtLAControl.Header.BufferSz = sizeof(m_ExtLAControl);
// m_ExtLAControl.LookAheadDepth = pInParams->nLADepth;
// m_ExtLAControl.DependencyDepth = 40;
// m_EncExtParams.push_back((mfxExtBuffer *)&m_ExtLAControl);
m_CodingOption2.LookAheadDepth = pInParams->nLADepth;
//m_CodingOption2.MaxSliceSize = 1;//pInParams->nMaxSliceSize;
m_EncExtParams.push_back((mfxExtBuffer *)&m_CodingOption2);
// m_CodingOption3.WinBRCMaxAvgKbps = 1200;
// m_CodingOption3.WinBRCSize = 50;
// m_EncExtParams.push_back((mfxExtBuffer *)&m_CodingOption3);
}
if (!m_EncExtParams.empty())
{
m_MfxEncParams.ExtParam = &m_EncExtParams[0]; // vector is stored linearly in memory
m_MfxEncParams.NumExtParam = (mfxU16)m_EncExtParams.size();
}
m_MfxEncParams.AsyncDepth = 1;//pInParams->nAsyncDepth;
return MFX_ERR_NONE;
}
mfxStatus CEncodingPipeline::InitMfxVppParams(sParams *pInParams)
{
MSDK_CHECK_POINTER(pInParams, MFX_ERR_NULL_PTR);
// specify memory type
m_mfxVppParams.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY | MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
// input frame info
m_mfxVppParams.vpp.In.FourCC = MFX_FOURCC_NV12;
// m_mfxVppParams.vpp.In.FourCC = MFX_FOURCC_YV12;
m_mfxVppParams.vpp.In.PicStruct = pInParams->nPicStruct;
ConvertFrameRate(pInParams->dFrameRate, &m_mfxVppParams.vpp.In.FrameRateExtN, &m_mfxVppParams.vpp.In.FrameRateExtD);
// width must be a multiple of 16
// height must be a multiple of 16 in case of frame picture and a multiple of 32 in case of field picture
m_mfxVppParams.vpp.In.Width = MSDK_ALIGN16(pInParams->nWidth);
m_mfxVppParams.vpp.In.Height = (MFX_PICSTRUCT_PROGRESSIVE == m_mfxVppParams.vpp.In.PicStruct)?
MSDK_ALIGN16(pInParams->nHeight) : MSDK_ALIGN32(pInParams->nHeight);
// set crops in input mfxFrameInfo for correct work of file reader
// VPP itself ignores crops at initialization
m_mfxVppParams.vpp.In.CropW = pInParams->nWidth;
m_mfxVppParams.vpp.In.CropH = pInParams->nHeight;
// fill output frame info
MSDK_MEMCPY_VAR(m_mfxVppParams.vpp.Out,&m_mfxVppParams.vpp.In, sizeof(mfxFrameInfo));
// only resizing is supported
m_mfxVppParams.vpp.Out.Width = MSDK_ALIGN16(pInParams->nDstWidth);
m_mfxVppParams.vpp.Out.Height = (MFX_PICSTRUCT_PROGRESSIVE == m_mfxVppParams.vpp.Out.PicStruct)?
MSDK_ALIGN16(pInParams->nDstHeight) : MSDK_ALIGN32(pInParams->nDstHeight);
// configure and attach external parameters
AllocAndInitVppDoNotUse();
m_VppExtParams.push_back((mfxExtBuffer *)&m_VppDoNotUse);
if (MVC_ENABLED & pInParams->MVC_flags)
m_VppExtParams.push_back((mfxExtBuffer *)&m_MVCSeqDesc);
m_mfxVppParams.ExtParam = &m_VppExtParams[0]; // vector is stored linearly in memory
m_mfxVppParams.NumExtParam = (mfxU16)m_VppExtParams.size();
m_mfxVppParams.AsyncDepth = pInParams->nAsyncDepth;
return MFX_ERR_NONE;
}
mfxStatus CEncodingPipeline::CreateHWDevice()
{
mfxStatus sts = MFX_ERR_NONE;
m_pHwDev = CreateVAAPIDevice();
if ( NULL == m_pHwDev )
return MFX_ERR_MEMORY_ALLOC;
sts = m_pHwDev->Init( NULL, 0, GetNumber(m_mfxSession) );
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
return MFX_ERR_NONE;
}
bool CEncodingPipeline::GetBuffer( PSAMPLE pSample )
{
if( NULL == m_pLoopListBuffer )
return false;
return m_pLoopListBuffer->Get( pSample );
}
int CEncodingPipeline::GetSampleCount()
{
if( m_pLoopListBuffer )
return m_pLoopListBuffer->GetSampleCount();
return 0;
}
bool CEncodingPipeline::GetTimeStamp(unsigned long &lTimeStamp)
{
bool bRet = false;
if( NULL == m_pLoopListBuffer )
return bRet;
PSAMPLE pSample = (PSAMPLE)new BYTE[8+4096*1024];
if( NULL == pSample )
return bRet;
if( m_pLoopListBuffer->Get( pSample, false ) )
{
lTimeStamp = pSample->lTimeStamp;
bRet = true;
}
delete pSample;
pSample = NULL;
return bRet;
}
int CEncodingPipeline::GetBitRate()
{
if( NULL == m_pMfxENC )
return 0;
mfxEncodeStat mfxStat;
mfxStatus sts = m_pMfxENC->GetEncodeStat( &mfxStat );
if( MFX_ERR_NONE == sts )
{
if( mfxStat.NumFrame )
return (mfxStat.NumBit/mfxStat.NumFrame*m_MfxEncParams.mfx.FrameInfo.FrameRateExtN)/1000;
}
return 0;
}
void CEncodingPipeline::ClearVideoBuffer()
{
if( m_pLoopListBuffer )
m_pLoopListBuffer->ClearBuffer();
}
void CEncodingPipeline::Quit()
{
m_bExitApplication = true;
}
void CEncodingPipeline::StopEncoder( bool bStop )
{
m_bStopEncoder = bStop;
}
mfxStatus CEncodingPipeline::AllocFrames()
{
MSDK_CHECK_POINTER(m_pMfxENC, MFX_ERR_NOT_INITIALIZED);
mfxStatus sts = MFX_ERR_NONE;
mfxFrameAllocRequest EncRequest;
mfxFrameAllocRequest VppRequest[2];
mfxU16 nEncSurfNum = 0; // number of surfaces for encoder
mfxU16 nVppSurfNum = 0; // number of surfaces for vpp
MSDK_ZERO_MEMORY(EncRequest);
MSDK_ZERO_MEMORY(VppRequest[0]);
MSDK_ZERO_MEMORY(VppRequest[1]);
// Calculate the number of surfaces for components.
// QueryIOSurf functions tell how many surfaces are required to produce at least 1 output.
// To achieve better performance we provide extra surfaces.
// 1 extra surface at input allows to get 1 extra output.
sts = m_pMfxENC->QueryIOSurf(&m_MfxEncParams, &EncRequest);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
if (EncRequest.NumFrameSuggested < m_MfxEncParams.AsyncDepth)
return MFX_ERR_MEMORY_ALLOC;
// The number of surfaces shared by vpp output and encode input.
nEncSurfNum = EncRequest.NumFrameSuggested;
if (m_pMfxVPP)
{
// VppRequest[0] for input frames request, VppRequest[1] for output frames request
sts = m_pMfxVPP->QueryIOSurf(&m_mfxVppParams, VppRequest);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
// The number of surfaces for vpp input - so that vpp can work at async depth = m_nAsyncDepth
nVppSurfNum = VppRequest[0].NumFrameSuggested;
// If surfaces are shared by 2 components, c1 and c2. NumSurf = c1_out + c2_in - AsyncDepth + 1
nEncSurfNum += nVppSurfNum - m_MfxEncParams.AsyncDepth + 1;
}
// prepare allocation requests
EncRequest.NumFrameSuggested = EncRequest.NumFrameMin = nEncSurfNum;
MSDK_MEMCPY_VAR(EncRequest.Info, &(m_MfxEncParams.mfx.FrameInfo), sizeof(mfxFrameInfo));
if (m_pMfxVPP)
{
EncRequest.Type |= MFX_MEMTYPE_FROM_VPPOUT; // surfaces are shared between vpp output and encode input
}
// alloc frames for encoder
sts = m_pMFXAllocator->Alloc(m_pMFXAllocator->pthis, &EncRequest, &m_EncResponse);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
// alloc frames for vpp if vpp is enabled
if (m_pMfxVPP)
{
VppRequest[0].NumFrameSuggested = VppRequest[0].NumFrameMin = nVppSurfNum;
MSDK_MEMCPY_VAR(VppRequest[0].Info, &(m_mfxVppParams.mfx.FrameInfo), sizeof(mfxFrameInfo));
sts = m_pMFXAllocator->Alloc(m_pMFXAllocator->pthis, &(VppRequest[0]), &m_VppResponse);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
}
// prepare mfxFrameSurface1 array for encoder
m_pEncSurfaces = new mfxFrameSurface1 [m_EncResponse.NumFrameActual];
MSDK_CHECK_POINTER(m_pEncSurfaces, MFX_ERR_MEMORY_ALLOC);
for (int i = 0; i < m_EncResponse.NumFrameActual; i++)
{
memset(&(m_pEncSurfaces[i]), 0, sizeof(mfxFrameSurface1));
MSDK_MEMCPY_VAR(m_pEncSurfaces[i].Info, &(m_MfxEncParams.mfx.FrameInfo), sizeof(mfxFrameInfo));
if (m_bExternalAlloc)
{
m_pEncSurfaces[i].Data.MemId = m_EncResponse.mids[i];
}
else
{
// get YUV pointers
sts = m_pMFXAllocator->Lock(m_pMFXAllocator->pthis, m_EncResponse.mids[i], &(m_pEncSurfaces[i].Data));
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
}
}
// prepare mfxFrameSurface1 array for vpp if vpp is enabled
if (m_pMfxVPP)
{
m_pVppSurfaces = new mfxFrameSurface1 [m_VppResponse.NumFrameActual];
MSDK_CHECK_POINTER(m_pVppSurfaces, MFX_ERR_MEMORY_ALLOC);
for (int i = 0; i < m_VppResponse.NumFrameActual; i++)
{
MSDK_ZERO_MEMORY(m_pVppSurfaces[i]);
MSDK_MEMCPY_VAR(m_pVppSurfaces[i].Info, &(m_mfxVppParams.mfx.FrameInfo), sizeof(mfxFrameInfo));
if (m_bExternalAlloc)
{
m_pVppSurfaces[i].Data.MemId = m_VppResponse.mids[i];
}
else
{
sts = m_pMFXAllocator->Lock(m_pMFXAllocator->pthis, m_VppResponse.mids[i], &(m_pVppSurfaces[i].Data));
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
}
}
}
return MFX_ERR_NONE;
}
mfxStatus CEncodingPipeline::CreateAllocator()
{
mfxStatus sts = MFX_ERR_NONE;
//in case of system memory allocator we also have to pass MFX_HANDLE_VA_DISPLAY to HW library
mfxIMPL impl;
m_mfxSession.QueryIMPL( &impl );
if( MFX_IMPL_HARDWARE == MFX_IMPL_BASETYPE( impl ) )
{
sts = CreateHWDevice();
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
mfxHDL hdl = NULL;
sts = m_pHwDev->GetHandle( MFX_HANDLE_VA_DISPLAY, &hdl ) ;
// provide device manager to MediaSDK
sts = m_mfxSession.SetHandle( MFX_HANDLE_VA_DISPLAY, hdl );
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
}
// create system memory allocator
m_pMFXAllocator = new SysMemFrameAllocator;
MSDK_CHECK_POINTER(m_pMFXAllocator, MFX_ERR_MEMORY_ALLOC);
/* In case of system memory we demonstrate "no external allocator" usage model.
We don't call SetAllocator, Media SDK uses internal allocator.
We use system memory allocator simply as a memory manager for application*/
// initialize memory allocator
sts = m_pMFXAllocator->Init(m_pmfxAllocatorParams);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
return MFX_ERR_NONE;
}
void CEncodingPipeline::DeleteFrames()
{
// delete surfaces array
MSDK_SAFE_DELETE_ARRAY(m_pEncSurfaces);
MSDK_SAFE_DELETE_ARRAY(m_pVppSurfaces);
// delete frames
if (m_pMFXAllocator)
{
m_pMFXAllocator->Free(m_pMFXAllocator->pthis, &m_EncResponse);
m_pMFXAllocator->Free(m_pMFXAllocator->pthis, &m_VppResponse);
}
}
void CEncodingPipeline::DeleteHWDevice()
{
MSDK_SAFE_DELETE(m_pHwDev);
}
void CEncodingPipeline::DeleteAllocator()
{
// delete allocator
MSDK_SAFE_DELETE(m_pMFXAllocator);
MSDK_SAFE_DELETE(m_pmfxAllocatorParams);
DeleteHWDevice();
}
CEncodingPipeline::CEncodingPipeline()
{
memset(&m_EncodeCtrl, 0, sizeof(mfxEncodeCtrl));
m_pMfxENC = NULL;
m_pMfxVPP = NULL;
m_pMFXAllocator = NULL;
m_pmfxAllocatorParams = NULL;
m_bExternalAlloc = false;
m_pEncSurfaces = NULL;
m_pVppSurfaces = NULL;
m_MVCflags = MVC_DISABLED;
m_nNumView = 0;
m_pLoopListBuffer = NULL;
m_pSample = NULL;
m_pVd = NULL;
m_bSelfCaluBuffsizeInKB = false;
// MSDK_ZERO_MEMORY(m_ExtLAControl);
// m_ExtLAControl.Header.BufferId = MFX_EXTBUFF_LOOKAHEAD_CTRL;
// m_ExtLAControl.Header.BufferSz = sizeof(m_ExtLAControl);
MSDK_ZERO_MEMORY(m_MVCSeqDesc);
m_MVCSeqDesc.Header.BufferId = MFX_EXTBUFF_MVC_SEQ_DESC;
m_MVCSeqDesc.Header.BufferSz = sizeof(m_MVCSeqDesc);
MSDK_ZERO_MEMORY(m_VppDoNotUse);
m_VppDoNotUse.Header.BufferId = MFX_EXTBUFF_VPP_DONOTUSE;
m_VppDoNotUse.Header.BufferSz = sizeof(m_VppDoNotUse);
MSDK_ZERO_MEMORY(m_CodingOption);
m_CodingOption.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
m_CodingOption.Header.BufferSz = sizeof(m_CodingOption);
// wcl 2015.03.16 add
m_CodingOption.CAVLC = MFX_CODINGOPTION_OFF;
m_CodingOption.EndOfStream = MFX_CODINGOPTION_OFF;
MSDK_ZERO_MEMORY(m_CodingOption2);
m_CodingOption2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
m_CodingOption2.Header.BufferSz = sizeof(m_CodingOption2);
// wcl 2015.03.16
m_CodingOption2.MaxFrameSize = 5000000;
m_CodingOption2.RepeatPPS = MFX_CODINGOPTION_OFF;
m_CodingOption2.BitrateLimit = MFX_CODINGOPTION_OFF;
MSDK_ZERO_MEMORY(m_CodingOption3);
m_CodingOption3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
m_CodingOption3.Header.BufferSz = sizeof(m_CodingOption3);
#if D3D_SURFACES_SUPPORT
m_pHwDev = NULL;
#endif
MSDK_ZERO_MEMORY(m_MfxEncParams);
MSDK_ZERO_MEMORY(m_mfxVppParams);
MSDK_ZERO_MEMORY(m_EncResponse);
MSDK_ZERO_MEMORY(m_VppResponse);
m_bExitApplication = false;
m_bStopEncoder = false;
}
CEncodingPipeline::~CEncodingPipeline()
{
Close();
if( m_pLoopListBuffer )
{
delete m_pLoopListBuffer;
m_pLoopListBuffer = NULL;
}
if( m_pSample )
{
delete m_pSample;
m_pSample = NULL;
}
}
mfxStatus CEncodingPipeline::InitSaveBuffer( int nW, int nH )
{
mfxStatus sts = MFX_ERR_NONE;
if( NULL == m_pLoopListBuffer )
{
long lPitch = ((nW &~ 15) * 12+ 7) / 8;
long lHeight = (nH + 31) &~ 31;
m_pLoopListBuffer = g_pLoopListBuffer[m_deviceid];
//m_pLoopListBuffer = new CLoopListBuffer(lPitch*lHeight*2);
// if(m_deviceid == 0)
// {
// FILE *fpVideo = fopen("cctv0.264", "wa+");
// m_pLoopListBuffer->fpVideo = fpVideo;
// }
MSDK_CHECK_POINTER(m_pLoopListBuffer, MFX_ERR_MEMORY_ALLOC);
}
if( NULL == m_pSample )
{
m_pSample = (PSAMPLE)new BYTE[8+4096*1024];
MSDK_CHECK_POINTER(m_pSample, MFX_ERR_MEMORY_ALLOC);
}
return sts;
}
mfxStatus CEncodingPipeline::Init( sParams *pParams )
{
MSDK_CHECK_POINTER( pParams, MFX_ERR_NULL_PTR );
mfxStatus sts = MFX_ERR_NONE;
// char pFileName[20];
// sprintf( pFileName,"cctv%d.wav", pParams->ndeviceid );
// fpAudio = fopen(pFileName, "wa+");
// sprintf( pFileName,"cctv%d.264", pParams->ndeviceid );
// fpVideo = fopen(pFileName, "wa+");
m_pVd = pParams->vd;
m_MVCflags = pParams->MVC_flags;
// prepare save buffer
sts = InitSaveBuffer( pParams->nDstWidth, pParams->nDstHeight );
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
// we set version to 1.0 and later we will query actual version of the library which will got leaded
mfxVersion min_version;
min_version.Minor = 13;
min_version.Major = 1;
mfxIMPL impl = MFX_IMPL_HARDWARE_ANY;
sts = m_mfxSession.Init( impl, &min_version );
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
// create and init frame allocator
sts = CreateAllocator();
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
sts = InitMfxEncParams(pParams);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
sts = InitMfxVppParams(pParams);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
// MVC specific options
if (MVC_ENABLED & m_MVCflags)
{
sts = AllocAndInitMVCSeqDesc();
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
}
// create encoder
m_pMfxENC = new MFXVideoENCODE(m_mfxSession);
MSDK_CHECK_POINTER(m_pMfxENC, MFX_ERR_MEMORY_ALLOC);
// create preprocessor if resizing was requested from command line
// or if different FourCC is set in InitMfxVppParams
if (pParams->nWidth != pParams->nDstWidth ||
pParams->nHeight != pParams->nDstHeight ||
m_mfxVppParams.vpp.In.FourCC != m_mfxVppParams.vpp.Out.FourCC)
{
m_pMfxVPP = new MFXVideoVPP(m_mfxSession);
MSDK_CHECK_POINTER(m_pMfxVPP, MFX_ERR_MEMORY_ALLOC);
}
sts = ResetMFXComponents(pParams);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
return MFX_ERR_NONE;
}
void CEncodingPipeline::Close()
{
MSDK_SAFE_DELETE(m_pMfxENC);
MSDK_SAFE_DELETE(m_pMfxVPP);
FreeMVCSeqDesc();
FreeVppDoNotUse();
DeleteFrames();
m_pPlugin.reset();
m_TaskPool.Close();
m_mfxSession.Close();
if (m_pLoopListBuffer)
MSDK_SAFE_DELETE(m_pLoopListBuffer);
// allocator if used as external for MediaSDK must be deleted after SDK components
DeleteAllocator();
}
mfxStatus CEncodingPipeline::ResetMFXComponents(sParams* pParams)
{
MSDK_CHECK_POINTER(pParams, MFX_ERR_NULL_PTR);
MSDK_CHECK_POINTER(m_pMfxENC, MFX_ERR_NOT_INITIALIZED);
mfxStatus sts = MFX_ERR_NONE;
sts = m_pMfxENC->Close();
MSDK_IGNORE_MFX_STS(sts, MFX_ERR_NOT_INITIALIZED);
MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);