1 /*****************************************************************************
2 * Copyright (C) 2013-2020 MulticoreWare, Inc
3 *
4 * Author: Steve Borho <steve@borho.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23 
24 #ifndef X265_FRAMEDATA_H
25 #define X265_FRAMEDATA_H
26 
27 #include "common.h"
28 #include "slice.h"
29 #include "cudata.h"
30 
31 namespace X265_NS {
32 // private namespace
33 
34 class PicYuv;
35 class JobProvider;
36 
37 #define INTER_MODES 4 // 2Nx2N, 2NxN, Nx2N, AMP modes
38 #define INTRA_MODES 3 // DC, Planar, Angular modes
39 
40 /* Current frame stats for 2 pass */
41 struct FrameStats
42 {
43     int         mvBits;    /* MV bits (MV+Ref+Block Type) */
44     int         coeffBits; /* Texture bits (DCT coefs) */
45     int         miscBits;
46 
47     int         intra8x8Cnt;
48     int         inter8x8Cnt;
49     int         skip8x8Cnt;
50 
51     /* CU type counts stored as percentage */
52     double      percent8x8Intra;
53     double      percent8x8Inter;
54     double      percent8x8Skip;
55     double      avgLumaDistortion;
56     double      avgChromaDistortion;
57     double      avgPsyEnergy;
58     double      avgSsimEnergy;
59     double      avgResEnergy;
60     double      percentIntraNxN;
61     double      percentSkipCu[NUM_CU_DEPTH];
62     double      percentMergeCu[NUM_CU_DEPTH];
63     double      percentIntraDistribution[NUM_CU_DEPTH][INTRA_MODES];
64     double      percentInterDistribution[NUM_CU_DEPTH][3];           // 2Nx2N, RECT, AMP modes percentage
65     double      ipCostRatio;
66 
67     uint64_t    cntIntraNxN;
68     uint64_t    totalCu;
69     uint64_t    totalCtu;
70     uint64_t    lumaDistortion;
71     uint64_t    chromaDistortion;
72     uint64_t    psyEnergy;
73     int64_t     ssimEnergy;
74     uint64_t    resEnergy;
75     uint64_t    cntSkipCu[NUM_CU_DEPTH];
76     uint64_t    cntMergeCu[NUM_CU_DEPTH];
77     uint64_t    cntInter[NUM_CU_DEPTH];
78     uint64_t    cntIntra[NUM_CU_DEPTH];
79     uint64_t    cuInterDistribution[NUM_CU_DEPTH][INTER_MODES];
80     uint64_t    cuIntraDistribution[NUM_CU_DEPTH][INTRA_MODES];
81 
82 
83     uint64_t    totalPu[NUM_CU_DEPTH + 1];
84     uint64_t    cntSkipPu[NUM_CU_DEPTH];
85     uint64_t    cntIntraPu[NUM_CU_DEPTH];
86     uint64_t    cntAmp[NUM_CU_DEPTH];
87     uint64_t    cnt4x4;
88     uint64_t    cntInterPu[NUM_CU_DEPTH][INTER_MODES - 1];
89     uint64_t    cntMergePu[NUM_CU_DEPTH][INTER_MODES - 1];
90 
91     /* Feature values per row for dynamic refinement */
92     uint64_t       rowRdDyn[MAX_NUM_DYN_REFINE];
93     uint32_t       rowVarDyn[MAX_NUM_DYN_REFINE];
94     uint32_t       rowCntDyn[MAX_NUM_DYN_REFINE];
95 
FrameStatsFrameStats96     FrameStats()
97     {
98         memset(this, 0, sizeof(FrameStats));
99     }
100 };
101 
102 /* Per-frame data that is used during encodes and referenced while the picture
103  * is available for reference. A FrameData instance is attached to a Frame as it
104  * comes out of the lookahead. Frames which are not being encoded do not have a
105  * FrameData instance. These instances are re-used once the encoded frame has
106  * no active references. They hold the Slice instance and the 'official' CTU
107  * data structures. They are maintained in a free-list pool along together with
108  * a reconstructed image PicYuv in order to conserve memory. */
109 class FrameData
110 {
111 public:
112 
113     Slice*         m_slice;
114     SAOParam*      m_saoParam;
115     const x265_param* m_param;
116 
117     FrameData*     m_freeListNext;
118     PicYuv*        m_reconPic;
119     bool           m_bHasReferences;   /* used during DPB/RPS updates */
120     int            m_frameEncoderID;   /* the ID of the FrameEncoder encoding this frame */
121     JobProvider*   m_jobProvider;
122 
123     CUDataMemPool  m_cuMemPool;
124     CUData*        m_picCTU;
125 
126     RPS*           m_spsrps;
127     int            m_spsrpsIdx;
128 
129     /* Rate control data used during encode and by references */
130     struct RCStatCU
131     {
132         uint32_t totalBits;     /* total bits to encode this CTU */
133         uint32_t vbvCost;       /* sum of lowres costs for 16x16 sub-blocks */
134         uint32_t intraVbvCost;  /* sum of lowres intra costs for 16x16 sub-blocks */
135         uint64_t avgCost[4];    /* stores the avg cost of CU's in frame for each depth */
136         uint32_t count[4];      /* count and avgCost only used by Analysis at RD0..4 */
137         double   baseQp;        /* Qp of Cu set from RateControl/Vbv (only used by frame encoder) */
138     };
139 
140     struct RCStatRow
141     {
142         uint32_t numEncodedCUs; /* ctuAddr of last encoded CTU in row */
143         uint32_t encodedBits;   /* sum of 'totalBits' of encoded CTUs */
144         uint32_t satdForVbv;    /* sum of lowres (estimated) costs for entire row */
145         uint32_t intraSatdForVbv; /* sum of lowres (estimated) intra costs for entire row */
146         uint32_t rowSatd;
147         uint32_t rowIntraSatd;
148         double   rowQp;
149         double   rowQpScale;
150         double   sumQpRc;
151         double   sumQpAq;
152     };
153 
154     RCStatCU*      m_cuStat;
155     RCStatRow*     m_rowStat;
156     FrameStats     m_frameStats; // stats of current frame for multi-pass encodes
157     /* data needed for periodic intra refresh */
158     struct PeriodicIR
159     {
160         uint32_t   pirStartCol;
161         uint32_t   pirEndCol;
162         int        framesSinceLastPir;
163     };
164 
165     PeriodicIR     m_pir;
166     double         m_avgQpRc;    /* avg QP as decided by rate-control */
167     double         m_avgQpAq;    /* avg QP as decided by AQ in addition to rate-control */
168     double         m_rateFactor; /* calculated based on the Frame QP */
169     int            m_picCsp;
170 
171     uint32_t*              m_meIntegral[INTEGRAL_PLANE_NUM];       // 12 integral planes for 32x32, 32x24, 32x8, 24x32, 16x16, 16x12, 16x4, 12x16, 8x32, 8x8, 4x16 and 4x4.
172     uint32_t*              m_meBuffer[INTEGRAL_PLANE_NUM];
173 
174     FrameData();
175 
176     bool create(const x265_param& param, const SPS& sps, int csp);
177     void reinit(const SPS& sps);
178     void destroy();
getPicCTU(uint32_t ctuAddr)179     inline CUData* getPicCTU(uint32_t ctuAddr) { return &m_picCTU[ctuAddr]; }
180 };
181 
182 }
183 #endif // ifndef X265_FRAMEDATA_H
184