Update NEWS for 1.3.2 pre1
[platform/upstream/libva-intel-driver.git] / src / i965_encoder_utils.c
index ab120b5..abd25b4 100644 (file)
@@ -27,6 +27,8 @@
 #include <assert.h>
 
 #include <va/va.h>
+#include <va/va_enc_h264.h>
+#include <va/va_enc_mpeg2.h>
 
 #include "i965_encoder_utils.h"
 
@@ -41,6 +43,7 @@
 #define NAL_IDR                 5
 #define NAL_SPS                 7
 #define NAL_PPS                 8
+#define NAL_SEI                 6
 
 #define SLICE_TYPE_P            0
 #define SLICE_TYPE_B            1
@@ -97,7 +100,7 @@ avc_bitstream_end(avc_bitstream *bs)
 
     // free(bs->buffer);
 }
+
 static void
 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
 {
@@ -123,6 +126,9 @@ avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
         if (pos + 1 == bs->max_size_in_dword) {
             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
+            
+            if (!bs->buffer)
+                return;
         }
 
         bs->buffer[pos + 1] = val;
@@ -176,7 +182,11 @@ avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
 
     avc_bitstream_put_ui(bs, new_val, bit_left);
 }
-
+static void avc_rbsp_trailing_bits(avc_bitstream *bs)
+{
+    avc_bitstream_put_ui(bs, 1, 1);
+    avc_bitstream_byte_aligning(bs, 0);
+}
 static void nal_start_code_prefix(avc_bitstream *bs)
 {
     avc_bitstream_put_ui(bs, 0x00000001, 32);
@@ -191,20 +201,19 @@ static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
 
 static void 
 slice_header(avc_bitstream *bs,
-             VAEncSequenceParameterBufferH264Ext *sps_param,
-             VAEncPictureParameterBufferH264Ext *pic_param,
-             VAEncSliceParameterBufferH264Ext *slice_param,
-             VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param)
+             VAEncSequenceParameterBufferH264 *sps_param,
+             VAEncPictureParameterBufferH264 *pic_param,
+             VAEncSliceParameterBufferH264 *slice_param)
 {
-    int first_mb_in_slice = slice_param->starting_macroblock_address;
+    int first_mb_in_slice = slice_param->macroblock_address;
 
     avc_bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
     avc_bitstream_put_ue(bs, slice_param->slice_type);  /* slice_type */
     avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
-    avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->log2_max_frame_num_minus4 + 4); /* frame_num */
+    avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
 
     /* frame_mbs_only_flag == 1 */
