Advertisement
Guest User

kierank

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