]> git.sesse.net Git - x264/blobdiff - encoder/encoder.c
macroblock-level ratecontrol: improved vbv strictness, and improved quality when...
[x264] / encoder / encoder.c
index 32c5072c74847db6564c5d262cf98b060a1cdb64..6ed7862e13991c314251de81d023b5b02106cb6b 100644 (file)
@@ -21,7 +21,6 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
 #include <windows.h>
 #define pthread_t               HANDLE
 #define pthread_create(t,u,f,d) *(t)=CreateThread(NULL,0,f,d,0,NULL)
-#define pthread_join(t,s)       WaitForSingleObject(t,INFINITE); \
-                                CloseHandle(t)
+#define pthread_join(t,s)       WaitForSingleObject(t,INFINITE); \
+                                  CloseHandle(t); } 
 #define HAVE_PTHREAD 1
-#elif HAVE_PTHREAD
-#ifdef SYS_BEOS
+
+#elif defined(SYS_BEOS)
 #include <kernel/OS.h>
 #define pthread_t               thread_id
-#define pthread_create(t,u,f,d) *(t)=spawn_thread(f,"",10,d)
+#define pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
+                                  resume_thread(*(t)); }
 #define pthread_join(t,s)       wait_for_thread(t,(long*)s)
-#else
+#define HAVE_PTHREAD 1
+
+#elif HAVE_PTHREAD
 #include <pthread.h>
 #endif
-#endif
 
 #include "common/common.h"
 #include "common/cpu.h"
@@ -85,29 +86,6 @@ static int64_t i_mtime_filter = 0;
  ******************************* x264 libs **********************************
  *
  ****************************************************************************/
-static int64_t x264_sqe( x264_t *h, uint8_t *pix1, int i_pix_stride, uint8_t *pix2, int i_pix2_stride, int i_width, int i_height )
-{
-    int64_t i_sqe = 0;
-    int x, y;
-
-#define SSD(size) i_sqe += h->pixf.ssd[size]( pix1+y*i_pix_stride+x, i_pix_stride, \
-                                              pix2+y*i_pix2_stride+x, i_pix2_stride );
-    for( y = 0; y < i_height-15; y += 16 )
-    {
-        for( x = 0; x < i_width-15; x += 16 )
-            SSD(PIXEL_16x16);
-        if( x < i_width-7 )
-            SSD(PIXEL_8x16);
-    }
-    if( y < i_height-7 )
-        for( x = 0; x < i_width-7; x += 8 )
-            SSD(PIXEL_8x8);
-#undef SSD
-    x264_cpu_restore( h->param.cpu );
-
-    return i_sqe;
-}
-
 static float x264_psnr( int64_t i_sqe, int64_t i_size )
 {
     double f_mse = (double)i_sqe / ((double)65025.0 * (double)i_size);
@@ -169,7 +147,14 @@ static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
 
     sh->i_redundant_pic_cnt = 0;
 
-    sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL );
+    if( !h->mb.b_direct_auto_read )
+    {
+        if( h->mb.b_direct_auto_write )
+            sh->b_direct_spatial_mv_pred = ( h->stat.i_direct_score[1] > h->stat.i_direct_score[0] );
+        else
+            sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL );
+    }
+    /* else b_direct_spatial_mv_pred was read from the 2pass statsfile */
 
     sh->b_num_ref_idx_override = 0;
     sh->i_num_ref_idx_l0_active = 1;
@@ -196,6 +181,7 @@ static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
 
     sh->i_cabac_init_idc = param->i_cabac_init_idc;
 
+    sh->i_qp = i_qp;
     sh->i_qp_delta = i_qp - pps->i_pic_init_qp;
     sh->b_sp_for_swidth = 0;
     sh->i_qs_delta = 0;
@@ -203,7 +189,7 @@ static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
     /* If effective qp <= 15, deblocking would have no effect anyway */
     if( param->b_deblocking_filter
         && ( h->mb.b_variable_qp
-        || 15 < i_qp + X264_MAX(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta) ) )
+        || 15 < i_qp + 2 * X264_MAX(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta) ) )
     {
         sh->i_disable_deblocking_filter_idc = 0;
     }
@@ -313,7 +299,6 @@ static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal
         else
         {
             bs_write1( s, 0 );  /* adaptive_ref_pic_marking_mode_flag */
-            /* FIXME */
         }
     }
 
@@ -322,16 +307,6 @@ static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal
         bs_write_ue( s, sh->i_cabac_init_idc );
     }
     bs_write_se( s, sh->i_qp_delta );      /* slice qp delta */
-#if 0
-    if( sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_SI )
-    {
-        if( sh->i_type == SLICE_TYPE_SP )
-        {
-            bs_write1( s, sh->b_sp_for_swidth );
-        }
-        bs_write_se( s, sh->i_qs_delta );
-    }
-#endif
 
     if( sh->pps->b_deblocking_filter_control )
     {
@@ -361,9 +336,9 @@ static int x264_validate_parameters( x264_t *h )
         return -1;
     }
 
-    if( h->param.i_width % 16 != 0 || h->param.i_height % 16 != 0 )
+    if( h->param.i_width % 2 || h->param.i_height % 2 )
     {
-        x264_log( h, X264_LOG_ERROR, "width %% 16 != 0 or height %% 16 != 0 (%dx%d)\n",
+        x264_log( h, X264_LOG_ERROR, "width or height not divisible by 2 (%dx%d)\n",
                   h->param.i_width, h->param.i_height );
         return -1;
     }
@@ -374,7 +349,7 @@ static int x264_validate_parameters( x264_t *h )
     }
 
     h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_SLICE_MAX );
-    h->param.i_threads = X264_MIN( h->param.i_threads, h->param.i_height / 16 );
+    h->param.i_threads = X264_MIN( h->param.i_threads, (h->param.i_height + 15) / 16 );
 #if !(HAVE_PTHREAD)
     if( h->param.i_threads > 1 )
     {
@@ -383,14 +358,32 @@ static int x264_validate_parameters( x264_t *h )
     }
 #endif
 
+    if( h->param.rc.b_cbr )
+        h->param.rc.i_rf_constant = 0;
+    if( h->param.rc.i_rf_constant > 0 )
+        h->param.rc.i_qp_constant = h->param.rc.i_rf_constant;
+    h->param.rc.i_rf_constant = x264_clip3( h->param.rc.i_rf_constant, 0, 51 );
+    h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
     if( !h->param.rc.b_cbr && h->param.rc.i_qp_constant == 0 )
     {
         h->mb.b_lossless = 1;
         h->param.analyse.b_transform_8x8 = 0;
         h->param.i_cqm_preset = X264_CQM_FLAT;
+        h->param.psz_cqm_file = NULL;
         h->param.rc.f_ip_factor = 1;
         h->param.rc.f_pb_factor = 1;
         h->param.analyse.b_psnr = 0;
+        h->param.analyse.i_chroma_qp_offset = 0;
+        h->param.analyse.i_trellis = 0;
+        h->param.analyse.b_fast_pskip = 0;
+        h->param.analyse.i_noise_reduction = 0;
+    }
+
+    if( ( h->param.i_width % 16 || h->param.i_height % 16 ) && !h->mb.b_lossless )
+    {
+        x264_log( h, X264_LOG_WARNING, 
+                  "width or height not divisible by 16 (%dx%d), compression will suffer.\n",
+                  h->param.i_width, h->param.i_height );
     }
 
     h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 16 );
@@ -401,19 +394,15 @@ static int x264_validate_parameters( x264_t *h )
     h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX );
     h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
     h->param.b_bframe_pyramid = h->param.b_bframe_pyramid && h->param.i_bframe > 1;
-    h->frames.i_delay = h->param.i_bframe;
-    h->frames.i_max_ref0 = h->param.i_frame_reference;
-    h->frames.i_max_ref1 = h->param.b_bframe_pyramid ? 2
-                            : h->param.i_bframe ? 1 : 0;
-    h->frames.i_max_dpb = X264_MIN( 16, h->frames.i_max_ref0 + h->frames.i_max_ref1 ) + 1;
+    h->param.b_bframe_adaptive = h->param.b_bframe_adaptive && h->param.i_bframe > 0;
+    h->mb.b_direct_auto_write = h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
+                                && h->param.i_bframe
+                                && ( h->param.rc.b_stat_write || !h->param.rc.b_stat_read );
 
     h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
     h->param.i_deblocking_filter_beta    = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
 
-    h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, -1, 2 );
-    /* don't yet support merging of cabac stats */
-    if( h->param.i_threads > 1 && h->param.i_cabac_init_idc == -1 )
-        h->param.i_cabac_init_idc = 0;
+    h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 );
 
     if( h->param.i_cqm_preset < X264_CQM_FLAT || h->param.i_cqm_preset > X264_CQM_CUSTOM )
         h->param.i_cqm_preset = X264_CQM_FLAT;
@@ -426,6 +415,11 @@ static int x264_validate_parameters( x264_t *h )
     if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX )
         h->param.analyse.i_me_range = 16;
     h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 6 );
+    h->param.analyse.b_bframe_rdo = h->param.analyse.b_bframe_rdo && h->param.analyse.i_subpel_refine >= 6;
+    h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1;
+    h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16|
+                              X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
+    h->param.analyse.intra &= X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
     if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) )
         h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
     if( !h->param.analyse.b_transform_8x8 )
