Advertisement
Guest User

kierank

a guest
Feb 18th, 2010
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 65.77 KB | None | 0 0
  1. diff --git a/common/common.c b/common/common.c
  2. index 0dd7af5..47b3ae0 100644
  3. --- a/common/common.c
  4. +++ b/common/common.c
  5. @@ -158,6 +158,10 @@ void x264_param_default( x264_param_t *param )
  6. param->b_aud = 0;
  7. param->b_vfr_input = 1;
  8. param->b_dts_compress = 0;
  9. + param->i_nal_hrd = X264_NAL_HRD_NONE;
  10. + param->b_tff = 1;
  11. + param->b_pic_struct = 0;
  12. + param->b_pulldown = 0;
  13. }
  14.  
  15. static int parse_enum( const char *arg, const char * const *names, int *dst )
  16. @@ -403,6 +407,13 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
  17. p->i_cabac_init_idc = atoi(value);
  18. OPT("interlaced")
  19. p->b_interlaced = atobool(value);
  20. + OPT("tff")
  21. + p->b_interlaced = p->b_tff = atobool(value);
  22. + OPT("bff")
  23. + {
  24. + p->b_interlaced = atobool(value);
  25. + p->b_tff = !p->b_interlaced;
  26. + }
  27. OPT("constrained-intra")
  28. p->b_constrained_intra = atobool(value);
  29. OPT("cqm")
  30. @@ -622,6 +633,10 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
  31. p->b_annexb = atobool(value);
  32. OPT("force-cfr")
  33. p->b_vfr_input = !atobool(value);
  34. + OPT("nal-hrd")
  35. + b_error |= parse_enum( value, x264_nal_hrd_names, &p->i_nal_hrd );
  36. + OPT("pic-struct")
  37. + p->b_pic_struct = atobool(value);
  38. else
  39. return X264_PARAM_BAD_NAME;
  40. #undef OPT
  41. @@ -697,6 +712,7 @@ int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_heigh
  42. pic->img.i_stride[1] = i_width / 2;
  43. pic->img.i_stride[2] = i_width / 2;
  44. pic->param = NULL;
  45. + pic->i_pic_struct = PIC_STRUCT_AUTO;
  46. return 0;
  47. }
  48.  
  49. @@ -907,7 +923,8 @@ char *x264_param2string( x264_param_t *p, int b_res )
  50. s += sprintf( s, " slice_max_mbs=%d", p->i_slice_max_mbs );
  51. s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
  52. s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
  53. - s += sprintf( s, " mbaff=%d", p->b_interlaced );
  54. + s += sprintf( s, " interlaced=%s", p->b_interlaced ? p->b_tff ? "tff" : "bff" : "0" );
  55. +
  56. s += sprintf( s, " constrained_intra=%d", p->b_constrained_intra );
  57.  
  58. s += sprintf( s, " bframes=%d", p->i_bframe );
  59. @@ -960,6 +977,9 @@ char *x264_param2string( x264_param_t *p, int b_res )
  60. s += sprintf( s, " zones" );
  61. }
  62.  
  63. + s += sprintf( s, " pulldown=%d", p->b_pulldown );
  64. + if( p->rc.i_vbv_buffer_size )
  65. + s += sprintf( s, " nal_hrd=%s", x264_nal_hrd_names[p->i_nal_hrd] );
  66. return buf;
  67. }
  68.  
  69. diff --git a/common/common.h b/common/common.h
  70. index e2e8fac..a94c69e 100644
  71. --- a/common/common.h
  72. +++ b/common/common.h
  73. @@ -67,6 +67,9 @@ do {\
  74.  
  75. #define X264_WEIGHTP_FAKE (-1)
  76.  
  77. +#define NALU_OVERHEAD 5 // startcode + NAL type costs 5 bytes per frame
  78. +#define FILLER_OVERHEAD (NALU_OVERHEAD+1)
  79. +
  80. /****************************************************************************
  81. * Includes
  82. ****************************************************************************/
  83. @@ -214,6 +217,17 @@ enum slice_type_e
  84.  
  85. static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
  86.  
  87. +enum sei_payload_type_e
  88. +{
  89. + SEI_BUFFERING_PERIOD = 0,
  90. + SEI_PIC_TIMING = 1,
  91. + SEI_PAN_SCAN_RECT = 2,
  92. + SEI_FILLER = 3,
  93. + SEI_USER_DATA_REGISTERED = 4,
  94. + SEI_USER_DATA_UNREGISTERED = 5,
  95. + SEI_RECOVERY_POINT = 6,
  96. +};
  97. +
  98. typedef struct
  99. {
  100. x264_sps_t *sps;
  101. @@ -367,6 +381,13 @@ struct x264_t
  102. int i_nal_type;
  103. int i_nal_ref_idc;
  104.  
  105. + int i_disp_fields; /* Number of displayed fields (both coded and implied via pic_struct) */
  106. + int i_coded_fields; /* Number of coded fields (both coded and implied via pic_struct) */
  107. +
  108. + int i_cpb_delay; /* Equal to number of fields preceding this field
  109. + * since last buffering_period SEI */
  110. + int i_last_duration; /* Duration of last frame */
  111. +
  112. /* We use only one SPS and one PPS */
  113. x264_sps_t sps_array[1];
  114. x264_sps_t *sps;
  115. @@ -449,7 +470,8 @@ struct x264_t
  116. x264_frame_t *fref1[16+3]; /* ref list 1 */
  117. int b_ref_reorder[2];
  118.  
  119. -
  120. + /* hrd */
  121. + int initial_cpb_removal_delay;
  122.  
  123. /* Current MB DCT coeffs */
  124. struct
  125. diff --git a/common/frame.c b/common/frame.c
  126. index d89f5ab..34376cc 100644
  127. --- a/common/frame.c
  128. +++ b/common/frame.c
  129. @@ -73,6 +73,13 @@ x264_frame_t *x264_frame_new( x264_t *h, int b_fdec )
  130. frame->i_frame_num = -1;
  131. frame->i_lines_completed = -1;
  132. frame->b_fdec = b_fdec;
  133. + frame->i_pic_struct = PIC_STRUCT_AUTO;
  134. + frame->i_field_cnt = -1;
  135. + frame->i_duration =
  136. + frame->i_cpb_duration =
  137. + frame->i_dpb_output_delay =
  138. + frame->i_cpb_delay = 0;
  139. +
  140. frame->orig = frame;
  141.  
  142. /* all 4 luma planes allocated together, since the cacheline split code
  143. @@ -225,6 +232,7 @@ int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
  144. dst->i_qpplus1 = src->i_qpplus1;
  145. dst->i_pts = dst->i_reordered_pts = src->i_pts;
  146. dst->param = src->param;
  147. + dst->i_pic_struct = src->i_pic_struct;
  148.  
  149. for( i=0; i<3; i++ )
  150. {
  151. diff --git a/common/frame.h b/common/frame.h
  152. index 7c8e2ff..f8436be 100644
  153. --- a/common/frame.h
  154. +++ b/common/frame.h
  155. @@ -36,12 +36,18 @@ typedef struct x264_frame
  156. int i_qpplus1;
  157. int64_t i_pts;
  158. int64_t i_reordered_pts;
  159. + int i_duration; /* in timebase units. used for vfr */
  160. + int i_cpb_duration;
  161. + int i_cpb_delay; /* in 2 * timebase units */
  162. + int i_dpb_output_delay;
  163. x264_param_t *param;
  164.  
  165. int i_frame; /* Presentation frame number */
  166. int i_coded; /* Coded frame number */
  167. + int i_field_cnt; /* Presentation field count */
  168. int i_frame_num; /* 7.4.3 frame_num */
  169. int b_kept_as_ref;
  170. + int i_pic_struct;
  171. int b_keyframe;
  172. uint8_t b_fdec;
  173. uint8_t b_last_minigop_bframe; /* this frame is the last b in a sequence of bframes */
  174. @@ -108,6 +114,9 @@ typedef struct x264_frame
  175. uint32_t i_pixel_sum;
  176. uint64_t i_pixel_ssd;
  177.  
  178. + /* hrd */
  179. + x264_hrd_t hrd_timing;
  180. +
  181. /* vbv */
  182. uint8_t i_planned_type[X264_LOOKAHEAD_MAX+1];
  183. int i_planned_satd[X264_LOOKAHEAD_MAX+1];
  184. diff --git a/common/osdep.h b/common/osdep.h
  185. index 7f680ed..5223ef5 100644
  186. --- a/common/osdep.h
  187. +++ b/common/osdep.h
  188. @@ -221,6 +221,7 @@ static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
  189.  
  190. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
  191. #define x264_clz(x) __builtin_clz(x)
  192. +#define x264_ctz(x) __builtin_ctz(x)
  193. #else
  194. static int ALWAYS_INLINE x264_clz( uint32_t x )
  195. {
  196. @@ -233,6 +234,18 @@ static int ALWAYS_INLINE x264_clz( uint32_t x )
  197. x >>= y^4;
  198. return z + lut[x];
  199. }
  200. +
  201. +static int ALWAYS_INLINE x264_ctz( uint32_t x )
  202. +{
  203. + static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
  204. + int y, z = (((x & 0xffff) - 1) >> 27) & 16;
  205. + x >>= z;
  206. + z += y = (((x & 0xff) - 1) >> 28) & 8;
  207. + x >>= y;
  208. + z += y = (((x & 0xf) - 1) >> 29) & 4;
  209. + x >>= y;
  210. + return z + lut[x&0xf];
  211. +}
  212. #endif
  213.  
  214. #ifdef USE_REAL_PTHREAD
  215. diff --git a/common/set.h b/common/set.h
  216. index e1b9cd9..f55e6e3 100644
  217. --- a/common/set.h
  218. +++ b/common/set.h
  219. @@ -116,6 +116,27 @@ typedef struct
  220. int i_time_scale;
  221. int b_fixed_frame_rate;
  222.  
  223. + int b_nal_hrd_parameters_present;
  224. + int b_vcl_hrd_parameters_present;
  225. +
  226. + struct
  227. + {
  228. + int i_cpb_cnt;
  229. + int i_bit_rate_scale;
  230. + int i_cpb_size_scale;
  231. + int i_bit_rate_value;
  232. + int i_cpb_size_value;
  233. + int i_bit_rate_unscaled;
  234. + int i_cpb_size_unscaled;
  235. + int b_cbr_hrd;
  236. +
  237. + int i_initial_cpb_removal_delay_length;
  238. + int i_cpb_removal_delay_length;
  239. + int i_dpb_output_delay_length;
  240. + int i_time_offset_length;
  241. + } hrd;
  242. +
  243. + int b_pic_struct_present;
  244. int b_bitstream_restriction;
  245. int b_motion_vectors_over_pic_boundaries;
  246. int i_max_bytes_per_pic_denom;
  247. diff --git a/encoder/encoder.c b/encoder/encoder.c
  248. index df62389..6b0eb47 100644
  249. --- a/encoder/encoder.c
  250. +++ b/encoder/encoder.c
  251. @@ -39,8 +39,6 @@
  252.  
  253. //#define DEBUG_MB_TYPE
  254.  
  255. -#define NALU_OVERHEAD 5 // startcode + NAL type costs 5 bytes per frame
  256. -
  257. #define bs_write_ue bs_write_ue_big
  258.  
  259. static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
  260. @@ -777,6 +775,26 @@ static int x264_validate_parameters( x264_t *h )
  261. h->param.analyse.b_ssim = 0;
  262. }
  263.  
  264. + // pulldown is a form of vfr
  265. + if( h->param.b_pulldown )
  266. + h->param.b_vfr_input = 1;
  267. +
  268. + if( h->param.b_pulldown || h->param.b_interlaced )
  269. + h->param.b_pic_struct = 1;
  270. +
  271. + if( h->param.i_nal_hrd && !h->param.rc.i_vbv_buffer_size )
  272. + {
  273. + x264_log( h, X264_LOG_WARNING, "NAL HRD parameters require VBV parameters\n" );
  274. + h->param.i_nal_hrd = X264_NAL_HRD_NONE;
  275. + }
  276. +
  277. + if( h->param.i_nal_hrd == X264_NAL_HRD_CBR && ( h->param.rc.i_bitrate != h->param.rc.i_vbv_max_bitrate ||
  278. + ( h->param.rc.i_vbv_max_bitrate && !h->param.rc.i_bitrate ) ) )
  279. + {
  280. + x264_log( h, X264_LOG_WARNING, "CBR HRD requires constant bitrate\n" );
  281. + h->param.i_nal_hrd = X264_NAL_HRD_VBR;
  282. + }
  283. +
  284. /* ensure the booleans are 0 or 1 so they can be used in math */
  285. #define BOOLIFY(x) h->param.x = !!h->param.x
  286. BOOLIFY( b_cabac );
  287. @@ -790,6 +808,8 @@ static int x264_validate_parameters( x264_t *h )
  288. BOOLIFY( b_aud );
  289. BOOLIFY( b_repeat_headers );
  290. BOOLIFY( b_annexb );
  291. + BOOLIFY( b_vfr_input );
  292. + BOOLIFY( b_pulldown );
  293. BOOLIFY( analyse.b_transform_8x8 );
  294. BOOLIFY( analyse.b_weighted_bipred );
  295. BOOLIFY( analyse.b_chroma_me );
  296. @@ -907,6 +927,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
  297. }
  298. else
  299. h->i_dts_compress_multiplier = 1;
  300. + h->i_cpb_delay = 0;
  301.  
  302. h->sps = &h->sps_array[0];
  303. x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
  304. @@ -934,6 +955,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
  305. h->frames.i_delay += h->i_thread_frames - 1;
  306. h->frames.i_delay = X264_MIN( h->frames.i_delay, X264_LOOKAHEAD_MAX );
  307. h->frames.i_delay += h->param.i_sync_lookahead;
  308. + h->frames.i_delay += h->param.b_vfr_input && h->param.rc.i_vbv_buffer_size > 0;
  309. h->frames.i_bframe_delay = h->param.i_bframe ? (h->param.i_bframe_pyramid ? 2 : 1) : 0;
  310.  
  311. h->frames.i_max_ref0 = h->param.i_frame_reference;
  312. @@ -961,7 +983,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
  313. CHECKED_MALLOCZERO( h->frames.blank_unused, h->i_thread_frames * 4 * sizeof(x264_frame_t *) );
  314. h->i_ref0 = 0;
  315. h->i_ref1 = 0;
  316. -
  317. + h->i_coded_fields = h->i_disp_fields = h->i_last_duration = 0;
  318. x264_rdo_init();
  319.  
  320. /* init CPU functions */
  321. @@ -1069,6 +1091,12 @@ x264_t *x264_encoder_open( x264_param_t *param )
  322. if( x264_ratecontrol_new( h ) < 0 )
  323. goto fail;
  324.  
  325. + if( h->param.i_nal_hrd )
  326. + {
  327. + x264_log( h, X264_LOG_DEBUG, "HRD bitrate: %i bits/sec\n", h->sps->vui.hrd.i_bit_rate_unscaled );
  328. + x264_log( h, X264_LOG_DEBUG, "CPB size: %i bits\n", h->sps->vui.hrd.i_cpb_size_unscaled );
  329. + }
  330. +
  331. if( h->param.psz_dump_yuv )
  332. {
  333. /* create or truncate the reconstructed video file */
  334. @@ -1142,6 +1170,7 @@ int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
  335. COPY( i_slice_max_size );
  336. COPY( i_slice_max_mbs );
  337. COPY( i_slice_count );
  338. +
  339. /* VBV can't be turned on if it wasn't on to begin with */
  340. if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 &&
  341. param->rc.i_vbv_max_bitrate > 0 && param->rc.i_vbv_buffer_size > 0 )
  342. @@ -1151,6 +1180,9 @@ int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
  343. COPY( rc.i_bitrate );
  344. }
  345. COPY( rc.f_rf_constant );
  346. +
  347. + if( h->param.b_pulldown != param->b_pulldown && h->param.b_pic_struct )
  348. + COPY( b_pulldown );
  349. #undef COPY
  350.  
  351. mbcmp_init( h );
  352. @@ -1211,10 +1243,14 @@ static int x264_nal_end( x264_t *h )
  353. return x264_nal_check_buffer( h );
  354. }
  355.  
  356. -static int x264_encoder_encapsulate_nals( x264_t *h )
  357. +static int x264_encoder_encapsulate_nals( x264_t *h, int start )
  358. {
  359. - int nal_size = 0, i;
  360. - for( i = 0; i < h->out.i_nal; i++ )
  361. + int nal_size = 0, previous_nal_size = 0, i;
  362. +
  363. + for( i = 0; i < start; i++ )
  364. + previous_nal_size += h->out.nal[i].i_payload;
  365. +
  366. + for( i = start; i < h->out.i_nal; i++ )
  367. nal_size += h->out.nal[i].i_payload;
  368.  
  369. /* Worst-case NAL unit escaping: reallocate the buffer if it's too small. */
  370. @@ -1223,13 +1259,15 @@ static int x264_encoder_encapsulate_nals( x264_t *h )
  371. uint8_t *buf = x264_malloc( nal_size * 2 + h->out.i_nal * 4 );
  372. if( !buf )
  373. return -1;
  374. + if( previous_nal_size )
  375. + memcpy( buf, h->nal_buffer, previous_nal_size );
  376. x264_free( h->nal_buffer );
  377. h->nal_buffer = buf;
  378. }
  379.  
  380. - uint8_t *nal_buffer = h->nal_buffer;
  381. + uint8_t *nal_buffer = h->nal_buffer + previous_nal_size;
  382.  
  383. - for( i = 0; i < h->out.i_nal; i++ )
  384. + for( i = start; i < h->out.i_nal; i++ )
  385. {
  386. int size = x264_nal_encode( nal_buffer, h->param.b_annexb, &h->out.nal[i] );
  387. h->out.nal[i].i_payload = size;
  388. @@ -1237,7 +1275,7 @@ static int x264_encoder_encapsulate_nals( x264_t *h )
  389. nal_buffer += size;
  390. }
  391.  
  392. - return nal_buffer - h->nal_buffer;
  393. + return nal_buffer - (h->nal_buffer + previous_nal_size);
  394. }
  395.  
  396. /****************************************************************************
  397. @@ -1251,11 +1289,6 @@ int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
  398. bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
  399.  
  400. /* Write SEI, SPS and PPS. */
  401. - x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
  402. - if( x264_sei_version_write( h, &h->out.bs ) )
  403. - return -1;
  404. - if( x264_nal_end( h ) )
  405. - return -1;
  406.  
  407. /* generate sequence parameters */
  408. x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
  409. @@ -1269,7 +1302,14 @@ int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
  410. if( x264_nal_end( h ) )
  411. return -1;
  412.  
  413. - frame_size = x264_encoder_encapsulate_nals( h );
  414. + /* identify ourselves */
  415. + x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
  416. + if( x264_sei_version_write( h, &h->out.bs ) )
  417. + return -1;
  418. + if( x264_nal_end( h ) )
  419. + return -1;
  420. +
  421. + frame_size = x264_encoder_encapsulate_nals( h, 0 );
  422.  
  423. /* now set output*/
  424. *pi_nal = h->out.i_nal;
  425. @@ -1693,7 +1733,14 @@ static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_global_qp )
  426. if( h->sps->i_poc_type == 0 )
  427. {
  428. h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
  429. - h->sh.i_delta_poc_bottom = 0;
  430. + if( h->param.b_interlaced )
  431. + {
  432. + h->sh.i_delta_poc_bottom = h->param.b_tff ? 1 : -1;
  433. + if( h->sh.i_delta_poc_bottom == -1 )
  434. + h->sh.i_poc_lsb = ( h->fdec->i_poc + 1 ) & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
  435. + }
  436. + else
  437. + h->sh.i_delta_poc_bottom = 0;
  438. }
  439. else if( h->sps->i_poc_type == 1 )
  440. {
  441. @@ -2101,6 +2148,7 @@ int x264_encoder_encode( x264_t *h,
  442. {
  443. x264_t *thread_current, *thread_prev, *thread_oldest;
  444. int i_nal_type, i_nal_ref_idc, i_global_qp, i;
  445. + int overhead = NALU_OVERHEAD;
  446.  
  447. if( h->i_thread_frames > 1 )
  448. {
  449. @@ -2148,6 +2196,21 @@ int x264_encoder_encode( x264_t *h,
  450. if( h->frames.i_bframe_delay && fenc->i_frame == h->frames.i_bframe_delay )
  451. h->frames.i_bframe_delay_time = fenc->i_pts;
  452.  
  453. + if( ( fenc->i_pic_struct < PIC_STRUCT_AUTO ) || ( fenc->i_pic_struct > PIC_STRUCT_TRIPLE ) )
  454. + fenc->i_pic_struct = PIC_STRUCT_AUTO;
  455. +
  456. + if( fenc->i_pic_struct == PIC_STRUCT_AUTO )
  457. + {
  458. + int b_interlaced = fenc->param ? fenc->param->b_interlaced : h->param.b_interlaced;
  459. + if( b_interlaced )
  460. + {
  461. + int b_tff = fenc->param ? fenc->param->b_tff : h->param.b_tff;
  462. + fenc->i_pic_struct = b_tff ? PIC_STRUCT_TOP_BOTTOM : PIC_STRUCT_BOTTOM_TOP;
  463. + }
  464. + else
  465. + fenc->i_pic_struct = PIC_STRUCT_PROGRESSIVE;
  466. + }
  467. +
  468. if( h->frames.b_have_lowres )
  469. {
  470. if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE || h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
  471. @@ -2296,13 +2359,12 @@ int x264_encoder_encode( x264_t *h,
  472. bs_rbsp_trailing( &h->out.bs );
  473. if( x264_nal_end( h ) )
  474. return -1;
  475. + overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD;
  476. }
  477.  
  478. h->i_nal_type = i_nal_type;
  479. h->i_nal_ref_idc = i_nal_ref_idc;
  480.  
  481. - int overhead = NALU_OVERHEAD;
  482. -
  483. if( h->param.b_intra_refresh && h->fenc->i_type == X264_TYPE_P )
  484. {
  485. int pocdiff = (h->fdec->i_poc - h->fref0[0]->i_poc)/2;
  486. @@ -2324,22 +2386,11 @@ int x264_encoder_encode( x264_t *h,
  487. h->fdec->i_pir_end_col = h->fdec->f_pir_position+0.5;
  488. }
  489.  
  490. - /* Write SPS and PPS */
  491. if( h->fenc->b_keyframe )
  492. {
  493. + /* Write SPS and PPS */
  494. if( h->param.b_repeat_headers )
  495. {
  496. - if( h->fenc->i_frame == 0 )
  497. - {
  498. - /* identify ourself */
  499. - x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
  500. - if( x264_sei_version_write( h, &h->out.bs ) )
  501. - return -1;
  502. - if( x264_nal_end( h ) )
  503. - return -1;
  504. - overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD;
  505. - }
  506. -
  507. /* generate sequence parameters */
  508. x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
  509. x264_sps_write( &h->out.bs, h->sps );
  510. @@ -2355,6 +2406,32 @@ int x264_encoder_encode( x264_t *h,
  511. overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD;
  512. }
  513.  
  514. + /* generate sei buffering period */
  515. + if( h->sps->vui.b_nal_hrd_parameters_present )
  516. + {
  517. + h->initial_cpb_removal_delay = x264_hrd_fullness( h, 8*overhead );
  518. +
  519. + x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
  520. + x264_sei_buffering_period_write( h, &h->out.bs, h->initial_cpb_removal_delay );
  521. + if( x264_nal_end( h ) )
  522. + return -1;
  523. + overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD;
  524. + }
  525. +
  526. + if( h->param.b_repeat_headers )
  527. + {
  528. + if( h->fenc->i_frame == 0 )
  529. + {
  530. + /* identify ourself */
  531. + x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
  532. + if( x264_sei_version_write( h, &h->out.bs ) )
  533. + return -1;
  534. + if( x264_nal_end( h ) )
  535. + return -1;
  536. + overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD;
  537. + }
  538. + }
  539. +
  540. if( h->fenc->i_type != X264_TYPE_IDR )
  541. {
  542. int time_to_recovery = X264_MIN( h->sps->i_mb_width - 1, h->param.i_keyint_max ) + h->param.i_bframe;
  543. @@ -2365,6 +2442,16 @@ int x264_encoder_encode( x264_t *h,
  544. }
  545. }
  546.  
  547. + /* generate sei pic timing */
  548. + if( h->sps->vui.b_pic_struct_present || h->sps->vui.b_nal_hrd_parameters_present )
  549. + {
  550. + x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
  551. + x264_sei_pic_timing_write( h, &h->out.bs, h->fenc->i_cpb_delay, h->fenc->i_dpb_output_delay, h->fenc->i_pic_struct );
  552. + if( x264_nal_end( h ) )
  553. + return -1;
  554. + overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD;
  555. + }
  556. +
  557. /* Init the rate control */
  558. /* FIXME: Include slice header bit cost. */
  559. x264_ratecontrol_start( h, h->fenc->i_qpplus1, overhead*8 );
  560. @@ -2438,13 +2525,7 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
  561.  
  562. x264_frame_push_unused( thread_current, h->fenc );
  563.  
  564. - /* End bitstream, set output */
  565. - *pi_nal = h->out.i_nal;
  566. - *pp_nal = h->out.nal;
  567. -
  568. - frame_size = x264_encoder_encapsulate_nals( h );
  569. -
  570. - h->out.i_nal = 0;
  571. + frame_size = x264_encoder_encapsulate_nals( h, 0 );
  572.  
  573. /* Set output picture properties */
  574. if( h->sh.i_type == SLICE_TYPE_I )
  575. @@ -2491,9 +2572,38 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
  576.  
  577. /* update rc */
  578. x264_emms();
  579. - if( x264_ratecontrol_end( h, frame_size * 8 ) < 0 )
  580. + int filler = 0;
  581. + if( x264_ratecontrol_end( h, frame_size * 8, &filler ) < 0 )
  582. return -1;
  583.  
  584. + pic_out->hrd_timing = h->fenc->hrd_timing;
  585. +
  586. + while( filler > 0 )
  587. + {
  588. + int f;
  589. + if( h->param.i_slice_max_size && filler > h->param.i_slice_max_size )
  590. + {
  591. + int next_size = filler - h->param.i_slice_max_size;
  592. + int overflow = next_size < FILLER_OVERHEAD ? FILLER_OVERHEAD - next_size : 0;
  593. + f = h->param.i_slice_max_size - FILLER_OVERHEAD - overflow;
  594. + }
  595. + else
  596. + f = X264_MAX( 0, filler - FILLER_OVERHEAD );
  597. +
  598. + x264_nal_start( h, NAL_FILLER, NAL_PRIORITY_DISPOSABLE );
  599. + x264_filler_write( h, &h->out.bs, f );
  600. + if( x264_nal_end( h ) )
  601. + return -1;
  602. + frame_size += x264_encoder_encapsulate_nals( h, h->out.i_nal-1 );
  603. + filler -= f + FILLER_OVERHEAD;
  604. + }
  605. +
  606. + /* End bitstream, set output */
  607. + *pi_nal = h->out.i_nal;
  608. + *pp_nal = h->out.nal;
  609. +
  610. + h->out.i_nal = 0;
  611. +
  612. x264_noise_reduction_update( thread_current );
  613.  
  614. /* ---------------------- Compute/Print statistics --------------------- */
  615. diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
  616. index 8c61582..49374af 100644
  617. --- a/encoder/ratecontrol.c
  618. +++ b/encoder/ratecontrol.c
  619. @@ -94,6 +94,7 @@ struct x264_ratecontrol_t
  620. double buffer_fill_final; /* real buffer as of the last finished frame */
  621. double buffer_fill; /* planned buffer, if all in-progress frames hit their bit budget */
  622. double buffer_rate; /* # of bits added to buffer_fill after each frame */
  623. + double vbv_max_rate; /* # of bits added to buffer_fill per second */
  624. predictor_t *pred; /* predict frame size from satd */
  625. int single_frame_vbv;
  626.  
  627. @@ -148,13 +149,18 @@ struct x264_ratecontrol_t
  628. int i_zones;
  629. x264_zone_t *zones;
  630. x264_zone_t *prev_zone;
  631. +
  632. + /* hrd stuff */
  633. + int initial_cpb_removal_delay;
  634. + double nrt_first_access_unit;
  635. + double previous_cpb_final_arrival_time;
  636. };
  637.  
  638.  
  639. static int parse_zones( x264_t *h );
  640. static int init_pass2(x264_t *);
  641. static float rate_estimate_qscale( x264_t *h );
  642. -static void update_vbv( x264_t *h, int bits );
  643. +static int update_vbv( x264_t *h, int bits );
  644. static void update_vbv_plan( x264_t *h, int overhead );
  645. static double predict_size( predictor_t *p, double q, double var );
  646. static void update_predictor( predictor_t *p, double q, double var, double bits );
  647. @@ -396,6 +402,16 @@ void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
  648. if( !b_init && rc->b_2pass )
  649. return;
  650.  
  651. + if( h->param.rc.i_rc_method == X264_RC_CRF )
  652. + {
  653. + /* Arbitrary rescaling to make CRF somewhat similar to QP.
  654. + * Try to compensate for MB-tree's effects as well. */
  655. + double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
  656. + double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
  657. + rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
  658. + / qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
  659. + }
  660. +
  661. if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 )
  662. {
  663. if( h->param.rc.i_vbv_buffer_size < (int)(h->param.rc.i_vbv_max_bitrate / rc->fps) )
  664. @@ -409,8 +425,59 @@ void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
  665. so if the stream starts as CBR, keep it CBR. */
  666. if( rc->b_vbv_min_rate )
  667. h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
  668. - rc->buffer_rate = h->param.rc.i_vbv_max_bitrate * 1000. / rc->fps;
  669. - rc->buffer_size = h->param.rc.i_vbv_buffer_size * 1000.;
  670. +
  671. + int vbv_buffer_size = h->param.rc.i_vbv_buffer_size * 1000;
  672. + int vbv_max_bitrate = h->param.rc.i_vbv_max_bitrate * 1000;
  673. +
  674. + /* Init HRD */
  675. + if( h->param.i_nal_hrd && b_init )
  676. + {
  677. + h->sps->vui.hrd.i_cpb_cnt = 1;
  678. + h->sps->vui.hrd.b_cbr_hrd = h->param.i_nal_hrd == X264_NAL_HRD_CBR;
  679. + h->sps->vui.hrd.i_time_offset_length = 0;
  680. +
  681. + #define BR_SHIFT 6
  682. + #define CPB_SHIFT 4
  683. +
  684. + int bitrate = 1000*h->param.rc.i_vbv_max_bitrate;
  685. + int bufsize = 1000*h->param.rc.i_vbv_buffer_size;
  686. +
  687. + // normalize HRD size and rate to the value / scale notation
  688. + h->sps->vui.hrd.i_bit_rate_scale = x264_clip3( x264_ctz( bitrate ) - BR_SHIFT, 0, 15 );
  689. + h->sps->vui.hrd.i_bit_rate_value = bitrate >> ( h->sps->vui.hrd.i_bit_rate_scale + BR_SHIFT );
  690. + h->sps->vui.hrd.i_bit_rate_unscaled = h->sps->vui.hrd.i_bit_rate_value << ( h->sps->vui.hrd.i_bit_rate_scale + BR_SHIFT );
  691. + h->sps->vui.hrd.i_cpb_size_scale = x264_clip3( x264_ctz( bufsize ) - CPB_SHIFT, 0, 15 );
  692. + h->sps->vui.hrd.i_cpb_size_value = bufsize >> ( h->sps->vui.hrd.i_cpb_size_scale + CPB_SHIFT );
  693. + h->sps->vui.hrd.i_cpb_size_unscaled = h->sps->vui.hrd.i_cpb_size_value << ( h->sps->vui.hrd.i_cpb_size_scale + CPB_SHIFT );
  694. +
  695. + #undef CPB_SHIFT
  696. + #undef BR_SHIFT
  697. +
  698. + // arbitrary
  699. + #define MAX_DURATION 0.5
  700. +
  701. + int max_cpb_output_delay = (int64_t)h->sps->vui.i_time_scale * h->param.i_keyint_max * MAX_DURATION / h->sps->vui.i_num_units_in_tick;
  702. + int max_dpb_output_delay = (int64_t)h->sps->vui.i_time_scale * h->sps->vui.i_max_dec_frame_buffering * MAX_DURATION / h->sps->vui.i_num_units_in_tick;
  703. + int max_delay = (int)(90000.0 * (double)h->sps->vui.hrd.i_cpb_size_unscaled / h->sps->vui.hrd.i_bit_rate_unscaled + 0.5);
  704. +
  705. + h->sps->vui.hrd.i_initial_cpb_removal_delay_length = 2 + x264_clip3( 32 - x264_clz( max_delay ), 4, 22);
  706. + h->sps->vui.hrd.i_cpb_removal_delay_length = x264_clip3( 32 - x264_clz( max_cpb_output_delay ), 4, 32 );
  707. + h->sps->vui.hrd.i_dpb_output_delay_length = x264_clip3( 32 - x264_clz( max_dpb_output_delay ), 4, 32 );
  708. +
  709. + #undef MAX_DURATION
  710. +
  711. + vbv_buffer_size = X264_MIN( vbv_buffer_size, h->sps->vui.hrd.i_cpb_size_unscaled );
  712. + vbv_max_bitrate = X264_MIN( vbv_max_bitrate, h->sps->vui.hrd.i_bit_rate_unscaled );
  713. + }
  714. + else if( h->param.i_nal_hrd && !b_init )
  715. + {
  716. + x264_log( h, X264_LOG_WARNING, "VBV parameters cannot be changed when NAL HRD is in use\n" );
  717. + return;
  718. + }
  719. +
  720. + rc->buffer_rate = (double)vbv_max_bitrate / rc->fps;
  721. + rc->vbv_max_rate = vbv_max_bitrate;
  722. + rc->buffer_size = vbv_buffer_size;
  723. rc->single_frame_vbv = rc->buffer_rate * 1.1 > rc->buffer_size;
  724. rc->cbr_decay = 1.0 - rc->buffer_rate / rc->buffer_size
  725. * 0.5 * X264_MAX(0, 1.5 - rc->buffer_rate * rc->fps / rc->bitrate);
  726. @@ -426,15 +493,6 @@ void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
  727. && h->param.rc.i_vbv_max_bitrate <= h->param.rc.i_bitrate;
  728. }
  729. }
  730. - if( h->param.rc.i_rc_method == X264_RC_CRF )
  731. - {
  732. - /* Arbitrary rescaling to make CRF somewhat similar to QP.
  733. - * Try to compensate for MB-tree's effects as well. */
  734. - double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
  735. - double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
  736. - rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
  737. - / qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
  738. - }
  739. }
  740.  
  741. int x264_ratecontrol_new( x264_t *h )
  742. @@ -1294,7 +1352,7 @@ void x264_ratecontrol_set_weights( x264_t *h, x264_frame_t *frm )
  743. }
  744.  
  745. /* After encoding one frame, save stats and update ratecontrol state */
  746. -int x264_ratecontrol_end( x264_t *h, int bits )
  747. +int x264_ratecontrol_end( x264_t *h, int bits, int *filler )
  748. {
  749. x264_ratecontrol_t *rc = h->rc;
  750. const int *mbs = h->stat.frame.i_mb_count;
  751. @@ -1405,7 +1463,44 @@ int x264_ratecontrol_end( x264_t *h, int bits )
  752. }
  753. }
  754.  
  755. - update_vbv( h, bits );
  756. + *filler = update_vbv( h, bits );
  757. +
  758. + if( h->sps->vui.b_nal_hrd_parameters_present )
  759. + {
  760. + if( h->fenc->i_frame == 0 )
  761. + {
  762. + // access unit initialises the HRD
  763. + h->fenc->hrd_timing.cpb_initial_arrival_time = 0;
  764. + rc->initial_cpb_removal_delay = h->initial_cpb_removal_delay;
  765. + h->fenc->hrd_timing.cpb_removal_time = rc->nrt_first_access_unit = (double)rc->initial_cpb_removal_delay / 90000;
  766. + }
  767. + else
  768. + {
  769. + h->fenc->hrd_timing.cpb_removal_time = rc->nrt_first_access_unit +
  770. + (double)h->fenc->i_cpb_delay * h->sps->vui.i_num_units_in_tick / h->sps->vui.i_time_scale;
  771. + if( h->fenc->b_keyframe )
  772. + {
  773. + rc->nrt_first_access_unit = h->fenc->hrd_timing.cpb_removal_time;
  774. + rc->initial_cpb_removal_delay = h->initial_cpb_removal_delay;
  775. + }
  776. +
  777. + if( h->sps->vui.hrd.b_cbr_hrd )
  778. + h->fenc->hrd_timing.cpb_initial_arrival_time = rc->previous_cpb_final_arrival_time;
  779. + else
  780. + {
  781. + // NOTE: Equation C-4 has initial_cpb_removal_delay_offset which is hardcoded to zero in x264.
  782. + double cpb_earliest_arrival_time = h->fenc->hrd_timing.cpb_removal_time - (double)rc->initial_cpb_removal_delay / 90000;
  783. + h->fenc->hrd_timing.cpb_initial_arrival_time = X264_MAX( rc->previous_cpb_final_arrival_time, cpb_earliest_arrival_time );
  784. + }
  785. + }
  786. + // Equation C-6
  787. + h->fenc->hrd_timing.cpb_final_arrival_time = rc->previous_cpb_final_arrival_time =
  788. + h->fenc->hrd_timing.cpb_initial_arrival_time + (double)(bits + X264_MAX( FILLER_OVERHEAD, *filler )*8) / h->sps->vui.hrd.i_bit_rate_unscaled;
  789. +
  790. + h->fenc->hrd_timing.dpb_output_time = (double)h->fenc->i_dpb_output_delay * h->sps->vui.i_num_units_in_tick / h->sps->vui.i_time_scale +
  791. + h->fenc->hrd_timing.cpb_removal_time;
  792. + }
  793. +
  794. return 0;
  795. fail:
  796. x264_log(h, X264_LOG_ERROR, "ratecontrol_end: stats file could not be written to\n");
  797. @@ -1542,8 +1637,10 @@ static void update_predictor( predictor_t *p, double q, double var, double bits
  798. }
  799.  
  800. // update VBV after encoding a frame
  801. -static void update_vbv( x264_t *h, int bits )
  802. +static int update_vbv( x264_t *h, int bits )
  803. {
  804. + int filler = 0;
  805. +
  806. x264_ratecontrol_t *rcc = h->rc;
  807. x264_ratecontrol_t *rct = h->thread[0]->rc;
  808.  
  809. @@ -1551,14 +1648,41 @@ static void update_vbv( x264_t *h, int bits )
  810. update_predictor( &rct->pred[h->sh.i_type], qp2qscale(rcc->qpa_rc), rcc->last_satd, bits );
  811.  
  812. if( !rcc->b_vbv )
  813. - return;
  814. + return filler;
  815.  
  816. rct->buffer_fill_final -= bits;
  817. +
  818. if( rct->buffer_fill_final < 0 )
  819. x264_log( h, X264_LOG_WARNING, "VBV underflow (frame %d, %.0f bits)\n", h->i_frame, rct->buffer_fill_final );
  820. rct->buffer_fill_final = X264_MAX( rct->buffer_fill_final, 0 );
  821. - rct->buffer_fill_final += rcc->buffer_rate;
  822. - rct->buffer_fill_final = X264_MIN( rct->buffer_fill_final, rcc->buffer_size );
  823. + rct->buffer_fill_final += h->fenc->i_cpb_duration * rcc->vbv_max_rate * h->sps->vui.i_num_units_in_tick / h->sps->vui.i_time_scale;
  824. +
  825. + if( h->sps->vui.hrd.b_cbr_hrd && rct->buffer_fill_final >= rcc->buffer_size )
  826. + {
  827. + filler = (rct->buffer_fill_final - rcc->buffer_size) / 8;
  828. + rct->buffer_fill_final -= X264_MAX( FILLER_OVERHEAD, filler ) * 8;
  829. + }
  830. + else
  831. + rct->buffer_fill_final = X264_MIN( rct->buffer_fill_final, rcc->buffer_size );
  832. +
  833. + return filler;
  834. +}
  835. +
  836. +int x264_hrd_fullness( x264_t *h, int overhead )
  837. +{
  838. + x264_ratecontrol_t *rct = h->thread[0]->rc;
  839. + double cpb_bits = rct->buffer_fill_final - overhead;
  840. + double bps = h->sps->vui.hrd.i_bit_rate_unscaled;
  841. + double cpb_size = h->sps->vui.hrd.i_cpb_size_unscaled;
  842. + double cpb_fullness = 90000.0*cpb_bits/bps;
  843. +
  844. + if( cpb_bits < 0 || cpb_bits > cpb_size )
  845. + {
  846. + x264_log( h, X264_LOG_WARNING, "CPB %s: %.0lf bits in a %.0lf-bit buffer\n",
  847. + cpb_bits < 0 ? "underflow" : "overflow", cpb_bits, cpb_size );
  848. + }
  849. +
  850. + return x264_clip3f( cpb_fullness + 0.5, 0, 90000.0*cpb_size/bps ); // just lie if we are in a weird state
  851. }
  852.  
  853. // provisionally update VBV according to the planned size of all frames currently in progress
  854. @@ -2078,6 +2202,9 @@ void x264_thread_sync_ratecontrol( x264_t *cur, x264_t *prev, x264_t *next )
  855. COPY(expected_bits_sum);
  856. COPY(wanted_bits_window);
  857. COPY(bframe_bits);
  858. + COPY(initial_cpb_removal_delay);
  859. + COPY(nrt_first_access_unit);
  860. + COPY(previous_cpb_final_arrival_time);
  861. #undef COPY
  862. }
  863. //FIXME row_preds[] (not strictly necessary, but would improve prediction)
  864. diff --git a/encoder/ratecontrol.h b/encoder/ratecontrol.h
  865. index 2767866..f070a9c 100644
  866. --- a/encoder/ratecontrol.h
  867. +++ b/encoder/ratecontrol.h
  868. @@ -39,7 +39,7 @@ int x264_ratecontrol_slice_type( x264_t *, int i_frame );
  869. void x264_ratecontrol_set_weights( x264_t *h, x264_frame_t *frm );
  870. void x264_ratecontrol_mb( x264_t *, int bits );
  871. int x264_ratecontrol_qp( x264_t * );
  872. -int x264_ratecontrol_end( x264_t *, int bits );
  873. +int x264_ratecontrol_end( x264_t *, int bits, int *filler );
  874. void x264_ratecontrol_summary( x264_t * );
  875. void x264_ratecontrol_set_estimated_size( x264_t *, int bits );
  876. int x264_ratecontrol_get_estimated_size( x264_t const *);
  877. @@ -47,6 +47,6 @@ int x264_rc_analyse_slice( x264_t *h );
  878. int x264_weighted_reference_duplicate( x264_t *h, int i_ref, const x264_weight_t *w );
  879. void x264_threads_distribute_ratecontrol( x264_t *h );
  880. void x264_threads_merge_ratecontrol( x264_t *h );
  881. -
  882. +int x264_hrd_fullness( x264_t *h, int overhead );
  883. #endif
  884.  
  885. diff --git a/encoder/set.c b/encoder/set.c
  886. index f79919b..392eefd 100644
  887. --- a/encoder/set.c
  888. +++ b/encoder/set.c
  889. @@ -28,6 +28,9 @@
  890.  
  891. #define bs_write_ue bs_write_ue_big
  892.  
  893. +// Indexed by pic_struct values
  894. +static int num_clock_ts[10] = { 0, 1, 1, 1, 2, 2, 3, 3, 2, 3 };
  895. +
  896. static void transpose( uint8_t *buf, int w )
  897. {
  898. int i, j;
  899. @@ -179,15 +182,21 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  900. sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
  901. }
  902.  
  903. - sps->vui.b_timing_info_present = 0;
  904. - if( param->i_timebase_num > 0 && param->i_timebase_den > 0 )
  905. + sps->vui.b_timing_info_present = !!(param->i_timebase_num && param->i_timebase_den > 0);
  906. +
  907. + if( sps->vui.b_timing_info_present )
  908. {
  909. - sps->vui.b_timing_info_present = 1;
  910. sps->vui.i_num_units_in_tick = param->i_timebase_num;
  911. sps->vui.i_time_scale = param->i_timebase_den * 2;
  912. - sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
  913. + sps->vui.b_fixed_frame_rate = !param->b_vfr_input || param->b_pulldown;
  914. }
  915.  
  916. + sps->vui.b_vcl_hrd_parameters_present = 0; // we don't support VCL HRD
  917. + sps->vui.b_nal_hrd_parameters_present = !!param->i_nal_hrd;
  918. + sps->vui.b_pic_struct_present = !!param->b_pic_struct;
  919. +
  920. + // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
  921. +
  922. sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
  923. /* extra slot with pyramid so that we don't have to override the
  924. * order of forgetting old pictures */
  925. @@ -203,11 +212,10 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  926. sps->vui.i_max_bytes_per_pic_denom = 0;
  927. sps->vui.i_max_bits_per_mb_denom = 0;
  928. sps->vui.i_log2_max_mv_length_horizontal =
  929. - sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
  930. + sps->vui.i_log2_max_mv_length_vertical = (int)log2f( param->analyse.i_mv_range*4-1 ) + 1;
  931. }
  932. }
  933.  
  934. -
  935. void x264_sps_write( bs_t *s, x264_sps_t *sps )
  936. {
  937. bs_realign( s );
  938. @@ -343,9 +351,30 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
  939. bs_write1( s, sps->vui.b_fixed_frame_rate );
  940. }
  941.  
  942. - bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */
  943. - bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */
  944. - bs_write1( s, 0 ); /* pic_struct_present_flag */
  945. + bs_write1( s, sps->vui.b_nal_hrd_parameters_present );
  946. + if( sps->vui.b_nal_hrd_parameters_present )
  947. + {
  948. + bs_write_ue( s, sps->vui.hrd.i_cpb_cnt - 1 );
  949. + bs_write( s, 4, sps->vui.hrd.i_bit_rate_scale );
  950. + bs_write( s, 4, sps->vui.hrd.i_cpb_size_scale );
  951. +
  952. + bs_write_ue( s, sps->vui.hrd.i_bit_rate_value - 1 );
  953. + bs_write_ue( s, sps->vui.hrd.i_cpb_size_value - 1 );
  954. +
  955. + bs_write1( s, sps->vui.hrd.b_cbr_hrd );
  956. +
  957. + bs_write( s, 5, sps->vui.hrd.i_initial_cpb_removal_delay_length - 1 );
  958. + bs_write( s, 5, sps->vui.hrd.i_cpb_removal_delay_length - 1 );
  959. + bs_write( s, 5, sps->vui.hrd.i_dpb_output_delay_length - 1 );
  960. + bs_write( s, 5, sps->vui.hrd.i_time_offset_length );
  961. + }
  962. +
  963. + bs_write1( s, sps->vui.b_vcl_hrd_parameters_present );
  964. +
  965. + if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
  966. + bs_write1( s, 0 ); /* low_delay_hrd_flag */
  967. +
  968. + bs_write1( s, sps->vui.b_pic_struct_present );
  969. bs_write1( s, sps->vui.b_bitstream_restriction );
  970. if( sps->vui.b_bitstream_restriction )
  971. {
  972. @@ -476,7 +505,7 @@ void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
  973. int payload_size;
  974.  
  975. bs_realign( s );
  976. - bs_write( s, 8, 0x06 ); // payload_type = Recovery Point
  977. + bs_write( s, 8, SEI_RECOVERY_POINT );
  978. payload_size = bs_size_ue( recovery_frame_cnt ) + 4;
  979.  
  980. bs_write( s, 8, (payload_size + 7) / 8);
  981. @@ -512,7 +541,7 @@ int x264_sei_version_write( x264_t *h, bs_t *s )
  982. length = strlen(version)+1+16;
  983.  
  984. bs_realign( s );
  985. - bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
  986. + bs_write( s, 8, SEI_USER_DATA_UNREGISTERED );
  987. // payload_size
  988. for( i = 0; i <= length-255; i += 255 )
  989. bs_write( s, 8, 255 );
  990. @@ -534,6 +563,96 @@ fail:
  991. return -1;
  992. }
  993.  
  994. +void x264_sei_buffering_period_write( x264_t *h, bs_t *s, int initial_cpb_removal_delay )
  995. +{
  996. + x264_sps_t *sps = h->sps;
  997. +
  998. + int payload_size; // in bits
  999. +
  1000. + payload_size = bs_size_ue( sps->i_id );
  1001. + if( sps->vui.b_nal_hrd_parameters_present )
  1002. + payload_size += sps->vui.hrd.i_initial_cpb_removal_delay_length * 2;
  1003. +
  1004. + bs_realign( s );
  1005. + bs_write( s, 8, SEI_BUFFERING_PERIOD );
  1006. + bs_write( s, 8, (payload_size + 7) / 8);
  1007. +
  1008. + bs_write_ue( s, sps->i_id );
  1009. +
  1010. + if( sps->vui.b_nal_hrd_parameters_present )
  1011. + {
  1012. + bs_write( s, sps->vui.hrd.i_initial_cpb_removal_delay_length, initial_cpb_removal_delay );
  1013. + bs_write( s, sps->vui.hrd.i_initial_cpb_removal_delay_length, 0 ); /* initial_cpb_removal_delay_offset */
  1014. + }
  1015. +
  1016. + if( s->i_left&7 )
  1017. + bs_write1( s, 1 );
  1018. + if( s->i_left&7 )
  1019. + bs_align_0( s );
  1020. +
  1021. + bs_rbsp_trailing( s );
  1022. + bs_flush( s );
  1023. +}
  1024. +
  1025. +void x264_sei_pic_timing_write( x264_t *h, bs_t *s, int cpb_removal_delay, int dpb_output_delay, int pic_struct )
  1026. +{
  1027. + x264_sps_t *sps = h->sps;
  1028. +
  1029. + int payload_size = 0; // in bits
  1030. +
  1031. + if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present ) // if CpbDpbDelaysPresentFlag
  1032. + payload_size += sps->vui.hrd.i_cpb_removal_delay_length + sps->vui.hrd.i_dpb_output_delay_length;
  1033. +
  1034. + if( sps->vui.b_pic_struct_present )
  1035. + {
  1036. + payload_size += 4; // size of (pic_struct)
  1037. + payload_size += num_clock_ts[pic_struct];
  1038. + }
  1039. +
  1040. + bs_realign( s );
  1041. + bs_write( s, 8, SEI_PIC_TIMING );
  1042. + bs_write( s, 8, (payload_size + 7) / 8 );
  1043. +
  1044. + if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
  1045. + {
  1046. + bs_write( s, sps->vui.hrd.i_cpb_removal_delay_length, cpb_removal_delay );
  1047. + bs_write( s, sps->vui.hrd.i_dpb_output_delay_length, dpb_output_delay );
  1048. + }
  1049. +
  1050. + if( sps->vui.b_pic_struct_present )
  1051. + {
  1052. + int i = 0;
  1053. +
  1054. + bs_write( s, 4, pic_struct-1 ); // We use index 0 for "Auto"
  1055. +
  1056. + // These clock timestamps are not standardised so we don't set them
  1057. + // They could be time of origin, capture or alternative ideal display
  1058. + for( i = 0; i < num_clock_ts[pic_struct]; i++ )
  1059. + bs_write1( s, 0 ); // clock_timestamp_flag
  1060. + }
  1061. +
  1062. + if( s->i_left&7 )
  1063. + bs_write1( s, 1 );
  1064. + if( s->i_left&7 )
  1065. + bs_align_0( s );
  1066. +
  1067. + bs_rbsp_trailing( s );
  1068. + bs_flush( s );
  1069. +}
  1070. +
  1071. +void x264_filler_write( x264_t *h, bs_t *s, int filler )
  1072. +{
  1073. + int i;
  1074. +
  1075. + bs_realign( s );
  1076. +
  1077. + for( i = 0; i < filler; i++ )
  1078. + bs_write( s, 8, 0xff );
  1079. +
  1080. + bs_rbsp_trailing( s );
  1081. + bs_flush( s );
  1082. +}
  1083. +
  1084. const x264_level_t x264_levels[] =
  1085. {
  1086. { 10, 1485, 99, 152064, 64, 175, 64, 64, 0, 0, 0, 1 },
  1087. diff --git a/encoder/set.h b/encoder/set.h
  1088. index 125f7e1..b3ed234 100644
  1089. --- a/encoder/set.h
  1090. +++ b/encoder/set.h
  1091. @@ -31,5 +31,8 @@ void x264_pps_write( bs_t *s, x264_pps_t *pps );
  1092. void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt );
  1093. int x264_sei_version_write( x264_t *h, bs_t *s );
  1094. int x264_validate_levels( x264_t *h, int verbose );
  1095. +void x264_sei_buffering_period_write( x264_t *h, bs_t *s, int initial_cpb_removal_delay );
  1096. +void x264_sei_pic_timing_write( x264_t *h, bs_t *s, int cpb_removal_delay, int dpb_output_delay, int pic_struct );
  1097. +void x264_filler_write( x264_t *h, bs_t *s, int filler );
  1098.  
  1099. #endif
  1100. diff --git a/encoder/slicetype.c b/encoder/slicetype.c
  1101. index bb2ed64..8acf6c7 100644
  1102. --- a/encoder/slicetype.c
  1103. +++ b/encoder/slicetype.c
  1104. @@ -1326,6 +1326,27 @@ void x264_slicetype_decide( x264_t *h )
  1105. x264_weights_analyse( h, h->lookahead->next.list[bframes], h->lookahead->last_nonb, 0 );
  1106. }
  1107.  
  1108. + int lookahead_size = h->lookahead->next.i_size;
  1109. +
  1110. + for( i = 0; i <= bframes; i++ )
  1111. + {
  1112. + if( h->param.b_vfr_input )
  1113. + {
  1114. + if( lookahead_size-- > 1 )
  1115. + h->i_last_duration = h->lookahead->next.list[i]->i_duration = h->lookahead->next.list[i+1]->i_pts -
  1116. + h->lookahead->next.list[i]->i_pts;
  1117. + else
  1118. + h->lookahead->next.list[i]->i_duration = h->i_last_duration;
  1119. + }
  1120. + else
  1121. + {
  1122. + h->lookahead->next.list[i]->i_duration = (int64_t)h->param.i_timebase_den * h->param.i_fps_den /
  1123. + h->param.i_timebase_num / h->param.i_fps_num;
  1124. + }
  1125. + h->lookahead->next.list[i]->i_field_cnt = h->i_disp_fields;
  1126. + h->i_disp_fields += 2 * h->lookahead->next.list[i]->i_duration;
  1127. + }
  1128. +
  1129. /* shift sequence to coded order.
  1130. use a small temporary list to avoid shifting the entire next buffer around */
  1131. int i_coded = h->lookahead->next.list[0]->i_frame;
  1132. @@ -1342,8 +1363,35 @@ void x264_slicetype_decide( x264_t *h )
  1133. frames[0]->i_reordered_pts = h->lookahead->next.list[0]->i_pts;
  1134. memcpy( h->lookahead->next.list, frames, (bframes+1) * sizeof(x264_frame_t*) );
  1135. }
  1136. +
  1137. for( i = 0; i <= bframes; i++ )
  1138. - h->lookahead->next.list[i]->i_coded = i_coded++;
  1139. + {
  1140. + x264_frame_t *cur_frame = h->lookahead->next.list[i];
  1141. +
  1142. + cur_frame->i_coded = i_coded++;
  1143. +
  1144. + cur_frame->i_cpb_delay = h->i_cpb_delay;
  1145. + cur_frame->i_dpb_output_delay = cur_frame->i_field_cnt - h->i_coded_fields;
  1146. +
  1147. + // add a correction term for frame reordering
  1148. + cur_frame->i_dpb_output_delay += h->sps->vui.i_num_reorder_frames*2;
  1149. +
  1150. + // fix possible negative dpb_output_delay because of pulldown changes and reordering
  1151. + if( cur_frame->i_dpb_output_delay < 0 )
  1152. + {
  1153. + cur_frame->i_cpb_delay += cur_frame->i_dpb_output_delay;
  1154. + cur_frame->i_dpb_output_delay = 0;
  1155. + if( i )
  1156. + h->lookahead->next.list[i-1]->i_cpb_duration += cur_frame->i_dpb_output_delay;
  1157. + }
  1158. +
  1159. + if( h->lookahead->next.list[i]->b_keyframe )
  1160. + h->i_cpb_delay = 0;
  1161. +
  1162. + h->i_cpb_delay += cur_frame->i_duration * 2;
  1163. + h->i_coded_fields += cur_frame->i_duration* 2;
  1164. + cur_frame->i_cpb_duration = cur_frame->i_duration * 2;
  1165. + }
  1166. }
  1167.  
  1168. int x264_rc_analyse_slice( x264_t *h )
  1169. diff --git a/input/avs.c b/input/avs.c
  1170. index 79b5c80..e4653aa 100644
  1171. --- a/input/avs.c
  1172. +++ b/input/avs.c
  1173. @@ -263,6 +263,7 @@ static int picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_hei
  1174. pic->img.i_csp = i_csp;
  1175. pic->img.i_plane = 3;
  1176. pic->param = NULL;
  1177. + pic->i_pic_struct = PIC_STRUCT_AUTO;
  1178. return 0;
  1179. }
  1180.  
  1181. diff --git a/output/flv.c b/output/flv.c
  1182. index 2e0a0e4..04f4428 100644
  1183. --- a/output/flv.c
  1184. +++ b/output/flv.c
  1185. @@ -154,9 +154,9 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  1186. flv_hnd_t *p_flv = handle;
  1187. flv_buffer *c = p_flv->c;
  1188.  
  1189. - int sei_size = p_nal[0].i_payload;
  1190. - int sps_size = p_nal[1].i_payload;
  1191. - int pps_size = p_nal[2].i_payload;
  1192. + int sps_size = p_nal[0].i_payload;
  1193. + int pps_size = p_nal[1].i_payload;
  1194. + int sei_size = p_nal[2].i_payload;
  1195.  
  1196. // SEI
  1197. /* It is within the spec to write this as-is but for
  1198. @@ -167,10 +167,10 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  1199. return -1;
  1200. p_flv->sei_len = sei_size;
  1201.  
  1202. - memcpy( p_flv->sei, p_nal[0].p_payload, sei_size );
  1203. + memcpy( p_flv->sei, p_nal[2].p_payload, sei_size );
  1204.  
  1205. // SPS
  1206. - uint8_t *sps = p_nal[1].p_payload + 4;
  1207. + uint8_t *sps = p_nal[0].p_payload + 4;
  1208.  
  1209. x264_put_byte( c, FLV_TAG_TYPE_VIDEO );
  1210. x264_put_be24( c, 0 ); // rewrite later
  1211. @@ -196,7 +196,7 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  1212. // PPS
  1213. x264_put_byte( c, 1 ); // number of pps
  1214. x264_put_be16( c, pps_size - 4 );
  1215. - flv_append_data( c, p_nal[2].p_payload + 4, pps_size - 4 );
  1216. + flv_append_data( c, p_nal[1].p_payload + 4, pps_size - 4 );
  1217.  
  1218. // rewrite data length info
  1219. unsigned length = c->d_cur - p_flv->start;
  1220. diff --git a/output/matroska.c b/output/matroska.c
  1221. index fb39ced..25e91d5 100644
  1222. --- a/output/matroska.c
  1223. +++ b/output/matroska.c
  1224. @@ -107,13 +107,13 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  1225. {
  1226. mkv_hnd_t *p_mkv = handle;
  1227.  
  1228. - int sei_size = p_nal[0].i_payload;
  1229. - int sps_size = p_nal[1].i_payload - 4;
  1230. - int pps_size = p_nal[2].i_payload - 4;
  1231. + int sps_size = p_nal[0].i_payload - 4;
  1232. + int pps_size = p_nal[1].i_payload - 4;
  1233. + int sei_size = p_nal[2].i_payload;
  1234.  
  1235. - uint8_t *sei = p_nal[0].p_payload;
  1236. - uint8_t *sps = p_nal[1].p_payload + 4;
  1237. - uint8_t *pps = p_nal[2].p_payload + 4;
  1238. + uint8_t *sps = p_nal[0].p_payload + 4;
  1239. + uint8_t *pps = p_nal[1].p_payload + 4;
  1240. + uint8_t *sei = p_nal[2].p_payload;
  1241.  
  1242. int ret;
  1243. uint8_t *avcC;
  1244. diff --git a/output/mp4.c b/output/mp4.c
  1245. index b99eaed..9fff33e 100644
  1246. --- a/output/mp4.c
  1247. +++ b/output/mp4.c
  1248. @@ -228,13 +228,13 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  1249. mp4_hnd_t *p_mp4 = handle;
  1250. GF_AVCConfigSlot *p_slot;
  1251.  
  1252. - int sei_size = p_nal[0].i_payload;
  1253. - int sps_size = p_nal[1].i_payload - 4;
  1254. - int pps_size = p_nal[2].i_payload - 4;
  1255. + int sps_size = p_nal[0].i_payload - 4;
  1256. + int pps_size = p_nal[1].i_payload - 4;
  1257. + int sei_size = p_nal[2].i_payload;
  1258.  
  1259. - uint8_t *sei = p_nal[0].p_payload;
  1260. - uint8_t *sps = p_nal[1].p_payload + 4;
  1261. - uint8_t *pps = p_nal[2].p_payload + 4;
  1262. + uint8_t *sps = p_nal[0].p_payload + 4;
  1263. + uint8_t *pps = p_nal[1].p_payload + 4;
  1264. + uint8_t *sei = p_nal[2].p_payload;
  1265.  
  1266. // SPS
  1267.  
  1268. diff --git a/x264.c b/x264.c
  1269. index 959626a..23d1caa 100644
  1270. --- a/x264.c
  1271. +++ b/x264.c
  1272. @@ -57,6 +57,7 @@ typedef struct {
  1273. hnd_t hin;
  1274. hnd_t hout;
  1275. FILE *qpfile;
  1276. + int i_pulldown;
  1277. } cli_opt_t;
  1278.  
  1279. /* i/o file operation function pointer structs */
  1280. @@ -92,10 +93,31 @@ static const char * const muxer_names[] =
  1281. 0
  1282. };
  1283.  
  1284. +static const char * const pulldown_names[] = { "", "22", "32", "64", "double", "triple", "euro", 0 };
  1285. +
  1286. +typedef struct{
  1287. + int mod;
  1288. + int *pattern;
  1289. + int i_timebase_multiplier;
  1290. +} cli_pulldown_t;
  1291. +
  1292. +enum pulldown_type_e
  1293. +{
  1294. + X264_PULLDOWN_22 = 1,
  1295. + X264_PULLDOWN_32 = 2,
  1296. + X264_PULLDOWN_64 = 3,
  1297. + X264_PULLDOWN_DOUBLE = 4,
  1298. + X264_PULLDOWN_TRIPLE = 5,
  1299. + X264_PULLDOWN_EURO = 6,
  1300. +};
  1301. +
  1302. +// indexed by pic_struct enum
  1303. +static float frame_duration[10] = { 0.0, 1, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 3 };
  1304. +
  1305. static void Help( x264_param_t *defaults, int longhelp );
  1306. static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
  1307. static int Encode( x264_param_t *param, cli_opt_t *opt );
  1308. -
  1309. +static int set_pulldown( cli_pulldown_t *pulldown, int i_pulldown, x264_param_t *param );
  1310. /****************************************************************************
  1311. * main:
  1312. ****************************************************************************/
  1313. @@ -334,7 +356,8 @@ static void Help( x264_param_t *defaults, int longhelp )
  1314. else H1( " --slices <integer> Number of slices per frame\n" );
  1315. H2( " --slice-max-size <integer> Limit the size of each slice in bytes\n");
  1316. H2( " --slice-max-mbs <integer> Limit the size of each slice in macroblocks\n");
  1317. - H0( " --interlaced Enable pure-interlaced mode\n" );
  1318. + H0( " --interlaced, --tff Enable pure-interlaced mode (top field first)\n" );
  1319. + H0( " --bff Enable pure-interlaced mode (bottom field first)\n" );
  1320. H2( " --constrained-intra Enable constrained intra prediction.\n" );
  1321. H0( "\n" );
  1322. H0( "Ratecontrol:\n" );
  1323. @@ -476,6 +499,11 @@ static void Help( x264_param_t *defaults, int longhelp )
  1324. strtable_lookup( x264_colmatrix_names, defaults->vui.i_colmatrix ) );
  1325. H2( " --chromaloc <integer> Specify chroma sample location (0 to 5) [%d]\n",
  1326. defaults->vui.i_chroma_loc );
  1327. +
  1328. + H0( " --nal-hrd <string> Signal HRD information (needed e.g. for Blu-Ray compliance)\n"
  1329. + " - vbr, cbr. (requires vbv-bufsize; cbr not allowed in .mp4)\n" );
  1330. + H2( " --pic-struct Send pic_struct in Picture Timing SEI (on for pulldown or interlaced) \n" );
  1331. +
  1332. H0( "\n" );
  1333. H0( "Input/Output:\n" );
  1334. H0( "\n" );
  1335. @@ -508,6 +536,8 @@ static void Help( x264_param_t *defaults, int longhelp )
  1336. H2( " --sps-id <integer> Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
  1337. H2( " --aud Use access unit delimiters\n" );
  1338. H2( " --force-cfr Force constant framerate timestamp generation\n" );
  1339. + H0( " --pulldown <string> Use soft pulldown and Timing SEI to change frame rate\n"
  1340. + " - 22, 32, 64, double, triple, euro\n" );
  1341. H0( "\n" );
  1342. }
  1343.  
  1344. @@ -529,6 +559,7 @@ static void Help( x264_param_t *defaults, int longhelp )
  1345. #define OPT_DEMUXER 271
  1346. #define OPT_INDEX 272
  1347. #define OPT_INTERLACED 273
  1348. +#define OPT_PULLDOWN 274
  1349.  
  1350. static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
  1351. static struct option long_options[] =
  1352. @@ -557,6 +588,8 @@ static struct option long_options[] =
  1353. { "filter", required_argument, NULL, 0 },
  1354. { "deblock", required_argument, NULL, 'f' },
  1355. { "interlaced", no_argument, NULL, OPT_INTERLACED },
  1356. + { "tff", no_argument, NULL, 0 },
  1357. + { "bff", no_argument, NULL, 0 },
  1358. { "no-interlaced", no_argument, NULL, OPT_INTERLACED },
  1359. { "constrained-intra", no_argument, NULL, 0 },
  1360. { "cabac", no_argument, NULL, 0 },
  1361. @@ -663,6 +696,9 @@ static struct option long_options[] =
  1362. { "colormatrix", required_argument, NULL, 0 },
  1363. { "chromaloc", required_argument, NULL, 0 },
  1364. { "force-cfr", no_argument, NULL, 0 },
  1365. + { "pic-struct", no_argument, NULL, 0 },
  1366. + { "nal-hrd", required_argument, NULL, 0 },
  1367. + { "pulldown", required_argument, NULL, OPT_PULLDOWN },
  1368. {0, 0, 0, 0}
  1369. };
  1370.  
  1371. @@ -677,9 +713,13 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
  1372. #ifdef MP4_OUTPUT
  1373. output = mp4_output;
  1374. param->b_annexb = 0;
  1375. - param->b_aud = 0;
  1376. param->b_dts_compress = 0;
  1377. param->b_repeat_headers = 0;
  1378. + if( param->i_nal_hrd == X264_NAL_HRD_CBR )
  1379. + {
  1380. + fprintf( stderr, "x264 [warning]: cbr nal-hrd is not compatible with mp4\n" );
  1381. + param->i_nal_hrd = X264_NAL_HRD_VBR;
  1382. + }
  1383. #else
  1384. fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
  1385. return -1;
  1386. @@ -689,7 +729,6 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
  1387. {
  1388. output = mkv_output;
  1389. param->b_annexb = 0;
  1390. - param->b_aud = 0;
  1391. param->b_dts_compress = 0;
  1392. param->b_repeat_headers = 0;
  1393. }
  1394. @@ -697,7 +736,6 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
  1395. {
  1396. output = flv_output;
  1397. param->b_annexb = 0;
  1398. - param->b_aud = 0;
  1399. param->b_dts_compress = 1;
  1400. param->b_repeat_headers = 0;
  1401. }
  1402. @@ -786,6 +824,88 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
  1403. return 0;
  1404. }
  1405.  
  1406. +static int set_pulldown( cli_pulldown_t *pulldown, int i_pulldown, x264_param_t *param )
  1407. +{
  1408. + float f_max_fps_factor = 1;
  1409. + // multiplier used when repeat field frames make frame duration not an multiple of timebase_den
  1410. + pulldown->i_timebase_multiplier = 1;
  1411. + param->b_vfr_input = 1;
  1412. +
  1413. + switch( i_pulldown )
  1414. + {
  1415. + case X264_PULLDOWN_22:
  1416. + pulldown->mod = 1;
  1417. + pulldown->pattern = malloc( pulldown->mod*sizeof(int) );
  1418. + if( !pulldown->pattern )
  1419. + return -1;
  1420. + pulldown->pattern[0] = PIC_STRUCT_TOP_BOTTOM;
  1421. + break;
  1422. + case X264_PULLDOWN_32:
  1423. + pulldown->mod = 4;
  1424. + pulldown->pattern = malloc( pulldown->mod*sizeof(int) );
  1425. + if( !pulldown->pattern )
  1426. + return -1;
  1427. + pulldown->pattern[0] = PIC_STRUCT_TOP_BOTTOM_TOP;
  1428. + pulldown->pattern[1] = PIC_STRUCT_BOTTOM_TOP;
  1429. + pulldown->pattern[2] = PIC_STRUCT_BOTTOM_TOP_BOTTOM;
  1430. + pulldown->pattern[3] = PIC_STRUCT_TOP_BOTTOM;
  1431. + f_max_fps_factor = 1.25;
  1432. + pulldown->i_timebase_multiplier = 2;
  1433. + break;
  1434. +
  1435. + case X264_PULLDOWN_64:
  1436. + pulldown->mod = 2;
  1437. + pulldown->pattern = malloc( pulldown->mod*sizeof(int) );
  1438. + if( !pulldown->pattern )
  1439. + return -1;
  1440. + pulldown->pattern[0] = PIC_STRUCT_DOUBLE;
  1441. + pulldown->pattern[1] = PIC_STRUCT_TRIPLE;
  1442. + f_max_fps_factor = 2.5;
  1443. + break;
  1444. +
  1445. + case X264_PULLDOWN_DOUBLE:
  1446. + pulldown->mod = 1;
  1447. + pulldown->pattern = malloc( pulldown->mod*sizeof(int) );
  1448. + if( !pulldown->pattern )
  1449. + return -1;
  1450. + pulldown->pattern[0] = PIC_STRUCT_DOUBLE;
  1451. + f_max_fps_factor = 2;
  1452. + break;
  1453. +
  1454. + case X264_PULLDOWN_TRIPLE:
  1455. + pulldown->mod = 1;
  1456. + pulldown->pattern = malloc( pulldown->mod*sizeof(int) );
  1457. + if( !pulldown->pattern )
  1458. + return -1;
  1459. + pulldown->pattern[0] = PIC_STRUCT_TRIPLE;
  1460. + f_max_fps_factor = 3;
  1461. + break;
  1462. +
  1463. + case X264_PULLDOWN_EURO:
  1464. + pulldown->mod = 24;
  1465. + pulldown->pattern = malloc( pulldown->mod*sizeof(int) );
  1466. + if( !pulldown->pattern )
  1467. + return -1;
  1468. +
  1469. + int j;
  1470. + for( j = 0; j < 11; j++ )
  1471. + {
  1472. + pulldown->pattern[j+1] = PIC_STRUCT_BOTTOM_TOP;
  1473. + pulldown->pattern[j+13] = PIC_STRUCT_TOP_BOTTOM;
  1474. + }
  1475. + pulldown->pattern[0] = PIC_STRUCT_TOP_BOTTOM_TOP;
  1476. + pulldown->pattern[12] = PIC_STRUCT_BOTTOM_TOP_BOTTOM;
  1477. + f_max_fps_factor = 25.0/24.0;
  1478. + pulldown->i_timebase_multiplier = 2;
  1479. + break;
  1480. + }
  1481. +
  1482. + param->i_timebase_num = param->i_fps_den;
  1483. + param->i_timebase_den = param->i_fps_num * f_max_fps_factor * pulldown->i_timebase_multiplier;
  1484. +
  1485. + return 0;
  1486. +}
  1487. +
  1488. /*****************************************************************************
  1489. * Parse:
  1490. *****************************************************************************/
  1491. @@ -1154,6 +1274,16 @@ psy_failure:
  1492. case OPT_INTERLACED:
  1493. b_user_interlaced = 1;
  1494. goto generic_option;
  1495. + case OPT_PULLDOWN:
  1496. + for( i = 0; pulldown_names[i] && strcasecmp( pulldown_names[i], optarg ); )
  1497. + i++;
  1498. + if( !pulldown_names[i] )
  1499. + {
  1500. + fprintf( stderr, "x264 [error]: invalid pulldown '%s'\n", optarg );
  1501. + return -1;
  1502. + }
  1503. + opt->i_pulldown = i;
  1504. + break;
  1505. default:
  1506. generic_option:
  1507. {
  1508. @@ -1440,6 +1570,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1509. {
  1510. x264_t *h;
  1511. x264_picture_t pic;
  1512. + cli_pulldown_t pulldown;
  1513.  
  1514. int i_frame, i_frame_total, i_frame_output;
  1515. int64_t i_start, i_end;
  1516. @@ -1455,6 +1586,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1517. double duration;
  1518. int prev_timebase_den = param->i_timebase_den / gcd( param->i_timebase_num, param->i_timebase_den );
  1519. int dts_compress_multiplier;
  1520. + int64_t pulldown_pts = 0;
  1521.  
  1522. opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
  1523. i_frame_total = input.get_frame_total( opt->hin );
  1524. @@ -1465,6 +1597,14 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1525. param->i_frame_total = i_frame_total;
  1526. i_update_interval = i_frame_total ? x264_clip3( i_frame_total / 1000, 1, 10 ) : 10;
  1527.  
  1528. + /* set up pulldown */
  1529. + if( opt->i_pulldown && !param->b_vfr_input )
  1530. + {
  1531. + param->b_pulldown = 1;
  1532. + if( set_pulldown( &pulldown, opt->i_pulldown, param ) < 0 )
  1533. + return -1;
  1534. + }
  1535. +
  1536. if( ( h = x264_encoder_open( param ) ) == NULL )
  1537. {
  1538. fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
  1539. @@ -1524,6 +1664,14 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1540.  
  1541. if( !param->b_vfr_input )
  1542. pic.i_pts = i_frame;
  1543. +
  1544. + if( param->b_pulldown )
  1545. + {
  1546. + pic.i_pic_struct = pulldown.pattern[ i_frame % pulldown.mod ];
  1547. + pic.i_pts = pulldown_pts;
  1548. + pulldown_pts += frame_duration[pic.i_pic_struct] * pulldown.i_timebase_multiplier;
  1549. + }
  1550. +
  1551. if( pic.i_pts <= largest_pts )
  1552. {
  1553. if( param->i_log_level >= X264_LOG_WARNING )
  1554. @@ -1537,6 +1685,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1555. }
  1556. pic.i_pts = largest_pts + ticks_per_frame;
  1557. }
  1558. +
  1559. second_largest_pts = largest_pts;
  1560. largest_pts = pic.i_pts;
  1561.  
  1562. @@ -1610,5 +1759,8 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1563. (double) i_file * 8 / ( 1000 * duration ) );
  1564. }
  1565.  
  1566. + if( param->b_pulldown )
  1567. + free( pulldown.pattern );
  1568. +
  1569. return 0;
  1570. }
  1571. diff --git a/x264.h b/x264.h
  1572. index e7d19b7..276cbaa 100644
  1573. --- a/x264.h
  1574. +++ b/x264.h
  1575. @@ -35,7 +35,7 @@
  1576.  
  1577. #include <stdarg.h>
  1578.  
  1579. -#define X264_BUILD 85
  1580. +#define X264_BUILD 86
  1581.  
  1582. /* x264_t:
  1583. * opaque handler for encoder */
  1584. @@ -111,6 +111,7 @@ static const char * const x264_fullrange_names[] = { "off", "on", 0 };
  1585. static const char * const x264_colorprim_names[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", 0 };
  1586. static const char * const x264_transfer_names[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", 0 };
  1587. static const char * const x264_colmatrix_names[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", 0 };
  1588. +static const char * const x264_nal_hrd_names[] = { "none", "vbr", "cbr", 0 };
  1589.  
  1590. /* Colorspace type
  1591. * legacy only; nothing other than I420 is really supported. */
  1592. @@ -148,6 +149,11 @@ static const char * const x264_colmatrix_names[] = { "GBR", "bt709", "undef", ""
  1593. #define X264_THREADS_AUTO 0 /* Automatically select optimal number of threads */
  1594. #define X264_SYNC_LOOKAHEAD_AUTO (-1) /* Automatically select optimal lookahead thread buffer size */
  1595.  
  1596. +/* HRD */
  1597. +#define X264_NAL_HRD_NONE 0
  1598. +#define X264_NAL_HRD_VBR 1
  1599. +#define X264_NAL_HRD_CBR 2
  1600. +
  1601. /* Zones: override ratecontrol or other options for specific sections of the video.
  1602. * See x264_encoder_reconfig() for which options can be changed.
  1603. * If zones overlap, whichever comes later in the list takes precedence. */
  1604. @@ -176,6 +182,8 @@ typedef struct x264_param_t
  1605. int i_level_idc;
  1606. int i_frame_total; /* number of frames to encode if known, else 0 */
  1607.  
  1608. + int i_nal_hrd; /* Use Buffering and Picture Timing SEI's to signal HRD */
  1609. +
  1610. struct
  1611. {
  1612. /* they will be reduced to be 0 < x <= 65535 and prime */
  1613. @@ -320,6 +328,21 @@ typedef struct x264_param_t
  1614. * by compressing them to be less than the second PTS.
  1615. * Warning: this will change the timebase! */
  1616.  
  1617. + int b_tff;
  1618. +
  1619. + /* Pulldown:
  1620. + * Set b_pulldown to use pulldown and pass the correct pic_struct with the frame
  1621. + * The input timebase should be the timebase relating to the output framerate. This should be constant.
  1622. + * b_pic_struct must also be on. Pulldown is (almost always) a form of VFR so b_vfr_input must be on.
  1623. + * PTS must also be the PTS of the pulldowned frame
  1624. + *
  1625. + * Pulldown changes are not clearly defined in H.264. Therefore, it is the calling app's responsibility to manage this.
  1626. + */
  1627. +
  1628. + int b_pulldown;
  1629. +
  1630. + int b_pic_struct;
  1631. +
  1632. /* Slicing parameters */
  1633. int i_slice_max_size; /* Max size per slice in bytes; includes estimated NAL overhead. */
  1634. int i_slice_max_mbs; /* Max number of MBs per slice; overrides i_slice_count. */
  1635. @@ -368,6 +391,29 @@ int x264_param_parse( x264_param_t *, const char *name, const char *value );
  1636. /****************************************************************************
  1637. * Picture structures and functions.
  1638. ****************************************************************************/
  1639. +
  1640. +enum pic_struct_e
  1641. +{
  1642. + PIC_STRUCT_AUTO = 0, // automatically decide (default)
  1643. + PIC_STRUCT_PROGRESSIVE = 1, // progressive frame
  1644. + // "TOP" and "BOTTOM" are not supported in x264 (PAFF only)
  1645. + PIC_STRUCT_TOP_BOTTOM = 4, // top field followed by bottom
  1646. + PIC_STRUCT_BOTTOM_TOP = 5, // bottom field followed by top
  1647. + PIC_STRUCT_TOP_BOTTOM_TOP = 6, // top field, bottom field, top field repeated
  1648. + PIC_STRUCT_BOTTOM_TOP_BOTTOM = 7, // bottom field, top field, bottom field repeated
  1649. + PIC_STRUCT_DOUBLE = 8, // double frame
  1650. + PIC_STRUCT_TRIPLE = 9 // triple frame
  1651. +};
  1652. +
  1653. +typedef struct
  1654. +{
  1655. + double cpb_initial_arrival_time;
  1656. + double cpb_final_arrival_time;
  1657. + double cpb_removal_time;
  1658. +
  1659. + double dpb_output_time;
  1660. +} x264_hrd_t;
  1661. +
  1662. typedef struct
  1663. {
  1664. int i_csp;
  1665. @@ -388,6 +434,9 @@ typedef struct
  1666. int i_type;
  1667. /* In: force quantizer for > 0 */
  1668. int i_qpplus1;
  1669. + /* In: pic_struct, for pulldown/doubling/etc...used only if b_pic_timing_sei=1.
  1670. + * use pic_struct_e for pic_struct inputs */
  1671. + int i_pic_struct;
  1672. /* Out: whether this frame is a keyframe. Important when using modes that result in
  1673. * SEI recovery points being used instead of IDR frames. */
  1674. int b_keyframe;
  1675. @@ -405,6 +454,8 @@ typedef struct
  1676. x264_param_t *param;
  1677. /* In: raw data */
  1678. x264_image_t img;
  1679. + /* Out: HRD timing information. Output only when b_nal_hrd is set. */
  1680. + x264_hrd_t hrd_timing;
  1681. /* private user data. libx264 doesn't touch this,
  1682. not even copy it from input to output frames. */
  1683. void *opaque;
  1684. @@ -436,6 +487,7 @@ enum nal_unit_type_e
  1685. NAL_SPS = 7,
  1686. NAL_PPS = 8,
  1687. NAL_AUD = 9,
  1688. + NAL_FILLER = 12,
  1689. /* ref_idc == 0 for 6,9,10,11,12 */
  1690. };
  1691. enum nal_priority_e
  1692.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement