📄 mbuffer.c
字号:
update_ref_list();
update_ltref_list();
dump_dpb();
return;
}
}
}
// this is a frame or a field which has no stored complementatry field
// sliding window, if necessary
if ((!img->idr_flag)&&(p->used_for_reference && (!img->adaptive_ref_pic_buffering_flag)))
{
sliding_window_memory_management(p);
}
// first try to remove unused frames
if (dpb.used_size==dpb.size)
{
remove_unused_frame_from_dpb();
}
// then output frames until one can be removed
while (dpb.used_size==dpb.size)
{
// non-reference frames may be output directly
if (!p->used_for_reference)
{
get_smallest_poc(&poc, &pos);
if ((-1==pos) || (p->poc < poc))
{
direct_output(p, p_out);
return;
}
}
// flush a frame
output_one_frame_from_dpb();
}
// check for duplicate frame number in short term reference buffer
if ((p->used_for_reference)&&(!p->is_long_term))
{
for (i=0; i<dpb.ref_frames_in_buffer; i++)
{
if (dpb.fs_ref[i]->frame_num == img->frame_num)
{
error("duplicate frame_num im short-term reference picture buffer", 500);
}
}
}
// store at end of buffer
// printf ("store frame/field at pos %d\n",dpb.used_size);
insert_picture_in_dpb(dpb.fs[dpb.used_size],p);
dpb.used_size++;
update_ref_list();
update_ltref_list();
dump_dpb();
}
/*!
************************************************************************
* \brief
* Insert the picture into the DPB. A free DPB position is necessary
* for frames, .
*
* \param fs
* FrameStore into which the picture will be inserted
* \param p
* StorablePicture to be inserted
*
************************************************************************
*/
static void insert_picture_in_dpb(FrameStore* fs, StorablePicture* p)
{
// printf ("insert (%s) pic with frame_num #%d, poc %d\n", (p->structure == FRAME)?"FRAME":(p->structure == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", img->frame_num, p->poc);
assert (p!=NULL);
assert (fs!=NULL);
switch (p->structure)
{
case FRAME:
fs->frame = p;
fs->is_used = 3;
if (p->used_for_reference)
{
fs->is_reference = 3;
if (p->is_long_term)
{
fs->is_long_term = 3;
}
}
// generate field views
dpb_split_field(fs);
break;
case TOP_FIELD:
fs->top_field = p;
fs->is_used |= 1;
if (p->used_for_reference)
{
fs->is_reference |= 1;
if (p->is_long_term)
{
fs->is_long_term |= 1;
fs->long_term_frame_idx = p->long_term_frame_idx;
}
}
if (fs->is_used == 3)
{
// generate frame view
dpb_combine_field(fs);
} else
{
fs->poc = p->poc;
}
break;
case BOTTOM_FIELD:
fs->bottom_field = p;
fs->is_used |= 2;
if (p->used_for_reference)
{
fs->is_reference |= 2;
if (p->is_long_term)
{
fs->is_long_term |= 2;
fs->long_term_frame_idx = p->long_term_frame_idx;
}
}
if (fs->is_used == 3)
{
// generate frame view
dpb_combine_field(fs);
} else
{
fs->poc = p->poc;
}
break;
}
fs->frame_num = img->frame_num;
fs->is_output = 0;
if (fs->is_used==3)
{
if (p_ref)
find_snr(snr, fs->frame, p_ref);
}
}
/*!
************************************************************************
* \brief
* Check if one of the frames/fields in frame store is used for reference
************************************************************************
*/
static int is_used_for_reference(FrameStore* fs)
{
if (fs->is_reference)
{
return 1;
}
if (fs->is_used==3) // frame
{
if (fs->frame->used_for_reference)
{
return 1;
}
}
if (fs->is_used&1) // top field
{
if (fs->top_field->used_for_reference)
{
return 1;
}
}
if (fs->is_used&2) // bottom field
{
if (fs->bottom_field->used_for_reference)
{
return 1;
}
}
return 0;
}
/*!
************************************************************************
* \brief
* Check if one of the frames/fields in frame store is used for short-term reference
************************************************************************
*/
static int is_short_term_reference(FrameStore* fs)
{
if (fs->is_used==3) // frame
{
if ((fs->frame->used_for_reference)&&(!fs->frame->is_long_term))
{
return 1;
}
}
if (fs->is_used&1) // top field
{
if ((fs->top_field->used_for_reference)&&(!fs->top_field->is_long_term))
{
return 1;
}
}
if (fs->is_used&2) // bottom field
{
if ((fs->bottom_field->used_for_reference)&&(!fs->top_field->is_long_term))
{
return 1;
}
}
return 0;
}
/*!
************************************************************************
* \brief
* Check if one of the frames/fields in frame store is used for short-term reference
************************************************************************
*/
static int is_long_term_reference(FrameStore* fs)
{
if (fs->is_used==3) // frame
{
if ((fs->frame->used_for_reference)&&(fs->frame->is_long_term))
{
return 1;
}
}
if (fs->is_used&1) // top field
{
if ((fs->top_field->used_for_reference)&&(fs->top_field->is_long_term))
{
return 1;
}
}
if (fs->is_used&2) // bottom field
{
if ((fs->bottom_field->used_for_reference)&&(fs->top_field->is_long_term))
{
return 1;
}
}
return 0;
}
/*!
************************************************************************
* \brief
* remove one frame from DPB
************************************************************************
*/
static void remove_frame_from_dpb(int pos)
{
FrameStore* fs = dpb.fs[pos];
FrameStore* tmp;
unsigned i;
// printf ("remove frame with frame_num #%d\n", fs->frame_num);
switch (fs->is_used)
{
case 3:
free_storable_picture(fs->frame);
free_storable_picture(fs->top_field);
free_storable_picture(fs->bottom_field);
fs->frame=NULL;
fs->top_field=NULL;
fs->bottom_field=NULL;
break;
case 2:
free_storable_picture(fs->bottom_field);
fs->bottom_field=NULL;
break;
case 1:
free_storable_picture(fs->top_field);
fs->top_field=NULL;
break;
case 0:
break;
default:
error("invalid frame store type",500);
}
fs->is_used = 0;
// move empty framestore to end of buffer
tmp = dpb.fs[pos];
for (i=pos; i<dpb.used_size-1;i++)
{
dpb.fs[i] = dpb.fs[i+1];
}
dpb.fs[dpb.used_size-1] = tmp;
dpb.used_size--;
}
/*!
************************************************************************
* \brief
* find smallest POC in the DPB.
************************************************************************
*/
static void get_smallest_poc(int *poc,int * pos)
{
unsigned i;
if (dpb.used_size<1)
{
error("Cannot determine smallest POC, DPB empty.",150);
}
*pos=-1;
*poc = INT_MAX;
for (i=0; i<dpb.used_size; i++)
{
if ((*poc>dpb.fs[i]->poc)&&(!dpb.fs[i]->is_output))
{
*poc = dpb.fs[i]->poc;
*pos=i;
}
}
}
/*!
************************************************************************
* \brief
* Remove a picture from DPB which is no longer needed.
************************************************************************
*/
static int remove_unused_frame_from_dpb()
{
unsigned i;
// check for frames that were already output and no longer used for reference
for (i=0; i<dpb.used_size; i++)
{
if (dpb.fs[i]->is_output && (!is_used_for_reference(dpb.fs[i])))
{
remove_frame_from_dpb(i);
return 1;
}
}
return 0;
}
/*!
************************************************************************
* \brief
* Output one picture stored in the DPB.
************************************************************************
*/
static void output_one_frame_from_dpb()
{
int poc, pos;
//diagnostics
if (dpb.used_size<1)
{
error("Cannot output frame, DPB empty.",150);
}
// find smallest POC
get_smallest_poc(&poc, &pos);
if(pos==-1)
{
error("no frames for output available", 150);
}
// call the output function
// printf ("output frame with frame_num #%d, poc %d (dpb. dpb.size=%d, dpb.used_size=%d)\n", dpb.fs[pos]->frame_num, dpb.fs[pos]->frame->poc, dpb.size, dpb.used_size);
write_stored_frame(dpb.fs[pos], p_out);
if (dpb.last_output_poc >= poc)
{
error ("output POC must be in ascending order", 150);
}
dpb.last_output_poc = poc;
// free frame store and move empty store to end of buffer
if (!is_used_for_reference(dpb.fs[pos]))
{
remove_frame_from_dpb(pos);
}
}
/*!
************************************************************************
* \brief
* All stored picture are output. Should be called to empty the buffer
************************************************************************
*/
void flush_dpb()
{
unsigned i;
//diagnostics
// printf("Flush remaining frames from dpb. dpb.size=%d, dpb.used_size=%d\n",dpb.size,dpb.used_size);
// mark all frames unused
for (i=0; i<dpb.used_size; i++)
{
unmark_for_reference (dpb.fs[i]);
}
while (remove_unused_frame_from_dpb()) ;
// output frames in POC order
while (dpb.used_size)
{
output_one_frame_from_dpb();
}
}
#define RSD(x) ((x&2)?(x|1):(x&(~1)))
/*!
************************************************************************
* \brief
* Extract top field from a frame
************************************************************************
*/
void dpb_split_field(FrameStore *fs)
{
int i,j;
fs->top_field = alloc_storable_picture(TOP_FIELD, fs->frame->size_x, fs->frame->size_y/2, fs->frame->size_x_cr, fs->frame->size_y_cr/2);
fs->bottom_field = alloc_storable_picture(BOTTOM_FIELD, fs->frame->size_x, fs->frame->size_y/2, fs->frame->size_x_cr, fs->frame->size_y_cr/2);
for (i=0; i<fs->frame->size_y/2; i++)
{
memcpy(fs->top_field->imgY[i], fs->frame->imgY[i*2], fs->frame->size_x);
}
for (i=0; i<fs->frame->size_y_cr/2; i++)
{
memcpy(fs->top_field->imgUV[0][i], fs->frame->imgUV[0][i*2], fs->frame->size_x_cr);
memcpy(fs->top_field->imgUV[1][i], fs->frame->imgUV[1][i*2], fs->frame->size_x_cr);
}
for (i=0; i<fs->frame->size_y/2; i++)
{
memcpy(fs->bottom_field->imgY[i], fs->frame->imgY[i*2 + 1], fs->frame->size_x);
}
for (i=0; i<fs->frame->size_y_cr/2; i++)
{
memcpy(fs->bottom_field->imgUV[0][i], fs->frame->imgUV[0][i*2 + 1],
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -