From 953c04d89627de8298af5f4473cae803b5f3f676 Mon Sep 17 00:00:00 2001 From: Marko Viitanen Date: Wed, 6 Jun 2012 16:20:29 +0300 Subject: [PATCH] More work on the slice encoding, trying to send some dummy data --- src/cabac.c | 5 ++-- src/cabac.h | 5 +--- src/encoder.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++----- src/encoder.h | 3 ++- src/global.h | 3 +-- src/nal.c | 9 ++++--- src/nal.h | 8 ++++-- 7 files changed, 86 insertions(+), 20 deletions(-) diff --git a/src/cabac.c b/src/cabac.c index 239348e2..bcf824bf 100644 --- a/src/cabac.c +++ b/src/cabac.c @@ -14,10 +14,11 @@ #include #include #include "global.h" -#include "cabac.h" #include "bitstream.h" +#include "cabac.h" -const uint8_t g_aucNextStateMPS[ 128 ] = + +const uint8_t g_aucNextStateMPS[ 128 ] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, diff --git a/src/cabac.h b/src/cabac.h index 691eda6f..253f3976 100644 --- a/src/cabac.h +++ b/src/cabac.h @@ -13,10 +13,6 @@ #ifndef __CABAC_H #define __CABAC_H -#include "bitstream.h" - - - extern const uint8_t g_aucNextStateMPS[ 128 ]; extern const uint8_t g_aucNextStateLPS[ 128 ]; extern const uint32_t g_entropyBits[128]; @@ -30,6 +26,7 @@ typedef struct uint8_t ucState; uint32_t binsCoded; } cabac_ctx; + #define CTX_STATE(ctx) (ctx.ucState>>1) #define CTX_MPS(ctx) (ctx.ucState&1) diff --git a/src/encoder.c b/src/encoder.c index 0fbfa146..0e311050 100644 --- a/src/encoder.c +++ b/src/encoder.c @@ -32,6 +32,13 @@ void init_encoder_input(encoder_input* input,FILE* inputfile, uint32_t width, ui input->width = width; input->height = height; + input->height_in_LCU = height / LCU_WIDTH; + input->width_in_LCU = width / LCU_WIDTH; + if(input->height_in_LCU * LCU_WIDTH < height) + input->height_in_LCU++; + if(input->width_in_LCU * LCU_WIDTH < width) + input->width_in_LCU++; + input->cur_pic.width = width; input->cur_pic.height = height; input->cur_pic.referenced = 0; @@ -59,8 +66,20 @@ void encode_one_frame(encoder_control* encoder) nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 1, NAL_PIC_PARAMETER_SET, 0); bitstream_clear_buffer(encoder->stream); + encode_slice_header(encoder); encode_slice_data(encoder); + cabac_flush(&cabac); + bitstream_align(encoder->stream); + bitstream_flush(encoder->stream); + nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_IDR_SLICE, 0); + bitstream_clear_buffer(encoder->stream); + } + else + { + encode_slice_header(encoder); + encode_slice_data(encoder); + cabac_flush(&cabac); bitstream_align(encoder->stream); bitstream_flush(encoder->stream); nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_IDR_SLICE, 0); @@ -116,7 +135,9 @@ void encode_seq_parameter_set(encoder_control* encoder) WRITE_U(encoder->stream, 0, 1, "pic_cropping_flag"); WRITE_UE(encoder->stream, 0, "bit_depth_luma_minus8"); WRITE_UE(encoder->stream, 0, "bit_depth_chroma_minus8"); - WRITE_U(encoder->stream, 0, 1, "pcm_enabled_flag"); + WRITE_U(encoder->stream, 1, 1, "pcm_enabled_flag"); + WRITE_U(encoder->stream, 7, 4, "pcm_bit_depth_luma_minus1"); + WRITE_U(encoder->stream, 7, 4, "pcm_bit_depth_chroma_minus1"); WRITE_U(encoder->stream, 0, 1, "qpprime_y_zero_transquant_bypass_flag"); WRITE_UE(encoder->stream, 0, "log2_max_pic_order_cnt_lsb_minus4"); WRITE_UE(encoder->stream, 0, "max_dec_pic_buffering"); @@ -131,15 +152,16 @@ void encode_seq_parameter_set(encoder_control* encoder) //If log2MinCUSize == 3 WRITE_U(encoder->stream, 0, 1, "DisInter4x4"); - /* //IF PCM + //IF PCM { WRITE_UE(encoder->stream, 0, "log2_min_pcm_coding_block_size_minus3"); WRITE_UE(encoder->stream, 0, "log2_diff_max_min_pcm_coding_block_size"); } - */ + WRITE_UE(encoder->stream, 2, "max_transform_hierarchy_depth_inter"); - WRITE_UE(encoder->stream, 2, "max_transform_hierarchy_depth_intra"); + 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"); @@ -148,7 +170,10 @@ void encode_seq_parameter_set(encoder_control* encoder) WRITE_U(encoder->stream, 0, 1, "asymmetric_motion_partitions_enabled_flag"); WRITE_U(encoder->stream, 0, 1, "nsrqt_enabled_flag"); WRITE_U(encoder->stream, 0, 1, "sample_adaptive_offset_enabled_flag"); - WRITE_U(encoder->stream, 0, 1, "adaptive_loop_filter_enabled_flag"); + WRITE_U(encoder->stream, 0, 1, "adaptive_loop_filter_enabled_flag"); + //IF PCM + WRITE_U(encoder->stream, 0, 1, "pcm_loop_filter_disable_flag"); + //endif WRITE_U(encoder->stream, 0, 1, "temporal_id_nesting_flag"); WRITE_UE(encoder->stream, 0, "num_short_term_ref_pic_sets"); WRITE_U(encoder->stream, 0, 1, "long_term_ref_pics_present_flag"); @@ -158,6 +183,9 @@ void encode_seq_parameter_set(encoder_control* encoder) void encode_slice_header(encoder_control* encoder) { +#ifdef _DEBUG + printf("=========== Slice ===========\n"); +#endif WRITE_U(encoder->stream, 1, 1, "first_slice_in_pic_flag"); WRITE_UE(encoder->stream, SLICE_I, "slice_type"); @@ -181,12 +209,45 @@ void encode_slice_header(encoder_control* encoder) /* Skip unpresent flags */ // if !entropy_slice_flag - WRITE_UE(encoder->stream, 0, "slice_qp_delta"); + WRITE_UE(encoder->stream, 0, "slice_qp_delta"); + WRITE_UE(encoder->stream, 0, "5_minus_max_num_merge_cand"); } +cabac_ctx SplitFlagSCModel; void encode_slice_data(encoder_control* encoder) { uint16_t xCtb,yCtb; + cxt_init(&SplitFlagSCModel, 26, 0); + cxt_init(&cabac.ctx, 26, 0); + for(yCtb = 0; yCtb < encoder->in.height_in_LCU; yCtb++) + { + for(xCtb = 0; xCtb < encoder->in.width_in_LCU; xCtb++) + { + uint8_t depth = 0; + encode_coding_tree(encoder, xCtb,yCtb, depth); + } + } +} + +void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, uint8_t depth) +{ + + //Split flag + cabac_encodeBin(&cabac, 5); + + /* coding_unit( x0, y0, log2CbSize ) */ + /* prediction_unit 2Nx2N*/ + //If MODE_INTRA + + //endif + + /* end prediction unit */ + cabac_encodeBin(&cabac, 0); //prev_intra_luma_pred_flag + + cabac_encodeBin(&cabac, 1); //rem_intra_luma_pred_mode + + /* end coding_unit */ + } diff --git a/src/encoder.h b/src/encoder.h index e062f4a0..2a292bb3 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -13,8 +13,8 @@ #ifndef _ENCODER_H #define _ENCODER_H -#include "bitstream.h" #include "picture.h" +#include "bitstream.h" typedef struct encoder_control; @@ -58,5 +58,6 @@ void encode_seq_parameter_set(encoder_control* encoder); void encode_pic_parameter_set(encoder_control* encoder); void encode_slice_data(encoder_control* encoder); void encode_slice_header(encoder_control* encoder); +void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, uint8_t depth); #endif \ No newline at end of file diff --git a/src/global.h b/src/global.h index 356d2312..df0bb88f 100644 --- a/src/global.h +++ b/src/global.h @@ -23,7 +23,7 @@ #define MAX(a,b) (((a)<(b))?(b):(a)) #define MIN(a,b) (((a)>(b))?(b):(a)) -#define LCU 64 /*!< Largest Coding Unit */ +#define LCU_WIDTH 64 /*!< Largest Coding Unit */ #define VERSION_STRING "0.1 " #define VERSION 0.1 @@ -43,5 +43,4 @@ #define MODE_NONE 15 - #endif \ No newline at end of file diff --git a/src/nal.c b/src/nal.c index fae25c87..287cf79b 100644 --- a/src/nal.c +++ b/src/nal.c @@ -17,9 +17,12 @@ #include #include "global.h" #include "config.h" -#include "encoder.h" -#include "cabac.h" +#include "bitstream.h" #include "picture.h" +#include "cabac.h" +#include "encoder.h" + + #include "nal.h" void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id) @@ -32,7 +35,7 @@ void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_r const uint8_t zero = 0x00; //start_code_prefix_one_3bytes - fwrite(&zero, 1, 1, output); + //fwrite(&zero, 1, 1, output); fwrite(&zero, 1, 1, output); fwrite(&zero, 1, 1, output); fwrite(&start_code_prefix_one_3bytes, 1, 1, output); diff --git a/src/nal.h b/src/nal.h index 32c45f93..2d21fd60 100644 --- a/src/nal.h +++ b/src/nal.h @@ -10,7 +10,11 @@ NAL function headers */ +#ifndef __NAL_H +#define __NAL_H -enum { NAL_IDR_SLICE = 5, NAL_SEQ_PARAMETER_SET = 7, NAL_PIC_PARAMETER_SET = 8 }; +enum { NAL_IDR_SLICE = 0x45, NAL_SEQ_PARAMETER_SET = 7, NAL_PIC_PARAMETER_SET = 8 }; -void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id); \ No newline at end of file +void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id); + +#endif