⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 header.c

📁 avs-s最新代码,包括编码器和解码器源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*****************************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2003, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*                     
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
* The AVS Working Group doesn't represent or warrant that the programs
* furnished here under 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 for standardization procedure is available at 
* AVS Web site http://www.avs.org.cn. Patent Licensing is outside
* of AVS Working Group.
*
* The Original Code is Reference Software for China National Standard 
* GB/T 20090.2-2006 (short for AVS-P2 or AVS Video) at version RM52J.
*
* The Initial Developer of the Original Code is Video subgroup of AVS
* Workinggroup (Audio and Video coding Standard Working Group of China).
* Contributors:   Guoping Li,    Siwei Ma,    Jian Lou,    Qiang Wang , 
*   Jianwen Chen,Haiwu Zhao,  Xiaozhen Zheng, Junhao Zheng, Zhiming Wang
* 
******************************************************************************
*/



/*
*************************************************************************************
* File name: header.c
* Function: AVS Slice headers
*
*************************************************************************************
*/


#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "memalloc.h"
#include "global.h"
#include "elements.h"
#include "defines.h"
#include "vlc.h"
#include "header.h"
//#ifdef CRPS
#include "math.h"
//#endif
CameraParamters CameraParameter, *camera=&CameraParameter;
RegionParamters RegionParameter, *region = &RegionParameter;

/*
*************************************************************************
* 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;
	
	//u_v(8, "stuffing bit"); //commented by cjw AVS Zhuhai 20070122
	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"  );

	if (frame_rate_code==15)	
	{
		u_v  (18, "bit_rate_lower"  );
		u_v  (1, "marker bit"  );
		u_v  (12, "bit_rate_upper"  );
		low_delay                   = u_v  (1, "low_delay"  );
		u_v  (1, "marker bit"  );
		u_v(18,"bbv buffer size");
	} 
	else
	{
		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");
	}
	
	//兼容AVS-P2 WANGJP START
	if (profile_id == _S_PROFILE_ID)
	{
		background_predictive = 0x01 ^ u_v(1,"background predictive flag");
		core_frame_enable_flag =0x01 ^ u_v(1,"coreframe_flag");
		if(core_frame_enable_flag)
		{
			log2_core_frame_buffer_size = u_v(2, "log2_core_frame_buffer_size-1")+1;
		}
		core_frame_buffer_size = (unsigned int )( pow(2, log2_core_frame_buffer_size) );
		slice_set_enable = 0x01 ^ u_v(1, "slice set flag");  //added by mz, 2008.04
	}
	else
		u_v  (3,"reseved bits");
	//WANGJP END

	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);
  
}


void video_edit_code_data(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);
	vec_flag = 1;
}
/*
*************************************************************************
* 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");
	
	marker_bit            =u_v(1,"marker_bit");
	picture_distance         = u_v(8,"picture_distance");
	
	//xyji 12.23 
	if(low_delay)
	{
		bbv_check_times = ue_v("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(progressive_frame==0)  //according to m1658 by oliver 
	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");
		}
	}

	//兼容AVS-P2 WANGJP START
	if (profile_id == _S_PROFILE_ID)
	{
		if(background_predictive)
		{
			picture_coding_type = u_v(1,"picture coding type");
			if(picture_coding_type){
				img->typeb = BACKGROUND_IMG;
				background_output_flag = u_v(1,"picture output flag");
			}
			else
				img->typeb = 0;
		}

		//核心帧
		if(core_frame_enable_flag)
		{
			cur_core_frame_flag = u_v(1, "Is_coreframe_flag");
			if(cur_core_frame_flag)
			{
				core_frame_num = u_v(log2_core_frame_buffer_size, "Coreframe_num");
				core_frame_ref_num = 0;
			}
		}
		else
		{
			cur_core_frame_flag = 0;
			core_frame_ref_num  = 0;
		}
	}
	//WANGJP END
	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)
{
	static int frames = 0;	//核心帧技术
	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");

#ifdef BACKGROUND
	if(background_predictive && picture_coding_type == 3){
		img->typeb = BP_IMG;
		picture_coding_type = 1;
	}
	else
		img->typeb = 0;
#endif
	
	//兼容AVS-P2 WANGJP START
	if (profile_id == _S_PROFILE_ID)
	{
		//核心帧
		if(picture_coding_type==1 && core_frame_enable_flag)
		{
			cur_core_frame_flag =u_v(1, "Is_coreframe_flag");
			if(cur_core_frame_flag)
			{
				//printf("core frame's img->pic_distance:%d\n",img->pic_distance);
				core_frame_num =u_v(log2_core_frame_buffer_size, "Coreframe_num");
				core_frame_ref_num =u_v(log2_core_frame_buffer_size, "Coreframe_ref_num");			
			}
		}
		else
		{
			cur_core_frame_flag = 0;
		}
	}
	//WANGJP END

	picture_distance         = u_v(8,"picture_distance");
	
	//xyji 12.23
	if(low_delay)
	{
		bbv_check_times = ue_v("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");

    if(!(((picture_coding_type == 2 ||img->typeb == BP_IMG) && img->picture_structure==1)||(cur_core_frame_flag&&img->picture_structure==1)))
	{
		picture_reference_flag = u_v(1,"picture_reference_flag");
	}
	else
	{
		picture_reference_flag = 1;
	}

	u_v(1, "no_forward_reference_flag");	// Added by cjw, 20070327
//	u_v(4,"reserved bits");					// Commented by cjw, 20070327
	u_v(3,"reserved bits");					// Added by cjw, 20070327
	
	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;
        vec_flag = 0;       // M1956 by Grandview 2006.12.12
    } 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 PICTURE_DISPLAY_EXTENSION_ID:
		picture_display_extension();
	case CAMERAPARAMETERS_EXTENSION_ID: 
		cameraparameters_extension();
		break;
	case REGION_EXTENSION_ID:
		region_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: decode picture display extension
* Input:
* Output:
* Return: 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -