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 #include "framedata.h"
25 #include "picyuv.h"
26 
27 using namespace X265_NS;
28 
FrameData()29 FrameData::FrameData()
30 {
31     memset(this, 0, sizeof(*this));
32 }
33 
create(const x265_param & param,const SPS & sps,int csp)34 bool FrameData::create(const x265_param& param, const SPS& sps, int csp)
35 {
36     m_param = &param;
37     m_slice  = new Slice;
38     m_picCTU = new CUData[sps.numCUsInFrame];
39     m_picCsp = csp;
40     m_spsrpsIdx = -1;
41     if (param.rc.bStatWrite)
42         m_spsrps = const_cast<RPS*>(sps.spsrps);
43     bool isallocated = m_cuMemPool.create(0, param.internalCsp, sps.numCUsInFrame, param);
44     if (m_param->bDynamicRefine)
45     {
46         CHECKED_MALLOC_ZERO(m_cuMemPool.dynRefineRdBlock, uint64_t, MAX_NUM_DYN_REFINE * sps.numCUsInFrame);
47         CHECKED_MALLOC_ZERO(m_cuMemPool.dynRefCntBlock, uint32_t, MAX_NUM_DYN_REFINE * sps.numCUsInFrame);
48         CHECKED_MALLOC_ZERO(m_cuMemPool.dynRefVarBlock, uint32_t, MAX_NUM_DYN_REFINE * sps.numCUsInFrame);
49     }
50     if (isallocated)
51     {
52         for (uint32_t ctuAddr = 0; ctuAddr < sps.numCUsInFrame; ctuAddr++)
53         {
54             if (m_param->bDynamicRefine)
55             {
56                 m_picCTU[ctuAddr].m_collectCURd = m_cuMemPool.dynRefineRdBlock + (ctuAddr * MAX_NUM_DYN_REFINE);
57                 m_picCTU[ctuAddr].m_collectCUVariance = m_cuMemPool.dynRefVarBlock + (ctuAddr * MAX_NUM_DYN_REFINE);
58                 m_picCTU[ctuAddr].m_collectCUCount = m_cuMemPool.dynRefCntBlock + (ctuAddr * MAX_NUM_DYN_REFINE);
59             }
60             m_picCTU[ctuAddr].initialize(m_cuMemPool, 0, param, ctuAddr);
61         }
62     }
63     else
64         return false;
65     CHECKED_MALLOC_ZERO(m_cuStat, RCStatCU, sps.numCUsInFrame);
66     CHECKED_MALLOC(m_rowStat, RCStatRow, sps.numCuInHeight);
67     reinit(sps);
68 
69     for (int i = 0; i < INTEGRAL_PLANE_NUM; i++)
70     {
71         m_meBuffer[i] = NULL;
72         m_meIntegral[i] = NULL;
73     }
74     return true;
75 
76 fail:
77     return false;
78 }
79 
reinit(const SPS & sps)80 void FrameData::reinit(const SPS& sps)
81 {
82     memset(m_cuStat, 0, sps.numCUsInFrame * sizeof(*m_cuStat));
83     memset(m_rowStat, 0, sps.numCuInHeight * sizeof(*m_rowStat));
84     if (m_param->bDynamicRefine)
85     {
86         memset(m_picCTU->m_collectCURd, 0, MAX_NUM_DYN_REFINE * sps.numCUsInFrame * sizeof(uint64_t));
87         memset(m_picCTU->m_collectCUVariance, 0, MAX_NUM_DYN_REFINE * sps.numCUsInFrame * sizeof(uint32_t));
88         memset(m_picCTU->m_collectCUCount, 0, MAX_NUM_DYN_REFINE * sps.numCUsInFrame * sizeof(uint32_t));
89     }
90 }
91 
destroy()92 void FrameData::destroy()
93 {
94     delete [] m_picCTU;
95     delete m_slice;
96     delete m_saoParam;
97 
98     m_cuMemPool.destroy();
99 
100     if (m_param->bDynamicRefine)
101     {
102         X265_FREE(m_cuMemPool.dynRefineRdBlock);
103         X265_FREE(m_cuMemPool.dynRefCntBlock);
104         X265_FREE(m_cuMemPool.dynRefVarBlock);
105     }
106     X265_FREE(m_cuStat);
107     X265_FREE(m_rowStat);
108     for (int i = 0; i < INTEGRAL_PLANE_NUM; i++)
109     {
110         if (m_meBuffer[i] != NULL)
111         {
112             X265_FREE(m_meBuffer[i]);
113             m_meBuffer[i] = NULL;
114         }
115     }
116 }
117