]> git.sesse.net Git - x264/blobdiff - encoder/set.c
fix an arithmetic overflow in trellis at high qp.
[x264] / encoder / set.c
index eb59b8668cf05815f96ba23617b2ab380e279710..04fe82613b237120a3cbb6a102a6907e50179e5a 100644 (file)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#ifdef HAVE_STDINT_H
-#include <stdint.h>
-#else
-#include <inttypes.h>
-#endif
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
 #include <math.h>
 
-#include "x264.h"
 #include "common/common.h"
 #ifndef _MSC_VER
 #include "config.h"
 #endif
 
+static void transpose( uint8_t *buf, int w )
+{
+    int i, j;
+    for( i = 0; i < w; i++ )
+        for( j = 0; j < i; j++ )
+            XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
+}
+
 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
 {
     const int len = idx<4 ? 16 : 64;
-    const int *zigzag = idx<4 ? x264_zigzag_scan4 : x264_zigzag_scan8;
+    const int *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0];
     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]
@@ -77,7 +75,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
 {
     sps->i_id = i_id;
 
-    sps->b_qpprime_y_zero_transform_bypass = !param->rc.b_cbr && param->rc.i_qp_constant == 0;
+    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 )
         sps->i_profile_idc  = PROFILE_HIGH444;
     else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
@@ -88,8 +86,11 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
         sps->i_profile_idc  = PROFILE_BASELINE;
     sps->i_level_idc = param->i_level_idc;
 
-    sps->b_constraint_set0  = 0;
-    sps->b_constraint_set1  = 0;
+    sps->b_constraint_set0  = sps->i_profile_idc == PROFILE_BASELINE;
+    /* x264 doesn't support the features that are in Baseline and not in Main,
+     * namely arbitrary_slice_order and slice_groups. */
+    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->i_log2_max_frame_num = 4;  /* at least 4 */
@@ -120,46 +121,37 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
         }
     }
 
-    sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
-    /* extra slot with pyramid so that we don't have to override the
-     * order of forgetting old pictures */
-    sps->vui.i_max_dec_frame_buffering =
-    sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames + param->b_bframe_pyramid);
+    sps->b_vui = 1;
 
     sps->b_gaps_in_frame_num_value_allowed = 0;
     sps->i_mb_width = ( param->i_width + 15 ) / 16;
-    sps->i_mb_height= ( param->i_height + 15 )/ 16;
-    sps->b_frame_mbs_only = 1;
-    sps->b_mb_adaptive_frame_field = 0;
-    sps->b_direct8x8_inference = 0;
-    if( sps->b_frame_mbs_only == 0 ||
-        !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
-    {
-        sps->b_direct8x8_inference = 1;
-    }
+    sps->i_mb_height= ( param->i_height + 15 ) / 16;
+    if( param->b_interlaced )
+        sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
+    sps->b_frame_mbs_only = ! param->b_interlaced;
+    sps->b_mb_adaptive_frame_field = param->b_interlaced;
+    sps->b_direct8x8_inference = param->analyse.i_direct_8x8_inference
+                              || ! sps->b_frame_mbs_only
+                              || !(param->analyse.inter & X264_ANALYSE_PSUB8x8);
 
     sps->crop.i_left   = 0;
     sps->crop.i_top    = 0;
-    sps->crop.i_right  = (- param->i_width)  & 15;
-    sps->crop.i_bottom = (- param->i_height) & 15;
+    sps->crop.i_right  = sps->i_mb_width*16 - param->i_width;
+    sps->crop.i_bottom = (sps->i_mb_height*16 - param->i_height) >> param->b_interlaced;
     sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
                   sps->crop.i_right || sps->crop.i_bottom;
 
-    sps->b_vui = 0;
     sps->vui.b_aspect_ratio_info_present = 0;
-
     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
     {
         sps->vui.b_aspect_ratio_info_present = 1;
         sps->vui.i_sar_width = param->vui.i_sar_width;
         sps->vui.i_sar_height= param->vui.i_sar_height;
     }
-    sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
     
     sps->vui.b_overscan_info_present = ( param->vui.i_overscan ? 1 : 0 );
     if( sps->vui.b_overscan_info_present )
         sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
-    sps->b_vui |= sps->vui.b_overscan_info_present;
     
     sps->vui.b_signal_type_present = 0;
     sps->vui.i_vidformat = ( param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
@@ -182,7 +174,6 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     {
         sps->vui.b_signal_type_present = 1;
     }
-    sps->b_vui |= sps->vui.b_signal_type_present;
     
     /* FIXME: not sufficient for interlaced video */
     sps->vui.b_chroma_loc_info_present = ( param->vui.i_chroma_loc ? 1 : 0 );
@@ -191,21 +182,23 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
         sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
         sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
     }
-    sps->b_vui |= sps->vui.b_chroma_loc_info_present;
 
     sps->vui.b_timing_info_present = 0;
     if( param->i_fps_num > 0 && param->i_fps_den > 0)
     {
         sps->vui.b_timing_info_present = 1;
-        /* The standard is confusing me, but this seems to work best
-           with other encoders */
         sps->vui.i_num_units_in_tick = param->i_fps_den;
-        sps->vui.i_time_scale = param->i_fps_num;
+        sps->vui.i_time_scale = param->i_fps_num * 2;
         sps->vui.b_fixed_frame_rate = 1;
     }
-    sps->b_vui |= sps->vui.b_timing_info_present;
 
-    sps->vui.b_bitstream_restriction = param->i_bframe > 0;
+    sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
+    /* extra slot with pyramid so that we don't have to override the
+     * order of forgetting old pictures */
+    sps->vui.i_max_dec_frame_buffering =
+    sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames + param->b_bframe_pyramid);
+
+    sps->vui.b_bitstream_restriction = 1;
     if( sps->vui.b_bitstream_restriction )
     {
         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
@@ -214,7 +207,6 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
         sps->vui.i_log2_max_mv_length_horizontal =
         sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
     }
