📄 slice.c
字号:
Slice *slice;
const int buffer_size = (img->width * img->height * 4); // AH 190202: There can be data expansion with
// low QP values. So, we make sure that buffer
// does not overflow. 4 is probably safe multiplier.
if ((slice = (Slice *) calloc(1, sizeof(Slice))) == NULL) no_mem_exit ("malloc_slice: slice structure");
if (input->symbol_mode == CABAC)
{
// create all context models
slice->mot_ctx = create_contexts_MotionInfo();
slice->tex_ctx = create_contexts_TextureInfo();
}
slice->max_part_nr = input->partition_mode==0?1:3;
//for IDR img there should be only one partition
if(img->currentPicture->idr_flag)
slice->max_part_nr = 1;
assignSE2partition[0] = assignSE2partition_NoDP;
//ZL
//for IDR img all the syntax element should be mapped to one partition
if(!img->currentPicture->idr_flag&&input->partition_mode==1)
assignSE2partition[1] = assignSE2partition_DP;
else
assignSE2partition[1] = assignSE2partition_NoDP;
slice->num_mb = 0; // no coded MBs so far
if ((slice->partArr = (DataPartition *) calloc(slice->max_part_nr, sizeof(DataPartition))) == NULL) no_mem_exit ("malloc_slice: partArr");
for (i=0; i<slice->max_part_nr; i++) // loop over all data partitions
{
dataPart = &(slice->partArr[i]);
if ((dataPart->bitstream = (Bitstream *) calloc(1, sizeof(Bitstream))) == NULL) no_mem_exit ("malloc_slice: Bitstream");
if ((dataPart->bitstream->streamBuffer = (byte *) calloc(buffer_size, sizeof(byte))) == NULL) no_mem_exit ("malloc_slice: StreamBuffer");
// Initialize storage of bitstream parameters
}
return slice;
}
/*!
************************************************************************
* \brief
* Memory frees of all Slice structures and of its dependent
* data structures
* \par Input:
* Image Parameters struct struct img_par *img
************************************************************************
*/
void free_slice_list(Picture *currPic)
{
int i;
for (i=0; i<currPic->no_slices; i++)
{
free_slice (currPic->slices[i]);
currPic->slices[i]=NULL;
}
}
/*!
************************************************************************
* \brief
* Memory frees of the Slice structure and of its dependent
* data structures
* \param slice:
* Slice to be freed
************************************************************************
*/
static void free_slice(Slice *slice)
{
int i;
DataPartition *dataPart;
if (slice != NULL)
{
for (i=0; i<slice->max_part_nr; i++) // loop over all data partitions
{
dataPart = &(slice->partArr[i]);
if (dataPart != NULL)
{
if (dataPart->bitstream->streamBuffer != NULL)
free(dataPart->bitstream->streamBuffer);
if (dataPart->bitstream != NULL)
free(dataPart->bitstream);
}
}
if (slice->partArr != NULL)
free(slice->partArr);
if (input->symbol_mode == CABAC)
{
delete_contexts_MotionInfo(slice->mot_ctx);
delete_contexts_TextureInfo(slice->tex_ctx);
}
//free(img->currentSlice);
free(slice);
}
}
/*!
************************************************************************
* \brief
* This function set the value of a bit in a bitstream to 1
************************************************************************
*/
void modify_redundant_pic_cnt(unsigned char *buffer)
{
unsigned char tmp = 1 << (rpc_bits_to_go-1);
buffer[rpc_bytes_to_go] |= tmp;
}
void set_ref_pic_num()
{
int i,j;
//! need to add field ref_pic_num that handles field pair.
for (i=0;i<listXsize[LIST_0];i++)
{
enc_picture->ref_pic_num [LIST_0][i]=listX[LIST_0][i]->poc * 2 + ((listX[LIST_0][i]->structure==BOTTOM_FIELD)?1:0) ;
enc_picture->frm_ref_pic_num [LIST_0][i]=listX[LIST_0][i]->frame_poc * 2;
enc_picture->top_ref_pic_num [LIST_0][i]=listX[LIST_0][i]->top_poc * 2;
enc_picture->bottom_ref_pic_num [LIST_0][i]=listX[LIST_0][i]->bottom_poc * 2 + 1;
}
for (i=0;i<listXsize[LIST_1];i++)
{
enc_picture->ref_pic_num [LIST_1][i]=listX[LIST_1][i]->poc *2 + ((listX[LIST_1][i]->structure==BOTTOM_FIELD)?1:0);
enc_picture->frm_ref_pic_num [LIST_1][i]=listX[LIST_1][i]->frame_poc * 2;
enc_picture->top_ref_pic_num [LIST_1][i]=listX[LIST_1][i]->top_poc * 2;
enc_picture->bottom_ref_pic_num [LIST_1][i]=listX[LIST_1][i]->bottom_poc * 2 + 1;
}
if (img->structure==FRAME)
for (j=2;j<6;j++)
for (i=0;i<listXsize[j];i++)
{
enc_picture->ref_pic_num[j][i] = listX[j][i]->poc * 2 + ((listX[j][i]->structure==BOTTOM_FIELD)?1:0);
enc_picture->frm_ref_pic_num[j][i] = listX[j][i]->frame_poc * 2 ;
enc_picture->top_ref_pic_num[j][i] = listX[j][i]->top_poc * 2 ;
enc_picture->bottom_ref_pic_num[j][i] = listX[j][i]->bottom_poc * 2 + 1;
}
}
/*!
************************************************************************
* \brief
* decide reference picture reordering, Frame only
************************************************************************
*/
void poc_ref_pic_reorder(StorablePicture **list, unsigned num_ref_idx_lX_active, int *remapping_of_pic_nums_idc, int *abs_diff_pic_num_minus1, int *long_term_pic_idx, int weighted_prediction, int list_no)
{
unsigned i,j,k;
int currPicNum, picNumLXPred;
int default_order[32];
int re_order[32];
int tmp_reorder[32];
int list_sign[32];
int reorder_stop, no_reorder;
int poc_diff[32];
int tmp_value, diff;
int abs_poc_dist;
if (img->structure==FRAME)
{
currPicNum = img->frame_num;
}
else
{
currPicNum = 2 * img->frame_num + 1;
}
picNumLXPred = currPicNum;
// First assign default list order.
for (i=0; i<num_ref_idx_lX_active; i++)
{
default_order[i] = list[i]->pic_num;
}
// Now access all references in buffer and assign them
// to a pottential reordering list. For each one of these
// references compute the poc distance compared to current
// frame.
for (i=0; i<dpb.ref_frames_in_buffer; i++)
{
re_order[i] = dpb.fs_ref[i]->frame->pic_num;
if (dpb.fs_ref[i]->is_used==3 && (dpb.fs_ref[i]->frame->used_for_reference)&&(!dpb.fs_ref[i]->frame->is_long_term))
{
abs_poc_dist = abs(dpb.fs_ref[i]->frame->poc - enc_picture->poc) ;
poc_diff[i] = abs_poc_dist;
if (list_no == LIST_0)
{
if ( enc_picture->poc < dpb.fs_ref[i]->frame->poc)
{
list_sign[i] = +1;
}
else
{
list_sign[i] = -1;
}
}
else
{
if (enc_picture->poc > dpb.fs_ref[i]->frame->poc)
{
list_sign[i] = +1;
}
else
{
list_sign[i] = -1;
}
}
}
}
// now sort these references based on poc (temporal) distance
for (i=0; i< dpb.ref_frames_in_buffer-1; i++)
{
for (j=i+1; j< dpb.ref_frames_in_buffer; j++)
{
if (poc_diff[i]>poc_diff[j] || (poc_diff[i] == poc_diff[j] && list_sign[j] > list_sign[i]))
{
tmp_value = poc_diff[i];
poc_diff[i] = poc_diff[j];
poc_diff[j] = tmp_value;
tmp_value = re_order[i];
re_order[i] = re_order[j];
re_order[j] = tmp_value ;
tmp_value = list_sign[i];
list_sign[i] = list_sign[j];
list_sign[j] = tmp_value ;
}
}
}
// Check versus default list to see if any
// change has happened
no_reorder = 1;
for (i=0; i<num_ref_idx_lX_active; i++)
{
if (default_order[i] != re_order[i])
{
no_reorder = 0;
}
}
// If different, then signal reordering
if (no_reorder==0)
{
for (i=0; i<num_ref_idx_lX_active; i++)
{
diff = re_order[i]-picNumLXPred;
if (diff <= 0)
{
remapping_of_pic_nums_idc[i] = 0;
abs_diff_pic_num_minus1[i] = abs(diff)-1;
}
else
{
remapping_of_pic_nums_idc[i] = 1;
abs_diff_pic_num_minus1[i] = abs(diff)-1;
}
picNumLXPred = re_order[i];
tmp_reorder[i] = re_order[i];
k = i;
for (j=i; j<num_ref_idx_lX_active; j++)
{
if (default_order[j] != re_order[i])
{
++k;
tmp_reorder[k] = default_order[j];
}
}
reorder_stop = 1;
for(j=i+1; j<num_ref_idx_lX_active; j++)
{
if (tmp_reorder[j] != re_order[j])
{
reorder_stop = 0;
break;
}
}
if (reorder_stop==1)
{
++i;
break;
}
for(j=0; j<num_ref_idx_lX_active; j++)
{
default_order[j] = tmp_reorder[j];
}
}
remapping_of_pic_nums_idc[i] = 3;
for(j=0; j<num_ref_idx_lX_active; j++)
{
default_order[j] = tmp_reorder[j];
}
if (list_no==0)
{
img->currentSlice->ref_pic_list_reordering_flag_l0=1;
}
else
{
img->currentSlice->ref_pic_list_reordering_flag_l1=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -