[54] | 1 | /** \file TAppEncLayerCfg.cpp |
---|
| 2 | \brief Handle encoder configuration parameters |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <cassert> |
---|
| 7 | #include <cstring> |
---|
| 8 | #include <string> |
---|
| 9 | #include "TLibCommon/TComRom.h" |
---|
| 10 | #include "TAppEncCfg.h" |
---|
| 11 | #include "TAppEncLayerCfg.h" |
---|
| 12 | #include "TAppCommon/program_options_lite.h" |
---|
| 13 | |
---|
| 14 | #ifdef WIN32 |
---|
| 15 | #define strdup _strdup |
---|
| 16 | #endif |
---|
| 17 | |
---|
| 18 | using namespace std; |
---|
| 19 | namespace po = df::program_options_lite; |
---|
| 20 | |
---|
| 21 | //! \ingroup TAppEncoder |
---|
| 22 | //! \{ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | // ==================================================================================================================== |
---|
| 26 | // Constructor / destructor / initialization / destroy |
---|
| 27 | // ==================================================================================================================== |
---|
| 28 | #if SVC_EXTENSION |
---|
| 29 | TAppEncLayerCfg::TAppEncLayerCfg() |
---|
| 30 | :m_cInputFile(string("")), |
---|
| 31 | m_cReconFile(string("")), |
---|
| 32 | m_conformanceMode( 0 ), |
---|
| 33 | m_aidQP(NULL) |
---|
| 34 | { |
---|
[87] | 35 | m_confLeft = m_confRight = m_confTop = m_confBottom = 0; |
---|
[54] | 36 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | TAppEncLayerCfg::~TAppEncLayerCfg() |
---|
| 40 | { |
---|
| 41 | if ( m_aidQP ) |
---|
| 42 | { |
---|
| 43 | delete[] m_aidQP; |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | Void TAppEncLayerCfg::create() |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | Void TAppEncLayerCfg::destroy() |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | // ==================================================================================================================== |
---|
| 57 | // Public member functions |
---|
| 58 | // ==================================================================================================================== |
---|
| 59 | |
---|
| 60 | /** \param argc number of arguments |
---|
| 61 | \param argv array of arguments |
---|
| 62 | \retval true when success |
---|
| 63 | */ |
---|
| 64 | bool TAppEncLayerCfg::parseCfg( const string& cfgFileName ) |
---|
| 65 | { |
---|
| 66 | string cfg_InputFile; |
---|
| 67 | string cfg_ReconFile; |
---|
| 68 | string cfg_dQPFile; |
---|
| 69 | po::Options opts; |
---|
| 70 | opts.addOptions() |
---|
| 71 | ("InputFile,i", cfg_InputFile, string(""), "original YUV input file name") |
---|
| 72 | #if AVC_BASE |
---|
| 73 | ("InputBLFile,-ibl", cfg_InputFile, string(""), "original YUV input file name") |
---|
| 74 | #endif |
---|
| 75 | ("ReconFile,o", cfg_ReconFile, string(""), "reconstructed YUV output file name") |
---|
| 76 | ("SourceWidth,-wdt", m_iSourceWidth, 0, "Source picture width") |
---|
| 77 | ("SourceHeight,-hgt", m_iSourceHeight, 0, "Source picture height") |
---|
| 78 | ("CroppingMode", m_conformanceMode, 0, "Cropping mode (0: no cropping, 1:automatic padding, 2: padding, 3:cropping") |
---|
| 79 | ("CropLeft", m_confLeft, 0, "Left cropping/padding for cropping mode 3") |
---|
| 80 | ("CropRight", m_confRight, 0, "Right cropping/padding for cropping mode 3") |
---|
| 81 | ("CropTop", m_confTop, 0, "Top cropping/padding for cropping mode 3") |
---|
| 82 | ("CropBottom", m_confBottom, 0, "Bottom cropping/padding for cropping mode 3") |
---|
| 83 | ("HorizontalPadding,-pdx",m_aiPad[0], 0, "horizontal source padding for cropping mode 2") |
---|
| 84 | ("VerticalPadding,-pdy", m_aiPad[1], 0, "vertical source padding for cropping mode 2") |
---|
| 85 | ("IntraPeriod,-ip", m_iIntraPeriod, -1, "intra period in frames, (-1: only first frame)") |
---|
| 86 | ("FrameRate,-fr", m_iFrameRate, 0, "Frame rate") |
---|
| 87 | ("dQPFile,m", cfg_dQPFile, string(""), "dQP file name") |
---|
| 88 | ("QP,q", m_fQP, 30.0, "Qp value, if value is float, QP is switched once during encoding") |
---|
| 89 | ; |
---|
| 90 | |
---|
| 91 | po::setDefaults(opts); |
---|
| 92 | po::parseConfigFile(opts, cfgFileName); |
---|
| 93 | |
---|
| 94 | m_cInputFile = cfg_InputFile; |
---|
| 95 | m_cReconFile = cfg_ReconFile; |
---|
| 96 | m_pchdQPFile = cfg_dQPFile.empty() ? NULL : strdup(cfg_dQPFile.c_str()); |
---|
| 97 | |
---|
| 98 | // reading external dQP description from file |
---|
| 99 | if ( m_pchdQPFile ) |
---|
| 100 | { |
---|
| 101 | FILE* fpt=fopen( m_pchdQPFile, "r" ); |
---|
| 102 | if ( fpt ) |
---|
| 103 | { |
---|
| 104 | Int iValue; |
---|
| 105 | Int iPOC = 0; |
---|
| 106 | while ( iPOC < m_cAppEncCfg->getNumFrameToBeEncoded() ) |
---|
| 107 | { |
---|
| 108 | if ( fscanf(fpt, "%d", &iValue ) == EOF ) break; |
---|
| 109 | m_aidQP[ iPOC ] = iValue; |
---|
| 110 | iPOC++; |
---|
| 111 | } |
---|
| 112 | fclose(fpt); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | return true; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | Void TAppEncLayerCfg::xPrintParameter() |
---|
| 119 | { |
---|
| 120 | printf("Input File : %s\n", m_cInputFile.c_str() ); |
---|
| 121 | printf("Reconstruction File : %s\n", m_cReconFile.c_str() ); |
---|
| 122 | #if AVC_SYNTAX |
---|
| 123 | printf("Base layer input file : %s\n", m_cAppEncCfg->getBLSyntaxFile() ); |
---|
| 124 | #endif |
---|
| 125 | printf("Real Format : %dx%d %dHz\n", m_iSourceWidth - m_confLeft - m_confRight, m_iSourceHeight - m_confTop - m_confBottom, m_iFrameRate ); |
---|
| 126 | printf("Internal Format : %dx%d %dHz\n", m_iSourceWidth, m_iSourceHeight, m_iFrameRate ); |
---|
| 127 | printf("QP : %5.2f\n", m_fQP ); |
---|
| 128 | printf("Intra period : %d\n", m_iIntraPeriod ); |
---|
| 129 | printf("WaveFrontSynchro:%d WaveFrontSubstreams:%d", m_cAppEncCfg->getWaveFrontSynchro(), m_iWaveFrontSubstreams); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | Bool confirmPara(Bool bflag, const char* message); |
---|
| 133 | |
---|
| 134 | Bool TAppEncLayerCfg::xCheckParameter() |
---|
| 135 | { |
---|
| 136 | switch (m_conformanceMode) |
---|
| 137 | { |
---|
| 138 | case 0: |
---|
| 139 | { |
---|
| 140 | // no cropping or padding |
---|
| 141 | m_confLeft = m_confRight = m_confTop = m_confBottom = 0; |
---|
| 142 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
| 143 | break; |
---|
| 144 | } |
---|
| 145 | case 1: |
---|
| 146 | { |
---|
| 147 | // automatic padding to minimum CU size |
---|
| 148 | Int minCuSize = m_cAppEncCfg->getMaxCUHeight() >> (m_cAppEncCfg->getMaxCUDepth() - 1); |
---|
| 149 | if (m_iSourceWidth % minCuSize) |
---|
| 150 | { |
---|
| 151 | m_aiPad[0] = m_confRight = ((m_iSourceWidth / minCuSize) + 1) * minCuSize - m_iSourceWidth; |
---|
| 152 | m_iSourceWidth += m_confRight; |
---|
| 153 | } |
---|
| 154 | if (m_iSourceHeight % minCuSize) |
---|
| 155 | { |
---|
| 156 | m_aiPad[1] = m_confBottom = ((m_iSourceHeight / minCuSize) + 1) * minCuSize - m_iSourceHeight; |
---|
| 157 | m_iSourceHeight += m_confBottom; |
---|
| 158 | } |
---|
| 159 | break; |
---|
| 160 | } |
---|
| 161 | case 2: |
---|
| 162 | { |
---|
| 163 | //padding |
---|
| 164 | m_iSourceWidth += m_aiPad[0]; |
---|
| 165 | m_iSourceHeight += m_aiPad[1]; |
---|
| 166 | m_confRight = m_aiPad[0]; |
---|
| 167 | m_confBottom = m_aiPad[1]; |
---|
| 168 | break; |
---|
| 169 | } |
---|
| 170 | case 3: |
---|
| 171 | { |
---|
| 172 | // conformance |
---|
| 173 | if ((m_confLeft == 0) && (m_confRight == 0) && (m_confTop == 0) && (m_confBottom == 0)) |
---|
| 174 | { |
---|
| 175 | fprintf(stderr, "Warning: Cropping enabled, but all cropping parameters set to zero\n"); |
---|
| 176 | } |
---|
| 177 | if ((m_aiPad[1] != 0) || (m_aiPad[0]!=0)) |
---|
| 178 | { |
---|
| 179 | fprintf(stderr, "Warning: Cropping enabled, padding parameters will be ignored\n"); |
---|
| 180 | } |
---|
| 181 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
| 182 | break; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | // allocate slice-based dQP values |
---|
| 187 | Int iFrameToBeEncoded = m_cAppEncCfg->getNumFrameToBeEncoded(); |
---|
| 188 | Int iGOPSize = m_cAppEncCfg->getGOPSize(); |
---|
| 189 | if( m_aidQP == NULL ) |
---|
| 190 | m_aidQP = new Int[iFrameToBeEncoded + iGOPSize + 1 ]; |
---|
| 191 | ::memset( m_aidQP, 0, sizeof(Int)*( iFrameToBeEncoded + iGOPSize + 1 ) ); |
---|
| 192 | |
---|
| 193 | // handling of floating-point QP values |
---|
| 194 | // if QP is not integer, sequence is split into two sections having QP and QP+1 |
---|
| 195 | m_iQP = (Int)( m_fQP ); |
---|
| 196 | if ( m_iQP < m_fQP ) |
---|
| 197 | { |
---|
| 198 | Int iSwitchPOC = (Int)( iFrameToBeEncoded - (m_fQP - m_iQP)*iFrameToBeEncoded + 0.5 ); |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | iSwitchPOC = (Int)( (Double)iSwitchPOC / iGOPSize + 0.5 )*iGOPSize; |
---|
| 202 | for ( Int i=iSwitchPOC; i<iFrameToBeEncoded + iGOPSize + 1; i++ ) |
---|
| 203 | { |
---|
| 204 | m_aidQP[i] = 1; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | UInt maxCUWidth = m_cAppEncCfg->getMaxCUWidth(); |
---|
| 209 | UInt maxCUHeight = m_cAppEncCfg->getMaxCUHeight(); |
---|
| 210 | UInt maxCUDepth = m_cAppEncCfg->getMaxCUDepth(); |
---|
| 211 | bool check_failed = false; /* abort if there is a fatal configuration problem */ |
---|
| 212 | #define xConfirmPara(a,b) check_failed |= confirmPara(a,b) |
---|
| 213 | // check range of parameters |
---|
| 214 | xConfirmPara( m_iFrameRate <= 0, "Frame rate must be more than 1" ); |
---|
| 215 | xConfirmPara( (m_iSourceWidth % (maxCUWidth >> (maxCUDepth-1)))!=0, "Resulting coded frame width must be a multiple of the minimum CU size"); |
---|
| 216 | xConfirmPara( (m_iSourceHeight % (maxCUHeight >> (maxCUDepth-1)))!=0, "Resulting coded frame height must be a multiple of the minimum CU size"); |
---|
| 217 | xConfirmPara( (m_iIntraPeriod > 0 && m_iIntraPeriod < iGOPSize) || m_iIntraPeriod == 0, "Intra period must be more than GOP size, or -1 , not 0" ); |
---|
| 218 | if (m_cAppEncCfg->getDecodingRefreshType() == 2) |
---|
| 219 | { |
---|
| 220 | xConfirmPara( m_iIntraPeriod > 0 && m_iIntraPeriod <= iGOPSize , "Intra period must be larger than GOP size for periodic IDR pictures"); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | xConfirmPara( m_iQP < -6 * ((Int)m_cAppEncCfg->getInternalBitDepthY() - 8) || m_iQP > 51, "QP exceeds supported range (-QpBDOffsety to 51)" ); |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | m_iWaveFrontSubstreams = m_cAppEncCfg->getWaveFrontSynchro() ? (m_iSourceHeight + m_cAppEncCfg->getMaxCUHeight() - 1) / m_cAppEncCfg->getMaxCUHeight() : 1; |
---|
| 227 | xConfirmPara( m_iWaveFrontSubstreams <= 0, "WaveFrontSubstreams must be positive" ); |
---|
| 228 | xConfirmPara( m_iWaveFrontSubstreams > 1 && !m_cAppEncCfg->getWaveFrontSynchro(), "Must have WaveFrontSynchro > 0 in order to have WaveFrontSubstreams > 1" ); |
---|
| 229 | |
---|
| 230 | //chekc parameters |
---|
| 231 | xConfirmPara( m_iSourceWidth % TComSPS::getWinUnitX(CHROMA_420) != 0, "Picture width must be an integer multiple of the specified chroma subsampling"); |
---|
| 232 | xConfirmPara( m_iSourceHeight % TComSPS::getWinUnitY(CHROMA_420) != 0, "Picture height must be an integer multiple of the specified chroma subsampling"); |
---|
| 233 | |
---|
| 234 | xConfirmPara( m_aiPad[0] % TComSPS::getWinUnitX(CHROMA_420) != 0, "Horizontal padding must be an integer multiple of the specified chroma subsampling"); |
---|
| 235 | xConfirmPara( m_aiPad[1] % TComSPS::getWinUnitY(CHROMA_420) != 0, "Vertical padding must be an integer multiple of the specified chroma subsampling"); |
---|
| 236 | |
---|
| 237 | xConfirmPara( m_confLeft % TComSPS::getWinUnitX(CHROMA_420) != 0, "Left conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
| 238 | xConfirmPara( m_confRight % TComSPS::getWinUnitX(CHROMA_420) != 0, "Right conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
| 239 | xConfirmPara( m_confTop % TComSPS::getWinUnitY(CHROMA_420) != 0, "Top conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
| 240 | xConfirmPara( m_confBottom % TComSPS::getWinUnitY(CHROMA_420) != 0, "Bottom conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
| 241 | #undef xConfirmPara |
---|
| 242 | return check_failed; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | #endif |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | //! \} |
---|