1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
25 #pragma warning(disable: 4127) // conditional expression is constant, yes I know
28 #include "input/input.h"
29 #include "output/output.h"
30 #include "filters/filters.h"
37 /* Visual Leak Detector */
54 #define GetConsoleTitle(t, n)
55 #define SetConsoleTitle(t)
60 static const char short_options
[] = "o:p:f:F:r:I:i:b:s:t:q:m:hwV?";
61 static const struct option long_options
[] =
63 { "help", no_argument
, NULL
, 'h' },
64 { "version", no_argument
, NULL
, 'V' },
65 { "asm", required_argument
, NULL
, 0 },
66 { "no-asm", no_argument
, NULL
, 0 },
67 { "threads", required_argument
, NULL
, 0 },
68 { "preset", required_argument
, NULL
, 'p' },
69 { "tune", required_argument
, NULL
, 't' },
70 { "frame-threads", required_argument
, NULL
, 'F' },
71 { "no-pmode", no_argument
, NULL
, 0 },
72 { "pmode", no_argument
, NULL
, 0 },
73 { "no-pme", no_argument
, NULL
, 0 },
74 { "pme", no_argument
, NULL
, 0 },
75 { "log-level", required_argument
, NULL
, 0 },
76 { "profile", required_argument
, NULL
, 0 },
77 { "level-idc", required_argument
, NULL
, 0 },
78 { "high-tier", no_argument
, NULL
, 0 },
79 { "no-high-tier", no_argument
, NULL
, 0 },
80 { "csv", required_argument
, NULL
, 0 },
81 { "no-cu-stats", no_argument
, NULL
, 0 },
82 { "cu-stats", no_argument
, NULL
, 0 },
83 { "y4m", no_argument
, NULL
, 0 },
84 { "no-progress", no_argument
, NULL
, 0 },
85 { "output", required_argument
, NULL
, 'o' },
86 { "input", required_argument
, NULL
, 0 },
87 { "input-depth", required_argument
, NULL
, 0 },
88 { "input-res", required_argument
, NULL
, 0 },
89 { "input-csp", required_argument
, NULL
, 0 },
90 { "interlace", required_argument
, NULL
, 0 },
91 { "no-interlace", no_argument
, NULL
, 0 },
92 { "fps", required_argument
, NULL
, 0 },
93 { "seek", required_argument
, NULL
, 0 },
94 { "frame-skip", required_argument
, NULL
, 0 },
95 { "frames", required_argument
, NULL
, 'f' },
96 { "recon", required_argument
, NULL
, 'r' },
97 { "recon-depth", required_argument
, NULL
, 0 },
98 { "no-wpp", no_argument
, NULL
, 0 },
99 { "wpp", no_argument
, NULL
, 0 },
100 { "ctu", required_argument
, NULL
, 's' },
101 { "tu-intra-depth", required_argument
, NULL
, 0 },
102 { "tu-inter-depth", required_argument
, NULL
, 0 },
103 { "me", required_argument
, NULL
, 0 },
104 { "subme", required_argument
, NULL
, 'm' },
105 { "merange", required_argument
, NULL
, 0 },
106 { "max-merge", required_argument
, NULL
, 0 },
107 { "no-temporal-mvp", no_argument
, NULL
, 0 },
108 { "temporal-mvp", no_argument
, NULL
, 0 },
109 { "rdpenalty", required_argument
, NULL
, 0 },
110 { "no-rect", no_argument
, NULL
, 0 },
111 { "rect", no_argument
, NULL
, 0 },
112 { "no-amp", no_argument
, NULL
, 0 },
113 { "amp", no_argument
, NULL
, 0 },
114 { "no-early-skip", no_argument
, NULL
, 0 },
115 { "early-skip", no_argument
, NULL
, 0 },
116 { "no-fast-cbf", no_argument
, NULL
, 0 },
117 { "fast-cbf", no_argument
, NULL
, 0 },
118 { "no-tskip", no_argument
, NULL
, 0 },
119 { "tskip", no_argument
, NULL
, 0 },
120 { "no-tskip-fast", no_argument
, NULL
, 0 },
121 { "tskip-fast", no_argument
, NULL
, 0 },
122 { "cu-lossless", no_argument
, NULL
, 0 },
123 { "no-cu-lossless", no_argument
, NULL
, 0 },
124 { "no-constrained-intra", no_argument
, NULL
, 0 },
125 { "constrained-intra", no_argument
, NULL
, 0 },
126 { "fast-intra", no_argument
, NULL
, 0 },
127 { "no-fast-intra", no_argument
, NULL
, 0 },
128 { "no-open-gop", no_argument
, NULL
, 0 },
129 { "open-gop", no_argument
, NULL
, 0 },
130 { "keyint", required_argument
, NULL
, 'I' },
131 { "min-keyint", required_argument
, NULL
, 'i' },
132 { "scenecut", required_argument
, NULL
, 0 },
133 { "no-scenecut", no_argument
, NULL
, 0 },
134 { "rc-lookahead", required_argument
, NULL
, 0 },
135 { "bframes", required_argument
, NULL
, 'b' },
136 { "bframe-bias", required_argument
, NULL
, 0 },
137 { "b-adapt", required_argument
, NULL
, 0 },
138 { "no-b-adapt", no_argument
, NULL
, 0 },
139 { "no-b-pyramid", no_argument
, NULL
, 0 },
140 { "b-pyramid", no_argument
, NULL
, 0 },
141 { "ref", required_argument
, NULL
, 0 },
142 { "no-weightp", no_argument
, NULL
, 0 },
143 { "weightp", no_argument
, NULL
, 'w' },
144 { "no-weightb", no_argument
, NULL
, 0 },
145 { "weightb", no_argument
, NULL
, 0 },
146 { "crf", required_argument
, NULL
, 0 },
147 { "crf-max", required_argument
, NULL
, 0 },
148 { "crf-min", required_argument
, NULL
, 0 },
149 { "vbv-maxrate", required_argument
, NULL
, 0 },
150 { "vbv-bufsize", required_argument
, NULL
, 0 },
151 { "vbv-init", required_argument
, NULL
, 0 },
152 { "bitrate", required_argument
, NULL
, 0 },
153 { "qp", required_argument
, NULL
, 'q' },
154 { "aq-mode", required_argument
, NULL
, 0 },
155 { "aq-strength", required_argument
, NULL
, 0 },
156 { "ipratio", required_argument
, NULL
, 0 },
157 { "pbratio", required_argument
, NULL
, 0 },
158 { "cbqpoffs", required_argument
, NULL
, 0 },
159 { "crqpoffs", required_argument
, NULL
, 0 },
160 { "rd", required_argument
, NULL
, 0 },
161 { "psy-rd", required_argument
, NULL
, 0 },
162 { "psy-rdoq", required_argument
, NULL
, 0 },
163 { "scaling-list", required_argument
, NULL
, 0 },
164 { "lossless", no_argument
, NULL
, 0 },
165 { "no-lossless", no_argument
, NULL
, 0 },
166 { "no-signhide", no_argument
, NULL
, 0 },
167 { "signhide", no_argument
, NULL
, 0 },
168 { "no-lft", no_argument
, NULL
, 0 },
169 { "lft", no_argument
, NULL
, 0 },
170 { "no-sao", no_argument
, NULL
, 0 },
171 { "sao", no_argument
, NULL
, 0 },
172 { "no-sao-non-deblock", no_argument
, NULL
, 0 },
173 { "sao-non-deblock", no_argument
, NULL
, 0 },
174 { "no-ssim", no_argument
, NULL
, 0 },
175 { "ssim", no_argument
, NULL
, 0 },
176 { "no-psnr", no_argument
, NULL
, 0 },
177 { "psnr", no_argument
, NULL
, 0 },
178 { "hash", required_argument
, NULL
, 0 },
179 { "no-strong-intra-smoothing", no_argument
, NULL
, 0 },
180 { "strong-intra-smoothing", no_argument
, NULL
, 0 },
181 { "no-cutree", no_argument
, NULL
, 0 },
182 { "cutree", no_argument
, NULL
, 0 },
183 { "no-hrd", no_argument
, NULL
, 0 },
184 { "hrd", no_argument
, NULL
, 0 },
185 { "sar", required_argument
, NULL
, 0 },
186 { "overscan", required_argument
, NULL
, 0 },
187 { "videoformat", required_argument
, NULL
, 0 },
188 { "range", required_argument
, NULL
, 0 },
189 { "colorprim", required_argument
, NULL
, 0 },
190 { "transfer", required_argument
, NULL
, 0 },
191 { "colormatrix", required_argument
, NULL
, 0 },
192 { "chromaloc", required_argument
, NULL
, 0 },
193 { "crop-rect", required_argument
, NULL
, 0 },
194 { "no-dither", no_argument
, NULL
, 0 },
195 { "dither", no_argument
, NULL
, 0 },
196 { "no-repeat-headers", no_argument
, NULL
, 0 },
197 { "repeat-headers", no_argument
, NULL
, 0 },
198 { "aud", no_argument
, NULL
, 0 },
199 { "no-aud", no_argument
, NULL
, 0 },
200 { "info", no_argument
, NULL
, 0 },
201 { "no-info", no_argument
, NULL
, 0 },
202 { "qpfile", required_argument
, NULL
, 0 },
203 { "lambda-file", required_argument
, NULL
, 0 },
204 { "b-intra", no_argument
, NULL
, 0 },
205 { "no-b-intra", no_argument
, NULL
, 0 },
206 { "nr", required_argument
, NULL
, 0 },
207 { "stats", required_argument
, NULL
, 0 },
208 { "pass", required_argument
, NULL
, 0 },
209 { "slow-firstpass", no_argument
, NULL
, 0 },
210 { "no-slow-firstpass", no_argument
, NULL
, 0 },
211 { "analysis-mode", required_argument
, NULL
, 0 },
212 { "analysis-file", required_argument
, NULL
, 0 },
217 static volatile sig_atomic_t b_ctrl_c
/* = 0 */;
218 static void sigint_handler(int)
227 std::fstream bitstreamFile
;
232 uint32_t seek
; // number of frames to skip from the beginning
233 uint32_t framesToBeEncoded
; // number of frames to encode
235 size_t analysisRecordSize
; // number of bytes read from or dumped into file
236 int analysisHeaderSize
;
239 int64_t prevUpdateTime
;
244 /* in microseconds */
245 static const int UPDATE_INTERVAL
= 250000;
251 framesToBeEncoded
= seek
= 0;
255 startTime
= x265_mdate();
260 analysisRecordSize
= 0;
261 analysisHeaderSize
= 0;
265 void writeNALs(const x265_nal
* nal
, uint32_t nalcount
);
266 void printStatus(uint32_t frameNum
, x265_param
*param
);
267 void printVersion(x265_param
*param
);
268 void showHelp(x265_param
*param
);
269 bool parse(int argc
, char **argv
, x265_param
* param
);
270 bool parseQPFile(x265_picture
&pic_org
);
271 void readAnalysisFile(x265_picture
* pic
, x265_param
*);
272 void writeAnalysisFile(x265_picture
* pic
, x265_param
*);
273 bool validateFanout(x265_param
*);
276 void CLIOptions::destroy()
288 fclose(analysisFile
);
292 void CLIOptions::writeNALs(const x265_nal
* nal
, uint32_t nalcount
)
294 PPAScopeEvent(bitstream_write
);
295 for (uint32_t i
= 0; i
< nalcount
; i
++)
297 bitstreamFile
.write((const char*)nal
->payload
, nal
->sizeBytes
);
298 totalbytes
+= nal
->sizeBytes
;
303 void CLIOptions::printStatus(uint32_t frameNum
, x265_param
*param
)
306 int64_t time
= x265_mdate();
308 if (!bProgress
|| !frameNum
|| (prevUpdateTime
&& time
- prevUpdateTime
< UPDATE_INTERVAL
))
310 int64_t elapsed
= time
- startTime
;
311 double fps
= elapsed
> 0 ? frameNum
* 1000000. / elapsed
: 0;
312 float bitrate
= 0.008f
* totalbytes
* (param
->fpsNum
/ param
->fpsDenom
) / ((float)frameNum
);
313 if (framesToBeEncoded
)
315 int eta
= (int)(elapsed
* (framesToBeEncoded
- frameNum
) / ((int64_t)frameNum
* 1000000));
316 sprintf(buf
, "x265 [%.1f%%] %d/%d frames, %.2f fps, %.2f kb/s, eta %d:%02d:%02d",
317 100. * frameNum
/ framesToBeEncoded
, frameNum
, framesToBeEncoded
, fps
, bitrate
,
318 eta
/ 3600, (eta
/ 60) % 60, eta
% 60);
322 sprintf(buf
, "x265 %d frames: %.2f fps, %.2f kb/s", frameNum
, fps
, bitrate
);
324 fprintf(stderr
, "%s \r", buf
+ 5);
325 SetConsoleTitle(buf
);
326 fflush(stderr
); // needed in windows
327 prevUpdateTime
= time
;
330 void CLIOptions::printVersion(x265_param
*param
)
332 x265_log(param
, X265_LOG_INFO
, "HEVC encoder version %s\n", x265_version_str
);
333 x265_log(param
, X265_LOG_INFO
, "build info %s\n", x265_build_info_str
);
336 void CLIOptions::showHelp(x265_param
*param
)
338 x265_param_default(param
);
342 #define OPT(value) (value ? "enabled" : "disabled")
343 H0("\nSyntax: x265 [options] infile [-o] outfile\n");
344 H0(" infile can be YUV or Y4M\n");
345 H0(" outfile is raw HEVC bitstream\n");
346 H0("\nExecutable Options:\n");
347 H0("-h/--help Show this help text and exit\n");
348 H0("-V/--version Show version info and exit\n");
349 H0("\nOutput Options:\n");
350 H0("-o/--output <filename> Bitstream output file name\n");
351 H0(" --log-level <string> Logging level: none error warning info debug full. Default %s\n", logLevelNames
[param
->logLevel
+ 1]);
352 H0(" --no-progress Disable CLI progress reports\n");
353 H0(" --[no-]cu-stats Enable logging stats about distribution of cu across all modes. Default %s\n",OPT(param
->bLogCuStats
));
354 H0(" --csv <filename> Comma separated log file, log level >= 3 frame log, else one line per run\n");
355 H0("\nInput Options:\n");
356 H0(" --input <filename> Raw YUV or Y4M input file name. `-` for stdin\n");
357 H0(" --y4m Force parsing of input stream as YUV4MPEG2 regardless of file extension\n");
358 H0(" --fps <float|rational> Source frame rate (float or num/denom), auto-detected if Y4M\n");
359 H0(" --input-res WxH Source picture size [w x h], auto-detected if Y4M\n");
360 H0(" --input-depth <integer> Bit-depth of input file. Default 8\n");
361 H0(" --input-csp <string> Source color space: i420, i444 or i422, auto-detected if Y4M. Default: i420\n");
362 H0("-f/--frames <integer> Maximum number of frames to encode. Default all\n");
363 H0(" --seek <integer> First frame to encode\n");
364 H0(" --[no-]interlace <bff|tff> Indicate input pictures are interlace fields in temporal order. Default progressive\n");
365 H0(" --dither Enable dither if downscaling to 8 bit pixels. Default disabled\n");
366 H0("\nQuality reporting metrics:\n");
367 H0(" --[no-]ssim Enable reporting SSIM metric scores. Default %s\n", OPT(param
->bEnableSsim
));
368 H0(" --[no-]psnr Enable reporting PSNR metric scores. Default %s\n", OPT(param
->bEnablePsnr
));
369 H0("\nProfile, Level, Tier:\n");
370 H0(" --profile <string> Enforce an encode profile: main, main10, mainstillpicture\n");
371 H0(" --level-idc <integer|float> Force a minumum required decoder level (as '5.0' or '50')\n");
372 H0(" --[no-]high-tier If a decoder level is specified, this modifier selects High tier of that level\n");
373 H0("\nThreading, performance:\n");
374 H0(" --threads <integer> Number of threads for thread pool (0: detect CPU core count, default)\n");
375 H0("-F/--frame-threads <integer> Number of concurrently encoded frames. 0: auto-determined by core count\n");
376 H0(" --[no-]wpp Enable Wavefront Parallel Processing. Default %s\n", OPT(param
->bEnableWavefront
));
377 H0(" --[no-]pmode Parallel mode analysis. Default %s\n", OPT(param
->bDistributeModeAnalysis
));
378 H0(" --[no-]pme Parallel motion estimation. Default %s\n", OPT(param
->bDistributeMotionEstimation
));
379 H0(" --[no-]asm <bool|int|string> Override CPU detection. Default: auto\n");
381 H0("-p/--preset <string> Trade off performance for compression efficiency. Default medium\n");
382 H0(" ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, or placebo\n");
383 H0("-t/--tune <string> Tune the settings for a particular type of source or situation:\n");
384 H0(" psnr, ssim, zerolatency, or fastdecode\n");
385 H0("\nQuad-Tree size and depth:\n");
386 H0("-s/--ctu <64|32|16> Maximum CU size (default: 64x64). Default %d\n", param
->maxCUSize
);
387 H0(" --tu-intra-depth <integer> Max TU recursive depth for intra CUs. Default %d\n", param
->tuQTMaxIntraDepth
);
388 H0(" --tu-inter-depth <integer> Max TU recursive depth for inter CUs. Default %d\n", param
->tuQTMaxInterDepth
);
389 H0(" --[no-]rect Enable rectangular motion partitions Nx2N and 2NxN. Default %s\n", OPT(param
->bEnableRectInter
));
390 H0(" --[no-]amp Enable asymmetric motion partitions, requires --rect. Default %s\n", OPT(param
->bEnableAMP
));
392 H0(" --rd <0..6> Level of RD in mode decision 0:least....6:full RDO. Default %d\n", param
->rdLevel
);
393 H0(" --psy-rd <0..2.0> Strength of psycho-visual rate distortion optimization, 0 to disable. Default %f\n", param
->psyRd
);
394 H0(" --psy-rdoq <0..50.0> Strength of psycho-visual optimization in quantization, 0 to disable. Default %f\n", param
->psyRdoq
);
395 H0(" --nr <integer> An integer value in range of 100 to 1000, which denotes strength of noise reduction. Default disabled\n");
396 H0(" --[no-]tskip-fast Enable fast intra transform skipping. Default %s\n", OPT(param
->bEnableTSkipFast
));
397 H0(" --[no-]early-skip Enable early SKIP detection. Default %s\n", OPT(param
->bEnableEarlySkip
));
398 H0(" --[no-]fast-cbf Enable early outs based on whether residual is coded. Default %s\n", OPT(param
->bEnableCbfFastMode
));
399 H0("\nCoding tools:\n");
400 H0("-w/--[no-]weightp Enable weighted prediction in P slices. Default %s\n", OPT(param
->bEnableWeightedPred
));
401 H0(" --[no-]weightb Enable weighted prediction in B slices. Default %s\n", OPT(param
->bEnableWeightedBiPred
));
402 H0(" --[no-]cu-lossless Consider lossless mode in CU RDO decisions. Default %s\n", OPT(param
->bCULossless
));
403 H0(" --[no-]signhide Hide sign bit of one coeff per TU (rdo). Default %s\n", OPT(param
->bEnableSignHiding
));
404 H0(" --[no-]tskip Enable intra 4x4 transform skipping. Default %s\n", OPT(param
->bEnableTransformSkip
));
405 H0("\nTemporal / motion search options:\n");
406 H0(" --me <string> Motion search method dia hex umh star full. Default %d\n", param
->searchMethod
);
407 H0("-m/--subme <integer> Amount of subpel refinement to perform (0:least .. 7:most). Default %d \n", param
->subpelRefine
);
408 H0(" --merange <integer> Motion search range. Default %d\n", param
->searchRange
);
409 H0(" --max-merge <1..5> Maximum number of merge candidates. Default %d\n", param
->maxNumMergeCand
);
410 H0(" --[no-]temporal-mvp Enable temporal MV predictors. Default %s\n", OPT(param
->bEnableTemporalMvp
));
411 H0("\nSpatial / intra options:\n");
412 H0(" --[no-]strong-intra-smoothing Enable strong intra smoothing for 32x32 blocks. Default %s\n", OPT(param
->bEnableStrongIntraSmoothing
));
413 H0(" --[no-]constrained-intra Constrained intra prediction (use only intra coded reference pixels) Default %s\n", OPT(param
->bEnableConstrainedIntra
));
414 H0(" --[no-]b-intra Enable intra in B frames in veryslow presets. Default %s\n", OPT(param
->bIntraInBFrames
));
415 H0(" --[no-]fast-intra Enable faster search method for angular intra predictions. Default %s\n", OPT(param
->bEnableFastIntra
));
416 H0(" --rdpenalty <0..2> penalty for 32x32 intra TU in non-I slices. 0:disabled 1:RD-penalty 2:maximum. Default %d\n", param
->rdPenalty
);
417 H0("\nSlice decision options:\n");
418 H0(" --[no-]open-gop Enable open-GOP, allows I slices to be non-IDR. Default %s\n", OPT(param
->bOpenGOP
));
419 H0("-I/--keyint <integer> Max IDR period in frames. -1 for infinite-gop. Default %d\n", param
->keyframeMax
);
420 H0("-i/--min-keyint <integer> Scenecuts closer together than this are coded as I, not IDR. Default: auto\n");
421 H0(" --no-scenecut Disable adaptive I-frame decision\n");
422 H0(" --scenecut <integer> How aggressively to insert extra I-frames. Default %d\n", param
->scenecutThreshold
);
423 H0(" --rc-lookahead <integer> Number of frames for frame-type lookahead (determines encoder latency) Default %d\n", param
->lookaheadDepth
);
424 H0(" --bframes <integer> Maximum number of consecutive b-frames (now it only enables B GOP structure) Default %d\n", param
->bframes
);
425 H0(" --bframe-bias <integer> Bias towards B frame decisions. Default %d\n", param
->bFrameBias
);
426 H0(" --b-adapt <0..2> 0 - none, 1 - fast, 2 - full (trellis) adaptive B frame scheduling. Default %d\n", param
->bFrameAdaptive
);
427 H0(" --[no-]b-pyramid Use B-frames as references. Default %s\n", OPT(param
->bBPyramid
));
428 H0(" --ref <integer> max number of L0 references to be allowed (1 .. 16) Default %d\n", param
->maxNumReferences
);
429 H0(" --qpfile <string> Force frametypes and QPs for some or all frames\n");
430 H0(" Format of each line: framenumber frametype QP\n");
431 H0(" QP is optional (none lets x265 choose). Frametypes: I,i,P,B,b.\n");
432 H0(" QPs are restricted by qpmin/qpmax.\n");
433 H0("\nRate control, Quantization:\n");
434 H0(" --bitrate <integer> Target bitrate (kbps) for ABR (implied). Default %d\n", param
->rc
.bitrate
);
435 H0("-q/--qp <integer> QP for P slices in CQP mode (implied). --ipratio and --pbration determine other slice QPs\n");
436 H0(" --crf <float> Quality-based VBR (0-51). Default %f\n", param
->rc
.rfConstant
);
437 H0(" --[no-]lossless Enable lossless: bypass transform, quant and loop filters globally. Default %s\n", OPT(param
->bLossless
));
438 H0(" --crf-max <float> With CRF+VBV, limit RF to this value. Default %f\n", param
->rc
.rfConstantMax
);
439 H0(" May cause VBV underflows!\n");
440 H0(" --crf-min <float> With CRF+VBV, limit RF to this value. Default %f\n", param
->rc
.rfConstantMin
);
441 H0(" this specifies a minimum rate factor value for encode!\n");
442 H0(" --vbv-maxrate <integer> Max local bitrate (kbit/s). Default %d\n", param
->rc
.vbvMaxBitrate
);
443 H0(" --vbv-bufsize <integer> Set size of the VBV buffer (kbit). Default %d\n", param
->rc
.vbvBufferSize
);
444 H0(" --vbv-init <float> Initial VBV buffer occupancy (fraction of bufsize or in kbits). Default %f\n", param
->rc
.vbvBufferInit
);
445 H0(" --aq-mode <integer> Mode for Adaptive Quantization - 0:none 1:uniform AQ 2:auto variance. Default %d\n", param
->rc
.aqMode
);
446 H0(" --aq-strength <float> Reduces blocking and blurring in flat and textured areas.(0 to 3.0). Default %f\n", param
->rc
.aqStrength
);
447 H0(" --[no-]cutree Enable cutree for Adaptive Quantization. Default %s\n", OPT(param
->rc
.cuTree
));
448 H0(" --ipratio <float> QP factor between I and P. Default %f\n", param
->rc
.ipFactor
);
449 H0(" --pbratio <float> QP factor between P and B. Default %f\n", param
->rc
.pbFactor
);
450 H0(" --cbqpoffs <integer> Chroma Cb QP Offset. Default %d\n", param
->cbQpOffset
);
451 H0(" --crqpoffs <integer> Chroma Cr QP Offset. Default %d\n", param
->crQpOffset
);
452 H0(" --stats Filename for stats file in multipass pass rate control. Default x265_2pass.log\n");
453 H0(" --pass Multi pass rate control.\n"
454 " - 1 : First pass, creates stats file\n"
455 " - 2 : Last pass, does not overwrite stats file\n"
456 " - 3 : Nth pass, overwrites stats file\n");
457 H0(" --[no-]slow-firstpass Enable a slow first pass in a multipass rate control mode. Default %s\n", OPT(param
->rc
.bEnableSlowFirstPass
));
458 H0(" --analysis-mode <string|int> save - Dump analysis info into file, load - Load analysis buffers from the file. Default %d\n", param
->analysisMode
);
459 H0(" --analysis-file <filename> Specify file name used for either dumping or reading analysis data.\n");
460 H0(" --scaling-list <string> Specify a file containing HM style quant scaling lists or 'default' or 'off'. Default: off\n");
461 H0(" --lambda-file <string> Specify a file containing replacement values for the lambda tables\n");
462 H0(" MAX_MAX_QP+1 floats for lambda table, then again for lambda2 table\n");
463 H0(" Blank lines and lines starting with hash(#) are ignored\n");
464 H0(" Comma is considered to be white-space\n");
465 H0("\nLoop filters (deblock and SAO):\n");
466 H0(" --[no-]lft Enable Deblocking Loop Filter. Default %s\n", OPT(param
->bEnableLoopFilter
));
467 H0(" --[no-]sao Enable Sample Adaptive Offset. Default %s\n", OPT(param
->bEnableSAO
));
468 H0(" --[no-]sao-non-deblock Use non-deblocked pixels, else right/bottom boundary areas skipped. Default %s\n", OPT(param
->bSaoNonDeblocked
));
469 H0("\nVUI options:\n");
470 H0(" --sar <width:height|int> Sample Aspect Ratio, the ratio of width to height of an individual pixel.\n");
471 H0(" Choose from 0=undef, 1=1:1(\"square\"), 2=12:11, 3=10:11, 4=16:11,\n");
472 H0(" 5=40:33, 6=24:11, 7=20:11, 8=32:11, 9=80:33, 10=18:11, 11=15:11,\n");
473 H0(" 12=64:33, 13=160:99, 14=4:3, 15=3:2, 16=2:1 or custom ratio of <int:int>. Default %d\n", param
->vui
.aspectRatioIdc
);
474 H0(" --crop-rect <string> Add 'left,top,right,bottom' to the bitstream-level cropping rectangle\n");
475 H0(" --overscan <string> Specify whether it is appropriate for decoder to show cropped region: undef, show or crop. Default undef\n");
476 H0(" --videoformat <string> Specify video format from undef, component, pal, ntsc, secam, mac. Default undef\n");
477 H0(" --range <string> Specify black level and range of luma and chroma signals as full or limited Default limited\n");
478 H0(" --colorprim <string> Specify color primaries from undef, bt709, bt470m, bt470bg, smpte170m,\n");
479 H0(" smpte240m, film, bt2020. Default undef\n");
480 H0(" --transfer <string> Specify transfer characteristics from undef, bt709, bt470m, bt470bg, smpte170m,\n");
481 H0(" smpte240m, linear, log100, log316, iec61966-2-4, bt1361e, iec61966-2-1,\n");
482 H0(" bt2020-10, bt2020-12. Default undef\n");
483 H0(" --colormatrix <string> Specify color matrix setting from undef, bt709, fcc, bt470bg, smpte170m,\n");
484 H0(" smpte240m, GBR, YCgCo, bt2020nc, bt2020c. Default undef\n");
485 H0(" --chromaloc <integer> Specify chroma sample location (0 to 5). Default of %d\n", param
->vui
.chromaSampleLocTypeTopField
);
486 H0("\nBitstream options:\n");
487 H0(" --[no-]info Emit SEI identifying encoder and parameters. Default %s\n", OPT(param
->bEmitInfoSEI
));
488 H0(" --[no-]aud Emit access unit delimiters at the start of each access unit. Default %s\n", OPT(param
->bEnableAccessUnitDelimiters
));
489 H0(" --[no-]hrd Enable HRD parameters signalling. Default %s\n", OPT(param
->bEmitHRDSEI
));
490 H0(" --[no-]repeat-headers Emit SPS and PPS headers at each keyframe. Default %s\n", OPT(param
->bRepeatHeaders
));
491 H0(" --hash <integer> Decoded Picture Hash SEI 0: disabled, 1: MD5, 2: CRC, 3: Checksum. Default %d\n", param
->decodedPictureHashSEI
);
492 H0("\nReconstructed video options (debugging):\n");
493 H0("-r/--recon <filename> Reconstructed raw image YUV or Y4M output file name\n");
494 H0(" --recon-depth <integer> Bit-depth of reconstructed raw image file. Defaults to input bit depth, or 8 if Y4M\n");
497 printf("\n\nFull documentation may be found at http://x265.readthedocs.org/en/default/cli.html\n");
501 bool CLIOptions::parse(int argc
, char **argv
, x265_param
* param
)
505 int inputBitDepth
= 8;
506 int reconFileBitDepth
= 0;
507 const char *inputfn
= NULL
;
508 const char *reconfn
= NULL
;
509 const char *bitstreamfn
= NULL
;
510 const char *preset
= NULL
;
511 const char *tune
= NULL
;
512 const char *profile
= NULL
;
513 const char *analysisfn
= "x265_analysis.dat";
517 x265_log(NULL
, X265_LOG_ERROR
, "No input file. Run x265 --help for a list of options.\n");
521 /* Presets are applied before all other options. */
524 int c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
535 if (x265_param_default_preset(param
, preset
, tune
) < 0)
537 x265_log(NULL
, X265_LOG_ERROR
, "preset or tune unrecognized\n");
543 int long_options_index
= -1;
544 int c
= getopt_long(argc
, argv
, short_options
, long_options
, &long_options_index
);
558 x265_setup_primitives(param
, -1);
562 if (long_options_index
< 0 && c
> 0)
564 for (size_t i
= 0; i
< sizeof(long_options
) / sizeof(long_options
[0]); i
++)
566 if (long_options
[i
].val
== c
)
568 long_options_index
= (int)i
;
573 if (long_options_index
< 0)
575 /* getopt_long might have already printed an error message */
577 x265_log(NULL
, X265_LOG_WARNING
, "internal error: short option '%c' has no long option\n", c
);
581 if (long_options_index
< 0)
583 x265_log(NULL
, X265_LOG_WARNING
, "short option '%c' unrecognized\n", c
);
586 #define OPT(longname) \
587 else if (!strcmp(long_options[long_options_index].name, longname))
588 #define OPT2(name1, name2) \
589 else if (!strcmp(long_options[long_options_index].name, name1) || \
590 !strcmp(long_options[long_options_index].name, name2))
593 OPT2("frame-skip", "seek") this->seek
= (uint32_t)x265_atoi(optarg
, bError
);
594 OPT("frames") this->framesToBeEncoded
= (uint32_t)x265_atoi(optarg
, bError
);
595 OPT("no-progress") this->bProgress
= false;
596 OPT("output") bitstreamfn
= optarg
;
597 OPT("input") inputfn
= optarg
;
598 OPT("recon") reconfn
= optarg
;
599 OPT("input-depth") inputBitDepth
= (uint32_t)x265_atoi(optarg
, bError
);
600 OPT("dither") this->bDither
= true;
601 OPT("recon-depth") reconFileBitDepth
= (uint32_t)x265_atoi(optarg
, bError
);
602 OPT("y4m") this->bForceY4m
= true;
603 OPT("profile") profile
= optarg
; /* handled last */
604 OPT("preset") /* handled above */;
605 OPT("tune") /* handled above */;
606 OPT("analysis-file") analysisfn
= optarg
;
609 this->qpfile
= fopen(optarg
, "rb");
612 x265_log(param
, X265_LOG_ERROR
, "%s qpfile not found or error in opening qp file \n", optarg
);
617 bError
|= !!x265_param_parse(param
, long_options
[long_options_index
].name
, optarg
);
621 const char *name
= long_options_index
> 0 ? long_options
[long_options_index
].name
: argv
[optind
- 2];
622 x265_log(NULL
, X265_LOG_ERROR
, "invalid argument: %s = %s\n", name
, optarg
);
629 if (optind
< argc
&& !inputfn
)
630 inputfn
= argv
[optind
++];
631 if (optind
< argc
&& !bitstreamfn
)
632 bitstreamfn
= argv
[optind
++];
635 x265_log(param
, X265_LOG_WARNING
, "extra unused command arguments given <%s>\n", argv
[optind
]);
639 if (argc
<= 1 || help
)
642 if (inputfn
== NULL
|| bitstreamfn
== NULL
)
644 x265_log(param
, X265_LOG_ERROR
, "input or output file not specified, try -V for help\n");
649 if (param
->internalBitDepth
!= 10)
651 x265_log(param
, X265_LOG_ERROR
, "Only bit depths of 10 are supported in this build\n");
655 if (param
->internalBitDepth
!= 8)
657 x265_log(param
, X265_LOG_ERROR
, "Only bit depths of 8 are supported in this build\n");
660 #endif // if HIGH_BIT_DEPTH
663 info
.filename
= inputfn
;
664 info
.depth
= inputBitDepth
;
665 info
.csp
= param
->internalCsp
;
666 info
.width
= param
->sourceWidth
;
667 info
.height
= param
->sourceHeight
;
668 info
.fpsNum
= param
->fpsNum
;
669 info
.fpsDenom
= param
->fpsDenom
;
670 info
.sarWidth
= param
->vui
.sarWidth
;
671 info
.sarHeight
= param
->vui
.sarHeight
;
672 info
.skipFrames
= seek
;
674 getParamAspectRatio(param
, info
.sarWidth
, info
.sarHeight
);
676 this->input
= Input::open(info
, this->bForceY4m
);
677 if (!this->input
|| this->input
->isFail())
679 x265_log(param
, X265_LOG_ERROR
, "unable to open input file <%s>\n", inputfn
);
683 if (info
.depth
< 8 || info
.depth
> 16)
685 x265_log(param
, X265_LOG_ERROR
, "Input bit depth (%d) must be between 8 and 16\n", inputBitDepth
);
689 /* Unconditionally accept height/width/csp from file info */
690 param
->sourceWidth
= info
.width
;
691 param
->sourceHeight
= info
.height
;
692 param
->internalCsp
= info
.csp
;
694 /* Accept fps and sar from file info if not specified by user */
695 if (param
->fpsDenom
== 0 || param
->fpsNum
== 0)
697 param
->fpsDenom
= info
.fpsDenom
;
698 param
->fpsNum
= info
.fpsNum
;
700 if (!param
->vui
.aspectRatioIdc
&& info
.sarWidth
&& info
.sarHeight
)
701 setParamAspectRatio(param
, info
.sarWidth
, info
.sarHeight
);
702 if (this->framesToBeEncoded
== 0 && info
.frameCount
> (int)seek
)
703 this->framesToBeEncoded
= info
.frameCount
- seek
;
704 param
->totalFrames
= this->framesToBeEncoded
;
706 if (x265_param_apply_profile(param
, profile
))
709 if (param
->logLevel
>= X265_LOG_INFO
)
712 int p
= sprintf(buf
, "%dx%d fps %d/%d %sp%d", param
->sourceWidth
, param
->sourceHeight
,
713 param
->fpsNum
, param
->fpsDenom
, x265_source_csp_names
[param
->internalCsp
], info
.depth
);
716 getParamAspectRatio(param
, width
, height
);
718 p
+= sprintf(buf
+ p
, " sar %d:%d", width
, height
);
720 if (framesToBeEncoded
<= 0 || info
.frameCount
<= 0)
721 strcpy(buf
+ p
, " unknown frame count");
723 sprintf(buf
+ p
, " frames %u - %d of %d", this->seek
, this->seek
+ this->framesToBeEncoded
- 1, info
.frameCount
);
725 fprintf(stderr
, "%s [info]: %s\n", input
->getName(), buf
);
728 this->input
->startReader();
732 if (reconFileBitDepth
== 0)
733 reconFileBitDepth
= param
->internalBitDepth
;
734 this->recon
= Output::open(reconfn
, param
->sourceWidth
, param
->sourceHeight
, reconFileBitDepth
,
735 param
->fpsNum
, param
->fpsDenom
, param
->internalCsp
);
736 if (this->recon
->isFail())
738 x265_log(param
, X265_LOG_WARNING
, "unable to write reconstruction file\n");
739 this->recon
->release();
743 fprintf(stderr
, "%s [info]: reconstructed images %dx%d fps %d/%d %s\n", this->recon
->getName(),
744 param
->sourceWidth
, param
->sourceHeight
, param
->fpsNum
, param
->fpsDenom
,
745 x265_source_csp_names
[param
->internalCsp
]);
748 this->bitstreamFile
.open(bitstreamfn
, std::fstream::binary
| std::fstream::out
);
749 if (!this->bitstreamFile
)
751 x265_log(NULL
, X265_LOG_ERROR
, "failed to open bitstream file <%s> for writing\n", bitstreamfn
);
755 if (param
->analysisMode
)
757 const char *mode
= param
->analysisMode
== X265_ANALYSIS_SAVE
? "wb" : "rb";
758 this->analysisFile
= fopen(analysisfn
, mode
);
759 if (!this->analysisFile
)
761 x265_log(NULL
, X265_LOG_ERROR
, "failed to open analysis file %s\n", analysisfn
);
769 bool CLIOptions::validateFanout(x265_param
*param
)
771 #define CMP_OPT_FANOUT(opt, param_val)\
774 p = strstr(paramBuf, opt "=");\
775 char* q = strstr(paramBuf, "no-"opt);\
776 if (p && sscanf(p, opt "=%d" , &i) && param_val != i)\
778 else if (!param_val && !q)\
780 else if (param_val && (q || !strstr(paramBuf, opt)))\
784 x265_log(param, X265_LOG_ERROR, "different " opt " setting than given in analysis file (%d vs %d)\n", param_val, i);\
785 X265_FREE(paramBuf);\
790 char *p
= NULL
, *paramBuf
;
795 paramBuf
= X265_MALLOC(char, MAXPARAMSIZE
);
799 fread(paramBuf
, 1, MAXPARAMSIZE
, this->analysisFile
);
801 /* check whether fanout options are compatible */
802 if (strncmp(paramBuf
, "#options:", 9))
804 x265_log(param
, X265_LOG_ERROR
, "options list in analysis file is not valid\n");
809 char* buf
= strchr(paramBuf
, '\n');
812 x265_log(param
, X265_LOG_ERROR
, "Malformed analysis file\n");
817 fseek(this->analysisFile
, (int)strlen(paramBuf
) + 1, SEEK_SET
);
819 if (sscanf(paramBuf
, "#options: %dx%d", &i
, &j
) != 2)
821 x265_log(param
, X265_LOG_ERROR
, "Resolution specified in analysis file is not valid\n");
825 if ((p
= strstr(paramBuf
, " fps=")) == 0 || sscanf(p
, " fps=%u/%u", &k
, &l
) != 2)
827 x265_log(param
, X265_LOG_ERROR
, "fps specified in analysis file is not valid\n");
831 if (k
!= param
->fpsNum
|| l
!= param
->fpsDenom
)
833 x265_log(param
, X265_LOG_ERROR
, "fps mismatch than given in analysis file (%u/%u vs %u/%u)\n",
834 param
->fpsNum
, param
->fpsDenom
, k
, l
);
839 CMP_OPT_FANOUT("bitdepth", param
->internalBitDepth
);
840 CMP_OPT_FANOUT("weightp", param
->bEnableWeightedPred
);
841 CMP_OPT_FANOUT("bframes", param
->bframes
);
842 CMP_OPT_FANOUT("b-pyramid", param
->bBPyramid
);
843 CMP_OPT_FANOUT("b-adapt", param
->bFrameAdaptive
);
844 CMP_OPT_FANOUT("open-gop", param
->bOpenGOP
);
845 CMP_OPT_FANOUT("keyint", param
->keyframeMax
);
846 CMP_OPT_FANOUT("min-keyint", param
->keyframeMin
);
847 CMP_OPT_FANOUT("scenecut", param
->scenecutThreshold
);
848 CMP_OPT_FANOUT("ctu", (int)param
->maxCUSize
);
849 CMP_OPT_FANOUT("ref", param
->maxNumReferences
);
850 CMP_OPT_FANOUT("rc-lookahead", param
->lookaheadDepth
);
852 #undef CMP_OPT_FANOUT
858 void CLIOptions::readAnalysisFile(x265_picture
* pic
, x265_param
* p
)
860 int poc
, width
, height
;
861 uint32_t numPart
, numCU
;
862 fread(&width
, sizeof(int), 1, this->analysisFile
);
863 fread(&height
, sizeof(int), 1, this->analysisFile
);
864 fread(&poc
, sizeof(int), 1, this->analysisFile
);
865 fread(&pic
->sliceType
, sizeof(int), 1, this->analysisFile
);
866 fread(&numCU
, sizeof(int), 1, this->analysisFile
);
867 fread(&numPart
, sizeof(int), 1, this->analysisFile
);
869 if (poc
!= pic
->poc
|| width
!= p
->sourceWidth
|| height
!= p
->sourceHeight
)
871 x265_log(NULL
, X265_LOG_WARNING
, "Error in reading intra-inter data.\n");
872 x265_free_analysis_data(pic
);
876 fread(pic
->analysisData
.intraData
->depth
,
877 sizeof(uint8_t), pic
->analysisData
.numPartitions
* pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
878 fread(pic
->analysisData
.intraData
->modes
,
879 sizeof(uint8_t), pic
->analysisData
.numPartitions
* pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
880 fread(pic
->analysisData
.intraData
->partSizes
,
881 sizeof(char), pic
->analysisData
.numPartitions
* pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
882 fread(pic
->analysisData
.intraData
->poc
,
883 sizeof(int), pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
884 fread(pic
->analysisData
.intraData
->cuAddr
,
885 sizeof(uint32_t), pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
886 fread(pic
->analysisData
.interData
, sizeof(x265_inter_data
), pic
->analysisData
.numCUsInFrame
* 85, this->analysisFile
);
889 void CLIOptions::writeAnalysisFile(x265_picture
* pic
, x265_param
*p
)
891 uint64_t seekTo
= pic
->poc
* this->analysisRecordSize
+ this->analysisHeaderSize
;
892 fseeko(this->analysisFile
, seekTo
, SEEK_SET
);
893 fwrite(&p
->sourceWidth
, sizeof(int), 1, this->analysisFile
);
894 fwrite(&p
->sourceHeight
, sizeof(int), 1, this->analysisFile
);
895 fwrite(&pic
->poc
, sizeof(int), 1, this->analysisFile
);
896 fwrite(&pic
->sliceType
, sizeof(int), 1, this->analysisFile
);
897 fwrite(&pic
->analysisData
.numCUsInFrame
, sizeof(int), 1, this->analysisFile
);
898 fwrite(&pic
->analysisData
.numPartitions
, sizeof(int), 1, this->analysisFile
);
900 fwrite(pic
->analysisData
.intraData
->depth
,
901 sizeof(uint8_t), pic
->analysisData
.numPartitions
* pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
902 fwrite(pic
->analysisData
.intraData
->modes
,
903 sizeof(uint8_t), pic
->analysisData
.numPartitions
* pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
904 fwrite(pic
->analysisData
.intraData
->partSizes
,
905 sizeof(char), pic
->analysisData
.numPartitions
* pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
906 fwrite(pic
->analysisData
.intraData
->poc
, sizeof(int), pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
907 fwrite(pic
->analysisData
.intraData
->cuAddr
, sizeof(uint32_t), pic
->analysisData
.numCUsInFrame
, this->analysisFile
);
908 fwrite(pic
->analysisData
.interData
, sizeof(x265_inter_data
), pic
->analysisData
.numCUsInFrame
* 85, this->analysisFile
);
911 bool CLIOptions::parseQPFile(x265_picture
&pic_org
)
913 int32_t num
= -1, qp
, ret
;
917 pic_org
.sliceType
= X265_TYPE_AUTO
;
918 while (num
< pic_org
.poc
)
920 filePos
= ftell(qpfile
);
922 ret
= fscanf(qpfile
, "%d %c%*[ \t]%d\n", &num
, &type
, &qp
);
924 if (num
> pic_org
.poc
|| ret
== EOF
)
926 fseek(qpfile
, filePos
, SEEK_SET
);
929 if (num
< pic_org
.poc
&& ret
>= 2)
931 if (ret
== 3 && qp
>= 0)
932 pic_org
.forceqp
= qp
+ 1;
933 if (type
== 'I') pic_org
.sliceType
= X265_TYPE_IDR
;
934 else if (type
== 'i') pic_org
.sliceType
= X265_TYPE_I
;
935 else if (type
== 'P') pic_org
.sliceType
= X265_TYPE_P
;
936 else if (type
== 'B') pic_org
.sliceType
= X265_TYPE_BREF
;
937 else if (type
== 'b') pic_org
.sliceType
= X265_TYPE_B
;
939 if (ret
< 2 || qp
< -1 || qp
> 51)
945 int main(int argc
, char **argv
)
948 // This uses Microsoft's proprietary WCHAR type, but this only builds on Windows to start with
949 VLDSetReportOptions(VLD_OPT_REPORT_TO_DEBUGGER
| VLD_OPT_REPORT_TO_FILE
, L
"x265_leaks.txt");
953 x265_param
*param
= x265_param_alloc();
956 if (cliopt
.parse(argc
, argv
, param
))
959 x265_param_free(param
);
963 x265_encoder
*encoder
= x265_encoder_open(param
);
966 x265_log(param
, X265_LOG_ERROR
, "failed to open encoder\n");
968 x265_param_free(param
);
973 /* get the encoder parameters post-initialization */
974 x265_encoder_parameters(encoder
, param
);
976 /* Control-C handler */
977 if (signal(SIGINT
, sigint_handler
) == SIG_ERR
)
978 x265_log(param
, X265_LOG_ERROR
, "Unable to register CTRL+C handler: %s\n", strerror(errno
));
980 x265_picture pic_orig
, pic_out
;
981 x265_picture
*pic_in
= &pic_orig
;
982 x265_picture
*pic_recon
= cliopt
.recon
? &pic_out
: NULL
;
983 uint32_t inFrameCount
= 0;
984 uint32_t outFrameCount
= 0;
988 int16_t *errorBuf
= NULL
;
990 if (!param
->bRepeatHeaders
)
992 if (x265_encoder_headers(encoder
, &p_nal
, &nal
) < 0)
994 x265_log(param
, X265_LOG_ERROR
, "Failure generating stream headers\n");
998 cliopt
.writeNALs(p_nal
, nal
);
1001 x265_picture_init(param
, pic_in
);
1003 if (param
->analysisMode
&& !pic_recon
)
1005 x265_log(NULL
, X265_LOG_ERROR
, "Must specify recon with analysis-mode option.\n");
1008 if (param
->analysisMode
)
1010 if (param
->analysisMode
== X265_ANALYSIS_SAVE
)
1012 char *p
= x265_param2string(param
);
1015 x265_log(NULL
, X265_LOG_ERROR
, "analysis: buffer allocation failure, aborting");
1018 uint32_t numCU
= pic_in
->analysisData
.numCUsInFrame
;
1019 uint32_t numPart
= pic_in
->analysisData
.numPartitions
;
1021 cliopt
.analysisRecordSize
= ((sizeof(int) * 4 + sizeof(uint32_t) * 2) + sizeof(x265_inter_data
) * numCU
* 85 +
1022 sizeof(uint8_t) * 2 * numPart
* numCU
+ sizeof(char) * numPart
* numCU
+ sizeof(int) * numCU
+ sizeof(uint32_t) * numCU
);
1024 fprintf(cliopt
.analysisFile
, "#options: %s\n", p
);
1025 cliopt
.analysisHeaderSize
= ftell(cliopt
.analysisFile
);
1030 if (!cliopt
.validateFanout(param
))
1037 errorBuf
= X265_MALLOC(int16_t, param
->sourceWidth
+ 1);
1039 memset(errorBuf
, 0, (param
->sourceWidth
+ 1) * sizeof(int16_t));
1041 cliopt
.bDither
= false;
1044 // main encoder loop
1045 while (pic_in
&& !b_ctrl_c
)
1047 pic_orig
.poc
= inFrameCount
;
1048 if (cliopt
.qpfile
&& !param
->rc
.bStatRead
)
1050 if (!cliopt
.parseQPFile(pic_orig
))
1052 x265_log(NULL
, X265_LOG_ERROR
, "can't parse qpfile for frame %d\n", pic_in
->poc
);
1053 fclose(cliopt
.qpfile
);
1054 cliopt
.qpfile
= NULL
;
1058 if (cliopt
.framesToBeEncoded
&& inFrameCount
>= cliopt
.framesToBeEncoded
)
1060 else if (cliopt
.input
->readPicture(pic_orig
))
1067 if (pic_in
->bitDepth
> X265_DEPTH
&& cliopt
.bDither
)
1069 ditherImage(*pic_in
, param
->sourceWidth
, param
->sourceHeight
, errorBuf
, X265_DEPTH
);
1070 pic_in
->bitDepth
= X265_DEPTH
;
1072 if (param
->analysisMode
)
1074 x265_alloc_analysis_data(pic_in
);
1076 if (param
->analysisMode
== X265_ANALYSIS_LOAD
)
1077 cliopt
.readAnalysisFile(pic_in
, param
);
1081 int numEncoded
= x265_encoder_encode(encoder
, &p_nal
, &nal
, pic_in
, pic_recon
);
1087 outFrameCount
+= numEncoded
;
1088 if (numEncoded
&& pic_recon
)
1090 cliopt
.recon
->writePicture(pic_out
);
1091 if (param
->analysisMode
== X265_ANALYSIS_SAVE
)
1092 cliopt
.writeAnalysisFile(pic_recon
, param
);
1093 if (param
->analysisMode
)
1094 x265_free_analysis_data(pic_recon
);
1098 cliopt
.writeNALs(p_nal
, nal
);
1100 // Because x265_encoder_encode() lazily encodes entire GOPs, updates are per-GOP
1101 cliopt
.printStatus(outFrameCount
, param
);
1104 /* Flush the encoder */
1107 uint32_t numEncoded
= x265_encoder_encode(encoder
, &p_nal
, &nal
, NULL
, pic_recon
);
1108 outFrameCount
+= numEncoded
;
1109 if (numEncoded
&& pic_recon
)
1111 cliopt
.recon
->writePicture(pic_out
);
1112 if (param
->analysisMode
== X265_ANALYSIS_SAVE
)
1113 cliopt
.writeAnalysisFile(pic_recon
, param
);
1114 if (param
->analysisMode
)
1115 x265_free_analysis_data(pic_recon
);
1119 cliopt
.writeNALs(p_nal
, nal
);
1121 cliopt
.printStatus(outFrameCount
, param
);
1127 /* clear progress report */
1128 if (cliopt
.bProgress
)
1129 fprintf(stderr
, "%*s\r", 80, " ");
1132 x265_encoder_get_stats(encoder
, &stats
, sizeof(stats
));
1133 if (param
->csvfn
&& !b_ctrl_c
)
1134 x265_encoder_log(encoder
, argc
, argv
);
1135 x265_encoder_close(encoder
);
1136 cliopt
.bitstreamFile
.close();
1139 fprintf(stderr
, "aborted at input frame %d, output frame %d\n",
1140 cliopt
.seek
+ inFrameCount
, stats
.encodedPictureCount
);
1142 if (stats
.encodedPictureCount
)
1144 printf("\nencoded %d frames in %.2fs (%.2f fps), %.2f kb/s", stats
.encodedPictureCount
,
1145 stats
.elapsedEncodeTime
, stats
.encodedPictureCount
/ stats
.elapsedEncodeTime
, stats
.bitrate
);
1147 if (param
->bEnablePsnr
)
1148 printf(", Global PSNR: %.3f", stats
.globalPsnr
);
1150 if (param
->bEnableSsim
)
1151 printf(", SSIM Mean Y: %.7f (%6.3f dB)", stats
.globalSsim
, x265_ssim2dB(stats
.globalSsim
));
1157 printf("\nencoded 0 frames\n");
1160 x265_cleanup(); /* Free library singletons */
1164 x265_param_free(param
);
1166 X265_FREE(errorBuf
);
1169 assert(VLDReportLeaks() == 0);