| 1 | /***************************************************************************** |
| 2 | * Copyright (C) 2013 x265 project |
| 3 | * |
| 4 | * Authors: Steve Borho <steve@borho.org> |
| 5 | * Min Chen <chenm003@163.com> |
| 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_SAO_H |
| 26 | #define X265_SAO_H |
| 27 | |
| 28 | #include "common.h" |
| 29 | #include "frame.h" |
| 30 | #include "entropy.h" |
| 31 | |
| 32 | namespace x265 { |
| 33 | // private namespace |
| 34 | |
| 35 | enum SAOTypeLen |
| 36 | { |
| 37 | SAO_EO_LEN = 4, |
| 38 | SAO_BO_LEN = 4, |
| 39 | SAO_NUM_BO_CLASSES = 32 |
| 40 | }; |
| 41 | |
| 42 | enum SAOType |
| 43 | { |
| 44 | SAO_EO_0 = 0, |
| 45 | SAO_EO_1, |
| 46 | SAO_EO_2, |
| 47 | SAO_EO_3, |
| 48 | SAO_BO, |
| 49 | MAX_NUM_SAO_TYPE |
| 50 | }; |
| 51 | |
| 52 | class SAO |
| 53 | { |
| 54 | protected: |
| 55 | |
| 56 | enum { SAO_MAX_DEPTH = 4 }; |
| 57 | enum { SAO_BO_BITS = 5 }; |
| 58 | enum { MAX_NUM_SAO_CLASS = 33 }; |
| 59 | enum { SAO_BIT_INC = X265_MAX(X265_DEPTH - 10, 0) }; |
| 60 | enum { OFFSET_THRESH = 1 << X265_MIN(X265_DEPTH - 5, 5) }; |
| 61 | enum { NUM_EDGETYPE = 5 }; |
| 62 | enum { NUM_PLANE = 3 }; |
| 63 | enum { NUM_MERGE_MODE = 3 }; |
| 64 | |
| 65 | static const uint32_t s_eoTable[NUM_EDGETYPE]; |
| 66 | |
| 67 | typedef int32_t (PerClass[MAX_NUM_SAO_TYPE][MAX_NUM_SAO_CLASS]); |
| 68 | typedef int32_t (PerPlane[NUM_PLANE][MAX_NUM_SAO_TYPE][MAX_NUM_SAO_CLASS]); |
| 69 | |
| 70 | /* allocated per part */ |
| 71 | PerClass* m_count; |
| 72 | PerClass* m_offset; |
| 73 | PerClass* m_offsetOrg; |
| 74 | |
| 75 | /* allocated per CTU */ |
| 76 | PerPlane* m_countPreDblk; |
| 77 | PerPlane* m_offsetOrgPreDblk; |
| 78 | |
| 79 | double m_depthSaoRate[2][4]; |
| 80 | pixel* m_offsetBo; |
| 81 | int8_t m_offsetEo[NUM_EDGETYPE]; |
| 82 | |
| 83 | int m_numCuInWidth; |
| 84 | int m_numCuInHeight; |
| 85 | int m_hChromaShift; |
| 86 | int m_vChromaShift; |
| 87 | |
| 88 | pixel* m_clipTable; |
| 89 | pixel* m_clipTableBase; |
| 90 | |
| 91 | pixel* m_tmpU1[3]; |
| 92 | pixel* m_tmpU2[3]; |
| 93 | pixel* m_tmpL1; |
| 94 | pixel* m_tmpL2; |
| 95 | |
| 96 | public: |
| 97 | |
| 98 | struct SAOContexts |
| 99 | { |
| 100 | Entropy cur; |
| 101 | Entropy next; |
| 102 | Entropy temp; |
| 103 | }; |
| 104 | |
| 105 | Frame* m_frame; |
| 106 | Entropy m_entropyCoder; |
| 107 | SAOContexts m_rdContexts; |
| 108 | |
| 109 | x265_param* m_param; |
| 110 | int m_refDepth; |
| 111 | int m_numNoSao[2]; |
| 112 | |
| 113 | double m_lumaLambda; |
| 114 | double m_chromaLambda; |
| 115 | /* TODO: No doubles for distortion */ |
| 116 | |
| 117 | SAO(); |
| 118 | |
| 119 | bool create(x265_param* param); |
| 120 | void destroy(); |
| 121 | |
| 122 | void allocSaoParam(SAOParam* saoParam) const; |
| 123 | |
| 124 | void startSlice(Frame* pic, Entropy& initState, int qp); |
| 125 | void resetStats(); |
| 126 | void resetSaoUnit(SaoCtuParam* saoUnit); |
| 127 | |
| 128 | // CTU-based SAO process without slice granularity |
| 129 | void processSaoCu(int addr, int typeIdx, int plane); |
| 130 | void processSaoUnitRow(SaoCtuParam* ctuParam, int idxY, int plane); |
| 131 | |
| 132 | void copySaoUnit(SaoCtuParam* saoUnitDst, const SaoCtuParam* saoUnitSrc); |
| 133 | |
| 134 | void calcSaoStatsCu(int addr, int plane); |
| 135 | void calcSaoStatsCu_BeforeDblk(Frame* pic, int idxX, int idxY); |
| 136 | |
| 137 | void saoComponentParamDist(SAOParam* saoParam, int addr, int addrUp, int addrLeft, SaoCtuParam mergeSaoParam[2], double* mergeDist); |
| 138 | void sao2ChromaParamDist(SAOParam* saoParam, int addr, int addrUp, int addrLeft, SaoCtuParam mergeSaoParam[][2], double* mergeDist); |
| 139 | |
| 140 | inline int estIterOffset(int typeIdx, int classIdx, double lambda, int offset, int32_t count, int32_t offsetOrg, |
| 141 | int32_t* currentDistortionTableBo, double* currentRdCostTableBo); |
| 142 | inline int64_t estSaoTypeDist(int plane, int typeIdx, double lambda, int32_t* currentDistortionTableBo, double* currentRdCostTableBo); |
| 143 | |
| 144 | void rdoSaoUnitRowInit(SAOParam* saoParam); |
| 145 | void rdoSaoUnitRowEnd(const SAOParam* saoParam, int numctus); |
| 146 | void rdoSaoUnitRow(SAOParam* saoParam, int idxY); |
| 147 | }; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | #endif // ifndef X265_SAO_H |