www.pudn.com > wm2.5.zip > header.c


 /* 
*********************************************************************** 
* COPYRIGHT AND WARRANTY INFORMATION 
* 
* Copyright 2004, Advanced Audio Video Coding Standard, Part II 
* 
* DISCLAIMER OF WARRANTY 
* 
* These software programs are available to the users without any 
* license fee or royalty on an "as is" basis. The AVS disclaims 
* any and all warranties, whether express, implied, or statutory, 
* including any implied warranties of merchantability or of fitness 
* for a particular purpose. In no event shall the contributors or  
* the AVS be liable for any incidental, punitive, or consequential 
* damages of any kind whatsoever arising from the use of this program. 
* 
* This disclaimer of warranty extends to the user of this program 
* and user's customers, employees, agents, transferees, successors, 
* and assigns. 
* 
* The AVS does not represent or warrant that the program furnished 
* hereunder are free of infringement of any third-party patents. 
* Commercial implementations of AVS, including shareware, may be 
* subject to royalty fees to patent holders. Information regarding 
* the AVS patent policy is available from the AVS Web site at 
* http://www.avs.org.cn 
* 
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY. 
************************************************************************ 
*/   
 
/* 
************************************************************************************* 
* File name: header.c 
* Function:  
* 
************************************************************************************* 
*/ 
 
 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
#include "memalloc.h" 
#include "global.h" 
#include "elements.h" 
#include "defines.h" 
#include "vlc.h" 
#include "header.h" 
#include "bitstream.h" 
#include "nalu.h"//WJP FOR NAL 
#include "parset.h" 
#include "nalu.h" 
extern FILE *bits; 
 
void NALU2RBSP(NALU_t *nalu,unsigned char *Buf,int *Buf_cur_pos) 
{ 
	memcpy (Buf + *Buf_cur_pos, nalu->buf+1, nalu->len-1);//WJP FOR NAL	 
	*Buf_cur_pos += (nalu->len-1); 
} 
 
