Imported Upstream version 1.4+222+hg5f9f7194267b
[deb_x265.git] / source / encoder / slicetype.h
CommitLineData
72b9787e
JB
1/*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Steve Borho <steve@borho.org>
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_SLICETYPE_H
25#define X265_SLICETYPE_H
26
27#include "common.h"
28#include "slice.h"
29#include "motion.h"
30#include "piclist.h"
31#include "wavefront.h"
32
33namespace x265 {
34// private namespace
35
36struct Lowres;
37class Frame;
38
39#define LOWRES_COST_MASK ((1 << 14) - 1)
40#define LOWRES_COST_SHIFT 14
41
42#define SET_WEIGHT(w, b, s, d, o) \
43 { \
44 (w).inputWeight = (s); \
45 (w).log2WeightDenom = (d); \
46 (w).inputOffset = (o); \
47 (w).bPresentFlag = b; \
48 }
49
50class EstimateRow
51{
52public:
53 x265_param* m_param;
54 MotionEstimate m_me;
55 Lock m_lock;
56 pixel* m_predictions; // buffer for 35 intra predictions
57
58 volatile uint32_t m_completed; // Number of CUs in this row for which cost estimation is completed
59 volatile bool m_active;
60
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
66
67 int m_merange;
68 int m_lookAheadLambda;
69
70 int m_widthInCU;
71 int m_heightInCU;
72
73 EstimateRow()
74 {
75 m_me.setQP(X265_LOOKAHEAD_QP);
b53f7c52 76 m_me.init(X265_HEX_SEARCH, 1, X265_CSP_I400);
72b9787e
JB
77 m_predictions = X265_MALLOC(pixel, 35 * 8 * 8);
78 m_merange = 16;
79 m_lookAheadLambda = (int)x265_lambda_tab[X265_LOOKAHEAD_QP];
80 }
81
82 ~EstimateRow()
83 {
84 X265_FREE(m_predictions);
85 }
86
87 void init();
88
89 void estimateCUCost(Lowres * *frames, ReferencePlanes * wfref0, int cux, int cuy, int p0, int p1, int b, bool bDoSearch[2]);
90};
91
92/* CostEstimate manages the cost estimation of a single frame, ie:
93 * estimateFrameCost() and everything below it in the call graph */
94class CostEstimate : public WaveFront
95{
96public:
97 CostEstimate(ThreadPool *p);
98 ~CostEstimate();
99 void init(x265_param *, Frame *);
100
101 x265_param *m_param;
102 EstimateRow *m_rows;
103 pixel *m_wbuffer[4];
104 Lowres **m_curframes;
105
106 ReferencePlanes m_weightedRef;
107 WeightParam m_w;
108
109 int m_paddedLines; // number of lines in padded frame
110 int m_widthInCU; // width of lowres frame in downscale CUs
111 int m_heightInCU; // height of lowres frame in downscale CUs
112
113 bool m_bDoSearch[2];
114 volatile bool m_bFrameCompleted;
115 int m_curb, m_curp0, m_curp1;
116
117 void processRow(int row, int threadId);
118 int64_t estimateFrameCost(Lowres **frames, int p0, int p1, int b, bool bIntraPenalty);
119
120protected:
121
122 void weightsAnalyse(Lowres **frames, int b, int p0);
123 uint32_t weightCostLuma(Lowres **frames, int b, int p0, WeightParam *w);
124};
125
126class Lookahead : public JobProvider
127{
128public:
129
130 Lookahead(x265_param *param, ThreadPool *pool);
131 ~Lookahead();
132 void init();
133 void destroy();
134
135 CostEstimate m_est; // Frame cost estimator
136 PicList m_inputQueue; // input pictures in order received
137 PicList m_outputQueue; // pictures to be encoded, in encode order
138
139 x265_param *m_param;
140 Lowres *m_lastNonB;
141 int *m_scratch; // temp buffer
142
143 int m_widthInCU; // width of lowres frame in downscale CUs
144 int m_heightInCU; // height of lowres frame in downscale CUs
145 int m_lastKeyframe;
146 int m_histogram[X265_BFRAME_MAX + 1];
147
148 void addPicture(Frame*, int sliceType);
149 void flush();
150 Frame* getDecidedPicture();
151
152 void getEstimatedPictureCost(Frame *pic);
153
154protected:
155
156 Lock m_inputQueueLock;
157 Lock m_outputQueueLock;
158 Lock m_decideLock;
159 Event m_outputAvailable;
160 volatile int m_bReady;
161 volatile bool m_bFilling;
162 volatile bool m_bFlushed;
163 bool findJob(int);
164
165 /* called by addPicture() or flush() to trigger slice decisions */
166 void slicetypeDecide();
167 void slicetypeAnalyse(Lowres **frames, bool bKeyframe);
168
169 /* called by slicetypeAnalyse() to make slice decisions */
170 bool scenecut(Lowres **frames, int p0, int p1, bool bRealScenecut, int numFrames, int maxSearch);
171 bool scenecutInternal(Lowres **frames, int p0, int p1, bool bRealScenecut);
172 void slicetypePath(Lowres **frames, int length, char(*best_paths)[X265_LOOKAHEAD_MAX + 1]);
173 int64_t slicetypePathCost(Lowres **frames, char *path, int64_t threshold);
174 int64_t vbvFrameCost(Lowres **frames, int p0, int p1, int b);
175 void vbvLookahead(Lowres **frames, int numFrames, int keyframes);
176
177 /* called by slicetypeAnalyse() to effect cuTree adjustments to adaptive
178 * quant offsets */
179 void cuTree(Lowres **frames, int numframes, bool bintra);
180 void estimateCUPropagate(Lowres **frames, double average_duration, int p0, int p1, int b, int referenced);
181 void cuTreeFinish(Lowres *frame, double averageDuration, int ref0Distance);
182
183 /* called by getEstimatedPictureCost() to finalize cuTree costs */
184 int64_t frameCostRecalculate(Lowres **frames, int p0, int p1, int b);
185};
186}
187
188#endif // ifndef X265_SLICETYPE_H