-    if (!sps_param->frame_mbs_only_flag) {
+    if (!sps_param->seq_fields.bits.frame_mbs_only_flag) {
         /* FIXME: */
         assert(0);
     }
@@ -212,8 +221,8 @@ slice_header(avc_bitstream *bs,
     if (pic_param->pic_fields.bits.idr_pic_flag)
         avc_bitstream_put_ue(bs, slice_param->idr_pic_id);             /* idr_pic_id: 0 */
 
-    if (sps_param->pic_order_cnt_type == 0) {
-        avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->log2_max_pic_order_cnt_lsb_minus4 + 4);
+    if (sps_param->seq_fields.bits.pic_order_cnt_type == 0) {
+        avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
         /* pic_order_present_flag == 0 */
     } else {
         /* FIXME: */
@@ -224,20 +233,26 @@ slice_header(avc_bitstream *bs,
     
     /* slice type */
     if (IS_P_SLICE(slice_param->slice_type)) {
-        avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
+        avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1);            /* num_ref_idx_active_override_flag: */
+
+        if (slice_param->num_ref_idx_active_override_flag)
+            avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
 
         /* ref_pic_list_reordering */
-        assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
-        avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
+        avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
     } else if (IS_B_SLICE(slice_param->slice_type)) {
         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
-        avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
+
+        avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1);       /* num_ref_idx_active_override_flag: */
+
+        if (slice_param->num_ref_idx_active_override_flag) {
+            avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
+            avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l1_active_minus1);
+        }
 
         /* ref_pic_list_reordering */
-        assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
-        assert(slice_param->ref_pic_list_modification_flag_l1 == 0);
-        avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
-        avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l1, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
+        avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
+        avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
     } 
 
     if ((pic_param->pic_fields.bits.weighted_pred_flag && 
@@ -254,14 +269,6 @@ slice_header(avc_bitstream *bs,
         unsigned char long_term_reference_flag = 0;
         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
 
-        if (dec_ref_pic_marking_param) {
-            no_output_of_prior_pics_flag = dec_ref_pic_marking_param->no_output_of_prior_pics_flag;
-            long_term_reference_flag = dec_ref_pic_marking_param->long_term_reference_flag;
-            adaptive_ref_pic_marking_mode_flag = dec_ref_pic_marking_param->adaptive_ref_pic_marking_mode_flag;
-            /* FIXME: XXX */
-            assert(adaptive_ref_pic_marking_mode_flag == 0);
-        }
-
         if (pic_param->pic_fields.bits.idr_pic_flag) {
             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
@@ -293,14 +300,14 @@ slice_header(avc_bitstream *bs,
 }
 
 int 
-build_avc_slice_header(VAEncSequenceParameterBufferH264Ext *sps_param,
-                       VAEncPictureParameterBufferH264Ext *pic_param,
-                       VAEncSliceParameterBufferH264Ext *slice_param,
-                       VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param,
+build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
+                       VAEncPictureParameterBufferH264 *pic_param,
+                       VAEncSliceParameterBufferH264 *slice_param,
                        unsigned char **slice_header_buffer)
 {
     avc_bitstream bs;
     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
+    int is_ref = !!pic_param->pic_fields.bits.reference_pic_flag;
 
     avc_bitstream_start(&bs);
     nal_start_code_prefix(&bs);
@@ -308,14 +315,182 @@ build_avc_slice_header(VAEncSequenceParameterBufferH264Ext *sps_param,
     if (IS_I_SLICE(slice_param->slice_type)) {
         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
     } else if (IS_P_SLICE(slice_param->slice_type)) {
-        nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
+        assert(!is_idr);
+        nal_header(&bs, NAL_REF_IDC_MEDIUM, NAL_NON_IDR);
     } else {
         assert(IS_B_SLICE(slice_param->slice_type));
-        nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
+        assert(!is_idr);
+        nal_header(&bs, is_ref ? NAL_REF_IDC_LOW : NAL_REF_IDC_NONE, NAL_NON_IDR);
     }
 
-    slice_header(&bs, sps_param, pic_param, slice_param, dec_ref_pic_marking_param);
+    slice_header(&bs, sps_param, pic_param, slice_param);
+
+    avc_bitstream_end(&bs);
+    *slice_header_buffer = (unsigned char *)bs.buffer;
+
+    return bs.bit_offset;
+}
+
+int 
+build_avc_sei_buffering_period(int cpb_removal_length,
+                               unsigned int init_cpb_removal_delay, 
+                               unsigned int init_cpb_removal_delay_offset,
+                               unsigned char **sei_buffer) 
+{
+    unsigned char *byte_buf;
+    int byte_size, i;
+
+    avc_bitstream nal_bs;
+    avc_bitstream sei_bs;
+
+    avc_bitstream_start(&sei_bs);
+    avc_bitstream_put_ue(&sei_bs, 0);       /*seq_parameter_set_id*/
+    avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay, cpb_removal_length); 
+    avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
+    if ( sei_bs.bit_offset & 0x7) {
+        avc_bitstream_put_ui(&sei_bs, 1, 1);
+    }
+    avc_bitstream_end(&sei_bs);
+    byte_size = (sei_bs.bit_offset + 7) / 8;
+    
+    avc_bitstream_start(&nal_bs);
+    nal_start_code_prefix(&nal_bs);
+    nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
+    
+    avc_bitstream_put_ui(&nal_bs, 0, 8);
+    avc_bitstream_put_ui(&nal_bs, byte_size, 8);
+    
+    byte_buf = (unsigned char *)sei_bs.buffer;
+    for(i = 0; i < byte_size; i++) {
+        avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
+    }
+    free(byte_buf);
+
+    avc_rbsp_trailing_bits(&nal_bs);
+    avc_bitstream_end(&nal_bs);
+
+    *sei_buffer = (unsigned char *)nal_bs.buffer; 
+   
+    return nal_bs.bit_offset;
+}
+
+int 
+build_avc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay, 
+                         unsigned int dpb_output_length, unsigned int dpb_output_delay,
+                         unsigned char **sei_buffer)
+{
+    unsigned char *byte_buf;
+    int byte_size, i;
+
+    avc_bitstream nal_bs;
+    avc_bitstream sei_bs;
 
+    avc_bitstream_start(&sei_bs);
+    avc_bitstream_put_ui(&sei_bs, cpb_removal_delay, cpb_removal_length); 
+    avc_bitstream_put_ui(&sei_bs, dpb_output_delay, dpb_output_length); 
+    if ( sei_bs.bit_offset & 0x7) {
+        avc_bitstream_put_ui(&sei_bs, 1, 1);
+    }
+    avc_bitstream_end(&sei_bs);
+    byte_size = (sei_bs.bit_offset + 7) / 8;
+    
+    avc_bitstream_start(&nal_bs);
+    nal_start_code_prefix(&nal_bs);
+    nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
+    
+    avc_bitstream_put_ui(&nal_bs, 0x01, 8);
+    avc_bitstream_put_ui(&nal_bs, byte_size, 8);
+    
+    byte_buf = (unsigned char *)sei_bs.buffer;
+    for(i = 0; i < byte_size; i++) {
+        avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
+    }
+    free(byte_buf);
+
+    avc_rbsp_trailing_bits(&nal_bs);
+    avc_bitstream_end(&nal_bs);
+
+    *sei_buffer = (unsigned char *)nal_bs.buffer; 
+   
+    return nal_bs.bit_offset;
+}
+
+
+int 
+build_avc_sei_buffer_timing(unsigned int init_cpb_removal_length,
+                               unsigned int init_cpb_removal_delay,
+                               unsigned int init_cpb_removal_delay_offset,
+                               unsigned int cpb_removal_length,
+                               unsigned int cpb_removal_delay,
+                               unsigned int dpb_output_length,
+                               unsigned int dpb_output_delay,
+                               unsigned char **sei_buffer)
+{
+    unsigned char *byte_buf;
+    int bp_byte_size, i, pic_byte_size;
+
+    avc_bitstream nal_bs;
+    avc_bitstream sei_bp_bs, sei_pic_bs;
+
+    avc_bitstream_start(&sei_bp_bs);
+    avc_bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
+    avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length); 
+    avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
+    if ( sei_bp_bs.bit_offset & 0x7) {
+        avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
+    }
+    avc_bitstream_end(&sei_bp_bs);
+    bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
+    
+    avc_bitstream_start(&sei_pic_bs);
+    avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length); 
+    avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length); 
+    if ( sei_pic_bs.bit_offset & 0x7) {
+        avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
+    }
+    avc_bitstream_end(&sei_pic_bs);
+    pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
+    
+    avc_bitstream_start(&nal_bs);
+    nal_start_code_prefix(&nal_bs);
+    nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
+
+       /* Write the SEI buffer period data */    
+    avc_bitstream_put_ui(&nal_bs, 0, 8);
+    avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
+    
+    byte_buf = (unsigned char *)sei_bp_bs.buffer;
+    for(i = 0; i < bp_byte_size; i++) {
+        avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
+    }
+    free(byte_buf);
+       /* write the SEI timing data */
+    avc_bitstream_put_ui(&nal_bs, 0x01, 8);
+    avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
+    
+    byte_buf = (unsigned char *)sei_pic_bs.buffer;
+    for(i = 0; i < pic_byte_size; i++) {
+        avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
+    }
+    free(byte_buf);
+
+    avc_rbsp_trailing_bits(&nal_bs);
+    avc_bitstream_end(&nal_bs);
+
+    *sei_buffer = (unsigned char *)nal_bs.buffer; 
+   
+    return nal_bs.bit_offset;
+}
+
+int 
+build_mpeg2_slice_header(VAEncSequenceParameterBufferMPEG2 *sps_param,
+                         VAEncPictureParameterBufferMPEG2 *pic_param,
+                         VAEncSliceParameterBufferMPEG2 *slice_param,
+                         unsigned char **slice_header_buffer)
+{
+    avc_bitstream bs;
+
+    avc_bitstream_start(&bs);
     avc_bitstream_end(&bs);
     *slice_header_buffer = (unsigned char *)bs.buffer;