Imported Upstream version 1.4+222+hg5f9f7194267b
[deb_x265.git] / source / common / param.cpp
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #include "common.h"
25 #include "slice.h"
26 #include "threading.h"
27 #include "param.h"
28 #include "cpu.h"
29 #include "x265.h"
30
31 #if _MSC_VER
32 #pragma warning(disable: 4996) // POSIX functions are just fine, thanks
33 #pragma warning(disable: 4706) // assignment within conditional
34 #pragma warning(disable: 4127) // conditional expression is constant
35 #endif
36
37 #if _WIN32
38 #define strcasecmp _stricmp
39 #endif
40
41 #if !defined(HAVE_STRTOK_R)
42
43 /*
44 * adapted from public domain strtok_r() by Charlie Gordon
45 *
46 * from comp.lang.c 9/14/2007
47 *
48 * http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
49 *
50 * (Declaration that it's public domain):
51 * http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
52 */
53
54 #undef strtok_r
55 char* strtok_r(char * str,
56 const char *delim,
57 char ** nextp)
58 {
59 if (!str)
60 str = *nextp;
61
62 str += strspn(str, delim);
63
64 if (!*str)
65 return NULL;
66
67 char *ret = str;
68
69 str += strcspn(str, delim);
70
71 if (*str)
72 *str++ = '\0';
73
74 *nextp = str;
75
76 return ret;
77 }
78
79 #endif // if !defined(HAVE_STRTOK_R)
80
81 using namespace x265;
82
83 extern "C"
84 x265_param *x265_param_alloc()
85 {
86 return (x265_param*)x265_malloc(sizeof(x265_param));
87 }
88
89 extern "C"
90 void x265_param_free(x265_param *p)
91 {
92 return x265_free(p);
93 }
94
95 extern "C"
96 void x265_param_default(x265_param *param)
97 {
98 memset(param, 0, sizeof(x265_param));
99
100 /* Applying default values to all elements in the param structure */
101 param->cpuid = x265::cpu_detect();
102 param->bEnableWavefront = 1;
103 param->poolNumThreads = 0;
104 param->frameNumThreads = 0;
105
106 param->logLevel = X265_LOG_INFO;
107 param->csvfn = NULL;
108 param->rc.lambdaFileName = NULL;
109 param->bLogCuStats = 0;
110 param->decodedPictureHashSEI = 0;
111
112 /* Quality Measurement Metrics */
113 param->bEnablePsnr = 0;
114 param->bEnableSsim = 0;
115
116 /* Source specifications */
117 param->internalBitDepth = x265_max_bit_depth;
118 param->internalCsp = X265_CSP_I420;
119
120 param->levelIdc = 0;
121 param->bHighTier = 0;
122 param->interlaceMode = 0;
123 param->bRepeatHeaders = 0;
124 param->bEnableAccessUnitDelimiters = 0;
125 param->bEmitHRDSEI = 0;
126 param->bEmitInfoSEI = 1;
127
128 /* CU definitions */
129 param->maxCUSize = 64;
130 param->tuQTMaxInterDepth = 1;
131 param->tuQTMaxIntraDepth = 1;
132
133 /* Coding Structure */
134 param->keyframeMin = 0;
135 param->keyframeMax = 250;
136 param->bOpenGOP = 1;
137 param->bframes = 4;
138 param->lookaheadDepth = 20;
139 param->bFrameAdaptive = X265_B_ADAPT_TRELLIS;
140 param->bBPyramid = 1;
141 param->scenecutThreshold = 40; /* Magic number pulled in from x264 */
142
143 /* Intra Coding Tools */
144 param->bEnableConstrainedIntra = 0;
145 param->bEnableStrongIntraSmoothing = 1;
146 param->bEnableFastIntra = 0;
147
148 /* Inter Coding tools */
149 param->searchMethod = X265_HEX_SEARCH;
150 param->subpelRefine = 2;
151 param->searchRange = 57;
152 param->maxNumMergeCand = 2;
153 param->bEnableWeightedPred = 1;
154 param->bEnableWeightedBiPred = 0;
155 param->bEnableEarlySkip = 0;
156 param->bEnableCbfFastMode = 0;
157 param->bEnableAMP = 0;
158 param->bEnableRectInter = 0;
159 param->rdLevel = 3;
160 param->bEnableSignHiding = 1;
161 param->bEnableTransformSkip = 0;
162 param->bEnableTSkipFast = 0;
163 param->maxNumReferences = 3;
164 param->bEnableTemporalMvp = 1;
165
166 /* Loop Filter */
167 param->bEnableLoopFilter = 1;
168
169 /* SAO Loop Filter */
170 param->bEnableSAO = 1;
171 param->bSaoNonDeblocked = 0;
172
173 /* Coding Quality */
174 param->cbQpOffset = 0;
175 param->crQpOffset = 0;
176 param->rdPenalty = 0;
177 param->psyRd = 0.0;
178 param->psyRdoq = 0.0;
179 param->analysisMode = 0;
180 param->analysisFileName = NULL;
181 param->bIntraInBFrames = 0;
182 param->bLossless = 0;
183 param->bCULossless = 0;
184
185 /* Rate control options */
186 param->rc.vbvMaxBitrate = 0;
187 param->rc.vbvBufferSize = 0;
188 param->rc.vbvBufferInit = 0.9;
189 param->rc.rfConstant = 28;
190 param->rc.bitrate = 0;
191 param->rc.rateTolerance = 1.0;
192 param->rc.qCompress = 0.6;
193 param->rc.ipFactor = 1.4f;
194 param->rc.pbFactor = 1.3f;
195 param->rc.qpStep = 4;
196 param->rc.rateControlMode = X265_RC_CRF;
197 param->rc.qp = 32;
198 param->rc.aqMode = X265_AQ_VARIANCE;
199 param->rc.aqStrength = 1.0;
200 param->rc.cuTree = 1;
201 param->rc.rfConstantMax = 0;
202 param->rc.rfConstantMin = 0;
203 param->rc.bStatRead = 0;
204 param->rc.bStatWrite = 0;
205 param->rc.statFileName = NULL;
206 param->rc.complexityBlur = 20;
207 param->rc.qblur = 0.5;
208 param->rc.bEnableSlowFirstPass = 0;
209
210 /* Video Usability Information (VUI) */
211 param->vui.aspectRatioIdc = 0;
212 param->vui.sarWidth = 0;
213 param->vui.sarHeight = 0;
214 param->vui.bEnableOverscanAppropriateFlag = 0;
215 param->vui.bEnableVideoSignalTypePresentFlag = 0;
216 param->vui.videoFormat = 5;
217 param->vui.bEnableVideoFullRangeFlag = 0;
218 param->vui.bEnableColorDescriptionPresentFlag = 0;
219 param->vui.colorPrimaries = 2;
220 param->vui.transferCharacteristics = 2;
221 param->vui.matrixCoeffs = 2;
222 param->vui.bEnableChromaLocInfoPresentFlag = 0;
223 param->vui.chromaSampleLocTypeTopField = 0;
224 param->vui.chromaSampleLocTypeBottomField = 0;
225 param->vui.bEnableDefaultDisplayWindowFlag = 0;
226 param->vui.defDispWinLeftOffset = 0;
227 param->vui.defDispWinRightOffset = 0;
228 param->vui.defDispWinTopOffset = 0;
229 param->vui.defDispWinBottomOffset = 0;
230 }
231
232 extern "C"
233 int x265_param_default_preset(x265_param *param, const char *preset, const char *tune)
234 {
235 x265_param_default(param);
236
237 if (preset)
238 {
239 char *end;
240 int i = strtol(preset, &end, 10);
241 if (*end == 0 && i >= 0 && i < (int)(sizeof(x265_preset_names) / sizeof(*x265_preset_names) - 1))
242 preset = x265_preset_names[i];
243
244 if (!strcmp(preset, "ultrafast"))
245 {
246 param->lookaheadDepth = 10;
247 param->scenecutThreshold = 0; // disable lookahead
248 param->maxCUSize = 32;
249 param->searchRange = 25;
250 param->bFrameAdaptive = 0;
251 param->subpelRefine = 0;
252 param->searchMethod = X265_DIA_SEARCH;
253 param->bEnableEarlySkip = 1;
254 param->bEnableSAO = 0;
255 param->bEnableSignHiding = 0;
256 param->bEnableWeightedPred = 0;
257 param->rdLevel = 2;
258 param->maxNumReferences = 1;
259 param->bEnableLoopFilter = 0;
260 param->rc.aqStrength = 0.0;
261 param->rc.aqMode = X265_AQ_NONE;
262 param->rc.cuTree = 0;
263 param->bEnableFastIntra = 1;
264 }
265 else if (!strcmp(preset, "superfast"))
266 {
267 param->lookaheadDepth = 10;
268 param->maxCUSize = 32;
269 param->searchRange = 44;
270 param->bFrameAdaptive = 0;
271 param->subpelRefine = 1;
272 param->bEnableEarlySkip = 1;
273 param->bEnableWeightedPred = 0;
274 param->rdLevel = 2;
275 param->maxNumReferences = 1;
276 param->rc.aqStrength = 0.0;
277 param->rc.aqMode = X265_AQ_NONE;
278 param->rc.cuTree = 0;
279 param->bEnableSAO = 0;
280 param->bEnableFastIntra = 1;
281 }
282 else if (!strcmp(preset, "veryfast"))
283 {
284 param->lookaheadDepth = 15;
285 param->maxCUSize = 32;
286 param->bFrameAdaptive = 0;
287 param->subpelRefine = 1;
288 param->bEnableEarlySkip = 1;
289 param->rdLevel = 2;
290 param->maxNumReferences = 1;
291 param->rc.cuTree = 0;
292 param->bEnableFastIntra = 1;
293 }
294 else if (!strcmp(preset, "faster"))
295 {
296 param->lookaheadDepth = 15;
297 param->bFrameAdaptive = 0;
298 param->bEnableEarlySkip = 1;
299 param->rdLevel = 2;
300 param->maxNumReferences = 1;
301 param->rc.cuTree = 0;
302 param->bEnableFastIntra = 1;
303 }
304 else if (!strcmp(preset, "fast"))
305 {
306 param->lookaheadDepth = 15;
307 param->bFrameAdaptive = 0;
308 param->rdLevel = 2;
309 param->maxNumReferences = 2;
310 param->bEnableFastIntra = 1;
311 }
312 else if (!strcmp(preset, "medium"))
313 {
314 /* defaults */
315 }
316 else if (!strcmp(preset, "slow"))
317 {
318 param->bEnableRectInter = 1;
319 param->lookaheadDepth = 25;
320 param->rdLevel = 4;
321 param->subpelRefine = 3;
322 param->maxNumMergeCand = 3;
323 param->searchMethod = X265_STAR_SEARCH;
324 }
325 else if (!strcmp(preset, "slower"))
326 {
327 param->bEnableWeightedBiPred = 1;
328 param->bEnableAMP = 1;
329 param->bEnableRectInter = 1;
330 param->lookaheadDepth = 30;
331 param->bframes = 8;
332 param->tuQTMaxInterDepth = 2;
333 param->tuQTMaxIntraDepth = 2;
334 param->rdLevel = 6;
335 param->subpelRefine = 3;
336 param->maxNumMergeCand = 3;
337 param->searchMethod = X265_STAR_SEARCH;
338 param->bIntraInBFrames = 1;
339 }
340 else if (!strcmp(preset, "veryslow"))
341 {
342 param->bEnableWeightedBiPred = 1;
343 param->bEnableAMP = 1;
344 param->bEnableRectInter = 1;
345 param->lookaheadDepth = 40;
346 param->bframes = 8;
347 param->tuQTMaxInterDepth = 3;
348 param->tuQTMaxIntraDepth = 3;
349 param->rdLevel = 6;
350 param->subpelRefine = 4;
351 param->maxNumMergeCand = 4;
352 param->searchMethod = X265_STAR_SEARCH;
353 param->maxNumReferences = 5;
354 param->bIntraInBFrames = 1;
355 }
356 else if (!strcmp(preset, "placebo"))
357 {
358 param->bEnableWeightedBiPred = 1;
359 param->bEnableAMP = 1;
360 param->bEnableRectInter = 1;
361 param->lookaheadDepth = 60;
362 param->searchRange = 92;
363 param->bframes = 8;
364 param->tuQTMaxInterDepth = 4;
365 param->tuQTMaxIntraDepth = 4;
366 param->rdLevel = 6;
367 param->subpelRefine = 5;
368 param->maxNumMergeCand = 5;
369 param->searchMethod = X265_STAR_SEARCH;
370 param->bEnableTransformSkip = 1;
371 param->maxNumReferences = 5;
372 param->rc.bEnableSlowFirstPass = 1;
373 param->bIntraInBFrames = 1;
374 // TODO: optimized esa
375 }
376 else
377 return -1;
378 }
379 if (tune)
380 {
381 if (!strcmp(tune, "psnr"))
382 {
383 param->rc.aqStrength = 0.0;
384 param->psyRd = 0.0;
385 param->psyRdoq = 0.0;
386 }
387 else if (!strcmp(tune, "ssim"))
388 {
389 param->rc.aqMode = X265_AQ_AUTO_VARIANCE;
390 param->psyRd = 0.0;
391 param->psyRdoq = 0.0;
392 }
393 else if (!strcmp(tune, "fastdecode") ||
394 !strcmp(tune, "fast-decode"))
395 {
396 param->bEnableLoopFilter = 0;
397 param->bEnableSAO = 0;
398 param->bEnableWeightedPred = 0;
399 param->bEnableWeightedBiPred = 0;
400 param->bIntraInBFrames = 0;
401 }
402 else if (!strcmp(tune, "zerolatency") ||
403 !strcmp(tune, "zero-latency"))
404 {
405 param->bFrameAdaptive = 0;
406 param->bframes = 0;
407 param->lookaheadDepth = 0;
408 param->scenecutThreshold = 0;
409 param->rc.cuTree = 0;
410 }
411 else if (!strcmp(tune, "grain"))
412 {
413 param->deblockingFilterBetaOffset = -2;
414 param->deblockingFilterTCOffset = -2;
415 param->bIntraInBFrames = 0;
416 param->psyRdoq = 30;
417 param->psyRd = 0.5;
418 param->rc.ipFactor = 1.1;
419 param->rc.pbFactor = 1.1;
420 param->rc.aqMode = X265_AQ_VARIANCE;
421 param->rc.aqStrength = 0.3;
422 param->rc.qCompress = 0.8;
423 }
424 else if (!strcmp(tune, "cbr"))
425 {
426 param->rc.pbFactor = 1.0;
427 param->rc.rateTolerance = 0.5;
428 }
429 else
430 return -1;
431 }
432
433 return 0;
434 }
435
436 static int x265_atobool(const char *str, bool& bError)
437 {
438 if (!strcmp(str, "1") ||
439 !strcmp(str, "true") ||
440 !strcmp(str, "yes"))
441 return 1;
442 if (!strcmp(str, "0") ||
443 !strcmp(str, "false") ||
444 !strcmp(str, "no"))
445 return 0;
446 bError = true;
447 return 0;
448 }
449
450 static double x265_atof(const char *str, bool& bError)
451 {
452 char *end;
453 double v = strtod(str, &end);
454
455 if (end == str || *end != '\0')
456 bError = true;
457 return v;
458 }
459
460 static int parseName(const char *arg, const char * const * names, bool& bError)
461 {
462 for (int i = 0; names[i]; i++)
463 {
464 if (!strcmp(arg, names[i]))
465 {
466 return i;
467 }
468 }
469
470 return x265_atoi(arg, bError);
471 }
472
473 /* internal versions of string-to-int with additional error checking */
474 #undef atoi
475 #undef atof
476 #define atoi(str) x265_atoi(str, bError)
477 #define atof(str) x265_atof(str, bError)
478 #define atobool(str) (bNameWasBool = true, x265_atobool(str, bError))
479
480 extern "C"
481 int x265_param_parse(x265_param *p, const char *name, const char *value)
482 {
483 bool bError = false;
484 bool bNameWasBool = false;
485 bool bValueWasNull = !value;
486 char nameBuf[64];
487
488 if (!name)
489 return X265_PARAM_BAD_NAME;
490
491 // skip -- prefix if provided
492 if (name[0] == '-' && name[1] == '-')
493 name += 2;
494
495 // s/_/-/g
496 if (strlen(name) + 1 < sizeof(nameBuf) && strchr(name, '_'))
497 {
498 char *c;
499 strcpy(nameBuf, name);
500 while ((c = strchr(nameBuf, '_')) != 0)
501 {
502 *c = '-';
503 }
504
505 name = nameBuf;
506 }
507
508 if (!strncmp(name, "no-", 3))
509 {
510 name += 3;
511 value = !value || x265_atobool(value, bError) ? "false" : "true";
512 }
513 else if (!strncmp(name, "no", 2))
514 {
515 name += 2;
516 value = !value || x265_atobool(value, bError) ? "false" : "true";
517 }
518 else if (!value)
519 value = "true";
520 else if (value[0] == '=')
521 value++;
522
523 #if defined(_MSC_VER)
524 #pragma warning(disable: 4127) // conditional expression is constant
525 #endif
526 #define OPT(STR) else if (!strcmp(name, STR))
527 #define OPT2(STR1, STR2) else if (!strcmp(name, STR1) || !strcmp(name, STR2))
528 if (0) ;
529 OPT("asm")
530 {
531 if (bValueWasNull)
532 p->cpuid = atobool(value);
533 else
534 p->cpuid = parseCpuName(value, bError);
535 }
536 OPT("fps")
537 {
538 if (sscanf(value, "%u/%u", &p->fpsNum, &p->fpsDenom) == 2)
539 ;
540 else
541 {
542 float fps = (float)atof(value);
543 if (fps > 0 && fps <= INT_MAX / 1000)
544 {
545 p->fpsNum = (int)(fps * 1000 + .5);
546 p->fpsDenom = 1000;
547 }
548 else
549 {
550 p->fpsNum = atoi(value);
551 p->fpsDenom = 1;
552 }
553 }
554 }
555 OPT("threads") p->poolNumThreads = atoi(value);
556 OPT("frame-threads") p->frameNumThreads = atoi(value);
557 OPT("pmode") p->bDistributeModeAnalysis = atobool(value);
558 OPT("pme") p->bDistributeMotionEstimation = atobool(value);
559 OPT2("level-idc", "level")
560 {
561 /* allow "5.1" or "51", both converted to integer 51 */
562 if (atof(value) < 7)
563 p->levelIdc = (int)(10 * atof(value) + .5);
564 else
565 p->levelIdc = atoi(value);
566 }
567 OPT("high-tier") p->bHighTier = atobool(value);
568 OPT2("log-level", "log")
569 {
570 p->logLevel = atoi(value);
571 if (bError)
572 {
573 bError = false;
574 p->logLevel = parseName(value, logLevelNames, bError) - 1;
575 }
576 }
577 OPT("cu-stats") p->bLogCuStats = atobool(value);
578 OPT("repeat-headers") p->bRepeatHeaders = atobool(value);
579 OPT("wpp") p->bEnableWavefront = atobool(value);
580 OPT("ctu") p->maxCUSize = (uint32_t)atoi(value);
581 OPT("tu-intra-depth") p->tuQTMaxIntraDepth = (uint32_t)atoi(value);
582 OPT("tu-inter-depth") p->tuQTMaxInterDepth = (uint32_t)atoi(value);
583 OPT("subme") p->subpelRefine = atoi(value);
584 OPT("merange") p->searchRange = atoi(value);
585 OPT("rect") p->bEnableRectInter = atobool(value);
586 OPT("amp") p->bEnableAMP = atobool(value);
587 OPT("max-merge") p->maxNumMergeCand = (uint32_t)atoi(value);
588 OPT("temporal-mvp") p->bEnableTemporalMvp = atobool(value);
589 OPT("early-skip") p->bEnableEarlySkip = atobool(value);
590 OPT("fast-cbf") p->bEnableCbfFastMode = atobool(value);
591 OPT("rdpenalty") p->rdPenalty = atoi(value);
592 OPT("tskip") p->bEnableTransformSkip = atobool(value);
593 OPT("no-tskip-fast") p->bEnableTSkipFast = atobool(value);
594 OPT("tskip-fast") p->bEnableTSkipFast = atobool(value);
595 OPT("strong-intra-smoothing") p->bEnableStrongIntraSmoothing = atobool(value);
596 OPT("lossless") p->bLossless = atobool(value);
597 OPT("cu-lossless") p->bCULossless = atobool(value);
598 OPT("constrained-intra") p->bEnableConstrainedIntra = atobool(value);
599 OPT("fast-intra") p->bEnableFastIntra = atobool(value);
600 OPT("open-gop") p->bOpenGOP = atobool(value);
601 OPT("scenecut")
602 {
603 p->scenecutThreshold = atobool(value);
604 if (bError || p->scenecutThreshold)
605 {
606 bError = false;
607 p->scenecutThreshold = atoi(value);
608 }
609 }
610 OPT("keyint") p->keyframeMax = atoi(value);
611 OPT("min-keyint") p->keyframeMin = atoi(value);
612 OPT("rc-lookahead") p->lookaheadDepth = atoi(value);
613 OPT("bframes") p->bframes = atoi(value);
614 OPT("bframe-bias") p->bFrameBias = atoi(value);
615 OPT("b-adapt")
616 {
617 p->bFrameAdaptive = atobool(value);
618 if (bError || p->bFrameAdaptive)
619 {
620 bError = false;
621 p->bFrameAdaptive = atoi(value);
622 }
623 }
624 OPT("interlace")
625 {
626 p->interlaceMode = atobool(value);
627 if (bError || p->interlaceMode)
628 {
629 bError = false;
630 p->interlaceMode = parseName(value, x265_interlace_names, bError);
631 }
632 }
633 OPT("ref") p->maxNumReferences = atoi(value);
634 OPT("weightp") p->bEnableWeightedPred = atobool(value);
635 OPT("weightb") p->bEnableWeightedBiPred = atobool(value);
636 OPT("cbqpoffs") p->cbQpOffset = atoi(value);
637 OPT("crqpoffs") p->crQpOffset = atoi(value);
638 OPT("rd") p->rdLevel = atoi(value);
639 OPT("psy-rd") p->psyRd = atof(value);
640 OPT("psy-rdoq") p->psyRdoq = atof(value);
641 OPT("signhide") p->bEnableSignHiding = atobool(value);
642 OPT("b-intra") p->bIntraInBFrames = atobool(value);
643 OPT("lft") p->bEnableLoopFilter = atobool(value); /* DEPRECATED */
644 OPT("deblock")
645 {
646 if (2 == sscanf(value, "%d:%d", &p->deblockingFilterTCOffset, &p->deblockingFilterBetaOffset) ||
647 2 == sscanf(value, "%d,%d", &p->deblockingFilterTCOffset, &p->deblockingFilterBetaOffset))
648 {
649 p->bEnableLoopFilter = true;
650 }
651 else if (sscanf(value, "%d", &p->deblockingFilterTCOffset))
652 {
653 p->bEnableLoopFilter = 1;
654 p->deblockingFilterBetaOffset = p->deblockingFilterTCOffset;
655 }
656 else
657 p->bEnableLoopFilter = atobool(value);
658 }
659 OPT("sao") p->bEnableSAO = atobool(value);
660 OPT("sao-non-deblock") p->bSaoNonDeblocked = atobool(value);
661 OPT("ssim") p->bEnableSsim = atobool(value);
662 OPT("psnr") p->bEnablePsnr = atobool(value);
663 OPT("hash") p->decodedPictureHashSEI = atoi(value);
664 OPT("aud") p->bEnableAccessUnitDelimiters = atobool(value);
665 OPT("info") p->bEmitInfoSEI = atobool(value);
666 OPT("b-pyramid") p->bBPyramid = atobool(value);
667 OPT("hrd") p->bEmitHRDSEI = atobool(value);
668 OPT2("ipratio", "ip-factor") p->rc.ipFactor = atof(value);
669 OPT2("pbratio", "pb-factor") p->rc.pbFactor = atof(value);
670 OPT("qcomp") p->rc.qCompress = atof(value);
671 OPT("qpstep") p->rc.qpStep = atoi(value);
672 OPT("ratetol") p->rc.rateTolerance = atof(value);
673 OPT("cplxblur") p->rc.complexityBlur = atof(value);
674 OPT("qblur") p->rc.qblur = atof(value);
675 OPT("aq-mode") p->rc.aqMode = atoi(value);
676 OPT("aq-strength") p->rc.aqStrength = atof(value);
677 OPT("vbv-maxrate") p->rc.vbvMaxBitrate = atoi(value);
678 OPT("vbv-bufsize") p->rc.vbvBufferSize = atoi(value);
679 OPT("vbv-init") p->rc.vbvBufferInit = atof(value);
680 OPT("crf-max") p->rc.rfConstantMax = atof(value);
681 OPT("crf-min") p->rc.rfConstantMin = atof(value);
682 OPT("crf")
683 {
684 p->rc.rfConstant = atof(value);
685 p->rc.rateControlMode = X265_RC_CRF;
686 }
687 OPT("bitrate")
688 {
689 p->rc.bitrate = atoi(value);
690 p->rc.rateControlMode = X265_RC_ABR;
691 }
692 OPT("qp")
693 {
694 p->rc.qp = atoi(value);
695 p->rc.rateControlMode = X265_RC_CQP;
696 }
697 OPT("input-res") bError |= sscanf(value, "%dx%d", &p->sourceWidth, &p->sourceHeight) != 2;
698 OPT("input-csp") p->internalCsp = parseName(value, x265_source_csp_names, bError);
699 OPT("me") p->searchMethod = parseName(value, x265_motion_est_names, bError);
700 OPT("cutree") p->rc.cuTree = atobool(value);
701 OPT("slow-firstpass") p->rc.bEnableSlowFirstPass = atobool(value);
702 OPT("analysis-mode") p->analysisMode = parseName(value, x265_analysis_names, bError);
703 OPT("sar")
704 {
705 p->vui.aspectRatioIdc = parseName(value, x265_sar_names, bError);
706 if (bError)
707 {
708 p->vui.aspectRatioIdc = X265_EXTENDED_SAR;
709 bError = sscanf(value, "%d:%d", &p->vui.sarWidth, &p->vui.sarHeight) != 2;
710 }
711 }
712 OPT("overscan")
713 {
714 if (!strcmp(value, "show"))
715 p->vui.bEnableOverscanInfoPresentFlag = 1;
716 else if (!strcmp(value, "crop"))
717 {
718 p->vui.bEnableOverscanInfoPresentFlag = 1;
719 p->vui.bEnableOverscanAppropriateFlag = 1;
720 }
721 else if (!strcmp(value, "undef"))
722 p->vui.bEnableOverscanInfoPresentFlag = 0;
723 else
724 bError = true;
725 }
726 OPT("videoformat")
727 {
728 p->vui.bEnableVideoSignalTypePresentFlag = 1;
729 p->vui.videoFormat = parseName(value, x265_video_format_names, bError);
730 }
731 OPT("range")
732 {
733 p->vui.bEnableVideoSignalTypePresentFlag = 1;
734 p->vui.bEnableVideoFullRangeFlag = parseName(value, x265_fullrange_names, bError);
735 }
736 OPT("colorprim")
737 {
738 p->vui.bEnableVideoSignalTypePresentFlag = 1;
739 p->vui.bEnableColorDescriptionPresentFlag = 1;
740 p->vui.colorPrimaries = parseName(value, x265_colorprim_names, bError);
741 }
742 OPT("transfer")
743 {
744 p->vui.bEnableVideoSignalTypePresentFlag = 1;
745 p->vui.bEnableColorDescriptionPresentFlag = 1;
746 p->vui.transferCharacteristics = parseName(value, x265_transfer_names, bError);
747 }
748 OPT("colormatrix")
749 {
750 p->vui.bEnableVideoSignalTypePresentFlag = 1;
751 p->vui.bEnableColorDescriptionPresentFlag = 1;
752 p->vui.matrixCoeffs = parseName(value, x265_colmatrix_names, bError);
753 }
754 OPT("chromaloc")
755 {
756 p->vui.bEnableChromaLocInfoPresentFlag = 1;
757 p->vui.chromaSampleLocTypeTopField = atoi(value);
758 p->vui.chromaSampleLocTypeBottomField = p->vui.chromaSampleLocTypeTopField;
759 }
760 OPT("crop-rect")
761 {
762 p->vui.bEnableDefaultDisplayWindowFlag = 1;
763 bError |= sscanf(value, "%d,%d,%d,%d",
764 &p->vui.defDispWinLeftOffset,
765 &p->vui.defDispWinTopOffset,
766 &p->vui.defDispWinRightOffset,
767 &p->vui.defDispWinBottomOffset) != 4;
768 }
769 OPT("nr-intra") p->noiseReductionIntra = atoi(value);
770 OPT("nr-inter") p->noiseReductionInter = atoi(value);
771 OPT("pass")
772 {
773 int pass = Clip3(0, 3, atoi(value));
774 p->rc.bStatWrite = pass & 1;
775 p->rc.bStatRead = pass & 2;
776 }
777 OPT("stats") p->rc.statFileName = strdup(value);
778 OPT("csv") p->csvfn = strdup(value);
779 OPT("scaling-list") p->scalingLists = strdup(value);
780 OPT("lambda-file") p->rc.lambdaFileName = strdup(value);
781 OPT("analysis-file") p->analysisFileName = strdup(value);
782 else
783 return X265_PARAM_BAD_NAME;
784 #undef OPT
785 #undef atobool
786 #undef atoi
787 #undef atof
788
789 bError |= bValueWasNull && !bNameWasBool;
790 return bError ? X265_PARAM_BAD_VALUE : 0;
791 }
792
793 namespace x265 {
794 // internal encoder functions
795
796 int x265_atoi(const char *str, bool& bError)
797 {
798 char *end;
799 int v = strtol(str, &end, 0);
800
801 if (end == str || *end != '\0')
802 bError = true;
803 return v;
804 }
805
806 /* cpu name can be:
807 * auto || true - x265::cpu_detect()
808 * false || no - disabled
809 * integer bitmap value
810 * comma separated list of SIMD names, eg: SSE4.1,XOP */
811 int parseCpuName(const char *value, bool& bError)
812 {
813 if (!value)
814 {
815 bError = 1;
816 return 0;
817 }
818 int cpu;
819 if (isdigit(value[0]))
820 cpu = x265_atoi(value, bError);
821 else
822 cpu = !strcmp(value, "auto") || x265_atobool(value, bError) ? x265::cpu_detect() : 0;
823
824 if (bError)
825 {
826 char *buf = strdup(value);
827 char *tok, *saveptr = NULL, *init;
828 bError = 0;
829 cpu = 0;
830 for (init = buf; (tok = strtok_r(init, ",", &saveptr)); init = NULL)
831 {
832 int i;
833 for (i = 0; x265::cpu_names[i].flags && strcasecmp(tok, x265::cpu_names[i].name); i++)
834 {
835 }
836
837 cpu |= x265::cpu_names[i].flags;
838 if (!x265::cpu_names[i].flags)
839 bError = 1;
840 }
841
842 free(buf);
843 if ((cpu & X265_CPU_SSSE3) && !(cpu & X265_CPU_SSE2_IS_SLOW))
844 cpu |= X265_CPU_SSE2_IS_FAST;
845 }
846
847 return cpu;
848 }
849
850 static const int fixedRatios[][2] =
851 {
852 { 1, 1 },
853 { 12, 11 },
854 { 10, 11 },
855 { 16, 11 },
856 { 40, 33 },
857 { 24, 11 },
858 { 20, 11 },
859 { 32, 11 },
860 { 80, 33 },
861 { 18, 11 },
862 { 15, 11 },
863 { 64, 33 },
864 { 160, 99 },
865 { 4, 3 },
866 { 3, 2 },
867 { 2, 1 },
868 };
869
870 void setParamAspectRatio(x265_param *p, int width, int height)
871 {
872 p->vui.aspectRatioIdc = X265_EXTENDED_SAR;
873 p->vui.sarWidth = width;
874 p->vui.sarHeight = height;
875 for (size_t i = 0; i < sizeof(fixedRatios) / sizeof(fixedRatios[0]); i++)
876 {
877 if (width == fixedRatios[i][0] && height == fixedRatios[i][1])
878 {
879 p->vui.aspectRatioIdc = (int)i + 1;
880 return;
881 }
882 }
883 }
884
885 void getParamAspectRatio(x265_param *p, int& width, int& height)
886 {
887 if (!p->vui.aspectRatioIdc)
888 {
889 width = height = 0;
890 }
891 else if ((size_t)p->vui.aspectRatioIdc <= sizeof(fixedRatios) / sizeof(fixedRatios[0]))
892 {
893 width = fixedRatios[p->vui.aspectRatioIdc - 1][0];
894 height = fixedRatios[p->vui.aspectRatioIdc - 1][1];
895 }
896 else if (p->vui.aspectRatioIdc == X265_EXTENDED_SAR)
897 {
898 width = p->vui.sarWidth;
899 height = p->vui.sarHeight;
900 }
901 else
902 {
903 width = height = 0;
904 }
905 }
906
907 static inline int _confirm(x265_param *param, bool bflag, const char* message)
908 {
909 if (!bflag)
910 return 0;
911
912 x265_log(param, X265_LOG_ERROR, "%s\n", message);
913 return 1;
914 }
915
916 int x265_check_params(x265_param *param)
917 {
918 #define CHECK(expr, msg) check_failed |= _confirm(param, expr, msg)
919 int check_failed = 0; /* abort if there is a fatal configuration problem */
920
921 CHECK(param->maxCUSize != 64 && param->maxCUSize != 32 && param->maxCUSize != 16,
922 "max ctu size must be 16, 32, or 64");
923 if (check_failed == 1)
924 return check_failed;
925
926 uint32_t maxLog2CUSize = (uint32_t)g_log2Size[param->maxCUSize];
927 uint32_t tuQTMaxLog2Size = X265_MIN(maxLog2CUSize, 5);
928 uint32_t tuQTMinLog2Size = 2; //log2(4)
929
930 /* These checks might be temporary */
931 #if HIGH_BIT_DEPTH
932 CHECK(param->internalBitDepth != 10,
933 "x265 was compiled for 10bit encodes, only 10bit internal depth supported");
934 #else
935 CHECK(param->internalBitDepth != 8,
936 "x265 was compiled for 8bit encodes, only 8bit internal depth supported");
937 #endif
938
939 CHECK(param->rc.qp < -6 * (param->internalBitDepth - 8) || param->rc.qp > 51,
940 "QP exceeds supported range (-QpBDOffsety to 51)");
941 CHECK(param->fpsNum == 0 || param->fpsDenom == 0,
942 "Frame rate numerator and denominator must be specified");
943 CHECK(param->interlaceMode < 0 || param->interlaceMode > 2,
944 "Interlace mode must be 0 (progressive) 1 (top-field first) or 2 (bottom field first)");
945 CHECK(param->searchMethod<0 || param->searchMethod> X265_FULL_SEARCH,
946 "Search method is not supported value (0:DIA 1:HEX 2:UMH 3:HM 5:FULL)");
947 CHECK(param->searchRange < 0,
948 "Search Range must be more than 0");
949 CHECK(param->searchRange >= 32768,
950 "Search Range must be less than 32768");
951 CHECK(param->subpelRefine > X265_MAX_SUBPEL_LEVEL,
952 "subme must be less than or equal to X265_MAX_SUBPEL_LEVEL (7)");
953 CHECK(param->subpelRefine < 0,
954 "subme must be greater than or equal to 0");
955 CHECK(param->frameNumThreads < 0,
956 "frameNumThreads (--frame-threads) must be 0 or higher");
957 CHECK(param->cbQpOffset < -12, "Min. Chroma Cb QP Offset is -12");
958 CHECK(param->cbQpOffset > 12, "Max. Chroma Cb QP Offset is 12");
959 CHECK(param->crQpOffset < -12, "Min. Chroma Cr QP Offset is -12");
960 CHECK(param->crQpOffset > 12, "Max. Chroma Cr QP Offset is 12");
961
962 CHECK(tuQTMaxLog2Size > maxLog2CUSize,
963 "QuadtreeTULog2MaxSize must be log2(maxCUSize) or smaller.");
964
965 CHECK(param->tuQTMaxInterDepth < 1 || param->tuQTMaxInterDepth > 4,
966 "QuadtreeTUMaxDepthInter must be greater than 0 and less than 5");
967 CHECK(maxLog2CUSize < tuQTMinLog2Size + param->tuQTMaxInterDepth - 1,
968 "QuadtreeTUMaxDepthInter must be less than or equal to the difference between log2(maxCUSize) and QuadtreeTULog2MinSize plus 1");
969 CHECK(param->tuQTMaxIntraDepth < 1 || param->tuQTMaxIntraDepth > 4,
970 "QuadtreeTUMaxDepthIntra must be greater 0 and less than 5");
971 CHECK(maxLog2CUSize < tuQTMinLog2Size + param->tuQTMaxIntraDepth - 1,
972 "QuadtreeTUMaxDepthInter must be less than or equal to the difference between log2(maxCUSize) and QuadtreeTULog2MinSize plus 1");
973
974 CHECK(param->maxNumMergeCand < 1, "MaxNumMergeCand must be 1 or greater.");
975 CHECK(param->maxNumMergeCand > 5, "MaxNumMergeCand must be 5 or smaller.");
976
977 CHECK(param->maxNumReferences < 1, "maxNumReferences must be 1 or greater.");
978 CHECK(param->maxNumReferences > MAX_NUM_REF, "maxNumReferences must be 16 or smaller.");
979
980 CHECK(param->sourceWidth < (int)param->maxCUSize || param->sourceHeight < (int)param->maxCUSize,
981 "Picture size must be at least one CTU");
982 CHECK(param->internalCsp < X265_CSP_I420 || X265_CSP_I444 < param->internalCsp,
983 "Color space must be i420, i422, or i444");
984 CHECK(param->sourceWidth & !!CHROMA_H_SHIFT(param->internalCsp),
985 "Picture width must be an integer multiple of the specified chroma subsampling");
986 CHECK(param->sourceHeight & !!CHROMA_V_SHIFT(param->internalCsp),
987 "Picture height must be an integer multiple of the specified chroma subsampling");
988
989 CHECK(param->rc.rateControlMode > X265_RC_CRF || param->rc.rateControlMode < X265_RC_ABR,
990 "Rate control mode is out of range");
991 CHECK(param->rdLevel < 0 || param->rdLevel > 6,
992 "RD Level is out of range");
993 CHECK(param->bframes > param->lookaheadDepth && !param->rc.bStatRead,
994 "Lookahead depth must be greater than the max consecutive bframe count");
995 CHECK(param->bframes < 0,
996 "bframe count should be greater than zero");
997 CHECK(param->bframes > X265_BFRAME_MAX,
998 "max consecutive bframe count must be 16 or smaller");
999 CHECK(param->lookaheadDepth > X265_LOOKAHEAD_MAX,
1000 "Lookahead depth must be less than 256");
1001 CHECK(param->rc.aqMode < X265_AQ_NONE || X265_AQ_AUTO_VARIANCE < param->rc.aqMode,
1002 "Aq-Mode is out of range");
1003 CHECK(param->rc.aqStrength < 0 || param->rc.aqStrength > 3,
1004 "Aq-Strength is out of range");
1005 CHECK(param->deblockingFilterTCOffset < -6 || param->deblockingFilterTCOffset > 6,
1006 "deblocking filter tC offset must be in the range of -6 to +6");
1007 CHECK(param->deblockingFilterBetaOffset < -6 || param->deblockingFilterBetaOffset > 6,
1008 "deblocking filter Beta offset must be in the range of -6 to +6");
1009 CHECK(param->psyRd < 0 || 2.0 < param->psyRd, "Psy-rd strength must be between 0 and 2.0");
1010 CHECK(param->psyRdoq < 0 || 50.0 < param->psyRdoq, "Psy-rdoq strength must be between 0 and 50.0");
1011 CHECK(param->bEnableWavefront < 0, "WaveFrontSynchro cannot be negative");
1012 CHECK(!param->bEnableWavefront && param->rc.vbvBufferSize, "VBV requires wave-front parallelism (--wpp)");
1013 CHECK((param->vui.aspectRatioIdc < 0
1014 || param->vui.aspectRatioIdc > 16)
1015 && param->vui.aspectRatioIdc != X265_EXTENDED_SAR,
1016 "Sample Aspect Ratio must be 0-16 or 255");
1017 CHECK(param->vui.aspectRatioIdc == X265_EXTENDED_SAR && param->vui.sarWidth <= 0,
1018 "Sample Aspect Ratio width must be greater than 0");
1019 CHECK(param->vui.aspectRatioIdc == X265_EXTENDED_SAR && param->vui.sarHeight <= 0,
1020 "Sample Aspect Ratio height must be greater than 0");
1021 CHECK(param->vui.videoFormat < 0 || param->vui.videoFormat > 5,
1022 "Video Format must be component,"
1023 " pal, ntsc, secam, mac or undef");
1024 CHECK(param->vui.colorPrimaries < 0
1025 || param->vui.colorPrimaries > 9
1026 || param->vui.colorPrimaries == 3,
1027 "Color Primaries must be undef, bt709, bt470m,"
1028 " bt470bg, smpte170m, smpte240m, film or bt2020");
1029 CHECK(param->vui.transferCharacteristics < 0
1030 || param->vui.transferCharacteristics > 15
1031 || param->vui.transferCharacteristics == 3,
1032 "Transfer Characteristics must be undef, bt709, bt470m, bt470bg,"
1033 " smpte170m, smpte240m, linear, log100, log316, iec61966-2-4, bt1361e,"
1034 " iec61966-2-1, bt2020-10 or bt2020-12");
1035 CHECK(param->vui.matrixCoeffs < 0
1036 || param->vui.matrixCoeffs > 10
1037 || param->vui.matrixCoeffs == 3,
1038 "Matrix Coefficients must be undef, bt709, fcc, bt470bg, smpte170m,"
1039 " smpte240m, GBR, YCgCo, bt2020nc or bt2020c");
1040 CHECK(param->vui.chromaSampleLocTypeTopField < 0
1041 || param->vui.chromaSampleLocTypeTopField > 5,
1042 "Chroma Sample Location Type Top Field must be 0-5");
1043 CHECK(param->vui.chromaSampleLocTypeBottomField < 0
1044 || param->vui.chromaSampleLocTypeBottomField > 5,
1045 "Chroma Sample Location Type Bottom Field must be 0-5");
1046 CHECK(param->vui.defDispWinLeftOffset < 0,
1047 "Default Display Window Left Offset must be 0 or greater");
1048 CHECK(param->vui.defDispWinRightOffset < 0,
1049 "Default Display Window Right Offset must be 0 or greater");
1050 CHECK(param->vui.defDispWinTopOffset < 0,
1051 "Default Display Window Top Offset must be 0 or greater");
1052 CHECK(param->vui.defDispWinBottomOffset < 0,
1053 "Default Display Window Bottom Offset must be 0 or greater");
1054 CHECK(param->rc.rfConstant < -6 * (param->internalBitDepth - 8) || param->rc.rfConstant > 51,
1055 "Valid quality based range: -qpBDOffsetY to 51");
1056 CHECK(param->rc.rfConstantMax < -6 * (param->internalBitDepth - 8) || param->rc.rfConstantMax > 51,
1057 "Valid quality based range: -qpBDOffsetY to 51");
1058 CHECK(param->rc.rfConstantMin < -6 * (param->internalBitDepth - 8) || param->rc.rfConstantMin > 51,
1059 "Valid quality based range: -qpBDOffsetY to 51");
1060 CHECK(param->bFrameAdaptive < 0 || param->bFrameAdaptive > 2,
1061 "Valid adaptive b scheduling values 0 - none, 1 - fast, 2 - full");
1062 CHECK(param->logLevel<-1 || param->logLevel> X265_LOG_FULL,
1063 "Valid Logging level -1:none 0:error 1:warning 2:info 3:debug 4:full");
1064 CHECK(param->scenecutThreshold < 0,
1065 "scenecutThreshold must be greater than 0");
1066 CHECK(param->rdPenalty < 0 || param->rdPenalty > 2,
1067 "Valid penalty for 32x32 intra TU in non-I slices. 0:disabled 1:RD-penalty 2:maximum");
1068 CHECK(param->keyframeMax < -1,
1069 "Invalid max IDR period in frames. value should be greater than -1");
1070 CHECK(param->decodedPictureHashSEI < 0 || param->decodedPictureHashSEI > 3,
1071 "Invalid hash option. Decoded Picture Hash SEI 0: disabled, 1: MD5, 2: CRC, 3: Checksum");
1072 CHECK(param->rc.vbvBufferSize < 0,
1073 "Size of the vbv buffer can not be less than zero");
1074 CHECK(param->rc.vbvMaxBitrate < 0,
1075 "Maximum local bit rate can not be less than zero");
1076 CHECK(param->rc.vbvBufferInit < 0,
1077 "Valid initial VBV buffer occupancy must be a fraction 0 - 1, or size in kbits");
1078 CHECK(param->rc.bitrate < 0,
1079 "Target bitrate can not be less than zero");
1080 CHECK(param->rc.qCompress < 0.5 || param->rc.qCompress > 1.0,
1081 "qCompress must be between 0.5 and 1.0");
1082 if (param->noiseReductionIntra)
1083 CHECK(0 > param->noiseReductionIntra || param->noiseReductionIntra > 2000, "Valid noise reduction range 0 - 2000");
1084 if (param->noiseReductionInter)
1085 CHECK(0 > param->noiseReductionInter || param->noiseReductionInter > 2000, "Valid noise reduction range 0 - 2000");
1086 CHECK(param->rc.rateControlMode == X265_RC_CRF && param->rc.bStatRead,
1087 "Constant rate-factor is incompatible with 2pass");
1088 CHECK(param->rc.rateControlMode == X265_RC_CQP && param->rc.bStatRead,
1089 "Constant QP is incompatible with 2pass");
1090 return check_failed;
1091 }
1092
1093 void x265_param_apply_fastfirstpass(x265_param* param)
1094 {
1095 /* Set faster options in case of turbo firstpass */
1096 if (param->rc.bStatWrite && !param->rc.bStatRead)
1097 {
1098 param->maxNumReferences = 1;
1099 param->maxNumMergeCand = 1;
1100 param->bEnableRectInter = 0;
1101 param->bEnableFastIntra = 1;
1102 param->bEnableAMP = 0;
1103 param->searchMethod = X265_DIA_SEARCH;
1104 param->subpelRefine = X265_MIN(2, param->subpelRefine);
1105 param->bEnableEarlySkip = 1;
1106 param->rdLevel = X265_MIN(2, param->rdLevel);
1107 }
1108 }
1109
1110 int x265_set_globals(x265_param *param)
1111 {
1112 static int once /* = 0 */;
1113
1114 if (ATOMIC_INC(&once) > 1)
1115 {
1116 if (param->maxCUSize != g_maxCUSize)
1117 {
1118 x265_log(param, X265_LOG_ERROR, "maxCUSize must be the same for all encoders in a single process");
1119 return -1;
1120 }
1121 }
1122 else
1123 {
1124 uint32_t maxLog2CUSize = (uint32_t)g_log2Size[param->maxCUSize];
1125
1126 // set max CU width & height
1127 g_maxCUSize = param->maxCUSize;
1128 g_maxLog2CUSize = maxLog2CUSize;
1129
1130 // compute actual CU depth with respect to config depth and max transform size
1131 g_maxCUDepth = maxLog2CUSize - MIN_LOG2_CU_SIZE;
1132 g_maxFullDepth = maxLog2CUSize - LOG2_UNIT_SIZE;
1133
1134 // initialize partition order
1135 uint32_t* tmp = &g_zscanToRaster[0];
1136 initZscanToRaster(g_maxFullDepth, 1, 0, tmp);
1137 initRasterToZscan(g_maxFullDepth);
1138 }
1139 return 0;
1140 }
1141
1142 void x265_print_params(x265_param *param)
1143 {
1144 if (param->logLevel < X265_LOG_INFO)
1145 return;
1146
1147 #if HIGH_BIT_DEPTH
1148 x265_log(param, X265_LOG_INFO, "Internal bit depth : %d\n", param->internalBitDepth);
1149 #endif
1150 if (param->interlaceMode)
1151 x265_log(param, X265_LOG_INFO, "Interlaced field inputs : %s\n", x265_interlace_names[param->interlaceMode]);
1152
1153 x265_log(param, X265_LOG_INFO, "CTU size / RQT depth inter / intra : %d / %d / %d\n",
1154 param->maxCUSize, param->tuQTMaxInterDepth, param->tuQTMaxIntraDepth);
1155
1156 x265_log(param, X265_LOG_INFO, "ME / range / subpel / merge : %s / %d / %d / %d\n",
1157 x265_motion_est_names[param->searchMethod], param->searchRange, param->subpelRefine, param->maxNumMergeCand);
1158
1159 if (param->keyframeMax != INT_MAX || param->scenecutThreshold)
1160 x265_log(param, X265_LOG_INFO, "Keyframe min / max / scenecut : %d / %d / %d\n", param->keyframeMin, param->keyframeMax, param->scenecutThreshold);
1161 else
1162 x265_log(param, X265_LOG_INFO, "Keyframe min / max / scenecut : disabled\n");
1163
1164 if (param->cbQpOffset || param->crQpOffset)
1165 x265_log(param, X265_LOG_INFO, "Cb/Cr QP Offset : %d / %d\n", param->cbQpOffset, param->crQpOffset);
1166
1167 if (param->rdPenalty)
1168 x265_log(param, X265_LOG_INFO, "Intra 32x32 TU penalty type : %d\n", param->rdPenalty);
1169
1170 x265_log(param, X265_LOG_INFO, "Lookahead / bframes / badapt : %d / %d / %d\n", param->lookaheadDepth, param->bframes, param->bFrameAdaptive);
1171 x265_log(param, X265_LOG_INFO, "b-pyramid / weightp / weightb / refs: %d / %d / %d / %d\n",
1172 param->bBPyramid, param->bEnableWeightedPred, param->bEnableWeightedBiPred, param->maxNumReferences);
1173
1174 if (param->bLossless)
1175 x265_log(param, X265_LOG_INFO, "Rate Control : Lossless\n");
1176 else switch (param->rc.rateControlMode)
1177 {
1178 case X265_RC_ABR:
1179 x265_log(param, X265_LOG_INFO, "Rate Control / AQ-Strength / CUTree : ABR-%d kbps / %0.1f / %d\n", param->rc.bitrate,
1180 param->rc.aqStrength, param->rc.cuTree);
1181 break;
1182 case X265_RC_CQP:
1183 x265_log(param, X265_LOG_INFO, "Rate Control / AQ-Strength / CUTree : CQP-%d / %0.1f / %d\n", param->rc.qp, param->rc.aqStrength,
1184 param->rc.cuTree);
1185 break;
1186 case X265_RC_CRF:
1187 x265_log(param, X265_LOG_INFO, "Rate Control / AQ-Strength / CUTree : CRF-%0.1f / %0.1f / %d\n", param->rc.rfConstant,
1188 param->rc.aqStrength, param->rc.cuTree);
1189 break;
1190 }
1191
1192 if (param->rc.vbvBufferSize)
1193 x265_log(param, X265_LOG_INFO, "VBV/HRD buffer / max-rate / init : %d / %d / %.3f\n",
1194 param->rc.vbvBufferSize, param->rc.vbvMaxBitrate, param->rc.vbvBufferInit);
1195
1196 x265_log(param, X265_LOG_INFO, "tools: ");
1197 #define TOOLOPT(FLAG, STR) if (FLAG) fprintf(stderr, "%s ", STR)
1198 TOOLOPT(param->bEnableRectInter, "rect");
1199 TOOLOPT(param->bEnableAMP, "amp");
1200 fprintf(stderr, "rd=%d ", param->rdLevel);
1201 if (param->psyRd > 0.)
1202 fprintf(stderr, "psy-rd=%.2lf ", param->psyRd);
1203 if (param->psyRdoq > 0.)
1204 fprintf(stderr, "psy-rdoq=%.2lf ", param->psyRdoq);
1205 TOOLOPT(param->bEnableEarlySkip, "early-skip");
1206 TOOLOPT(param->bEnableCbfFastMode, "fast-cbf");
1207 if (param->noiseReductionIntra)
1208 fprintf(stderr, "nr-intra=%d ", param->noiseReductionIntra);
1209 if (param->noiseReductionInter)
1210 fprintf(stderr, "nr-inter=%d ", param->noiseReductionInter);
1211 if (param->bEnableLoopFilter)
1212 {
1213 if (param->deblockingFilterBetaOffset || param->deblockingFilterTCOffset)
1214 fprintf(stderr, "deblock(tC=%d:B=%d) ", param->deblockingFilterTCOffset, param->deblockingFilterBetaOffset);
1215 else
1216 TOOLOPT(param->bEnableLoopFilter, "deblock");
1217 }
1218 if (param->bEnableSAO)
1219 fprintf(stderr, "sao%s ", param->bSaoNonDeblocked ? "-non-deblock" : "");
1220 TOOLOPT(param->bEnableSignHiding, "signhide");
1221 TOOLOPT(param->bEnableConstrainedIntra, "cip");
1222 TOOLOPT(param->bIntraInBFrames, "b-intra");
1223 TOOLOPT(param->bEnableFastIntra, "fast-intra");
1224 TOOLOPT(param->bEnableTemporalMvp, "tmvp");
1225 if (param->bEnableTransformSkip)
1226 fprintf(stderr, "tskip%s ", param->bEnableTSkipFast ? "-fast" : "");
1227 TOOLOPT(param->bCULossless, "cu-lossless");
1228 TOOLOPT(param->rc.bStatWrite, "stats-write");
1229 TOOLOPT(param->rc.bStatRead, "stats-read");
1230 fprintf(stderr, "\n");
1231 fflush(stderr);
1232 }
1233
1234 char *x265_param2string(x265_param *p)
1235 {
1236 char *buf, *s;
1237
1238 buf = s = X265_MALLOC(char, MAXPARAMSIZE);
1239 if (!buf)
1240 return NULL;
1241
1242 #define BOOL(param, cliopt) \
1243 s += sprintf(s, " %s", (param) ? cliopt : "no-"cliopt);
1244
1245 s += sprintf(s, "%dx%d", p->sourceWidth,p->sourceHeight);
1246 s += sprintf(s, " fps=%u/%u", p->fpsNum, p->fpsDenom);
1247 s += sprintf(s, " bitdepth=%d", p->internalBitDepth);
1248 BOOL(p->bEnableWavefront, "wpp");
1249 s += sprintf(s, " ctu=%d", p->maxCUSize);
1250 s += sprintf(s, " tu-intra-depth=%d", p->tuQTMaxIntraDepth);
1251 s += sprintf(s, " tu-inter-depth=%d", p->tuQTMaxInterDepth);
1252 s += sprintf(s, " me=%d", p->searchMethod);
1253 s += sprintf(s, " subme=%d", p->subpelRefine);
1254 s += sprintf(s, " merange=%d", p->searchRange);
1255 BOOL(p->bEnableRectInter, "rect");
1256 BOOL(p->bEnableAMP, "amp");
1257 s += sprintf(s, " max-merge=%d", p->maxNumMergeCand);
1258 BOOL(p->bEnableTemporalMvp, "temporal-mvp");
1259 BOOL(p->bEnableEarlySkip, "early-skip");
1260 BOOL(p->bEnableCbfFastMode, "fast-cbf");
1261 s += sprintf(s, " rdpenalty=%d", p->rdPenalty);
1262 BOOL(p->bEnableTransformSkip, "tskip");
1263 BOOL(p->bEnableTSkipFast, "tskip-fast");
1264 BOOL(p->bEnableStrongIntraSmoothing, "strong-intra-smoothing");
1265 BOOL(p->bLossless, "lossless");
1266 BOOL(p->bCULossless, "cu-lossless");
1267 BOOL(p->bEnableConstrainedIntra, "constrained-intra");
1268 BOOL(p->bEnableFastIntra, "fast-intra");
1269 BOOL(p->bOpenGOP, "open-gop");
1270 s += sprintf(s, " interlace=%d", p->interlaceMode);
1271 s += sprintf(s, " keyint=%d", p->keyframeMax);
1272 s += sprintf(s, " min-keyint=%d", p->keyframeMin);
1273 s += sprintf(s, " scenecut=%d", p->scenecutThreshold);
1274 s += sprintf(s, " rc-lookahead=%d", p->lookaheadDepth);
1275 s += sprintf(s, " bframes=%d", p->bframes);
1276 s += sprintf(s, " bframe-bias=%d", p->bFrameBias);
1277 s += sprintf(s, " b-adapt=%d", p->bFrameAdaptive);
1278 s += sprintf(s, " ref=%d", p->maxNumReferences);
1279 BOOL(p->bEnableWeightedPred, "weightp");
1280 BOOL(p->bEnableWeightedBiPred, "weightb");
1281 s += sprintf(s, " aq-mode=%d", p->rc.aqMode);
1282 s += sprintf(s, " aq-strength=%.2f", p->rc.aqStrength);
1283 s += sprintf(s, " cbqpoffs=%d", p->cbQpOffset);
1284 s += sprintf(s, " crqpoffs=%d", p->crQpOffset);
1285 s += sprintf(s, " rd=%d", p->rdLevel);
1286 s += sprintf(s, " psy-rd=%.2f", p->psyRd);
1287 s += sprintf(s, " psy-rdoq=%.2f", p->psyRdoq);
1288 BOOL(p->bEnableSignHiding, "signhide");
1289 BOOL(p->bEnableLoopFilter, "lft");
1290 BOOL(p->bEnableSAO, "sao");
1291 BOOL(p->bSaoNonDeblocked, "sao-non-deblock");
1292 BOOL(p->bBPyramid, "b-pyramid");
1293 BOOL(p->rc.cuTree, "cutree");
1294 s += sprintf(s, " rc=%s", p->rc.rateControlMode == X265_RC_ABR ? (
1295 p->rc.bStatRead ? "2 pass" : p->rc.bitrate == p->rc.vbvMaxBitrate ? "cbr" : "abr")
1296 : p->rc.rateControlMode == X265_RC_CRF ? "crf" : "cqp");
1297 if (p->rc.rateControlMode == X265_RC_ABR || p->rc.rateControlMode == X265_RC_CRF)
1298 {
1299 if (p->rc.rateControlMode == X265_RC_CRF)
1300 s += sprintf(s, " crf=%.1f", p->rc.rfConstant);
1301 else
1302 s += sprintf(s, " bitrate=%d ratetol=%.1f",
1303 p->rc.bitrate, p->rc.rateTolerance);
1304 s += sprintf(s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
1305 p->rc.qCompress, QP_MIN, QP_MAX_SPEC, p->rc.qpStep);
1306 if (p->rc.bStatRead)
1307 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
1308 p->rc.complexityBlur, p->rc.qblur);
1309 if (p->rc.vbvBufferSize)
1310 {
1311 s += sprintf(s, " vbv-maxrate=%d vbv-bufsize=%d",
1312 p->rc.vbvMaxBitrate, p->rc.vbvBufferSize);
1313 if (p->rc.rateControlMode == X265_RC_CRF)
1314 s += sprintf(s, " crf-max=%.1f", p->rc.rfConstantMax);
1315 }
1316 }
1317 else if (p->rc.rateControlMode == X265_RC_CQP)
1318 s += sprintf(s, " qp=%d", p->rc.qp);
1319 if (!(p->rc.rateControlMode == X265_RC_CQP && p->rc.qp == 0))
1320 {
1321 s += sprintf(s, " ipratio=%.2f", p->rc.ipFactor);
1322 if (p->bframes)
1323 s += sprintf(s, " pbratio=%.2f", p->rc.pbFactor);
1324 }
1325 #undef BOOL
1326 return buf;
1327 }
1328
1329 bool parseLambdaFile(x265_param *param)
1330 {
1331 if (!param->rc.lambdaFileName)
1332 return false;
1333
1334 FILE *lfn = fopen(param->rc.lambdaFileName, "r");
1335 if (!lfn)
1336 {
1337 x265_log(param, X265_LOG_ERROR, "unable to read lambda file <%s>\n", param->rc.lambdaFileName);
1338 return true;
1339 }
1340
1341 char line[2048];
1342 char *toksave = NULL, *tok = NULL, *buf = NULL;
1343
1344 for (int t = 0; t < 3; t++)
1345 {
1346 double *table = t ? x265_lambda2_tab : x265_lambda_tab;
1347
1348 for (int i = 0; i < QP_MAX_MAX + 1; i++)
1349 {
1350 double value;
1351
1352 do
1353 {
1354 if (!tok)
1355 {
1356 /* consume a line of text file */
1357 if (!fgets(line, sizeof(line), lfn))
1358 {
1359 fclose(lfn);
1360
1361 if (t < 2)
1362 {
1363 x265_log(param, X265_LOG_ERROR, "lambda file is incomplete\n");
1364 return true;
1365 }
1366 else
1367 return false;
1368 }
1369
1370 /* truncate at first hash */
1371 char *hash = strchr(line, '#');
1372 if (hash) *hash = 0;
1373 buf = line;
1374 }
1375
1376 tok = strtok_r(buf, " ,", &toksave);
1377 buf = NULL;
1378 if (tok && sscanf(tok, "%lf", &value) == 1)
1379 break;
1380 }
1381 while (1);
1382
1383 if (t == 2)
1384 {
1385 x265_log(param, X265_LOG_ERROR, "lambda file contains too many values\n");
1386 fclose(lfn);
1387 return true;
1388 }
1389 else
1390 x265_log(param, X265_LOG_DEBUG, "lambda%c[%d] = %lf\n", t ? '2' : ' ', i, value);
1391 table[i] = value;
1392 }
1393 }
1394
1395 fclose(lfn);
1396 return false;
1397 }
1398
1399 }