1 /*****************************************************************************
2  * Copyright (C) 2013-2020 MulticoreWare, Inc
3  *
4  * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
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 "common.h"
25 #include "yuv.h"
26 #include "shortyuv.h"
27 #include "primitives.h"
28 
29 #include "x265.h"
30 
31 using namespace X265_NS;
32 
ShortYuv()33 ShortYuv::ShortYuv()
34 {
35     m_buf[0] = NULL;
36     m_buf[1] = NULL;
37     m_buf[2] = NULL;
38 }
39 
create(uint32_t size,int csp)40 bool ShortYuv::create(uint32_t size, int csp)
41 {
42     m_csp = csp;
43     m_size = size;
44     m_hChromaShift = CHROMA_H_SHIFT(csp);
45     m_vChromaShift = CHROMA_V_SHIFT(csp);
46     size_t sizeL = size * size;
47 
48     if (csp != X265_CSP_I400)
49     {
50         m_csize = size >> m_hChromaShift;
51         size_t sizeC = sizeL >> (m_hChromaShift + m_vChromaShift);
52         X265_CHECK((sizeC & 15) == 0, "invalid size");
53 
54         CHECKED_MALLOC(m_buf[0], int16_t, sizeL + sizeC * 2);
55         m_buf[1] = m_buf[0] + sizeL;
56         m_buf[2] = m_buf[0] + sizeL + sizeC;
57     }
58     else
59     {
60         CHECKED_MALLOC(m_buf[0], int16_t, sizeL);
61         m_buf[1] = m_buf[2] = NULL;
62     }
63     return true;
64 
65 fail:
66     return false;
67 }
68 
destroy()69 void ShortYuv::destroy()
70 {
71     X265_FREE(m_buf[0]);
72 }
73 
clear()74 void ShortYuv::clear()
75 {
76     memset(m_buf[0], 0, (m_size  * m_size) *  sizeof(int16_t));
77     memset(m_buf[1], 0, (m_csize * m_csize) * sizeof(int16_t));
78     memset(m_buf[2], 0, (m_csize * m_csize) * sizeof(int16_t));
79 }
80 
subtract(const Yuv & srcYuv0,const Yuv & srcYuv1,uint32_t log2Size,int picCsp)81 void ShortYuv::subtract(const Yuv& srcYuv0, const Yuv& srcYuv1, uint32_t log2Size, int picCsp)
82 {
83     const int sizeIdx = log2Size - 2;
84     primitives.cu[sizeIdx].sub_ps(m_buf[0], m_size, srcYuv0.m_buf[0], srcYuv1.m_buf[0], srcYuv0.m_size, srcYuv1.m_size);
85     if (m_csp != X265_CSP_I400 && picCsp != X265_CSP_I400)
86     {
87         primitives.chroma[m_csp].cu[sizeIdx].sub_ps(m_buf[1], m_csize, srcYuv0.m_buf[1], srcYuv1.m_buf[1], srcYuv0.m_csize, srcYuv1.m_csize);
88         primitives.chroma[m_csp].cu[sizeIdx].sub_ps(m_buf[2], m_csize, srcYuv0.m_buf[2], srcYuv1.m_buf[2], srcYuv0.m_csize, srcYuv1.m_csize);
89     }
90 }
91 
copyPartToPartLuma(ShortYuv & dstYuv,uint32_t absPartIdx,uint32_t log2Size) const92 void ShortYuv::copyPartToPartLuma(ShortYuv& dstYuv, uint32_t absPartIdx, uint32_t log2Size) const
93 {
94     const int16_t* src = getLumaAddr(absPartIdx);
95     int16_t* dst = dstYuv.getLumaAddr(absPartIdx);
96 
97     primitives.cu[log2Size - 2].copy_ss(dst, dstYuv.m_size, src, m_size);
98 }
99 
copyPartToPartLuma(Yuv & dstYuv,uint32_t absPartIdx,uint32_t log2Size) const100 void ShortYuv::copyPartToPartLuma(Yuv& dstYuv, uint32_t absPartIdx, uint32_t log2Size) const
101 {
102     const int16_t* src = getLumaAddr(absPartIdx);
103     pixel* dst = dstYuv.getLumaAddr(absPartIdx);
104 
105     primitives.cu[log2Size - 2].copy_sp(dst, dstYuv.m_size, src, m_size);
106 }
107 
copyPartToPartChroma(ShortYuv & dstYuv,uint32_t absPartIdx,uint32_t log2SizeL) const108 void ShortYuv::copyPartToPartChroma(ShortYuv& dstYuv, uint32_t absPartIdx, uint32_t log2SizeL) const
109 {
110     int part = partitionFromLog2Size(log2SizeL);
111     const int16_t* srcU = getCbAddr(absPartIdx);
112     const int16_t* srcV = getCrAddr(absPartIdx);
113     int16_t* dstU = dstYuv.getCbAddr(absPartIdx);
114     int16_t* dstV = dstYuv.getCrAddr(absPartIdx);
115 
116     primitives.chroma[m_csp].cu[part].copy_ss(dstU, dstYuv.m_csize, srcU, m_csize);
117     primitives.chroma[m_csp].cu[part].copy_ss(dstV, dstYuv.m_csize, srcV, m_csize);
118 }
119 
copyPartToPartChroma(Yuv & dstYuv,uint32_t absPartIdx,uint32_t log2SizeL) const120 void ShortYuv::copyPartToPartChroma(Yuv& dstYuv, uint32_t absPartIdx, uint32_t log2SizeL) const
121 {
122     int part = partitionFromLog2Size(log2SizeL);
123     const int16_t* srcU = getCbAddr(absPartIdx);
124     const int16_t* srcV = getCrAddr(absPartIdx);
125     pixel* dstU = dstYuv.getCbAddr(absPartIdx);
126     pixel* dstV = dstYuv.getCrAddr(absPartIdx);
127 
128     primitives.chroma[m_csp].cu[part].copy_sp(dstU, dstYuv.m_csize, srcU, m_csize);
129     primitives.chroma[m_csp].cu[part].copy_sp(dstV, dstYuv.m_csize, srcV, m_csize);
130 }
131