1 // Copyright (c) 2021 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #pragma once
22 
23 #include "mfx_brc_common.h"
24 #include "mfxla.h"
25 
26 namespace HEVCEHW
27 {
28 namespace Base
29 {
30 
31 
32 template <size_t N>
33 class Regression
34 {
35 public:
36     static const mfxU32 MAX_WINDOW = N;
37 
Regression()38     Regression() {}
Reset(mfxU32 size,mfxF64 initX,mfxF64 initY)39     void Reset(mfxU32 size, mfxF64 initX, mfxF64 initY) {
40         windowSize = size;
41         normX = initX;
42         std::fill_n(x, windowSize, initX);
43         std::fill_n(y, windowSize, initY);
44         sumxx = initX * initX * windowSize;
45         sumxy = initX * initY * windowSize;
46     }
Add(mfxF64 newx,mfxF64 newy)47     void Add(mfxF64 newx, mfxF64 newy) {
48         newy = newy / newx * normX;
49         newx = normX;
50         sumxy += newx * newy - x[0] * y[0];
51         sumxx += newx * newx - x[0] * x[0];
52         std::copy(x + 1, x + windowSize, x);
53         std::copy(y + 1, y + windowSize, y);
54         x[windowSize - 1] = newx;
55         y[windowSize - 1] = newy;
56     }
57 
GetCoeff()58     mfxF64 GetCoeff() const {
59         return sumxy / sumxx;
60     }
61 
62 //protected:
63 public: // temporary for debugging and dumping
64     mfxF64 x[N] = {};
65     mfxF64 y[N] = {};
66     mfxU32 windowSize = 0.0;
67     mfxF64 normX = 0.0;
68     mfxF64 sumxy = 0.0;
69     mfxF64 sumxx = 0.0;
70 };
71 
72 class LAExtBrc
73 {
74 public:
~LAExtBrc()75     virtual ~LAExtBrc() { Close(); }
76 
77     mfxStatus Init(mfxVideoParam &video, mfxI32 enableRecode = 1);
78 
Close()79     mfxStatus Close() { return MFX_ERR_NONE; }
80 
81     mfxI32 GetQP(mfxU32 encodedOrder);
82 
83     mfxU32 Report(mfxU32 frameType, mfxU32 dataLength, mfxU32 userDataLength, mfxU32 repack, mfxU32 picOrder, mfxU32 maxFrameSize, mfxU32 qp);
84 
85     mfxStatus SetFrameVMEData(const mfxExtLAFrameStatistics *, mfxU32 widthMB, mfxU32 heightMB);
86 
87 public:
88     struct LaFrameData
89     {
90         mfxU32  encOrder;
91         mfxU32  dispOrder;
92         mfxI32  poc;
93         mfxI32  deltaQp;
94         mfxF64  estRate[52];
95         mfxF64  estRateTotal[52];
96         mfxU32  interCost;
97         mfxU32  intraCost;
98         mfxU32  propCost;
99         mfxU32  bframe;
100         mfxI32  qp;
101         mfxU16   layer;
102         bool    bNotUsed;
103     };
104 
105 protected:
106     mfxU32  m_lookAheadDep;
107     mfxU32  m_totNumMb;
108     mfxF64  m_initTargetRate;
109     mfxF64  m_targetRateMin;
110     mfxF64  m_targetRateMax;
111     mfxU32  m_framesBehind;
112     mfxF64  m_bitsBehind;
113     mfxI32  m_curBaseQp;
114     mfxI32  m_curQp;
115     mfxU16  m_qpUpdateRange;
116 
117     std::list <LaFrameData> m_laData;
118     Regression<20>   m_rateCoeffHistory[52];
119     UMC::Mutex    m_mutex;
120 
121 };
122 
123 
124 }//namespaceBase
125 }//namespace HEVCEHW
126