Added new function to free/clear pointers and added new types of SLICE_TRAIL_N NAL units to get multiple I-pictures in the stream.

This commit is contained in:
Marko Viitanen 2013-02-21 16:45:22 +02:00
parent 26a2c71e3a
commit 10fe0f8018
11 changed files with 114 additions and 64 deletions

View file

@ -162,6 +162,15 @@ void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits)
* \brief Align the bitstream
*/
void bitstream_align(bitstream* stream)
{
bitstream_put(stream,1, 1);
if((stream->cur_bit&7) != 0)
{
bitstream_put(stream,0, 8-(stream->cur_bit&7));
}
}
void bitstream_align_zero(bitstream* stream)
{
if((stream->cur_bit&7) != 0)
{

View file

@ -41,6 +41,7 @@ void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits);
bitstream_put(stream,g_exp_table[index].value,g_exp_table[index].len); }
void bitstream_align(bitstream* stream);
void bitstream_align_zero(bitstream* stream);
void bitstream_flush(bitstream* stream);
void init_exp_golomb(uint32_t len);

View file

@ -221,7 +221,6 @@ void cabac_encodeFlush(cabac_data* data, uint8_t end )
}
cabac_finish(data);
bitstream_put(data->stream,1,1);
bitstream_align(data->stream);
cabac_start(data);

View file

