set.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*****************************************************************************
  2. * set: header writing
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  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 "common/common.h"
  27. #include "set.h"
  28. #define bs_write_ue bs_write_ue_big
  29. // Indexed by pic_struct values
  30. static const uint8_t num_clock_ts[10] = { 0, 1, 1, 1, 2, 2, 3, 3, 2, 3 };
  31. static const uint8_t avcintra_uuid[] = {0xF7, 0x49, 0x3E, 0xB3, 0xD4, 0x00, 0x47, 0x96, 0x86, 0x86, 0xC9, 0x70, 0x7B, 0x64, 0x37, 0x2A};
  32. static void transpose( uint8_t *buf, int w )
  33. {
  34. for( int i = 0; i < w; i++ )
  35. for( int j = 0; j < i; j++ )
  36. XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
  37. }
  38. static void scaling_list_write( bs_t *s, x264_sps_t *sps, int idx )
  39. {
  40. const int len = idx<4 ? 16 : 64;
  41. const uint8_t *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0];
  42. const uint8_t *list = sps->scaling_list[idx];
  43. const uint8_t *def_list = (idx==CQM_4IC) ? sps->scaling_list[CQM_4IY]
  44. : (idx==CQM_4PC) ? sps->scaling_list[CQM_4PY]
  45. : (idx==CQM_8IC+4) ? sps->scaling_list[CQM_8IY+4]
  46. : (idx==CQM_8PC+4) ? sps->scaling_list[CQM_8PY+4]
  47. : x264_cqm_jvt[idx];
  48. if( !memcmp( list, def_list, len ) )
  49. bs_write1( s, 0 ); // scaling_list_present_flag
  50. else if( !memcmp( list, x264_cqm_jvt[idx], len ) )
  51. {
  52. bs_write1( s, 1 ); // scaling_list_present_flag
  53. bs_write_se( s, -8 ); // use jvt list
  54. }
  55. else
  56. {
  57. int run;
  58. bs_write1( s, 1 ); // scaling_list_present_flag
  59. // try run-length compression of trailing values
  60. for( run = len; run > 1; run-- )
  61. if( list[zigzag[run-1]] != list[zigzag[run-2]] )
  62. break;
  63. if( run < len && len - run < bs_size_se( (int8_t)-list[zigzag[run]] ) )
  64. run = len;
  65. for( int j = 0; j < run; j++ )
  66. bs_write_se( s, (int8_t)(list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8)) ); // delta
  67. if( run < len )
  68. bs_write_se( s, (int8_t)-list[zigzag[run]] );
  69. }
  70. }
  71. void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_type )
  72. {
  73. int i;
  74. bs_realign( s );
  75. for( i = 0; i <= payload_type-255; i += 255 )
  76. bs_write( s, 8, 255 );
  77. bs_write( s, 8, payload_type-i );
  78. for( i = 0; i <= payload_size-255; i += 255 )
  79. bs_write( s, 8, 255 );
  80. bs_write( s, 8, payload_size-i );
  81. for( i = 0; i < payload_size; i++ )
  82. bs_write( s, 8, payload[i] );
  83. bs_rbsp_trailing( s );
  84. bs_flush( s );
  85. }
  86. void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  87. {
  88. int csp = param->i_csp & X264_CSP_MASK;
  89. sps->i_id = i_id;
  90. sps->i_mb_width = ( param->i_width + 15 ) / 16;
  91. sps->i_mb_height= ( param->i_height + 15 ) / 16;
  92. sps->i_chroma_format_idc = csp >= X264_CSP_I444 ? CHROMA_444 :
  93. csp >= X264_CSP_I422 ? CHROMA_422 :
  94. csp >= X264_CSP_I420 ? CHROMA_420 : CHROMA_400;
  95. sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
  96. if( sps->b_qpprime_y_zero_transform_bypass || sps->i_chroma_format_idc == CHROMA_444 )
  97. sps->i_profile_idc = PROFILE_HIGH444_PREDICTIVE;
  98. else if( sps->i_chroma_format_idc == CHROMA_422 )
  99. sps->i_profile_idc = PROFILE_HIGH422;
  100. else if( BIT_DEPTH > 8 )
  101. sps->i_profile_idc = PROFILE_HIGH10;
  102. else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT || sps->i_chroma_format_idc == CHROMA_400 )
  103. sps->i_profile_idc = PROFILE_HIGH;
  104. else if( param->b_cabac || param->i_bframe > 0 || param->b_interlaced || param->b_fake_interlaced || param->analyse.i_weighted_pred > 0 )
  105. sps->i_profile_idc = PROFILE_MAIN;
  106. else
  107. sps->i_profile_idc = PROFILE_BASELINE;
  108. sps->b_constraint_set0 = sps->i_profile_idc == PROFILE_BASELINE;
  109. /* x264 doesn't support the features that are in Baseline and not in Main,
  110. * namely arbitrary_slice_order and slice_groups. */
  111. sps->b_constraint_set1 = sps->i_profile_idc <= PROFILE_MAIN;
  112. /* Never set constraint_set2, it is not necessary and not used in real world. */
  113. sps->b_constraint_set2 = 0;
  114. sps->b_constraint_set3 = 0;
  115. sps->i_level_idc = param->i_level_idc;
  116. if( param->i_level_idc == 9 && ( sps->i_profile_idc == PROFILE_BASELINE || sps->i_profile_idc == PROFILE_MAIN ) )
  117. {
  118. sps->b_constraint_set3 = 1; /* level 1b with Baseline or Main profile is signalled via constraint_set3 */
  119. sps->i_level_idc = 11;
  120. }
  121. /* Intra profiles */
  122. if( param->i_keyint_max == 1 && sps->i_profile_idc >= PROFILE_HIGH )
  123. sps->b_constraint_set3 = 1;
  124. sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
  125. /* extra slot with pyramid so that we don't have to override the
  126. * order of forgetting old pictures */
  127. sps->vui.i_max_dec_frame_buffering =
  128. sps->i_num_ref_frames = X264_MIN(X264_REF_MAX, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
  129. param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
  130. sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
  131. if( param->i_keyint_max == 1 )
  132. {
  133. sps->i_num_ref_frames = 0;
  134. sps->vui.i_max_dec_frame_buffering = 0;
  135. }
  136. /* number of refs + current frame */
  137. int max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
  138. /* Intra refresh cannot write a recovery time greater than max frame num-1 */
  139. if( param->b_intra_refresh )
  140. {
  141. int time_to_recovery = X264_MIN( sps->i_mb_width - 1, param->i_keyint_max ) + param->i_bframe - 1;
  142. max_frame_num = X264_MAX( max_frame_num, time_to_recovery+1 );
  143. }
  144. sps->i_log2_max_frame_num = 4;
  145. while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
  146. sps->i_log2_max_frame_num++;
  147. sps->i_poc_type = param->i_bframe || param->b_interlaced || param->i_avcintra_class ? 0 : 2;
  148. if( sps->i_poc_type == 0 )
  149. {
  150. int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
  151. sps->i_log2_max_poc_lsb = 4;
  152. while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
  153. sps->i_log2_max_poc_lsb++;
  154. }
  155. sps->b_vui = 1;
  156. sps->b_gaps_in_frame_num_value_allowed = 0;
  157. sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced);
  158. if( !sps->b_frame_mbs_only )
  159. sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
  160. sps->b_mb_adaptive_frame_field = param->b_interlaced;
  161. sps->b_direct8x8_inference = 1;
  162. x264_sps_init_reconfigurable( sps, param );
  163. sps->vui.b_overscan_info_present = param->vui.i_overscan > 0 && param->vui.i_overscan <= 2;
  164. if( sps->vui.b_overscan_info_present )
  165. sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
  166. sps->vui.b_signal_type_present = 0;
  167. sps->vui.i_vidformat = ( param->vui.i_vidformat >= 0 && param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
  168. sps->vui.b_fullrange = ( param->vui.b_fullrange >= 0 && param->vui.b_fullrange <= 1 ? param->vui.b_fullrange :
  169. ( csp >= X264_CSP_BGR ? 1 : 0 ) );
  170. sps->vui.b_color_description_present = 0;
  171. sps->vui.i_colorprim = ( param->vui.i_colorprim >= 0 && param->vui.i_colorprim <= 12 ? param->vui.i_colorprim : 2 );
  172. sps->vui.i_transfer = ( param->vui.i_transfer >= 0 && param->vui.i_transfer <= 18 ? param->vui.i_transfer : 2 );
  173. sps->vui.i_colmatrix = ( param->vui.i_colmatrix >= 0 && param->vui.i_colmatrix <= 14 ? param->vui.i_colmatrix :
  174. ( csp >= X264_CSP_BGR ? 0 : 2 ) );
  175. if( sps->vui.i_colorprim != 2 || sps->vui.i_transfer != 2 || sps->vui.i_colmatrix != 2 )
  176. sps->vui.b_color_description_present = 1;
  177. if( sps->vui.i_vidformat != 5 || sps->vui.b_fullrange || sps->vui.b_color_description_present )
  178. sps->vui.b_signal_type_present = 1;
  179. /* FIXME: not sufficient for interlaced video */
  180. sps->vui.b_chroma_loc_info_present = param->vui.i_chroma_loc > 0 && param->vui.i_chroma_loc <= 5 &&
  181. sps->i_chroma_format_idc == CHROMA_420;
  182. if( sps->vui.b_chroma_loc_info_present )
  183. {
  184. sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
  185. sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
  186. }
  187. sps->vui.b_timing_info_present = param->i_timebase_num > 0 && param->i_timebase_den > 0;
  188. if( sps->vui.b_timing_info_present )
  189. {
  190. sps->vui.i_num_units_in_tick = param->i_timebase_num;
  191. sps->vui.i_time_scale = param->i_timebase_den * 2;
  192. sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
  193. }
  194. sps->vui.b_vcl_hrd_parameters_present = 0; // we don't support VCL HRD
  195. sps->vui.b_nal_hrd_parameters_present = !!param->i_nal_hrd;
  196. sps->vui.b_pic_struct_present = param->b_pic_struct;
  197. // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
  198. sps->vui.b_bitstream_restriction = !(sps->b_constraint_set3 && sps->i_profile_idc >= PROFILE_HIGH);
  199. if( sps->vui.b_bitstream_restriction )
  200. {
  201. sps->vui.b_motion_vectors_over_pic_boundaries = 1;
  202. sps->vui.i_max_bytes_per_pic_denom = 0;
  203. sps->vui.i_max_bits_per_mb_denom = 0;
  204. sps->vui.i_log2_max_mv_length_horizontal =
  205. sps->vui.i_log2_max_mv_length_vertical = (int)log2f( X264_MAX( 1, param->analyse.i_mv_range*4-1 ) ) + 1;
  206. }
  207. sps->b_avcintra = !!param->i_avcintra_class;
  208. sps->i_cqm_preset = param->i_cqm_preset;
  209. }
  210. void x264_sps_init_reconfigurable( x264_sps_t *sps, x264_param_t *param )
  211. {
  212. sps->crop.i_left = param->crop_rect.i_left;
  213. sps->crop.i_top = param->crop_rect.i_top;
  214. sps->crop.i_right = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
  215. sps->crop.i_bottom = param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height;
  216. sps->b_crop = sps->crop.i_left || sps->crop.i_top ||
  217. sps->crop.i_right || sps->crop.i_bottom;
  218. sps->vui.b_aspect_ratio_info_present = 0;
  219. if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
  220. {
  221. sps->vui.b_aspect_ratio_info_present = 1;
  222. sps->vui.i_sar_width = param->vui.i_sar_width;
  223. sps->vui.i_sar_height= param->vui.i_sar_height;
  224. }
  225. }
  226. void x264_sps_init_scaling_list( x264_sps_t *sps, x264_param_t *param )
  227. {
  228. switch( sps->i_cqm_preset )
  229. {
  230. case X264_CQM_FLAT:
  231. for( int i = 0; i < 8; i++ )
  232. sps->scaling_list[i] = x264_cqm_flat16;
  233. break;
  234. case X264_CQM_JVT:
  235. for( int i = 0; i < 8; i++ )
  236. sps->scaling_list[i] = x264_cqm_jvt[i];
  237. break;
  238. case X264_CQM_CUSTOM:
  239. /* match the transposed DCT & zigzag */
  240. transpose( param->cqm_4iy, 4 );
  241. transpose( param->cqm_4py, 4 );
  242. transpose( param->cqm_4ic, 4 );
  243. transpose( param->cqm_4pc, 4 );
  244. transpose( param->cqm_8iy, 8 );
  245. transpose( param->cqm_8py, 8 );
  246. transpose( param->cqm_8ic, 8 );
  247. transpose( param->cqm_8pc, 8 );
  248. sps->scaling_list[CQM_4IY] = param->cqm_4iy;
  249. sps->scaling_list[CQM_4PY] = param->cqm_4py;
  250. sps->scaling_list[CQM_4IC] = param->cqm_4ic;
  251. sps->scaling_list[CQM_4PC] = param->cqm_4pc;
  252. sps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
  253. sps->scaling_list[CQM_8PY+4] = param->cqm_8py;
  254. sps->scaling_list[CQM_8IC+4] = param->cqm_8ic;
  255. sps->scaling_list[CQM_8PC+4] = param->cqm_8pc;
  256. for( int i = 0; i < 8; i++ )
  257. for( int j = 0; j < (i < 4 ? 16 : 64); j++ )
  258. if( sps->scaling_list[i][j] == 0 )
  259. sps->scaling_list[i] = x264_cqm_jvt[i];
  260. break;
  261. }
  262. }
  263. void x264_sps_write( bs_t *s, x264_sps_t *sps )
  264. {
  265. bs_realign( s );
  266. bs_write( s, 8, sps->i_profile_idc );
  267. bs_write1( s, sps->b_constraint_set0 );
  268. bs_write1( s, sps->b_constraint_set1 );
  269. bs_write1( s, sps->b_constraint_set2 );
  270. bs_write1( s, sps->b_constraint_set3 );
  271. bs_write( s, 4, 0 ); /* reserved */
  272. bs_write( s, 8, sps->i_level_idc );
  273. bs_write_ue( s, sps->i_id );
  274. if( sps->i_profile_idc >= PROFILE_HIGH )
  275. {
  276. bs_write_ue( s, sps->i_chroma_format_idc );
  277. if( sps->i_chroma_format_idc == CHROMA_444 )
  278. bs_write1( s, 0 ); // separate_colour_plane_flag
  279. bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_luma_minus8
  280. bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_chroma_minus8
  281. bs_write1( s, sps->b_qpprime_y_zero_transform_bypass );
  282. /* Exactly match the AVC-Intra bitstream */
  283. bs_write1( s, sps->b_avcintra ); // seq_scaling_matrix_present_flag
  284. if( sps->b_avcintra )
  285. {
  286. scaling_list_write( s, sps, CQM_4IY );
  287. scaling_list_write( s, sps, CQM_4IC );
  288. scaling_list_write( s, sps, CQM_4IC );
  289. bs_write1( s, 0 ); // no inter
  290. bs_write1( s, 0 ); // no inter
  291. bs_write1( s, 0 ); // no inter
  292. scaling_list_write( s, sps, CQM_8IY+4 );
  293. bs_write1( s, 0 ); // no inter
  294. if( sps->i_chroma_format_idc == CHROMA_444 )
  295. {
  296. scaling_list_write( s, sps, CQM_8IC+4 );
  297. bs_write1( s, 0 ); // no inter
  298. scaling_list_write( s, sps, CQM_8IC+4 );
  299. bs_write1( s, 0 ); // no inter
  300. }
  301. }
  302. }
  303. bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
  304. bs_write_ue( s, sps->i_poc_type );
  305. if( sps->i_poc_type == 0 )
  306. bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
  307. bs_write_ue( s, sps->i_num_ref_frames );
  308. bs_write1( s, sps->b_gaps_in_frame_num_value_allowed );
  309. bs_write_ue( s, sps->i_mb_width - 1 );
  310. bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
  311. bs_write1( s, sps->b_frame_mbs_only );
  312. if( !sps->b_frame_mbs_only )
  313. bs_write1( s, sps->b_mb_adaptive_frame_field );
  314. bs_write1( s, sps->b_direct8x8_inference );
  315. bs_write1( s, sps->b_crop );
  316. if( sps->b_crop )
  317. {
  318. int h_shift = sps->i_chroma_format_idc == CHROMA_420 || sps->i_chroma_format_idc == CHROMA_422;
  319. int v_shift = (sps->i_chroma_format_idc == CHROMA_420) + !sps->b_frame_mbs_only;
  320. bs_write_ue( s, sps->crop.i_left >> h_shift );
  321. bs_write_ue( s, sps->crop.i_right >> h_shift );
  322. bs_write_ue( s, sps->crop.i_top >> v_shift );
  323. bs_write_ue( s, sps->crop.i_bottom >> v_shift );
  324. }
  325. bs_write1( s, sps->b_vui );
  326. if( sps->b_vui )
  327. {
  328. bs_write1( s, sps->vui.b_aspect_ratio_info_present );
  329. if( sps->vui.b_aspect_ratio_info_present )
  330. {
  331. int i;
  332. static const struct { uint8_t w, h, sar; } sar[] =
  333. {
  334. // aspect_ratio_idc = 0 -> unspecified
  335. { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
  336. { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
  337. { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
  338. {160, 99, 13}, { 4, 3, 14}, { 3, 2, 15}, { 2, 1, 16},
  339. // aspect_ratio_idc = [17..254] -> reserved
  340. { 0, 0, 255 }
  341. };
  342. for( i = 0; sar[i].sar != 255; i++ )
  343. {
  344. if( sar[i].w == sps->vui.i_sar_width &&
  345. sar[i].h == sps->vui.i_sar_height )
  346. break;
  347. }
  348. bs_write( s, 8, sar[i].sar );
  349. if( sar[i].sar == 255 ) /* aspect_ratio_idc (extended) */
  350. {
  351. bs_write( s, 16, sps->vui.i_sar_width );
  352. bs_write( s, 16, sps->vui.i_sar_height );
  353. }
  354. }
  355. bs_write1( s, sps->vui.b_overscan_info_present );
  356. if( sps->vui.b_overscan_info_present )
  357. bs_write1( s, sps->vui.b_overscan_info );
  358. bs_write1( s, sps->vui.b_signal_type_present );
  359. if( sps->vui.b_signal_type_present )
  360. {
  361. bs_write( s, 3, sps->vui.i_vidformat );
  362. bs_write1( s, sps->vui.b_fullrange );
  363. bs_write1( s, sps->vui.b_color_description_present );
  364. if( sps->vui.b_color_description_present )
  365. {
  366. bs_write( s, 8, sps->vui.i_colorprim );
  367. bs_write( s, 8, sps->vui.i_transfer );
  368. bs_write( s, 8, sps->vui.i_colmatrix );
  369. }
  370. }
  371. bs_write1( s, sps->vui.b_chroma_loc_info_present );
  372. if( sps->vui.b_chroma_loc_info_present )
  373. {
  374. bs_write_ue( s, sps->vui.i_chroma_loc_top );
  375. bs_write_ue( s, sps->vui.i_chroma_loc_bottom );
  376. }
  377. bs_write1( s, sps->vui.b_timing_info_present );
  378. if( sps->vui.b_timing_info_present )
  379. {
  380. bs_write32( s, sps->vui.i_num_units_in_tick );
  381. bs_write32( s, sps->vui.i_time_scale );
  382. bs_write1( s, sps->vui.b_fixed_frame_rate );
  383. }
  384. bs_write1( s, sps->vui.b_nal_hrd_parameters_present );
  385. if( sps->vui.b_nal_hrd_parameters_present )
  386. {
  387. bs_write_ue( s, sps->vui.hrd.i_cpb_cnt - 1 );
  388. bs_write( s, 4, sps->vui.hrd.i_bit_rate_scale );
  389. bs_write( s, 4, sps->vui.hrd.i_cpb_size_scale );
  390. bs_write_ue( s, sps->vui.hrd.i_bit_rate_value - 1 );
  391. bs_write_ue( s, sps->vui.hrd.i_cpb_size_value - 1 );
  392. bs_write1( s, sps->vui.hrd.b_cbr_hrd );
  393. bs_write( s, 5, sps->vui.hrd.i_initial_cpb_removal_delay_length - 1 );
  394. bs_write( s, 5, sps->vui.hrd.i_cpb_removal_delay_length - 1 );
  395. bs_write( s, 5, sps->vui.hrd.i_dpb_output_delay_length - 1 );
  396. bs_write( s, 5, sps->vui.hrd.i_time_offset_length );
  397. }
  398. bs_write1( s, sps->vui.b_vcl_hrd_parameters_present );
  399. if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
  400. bs_write1( s, 0 ); /* low_delay_hrd_flag */
  401. bs_write1( s, sps->vui.b_pic_struct_present );
  402. bs_write1( s, sps->vui.b_bitstream_restriction );
  403. if( sps->vui.b_bitstream_restriction )
  404. {
  405. bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
  406. bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
  407. bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
  408. bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
  409. bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
  410. bs_write_ue( s, sps->vui.i_num_reorder_frames );
  411. bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
  412. }
  413. }
  414. bs_rbsp_trailing( s );
  415. bs_flush( s );
  416. }
  417. void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
  418. {
  419. pps->i_id = i_id;
  420. pps->i_sps_id = sps->i_id;
  421. pps->b_cabac = param->b_cabac;
  422. pps->b_pic_order = !param->i_avcintra_class && param->b_interlaced;
  423. pps->i_num_slice_groups = 1;
  424. pps->i_num_ref_idx_l0_default_active = param->i_frame_reference;
  425. pps->i_num_ref_idx_l1_default_active = 1;
  426. pps->b_weighted_pred = param->analyse.i_weighted_pred > 0;
  427. pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
  428. pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR || param->b_stitchable ? 26 + QP_BD_OFFSET : SPEC_QP( param->rc.i_qp_constant );
  429. pps->i_pic_init_qs = 26 + QP_BD_OFFSET;
  430. pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
  431. pps->b_deblocking_filter_control = 1;
  432. pps->b_constrained_intra_pred = param->b_constrained_intra;
  433. pps->b_redundant_pic_cnt = 0;
  434. pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
  435. }
  436. void x264_pps_write( bs_t *s, x264_sps_t *sps, x264_pps_t *pps )
  437. {
  438. bs_realign( s );
  439. bs_write_ue( s, pps->i_id );
  440. bs_write_ue( s, pps->i_sps_id );
  441. bs_write1( s, pps->b_cabac );
  442. bs_write1( s, pps->b_pic_order );
  443. bs_write_ue( s, pps->i_num_slice_groups - 1 );
  444. bs_write_ue( s, pps->i_num_ref_idx_l0_default_active - 1 );
  445. bs_write_ue( s, pps->i_num_ref_idx_l1_default_active - 1 );
  446. bs_write1( s, pps->b_weighted_pred );
  447. bs_write( s, 2, pps->b_weighted_bipred );
  448. bs_write_se( s, pps->i_pic_init_qp - 26 - QP_BD_OFFSET );
  449. bs_write_se( s, pps->i_pic_init_qs - 26 - QP_BD_OFFSET );
  450. bs_write_se( s, pps->i_chroma_qp_index_offset );
  451. bs_write1( s, pps->b_deblocking_filter_control );
  452. bs_write1( s, pps->b_constrained_intra_pred );
  453. bs_write1( s, pps->b_redundant_pic_cnt );
  454. int b_scaling_list = !sps->b_avcintra && sps->i_cqm_preset != X264_CQM_FLAT;
  455. if( pps->b_transform_8x8_mode || b_scaling_list )
  456. {
  457. bs_write1( s, pps->b_transform_8x8_mode );
  458. bs_write1( s, b_scaling_list );
  459. if( b_scaling_list )
  460. {
  461. scaling_list_write( s, sps, CQM_4IY );
  462. scaling_list_write( s, sps, CQM_4IC );
  463. bs_write1( s, 0 ); // Cr = Cb
  464. scaling_list_write( s, sps, CQM_4PY );
  465. scaling_list_write( s, sps, CQM_4PC );
  466. bs_write1( s, 0 ); // Cr = Cb
  467. if( pps->b_transform_8x8_mode )
  468. {
  469. scaling_list_write( s, sps, CQM_8IY+4 );
  470. scaling_list_write( s, sps, CQM_8PY+4 );
  471. if( sps->i_chroma_format_idc == CHROMA_444 )
  472. {
  473. scaling_list_write( s, sps, CQM_8IC+4 );
  474. scaling_list_write( s, sps, CQM_8PC+4 );
  475. bs_write1( s, 0 ); // Cr = Cb
  476. bs_write1( s, 0 ); // Cr = Cb
  477. }
  478. }
  479. }
  480. bs_write_se( s, pps->i_chroma_qp_index_offset );
  481. }
  482. bs_rbsp_trailing( s );
  483. bs_flush( s );
  484. }
  485. void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
  486. {
  487. bs_t q;
  488. ALIGNED_4( uint8_t tmp_buf[100] );
  489. M32( tmp_buf ) = 0; // shut up gcc
  490. bs_init( &q, tmp_buf, 100 );
  491. bs_realign( &q );
  492. bs_write_ue( &q, recovery_frame_cnt ); // recovery_frame_cnt
  493. bs_write1( &q, 1 ); //exact_match_flag 1
  494. bs_write1( &q, 0 ); //broken_link_flag 0
  495. bs_write( &q, 2, 0 ); //changing_slice_group 0
  496. bs_align_10( &q );
  497. x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_RECOVERY_POINT );
  498. }
  499. int x264_sei_version_write( x264_t *h, bs_t *s )
  500. {
  501. // random ID number generated according to ISO-11578
  502. static const uint8_t uuid[16] =
  503. {
  504. 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
  505. 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
  506. };
  507. char *opts = x264_param2string( &h->param, 0 );
  508. char *payload;
  509. int length;
  510. if( !opts )
  511. return -1;
  512. CHECKED_MALLOC( payload, 200 + strlen( opts ) );
  513. memcpy( payload, uuid, 16 );
  514. sprintf( payload+16, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
  515. "Copy%s 2003-2018 - http://www.videolan.org/x264.html - options: %s",
  516. X264_BUILD, X264_VERSION, HAVE_GPL?"left":"right", opts );
  517. length = strlen(payload)+1;
  518. x264_sei_write( s, (uint8_t *)payload, length, SEI_USER_DATA_UNREGISTERED );
  519. x264_free( opts );
  520. x264_free( payload );
  521. return 0;
  522. fail:
  523. x264_free( opts );
  524. return -1;
  525. }
  526. void x264_sei_buffering_period_write( x264_t *h, bs_t *s )
  527. {
  528. x264_sps_t *sps = h->sps;
  529. bs_t q;
  530. ALIGNED_4( uint8_t tmp_buf[100] );
  531. M32( tmp_buf ) = 0; // shut up gcc
  532. bs_init( &q, tmp_buf, 100 );
  533. bs_realign( &q );
  534. bs_write_ue( &q, sps->i_id );
  535. if( sps->vui.b_nal_hrd_parameters_present )
  536. {
  537. bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
  538. bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
  539. }
  540. bs_align_10( &q );
  541. x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_BUFFERING_PERIOD );
  542. }
  543. void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
  544. {
  545. x264_sps_t *sps = h->sps;
  546. bs_t q;
  547. ALIGNED_4( uint8_t tmp_buf[100] );
  548. M32( tmp_buf ) = 0; // shut up gcc
  549. bs_init( &q, tmp_buf, 100 );
  550. bs_realign( &q );
  551. if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
  552. {
  553. bs_write( &q, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay - h->i_cpb_delay_pir_offset );
  554. bs_write( &q, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_delay );
  555. }
  556. if( sps->vui.b_pic_struct_present )
  557. {
  558. bs_write( &q, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
  559. // These clock timestamps are not standardised so we don't set them
  560. // They could be time of origin, capture or alternative ideal display
  561. for( int i = 0; i < num_clock_ts[h->fenc->i_pic_struct]; i++ )
  562. bs_write1( &q, 0 ); // clock_timestamp_flag
  563. }
  564. bs_align_10( &q );
  565. x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_PIC_TIMING );
  566. }
  567. void x264_sei_frame_packing_write( x264_t *h, bs_t *s )
  568. {
  569. int quincunx_sampling_flag = h->param.i_frame_packing == 0;
  570. bs_t q;
  571. ALIGNED_4( uint8_t tmp_buf[100] );
  572. M32( tmp_buf ) = 0; // shut up gcc
  573. bs_init( &q, tmp_buf, 100 );
  574. bs_realign( &q );
  575. bs_write_ue( &q, 0 ); // frame_packing_arrangement_id
  576. bs_write1( &q, 0 ); // frame_packing_arrangement_cancel_flag
  577. bs_write ( &q, 7, h->param.i_frame_packing ); // frame_packing_arrangement_type
  578. bs_write1( &q, quincunx_sampling_flag ); // quincunx_sampling_flag
  579. // 0: views are unrelated, 1: left view is on the left, 2: left view is on the right
  580. bs_write ( &q, 6, h->param.i_frame_packing != 6 ); // content_interpretation_type
  581. bs_write1( &q, 0 ); // spatial_flipping_flag
  582. bs_write1( &q, 0 ); // frame0_flipped_flag
  583. bs_write1( &q, 0 ); // field_views_flag
  584. bs_write1( &q, h->param.i_frame_packing == 5 && !(h->fenc->i_frame&1) ); // current_frame_is_frame0_flag
  585. bs_write1( &q, 0 ); // frame0_self_contained_flag
  586. bs_write1( &q, 0 ); // frame1_self_contained_flag
  587. if( quincunx_sampling_flag == 0 && h->param.i_frame_packing != 5 )
  588. {
  589. bs_write( &q, 4, 0 ); // frame0_grid_position_x
  590. bs_write( &q, 4, 0 ); // frame0_grid_position_y
  591. bs_write( &q, 4, 0 ); // frame1_grid_position_x
  592. bs_write( &q, 4, 0 ); // frame1_grid_position_y
  593. }
  594. bs_write( &q, 8, 0 ); // frame_packing_arrangement_reserved_byte
  595. // "frame_packing_arrangement_repetition_period equal to 1 specifies that the frame packing arrangement SEI message persists in output"
  596. // for (i_frame_packing == 5) this will undermine current_frame_is_frame0_flag which must alternate every view sequence
  597. bs_write_ue( &q, h->param.i_frame_packing != 5 ); // frame_packing_arrangement_repetition_period
  598. bs_write1( &q, 0 ); // frame_packing_arrangement_extension_flag
  599. bs_align_10( &q );
  600. x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_FRAME_PACKING );
  601. }
  602. void x264_sei_alternative_transfer_write( x264_t *h, bs_t *s )
  603. {
  604. bs_t q;
  605. ALIGNED_4( uint8_t tmp_buf[100] );
  606. M32( tmp_buf ) = 0; // shut up gcc
  607. bs_init( &q, tmp_buf, 100 );
  608. bs_realign( &q );
  609. bs_write ( &q, 8, h->param.i_alternative_transfer ); // preferred_transfer_characteristics
  610. bs_align_10( &q );
  611. x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_ALTERNATIVE_TRANSFER );
  612. }
  613. void x264_filler_write( x264_t *h, bs_t *s, int filler )
  614. {
  615. bs_realign( s );
  616. for( int i = 0; i < filler; i++ )
  617. bs_write( s, 8, 0xff );
  618. bs_rbsp_trailing( s );
  619. bs_flush( s );
  620. }
  621. void x264_sei_dec_ref_pic_marking_write( x264_t *h, bs_t *s )
  622. {
  623. x264_slice_header_t *sh = &h->sh_backup;
  624. bs_t q;
  625. ALIGNED_4( uint8_t tmp_buf[100] );
  626. M32( tmp_buf ) = 0; // shut up gcc
  627. bs_init( &q, tmp_buf, 100 );
  628. bs_realign( &q );
  629. /* We currently only use this for repeating B-refs, as required by Blu-ray. */
  630. bs_write1( &q, 0 ); //original_idr_flag
  631. bs_write_ue( &q, sh->i_frame_num ); //original_frame_num
  632. if( !h->sps->b_frame_mbs_only )
  633. bs_write1( &q, 0 ); //original_field_pic_flag
  634. bs_write1( &q, sh->i_mmco_command_count > 0 );
  635. if( sh->i_mmco_command_count > 0 )
  636. {
  637. for( int i = 0; i < sh->i_mmco_command_count; i++ )
  638. {
  639. bs_write_ue( &q, 1 );
  640. bs_write_ue( &q, sh->mmco[i].i_difference_of_pic_nums - 1 );
  641. }
  642. bs_write_ue( &q, 0 );
  643. }
  644. bs_align_10( &q );
  645. x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_DEC_REF_PIC_MARKING );
  646. }
  647. int x264_sei_avcintra_umid_write( x264_t *h, bs_t *s )
  648. {
  649. uint8_t data[512];
  650. const char *msg = "UMID";
  651. const int len = 497;
  652. memset( data, 0xff, len );
  653. memcpy( data, avcintra_uuid, sizeof(avcintra_uuid) );
  654. memcpy( data+16, msg, strlen(msg) );
  655. data[20] = 0x13;
  656. /* These bytes appear to be some sort of frame/seconds counter in certain applications,
  657. * but others jump around, so leave them as zero for now */
  658. data[22] = data[23] = data[25] = data[26] = 0;
  659. data[28] = 0x14;
  660. data[30] = data[31] = data[33] = data[34] = 0;
  661. data[36] = 0x60;
  662. data[41] = 0x22; /* Believed to be some sort of end of basic UMID identifier */
  663. data[60] = 0x62;
  664. data[62] = data[63] = data[65] = data[66] = 0;
  665. data[68] = 0x63;
  666. data[70] = data[71] = data[73] = data[74] = 0;
  667. x264_sei_write( &h->out.bs, data, len, SEI_USER_DATA_UNREGISTERED );
  668. return 0;
  669. }
  670. int x264_sei_avcintra_vanc_write( x264_t *h, bs_t *s, int len )
  671. {
  672. uint8_t data[6000];
  673. const char *msg = "VANC";
  674. if( len > sizeof(data) )
  675. {
  676. x264_log( h, X264_LOG_ERROR, "AVC-Intra SEI is too large (%d)\n", len );
  677. return -1;
  678. }
  679. memset( data, 0xff, len );
  680. memcpy( data, avcintra_uuid, sizeof(avcintra_uuid) );
  681. memcpy( data+16, msg, strlen(msg) );
  682. x264_sei_write( &h->out.bs, data, len, SEI_USER_DATA_UNREGISTERED );
  683. return 0;
  684. }
  685. #define ERROR(...)\
  686. {\
  687. if( verbose )\
  688. x264_log( h, X264_LOG_WARNING, __VA_ARGS__ );\
  689. ret = 1;\
  690. }
  691. int x264_validate_levels( x264_t *h, int verbose )
  692. {
  693. int ret = 0;
  694. int mbs = h->sps->i_mb_width * h->sps->i_mb_height;
  695. int dpb = mbs * h->sps->vui.i_max_dec_frame_buffering;
  696. int cbp_factor = h->sps->i_profile_idc>=PROFILE_HIGH422 ? 16 :
  697. h->sps->i_profile_idc==PROFILE_HIGH10 ? 12 :
  698. h->sps->i_profile_idc==PROFILE_HIGH ? 5 : 4;
  699. const x264_level_t *l = x264_levels;
  700. while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
  701. l++;
  702. if( l->frame_size < mbs
  703. || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width
  704. || l->frame_size*8 < h->sps->i_mb_height * h->sps->i_mb_height )
  705. ERROR( "frame MB size (%dx%d) > level limit (%d)\n",
  706. h->sps->i_mb_width, h->sps->i_mb_height, l->frame_size );
  707. if( dpb > l->dpb )
  708. ERROR( "DPB size (%d frames, %d mbs) > level limit (%d frames, %d mbs)\n",
  709. h->sps->vui.i_max_dec_frame_buffering, dpb, l->dpb / mbs, l->dpb );
  710. #define CHECK( name, limit, val ) \
  711. if( (val) > (limit) ) \
  712. ERROR( name " (%"PRId64") > level limit (%d)\n", (int64_t)(val), (limit) );
  713. CHECK( "VBV bitrate", (l->bitrate * cbp_factor) / 4, h->param.rc.i_vbv_max_bitrate );
  714. CHECK( "VBV buffer", (l->cpb * cbp_factor) / 4, h->param.rc.i_vbv_buffer_size );
  715. CHECK( "MV range", l->mv_range, h->param.analyse.i_mv_range );
  716. CHECK( "interlaced", !l->frame_only, h->param.b_interlaced );
  717. CHECK( "fake interlaced", !l->frame_only, h->param.b_fake_interlaced );
  718. if( h->param.i_fps_den > 0 )
  719. CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
  720. /* TODO check the rest of the limits */
  721. return ret;
  722. }