[2] | 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_croppingMode( 0 ), |
---|
| 33 | m_aidQP(NULL) |
---|
| 34 | { |
---|
| 35 | m_cropLeft = m_cropRight = m_cropTop = m_cropBottom = 0; |
---|
| 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_croppingMode, 0, "Cropping mode (0: no cropping, 1:automatic padding, 2: padding, 3:cropping") |
---|
| 79 | ("CropLeft", m_cropLeft, 0, "Left cropping/padding for cropping mode 3") |
---|
| 80 | ("CropRight", m_cropRight, 0, "Right cropping/padding for cropping mode 3") |
---|
| 81 | ("CropTop", m_cropTop, 0, "Top cropping/padding for cropping mode 3") |
---|
| 82 | ("CropBottom", m_cropBottom, 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() ); |
---|
[38] | 122 | #if AVC_SYNTAX |
---|
| 123 | printf("Base layer input file : %s\n", m_cAppEncCfg->getBLSyntaxFile() ); |
---|
| 124 | #endif |
---|
[2] | 125 | printf("Real Format : %dx%d %dHz\n", m_iSourceWidth - m_cropLeft - m_cropRight, m_iSourceHeight - m_cropTop - m_cropBottom, 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 | #if SVC_EXTENSION |
---|
| 130 | printf("WaveFrontSynchro:%d WaveFrontSubstreams:%d", |
---|
| 131 | m_cAppEncCfg->getWaveFrontSynchro(), m_iWaveFrontSubstreams); |
---|
| 132 | #endif |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | Bool confirmPara(Bool bflag, const char* message); |
---|
| 136 | |
---|
| 137 | Bool TAppEncLayerCfg::xCheckParameter() |
---|
| 138 | { |
---|
| 139 | switch (m_croppingMode) |
---|
| 140 | { |
---|
| 141 | case 0: |
---|
| 142 | { |
---|
| 143 | // no cropping or padding |
---|
| 144 | m_cropLeft = m_cropRight = m_cropTop = m_cropBottom = 0; |
---|
| 145 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | case 1: |
---|
| 149 | { |
---|
| 150 | // automatic padding to minimum CU size |
---|
| 151 | Int minCuSize = m_cAppEncCfg->getMaxCUHeight() >> (m_cAppEncCfg->getMaxCUDepth() - 1); |
---|
| 152 | if (m_iSourceWidth % minCuSize) |
---|
| 153 | { |
---|
| 154 | m_aiPad[0] = m_cropRight = ((m_iSourceWidth / minCuSize) + 1) * minCuSize - m_iSourceWidth; |
---|
| 155 | m_iSourceWidth += m_cropRight; |
---|
| 156 | } |
---|
| 157 | if (m_iSourceHeight % minCuSize) |
---|
| 158 | { |
---|
| 159 | m_aiPad[1] = m_cropBottom = ((m_iSourceHeight / minCuSize) + 1) * minCuSize - m_iSourceHeight; |
---|
| 160 | m_iSourceHeight += m_cropBottom; |
---|
| 161 | } |
---|
| 162 | break; |
---|
| 163 | } |
---|
| 164 | case 2: |
---|
| 165 | { |
---|
| 166 | //padding |
---|
| 167 | m_iSourceWidth += m_aiPad[0]; |
---|
| 168 | m_iSourceHeight += m_aiPad[1]; |
---|
| 169 | m_cropRight = m_aiPad[0]; |
---|
| 170 | m_cropBottom = m_aiPad[1]; |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | case 3: |
---|
| 174 | { |
---|
| 175 | // cropping |
---|
| 176 | if ((m_cropLeft == 0) && (m_cropRight == 0) && (m_cropTop == 0) && (m_cropBottom == 0)) |
---|
| 177 | { |
---|
| 178 | fprintf(stderr, "Warning: Cropping enabled, but all cropping parameters set to zero\n"); |
---|
| 179 | } |
---|
| 180 | if ((m_aiPad[1] != 0) || (m_aiPad[0]!=0)) |
---|
| 181 | { |
---|
| 182 | fprintf(stderr, "Warning: Cropping enabled, padding parameters will be ignored\n"); |
---|
| 183 | } |
---|
| 184 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
| 185 | break; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | // allocate slice-based dQP values |
---|
| 190 | Int iFrameToBeEncoded = m_cAppEncCfg->getNumFrameToBeEncoded(); |
---|
| 191 | Int iGOPSize = m_cAppEncCfg->getGOPSize(); |
---|
| 192 | if( m_aidQP == NULL ) |
---|
| 193 | m_aidQP = new Int[iFrameToBeEncoded + iGOPSize + 1 ]; |
---|
| 194 | ::memset( m_aidQP, 0, sizeof(Int)*( iFrameToBeEncoded + iGOPSize + 1 ) ); |
---|
| 195 | |
---|
| 196 | // handling of floating-point QP values |
---|
| 197 | // if QP is not integer, sequence is split into two sections having QP and QP+1 |
---|
| 198 | m_iQP = (Int)( m_fQP ); |
---|
| 199 | if ( m_iQP < m_fQP ) |
---|
| 200 | { |
---|
| 201 | Int iSwitchPOC = (Int)( iFrameToBeEncoded - (m_fQP - m_iQP)*iFrameToBeEncoded + 0.5 ); |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | iSwitchPOC = (Int)( (Double)iSwitchPOC / iGOPSize + 0.5 )*iGOPSize; |
---|
| 205 | for ( Int i=iSwitchPOC; i<iFrameToBeEncoded + iGOPSize + 1; i++ ) |
---|
| 206 | { |
---|
| 207 | m_aidQP[i] = 1; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | UInt maxCUWidth = m_cAppEncCfg->getMaxCUWidth(); |
---|
| 212 | UInt maxCUHeight = m_cAppEncCfg->getMaxCUHeight(); |
---|
| 213 | UInt maxCUDepth = m_cAppEncCfg->getMaxCUDepth(); |
---|
| 214 | bool check_failed = false; /* abort if there is a fatal configuration problem */ |
---|
| 215 | #define xConfirmPara(a,b) check_failed |= confirmPara(a,b) |
---|
| 216 | // check range of parameters |
---|
| 217 | xConfirmPara( m_iFrameRate <= 0, "Frame rate must be more than 1" ); |
---|
| 218 | xConfirmPara( (m_iSourceWidth % (maxCUWidth >> (maxCUDepth-1)))!=0, "Resulting coded frame width must be a multiple of the minimum CU size"); |
---|
| 219 | xConfirmPara( (m_iSourceHeight % (maxCUHeight >> (maxCUDepth-1)))!=0, "Resulting coded frame height must be a multiple of the minimum CU size"); |
---|
| 220 | xConfirmPara( (m_iIntraPeriod > 0 && m_iIntraPeriod < iGOPSize) || m_iIntraPeriod == 0, "Intra period must be more than GOP size, or -1 , not 0" ); |
---|
| 221 | if (m_cAppEncCfg->getDecodingRefreshType() == 2) |
---|
| 222 | { |
---|
| 223 | xConfirmPara( m_iIntraPeriod > 0 && m_iIntraPeriod <= iGOPSize , "Intra period must be larger than GOP size for periodic IDR pictures"); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | xConfirmPara( m_iQP < -6 * ((Int)m_cAppEncCfg->getInternalBitDepth() - 8) || m_iQP > 51, "QP exceeds supported range (-QpBDOffsety to 51)" ); |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | #if SVC_EXTENSION |
---|
| 230 | m_iWaveFrontSubstreams = m_cAppEncCfg->getWaveFrontSynchro() ? (m_iSourceHeight + m_cAppEncCfg->getMaxCUHeight() - 1) / m_cAppEncCfg->getMaxCUHeight() : 1; |
---|
| 231 | xConfirmPara( m_iWaveFrontSubstreams <= 0, "WaveFrontSubstreams must be positive" ); |
---|
| 232 | xConfirmPara( m_iWaveFrontSubstreams > 1 && !m_cAppEncCfg->getWaveFrontSynchro(), "Must have WaveFrontSynchro > 0 in order to have WaveFrontSubstreams > 1" ); |
---|
| 233 | #endif |
---|
| 234 | |
---|
| 235 | #undef xConfirmPara |
---|
| 236 | return check_failed; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | #endif |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | //! \} |
---|