📄 image.c
字号:
else
img->current_mb_nr = currSlice->start_mb_nr;
/*
LC:
Now I need to read the slice ID, which depends on the value of
redundant_pic_cnt_present_flag (pag.49).
*/
slice_id_a = ue_v("NALU:SLICE_A slice_idr", currStream);
if (active_pps->entropy_coding_mode_flag)
{
int ByteStartPosition = currStream->frame_bitoffset/8;
if (currStream->frame_bitoffset%8 != 0)
{
ByteStartPosition++;
}
arideco_start_decoding (&currSlice->partArr[0].de_cabac, currStream->streamBuffer, ByteStartPosition, &currStream->read_len, img->type);
}
// printf ("read_new_slice: returning %s\n", current_header == SOP?"SOP":"SOS");
break;
case NALU_TYPE_DPB:
/* LC: inserting the code related to DP processing */
currStream = currSlice->partArr[1].bitstream;
currStream->ei_flag = 0;
currStream->frame_bitoffset = currStream->read_len = 0;
memcpy (currStream->streamBuffer, &nalu->buf[1], nalu->len-1);
currStream->code_len = currStream->bitstream_length = RBSPtoSODB(currStream->streamBuffer, nalu->len-1);
slice_id_b = ue_v("NALU:SLICE_B slice_idr", currStream);
if (active_pps->redundant_pic_cnt_present_flag)
redundant_pic_cnt_b = ue_v("NALU:SLICE_B redudand_pic_cnt", currStream);
else
redundant_pic_cnt_b = 0;
/* LC: Initializing CABAC for the current data stream. */
if (active_pps->entropy_coding_mode_flag)
{
int ByteStartPosition = currStream->frame_bitoffset/8;
if (currStream->frame_bitoffset % 8 != 0)
ByteStartPosition++;
arideco_start_decoding (&currSlice->partArr[1].de_cabac, currStream->streamBuffer,
ByteStartPosition, &currStream->read_len, img->type);
}
/* LC: resilience code to be inserted */
/* FreeNALU(nalu); */
/* return current_header; */
break;
case NALU_TYPE_DPC:
/* LC: inserting the code related to DP processing */
currStream = currSlice->partArr[2].bitstream;
currStream->ei_flag = 0;
currStream->frame_bitoffset = currStream->read_len = 0;
memcpy (currStream->streamBuffer, &nalu->buf[1], nalu->len-1);
currStream->code_len = currStream->bitstream_length = RBSPtoSODB(currStream->streamBuffer, nalu->len-1);
slice_id_c = ue_v("NALU:SLICE_C slice_idr", currStream);
if (active_pps->redundant_pic_cnt_present_flag)
redundant_pic_cnt_c = ue_v("NALU:SLICE_C redudand_pic_cnt", currStream);
else
redundant_pic_cnt_c = 0;
/* LC: Initializing CABAC for the current data stream. */
if (active_pps->entropy_coding_mode_flag)
{
int ByteStartPosition = currStream->frame_bitoffset/8;
if (currStream->frame_bitoffset % 8 != 0)
ByteStartPosition++;
arideco_start_decoding (&currSlice->partArr[2].de_cabac, currStream->streamBuffer,
ByteStartPosition, &currStream->read_len, img->type);
}
/* LC: resilience code to be inserted */
FreeNALU(nalu);
return current_header;
break;
case NALU_TYPE_SEI:
printf ("read_new_slice: Found NALU_TYPE_SEI, len %d\n", nalu->len);
InterpretSEIMessage(nalu->buf,nalu->len,img);
break;
case NALU_TYPE_PPS:
ProcessPPS(nalu);
break;
case NALU_TYPE_SPS:
ProcessSPS(nalu);
break;
case NALU_TYPE_AUD:
// printf ("read_new_slice: Found 'Access Unit Delimiter' NAL unit, len %d, ignored\n", nalu->len);
break;
case NALU_TYPE_EOSEQ:
// printf ("read_new_slice: Found 'End of Sequence' NAL unit, len %d, ignored\n", nalu->len);
break;
case NALU_TYPE_EOSTREAM:
// printf ("read_new_slice: Found 'End of Stream' NAL unit, len %d, ignored\n", nalu->len);
break;
case NALU_TYPE_FILL:
printf ("read_new_slice: Found NALU_TYPE_FILL, len %d\n", nalu->len);
printf ("Skipping these filling bits, proceeding w/ next NALU\n");
break;
default:
printf ("Found NALU type %d, len %d undefined, ignore NALU, moving on\n", nalu->nal_unit_type, nalu->len);
}
}
FreeNALU(nalu);
return current_header;
}
/*!
************************************************************************
* \brief
* Initializes the parameters for a new picture
************************************************************************
*/
void init_picture(struct img_par *img, struct inp_par *inp)
{
int i,k,l;
Slice *currSlice = img->currentSlice;
if (dec_picture)
{
// this may only happen on slice loss
exit_picture();
}
if (img->frame_num != img->pre_frame_num && img->frame_num != (img->pre_frame_num + 1) % img->MaxFrameNum)
{
if (active_sps->gaps_in_frame_num_value_allowed_flag == 0)
{
// picture error concealment
if(inp->conceal_mode !=0)
{
if((img->frame_num) < (img->pre_frame_num))
{
/* Conceal lost IDR frames and any frames immediately
following the IDR. Use frame copy for these since
lists cannot be formed correctly for motion copy*/
img->conceal_mode = 1;
img->IDR_concealment_flag = 1;
conceal_lost_frames(img);
//reset to original concealment mode for future drops
img->conceal_mode = inp->conceal_mode;
}
else
{
//reset to original concealment mode for future drops
img->conceal_mode = inp->conceal_mode;
img->IDR_concealment_flag = 0;
conceal_lost_frames(img);
}
}
else
{ /* Advanced Error Concealment would be called here to combat unintentional loss of pictures. */
error("An unintentional loss of pictures occurs! Exit\n", 100);
}
}
if(img->conceal_mode == 0)
fill_frame_num_gap(img);
}
if(img->nal_reference_idc)
{
img->pre_frame_num = img->frame_num;
}
//img->num_dec_mb = 0;
//calculate POC
decode_poc(img);
if(img->nal_reference_idc)
img->last_ref_pic_poc = img->framepoc;
// dumppoc (img);
if (img->structure==FRAME ||img->structure==TOP_FIELD)
{
#ifdef WIN32
_ftime (&(img->tstruct_start)); // start time ms
#else
ftime (&(img->tstruct_start)); // start time ms
#endif
time( &(img->ltime_start)); // start time s
}
dec_picture = alloc_storable_picture (img->structure, img->width, img->height, img->width_cr, img->height_cr);
dec_picture->top_poc=img->toppoc;
dec_picture->bottom_poc=img->bottompoc;
dec_picture->frame_poc=img->framepoc;
dec_picture->qp=img->qp;
dec_picture->slice_qp_delta=currSlice->slice_qp_delta;
dec_picture->chroma_qp_offset[0] = active_pps->chroma_qp_index_offset;
dec_picture->chroma_qp_offset[1] = active_pps->second_chroma_qp_index_offset;
// reset all variables of the error concealment instance before decoding of every frame.
// here the third parameter should, if perfectly, be equal to the number of slices per frame.
// using little value is ok, the code will allocate more memory if the slice number is larger
ercReset(erc_errorVar, img->PicSizeInMbs, img->PicSizeInMbs, dec_picture->size_x);
erc_mvperMB = 0;
switch (img->structure )
{
case TOP_FIELD:
{
dec_picture->poc=img->toppoc;
img->number *= 2;
break;
}
case BOTTOM_FIELD:
{
dec_picture->poc=img->bottompoc;
img->number++;
break;
}
case FRAME:
{
dec_picture->poc=img->framepoc;
break;
}
default:
error("img->structure not initialized", 235);
}
img->current_slice_nr=0;
if (img->type > SI_SLICE)
{
set_ec_flag(SE_PTYPE);
img->type = P_SLICE; // concealed element
}
// CAVLC init
for (i=0;i < (int)img->PicSizeInMbs; i++)
for (k=0;k<4;k++)
for (l=0;l<(4 + img->num_blk8x8_uv);l++)
img->nz_coeff[i][k][l]=-1; // CAVLC
if(active_pps->constrained_intra_pred_flag)
{
for (i=0; i<(int)img->PicSizeInMbs; i++)
{
img->intra_block[i] = 1;
}
}
// Set the slice_nr member of each MB to -1, to ensure correct when packet loss occurs
// TO set Macroblock Map (mark all MBs as 'have to be concealed')
for(i=0; i<(int)img->PicSizeInMbs; i++)
{
img->mb_data[i].slice_nr = -1;
img->mb_data[i].ei_flag = 1;
}
img->mb_y = img->mb_x = 0;
img->block_y = img->pix_y = img->pix_c_y = 0; // define vertical positions
img->block_x = img->pix_x = img->pix_c_x = 0; // define horizontal positions
dec_picture->slice_type = img->type;
dec_picture->used_for_reference = (img->nal_reference_idc != 0);
dec_picture->idr_flag = img->idr_flag;
dec_picture->no_output_of_prior_pics_flag = img->no_output_of_prior_pics_flag;
dec_picture->long_term_reference_flag = img->long_term_reference_flag;
dec_picture->adaptive_ref_pic_buffering_flag = img->adaptive_ref_pic_buffering_flag;
dec_picture->dec_ref_pic_marking_buffer = img->dec_ref_pic_marking_buffer;
img->dec_ref_pic_marking_buffer = NULL;
dec_picture->MbaffFrameFlag = img->MbaffFrameFlag;
dec_picture->PicWidthInMbs = img->PicWidthInMbs;
dec_picture->pic_num = img->frame_num;
dec_picture->frame_num = img->frame_num;
dec_picture->coded_frame = (img->structure==FRAME);
dec_picture->chroma_format_idc = active_sps->chroma_format_idc;
dec_picture->frame_mbs_only_flag = active_sps->frame_mbs_only_flag;
dec_picture->frame_cropping_flag = active_sps->frame_cropping_flag;
if (dec_picture->frame_cropping_flag)
{
dec_picture->frame_cropping_rect_left_offset = active_sps->frame_cropping_rect_left_offset;
dec_picture->frame_cropping_rect_right_offset = active_sps->frame_cropping_rect_right_offset;
dec_picture->frame_cropping_rect_top_offset = active_sps->frame_cropping_rect_top_offset;
dec_picture->frame_cropping_rect_bottom_offset = active_sps->frame_cropping_rect_bottom_offset;
}
}
/*!
************************************************************************
* \brief
* finish decoding of a picture, conceal errors and store it
* into the DPB
************************************************************************
*/
void exit_picture()
{
char yuv_types[4][6]= {"4:0:0","4:2:0","4:2:2","4:4:4"};
int ercStartMB;
int ercSegment;
frame recfr;
unsigned int i;
int structure, frame_poc, slice_type, refpic, qp, pic_num, chroma_format_idc;
int tmp_time; // time used by decoding the last frame
char yuvFormat[10];
// return if the last picture has already been finished
if (dec_picture==NULL)
{
return;
}
//deblocking for frame or field
DeblockPicture( img, dec_picture );
if (dec_picture->MbaffFrameFlag)
MbAffPostProc();
recfr.yptr = &dec_picture->imgY[0][0];
if (dec_picture->chroma_format_idc != YUV400)
{
recfr.uptr = &dec_picture->imgUV[0][0][0];
recfr.vptr = &dec_picture->imgUV[1][0][0];
}
//! this is always true at the beginning of a picture
ercStartMB = 0;
ercSegment = 0;
//! mark the start of the first segment
if (!dec_picture->MbaffFrameFlag)
{
ercStartSegment(0, ercSegment, 0 , erc_errorVar);
//! generate the segments according to the macroblock map
for(i = 1; i<dec_picture->PicSizeInMbs; i++)
{
if(img->mb_data[i].ei_flag != img->mb_data[i-1].ei_flag)
{
ercStopSegment(i-1, ercSegment, 0, erc_errorVar); //! stop current segment
//! mark current segment as lost or OK
if(img->mb_data[i-1].ei_flag)
ercMarkCurrSegmentLost(dec_picture->size_x, erc_errorVar);
else
ercMarkCurrSegmentOK(dec_picture->size_x, erc_errorVar);
ercSegment++; //! next segment
ercStartSegment(i, ercSegment, 0 , erc_errorVar); //! start new segment
ercStartMB = i;//! save start MB for this segment
}
}
//! mark end of the last segment
ercStopSegment(dec_picture->PicSizeInMbs-1, ercSegment, 0, erc_errorVar);
if(img->mb_data[i-1].ei_flag)
ercMarkCurrSegmentLost(dec_picture->size_x, erc_errorVar);
else
ercMarkCurrSegmentOK(dec_picture->size_x, erc_errorVar);
//! call the right error concealment function depending on the frame type.
erc_mvperMB /= dec_picture->PicSizeInMbs;
erc_img = img;
if(dec_picture->slice_type == I_SLICE || dec_picture->slice_type == SI_SLICE) // I-frame
ercConcealIntraFrame(&recfr, dec_picture->size_x, dec_picture->size_y, erc_errorVar);
else
ercConcealInterFrame(&recfr, erc_object_list, dec_picture->size_x, dec_picture->size_y, erc_errorVar, dec_picture->chroma_format_idc);
}
if (img->structure == FRAME) // buffer mgt. for frame mode
frame_postprocessing(img, input);
else
field_postprocessing(img, input); // reset all interlaced variables
structure = dec_picture->structure;
slice_type = dec_picture->slice_type;
frame_poc = dec_picture->frame_poc;
refpic = dec_picture->used_for_reference;
qp = dec_picture->qp;
pic_num = dec_picture->pic_num;
chroma_format_idc= dec_picture->chroma_format_idc;
store_picture_in_dpb(dec_picture);
dec_picture=NULL;
if (img->last_has_mmco_5)
{
img->pre_frame_num = 0;
}
if ((structure==FRAME)||structure==BOTTOM_FIELD)
{
#ifdef WIN32
_ftime (&(img->tstruct_end)); // start time ms
#else
ftime (&(img->tstruct_end)); // start time ms
#endif
time( &(img->ltime_end)); // start time s
tmp_time=(img->ltime_end*1000+img->tstruct_end.millitm) - (img->ltime_start*1000+img->tstruct_start.millitm);
tot_time=tot_time + tmp_time;
sprintf(yuvFormat,"%s", yuv_types[chroma_format_idc]);
if(slice_type == I_SLICE) // I picture
fprintf(stdout,"%04d(I) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
frame_no, frame_poc, pic_num, qp, snr->snr_y, snr->snr_u, snr->snr_v, yuvFormat, tmp_time);
else if(slice_type == P_SLICE) // P pictures
fprintf(stdout,"%04d(P) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
frame_no, frame_poc, pic_num, qp, snr->snr_y, snr->snr_u, snr->snr_v, yuvFormat, tmp_time);
else if(slice_type == SP_SLICE) // SP pictures
fprintf(stdout,"%04d(SP) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
frame_no, frame_poc, pic_num, qp, snr->snr_y, snr->snr_u, snr->snr_v, yuvFormat, tmp_time);
else if (slice_type == SI_SLICE)
fprintf(stdout,"%04d(SI) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
frame_no, frame_poc, pic_num, qp, snr->snr_y, snr->snr_u, snr->snr_v, yuvFormat, tmp_time);
else if(refpic) // stored B pictures
fprintf(stdout,"%04d(RB) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
frame_no, frame_poc, pic_num, qp, snr->snr_y, snr->snr_u, snr->snr_v, yuvFormat, tmp_time);
else // B pictures
fprintf(stdout,"%04d(B) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
frame_no, frame_poc, pic_num, qp, snr->snr_y, snr->snr_u, snr->snr_v, yuvFormat, tmp_time);
fflush(stdout);
if(slice_type == I_SLICE || slice_type == SI_SLICE || slice_type == P_SLICE || refpic) // I or P pictures
img->number++;
else
Bframe_ctr++; // B pictures
snr->frame_ctr++;
g_nFrame++;
}
img->current_mb_nr = -4712; // impossible value for debugging, StW
img->current_slice_nr = 0;
}
/*!
************************************************************************
* \brief
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -