Imported Upstream version 1.4+222+hg5f9f7194267b
[deb_x265.git] / source / common / shortyuv.cpp
CommitLineData
72b9787e
JB
1/*****************************************************************************
2 * Copyright (C) 2013 x265 project
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
31using namespace x265;
32
33ShortYuv::ShortYuv()
34{
35 m_buf[0] = NULL;
36 m_buf[1] = NULL;
37 m_buf[2] = NULL;
38}
39
40bool ShortYuv::create(uint32_t size, int csp)
41{
42 m_csp = csp;
43 m_hChromaShift = CHROMA_H_SHIFT(csp);
44 m_vChromaShift = CHROMA_V_SHIFT(csp);
45
46 m_size = size;
47 m_csize = size >> m_hChromaShift;
48
49 size_t sizeL = size * size;
50 size_t sizeC = sizeL >> (m_hChromaShift + m_vChromaShift);
51 X265_CHECK((sizeC & 15) == 0, "invalid size");
52
53 CHECKED_MALLOC(m_buf[0], int16_t, sizeL + sizeC * 2);
54 m_buf[1] = m_buf[0] + sizeL;
55 m_buf[2] = m_buf[0] + sizeL + sizeC;
56 return true;
57
58fail:
59 return false;
60}
61
62void ShortYuv::destroy()
63{
64 X265_FREE(m_buf[0]);
65}
66
67void ShortYuv::clear()
68{
69 ::memset(m_buf[0], 0, (m_size * m_size) * sizeof(int16_t));
70 ::memset(m_buf[1], 0, (m_csize * m_csize) * sizeof(int16_t));
71 ::memset(m_buf[2], 0, (m_csize * m_csize) * sizeof(int16_t));
72}
73
74void ShortYuv::subtract(const Yuv& srcYuv0, const Yuv& srcYuv1, uint32_t log2Size)
75{
76 const int sizeIdx = log2Size - 2;
77 primitives.luma_sub_ps[sizeIdx](m_buf[0], m_size, srcYuv0.m_buf[0], srcYuv1.m_buf[0], srcYuv0.m_size, srcYuv1.m_size);
78 primitives.chroma[m_csp].sub_ps[sizeIdx](m_buf[1], m_csize, srcYuv0.m_buf[1], srcYuv1.m_buf[1], srcYuv0.m_csize, srcYuv1.m_csize);
79 primitives.chroma[m_csp].sub_ps[sizeIdx](m_buf[2], m_csize, srcYuv0.m_buf[2], srcYuv1.m_buf[2], srcYuv0.m_csize, srcYuv1.m_csize);
80}
81
82void ShortYuv::copyPartToPartLuma(ShortYuv& dstYuv, uint32_t absPartIdx, uint32_t log2Size) const
83{
84 const int16_t* src = getLumaAddr(absPartIdx);
85 int16_t* dst = dstYuv.getLumaAddr(absPartIdx);
86
b53f7c52 87 primitives.luma_copy_ss[log2Size - 2](dst, dstYuv.m_size, src, m_size);
72b9787e
JB
88}
89
90void ShortYuv::copyPartToPartLuma(Yuv& dstYuv, uint32_t absPartIdx, uint32_t log2Size) const
91{
92 const int16_t* src = getLumaAddr(absPartIdx);
93 pixel* dst = dstYuv.getLumaAddr(absPartIdx);
94
b53f7c52 95 primitives.luma_copy_sp[log2Size - 2](dst, dstYuv.m_size, src, m_size);
72b9787e
JB
96}
97
98void ShortYuv::copyPartToPartChroma(ShortYuv& dstYuv, uint32_t absPartIdx, uint32_t log2SizeL) const
99{
100 int part = partitionFromLog2Size(log2SizeL);
101 const int16_t* srcU = getCbAddr(absPartIdx);
102 const int16_t* srcV = getCrAddr(absPartIdx);
103 int16_t* dstU = dstYuv.getCbAddr(absPartIdx);
104 int16_t* dstV = dstYuv.getCrAddr(absPartIdx);
105
b53f7c52
JB
106 primitives.chroma[m_csp].copy_ss[part](dstU, dstYuv.m_csize, srcU, m_csize);
107 primitives.chroma[m_csp].copy_ss[part](dstV, dstYuv.m_csize, srcV, m_csize);
72b9787e
JB
108}
109
110void ShortYuv::copyPartToPartChroma(Yuv& dstYuv, uint32_t absPartIdx, uint32_t log2SizeL) const
111{
112 int part = partitionFromLog2Size(log2SizeL);
113 const int16_t* srcU = getCbAddr(absPartIdx);
114 const int16_t* srcV = getCrAddr(absPartIdx);
115 pixel* dstU = dstYuv.getCbAddr(absPartIdx);
116 pixel* dstV = dstYuv.getCrAddr(absPartIdx);
117
b53f7c52
JB
118 primitives.chroma[m_csp].copy_sp[part](dstU, dstYuv.m_csize, srcU, m_csize);
119 primitives.chroma[m_csp].copy_sp[part](dstV, dstYuv.m_csize, srcV, m_csize);
72b9787e 120}