/src/x265/source/encoder/reference.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright (C) 2013-2020 MulticoreWare, Inc |
3 | | * |
4 | | * Authors: Steve Borho <steve@borho.org> |
5 | | * Deepthi Devaki <deepthidevaki@multicorewareinc.com> |
6 | | * Min Chen <chenm003@163.com> |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 2 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
21 | | * |
22 | | * This program is also available under a commercial proprietary license. |
23 | | * For more information, contact us at license @ x265.com. |
24 | | *****************************************************************************/ |
25 | | |
26 | | #include "common.h" |
27 | | #include "primitives.h" |
28 | | #include "slice.h" |
29 | | #include "picyuv.h" |
30 | | |
31 | | #include "reference.h" |
32 | | |
33 | | using namespace X265_NS; |
34 | | |
35 | | MotionReference::MotionReference() |
36 | 0 | { |
37 | 0 | weightBuffer[0] = NULL; |
38 | 0 | weightBuffer[1] = NULL; |
39 | 0 | weightBuffer[2] = NULL; |
40 | 0 | numSliceWeightedRows = NULL; |
41 | 0 | } |
42 | | |
43 | | MotionReference::~MotionReference() |
44 | 0 | { |
45 | 0 | X265_FREE(numSliceWeightedRows); |
46 | 0 | X265_FREE(weightBuffer[0]); |
47 | 0 | X265_FREE(weightBuffer[1]); |
48 | 0 | X265_FREE(weightBuffer[2]); |
49 | 0 | } |
50 | | |
51 | | int MotionReference::init(PicYuv* recPic, WeightParam *wp, const x265_param& p) |
52 | 0 | { |
53 | 0 | reconPic = recPic; |
54 | 0 | lumaStride = recPic->m_stride; |
55 | 0 | chromaStride = recPic->m_strideC; |
56 | 0 | numInterpPlanes = p.subpelRefine > 2 ? 3 : 1; /* is chroma satd possible? */ |
57 | |
|
58 | 0 | if (numSliceWeightedRows) |
59 | 0 | { |
60 | | // Unnecessary, but avoid risk on parameters dynamic modify in future. |
61 | 0 | X265_FREE(numSliceWeightedRows); |
62 | 0 | numSliceWeightedRows = NULL; |
63 | 0 | } |
64 | 0 | numSliceWeightedRows = X265_MALLOC(uint32_t, p.maxSlices); |
65 | 0 | memset(numSliceWeightedRows, 0, p.maxSlices * sizeof(uint32_t)); |
66 | | |
67 | | /* directly reference the extended integer pel planes */ |
68 | 0 | fpelPlane[0] = recPic->m_picOrg[0]; |
69 | 0 | fpelPlane[1] = recPic->m_picOrg[1]; |
70 | 0 | fpelPlane[2] = recPic->m_picOrg[2]; |
71 | 0 | isWeighted = false; |
72 | |
|
73 | 0 | if (wp) |
74 | 0 | { |
75 | 0 | uint32_t numCUinHeight = (reconPic->m_picHeight + p.maxCUSize - 1) / p.maxCUSize; |
76 | |
|
77 | 0 | int marginX = reconPic->m_lumaMarginX; |
78 | 0 | int marginY = reconPic->m_lumaMarginY; |
79 | 0 | intptr_t stride = reconPic->m_stride; |
80 | 0 | int cuHeight = p.maxCUSize; |
81 | |
|
82 | 0 | for (int c = 0; c < (p.internalCsp != X265_CSP_I400 && recPic->m_picCsp != X265_CSP_I400 ? numInterpPlanes : 1); c++) |
83 | 0 | { |
84 | 0 | if (c == 1) |
85 | 0 | { |
86 | 0 | marginX = reconPic->m_chromaMarginX; |
87 | 0 | marginY = reconPic->m_chromaMarginY; |
88 | 0 | stride = reconPic->m_strideC; |
89 | 0 | cuHeight >>= reconPic->m_vChromaShift; |
90 | 0 | } |
91 | |
|
92 | 0 | if (wp[c].wtPresent) |
93 | 0 | { |
94 | 0 | if (!weightBuffer[c]) |
95 | 0 | { |
96 | 0 | size_t padheight = (numCUinHeight * cuHeight) + marginY * 2; |
97 | 0 | weightBuffer[c] = X265_MALLOC(pixel, stride * padheight); |
98 | 0 | if (!weightBuffer[c]) |
99 | 0 | return -1; |
100 | 0 | } |
101 | | |
102 | | /* use our buffer which will have weighted pixels written to it */ |
103 | 0 | fpelPlane[c] = weightBuffer[c] + marginY * stride + marginX; |
104 | 0 | X265_CHECK(recPic->m_picOrg[c] - recPic->m_picBuf[c] == marginY * stride + marginX, "PicYuv pad calculation mismatch\n"); |
105 | |
|
106 | 0 | w[c].weight = wp[c].inputWeight; |
107 | 0 | w[c].offset = wp[c].inputOffset * (1 << (X265_DEPTH - 8)); |
108 | 0 | w[c].shift = wp[c].log2WeightDenom; |
109 | 0 | w[c].round = w[c].shift ? 1 << (w[c].shift - 1) : 0; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | 0 | isWeighted = true; |
114 | 0 | } |
115 | | |
116 | 0 | return 0; |
117 | 0 | } |
118 | | |
119 | | void MotionReference::applyWeight(uint32_t finishedRows, uint32_t maxNumRows, uint32_t maxNumRowsInSlice, uint32_t sliceId) |
120 | 0 | { |
121 | 0 | const uint32_t numWeightedRows = numSliceWeightedRows[sliceId]; |
122 | 0 | finishedRows = X265_MIN(finishedRows, maxNumRowsInSlice); |
123 | 0 | if (numWeightedRows >= finishedRows) |
124 | 0 | return; |
125 | | |
126 | 0 | int marginX = reconPic->m_lumaMarginX; |
127 | 0 | int marginY = reconPic->m_lumaMarginY; |
128 | 0 | intptr_t stride = reconPic->m_stride; |
129 | 0 | int width = reconPic->m_picWidth; |
130 | 0 | int height = (finishedRows - numWeightedRows) * reconPic->m_param->maxCUSize; |
131 | | /* the last row may be partial height */ |
132 | 0 | if (finishedRows == maxNumRows - 1) |
133 | 0 | { |
134 | 0 | const int leftRows = (reconPic->m_picHeight & (reconPic->m_param->maxCUSize - 1)); |
135 | |
|
136 | 0 | height += leftRows ? leftRows : reconPic->m_param->maxCUSize; |
137 | 0 | } |
138 | 0 | int cuHeight = reconPic->m_param->maxCUSize; |
139 | |
|
140 | 0 | for (int c = 0; c < numInterpPlanes; c++) |
141 | 0 | { |
142 | 0 | if (c == 1) |
143 | 0 | { |
144 | 0 | marginX = reconPic->m_chromaMarginX; |
145 | 0 | marginY = reconPic->m_chromaMarginY; |
146 | 0 | stride = reconPic->m_strideC; |
147 | 0 | width >>= reconPic->m_hChromaShift; |
148 | 0 | height >>= reconPic->m_vChromaShift; |
149 | 0 | cuHeight >>= reconPic->m_vChromaShift; |
150 | 0 | } |
151 | | |
152 | | /* Do not generate weighted predictions if using original picture */ |
153 | 0 | if (fpelPlane[c] == reconPic->m_picOrg[c]) |
154 | 0 | continue; |
155 | | |
156 | 0 | const pixel* src = reconPic->m_picOrg[c] + numWeightedRows * cuHeight * stride; |
157 | 0 | pixel* dst = fpelPlane[c] + numWeightedRows * cuHeight * stride; |
158 | | // Computing weighted CU rows |
159 | 0 | int correction = IF_INTERNAL_PREC - X265_DEPTH; // intermediate interpolation depth |
160 | 0 | int padwidth = (width + 31) & ~31; // weightp assembly needs even 32 byte widths |
161 | 0 | primitives.weight_pp(src, dst, stride, padwidth, height, w[c].weight, w[c].round << correction, w[c].shift + correction, w[c].offset); |
162 | | // Extending Left & Right |
163 | 0 | primitives.extendRowBorder(dst, stride, width, height, marginX); |
164 | | |
165 | | // Extending Above |
166 | 0 | if (numWeightedRows == 0) |
167 | 0 | { |
168 | 0 | pixel *pixY = fpelPlane[c] - marginX; |
169 | 0 | for (int y = 0; y < marginY; y++) |
170 | 0 | memcpy(pixY - (y + 1) * stride, pixY, stride * sizeof(pixel)); |
171 | 0 | } |
172 | | |
173 | | // Extending Bottom |
174 | 0 | if (finishedRows == maxNumRows - 1) |
175 | 0 | { |
176 | 0 | int picHeight = reconPic->m_picHeight; |
177 | 0 | if (c) picHeight >>= reconPic->m_vChromaShift; |
178 | 0 | pixel *pixY = fpelPlane[c] - marginX + (picHeight - 1) * stride; |
179 | 0 | for (int y = 0; y < marginY; y++) |
180 | 0 | memcpy(pixY + (y + 1) * stride, pixY, stride * sizeof(pixel)); |
181 | 0 | } |
182 | 0 | } |
183 | |
|
184 | 0 | numSliceWeightedRows[sliceId] = finishedRows; |
185 | 0 | } |