1 /*!
2 * \copy
3 * Copyright (c) 2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33 #define _CRT_SECURE_NO_WARNINGS
34 #include <stdio.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #if defined (ANDROID_NDK)
40 #include <android/log.h>
41 #endif
42 #ifdef ONLY_ENC_FRAMES_NUM
43 #undef ONLY_ENC_FRAMES_NUM
44 #endif//ONLY_ENC_FRAMES_NUM
45 #define ONLY_ENC_FRAMES_NUM INT_MAX // 2, INT_MAX // type the num you try to encode here, 2, 10, etc
46
47 #if defined (WINDOWS_PHONE)
48 float g_fFPS = 0.0;
49 double g_dEncoderTime = 0.0;
50 int g_iEncodedFrame = 0;
51 #endif
52
53 #if defined (ANDROID_NDK)
54 #define LOG_TAG "welsenc"
55 #define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
56 #define printf(...) LOGI(__VA_ARGS__)
57 #define fprintf(a, ...) LOGI(__VA_ARGS__)
58 #endif
59 //#define STICK_STREAM_SIZE
60
61 #include "measure_time.h"
62 #include "read_config.h"
63
64 #include "typedefs.h"
65
66 #ifdef _MSC_VER
67 #include <io.h> /* _setmode() */
68 #include <fcntl.h> /* _O_BINARY */
69 #endif//_MSC_VER
70
71 #include "codec_def.h"
72 #include "codec_api.h"
73 #include "extern.h"
74 #include "macros.h"
75 #include "wels_const.h"
76
77 #include "mt_defs.h"
78 #include "WelsThreadLib.h"
79
80 #ifdef _WIN32
81 #ifdef WINAPI_FAMILY
82 #include <winapifamily.h>
83 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
84 #define HAVE_PROCESS_AFFINITY
85 #endif
86 #else /* defined(WINAPI_FAMILY) */
87 #define HAVE_PROCESS_AFFINITY
88 #endif
89 #endif /* _WIN32 */
90
91 #if defined(__linux__) || defined(__unix__)
92 #define _FILE_OFFSET_BITS 64
93 #endif
94
95 #include <iostream>
96 using namespace std;
97 using namespace WelsEnc;
98
99 /*
100 * Layer Context
101 */
102 typedef struct LayerpEncCtx_s {
103 int32_t iDLayerQp;
104 SSliceArgument sSliceArgument;
105 } SLayerPEncCtx;
106
107 typedef struct tagFilesSet {
108 string strBsFile;
109 string strSeqFile; // for cmd lines
110 string strLayerCfgFile[MAX_DEPENDENCY_LAYER];
111 char sRecFileName[MAX_DEPENDENCY_LAYER][MAX_FNAME_LEN];
112 uint32_t uiFrameToBeCoded;
113 bool bEnableMultiBsFile;
114
tagFilesSettagFilesSet115 tagFilesSet() {
116 uiFrameToBeCoded = 0;
117 bEnableMultiBsFile = false;
118 for (int i = 0; i < MAX_DEPENDENCY_LAYER; i++)
119 sRecFileName[i][0] = '\0';
120 }
121 } SFilesSet;
122
123
124
125 /* Ctrl-C handler */
126 static int g_iCtrlC = 0;
SigIntHandler(int a)127 static void SigIntHandler (int a) {
128 g_iCtrlC = 1;
129 }
130 static int g_LevelSetting = WELS_LOG_ERROR;
131
ParseLayerConfig(CReadConfig & cRdLayerCfg,const int iLayer,SEncParamExt & pSvcParam,SFilesSet & sFileSet)132 int ParseLayerConfig (CReadConfig& cRdLayerCfg, const int iLayer, SEncParamExt& pSvcParam, SFilesSet& sFileSet) {
133 if (!cRdLayerCfg.ExistFile()) {
134 fprintf (stderr, "Unabled to open layer #%d configuration file: %s.\n", iLayer, cRdLayerCfg.GetFileName().c_str());
135 return 1;
136 }
137
138 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
139 int iLeftTargetBitrate = (pSvcParam.iRCMode != RC_OFF_MODE) ? pSvcParam.iTargetBitrate : 0;
140 SLayerPEncCtx sLayerCtx;
141 memset (&sLayerCtx, 0, sizeof (SLayerPEncCtx));
142
143 string strTag[4];
144 string str_ ("SlicesAssign");
145 const int kiSize = (int)str_.size();
146
147 while (!cRdLayerCfg.EndOfFile()) {
148 long iLayerRd = cRdLayerCfg.ReadLine (&strTag[0]);
149 if (iLayerRd > 0) {
150 if (strTag[0].empty())
151 continue;
152 if (strTag[0].compare ("FrameWidth") == 0) {
153 pDLayer->iVideoWidth = atoi (strTag[1].c_str());
154 } else if (strTag[0].compare ("FrameHeight") == 0) {
155 pDLayer->iVideoHeight = atoi (strTag[1].c_str());
156 } else if (strTag[0].compare ("FrameRateOut") == 0) {
157 pDLayer->fFrameRate = (float)atof (strTag[1].c_str());
158 } else if (strTag[0].compare ("ReconFile") == 0) {
159 const unsigned int kiLen = (unsigned int)strTag[1].length();
160 if (kiLen >= sizeof (sFileSet.sRecFileName[iLayer]))
161 return -1;
162 sFileSet.sRecFileName[iLayer][kiLen] = '\0';
163 strncpy (sFileSet.sRecFileName[iLayer], strTag[1].c_str(), kiLen); // confirmed_safe_unsafe_usage
164 } else if (strTag[0].compare ("ProfileIdc") == 0) {
165 pDLayer->uiProfileIdc = (EProfileIdc)atoi (strTag[1].c_str());
166 } else if (strTag[0].compare ("FRExt") == 0) {
167 // pDLayer->frext_mode = (bool)atoi(strTag[1].c_str());
168 } else if (strTag[0].compare ("SpatialBitrate") == 0) {
169 pDLayer->iSpatialBitrate = 1000 * atoi (strTag[1].c_str());
170 if (pSvcParam.iRCMode != RC_OFF_MODE) {
171 if (pDLayer->iSpatialBitrate <= 0) {
172 fprintf (stderr, "Invalid spatial bitrate(%d) in dependency layer #%d.\n", pDLayer->iSpatialBitrate, iLayer);
173 return -1;
174 }
175 if (pDLayer->iSpatialBitrate > iLeftTargetBitrate) {
176 fprintf (stderr, "Invalid spatial(#%d) bitrate(%d) setting due to unavailable left(%d)!\n", iLayer,
177 pDLayer->iSpatialBitrate, iLeftTargetBitrate);
178 return -1;
179 }
180 iLeftTargetBitrate -= pDLayer->iSpatialBitrate;
181 }
182 } else if (strTag[0].compare ("MaxSpatialBitrate") == 0) {
183 pDLayer->iMaxSpatialBitrate = 1000 * atoi (strTag[1].c_str());
184 if (pSvcParam.iRCMode != RC_OFF_MODE) {
185 if (pDLayer->iMaxSpatialBitrate < 0) {
186 fprintf (stderr, "Invalid max spatial bitrate(%d) in dependency layer #%d.\n", pDLayer->iMaxSpatialBitrate, iLayer);
187 return -1;
188 }
189 if (pDLayer->iMaxSpatialBitrate > 0 && pDLayer->iMaxSpatialBitrate < pDLayer->iSpatialBitrate) {
190 fprintf (stderr, "Invalid max spatial(#%d) bitrate(%d) setting::: < layerBitrate(%d)!\n", iLayer,
191 pDLayer->iMaxSpatialBitrate, pDLayer->iSpatialBitrate);
192 return -1;
193 }
194 }
195 } else if (strTag[0].compare ("InitialQP") == 0) {
196 sLayerCtx.iDLayerQp = atoi (strTag[1].c_str());
197 } else if (strTag[0].compare ("SliceMode") == 0) {
198 sLayerCtx.sSliceArgument.uiSliceMode = (SliceModeEnum)atoi (strTag[1].c_str());
199 } else if (strTag[0].compare ("SliceSize") == 0) { //SM_SIZELIMITED_SLICE
200 sLayerCtx.sSliceArgument.uiSliceSizeConstraint = atoi (strTag[1].c_str());
201 continue;
202 } else if (strTag[0].compare ("SliceNum") == 0) {
203 sLayerCtx.sSliceArgument.uiSliceNum = atoi (strTag[1].c_str());
204 } else if (strTag[0].compare (0, kiSize, str_) == 0) {
205 const char* kpString = strTag[0].c_str();
206 int uiSliceIdx = atoi (&kpString[kiSize]);
207 assert (uiSliceIdx < MAX_SLICES_NUM);
208 sLayerCtx.sSliceArgument.uiSliceMbNum[uiSliceIdx] = atoi (strTag[1].c_str());
209 }
210 }
211 }
212 pDLayer->iDLayerQp = sLayerCtx.iDLayerQp;
213 pDLayer->sSliceArgument.uiSliceMode = sLayerCtx.sSliceArgument.uiSliceMode;
214
215 memcpy (&pDLayer->sSliceArgument, &sLayerCtx.sSliceArgument, sizeof (SSliceArgument)); // confirmed_safe_unsafe_usage
216 memcpy (&pDLayer->sSliceArgument.uiSliceMbNum[0], &sLayerCtx.sSliceArgument.uiSliceMbNum[0],
217 sizeof (sLayerCtx.sSliceArgument.uiSliceMbNum)); // confirmed_safe_unsafe_usage
218
219 return 0;
220 }
ParseConfig(CReadConfig & cRdCfg,SSourcePicture * pSrcPic,SEncParamExt & pSvcParam,SFilesSet & sFileSet)221 int ParseConfig (CReadConfig& cRdCfg, SSourcePicture* pSrcPic, SEncParamExt& pSvcParam, SFilesSet& sFileSet) {
222 string strTag[4];
223 int32_t iRet = 0;
224 int8_t iLayerCount = 0;
225
226 while (!cRdCfg.EndOfFile()) {
227 long iRd = cRdCfg.ReadLine (&strTag[0]);
228 if (iRd > 0) {
229 if (strTag[0].empty())
230 continue;
231
232 if (strTag[0].compare ("UsageType") == 0) {
233 pSvcParam.iUsageType = (EUsageType)atoi (strTag[1].c_str());
234 } else if (strTag[0].compare ("SimulcastAVC") == 0) {
235 pSvcParam.bSimulcastAVC = atoi (strTag[1].c_str()) ? true : false;
236 } else if (strTag[0].compare ("SourceWidth") == 0) {
237 pSrcPic->iPicWidth = atoi (strTag[1].c_str());
238 } else if (strTag[0].compare ("SourceHeight") == 0) {
239 pSrcPic->iPicHeight = atoi (strTag[1].c_str());
240 } else if (strTag[0].compare ("InputFile") == 0) {
241 if (strTag[1].length() > 0)
242 sFileSet.strSeqFile = strTag[1];
243 } else if (strTag[0].compare ("OutputFile") == 0) {
244 sFileSet.strBsFile = strTag[1];
245 } else if (strTag[0].compare ("MaxFrameRate") == 0) {
246 pSvcParam.fMaxFrameRate = (float)atof (strTag[1].c_str());
247 } else if (strTag[0].compare ("FramesToBeEncoded") == 0) {
248 sFileSet.uiFrameToBeCoded = atoi (strTag[1].c_str());
249 } else if (strTag[0].compare ("TemporalLayerNum") == 0) {
250 pSvcParam.iTemporalLayerNum = atoi (strTag[1].c_str());
251 } else if (strTag[0].compare ("IntraPeriod") == 0) {
252 pSvcParam.uiIntraPeriod = atoi (strTag[1].c_str());
253 } else if (strTag[0].compare ("MaxNalSize") == 0) {
254 pSvcParam.uiMaxNalSize = atoi (strTag[1].c_str());
255 } else if (strTag[0].compare ("SpsPpsIDStrategy") == 0) {
256 int32_t iValue = atoi (strTag[1].c_str());
257 switch (iValue) {
258 case 0:
259 pSvcParam.eSpsPpsIdStrategy = CONSTANT_ID;
260 break;
261 case 0x01:
262 pSvcParam.eSpsPpsIdStrategy = INCREASING_ID;
263 break;
264 case 0x02:
265 pSvcParam.eSpsPpsIdStrategy = SPS_LISTING;
266 break;
267 case 0x03:
268 pSvcParam.eSpsPpsIdStrategy = SPS_LISTING_AND_PPS_INCREASING;
269 break;
270 case 0x06:
271 pSvcParam.eSpsPpsIdStrategy = SPS_PPS_LISTING;
272 break;
273 default:
274 pSvcParam.eSpsPpsIdStrategy = CONSTANT_ID;
275 break;
276 }
277 } else if (strTag[0].compare ("EnableMultiBsFile") == 0) {
278 sFileSet.bEnableMultiBsFile = atoi (strTag[1].c_str()) ? true : false;
279 } else if (strTag[0].compare ("EnableScalableSEI") == 0) {
280 pSvcParam.bEnableSSEI = atoi (strTag[1].c_str()) ? true : false;
281 } else if (strTag[0].compare ("EnableFrameCropping") == 0) {
282 pSvcParam.bEnableFrameCroppingFlag = (atoi (strTag[1].c_str()) != 0);
283 } else if (strTag[0].compare ("EntropyCodingModeFlag") == 0) {
284 pSvcParam.iEntropyCodingModeFlag = (atoi (strTag[1].c_str()) != 0);
285 } else if (strTag[0].compare ("ComplexityMode") == 0) {
286 pSvcParam.iComplexityMode = (ECOMPLEXITY_MODE) (atoi (strTag[1].c_str()));
287 } else if (strTag[0].compare ("LoopFilterDisableIDC") == 0) {
288 pSvcParam.iLoopFilterDisableIdc = (int8_t)atoi (strTag[1].c_str());
289 if (pSvcParam.iLoopFilterDisableIdc > 6 || pSvcParam.iLoopFilterDisableIdc < 0) {
290 fprintf (stderr, "Invalid parameter in iLoopFilterDisableIdc: %d.\n", pSvcParam.iLoopFilterDisableIdc);
291 iRet = 1;
292 break;
293 }
294 } else if (strTag[0].compare ("LoopFilterAlphaC0Offset") == 0) {
295 pSvcParam.iLoopFilterAlphaC0Offset = (int8_t)atoi (strTag[1].c_str());
296 if (pSvcParam.iLoopFilterAlphaC0Offset < -6)
297 pSvcParam.iLoopFilterAlphaC0Offset = -6;
298 else if (pSvcParam.iLoopFilterAlphaC0Offset > 6)
299 pSvcParam.iLoopFilterAlphaC0Offset = 6;
300 } else if (strTag[0].compare ("LoopFilterBetaOffset") == 0) {
301 pSvcParam.iLoopFilterBetaOffset = (int8_t)atoi (strTag[1].c_str());
302 if (pSvcParam.iLoopFilterBetaOffset < -6)
303 pSvcParam.iLoopFilterBetaOffset = -6;
304 else if (pSvcParam.iLoopFilterBetaOffset > 6)
305 pSvcParam.iLoopFilterBetaOffset = 6;
306 } else if (strTag[0].compare ("MultipleThreadIdc") == 0) {
307 // # 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
308 pSvcParam.iMultipleThreadIdc = atoi (strTag[1].c_str());
309 if (pSvcParam.iMultipleThreadIdc < 0)
310 pSvcParam.iMultipleThreadIdc = 0;
311 else if (pSvcParam.iMultipleThreadIdc > MAX_THREADS_NUM)
312 pSvcParam.iMultipleThreadIdc = MAX_THREADS_NUM;
313 } else if (strTag[0].compare ("UseLoadBalancing") == 0) {
314 pSvcParam.bUseLoadBalancing = (atoi (strTag[1].c_str())) ? true : false;
315 } else if (strTag[0].compare ("RCMode") == 0) {
316 pSvcParam.iRCMode = (RC_MODES) atoi (strTag[1].c_str());
317 } else if (strTag[0].compare ("TargetBitrate") == 0) {
318 pSvcParam.iTargetBitrate = 1000 * atoi (strTag[1].c_str());
319 if ((pSvcParam.iRCMode != RC_OFF_MODE) && pSvcParam.iTargetBitrate <= 0) {
320 fprintf (stderr, "Invalid target bitrate setting due to RC enabled. Check TargetBitrate field please!\n");
321 return 1;
322 }
323 } else if (strTag[0].compare ("MaxOverallBitrate") == 0) {
324 pSvcParam.iMaxBitrate = 1000 * atoi (strTag[1].c_str());
325 if ((pSvcParam.iRCMode != RC_OFF_MODE) && pSvcParam.iMaxBitrate < 0) {
326 fprintf (stderr, "Invalid max overall bitrate setting due to RC enabled. Check MaxOverallBitrate field please!\n");
327 return 1;
328 }
329 } else if (strTag[0].compare ("MaxQp") == 0) {
330 pSvcParam.iMaxQp = atoi (strTag[1].c_str());
331 } else if (strTag[0].compare ("MinQp") == 0) {
332 pSvcParam.iMinQp = atoi (strTag[1].c_str());
333 } else if (strTag[0].compare ("EnableDenoise") == 0) {
334 pSvcParam.bEnableDenoise = atoi (strTag[1].c_str()) ? true : false;
335 } else if (strTag[0].compare ("EnableSceneChangeDetection") == 0) {
336 pSvcParam.bEnableSceneChangeDetect = atoi (strTag[1].c_str()) ? true : false;
337 } else if (strTag[0].compare ("EnableBackgroundDetection") == 0) {
338 pSvcParam.bEnableBackgroundDetection = atoi (strTag[1].c_str()) ? true : false;
339 } else if (strTag[0].compare ("EnableAdaptiveQuantization") == 0) {
340 pSvcParam.bEnableAdaptiveQuant = atoi (strTag[1].c_str()) ? true : false;
341 } else if (strTag[0].compare ("EnableFrameSkip") == 0) {
342 pSvcParam.bEnableFrameSkip = atoi (strTag[1].c_str()) ? true : false;
343 } else if (strTag[0].compare ("EnableLongTermReference") == 0) {
344 pSvcParam.bEnableLongTermReference = atoi (strTag[1].c_str()) ? true : false;
345 } else if (strTag[0].compare ("LongTermReferenceNumber") == 0) {
346 pSvcParam.iLTRRefNum = atoi (strTag[1].c_str());
347 } else if (strTag[0].compare ("LtrMarkPeriod") == 0) {
348 pSvcParam.iLtrMarkPeriod = (uint32_t)atoi (strTag[1].c_str());
349 } else if (strTag[0].compare ("LosslessLink") == 0) {
350 pSvcParam.bIsLosslessLink = atoi (strTag[1].c_str()) ? true : false;
351 } else if (strTag[0].compare ("NumLayers") == 0) {
352 pSvcParam.iSpatialLayerNum = (int8_t)atoi (strTag[1].c_str());
353 if (pSvcParam.iSpatialLayerNum > MAX_DEPENDENCY_LAYER || pSvcParam.iSpatialLayerNum <= 0) {
354 fprintf (stderr, "Invalid parameter in iSpatialLayerNum: %d.\n", pSvcParam.iSpatialLayerNum);
355 iRet = 1;
356 break;
357 }
358 } else if (strTag[0].compare ("LayerCfg") == 0) {
359 if (strTag[1].length() > 0)
360 sFileSet.strLayerCfgFile[iLayerCount] = strTag[1];
361 // pSvcParam.sDependencyLayers[iLayerCount].uiDependencyId = iLayerCount;
362 ++ iLayerCount;
363 } else if (strTag[0].compare ("PrefixNALAddingCtrl") == 0) {
364 int ctrl_flag = atoi (strTag[1].c_str());
365 if (ctrl_flag > 1)
366 ctrl_flag = 1;
367 else if (ctrl_flag < 0)
368 ctrl_flag = 0;
369 pSvcParam.bPrefixNalAddingCtrl = ctrl_flag ? true : false;
370 }
371 }
372 }
373
374 const int8_t kiActualLayerNum = WELS_MIN (pSvcParam.iSpatialLayerNum, iLayerCount);
375 if (pSvcParam.iSpatialLayerNum >
376 kiActualLayerNum) { // fixed number of dependency layer due to parameter error in settings
377 pSvcParam.iSpatialLayerNum = kiActualLayerNum;
378 }
379
380 assert (kiActualLayerNum <= MAX_DEPENDENCY_LAYER);
381
382 for (int8_t iLayer = 0; iLayer < kiActualLayerNum; ++ iLayer) {
383 CReadConfig cRdLayerCfg (sFileSet.strLayerCfgFile[iLayer]);
384 if (-1 == ParseLayerConfig (cRdLayerCfg, iLayer, pSvcParam, sFileSet)) {
385 iRet = 1;
386 break;
387 }
388 }
389
390 return iRet;
391 }
392
PrintHelp()393 void PrintHelp() {
394 printf ("\n Wels SVC Encoder Usage:\n\n");
395 printf (" Syntax: welsenc.exe -h\n");
396 printf (" Syntax: welsenc.exe welsenc.cfg\n");
397 printf (" Syntax: welsenc.exe welsenc.cfg [options]\n");
398
399 printf ("\n Supported Options:\n");
400 printf (" -bf Bit Stream File\n");
401 printf (" -org Original file, example: -org src.yuv\n");
402 printf (" -sw the source width\n");
403 printf (" -sh the source height\n");
404 printf (" -utype usage type\n");
405 printf (" -savc simulcast avc\n");
406 printf (" -frms Number of total frames to be encoded\n");
407 printf (" -frin input frame rate\n");
408 printf (" -numtl Temporal layer number (default: 1)\n");
409 printf (" -iper Intra period (default: -1) : must be a power of 2 of GOP size (or -1)\n");
410 printf (" -nalsize the Maximum NAL size. which should be larger than the each layer slicesize when slice mode equals to SM_SIZELIMITED_SLICE\n");
411 printf (" -spsid SPS/PPS id strategy: 0:const, 1: increase, 2: sps list, 3: sps list and pps increase, 4: sps/pps list\n");
412 printf (" -cabac Entropy coding mode(0:cavlc 1:cabac \n");
413 printf (" -complexity Complexity mode (default: 0),0: low complexity, 1: medium complexity, 2: high complexity\n");
414 printf (" -denois Control denoising (default: 0)\n");
415 printf (" -scene Control scene change detection (default: 0)\n");
416 printf (" -bgd Control background detection (default: 0)\n");
417 printf (" -aq Control adaptive quantization (default: 0)\n");
418 printf (" -ltr Control long term reference (default: 0)\n");
419 printf (" -ltrnum Control the number of long term reference((1-4):screen LTR,(1-2):video LTR \n");
420 printf (" -ltrper Control the long term reference marking period \n");
421 printf (" -threadIdc 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads \n");
422 printf (" -loadbalancing 0: turn off loadbalancing between slices when multi-threading available; 1: (default value) turn on loadbalancing between slices when multi-threading available\n");
423 printf (" -deblockIdc Loop filter idc (0: on, 1: off, \n");
424 printf (" -alphaOffset AlphaOffset(-6..+6): valid range \n");
425 printf (" -betaOffset BetaOffset (-6..+6): valid range\n");
426 printf (" -rc rate control mode: -1-rc off; 0-quality mode; 1-bitrate mode; 2: buffer based mode,can't control bitrate; 3: bitrate mode based on timestamp input;\n");
427 printf (" -tarb Overall target bitrate\n");
428 printf (" -maxbrTotal Overall max bitrate\n");
429 printf (" -maxqp Maximum Qp (default: %d, or for screen content usage: %d)\n", QP_MAX_VALUE, MAX_SCREEN_QP);
430 printf (" -minqp Minimum Qp (default: %d, or for screen content usage: %d)\n", QP_MIN_VALUE, MIN_SCREEN_QP);
431 printf (" -numl Number Of Layers: Must exist with layer_cfg file and the number of input layer_cfg file must equal to the value set by this command\n");
432 printf (" The options below are layer-based: (need to be set with layer id)\n");
433 printf (" -lconfig (Layer) (spatial layer configure file)\n");
434 printf (" -drec (Layer) (reconstruction file);example: -drec 0 rec.yuv. Setting the reconstruction file, this will only functioning when dumping reconstruction is enabled\n");
435 printf (" -dprofile (Layer) (layer profile);example: -dprofile 0 66. Setting the layer profile, this should be the same for all layers\n");
436 printf (" -dw (Layer) (output width)\n");
437 printf (" -dh (Layer) (output height)\n");
438 printf (" -frout (Layer) (output frame rate)\n");
439 printf (" -lqp (Layer) (base quality layer qp : must work with -ldeltaqp or -lqparr)\n");
440 printf (" -ltarb (Layer) (spatial layer target bitrate)\n");
441 printf (" -lmaxb (Layer) (spatial layer max bitrate)\n");
442 printf (" -slcmd (Layer) (spatial layer slice mode): pls refer to layerX.cfg for details ( -slcnum: set target slice num; -slcsize: set target slice size constraint ; -slcmbnum: set the first slice mb num under some slice modes) \n");
443 printf (" -trace (Level)\n");
444 printf ("\n");
445 }
446
ParseCommandLine(int argc,char ** argv,SSourcePicture * pSrcPic,SEncParamExt & pSvcParam,SFilesSet & sFileSet)447 int ParseCommandLine (int argc, char** argv, SSourcePicture* pSrcPic, SEncParamExt& pSvcParam, SFilesSet& sFileSet) {
448 char* pCommand = NULL;
449 SLayerPEncCtx sLayerCtx[MAX_SPATIAL_LAYER_NUM];
450 int n = 0;
451 string str_ ("SlicesAssign");
452
453 while (n < argc) {
454 pCommand = argv[n++];
455
456 if (!strcmp (pCommand, "-bf") && (n < argc))
457 sFileSet.strBsFile.assign (argv[n++]);
458 else if (!strcmp (pCommand, "-utype") && (n < argc))
459 pSvcParam.iUsageType = (EUsageType)atoi (argv[n++]);
460
461 else if (!strcmp (pCommand, "-savc") && (n < argc))
462 pSvcParam.bSimulcastAVC = atoi (argv[n++]) ? true : false;
463
464 else if (!strcmp (pCommand, "-org") && (n < argc))
465 sFileSet.strSeqFile.assign (argv[n++]);
466
467 else if (!strcmp (pCommand, "-sw") && (n < argc))//source width
468 pSrcPic->iPicWidth = atoi (argv[n++]);
469
470 else if (!strcmp (pCommand, "-sh") && (n < argc))//source height
471 pSrcPic->iPicHeight = atoi (argv[n++]);
472
473 else if (!strcmp (pCommand, "-frms") && (n < argc))
474 sFileSet.uiFrameToBeCoded = atoi (argv[n++]);
475
476 else if (!strcmp (pCommand, "-frin") && (n < argc))
477 pSvcParam.fMaxFrameRate = (float) atof (argv[n++]);
478
479 else if (!strcmp (pCommand, "-numtl") && (n < argc))
480 pSvcParam.iTemporalLayerNum = atoi (argv[n++]);
481
482 else if (!strcmp (pCommand, "-mfile") && (n < argc))
483 sFileSet.bEnableMultiBsFile = atoi (argv[n++]) ? true : false;
484
485 else if (!strcmp (pCommand, "-iper") && (n < argc))
486 pSvcParam.uiIntraPeriod = atoi (argv[n++]);
487
488 else if (!strcmp (pCommand, "-nalsize") && (n < argc))
489 pSvcParam.uiMaxNalSize = atoi (argv[n++]);
490
491 else if (!strcmp (pCommand, "-spsid") && (n < argc)) {
492 int32_t iValue = atoi (argv[n++]);
493 switch (iValue) {
494 case 0:
495 pSvcParam.eSpsPpsIdStrategy = CONSTANT_ID;
496 break;
497 case 0x01:
498 pSvcParam.eSpsPpsIdStrategy = INCREASING_ID;
499 break;
500 case 0x02:
501 pSvcParam.eSpsPpsIdStrategy = SPS_LISTING;
502 break;
503 case 0x03:
504 pSvcParam.eSpsPpsIdStrategy = SPS_LISTING_AND_PPS_INCREASING;
505 break;
506 case 0x06:
507 pSvcParam.eSpsPpsIdStrategy = SPS_PPS_LISTING;
508 break;
509 default:
510 pSvcParam.eSpsPpsIdStrategy = CONSTANT_ID;
511 break;
512 }
513 } else if (!strcmp (pCommand, "-cabac") && (n < argc))
514 pSvcParam.iEntropyCodingModeFlag = atoi (argv[n++]);
515
516 else if (!strcmp (pCommand, "-complexity") && (n < argc))
517 pSvcParam.iComplexityMode = (ECOMPLEXITY_MODE)atoi (argv[n++]);
518
519 else if (!strcmp (pCommand, "-denois") && (n < argc))
520 pSvcParam.bEnableDenoise = atoi (argv[n++]) ? true : false;
521
522 else if (!strcmp (pCommand, "-scene") && (n < argc))
523 pSvcParam.bEnableSceneChangeDetect = atoi (argv[n++]) ? true : false;
524
525 else if (!strcmp (pCommand, "-bgd") && (n < argc))
526 pSvcParam.bEnableBackgroundDetection = atoi (argv[n++]) ? true : false;
527
528 else if (!strcmp (pCommand, "-aq") && (n < argc))
529 pSvcParam.bEnableAdaptiveQuant = atoi (argv[n++]) ? true : false;
530
531 else if (!strcmp (pCommand, "-fs") && (n < argc))
532 pSvcParam.bEnableFrameSkip = atoi (argv[n++]) ? true : false;
533
534 else if (!strcmp (pCommand, "-ltr") && (n < argc))
535 pSvcParam.bEnableLongTermReference = atoi (argv[n++]) ? true : false;
536
537 else if (!strcmp (pCommand, "-ltrnum") && (n < argc))
538 pSvcParam.iLTRRefNum = atoi (argv[n++]);
539
540 else if (!strcmp (pCommand, "-ltrper") && (n < argc))
541 pSvcParam.iLtrMarkPeriod = atoi (argv[n++]);
542
543 else if (!strcmp (pCommand, "-threadIdc") && (n < argc))
544 pSvcParam.iMultipleThreadIdc = atoi (argv[n++]);
545 else if (!strcmp (pCommand, "-loadbalancing") && (n + 1 < argc)) {
546 pSvcParam.bUseLoadBalancing = (atoi (argv[n++])) ? true : false;
547 } else if (!strcmp (pCommand, "-deblockIdc") && (n < argc))
548 pSvcParam.iLoopFilterDisableIdc = atoi (argv[n++]);
549
550 else if (!strcmp (pCommand, "-alphaOffset") && (n < argc))
551 pSvcParam.iLoopFilterAlphaC0Offset = atoi (argv[n++]);
552
553 else if (!strcmp (pCommand, "-betaOffset") && (n < argc))
554 pSvcParam.iLoopFilterBetaOffset = atoi (argv[n++]);
555
556 else if (!strcmp (pCommand, "-rc") && (n < argc))
557 pSvcParam.iRCMode = static_cast<RC_MODES> (atoi (argv[n++]));
558
559 else if (!strcmp (pCommand, "-trace") && (n < argc))
560 g_LevelSetting = atoi (argv[n++]);
561
562 else if (!strcmp (pCommand, "-tarb") && (n < argc))
563 pSvcParam.iTargetBitrate = 1000 * atoi (argv[n++]);
564
565 else if (!strcmp (pCommand, "-maxbrTotal") && (n < argc))
566 pSvcParam.iMaxBitrate = 1000 * atoi (argv[n++]);
567
568 else if (!strcmp (pCommand, "-maxqp") && (n < argc))
569 pSvcParam.iMaxQp = atoi (argv[n++]);
570
571 else if (!strcmp (pCommand, "-minqp") && (n < argc))
572 pSvcParam.iMinQp = atoi (argv[n++]);
573
574 else if (!strcmp (pCommand, "-numl") && (n < argc)) {
575 pSvcParam.iSpatialLayerNum = atoi (argv[n++]);
576 } else if (!strcmp (pCommand, "-lconfig") && (n < argc)) {
577 unsigned int iLayer = atoi (argv[n++]);
578 sFileSet.strLayerCfgFile[iLayer].assign (argv[n++]);
579 CReadConfig cRdLayerCfg (sFileSet.strLayerCfgFile[iLayer]);
580 if (-1 == ParseLayerConfig (cRdLayerCfg, iLayer, pSvcParam, sFileSet)) {
581 return 1;
582 }
583 } else if (!strcmp (pCommand, "-dprofile") && (n + 1 < argc)) {
584 unsigned int iLayer = atoi (argv[n++]);
585 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
586 pDLayer->uiProfileIdc = (EProfileIdc) atoi (argv[n++]);
587 } else if (!strcmp (pCommand, "-drec") && (n + 1 < argc)) {
588 unsigned int iLayer = atoi (argv[n++]);
589 const unsigned int iLen = (int) strlen (argv[n]);
590 if (iLen >= sizeof (sFileSet.sRecFileName[iLayer]))
591 return 1;
592 sFileSet.sRecFileName[iLayer][iLen] = '\0';
593 strncpy (sFileSet.sRecFileName[iLayer], argv[n++], iLen); // confirmed_safe_unsafe_usage
594 } else if (!strcmp (pCommand, "-dw") && (n + 1 < argc)) {
595 unsigned int iLayer = atoi (argv[n++]);
596 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
597 pDLayer->iVideoWidth = atoi (argv[n++]);
598 }
599
600 else if (!strcmp (pCommand, "-dh") && (n + 1 < argc)) {
601 unsigned int iLayer = atoi (argv[n++]);
602 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
603 pDLayer->iVideoHeight = atoi (argv[n++]);
604 }
605
606 else if (!strcmp (pCommand, "-frout") && (n + 1 < argc)) {
607 unsigned int iLayer = atoi (argv[n++]);
608 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
609 pDLayer->fFrameRate = (float)atof (argv[n++]);
610 }
611
612 else if (!strcmp (pCommand, "-lqp") && (n + 1 < argc)) {
613 unsigned int iLayer = atoi (argv[n++]);
614 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
615 pDLayer->iDLayerQp = sLayerCtx[iLayer].iDLayerQp = atoi (argv[n++]);
616 }
617 //sLayerCtx[iLayer].num_quality_layers = pDLayer->num_quality_layers = 1;
618
619 else if (!strcmp (pCommand, "-ltarb") && (n + 1 < argc)) {
620 unsigned int iLayer = atoi (argv[n++]);
621 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
622 pDLayer->iSpatialBitrate = 1000 * atoi (argv[n++]);
623 }
624
625 else if (!strcmp (pCommand, "-lmaxb") && (n + 1 < argc)) {
626 unsigned int iLayer = atoi (argv[n++]);
627 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
628 pDLayer->iMaxSpatialBitrate = 1000 * atoi (argv[n++]);
629 }
630
631 else if (!strcmp (pCommand, "-slcmd") && (n + 1 < argc)) {
632 unsigned int iLayer = atoi (argv[n++]);
633 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
634
635 switch (atoi (argv[n++])) {
636 case 0:
637 pDLayer->sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
638 break;
639 case 1:
640 pDLayer->sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
641 break;
642 case 2:
643 pDLayer->sSliceArgument.uiSliceMode = SM_RASTER_SLICE;
644 break;
645 case 3:
646 pDLayer->sSliceArgument.uiSliceMode = SM_SIZELIMITED_SLICE;
647 break;
648 default:
649 pDLayer->sSliceArgument.uiSliceMode = SM_RESERVED;
650 break;
651 }
652 }
653
654 else if (!strcmp (pCommand, "-slcsize") && (n + 1 < argc)) {
655 unsigned int iLayer = atoi (argv[n++]);
656 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
657 pDLayer->sSliceArgument.uiSliceSizeConstraint = atoi (argv[n++]);
658 }
659
660 else if (!strcmp (pCommand, "-slcnum") && (n + 1 < argc)) {
661 unsigned int iLayer = atoi (argv[n++]);
662 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
663 pDLayer->sSliceArgument.uiSliceNum = atoi (argv[n++]);
664 } else if (!strcmp (pCommand, "-slcmbnum") && (n + 1 < argc)) {
665 unsigned int iLayer = atoi (argv[n++]);
666 SSpatialLayerConfig* pDLayer = &pSvcParam.sSpatialLayers[iLayer];
667 pDLayer->sSliceArgument.uiSliceMbNum[0] = atoi (argv[n++]);
668 }
669 }
670 return 0;
671 }
672
673
674
FillSpecificParameters(SEncParamExt & sParam)675 int FillSpecificParameters (SEncParamExt& sParam) {
676 /* Test for temporal, spatial, SNR scalability */
677 sParam.iUsageType = CAMERA_VIDEO_REAL_TIME;
678 sParam.fMaxFrameRate = 60.0f; // input frame rate
679 sParam.iPicWidth = 1280; // width of picture in samples
680 sParam.iPicHeight = 720; // height of picture in samples
681 sParam.iTargetBitrate = 2500000; // target bitrate desired
682 sParam.iMaxBitrate = UNSPECIFIED_BIT_RATE;
683 sParam.iRCMode = RC_QUALITY_MODE; // rc mode control
684 sParam.iTemporalLayerNum = 3; // layer number at temporal level
685 sParam.iSpatialLayerNum = 4; // layer number at spatial level
686 sParam.bEnableDenoise = 0; // denoise control
687 sParam.bEnableBackgroundDetection = 1; // background detection control
688 sParam.bEnableAdaptiveQuant = 1; // adaptive quantization control
689 sParam.bEnableFrameSkip = 1; // frame skipping
690 sParam.bEnableLongTermReference = 0; // long term reference control
691 sParam.iLtrMarkPeriod = 30;
692 sParam.uiIntraPeriod = 320; // period of Intra frame
693 sParam.eSpsPpsIdStrategy = INCREASING_ID;
694 sParam.bPrefixNalAddingCtrl = 0;
695 sParam.iComplexityMode = LOW_COMPLEXITY;
696 sParam.bSimulcastAVC = false;
697 int iIndexLayer = 0;
698 sParam.sSpatialLayers[iIndexLayer].uiProfileIdc = PRO_BASELINE;
699 sParam.sSpatialLayers[iIndexLayer].iVideoWidth = 160;
700 sParam.sSpatialLayers[iIndexLayer].iVideoHeight = 90;
701 sParam.sSpatialLayers[iIndexLayer].fFrameRate = 7.5f;
702 sParam.sSpatialLayers[iIndexLayer].iSpatialBitrate = 64000;
703 sParam.sSpatialLayers[iIndexLayer].iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
704 sParam.sSpatialLayers[iIndexLayer].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
705
706 ++ iIndexLayer;
707 sParam.sSpatialLayers[iIndexLayer].uiProfileIdc = PRO_SCALABLE_BASELINE;
708 sParam.sSpatialLayers[iIndexLayer].iVideoWidth = 320;
709 sParam.sSpatialLayers[iIndexLayer].iVideoHeight = 180;
710 sParam.sSpatialLayers[iIndexLayer].fFrameRate = 15.0f;
711 sParam.sSpatialLayers[iIndexLayer].iSpatialBitrate = 160000;
712 sParam.sSpatialLayers[iIndexLayer].iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
713 sParam.sSpatialLayers[iIndexLayer].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
714
715 ++ iIndexLayer;
716 sParam.sSpatialLayers[iIndexLayer].uiProfileIdc = PRO_SCALABLE_BASELINE;
717 sParam.sSpatialLayers[iIndexLayer].iVideoWidth = 640;
718 sParam.sSpatialLayers[iIndexLayer].iVideoHeight = 360;
719 sParam.sSpatialLayers[iIndexLayer].fFrameRate = 30.0f;
720 sParam.sSpatialLayers[iIndexLayer].iSpatialBitrate = 512000;
721 sParam.sSpatialLayers[iIndexLayer].iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
722 sParam.sSpatialLayers[iIndexLayer].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
723 sParam.sSpatialLayers[iIndexLayer].sSliceArgument.uiSliceNum = 1;
724
725 ++ iIndexLayer;
726 sParam.sSpatialLayers[iIndexLayer].uiProfileIdc = PRO_SCALABLE_BASELINE;
727 sParam.sSpatialLayers[iIndexLayer].iVideoWidth = 1280;
728 sParam.sSpatialLayers[iIndexLayer].iVideoHeight = 720;
729 sParam.sSpatialLayers[iIndexLayer].fFrameRate = 30.0f;
730 sParam.sSpatialLayers[iIndexLayer].iSpatialBitrate = 1500000;
731 sParam.sSpatialLayers[iIndexLayer].iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
732 sParam.sSpatialLayers[iIndexLayer].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
733 sParam.sSpatialLayers[iIndexLayer].sSliceArgument.uiSliceNum = 1;
734
735 float fMaxFr = sParam.sSpatialLayers[sParam.iSpatialLayerNum - 1].fFrameRate;
736 for (int32_t i = sParam.iSpatialLayerNum - 2; i >= 0; -- i) {
737 if (sParam.sSpatialLayers[i].fFrameRate > fMaxFr + EPSN)
738 fMaxFr = sParam.sSpatialLayers[i].fFrameRate;
739 }
740 sParam.fMaxFrameRate = fMaxFr;
741
742 return 0;
743 }
744
ProcessEncoding(ISVCEncoder * pPtrEnc,int argc,char ** argv,bool bConfigFile)745 int ProcessEncoding (ISVCEncoder* pPtrEnc, int argc, char** argv, bool bConfigFile) {
746 int iRet = 0;
747
748 if (pPtrEnc == NULL)
749 return 1;
750
751 SFrameBSInfo sFbi;
752 SEncParamExt sSvcParam;
753 int64_t iStart = 0, iTotal = 0;
754
755 // Preparing encoding process
756 FILE* pFileYUV = NULL;
757 int32_t iActualFrameEncodedCount = 0;
758 int32_t iFrameIdx = 0;
759 int32_t iTotalFrameMax = -1;
760 uint8_t* pYUV = NULL;
761 SSourcePicture* pSrcPic = NULL;
762 uint32_t iSourceWidth, iSourceHeight, kiPicResSize;
763 // Inactive with sink with output file handler
764 FILE* pFpBs[4];
765 pFpBs[0] = pFpBs[1] = pFpBs[2] = pFpBs[3] = NULL;
766 #if defined(COMPARE_DATA)
767 //For getting the golden file handle
768 FILE* fpGolden = NULL;
769 #endif
770 #if defined ( STICK_STREAM_SIZE )
771 FILE* fTrackStream = fopen ("coding_size.stream", "wb");
772 #endif
773 SFilesSet fs;
774 // for configuration file
775 CReadConfig cRdCfg;
776 int iParsedNum = 1;
777
778 memset (&sFbi, 0, sizeof (SFrameBSInfo));
779 pPtrEnc->GetDefaultParams (&sSvcParam);
780 fs.bEnableMultiBsFile = false;
781
782 FillSpecificParameters (sSvcParam);
783 pSrcPic = new SSourcePicture;
784 if (pSrcPic == NULL) {
785 iRet = 1;
786 goto INSIDE_MEM_FREE;
787 }
788 //fill default pSrcPic
789 pSrcPic->iColorFormat = videoFormatI420;
790 pSrcPic->uiTimeStamp = 0;
791
792 // if configure file exit, reading configure file firstly
793 if (bConfigFile) {
794 iParsedNum = 2;
795 cRdCfg.Openf (argv[1]);
796 if (!cRdCfg.ExistFile()) {
797 fprintf (stderr, "Specified file: %s not exist, maybe invalid path or parameter settting.\n",
798 cRdCfg.GetFileName().c_str());
799 iRet = 1;
800 goto INSIDE_MEM_FREE;
801 }
802 iRet = ParseConfig (cRdCfg, pSrcPic, sSvcParam, fs);
803 if (iRet) {
804 fprintf (stderr, "parse svc parameter config file failed.\n");
805 iRet = 1;
806 goto INSIDE_MEM_FREE;
807 }
808 }
809 if (ParseCommandLine (argc - iParsedNum, argv + iParsedNum, pSrcPic, sSvcParam, fs) != 0) {
810 printf ("parse pCommand line failed\n");
811 iRet = 1;
812 goto INSIDE_MEM_FREE;
813 }
814 pPtrEnc->SetOption (ENCODER_OPTION_TRACE_LEVEL, &g_LevelSetting);
815 //finish reading the configurations
816 iSourceWidth = pSrcPic->iPicWidth;
817 iSourceHeight = pSrcPic->iPicHeight;
818 kiPicResSize = iSourceWidth * iSourceHeight * 3 >> 1;
819
820 pYUV = new uint8_t [kiPicResSize];
821 if (pYUV == NULL) {
822 iRet = 1;
823 goto INSIDE_MEM_FREE;
824 }
825
826 //update pSrcPic
827 pSrcPic->iStride[0] = iSourceWidth;
828 pSrcPic->iStride[1] = pSrcPic->iStride[2] = pSrcPic->iStride[0] >> 1;
829
830 pSrcPic->pData[0] = pYUV;
831 pSrcPic->pData[1] = pSrcPic->pData[0] + (iSourceWidth * iSourceHeight);
832 pSrcPic->pData[2] = pSrcPic->pData[1] + (iSourceWidth * iSourceHeight >> 2);
833
834 //update sSvcParam
835 sSvcParam.iPicWidth = 0;
836 sSvcParam.iPicHeight = 0;
837 for (int iLayer = 0; iLayer < sSvcParam.iSpatialLayerNum; iLayer++) {
838 SSpatialLayerConfig* pDLayer = &sSvcParam.sSpatialLayers[iLayer];
839 sSvcParam.iPicWidth = WELS_MAX (sSvcParam.iPicWidth, pDLayer->iVideoWidth);
840 sSvcParam.iPicHeight = WELS_MAX (sSvcParam.iPicHeight, pDLayer->iVideoHeight);
841 }
842 //if target output resolution is not set, use the source size
843 sSvcParam.iPicWidth = (!sSvcParam.iPicWidth) ? iSourceWidth : sSvcParam.iPicWidth;
844 sSvcParam.iPicHeight = (!sSvcParam.iPicHeight) ? iSourceHeight : sSvcParam.iPicHeight;
845
846 iTotalFrameMax = (int32_t)fs.uiFrameToBeCoded;
847 // sSvcParam.bSimulcastAVC = true;
848 if (cmResultSuccess != pPtrEnc->InitializeExt (&sSvcParam)) { // SVC encoder initialization
849 fprintf (stderr, "SVC encoder Initialize failed\n");
850 iRet = 1;
851 goto INSIDE_MEM_FREE;
852 }
853 for (int iLayer = 0; iLayer < MAX_DEPENDENCY_LAYER; iLayer++) {
854 if (fs.sRecFileName[iLayer][0] != 0) {
855 SDumpLayer sDumpLayer;
856 sDumpLayer.iLayer = iLayer;
857 sDumpLayer.pFileName = fs.sRecFileName[iLayer];
858 if (cmResultSuccess != pPtrEnc->SetOption (ENCODER_OPTION_DUMP_FILE, &sDumpLayer)) {
859 fprintf (stderr, "SetOption ENCODER_OPTION_DUMP_FILE failed!\n");
860 iRet = 1;
861 goto INSIDE_MEM_FREE;
862 }
863 }
864 }
865 // Inactive with sink with output file handler
866 if (fs.strBsFile.length() > 0) {
867 bool bFileOpenErr = false;
868 if (sSvcParam.iSpatialLayerNum == 1 || fs.bEnableMultiBsFile == false) {
869 pFpBs[0] = fopen (fs.strBsFile.c_str(), "wb");
870 bFileOpenErr = (pFpBs[0] == NULL);
871 } else { //enable multi bs file writing
872 string filename_layer;
873 string add_info[4] = {"_layer0", "_layer1", "_layer2", "_layer3"};
874 string::size_type found = fs.strBsFile.find_last_of ('.');
875 for (int i = 0; i < sSvcParam.iSpatialLayerNum; ++i) {
876 filename_layer = fs.strBsFile.insert (found, add_info[i]);
877 pFpBs[i] = fopen (filename_layer.c_str(), "wb");
878 fs.strBsFile = filename_layer.erase (found, 7);
879 bFileOpenErr |= (pFpBs[i] == NULL);
880 }
881 }
882 if (bFileOpenErr) {
883 fprintf (stderr, "Can not open file (%s) to write bitstream!\n", fs.strBsFile.c_str());
884 iRet = 1;
885 goto INSIDE_MEM_FREE;
886 }
887 } else {
888 fprintf (stderr, "Don't set the proper bitstream filename!\n");
889 iRet = 1;
890 goto INSIDE_MEM_FREE;
891 }
892
893 #if defined(COMPARE_DATA)
894 //For getting the golden file handle
895 if ((fpGolden = fopen (argv[3], "rb")) == NULL) {
896 fprintf (stderr, "Unable to open golden sequence file, check corresponding path!\n");
897 iRet = 1;
898 goto INSIDE_MEM_FREE;
899 }
900 #endif
901
902 pFileYUV = fopen (fs.strSeqFile.c_str(), "rb");
903 if (pFileYUV != NULL) {
904 #if defined(_WIN32) || defined(_WIN64)
905 #if _MSC_VER >= 1400
906 if (!_fseeki64 (pFileYUV, 0, SEEK_END)) {
907 int64_t i_size = _ftelli64 (pFileYUV);
908 _fseeki64 (pFileYUV, 0, SEEK_SET);
909 iTotalFrameMax = WELS_MAX ((int32_t) (i_size / kiPicResSize), iTotalFrameMax);
910 }
911 #else
912 if (!fseek (pFileYUV, 0, SEEK_END)) {
913 int64_t i_size = ftell (pFileYUV);
914 fseek (pFileYUV, 0, SEEK_SET);
915 iTotalFrameMax = WELS_MAX ((int32_t) (i_size / kiPicResSize), iTotalFrameMax);
916 }
917 #endif
918 #else
919 if (!fseeko (pFileYUV, 0, SEEK_END)) {
920 int64_t i_size = ftello (pFileYUV);
921 fseeko (pFileYUV, 0, SEEK_SET);
922 iTotalFrameMax = WELS_MAX ((int32_t) (i_size / kiPicResSize), iTotalFrameMax);
923 }
924 #endif
925 } else {
926 fprintf (stderr, "Unable to open source sequence file (%s), check corresponding path!\n",
927 fs.strSeqFile.c_str());
928 iRet = 1;
929 goto INSIDE_MEM_FREE;
930 }
931
932 iFrameIdx = 0;
933 while (iFrameIdx < iTotalFrameMax && (((int32_t)fs.uiFrameToBeCoded <= 0)
934 || (iFrameIdx < (int32_t)fs.uiFrameToBeCoded))) {
935
936 #ifdef ONLY_ENC_FRAMES_NUM
937 // Only encoded some limited frames here
938 if (iActualFrameEncodedCount >= ONLY_ENC_FRAMES_NUM) {
939 break;
940 }
941 #endif//ONLY_ENC_FRAMES_NUM
942 bool bCanBeRead = false;
943 bCanBeRead = (fread (pYUV, 1, kiPicResSize, pFileYUV) == kiPicResSize);
944
945 if (!bCanBeRead)
946 break;
947 // To encoder this frame
948 iStart = WelsTime();
949 pSrcPic->uiTimeStamp = WELS_ROUND (iFrameIdx * (1000 / sSvcParam.fMaxFrameRate));
950 int iEncFrames = pPtrEnc->EncodeFrame (pSrcPic, &sFbi);
951 iTotal += WelsTime() - iStart;
952 ++ iFrameIdx;
953 if (videoFrameTypeSkip == sFbi.eFrameType) {
954 continue;
955 }
956
957 if (iEncFrames == cmResultSuccess) {
958 int iLayer = 0;
959 int iFrameSize = 0;
960 while (iLayer < sFbi.iLayerNum) {
961 SLayerBSInfo* pLayerBsInfo = &sFbi.sLayerInfo[iLayer];
962 if (pLayerBsInfo != NULL) {
963 int iLayerSize = 0;
964 int iNalIdx = pLayerBsInfo->iNalCount - 1;
965 do {
966 iLayerSize += pLayerBsInfo->pNalLengthInByte[iNalIdx];
967 -- iNalIdx;
968 } while (iNalIdx >= 0);
969 #if defined(COMPARE_DATA)
970 //Comparing the result of encoder with golden pData
971 {
972 unsigned char* pUCArry = new unsigned char [iLayerSize];
973
974 fread (pUCArry, 1, iLayerSize, fpGolden);
975
976 for (int w = 0; w < iLayerSize; w++) {
977 if (pUCArry[w] != pLayerBsInfo->pBsBuf[w]) {
978 fprintf (stderr, "error @frame%d/layer%d/byte%d!!!!!!!!!!!!!!!!!!!!!!!!\n", iFrameIdx, iLayer, w);
979 //fprintf(stderr, "%x - %x\n", pUCArry[w], pLayerBsInfo->pBsBuf[w]);
980 break;
981 }
982 }
983 fprintf (stderr, "frame%d/layer%d comparation completed!\n", iFrameIdx, iLayer);
984
985 delete [] pUCArry;
986 }
987 #endif
988 if (sSvcParam.iSpatialLayerNum == 1 || fs.bEnableMultiBsFile == false)
989 fwrite (pLayerBsInfo->pBsBuf, 1, iLayerSize, pFpBs[0]); // write pure bit stream into file
990 else { //multi bs file write
991 if (pLayerBsInfo->uiSpatialId == 0) {
992 unsigned char five_bits = pLayerBsInfo->pBsBuf[4] & 0x1f;
993 if ((five_bits == 0x07) || (five_bits == 0x08)) { //sps or pps
994 for (int i = 0; i < sSvcParam.iSpatialLayerNum; ++i) {
995 fwrite (pLayerBsInfo->pBsBuf, 1, iLayerSize, pFpBs[i]);
996 }
997 } else {
998 fwrite (pLayerBsInfo->pBsBuf, 1, iLayerSize, pFpBs[0]);
999 }
1000 } else {
1001 fwrite (pLayerBsInfo->pBsBuf, 1, iLayerSize, pFpBs[pLayerBsInfo->uiSpatialId]);
1002 }
1003 }
1004 iFrameSize += iLayerSize;
1005 }
1006 ++ iLayer;
1007 }
1008 #if defined (STICK_STREAM_SIZE)
1009 if (fTrackStream) {
1010 fwrite (&iFrameSize, 1, sizeof (int), fTrackStream);
1011 }
1012 #endif//STICK_STREAM_SIZE
1013 ++ iActualFrameEncodedCount; // excluding skipped frame time
1014 } else {
1015 fprintf (stderr, "EncodeFrame(), ret: %d, frame index: %d.\n", iEncFrames, iFrameIdx);
1016 }
1017
1018 }
1019
1020 if (iActualFrameEncodedCount > 0) {
1021 double dElapsed = iTotal / 1e6;
1022 printf ("Width:\t\t%d\nHeight:\t\t%d\nFrames:\t\t%d\nencode time:\t%f sec\nFPS:\t\t%f fps\n",
1023 sSvcParam.iPicWidth, sSvcParam.iPicHeight,
1024 iActualFrameEncodedCount, dElapsed, (iActualFrameEncodedCount * 1.0) / dElapsed);
1025 #if defined (WINDOWS_PHONE)
1026 g_fFPS = (iActualFrameEncodedCount * 1.0f) / (float) dElapsed;
1027 g_dEncoderTime = dElapsed;
1028 g_iEncodedFrame = iActualFrameEncodedCount;
1029 #endif
1030 }
1031 INSIDE_MEM_FREE:
1032 for (int i = 0; i < sSvcParam.iSpatialLayerNum; ++i) {
1033 if (pFpBs[i]) {
1034 fclose (pFpBs[i]);
1035 pFpBs[i] = NULL;
1036 }
1037 }
1038 #if defined (STICK_STREAM_SIZE)
1039 if (fTrackStream) {
1040 fclose (fTrackStream);
1041 fTrackStream = NULL;
1042 }
1043 #endif
1044 #if defined (COMPARE_DATA)
1045 if (fpGolden) {
1046 fclose (fpGolden);
1047 fpGolden = NULL;
1048 }
1049 #endif
1050 // Destruction memory introduced in this routine
1051 if (pFileYUV != NULL) {
1052 fclose (pFileYUV);
1053 pFileYUV = NULL;
1054 }
1055 if (pYUV) {
1056 delete[] pYUV;
1057 pYUV = NULL;
1058 }
1059 if (pSrcPic) {
1060 delete pSrcPic;
1061 pSrcPic = NULL;
1062 }
1063 return iRet;
1064 }
1065
1066 // Merge from Heifei's Wonder. Lock process to a single core
LockToSingleCore()1067 void LockToSingleCore() {
1068 #ifdef HAVE_PROCESS_AFFINITY
1069 //for 2005 compiler, change "DWORD" to "DWORD_PTR"
1070 ULONG_PTR ProcessAffMask = 0, SystemAffMask = 0;
1071 HANDLE hProcess = GetCurrentProcess();
1072
1073 GetProcessAffinityMask (hProcess, &ProcessAffMask, &SystemAffMask);
1074 if (ProcessAffMask > 1) {
1075 // more than one CPU core available. Fix to only one:
1076 if (ProcessAffMask & 2) {
1077 ProcessAffMask = 2;
1078 } else {
1079 ProcessAffMask = 1;
1080 }
1081 // Lock process to a single CPU core
1082 SetProcessAffinityMask (hProcess, ProcessAffMask);
1083 }
1084
1085 // set high priority to avoid interrupts during test
1086 SetPriorityClass (hProcess, REALTIME_PRIORITY_CLASS);
1087 #endif
1088 return ;
1089 }
1090
CreateSVCEncHandle(ISVCEncoder ** ppEncoder)1091 int32_t CreateSVCEncHandle (ISVCEncoder** ppEncoder) {
1092 int32_t ret = 0;
1093 ret = WelsCreateSVCEncoder (ppEncoder);
1094 return ret;
1095 }
1096
DestroySVCEncHandle(ISVCEncoder * pEncoder)1097 void DestroySVCEncHandle (ISVCEncoder* pEncoder) {
1098 if (pEncoder) {
1099 WelsDestroySVCEncoder (pEncoder);
1100
1101 }
1102 }
1103
1104 /****************************************************************************
1105 * main:
1106 ****************************************************************************/
1107 #if defined(ANDROID_NDK) || defined(APPLE_IOS) || defined (WINDOWS_PHONE)
EncMain(int argc,char ** argv)1108 extern "C" int EncMain (int argc, char** argv)
1109 #else
1110 int main (int argc, char** argv)
1111 #endif
1112 {
1113 ISVCEncoder* pSVCEncoder = NULL;
1114 int iRet = 0;
1115
1116 #ifdef _MSC_VER
1117 _setmode (_fileno (stdin), _O_BINARY); /* thanks to Marcoss Morais <morais at dee.ufcg.edu.br> */
1118 _setmode (_fileno (stdout), _O_BINARY);
1119
1120 // remove the LOCK_TO_SINGLE_CORE micro, user need to enable it with manual
1121 // LockToSingleCore();
1122 #endif
1123
1124 /* Control-C handler */
1125 signal (SIGINT, SigIntHandler);
1126
1127 iRet = CreateSVCEncHandle (&pSVCEncoder);
1128 if (iRet) {
1129 cout << "WelsCreateSVCEncoder() failed!!" << endl;
1130 goto exit;
1131 }
1132
1133 if (argc < 2) {
1134 goto exit;
1135 } else {
1136 if (!strstr (argv[1], ".cfg")) { // check configuration type (like .cfg?)
1137 if (argc > 2) {
1138 iRet = ProcessEncoding (pSVCEncoder, argc, argv, false);
1139 if (iRet != 0)
1140 goto exit;
1141 } else if (argc == 2 && ! strcmp (argv[1], "-h"))
1142 PrintHelp();
1143 else {
1144 cout << "You specified pCommand is invalid!!" << endl;
1145 goto exit;
1146 }
1147 } else {
1148 iRet = ProcessEncoding (pSVCEncoder, argc, argv, true);
1149 if (iRet > 0)
1150 goto exit;
1151 }
1152 }
1153
1154 DestroySVCEncHandle (pSVCEncoder);
1155 return 0;
1156
1157 exit:
1158 DestroySVCEncHandle (pSVCEncoder);
1159 PrintHelp();
1160 return 1;
1161 }
1162