50bbc89528d2926092643c5aac75875dbc83f398
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Gopu Govindaswamy <gopu@multicorewareinc.com>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
30 bool Lowres::create(PicYuv
*origPic
, int _bframes
, bool bAQEnabled
)
34 width
= origPic
->m_picWidth
/ 2;
35 lines
= origPic
->m_picHeight
/ 2;
36 lumaStride
= width
+ 2 * origPic
->m_lumaMarginX
;
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
;
43 /* rounding the width to multiple of lowres CU size */
44 width
= cuWidth
* X265_LOWRES_CU_SIZE
;
45 lines
= cuHeight
* X265_LOWRES_CU_SIZE
;
47 size_t planesize
= lumaStride
* (lines
+ 2 * origPic
->m_lumaMarginY
);
48 size_t padoffset
= lumaStride
* origPic
->m_lumaMarginY
+ origPic
->m_lumaMarginX
;
52 CHECKED_MALLOC(qpAqOffset
, double, cuCount
);
53 CHECKED_MALLOC(invQscaleFactor
, int, cuCount
);
54 CHECKED_MALLOC(qpCuTreeOffset
, double, cuCount
);
56 CHECKED_MALLOC(propagateCost
, uint16_t, cuCount
);
58 /* allocate lowres buffers */
59 for (int i
= 0; i
< 4; i
++)
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
);
66 lowresPlane
[0] = buffer
[0] + padoffset
;
67 lowresPlane
[1] = buffer
[1] + padoffset
;
68 lowresPlane
[2] = buffer
[2] + padoffset
;
69 lowresPlane
[3] = buffer
[3] + padoffset
;
71 CHECKED_MALLOC(intraCost
, int32_t, cuCount
);
72 CHECKED_MALLOC(intraMode
, uint8_t, cuCount
);
74 for (int i
= 0; i
< bframes
+ 2; i
++)
76 for (int j
= 0; j
< bframes
+ 2; j
++)
78 CHECKED_MALLOC(rowSatds
[i
][j
], int32_t, cuHeight
);
79 CHECKED_MALLOC(lowresCosts
[i
][j
], uint16_t, cuCount
);
83 for (int i
= 0; i
< bframes
+ 1; i
++)
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
);
97 void Lowres::destroy()
99 for (int i
= 0; i
< 4; i
++)
100 X265_FREE(buffer
[i
]);
102 X265_FREE(intraCost
);
103 X265_FREE(intraMode
);
105 for (int i
= 0; i
< bframes
+ 2; i
++)
107 for (int j
= 0; j
< bframes
+ 2; j
++)
109 X265_FREE(rowSatds
[i
][j
]);
110 X265_FREE(lowresCosts
[i
][j
]);
114 for (int i
= 0; i
< bframes
+ 1; i
++)
116 X265_FREE(lowresMvs
[0][i
]);
117 X265_FREE(lowresMvs
[1][i
]);
118 X265_FREE(lowresMvCosts
[0][i
]);
119 X265_FREE(lowresMvCosts
[1][i
]);
122 X265_FREE(qpAqOffset
);
123 X265_FREE(invQscaleFactor
);
124 X265_FREE(qpCuTreeOffset
);
125 X265_FREE(propagateCost
);
128 // (re) initialize lowres state
129 void Lowres::init(PicYuv
*origPic
, int poc
, int type
)
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
139 satdCost
= (int64_t)-1;
140 memset(costEst
, -1, sizeof(costEst
));
141 memset(weightedCostDelta
, 0, sizeof(weightedCostDelta
));
143 if (qpAqOffset
&& invQscaleFactor
)
144 memset(costEstAq
, -1, sizeof(costEstAq
));
146 for (int y
= 0; y
< bframes
+ 2; y
++)
147 for (int x
= 0; x
< bframes
+ 2; x
++)
148 rowSatds
[y
][x
][0] = -1;
150 for (int i
= 0; i
< bframes
+ 1; i
++)
152 lowresMvs
[0][i
][0].x
= 0x7FFF;
153 lowresMvs
[1][i
][0].x
= 0x7FFF;
156 for (int i
= 0; i
< bframes
+ 2; i
++)
159 /* downscale and generate 4 hpel planes for lookahead */
160 primitives
.frameInitLowres(origPic
->m_picOrg
[0],
161 lowresPlane
[0], lowresPlane
[1], lowresPlane
[2], lowresPlane
[3],
162 origPic
->m_stride
, lumaStride
, width
, lines
);
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
);
169 fpelPlane
[0] = lowresPlane
[0];