| 1 | /***************************************************************************** |
| 2 | * Copyright (C) 2013 x265 project |
| 3 | * |
| 4 | * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com> |
| 5 | * Steve Borho <steve@borho.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
| 20 | * |
| 21 | * This program is also available under a commercial proprietary license. |
| 22 | * For more information, contact us at license @ x265.com. |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | #ifndef X265_ANALYSIS_H |
| 26 | #define X265_ANALYSIS_H |
| 27 | |
| 28 | #include "common.h" |
| 29 | #include "predict.h" |
| 30 | #include "quant.h" |
| 31 | #include "yuv.h" |
| 32 | #include "shortyuv.h" |
| 33 | #include "cudata.h" |
| 34 | |
| 35 | #include "entropy.h" |
| 36 | #include "search.h" |
| 37 | |
| 38 | namespace x265 { |
| 39 | // private namespace |
| 40 | |
| 41 | class Entropy; |
| 42 | |
| 43 | class Analysis : public Search |
| 44 | { |
| 45 | public: |
| 46 | |
| 47 | enum { |
| 48 | PRED_MERGE, |
| 49 | PRED_SKIP, |
| 50 | PRED_INTRA, |
| 51 | PRED_2Nx2N, |
| 52 | PRED_BIDIR, |
| 53 | PRED_Nx2N, |
| 54 | PRED_2NxN, |
| 55 | PRED_SPLIT, |
| 56 | PRED_2NxnU, |
| 57 | PRED_2NxnD, |
| 58 | PRED_nLx2N, |
| 59 | PRED_nRx2N, |
| 60 | PRED_INTRA_NxN, /* 4x4 intra PU blocks for 8x8 CU */ |
| 61 | PRED_LOSSLESS, /* lossless encode of best mode */ |
| 62 | MAX_PRED_TYPES |
| 63 | }; |
| 64 | |
| 65 | struct ModeDepth |
| 66 | { |
| 67 | Mode pred[MAX_PRED_TYPES]; |
| 68 | Mode* bestMode; |
| 69 | Yuv fencYuv; |
| 70 | CUDataMemPool cuMemPool; |
| 71 | }; |
| 72 | |
| 73 | ModeDepth m_modeDepth[NUM_CU_DEPTH]; |
| 74 | bool m_bTryLossless; |
| 75 | bool m_bChromaSa8d; |
| 76 | |
| 77 | /* Analysis data for load/save modes, keeps getting incremented as CTU analysis proceeds and data is consumed or read */ |
| 78 | analysis_intra_data* m_reuseIntraDataCTU; |
| 79 | analysis_inter_data* m_reuseInterDataCTU; |
| 80 | Analysis(); |
| 81 | bool create(ThreadLocalData* tld); |
| 82 | void destroy(); |
| 83 | Mode& compressCTU(CUData& ctu, Frame& frame, const CUGeom& cuGeom, const Entropy& initialContext); |
| 84 | |
| 85 | protected: |
| 86 | |
| 87 | /* mode analysis distribution */ |
| 88 | int m_totalNumJobs; |
| 89 | volatile int m_numAcquiredJobs; |
| 90 | volatile int m_numCompletedJobs; |
| 91 | Lock m_pmodeLock; |
| 92 | Event m_modeCompletionEvent; |
| 93 | bool findJob(int threadId); |
| 94 | void parallelModeAnalysis(int threadId, int jobId); |
| 95 | void parallelME(int threadId, int meId); |
| 96 | |
| 97 | /* full analysis for an I-slice CU */ |
| 98 | void compressIntraCU(const CUData& parentCTU, const CUGeom& cuGeom, uint32_t &zOrder); |
| 99 | |
| 100 | /* full analysis for a P or B slice CU */ |
| 101 | void compressInterCU_dist(const CUData& parentCTU, const CUGeom& cuGeom); |
| 102 | void compressInterCU_rd0_4(const CUData& parentCTU, const CUGeom& cuGeom); |
| 103 | void compressInterCU_rd5_6(const CUData& parentCTU, const CUGeom& cuGeom); |
| 104 | |
| 105 | /* measure merge and skip */ |
| 106 | void checkMerge2Nx2N_rd0_4(Mode& skip, Mode& merge, const CUGeom& cuGeom); |
| 107 | void checkMerge2Nx2N_rd5_6(Mode& skip, Mode& merge, const CUGeom& cuGeom); |
| 108 | |
| 109 | /* measure inter options */ |
| 110 | void checkInter_rd0_4(Mode& interMode, const CUGeom& cuGeom, PartSize partSize); |
| 111 | void checkInter_rd5_6(Mode& interMode, const CUGeom& cuGeom, PartSize partSize, bool bMergeOnly); |
| 112 | |
| 113 | void checkBidir2Nx2N(Mode& inter2Nx2N, Mode& bidir2Nx2N, const CUGeom& cuGeom); |
| 114 | |
| 115 | /* encode current bestMode losslessly, pick best RD cost */ |
| 116 | void tryLossless(const CUGeom& cuGeom); |
| 117 | |
| 118 | /* add the RD cost of coding a split flag (0 or 1) to the given mode */ |
| 119 | void addSplitFlagCost(Mode& mode, uint32_t depth); |
| 120 | |
| 121 | /* update CBF flags and QP values to be internally consistent */ |
| 122 | void checkDQP(CUData& cu, const CUGeom& cuGeom); |
| 123 | |
| 124 | /* work-avoidance heuristics for RD levels < 5 */ |
| 125 | uint32_t topSkipMinDepth(const CUData& parentCTU, const CUGeom& cuGeom); |
| 126 | bool recursionDepthCheck(const CUData& parentCTU, const CUGeom& cuGeom, const Mode& bestMode); |
| 127 | |
| 128 | /* generate residual and recon pixels for an entire CTU recursively (RD0) */ |
| 129 | void encodeResidue(const CUData& parentCTU, const CUGeom& cuGeom); |
| 130 | |
| 131 | /* check whether current mode is the new best */ |
| 132 | inline void checkBestMode(Mode& mode, uint32_t depth) |
| 133 | { |
| 134 | ModeDepth& md = m_modeDepth[depth]; |
| 135 | if (md.bestMode) |
| 136 | { |
| 137 | if (mode.rdCost < md.bestMode->rdCost) |
| 138 | md.bestMode = &mode; |
| 139 | } |
| 140 | else |
| 141 | md.bestMode = &mode; |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | struct ThreadLocalData |
| 146 | { |
| 147 | Analysis analysis; |
| 148 | |
| 149 | void destroy() { analysis.destroy(); } |
| 150 | }; |
| 151 | |
| 152 | } |
| 153 | |
| 154 | #endif // ifndef X265_ANALYSIS_H |