]> git.videolan.org Git - x264.git/commitdiff
Improve Blu-ray compliance
authorJason Garrett-Glaser <jason@x264.com>
Wed, 6 Apr 2011 09:16:42 +0000 (02:16 -0700)
committerJason Garrett-Glaser <jason@x264.com>
Wed, 13 Apr 2011 01:20:31 +0000 (18:20 -0700)
Use dec_ref_pic_marking SEIs to repeat B-ref referencing information.
Don't allow B-frames to reference frames outside their minigop.

common/common.h
encoder/encoder.c
encoder/set.c
encoder/set.h

index 496542e2055090b09b0e8d6e7792951d17ab56d0..a9be0788f283d977b3639bcb47dc2892c4a12f6e 100644 (file)
@@ -310,6 +310,7 @@ enum sei_payload_type_e
     SEI_USER_DATA_REGISTERED   = 4,
     SEI_USER_DATA_UNREGISTERED = 5,
     SEI_RECOVERY_POINT         = 6,
+    SEI_DEC_REF_PIC_MARKING    = 7,
     SEI_FRAME_PACKING          = 45,
 };
 
@@ -473,6 +474,10 @@ struct x264_t
     /* Slice header */
     x264_slice_header_t sh;
 
+    /* Slice header backup, for SEI_DEC_REF_PIC_MARKING */
+    int b_sh_backup;
+    x264_slice_header_t sh_backup;
+
     /* cabac context */
     x264_cabac_t    cabac;
 
index b0fc7b907ea55e93ab2903a06e48ba02d71db4bb..56cbd27cffbbe008d7b944be78bed59e1164d05f 100644 (file)
@@ -1665,6 +1665,10 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc )
     h->i_ref[0] = X264_MIN( h->i_ref[0], h->frames.i_max_ref0 );
     h->i_ref[0] = X264_MIN( h->i_ref[0], h->param.i_frame_reference ); // if reconfig() has lowered the limit
 
+    /* For Blu-ray compliance, don't reference frames outside of the minigop. */
+    if( IS_X264_TYPE_B( h->fenc->i_type ) && h->param.b_bluray_compat )
+        h->i_ref[0] = X264_MIN( h->i_ref[0], IS_X264_TYPE_B( h->fref[0][0]->i_type ) + 1 );
+
     /* add duplicates */
     if( h->fenc->i_type == X264_TYPE_P )
     {
@@ -1879,6 +1883,12 @@ static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_global_qp )
         }
     }
 
+    if( h->fenc->i_type == X264_TYPE_BREF && h->param.b_bluray_compat && h->sh.i_mmco_command_count )
+    {
+        h->b_sh_backup = 1;
+        h->sh_backup = h->sh;
+    }
+
     h->fdec->i_frame_num = h->sh.i_frame_num;
 
     if( h->sps->i_poc_type == 0 )
@@ -2775,6 +2785,17 @@ int     x264_encoder_encode( x264_t *h,
         overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD - (h->param.b_annexb && h->out.i_nal-1);
     }
 
+    /* As required by Blu-ray. */
+    if( !IS_X264_TYPE_B( h->fenc->i_type ) && h->b_sh_backup )
+    {
+        h->b_sh_backup = 0;
+        x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
+        x264_sei_dec_ref_pic_marking_write( h, &h->out.bs );
+        if( x264_nal_end( h ) )
+            return -1;
+        overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD - (h->param.b_annexb && h->out.i_nal-1);
+    }
+
     if( h->fenc->b_keyframe && h->param.b_intra_refresh )
         h->i_cpb_delay_pir_offset = h->fenc->i_cpb_delay;
 
index 78f1b56dd489340e014d3f53482e9379ab72004e..e7cdf1c78da0fd2183348e5d265a2bad9334b3fd 100644 (file)
@@ -551,7 +551,6 @@ void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
     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 )
@@ -689,6 +688,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 },
index 7fcca13415cde5ece755c4ec87a2701001e02ca5..4f1eeabaac3b84d222f7b574c59402f573dc1ad5 100644 (file)
@@ -36,6 +36,7 @@ int  x264_sei_version_write( x264_t *h, bs_t *s );
 int  x264_validate_levels( x264_t *h, int verbose );
 void x264_sei_buffering_period_write( x264_t *h, bs_t *s );
 void x264_sei_pic_timing_write( x264_t *h, bs_t *s );
+void x264_sei_dec_ref_pic_marking_write( x264_t *h, bs_t *s );
 void x264_sei_frame_packing_write( x264_t *h, bs_t *s );
 void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_type );
 void x264_filler_write( x264_t *h, bs_t *s, int filler );