void xavs_pb_picture_write (bs_t * s, xavs_pb_pic_header_t * pbh, xavs_seq_header_t * sqh) { bs_write (s, 8, pbh->i_pb_picture_start_code); bs_write (s, 16, pbh->i_bbv_delay); bs_write (s, 2, pbh->i_picture_coding_type); bs_write (s, 8, pbh->i_picture_distance); if (sqh->b_low_delay) bs_write_ue (s, 1); //pbh->i_bbv_check_times); bs_write1 (s, 1); //pbh->b_progressive_frame); /*if(!pbh->b_progressive_frame){ bs_write1( s, pbh->b_picture_structure); if(!pbh->b_picture_structure) bs_write1( s, pbh->b_advanced_pred_mode_disable); } */ bs_write1 (s, 0); //pbh->b_top_field_first); bs_write1 (s, 0); //pbh->b_repeat_first_field); bs_write1 (s, pbh->b_fixed_picture_qp); bs_write (s, 6, pbh->i_picture_qp); if (pbh->i_picture_coding_type == 1) bs_write1 (s, pbh->b_picture_reference_flag); //bs_write1( s, pbh->b_no_forward_reference_flag); bs_write (s, 4, 0); //reserved bits bs_write1 (s, 1); //pbh->b_skip_mode_flag); bs_write1 (s, pbh->b_loop_filter_disable); if (!pbh->b_loop_filter_disable) bs_write1 (s, pbh->b_loop_filter_parameter_flag); if (pbh->b_loop_filter_parameter_flag) { bs_write_se (s, pbh->i_alpha_c_offset); bs_write_se (s, pbh->i_beta_offset); } bs_rbsp_trailing (s); }
static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx ) { const int len = idx<4 ? 16 : 64; const uint8_t *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] : x264_cqm_jvt[idx]; if( !memcmp( list, def_list, len ) ) bs_write( s, 1, 0 ); // scaling_list_present_flag else if( !memcmp( list, x264_cqm_jvt[idx], len ) ) { bs_write( s, 1, 1 ); // scaling_list_present_flag bs_write_se( s, -8 ); // use jvt list } else { int j, run; bs_write( s, 1, 1 ); // scaling_list_present_flag // try run-length compression of trailing values for( run = len; run > 1; run-- ) if( list[zigzag[run-1]] != list[zigzag[run-2]] ) break; if( run < len && len - run < bs_size_se( (int8_t)-list[zigzag[run]] ) ) run = len; for( j = 0; j < run; j++ ) bs_write_se( s, (int8_t)(list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8)) ); // delta if( run < len ) bs_write_se( s, (int8_t)-list[zigzag[run]] ); } }
void xavs_i_picture_write (bs_t * s, xavs_i_pic_header_t * ih, xavs_seq_header_t * sqh) { bs_write (s, 8, 0xB3); //ih->i_i_picture_start_code); bs_write (s, 16, ih->i_bbv_delay); bs_write1 (s, ih->b_time_code_flag); if (ih->b_time_code_flag) bs_write (s, 24, ih->i_time_code); bs_write1 (s, 1); //marker bit bs_write (s, 8, ih->i_picture_distance); if (sqh->b_low_delay) bs_write_ue (s, 1); //ih->i_bbv_check_times); bs_write1 (s, ih->b_progressive_frame); if (!ih->b_progressive_frame) bs_write1 (s, ih->b_picture_structure); bs_write1 (s, 1); //ih->b_top_field_first); bs_write1 (s, 0); //ih->b_repeat_first_field); bs_write1 (s, ih->b_fixed_picture_qp); bs_write (s, 6, ih->i_picture_qp); //if(!ih->b_progressive_frame && !ih->b_picture_structure) //bs_write1( s, ih->b_skip_mode_flag); bs_write (s, 4, ih->i_reserved_bits); bs_write1 (s, ih->b_loop_filter_disable); if (!ih->b_loop_filter_disable) bs_write1 (s, ih->b_loop_filter_parameter_flag); if (ih->b_loop_filter_parameter_flag) { bs_write_se (s, ih->i_alpha_c_offset); bs_write_se (s, ih->i_beta_offset); } bs_rbsp_trailing (s); }
//7.3.2.1.1 Scaling list syntax void write_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag) { int j; int lastScale = 8; int nextScale = 8; for (j = 0; j < sizeOfScalingList; j++) { int delta_scale; if (nextScale != 0) { // FIXME will not write in most compact way - could truncate list if all remaining elements are equal nextScale = scalingList[j]; if (useDefaultScalingMatrixFlag) { nextScale = 0; } delta_scale = (nextScale - lastScale) % 256; bs_write_se(b, delta_scale); } lastScale = scalingList[j]; } }
void x264_pps_write( bs_t *s, x264_pps_t *pps ) { bs_realign( s ); bs_write_ue( s, pps->i_id ); bs_write_ue( s, pps->i_sps_id ); 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_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 - QP_BD_OFFSET ); bs_write_se( s, pps->i_chroma_qp_index_offset ); 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_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_write1( s, 0 ); // Cr = Cb scaling_list_write( s, pps, CQM_4PY ); scaling_list_write( s, pps, CQM_4PC ); 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 ); } } bs_write_se( s, pps->i_chroma_qp_index_offset ); } bs_rbsp_trailing( s ); bs_flush( s ); }
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 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] : x264_cqm_jvt[idx]; int j; if( memcmp( list, def_list, len ) ) { bs_write( s, 1, 1 ); // scaling_list_present_flag for( j = 0; j < len; j++ ) bs_write_se( s, list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8) ); // delta } else bs_write( s, 1, 0 ); // scaling_list_present_flag }
void x264_sps_write( bs_t *s, x264_sps_t *sps ) { 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, 5, 0 ); /* reserved */ bs_write( s, 8, sps->i_level_idc ); bs_write_ue( s, sps->i_id ); if( sps->i_profile_idc >= PROFILE_HIGH ) { bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0 bs_write_ue( s, 0 ); // bit_depth_luma_minus8 bs_write_ue( s, 0 ); // 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_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 ) { 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++ ) { 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( 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 ); 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 ); } bs_write( s, 1, sps->b_vui ); if( sps->b_vui ) { bs_write1( s, sps->vui.b_aspect_ratio_info_present ); if( sps->vui.b_aspect_ratio_info_present ) { int i; static const struct { int w, h; int sar; } sar[] = { { 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, -1 } }; for( i = 0; sar[i].sar != -1; i++ ) { if( sar[i].w == sps->vui.i_sar_width && sar[i].h == sps->vui.i_sar_height ) break; } if( sar[i].sar != -1 ) { bs_write( s, 8, sar[i].sar ); } else { 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 ); } } bs_write1( s, sps->vui.b_overscan_info_present ); if( sps->vui.b_overscan_info_present ) bs_write1( s, sps->vui.b_overscan_info ); bs_write1( s, sps->vui.b_signal_type_present ); if( sps->vui.b_signal_type_present ) { bs_write( s, 3, sps->vui.i_vidformat ); bs_write1( s, sps->vui.b_fullrange ); bs_write1( s, sps->vui.b_color_description_present ); if( sps->vui.b_color_description_present ) { bs_write( s, 8, sps->vui.i_colorprim ); bs_write( s, 8, sps->vui.i_transfer ); bs_write( s, 8, sps->vui.i_colmatrix ); } } bs_write1( s, sps->vui.b_chroma_loc_info_present ); if( sps->vui.b_chroma_loc_info_present ) { bs_write_ue( s, sps->vui.i_chroma_loc_top ); bs_write_ue( s, sps->vui.i_chroma_loc_bottom ); } bs_write1( s, sps->vui.b_timing_info_present ); if( sps->vui.b_timing_info_present ) { bs_write32( s, sps->vui.i_num_units_in_tick ); bs_write32( s, sps->vui.i_time_scale ); bs_write1( s, sps->vui.b_fixed_frame_rate ); } bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */ bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */ bs_write1( s, 0 ); /* pic_struct_present_flag */ bs_write1( s, sps->vui.b_bitstream_restriction ); if( sps->vui.b_bitstream_restriction ) { bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries ); bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom ); bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom ); bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal ); bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical ); bs_write_ue( s, sps->vui.i_num_reorder_frames ); bs_write_ue( s, sps->vui.i_max_dec_frame_buffering ); } } bs_rbsp_trailing( s ); }
void x264_pps_write( bs_t *s, x264_pps_t *pps ) { 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_write_ue( s, pps->i_num_slice_groups - 1 ); #if 0 if( pps->i_num_slice_groups > 1 ) { int i; bs_write_ue( s, pps->i_slice_group_map_type ); if( pps->i_slice_group_map_type == 0 ) { for( i = 0; i < pps->i_num_slice_groups; i++ ) { bs_write_ue( s, pps->i_run_length[i] - 1 ); } } else if( pps->i_slice_group_map_type == 2 ) { for( i = 0; i < pps->i_num_slice_groups; i++ ) { bs_write_ue( s, pps->i_top_left[i] ); bs_write_ue( s, pps->i_bottom_right[i] ); } } else if( pps->i_slice_group_map_type >= 3 && pps->i_slice_group_map_type <= 5 ) { bs_write( s, 1, pps->b_slice_group_change_direction ); bs_write_ue( s, pps->b_slice_group_change_direction - 1 ); } else if( pps->i_slice_group_map_type == 6 ) { bs_write_ue( s, pps->i_pic_size_in_map_units - 1 ); for( i = 0; i < pps->i_pic_size_in_map_units; i++ ) { /* FIXME */ /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ), * pps->i_slice_group_id[i] ); */ } } } #endif bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 ); bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 ); bs_write( s, 1, pps->b_weighted_pred ); bs_write( s, 2, pps->b_weighted_bipred ); bs_write_se( s, pps->i_pic_init_qp - 26 ); bs_write_se( s, pps->i_pic_init_qs - 26 ); 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 ); 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) ); 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 scaling_list_write( s, pps, CQM_4PY ); scaling_list_write( s, pps, CQM_4PC ); bs_write( s, 1, 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 ); } } bs_write_se( s, pps->i_chroma_qp_index_offset ); } bs_rbsp_trailing( s ); }
//7.3.2.1 Sequence parameter set RBSP syntax void write_seq_parameter_set_rbsp(sps_t *sps, bs_t* b) { int i; bs_write(b, 8, sps->profile_idc); bs_write(b, 1, sps->constraint_set0_flag); bs_write(b, 1, sps->constraint_set1_flag); bs_write(b, 1, sps->constraint_set2_flag); bs_write(b, 1, sps->constraint_set3_flag); bs_write(b, 1, sps->constraint_set4_flag); bs_write(b, 1, sps->constraint_set5_flag); bs_write(b, 2, 0); /* reserved_zero_2bits */ bs_write(b, 8, sps->level_idc); bs_write_ue(b, sps->seq_parameter_set_id); if (sps->profile_idc == 100 || sps->profile_idc == 110 || sps->profile_idc == 122 || sps->profile_idc == 144) { bs_write_ue(b, sps->chroma_format_idc); if (sps->chroma_format_idc == 3) { bs_write(b, 1, sps->residual_colour_transform_flag); } bs_write_ue(b, sps->bit_depth_luma_minus8); bs_write_ue(b, sps->bit_depth_chroma_minus8); bs_write(b, 1, sps->qpprime_y_zero_transform_bypass_flag); bs_write(b, 1, sps->seq_scaling_matrix_present_flag); if (sps->seq_scaling_matrix_present_flag) { for (i = 0; i < 8; i++) { bs_write(b, 1, sps->seq_scaling_list_present_flag[i]); if (sps->seq_scaling_list_present_flag[i]) { if (i < 6) { write_scaling_list(b, sps->ScalingList4x4[i], 16, sps->UseDefaultScalingMatrix4x4Flag[i]); } else { write_scaling_list(b, sps->ScalingList8x8[i - 6], 64, sps->UseDefaultScalingMatrix8x8Flag[i - 6]); } } } } } bs_write_ue(b, sps->log2_max_frame_num_minus4); bs_write_ue(b, sps->pic_order_cnt_type); if (sps->pic_order_cnt_type == 0) { bs_write_ue(b, sps->log2_max_pic_order_cnt_lsb_minus4); } else if (sps->pic_order_cnt_type == 1) { bs_write(b, 1, sps->delta_pic_order_always_zero_flag); bs_write_se(b, sps->offset_for_non_ref_pic); bs_write_se(b, sps->offset_for_top_to_bottom_field); bs_write_ue(b, sps->num_ref_frames_in_pic_order_cnt_cycle); for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) { bs_write_se(b, sps->offset_for_ref_frame[i]); } } bs_write_ue(b, sps->num_ref_frames); bs_write(b, 1, sps->gaps_in_frame_num_value_allowed_flag); bs_write_ue(b, sps->pic_width_in_mbs_minus1); bs_write_ue(b, sps->pic_height_in_map_units_minus1); bs_write(b, 1, sps->frame_mbs_only_flag); if (!sps->frame_mbs_only_flag) { bs_write(b, 1, sps->mb_adaptive_frame_field_flag); } bs_write(b, 1, sps->direct_8x8_inference_flag); bs_write(b, 1, sps->frame_cropping_flag); if (sps->frame_cropping_flag) { bs_write_ue(b, sps->frame_crop_left_offset); bs_write_ue(b, sps->frame_crop_right_offset); bs_write_ue(b, sps->frame_crop_top_offset); bs_write_ue(b, sps->frame_crop_bottom_offset); } bs_write(b, 1, sps->vui_parameters_present_flag); if (sps->vui_parameters_present_flag) { write_vui_parameters(sps, b); } write_rbsp_trailing_bits(b); }