-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathmfx_enctools_brc.cpp
More file actions
2562 lines (2253 loc) · 107 KB
/
Copy pathmfx_enctools_brc.cpp
File metadata and controls
2562 lines (2253 loc) · 107 KB
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) 2009-2020 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "mfx_enctools.h"
#include "mfx_enctools_brc.h"
#include <algorithm>
#include <math.h>
#define MAX_DQP_LTR 4
#define MAX_MODEL_ERR 6
#define BRC_BUFK(verylowdelay) (verylowdelay? 4.0 : 2.5)
#define SCH_BUFK(verylowdelay) (verylowdelay? 6.0 : 3.5)
#define LTR_BUFK(verylowdelay) (verylowdelay? 8.0: 4.5)
static mfxF64 LTR_BUF(mfxU16 type, mfxU32 dqp, bool boost, bool schg, bool shstrt, bool verylowdelay)
{
if (type == MFX_FRAMETYPE_IDR)
{
if ((schg && !boost) || !dqp)
{
return BRC_BUFK(verylowdelay);
}
else
{
return LTR_BUFK(verylowdelay);
}
}
else
{
if (shstrt)
{
return SCH_BUFK(verylowdelay);
}
else
{
return BRC_BUFK(verylowdelay);
}
}
}
#define DQFF0 1.0
#define DQFF1 1.66
#define DQF(type, dqp, boost, schg) \
((type == MFX_FRAMETYPE_IDR) ? ((dqp?pow(2, ((mfxF64)dqp / 6.0)) : 1.0) * ((schg && !boost) ? DQFF0 : DQFF1)) : 1.0)
#define FRM_RATIO(type, encorder, shstrt, pyr) \
((((encorder == 0 && !pyr) || type == MFX_FRAMETYPE_I) ? 6.0 : (shstrt || type == MFX_FRAMETYPE_IDR) ? 8.0 : 4.0) * ((pyr) ? 1.5 : 1.0))
#define BRC_CONST_MUL_P1 2.253264596
#define BRC_CONST_EXP_R_P1 0.42406423
#define ltrprintf(...)
//#define ltrprintf printf
#define BRC_SCENE_CHANGE_RATIO1 20.0
#define BRC_SCENE_CHANGE_RATIO2 5.0
#define BRC_QP_MODULATION_GOP8_FIXED MFX_QP_MODULATION_RESERVED0
namespace EncToolsBRC {
struct CompareByDisplayOrder
{
mfxU32 m_DispOrder;
CompareByDisplayOrder(mfxU32 frameOrder) : m_DispOrder(frameOrder) {}
bool operator ()(BRC_FrameStruct const & frameStruct) const { return frameStruct.dispOrder == m_DispOrder; }
};
static mfxU32 hevcBitRateScale(mfxU32 bitrate)
{
mfxU32 bit_rate_scale = 0;
while (bit_rate_scale < 16 && (bitrate & ((1 << (6 + bit_rate_scale + 1)) - 1)) == 0)
bit_rate_scale++;
return bit_rate_scale;
}
static mfxU32 hevcCbpSizeScale(mfxU32 cpbSize)
{
mfxU32 cpb_size_scale = 2;
while (cpb_size_scale < 16 && (cpbSize & ((1 << (4 + cpb_size_scale + 1)) - 1)) == 0)
cpb_size_scale++;
return cpb_size_scale;
}
const mfxU32 h264_h265_au_cpb_removal_delay_length_minus1 = 23;
const mfxU32 h264_bit_rate_scale = 4;
const mfxU32 h264_cpb_size_scale = 2;
mfxI32 GetRawFrameSize(mfxU32 lumaSize, mfxU16 chromaFormat, mfxU16 bitDepthLuma)
{
mfxI32 frameSize = lumaSize;
if (chromaFormat == MFX_CHROMAFORMAT_YUV420)
frameSize += lumaSize / 2;
else if (chromaFormat == MFX_CHROMAFORMAT_YUV422)
frameSize += lumaSize;
else if (chromaFormat == MFX_CHROMAFORMAT_YUV444)
frameSize += lumaSize * 2;
frameSize = frameSize * bitDepthLuma / 8;
return frameSize * 8; //frame size in bits
}
mfxStatus cBRCParams::Init(mfxEncToolsCtrl const & ctrl, bool bMBBRC, bool fieldMode)
{
MFX_CHECK(ctrl.RateControlMethod == MFX_RATECONTROL_CBR ||
ctrl.RateControlMethod == MFX_RATECONTROL_VBR,
MFX_ERR_UNDEFINED_BEHAVIOR);
bFieldMode= fieldMode;
codecId = ctrl.CodecId;
targetbps = ctrl.TargetKbps * 1000;
maxbps = ctrl.MaxKbps * 1000;
maxbps = (ctrl.RateControlMethod == MFX_RATECONTROL_CBR) ?
targetbps : ((maxbps >= targetbps) ? maxbps : targetbps);
mfxU32 bit_rate_scale = (ctrl.CodecId == MFX_CODEC_AVC) ?
h264_bit_rate_scale : hevcBitRateScale(maxbps);
mfxU32 cpb_size_scale = (ctrl.CodecId == MFX_CODEC_AVC) ?
h264_cpb_size_scale : hevcCbpSizeScale(maxbps);
rateControlMethod = ctrl.RateControlMethod;
maxbps = ((maxbps >> (6 + bit_rate_scale)) << (6 + bit_rate_scale));
HRDConformance = ctrl.HRDConformance;
if (HRDConformance != MFX_BRC_NO_HRD)
{
bufferSizeInBytes = ((ctrl.BufferSizeInKB * 1000) >> (cpb_size_scale + 1)) << (cpb_size_scale + 1);
initialDelayInBytes = ((ctrl.InitialDelayInKB * 1000) >> (cpb_size_scale + 1)) << (cpb_size_scale + 1);
bRec = 1;
bPanic = (HRDConformance == MFX_BRC_HRD_STRONG) ? 1 : 0;
}
else if(ctrl.MaxDelayInFrames > ctrl.MaxGopRefDist) {
bRec = 1; // Needed for LPLA
}
MFX_CHECK (ctrl.FrameInfo.FrameRateExtD != 0 &&
ctrl.FrameInfo.FrameRateExtN != 0,
MFX_ERR_UNDEFINED_BEHAVIOR);
frameRate = (mfxF64)ctrl.FrameInfo.FrameRateExtN / (mfxF64)ctrl.FrameInfo.FrameRateExtD;
width = ctrl.FrameInfo.Width;
height = ctrl.FrameInfo.Height;
cropWidth = ctrl.FrameInfo.CropW ? ctrl.FrameInfo.CropW : width;
cropHeight = ctrl.FrameInfo.CropH ? ctrl.FrameInfo.CropH : height;
chromaFormat = ctrl.FrameInfo.ChromaFormat == 0 ? MFX_CHROMAFORMAT_YUV420 : ctrl.FrameInfo.ChromaFormat ;
bitDepthLuma = ctrl.FrameInfo.BitDepthLuma == 0 ? 8 : ctrl.FrameInfo.BitDepthLuma;
mfxI32 bitdepthOffset = (6 * (bitDepthLuma - 8));
quantOffset = IsOn(ctrl.LowPower) ? 0 : bitdepthOffset;
inputBitsPerFrame = targetbps / frameRate;
maxInputBitsPerFrame = maxbps / frameRate;
gopPicSize = ctrl.MaxGopSize*(bFieldMode ? 2 : 1);
gopRefDist = ctrl.MaxGopRefDist*(bFieldMode ? 2 : 1);
bPyr = (ctrl.BRefType == MFX_B_REF_PYRAMID);
maxFrameSizeInBits = std::max(std::max (ctrl.MaxFrameSizeInBytes[0], ctrl.MaxFrameSizeInBytes[1]),
ctrl.MaxFrameSizeInBytes[2])*8 ;
if (gopRefDist <= 3) {
fAbPeriodShort = 6;
} else {
fAbPeriodShort = 16;
}
// P update, future deviation control, only for LA NO HRD
mfxU32 CodecId = ctrl.CodecId;
mfxI32 reaction_mult = (HRDConformance == MFX_BRC_NO_HRD
&& ctrl.MaxDelayInFrames > ctrl.MaxGopRefDist
&& (CodecId == MFX_CODEC_HEVC || CodecId == MFX_CODEC_AVC || CodecId == MFX_CODEC_AV1)) ?
(CodecId == MFX_CODEC_HEVC || CodecId == MFX_CODEC_AV1 ? 4 : 3) : 1;
fAbPeriodLong = 120 * reaction_mult;
dqAbPeriod = 120 * reaction_mult;
bAbPeriod = 120 * reaction_mult;
fAbPeriodLA = 16;
mRawFrameSizeInBits = GetRawFrameSize(width * height, chromaFormat, IsOn(ctrl.LowPower) ? 8 : bitDepthLuma);
mRawFrameSizeInPixs = mRawFrameSizeInBits / (IsOn(ctrl.LowPower) ? 8 : bitDepthLuma);
if (maxFrameSizeInBits)
{
bRec = 1;
bPanic = 1;
}
mfxI32 minQPI = IsOn(ctrl.LowPower) ? 10 : 1;
mfxI32 maxQPI = IsOn(ctrl.LowPower) ? 51 : (51 + bitdepthOffset);
mfxI32 minQPP = IsOn(ctrl.LowPower) ? 10 : 1;
mfxI32 maxQPP = IsOn(ctrl.LowPower) ? 51 : (51 + bitdepthOffset);
mfxI32 minQPB = IsOn(ctrl.LowPower) ? 10 : 1;
mfxI32 maxQPB = IsOn(ctrl.LowPower) ? 51 : (51 + bitdepthOffset);
mfxI32 minmaxOffset = IsOn(ctrl.LowPower) ? bitdepthOffset : 0;
quantMinI = (ctrl.MinQPLevel[0] != 0) ?
std::max((mfxI32)ctrl.MinQPLevel[0]-minmaxOffset, minQPI) : minQPI;
quantMaxI = (ctrl.MaxQPLevel[0] != 0) ?
std::min((mfxI32)ctrl.MaxQPLevel[0]-minmaxOffset, maxQPI) : maxQPI;
quantMinP = (ctrl.MinQPLevel[1] != 0) ?
std::max((mfxI32)ctrl.MinQPLevel[1]-minmaxOffset, minQPP) : minQPP;
quantMaxP = (ctrl.MaxQPLevel[1] != 0) ?
std::min((mfxI32)ctrl.MaxQPLevel[1]-minmaxOffset, maxQPP) : maxQPP;
quantMinB = (ctrl.MinQPLevel[2] != 0) ?
std::max((mfxI32)ctrl.MinQPLevel[2]-minmaxOffset, minQPB) : minQPB;
quantMaxB = (ctrl.MaxQPLevel[2] != 0) ?
std::min((mfxI32)ctrl.MaxQPLevel[2]-minmaxOffset, maxQPB) : maxQPB;
WinBRCMaxAvgKbps = ctrl.WinBRCMaxAvgKbps;
WinBRCSize = ctrl.WinBRCSize;
iDQp0 = 0;
mNumRefsInGop = (mfxU32)(std::max(1.0, (!bPyr ? (mfxF64)gopPicSize / (mfxF64)gopRefDist : (mfxF64)gopPicSize / 2.0)));
mfxF64 maxFrameRatio = 1.5874 * FRM_RATIO(MFX_FRAMETYPE_IDR, 0, 0, bPyr);
mIntraBoost = (mNumRefsInGop > maxFrameRatio * 8.0) ? 1 : 0;
mVeryLowDelay = (HRDConformance != MFX_BRC_NO_HRD && (bufferSizeInBytes * 8.0) / targetbps < 0.12) ? 1 : 0;
mfxF64 maxFrameSize = mRawFrameSizeInBits;
if (maxFrameSizeInBits) {
maxFrameSize = std::min<mfxF64>(maxFrameSize, maxFrameSizeInBits);
}
if (HRDConformance != MFX_BRC_NO_HRD) {
mfxF64 bufOccupy = LTR_BUF(MFX_FRAMETYPE_IDR, 1, mIntraBoost, 1, 0, mVeryLowDelay);
maxFrameSize = std::min(maxFrameSize, bufOccupy / 9.* (initialDelayInBytes * 8.0) + (9.0 - bufOccupy) / 9.*inputBitsPerFrame);
}
mfxF64 minFrameRatio = FRM_RATIO(MFX_FRAMETYPE_IDR, 0, 0, bPyr);
maxFrameRatio = std::min({maxFrameRatio, maxFrameSize / inputBitsPerFrame, mfxF64(mNumRefsInGop)});
mfxF64 dqp = std::max(0.0, 6.0 * (log(maxFrameRatio / minFrameRatio) / log(2.0)));
iDQp0 = (mfxU32)(dqp + 0.5);
if (iDQp0 < 1) iDQp0 = 1;
if (iDQp0 > MAX_DQP_LTR) iDQp0 = MAX_DQP_LTR;
// MaxFrameSize violation prevention
mMinQstepCmplxKP = BRC_CONST_MUL_P1;
mMinQstepRateEP = BRC_CONST_EXP_R_P1;
mMinQstepCmplxKPUpdt = 0;
mMinQstepCmplxKPUpdtErr = 0.16;
mLaQp = ctrl.LaQp;
mLaScale = ctrl.LaScale;
mLaDepth = ctrl.MaxDelayInFrames;
mHasALTR = (ctrl.CodecId == MFX_CODEC_AVC); // check if codec support ALTR
mMBBRC = (CodecId == MFX_CODEC_HEVC || CodecId == MFX_CODEC_AVC || CodecId == MFX_CODEC_AV1) && bMBBRC;
return MFX_ERR_NONE;
}
mfxStatus cBRCParams::GetBRCResetType(mfxEncToolsCtrl const & ctrl, bool bNewSequence, bool bMBBRC, bool &bBRCReset, bool &bSlidingWindowReset)
{
bBRCReset = false;
bSlidingWindowReset = false;
if (bNewSequence)
return MFX_ERR_NONE;
cBRCParams new_par;
mfxStatus sts = new_par.Init(ctrl, bMBBRC,false);
MFX_CHECK_STS(sts);
MFX_CHECK(new_par.rateControlMethod == rateControlMethod, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM) ;
MFX_CHECK(new_par.HRDConformance == HRDConformance, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM) ;
MFX_CHECK(new_par.frameRate == frameRate, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.width == width, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.height == height, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.chromaFormat == chromaFormat, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.bitDepthLuma == bitDepthLuma, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
if (HRDConformance == MFX_BRC_HRD_STRONG)
{
MFX_CHECK(new_par.bufferSizeInBytes == bufferSizeInBytes, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.initialDelayInBytes == initialDelayInBytes, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.targetbps == targetbps, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(new_par.maxbps == maxbps, MFX_ERR_INCOMPATIBLE_VIDEO_PARAM);
}
else if (new_par.targetbps != targetbps || new_par.maxbps != maxbps)
{
bBRCReset = true;
}
if (new_par.WinBRCMaxAvgKbps != WinBRCMaxAvgKbps)
{
bBRCReset = true;
bSlidingWindowReset = true;
}
if (new_par.maxFrameSizeInBits != maxFrameSizeInBits) bBRCReset = true;
if (new_par.gopPicSize != gopPicSize) bBRCReset = true;
if (new_par.gopRefDist != gopRefDist) bBRCReset = true;
if (new_par.bPyr != bPyr) bBRCReset = true;
if (new_par.quantMaxI != quantMaxI) bBRCReset = true;
if (new_par.quantMinI != quantMinI) bBRCReset = true;
if (new_par.quantMaxP != quantMaxP) bBRCReset = true;
if (new_par.quantMinP != quantMinP) bBRCReset = true;
if (new_par.quantMaxB != quantMaxB) bBRCReset = true;
if (new_par.quantMinB != quantMinB) bBRCReset = true;
return MFX_ERR_NONE;
}
enum
{
MFX_BRC_RECODE_NONE = 0,
MFX_BRC_RECODE_QP = 1,
MFX_BRC_RECODE_PANIC = 2,
};
mfxF64 const QSTEP[88] =
{
0.630, 0.707, 0.794, 0.891, 1.000, 1.122, 1.260, 1.414, 1.587, 1.782, 2.000, 2.245, 2.520,
2.828, 3.175, 3.564, 4.000, 4.490, 5.040, 5.657, 6.350, 7.127, 8.000, 8.980, 10.079, 11.314,
12.699, 14.254, 16.000, 17.959, 20.159, 22.627, 25.398, 28.509, 32.000, 35.919, 40.317, 45.255, 50.797,
57.018, 64.000, 71.838, 80.635, 90.510, 101.594, 114.035, 128.000, 143.675, 161.270, 181.019, 203.187, 228.070,
256.000, 287.350, 322.540, 362.039, 406.375, 456.140, 512.000, 574.701, 645.080, 724.077, 812.749, 912.280,
1024.000, 1149.401, 1290.159, 1448.155, 1625.499, 1824.561, 2048.000, 2298.802, 2580.318, 2896.309, 3250.997, 3649.121,
4096.000, 4597.605, 5160.637, 5792.619, 6501.995, 7298.242, 8192.000, 9195.209, 10321.273, 11585.238, 13003.989, 14596.485
};
inline
mfxI32 QStep2QpFloor(mfxF64 qstep, mfxI32 qpoffset = 0) // QSTEP[qp] <= qstep, return 0<=qp<=51+mQuantOffset
{
mfxU8 qp = mfxU8(std::upper_bound(QSTEP, QSTEP + 51 + qpoffset, qstep) - QSTEP);
return qp > 0 ? qp - 1 : 0;
}
inline
mfxI32 Qstep2QP(mfxF64 qstep, mfxI32 qpoffset = 0) // return 0<=qp<=51+mQuantOffset
{
mfxI32 qp = QStep2QpFloor(qstep, qpoffset);
// prevent going QSTEP index out of bounds
if (qp >= (mfxI32)(sizeof(QSTEP)/sizeof(QSTEP[0])) - 1)
return 0;
return (qp == 51 + qpoffset || qstep < (QSTEP[qp] + QSTEP[qp + 1]) / 2) ? qp : qp + 1;
}
inline
mfxF64 QP2Qstep(mfxI32 qp, mfxI32 qpoffset = 0)
{
return QSTEP[std::min(51 + qpoffset, qp)];
}
inline
mfxU16 CheckHrdAndUpdateQP(HRDCodecSpec &hrd, mfxU32 frameSizeInBits, mfxU32 eo, bool bIdr, mfxI32 currQP)
{
if (frameSizeInBits > hrd.GetMaxFrameSizeInBits(eo, bIdr))
{
hrd.SetUnderflowQuant(currQP);
return MFX_BRC_BIG_FRAME;
}
else if (frameSizeInBits < hrd.GetMinFrameSizeInBits(eo, bIdr))
{
hrd.SetOverflowQuant(currQP);
return MFX_BRC_SMALL_FRAME;
}
return MFX_BRC_OK;
}
inline
mfxI32 GetFrameTargetSize(mfxU32 brcSts, mfxI32 minFrameSize, mfxI32 maxFrameSize)
{
if (brcSts != MFX_BRC_BIG_FRAME && brcSts != MFX_BRC_SMALL_FRAME) return 0;
return (brcSts == MFX_BRC_BIG_FRAME) ? maxFrameSize * 3 / 4 : minFrameSize * 5 / 4;
}
static
mfxI32 GetNewQP(mfxF64 totalFrameBits, mfxF64 targetFrameSizeInBits, mfxI32 minQP , mfxI32 maxQP, mfxI32 qp , mfxI32 qp_offset, mfxF64 f_pow, bool bStrict = false, bool bLim = true)
{
mfxF64 qstep = 0, qstep_new = 0;
mfxI32 qp_new = qp;
qstep = QP2Qstep(qp, qp_offset);
qstep_new = qstep * pow(totalFrameBits / targetFrameSizeInBits, f_pow);
qp_new = Qstep2QP(qstep_new, qp_offset);
if (totalFrameBits < targetFrameSizeInBits) // overflow
{
if (qp <= minQP)
{
return qp; // QP change is impossible
}
if (bLim)
qp_new = std::max(qp_new, (minQP + qp + 1) >> 1);
if (bStrict)
qp_new = std::min(qp_new, qp - 1);
}
else // underflow
{
if (qp >= maxQP)
{
return qp; // QP change is impossible
}
if (bLim)
qp_new = std::min(qp_new, (maxQP + qp + 1) >> 1);
if (bStrict)
qp_new = std::max(qp_new, qp + 1);
}
return mfx::clamp(qp_new, minQP, maxQP);
}
// Get QP Offset for given frame and Adaptive Pyramid QP class
// level = Pyramid level or Layer, value [1-3]
// isRef = zero for non-reference frame
// qpMod = Adaptive Pyramid QP class, value [0-4]
// QP Offset is realtive QuantB.
// QuantB = QuantP+1
// qpMod=0, can be for used non 8GOP and/or non Pyramid cases.
static
mfxI32 GetOffsetAPQ(mfxI32 level, mfxU16 isRef, mfxU16 qpMod, mfxU32 codecId)
{
mfxI32 qp = 0;
level = std::max(mfxI32(1), std::min(mfxI32(3), level));
if (codecId == MFX_CODEC_HEVC || codecId == MFX_CODEC_AV1) {
if (qpMod == MFX_QP_MODULATION_HIGH || qpMod == BRC_QP_MODULATION_GOP8_FIXED) {
switch (level) {
case 3:
qp += (codecId == MFX_CODEC_HEVC) ? 1 : 2;
case 2:
qp += (codecId == MFX_CODEC_HEVC) ? 2 : 1;
case 1:
default:
qp += (codecId == MFX_CODEC_HEVC) ? 2 : 2;
break;
}
}
else if (qpMod == MFX_QP_MODULATION_MIXED) {
switch (level) {
case 3:
qp += 2;
case 2:
qp += 1;
case 1:
default:
qp += 4;
break;
}
}
else if (qpMod == MFX_QP_MODULATION_MEDIUM) {
switch (level) {
case 3:
qp += 1;
case 2:
qp += 1;
case 1:
default:
qp += 2;
break;
}
}
else if (qpMod == MFX_QP_MODULATION_LOW) {
switch (level) {
case 3:
qp += 1;
case 2:
qp += 1;
case 1:
default:
qp += 1;
break;
}
}
else {
qp += (level > 0 ? level - 1 : 0);
if (level && !isRef) qp += 1;
}
}
else if (codecId == MFX_CODEC_AVC) {
if (qpMod == MFX_QP_MODULATION_HIGH) {
switch (level) {
case 3:
qp += 2;
case 2:
qp += 1;
case 1:
default:
qp += 3;
break;
}
}
else if (qpMod == MFX_QP_MODULATION_MEDIUM) {
switch (level) {
case 3:
qp += 2;
case 2:
qp += 1;
case 1:
default:
qp += 2;
break;
}
}
else if (qpMod == MFX_QP_MODULATION_LOW) {
switch (level) {
case 3:
qp += 1;
case 2:
qp += 1;
case 1:
default:
qp += 1;
break;
}
}
else {
qp += (level > 0 ? level - 1 : 0);
if (level && !isRef) qp += 1;
}
}
else {
qp += (level > 0 ? level - 1 : 0);
if (level && !isRef) qp += 1;
}
return qp;
}
static
void SetQPParams(mfxI32 qp, mfxU32 type, BRC_Ctx& ctx, mfxU32 /* rec_num */, mfxI32 minQuant, mfxI32 maxQuant, mfxU32 level, mfxU32 iDQp, mfxU16 isRef, mfxU16 qpMod, mfxI32 qpDeltaP, mfxU32 codecId)
{
if (type == MFX_FRAMETYPE_IDR)
{
ctx.QuantIDR = qp;
ctx.QuantI = qp + iDQp;
ctx.QuantP = qp + 1 + iDQp;
ctx.QuantB = qp + 2 + iDQp;
}
else if (type == MFX_FRAMETYPE_I)
{
ctx.QuantIDR = qp - iDQp;
ctx.QuantI = qp;
ctx.QuantP = qp + 1;
ctx.QuantB = qp + 2;
}
else if (type == MFX_FRAMETYPE_P)
{
qp -= level;
qp -= qpDeltaP;
ctx.QuantIDR = qp - 1 - iDQp;
ctx.QuantI = qp - 1;
ctx.QuantP = qp;
ctx.QuantB = qp + 1;
}
else if (type == MFX_FRAMETYPE_B)
{
qp -= GetOffsetAPQ(level, isRef, qpMod, codecId);
ctx.QuantIDR = qp - 2 - iDQp;
ctx.QuantI = qp - 2;
ctx.QuantP = qp - 1;
ctx.QuantB = qp;
}
ctx.QuantIDR = mfx::clamp(ctx.QuantIDR, minQuant, maxQuant);
ctx.QuantI = mfx::clamp(ctx.QuantI, minQuant, maxQuant);
ctx.QuantP = mfx::clamp(ctx.QuantP, minQuant, maxQuant);
ctx.QuantB = mfx::clamp(ctx.QuantB, minQuant, maxQuant);
//printf("ctx.QuantIDR %d, QuantI %d, ctx.QuantP %d, ctx.QuantB %d, level %d iDQp %d\n", ctx.QuantIDR, ctx.QuantI, ctx.QuantP, ctx.QuantB, level, iDQp);
}
inline
void UpdateQPParams(mfxI32 qp, mfxU32 type , BRC_Ctx &ctx, mfxU32 rec_num, mfxI32 minQuant, mfxI32 maxQuant, mfxU32 level, mfxU32 iDQp, mfxU16 isRef, mfxU16 qpMod, mfxI32 qpDeltaP, mfxU32 codecId)
{
ctx.Quant = qp;
if (ctx.LastIQpSetOrder > ctx.encOrder) return;
if (ctx.LastQpUpdateOrder > ctx.encOrder) return;
ctx.LastQpUpdateOrder = ctx.encOrder;
SetQPParams(qp, type, ctx, rec_num, minQuant, maxQuant, level, iDQp, isRef, qpMod, qpDeltaP, codecId);
}
inline
mfxU16 GetFrameType(mfxU16 m_frameType, mfxU16 level, mfxU16 gopRefDist, mfxU32 codecId)
{
if (m_frameType & MFX_FRAMETYPE_IDR)
return MFX_FRAMETYPE_IDR;
else if ((m_frameType & MFX_FRAMETYPE_I) && codecId == MFX_CODEC_HEVC)
return MFX_FRAMETYPE_IDR; // For CRA
else if (m_frameType & MFX_FRAMETYPE_I)
return MFX_FRAMETYPE_I;
else if (m_frameType & MFX_FRAMETYPE_P)
return MFX_FRAMETYPE_P;
else if ((m_frameType & MFX_FRAMETYPE_REF) && (level == 0 || gopRefDist == 1))
return MFX_FRAMETYPE_P; //low delay B
else
return MFX_FRAMETYPE_B;
}
inline
bool isFrameBeforeIntra(mfxU32 order, mfxU32 intraOrder, mfxU32 gopPicSize, mfxU32 gopRefDist)
{
mfxI32 distance0 = gopPicSize * 3 / 4;
mfxI32 distance1 = gopPicSize - gopRefDist * 3;
return (order - intraOrder) > (mfxU32)(std::max(distance0, distance1));
}
static
mfxStatus SetRecodeParams(mfxU16 brcStatus, mfxI32 qp, mfxI32 qp_new, mfxI32 minQP, mfxI32 maxQP, BRC_Ctx &ctx, mfxBRCFrameStatus* status)
{
ctx.bToRecode = 1;
if (brcStatus == MFX_BRC_BIG_FRAME || brcStatus == MFX_BRC_PANIC_BIG_FRAME )
{
MFX_CHECK(qp_new >= qp, MFX_ERR_UNDEFINED_BEHAVIOR);
ctx.Quant = qp_new;
ctx.QuantMax = maxQP;
if (brcStatus == MFX_BRC_BIG_FRAME && qp_new > qp)
{
ctx.QuantMin = std::max(qp + 1, minQP); //limit QP range for recoding
status->BRCStatus = MFX_BRC_BIG_FRAME;
}
else
{
ctx.QuantMin = minQP;
ctx.bPanic = 1;
status->BRCStatus = MFX_BRC_PANIC_BIG_FRAME;
}
}
else if (brcStatus == MFX_BRC_SMALL_FRAME || brcStatus == MFX_BRC_PANIC_SMALL_FRAME)
{
MFX_CHECK(qp_new <= qp, MFX_ERR_UNDEFINED_BEHAVIOR);
ctx.Quant = qp_new;
ctx.QuantMin = minQP; //limit QP range for recoding
if (brcStatus == MFX_BRC_SMALL_FRAME && qp_new < qp)
{
ctx.QuantMax = std::min(qp - 1, maxQP);
status->BRCStatus = MFX_BRC_SMALL_FRAME;
}
else
{
ctx.QuantMax = maxQP;
status->BRCStatus = MFX_BRC_PANIC_SMALL_FRAME;
ctx.bPanic = 1;
}
}
//printf("recode %d, qp %d new %d, status %d\n", ctx.encOrder, qp, qp_new, status->BRCStatus);
return MFX_ERR_NONE;
}
static
mfxI32 GetNewQPTotal(mfxF64 bo, mfxF64 dQP, mfxI32 minQP , mfxI32 maxQP, mfxI32 qp, bool bPyr, bool bSC)
{
mfxU8 mode = (!bPyr) ;
bo = mfx::clamp(bo, -1.0, 1.0);
dQP = mfx::clamp(dQP, 1./maxQP, 1./minQP);
mfxF64 ndQP = dQP + (1. / maxQP - dQP) * bo;
ndQP = mfx::clamp(ndQP, 1. / maxQP, 1. / minQP);
mfxI32 quant_new = (mfxI32) (1. / ndQP + 0.5);
//printf(" GetNewQPTotal: bo %f, quant %d, quant_new %d, mode %d\n", bo, qp, quant_new, mode);
if (!bSC)
{
if (mode == 0) // low: qp_diff [-2; 2]
{
if (quant_new >= qp + 5)
quant_new = qp + 2;
else if (quant_new > qp + 1)
quant_new = qp + 1;
else if (quant_new <= qp - 5)
quant_new = qp - 2;
else if (quant_new < qp - 1)
quant_new = qp - 1;
}
else // (mode == 1) midle: qp_diff [-3; 3]
{
if (quant_new >= qp + 5)
quant_new = qp + 3;
else if (quant_new > qp + 2)
quant_new = qp + 2;
else if (quant_new <= qp - 5)
quant_new = qp - 3;
else if (quant_new < qp - 2)
quant_new = qp - 2;
}
}
else
{
quant_new = mfx::clamp(quant_new, qp - 5, qp + 5);
}
return mfx::clamp(quant_new, minQP, maxQP);
}
// Reduce AB period before intra and increase it after intra (to avoid intra frame affect on the bottom of hrd)
static
mfxF64 GetAbPeriodCoeff(mfxU32 numInGop, mfxU32 gopPicSize, mfxU32 SC)
{
const mfxU32 maxForCorrection = 30;
mfxF64 maxValue = (SC) ? 1.3 : 1.5;
const mfxF64 minValue = 1.0;
mfxU32 numForCorrection = std::min(gopPicSize /2, maxForCorrection);
mfxF64 k[maxForCorrection] = {0};
if (numInGop >= gopPicSize || gopPicSize < 2)
return 1.0;
for (mfxU32 i = 0; i < numForCorrection; i ++)
{
k[i] = maxValue - (maxValue - minValue)*i/numForCorrection;
}
if (numInGop < gopPicSize/2)
{
return k [numInGop < numForCorrection ? numInGop : numForCorrection - 1];
}
else
{
mfxU32 n = gopPicSize - 1 - numInGop;
return 1.0/ k[n < numForCorrection ? n : numForCorrection - 1];
}
}
static
void ResetMinQForMaxFrameSize(cBRCParams* par, mfxU32 type)
{
if (type == MFX_FRAMETYPE_IDR || type == MFX_FRAMETYPE_I || type == MFX_FRAMETYPE_P) {
par->mMinQstepCmplxKPUpdt = 0;
par->mMinQstepCmplxKPUpdtErr = 0.16;
par->mMinQstepCmplxKP = BRC_CONST_MUL_P1;
par->mMinQstepRateEP = BRC_CONST_EXP_R_P1;
}
}
static
mfxI32 GetMinQForMaxFrameSize(const cBRCParams& par, mfxF64 targetBits, mfxU32 type)
{
mfxI32 qp = 0;
if (type == MFX_FRAMETYPE_P) {
if (par.mMinQstepCmplxKPUpdt > 2 && par.mMinQstepCmplxKPUpdtErr < 0.69) {
mfxI32 rawSize = par.mRawFrameSizeInPixs;
mfxF64 BitsDesiredFrame = targetBits * (1.0 - 0.165 - std::min(0.115, par.mMinQstepCmplxKPUpdtErr/3.0));
mfxF64 R = (mfxF64)rawSize / BitsDesiredFrame;
mfxF64 QstepScale = pow(R, par.mMinQstepRateEP) * par.mMinQstepCmplxKP;
QstepScale = std::min(128.0, QstepScale);
mfxF64 minqp = 6.0*log(QstepScale) / log(2.0) + 12.0;
minqp = std::max(0.0, minqp);
qp = (mfxU32)(minqp + 0.5);
qp = mfx::clamp(qp, 1, 51);
}
}
return qp;
}
static
void UpdateMinQForMaxFrameSize(cBRCParams* par, mfxI32 bits, mfxI32 qp, const BRC_Ctx& ctx, mfxU32 type, bool shstrt, mfxU16 brcSts)
{
if (type == MFX_FRAMETYPE_I || type == MFX_FRAMETYPE_IDR) {
mfxI32 rawSize = par->mRawFrameSizeInPixs;
mfxF64 R = (mfxF64)rawSize / (mfxF64)bits;
mfxF64 QstepScaleComputed = pow(R, par->mMinQstepRateEP) * par->mMinQstepCmplxKP;
mfxF64 QstepScaleReal = pow(2.0, ((mfxF64)qp - 12.0) / 6.0);
if (QstepScaleComputed > QstepScaleReal) {
// Next P Frame atleast as complex as I Frame
mfxF64 dS = log(QstepScaleReal) - log(QstepScaleComputed);
par->mMinQstepCmplxKPUpdtErr = std::max<mfxF64>((par->mMinQstepCmplxKPUpdtErr + abs(dS)) / 2, abs(dS));
mfxF64 upDlt = 0.5;
dS = mfx::clamp(dS, -0.5, 1.0);
par->mMinQstepCmplxKP = par->mMinQstepCmplxKP*(1.0 + upDlt*dS);
//par->mMinQstepCmplxKPUpdt++;
par->mMinQstepRateEP = mfx::clamp(par->mMinQstepRateEP + mfx::clamp(0.01 * (log(QstepScaleReal) - log(QstepScaleComputed))*log(R), -0.1, 0.2), 0.125, 1.0);
// Sanity Check / Force
if (qp < 50) {
mfxF64 rateQstepNew = pow(R, par->mMinQstepRateEP);
mfxF64 QstepScaleUpdtComputed = rateQstepNew * par->mMinQstepCmplxKP;
mfxI32 qp_now = (mfxI32)(6.0*log(QstepScaleUpdtComputed) / log(2.0) + 12.0);
if (qp < qp_now -1) {
qp_now = qp + 2;
QstepScaleUpdtComputed = pow(2.0, ((mfxF64)qp_now - 12.0) / 6.0);
par->mMinQstepCmplxKP = QstepScaleUpdtComputed / rateQstepNew;
par->mMinQstepCmplxKPUpdtErr = 0.16;
}
}
}
} else if (type == MFX_FRAMETYPE_P) {
if (ctx.LastIQpSetOrder < ctx.encOrder) {
mfxI32 rawSize = par->mRawFrameSizeInPixs;
mfxF64 R = (mfxF64)rawSize / (mfxF64)bits;
mfxF64 QstepScaleComputed = pow(R, par->mMinQstepRateEP) * par->mMinQstepCmplxKP;
mfxF64 QstepScaleReal = pow(2.0, ((mfxF64)qp - 12.0) / 6.0);
mfxF64 dS = log(QstepScaleReal) - log(QstepScaleComputed);
par->mMinQstepCmplxKPUpdtErr = std::max<mfxF64>((par->mMinQstepCmplxKPUpdtErr + abs(dS)) / 2, abs(dS));
mfxF64 upDlt = mfx::clamp(1.3042 * pow(R, -0.922), 0.025, 0.5);
if (shstrt || par->mMinQstepCmplxKPUpdt <= 2 || par->mMinQstepCmplxKPUpdtErr > 0.69) upDlt = 0.5;
else if (brcSts != MFX_BRC_OK || par->mMinQstepCmplxKPUpdtErr > 0.41) upDlt = std::max(0.125, upDlt);
dS = mfx::clamp(dS, -0.5, 1.0);
par->mMinQstepCmplxKP = par->mMinQstepCmplxKP*(1.0 + upDlt*dS);
par->mMinQstepCmplxKPUpdt++;
par->mMinQstepRateEP = mfx::clamp(par->mMinQstepRateEP + mfx::clamp(0.01 * (log(QstepScaleReal) - log(QstepScaleComputed))*log(R), -0.1, 0.2), 0.125, 1.0);
}
}
}
mfxI32 BRC_EncToolBase::GetCurQP(mfxU32 type, mfxI32 layer, mfxU16 isRef, mfxU16 qpMod, mfxI32 qpDeltaP) const
{
mfxI32 qp = 0;
if (type == MFX_FRAMETYPE_IDR)
{
qp = m_ctx.QuantIDR;
qp = mfx::clamp(qp, m_par.quantMinI, m_par.quantMaxI);
}
else if (type == MFX_FRAMETYPE_I)
{
qp = m_ctx.QuantI;
qp = mfx::clamp(qp, m_par.quantMinI, m_par.quantMaxI);
}
else if (type == MFX_FRAMETYPE_P)
{
qp = m_ctx.QuantP + layer;
qp += qpDeltaP;
qp = mfx::clamp(qp, m_par.quantMinP, m_par.quantMaxP);
}
else
{
qp = m_ctx.QuantB;
qp += GetOffsetAPQ(layer, isRef, qpMod, m_par.codecId);
qp = mfx::clamp(qp, m_par.quantMinB, m_par.quantMaxB);
}
//printf("GetCurQP IDR %d I %d P %d B %d, min %d max %d type %d \n", m_ctx.QuantIDR, m_ctx.QuantI, m_ctx.QuantP, m_ctx.QuantB, m_par.quantMinI, m_par.quantMaxI, type);
return qp;
}
mfxF64 BRC_EncToolBase::ResetQuantAb(mfxI32 qp, mfxU32 type, mfxI32 layer, mfxU16 isRef, mfxF64 fAbLong, mfxU32 eo, bool bIdr, mfxU16 qpMod, mfxI32 qpDeltaP, bool bNoNewQp) const
{
mfxI32 seqQP_new = GetSeqQP(qp, type, layer, isRef, qpMod, qpDeltaP);
mfxF64 dQuantAb_new = 1.0 / seqQP_new;
mfxF64 bAbPreriod = m_par.bAbPeriod;
mfxF64 totDev = m_ctx.totalDeviation;
mfxF64 HRDDevFactor = 0.0;
mfxF64 maxFrameSizeHrd = 0.0;
mfxF64 HRDDev = 0.0;
if (m_par.HRDConformance != MFX_BRC_NO_HRD)
{
HRDDevFactor = m_hrdSpec->GetBufferDeviationFactor(eo);
HRDDev = m_hrdSpec->GetBufferDeviation(eo);
maxFrameSizeHrd = m_hrdSpec->GetMaxFrameSizeInBits(eo, bIdr);
}
mfxF64 lf = 1.0 / pow(m_par.inputBitsPerFrame / fAbLong, 1.0 + HRDDevFactor);
if (m_par.HRDConformance != MFX_BRC_NO_HRD && totDev > 0)
{
if (m_par.rateControlMethod == MFX_RATECONTROL_VBR)
{
totDev = std::max(totDev, HRDDev);
}
bAbPreriod = (mfxF64)(m_par.bPyr ? 4 : 3)*(mfxF64)maxFrameSizeHrd / m_par.inputBitsPerFrame*GetAbPeriodCoeff(m_ctx.encOrder - m_ctx.LastIDREncOrder, m_par.gopPicSize, m_ctx.LastIDRSceneChange);
bAbPreriod = mfx::clamp(bAbPreriod, m_par.bAbPeriod / 10, m_par.bAbPeriod);
}
if (!bNoNewQp)
{
mfxI32 quant_new = GetNewQPTotal(totDev / bAbPreriod / m_par.inputBitsPerFrame,
dQuantAb_new, m_ctx.QuantMin, m_ctx.QuantMax,
seqQP_new, m_par.bPyr && m_par.bRec, false);
seqQP_new += (seqQP_new - quant_new);
}
mfxF64 dQuantAb = lf * (1.0 / seqQP_new);
return dQuantAb;
}
mfxI32 BRC_EncToolBase::GetSeqQP(mfxI32 qp, mfxU32 type, mfxI32 layer, mfxU16 isRef, mfxU16 qpMod, mfxI32 qpDeltaP) const
{
mfxI32 pqp = 0;
if (type == MFX_FRAMETYPE_IDR) {
pqp = qp + m_par.iDQp + 1;
} else if (type == MFX_FRAMETYPE_I) {
pqp = qp + 1;
} else if (type == MFX_FRAMETYPE_P) {
pqp = qp - layer - qpDeltaP;
} else {
qp -= GetOffsetAPQ(layer, isRef, qpMod, m_par.codecId);
pqp = qp - 1;
}
pqp = mfx::clamp(pqp, m_par.quantMinP, m_par.quantMaxP);
return pqp;
}
mfxI32 BRC_EncToolBase::GetPicQP(mfxI32 pqp, mfxU32 type, mfxI32 layer, mfxU16 isRef, mfxU16 qpMod, mfxI32 qpDeltaP) const
{
mfxI32 qp = 0;
if (type == MFX_FRAMETYPE_IDR)
{
qp = pqp - 1 - m_par.iDQp;
qp = mfx::clamp(qp, m_par.quantMinI, m_par.quantMaxI);
}
else if (type == MFX_FRAMETYPE_I)
{
qp = pqp - 1;
qp = mfx::clamp(qp, m_par.quantMinI, m_par.quantMaxI);
}
else if (type == MFX_FRAMETYPE_P)
{
qp =pqp + layer;
qp += qpDeltaP;
qp = mfx::clamp(qp, m_par.quantMinP, m_par.quantMaxP);
}
else
{
qp = pqp + 1;
qp += GetOffsetAPQ(layer, isRef, qpMod, m_par.codecId);
qp = mfx::clamp(qp, m_par.quantMinB, m_par.quantMaxB);
}
return qp;
}
mfxStatus BRC_EncToolBase::Init(mfxEncToolsCtrl const & ctrl, bool bMBBRC)
{
MFX_CHECK(!m_bInit, MFX_ERR_UNDEFINED_BEHAVIOR);
mfxStatus sts = MFX_ERR_NONE;
sts = m_par.Init(ctrl, bMBBRC, isFieldMode(ctrl));
MFX_CHECK_STS(sts);
if (m_par.HRDConformance != MFX_BRC_NO_HRD)
{
if (m_par.codecId == MFX_CODEC_AVC)
m_hrdSpec.reset(new H264_HRD());
else
m_hrdSpec.reset(new HEVC_HRD());
m_hrdSpec->Init(m_par);
}
m_ctx = {};
m_ctx.fAbLong = m_par.inputBitsPerFrame;
m_ctx.fAbShort = m_par.inputBitsPerFrame;
m_ctx.fAbLA = m_par.inputBitsPerFrame;
mfxI32 rawSize = GetRawFrameSize(m_par.width * m_par.height, m_par.chromaFormat, m_par.quantOffset ? m_par.bitDepthLuma : 8);
mfxI32 qp = GetNewQP(rawSize, m_par.inputBitsPerFrame, m_par.quantMinI, m_par.quantMaxI, 1, m_par.quantOffset, 0.5, false, false);
UpdateQPParams(qp, MFX_FRAMETYPE_IDR, m_ctx, 0, m_par.quantMinI, m_par.quantMaxI, 0, m_par.iDQp, MFX_FRAMETYPE_REF, 0, 0, m_par.codecId);
m_ctx.dQuantAb = qp > 0 ? 1.0 / qp : 1.0; //kw
if (m_par.WinBRCSize)
{
m_avg.reset(new AVGBitrate(m_par.WinBRCSize, (mfxU32)(m_par.WinBRCMaxAvgKbps*1000.0 / m_par.frameRate), (mfxU32)m_par.inputBitsPerFrame));
MFX_CHECK_NULL_PTR1(m_avg.get());
}
m_bInit = true;
return sts;
}
mfxStatus BRC_EncToolBase::Reset(mfxEncToolsCtrl const & ctrl, bool bMBBRC)
{
mfxStatus sts = MFX_ERR_NONE;
MFX_CHECK(m_bInit, MFX_ERR_NOT_INITIALIZED);
mfxExtEncoderResetOption * pRO = (mfxExtEncoderResetOption *)Et_GetExtBuffer(ctrl.ExtParam, ctrl.NumExtParam, MFX_EXTBUFF_ENCODER_RESET_OPTION);
if (pRO && pRO->StartNewSequence == MFX_CODINGOPTION_ON)
{
Close();
sts = Init(ctrl, bMBBRC);
}
else
{
bool brcReset = false;
bool slidingWindowReset = false;
sts = m_par.GetBRCResetType(ctrl, false, bMBBRC, brcReset, slidingWindowReset);
MFX_CHECK_STS(sts);
if (brcReset)
{
sts = m_par.Init(ctrl, bMBBRC, isFieldMode(ctrl));
MFX_CHECK_STS(sts);
m_ctx.Quant = (mfxI32)(1. / m_ctx.dQuantAb * pow(m_ctx.fAbLong / m_par.inputBitsPerFrame, 0.32) + 0.5);
m_ctx.Quant = mfx::clamp(m_ctx.Quant, m_par.quantMinI, m_par.quantMaxI);