Bump dates to 2011
[x262.git] / encoder / set.c
index e0c9a1c..b02772a 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>
@@ -76,27 +76,25 @@ static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
     }
 }
 
-static uint8_t *x264_sei_write_header( bs_t *s, int payload_type )
+void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_type )
 {
-    bs_write( s, 8, payload_type );
+    int i;
 
-    bs_flush( s );
-    uint8_t *p_start = s->p;
     bs_realign( s );
 
-    bs_write( s, 8, 0 );
-    return p_start;
-}
+    for( i = 0; i <= payload_type-255; i += 255 )
+        bs_write( s, 8, 255 );
+    bs_write( s, 8, payload_type-i );
 
-static void x264_sei_write( bs_t *s, uint8_t *p_start )
-{
-    bs_align_10( s );
-    bs_flush( s );
+    for( i = 0; i <= payload_size-255; i += 255 )
+        bs_write( s, 8, 255 );
+    bs_write( s, 8, payload_size-i );
 
-    p_start[0] = s->p - p_start - 1; // -1 for the length byte
-    bs_realign( s );
+    for( i = 0; i < payload_size; i++ )
+        bs_write(s, 8, payload[i] );
 
     bs_rbsp_trailing( s );
+    bs_flush( s );
 }
 
 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
@@ -123,17 +121,17 @@ 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;
 
     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
@@ -142,6 +140,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;
@@ -156,7 +159,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;
@@ -189,10 +192,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;
 
@@ -296,34 +299,21 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
     }
     else if( sps->i_poc_type == 1 )
     {
-        int i;
-
         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( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
-        {
+        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_write_ue( s, sps->i_mb_width - 1 );
-    if (sps->b_frame_mbs_only)
-    {
-        bs_write_ue( s, sps->i_mb_height - 1);
-    }
-    else // interlaced
-    {
-        bs_write_ue( s, sps->i_mb_height/2 - 1);
-    }
+    bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
     bs_write( s, 1, 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_write( s, 1, sps->b_crop );
@@ -452,8 +442,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 : 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;
@@ -511,7 +501,7 @@ void x264_pps_write( bs_t *s, x264_pps_t *pps )
     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 );
@@ -545,21 +535,26 @@ void x264_pps_write( bs_t *s, x264_pps_t *pps )
 
 void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
 {
-    bs_realign( s );
-    uint8_t *p_start = x264_sei_write_header( s, SEI_RECOVERY_POINT );
+    bs_t q;
+    uint8_t tmp_buf[100];
+    bs_init( &q, tmp_buf, 100 );
 
-    bs_write_ue( s, recovery_frame_cnt ); // recovery_frame_cnt
-    bs_write( s, 1, 1 ); //exact_match_flag 1
-    bs_write( s, 1, 0 ); //broken_link_flag 0
-    bs_write( s, 2, 0 ); //changing_slice_group 0
+    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_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 );
 
-    x264_sei_write( s, p_start );
-    bs_flush( s );
 }
 
 int x264_sei_version_write( x264_t *h, bs_t *s )
 {
-    int i;
     // random ID number generated according to ISO-11578
     static const uint8_t uuid[16] =
     {
@@ -567,35 +562,23 @@ int x264_sei_version_write( x264_t *h, bs_t *s )
         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
     };
     char *opts = x264_param2string( &h->param, 0 );
-    char *version;
+    char *payload;
     int length;
 
     if( !opts )
         return -1;
-    CHECKED_MALLOC( version, 200 + strlen( opts ) );
+    CHECKED_MALLOC( payload, 200 + strlen( opts ) );
 
-    sprintf( version, "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 );
-    length = strlen(version)+1+16;
+    memcpy( payload, uuid, 16 );
+    sprintf( payload+16, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
+             "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;
 
-    bs_realign( s );
-    bs_write( s, 8, SEI_USER_DATA_UNREGISTERED );
-    // payload_size
-    for( i = 0; i <= length-255; i += 255 )
-        bs_write( s, 8, 255 );
-    bs_write( s, 8, length-i );
-
-    for( int j = 0; j < 16; j++ )
-        bs_write( s, 8, uuid[j] );
-    for( int j = 0; j < length-16; j++ )
-        bs_write( s, 8, version[j] );
-
-    bs_rbsp_trailing( s );
-    bs_flush( s );
+    x264_sei_write( s, (uint8_t *)payload, length, SEI_USER_DATA_UNREGISTERED );
 
     x264_free( opts );
-    x264_free( version );
+    x264_free( payload );
     return 0;
 fail:
     x264_free( opts );
@@ -605,45 +588,93 @@ fail:
 void x264_sei_buffering_period_write( x264_t *h, bs_t *s )
 {
     x264_sps_t *sps = h->sps;
-    bs_realign( s );
-    uint8_t *p_start = x264_sei_write_header( s, SEI_BUFFERING_PERIOD );
+    bs_t q;
+    uint8_t tmp_buf[100];
+    bs_init( &q, tmp_buf, 100 );
 
-    bs_write_ue( s, sps->i_id );
+    bs_realign( &q );
+    bs_write_ue( &q, sps->i_id );
 
     if( sps->vui.b_nal_hrd_parameters_present )
     {
-        bs_write( s, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
-        bs_write( s, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
+        bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
+        bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
     }
 
-    x264_sei_write( s, p_start );
-    bs_flush( s );
+    bs_align_10( &q );
+    bs_flush( &q );
+
+    x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_BUFFERING_PERIOD );
 }
 
 void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
 {
     x264_sps_t *sps = h->sps;
-    bs_realign( s );
-    uint8_t *p_start = x264_sei_write_header( s, SEI_PIC_TIMING );
+    bs_t q;
+    uint8_t tmp_buf[100];
+    bs_init( &q, tmp_buf, 100 );
+
+    bs_realign( &q );
 
     if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
     {
-        bs_write( s, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay );
-        bs_write( s, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_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 );
     }
 
     if( sps->vui.b_pic_struct_present )
     {
-        bs_write( s, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
+        bs_write( &q, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
 
         // These clock timestamps are not standardised so we don't set them
         // They could be time of origin, capture or alternative ideal display
         for( int i = 0; i < num_clock_ts[h->fenc->i_pic_struct]; i++ )
-            bs_write1( s, 0 ); // clock_timestamp_flag
+            bs_write1( &q, 0 ); // clock_timestamp_flag
     }
 
-    x264_sei_write( s, p_start );
-    bs_flush( s );
+    bs_align_10( &q );
+    bs_flush( &q );
+
+    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 )