Commit | Line | Data |
---|---|---|
72b9787e JB |
1 | /***************************************************************************** |
2 | * Copyright (C) 2013 x265 project | |
3 | * | |
4 | * Authors: Gopu Govindaswamy <gopu@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 "picyuv.h" | |
25 | #include "lowres.h" | |
26 | #include "mv.h" | |
27 | ||
28 | using namespace x265; | |
29 | ||
30 | bool Lowres::create(PicYuv *origPic, int _bframes, bool bAQEnabled) | |
31 | { | |
32 | isLowres = true; | |
33 | bframes = _bframes; | |
34 | width = origPic->m_picWidth / 2; | |
35 | lines = origPic->m_picHeight / 2; | |
36 | lumaStride = width + 2 * origPic->m_lumaMarginX; | |
37 | if (lumaStride & 31) | |
38 | lumaStride += 32 - (lumaStride & 31); | |
39 | int cuWidth = (width + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS; | |
40 | int cuHeight = (lines + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS; | |
41 | int cuCount = cuWidth * cuHeight; | |
42 | ||
43 | /* rounding the width to multiple of lowres CU size */ | |
44 | width = cuWidth * X265_LOWRES_CU_SIZE; | |
45 | lines = cuHeight * X265_LOWRES_CU_SIZE; | |
46 | ||
47 | size_t planesize = lumaStride * (lines + 2 * origPic->m_lumaMarginY); | |
48 | size_t padoffset = lumaStride * origPic->m_lumaMarginY + origPic->m_lumaMarginX; | |
49 | ||
50 | if (bAQEnabled) | |
51 | { | |
52 | CHECKED_MALLOC(qpAqOffset, double, cuCount); | |
53 | CHECKED_MALLOC(invQscaleFactor, int, cuCount); | |
54 | CHECKED_MALLOC(qpCuTreeOffset, double, cuCount); | |
55 | } | |
56 | CHECKED_MALLOC(propagateCost, uint16_t, cuCount); | |
57 | ||
58 | /* allocate lowres buffers */ | |
59 | for (int i = 0; i < 4; i++) | |
60 | { | |
61 | CHECKED_MALLOC(buffer[i], pixel, planesize); | |
62 | /* initialize the whole buffer to prevent valgrind warnings on right edge */ | |
63 | memset(buffer[i], 0, sizeof(pixel) * planesize); | |
64 | } | |
65 | ||
66 | lowresPlane[0] = buffer[0] + padoffset; | |
67 | lowresPlane[1] = buffer[1] + padoffset; | |
68 | lowresPlane[2] = buffer[2] + padoffset; | |
69 | lowresPlane[3] = buffer[3] + padoffset; | |
70 | ||
71 | CHECKED_MALLOC(intraCost, int32_t, cuCount); | |
b53f7c52 | 72 | CHECKED_MALLOC(intraMode, uint8_t, cuCount); |
72b9787e JB |
73 | |
74 | for (int i = 0; i < bframes + 2; i++) | |
75 | { | |
76 | for (int j = 0; j < bframes + 2; j++) | |
77 | { | |
78 | CHECKED_MALLOC(rowSatds[i][j], int32_t, cuHeight); | |
79 | CHECKED_MALLOC(lowresCosts[i][j], uint16_t, cuCount); | |
80 | } | |
81 | } | |
82 | ||
83 | for (int i = 0; i < bframes + 1; i++) | |
84 | { | |
85 | CHECKED_MALLOC(lowresMvs[0][i], MV, cuCount); | |
86 | CHECKED_MALLOC(lowresMvs[1][i], MV, cuCount); | |
87 | CHECKED_MALLOC(lowresMvCosts[0][i], int32_t, cuCount); | |
88 | CHECKED_MALLOC(lowresMvCosts[1][i], int32_t, cuCount); | |
89 | } | |
90 | ||
91 | return true; | |
92 | ||
93 | fail: | |
94 | return false; | |
95 | } | |
96 | ||
97 | void Lowres::destroy() | |
98 | { | |
99 | for (int i = 0; i < 4; i++) | |
100 | X265_FREE(buffer[i]); | |
101 | ||
102 | X265_FREE(intraCost); | |
b53f7c52 | 103 | X265_FREE(intraMode); |
72b9787e JB |
104 | |
105 | for (int i = 0; i < bframes + 2; i++) | |
106 | { | |
107 | for (int j = 0; j < bframes + 2; j++) | |
108 | { | |
109 | X265_FREE(rowSatds[i][j]); | |
110 | X265_FREE(lowresCosts[i][j]); | |
111 | } | |
112 | } | |
113 | ||
114 | for (int i = 0; i < bframes + 1; i++) | |
115 | { | |
116 | X265_FREE(lowresMvs[0][i]); | |
117 | X265_FREE(lowresMvs[1][i]); | |
118 | X265_FREE(lowresMvCosts[0][i]); | |
119 | X265_FREE(lowresMvCosts[1][i]); | |
120 | } | |
121 | ||
122 | X265_FREE(qpAqOffset); | |
123 | X265_FREE(invQscaleFactor); | |
124 | X265_FREE(qpCuTreeOffset); | |
125 | X265_FREE(propagateCost); | |
126 | } | |
127 | ||
128 | // (re) initialize lowres state | |
129 | void Lowres::init(PicYuv *origPic, int poc, int type) | |
130 | { | |
131 | bIntraCalculated = false; | |
132 | bLastMiniGopBFrame = false; | |
133 | bScenecut = true; // could be a scene-cut, until ruled out by flash detection | |
134 | bKeyframe = false; // Not a keyframe unless identified by lookahead | |
135 | sliceType = type; | |
136 | frameNum = poc; | |
137 | leadingBframes = 0; | |
138 | indB = 0; | |
139 | satdCost = (int64_t)-1; | |
140 | memset(costEst, -1, sizeof(costEst)); | |
141 | memset(weightedCostDelta, 0, sizeof(weightedCostDelta)); | |
142 | ||
143 | if (qpAqOffset && invQscaleFactor) | |
144 | memset(costEstAq, -1, sizeof(costEstAq)); | |
145 | ||
146 | for (int y = 0; y < bframes + 2; y++) | |
147 | for (int x = 0; x < bframes + 2; x++) | |
148 | rowSatds[y][x][0] = -1; | |
149 | ||
150 | for (int i = 0; i < bframes + 1; i++) | |
151 | { | |
152 | lowresMvs[0][i][0].x = 0x7FFF; | |
153 | lowresMvs[1][i][0].x = 0x7FFF; | |
154 | } | |
155 | ||
156 | for (int i = 0; i < bframes + 2; i++) | |
157 | intraMbs[i] = 0; | |
158 | ||
159 | /* downscale and generate 4 hpel planes for lookahead */ | |
b53f7c52 | 160 | primitives.frameInitLowres(origPic->m_picOrg[0], |
72b9787e JB |
161 | lowresPlane[0], lowresPlane[1], lowresPlane[2], lowresPlane[3], |
162 | origPic->m_stride, lumaStride, width, lines); | |
163 | ||
164 | /* extend hpel planes for motion search */ | |
165 | extendPicBorder(lowresPlane[0], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY); | |
166 | extendPicBorder(lowresPlane[1], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY); | |
167 | extendPicBorder(lowresPlane[2], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY); | |
168 | extendPicBorder(lowresPlane[3], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY); | |
b53f7c52 | 169 | fpelPlane[0] = lowresPlane[0]; |
72b9787e | 170 | } |