@ -53,17 +53,8 @@ int config_init(config* cfg)
*/
int config_destroy(config* cfg)
{
if(cfg->input != NULL)
{
free(cfg->input);
cfg->input = NULL;
}
if(cfg->output != NULL)
{
free(cfg->output);
cfg->output = NULL;
}
free_pointer(cfg->input);
free_pointer(cfg->output);
free(cfg);
return 1;
@ -96,31 +87,31 @@ int config_read(config* cfg,int argc, char* argv[])
{
case 'i': /* Input */
/* Allocate +1 for \0 */
cfg->input = malloc(strlen(argv[arg])+1);
cfg->input = (char *)malloc(strlen(argv[arg])+1);
memcpy(cfg->input, argv[arg], strlen(argv[arg])+1);
break;
case 'o': /* Output */
cfg->output = malloc(strlen(argv[arg])+1);
cfg->output = (char *)malloc(strlen(argv[arg])+1);
memcpy(cfg->output, argv[arg], strlen(argv[arg])+1);
break;
case 'd': /* Debug */
cfg->debug = malloc(strlen(argv[arg])+1);
cfg->debug = (char *)malloc(strlen(argv[arg])+1);
memcpy(cfg->debug, argv[arg], strlen(argv[arg])+1);
break;
case 'w': /* width */
/* Get skipped frames count */
/* Get picture width */
cfg->width = atoi(argv[arg]);
break;
case 'h': /* height */
/* Get skipped frames count */
/* Get picture height */
cfg->height = atoi(argv[arg]);
break;
case 'n': /* Framecount */
/* Get frame count */
/* Get frame count to encode */
cfg->frames = atoi(argv[arg]);
break;
default:
/* Unknown command */
/* Unknown command, print error message and ignore */
fprintf(stderr, "%c is not a known option\r\n", option);
break;
}

View file

@ -25,7 +25,7 @@ cabac_ctx *SplitFlagSCModel;
cabac_ctx g_SplitFlagSCModel[3]; /*<! \brief split flag context models */
cabac_ctx g_IntraModeSCModel; /*<! \brief intra mode context models */
cabac_ctx g_ChromaPredSCModel[2];
cabac_ctx g_TransSubdivSCModel[4]; /*<! \brief intra mode context models */
cabac_ctx g_TransSubdivSCModel[3]; /*<! \brief intra mode context models */
cabac_ctx g_QtCbfSCModelY[3];
cabac_ctx g_QtCbfSCModelU[3];
//cabac_ctx g_QtCbfSCModelV[3];
@ -61,14 +61,13 @@ void init_contexts(encoder_control *encoder, int8_t SLICE)
ctx_init(&g_cCUAbsSCModel_chroma[1], encoder->QP, INIT_ABS_FLAG[SLICE][5]);
for(i = 0; i < 4; i++)
{
ctx_init(&g_TransSubdivSCModel[i], encoder->QP, INIT_TRANS_SUBDIV_FLAG[SLICE][i]);
{
ctx_init(&g_CUSigCoeffGroupSCModel[i], encoder->QP, INIT_SIG_CG_FLAG[SLICE][i]);
ctx_init(&g_cCUAbsSCModel_luma[i], encoder->QP, INIT_ABS_FLAG[SLICE][i]);
}
for(i = 0; i < 3; i++)
{
ctx_init(&g_TransSubdivSCModel[i], encoder->QP, INIT_TRANS_SUBDIV_FLAG[SLICE][i]);
ctx_init(&g_QtCbfSCModelY[i], encoder->QP, INIT_QT_CBF[SLICE][i]);
ctx_init(&g_QtCbfSCModelU[i], encoder->QP, INIT_QT_CBF[SLICE][i+3]);
//cxt_init(&g_QtCbfSCModelV[i], encoder->QP, INIT_QT_CBF[SLICE][i]);

View file

@ -34,7 +34,7 @@ extern cabac_ctx *SplitFlagSCModel;
extern cabac_ctx g_SplitFlagSCModel[3];
extern cabac_ctx g_IntraModeSCModel;
extern cabac_ctx g_ChromaPredSCModel[2];
extern cabac_ctx g_TransSubdivSCModel[4];
extern cabac_ctx g_TransSubdivSCModel[3];
extern cabac_ctx g_QtCbfSCModelY[3];
extern cabac_ctx g_QtCbfSCModelU[3];
extern cabac_ctx g_PartSizeSCModel;
@ -60,11 +60,11 @@ static const uint8_t INIT_INTRA_PRED_MODE[3] = { 183,154,184 };
static const uint8_t INIT_CHROMA_PRED_MODE[3][2] = { { 152, 139 }, { 152, 139 }, { 63, 139 } };
#define CNU 154
static const uint8_t INIT_TRANS_SUBDIV_FLAG[3][4] =
static const uint8_t INIT_TRANS_SUBDIV_FLAG[3][3] =
{
{ CNU, 153, 138, 138 },
{ CNU, 124, 138, 94 },
{ CNU, 224, 167, 122 }
{ 224, 167, 122 },
{ 124, 138, 94 },
{ 153, 138, 138 }
};
static const uint8_t INIT_QT_CBF[3][6] =

View file

@ -131,7 +131,7 @@
/* Start coding cycle */
while(!feof(input) && (!cfg->frames || curFrame < cfg->frames))
{
{
/* Read one frame from the input */
fread(encoder->in.cur_pic.yData, cfg->width*cfg->height,1,input);
fread(encoder->in.cur_pic.uData, cfg->width*cfg->height/4,1,input);

View file

@ -233,25 +233,26 @@ void encode_one_frame(encoder_control* encoder)
/* output parameters before first frame */
if(encoder->frame == 0)
{
/* Sequence Parameter Set (SPS) */
encode_seq_parameter_set(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, 1);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_SEQ_PARAMETER_SET, 0);
bitstream_clear_buffer(encoder->stream);
/* Video Parameter Set (VPS) */
encode_vid_parameter_set(encoder);
bitstream_align(encoder->stream);
bitstream_flush(encoder->stream);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 1, NAL_VID_PARAMETER_SET, 1);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_VID_PARAMETER_SET, 0);
bitstream_clear_buffer(encoder->stream);
/* Picture Parameter Set (PPS) */
encode_pic_parameter_set(encoder);
bitstream_align(encoder->stream);
bitstream_flush(encoder->stream);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 1, NAL_PIC_PARAMETER_SET, 1);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_PIC_PARAMETER_SET, 0);
bitstream_clear_buffer(encoder->stream);
/* First slice is IDR */
@ -263,11 +264,45 @@ void encode_one_frame(encoder_control* 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, 1);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_IDR_SLICE, 0);
bitstream_clear_buffer(encoder->stream);
}
else if(encoder->frame < 3)
else if(encoder->frame < 10)
{
cabac_start(&cabac);
encoder->in.cur_pic.type = 0;
encode_slice_header(encoder);
bitstream_align(encoder->stream);
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, 0, encoder->frame);
bitstream_clear_buffer(encoder->stream);
/*
encoder->in.cur_pic.type = 0;
encode_slice_header(encoder);
bitstream_align(encoder->stream);
bitstream_flush(encoder->stream);
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, 0, 0);
bitstream_clear_buffer(encoder->stream);
*/
/*
cabac_start(&cabac);
encoder->in.cur_pic.type = NAL_IDR_SLICE;
encode_slice_header(encoder);
bitstream_align(encoder->stream);
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);
*/
/* Non-IDR slice */
/*
cabac_start(&cabac);
@ -335,6 +370,7 @@ void encode_pic_parameter_set(encoder_control* encoder)
void encode_PTL(encoder_control *encoder)
{
int i;
/*PTL*/
/*Profile Tier*/
WRITE_U(encoder->stream, 0, 2, "XXX_profile_space[]");
@ -353,6 +389,14 @@ void encode_PTL(encoder_control *encoder)
/*end Profile Tier */
WRITE_U(encoder->stream, 0, 8, "general_level_idc");
WRITE_U(encoder->stream, 0, 1, "sub_layer_profile_present_flag");
WRITE_U(encoder->stream, 0, 1, "sub_layer_level_present_flag");
for(i = 1; i < 8; i++)
{
WRITE_U(encoder->stream, 0, 2, "reserved_zero_2bits");
}
/*end PTL*/
}
@ -363,9 +407,9 @@ void encode_seq_parameter_set(encoder_control* encoder)
#endif
/* ToDo: profile IDC and level IDC should be defined later on */
WRITE_U(encoder->stream, 0, 4, "sps_video_parameter_set_id");
WRITE_U(encoder->stream, 0, 3, "sps_max_sub_layers_minus1");
WRITE_U(encoder->stream, 1, 3, "sps_max_sub_layers_minus1");
WRITE_U(encoder->stream, 1, 1, "sps_temporal_id_nesting_flag");
WRITE_U(encoder->stream, 0, 1, "sps_temporal_id_nesting_flag");
encode_PTL(encoder);
@ -434,6 +478,7 @@ void encode_seq_parameter_set(encoder_control* encoder)
void encode_vid_parameter_set(encoder_control* encoder)
{
int i;
#ifdef _DEBUG
printf("=========== Video Parameter Set ID: 0 ===========\n");
#endif
@ -441,17 +486,20 @@ void encode_vid_parameter_set(encoder_control* encoder)
WRITE_U(encoder->stream, 0, 4, "vps_video_parameter_set_id");
WRITE_U(encoder->stream, 3, 2, "vps_reserved_three_2bits" );
WRITE_U(encoder->stream, 0, 6, "vps_reserved_zero_6bits" );
WRITE_U(encoder->stream, 0, 3, "vps_max_sub_layers_minus1");
WRITE_U(encoder->stream, 1, 1, "vps_temporal_id_nesting_flag");
WRITE_U(encoder->stream, 1, 3, "vps_max_sub_layers_minus1");
WRITE_U(encoder->stream, 0, 1, "vps_temporal_id_nesting_flag");
WRITE_U(encoder->stream, 0xffff, 16, "vps_reserved_ffff_16bits");
encode_PTL(encoder);
WRITE_U(encoder->stream, 0, 1, "vps_sub_layer_ordering_info_present_flag");
//for each layer
for(i = 0; i < 1; i++)
{
WRITE_UE(encoder->stream, 0, "vps_max_dec_pic_buffering");
WRITE_UE(encoder->stream, 0, "vps_num_reorder_pics");
WRITE_UE(encoder->stream, 0, "vps_max_latency_increase");
}
//end for
WRITE_U(encoder->stream, 0, 6, "vps_max_nuh_reserved_zero_layer_id");
WRITE_UE(encoder->stream, 0, "vps_max_op_sets_minus1");
@ -531,10 +579,9 @@ void encode_slice_header(encoder_control* encoder)
}
else
{
WRITE_U(encoder->stream, encoder->frame+1, 4, "pic_order_cnt_lsb");
WRITE_U(encoder->stream, encoder->frame, 4, "pic_order_cnt_lsb");
WRITE_U(encoder->stream, 1, 1, "short_term_ref_pic_set_sps_flag");
//WRITE_U(encoder->stream, 1, 1, "inter_ref_pic_set_prediction_flag");
WRITE_UE(encoder->stream, 0, "short_term_ref_pic_set_idx");
//WRITE_UE(encoder->stream, 0, "short_term_ref_pic_set_idx");
}
//end if
//end if
@ -547,12 +594,7 @@ void encode_slice_header(encoder_control* encoder)
/* Skip flags that are not present */
// if !entropy_slice_flag
WRITE_SE(encoder->stream, 0, "slice_qp_delta");
/*
WRITE_UE(encoder->stream, 0, "5_minus_max_num_merge_cand");
WRITE_U(encoder->stream, 1, 1, "alignment");
*/
WRITE_U(encoder->stream, 1, 1, "alignment");
//WRITE_U(encoder->stream, 1, 1, "alignment");
}
@ -641,7 +683,7 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
}
}
/*
if(yCtb > 20 && xCtb > 20)
if(border_x && border_y)//yCtb == 20 && xCtb == 20)
{
cur_CU->type = CU_INTRA;
}
@ -660,13 +702,13 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
/*end partsize*/
//If MODE_INTRA
/* Code IPCM block */
if(cur_CU->type == CU_PCM || cur_CU->type == CU_NOTSET)
if(cur_CU->type == CU_PCM || cur_CU->type == CU_NOTSET) /* also handling NOTSET conditions */
{
cabac_encodeBinTrm(&cabac, 1); /* IPCMFlag == 1 */
//printf("\tIPCMFlag = 1\n");
cabac_finish(&cabac);
WRITE_U(cabac.stream, 1, 1, "stop_bit");
WRITE_U(cabac.stream, 0, 1, "numSubseqIPCM_flag");
//WRITE_U(cabac.stream, 1, 1, "stop_bit");
//WRITE_U(cabac.stream, 0, 1, "numSubseqIPCM_flag");
bitstream_align(cabac.stream);
/* PCM sample */
{
@ -707,6 +749,7 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
} /* end Code IPCM block */
else if(cur_CU->type == CU_INTRA)
{
static int predMode = 0;
cabac_encodeBinTrm(&cabac, 0); /* IPCMFlag == 0 */
cabac.ctx = &g_IntraModeSCModel;
@ -716,17 +759,21 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
Int preds[3] = {-1, -1, -1};
Int predNum = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds);
*/
CABAC_BINS_EP(&cabac, 0, 5, "intraPredMode");
CABAC_BINS_EP(&cabac, predMode, 5, "intraPredMode");
predMode++;
predMode = predMode % 10;
if(encoder->in.video_format != FORMAT_400)
{
cabac.ctx = &g_ChromaPredSCModel[0];
CABAC_BIN(&cabac,0,"IntraPredChroma");
CABAC_BIN(&cabac,1,"IntraPredChroma");
CABAC_BINS_EP(&cabac, 0, 2, "intraPredModeChroma");
}
/* Coeff */
/* Transform tree */
cabac.ctx = &g_TransSubdivSCModel[1]; /* //uiLog2TransformBlockSize */
cabac.ctx = &g_TransSubdivSCModel[0]; /* //uiLog2TransformBlockSize */
CABAC_BIN(&cabac,0,"TransformSubdivFlag");
/* We don't subdiv and we have 64>>depth transform size */
@ -737,8 +784,9 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
/*
Quant and transform here...
*/
CbY = 1; /* Let's pretend we have luma coefficients */
//CbY = 1; /* Let's pretend we have luma coefficients */
/* Signal if chroma data is present */
if(encoder->in.video_format != FORMAT_400)
{
/* Non-zero chroma U Tcoeffs */
@ -1014,6 +1062,7 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
}
/* end Transform tree */
/* end Coeff */
}
else
{
@ -1022,11 +1071,6 @@ void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, ui
}
//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 */
}

