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 | { |
---|
35 | m_confLeft = m_confRight = m_confTop = m_confBottom = 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_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 | #if AVC_SYNTAX |
---|
119 | Void TAppEncLayerCfg::xPrintParameter( UInt layerId ) |
---|
120 | #else |
---|
121 | Void TAppEncLayerCfg::xPrintParameter() |
---|
122 | #endif |
---|
123 | { |
---|
124 | printf("Input File : %s\n", m_cInputFile.c_str() ); |
---|
125 | printf("Reconstruction File : %s\n", m_cReconFile.c_str() ); |
---|
126 | #if AVC_SYNTAX |
---|
127 | if( layerId == 0 ) |
---|
128 | { |
---|
129 | printf("Base layer syntax file : %s\n", m_cAppEncCfg->getBLSyntaxFile() ); |
---|
130 | } |
---|
131 | #endif |
---|
132 | printf("Real Format : %dx%d %dHz\n", m_iSourceWidth - m_confLeft - m_confRight, m_iSourceHeight - m_confTop - m_confBottom, m_iFrameRate ); |
---|
133 | printf("Internal Format : %dx%d %dHz\n", m_iSourceWidth, m_iSourceHeight, m_iFrameRate ); |
---|
134 | printf("QP : %5.2f\n", m_fQP ); |
---|
135 | printf("Intra period : %d\n", m_iIntraPeriod ); |
---|
136 | #if RC_SHVC_HARMONIZATION |
---|
137 | printf("RateControl : %d\n", m_RCEnableRateControl ); |
---|
138 | if(m_RCEnableRateControl) |
---|
139 | { |
---|
140 | printf("TargetBitrate : %d\n", m_RCTargetBitrate ); |
---|
141 | printf("KeepHierarchicalBit : %d\n", m_RCKeepHierarchicalBit ); |
---|
142 | printf("LCULevelRC : %d\n", m_RCLCULevelRC ); |
---|
143 | printf("UseLCUSeparateModel : %d\n", m_RCUseLCUSeparateModel ); |
---|
144 | printf("InitialQP : %d\n", m_RCInitialQP ); |
---|
145 | printf("ForceIntraQP : %d\n", m_RCForceIntraQP ); |
---|
146 | } |
---|
147 | #endif |
---|
148 | printf("WaveFrontSynchro:%d WaveFrontSubstreams:%d", m_cAppEncCfg->getWaveFrontSynchro(), m_iWaveFrontSubstreams); |
---|
149 | } |
---|
150 | |
---|
151 | Bool confirmPara(Bool bflag, const char* message); |
---|
152 | |
---|
153 | Bool TAppEncLayerCfg::xCheckParameter() |
---|
154 | { |
---|
155 | switch (m_conformanceMode) |
---|
156 | { |
---|
157 | case 0: |
---|
158 | { |
---|
159 | // no cropping or padding |
---|
160 | m_confLeft = m_confRight = m_confTop = m_confBottom = 0; |
---|
161 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
162 | break; |
---|
163 | } |
---|
164 | case 1: |
---|
165 | { |
---|
166 | // automatic padding to minimum CU size |
---|
167 | Int minCuSize = m_cAppEncCfg->getMaxCUHeight() >> (m_cAppEncCfg->getMaxCUDepth() - 1); |
---|
168 | if (m_iSourceWidth % minCuSize) |
---|
169 | { |
---|
170 | m_aiPad[0] = m_confRight = ((m_iSourceWidth / minCuSize) + 1) * minCuSize - m_iSourceWidth; |
---|
171 | m_iSourceWidth += m_confRight; |
---|
172 | } |
---|
173 | if (m_iSourceHeight % minCuSize) |
---|
174 | { |
---|
175 | m_aiPad[1] = m_confBottom = ((m_iSourceHeight / minCuSize) + 1) * minCuSize - m_iSourceHeight; |
---|
176 | m_iSourceHeight += m_confBottom; |
---|
177 | } |
---|
178 | break; |
---|
179 | } |
---|
180 | case 2: |
---|
181 | { |
---|
182 | //padding |
---|
183 | m_iSourceWidth += m_aiPad[0]; |
---|
184 | m_iSourceHeight += m_aiPad[1]; |
---|
185 | m_confRight = m_aiPad[0]; |
---|
186 | m_confBottom = m_aiPad[1]; |
---|
187 | break; |
---|
188 | } |
---|
189 | case 3: |
---|
190 | { |
---|
191 | // conformance |
---|
192 | if ((m_confLeft == 0) && (m_confRight == 0) && (m_confTop == 0) && (m_confBottom == 0)) |
---|
193 | { |
---|
194 | fprintf(stderr, "Warning: Cropping enabled, but all cropping parameters set to zero\n"); |
---|
195 | } |
---|
196 | if ((m_aiPad[1] != 0) || (m_aiPad[0]!=0)) |
---|
197 | { |
---|
198 | fprintf(stderr, "Warning: Cropping enabled, padding parameters will be ignored\n"); |
---|
199 | } |
---|
200 | m_aiPad[1] = m_aiPad[0] = 0; |
---|
201 | break; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | // allocate slice-based dQP values |
---|
206 | Int iFrameToBeEncoded = m_cAppEncCfg->getNumFrameToBeEncoded(); |
---|
207 | Int iGOPSize = m_cAppEncCfg->getGOPSize(); |
---|
208 | if( m_aidQP == NULL ) |
---|
209 | m_aidQP = new Int[iFrameToBeEncoded + iGOPSize + 1 ]; |
---|
210 | ::memset( m_aidQP, 0, sizeof(Int)*( iFrameToBeEncoded + iGOPSize + 1 ) ); |
---|
211 | |
---|
212 | // handling of floating-point QP values |
---|
213 | // if QP is not integer, sequence is split into two sections having QP and QP+1 |
---|
214 | m_iQP = (Int)( m_fQP ); |
---|
215 | if ( m_iQP < m_fQP ) |
---|
216 | { |
---|
217 | Int iSwitchPOC = (Int)( iFrameToBeEncoded - (m_fQP - m_iQP)*iFrameToBeEncoded + 0.5 ); |
---|
218 | |
---|
219 | |
---|
220 | iSwitchPOC = (Int)( (Double)iSwitchPOC / iGOPSize + 0.5 )*iGOPSize; |
---|
221 | for ( Int i=iSwitchPOC; i<iFrameToBeEncoded + iGOPSize + 1; i++ ) |
---|
222 | { |
---|
223 | m_aidQP[i] = 1; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | UInt maxCUWidth = m_cAppEncCfg->getMaxCUWidth(); |
---|
228 | UInt maxCUHeight = m_cAppEncCfg->getMaxCUHeight(); |
---|
229 | UInt maxCUDepth = m_cAppEncCfg->getMaxCUDepth(); |
---|
230 | bool check_failed = false; /* abort if there is a fatal configuration problem */ |
---|
231 | #define xConfirmPara(a,b) check_failed |= confirmPara(a,b) |
---|
232 | // check range of parameters |
---|
233 | xConfirmPara( m_iFrameRate <= 0, "Frame rate must be more than 1" ); |
---|
234 | xConfirmPara( (m_iSourceWidth % (maxCUWidth >> (maxCUDepth-1)))!=0, "Resulting coded frame width must be a multiple of the minimum CU size"); |
---|
235 | xConfirmPara( (m_iSourceHeight % (maxCUHeight >> (maxCUDepth-1)))!=0, "Resulting coded frame height must be a multiple of the minimum CU size"); |
---|
236 | xConfirmPara( (m_iIntraPeriod > 0 && m_iIntraPeriod < iGOPSize) || m_iIntraPeriod == 0, "Intra period must be more than GOP size, or -1 , not 0" ); |
---|
237 | if (m_cAppEncCfg->getDecodingRefreshType() == 2) |
---|
238 | { |
---|
239 | xConfirmPara( m_iIntraPeriod > 0 && m_iIntraPeriod <= iGOPSize , "Intra period must be larger than GOP size for periodic IDR pictures"); |
---|
240 | } |
---|
241 | |
---|
242 | xConfirmPara( m_iQP < -6 * ((Int)m_cAppEncCfg->getInternalBitDepthY() - 8) || m_iQP > 51, "QP exceeds supported range (-QpBDOffsety to 51)" ); |
---|
243 | |
---|
244 | |
---|
245 | m_iWaveFrontSubstreams = m_cAppEncCfg->getWaveFrontSynchro() ? (m_iSourceHeight + m_cAppEncCfg->getMaxCUHeight() - 1) / m_cAppEncCfg->getMaxCUHeight() : 1; |
---|
246 | xConfirmPara( m_iWaveFrontSubstreams <= 0, "WaveFrontSubstreams must be positive" ); |
---|
247 | xConfirmPara( m_iWaveFrontSubstreams > 1 && !m_cAppEncCfg->getWaveFrontSynchro(), "Must have WaveFrontSynchro > 0 in order to have WaveFrontSubstreams > 1" ); |
---|
248 | |
---|
249 | //chekc parameters |
---|
250 | xConfirmPara( m_iSourceWidth % TComSPS::getWinUnitX(CHROMA_420) != 0, "Picture width must be an integer multiple of the specified chroma subsampling"); |
---|
251 | xConfirmPara( m_iSourceHeight % TComSPS::getWinUnitY(CHROMA_420) != 0, "Picture height must be an integer multiple of the specified chroma subsampling"); |
---|
252 | |
---|
253 | xConfirmPara( m_aiPad[0] % TComSPS::getWinUnitX(CHROMA_420) != 0, "Horizontal padding must be an integer multiple of the specified chroma subsampling"); |
---|
254 | xConfirmPara( m_aiPad[1] % TComSPS::getWinUnitY(CHROMA_420) != 0, "Vertical padding must be an integer multiple of the specified chroma subsampling"); |
---|
255 | |
---|
256 | xConfirmPara( m_confLeft % TComSPS::getWinUnitX(CHROMA_420) != 0, "Left conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
257 | xConfirmPara( m_confRight % TComSPS::getWinUnitX(CHROMA_420) != 0, "Right conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
258 | xConfirmPara( m_confTop % TComSPS::getWinUnitY(CHROMA_420) != 0, "Top conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
259 | xConfirmPara( m_confBottom % TComSPS::getWinUnitY(CHROMA_420) != 0, "Bottom conformance window offset must be an integer multiple of the specified chroma subsampling"); |
---|
260 | #undef xConfirmPara |
---|
261 | return check_failed; |
---|
262 | } |
---|
263 | |
---|
264 | #endif |
---|
265 | |
---|
266 | |
---|
267 | //! \} |
---|