Fixed headers and NAL writing to match the latest VTM master 988c22cbb9c58584cac3ef0ec7794cafbea6dfd6

This commit is contained in:
Marko Viitanen 2019-05-29 16:17:02 +03:00
parent 4768ba0628
commit 8282a18c36
7 changed files with 64 additions and 69 deletions

View file

@ -266,17 +266,7 @@ void kvz_bitstream_put_se(bitstream_t *stream, int32_t data)
void kvz_bitstream_add_rbsp_trailing_bits(bitstream_t * const stream) void kvz_bitstream_add_rbsp_trailing_bits(bitstream_t * const stream)
{ {
kvz_bitstream_put(stream, 1, 1); kvz_bitstream_put(stream, 1, 1);
#if VERBOSE kvz_bitstream_align_zero(stream);
printf("%-40s u(%d) : %d\n", "rbsp_stop_one_bit", 1, 1);
#endif
if ((stream->cur_bit & 7) != 0) {
#if VERBOSE
for (int i = 0; i < (8 - stream->cur_bit) & 7; i++) {
printf("%-40s u(%d) : %d\n", "rbsp_alignment_zero_bit", 1, 0);
}
#endif
kvz_bitstream_put(stream, 0, (8 - stream->cur_bit) & 7);
}
} }
/** /**

View file

@ -133,7 +133,7 @@ void kvz_cabac_write_unary_max_symbol_ep(cabac_data_t *data, unsigned int symbol
(ctx)->state[1]=(state >> 1) & (int)CTX_MASK_1;\ (ctx)->state[1]=(state >> 1) & (int)CTX_MASK_1;\
} }
#define CTX_MPS(ctx) (CTX_STATE(ctx)>>7) #define CTX_MPS(ctx) (CTX_STATE(ctx)>>7)
#define CTX_LPS(ctx,range) ( ((((CTX_STATE(ctx)&0x80) ? (CTX_STATE(ctx)^0xff) : (CTX_STATE(ctx))) >>2)*(range>>5)>>1)+4 ) #define CTX_LPS(ctx,range) ((uint8_t)( ((((CTX_STATE(ctx)&0x80) ? (CTX_STATE(ctx)^0xff) : (CTX_STATE(ctx))) >>2)*(range>>5)>>1)+4 ))
#define CTX_UPDATE(ctx,bin) { \ #define CTX_UPDATE(ctx,bin) { \
int rate0 = (ctx)->rate >> 4;\ int rate0 = (ctx)->rate >> 4;\
int rate1 = (ctx)->rate & 15;\ int rate1 = (ctx)->rate & 15;\

View file

@ -305,7 +305,7 @@ void kvz_ctx_init(cabac_ctx_t *ctx, uint32_t qp, uint32_t init_value, uint8_t ra
{ {
int slope = (init_value >> 4) * 5 - 45; int slope = (init_value >> 4) * 5 - 45;
int offset = ((init_value & 15) << 3) - 16; int offset = ((init_value & 15) << 3) - 16;
int init_state = MIN(MAX(1, ((slope * (int)qp) >> 4) + offset), 126); int init_state = ((slope * (int)qp) >> 4) + offset;
const int p1 = g_inistateToCount[init_state < 0 ? 0 : init_state > 127 ? 127 : init_state]; const int p1 = g_inistateToCount[init_state < 0 ? 0 : init_state > 127 ? 127 : init_state];

View file

@ -248,8 +248,7 @@ static void encoder_state_write_bitstream_VUI(bitstream_t *stream,
} else } else
WRITE_U(stream, 0, 1, "aspect_ratio_info_present_flag"); WRITE_U(stream, 0, 1, "aspect_ratio_info_present_flag");
//IF aspect ratio info WRITE_U(stream, 0, 1, "colour_description_present_flag");
//ENDIF
if (encoder->cfg.vui.overscan > 0) { if (encoder->cfg.vui.overscan > 0) {
WRITE_U(stream, 1, 1, "overscan_info_present_flag"); WRITE_U(stream, 1, 1, "overscan_info_present_flag");
@ -257,6 +256,7 @@ static void encoder_state_write_bitstream_VUI(bitstream_t *stream,
} else } else
WRITE_U(stream, 0, 1, "overscan_info_present_flag"); WRITE_U(stream, 0, 1, "overscan_info_present_flag");
//IF overscan info //IF overscan info
//ENDIF //ENDIF
@ -353,6 +353,9 @@ static void encoder_state_write_bitstream_seq_parameter_set(bitstream_t* stream,
*/ */
WRITE_U(stream, 0, 4, "sps_decoding_parameter_set_id");
WRITE_U(stream, 1, 3, "sps_max_sub_layers_minus1"); WRITE_U(stream, 1, 3, "sps_max_sub_layers_minus1");
WRITE_U(stream, 0, 5, "sps_reserved_zero_5bits"); WRITE_U(stream, 0, 5, "sps_reserved_zero_5bits");
@ -515,10 +518,17 @@ if (encoder->scaling_list.enable) {
//ENDIF //ENDIF
//WRITE_U(stream, 0, 1, "sps_strong_intra_smoothing_enable_flag"); WRITE_U(stream, encoder->vui.timing_info_present_flag, 1, "timing_info_present_flag");
WRITE_U(stream, 1, 1, "vui_parameters_present_flag"); if (encoder->vui.timing_info_present_flag) {
WRITE_U(stream, encoder->vui.num_units_in_tick, 32, "vui_num_units_in_tick");
WRITE_U(stream, encoder->vui.time_scale, 32, "vui_time_scale");
encoder_state_write_bitstream_VUI(stream, state); WRITE_U(stream, 0, 1, "vui_hrd_parameters_present_flag");
}
WRITE_U(stream, 0, 1, "vui_parameters_present_flag");
//encoder_state_write_bitstream_VUI(stream, state);
encoder_state_write_bitstream_SPS_extension(stream, state); encoder_state_write_bitstream_SPS_extension(stream, state);
@ -564,9 +574,7 @@ static void encoder_state_write_bitstream_pic_parameter_set(bitstream_t* stream,
WRITE_U(stream, encoder->cfg.lossless, 1, "transquant_bypass_enabled_flag"); WRITE_U(stream, encoder->cfg.lossless, 1, "transquant_bypass_enabled_flag");
WRITE_U(stream, encoder->tiles_enable, 1, "tiles_enabled_flag"); WRITE_U(stream, encoder->tiles_enable?0:1, 1, "single_tile_in_pic_flag");
//wavefronts
WRITE_U(stream, encoder->cfg.wpp, 1, "entropy_coding_sync_enabled_flag");
if (encoder->tiles_enable) { if (encoder->tiles_enable) {
WRITE_UE(stream, encoder->cfg.tiles_width_count - 1, "num_tile_columns_minus1"); WRITE_UE(stream, encoder->cfg.tiles_width_count - 1, "num_tile_columns_minus1");
@ -587,7 +595,11 @@ static void encoder_state_write_bitstream_pic_parameter_set(bitstream_t* stream,
} }
WRITE_U(stream, 0, 1, "pps_loop_filter_across_slices_enabled_flag"); WRITE_U(stream, 0, 1, "signalled_slice_id_flag");
//wavefronts
WRITE_U(stream, encoder->cfg.wpp, 1, "entropy_coding_sync_enabled_flag");
WRITE_U(stream, 1, 1, "deblocking_filter_control_present_flag"); WRITE_U(stream, 1, 1, "deblocking_filter_control_present_flag");
//IF deblocking_filter //IF deblocking_filter
@ -601,6 +613,8 @@ static void encoder_state_write_bitstream_pic_parameter_set(bitstream_t* stream,
WRITE_SE(stream, encoder->cfg.deblock_tc, "tc_offset_div2"); WRITE_SE(stream, encoder->cfg.deblock_tc, "tc_offset_div2");
} }
WRITE_U(stream, 0, 1, "pps_loop_filter_across_virtual_boundaries_disabled_flag");
//ENDIF //ENDIF
//ENDIF //ENDIF
//WRITE_U(stream, 0, 1, "pps_scaling_list_data_present_flag"); //WRITE_U(stream, 0, 1, "pps_scaling_list_data_present_flag");

View file

@ -1220,9 +1220,9 @@ static void encoder_state_init_new_frame(encoder_state_t * const state, kvz_pict
state->frame->pictype = KVZ_NAL_CRA_NUT; state->frame->pictype = KVZ_NAL_CRA_NUT;
} }
} else if (state->frame->poc < state->frame->irap_poc) { } else if (state->frame->poc < state->frame->irap_poc) {
state->frame->pictype = KVZ_NAL_RASL_R; state->frame->pictype = KVZ_NAL_RASL;
} else { } else {
state->frame->pictype = KVZ_NAL_TRAIL_R; state->frame->pictype = KVZ_NAL_TRAIL;
} }
encoder_state_remove_refs(state); encoder_state_remove_refs(state);