@@ -434,13 +428,44 @@ static int x264_validate_parameters( x264_t *h )
         h->param.analyse.intra &= ~X264_ANALYSE_I8x8;
     }
     h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12);
-    h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 2048);
+    if( !h->param.b_cabac )
+        h->param.analyse.i_trellis = 0;
+    h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
+    h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
+
+    {
+        const x264_level_t *l = x264_levels;
+        while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
+            l++;
+        if( l->level_idc == 0 )
+        {
+            x264_log( h, X264_LOG_ERROR, "invalid level_idc: %d\n", h->param.i_level_idc );
+            return -1;
+        }
+        if( h->param.analyse.i_mv_range <= 0 )
+            h->param.analyse.i_mv_range = l->mv_range;
+        else
+            h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 2048);
+    }
 
     if( h->param.rc.f_qblur < 0 )
         h->param.rc.f_qblur = 0;
     if( h->param.rc.f_complexity_blur < 0 )
         h->param.rc.f_complexity_blur = 0;
-    h->param.rc.i_qp_constant = x264_clip3(h->param.rc.i_qp_constant, 0, 51);
+
+    /* ensure the booleans are 0 or 1 so they can be used in math */
+#define BOOLIFY(x) h->param.x = !!h->param.x
+    BOOLIFY( b_cabac );
+    BOOLIFY( b_deblocking_filter );
+    BOOLIFY( analyse.b_transform_8x8 );
+    BOOLIFY( analyse.b_weighted_bipred );
+    BOOLIFY( analyse.b_bidir_me );
+    BOOLIFY( analyse.b_chroma_me );
+    BOOLIFY( analyse.b_fast_pskip );
+    BOOLIFY( rc.b_cbr );
+    BOOLIFY( rc.b_stat_write );
+    BOOLIFY( rc.b_stat_read );
+#undef BOOLIFY
 
     return 0;
 }
@@ -464,6 +489,13 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
         return NULL;
     }
 
+    if( h->param.psz_cqm_file )
+        if( x264_cqm_parse_file( h, h->param.psz_cqm_file ) < 0 )
+        {
+            x264_free( h );
+            return NULL;
+        }
+
     if( h->param.rc.psz_stat_out )
         h->param.rc.psz_stat_out = strdup( h->param.rc.psz_stat_out );
     if( h->param.rc.psz_stat_in )
@@ -515,7 +547,9 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
 
     /* Init x264_t */
     h->out.i_nal = 0;
-    h->out.i_bitstream = 1000000; /* FIXME estimate max size (idth/height) */
+    h->out.i_bitstream = X264_MAX( 1000000, h->param.i_width * h->param.i_height * 1.7
+        * ( h->param.rc.b_cbr ? pow( 0.5, h->param.rc.i_qp_min )
+          : pow( 0.5, h->param.rc.i_qp_constant ) * X264_MAX( 1, h->param.rc.f_ip_factor )));
     h->out.p_bitstream = x264_malloc( h->out.i_bitstream );
 
     h->i_frame = 0;
@@ -528,11 +562,20 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
     h->pps = &h->pps_array[0];
     x264_pps_init( h->pps, 0, &h->param, h->sps);
 
+    x264_validate_levels( h );
+
     x264_cqm_init( h );
     
     h->mb.i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;
 
     /* Init frames. */