void pic_header(unsigned char *Buf, int Buf_cur_pos) 
{ 
	picture_header *p; 
	p = AllocPicHeader(); 
	picture_header_rbsp = p; 
	 
	memcpy (currStream->streamBuffer, Buf, Buf_cur_pos); 
	currStream->code_len = currStream->bitstream_length = Buf_cur_pos; 
	currStream->read_len = currStream->frame_bitoffset = 0; 
 
	//image_code_type = u_v  (8, "img code type"  ); 
 
	 
	//if(image_code_type==0xb3) 
		//I_Picture_Header(); 
	//	pgImage->type = I_IMG; 
	//else if(image_code_type==0xb6) 
		//P_Picture_Header(); 
	//	pgImage->type = P_IMG; 
 
	//cbzhu 041221 
	picture_header_rbsp->picture_coding_type = u_v(2 , "pic coding type"); 
	if(picture_header_rbsp->picture_coding_type == 0) 
		pgImage->type = I_IMG; 
	else 
		pgImage->type = P_IMG; 
	 
	picture_header_rbsp->picture_distance = u_v(8 , "picture distance"); 
	picture_header_rbsp->pic_parameter_set_id = ue_v("pic parameter set id"); 
  //if(nalu->nal_unit_type == NALU_TYPE_IDR) //2 
	  picture_header_rbsp->picture_distance_gap_minus1 = ue_v("pic_gap_minus1"); 
	picture_header_rbsp->frame_num = u_v (5 , "frame num"); 
	picture_header_rbsp->picture_qp = u_v(6 , "pic qp"); 
	 
	if(loop_filter_parameter_flag) 
	{ 
		picture_header_rbsp->alpha_ci_offset=se_v("alpha_ci_offset"); 
		picture_header_rbsp->cp_offset=se_v("cp_offset"); 
		picture_header_rbsp->loopfilter_qp_offset=se_v("qp offset"); 
	} 
	if(pps->sliding_window_size_flag) 
	{ 
		picture_header_rbsp->sliding_window_size_minus1=u_v(1 , "sliding window size minus 1"); 
	} 
 
	 
	picture_qp      =   picture_header_rbsp->picture_qp; 
	pgImage->qp     =	picture_header_rbsp->picture_qp; 
	pgImage->half_pixel_mv_flag = pps->half_pixel_mv_flag; 
	pgImage->half_pixel_mv_enable_flag = pgImage->half_pixel_mv_flag; 
	 
	if(loop_filter_parameter_flag) 
	{ 
		alpha_ci_offset = picture_header_rbsp->alpha_ci_offset; 
		cp_offset = picture_header_rbsp->cp_offset; 
		loopfilter_qp_offset = picture_header_rbsp->loopfilter_qp_offset; 
	} 
 
	if(nb_references == 1) 
		picture_reference_flag = 1; 
	else if(nb_references>1) 
		picture_reference_flag = 0; 
	 
	//printf("%d  %d %d\n" , picture_reference_flag , picture_header_rbsp->frame_num , pgImage->number); 
	FreePicHeader(picture_header_rbsp); 
} 
/* 
************************************************************************* 
* Function:Reads new slice from bit_stream 
* Input: 
* Output: 
* Return:  
* Attention:modified by WJP FOR NAL 
************************************************************************* 
*/ 
int Header() 
{ 
	byte *Buf; 
	int Buf_cur_pos=0; 
	 
	int next_nalu_type; 
	int i,slice_num=0; 
 
	Slice_Enable=1; 
 
	for(i=0;i<176;i++) 
	{ 
		Position_of_Slice_in_Picbuff[i]=0; 
	}	 
 
	nalu = AllocNALU(MAX_CODED_FRAME_SIZE);//pgImage->width * pgImage->height * 4 
	 
  if ((Buf = (char*)calloc (MAX_CODED_FRAME_SIZE , sizeof(byte))) == NULL)  
		no_mem_exit("GetAnnexbNALU: Buf"); 
 
	while (1) 
	{ 
		GetOneUnit(nalu,&next_nalu_type); 
		switch(nalu->nal_unit_type) 
		{ 
		case NALU_TYPE_SPS: 
			SequenceParameterSet(nalu); 
			break; 
		case NALU_TYPE_PPS: 
			PictureParameterSet(nalu); 
			break; 
		case NALU_TYPE_P_HEADER: 
			p_pic_type = !nalu->nal_reference_idc; 
			NALU2RBSP(nalu,Buf,&Buf_cur_pos); 
			break; 
		case NALU_TYPE_SLICE: 
			Position_of_Slice_in_Picbuff[slice_num++]=Buf_cur_pos; 
			NALU2RBSP(nalu,Buf,&Buf_cur_pos); 
 
			if(next_nalu_type==NALU_TYPE_SLICE) 
				break; 
			else 
			{ 
				pic_header(Buf,Buf_cur_pos); 
				FreeNALU(nalu); 
				free(Buf); 
				if(next_nalu_type==-1)//end of bitstream 
					return LPOS; 
				else 
					return SOP; 
			} 
		case NALU_TYPE_IDR: 
			Position_of_Slice_in_Picbuff[slice_num++]=Buf_cur_pos; 
			NALU2RBSP(nalu,Buf,&Buf_cur_pos); 
			if(next_nalu_type==NALU_TYPE_IDR) 
				break; 
			else 
			{ 
				pic_header(Buf,Buf_cur_pos); 
				FreeNALU(nalu); 
				free(Buf); 
				if(next_nalu_type==-1)//end of bitstream 
					return LPOS; 
				else 
					return SOP; 
			} 
		case NALU_TYPE_SEI: 
			// 
			break; 
		default: 
			printf("Can't find start code\n"); 
			FreeNALU(nalu); 
			free(Buf); 
			return EOS; 
		} 
	} 
} 
 
