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