+    h->frames.i_delay = h->param.i_bframe;
+    h->frames.i_max_ref0 = h->param.i_frame_reference;
+    h->frames.i_max_ref1 = h->sps->vui.i_num_reorder_frames;
+    h->frames.i_max_dpb  = h->sps->vui.i_max_dec_frame_buffering + 1;
+    h->frames.b_have_lowres = !h->param.rc.b_stat_read
+        && ( h->param.rc.b_cbr || h->param.rc.i_rf_constant || h->param.b_bframe_adaptive );
+
     for( i = 0; i < X264_BFRAME_MAX + 3; i++ )
     {
         h->frames.current[i] = NULL;
@@ -557,11 +600,8 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
 
     h->fdec = h->frames.reference[0];
 
-    /* init mb cache */
     x264_macroblock_cache_init( h );
-
-    /* init cabac adaptive model */
-    x264_cabac_model_init( &h->cabac );
+    x264_rdo_init( );
 
     /* init CPU functions */
     x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
@@ -573,6 +613,8 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
     x264_dct_init( h->param.cpu, &h->dctf );
     x264_mc_init( h->param.cpu, &h->mc );
     x264_csp_init( h->param.cpu, h->param.i_csp, &h->csp );
+    x264_quant_init( h, h->param.cpu, &h->quantf );
+    x264_deblock_init( h->param.cpu, &h->loopf );
 
     memcpy( h->pixf.mbcmp,
             ( h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1 ) ? h->pixf.sad : h->pixf.satd,
@@ -591,6 +633,7 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
              param->cpu&X264_CPU_ALTIVEC ? "Altivec " : "" );
 
     h->thread[0] = h;
+    h->i_thread_num = 0;
     for( i = 1; i < param->i_threads; i++ )
         h->thread[i] = x264_malloc( sizeof(x264_t) );
 
@@ -608,11 +651,9 @@ int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
     h->param.analyse.i_me_method = param->analyse.i_me_method;
     h->param.analyse.i_me_range = param->analyse.i_me_range;
     h->param.analyse.i_subpel_refine = param->analyse.i_subpel_refine;
+    h->param.analyse.i_trellis = param->analyse.i_trellis;
     h->param.analyse.intra = param->analyse.intra;
     h->param.analyse.inter = param->analyse.inter;
-    if( h->sps->b_direct8x8_inference && h->param.i_bframe
-        && h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_TEMPORAL )
-        h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
 
     memcpy( h->pixf.mbcmp,
             ( h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1 ) ? h->pixf.sad : h->pixf.satd,
@@ -659,7 +700,7 @@ int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
     {
         /* identify ourself */
         x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
-        x264_sei_version_write( &h->out.bs );
+        x264_sei_version_write( h, &h->out.bs );
         x264_nal_end( h );
 
         /* generate sequence parameters */
@@ -718,9 +759,7 @@ static void x264_frame_sort( x264_frame_t *list[X264_BFRAME_MAX+1], int b_dts )
                              : dtime > 0;
             if( swap )
             {
-                x264_frame_t *tmp = list[i+1];
-                list[i+1] = list[i];
-                list[i] = tmp;
+                XCHG( x264_frame_t*, list[i], list[i+1] );
                 b_ok = 0;
             }
         }
@@ -760,10 +799,7 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc, int i_slice_
         {
             if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
             {
-                x264_frame_t *tmp = h->fref0[i+1];
-
-                h->fref0[i+1] = h->fref0[i];
-                h->fref0[i] = tmp;
+                XCHG( x264_frame_t*, h->fref0[i], h->fref0[i+1] );
                 b_ok = 0;
                 break;
             }
@@ -777,10 +813,7 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc, int i_slice_
         {
             if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
             {
-                x264_frame_t *tmp = h->fref1[i+1];
-
-                h->fref1[i+1] = h->fref1[i];
-                h->fref1[i] = tmp;
+                XCHG( x264_frame_t*, h->fref1[i], h->fref1[i+1] );
                 b_ok = 0;
                 break;
             }
@@ -828,18 +861,13 @@ static inline void x264_reference_update( x264_t *h )
 
     /* move lowres copy of the image to the ref frame */
     for( i = 0; i < 4; i++)
-    {
-        uint8_t *tmp = h->fdec->lowres[i];
-        h->fdec->lowres[i] = h->fenc->lowres[i];
-        h->fenc->lowres[i] = tmp;
-    }
+        XCHG( uint8_t*, h->fdec->lowres[i], h->fenc->lowres[i] );
 
     /* adaptive B decision needs a pointer, since it can't use the ref lists */
     if( h->sh.i_type != SLICE_TYPE_B )
         h->frames.last_nonb = h->fdec;
 
     /* move frame in the buffer */
-    /* FIXME: override to forget earliest pts, not earliest dts */
     h->fdec = h->frames.reference[h->frames.i_max_dpb-1];
     for( i = h->frames.i_max_dpb-1; i > 0; i-- )
     {
@@ -896,15 +924,6 @@ static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_slice_type,
         /* Nothing to do ? */
     }
 
-    /* get adapative cabac model if needed */
-    if( h->param.b_cabac )
-    {
-        if( h->param.i_cabac_init_idc == -1 )
-        {
-            h->sh.i_cabac_init_idc = x264_cabac_model_get( &h->cabac, i_slice_type );
-        }
-    }
-
     x264_macroblock_slice_init( h );
 }
 
@@ -912,6 +931,7 @@ static int x264_slice_write( x264_t *h )
 {
     int i_skip;
     int mb_xy;
+    int i;
 
     /* init stats */
     memset( &h->stat.frame, 0, sizeof(h->stat.frame) );
@@ -927,10 +947,10 @@ static int x264_slice_write( x264_t *h )
         bs_align_1( &h->out.bs );
 
         /* init cabac */
-        x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta, h->sh.i_cabac_init_idc );
+        x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.i_qp, h->sh.i_cabac_init_idc );
         x264_cabac_encode_init ( &h->cabac, &h->out.bs );
     }
-    h->mb.i_last_qp = h->pps->i_pic_init_qp + h->sh.i_qp_delta;
+    h->mb.i_last_qp = h->sh.i_qp;
     h->mb.i_last_dqp = 0;
 
     for( mb_xy = h->sh.i_first_mb, i_skip = 0; mb_xy < h->sh.i_last_mb; mb_xy++ )
@@ -995,7 +1015,25 @@ static int x264_slice_write( x264_t *h )
         /* save cache */
         x264_macroblock_cache_save( h );
 
+        /* accumulate mb stats */
         h->stat.frame.i_mb_count[h->mb.i_type]++;
+        if( !IS_SKIP(h->mb.i_type) && !IS_INTRA(h->mb.i_type) && !IS_DIRECT(h->mb.i_type) )
+        {
+            if( h->mb.i_partition != D_8x8 )
+                h->stat.frame.i_mb_count_size[ x264_mb_partition_pixel_table[ h->mb.i_partition ] ] += 4;
+            else
+                for( i = 0; i < 4; i++ )
+                    h->stat.frame.i_mb_count_size[ x264_mb_partition_pixel_table[ h->mb.i_sub_partition[i] ] ] ++;
+            if( h->param.i_frame_reference > 1 )
+            {
+                for( i = 0; i < 4; i++ )
+                {
+                    int i_ref = h->mb.cache.ref[0][ x264_scan8[4*i] ];
+                    if( i_ref >= 0 )
+                        h->stat.frame.i_mb_count_ref[i_ref] ++;
+                }
+            }
+        }
         if( h->mb.i_cbp_luma && !IS_INTRA(h->mb.i_type) )
         {
             h->stat.frame.i_mb_count_8x8dct[0] ++;
@@ -1018,16 +1056,8 @@ static int x264_slice_write( x264_t *h )
 
     if( h->param.b_cabac )
     {
-        int i_cabac_word;
         x264_cabac_encode_flush( &h->cabac );
-        /* TODO cabac stuffing things (p209) */
-        i_cabac_word = (((3 * h->cabac.i_sym_cnt - 3 * 96 * h->sps->i_mb_width * h->sps->i_mb_height)/32) - bs_pos( &h->out.bs)/8)/3;
 
-        while( i_cabac_word > 0 )
-        {
-            bs_write( &h->out.bs, 16, 0x0000 );
-            i_cabac_word--;
-        }
     }
     else
     {
@@ -1058,6 +1088,7 @@ static inline int x264_slices_write( x264_t *h )
 
     if( h->param.i_threads == 1 )
     {
+        x264_ratecontrol_threads_start( h );
         x264_slice_write( h );
         i_frame_size = h->out.nal[h->out.i_nal-1].i_payload;
     }
@@ -1075,11 +1106,13 @@ static inline int x264_slices_write( x264_t *h )
                 memcpy( t, h, sizeof(x264_t) );
                 t->out.p_bitstream += i*i_bs_size;
                 bs_init( &t->out.bs, t->out.p_bitstream, i_bs_size );
+                t->i_thread_num = i;
             }
             t->sh.i_first_mb = (i    * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width;
             t->sh.i_last_mb = ((i+1) * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width;
             t->out.i_nal = i_nal + i;
         }
+        x264_ratecontrol_threads_start( h );
 
         /* dispatch */
 #if HAVE_PTHREAD
@@ -1166,11 +1199,15 @@ int     x264_encoder_encode( x264_t *h,
 
         x264_frame_copy_picture( h, fenc, pic_in );
 
+        if( h->param.i_width % 16 || h->param.i_height % 16 )
+            x264_frame_expand_border_mod16( h, fenc );
+
         fenc->i_frame = h->frames.i_input++;
 
         x264_frame_put( h->frames.next, fenc );
 
-        x264_frame_init_lowres( h->param.cpu, fenc );
+        if( h->frames.b_have_lowres )
+            x264_frame_init_lowres( h->param.cpu, fenc );
 
         if( h->frames.i_input <= h->frames.i_delay )
         {
@@ -1285,11 +1322,8 @@ do_encode:
     if( i_slice_type == SLICE_TYPE_B )
         x264_macroblock_bipred_init( h );
 
-    /* increase frame num but only once for B frame */
-    if( i_slice_type != SLICE_TYPE_B || h->sh.i_type != SLICE_TYPE_B )
-    {
+    if( h->fenc->b_kept_as_ref )
         h->i_frame_num++;
-    }
 
     /* ------------------------ Create slice header  ----------------------- */
     x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );
@@ -1321,13 +1355,13 @@ do_encode:
     h->i_nal_ref_idc = i_nal_ref_idc;
 
     /* Write SPS and PPS */
-    if( i_nal_type == NAL_SLICE_IDR )
+    if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers )
     {
         if( h->fenc->i_frame == 0 )
         {
             /* identify ourself */
             x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
-            x264_sei_version_write( &h->out.bs );
+            x264_sei_version_write( h, &h->out.bs );
             x264_nal_end( h );
         }
 
@@ -1467,29 +1501,23 @@ do_encode:
     }
 
     /* ---------------------- Update encoder state ------------------------- */
-    /* update cabac */
-    if( h->param.b_cabac && h->param.i_cabac_init_idc == -1 )
-    {
-        x264_cabac_model_update( &h->cabac, i_slice_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta );
-    }
+
+    /* update rc */
+    x264_cpu_restore( h->param.cpu );
+    x264_ratecontrol_end( h, i_frame_size * 8 );
 
     /* handle references */
     if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
-    {
         x264_reference_update( h );
-    }
+    x264_frame_put( h->frames.unused, h->fenc );
 
     /* increase frame count */
     h->i_frame++;
 
     /* restore CPU state (before using float again) */
-    /* XXX: not needed? (done above) */
     x264_cpu_restore( h->param.cpu );
 
-    /* update rc */
-    x264_ratecontrol_end( h, i_frame_size * 8 );
-
-    x264_frame_put( h->frames.unused, h->fenc );
+    x264_noise_reduction_update( h );
 
     TIMER_STOP( i_mtime_encode_frame );
 
@@ -1503,15 +1531,38 @@ do_encode:
         h->stat.i_mb_count[h->sh.i_type][i] += h->stat.frame.i_mb_count[i];
     for( i = 0; i < 2; i++ )
         h->stat.i_mb_count_8x8dct[i] += h->stat.frame.i_mb_count_8x8dct[i];
+    if( h->sh.i_type != SLICE_TYPE_I )
+    {
+        for( i = 0; i < 7; i++ )
+            h->stat.i_mb_count_size[h->sh.i_type][i] += h->stat.frame.i_mb_count_size[i];
+        for( i = 0; i < 16; i++ )
+            h->stat.i_mb_count_ref[h->sh.i_type][i] += h->stat.frame.i_mb_count_ref[i];
+    }
+    if( i_slice_type == SLICE_TYPE_B )
+    {
+        h->stat.i_direct_frames[ h->sh.b_direct_spatial_mv_pred ] ++;
+        if( h->mb.b_direct_auto_write )
+        {
+            //FIXME somewhat arbitrary time constants
+            if( h->stat.i_direct_score[0] + h->stat.i_direct_score[1] > h->mb.i_mb_count )
+            {
+                for( i = 0; i < 2; i++ )
+                    h->stat.i_direct_score[i] = h->stat.i_direct_score[i] * 9/10;
+            }
+            for( i = 0; i < 2; i++ )
+                h->stat.i_direct_score[i] += h->stat.frame.i_direct_score[i];
+        }
+    }
 
     if( h->param.analyse.b_psnr )
     {
         int64_t i_sqe_y, i_sqe_u, i_sqe_v;
 
         /* PSNR */
-        i_sqe_y = x264_sqe( h, frame_psnr->plane[0], frame_psnr->i_stride[0], h->fenc->plane[0], h->fenc->i_stride[0], h->param.i_width, h->param.i_height );
-        i_sqe_u = x264_sqe( h, frame_psnr->plane[1], frame_psnr->i_stride[1], h->fenc->plane[1], h->fenc->i_stride[1], h->param.i_width/2, h->param.i_height/2);
-        i_sqe_v = x264_sqe( h, frame_psnr->plane[2], frame_psnr->i_stride[2], h->fenc->plane[2], h->fenc->i_stride[2], h->param.i_width/2, h->param.i_height/2);
+        i_sqe_y = x264_pixel_ssd_wxh( &h->pixf, frame_psnr->plane[0], frame_psnr->i_stride[0], h->fenc->plane[0], h->fenc->i_stride[0], h->param.i_width, h->param.i_height );
+        i_sqe_u = x264_pixel_ssd_wxh( &h->pixf, frame_psnr->plane[1], frame_psnr->i_stride[1], h->fenc->plane[1], h->fenc->i_stride[1], h->param.i_width/2, h->param.i_height/2);
+        i_sqe_v = x264_pixel_ssd_wxh( &h->pixf, frame_psnr->plane[2], frame_psnr->i_stride[2], h->fenc->plane[2], h->fenc->i_stride[2], h->param.i_width/2, h->param.i_height/2);
+        x264_cpu_restore( h->param.cpu );
 
         h->stat.i_sqe_global[i_slice_type] += i_sqe_y + i_sqe_u + i_sqe_v;
         h->stat.f_psnr_average[i_slice_type] += x264_psnr( i_sqe_y + i_sqe_u + i_sqe_v, 3 * h->param.i_width * h->param.i_height / 2 );
@@ -1565,16 +1616,6 @@ do_encode:
 #ifdef DEBUG_DUMP_FRAME
     /* Dump reconstructed frame */
     x264_frame_dump( h, frame_psnr, "fdec.yuv" );
-#endif
-#if 0
-    if( h->i_ref0 > 0 )
-    {
-        x264_frame_dump( h, h->fref0[0], "ref0.yuv" );
-    }
-    if( h->i_ref1 > 0 )
-    {
-        x264_frame_dump( h, h->fref1[0], "ref1.yuv" );
-    }
 #endif
     return 0;
 }
@@ -1612,7 +1653,7 @@ void    x264_encoder_close  ( x264_t *h )
             if( h->param.analyse.b_psnr )
             {
                 x264_log( h, X264_LOG_INFO,
-                          "slice %s:%-4d Avg QP:%5.2f Avg size:%6.0f PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f\n",
+                          "slice %s:%-5d Avg QP:%5.2f  size:%6.0f  PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f\n",
                           slice_name[i_slice],
                           i_count,
                           (double)h->stat.i_slice_qp[i_slice] / i_count,
@@ -1624,7 +1665,7 @@ void    x264_encoder_close  ( x264_t *h )
             else
             {
                 x264_log( h, X264_LOG_INFO,
-                          "slice %s:%-4d Avg QP:%5.2f Avg size:%6.0f\n",
+                          "slice %s:%-5d Avg QP:%5.2f  size:%6.0f\n",
                           slice_name[i_slice],
                           i_count,
                           (double)h->stat.i_slice_qp[i_slice] / i_count,
@@ -1639,40 +1680,47 @@ void    x264_encoder_close  ( x264_t *h )
         const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_I];
         const double i_count = h->stat.i_slice_count[SLICE_TYPE_I] * h->mb.i_mb_count / 100.0;
         x264_log( h, X264_LOG_INFO,
-                  "slice I   Avg I4x4:%.1f%%  I8x8:%.1f%%  I16x16:%.1f%%\n",
-                  i_mb_count[I_4x4]  / i_count,
+                  "mb I  I16..4: %4.1f%% %4.1f%% %4.1f%%\n",
+                  i_mb_count[I_16x16]/ i_count,
                   i_mb_count[I_8x8]  / i_count,
-                  i_mb_count[I_16x16]/ i_count );
+                  i_mb_count[I_4x4]  / i_count );
     }
     if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
     {
         const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_P];
+        const int64_t *i_mb_size = h->stat.i_mb_count_size[SLICE_TYPE_P];
         const double i_count = h->stat.i_slice_count[SLICE_TYPE_P] * h->mb.i_mb_count / 100.0;
         x264_log( h, X264_LOG_INFO,
-                  "slice P   Avg I4x4:%.1f%%  I8x8:%.1f%%  I16x16:%.1f%%  P:%.1f%%  P8x8:%.1f%%  PSKIP:%.1f%%\n",
-                  i_mb_count[I_4x4]  / i_count,
-                  i_mb_count[I_8x8]  / i_count,
+                  "mb P  I16..4: %4.1f%% %4.1f%% %4.1f%%  P16..4: %4.1f%% %4.1f%% %4.1f%% %4.1f%% %4.1f%%    skip:%4.1f%%\n",
                   i_mb_count[I_16x16]/ i_count,
-                  i_mb_count[P_L0]   / i_count,
-                  i_mb_count[P_8x8]  / i_count,
+                  i_mb_count[I_8x8]  / i_count,
+                  i_mb_count[I_4x4]  / i_count,
+                  i_mb_size[PIXEL_16x16] / (i_count*4),
+                  (i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4),
+                  i_mb_size[PIXEL_8x8] / (i_count*4),
+                  (i_mb_size[PIXEL_8x4] + i_mb_size[PIXEL_4x8]) / (i_count*4),
+                  i_mb_size[PIXEL_4x4] / (i_count*4),
                   i_mb_count[P_SKIP] / i_count );
     }
     if( h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
     {
         const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_B];
+        const int64_t *i_mb_size = h->stat.i_mb_count_size[SLICE_TYPE_B];
         const double i_count = h->stat.i_slice_count[SLICE_TYPE_B] * h->mb.i_mb_count / 100.0;
         x264_log( h, X264_LOG_INFO,
-                  "slice B   Avg I4x4:%.1f%%  I8x8:%.1f%%  I16x16:%.1f%%  P:%.1f%%  B:%.1f%%  B8x8:%.1f%%  DIRECT:%.1f%%  BSKIP:%.1f%%\n",
-                  i_mb_count[I_4x4]    / i_count,
-                  i_mb_count[I_8x8]    / i_count,
+                  "mb B  I16..4: %4.1f%% %4.1f%% %4.1f%%  B16..8: %4.1f%% %4.1f%% %4.1f%%  direct:%4.1f%%  skip:%4.1f%%\n",
                   i_mb_count[I_16x16]  / i_count,
-                  (i_mb_count[B_L0_L0] + i_mb_count[B_L1_L1] + i_mb_count[B_L1_L0] + i_mb_count[B_L0_L1]) / i_count,
-                  (i_mb_count[B_BI_BI] + i_mb_count[B_L0_BI] + i_mb_count[B_L1_BI] + i_mb_count[B_BI_L0] + i_mb_count[B_BI_L1]) / i_count,
-                  i_mb_count[B_8x8]    / i_count,
+                  i_mb_count[I_8x8]    / i_count,
+                  i_mb_count[I_4x4]    / i_count,
+                  i_mb_size[PIXEL_16x16] / (i_count*4),
+                  (i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4),
+                  i_mb_size[PIXEL_8x8] / (i_count*4),
                   i_mb_count[B_DIRECT] / i_count,
                   i_mb_count[B_SKIP]   / i_count );
     }
 
+    x264_ratecontrol_summary( h );
+
     if( h->stat.i_slice_count[SLICE_TYPE_I] + h->stat.i_slice_count[SLICE_TYPE_P] + h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
     {
         const int i_count = h->stat.i_slice_count[SLICE_TYPE_I] +
@@ -1686,15 +1734,47 @@ void    x264_encoder_close  ( x264_t *h )
         if( h->param.analyse.b_transform_8x8 )
         {
             int64_t i_i8x8 = SUM3b( h->stat.i_mb_count, I_8x8 );
-            int64_t i_intra = i_i8x8 + SUM3b( h->stat.i_mb_count, I_4x4 ) + SUM3b( h->stat.i_mb_count, I_16x16 );
+            int64_t i_intra = i_i8x8 + SUM3b( h->stat.i_mb_count, I_4x4 )
+                                     + SUM3b( h->stat.i_mb_count, I_16x16 );
             x264_log( h, X264_LOG_INFO, "8x8 transform  intra:%.1f%%  inter:%.1f%%\n",
                       100. * i_i8x8 / i_intra,
                       100. * h->stat.i_mb_count_8x8dct[1] / h->stat.i_mb_count_8x8dct[0] );
         }
 
+        if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
+            && h->stat.i_slice_count[SLICE_TYPE_B] )
+        {
+            x264_log( h, X264_LOG_INFO, "direct mvs  spatial:%.1f%%  temporal:%.1f%%\n",
+                      h->stat.i_direct_frames[1] * 100. / h->stat.i_slice_count[SLICE_TYPE_B],
+                      h->stat.i_direct_frames[0] * 100. / h->stat.i_slice_count[SLICE_TYPE_B] );
+        }
+
+        if( h->param.i_frame_reference > 1 )
+        {
+            int i_slice;
+            for( i_slice = 0; i_slice < 2; i_slice++ )
+            {
+                char buf[200];
+                char *p = buf;
+                int64_t i_den = 0;
+                int i_max = 0;
+                for( i = 0; i < h->param.i_frame_reference; i++ )
+                    if( h->stat.i_mb_count_ref[i_slice][i] )
+                    {
+                        i_den += h->stat.i_mb_count_ref[i_slice][i];
+                        i_max = i;
+                    }
+                if( i_max == 0 )
+                    continue;
+                for( i = 0; i <= i_max; i++ )
+                    p += sprintf( p, " %4.1f%%", 100. * h->stat.i_mb_count_ref[i_slice][i] / i_den );
+                x264_log( h, X264_LOG_INFO, "ref %c %s\n", i_slice==SLICE_TYPE_P ? 'P' : 'B', buf );
+            }
+        }
+
         if( h->param.analyse.b_psnr )
             x264_log( h, X264_LOG_INFO,
-                      "PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f kb/s:%.1f\n",
+                      "PSNR Mean Y:%6.3f U:%6.3f V:%6.3f Avg:%6.3f Global:%6.3f kb/s:%.2f\n",
                       SUM3( h->stat.f_psnr_mean_y ) / i_count,
                       SUM3( h->stat.f_psnr_mean_u ) / i_count,
                       SUM3( h->stat.f_psnr_mean_v ) / i_count,