www.pudn.com > rm52c.rar > header.c


 /* 
*********************************************************************** 
* COPYRIGHT AND WARRANTY INFORMATION 
* 
* Copyright 2003, 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: H.26L Slice headers 
* 
************************************************************************************* 
*/ 
 
 
#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" 
CameraParamters CameraParameter, *camera=&CameraParameter; 
/* 
************************************************************************* 
* Function:sequence header   
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void SequenceHeader (char *buf,int startcodepos, int length) 
{ 
 
	memcpy (currStream->streamBuffer, buf, length); 
	currStream->code_len = currStream->bitstream_length = length; 
	currStream->read_len = currStream->frame_bitoffset = (startcodepos+1)*8; 
	 
	profile_id                  = u_v  (8, "profile_id"  ); 
	level_id                    = u_v  (8, "level_id"    ); 
	progressive_sequence        = u_v  (1, "progressive_sequence"  ); 
	horizontal_size             = u_v  (14, "horizontal_size"  ); 
	vertical_size               = u_v  (14, "vertical_size"  ); 
	chroma_format               = u_v  (2, "chroma_format"  ); 
	sample_precision            = u_v  (3, "sample_precision"  ); 
	aspect_ratio_information    = u_v  (4, "aspect_ratio_information"  ); 
	frame_rate_code             = u_v  (4, "frame_rate_code"  ); 
	//bit_rate                    = u_v  (30, "bit_rate_lower"  ); 
	bit_rate_lower              = u_v  (18, "bit_rate_lower"  ); 
	u_v  (1, "marker bit"  ); 
	bit_rate_upper              = u_v  (12, "bit_rate_upper"  ); 
	low_delay                   = u_v  (1, "low_delay"  ); 
	u_v  (1, "marker bit"  ); 
	bbv_buffer_size = u_v(18,"bbv buffer size"); 
	//slice_enable = u_v(1,"slice enable"); 
//	slice_row_nr = u_v(8,"slice row nr"); 
 
	u_v  (3,"reseved bits"  ); 
 
	 
	img->width          = horizontal_size; 
	img->height         = vertical_size; 
	img->width_cr       = (img->width>>1); 
	img->height_cr      = (img->height>>1); 
	img->PicWidthInMbs  = img->width/MB_BLOCK_SIZE; 
	img->PicHeightInMbs = img->height/MB_BLOCK_SIZE; 
	img->PicSizeInMbs   = img->PicWidthInMbs * img->PicHeightInMbs; 
	img->buf_cycle      = input->buf_cycle+1; 
	img->max_mb_nr      =(img->width * img->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE); 
   
} 
/* 
************************************************************************* 
* Function:I picture header  //sw 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void I_Picture_Header(char *buf,int startcodepos, int length) 
{ 
	currStream->frame_bitoffset = currStream->read_len = (startcodepos+1)*8; 
	currStream->code_len = currStream->bitstream_length = length; 
	memcpy (currStream->streamBuffer, buf, length); 
	 
	bbv_delay = u_v(16,"bbv delay"); 
	 
	time_code_flag       = u_v(1,"time_code_flag"); 
	if (time_code_flag) 
		time_code        =u_v(24,"time_code"); 
	 
	picture_distance         = u_v(8,"picture_distance"); 
	 
	//xyji 12.23 ???????????? 
	if(low_delay) 
	{ 
		bbv_check_times = ue_v(8,"bbv check times"); 
	} 
	 
	progressive_frame    = u_v(1,"progressive_frame"); 
	if (!progressive_frame) 
	{ 
		img->picture_structure   = u_v(1,"picture_structure"); 
	}else 
	{ 
		img->picture_structure = 1; 
	} 
	 
	top_field_first      = u_v(1,"top_field_first"); 
	repeat_first_field   = u_v(1,"repeat_first_field"); 
	fixed_picture_qp     = u_v(1,"fixed_picture_qp"); 
	picture_qp           = u_v(6,"picture_qp"); 
	 
	//xyji 12.23   
	if(img->picture_structure == 0) 
	{ 
		skip_mode_flag =u_v(1,"skip mode flag"); 
	} 
	 
	u_v(4,"reserved bits"); 
 
	loop_filter_disable = u_v(1,"loop_filter_disable"); 
	if (!loop_filter_disable) 
	{ 
		loop_filter_parameter_flag = u_v(1,"loop_filter_parameter_flag"); 
		if (loop_filter_parameter_flag) 
		{ 
			alpha_offset = se_v("alpha_offset"); 
			beta_offset  = se_v("beta_offset"); 
		} 
	} 
	 
	img->qp                = picture_qp; 
	img->pic_distance      = picture_distance;	 
	img->type              = I_IMG; 
   
} 
 
/* 
************************************************************************* 
* Function:pb picture header  
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
void PB_Picture_Header(char *buf,int startcodepos, int length) 
{ 
	int i; 
	 
	currStream->frame_bitoffset = currStream->read_len = (startcodepos+1)*8; 
	currStream->code_len = currStream->bitstream_length = length; 
	memcpy (currStream->streamBuffer, buf, length); 
	 
	bbv_delay = u_v(16,"bbv delay");   
	picture_coding_type       = u_v(2,"picture_coding_type"); 
	 
	picture_distance         = u_v(8,"picture_distance"); 
	 
	//xyji 12.23 ???????????? 
	if(low_delay) 
	{ 
		bbv_check_times = ue_v(8,"bbv check times"); 
	} 
	 
	progressive_frame        = u_v(1,"progressive_frame"); 
	if (!progressive_frame) 
	{ 
		img->picture_structure = u_v(1,"picture_structure"); 
		if (!img->picture_structure) 
			img->advanced_pred_mode_disable = u_v(1,"advanced_pred_mode_disable"); 
	}else 
		img->picture_structure   = 1; 
	top_field_first        = u_v(1,"top_field_first"); 
	repeat_first_field     = u_v(1,"repeat_first_field"); 
	 
	fixed_picture_qp       = u_v(1,"fixed_picture_qp"); 
	picture_qp             = u_v(6,"picture_qp"); 
	 
	//picture_coding_type       = u_v(2,"picture_coding_type"); 
	 
	if (!(picture_coding_type==2 && img->picture_structure==1)) 
	{ 
		picture_reference_flag = u_v(1,"picture_reference_flag"); 
	} 
	u_v(4,"reserved bits"); 
	 
	skip_mode_flag      = u_v(1,"skip_mode_flag"); 
	loop_filter_disable = u_v(1,"loop_filter_disable"); 
	if (!loop_filter_disable) 
	{ 
		loop_filter_parameter_flag = u_v(1,"loop_filter_parameter_flag"); 
		if (loop_filter_parameter_flag) 
		{ 
			alpha_offset = se_v("alpha_offset"); 
			beta_offset  = se_v("beta_offset"); 
		} 
	} 
	 
	img->qp                = picture_qp; 
	 
	if(picture_coding_type==1) 
		img->type = P_IMG; 
	else 
		img->type = B_IMG; 
	 
	img->pic_distance = picture_distance;    
} 
/* 
************************************************************************* 
* 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"); 
	 
	switch (ext_ID) 
	{ 
	case SEQUENCE_DISPLAY_EXTENSION_ID: 
		sequence_display_extension(); 
		break; 
	case COPYRIGHT_EXTENSION_ID: 
		copyright_extension(); 
		break; 
	case CAMERAPARAMETERS_EXTENSION_ID:  
		cameraparameters_extension(); 
		break; 
	default: 
		printf("reserved extension start code ID %d\n",ext_ID); 
		break; 
	} 
 
} 
/* 
************************************************************************* 
* Function: decode sequence display extension 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
void sequence_display_extension() 
{ 
	video_format      = u_v(3,"video format ID"); 
	video_range       = u_v(1,"video range"); 
	color_description = u_v(1,"color description"); 
	 
	if (color_description) 
	{ 
		color_primaries          = u_v(8,"color primaries"); 
		transfer_characteristics = u_v(8,"transfer characteristics"); 
		matrix_coefficients      = u_v(8,"matrix coefficients"); 
	} 
	 
	display_horizontal_size = u_v(14,"display_horizontaol_size"); 
	u_v  (1, "marker bit"  ); 
	display_vertical_size   = u_v(14,"display_vertical_size"); 
							u_v  (2, "reserved bits"  ); 
} 
 
/* 
************************************************************************* 
* 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; ipic_distance  <  img->PrevPicDistanceLsb  &&   
		( img->PrevPicDistanceLsb - img->pic_distance )  >=  ( MaxPicDistanceLsb / 2 ) ) 
		img->CurrPicDistanceMsb = img->PicDistanceMsb + MaxPicDistanceLsb; 
	else if ( img->pic_distance  >  img->PrevPicDistanceLsb  && 
		( img->pic_distance - img->PrevPicDistanceLsb )  >  ( MaxPicDistanceLsb / 2 ) ) 
		img->CurrPicDistanceMsb = img->PicDistanceMsb - MaxPicDistanceLsb; 
	else 
		img->CurrPicDistanceMsb = img->PicDistanceMsb; 
	 
	// 2nd 
  img->toppoc = img->CurrPicDistanceMsb + img->pic_distance; 
	img->bottompoc = img->toppoc + img->delta_pic_order_cnt_bottom; 
	 
	// last: some post-processing.  
	if ( img->toppoc <= img->bottompoc ) 
		img->ThisPOC = img->framepoc = img->toppoc; 
	else 
		img->ThisPOC = img->framepoc = img->bottompoc; 
	 
	//frame pix  -  use toppoc/2 
  img->tr_frm = img->ThisPOC/2; 
  img->tr = img->ThisPOC; 
	img->tr_fld = img->ThisPOC;	 
	//moved from above for stuff that still uses img->tr 
	//soon to be obsolete 
 
  if(img->type != B_IMG)  
  { 
		img->imgtr_last_prev_P = img->imgtr_last_P;//Lou 1016 
    img->imgtr_last_P = img->imgtr_next_P; 
    img->imgtr_next_P = img->tr; 
  } 
} 
 
/* 
************************************************************************* 
* Function:slice header   
* Input: 
* Output: 
* Return:  
* Attention:         // jlzheng  6.30 
************************************************************************* 
*/ 
 
