Advertisement
Guest User

Untitled

a guest
May 4th, 2017
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 83.28 KB | None | 0 0
  1. From 90a249aa40deec9234f9ea7554deb8a418183094 Mon Sep 17 00:00:00 2001
  2. From: Jason Garrett-Glaser <darkshikari@gmail.com>
  3. Date: Thu, 21 Jan 2010 10:00:07 -0800
  4. Subject: [PATCH 01/13] Merge nnz_backup with scratch buffer
  5. Slightly less memory usage.
  6.  
  7. ---
  8. common/common.h | 1 -
  9. common/frame.c | 5 +++--
  10. common/macroblock.c | 2 --
  11. encoder/encoder.c | 4 +++-
  12. 4 files changed, 6 insertions(+), 6 deletions(-)
  13.  
  14. diff --git a/common/common.h b/common/common.h
  15. index 02d1748..df39f26 100644
  16. --- a/common/common.h
  17. +++ b/common/common.h
  18. @@ -532,7 +532,6 @@ struct x264_t
  19. int8_t *skipbp; /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
  20. int8_t *mb_transform_size; /* transform_size_8x8_flag of each mb */
  21. uint8_t *intra_border_backup[2][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
  22. - uint8_t (*nnz_backup)[16]; /* when using cavlc + 8x8dct, the deblocker uses a modified nnz */
  23.  
  24. /* buffer for weighted versions of the reference frames */
  25. uint8_t *p_weight_buf[16];
  26. diff --git a/common/frame.c b/common/frame.c
  27. index 08ef87f..3ef303a 100644
  28. --- a/common/frame.c
  29. +++ b/common/frame.c
  30. @@ -661,9 +661,10 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  31. int stride2y = stridey << b_interlaced;
  32. int strideuv = h->fdec->i_stride[1];
  33. int stride2uv = strideuv << b_interlaced;
  34. + uint8_t (*nnz_backup)[16] = h->scratch_buffer;
  35.  
  36. if( !h->pps->b_cabac && h->pps->b_transform_8x8_mode )
  37. - munge_cavlc_nnz( h, mb_y, h->mb.nnz_backup, munge_cavlc_nnz_row );
  38. + munge_cavlc_nnz( h, mb_y, nnz_backup, munge_cavlc_nnz_row );
  39.  
  40. for( mb_x = 0; mb_x < h->sps->i_mb_width; mb_x += (~b_interlaced | mb_y)&1, mb_y ^= b_interlaced )
  41. {
  42. @@ -823,7 +824,7 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  43. }
  44.  
  45. if( !h->pps->b_cabac && h->pps->b_transform_8x8_mode )
  46. - munge_cavlc_nnz( h, mb_y, h->mb.nnz_backup, restore_cavlc_nnz_row );
  47. + munge_cavlc_nnz( h, mb_y, nnz_backup, restore_cavlc_nnz_row );
  48. }
  49.  
  50. void x264_frame_deblock( x264_t *h )
  51. diff --git a/common/macroblock.c b/common/macroblock.c
  52. index 921d2b9..10f09ac 100644
  53. --- a/common/macroblock.c
  54. +++ b/common/macroblock.c
  55. @@ -701,7 +701,6 @@ int x264_macroblock_cache_init( x264_t *h )
  56.  
  57. /* all coeffs */
  58. CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
  59. - CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
  60.  
  61. if( h->param.b_cabac )
  62. {
  63. @@ -797,7 +796,6 @@ void x264_macroblock_cache_end( x264_t *h )
  64. }
  65. x264_free( h->mb.intra4x4_pred_mode );
  66. x264_free( h->mb.non_zero_count );
  67. - x264_free( h->mb.nnz_backup );
  68. x264_free( h->mb.mb_transform_size );
  69. x264_free( h->mb.skipbp );
  70. x264_free( h->mb.cbp );
  71. diff --git a/encoder/encoder.c b/encoder/encoder.c
  72. index f97df1b..db2c861 100644
  73. --- a/encoder/encoder.c
  74. +++ b/encoder/encoder.c
  75. @@ -1018,7 +1018,9 @@ x264_t *x264_encoder_open( x264_param_t *param )
  76. int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
  77. ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
  78. int buf_mbtree = h->param.rc.b_mb_tree * ((h->sps->i_mb_width+3)&~3) * sizeof(int);
  79. - CHECKED_MALLOC( h->thread[i]->scratch_buffer, X264_MAX4( buf_hpel, buf_ssim, buf_tesa, buf_mbtree ) );
  80. + int buf_nnz = !h->param.b_cabac * h->pps->b_transform_8x8_mode * (h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t));
  81. + int scratch_size = X264_MAX4( buf_hpel, buf_ssim, buf_tesa, X264_MAX( buf_mbtree, buf_nnz ) );
  82. + CHECKED_MALLOC( h->thread[i]->scratch_buffer, scratch_size );
  83. }
  84.  
  85. if( x264_ratecontrol_new( h ) < 0 )
  86. --
  87. 1.6.1.2
  88.  
  89.  
  90. From 1a1b356a2996629d2c5ad2b568df8df9bff32954 Mon Sep 17 00:00:00 2001
  91. From: Jason Garrett-Glaser <darkshikari@gmail.com>
  92. Date: Thu, 21 Jan 2010 23:07:11 -0800
  93. Subject: [PATCH 02/13] Fix bitstream alignment with multiple slices
  94. Broke multi-slice encoding on CPUs without unaligned access.
  95. New system simply forces a bitstream realignment at the start of each writing function and flushes when it reaches the end.
  96.  
  97. ---
  98. common/bs.h | 14 +++++++++++++-
  99. encoder/encoder.c | 2 +-
  100. encoder/set.c | 8 ++++++++
  101. 3 files changed, 22 insertions(+), 2 deletions(-)
  102.  
  103. diff --git a/common/bs.h b/common/bs.h
  104. index d13eb03..0773de6 100644
  105. --- a/common/bs.h
  106. +++ b/common/bs.h
  107. @@ -77,7 +77,7 @@ static inline void bs_init( bs_t *s, void *p_data, int i_data )
  108. s->p = s->p_start = (uint8_t*)p_data - offset;
  109. s->p_end = (uint8_t*)p_data + i_data;
  110. s->i_left = (WORD_SIZE - offset)*8;
  111. - s->cur_bits = endian_fix32(*(uint32_t *)(s->p));
  112. + s->cur_bits = endian_fix32( M32(s->p) );
  113. s->cur_bits >>= (4-offset)*8;
  114. }
  115. static inline int bs_pos( bs_t *s )
  116. @@ -92,6 +92,18 @@ static inline void bs_flush( bs_t *s )
  117. s->p += WORD_SIZE - s->i_left / 8;
  118. s->i_left = WORD_SIZE*8;
  119. }
  120. +/* The inverse of bs_flush: prepare the bitstream to be written to again. */
  121. +static inline void bs_realign( bs_t *s )
  122. +{
  123. + int offset = ((intptr_t)s->p & 3);
  124. + if( offset )
  125. + {
  126. + s->p = (uint8_t*)s->p - offset;
  127. + s->i_left = (WORD_SIZE - offset)*8;
  128. + s->cur_bits = endian_fix32( M32(s->p) );
  129. + s->cur_bits >>= (4-offset)*8;
  130. + }
  131. +}
  132.  
  133. static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
  134. {
  135. diff --git a/encoder/encoder.c b/encoder/encoder.c
  136. index db2c861..c7065a2 100644
  137. --- a/encoder/encoder.c
  138. +++ b/encoder/encoder.c
  139. @@ -1206,7 +1206,6 @@ int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
  140. x264_pps_write( &h->out.bs, h->pps );
  141. if( x264_nal_end( h ) )
  142. return -1;
  143. - bs_flush( &h->out.bs );
  144.  
  145. frame_size = x264_encoder_encapsulate_nals( h );
  146.  
  147. @@ -1657,6 +1656,7 @@ static int x264_slice_write( x264_t *h )
  148. /* Assume no more than 3 bytes of NALU escaping. */
  149. int slice_max_size = h->param.i_slice_max_size > 0 ? (h->param.i_slice_max_size-3-NALU_OVERHEAD)*8 : INT_MAX;
  150. int starting_bits = bs_pos(&h->out.bs);
  151. + bs_realign( &h->out.bs );
  152.  
  153. /* Slice */
  154. x264_nal_start( h, h->i_nal_type, h->i_nal_ref_idc );
  155. diff --git a/encoder/set.c b/encoder/set.c
  156. index 641eae9..f79919b 100644
  157. --- a/encoder/set.c
  158. +++ b/encoder/set.c
  159. @@ -210,6 +210,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  160.  
  161. void x264_sps_write( bs_t *s, x264_sps_t *sps )
  162. {
  163. + bs_realign( s );
  164. bs_write( s, 8, sps->i_profile_idc );
  165. bs_write( s, 1, sps->b_constraint_set0 );
  166. bs_write( s, 1, sps->b_constraint_set1 );
  167. @@ -359,6 +360,7 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
  168. }
  169.  
  170. bs_rbsp_trailing( s );
  171. + bs_flush( s );
  172. }
  173.  
  174. void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
  175. @@ -423,6 +425,7 @@ void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *
  176.  
  177. void x264_pps_write( bs_t *s, x264_pps_t *pps )
  178. {
  179. + bs_realign( s );
  180. bs_write_ue( s, pps->i_id );
  181. bs_write_ue( s, pps->i_sps_id );
  182.  
  183. @@ -465,12 +468,14 @@ void x264_pps_write( bs_t *s, x264_pps_t *pps )
  184. }
  185.  
  186. bs_rbsp_trailing( s );
  187. + bs_flush( s );
  188. }
  189.  
  190. void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
  191. {
  192. int payload_size;
  193.  
  194. + bs_realign( s );
  195. bs_write( s, 8, 0x06 ); // payload_type = Recovery Point
  196. payload_size = bs_size_ue( recovery_frame_cnt ) + 4;
  197.  
  198. @@ -482,6 +487,7 @@ void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
  199.  
  200. bs_align_10( s );
  201. bs_rbsp_trailing( s );
  202. + bs_flush( s );
  203. }
  204.  
  205. int x264_sei_version_write( x264_t *h, bs_t *s )
  206. @@ -505,6 +511,7 @@ int x264_sei_version_write( x264_t *h, bs_t *s )
  207. X264_BUILD, X264_VERSION, opts );
  208. length = strlen(version)+1+16;
  209.  
  210. + bs_realign( s );
  211. bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
  212. // payload_size
  213. for( i = 0; i <= length-255; i += 255 )
  214. @@ -517,6 +524,7 @@ int x264_sei_version_write( x264_t *h, bs_t *s )
  215. bs_write( s, 8, version[i] );
  216.  
  217. bs_rbsp_trailing( s );
  218. + bs_flush( s );
  219.  
  220. x264_free( opts );
  221. x264_free( version );
  222. --
  223. 1.6.1.2
  224.  
  225.  
  226. From 00f933f05df722e4b133d2c57f1f45dba285430c Mon Sep 17 00:00:00 2001
  227. From: David Conrad <lessen42@gmail.com>
  228. Date: Sat, 23 Jan 2010 18:05:25 -0800
  229. Subject: [PATCH 03/13] Fix lavf input with pipes and image sequences
  230. x264 should now be able to encode from an image sequence using an image2-style formatted string (e.g. file%02d.jpg).
  231.  
  232. ---
  233. x264.c | 10 ++++------
  234. 1 files changed, 4 insertions(+), 6 deletions(-)
  235.  
  236. diff --git a/x264.c b/x264.c
  237. index 3a07bb3..db33536 100644
  238. --- a/x264.c
  239. +++ b/x264.c
  240. @@ -719,13 +719,11 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
  241. if( b_regular )
  242. {
  243. FILE *f = fopen( filename, "r" );
  244. - if( !f )
  245. + if( f )
  246. {
  247. - fprintf( stderr, "x264 [error]: could not open input file `%s'\n", filename );
  248. - return -1;
  249. + b_regular = x264_is_regular_file( f );
  250. + fclose( f );
  251. }
  252. - b_regular = x264_is_regular_file( f );
  253. - fclose( f );
  254. }
  255. const char *module = b_auto ? ext : demuxer;
  256.  
  257. @@ -756,7 +754,7 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
  258. #endif
  259. #ifdef LAVF_INPUT
  260. if( (b_auto || !strcasecmp( demuxer, "lavf" )) &&
  261. - (!b_regular || !lavf_input.open_file( filename, p_handle, info, opt )) )
  262. + !lavf_input.open_file( filename, p_handle, info, opt ) )
  263. {
  264. module = "lavf";
  265. b_auto = 0;
  266. --
  267. 1.6.1.2
  268.  
  269.  
  270. From 0f985245093047980c1b6148562222265b230dff Mon Sep 17 00:00:00 2001
  271. From: Jason Garrett-Glaser <darkshikari@gmail.com>
  272. Date: Mon, 25 Jan 2010 11:23:55 -0800
  273. Subject: [PATCH 04/13] Hardcode the bs_t in cavlc.c; passing it around is a waste
  274.  
  275. Saves ~1.5kb of code size, very slight speed boost.
  276. ---
  277. encoder/cavlc.c | 143 ++++++++++++++++++++++++++------------------------
  278. encoder/encoder.c | 2 +-
  279. encoder/macroblock.h | 2 +-
  280. encoder/rdo.c | 6 +--
  281. 4 files changed, 79 insertions(+), 74 deletions(-)
  282.  
  283. diff --git a/encoder/cavlc.c b/encoder/cavlc.c
  284. index 59f362a..c65c9bd 100644
  285. --- a/encoder/cavlc.c
  286. +++ b/encoder/cavlc.c
  287. @@ -61,8 +61,9 @@ static const uint8_t sub_mb_type_b_to_golomb[13]=
  288. /****************************************************************************
  289. * block_residual_write_cavlc:
  290. ****************************************************************************/
  291. -static inline int block_residual_write_cavlc_escape( x264_t *h, bs_t *s, int i_suffix_length, int level )
  292. +static inline int block_residual_write_cavlc_escape( x264_t *h, int i_suffix_length, int level )
  293. {
  294. + bs_t *s = &h->out.bs;
  295. static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
  296. int i_level_prefix = 15;
  297. int mask = level >> 15;
  298. @@ -112,8 +113,9 @@ static inline int block_residual_write_cavlc_escape( x264_t *h, bs_t *s, int i_s
  299. return i_suffix_length;
  300. }
  301.  
  302. -static int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int16_t *l, int nC )
  303. +static int block_residual_write_cavlc( x264_t *h, int i_ctxBlockCat, int16_t *l, int nC )
  304. {
  305. + bs_t *s = &h->out.bs;
  306. static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
  307. static const int count_cat[5] = {16, 15, 16, 4, 15};
  308. x264_run_level_t runlevel;
  309. @@ -157,7 +159,7 @@ static int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, in
  310. i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
  311. }
  312. else
  313. - i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
  314. + i_suffix_length = block_residual_write_cavlc_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
  315. for( i = i_trailing+1; i < i_total; i++ )
  316. {
  317. val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
  318. @@ -167,7 +169,7 @@ static int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, in
  319. i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
  320. }
  321. else
  322. - i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
  323. + i_suffix_length = block_residual_write_cavlc_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
  324. }
  325. }
  326.  
  327. @@ -191,18 +193,19 @@ static int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, in
  328.  
  329. static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
  330.  
  331. -#define block_residual_write_cavlc(h,s,cat,idx,l)\
  332. +#define block_residual_write_cavlc(h,cat,idx,l)\
  333. {\
  334. int nC = cat == DCT_CHROMA_DC ? 4 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? 0 : idx )];\
  335. uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
  336. if( !*nnz )\
  337. - bs_write_vlc( s, x264_coeff0_token[nC] );\
  338. + bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
  339. else\
  340. - *nnz = block_residual_write_cavlc(h,s,cat,l,nC);\
  341. + *nnz = block_residual_write_cavlc(h,cat,l,nC);\
  342. }
  343.  
  344. -static void cavlc_qp_delta( x264_t *h, bs_t *s )
  345. +static void cavlc_qp_delta( x264_t *h )
  346. {
  347. + bs_t *s = &h->out.bs;
  348. int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
  349.  
  350. /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
  351. @@ -225,39 +228,40 @@ static void cavlc_qp_delta( x264_t *h, bs_t *s )
  352. bs_write_se( s, i_dqp );
  353. }
  354.  
  355. -static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
  356. +static void cavlc_mb_mvd( x264_t *h, int i_list, int idx, int width )
  357. {
  358. + bs_t *s = &h->out.bs;
  359. ALIGNED_4( int16_t mvp[2] );
  360. x264_mb_predict_mv( h, i_list, idx, width, mvp );
  361. bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
  362. bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
  363. }
  364.  
  365. -static inline void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i )
  366. +static inline void cavlc_mb8x8_mvd( x264_t *h, int i )
  367. {
  368. switch( h->mb.i_sub_partition[i] )
  369. {
  370. case D_L0_8x8:
  371. - cavlc_mb_mvd( h, s, 0, 4*i, 2 );
  372. + cavlc_mb_mvd( h, 0, 4*i, 2 );
  373. break;
  374. case D_L0_8x4:
  375. - cavlc_mb_mvd( h, s, 0, 4*i+0, 2 );
  376. - cavlc_mb_mvd( h, s, 0, 4*i+2, 2 );
  377. + cavlc_mb_mvd( h, 0, 4*i+0, 2 );
  378. + cavlc_mb_mvd( h, 0, 4*i+2, 2 );
  379. break;
  380. case D_L0_4x8:
  381. - cavlc_mb_mvd( h, s, 0, 4*i+0, 1 );
  382. - cavlc_mb_mvd( h, s, 0, 4*i+1, 1 );
  383. + cavlc_mb_mvd( h, 0, 4*i+0, 1 );
  384. + cavlc_mb_mvd( h, 0, 4*i+1, 1 );
  385. break;
  386. case D_L0_4x4:
  387. - cavlc_mb_mvd( h, s, 0, 4*i+0, 1 );
  388. - cavlc_mb_mvd( h, s, 0, 4*i+1, 1 );
  389. - cavlc_mb_mvd( h, s, 0, 4*i+2, 1 );
  390. - cavlc_mb_mvd( h, s, 0, 4*i+3, 1 );
  391. + cavlc_mb_mvd( h, 0, 4*i+0, 1 );
  392. + cavlc_mb_mvd( h, 0, 4*i+1, 1 );
  393. + cavlc_mb_mvd( h, 0, 4*i+2, 1 );
  394. + cavlc_mb_mvd( h, 0, 4*i+3, 1 );
  395. break;
  396. }
  397. }
  398.  
  399. -static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
  400. +static inline void x264_macroblock_luma_write_cavlc( x264_t *h, int i8start, int i8end )
  401. {
  402. int i8, i4;
  403. if( h->mb.b_transform_8x8 )
  404. @@ -271,20 +275,23 @@ static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8s
  405. for( i8 = i8start; i8 <= i8end; i8++ )
  406. if( h->mb.i_cbp_luma & (1 << i8) )
  407. for( i4 = 0; i4 < 4; i4++ )
  408. - block_residual_write_cavlc( h, s, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
  409. + block_residual_write_cavlc( h, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
  410. }
  411.  
  412. /*****************************************************************************
  413. * x264_macroblock_write:
  414. *****************************************************************************/
  415. -void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  416. +void x264_macroblock_write_cavlc( x264_t *h )
  417. {
  418. + bs_t *s = &h->out.bs;
  419. const int i_mb_type = h->mb.i_type;
  420. static const uint8_t i_offsets[3] = {5,23,0};
  421. int i_mb_i_offset = i_offsets[h->sh.i_type];
  422. int i;
  423.  
  424. -#if !RDO_SKIP_BS
  425. +#if RDO_SKIP_BS
  426. + s->i_bits_encoded = 0;
  427. +#else
  428. const int i_mb_pos_start = bs_pos( s );
  429. int i_mb_pos_tex;
  430. #endif
  431. @@ -365,7 +372,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  432.  
  433. if( h->mb.pic.i_fref[0] > 1 )
  434. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  435. - cavlc_mb_mvd( h, s, 0, 0, 4 );
  436. + cavlc_mb_mvd( h, 0, 0, 4 );
  437. }
  438. else if( h->mb.i_partition == D_16x8 )
  439. {
  440. @@ -375,8 +382,8 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  441. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  442. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
  443. }
  444. - cavlc_mb_mvd( h, s, 0, 0, 4 );
  445. - cavlc_mb_mvd( h, s, 0, 8, 4 );
  446. + cavlc_mb_mvd( h, 0, 0, 4 );
  447. + cavlc_mb_mvd( h, 0, 8, 4 );
  448. }
  449. else if( h->mb.i_partition == D_8x16 )
  450. {
  451. @@ -386,8 +393,8 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  452. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  453. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
  454. }
  455. - cavlc_mb_mvd( h, s, 0, 0, 2 );
  456. - cavlc_mb_mvd( h, s, 0, 4, 2 );
  457. + cavlc_mb_mvd( h, 0, 0, 2 );
  458. + cavlc_mb_mvd( h, 0, 4, 2 );
  459. }
  460. }
  461. else if( i_mb_type == P_8x8 )
  462. @@ -422,7 +429,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  463. }
  464.  
  465. for( i = 0; i < 4; i++ )
  466. - cavlc_mb8x8_mvd( h, s, i );
  467. + cavlc_mb8x8_mvd( h, i );
  468. }
  469. else if( i_mb_type == B_8x8 )
  470. {
  471. @@ -445,10 +452,10 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  472. /* mvd */
  473. for( i = 0; i < 4; i++ )
  474. if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
  475. - cavlc_mb_mvd( h, s, 0, 4*i, 2 );
  476. + cavlc_mb_mvd( h, 0, 4*i, 2 );
  477. for( i = 0; i < 4; i++ )
  478. if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
  479. - cavlc_mb_mvd( h, s, 1, 4*i, 2 );
  480. + cavlc_mb_mvd( h, 1, 4*i, 2 );
  481. }
  482. else if( i_mb_type != B_DIRECT )
  483. {
  484. @@ -463,8 +470,8 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  485. {
  486. if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
  487. if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
  488. - if( b_list[0][0] ) cavlc_mb_mvd( h, s, 0, 0, 4 );
  489. - if( b_list[1][0] ) cavlc_mb_mvd( h, s, 1, 0, 4 );
  490. + if( b_list[0][0] ) cavlc_mb_mvd( h, 0, 0, 4 );
  491. + if( b_list[1][0] ) cavlc_mb_mvd( h, 1, 0, 4 );
  492. }
  493. else
  494. {
  495. @@ -474,17 +481,17 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  496. if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[12]] );
  497. if( h->mb.i_partition == D_16x8 )
  498. {
  499. - if( b_list[0][0] ) cavlc_mb_mvd( h, s, 0, 0, 4 );
  500. - if( b_list[0][1] ) cavlc_mb_mvd( h, s, 0, 8, 4 );
  501. - if( b_list[1][0] ) cavlc_mb_mvd( h, s, 1, 0, 4 );
  502. - if( b_list[1][1] ) cavlc_mb_mvd( h, s, 1, 8, 4 );
  503. + if( b_list[0][0] ) cavlc_mb_mvd( h, 0, 0, 4 );
  504. + if( b_list[0][1] ) cavlc_mb_mvd( h, 0, 8, 4 );
  505. + if( b_list[1][0] ) cavlc_mb_mvd( h, 1, 0, 4 );
  506. + if( b_list[1][1] ) cavlc_mb_mvd( h, 1, 8, 4 );
  507. }
  508. else //if( h->mb.i_partition == D_8x16 )
  509. {
  510. - if( b_list[0][0] ) cavlc_mb_mvd( h, s, 0, 0, 2 );
  511. - if( b_list[0][1] ) cavlc_mb_mvd( h, s, 0, 4, 2 );
  512. - if( b_list[1][0] ) cavlc_mb_mvd( h, s, 1, 0, 2 );
  513. - if( b_list[1][1] ) cavlc_mb_mvd( h, s, 1, 4, 2 );
  514. + if( b_list[0][0] ) cavlc_mb_mvd( h, 0, 0, 2 );
  515. + if( b_list[0][1] ) cavlc_mb_mvd( h, 0, 4, 2 );
  516. + if( b_list[1][0] ) cavlc_mb_mvd( h, 1, 0, 2 );
  517. + if( b_list[1][1] ) cavlc_mb_mvd( h, 1, 4, 2 );
  518. }
  519. }
  520. }
  521. @@ -509,29 +516,29 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  522. /* write residual */
  523. if( i_mb_type == I_16x16 )
  524. {
  525. - cavlc_qp_delta( h, s );
  526. + cavlc_qp_delta( h );
  527.  
  528. /* DC Luma */
  529. - block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc );
  530. + block_residual_write_cavlc( h, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc );
  531.  
  532. /* AC Luma */
  533. if( h->mb.i_cbp_luma )
  534. for( i = 0; i < 16; i++ )
  535. - block_residual_write_cavlc( h, s, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
  536. + block_residual_write_cavlc( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
  537. }
  538. else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
  539. {
  540. - cavlc_qp_delta( h, s );
  541. - x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
  542. + cavlc_qp_delta( h );
  543. + x264_macroblock_luma_write_cavlc( h, 0, 3 );
  544. }
  545. if( h->mb.i_cbp_chroma )
  546. {
  547. /* Chroma DC residual present */
  548. - block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
  549. - block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
  550. + block_residual_write_cavlc( h, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
  551. + block_residual_write_cavlc( h, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
  552. if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
  553. for( i = 16; i < 24; i++ )
  554. - block_residual_write_cavlc( h, s, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
  555. + block_residual_write_cavlc( h, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
  556. }
  557.  
  558. #if !RDO_SKIP_BS
  559. @@ -549,36 +556,36 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
  560. *****************************************************************************/
  561. static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
  562. {
  563. + bs_t *s = &h->out.bs;
  564. const int i_mb_type = h->mb.i_type;
  565. int b_8x16 = h->mb.i_partition == D_8x16;
  566. int j;
  567. - h->out.bs.i_bits_encoded = 0;
  568.  
  569. if( i_mb_type == P_8x8 )
  570. {
  571. - cavlc_mb8x8_mvd( h, &h->out.bs, i8 );
  572. - bs_write_ue( &h->out.bs, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
  573. + cavlc_mb8x8_mvd( h, i8 );
  574. + bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
  575. }
  576. else if( i_mb_type == P_L0 )
  577. - cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
  578. + cavlc_mb_mvd( h, 0, 4*i8, 4>>b_8x16 );
  579. else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
  580. {
  581. - if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
  582. - if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 4>>b_8x16 );
  583. + if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, 0, 4*i8, 4>>b_8x16 );
  584. + if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, 1, 4*i8, 4>>b_8x16 );
  585. }
  586. else //if( i_mb_type == B_8x8 )
  587. {
  588. if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
  589. - cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 2 );
  590. + cavlc_mb_mvd( h, 0, 4*i8, 2 );
  591. if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
  592. - cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 2 );
  593. + cavlc_mb_mvd( h, 1, 4*i8, 2 );
  594. }
  595.  
  596. for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
  597. {
  598. - x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
  599. - block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
  600. - block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1 );
  601. + x264_macroblock_luma_write_cavlc( h, i8, i8 );
  602. + block_residual_write_cavlc( h, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
  603. + block_residual_write_cavlc( h, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1 );
  604. i8 += x264_pixel_size[i_pixel].h >> 3;
  605. }
  606.  
  607. @@ -589,12 +596,12 @@ static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
  608. {
  609. int b_8x4 = i_pixel == PIXEL_8x4;
  610. h->out.bs.i_bits_encoded = 0;
  611. - cavlc_mb_mvd( h, &h->out.bs, 0, i4, 1+b_8x4 );
  612. - block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
  613. + cavlc_mb_mvd( h, 0, i4, 1+b_8x4 );
  614. + block_residual_write_cavlc( h, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
  615. if( i_pixel != PIXEL_4x4 )
  616. {
  617. i4 += 2-b_8x4;
  618. - block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
  619. + block_residual_write_cavlc( h, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
  620. }
  621.  
  622. return h->out.bs.i_bits_encoded;
  623. @@ -612,14 +619,14 @@ static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
  624. {
  625. h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
  626. bs_write_ue( &h->out.bs, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
  627. - x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
  628. + x264_macroblock_luma_write_cavlc( h, i8, i8 );
  629. return h->out.bs.i_bits_encoded;
  630. }
  631.  
  632. static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
  633. {
  634. h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
  635. - block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
  636. + block_residual_write_cavlc( h, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
  637. return h->out.bs.i_bits_encoded;
  638. }
  639.  
  640. @@ -628,14 +635,14 @@ static int x264_i8x8_chroma_size_cavlc( x264_t *h )
  641. h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
  642. if( h->mb.i_cbp_chroma )
  643. {
  644. - block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
  645. - block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
  646. + block_residual_write_cavlc( h, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
  647. + block_residual_write_cavlc( h, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
  648.  
  649. if( h->mb.i_cbp_chroma == 2 )
  650. {
  651. int i;
  652. for( i = 16; i < 24; i++ )
  653. - block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
  654. + block_residual_write_cavlc( h, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
  655. }
  656. }
  657. return h->out.bs.i_bits_encoded;
  658. diff --git a/encoder/encoder.c b/encoder/encoder.c
  659. index c7065a2..15b373d 100644
  660. --- a/encoder/encoder.c
  661. +++ b/encoder/encoder.c
  662. @@ -1741,7 +1741,7 @@ static int x264_slice_write( x264_t *h )
  663. bs_write_ue( &h->out.bs, i_skip ); /* skip run */
  664. i_skip = 0;
  665. }
  666. - x264_macroblock_write_cavlc( h, &h->out.bs );
  667. + x264_macroblock_write_cavlc( h );
  668. }
  669. }
  670.  
  671. diff --git a/encoder/macroblock.h b/encoder/macroblock.h
  672. index 24a43b1..25beb18 100644
  673. --- a/encoder/macroblock.h
  674. +++ b/encoder/macroblock.h
  675. @@ -45,7 +45,7 @@ void x264_predict_lossless_16x16( x264_t *h, int i_mode );
  676.  
  677. void x264_macroblock_encode ( x264_t *h );
  678. void x264_macroblock_write_cabac ( x264_t *h, x264_cabac_t *cb );
  679. -void x264_macroblock_write_cavlc ( x264_t *h, bs_t *s );
  680. +void x264_macroblock_write_cavlc ( x264_t *h );
  681.  
  682. void x264_macroblock_encode_p8x8( x264_t *h, int i8 );
  683. void x264_macroblock_encode_p4x4( x264_t *h, int i4 );
  684. diff --git a/encoder/rdo.c b/encoder/rdo.c
  685. index 9dee56d..3ed4a47 100644
  686. --- a/encoder/rdo.c
  687. +++ b/encoder/rdo.c
  688. @@ -159,10 +159,8 @@ static int x264_rd_cost_mb( x264_t *h, int i_lambda2 )
  689. }
  690. else
  691. {
  692. - bs_t bs_tmp = h->out.bs;
  693. - bs_tmp.i_bits_encoded = 0;
  694. - x264_macroblock_size_cavlc( h, &bs_tmp );
  695. - i_bits = ( bs_tmp.i_bits_encoded * i_lambda2 + 128 ) >> 8;
  696. + x264_macroblock_size_cavlc( h );
  697. + i_bits = ( h->out.bs.i_bits_encoded * i_lambda2 + 128 ) >> 8;
  698. }
  699.  
  700. h->mb.b_transform_8x8 = b_transform_bak;
  701. --
  702. 1.6.1.2
  703.  
  704.  
  705. From 85056bb1c99a38a9a7c0a7642eb0ee03294a29c3 Mon Sep 17 00:00:00 2001
  706. From: Anton Mitrofanov <BugMaster@narod.ru>
  707. Date: Tue, 26 Jan 2010 11:41:18 -0800
  708. Subject: [PATCH 05/13] Various threading-related cosmetics
  709. Simplify a lot of code and remove some unnecessary variables.
  710.  
  711. ---
  712. common/common.h | 14 +++------
  713. encoder/analyse.c | 12 ++++----
  714. encoder/encoder.c | 68 ++++++++++++++++++++++++------------------------
  715. encoder/ratecontrol.c | 22 ++++++++--------
  716. 4 files changed, 56 insertions(+), 60 deletions(-)
  717.  
  718. diff --git a/common/common.h b/common/common.h
  719. index df39f26..0f16e0a 100644
  720. --- a/common/common.h
  721. +++ b/common/common.h
  722. @@ -362,16 +362,12 @@ struct x264_t
  723.  
  724. /* frame number/poc */
  725. int i_frame;
  726. + int i_frame_num;
  727.  
  728. - int i_frame_offset; /* decoding only */
  729. - int i_frame_num; /* decoding only */
  730. - int i_poc_msb; /* decoding only */
  731. - int i_poc_lsb; /* decoding only */
  732. - int i_poc; /* decoding only */
  733. -
  734. - int i_thread_num; /* threads only */
  735. - int i_nal_type; /* threads only */
  736. - int i_nal_ref_idc; /* threads only */
  737. + int i_thread_frames; /* Number of different frames being encoded by threads;
  738. + * 1 when sliced-threads is on. */
  739. + int i_nal_type;
  740. + int i_nal_ref_idc;
  741.  
  742. /* We use only one SPS and one PPS */
  743. x264_sps_t sps_array[1];
  744. diff --git a/encoder/analyse.c b/encoder/analyse.c
  745. index 37d7fd9..666596b 100644
  746. --- a/encoder/analyse.c
  747. +++ b/encoder/analyse.c
  748. @@ -413,7 +413,7 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
  749. int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
  750. int thread_mvy_range = i_fmv_range;
  751.  
  752. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  753. + if( h->i_thread_frames > 1 )
  754. {
  755. int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
  756. int thresh = pix_y + h->param.analyse.i_mv_range_thread;
  757. @@ -1167,7 +1167,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
  758. {
  759. h->mb.i_type = P_SKIP;
  760. x264_analyse_update_cache( h, a );
  761. - assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 || h->param.b_sliced_threads );
  762. + assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
  763. return;
  764. }
  765.  
  766. @@ -1183,7 +1183,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
  767. }
  768.  
  769. x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
  770. - assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 || h->param.b_sliced_threads );
  771. + assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
  772.  
  773. h->mb.i_type = P_L0;
  774. if( a->i_mbrd )
  775. @@ -2403,7 +2403,7 @@ intra_analysis:
  776. /* Fast P_SKIP detection */
  777. if( h->param.analyse.b_fast_pskip )
  778. {
  779. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
  780. + if( h->i_thread_frames > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
  781. // FIXME don't need to check this if the reference frame is done
  782. {}
  783. else if( h->param.analyse.i_subpel_refine >= 3 )
  784. @@ -2422,7 +2422,7 @@ intra_analysis:
  785. {
  786. h->mb.i_type = P_SKIP;
  787. h->mb.i_partition = D_16x16;
  788. - assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 || h->param.b_sliced_threads );
  789. + assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
  790. }
  791. else
  792. {
  793. @@ -3143,7 +3143,7 @@ static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
  794. }
  795.  
  796. #ifndef NDEBUG
  797. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads && !IS_INTRA(h->mb.i_type) )
  798. + if( h->i_thread_frames > 1 && !IS_INTRA(h->mb.i_type) )
  799. {
  800. int l;
  801. for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
  802. diff --git a/encoder/encoder.c b/encoder/encoder.c
  803. index 15b373d..570f1d0 100644
  804. --- a/encoder/encoder.c
  805. +++ b/encoder/encoder.c
  806. @@ -421,6 +421,7 @@ static int x264_validate_parameters( x264_t *h )
  807. }
  808. else
  809. h->param.b_sliced_threads = 0;
  810. + h->i_thread_frames = h->param.b_sliced_threads ? 1 : h->param.i_threads;
  811.  
  812. if( h->param.b_interlaced )
  813. {
  814. @@ -589,8 +590,8 @@ static int x264_validate_parameters( x264_t *h )
  815. h->param.rc.i_lookahead = 0;
  816. #ifdef HAVE_PTHREAD
  817. if( h->param.i_sync_lookahead )
  818. - h->param.i_sync_lookahead = x264_clip3( h->param.i_sync_lookahead, h->param.i_threads + h->param.i_bframe, X264_LOOKAHEAD_MAX );
  819. - if( h->param.rc.b_stat_read || h->param.i_threads == 1 || h->param.b_sliced_threads )
  820. + h->param.i_sync_lookahead = x264_clip3( h->param.i_sync_lookahead, h->i_thread_frames + h->param.i_bframe, X264_LOOKAHEAD_MAX );
  821. + if( h->param.rc.b_stat_read || h->i_thread_frames == 1 )
  822. h->param.i_sync_lookahead = 0;
  823. #else
  824. h->param.i_sync_lookahead = 0;
  825. @@ -708,7 +709,7 @@ static int x264_validate_parameters( x264_t *h )
  826. if( !h->param.analyse.i_weighted_pred && h->param.rc.b_mb_tree && h->param.analyse.b_psy && !h->param.b_interlaced )
  827. h->param.analyse.i_weighted_pred = X264_WEIGHTP_FAKE;
  828.  
  829. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  830. + if( h->i_thread_frames > 1 )
  831. {
  832. int r = h->param.analyse.i_mv_range_thread;
  833. int r2;
  834. @@ -718,7 +719,7 @@ static int x264_validate_parameters( x264_t *h )
  835. // the rest is allocated to whichever thread is far enough ahead to use it.
  836. // reserving more space increases quality for some videos, but costs more time
  837. // in thread synchronization.
  838. - int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->param.i_threads - X264_THREAD_HEIGHT;
  839. + int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->i_thread_frames - X264_THREAD_HEIGHT;
  840. r = max_range / 2;
  841. }
  842. r = X264_MAX( r, h->param.analyse.i_me_range );
  843. @@ -886,8 +887,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
  844. if( h->param.rc.b_mb_tree || h->param.rc.i_vbv_buffer_size )
  845. h->frames.i_delay = X264_MAX( h->frames.i_delay, h->param.rc.i_lookahead );
  846. i_slicetype_length = h->frames.i_delay;
  847. - if( !h->param.b_sliced_threads )
  848. - h->frames.i_delay += h->param.i_threads - 1;
  849. + h->frames.i_delay += h->i_thread_frames - 1;
  850. h->frames.i_delay = X264_MIN( h->frames.i_delay, X264_LOOKAHEAD_MAX );
  851. h->frames.i_delay += h->param.i_sync_lookahead;
  852. h->frames.i_bframe_delay = h->param.i_bframe ? (h->param.i_bframe_pyramid ? 2 : 1) : 0;
  853. @@ -910,11 +910,11 @@ x264_t *x264_encoder_open( x264_param_t *param )
  854.  
  855. CHECKED_MALLOCZERO( h->frames.unused[0], (h->frames.i_delay + 3) * sizeof(x264_frame_t *) );
  856. /* Allocate room for max refs plus a few extra just in case. */
  857. - CHECKED_MALLOCZERO( h->frames.unused[1], (h->param.i_threads + 20) * sizeof(x264_frame_t *) );
  858. + CHECKED_MALLOCZERO( h->frames.unused[1], (h->i_thread_frames + 20) * sizeof(x264_frame_t *) );
  859. CHECKED_MALLOCZERO( h->frames.current, (h->param.i_sync_lookahead + h->param.i_bframe
  860. - + h->param.i_threads + 3) * sizeof(x264_frame_t *) );
  861. + + h->i_thread_frames + 3) * sizeof(x264_frame_t *) );
  862. if( h->param.analyse.i_weighted_pred > 0 )
  863. - CHECKED_MALLOCZERO( h->frames.blank_unused, h->param.i_threads * 4 * sizeof(x264_frame_t *) );
  864. + CHECKED_MALLOCZERO( h->frames.blank_unused, h->i_thread_frames * 4 * sizeof(x264_frame_t *) );
  865. h->i_ref0 = 0;
  866. h->i_ref1 = 0;
  867.  
  868. @@ -977,7 +977,6 @@ x264_t *x264_encoder_open( x264_param_t *param )
  869. h->nal_buffer_size = h->out.i_bitstream * 3/2 + 4;
  870.  
  871. h->thread[0] = h;
  872. - h->i_thread_num = 0;
  873. for( i = 1; i < h->param.i_threads + !!h->param.i_sync_lookahead; i++ )
  874. CHECKED_MALLOC( h->thread[i], sizeof(x264_t) );
  875.  
  876. @@ -1501,7 +1500,7 @@ static void x264_fdec_filter_row( x264_t *h, int mb_y )
  877. }
  878. }
  879.  
  880. - if( h->param.i_threads > 1 && h->fdec->b_kept_as_ref && !h->param.b_sliced_threads )
  881. + if( h->i_thread_frames > 1 && h->fdec->b_kept_as_ref )
  882. x264_frame_cond_broadcast( h->fdec, mb_y*16 + (b_end ? 10000 : -(X264_THREAD_HEIGHT << h->sh.b_mbaff)) );
  883.  
  884. min_y = X264_MAX( min_y*16-8, 0 );
  885. @@ -1537,7 +1536,7 @@ static inline int x264_reference_update( x264_t *h )
  886. int i, j;
  887. if( !h->fdec->b_kept_as_ref )
  888. {
  889. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  890. + if( h->i_thread_frames > 1 )
  891. {
  892. x264_frame_push_unused( h, h->fdec );
  893. h->fdec = x264_frame_pop_unused( h, 1 );
  894. @@ -2036,12 +2035,12 @@ int x264_encoder_encode( x264_t *h,
  895. x264_t *thread_current, *thread_prev, *thread_oldest;
  896. int i_nal_type, i_nal_ref_idc, i_global_qp, i;
  897.  
  898. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  899. + if( h->i_thread_frames > 1 )
  900. {
  901. thread_prev = h->thread[ h->i_thread_phase ];
  902. - h->i_thread_phase = (h->i_thread_phase + 1) % h->param.i_threads;
  903. + h->i_thread_phase = (h->i_thread_phase + 1) % h->i_thread_frames;
  904. thread_current = h->thread[ h->i_thread_phase ];
  905. - thread_oldest = h->thread[ (h->i_thread_phase + 1) % h->param.i_threads ];
  906. + thread_oldest = h->thread[ (h->i_thread_phase + 1) % h->i_thread_frames ];
  907. x264_thread_sync_context( thread_current, thread_prev );
  908. x264_thread_sync_ratecontrol( thread_current, thread_prev, thread_oldest );
  909. h = thread_current;
  910. @@ -2100,7 +2099,7 @@ int x264_encoder_encode( x264_t *h,
  911. /* 2: Place the frame into the queue for its slice type decision */
  912. x264_lookahead_put_frame( h, fenc );
  913.  
  914. - if( h->frames.i_input <= h->frames.i_delay + (h->param.b_sliced_threads ? 0 : 1 - h->param.i_threads) )
  915. + if( h->frames.i_input <= h->frames.i_delay + 1 - h->i_thread_frames )
  916. {
  917. /* Nothing yet to encode, waiting for filling of buffers */
  918. pic_out->i_type = X264_TYPE_AUTO;
  919. @@ -2327,7 +2326,7 @@ int x264_encoder_encode( x264_t *h,
  920. /* Write frame */
  921. h->i_threadslice_start = 0;
  922. h->i_threadslice_end = h->sps->i_mb_height;
  923. - if( !h->param.b_sliced_threads && h->param.i_threads > 1 )
  924. + if( h->i_thread_frames > 1 )
  925. {
  926. if( x264_pthread_create( &h->thread_handle, NULL, (void*)x264_slices_write, h ) )
  927. return -1;
  928. @@ -2564,25 +2563,23 @@ void x264_encoder_close ( x264_t *h )
  929.  
  930. x264_lookahead_delete( h );
  931.  
  932. - for( i = 0; i < h->param.i_threads; i++ )
  933. + if( h->i_thread_frames > 1 )
  934. {
  935. - // don't strictly have to wait for the other threads, but it's simpler than canceling them
  936. - if( h->thread[i]->b_thread_active )
  937. + for( i = 0; i < h->i_thread_frames; i++ )
  938. {
  939. - x264_pthread_join( h->thread[i]->thread_handle, NULL );
  940. - assert( h->thread[i]->fenc->i_reference_count == 1 );
  941. - x264_frame_delete( h->thread[i]->fenc );
  942. + // don't strictly have to wait for the other threads, but it's simpler than canceling them
  943. + if( h->thread[i]->b_thread_active )
  944. + {
  945. + x264_pthread_join( h->thread[i]->thread_handle, NULL );
  946. + assert( h->thread[i]->fenc->i_reference_count == 1 );
  947. + x264_frame_delete( h->thread[i]->fenc );
  948. + }
  949. }
  950. - }
  951. -
  952. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  953. - {
  954. - x264_t *thread_prev;
  955.  
  956. - thread_prev = h->thread[h->i_thread_phase];
  957. + x264_t *thread_prev = h->thread[h->i_thread_phase];
  958. x264_thread_sync_ratecontrol( h, thread_prev, h );
  959. x264_thread_sync_ratecontrol( thread_prev, thread_prev, h );
  960. - h->i_frame = thread_prev->i_frame + 1 - h->param.i_threads;
  961. + h->i_frame = thread_prev->i_frame + 1 - h->i_thread_frames;
  962. }
  963. h->i_frame++;
  964.  
  965. @@ -2833,7 +2830,7 @@ void x264_encoder_close ( x264_t *h )
  966. x264_free( h->nal_buffer );
  967. x264_analyse_free_costs( h );
  968.  
  969. - if( h->param.i_threads > 1)
  970. + if( h->i_thread_frames > 1)
  971. h = h->thread[h->i_thread_phase];
  972.  
  973. /* frames */
  974. @@ -2878,9 +2875,12 @@ int x264_encoder_delayed_frames( x264_t *h )
  975. {
  976. int delayed_frames = 0;
  977. int i;
  978. - for( i=0; i<h->param.i_threads; i++ )
  979. - delayed_frames += h->thread[i]->b_thread_active;
  980. - h = h->thread[h->i_thread_phase];
  981. + if( h->i_thread_frames > 1 )
  982. + {
  983. + for( i=0; i<h->i_thread_frames; i++ )
  984. + delayed_frames += h->thread[i]->b_thread_active;
  985. + h = h->thread[h->i_thread_phase];
  986. + }
  987. for( i=0; h->frames.current[i]; i++ )
  988. delayed_frames++;
  989. x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  990. diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
  991. index 761ff2c..746b17a 100644
  992. --- a/encoder/ratecontrol.c
  993. +++ b/encoder/ratecontrol.c
  994. @@ -1578,13 +1578,13 @@ static void update_vbv_plan( x264_t *h, int overhead )
  995. {
  996. x264_ratecontrol_t *rcc = h->rc;
  997. rcc->buffer_fill = h->thread[0]->rc->buffer_fill_final - overhead;
  998. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  999. + if( h->i_thread_frames > 1 )
  1000. {
  1001. int j = h->rc - h->thread[0]->rc;
  1002. int i;
  1003. - for( i=1; i<h->param.i_threads; i++ )
  1004. + for( i=1; i<h->i_thread_frames; i++ )
  1005. {
  1006. - x264_t *t = h->thread[ (j+i)%h->param.i_threads ];
  1007. + x264_t *t = h->thread[ (j+i)%h->i_thread_frames ];
  1008. double bits = t->rc->frame_size_planned;
  1009. if( !t->b_thread_active )
  1010. continue;
  1011. @@ -1794,7 +1794,7 @@ static float rate_estimate_qscale( x264_t *h )
  1012. }
  1013. else
  1014. {
  1015. - double abr_buffer = 2 * rcc->rate_tolerance * rcc->bitrate * (h->param.b_sliced_threads?1:h->param.i_threads);
  1016. + double abr_buffer = 2 * rcc->rate_tolerance * rcc->bitrate * h->i_thread_frames;
  1017.  
  1018. if( rcc->b_2pass )
  1019. {
  1020. @@ -1804,13 +1804,13 @@ static float rate_estimate_qscale( x264_t *h )
  1021.  
  1022. if( rcc->b_vbv )
  1023. {
  1024. - if( h->param.i_threads > 1 && !h->param.b_sliced_threads )
  1025. + if( h->i_thread_frames > 1 )
  1026. {
  1027. int j = h->rc - h->thread[0]->rc;
  1028. int i;
  1029. - for( i=1; i<h->param.i_threads; i++ )
  1030. + for( i=1; i<h->i_thread_frames; i++ )
  1031. {
  1032. - x264_t *t = h->thread[ (j+i)%h->param.i_threads ];
  1033. + x264_t *t = h->thread[ (j+i)%h->i_thread_frames ];
  1034. double bits = t->rc->frame_size_planned;
  1035. if( !t->b_thread_active )
  1036. continue;
  1037. @@ -1821,16 +1821,16 @@ static float rate_estimate_qscale( x264_t *h )
  1038. }
  1039. else
  1040. {
  1041. - if( h->fenc->i_frame < h->param.i_threads )
  1042. + if( h->fenc->i_frame < h->i_thread_frames )
  1043. predicted_bits += (int64_t)h->fenc->i_frame * rcc->bitrate / rcc->fps;
  1044. else
  1045. - predicted_bits += (int64_t)(h->param.i_threads - 1) * rcc->bitrate / rcc->fps;
  1046. + predicted_bits += (int64_t)(h->i_thread_frames - 1) * rcc->bitrate / rcc->fps;
  1047. }
  1048.  
  1049. diff = predicted_bits - (int64_t)rce.expected_bits;
  1050. q = rce.new_qscale;
  1051. q /= x264_clip3f((double)(abr_buffer - diff) / abr_buffer, .5, 2);
  1052. - if( ((h->fenc->i_frame + 1 - h->param.i_threads) >= rcc->fps) &&
  1053. + if( ((h->fenc->i_frame + 1 - h->i_thread_frames) >= rcc->fps) &&
  1054. (rcc->expected_bits_sum > 0))
  1055. {
  1056. /* Adjust quant based on the difference between
  1057. @@ -1897,7 +1897,7 @@ static float rate_estimate_qscale( x264_t *h )
  1058. }
  1059. else
  1060. {
  1061. - int i_frame_done = h->fenc->i_frame + 1 - h->param.i_threads;
  1062. + int i_frame_done = h->fenc->i_frame + 1 - h->i_thread_frames;
  1063.  
  1064. q = get_qscale( h, &rce, rcc->wanted_bits_window / rcc->cplxr_sum, h->fenc->i_frame );
  1065.  
  1066. --
  1067. 1.6.1.2
  1068.  
  1069.  
  1070. From 6d2cc932df587a109aac8c0b5a9370fe0a326baa Mon Sep 17 00:00:00 2001
  1071. From: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
  1072. Date: Tue, 26 Jan 2010 16:01:54 -0800
  1073. Subject: [PATCH 06/13] Improve DTS generation, move DTS compression into libx264
  1074. This change fixes some cases in which PTS could be less than DTS.
  1075.  
  1076. Additionally, a new parameter, b_dts_compress, enables DTS compression.
  1077. DTS compression eliminates negative DTS (i.e. initial delay) due to B-frames.
  1078. The algorithm changes timebase in order to avoid duplicating DTS.
  1079. Currently, in x264cli, only the FLV muxer uses it. The MP4 muxer doesn't need it, as it uses an EditBox instead.
  1080. ---
  1081. common/common.c | 1 +
  1082. common/common.h | 5 ++++
  1083. encoder/encoder.c | 40 ++++++++++++++++++++++++++++++++++-
  1084. output/flv.c | 58 +++++++++++++++++-----------------------------------
  1085. output/mp4.c | 28 ++----------------------
  1086. x264.c | 6 ++++-
  1087. x264.h | 5 +++-
  1088. 7 files changed, 75 insertions(+), 68 deletions(-)
  1089.  
  1090. diff --git a/common/common.c b/common/common.c
  1091. index 9eed5c3..b454e37 100644
  1092. --- a/common/common.c
  1093. +++ b/common/common.c
  1094. @@ -157,6 +157,7 @@ void x264_param_default( x264_param_t *param )
  1095. param->b_annexb = 1;
  1096. param->b_aud = 0;
  1097. param->b_vfr_input = 1;
  1098. + param->b_dts_compress = 0;
  1099. }
  1100.  
  1101. static int parse_enum( const char *arg, const char * const *names, int *dst )
  1102. diff --git a/common/common.h b/common/common.h
  1103. index 0f16e0a..ca15330 100644
  1104. --- a/common/common.h
  1105. +++ b/common/common.h
  1106. @@ -376,6 +376,9 @@ struct x264_t
  1107. x264_pps_t *pps;
  1108. int i_idr_pic_id;
  1109.  
  1110. + /* Timebase multiplier for DTS compression */
  1111. + int i_dts_compress_multiplier;
  1112. +
  1113. /* quantization matrix for decoding, [cqm][qp%6][coef] */
  1114. int (*dequant4_mf[4])[16]; /* [4][6][16] */
  1115. int (*dequant8_mf[2])[64]; /* [2][6][64] */
  1116. @@ -429,6 +432,8 @@ struct x264_t
  1117. int i_delay; /* Number of frames buffered for B reordering */
  1118. int i_bframe_delay;
  1119. int64_t i_bframe_delay_time;
  1120. + int64_t i_init_delta;
  1121. + int64_t i_prev_dts[2];
  1122. int b_have_lowres; /* Whether 1/2 resolution luma planes are being used */
  1123. int b_have_sub8x8_esa;
  1124. } frames;
  1125. diff --git a/encoder/encoder.c b/encoder/encoder.c
  1126. index 570f1d0..9524c10 100644
  1127. --- a/encoder/encoder.c
  1128. +++ b/encoder/encoder.c
  1129. @@ -863,6 +863,18 @@ x264_t *x264_encoder_open( x264_param_t *param )
  1130. h->i_frame = -1;
  1131. h->i_frame_num = 0;
  1132. h->i_idr_pic_id = 0;
  1133. + if( h->param.b_dts_compress )
  1134. + {
  1135. + /* h->i_dts_compress_multiplier == h->frames.i_bframe_delay + 1 */
  1136. + h->i_dts_compress_multiplier = h->param.i_bframe ? (h->param.i_bframe_pyramid ? 3 : 2) : 1;
  1137. + if( h->i_dts_compress_multiplier != 1 )
  1138. + x264_log( h, X264_LOG_DEBUG, "DTS compresion changed timebase: %d/%d -> %d/%d\n",
  1139. + h->param.i_timebase_num, h->param.i_timebase_den,
  1140. + h->param.i_timebase_num, h->param.i_timebase_den * h->i_dts_compress_multiplier );
  1141. + h->param.i_timebase_den *= h->i_dts_compress_multiplier;
  1142. + }
  1143. + else
  1144. + h->i_dts_compress_multiplier = 1;
  1145.  
  1146. h->sps = &h->sps_array[0];
  1147. x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
  1148. @@ -2384,8 +2396,32 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
  1149. pic_out->i_type = X264_TYPE_B;
  1150.  
  1151. pic_out->b_keyframe = h->fenc->b_keyframe;
  1152. - pic_out->i_pts = h->fenc->i_pts;
  1153. - pic_out->i_dts = h->fenc->i_dts - h->frames.i_bframe_delay_time;
  1154. +
  1155. + pic_out->i_pts = h->fenc->i_pts *= h->i_dts_compress_multiplier;
  1156. + if( h->frames.i_bframe_delay )
  1157. + {
  1158. + int64_t *i_prev_dts = thread_current->frames.i_prev_dts;
  1159. + if( h->i_frame <= h->frames.i_bframe_delay )
  1160. + {
  1161. + if( h->i_dts_compress_multiplier == 1 )
  1162. + pic_out->i_dts = h->fenc->i_dts - h->frames.i_bframe_delay_time;
  1163. + else
  1164. + {
  1165. + /* DTS compression */
  1166. + if( h->i_frame == 1 )
  1167. + thread_current->frames.i_init_delta = h->fenc->i_dts * h->i_dts_compress_multiplier;
  1168. + pic_out->i_dts = h->i_frame * thread_current->frames.i_init_delta / h->i_dts_compress_multiplier;
  1169. + }
  1170. + }
  1171. + else
  1172. + pic_out->i_dts = i_prev_dts[ (h->i_frame - h->frames.i_bframe_delay) % h->frames.i_bframe_delay ];
  1173. + i_prev_dts[ h->i_frame % h->frames.i_bframe_delay ] = h->fenc->i_dts * h->i_dts_compress_multiplier;
  1174. + h->fenc->i_dts = pic_out->i_dts;
  1175. + }
  1176. + else
  1177. + pic_out->i_dts = h->fenc->i_dts;
  1178. + assert( pic_out->i_pts >= pic_out->i_dts );
  1179. +
  1180. pic_out->img.i_plane = h->fdec->i_plane;
  1181. for(i = 0; i < 3; i++)
  1182. {
  1183. diff --git a/output/flv.c b/output/flv.c
  1184. index 8a937cf..5ef5b0f 100644
  1185. --- a/output/flv.c
  1186. +++ b/output/flv.c
  1187. @@ -37,8 +37,6 @@ typedef struct
  1188. int64_t i_fps_num;
  1189. int64_t i_fps_den;
  1190. int64_t i_framenum;
  1191. - int i_init_delay;
  1192. - int i_delay_time;
  1193.  
  1194. uint64_t i_framerate_pos;
  1195. uint64_t i_duration_pos;
  1196. @@ -46,8 +44,8 @@ typedef struct
  1197. uint64_t i_bitrate_pos;
  1198.  
  1199. uint8_t b_write_length;
  1200. - int64_t i_init_delta;
  1201. - int64_t i_prev_timestamps[2];
  1202. + int64_t i_prev_dts;
  1203. + int64_t i_prev_pts;
  1204.  
  1205. int i_timebase_num;
  1206. int i_timebase_den;
  1207. @@ -146,10 +144,8 @@ static int set_param( hnd_t handle, x264_param_t *p_param )
  1208. p_flv->i_fps_den = p_param->i_fps_den;
  1209. p_flv->i_timebase_num = p_param->i_timebase_num;
  1210. p_flv->i_timebase_den = p_param->i_timebase_den;
  1211. - p_flv->i_init_delay = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
  1212. p_flv->b_vfr_input = p_param->b_vfr_input;
  1213.  
  1214. -
  1215. return 0;
  1216. }
  1217.  
  1218. @@ -216,45 +212,29 @@ static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_
  1219. flv_hnd_t *p_flv = handle;
  1220. flv_buffer *c = p_flv->c;
  1221.  
  1222. - int64_t dts;
  1223. - int64_t cts;
  1224. - int64_t offset;
  1225. -
  1226. - if( !p_flv->i_framenum )
  1227. - p_flv->i_delay_time = p_picture->i_dts;
  1228. + int64_t dts = (int64_t)( (p_picture->i_dts * 1000 * ((double)p_flv->i_timebase_num / p_flv->i_timebase_den)) + 0.5 );
  1229. + int64_t cts = (int64_t)( (p_picture->i_pts * 1000 * ((double)p_flv->i_timebase_num / p_flv->i_timebase_den)) + 0.5 );
  1230. + int64_t offset = cts - dts;
  1231.  
  1232. - if( !p_flv->i_init_delay )
  1233. - dts = cts = (int64_t)((p_picture->i_pts * 1000 * p_flv->i_timebase_num / p_flv->i_timebase_den) + 0.5);
  1234. - else
  1235. + if( p_flv->i_framenum )
  1236. {
  1237. - // Use DTS compression
  1238. - dts = p_picture->i_dts - p_flv->i_delay_time;
  1239. -
  1240. - if( p_flv->i_framenum == 1 )
  1241. - p_flv->i_init_delta = p_picture->i_dts - p_flv->i_delay_time;
  1242. -
  1243. - if( p_flv->i_framenum > p_flv->i_init_delay )
  1244. + int64_t prev_dts = (int64_t)( (p_flv->i_prev_dts * 1000 * ((double)p_flv->i_timebase_num / p_flv->i_timebase_den)) + 0.5 );
  1245. + int64_t prev_cts = (int64_t)( (p_flv->i_prev_pts * 1000 * ((double)p_flv->i_timebase_num / p_flv->i_timebase_den)) + 0.5 );
  1246. + if( prev_dts == dts )
  1247. {
  1248. - dts = p_flv->i_prev_timestamps[ (p_flv->i_framenum - p_flv->i_init_delay) % p_flv->i_init_delay ];
  1249. - dts = (int64_t)((dts * 1000 * p_flv->i_timebase_num / p_flv->i_timebase_den) + 0.5);
  1250. + double fps = ((double)p_flv->i_timebase_den / p_flv->i_timebase_num) / (p_picture->i_dts - p_flv->i_prev_dts);
  1251. + fprintf( stderr, "flv [warning]: duplicate DTS %"PRId64" generated by rounding\n"
  1252. + " current internal decoding framerate: %.6f fps\n", dts, fps );
  1253. }
  1254. - else if( p_flv->i_init_delta )
  1255. + if( prev_cts == cts )
  1256. {
  1257. - // Compressed DTSs might not fit in input timescale
  1258. - double compressed_dts;
  1259. - compressed_dts = (p_flv->i_framenum * ((double)p_flv->i_init_delta / (2 * p_flv->i_init_delay)));
  1260. - dts = (int64_t)((compressed_dts * 1000 * p_flv->i_timebase_num / p_flv->i_timebase_den) + 0.5);
  1261. + double fps = ((double)p_flv->i_timebase_den / p_flv->i_timebase_num) / (p_picture->i_pts - p_flv->i_prev_pts);
  1262. + fprintf( stderr, "flv [warning]: duplicating CTS %"PRId64" is generated by rounding\n"
  1263. + " current internal composition framerate: %.6f fps\n", cts, fps );
  1264. }
  1265. -
  1266. - p_flv->i_prev_timestamps[ p_flv->i_framenum % p_flv->i_init_delay ] = p_picture->i_dts - p_flv->i_delay_time;
  1267. -
  1268. - cts = p_picture->i_pts;
  1269. - cts = (int64_t)((cts * 1000 * p_flv->i_timebase_num / p_flv->i_timebase_den) + 0.5);
  1270. - }
  1271. -
  1272. - offset = cts - dts;
  1273. -
  1274. - assert( cts >= dts );
  1275. + }
  1276. + p_flv->i_prev_dts = p_picture->i_dts;
  1277. + p_flv->i_prev_pts = p_picture->i_pts;
  1278.  
  1279. // A new frame - write packet header
  1280. x264_put_byte( c, FLV_TAG_TYPE_VIDEO );
  1281. diff --git a/output/mp4.c b/output/mp4.c
  1282. index 7889e4f..e3ad9c6 100644
  1283. --- a/output/mp4.c
  1284. +++ b/output/mp4.c
  1285. @@ -34,11 +34,7 @@ typedef struct
  1286. int i_time_res;
  1287. int64_t i_time_inc;
  1288. int i_numframe;
  1289. - int i_init_delay;
  1290. int i_delay_time;
  1291. -
  1292. - int64_t i_prev_timestamps[2];
  1293. - int64_t i_init_delta;
  1294. } mp4_hnd_t;
  1295.  
  1296. static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
  1297. @@ -195,8 +191,6 @@ static int set_param( hnd_t handle, x264_param_t *p_param )
  1298. p_mp4->i_time_res = p_param->i_timebase_den;
  1299. p_mp4->i_time_inc = p_param->i_timebase_num;
  1300.  
  1301. - p_mp4->i_init_delay = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
  1302. -
  1303. p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
  1304. p_mp4->i_time_res );
  1305.  
  1306. @@ -282,7 +276,6 @@ static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_
  1307. mp4_hnd_t *p_mp4 = handle;
  1308. int64_t dts;
  1309. int64_t cts;
  1310. - int32_t offset = 0;
  1311.  
  1312. memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
  1313. p_mp4->p_sample->dataLength += i_size;
  1314. @@ -290,27 +283,12 @@ static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_
  1315. if( !p_mp4->i_numframe )
  1316. p_mp4->i_delay_time = p_picture->i_dts * -1;
  1317.  
  1318. - if( !p_mp4->i_init_delay )
  1319. - dts = cts = p_picture->i_pts * p_mp4->i_time_inc;
  1320. - else
  1321. - {
  1322. - if( p_mp4->i_numframe <= p_mp4->i_init_delay )
  1323. - dts = p_picture->i_dts + p_mp4->i_delay_time;
  1324. - else
  1325. - dts = p_mp4->i_prev_timestamps[ (p_mp4->i_numframe - p_mp4->i_init_delay) % p_mp4->i_init_delay ] + p_mp4->i_delay_time;
  1326. -
  1327. - // unordered pts
  1328. - p_mp4->i_prev_timestamps[ p_mp4->i_numframe % p_mp4->i_init_delay ] = p_picture->i_dts + p_mp4->i_delay_time;
  1329. -
  1330. - dts *= p_mp4->i_time_inc;
  1331. - cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
  1332. -
  1333. - offset = cts - dts;
  1334. - }
  1335. + dts = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
  1336. + cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
  1337.  
  1338. p_mp4->p_sample->IsRAP = p_picture->b_keyframe;
  1339. p_mp4->p_sample->DTS = dts;
  1340. - p_mp4->p_sample->CTS_Offset = offset;
  1341. + p_mp4->p_sample->CTS_Offset = (uint32_t)(cts - dts);
  1342. gf_isom_add_sample( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample );
  1343.  
  1344. p_mp4->p_sample->dataLength = 0;
  1345. diff --git a/x264.c b/x264.c
  1346. index db33536..d77fa47 100644
  1347. --- a/x264.c
  1348. +++ b/x264.c
  1349. @@ -683,6 +683,7 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
  1350. output = mp4_output;
  1351. param->b_annexb = 0;
  1352. param->b_aud = 0;
  1353. + param->b_dts_compress = 0;
  1354. param->b_repeat_headers = 0;
  1355. #else
  1356. fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
  1357. @@ -694,6 +695,7 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
  1358. output = mkv_output;
  1359. param->b_annexb = 0;
  1360. param->b_aud = 0;
  1361. + param->b_dts_compress = 0;
  1362. param->b_repeat_headers = 0;
  1363. }
  1364. else if( !strcasecmp( ext, "flv" ) )
  1365. @@ -701,6 +703,7 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
  1366. output = flv_output;
  1367. param->b_annexb = 0;
  1368. param->b_aud = 0;
  1369. + param->b_dts_compress = 1;
  1370. param->b_repeat_headers = 0;
  1371. }
  1372. else
  1373. @@ -1528,7 +1531,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1374. {
  1375. if( h->param.i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
  1376. fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
  1377. - i_frame, pic.i_pts, largest_pts );
  1378. + i_frame, pic.i_pts * h->i_dts_compress_multiplier, largest_pts * h->i_dts_compress_multiplier );
  1379. else if( pts_warning_cnt == MAX_PTS_WARNING )
  1380. fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
  1381. pts_warning_cnt++;
  1382. @@ -1583,6 +1586,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
  1383. duration = (double)param->i_fps_den / param->i_fps_num;
  1384. else
  1385. duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
  1386. + duration *= h->i_dts_compress_multiplier;
  1387.  
  1388. i_end = x264_mdate();
  1389. input.picture_clean( &pic );
  1390. diff --git a/x264.h b/x264.h
  1391. index 1223df7..2550864 100644
  1392. --- a/x264.h
  1393. +++ b/x264.h
  1394. @@ -35,7 +35,7 @@
  1395.  
  1396. #include <stdarg.h>
  1397.  
  1398. -#define X264_BUILD 83
  1399. +#define X264_BUILD 84
  1400.  
  1401. /* x264_t:
  1402. * opaque handler for encoder */
  1403. @@ -316,6 +316,9 @@ typedef struct x264_param_t
  1404. int b_vfr_input; /* VFR input */
  1405. int i_timebase_num; /* Timebase numerator */
  1406. int i_timebase_den; /* Timebase denominator */
  1407. + int b_dts_compress; /* DTS compression: this algorithm eliminates negative DTS
  1408. + * by compressing them to be less than the second PTS.
  1409. + * Warning: this will change the timebase! */
  1410.  
  1411. /* Slicing parameters */
  1412. int i_slice_max_size; /* Max size per slice in bytes; includes estimated NAL overhead. */
  1413. --
  1414. 1.6.1.2
  1415.  
  1416.  
  1417. From 8a28b47a896fbe43866d9e6d3481902210f4107c Mon Sep 17 00:00:00 2001
  1418. From: Diogo Franco <diogomfranco@gmail.com>
  1419. Date: Wed, 27 Jan 2010 09:26:35 -0800
  1420. Subject: [PATCH 07/13] Fix cross-compiling with lavf, add support for ffms2.pc
  1421. Also update configure script to work with newest ffms.
  1422.  
  1423. ---
  1424. configure | 52 ++++++++++++++++++++++++++++++----------------------
  1425. 1 files changed, 30 insertions(+), 22 deletions(-)
  1426.  
  1427. diff --git a/configure b/configure
  1428. index 9f04a18..133a569 100755
  1429. --- a/configure
  1430. +++ b/configure
  1431. @@ -416,25 +416,23 @@ fi
  1432.  
  1433. if [ "$lavf_input" = "auto" ] ; then
  1434. lavf_input="no"
  1435. - if [ `${cross_prefix}pkg-config --exists libavformat libavcodec libswscale 2>$DEVNULL` ] ; then
  1436. - LAVF_LDFLAGS="$LAVF_LDFLAGS $(pkg-config --libs libavformat libavcodec libswscale)"
  1437. - LAVF_CFLAGS="$LAVF_CFLAGS $(pkg-config --cflags libavformat libavcodec libswscale)"
  1438. + if ${cross_prefix}pkg-config --exists libavformat libavcodec libswscale 2>$DEVNULL; then
  1439. + LAVF_LIBS="$LAVF_LIBS $(${cross_prefix}pkg-config --libs libavformat libavcodec libswscale)"
  1440. + LAVF_CFLAGS="$LAVF_CFLAGS $(${cross_prefix}pkg-config --cflags libavformat libavcodec libswscale)"
  1441. fi
  1442. - if [ -z "$LAVF_LDFLAGS" -a -z "$LAVF_CFLAGS" ]; then
  1443. - LAVF_LDFLAGS="-lavformat -lswscale"
  1444. + if [ -z "$LAVF_LIBS" -a -z "$LAVF_CFLAGS" ]; then
  1445. + LAVF_LIBS="-lavformat -lswscale"
  1446. for lib in -lpostproc -lavcodec -lavutil -lm -lz -lbz2 $libpthread -lavifil32; do
  1447. - cc_check "" $lib && LAVF_LDFLAGS="$LAVF_LDFLAGS $lib"
  1448. + cc_check "" $lib && LAVF_LIBS="$LAVF_LIBS $lib"
  1449. done
  1450. fi
  1451. - LAVF_LDFLAGS="-L. $LAVF_LDFLAGS"
  1452. - if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LDFLAGS" && \
  1453. - cc_check libswscale/swscale.h "$LAVF_CFLAGS $LAVF_LDFLAGS" ; then
  1454. + LAVF_LIBS="-L. $LAVF_LIBS"
  1455. + if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" && \
  1456. + cc_check libswscale/swscale.h "$LAVF_CFLAGS $LAVF_LIBS" ; then
  1457. # avcodec_decode_video2 is currently the most recently added function that we use; it was added in r18351
  1458. - if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LDFLAGS" "avcodec_decode_video2( NULL, NULL, NULL, NULL );" ; then
  1459. + if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "avcodec_decode_video2( NULL, NULL, NULL, NULL );" ; then
  1460. lavf_input="yes"
  1461. echo "#define LAVF_INPUT" >> config.h
  1462. - LDFLAGSCLI="$LDFLAGSCLI $LAVF_LDFLAGS"
  1463. - [ -n "$LAVF_CFLAGS" ] && CFLAGS="$CFLAGS $LAVF_CFLAGS"
  1464. else
  1465. echo "Warning: libavformat is too old, update to ffmpeg r18351+"
  1466. fi
  1467. @@ -443,19 +441,29 @@ fi
  1468.  
  1469. if [ "$ffms_input" = "auto" ] ; then
  1470. ffms_input="no"
  1471. - if [ "$lavf_input" = "yes" ] ; then
  1472. - if cc_check ffms.h -lFFMS2 "FFMS_DestroyVideoSource(0);" ; then
  1473. - ffms_input="yes"
  1474. - echo "#define FFMS_INPUT" >> config.h
  1475. - LDFLAGSCLI="$LDFLAGSCLI -lFFMS2"
  1476. - elif cc_check ffms.h "-lFFMS2 $LAVF_LDFLAGS -lstdc++" "FFMS_DestroyVideoSource(0);" ; then
  1477. - ffms_input="yes"
  1478. - echo "#define FFMS_INPUT" >> config.h
  1479. - LDFLAGSCLI="-lFFMS2 $LDFLAGSCLI -lstdc++"
  1480. - fi
  1481. + if ${cross_prefix}pkg-config --exists ffms2 2>$DEVNULL; then
  1482. + FFMS2_LIBS="$FFMS2_LIBS $(${cross_prefix}pkg-config --libs ffms2)"
  1483. + FFMS2_CFLAGS="$FFMS2_LIBS $(${cross_prefix}pkg-config --cflags ffms2)"
  1484. + fi
  1485. + [ -z "$FFMS2_LIBS" ] && FFMS2_LIBS="-lffms2"
  1486. +
  1487. + if cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS" "FFMS_DestroyVideoSource(0);" ; then
  1488. + ffms_input="yes"
  1489. + elif cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS -lstdc++ $LAVF_LIBS" "FFMS_DestroyVideoSource(0);" ; then
  1490. + ffms_input="yes"
  1491. + FFMS2_LIBS="$FFMS2_LIBS -lstdc++ $LAVF_LIBS"
  1492. fi
  1493. fi
  1494.  
  1495. +if [ "$ffms_input" = "yes" ]; then
  1496. + LDFLAGSCLI="$FFMS2_LIBS $LDFLAGSCLI"
  1497. + [ -n "$FFMS2_CFLAGS" ] && CFLAGS="$CFLAGS $FFMS2_CFLAGS"
  1498. + echo "#define FFMS_INPUT" >> config.h
  1499. +elif [ "$lavf_input" = "yes" ]; then
  1500. + LDFLAGSCLI="$LAVF_LIBS $LDFLAGSCLI"
  1501. + [ -n "$LAVF_CFLAGS" ] && CFLAGS="$CFLAGS $LAVF_CFLAGS"
  1502. +fi
  1503. +
  1504. MP4_LDFLAGS="-lgpac_static"
  1505. if [ $SYS = MINGW ]; then
  1506. MP4_LDFLAGS="$MP4_LDFLAGS -lwinmm"
  1507. --
  1508. 1.6.1.2
  1509.  
  1510.  
  1511. From 6ec5692f70d45538e2ae7a76bd4656e5ca6ff918 Mon Sep 17 00:00:00 2001
  1512. From: Diogo Franco <diogomfranco@gmail.com>
  1513. Date: Wed, 27 Jan 2010 10:12:42 -0800
  1514. Subject: [PATCH 08/13] Add config.log support
  1515. Now, if configure fails, you'll be able to see why.
  1516.  
  1517. ---
  1518. .gitignore | 1 +
  1519. configure | 106 +++++++++++++++++++++++++++++++++++++++++++++++++----------
  1520. 2 files changed, 89 insertions(+), 18 deletions(-)
  1521.  
  1522. diff --git a/.gitignore b/.gitignore
  1523. index 308b793..9d8cb70 100644
  1524. --- a/.gitignore
  1525. +++ b/.gitignore
  1526. @@ -12,6 +12,7 @@
  1527. .depend
  1528. config.h
  1529. config.mak
  1530. +config.log
  1531. x264
  1532. checkasm
  1533.  
  1534. diff --git a/configure b/configure
  1535. index 133a569..7cb14ba 100755
  1536. --- a/configure
  1537. +++ b/configure
  1538. @@ -27,24 +27,77 @@ echo ""
  1539. exit 1
  1540. fi
  1541.  
  1542. +log_check() {
  1543. + echo -n "checking $1... " >> config.log
  1544. +}
  1545. +
  1546. +log_ok() {
  1547. + echo "yes" >> config.log
  1548. +}
  1549. +
  1550. +log_fail() {
  1551. + echo "no" >> config.log
  1552. +}
  1553. +
  1554. +log_msg() {
  1555. + echo "$1" >> config.log
  1556. +}
  1557. +
  1558. cc_check() {
  1559. + if [ -z "$3" ]; then
  1560. + if [ -z "$1" ]; then
  1561. + log_check "whether $CC works"
  1562. + else
  1563. + log_check "for $1"
  1564. + fi
  1565. + elif [ -z "$1" ]; then
  1566. + log_check "whether $CC supports $3"
  1567. + else
  1568. + log_check "for $3 on $1";
  1569. + fi
  1570. rm -f conftest.c
  1571. [ -n "$1" ] && echo "#include <$1>" > conftest.c
  1572. echo "int main () { $3 return 0; }" >> conftest.c
  1573. - $CC conftest.c $CFLAGS $LDFLAGS $LDFLAGSCLI $2 -o conftest 2>$DEVNULL
  1574. + if $CC conftest.c $CFLAGS $LDFLAGS $LDFLAGSCLI $2 -o conftest >conftest.log 2>&1; then
  1575. + res=$?
  1576. + log_ok
  1577. + else
  1578. + res=$?
  1579. + log_fail
  1580. + log_msg "Failed commandline was:"
  1581. + log_msg "--------------------------------------------------"
  1582. + log_msg "$CC conftest.c $CFLAGS $LDFLAGS $LDFLAGSCLI $2"
  1583. + cat conftest.log >> config.log
  1584. + log_msg "--------------------------------------------------"
  1585. + fi
  1586. + return $res
  1587. }
  1588.  
  1589. as_check() {
  1590. + log_check "whether $AS supports $1"
  1591. echo "$1" > conftest.asm
  1592. - $AS conftest.asm $ASFLAGS $2 -o conftest.o 2>$DEVNULL
  1593. + if $AS conftest.asm $ASFLAGS $2 -o conftest.o >conftest.log 2>&1; then
  1594. + res=$?
  1595. + log_ok
  1596. + else
  1597. + res=$?
  1598. + log_fail
  1599. + log_msg "Failed commandline was:"
  1600. + log_msg "--------------------------------------------------"
  1601. + log_msg "$AS conftest.asm $ASFLAGS $2 -o conftest.o"
  1602. + cat conftest.log >> config.log
  1603. + log_msg "--------------------------------------------------"
  1604. + fi
  1605. + return $res
  1606. }
  1607.  
  1608. die() {
  1609. + log_msg "DIED: $@"
  1610. echo "$@"
  1611. exit 1
  1612. }
  1613.  
  1614. -rm -f config.h config.mak x264.pc conftest*
  1615. +rm -f config.h config.mak config.log x264.pc conftest*
  1616.  
  1617. prefix='/usr/local'
  1618. exec_prefix='${prefix}'
  1619. @@ -320,6 +373,16 @@ case $host_cpu in
  1620. ;;
  1621. esac
  1622.  
  1623. +log_msg "x264 configure script"
  1624. +if [ -n "$*" ]; then
  1625. + msg="Command line options:"
  1626. + for i in $@; do
  1627. + msg="$msg \"$i\""
  1628. + done
  1629. + log_msg "$msg"
  1630. +fi
  1631. +log_msg ""
  1632. +
  1633. # check requirements
  1634.  
  1635. cc_check || die "No working C compiler found."
  1636. @@ -506,9 +569,9 @@ if [ "$debug" = "yes" ]; then
  1637. elif [ $ARCH = ARM ]; then
  1638. # arm-gcc-4.2 produces incorrect output with -ffast-math
  1639. # and it doesn't save any speed anyway on 4.4, so disable it
  1640. - CFLAGS="-O4 -fno-fast-math $CFLAGS"
  1641. + CFLAGS="-O3 -fno-fast-math $CFLAGS"
  1642. else
  1643. - CFLAGS="-O4 -ffast-math $CFLAGS"
  1644. + CFLAGS="-O3 -ffast-math $CFLAGS"
  1645. fi
  1646.  
  1647. if cc_check "stdio.h" "" "fseeko(stdin,0,0);" ; then
  1648. @@ -585,20 +648,27 @@ Libs: $pclibs
  1649. Cflags: -I$includedir
  1650. EOF
  1651.  
  1652. +cat > conftest.log <<EOF
  1653. +Platform: $ARCH
  1654. +System: $SYS
  1655. +asm: $asm
  1656. +avs input: $avs_input
  1657. +lavf input: $lavf_input
  1658. +ffms input: $ffms_input
  1659. +mp4 output: $mp4_output
  1660. +pthread: $pthread
  1661. +debug: $debug
  1662. +gprof: $gprof
  1663. +PIC: $pic
  1664. +shared: $shared
  1665. +visualize: $vis
  1666. +EOF
  1667. +
  1668. +echo >> config.log
  1669. +cat conftest.log >> config.log
  1670. +cat conftest.log
  1671. +rm conftest.log
  1672.  
  1673. -echo "Platform: $ARCH"
  1674. -echo "System: $SYS"
  1675. -echo "asm: $asm"
  1676. -echo "avs input: $avs_input"
  1677. -echo "lavf input: $lavf_input"
  1678. -echo "ffms input: $ffms_input"
  1679. -echo "mp4 output: $mp4_output"
  1680. -echo "pthread: $pthread"
  1681. -echo "debug: $debug"
  1682. -echo "gprof: $gprof"
  1683. -echo "PIC: $pic"
  1684. -echo "shared: $shared"
  1685. -echo "visualize: $vis"
  1686. echo
  1687. echo "You can run 'make' or 'make fprofiled' now."
  1688.  
  1689. --
  1690. 1.6.1.2
  1691.  
  1692.  
  1693. From 959e19ea08325dc7859b3dbc2c797e64e03899cc Mon Sep 17 00:00:00 2001
  1694. From: Diogo Franco <diogomfranco@gmail.com>
  1695. Date: Wed, 27 Jan 2010 13:11:08 -0800
  1696. Subject: [PATCH 09/13] Add configure check for log2 support
  1697. Some incredibly braindamaged operating systems, such as FreeBSD, blatantly ignore the C specification and omit certain functions that are required by ISO C.
  1698. log2f is one of these functions that periodically goes missing in such operating systems.
  1699.  
  1700. ---
  1701. common/osdep.h | 4 ++++
  1702. configure | 4 ++++
  1703. 2 files changed, 8 insertions(+), 0 deletions(-)
  1704.  
  1705. diff --git a/common/osdep.h b/common/osdep.h
  1706. index abae9ac..9988803 100644
  1707. --- a/common/osdep.h
  1708. +++ b/common/osdep.h
  1709. @@ -34,6 +34,10 @@
  1710. #include <inttypes.h>
  1711. #endif
  1712.  
  1713. +#ifndef HAVE_LOG2F
  1714. +#define log2f(x) (logf((x))/0.693147180559945f)
  1715. +#endif
  1716. +
  1717. #ifdef _WIN32
  1718. #include <io.h> // _setmode()
  1719. #include <fcntl.h> // _O_BINARY
  1720. diff --git a/configure b/configure
  1721. index 7cb14ba..271d919 100755
  1722. --- a/configure
  1723. +++ b/configure
  1724. @@ -477,6 +477,10 @@ if test "$pthread" = "yes" ; then
  1725. LDFLAGS="$LDFLAGS $libpthread"
  1726. fi
  1727.  
  1728. +if cc_check "math.h" "-Werror" "log2f(2);" ; then
  1729. + CFLAGS="$CFLAGS -DHAVE_LOG2F"
  1730. +fi
  1731. +
  1732. if [ "$lavf_input" = "auto" ] ; then
  1733. lavf_input="no"
  1734. if ${cross_prefix}pkg-config --exists libavformat libavcodec libswscale 2>$DEVNULL; then
  1735. --
  1736. 1.6.1.2
  1737.  
  1738.  
  1739. From bc027c79f3c75cbc89163443511a1009b46344b9 Mon Sep 17 00:00:00 2001
  1740. From: Jason Garrett-Glaser <darkshikari@gmail.com>
  1741. Date: Wed, 27 Jan 2010 19:41:27 -0800
  1742. Subject: [PATCH 10/13] Fix implicit CBR message to only print when in ABR mode
  1743. Also make it print outside of debug mode.
  1744.  
  1745. ---
  1746. encoder/ratecontrol.c | 12 ++++++++++--
  1747. 1 files changed, 10 insertions(+), 2 deletions(-)
  1748.  
  1749. diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
  1750. index 746b17a..5304616 100644
  1751. --- a/encoder/ratecontrol.c
  1752. +++ b/encoder/ratecontrol.c
  1753. @@ -436,8 +436,16 @@ int x264_ratecontrol_new( x264_t *h )
  1754. }
  1755. else if( h->param.rc.i_vbv_max_bitrate == 0 )
  1756. {
  1757. - x264_log( h, X264_LOG_DEBUG, "VBV maxrate unspecified, assuming CBR\n" );
  1758. - h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
  1759. + if( h->param.rc.i_rc_method == X264_RC_ABR )
  1760. + {
  1761. + x264_log( h, X264_LOG_INFO, "VBV maxrate unspecified, assuming CBR\n" );
  1762. + h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
  1763. + }
  1764. + else
  1765. + {
  1766. + x264_log( h, X264_LOG_INFO, "VBV bufsize set but maxrate unspecified, ignored\n" );
  1767. + h->param.rc.i_vbv_buffer_size = 0;
  1768. + }
  1769. }
  1770. }
  1771. if( h->param.rc.i_vbv_max_bitrate < h->param.rc.i_bitrate &&
  1772. --
  1773. 1.6.1.2
  1774.  
  1775.  
  1776. From 3d456de7c5d4ca0e031a310ba6566690f4ff82fc Mon Sep 17 00:00:00 2001
  1777. From: Diogo Franco <diogomfranco@gmail.com>
  1778. Date: Wed, 27 Jan 2010 20:29:50 -0800
  1779. Subject: [PATCH 11/13] Implement ffms2 version check
  1780. Depends on ffms2 version 2.13.1 (r272).
  1781. Tries pkg-config's built-in version checking first.
  1782. Uses only the preprocessor to avoid cross-compilation issues.
  1783.  
  1784. ---
  1785. configure | 20 +++++++++++++++++++-
  1786. 1 files changed, 19 insertions(+), 1 deletions(-)
  1787.  
  1788. diff --git a/configure b/configure
  1789. index 271d919..adebdb8 100755
  1790. --- a/configure
  1791. +++ b/configure
  1792. @@ -507,10 +507,17 @@ if [ "$lavf_input" = "auto" ] ; then
  1793. fi
  1794.  
  1795. if [ "$ffms_input" = "auto" ] ; then
  1796. + ffms_major="2"; ffms_minor="13"; ffms_micro="1"; ffms_bump="0"
  1797. +
  1798. ffms_input="no"
  1799. - if ${cross_prefix}pkg-config --exists ffms2 2>$DEVNULL; then
  1800. + [ $ffms_micro -gt 0 -o $ffms_bump -gt 0 ] && vmicro=".$ffms_micro"
  1801. + [ $ffms_bump -gt 0 ] && vbump=".$ffms_bump"
  1802. + if ${cross_prefix}pkg-config --atleast-version="$ffms_major.$ffms_minor$vmicro$vbump" ffms2 2>$DEVNULL; then
  1803. FFMS2_LIBS="$FFMS2_LIBS $(${cross_prefix}pkg-config --libs ffms2)"
  1804. FFMS2_CFLAGS="$FFMS2_LIBS $(${cross_prefix}pkg-config --cflags ffms2)"
  1805. + api_check="no"
  1806. + else
  1807. + api_check="yes"
  1808. fi
  1809. [ -z "$FFMS2_LIBS" ] && FFMS2_LIBS="-lffms2"
  1810.  
  1811. @@ -520,6 +527,17 @@ if [ "$ffms_input" = "auto" ] ; then
  1812. ffms_input="yes"
  1813. FFMS2_LIBS="$FFMS2_LIBS -lstdc++ $LAVF_LIBS"
  1814. fi
  1815. +
  1816. + if [ $api_check = "yes" -a $ffms_input = "yes" ]; then
  1817. + log_check "whether ffms2 version is at least $ffms_major.$ffms_minor$vmicro$vbump"
  1818. + $CC $CFLAGS $FFMS2_CFLAGS -c -o conftest -x c - >$DEVNULL 2>&1 <<EOF
  1819. +#include <ffms.h>
  1820. +#if FFMS_VERSION < (($ffms_major << 24) | ($ffms_minor << 16) | ($ffms_micro << 8) | $ffms_bump)
  1821. +#error Requires ffms2 version 2.13.1
  1822. +#endif
  1823. +EOF
  1824. + [ $? = 0 ] && log_ok || { ffms_input="no"; log_fail; }
  1825. + fi
  1826. fi
  1827.  
  1828. if [ "$ffms_input" = "yes" ]; then
  1829. --
  1830. 1.6.1.2
  1831.  
  1832.  
  1833. From c0e2d3d05b26e48ade557eface323c42649e1d83 Mon Sep 17 00:00:00 2001
  1834. From: Steven Walters <kemuri9@gmail.com>
  1835. Date: Thu, 28 Jan 2010 17:26:40 -0800
  1836. Subject: [PATCH 12/13] Fix stat with large file support
  1837.  
  1838. ---
  1839. common/common.h | 1 -
  1840. common/osdep.h | 1 +
  1841. 2 files changed, 1 insertions(+), 1 deletions(-)
  1842.  
  1843. diff --git a/common/common.h b/common/common.h
  1844. index ca15330..455bc61 100644
  1845. --- a/common/common.h
  1846. +++ b/common/common.h
  1847. @@ -70,7 +70,6 @@ do {\
  1848. /****************************************************************************
  1849. * Includes
  1850. ****************************************************************************/
  1851. -#include <sys/stat.h>
  1852. #include "osdep.h"
  1853. #include <stdarg.h>
  1854. #include <stddef.h>
  1855. diff --git a/common/osdep.h b/common/osdep.h
  1856. index 9988803..3d12072 100644
  1857. --- a/common/osdep.h
  1858. +++ b/common/osdep.h
  1859. @@ -27,6 +27,7 @@
  1860. #define _LARGEFILE_SOURCE 1
  1861. #define _FILE_OFFSET_BITS 64
  1862. #include <stdio.h>
  1863. +#include <sys/stat.h>
  1864.  
  1865. #ifdef HAVE_STDINT_H
  1866. #include <stdint.h>
  1867. --
  1868. 1.6.1.2
  1869.  
  1870.  
  1871. From b4276573f79fef3084e50b22a0ffad8bb0b8779c Mon Sep 17 00:00:00 2001
  1872. From: Diogo Franco <diogomfranco@gmail.com>
  1873. Date: Thu, 28 Jan 2010 17:28:03 -0800
  1874. Subject: [PATCH 13/13] Move -D CFLAGS to config.h
  1875.  
  1876. ---
  1877. Makefile | 12 +++++-----
  1878. common/common.h | 1 -
  1879. common/osdep.h | 2 +
  1880. configure | 60 ++++++++++++++++++++++++++++++------------------------
  1881. 4 files changed, 41 insertions(+), 34 deletions(-)
  1882.  
  1883. diff --git a/Makefile b/Makefile
  1884. index f643228..cef8725 100644
  1885. --- a/Makefile
  1886. +++ b/Makefile
  1887. @@ -18,26 +18,26 @@ SRCCLI = x264.c input/yuv.c input/y4m.c output/raw.c \
  1888.  
  1889. SRCSO =
  1890.  
  1891. -MUXERS := $(shell grep -E "(IN|OUT)PUT" config.h)
  1892. +CONFIG := $(shell cat config.h)
  1893.  
  1894. # Optional muxer module sources
  1895. -ifneq ($(findstring AVS_INPUT, $(MUXERS)),)
  1896. +ifneq ($(findstring AVS_INPUT, $(CONFIG)),)
  1897. SRCCLI += input/avs.c
  1898. endif
  1899.  
  1900. -ifneq ($(findstring HAVE_PTHREAD, $(CFLAGS)),)
  1901. +ifneq ($(findstring HAVE_PTHREAD, $(CONFIG)),)
  1902. SRCCLI += input/thread.c
  1903. endif
  1904.  
  1905. -ifneq ($(findstring LAVF_INPUT, $(MUXERS)),)
  1906. +ifneq ($(findstring LAVF_INPUT, $(CONFIG)),)
  1907. SRCCLI += input/lavf.c
  1908. endif
  1909.  
  1910. -ifneq ($(findstring FFMS_INPUT, $(MUXERS)),)
  1911. +ifneq ($(findstring FFMS_INPUT, $(CONFIG)),)
  1912. SRCCLI += input/ffms.c
  1913. endif
  1914.  
  1915. -ifneq ($(findstring MP4_OUTPUT, $(MUXERS)),)
  1916. +ifneq ($(findstring MP4_OUTPUT, $(CONFIG)),)
  1917. SRCCLI += output/mp4.c
  1918. endif
  1919.  
  1920. diff --git a/common/common.h b/common/common.h
  1921. index 455bc61..a52e531 100644
  1922. --- a/common/common.h
  1923. +++ b/common/common.h
  1924. @@ -102,7 +102,6 @@ typedef union { uint64_t i; uint32_t a[2]; uint16_t b[4]; uint8_t c[8]; } MAY_AL
  1925. #include "dct.h"
  1926. #include "cabac.h"
  1927. #include "quant.h"
  1928. -#include "config.h"
  1929.  
  1930. /****************************************************************************
  1931. * General functions
  1932. diff --git a/common/osdep.h b/common/osdep.h
  1933. index 3d12072..907bcee 100644
  1934. --- a/common/osdep.h
  1935. +++ b/common/osdep.h
  1936. @@ -29,6 +29,8 @@
  1937. #include <stdio.h>
  1938. #include <sys/stat.h>
  1939.  
  1940. +#include "config.h"
  1941. +
  1942. #ifdef HAVE_STDINT_H
  1943. #include <stdint.h>
  1944. #else
  1945. diff --git a/configure b/configure
  1946. index adebdb8..3bb5a40 100755
  1947. --- a/configure
  1948. +++ b/configure
  1949. @@ -91,6 +91,10 @@ as_check() {
  1950. return $res
  1951. }
  1952.  
  1953. +define() {
  1954. + echo "#define $1$([ -n "$2" ] && echo " $2")" >> config.h
  1955. +}
  1956. +
  1957. die() {
  1958. log_msg "DIED: $@"
  1959. echo "$@"
  1960. @@ -208,7 +212,7 @@ for opt do
  1961. ;;
  1962. --enable-visualize)
  1963. LDFLAGS="$LDFLAGS -L/usr/X11R6/lib -lX11"
  1964. - CFLAGS="$CFLAGS -DVISUALIZE=1"
  1965. + define VISUALIZE
  1966. vis="yes"
  1967. ;;
  1968. --host=*)
  1969. @@ -243,7 +247,7 @@ host_os="${host#*-}"
  1970. case $host_os in
  1971. beos*)
  1972. SYS="BEOS"
  1973. - CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
  1974. + define HAVE_MALLOC_H
  1975. ;;
  1976. darwin*)
  1977. SYS="MACOSX"
  1978. @@ -259,7 +263,7 @@ case $host_os in
  1979. ;;
  1980. kfreebsd*-gnu)
  1981. SYS="FREEBSD"
  1982. - CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
  1983. + define HAVE_MALLOC_H
  1984. LDFLAGS="$LDFLAGS -lm"
  1985. ;;
  1986. netbsd*)
  1987. @@ -273,7 +277,7 @@ case $host_os in
  1988. ;;
  1989. *linux*)
  1990. SYS="LINUX"
  1991. - CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
  1992. + define HAVE_MALLOC_H
  1993. LDFLAGS="$LDFLAGS -lm"
  1994. ;;
  1995. cygwin*)
  1996. @@ -292,7 +296,7 @@ case $host_os in
  1997. ;;
  1998. sunos*|solaris*)
  1999. SYS="SunOS"
  2000. - CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
  2001. + define HAVE_MALLOC_H
  2002. LDFLAGS="$LDFLAGS -lm"
  2003. HAVE_GETOPT_LONG=0
  2004. ;;
  2005. @@ -341,7 +345,8 @@ case $host_cpu in
  2006. then
  2007. CFLAGS="$CFLAGS -faltivec -fastf -mcpu=G4"
  2008. else
  2009. - CFLAGS="$CFLAGS -maltivec -mabi=altivec -DHAVE_ALTIVEC_H"
  2010. + CFLAGS="$CFLAGS -maltivec -mabi=altivec"
  2011. + define HAVE_ALTIVEC_H
  2012. fi
  2013. ;;
  2014. sparc)
  2015. @@ -407,17 +412,17 @@ if [ $asm = yes -a \( $ARCH = X86 -o $ARCH = X86_64 \) ] ; then
  2016. echo "If you really want to compile without asm, configure with --disable-asm."
  2017. exit 1
  2018. fi
  2019. - CFLAGS="$CFLAGS -DHAVE_MMX"
  2020. + define HAVE_MMX
  2021. fi
  2022.  
  2023. if [ $asm = yes -a $ARCH = ARM ] ; then
  2024. # set flags so neon is built by default
  2025. echo $CFLAGS | grep -Eq '(-mcpu|-march|-mfpu|-mfloat-abi)' || CFLAGS="$CFLAGS -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp"
  2026.  
  2027. - if cc_check '' '' 'asm("rev ip, ip");' ; then CFLAGS="$CFLAGS -DHAVE_ARMV6"
  2028. - cc_check '' '' 'asm("movt r0, #0");' && CFLAGS="$CFLAGS -DHAVE_ARMV6T2"
  2029. - cc_check '' '' 'asm("vadd.i16 q0, q0, q0");' && CFLAGS="$CFLAGS -DHAVE_NEON"
  2030. - ASFLAGS="$ASFLAGS $CFLAGS -c"
  2031. + if cc_check '' '' 'asm("rev ip, ip");' ; then define HAVE_ARMV6 && ASFLAGS="$ASFLAGS -DHAVE_ARMV6"
  2032. + cc_check '' '' 'asm("movt r0, #0");' && define HAVE_ARMV6T2 && ASFLAGS="$ASFLAGS -DHAVE_ARMV6T2"
  2033. + cc_check '' '' 'asm("vadd.i16 q0, q0, q0");' && define HAVE_NEON && ASFLAGS="$ASFLAGS -DHAVE_NEON"
  2034. + ASFLAGS="$ASFLAGS -c"
  2035. else
  2036. echo "You specified a pre-ARMv6 or Thumb-1 CPU in your CFLAGS."
  2037. echo "If you really want to run on such a CPU, configure with --disable-asm."
  2038. @@ -428,12 +433,13 @@ fi
  2039. [ $asm = no ] && AS=""
  2040. [ "x$AS" = x ] && asm="no"
  2041.  
  2042. -CFLAGS="$CFLAGS -DARCH_$ARCH -DSYS_$SYS"
  2043. +define ARCH_$ARCH
  2044. +define SYS_$SYS
  2045.  
  2046. echo "int i = 0x42494745; double f = 0x1.0656e6469616ep+102;" > conftest.c
  2047. $CC $CFLAGS conftest.c -c -o conftest.o 2>$DEVNULL || die "endian test failed"
  2048. if grep -q BIGE conftest.o && grep -q FPendian conftest.o ; then
  2049. - CFLAGS="$CFLAGS -DWORDS_BIGENDIAN"
  2050. + define WORDS_BIGENDIAN
  2051. elif !(grep -q EGIB conftest.o && grep -q naidnePF conftest.o) ; then
  2052. die "endian test failed"
  2053. fi
  2054. @@ -457,11 +463,11 @@ if test "$pthread" = "auto" ; then
  2055. elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
  2056. pthread="yes"
  2057. libpthread="-lpthreadGC2 -lwsock32"
  2058. - CFLAGS="$CFLAGS -DPTW32_STATIC_LIB"
  2059. + define PTW32_STATIC_LIB
  2060. elif cc_check pthread.h "-lpthreadGC2 -lws2_32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
  2061. pthread="yes"
  2062. libpthread="-lpthreadGC2 -lws2_32"
  2063. - CFLAGS="$CFLAGS -DPTW32_STATIC_LIB"
  2064. + define PTW32_STATIC_LIB
  2065. fi
  2066. ;;
  2067. OPENBSD)
  2068. @@ -473,12 +479,12 @@ if test "$pthread" = "auto" ; then
  2069. esac
  2070. fi
  2071. if test "$pthread" = "yes" ; then
  2072. - CFLAGS="$CFLAGS -DHAVE_PTHREAD"
  2073. + define HAVE_PTHREAD
  2074. LDFLAGS="$LDFLAGS $libpthread"
  2075. fi
  2076.  
  2077. if cc_check "math.h" "-Werror" "log2f(2);" ; then
  2078. - CFLAGS="$CFLAGS -DHAVE_LOG2F"
  2079. + define HAVE_LOG2F
  2080. fi
  2081.  
  2082. if [ "$lavf_input" = "auto" ] ; then
  2083. @@ -499,7 +505,7 @@ if [ "$lavf_input" = "auto" ] ; then
  2084. # avcodec_decode_video2 is currently the most recently added function that we use; it was added in r18351
  2085. if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "avcodec_decode_video2( NULL, NULL, NULL, NULL );" ; then
  2086. lavf_input="yes"
  2087. - echo "#define LAVF_INPUT" >> config.h
  2088. + define LAVF_INPUT
  2089. else
  2090. echo "Warning: libavformat is too old, update to ffmpeg r18351+"
  2091. fi
  2092. @@ -543,7 +549,7 @@ fi
  2093. if [ "$ffms_input" = "yes" ]; then
  2094. LDFLAGSCLI="$FFMS2_LIBS $LDFLAGSCLI"
  2095. [ -n "$FFMS2_CFLAGS" ] && CFLAGS="$CFLAGS $FFMS2_CFLAGS"
  2096. - echo "#define FFMS_INPUT" >> config.h
  2097. + define FFMS_INPUT
  2098. elif [ "$lavf_input" = "yes" ]; then
  2099. LDFLAGSCLI="$LAVF_LIBS $LDFLAGSCLI"
  2100. [ -n "$LAVF_CFLAGS" ] && CFLAGS="$CFLAGS $LAVF_CFLAGS"
  2101. @@ -558,7 +564,7 @@ if [ "$mp4_output" = "auto" ] ; then
  2102. cc_check gpac/isomedia.h "$MP4_LDFLAGS" && mp4_output="yes"
  2103. fi
  2104. if [ "$mp4_output" = "yes" ] ; then
  2105. - echo "#define MP4_OUTPUT" >> config.h
  2106. + define MP4_OUTPUT
  2107. LDFLAGSCLI="$LDFLAGSCLI $MP4_LDFLAGS"
  2108. fi
  2109.  
  2110. @@ -566,11 +572,11 @@ if [ "$avs_input" = "auto" ] ; then
  2111. avs_input=no
  2112. if [ $SYS = MINGW ] && cc_check avisynth_c.h ; then
  2113. avs_input="yes"
  2114. - echo "#define AVS_INPUT" >> config.h
  2115. - echo "#define HAVE_AVISYNTH_C_H" >> config.h
  2116. + define AVS_INPUT
  2117. + define HAVE_AVISYNTH_C_H
  2118. elif [ $SYS = MINGW ] && cc_check extras/avisynth_c.h ; then
  2119. avs_input="yes"
  2120. - echo "#define AVS_INPUT" >> config.h
  2121. + define AVS_INPUT
  2122. fi
  2123. fi
  2124.  
  2125. @@ -597,11 +603,11 @@ else
  2126. fi
  2127.  
  2128. if cc_check "stdio.h" "" "fseeko(stdin,0,0);" ; then
  2129. - echo "#define fseek fseeko" >> config.h
  2130. - echo "#define ftell ftello" >> config.h
  2131. + define fseek fseeko
  2132. + define ftell ftello
  2133. elif cc_check "stdio.h" "" "fseeko64(stdin,0,0);" ; then
  2134. - echo "#define fseek fseeko64" >> config.h
  2135. - echo "#define ftell ftello64" >> config.h
  2136. + define fseek fseeko64
  2137. + define ftell ftello64
  2138. fi
  2139.  
  2140. rm -f conftest*
  2141. --
  2142. 1.6.1.2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement