1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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 *****************************************************************************/
24 #ifndef X265_SLICETYPE_H
25 #define X265_SLICETYPE_H
31 #include "wavefront.h"
39 #define LOWRES_COST_MASK ((1 << 14) - 1)
40 #define LOWRES_COST_SHIFT 14
42 #define SET_WEIGHT(w, b, s, d, o) \
44 (w).inputWeight = (s); \
45 (w).log2WeightDenom = (d); \
46 (w).inputOffset = (o); \
47 (w).bPresentFlag = b; \
56 pixel
* m_predictions
; // buffer for 35 intra predictions
58 volatile uint32_t m_completed
; // Number of CUs in this row for which cost estimation is completed
59 volatile bool m_active
;
61 uint64_t m_costEst
; // Estimated cost for all CUs in a row
62 uint64_t m_costEstAq
; // Estimated weight Aq cost for all CUs in a row
63 uint64_t m_costIntraAq
; // Estimated weighted Aq Intra cost for all CUs in a row
64 int m_intraMbs
; // Number of Intra CUs
65 int m_costIntra
; // Estimated Intra cost for all CUs in a row
68 int m_lookAheadLambda
;
75 m_me
.setQP(X265_LOOKAHEAD_QP
);
76 m_me
.setSearchMethod(X265_HEX_SEARCH
);
77 m_me
.setSubpelRefine(1);
78 m_predictions
= X265_MALLOC(pixel
, 35 * 8 * 8);
80 m_lookAheadLambda
= (int)x265_lambda_tab
[X265_LOOKAHEAD_QP
];
85 X265_FREE(m_predictions
);
90 void estimateCUCost(Lowres
* *frames
, ReferencePlanes
* wfref0
, int cux
, int cuy
, int p0
, int p1
, int b
, bool bDoSearch
[2]);
93 /* CostEstimate manages the cost estimation of a single frame, ie:
94 * estimateFrameCost() and everything below it in the call graph */
95 class CostEstimate
: public WaveFront
98 CostEstimate(ThreadPool
*p
);
100 void init(x265_param
*, Frame
*);
105 Lowres
**m_curframes
;
107 ReferencePlanes m_weightedRef
;
110 int m_paddedLines
; // number of lines in padded frame
111 int m_widthInCU
; // width of lowres frame in downscale CUs
112 int m_heightInCU
; // height of lowres frame in downscale CUs
115 volatile bool m_bFrameCompleted
;
116 int m_curb
, m_curp0
, m_curp1
;
118 void processRow(int row
, int threadId
);
119 int64_t estimateFrameCost(Lowres
**frames
, int p0
, int p1
, int b
, bool bIntraPenalty
);
123 void weightsAnalyse(Lowres
**frames
, int b
, int p0
);
124 uint32_t weightCostLuma(Lowres
**frames
, int b
, int p0
, WeightParam
*w
);
127 class Lookahead
: public JobProvider
131 Lookahead(x265_param
*param
, ThreadPool
*pool
);
136 CostEstimate m_est
; // Frame cost estimator
137 PicList m_inputQueue
; // input pictures in order received
138 PicList m_outputQueue
; // pictures to be encoded, in encode order
142 int *m_scratch
; // temp buffer
144 int m_widthInCU
; // width of lowres frame in downscale CUs
145 int m_heightInCU
; // height of lowres frame in downscale CUs
147 int m_histogram
[X265_BFRAME_MAX
+ 1];
149 void addPicture(Frame
*, int sliceType
);
151 Frame
* getDecidedPicture();
153 void getEstimatedPictureCost(Frame
*pic
);
157 Lock m_inputQueueLock
;
158 Lock m_outputQueueLock
;
160 Event m_outputAvailable
;
161 volatile int m_bReady
;
162 volatile bool m_bFilling
;
163 volatile bool m_bFlushed
;
166 /* called by addPicture() or flush() to trigger slice decisions */
167 void slicetypeDecide();
168 void slicetypeAnalyse(Lowres
**frames
, bool bKeyframe
);
170 /* called by slicetypeAnalyse() to make slice decisions */
171 bool scenecut(Lowres
**frames
, int p0
, int p1
, bool bRealScenecut
, int numFrames
, int maxSearch
);
172 bool scenecutInternal(Lowres
**frames
, int p0
, int p1
, bool bRealScenecut
);
173 void slicetypePath(Lowres
**frames
, int length
, char(*best_paths
)[X265_LOOKAHEAD_MAX
+ 1]);
174 int64_t slicetypePathCost(Lowres
**frames
, char *path
, int64_t threshold
);
175 int64_t vbvFrameCost(Lowres
**frames
, int p0
, int p1
, int b
);
176 void vbvLookahead(Lowres
**frames
, int numFrames
, int keyframes
);
178 /* called by slicetypeAnalyse() to effect cuTree adjustments to adaptive
180 void cuTree(Lowres
**frames
, int numframes
, bool bintra
);
181 void estimateCUPropagate(Lowres
**frames
, double average_duration
, int p0
, int p1
, int b
, int referenced
);
182 void cuTreeFinish(Lowres
*frame
, double averageDuration
, int ref0Distance
);
184 /* called by getEstimatedPictureCost() to finalize cuTree costs */
185 int64_t frameCostRecalculate(Lowres
**frames
, int p0
, int p1
, int b
);
189 #endif // ifndef X265_SLICETYPE_H