| 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 | |
| 33 | namespace x265 { |
| 34 | // private namespace |
| 35 | |
| 36 | struct Lowres; |
| 37 | class 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 | |
| 50 | class EstimateRow |
| 51 | { |
| 52 | public: |
| 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); |
| 76 | m_me.setSearchMethod(X265_HEX_SEARCH); |
| 77 | m_me.setSubpelRefine(1); |
| 78 | m_predictions = X265_MALLOC(pixel, 35 * 8 * 8); |
| 79 | m_merange = 16; |
| 80 | m_lookAheadLambda = (int)x265_lambda_tab[X265_LOOKAHEAD_QP]; |
| 81 | } |
| 82 | |
| 83 | ~EstimateRow() |
| 84 | { |
| 85 | X265_FREE(m_predictions); |
| 86 | } |
| 87 | |
| 88 | void init(); |
| 89 | |
| 90 | void estimateCUCost(Lowres * *frames, ReferencePlanes * wfref0, int cux, int cuy, int p0, int p1, int b, bool bDoSearch[2]); |
| 91 | }; |
| 92 | |
| 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 |
| 96 | { |
| 97 | public: |
| 98 | CostEstimate(ThreadPool *p); |
| 99 | ~CostEstimate(); |
| 100 | void init(x265_param *, Frame *); |
| 101 | |
| 102 | x265_param *m_param; |
| 103 | EstimateRow *m_rows; |
| 104 | pixel *m_wbuffer[4]; |
| 105 | Lowres **m_curframes; |
| 106 | |
| 107 | ReferencePlanes m_weightedRef; |
| 108 | WeightParam m_w; |
| 109 | |
| 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 |
| 113 | |
| 114 | bool m_bDoSearch[2]; |
| 115 | volatile bool m_bFrameCompleted; |
| 116 | int m_curb, m_curp0, m_curp1; |
| 117 | |
| 118 | void processRow(int row, int threadId); |
| 119 | int64_t estimateFrameCost(Lowres **frames, int p0, int p1, int b, bool bIntraPenalty); |
| 120 | |
| 121 | protected: |
| 122 | |
| 123 | void weightsAnalyse(Lowres **frames, int b, int p0); |
| 124 | uint32_t weightCostLuma(Lowres **frames, int b, int p0, WeightParam *w); |
| 125 | }; |
| 126 | |
| 127 | class Lookahead : public JobProvider |
| 128 | { |
| 129 | public: |
| 130 | |
| 131 | Lookahead(x265_param *param, ThreadPool *pool); |
| 132 | ~Lookahead(); |
| 133 | void init(); |
| 134 | void destroy(); |
| 135 | |
| 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 |
| 139 | |
| 140 | x265_param *m_param; |
| 141 | Lowres *m_lastNonB; |
| 142 | int *m_scratch; // temp buffer |
| 143 | |
| 144 | int m_widthInCU; // width of lowres frame in downscale CUs |
| 145 | int m_heightInCU; // height of lowres frame in downscale CUs |
| 146 | int m_lastKeyframe; |
| 147 | int m_histogram[X265_BFRAME_MAX + 1]; |
| 148 | |
| 149 | void addPicture(Frame*, int sliceType); |
| 150 | void flush(); |
| 151 | Frame* getDecidedPicture(); |
| 152 | |
| 153 | void getEstimatedPictureCost(Frame *pic); |
| 154 | |
| 155 | protected: |
| 156 | |
| 157 | Lock m_inputQueueLock; |
| 158 | Lock m_outputQueueLock; |
| 159 | Lock m_decideLock; |
| 160 | Event m_outputAvailable; |
| 161 | volatile int m_bReady; |
| 162 | volatile bool m_bFilling; |
| 163 | volatile bool m_bFlushed; |
| 164 | bool findJob(int); |
| 165 | |
| 166 | /* called by addPicture() or flush() to trigger slice decisions */ |
| 167 | void slicetypeDecide(); |
| 168 | void slicetypeAnalyse(Lowres **frames, bool bKeyframe); |
| 169 | |
| 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); |
| 177 | |
| 178 | /* called by slicetypeAnalyse() to effect cuTree adjustments to adaptive |
| 179 | * quant offsets */ |
| 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); |
| 183 | |
| 184 | /* called by getEstimatedPictureCost() to finalize cuTree costs */ |
| 185 | int64_t frameCostRecalculate(Lowres **frames, int p0, int p1, int b); |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | #endif // ifndef X265_SLICETYPE_H |