/* 
************************************************************************* 
* Function:sequence header   
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void SequenceParameterSet (NALU_t *nalu) 
{ 
	seq_parameter_set *p = NULL; 
	p = AllocSPS(); 
	sps = p; 
	 
	memcpy (currStream->streamBuffer, nalu->buf+1, nalu->len-1);//WJP FOR NAL 
	currStream->code_len = currStream->bitstream_length = nalu->len-1;//WJP FOR NAL 
	currStream->read_len = currStream->frame_bitoffset = 0;//WJP FOR NAL 
 
	 
	sps->profile_idc             = u_v(8, "profile_idc"); 
	sps->level_idc               = u_v(8, "level_idc"); 
	sps->seq_parameter_set_id    = ue_v("sps_id"); 
	sps->delta_time_picture_distance_1 = u_v(16 , "delta_time_picture_distance_1"); 
	sps->num_ref_frames          = ue_v("num_ref_frames"); 
	sps->horizontal_size_minus1  = ue_v("horizontal_size_minus1"); 
	sps->vertical_size_minus1    = ue_v("vertical_size_minus1"); 
	sps->aspect_ratio            = u_v(4, "aspect_ratio"); 
	sps->frame_cropping_flag     = u_v(1, "frame_cropping_flag"); 
	 
	if(sps->frame_cropping_flag ) 
	{ 
		sps->frame_cropping_left_offset = ue_v("frame_cropping_left_offset"); 
		sps->frame_crop_right_offset = ue_v("frame_cropping_right_offset"); 
		sps->frame_crop_top_offset  = ue_v("frame_cropping_top_offset"); 
		sps->frame_crop_bottom_offset= ue_v("frame_cropping_bottom_offset"); 
	} 
	 
	//sps->delta_time_picture_distance_1 = u_v(16 , "delta_time_picture_distance_1"); 
	sps->hrd_parameters_present_flag =u_v(1,"hrd_par_present_flag"); 
	 
	//cbzhu 0412 
	//added for the time being  
	//for encoder-decoder matching even if not included in fcd :( 
//#ifdef _ISOLATED_REGION_  
//	iREGenable = u_v(1,"iREGenable"); 
//	iREGrate   = u_v(8,"iREGrate"); 
//#endif //_ISOLATED_REGION_ 
 
	horizontal_size         = (sps->horizontal_size_minus1+1)*MB_BLOCK_SIZE; 
	vertical_size           = (sps->vertical_size_minus1+1)*MB_BLOCK_SIZE; 
	pgImage->width          = horizontal_size; 
	pgImage->height         = vertical_size; 
	pgImage->width_cr       = (pgImage->width>>1); 
	pgImage->height_cr      = (pgImage->height>>1); 
	pgImage->PicWidthInMbs  = pgImage->width/MB_BLOCK_SIZE; 
	pgImage->PicHeightInMbs = pgImage->height/MB_BLOCK_SIZE; 
	pgImage->PicSizeInMbs   = pgImage->PicWidthInMbs * pgImage->PicHeightInMbs; 
	pgImage->max_mb_nr      = (pgImage->width * pgImage->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE); 
	 
} 
 
//cbzhu 041221 
void PictureParameterSet(NALU_t *nalu) 
{ 
	pic_parameter_set *p = NULL; 
	p = AllocPPS(); 
	pps = p; 
 
	memcpy (currStream->streamBuffer, nalu->buf+1, nalu->len-1);//WJP FOR NAL 
	currStream->code_len = currStream->bitstream_length = nalu->len-1;//WJP FOR NAL 
	currStream->read_len = currStream->frame_bitoffset = 0;//WJP FOR NAL 
 
	pps->pic_parameter_set_id                  = ue_v("pps_id"); 
	pps->pic_parameter_set_id                  = ue_v("sps_id"); 
	pps->fixed_picture_qp_flag                 = u_v(1,"fixed pic qp flag"); 
	pps->picture_reference_flag  = u_v(1,"pic reference flag"); 
	if(pps->picture_reference_flag==0) 
	{ 
		pps->sliding_window_size_flag = u_v(1,"sliding window size flag"); 
	} 
	pps->skip_mode_flag          = u_v(1,"skip mode flag"); 
	pps->loop_filter_disable_flag= u_v(1,"loop filter disable flag"); 
	if(pps->loop_filter_disable_flag==0) 
	{ 
		pps->loop_filter_parameter_flag = u_v(1,"loop filter parameter flag"); 
	} 
	pps->constrained_intra_pred_flag = u_v(1,"constrained intra prediction flag"); 
	pps->half_pixel_mv_flag = u_v(1,"half_pix_mv_flag"); 
 
	pgImage->half_pixel_mv_enable_flag = pps->half_pixel_mv_flag; 
	pgImage->half_pixel_mv_flag = pps->half_pixel_mv_flag; 
	fixed_picture_qp = pps->fixed_picture_qp_flag; 
	loop_filter_disable = pps->loop_filter_disable_flag; 
	loop_filter_parameter_flag = pps->loop_filter_parameter_flag; 
} 
/* 
************************************************************************* 
* Function:decode extension and user data 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void extension_data(char *buf,int startcodepos, int length) 
{ 
  int ext_ID; 
	 
  memcpy (currStream->streamBuffer, buf, length); 
  currStream->code_len = currStream->bitstream_length = length; 
  currStream->read_len = currStream->frame_bitoffset = (startcodepos+1)*8; 
	 
	ext_ID = u_v(4,"extension ID"); 
	 
} 
 
/* 
************************************************************************* 
* Function:user data //sw 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void user_data(char *buf,int startcodepos, int length) 
{ 
  int user_data; 
  int i; 
 
  memcpy (currStream->streamBuffer, buf, length); 
 
  currStream->code_len = currStream->bitstream_length = length; 
  currStream->read_len = currStream->frame_bitoffset = (startcodepos+1)*8; 
   
  for(i=0; i0) 
    return(x); 
  else  
    return(-x); 
}