]> git.sesse.net Git - x264/blobdiff - encoder/encoder.c
copy current macroblock to a smaller buffer, to improve cache coherency and reduce...
[x264] / encoder / encoder.c
index c57555f22db9e3857c64b1fa3e65feb0585bc66a..3adaa312ac967b7ce935a737801b55a4b8225f58 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>
@@ -352,6 +351,8 @@ 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 );
@@ -365,6 +366,10 @@ static int x264_validate_parameters( x264_t *h )
         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 )
@@ -400,6 +405,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 )
@@ -408,13 +418,45 @@ 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;
 
+    /* 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;
 }
 
@@ -510,6 +552,8 @@ 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;
@@ -546,8 +590,8 @@ x264_t *x264_encoder_open   ( x264_param_t *param )
 
     h->fdec = h->frames.reference[0];
 
-    /* init mb cache */
     x264_macroblock_cache_init( h );
+    x264_rdo_init( );
 
     /* init CPU functions */
     x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
@@ -596,11 +640,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,
@@ -647,7 +689,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 */
@@ -1299,13 +1341,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 );
         }
 
@@ -1464,6 +1506,8 @@ do_encode:
 
     x264_frame_put( h->frames.unused, h->fenc );
 
+    x264_noise_reduction_update( h );
+
     TIMER_STOP( i_mtime_encode_frame );
 
     /* ---------------------- Compute/Print statistics --------------------- */
@@ -1649,6 +1693,8 @@ void    x264_encoder_close  ( x264_t *h )
                   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] +