View file

@ -407,48 +407,32 @@ typedef struct kvz_picture {
*/ */
enum kvz_nal_unit_type { enum kvz_nal_unit_type {
// Trailing pictures //
KVZ_NAL_TRAIL_N = 0, KVZ_NAL_PPS_NUT = 0,
KVZ_NAL_TRAIL_R = 0, KVZ_NAL_AUD_NUT = 1,
KVZ_NAL_PREFIX_SEI_NUT = 2,
KVZ_NAL_SUFFIX_SEI_NUT = 3,
KVZ_NAL_STSA_N = 1, // Coded slices
KVZ_NAL_STSA_R = 1,
// Leading pictures
KVZ_NAL_RADL_N = 2,
KVZ_NAL_RADL_R = 2,
KVZ_NAL_RASL_N = 3,
KVZ_NAL_RASL_R = 3,
// Intra random access point pictures
KVZ_NAL_IDR_W_RADL = 8,
KVZ_NAL_IDR_N_LP = 9,
KVZ_NAL_CRA_NUT = 10,
// Reserved IRAP
KVZ_NAL_RSV_IRAP_VCL11 = 11,
KVZ_NAL_RSV_IRAP_VCL12 = 12,
KVZ_NAL_TRAIL = 8,
KVZ_NAL_STSA = 9,
KVZ_NAL_RADL = 10,
KVZ_NAL_RASL = 11,
// non-VCL // non-VCL
KVZ_NAL_VPS_NUT = 16,
KVZ_NAL_SPS_NUT = 17, KVZ_NAL_SPS_NUT = 17,
KVZ_NAL_PPS_NUT = 18,
KVZ_NAL_AUD_NUT = 20, KVZ_NAL_EOS_NUT = 18,
KVZ_NAL_EOS_NUT = 21, KVZ_NAL_EOB_NUT = 19,
KVZ_NAL_EOB_NUT = 22, KVZ_NAL_VPS_NUT = 20,
KVZ_NAL_PREFIX_SEI_NUT = 23,
KVZ_NAL_SUFFIX_SEI_NUT = 24, // Intra random access point pictures
KVZ_NAL_FD_NUT = 25, KVZ_NAL_IDR_W_RADL = 24,
KVZ_NAL_IDR_N_LP = 25,
KVZ_NAL_CRA_NUT = 26,
KVZ_NAL_GRA_NUT = 27,
}; };

View file

@ -52,19 +52,26 @@ void kvz_nal_write(bitstream_t * const bitstream, const uint8_t nal_type,
kvz_bitstream_writebyte(bitstream, start_code_prefix_one_3bytes); kvz_bitstream_writebyte(bitstream, start_code_prefix_one_3bytes);
// Handle header bits with full bytes instead of using bitstream // Handle header bits with full bytes instead of using bitstream
// forbidden_zero_flag(1) + nal_unit_type(6) + 1bit of nuh_layer_id // forbidden_zero_flag(1) + nuh_temporal_id_plus1(3) + nal_unit_type(4)
byte = nal_type << 1; uint8_t zero_tid_required_flag = 0;
if ((nal_type >= 16) && (nal_type <= 31)) {
zero_tid_required_flag = 1;
}
uint8_t nal_type_lsb = nal_type - (zero_tid_required_flag << 4);
byte = (zero_tid_required_flag<<7) + ((temporal_id + 1) << 4) + nal_type_lsb;
kvz_bitstream_writebyte(bitstream, byte); kvz_bitstream_writebyte(bitstream, byte);
// 5bits of nuh_layer_id + nuh_temporal_id_plus1(3) // 7bits of nuh_layer_id_plus1
byte = (temporal_id + 1) & 7; byte = 1<<1;
kvz_bitstream_writebyte(bitstream, byte); kvz_bitstream_writebyte(bitstream, byte);
#if VERBOSE #if VERBOSE
printf("%-40s u(%d) : %d\n", "forbidden_zero_bit", 1, 0); printf("%-40s u(%d) : %d\n", "zero_tid_required_flag", 1, zero_tid_required_flag);
printf("%-40s u(%d) : %d\n", "nal_unit_type", 6, nal_type); printf("%-40s u(%d) : %d\n", "nuh_temporal_id_plus1", 3, temporal_id + 1);
printf("%-40s u(%d) : %d\n", "nuh_layer_id", 6, temporal_id); printf("%-40s u(%d) : %d\n", "nal_unit_type_lsb", 4, nal_type_lsb);
printf("%-40s u(%d) : %d\n", "nuh_temporal_id", 3, temporal_id + 1); printf("%-40s u(%d) : %d\n", "nuh_layer_id_plus1", 6, 1);
#endif #endif
} }