diff --git a/src/bitstream.c b/src/bitstream.c index 736f7677..6ae2ab79 100644 --- a/src/bitstream.c +++ b/src/bitstream.c @@ -88,8 +88,14 @@ void bitstream_init(bitstream* stream) void bitstream_alloc(bitstream* stream, uint32_t alloc) { stream->buffer = (uint8_t*)malloc(alloc); + stream->bufferlen = alloc; //Clear just to be sure - memset(stream->buffer,0,alloc); + bitstream_clear_buffer(stream); +} + +void bitstream_clear_buffer(bitstream* stream) +{ + memset(stream->buffer,0,stream->bufferlen); stream->buffer_pos = 0; } @@ -139,8 +145,6 @@ void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits) stream->cur_bit = 0; if(stream->cur_byte==32) { - //We only have bytes 0..31 - stream->cur_byte--; //Flush data out bitstream_flush(stream); } @@ -151,10 +155,7 @@ void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits) { stream->data[stream->cur_byte] |= (data<<(bitsleft-bits)); stream->cur_bit+=bits; - } - - - + } } /* @@ -193,7 +194,7 @@ void bitstream_flush(bitstream* stream) if(stream->cur_byte) { /* Handle endianness issue */ - for(i = 0; i < stream->cur_byte+1; i++) + for(i = 0; i < stream->cur_byte; i++) { /* "network" is big-endian */ correct_endian = htonl(stream->data[i]); diff --git a/src/bitstream.h b/src/bitstream.h index b1def31b..867b2a7c 100644 --- a/src/bitstream.h +++ b/src/bitstream.h @@ -17,6 +17,7 @@ typedef struct FILE* output; uint8_t* buffer; uint32_t buffer_pos; + uint32_t bufferlen; } bitstream; typedef struct @@ -29,7 +30,8 @@ extern bitTable *g_exp_table; int floorLog2(unsigned int n); -void bitstream_alloc(bitstream* stream, uint32_t alloc); +void bitstream_alloc(bitstream* stream, uint32_t alloc); +void bitstream_clear_buffer(bitstream* stream); void bitstream_init(bitstream* stream); void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits); /* diff --git a/src/encoder.c b/src/encoder.c index 125ca558..d17c5880 100644 --- a/src/encoder.c +++ b/src/encoder.c @@ -51,6 +51,8 @@ void encode_one_frame(encoder_control* encoder) bitstream_align(encoder->stream); bitstream_flush(encoder->stream); nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 1, NAL_SEQ_PARAMETER_SET, 0); + + bitstream_clear_buffer(encoder->stream); encode_pic_parameter_set(encoder); bitstream_align(encoder->stream); @@ -72,8 +74,12 @@ void encode_pic_parameter_set(encoder_control* encoder) WRITE_U(encoder->stream, 0, 1, "sign_data_hiding_flag"); WRITE_U(encoder->stream, 0, 1, "cabac_init_present_flag"); + WRITE_U(encoder->stream, 0, 3, "num_ref_idx_l0_default_active_minus1"); + WRITE_U(encoder->stream, 0, 3, "num_ref_idx_l1_default_active_minus1"); + /* WRITE_UE(encoder->stream, 0, "num_ref_idx_l0_default_active_minus1"); WRITE_UE(encoder->stream, 0, "num_ref_idx_l1_default_active_minus1"); + */ WRITE_SE(encoder->stream, 0, "pic_init_qp_minus26"); WRITE_U(encoder->stream, 0, 1, "constrained_intra_pred_flag"); @@ -119,7 +125,7 @@ void encode_seq_parameter_set(encoder_control* encoder) WRITE_U(encoder->stream, 0, 1, "pcm_enabled_flag"); - WRITE_U(encoder->stream, 0, 1, "qpprime_y_zero_transquant_bypass_flag"); + WRITE_U(encoder->stream, 0, 1, "qpprime_y_zero_transquant_bypass_flag"); WRITE_UE(encoder->stream, 0, "log2_max_pic_order_cnt_lsb_minus4"); @@ -134,14 +140,14 @@ void encode_seq_parameter_set(encoder_control* encoder) WRITE_UE(encoder->stream, 0, "log2_min_transform_block_size_minus2"); WRITE_UE(encoder->stream, 3, "log2_diff_max_min_transform_block_size"); + WRITE_U(encoder->stream, 0, 1, "unknown_flag"); + WRITE_UE(encoder->stream, 2, "max_transform_hierarchy_depth_inter"); WRITE_UE(encoder->stream, 2, "max_transform_hierarchy_depth_intra"); - - + WRITE_U(encoder->stream, 0, 1, "scaling_list_enable_flag"); WRITE_U(encoder->stream, 0, 1, "chroma_pred_from_luma_enabled_flag"); WRITE_U(encoder->stream, 0, 1, "transform_skip_enabled_flag"); - WRITE_U(encoder->stream, 0, 1, "deblocking_filter_in_aps_enabled_flag"); WRITE_U(encoder->stream, 0, 1, "seq_loop_filter_across_slices_enabled_flag"); diff --git a/src/nal.c b/src/nal.c index 5d25637c..fae25c87 100644 --- a/src/nal.c +++ b/src/nal.c @@ -27,10 +27,10 @@ void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_r uint8_t byte; uint32_t i; uint8_t zerocount=0; - uint8_t emulation_prevention_three_byte = 0x03; - uint8_t start_code_prefix_one_3bytes = 0x01; - uint8_t zero = 0x00; - + const uint8_t emulation_prevention_three_byte = 0x03; + const uint8_t start_code_prefix_one_3bytes = 0x01; + const uint8_t zero = 0x00; + //start_code_prefix_one_3bytes fwrite(&zero, 1, 1, output); fwrite(&zero, 1, 1, output); @@ -59,10 +59,12 @@ void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_r else zerocount = 0; + /* Write the actual data */ fwrite(&buffer[i], 1, 1, output); } + //If last byte was 0, add emulation_prevention_three_byte - //if(buffer[buffer_len-1] == 0) - // fwrite(&emulation_prevention_three_byte, 1, 1, output); + if(buffer[buffer_len-1] == 0) + fwrite(&emulation_prevention_three_byte, 1, 1, output); } \ No newline at end of file