Advertisement
Guest User

kierank

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