]> git.sesse.net Git - x264/blobdiff - encoder/set.c
Fix zeroing of mvr predictors in bskip blocks
[x264] / encoder / set.c
index a0030128347d9686650760f8003428baa1ece9bf..09be96e5a45db639ee6507e43d9146399dd2a9cf 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * set: header writing
  *****************************************************************************
- * Copyright (C) 2003-2010 x264 project
+ * Copyright (C) 2003-2011 x264 project
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Loren Merritt <lorenm@u.washington.edu>
@@ -24,8 +24,6 @@
  * For more information, contact us at licensing@x264.com.
  *****************************************************************************/
 
-#include <math.h>
-
 #include "common/common.h"
 #include "set.h"
 
@@ -48,18 +46,20 @@ static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
     const uint8_t *list = pps->scaling_list[idx];
     const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
                             : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
+                            : (idx==CQM_8IC+4) ? pps->scaling_list[CQM_8IY+4]
+                            : (idx==CQM_8PC+4) ? pps->scaling_list[CQM_8PY+4]
                             : x264_cqm_jvt[idx];
     if( !memcmp( list, def_list, len ) )
-        bs_write( s, 1, 0 ); // scaling_list_present_flag
+        bs_write1( s, 0 );   // scaling_list_present_flag
     else if( !memcmp( list, x264_cqm_jvt[idx], len ) )
     {
-        bs_write( s, 1, 1 ); // scaling_list_present_flag
+        bs_write1( s, 1 );   // scaling_list_present_flag
         bs_write_se( s, -8 ); // use jvt list
     }
     else
     {
         int run;
-        bs_write( s, 1, 1 ); // scaling_list_present_flag
+        bs_write1( s, 1 );   // scaling_list_present_flag
 
         // try run-length compression of trailing values
         for( run = len; run > 1; run-- )
@@ -99,12 +99,15 @@ void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_ty
 
 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
 {
+    int csp = param->i_csp & X264_CSP_MASK;
+
     sps->i_id = i_id;
     sps->i_mb_width = ( param->i_width + 15 ) / 16;
     sps->i_mb_height= ( param->i_height + 15 ) / 16;
+    sps->i_chroma_format_idc = csp >= X264_CSP_I444 ? 3 : 1;
 
     sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
-    if( sps->b_qpprime_y_zero_transform_bypass )
+    if( sps->b_qpprime_y_zero_transform_bypass || sps->i_chroma_format_idc == 3 )
         sps->i_profile_idc  = PROFILE_HIGH444_PREDICTIVE;
     else if( BIT_DEPTH > 8 )
         sps->i_profile_idc  = PROFILE_HIGH10;
@@ -121,17 +124,20 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     sps->b_constraint_set1  = sps->i_profile_idc <= PROFILE_MAIN;
     /* Never set constraint_set2, it is not necessary and not used in real world. */
     sps->b_constraint_set2  = 0;
+    sps->b_constraint_set3  = 0;
 
+    sps->i_level_idc = param->i_level_idc;
     if( param->i_level_idc == 9 && ( sps->i_profile_idc >= PROFILE_BASELINE && sps->i_profile_idc <= PROFILE_EXTENDED ) )
     {
         sps->b_constraint_set3 = 1; /* level 1b with Baseline, Main or Extended profile is signalled via constraint_set3 */
         sps->i_level_idc      = 11;
     }
-    else
-    {
-        sps->b_constraint_set3 = 0;
-        sps->i_level_idc = param->i_level_idc;
-    }
+    /* High 10 Intra profile */
+    if( param->i_keyint_max == 1 && sps->i_profile_idc == PROFILE_HIGH10 )
+        sps->b_constraint_set3 = 1;
+    /* High 4:4:4 Intra profile */
+    if( param->i_keyint_max == 1 && sps->i_profile_idc == PROFILE_HIGH444_PREDICTIVE )
+        sps->b_constraint_set3 = 1;
 
     sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
     /* extra slot with pyramid so that we don't have to override the
@@ -140,6 +146,11 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     sps->i_num_ref_frames = X264_MIN(X264_REF_MAX, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
                             param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
     sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
+    if( param->i_keyint_max == 1 )
+    {
+        sps->i_num_ref_frames = 0;
+        sps->vui.i_max_dec_frame_buffering = 0;
+    }
 
     /* number of refs + current frame */
     int max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
@@ -154,7 +165,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
         sps->i_log2_max_frame_num++;
 
-    sps->i_poc_type = param->i_bframe ? 0 : 2;
+    sps->i_poc_type = param->i_bframe || param->b_interlaced ? 0 : 2;
     if( sps->i_poc_type == 0 )
     {
         int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
@@ -162,21 +173,6 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
         while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
             sps->i_log2_max_poc_lsb++;
     }
-    else if( sps->i_poc_type == 1 )
-    {
-        int i;
-
-        /* FIXME */
-        sps->b_delta_pic_order_always_zero = 1;
-        sps->i_offset_for_non_ref_pic = 0;
-        sps->i_offset_for_top_to_bottom_field = 0;
-        sps->i_num_ref_frames_in_poc_cycle = 0;
-
-        for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
-        {
-            sps->i_offset_for_ref_frame[i] = 0;
-        }
-    }
 
     sps->b_vui = 1;
 
@@ -187,10 +183,10 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     sps->b_mb_adaptive_frame_field = param->b_interlaced;
     sps->b_direct8x8_inference = 1;
 
-    sps->crop.i_left   = 0;
-    sps->crop.i_top    = 0;
-    sps->crop.i_right  = sps->i_mb_width*16 - param->i_width;
-    sps->crop.i_bottom = (sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
+    sps->crop.i_left   = param->crop_rect.i_left;
+    sps->crop.i_top    = param->crop_rect.i_top;
+    sps->crop.i_right  = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
+    sps->crop.i_bottom = (param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
     sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
                   sps->crop.i_right || sps->crop.i_bottom;
 
@@ -202,18 +198,20 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
         sps->vui.i_sar_height= param->vui.i_sar_height;
     }
 
-    sps->vui.b_overscan_info_present = ( param->vui.i_overscan ? 1 : 0 );
+    sps->vui.b_overscan_info_present = param->vui.i_overscan > 0 && param->vui.i_overscan <= 2;
     if( sps->vui.b_overscan_info_present )
         sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
 
     sps->vui.b_signal_type_present = 0;
-    sps->vui.i_vidformat = ( param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
-    sps->vui.b_fullrange = ( param->vui.b_fullrange ? 1 : 0 );
+    sps->vui.i_vidformat = ( param->vui.i_vidformat >= 0 && param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
+    sps->vui.b_fullrange = ( param->vui.b_fullrange >= 0 && param->vui.b_fullrange <= 1 ? param->vui.b_fullrange :
+                           ( csp >= X264_CSP_BGR ? 1 : 0 ) );
     sps->vui.b_color_description_present = 0;
 
-    sps->vui.i_colorprim = ( param->vui.i_colorprim <=  9 ? param->vui.i_colorprim : 2 );
-    sps->vui.i_transfer  = ( param->vui.i_transfer  <= 11 ? param->vui.i_transfer  : 2 );
-    sps->vui.i_colmatrix = ( param->vui.i_colmatrix <=  9 ? param->vui.i_colmatrix : 2 );
+    sps->vui.i_colorprim = ( param->vui.i_colorprim >= 0 && param->vui.i_colorprim <=  8 ? param->vui.i_colorprim : 2 );
+    sps->vui.i_transfer  = ( param->vui.i_transfer  >= 0 && param->vui.i_transfer  <= 10 ? param->vui.i_transfer  : 2 );
+    sps->vui.i_colmatrix = ( param->vui.i_colmatrix >= 0 && param->vui.i_colmatrix <=  8 ? param->vui.i_colmatrix :
+                           ( csp >= X264_CSP_BGR ? 0 : 2 ) );
     if( sps->vui.i_colorprim != 2 ||
         sps->vui.i_transfer  != 2 ||
         sps->vui.i_colmatrix != 2 )
@@ -229,7 +227,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     }
 
     /* FIXME: not sufficient for interlaced video */
-    sps->vui.b_chroma_loc_info_present = ( param->vui.i_chroma_loc ? 1 : 0 );
+    sps->vui.b_chroma_loc_info_present = param->vui.i_chroma_loc > 0 && param->vui.i_chroma_loc <= 5;
     if( sps->vui.b_chroma_loc_info_present )
     {
         sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
@@ -266,10 +264,10 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
 {
     bs_realign( s );
     bs_write( s, 8, sps->i_profile_idc );
-    bs_write( s, 1, sps->b_constraint_set0 );
-    bs_write( s, 1, sps->b_constraint_set1 );
-    bs_write( s, 1, sps->b_constraint_set2 );
-    bs_write( s, 1, sps->b_constraint_set3 );
+    bs_write1( s, sps->b_constraint_set0 );
+    bs_write1( s, sps->b_constraint_set1 );
+    bs_write1( s, sps->b_constraint_set2 );
+    bs_write1( s, sps->b_constraint_set3 );
 
     bs_write( s, 4, 0 );    /* reserved */
 
@@ -279,48 +277,39 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
 
     if( sps->i_profile_idc >= PROFILE_HIGH )
     {
-        bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
+        bs_write_ue( s, sps->i_chroma_format_idc );
+        if( sps->i_chroma_format_idc == 3 )
+            bs_write1( s, 0 ); // separate_colour_plane_flag
         bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_luma_minus8
         bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_chroma_minus8
-        bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
-        bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
+        bs_write1( s, sps->b_qpprime_y_zero_transform_bypass );
+        bs_write1( s, 0 ); // seq_scaling_matrix_present_flag
     }
 
     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
     bs_write_ue( s, sps->i_poc_type );
     if( sps->i_poc_type == 0 )
-    {
         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
-    }
-    else if( sps->i_poc_type == 1 )
-    {
-        bs_write( s, 1, sps->b_delta_pic_order_always_zero );
-        bs_write_se( s, sps->i_offset_for_non_ref_pic );
-        bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
-        bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
-
-        for( int i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
-            bs_write_se( s, sps->i_offset_for_ref_frame[i] );
-    }
     bs_write_ue( s, sps->i_num_ref_frames );
-    bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
+    bs_write1( s, sps->b_gaps_in_frame_num_value_allowed );
     bs_write_ue( s, sps->i_mb_width - 1 );
     bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
-    bs_write( s, 1, sps->b_frame_mbs_only );
+    bs_write1( s, sps->b_frame_mbs_only );
     if( !sps->b_frame_mbs_only )
-        bs_write( s, 1, sps->b_mb_adaptive_frame_field );
-    bs_write( s, 1, sps->b_direct8x8_inference );
+        bs_write1( s, sps->b_mb_adaptive_frame_field );
+    bs_write1( s, sps->b_direct8x8_inference );
 
-    bs_write( s, 1, sps->b_crop );
+    bs_write1( s, sps->b_crop );
     if( sps->b_crop )
     {
-        bs_write_ue( s, sps->crop.i_left   / 2 );
-        bs_write_ue( s, sps->crop.i_right  / 2 );
-        bs_write_ue( s, sps->crop.i_top    / 2 );
-        bs_write_ue( s, sps->crop.i_bottom / 2 );
+        int cropshift = sps->i_chroma_format_idc != 3;
+        bs_write_ue( s, sps->crop.i_left   >> cropshift );
+        bs_write_ue( s, sps->crop.i_right  >> cropshift );
+        bs_write_ue( s, sps->crop.i_top    >> cropshift );
+        bs_write_ue( s, sps->crop.i_bottom >> cropshift );
     }
 
-    bs_write( s, 1, sps->b_vui );
+    bs_write1( s, sps->b_vui );
     if( sps->b_vui )
     {
         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
@@ -329,10 +318,13 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
             int i;
             static const struct { uint8_t w, h, sar; } sar[] =
             {
-                { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
+                // aspect_ratio_idc = 0 -> unspecified
+                {  1,  1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
-                { 160,99, 13}, { 0, 0, 255 }
+                {160, 99, 13}, {  4,  3, 14}, {  3,  2, 15}, {  2,  1, 16},
+                // aspect_ratio_idc = [17..254] -> reserved
+                { 0, 0, 255 }
             };
             for( i = 0; sar[i].sar != 255; i++ )
             {
@@ -437,8 +429,8 @@ void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *
     pps->b_weighted_pred = param->analyse.i_weighted_pred > 0;
     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
 
-    pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR ? 26 : param->rc.i_qp_constant;
-    pps->i_pic_init_qs = 26;
+    pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR ? 26 + QP_BD_OFFSET : SPEC_QP( param->rc.i_qp_constant );
+    pps->i_pic_init_qs = 26 + QP_BD_OFFSET;
 
     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
     pps->b_deblocking_filter_control = 1;
@@ -448,31 +440,36 @@ void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *
     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
 
     pps->i_cqm_preset = param->i_cqm_preset;
+
     switch( pps->i_cqm_preset )
     {
     case X264_CQM_FLAT:
-        for( int i = 0; i < 6; i++ )
+        for( int i = 0; i < 8; i++ )
             pps->scaling_list[i] = x264_cqm_flat16;
         break;
     case X264_CQM_JVT:
-        for( int i = 0; i < 6; i++ )
+        for( int i = 0; i < 8; i++ )
             pps->scaling_list[i] = x264_cqm_jvt[i];
         break;
     case X264_CQM_CUSTOM:
         /* match the transposed DCT & zigzag */
         transpose( param->cqm_4iy, 4 );
-        transpose( param->cqm_4ic, 4 );
         transpose( param->cqm_4py, 4 );
+        transpose( param->cqm_4ic, 4 );
         transpose( param->cqm_4pc, 4 );
         transpose( param->cqm_8iy, 8 );
         transpose( param->cqm_8py, 8 );
+        transpose( param->cqm_8ic, 8 );
+        transpose( param->cqm_8pc, 8 );
         pps->scaling_list[CQM_4IY] = param->cqm_4iy;
-        pps->scaling_list[CQM_4IC] = param->cqm_4ic;
         pps->scaling_list[CQM_4PY] = param->cqm_4py;
+        pps->scaling_list[CQM_4IC] = param->cqm_4ic;
         pps->scaling_list[CQM_4PC] = param->cqm_4pc;
         pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
         pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
-        for( int i = 0; i < 6; i++ )
+        pps->scaling_list[CQM_8IC+4] = param->cqm_8ic;
+        pps->scaling_list[CQM_8PC+4] = param->cqm_8pc;
+        for( int i = 0; i < 8; i++ )
             for( int j = 0; j < (i < 4 ? 16 : 64); j++ )
                 if( pps->scaling_list[i][j] == 0 )
                     pps->scaling_list[i] = x264_cqm_jvt[i];
@@ -480,45 +477,57 @@ void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *
     }
 }
 
-void x264_pps_write( bs_t *s, x264_pps_t *pps )
+void x264_pps_write( bs_t *s, x264_sps_t *sps, x264_pps_t *pps )
 {
     bs_realign( s );
     bs_write_ue( s, pps->i_id );
     bs_write_ue( s, pps->i_sps_id );
 
-    bs_write( s, 1, pps->b_cabac );
-    bs_write( s, 1, pps->b_pic_order );
+    bs_write1( s, pps->b_cabac );
+    bs_write1( s, pps->b_pic_order );
     bs_write_ue( s, pps->i_num_slice_groups - 1 );
 
     bs_write_ue( s, pps->i_num_ref_idx_l0_default_active - 1 );
     bs_write_ue( s, pps->i_num_ref_idx_l1_default_active - 1 );
-    bs_write( s, 1, pps->b_weighted_pred );
+    bs_write1( s, pps->b_weighted_pred );
     bs_write( s, 2, pps->b_weighted_bipred );
 
     bs_write_se( s, pps->i_pic_init_qp - 26 - QP_BD_OFFSET );
-    bs_write_se( s, pps->i_pic_init_qs - 26 );
+    bs_write_se( s, pps->i_pic_init_qs - 26 - QP_BD_OFFSET );
     bs_write_se( s, pps->i_chroma_qp_index_offset );
 
-    bs_write( s, 1, pps->b_deblocking_filter_control );
-    bs_write( s, 1, pps->b_constrained_intra_pred );
-    bs_write( s, 1, pps->b_redundant_pic_cnt );
+    bs_write1( s, pps->b_deblocking_filter_control );
+    bs_write1( s, pps->b_constrained_intra_pred );
+    bs_write1( s, pps->b_redundant_pic_cnt );
 
     if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
     {
-        bs_write( s, 1, pps->b_transform_8x8_mode );
-        bs_write( s, 1, (pps->i_cqm_preset != X264_CQM_FLAT) );
+        bs_write1( s, pps->b_transform_8x8_mode );
+        bs_write1( s, (pps->i_cqm_preset != X264_CQM_FLAT) );
         if( pps->i_cqm_preset != X264_CQM_FLAT )
         {
             scaling_list_write( s, pps, CQM_4IY );
             scaling_list_write( s, pps, CQM_4IC );
-            bs_write( s, 1, 0 ); // Cr = Cb
+            bs_write1( s, 0 ); // Cr = Cb
             scaling_list_write( s, pps, CQM_4PY );
             scaling_list_write( s, pps, CQM_4PC );
-            bs_write( s, 1, 0 ); // Cr = Cb
+            bs_write1( s, 0 ); // Cr = Cb
             if( pps->b_transform_8x8_mode )
             {
-                scaling_list_write( s, pps, CQM_8IY+4 );
-                scaling_list_write( s, pps, CQM_8PY+4 );
+                if( sps->i_chroma_format_idc == 3 )
+                {
+                    scaling_list_write( s, pps, CQM_8IY+4 );
+                    scaling_list_write( s, pps, CQM_8IC+4 );
+                    bs_write1( s, 0 ); // Cr = Cb
+                    scaling_list_write( s, pps, CQM_8PY+4 );
+                    scaling_list_write( s, pps, CQM_8PC+4 );
+                    bs_write1( s, 0 ); // Cr = Cb
+                }
+                else
+                {
+                    scaling_list_write( s, pps, CQM_8IY+4 );
+                    scaling_list_write( s, pps, CQM_8PY+4 );
+                }
             }
         }
         bs_write_se( s, pps->i_chroma_qp_index_offset );
@@ -537,15 +546,14 @@ void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
     bs_realign( &q );
 
     bs_write_ue( &q, recovery_frame_cnt ); // recovery_frame_cnt
-    bs_write( &q, 1, 1 ); //exact_match_flag 1
-    bs_write( &q, 1, 0 ); //broken_link_flag 0
+    bs_write1( &q, 1 );   //exact_match_flag 1
+    bs_write1( &q, 0 );   //broken_link_flag 0
     bs_write( &q, 2, 0 ); //changing_slice_group 0
 
     bs_align_10( &q );
     bs_flush( &q );
 
     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_RECOVERY_POINT );
-
 }
 
 int x264_sei_version_write( x264_t *h, bs_t *s )
@@ -566,8 +574,8 @@ int x264_sei_version_write( x264_t *h, bs_t *s )
 
     memcpy( payload, uuid, 16 );
     sprintf( payload+16, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
-             "Copyleft 2003-2010 - http://www.videolan.org/x264.html - options: %s",
-             X264_BUILD, X264_VERSION, opts );
+             "Copy%s 2003-2011 - http://www.videolan.org/x264.html - options: %s",
+             X264_BUILD, X264_VERSION, HAVE_GPL?"left":"right", opts );
     length = strlen(payload)+1;
 
     x264_sei_write( s, (uint8_t *)payload, length, SEI_USER_DATA_UNREGISTERED );
@@ -613,7 +621,7 @@ void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
 
     if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
     {
-        bs_write( &q, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay );
+        bs_write( &q, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay - h->i_cpb_delay_pir_offset );
         bs_write( &q, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_delay );
     }
 
@@ -633,6 +641,45 @@ void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_PIC_TIMING );
 }
 
+void x264_sei_frame_packing_write( x264_t *h, bs_t *s )
+{
+    bs_t q;
+    uint8_t tmp_buf[100];
+    bs_init( &q, tmp_buf, 100 );
+
+    bs_realign( &q );
+
+    bs_write_ue( &q, 0 );                         // frame_packing_arrangement_id
+    bs_write1( &q, 0 );                           // frame_packing_arrangement_cancel_flag
+    bs_write ( &q, 7, h->param.i_frame_packing ); // frame_packing_arrangement_type
+    bs_write1( &q, 0 );                           // quincunx_sampling_flag
+
+    // 0: views are unrelated, 1: left view is on the left, 2: left view is on the right
+    bs_write ( &q, 6, 1 );                        // content_interpretation_type
+
+    bs_write1( &q, 0 );                           // spatial_flipping_flag
+    bs_write1( &q, 0 );                           // frame0_flipped_flag
+    bs_write1( &q, 0 );                           // field_views_flag
+    bs_write1( &q, h->param.i_frame_packing == 5 && !(h->fenc->i_frame&1) ); // current_frame_is_frame0_flag
+    bs_write1( &q, 0 );                           // frame0_self_contained_flag
+    bs_write1( &q, 0 );                           // frame1_self_contained_flag
+    if ( /* quincunx_sampling_flag == 0 && */ h->param.i_frame_packing != 5 )
+    {
+        bs_write( &q, 4, 0 );                     // frame0_grid_position_x
+        bs_write( &q, 4, 0 );                     // frame0_grid_position_y
+        bs_write( &q, 4, 0 );                     // frame1_grid_position_x
+        bs_write( &q, 4, 0 );                     // frame1_grid_position_y
+    }
+    bs_write( &q, 8, 0 );                         // frame_packing_arrangement_reserved_byte
+    bs_write_ue( &q, 1 );                         // frame_packing_arrangement_repetition_period
+    bs_write1( &q, 0 );                           // frame_packing_arrangement_extension_flag
+
+    bs_align_10( &q );
+    bs_flush( &q );
+
+    x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_FRAME_PACKING );
+}
+
 void x264_filler_write( x264_t *h, bs_t *s, int filler )
 {
     bs_realign( s );
@@ -644,6 +691,38 @@ void x264_filler_write( x264_t *h, bs_t *s, int filler )
     bs_flush( s );
 }
 
+void x264_sei_dec_ref_pic_marking_write( x264_t *h, bs_t *s )
+{
+    x264_slice_header_t *sh = &h->sh_backup;
+    bs_t q;
+    uint8_t tmp_buf[100];
+    bs_init( &q, tmp_buf, 100 );
+
+    bs_realign( &q );
+
+    /* We currently only use this for repeating B-refs, as required by Blu-ray. */
+    bs_write1( &q, 0 );                 //original_idr_flag
+    bs_write_ue( &q, sh->i_frame_num ); //original_frame_num
+    if( !h->sps->b_frame_mbs_only )
+        bs_write1( &q, 0 );             //original_field_pic_flag
+
+    bs_write1( &q, sh->i_mmco_command_count > 0 );
+    if( sh->i_mmco_command_count > 0 )
+    {
+        for( int i = 0; i < sh->i_mmco_command_count; i++ )
+        {
+            bs_write_ue( &q, 1 );
+            bs_write_ue( &q, sh->mmco[i].i_difference_of_pic_nums - 1 );
+        }
+        bs_write_ue( &q, 0 );
+    }
+
+    bs_align_10( &q );
+    bs_flush( &q );
+
+    x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_DEC_REF_PIC_MARKING );
+}
+
 const x264_level_t x264_levels[] =
 {
     { 10,   1485,    99,   152064,     64,    175,  64, 64,  0, 2, 0, 0, 1 },
@@ -677,7 +756,8 @@ int x264_validate_levels( x264_t *h, int verbose )
     int ret = 0;
     int mbs = h->sps->i_mb_width * h->sps->i_mb_height;
     int dpb = mbs * 384 * h->sps->vui.i_max_dec_frame_buffering;
-    int cbp_factor = h->sps->i_profile_idc==PROFILE_HIGH10 ? 12 :
+    int cbp_factor = h->sps->i_profile_idc==PROFILE_HIGH444_PREDICTIVE ? 16 :
+                     h->sps->i_profile_idc==PROFILE_HIGH10 ? 12 :
                      h->sps->i_profile_idc==PROFILE_HIGH ? 5 : 4;
 
     const x264_level_t *l = x264_levels;