Imported Upstream version 1.4
[deb_x265.git] / source / common / lowres.cpp
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);
72
73 for (int i = 0; i < bframes + 2; i++)
74 {
75 for (int j = 0; j < bframes + 2; j++)
76 {
77 CHECKED_MALLOC(rowSatds[i][j], int32_t, cuHeight);
78 CHECKED_MALLOC(lowresCosts[i][j], uint16_t, cuCount);
79 }
80 }
81
82 for (int i = 0; i < bframes + 1; i++)
83 {
84 CHECKED_MALLOC(lowresMvs[0][i], MV, cuCount);
85 CHECKED_MALLOC(lowresMvs[1][i], MV, cuCount);
86 CHECKED_MALLOC(lowresMvCosts[0][i], int32_t, cuCount);
87 CHECKED_MALLOC(lowresMvCosts[1][i], int32_t, cuCount);
88 }
89
90 return true;
91
92 fail:
93 return false;
94 }
95
96 void Lowres::destroy()
97 {
98 for (int i = 0; i < 4; i++)
99 X265_FREE(buffer[i]);
100
101 X265_FREE(intraCost);
102
103 for (int i = 0; i < bframes + 2; i++)
104 {
105 for (int j = 0; j < bframes + 2; j++)
106 {
107 X265_FREE(rowSatds[i][j]);
108 X265_FREE(lowresCosts[i][j]);
109 }
110 }
111
112 for (int i = 0; i < bframes + 1; i++)
113 {
114 X265_FREE(lowresMvs[0][i]);
115 X265_FREE(lowresMvs[1][i]);
116 X265_FREE(lowresMvCosts[0][i]);
117 X265_FREE(lowresMvCosts[1][i]);
118 }
119
120 X265_FREE(qpAqOffset);
121 X265_FREE(invQscaleFactor);
122 X265_FREE(qpCuTreeOffset);
123 X265_FREE(propagateCost);
124 }
125
126 // (re) initialize lowres state
127 void Lowres::init(PicYuv *origPic, int poc, int type)
128 {
129 bIntraCalculated = false;
130 bLastMiniGopBFrame = false;
131 bScenecut = true; // could be a scene-cut, until ruled out by flash detection
132 bKeyframe = false; // Not a keyframe unless identified by lookahead
133 sliceType = type;
134 frameNum = poc;
135 leadingBframes = 0;
136 indB = 0;
137 satdCost = (int64_t)-1;
138 memset(costEst, -1, sizeof(costEst));
139 memset(weightedCostDelta, 0, sizeof(weightedCostDelta));
140
141 if (qpAqOffset && invQscaleFactor)
142 memset(costEstAq, -1, sizeof(costEstAq));
143
144 for (int y = 0; y < bframes + 2; y++)
145 for (int x = 0; x < bframes + 2; x++)
146 rowSatds[y][x][0] = -1;
147
148 for (int i = 0; i < bframes + 1; i++)
149 {
150 lowresMvs[0][i][0].x = 0x7FFF;
151 lowresMvs[1][i][0].x = 0x7FFF;
152 }
153
154 for (int i = 0; i < bframes + 2; i++)
155 intraMbs[i] = 0;
156
157 /* downscale and generate 4 hpel planes for lookahead */
158 primitives.frame_init_lowres_core(origPic->m_picOrg[0],
159 lowresPlane[0], lowresPlane[1], lowresPlane[2], lowresPlane[3],
160 origPic->m_stride, lumaStride, width, lines);
161
162 /* extend hpel planes for motion search */
163 extendPicBorder(lowresPlane[0], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY);
164 extendPicBorder(lowresPlane[1], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY);
165 extendPicBorder(lowresPlane[2], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY);
166 extendPicBorder(lowresPlane[3], lumaStride, width, lines, origPic->m_lumaMarginX, origPic->m_lumaMarginY);
167 fpelPlane = lowresPlane[0];
168 }