-    sps->b_vui |= sps->vui.b_bitstream_restriction;
 }
 
 
@@ -263,7 +255,14 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
     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 );
-    bs_write_ue( s, sps->i_mb_height - 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( s, 1, sps->b_frame_mbs_only );
     if( !sps->b_frame_mbs_only )
     {
@@ -306,7 +305,7 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
             }
             else
             {
-                bs_write( s, 8, 255);   /* aspect_ratio_idc (extented) */
+                bs_write( s, 8, 255);   /* aspect_ratio_idc (extended) */
                 bs_write( s, 16, sps->vui.i_sar_width );
                 bs_write( s, 16, sps->vui.i_sar_height );
             }
@@ -381,7 +380,7 @@ void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *
     pps->b_weighted_pred = 0;
     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
 
-    pps->i_pic_init_qp = param->rc.b_cbr ? 26 : param->rc.i_qp_constant;
+    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_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
@@ -403,6 +402,13 @@ void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *
             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_4pc, 4 );
+        transpose( param->cqm_8iy, 8 );
+        transpose( param->cqm_8py, 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;
@@ -471,14 +477,13 @@ void x264_sei_version_write( x264_t *h, bs_t *s )
         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
     };
-    char version[1200];
-    int length;
     char *opts = x264_param2string( &h->param, 0 );
+    char *version = x264_malloc( 200 + strlen(opts) );
+    int length;
 
     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
              "Copyleft 2005 - http://www.videolan.org/x264.html - options: %s",
              X264_BUILD, X264_VERSION, opts );
-    x264_free( opts );
     length = strlen(version)+1+16;
 
     bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
@@ -493,6 +498,9 @@ void x264_sei_version_write( x264_t *h, bs_t *s )
         bs_write( s, 8, version[i] );
 
     bs_rbsp_trailing( s );
+
+    x264_free( opts );
+    x264_free( version );
 }
 
 const x264_level_t x264_levels[] =
@@ -542,6 +550,8 @@ void x264_validate_levels( x264_t *h )
 
     if( h->param.i_fps_den > 0 )
         CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
+    if( h->sps->b_direct8x8_inference < l->direct8x8 )
+        x264_log( h, X264_LOG_WARNING, "direct 8x8 inference (0) < level requirement (1)\n" );
 
     /* TODO check the rest of the limits */
 }