#include "global.h"
#include "report.h"
#include "image.h"


#if(REPORT_STATISTIC)

static int get_position_of_smallest_poc(DecoderReportBuffer* p_Drb);


int get_position_of_smallest_poc(DecoderReportBuffer* p_Drb)
{
	int i=0;
	int min_poc=MAX_VALUE;
	int position=-1;
	DecoderReportContent**p_Drc=p_Drb->p_Drc;

	for(i=0;i<p_Drb->CurrSizeOfDrc;++i)
	{
		if(p_Drc[i]->POC<min_poc)
		{
			min_poc=p_Drc[i]->POC;
			position=i;
		}
	}
	if(position==-1)
		assert(0);

	return position;
}
void report_content(VideoParameters* p_Vid,DecoderReportContent* p_Drc)
{
	FILE* output_file=p_Vid->p_Drf;
	static int first_report=1;
	int num_of_views=p_Vid->num_of_views;
	int i=0;
	int BitUsedByTexture=0;
	int BitUsedByDepth=0;

	for(i=0;i<num_of_views;++i)
		BitUsedByTexture+=p_Drc->DecReportInfo.BitUsedForTexture[i];
	for(i=0;i<num_of_views;++i)
		BitUsedByDepth+=p_Drc->DecReportInfo.BitUsedForDepth[i];

	if(first_report)
	{
		fprintf(output_file,"poc   t_bits     ");
		for(i=0;i<num_of_views;++i)
			fprintf(output_file,"          ",i);
		fprintf(output_file,"   d_bits     ");
		for(i=0;i<num_of_views;++i)
			fprintf(output_file,"          ",i);
		fprintf(output_file,"   s_bits    \n");
		first_report=0;
	}
	fprintf(output_file,"%-6d%-10d(",(p_Drc->POC)/2,BitUsedByTexture);
	for(i=0;i<num_of_views;++i)
		fprintf(output_file,"%-10d",p_Drc->DecReportInfo.BitUsedForTexture[i]);
	fprintf(output_file,")  ");
	fprintf(output_file,"%-10d(",BitUsedByDepth);
	for(i=0;i<num_of_views;++i)
		fprintf(output_file,"%-10d",p_Drc->DecReportInfo.BitUsedForDepth[i]);
	fprintf(output_file,")  %-10d\n",p_Drc->DecReportInfo.BitUsedForSupp);

}
void add_new_report_content(VideoParameters* p_Vid,Slice* currSlice,StorablePicture* dec_picture)
{
	DecoderReportBuffer* p_Drb=p_Vid->p_Drb;
	int VOIdx=GetVOIdx(p_Vid,currSlice->view_id);


	int pps_id=currSlice->active_pps->pic_parameter_set_id;
	int sps_id=currSlice->active_sps->seq_parameter_set_id;

	int i=0;

	if(p_Vid->texture)
	{
		if(VOIdx==0)
		{
			DecoderReportContent* new_report_content=calloc(1,sizeof(DecoderReportContent));
			new_report_content->POC=currSlice->framepoc;
			//printf("\n%d\n",currSlice->framepoc);
			new_report_content->FrmNum=currSlice->frame_num;
			if(p_Drb->CurrSizeOfDrc==p_Drb->MaxSizeOfDrc)
			{
				int position=get_position_of_smallest_poc(p_Drb);
				DecoderReportContent* output_content=p_Drb->p_Drc[position];
				report_content(p_Vid,output_content);

				for(i=position+1;i<p_Drb->CurrSizeOfDrc;++i)
					p_Drb->p_Drc[i-1]=p_Drb->p_Drc[i];
				--p_Drb->CurrSizeOfDrc;
				free(output_content);
			}
			p_Drb->p_Drc[p_Drb->CurrSizeOfDrc]=new_report_content;
			++p_Drb->CurrSizeOfDrc;
			p_Vid->p_Vid1->p_Drc=p_Vid->p_Drc=new_report_content;
			++p_Drb->NumOfFrames;

		}
	}
	dec_picture->p_Drc=p_Vid->p_Drc;

	if(VOIdx==0)
	{
		if(p_Vid->texture)
		{
			p_Drb->TotalBitUsedForTexturePara[0]+=
				p_Vid->BitUsedBySPS[0][sps_id]+p_Vid->BitUsedByPPS[pps_id];
		}
		else
		{
			p_Drb->TotalBitUsedForDepthPara[0]+=
				p_Vid->BitUsedBySPS[0][sps_id]+p_Vid->BitUsedByPPS[pps_id];
		}
		p_Vid->BitUsedBySPS[0][sps_id]=p_Vid->BitUsedByPPS[pps_id]=0;
	}
	else if(VOIdx==1)
	{
		if(p_Vid->texture)
		{
			p_Drb->TotalBitUsedForTexturePara[1]+=p_Vid->BitUsedBySPS[1][sps_id];
		}
		else
		{
			p_Drb->TotalBitUsedForDepthPara[1]+=p_Vid->BitUsedBySPS[1][sps_id];
		}
		p_Vid->BitUsedBySPS[1][sps_id]=0;
	}

}

void clear_report_content(VideoParameters*p_Vid)
{
	int j=0;
	DecoderReportBuffer*p_Drb=p_Vid->p_Drb;

	while(p_Drb->CurrSizeOfDrc)
	{
		int position=get_position_of_smallest_poc(p_Drb);
		DecoderReportContent* output_content=p_Drb->p_Drc[position];
		report_content(p_Vid,output_content);
		for(j=position+1;j<p_Drb->CurrSizeOfDrc;++j)
			p_Drb->p_Drc[j-1]=p_Drb->p_Drc[j];
		--p_Drb->CurrSizeOfDrc;
		free(output_content);
	}
}
#endif

