b88ad3e07af18a9d547f525aca1cbd6344c6f424
[deb_x265.git] / source / common / lowres.h
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 #ifndef X265_LOWRES_H
25 #define X265_LOWRES_H
26
27 #include "primitives.h"
28 #include "common.h"
29 #include "mv.h"
30
31 namespace x265 {
32 // private namespace
33
34 class PicYuv;
35
36 struct ReferencePlanes
37 {
38 ReferencePlanes() { memset(this, 0, sizeof(ReferencePlanes)); }
39
40 pixel* fpelPlane;
41 pixel* lowresPlane[4];
42
43 bool isWeighted;
44 bool isLowres;
45 intptr_t lumaStride;
46 int weight;
47 int offset;
48 int shift;
49 int round;
50
51 /* lowres motion compensation, you must provide a buffer and stride for QPEL averaged pixels
52 * in case QPEL is required. Else it returns a pointer to the HPEL pixels */
53 inline pixel *lowresMC(intptr_t blockOffset, const MV& qmv, pixel *buf, intptr_t& outstride)
54 {
55 if ((qmv.x | qmv.y) & 1)
56 {
57 int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1);
58 pixel *frefA = lowresPlane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride;
59
60 MV qmvB = qmv + MV((qmv.x & 1) * 2, (qmv.y & 1) * 2);
61 int hpelB = (qmvB.y & 2) | ((qmvB.x & 2) >> 1);
62
63 pixel *frefB = lowresPlane[hpelB] + blockOffset + (qmvB.x >> 2) + (qmvB.y >> 2) * lumaStride;
64 primitives.pixelavg_pp[LUMA_8x8](buf, outstride, frefA, lumaStride, frefB, lumaStride, 32);
65 return buf;
66 }
67 else
68 {
69 outstride = lumaStride;
70 int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1);
71 return lowresPlane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride;
72 }
73 }
74
75 inline int lowresQPelCost(pixel *fenc, intptr_t blockOffset, const MV& qmv, pixelcmp_t comp)
76 {
77 if ((qmv.x | qmv.y) & 1)
78 {
79 ALIGN_VAR_16(pixel, subpelbuf[8 * 8]);
80 int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1);
81 pixel *frefA = lowresPlane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride;
82 MV qmvB = qmv + MV((qmv.x & 1) * 2, (qmv.y & 1) * 2);
83 int hpelB = (qmvB.y & 2) | ((qmvB.x & 2) >> 1);
84 pixel *frefB = lowresPlane[hpelB] + blockOffset + (qmvB.x >> 2) + (qmvB.y >> 2) * lumaStride;
85 primitives.pixelavg_pp[LUMA_8x8](subpelbuf, 8, frefA, lumaStride, frefB, lumaStride, 32);
86 return comp(fenc, FENC_STRIDE, subpelbuf, 8);
87 }
88 else
89 {
90 int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1);
91 pixel *fref = lowresPlane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride;
92 return comp(fenc, FENC_STRIDE, fref, lumaStride);
93 }
94 }
95 };
96
97 /* lowres buffers, sizes and strides */
98 struct Lowres : public ReferencePlanes
99 {
100 pixel *buffer[4];
101
102 int frameNum; // Presentation frame number
103 int sliceType; // Slice type decided by lookahead
104 int width; // width of lowres frame in pixels
105 int lines; // height of lowres frame in pixel lines
106 int leadingBframes; // number of leading B frames for P or I
107
108 bool bIntraCalculated;
109 bool bScenecut; // Set to false if the frame cannot possibly be part of a real scenecut.
110 bool bKeyframe;
111 bool bLastMiniGopBFrame;
112
113 /* lookahead output data */
114 int64_t costEst[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
115 int64_t costEstAq[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
116 int32_t* rowSatds[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
117 int intraMbs[X265_BFRAME_MAX + 2];
118 int32_t* intraCost;
119 int64_t satdCost;
120 uint16_t* lowresCostForRc;
121 uint16_t(*lowresCosts[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]);
122 int32_t* lowresMvCosts[2][X265_BFRAME_MAX + 1];
123 MV* lowresMvs[2][X265_BFRAME_MAX + 1];
124
125 /* used for vbvLookahead */
126 int plannedType[X265_LOOKAHEAD_MAX + 1];
127 int64_t plannedSatd[X265_LOOKAHEAD_MAX + 1];
128 int indB;
129 int bframes;
130
131 /* rate control / adaptive quant data */
132 double* qpAqOffset; // AQ QP offset values for each 16x16 CU
133 double* qpCuTreeOffset; // cuTree QP offset values for each 16x16 CU
134 int* invQscaleFactor; // qScale values for qp Aq Offsets
135 uint64_t wp_ssd[3]; // This is different than SSDY, this is sum(pixel^2) - sum(pixel)^2 for entire frame
136 uint64_t wp_sum[3];
137
138 /* cutree intermediate data */
139 uint16_t* propagateCost;
140 double weightedCostDelta[X265_BFRAME_MAX + 2];
141
142 bool create(PicYuv *origPic, int _bframes, bool bAqEnabled);
143 void destroy();
144 void init(PicYuv *origPic, int poc, int sliceType);
145 };
146 }
147
148 #endif // ifndef X265_LOWRES_H