void SliceHeader (char *buf,int startcodepos, int length) 
{ 
  int i; 
 
	memcpy (currStream->streamBuffer, buf, length); 
	currStream->code_len = currStream->bitstream_length = length; 
	currStream->read_len = currStream->frame_bitoffset = (startcodepos+1)*8; 
 
	//start_code_prefix                  = u_v  (24,"start code prefix"); 
	 
	 
	if(img_width > 2800) 
	slice_vertical_position_extension  = u_v  (3, "slice vertical position extension"); 
	 
    //slice_start_code                   = u_v  (8, "slice start code"); 
	if (!fixed_picture_qp) 
	{ 
	fixed_slice_qp   = u_v  (1,"fixed_slice_qp"); 
    slice_qp         = u_v  (6,"slice_qp"); 
	} 
 
  	// 2004/08 
	 
	if(img->picture_structure == 0){  // field coding 
		input->buf_cycle = input->buf_cycle * 2 ; 
	} 
	if(img->type != I_IMG){ 
		img->picture_weighting_flag = u_v(1,"picture weighting flag"); 
		if(img->picture_weighting_flag) 
		{ 
			for(i=0;ibuf_cycle;i++) 
			{ 
				img->lum_scale[i]    = u_v(8,"luma scale"); 
				img->lum_shift[i]    = u_v(8,"luma shift"); //u_i 
				img->lum_shift[i] = (img->lum_shift[i] - 127) ; 
				u_1 ("insert bit"); 
				img->chroma_scale[i] = u_v(8,"chroma scale"); 
				img->chroma_shift[i] = u_v(8,"chroma shift");//u_i 
			} 
			img->allframeweight    =u_v(1,"mb weighting flag"); 
		} 
	} 
	if(img->picture_structure == 0){  // field coding 
		input->buf_cycle = input->buf_cycle / 2 ; 
	} 
	 
	// end for  2004/08 
} 
 
/* 
************************************************************************* 
* Function:Error handling procedure. Print error message to stderr and exit 
      with supplied code. 
* Input:text 
      Error message 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void error(char *text, int code) 
{ 
  fprintf(stderr, "%s\n", text); 
  exit(code); 
} 
 
//Lou 
int sign(int a , int b) 
{ 
  int x; 
	 
  x=abs(a); 
 
  if (b>0) 
    return(x); 
  else  
    return(-x); 
 
}