base.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*****************************************************************************
  2. * base.c: misc common functions (bit depth independent)
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Loren Merritt <lorenm@u.washington.edu>
  7. * Laurent Aimar <fenrir@via.ecp.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  22. *
  23. * This program is also available under a commercial proprietary license.
  24. * For more information, contact us at licensing@x264.com.
  25. *****************************************************************************/
  26. #include "base.h"
  27. #include <ctype.h>
  28. #if HAVE_MALLOC_H
  29. #include <malloc.h>
  30. #endif
  31. #if HAVE_THP
  32. #include <sys/mman.h>
  33. #endif
  34. /****************************************************************************
  35. * x264_reduce_fraction:
  36. ****************************************************************************/
  37. #define REDUCE_FRACTION( name, type )\
  38. void name( type *n, type *d )\
  39. { \
  40. type a = *n; \
  41. type b = *d; \
  42. type c; \
  43. if( !a || !b ) \
  44. return; \
  45. c = a % b; \
  46. while( c ) \
  47. { \
  48. a = b; \
  49. b = c; \
  50. c = a % b; \
  51. } \
  52. *n /= b; \
  53. *d /= b; \
  54. }
  55. REDUCE_FRACTION( x264_reduce_fraction , uint32_t )
  56. REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
  57. /****************************************************************************
  58. * x264_log:
  59. ****************************************************************************/
  60. void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
  61. {
  62. char *psz_prefix;
  63. switch( i_level )
  64. {
  65. case X264_LOG_ERROR:
  66. psz_prefix = "error";
  67. break;
  68. case X264_LOG_WARNING:
  69. psz_prefix = "warning";
  70. break;
  71. case X264_LOG_INFO:
  72. psz_prefix = "info";
  73. break;
  74. case X264_LOG_DEBUG:
  75. psz_prefix = "debug";
  76. break;
  77. default:
  78. psz_prefix = "unknown";
  79. break;
  80. }
  81. fprintf( stderr, "x264 [%s]: ", psz_prefix );
  82. x264_vfprintf( stderr, psz_fmt, arg );
  83. }
  84. void x264_log_internal( int i_level, const char *psz_fmt, ... )
  85. {
  86. va_list arg;
  87. va_start( arg, psz_fmt );
  88. x264_log_default( NULL, i_level, psz_fmt, arg );
  89. va_end( arg );
  90. }
  91. /****************************************************************************
  92. * x264_malloc:
  93. ****************************************************************************/
  94. void *x264_malloc( int i_size )
  95. {
  96. uint8_t *align_buf = NULL;
  97. #if HAVE_MALLOC_H
  98. #if HAVE_THP
  99. #define HUGE_PAGE_SIZE 2*1024*1024
  100. #define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
  101. /* Attempt to allocate huge pages to reduce TLB misses. */
  102. if( i_size >= HUGE_PAGE_THRESHOLD )
  103. {
  104. align_buf = memalign( HUGE_PAGE_SIZE, i_size );
  105. if( align_buf )
  106. {
  107. /* Round up to the next huge page boundary if we are close enough. */
  108. size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
  109. madvise( align_buf, madv_size, MADV_HUGEPAGE );
  110. }
  111. }
  112. else
  113. #undef HUGE_PAGE_SIZE
  114. #undef HUGE_PAGE_THRESHOLD
  115. #endif
  116. align_buf = memalign( NATIVE_ALIGN, i_size );
  117. #else
  118. uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
  119. if( buf )
  120. {
  121. align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
  122. align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
  123. *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
  124. }
  125. #endif
  126. if( !align_buf )
  127. x264_log_internal( X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
  128. return align_buf;
  129. }
  130. /****************************************************************************
  131. * x264_free:
  132. ****************************************************************************/
  133. void x264_free( void *p )
  134. {
  135. if( p )
  136. {
  137. #if HAVE_MALLOC_H
  138. free( p );
  139. #else
  140. free( *( ( ( void **) p ) - 1 ) );
  141. #endif
  142. }
  143. }
  144. /****************************************************************************
  145. * x264_slurp_file:
  146. ****************************************************************************/
  147. char *x264_slurp_file( const char *filename )
  148. {
  149. int b_error = 0;
  150. int64_t i_size;
  151. char *buf;
  152. FILE *fh = x264_fopen( filename, "rb" );
  153. if( !fh )
  154. return NULL;
  155. b_error |= fseek( fh, 0, SEEK_END ) < 0;
  156. b_error |= ( i_size = ftell( fh ) ) <= 0;
  157. if( WORD_SIZE == 4 )
  158. b_error |= i_size > INT32_MAX;
  159. b_error |= fseek( fh, 0, SEEK_SET ) < 0;
  160. if( b_error )
  161. goto error;
  162. buf = x264_malloc( i_size+2 );
  163. if( !buf )
  164. goto error;
  165. b_error |= fread( buf, 1, i_size, fh ) != i_size;
  166. fclose( fh );
  167. if( b_error )
  168. {
  169. x264_free( buf );
  170. return NULL;
  171. }
  172. if( buf[i_size-1] != '\n' )
  173. buf[i_size++] = '\n';
  174. buf[i_size] = '\0';
  175. return buf;
  176. error:
  177. fclose( fh );
  178. return NULL;
  179. }
  180. /****************************************************************************
  181. * x264_picture_init:
  182. ****************************************************************************/
  183. static void picture_init( x264_picture_t *pic )
  184. {
  185. memset( pic, 0, sizeof( x264_picture_t ) );
  186. pic->i_type = X264_TYPE_AUTO;
  187. pic->i_qpplus1 = X264_QP_AUTO;
  188. pic->i_pic_struct = PIC_STRUCT_AUTO;
  189. }
  190. void x264_picture_init( x264_picture_t *pic )
  191. {
  192. x264_stack_align( picture_init, pic );
  193. }
  194. /****************************************************************************
  195. * x264_picture_alloc:
  196. ****************************************************************************/
  197. static int picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
  198. {
  199. typedef struct
  200. {
  201. int planes;
  202. int width_fix8[3];
  203. int height_fix8[3];
  204. } x264_csp_tab_t;
  205. static const x264_csp_tab_t csp_tab[] =
  206. {
  207. [X264_CSP_I420] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
  208. [X264_CSP_YV12] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256/2, 256/2 } },
  209. [X264_CSP_NV12] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
  210. [X264_CSP_NV21] = { 2, { 256*1, 256*1 }, { 256*1, 256/2 }, },
  211. [X264_CSP_I422] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
  212. [X264_CSP_YV16] = { 3, { 256*1, 256/2, 256/2 }, { 256*1, 256*1, 256*1 } },
  213. [X264_CSP_NV16] = { 2, { 256*1, 256*1 }, { 256*1, 256*1 }, },
  214. [X264_CSP_YUYV] = { 1, { 256*2 }, { 256*1 }, },
  215. [X264_CSP_UYVY] = { 1, { 256*2 }, { 256*1 }, },
  216. [X264_CSP_I444] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
  217. [X264_CSP_YV24] = { 3, { 256*1, 256*1, 256*1 }, { 256*1, 256*1, 256*1 } },
  218. [X264_CSP_BGR] = { 1, { 256*3 }, { 256*1 }, },
  219. [X264_CSP_BGRA] = { 1, { 256*4 }, { 256*1 }, },
  220. [X264_CSP_RGB] = { 1, { 256*3 }, { 256*1 }, },
  221. };
  222. int csp = i_csp & X264_CSP_MASK;
  223. if( csp <= X264_CSP_NONE || csp >= X264_CSP_MAX || csp == X264_CSP_V210 )
  224. return -1;
  225. picture_init( pic );
  226. pic->img.i_csp = i_csp;
  227. pic->img.i_plane = csp_tab[csp].planes;
  228. int depth_factor = i_csp & X264_CSP_HIGH_DEPTH ? 2 : 1;
  229. int plane_offset[3] = {0};
  230. int frame_size = 0;
  231. for( int i = 0; i < pic->img.i_plane; i++ )
  232. {
  233. int stride = (((int64_t)i_width * csp_tab[csp].width_fix8[i]) >> 8) * depth_factor;
  234. int plane_size = (((int64_t)i_height * csp_tab[csp].height_fix8[i]) >> 8) * stride;
  235. pic->img.i_stride[i] = stride;
  236. plane_offset[i] = frame_size;
  237. frame_size += plane_size;
  238. }
  239. pic->img.plane[0] = x264_malloc( frame_size );
  240. if( !pic->img.plane[0] )
  241. return -1;
  242. for( int i = 1; i < pic->img.i_plane; i++ )
  243. pic->img.plane[i] = pic->img.plane[0] + plane_offset[i];
  244. return 0;
  245. }
  246. int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
  247. {
  248. return x264_stack_align( picture_alloc, pic, i_csp, i_width, i_height );
  249. }
  250. /****************************************************************************
  251. * x264_picture_clean:
  252. ****************************************************************************/
  253. static void picture_clean( x264_picture_t *pic )
  254. {
  255. x264_free( pic->img.plane[0] );
  256. /* just to be safe */
  257. memset( pic, 0, sizeof( x264_picture_t ) );
  258. }
  259. void x264_picture_clean( x264_picture_t *pic )
  260. {
  261. x264_stack_align( picture_clean, pic );
  262. }
  263. /****************************************************************************
  264. * x264_param_default:
  265. ****************************************************************************/
  266. static void param_default( x264_param_t *param )
  267. {
  268. /* */
  269. memset( param, 0, sizeof( x264_param_t ) );
  270. /* CPU autodetect */
  271. param->cpu = x264_cpu_detect();
  272. param->i_threads = X264_THREADS_AUTO;
  273. param->i_lookahead_threads = X264_THREADS_AUTO;
  274. param->b_deterministic = 1;
  275. param->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
  276. /* Video properties */
  277. param->i_csp = X264_CHROMA_FORMAT ? X264_CHROMA_FORMAT : X264_CSP_I420;
  278. param->i_width = 0;
  279. param->i_height = 0;
  280. param->vui.i_sar_width = 0;
  281. param->vui.i_sar_height= 0;
  282. param->vui.i_overscan = 0; /* undef */
  283. param->vui.i_vidformat = 5; /* undef */
  284. param->vui.b_fullrange = -1; /* default depends on input */
  285. param->vui.i_colorprim = 2; /* undef */
  286. param->vui.i_transfer = 2; /* undef */
  287. param->vui.i_colmatrix = -1; /* default depends on input */
  288. param->vui.i_chroma_loc= 0; /* left center */
  289. param->i_fps_num = 25;
  290. param->i_fps_den = 1;
  291. param->i_level_idc = -1;
  292. param->i_slice_max_size = 0;
  293. param->i_slice_max_mbs = 0;
  294. param->i_slice_count = 0;
  295. #if HAVE_BITDEPTH8
  296. param->i_bitdepth = 8;
  297. #elif HAVE_BITDEPTH10
  298. param->i_bitdepth = 10;
  299. #else
  300. param->i_bitdepth = 8;
  301. #endif
  302. /* Encoder parameters */
  303. param->i_frame_reference = 3;
  304. param->i_keyint_max = 250;
  305. param->i_keyint_min = X264_KEYINT_MIN_AUTO;
  306. param->i_bframe = 3;
  307. param->i_scenecut_threshold = 40;
  308. param->i_bframe_adaptive = X264_B_ADAPT_FAST;
  309. param->i_bframe_bias = 0;
  310. param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
  311. param->b_interlaced = 0;
  312. param->b_constrained_intra = 0;
  313. param->b_deblocking_filter = 1;
  314. param->i_deblocking_filter_alphac0 = 0;
  315. param->i_deblocking_filter_beta = 0;
  316. param->b_cabac = 1;
  317. param->i_cabac_init_idc = 0;
  318. param->rc.i_rc_method = X264_RC_CRF;
  319. param->rc.i_bitrate = 0;
  320. param->rc.f_rate_tolerance = 1.0;
  321. param->rc.i_vbv_max_bitrate = 0;
  322. param->rc.i_vbv_buffer_size = 0;
  323. param->rc.f_vbv_buffer_init = 0.9;
  324. param->rc.i_qp_constant = -1;
  325. param->rc.f_rf_constant = 23;
  326. param->rc.i_qp_min = 0;
  327. param->rc.i_qp_max = INT_MAX;
  328. param->rc.i_qp_step = 4;
  329. param->rc.f_ip_factor = 1.4;
  330. param->rc.f_pb_factor = 1.3;
  331. param->rc.i_aq_mode = X264_AQ_VARIANCE;
  332. param->rc.f_aq_strength = 1.0;
  333. param->rc.i_lookahead = 40;
  334. param->rc.b_stat_write = 0;
  335. param->rc.psz_stat_out = "x264_2pass.log";
  336. param->rc.b_stat_read = 0;
  337. param->rc.psz_stat_in = "x264_2pass.log";
  338. param->rc.f_qcompress = 0.6;
  339. param->rc.f_qblur = 0.5;
  340. param->rc.f_complexity_blur = 20;
  341. param->rc.i_zones = 0;
  342. param->rc.b_mb_tree = 1;
  343. /* Log */
  344. param->pf_log = x264_log_default;
  345. param->p_log_private = NULL;
  346. param->i_log_level = X264_LOG_INFO;
  347. /* */
  348. param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
  349. param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
  350. | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
  351. param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
  352. param->analyse.i_me_method = X264_ME_HEX;
  353. param->analyse.f_psy_rd = 1.0;
  354. param->analyse.b_psy = 1;
  355. param->analyse.f_psy_trellis = 0;
  356. param->analyse.i_me_range = 16;
  357. param->analyse.i_subpel_refine = 7;
  358. param->analyse.b_mixed_references = 1;
  359. param->analyse.b_chroma_me = 1;
  360. param->analyse.i_mv_range_thread = -1;
  361. param->analyse.i_mv_range = -1; // set from level_idc
  362. param->analyse.i_chroma_qp_offset = 0;
  363. param->analyse.b_fast_pskip = 1;
  364. param->analyse.b_weighted_bipred = 1;
  365. param->analyse.i_weighted_pred = X264_WEIGHTP_SMART;
  366. param->analyse.b_dct_decimate = 1;
  367. param->analyse.b_transform_8x8 = 1;
  368. param->analyse.i_trellis = 1;
  369. param->analyse.i_luma_deadzone[0] = 21;
  370. param->analyse.i_luma_deadzone[1] = 11;
  371. param->analyse.b_psnr = 0;
  372. param->analyse.b_ssim = 0;
  373. param->i_cqm_preset = X264_CQM_FLAT;
  374. memset( param->cqm_4iy, 16, sizeof( param->cqm_4iy ) );
  375. memset( param->cqm_4py, 16, sizeof( param->cqm_4py ) );
  376. memset( param->cqm_4ic, 16, sizeof( param->cqm_4ic ) );
  377. memset( param->cqm_4pc, 16, sizeof( param->cqm_4pc ) );
  378. memset( param->cqm_8iy, 16, sizeof( param->cqm_8iy ) );
  379. memset( param->cqm_8py, 16, sizeof( param->cqm_8py ) );
  380. memset( param->cqm_8ic, 16, sizeof( param->cqm_8ic ) );
  381. memset( param->cqm_8pc, 16, sizeof( param->cqm_8pc ) );
  382. param->b_repeat_headers = 1;
  383. param->b_annexb = 1;
  384. param->b_aud = 0;
  385. param->b_vfr_input = 1;
  386. param->i_nal_hrd = X264_NAL_HRD_NONE;
  387. param->b_tff = 1;
  388. param->b_pic_struct = 0;
  389. param->b_fake_interlaced = 0;
  390. param->i_frame_packing = -1;
  391. param->i_alternative_transfer = 2; /* undef */
  392. param->b_opencl = 0;
  393. param->i_opencl_device = 0;
  394. param->opencl_device_id = NULL;
  395. param->psz_clbin_file = NULL;
  396. param->i_avcintra_class = 0;
  397. param->i_avcintra_flavor = X264_AVCINTRA_FLAVOR_PANASONIC;
  398. }
  399. void x264_param_default( x264_param_t *param )
  400. {
  401. x264_stack_align( param_default, param );
  402. }
  403. static int param_apply_preset( x264_param_t *param, const char *preset )
  404. {
  405. char *end;
  406. int i = strtol( preset, &end, 10 );
  407. if( *end == 0 && i >= 0 && i < sizeof(x264_preset_names)/sizeof(*x264_preset_names)-1 )
  408. preset = x264_preset_names[i];
  409. if( !strcasecmp( preset, "ultrafast" ) )
  410. {
  411. param->i_frame_reference = 1;
  412. param->i_scenecut_threshold = 0;
  413. param->b_deblocking_filter = 0;
  414. param->b_cabac = 0;
  415. param->i_bframe = 0;
  416. param->analyse.intra = 0;
  417. param->analyse.inter = 0;
  418. param->analyse.b_transform_8x8 = 0;
  419. param->analyse.i_me_method = X264_ME_DIA;
  420. param->analyse.i_subpel_refine = 0;
  421. param->rc.i_aq_mode = 0;
  422. param->analyse.b_mixed_references = 0;
  423. param->analyse.i_trellis = 0;
  424. param->i_bframe_adaptive = X264_B_ADAPT_NONE;
  425. param->rc.b_mb_tree = 0;
  426. param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
  427. param->analyse.b_weighted_bipred = 0;
  428. param->rc.i_lookahead = 0;
  429. }
  430. else if( !strcasecmp( preset, "superfast" ) )
  431. {
  432. param->analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
  433. param->analyse.i_me_method = X264_ME_DIA;
  434. param->analyse.i_subpel_refine = 1;
  435. param->i_frame_reference = 1;
  436. param->analyse.b_mixed_references = 0;
  437. param->analyse.i_trellis = 0;
  438. param->rc.b_mb_tree = 0;
  439. param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
  440. param->rc.i_lookahead = 0;
  441. }
  442. else if( !strcasecmp( preset, "veryfast" ) )
  443. {
  444. param->analyse.i_subpel_refine = 2;
  445. param->i_frame_reference = 1;
  446. param->analyse.b_mixed_references = 0;
  447. param->analyse.i_trellis = 0;
  448. param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
  449. param->rc.i_lookahead = 10;
  450. }
  451. else if( !strcasecmp( preset, "faster" ) )
  452. {
  453. param->analyse.b_mixed_references = 0;
  454. param->i_frame_reference = 2;
  455. param->analyse.i_subpel_refine = 4;
  456. param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
  457. param->rc.i_lookahead = 20;
  458. }
  459. else if( !strcasecmp( preset, "fast" ) )
  460. {
  461. param->i_frame_reference = 2;
  462. param->analyse.i_subpel_refine = 6;
  463. param->analyse.i_weighted_pred = X264_WEIGHTP_SIMPLE;
  464. param->rc.i_lookahead = 30;
  465. }
  466. else if( !strcasecmp( preset, "medium" ) )
  467. {
  468. /* Default is medium */
  469. }
  470. else if( !strcasecmp( preset, "slow" ) )
  471. {
  472. param->analyse.i_subpel_refine = 8;
  473. param->i_frame_reference = 5;
  474. param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
  475. param->analyse.i_trellis = 2;
  476. param->rc.i_lookahead = 50;
  477. }
  478. else if( !strcasecmp( preset, "slower" ) )
  479. {
  480. param->analyse.i_me_method = X264_ME_UMH;
  481. param->analyse.i_subpel_refine = 9;
  482. param->i_frame_reference = 8;
  483. param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
  484. param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
  485. param->analyse.inter |= X264_ANALYSE_PSUB8x8;
  486. param->analyse.i_trellis = 2;
  487. param->rc.i_lookahead = 60;
  488. }
  489. else if( !strcasecmp( preset, "veryslow" ) )
  490. {
  491. param->analyse.i_me_method = X264_ME_UMH;
  492. param->analyse.i_subpel_refine = 10;
  493. param->analyse.i_me_range = 24;
  494. param->i_frame_reference = 16;
  495. param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
  496. param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
  497. param->analyse.inter |= X264_ANALYSE_PSUB8x8;
  498. param->analyse.i_trellis = 2;
  499. param->i_bframe = 8;
  500. param->rc.i_lookahead = 60;
  501. }
  502. else if( !strcasecmp( preset, "placebo" ) )
  503. {
  504. param->analyse.i_me_method = X264_ME_TESA;
  505. param->analyse.i_subpel_refine = 11;
  506. param->analyse.i_me_range = 24;
  507. param->i_frame_reference = 16;
  508. param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
  509. param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
  510. param->analyse.inter |= X264_ANALYSE_PSUB8x8;
  511. param->analyse.b_fast_pskip = 0;
  512. param->analyse.i_trellis = 2;
  513. param->i_bframe = 16;
  514. param->rc.i_lookahead = 60;
  515. }
  516. else
  517. {
  518. x264_log_internal( X264_LOG_ERROR, "invalid preset '%s'\n", preset );
  519. return -1;
  520. }
  521. return 0;
  522. }
  523. static int param_apply_tune( x264_param_t *param, const char *tune )
  524. {
  525. int psy_tuning_used = 0;
  526. for( int len; tune += strspn( tune, ",./-+" ), (len = strcspn( tune, ",./-+" )); tune += len )
  527. {
  528. if( len == 4 && !strncasecmp( tune, "film", 4 ) )
  529. {
  530. if( psy_tuning_used++ ) goto psy_failure;
  531. param->i_deblocking_filter_alphac0 = -1;
  532. param->i_deblocking_filter_beta = -1;
  533. param->analyse.f_psy_trellis = 0.15;
  534. }
  535. else if( len == 9 && !strncasecmp( tune, "animation", 9 ) )
  536. {
  537. if( psy_tuning_used++ ) goto psy_failure;
  538. param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
  539. param->i_deblocking_filter_alphac0 = 1;
  540. param->i_deblocking_filter_beta = 1;
  541. param->analyse.f_psy_rd = 0.4;
  542. param->rc.f_aq_strength = 0.6;
  543. param->i_bframe += 2;
  544. }
  545. else if( len == 5 && !strncasecmp( tune, "grain", 5 ) )
  546. {
  547. if( psy_tuning_used++ ) goto psy_failure;
  548. param->i_deblocking_filter_alphac0 = -2;
  549. param->i_deblocking_filter_beta = -2;
  550. param->analyse.f_psy_trellis = 0.25;
  551. param->analyse.b_dct_decimate = 0;
  552. param->rc.f_pb_factor = 1.1;
  553. param->rc.f_ip_factor = 1.1;
  554. param->rc.f_aq_strength = 0.5;
  555. param->analyse.i_luma_deadzone[0] = 6;
  556. param->analyse.i_luma_deadzone[1] = 6;
  557. param->rc.f_qcompress = 0.8;
  558. }
  559. else if( len == 10 && !strncasecmp( tune, "stillimage", 10 ) )
  560. {
  561. if( psy_tuning_used++ ) goto psy_failure;
  562. param->i_deblocking_filter_alphac0 = -3;
  563. param->i_deblocking_filter_beta = -3;
  564. param->analyse.f_psy_rd = 2.0;
  565. param->analyse.f_psy_trellis = 0.7;
  566. param->rc.f_aq_strength = 1.2;
  567. }
  568. else if( len == 4 && !strncasecmp( tune, "psnr", 4 ) )
  569. {
  570. if( psy_tuning_used++ ) goto psy_failure;
  571. param->rc.i_aq_mode = X264_AQ_NONE;
  572. param->analyse.b_psy = 0;
  573. }
  574. else if( len == 4 && !strncasecmp( tune, "ssim", 4 ) )
  575. {
  576. if( psy_tuning_used++ ) goto psy_failure;
  577. param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
  578. param->analyse.b_psy = 0;
  579. }
  580. else if( len == 10 && !strncasecmp( tune, "fastdecode", 10 ) )
  581. {
  582. param->b_deblocking_filter = 0;
  583. param->b_cabac = 0;
  584. param->analyse.b_weighted_bipred = 0;
  585. param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
  586. }
  587. else if( len == 11 && !strncasecmp( tune, "zerolatency", 11 ) )
  588. {
  589. param->rc.i_lookahead = 0;
  590. param->i_sync_lookahead = 0;
  591. param->i_bframe = 0;
  592. param->b_sliced_threads = 1;
  593. param->b_vfr_input = 0;
  594. param->rc.b_mb_tree = 0;
  595. }
  596. else if( len == 6 && !strncasecmp( tune, "touhou", 6 ) )
  597. {
  598. if( psy_tuning_used++ ) goto psy_failure;
  599. param->i_frame_reference = param->i_frame_reference > 1 ? param->i_frame_reference*2 : 1;
  600. param->i_deblocking_filter_alphac0 = -1;
  601. param->i_deblocking_filter_beta = -1;
  602. param->analyse.f_psy_trellis = 0.2;
  603. param->rc.f_aq_strength = 1.3;
  604. if( param->analyse.inter & X264_ANALYSE_PSUB16x16 )
  605. param->analyse.inter |= X264_ANALYSE_PSUB8x8;
  606. }
  607. else
  608. {
  609. x264_log_internal( X264_LOG_ERROR, "invalid tune '%.*s'\n", len, tune );
  610. return -1;
  611. psy_failure:
  612. x264_log_internal( X264_LOG_WARNING, "only 1 psy tuning can be used: ignoring tune %.*s\n", len, tune );
  613. }
  614. }
  615. return 0;
  616. }
  617. static int param_default_preset( x264_param_t *param, const char *preset, const char *tune )
  618. {
  619. param_default( param );
  620. if( preset && param_apply_preset( param, preset ) < 0 )
  621. return -1;
  622. if( tune && param_apply_tune( param, tune ) < 0 )
  623. return -1;
  624. return 0;
  625. }
  626. int x264_param_default_preset( x264_param_t *param, const char *preset, const char *tune )
  627. {
  628. return x264_stack_align( param_default_preset, param, preset, tune );
  629. }
  630. static void param_apply_fastfirstpass( x264_param_t *param )
  631. {
  632. /* Set faster options in case of turbo firstpass. */
  633. if( param->rc.b_stat_write && !param->rc.b_stat_read )
  634. {
  635. param->i_frame_reference = 1;
  636. param->analyse.b_transform_8x8 = 0;
  637. param->analyse.inter = 0;
  638. param->analyse.i_me_method = X264_ME_DIA;
  639. param->analyse.i_subpel_refine = X264_MIN( 2, param->analyse.i_subpel_refine );
  640. param->analyse.i_trellis = 0;
  641. param->analyse.b_fast_pskip = 1;
  642. }
  643. }
  644. void x264_param_apply_fastfirstpass( x264_param_t *param )
  645. {
  646. x264_stack_align( param_apply_fastfirstpass, param );
  647. }
  648. static int profile_string_to_int( const char *str )
  649. {
  650. if( !strcasecmp( str, "baseline" ) )
  651. return PROFILE_BASELINE;
  652. if( !strcasecmp( str, "main" ) )
  653. return PROFILE_MAIN;
  654. if( !strcasecmp( str, "high" ) )
  655. return PROFILE_HIGH;
  656. if( !strcasecmp( str, "high10" ) )
  657. return PROFILE_HIGH10;
  658. if( !strcasecmp( str, "high422" ) )
  659. return PROFILE_HIGH422;
  660. if( !strcasecmp( str, "high444" ) )
  661. return PROFILE_HIGH444_PREDICTIVE;
  662. return -1;
  663. }
  664. static int param_apply_profile( x264_param_t *param, const char *profile )
  665. {
  666. if( !profile )
  667. return 0;
  668. const int qp_bd_offset = 6 * (param->i_bitdepth-8);
  669. int p = profile_string_to_int( profile );
  670. if( p < 0 )
  671. {
  672. x264_log_internal( X264_LOG_ERROR, "invalid profile: %s\n", profile );
  673. return -1;
  674. }
  675. if( p < PROFILE_HIGH444_PREDICTIVE && ((param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant <= 0) ||
  676. (param->rc.i_rc_method == X264_RC_CRF && (int)(param->rc.f_rf_constant + qp_bd_offset) <= 0)) )
  677. {
  678. x264_log_internal( X264_LOG_ERROR, "%s profile doesn't support lossless\n", profile );
  679. return -1;
  680. }
  681. if( p < PROFILE_HIGH444_PREDICTIVE && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I444 )
  682. {
  683. x264_log_internal( X264_LOG_ERROR, "%s profile doesn't support 4:4:4\n", profile );
  684. return -1;
  685. }
  686. if( p < PROFILE_HIGH422 && (param->i_csp & X264_CSP_MASK) >= X264_CSP_I422 )
  687. {
  688. x264_log_internal( X264_LOG_ERROR, "%s profile doesn't support 4:2:2\n", profile );
  689. return -1;
  690. }
  691. if( p < PROFILE_HIGH10 && param->i_bitdepth > 8 )
  692. {
  693. x264_log_internal( X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d\n", profile, param->i_bitdepth );
  694. return -1;
  695. }
  696. if( p < PROFILE_HIGH && (param->i_csp & X264_CSP_MASK) == X264_CSP_I400 )
  697. {
  698. x264_log_internal( X264_LOG_ERROR, "%s profile doesn't support 4:0:0\n", profile );
  699. return -1;
  700. }
  701. if( p == PROFILE_BASELINE )
  702. {
  703. param->analyse.b_transform_8x8 = 0;
  704. param->b_cabac = 0;
  705. param->i_cqm_preset = X264_CQM_FLAT;
  706. param->psz_cqm_file = NULL;
  707. param->i_bframe = 0;
  708. param->analyse.i_weighted_pred = X264_WEIGHTP_NONE;
  709. if( param->b_interlaced )
  710. {
  711. x264_log_internal( X264_LOG_ERROR, "baseline profile doesn't support interlacing\n" );
  712. return -1;
  713. }
  714. if( param->b_fake_interlaced )
  715. {
  716. x264_log_internal( X264_LOG_ERROR, "baseline profile doesn't support fake interlacing\n" );
  717. return -1;
  718. }
  719. }
  720. else if( p == PROFILE_MAIN )
  721. {
  722. param->analyse.b_transform_8x8 = 0;
  723. param->i_cqm_preset = X264_CQM_FLAT;
  724. param->psz_cqm_file = NULL;
  725. }
  726. return 0;
  727. }
  728. int x264_param_apply_profile( x264_param_t *param, const char *profile )
  729. {
  730. return x264_stack_align( param_apply_profile, param, profile );
  731. }
  732. static int parse_enum( const char *arg, const char * const *names, int *dst )
  733. {
  734. for( int i = 0; names[i]; i++ )
  735. if( !strcasecmp( arg, names[i] ) )
  736. {
  737. *dst = i;
  738. return 0;
  739. }
  740. return -1;
  741. }
  742. static int parse_cqm( const char *str, uint8_t *cqm, int length )
  743. {
  744. int i = 0;
  745. do {
  746. int coef;
  747. if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
  748. return -1;
  749. cqm[i++] = coef;
  750. } while( i < length && (str = strchr( str, ',' )) && str++ );
  751. return (i == length) ? 0 : -1;
  752. }
  753. static int atobool_internal( const char *str, int *b_error )
  754. {
  755. if( !strcmp(str, "1") ||
  756. !strcasecmp(str, "true") ||
  757. !strcasecmp(str, "yes") )
  758. return 1;
  759. if( !strcmp(str, "0") ||
  760. !strcasecmp(str, "false") ||
  761. !strcasecmp(str, "no") )
  762. return 0;
  763. *b_error = 1;
  764. return 0;
  765. }
  766. static int atoi_internal( const char *str, int *b_error )
  767. {
  768. char *end;
  769. int v = strtol( str, &end, 0 );
  770. if( end == str || *end != '\0' )
  771. *b_error = 1;
  772. return v;
  773. }
  774. static double atof_internal( const char *str, int *b_error )
  775. {
  776. char *end;
  777. double v = strtod( str, &end );
  778. if( end == str || *end != '\0' )
  779. *b_error = 1;
  780. return v;
  781. }
  782. #define atobool(str) ( name_was_bool = 1, atobool_internal( str, &b_error ) )
  783. #undef atoi
  784. #undef atof
  785. #define atoi(str) atoi_internal( str, &b_error )
  786. #define atof(str) atof_internal( str, &b_error )
  787. static int param_parse( x264_param_t *p, const char *name, const char *value )
  788. {
  789. char *name_buf = NULL;
  790. int b_error = 0;
  791. int errortype = X264_PARAM_BAD_VALUE;
  792. int name_was_bool;
  793. int value_was_null = !value;
  794. if( !name )
  795. return X264_PARAM_BAD_NAME;
  796. if( !value )
  797. value = "true";
  798. if( value[0] == '=' )
  799. value++;
  800. if( strchr( name, '_' ) ) // s/_/-/g
  801. {
  802. char *c;
  803. name_buf = strdup(name);
  804. if( !name_buf )
  805. return X264_PARAM_BAD_NAME;
  806. while( (c = strchr( name_buf, '_' )) )
  807. *c = '-';
  808. name = name_buf;
  809. }
  810. if( !strncmp( name, "no", 2 ) )
  811. {
  812. name += 2;
  813. if( name[0] == '-' )
  814. name++;
  815. value = atobool(value) ? "false" : "true";
  816. }
  817. name_was_bool = 0;
  818. #define OPT(STR) else if( !strcmp( name, STR ) )
  819. #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
  820. if( 0 );
  821. OPT("asm")
  822. {
  823. p->cpu = isdigit(value[0]) ? atoi(value) :
  824. !strcasecmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
  825. if( b_error )
  826. {
  827. char *buf = strdup( value );
  828. if( buf )
  829. {
  830. char *tok, UNUSED *saveptr=NULL, *init;
  831. b_error = 0;
  832. p->cpu = 0;
  833. for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
  834. {
  835. int i = 0;
  836. while( x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name) )
  837. i++;
  838. p->cpu |= x264_cpu_names[i].flags;
  839. if( !x264_cpu_names[i].flags )
  840. b_error = 1;
  841. }
  842. free( buf );
  843. if( (p->cpu&X264_CPU_SSSE3) && !(p->cpu&X264_CPU_SSE2_IS_SLOW) )
  844. p->cpu |= X264_CPU_SSE2_IS_FAST;
  845. }
  846. }
  847. }
  848. OPT("threads")
  849. {
  850. if( !strcasecmp(value, "auto") )
  851. p->i_threads = X264_THREADS_AUTO;
  852. else
  853. p->i_threads = atoi(value);
  854. }
  855. OPT("lookahead-threads")
  856. {
  857. if( !strcasecmp(value, "auto") )
  858. p->i_lookahead_threads = X264_THREADS_AUTO;
  859. else
  860. p->i_lookahead_threads = atoi(value);
  861. }
  862. OPT("sliced-threads")
  863. p->b_sliced_threads = atobool(value);
  864. OPT("sync-lookahead")
  865. {
  866. if( !strcasecmp(value, "auto") )
  867. p->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
  868. else
  869. p->i_sync_lookahead = atoi(value);
  870. }
  871. OPT2("deterministic", "n-deterministic")
  872. p->b_deterministic = atobool(value);
  873. OPT("cpu-independent")
  874. p->b_cpu_independent = atobool(value);
  875. OPT2("level", "level-idc")
  876. {
  877. if( !strcmp(value, "1b") )
  878. p->i_level_idc = 9;
  879. else if( atof(value) < 7 )
  880. p->i_level_idc = (int)(10*atof(value)+.5);
  881. else
  882. p->i_level_idc = atoi(value);
  883. }
  884. OPT("bluray-compat")
  885. p->b_bluray_compat = atobool(value);
  886. OPT("avcintra-class")
  887. p->i_avcintra_class = atoi(value);
  888. OPT("avcintra-flavor")
  889. b_error |= parse_enum( value, x264_avcintra_flavor_names, &p->i_avcintra_flavor );
  890. OPT("sar")
  891. {
  892. b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
  893. 2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
  894. }
  895. OPT("overscan")
  896. b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
  897. OPT("videoformat")
  898. b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
  899. OPT("fullrange")
  900. b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
  901. OPT("colorprim")
  902. b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
  903. OPT("transfer")
  904. b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
  905. OPT("colormatrix")
  906. b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
  907. OPT("chromaloc")
  908. {
  909. p->vui.i_chroma_loc = atoi(value);
  910. b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
  911. }
  912. OPT("alternative-transfer")
  913. b_error |= parse_enum( value, x264_transfer_names, &p->i_alternative_transfer );
  914. OPT("fps")
  915. {
  916. if( sscanf( value, "%u/%u", &p->i_fps_num, &p->i_fps_den ) != 2 )
  917. {
  918. double fps = atof(value);
  919. if( fps > 0.0 && fps <= INT_MAX/1000.0 )
  920. {
  921. p->i_fps_num = (int)(fps * 1000.0 + .5);
  922. p->i_fps_den = 1000;
  923. }
  924. else
  925. {
  926. p->i_fps_num = atoi(value);
  927. p->i_fps_den = 1;
  928. }
  929. }
  930. }
  931. OPT2("ref", "frameref")
  932. p->i_frame_reference = atoi(value);
  933. OPT("dpb-size")
  934. p->i_dpb_size = atoi(value);
  935. OPT("keyint")
  936. {
  937. if( strstr( value, "infinite" ) )
  938. p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
  939. else
  940. p->i_keyint_max = atoi(value);
  941. }
  942. OPT2("min-keyint", "keyint-min")
  943. {
  944. p->i_keyint_min = atoi(value);
  945. if( p->i_keyint_max < p->i_keyint_min )
  946. p->i_keyint_max = p->i_keyint_min;
  947. }
  948. OPT("scenecut")
  949. {
  950. p->i_scenecut_threshold = atobool(value);
  951. if( b_error || p->i_scenecut_threshold )
  952. {
  953. b_error = 0;
  954. p->i_scenecut_threshold = atoi(value);
  955. }
  956. }
  957. OPT("intra-refresh")
  958. p->b_intra_refresh = atobool(value);
  959. OPT("bframes")
  960. p->i_bframe = atoi(value);
  961. OPT("b-adapt")
  962. {
  963. p->i_bframe_adaptive = atobool(value);
  964. if( b_error )
  965. {
  966. b_error = 0;
  967. p->i_bframe_adaptive = atoi(value);
  968. }
  969. }
  970. OPT("b-bias")
  971. p->i_bframe_bias = atoi(value);
  972. OPT("b-pyramid")
  973. {
  974. b_error |= parse_enum( value, x264_b_pyramid_names, &p->i_bframe_pyramid );
  975. if( b_error )
  976. {
  977. b_error = 0;
  978. p->i_bframe_pyramid = atoi(value);
  979. }
  980. }
  981. OPT("open-gop")
  982. p->b_open_gop = atobool(value);
  983. OPT("nf")
  984. p->b_deblocking_filter = !atobool(value);
  985. OPT2("filter", "deblock")
  986. {
  987. if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
  988. 2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
  989. {
  990. p->b_deblocking_filter = 1;
  991. }
  992. else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
  993. {
  994. p->b_deblocking_filter = 1;
  995. p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
  996. }
  997. else
  998. p->b_deblocking_filter = atobool(value);
  999. }
  1000. OPT("slice-max-size")
  1001. p->i_slice_max_size = atoi(value);
  1002. OPT("slice-max-mbs")
  1003. p->i_slice_max_mbs = atoi(value);
  1004. OPT("slice-min-mbs")
  1005. p->i_slice_min_mbs = atoi(value);
  1006. OPT("slices")
  1007. p->i_slice_count = atoi(value);
  1008. OPT("slices-max")
  1009. p->i_slice_count_max = atoi(value);
  1010. OPT("cabac")
  1011. p->b_cabac = atobool(value);
  1012. OPT("cabac-idc")
  1013. p->i_cabac_init_idc = atoi(value);
  1014. OPT("interlaced")
  1015. p->b_interlaced = atobool(value);
  1016. OPT("tff")
  1017. p->b_interlaced = p->b_tff = atobool(value);
  1018. OPT("bff")
  1019. {
  1020. p->b_interlaced = atobool(value);
  1021. p->b_tff = !p->b_interlaced;
  1022. }
  1023. OPT("constrained-intra")
  1024. p->b_constrained_intra = atobool(value);
  1025. OPT("cqm")
  1026. {
  1027. if( strstr( value, "flat" ) )
  1028. p->i_cqm_preset = X264_CQM_FLAT;
  1029. else if( strstr( value, "jvt" ) )
  1030. p->i_cqm_preset = X264_CQM_JVT;
  1031. else
  1032. p->psz_cqm_file = strdup(value);
  1033. }
  1034. OPT("cqmfile")
  1035. p->psz_cqm_file = strdup(value);
  1036. OPT("cqm4")
  1037. {
  1038. p->i_cqm_preset = X264_CQM_CUSTOM;
  1039. b_error |= parse_cqm( value, p->cqm_4iy, 16 );
  1040. b_error |= parse_cqm( value, p->cqm_4py, 16 );
  1041. b_error |= parse_cqm( value, p->cqm_4ic, 16 );
  1042. b_error |= parse_cqm( value, p->cqm_4pc, 16 );
  1043. }
  1044. OPT("cqm8")
  1045. {
  1046. p->i_cqm_preset = X264_CQM_CUSTOM;
  1047. b_error |= parse_cqm( value, p->cqm_8iy, 64 );
  1048. b_error |= parse_cqm( value, p->cqm_8py, 64 );
  1049. b_error |= parse_cqm( value, p->cqm_8ic, 64 );
  1050. b_error |= parse_cqm( value, p->cqm_8pc, 64 );
  1051. }
  1052. OPT("cqm4i")
  1053. {
  1054. p->i_cqm_preset = X264_CQM_CUSTOM;
  1055. b_error |= parse_cqm( value, p->cqm_4iy, 16 );
  1056. b_error |= parse_cqm( value, p->cqm_4ic, 16 );
  1057. }
  1058. OPT("cqm4p")
  1059. {
  1060. p->i_cqm_preset = X264_CQM_CUSTOM;
  1061. b_error |= parse_cqm( value, p->cqm_4py, 16 );
  1062. b_error |= parse_cqm( value, p->cqm_4pc, 16 );
  1063. }
  1064. OPT("cqm4iy")
  1065. {
  1066. p->i_cqm_preset = X264_CQM_CUSTOM;
  1067. b_error |= parse_cqm( value, p->cqm_4iy, 16 );
  1068. }
  1069. OPT("cqm4ic")
  1070. {
  1071. p->i_cqm_preset = X264_CQM_CUSTOM;
  1072. b_error |= parse_cqm( value, p->cqm_4ic, 16 );
  1073. }
  1074. OPT("cqm4py")
  1075. {
  1076. p->i_cqm_preset = X264_CQM_CUSTOM;
  1077. b_error |= parse_cqm( value, p->cqm_4py, 16 );
  1078. }
  1079. OPT("cqm4pc")
  1080. {
  1081. p->i_cqm_preset = X264_CQM_CUSTOM;
  1082. b_error |= parse_cqm( value, p->cqm_4pc, 16 );
  1083. }
  1084. OPT("cqm8i")
  1085. {
  1086. p->i_cqm_preset = X264_CQM_CUSTOM;
  1087. b_error |= parse_cqm( value, p->cqm_8iy, 64 );
  1088. b_error |= parse_cqm( value, p->cqm_8ic, 64 );
  1089. }
  1090. OPT("cqm8p")
  1091. {
  1092. p->i_cqm_preset = X264_CQM_CUSTOM;
  1093. b_error |= parse_cqm( value, p->cqm_8py, 64 );
  1094. b_error |= parse_cqm( value, p->cqm_8pc, 64 );
  1095. }
  1096. OPT("log")
  1097. p->i_log_level = atoi(value);
  1098. OPT("dump-yuv")
  1099. p->psz_dump_yuv = strdup(value);
  1100. OPT2("analyse", "partitions")
  1101. {
  1102. p->analyse.inter = 0;
  1103. if( strstr( value, "none" ) ) p->analyse.inter = 0;
  1104. if( strstr( value, "all" ) ) p->analyse.inter = ~0;
  1105. if( strstr( value, "i4x4" ) ) p->analyse.inter |= X264_ANALYSE_I4x4;
  1106. if( strstr( value, "i8x8" ) ) p->analyse.inter |= X264_ANALYSE_I8x8;
  1107. if( strstr( value, "p8x8" ) ) p->analyse.inter |= X264_ANALYSE_PSUB16x16;
  1108. if( strstr( value, "p4x4" ) ) p->analyse.inter |= X264_ANALYSE_PSUB8x8;
  1109. if( strstr( value, "b8x8" ) ) p->analyse.inter |= X264_ANALYSE_BSUB16x16;
  1110. }
  1111. OPT("8x8dct")
  1112. p->analyse.b_transform_8x8 = atobool(value);
  1113. OPT2("weightb", "weight-b")
  1114. p->analyse.b_weighted_bipred = atobool(value);
  1115. OPT("weightp")
  1116. p->analyse.i_weighted_pred = atoi(value);
  1117. OPT2("direct", "direct-pred")
  1118. b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
  1119. OPT("chroma-qp-offset")
  1120. p->analyse.i_chroma_qp_offset = atoi(value);
  1121. OPT("me")
  1122. b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
  1123. OPT2("merange", "me-range")
  1124. p->analyse.i_me_range = atoi(value);
  1125. OPT2("mvrange", "mv-range")
  1126. p->analyse.i_mv_range = atoi(value);
  1127. OPT2("mvrange-thread", "mv-range-thread")
  1128. p->analyse.i_mv_range_thread = atoi(value);
  1129. OPT2("subme", "subq")
  1130. p->analyse.i_subpel_refine = atoi(value);
  1131. OPT("psy-rd")
  1132. {
  1133. if( 2 == sscanf( value, "%f:%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
  1134. 2 == sscanf( value, "%f,%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ) ||
  1135. 2 == sscanf( value, "%f|%f", &p->analyse.f_psy_rd, &p->analyse.f_psy_trellis ))
  1136. { }
  1137. else if( sscanf( value, "%f", &p->analyse.f_psy_rd ) )
  1138. {
  1139. p->analyse.f_psy_trellis = 0;
  1140. }
  1141. else
  1142. {
  1143. p->analyse.f_psy_rd = 0;
  1144. p->analyse.f_psy_trellis = 0;
  1145. }
  1146. }
  1147. OPT("psy")
  1148. p->analyse.b_psy = atobool(value);
  1149. OPT("chroma-me")
  1150. p->analyse.b_chroma_me = atobool(value);
  1151. OPT("mixed-refs")
  1152. p->analyse.b_mixed_references = atobool(value);
  1153. OPT("trellis")
  1154. p->analyse.i_trellis = atoi(value);
  1155. OPT("fast-pskip")
  1156. p->analyse.b_fast_pskip = atobool(value);
  1157. OPT("dct-decimate")
  1158. p->analyse.b_dct_decimate = atobool(value);
  1159. OPT("deadzone-inter")
  1160. p->analyse.i_luma_deadzone[0] = atoi(value);
  1161. OPT("deadzone-intra")
  1162. p->analyse.i_luma_deadzone[1] = atoi(value);
  1163. OPT("nr")
  1164. p->analyse.i_noise_reduction = atoi(value);
  1165. OPT("bitrate")
  1166. {
  1167. p->rc.i_bitrate = atoi(value);
  1168. p->rc.i_rc_method = X264_RC_ABR;
  1169. }
  1170. OPT2("qp", "qp_constant")
  1171. {
  1172. p->rc.i_qp_constant = atoi(value);
  1173. p->rc.i_rc_method = X264_RC_CQP;
  1174. }
  1175. OPT("crf")
  1176. {
  1177. p->rc.f_rf_constant = atof(value);
  1178. p->rc.i_rc_method = X264_RC_CRF;
  1179. }
  1180. OPT("crf-max")
  1181. p->rc.f_rf_constant_max = atof(value);
  1182. OPT("rc-lookahead")
  1183. p->rc.i_lookahead = atoi(value);
  1184. OPT2("qpmin", "qp-min")
  1185. p->rc.i_qp_min = atoi(value);
  1186. OPT2("qpmax", "qp-max")
  1187. p->rc.i_qp_max = atoi(value);
  1188. OPT2("qpstep", "qp-step")
  1189. p->rc.i_qp_step = atoi(value);
  1190. OPT("ratetol")
  1191. p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
  1192. OPT("vbv-maxrate")
  1193. p->rc.i_vbv_max_bitrate = atoi(value);
  1194. OPT("vbv-bufsize")
  1195. p->rc.i_vbv_buffer_size = atoi(value);
  1196. OPT("vbv-init")
  1197. p->rc.f_vbv_buffer_init = atof(value);
  1198. OPT2("ipratio", "ip-factor")
  1199. p->rc.f_ip_factor = atof(value);
  1200. OPT2("pbratio", "pb-factor")
  1201. p->rc.f_pb_factor = atof(value);
  1202. OPT("aq-mode")
  1203. p->rc.i_aq_mode = atoi(value);
  1204. OPT("aq-strength")
  1205. p->rc.f_aq_strength = atof(value);
  1206. OPT("pass")
  1207. {
  1208. int pass = x264_clip3( atoi(value), 0, 3 );
  1209. p->rc.b_stat_write = pass & 1;
  1210. p->rc.b_stat_read = pass & 2;
  1211. }
  1212. OPT("stats")
  1213. {
  1214. p->rc.psz_stat_in = strdup(value);
  1215. p->rc.psz_stat_out = strdup(value);
  1216. }
  1217. OPT("qcomp")
  1218. p->rc.f_qcompress = atof(value);
  1219. OPT("mbtree")
  1220. p->rc.b_mb_tree = atobool(value);
  1221. OPT("qblur")
  1222. p->rc.f_qblur = atof(value);
  1223. OPT2("cplxblur", "cplx-blur")
  1224. p->rc.f_complexity_blur = atof(value);
  1225. OPT("zones")
  1226. p->rc.psz_zones = strdup(value);
  1227. OPT("crop-rect")
  1228. b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
  1229. &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
  1230. OPT("psnr")
  1231. p->analyse.b_psnr = atobool(value);
  1232. OPT("ssim")
  1233. p->analyse.b_ssim = atobool(value);
  1234. OPT("aud")
  1235. p->b_aud = atobool(value);
  1236. OPT("sps-id")
  1237. p->i_sps_id = atoi(value);
  1238. OPT("global-header")
  1239. p->b_repeat_headers = !atobool(value);
  1240. OPT("repeat-headers")
  1241. p->b_repeat_headers = atobool(value);
  1242. OPT("annexb")
  1243. p->b_annexb = atobool(value);
  1244. OPT("force-cfr")
  1245. p->b_vfr_input = !atobool(value);
  1246. OPT("nal-hrd")
  1247. b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
  1248. OPT("filler")
  1249. p->rc.b_filler = atobool(value);
  1250. OPT("pic-struct")
  1251. p->b_pic_struct = atobool(value);
  1252. OPT("fake-interlaced")
  1253. p->b_fake_interlaced = atobool(value);
  1254. OPT("frame-packing")
  1255. p->i_frame_packing = atoi(value);
  1256. OPT("stitchable")
  1257. p->b_stitchable = atobool(value);
  1258. OPT("opencl")
  1259. p->b_opencl = atobool( value );
  1260. OPT("opencl-clbin")
  1261. p->psz_clbin_file = strdup( value );
  1262. OPT("opencl-device")
  1263. p->i_opencl_device = atoi( value );
  1264. else
  1265. {
  1266. b_error = 1;
  1267. errortype = X264_PARAM_BAD_NAME;
  1268. }
  1269. #undef OPT
  1270. #undef OPT2
  1271. #undef atobool
  1272. #undef atoi
  1273. #undef atof
  1274. if( name_buf )
  1275. free( name_buf );
  1276. b_error |= value_was_null && !name_was_bool;
  1277. return b_error ? errortype : 0;
  1278. }
  1279. int x264_param_parse( x264_param_t *param, const char *name, const char *value )
  1280. {
  1281. return x264_stack_align( param_parse, param, name, value );
  1282. }
  1283. /****************************************************************************
  1284. * x264_param2string:
  1285. ****************************************************************************/
  1286. char *x264_param2string( x264_param_t *p, int b_res )
  1287. {
  1288. int len = 1000;
  1289. char *buf, *s;
  1290. if( p->rc.psz_zones )
  1291. len += strlen(p->rc.psz_zones);
  1292. buf = s = x264_malloc( len );
  1293. if( !buf )
  1294. return NULL;
  1295. if( b_res )
  1296. {
  1297. s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
  1298. s += sprintf( s, "fps=%u/%u ", p->i_fps_num, p->i_fps_den );
  1299. s += sprintf( s, "timebase=%u/%u ", p->i_timebase_num, p->i_timebase_den );
  1300. s += sprintf( s, "bitdepth=%d ", p->i_bitdepth );
  1301. }
  1302. if( p->b_opencl )
  1303. s += sprintf( s, "opencl=%d ", p->b_opencl );
  1304. s += sprintf( s, "cabac=%d", p->b_cabac );
  1305. s += sprintf( s, " ref=%d", p->i_frame_reference );
  1306. s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
  1307. p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
  1308. s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
  1309. s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
  1310. s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
  1311. s += sprintf( s, " psy=%d", p->analyse.b_psy );
  1312. if( p->analyse.b_psy )
  1313. s += sprintf( s, " psy_rd=%.2f:%.2f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis );
  1314. s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
  1315. s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
  1316. s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
  1317. s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
  1318. s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
  1319. s += sprintf( s, " cqm=%d", p->i_cqm_preset );
  1320. s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
  1321. s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
  1322. s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
  1323. s += sprintf( s, " threads=%d", p->i_threads );
  1324. s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
  1325. s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
  1326. if( p->i_slice_count )
  1327. s += sprintf( s, " slices=%d", p->i_slice_count );
  1328. if( p->i_slice_count_max )
  1329. s += sprintf( s, " slices_max=%d", p->i_slice_count_max );
  1330. if( p->i_slice_max_size )
  1331. s += sprintf( s, " slice_max_size=%d", p->i_slice_max_size );
  1332. if( p->i_slice_max_mbs )
  1333. s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
  1334. if( p->i_slice_min_mbs )
  1335. s += sprintf( s, " slice_min_mbs=%d", p->i_slice_min_mbs );
  1336. s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
  1337. s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
  1338. s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : p->b_fake_interlaced ? "fake" : "0" );
  1339. s += sprintf( s, " bluray_compat=%d", p->b_bluray_compat );
  1340. if( p->b_stitchable )
  1341. s += sprintf( s, " stitchable=%d", p->b_stitchable );
  1342. s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
  1343. s += sprintf( s, " bframes=%d", p->i_bframe );
  1344. if( p->i_bframe )
  1345. {
  1346. s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d weightb=%d open_gop=%d",
  1347. p->i_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
  1348. p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->b_open_gop );
  1349. }
  1350. s += sprintf( s, " weightp=%d", p->analyse.i_weighted_pred > 0 ? p->analyse.i_weighted_pred : 0 );
  1351. if( p->i_keyint_max == X264_KEYINT_MAX_INFINITE )
  1352. s += sprintf( s, " keyint=infinite" );
  1353. else
  1354. s += sprintf( s, " keyint=%d", p->i_keyint_max );
  1355. s += sprintf( s, " keyint_min=%d scenecut=%d intra_refresh=%d",
  1356. p->i_keyint_min, p->i_scenecut_threshold, p->b_intra_refresh );
  1357. if( p->rc.b_mb_tree || p->rc.i_vbv_buffer_size )
  1358. s += sprintf( s, " rc_lookahead=%d", p->rc.i_lookahead );
  1359. s += sprintf( s, " rc=%s mbtree=%d", p->rc.i_rc_method == X264_RC_ABR ?
  1360. ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_max_bitrate == p->rc.i_bitrate ? "cbr" : "abr" )
  1361. : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp", p->rc.b_mb_tree );
  1362. if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
  1363. {
  1364. if( p->rc.i_rc_method == X264_RC_CRF )
  1365. s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
  1366. else
  1367. s += sprintf( s, " bitrate=%d ratetol=%.1f",
  1368. p->rc.i_bitrate, p->rc.f_rate_tolerance );
  1369. s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
  1370. p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
  1371. if( p->rc.b_stat_read )
  1372. s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
  1373. p->rc.f_complexity_blur, p->rc.f_qblur );
  1374. if( p->rc.i_vbv_buffer_size )
  1375. {
  1376. s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
  1377. p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
  1378. if( p->rc.i_rc_method == X264_RC_CRF )
  1379. s += sprintf( s, " crf_max=%.1f", p->rc.f_rf_constant_max );
  1380. }
  1381. }
  1382. else if( p->rc.i_rc_method == X264_RC_CQP )
  1383. s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
  1384. if( p->rc.i_vbv_buffer_size )
  1385. s += sprintf( s, " nal_hrd=%s filler=%d", x264_nal_hrd_names[p->i_nal_hrd], p->rc.b_filler );
  1386. if( p->crop_rect.i_left | p->crop_rect.i_top | p->crop_rect.i_right | p->crop_rect.i_bottom )
  1387. s += sprintf( s, " crop_rect=%u,%u,%u,%u", p->crop_rect.i_left, p->crop_rect.i_top,
  1388. p->crop_rect.i_right, p->crop_rect.i_bottom );
  1389. if( p->i_frame_packing >= 0 )
  1390. s += sprintf( s, " frame-packing=%d", p->i_frame_packing );
  1391. if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
  1392. {
  1393. s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
  1394. if( p->i_bframe && !p->rc.b_mb_tree )
  1395. s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
  1396. s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
  1397. if( p->rc.i_aq_mode )
  1398. s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
  1399. if( p->rc.psz_zones )
  1400. s += sprintf( s, " zones=%s", p->rc.psz_zones );
  1401. else if( p->rc.i_zones )
  1402. s += sprintf( s, " zones" );
  1403. }
  1404. return buf;
  1405. }