View file

@ -61,4 +61,6 @@
#define INLINE inline
#endif
#define free_pointer(pointer) if(pointer != NULL) { free(pointer); pointer = NULL; }
#endif

View file

@ -37,16 +37,21 @@ 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 */
if(temporal_id == 0)
{
fwrite(&zero, 1, 1, output);
}
fwrite(&zero, 1, 1, output);
fwrite(&zero, 1, 1, output);
fwrite(&start_code_prefix_one_3bytes, 1, 1, output);
/* forbidden_zero_flag(1) + nal_unit_type(6) + 1bit of reserved_one_6bits(3)*/
/* Handle header bits with full bytes instead of using bitstream */
/* forbidden_zero_flag(1) + nal_unit_type(6) + 1bit of nuh_layer_id*/
byte = nal_type<<1;
fwrite(&byte, 1, 1, output);
/* 5bits of reserved_one_6bits + nuh_temporal_id_plus1(3) */
byte = temporal_id&7;
/* 5bits of nuh_layer_id + nuh_temporal_id_plus1(3) */
byte = (temporal_id+1)&7;
fwrite(&byte, 1, 1, output);
/* Write out bytes and add emulation_prevention_three_byte when needed */

View file

@ -13,7 +13,7 @@
#ifndef __NAL_H
#define __NAL_H
enum { NAL_NONIDR_SLICE = 0x1,NAL_IDR_SLICE = 19/*0x8*/, NAL_VID_PARAMETER_SET = 32,NAL_SEQ_PARAMETER_SET = 33, NAL_PIC_PARAMETER_SET = 34 };
enum { NAL_NONIDR_SLICE = 0x1,NAL_IDR_SLICE = 19, NAL_VID_PARAMETER_SET = 32,NAL_SEQ_PARAMETER_SET = 33, NAL_PIC_PARAMETER_SET = 34 };
void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id);