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

📄 writempegheader.c

📁 DM642的mpeg4编码
💻 C
字号:
#include "writempegheader.h"
#include "../global.h"


unsigned int WriteMpeg4VolHeader(Stream* bs, Encoder* pEncoder)
{
	static const unsigned int vo_id = 0;
	static const unsigned int vol_id = 0;

	//	int iCustomMatrixFlag;
	unsigned int iBits;

#if 0
	StreamPad(bs);
#endif

	iBits = GetStreamPosition(bs);

    /* visual_object_sequence_start_code */
    StreamPutBits(bs, visual_object_sequence_start_code, 32);

	/* profile_and_level_indication */
	StreamPutBits(bs, pEncoder->pVol->profile_and_level_indication, 8);

	StreamPad(bs);
	/* visual_object_start_code */
	StreamPutBits(bs, visual_object_start_code, 32);
	/* is_visual_object_identifier */
	StreamPutBits(bs, 0, 1);
	/* visual_object_type */
	StreamPutBits(bs, Visual_Object_Type_Video, 4);
    /* video_signal_type */
	StreamPutBit(bs, 0);

	StreamPad(bs);
	/* video_object_start_code */
	StreamPutBits(bs, video_object_start_code|(vo_id&0x5), 32);

    StreamPad(bs);
    /*  video_object_layer_start_code  */
    StreamPutBits(bs, video_object_layer_start_code|(vol_id&0x4), 32);
   
    /*  random_accessible_vol  */
    StreamPutBits(bs, pEncoder->pVol->random_accessible_vol, 1);

	/*  video_object_type_indication  */
	StreamPutBits(bs, pEncoder->pVol->video_object_type_indication, 8);

	/*  is_object_layer_identifier  */
	StreamPutBits(bs, pEncoder->pVol->is_object_layer_identifier, 1);
	if (pEncoder->pVol->is_object_layer_identifier)
	{
		StreamPutBits(bs, pEncoder->pVol->video_object_layer_verid, 4);
		StreamPutBits(bs, pEncoder->pVol->video_object_layer_priority, 3);
	}

    /*  aspect_ratio_info */
	StreamPutBits(bs, pEncoder->pVol->aspect_ratio_info, 4);
	if (pEncoder->pVol->aspect_ratio_info == Pixel_Aspect_Ratio_Extended_PAR)
	{
		StreamPutBits(bs, pEncoder->pVol->par_width, 8);
		StreamPutBits(bs, pEncoder->pVol->par_height, 8);
	}

	/* vol_control_parameters */
	StreamPutBits(bs, pEncoder->pVol->vol_control_parameters, 1);
	if (pEncoder->pVol->vol_control_parameters)
	{
		StreamPutBits(bs, pEncoder->pVol->chroma_format, 2);
		StreamPutBits(bs, pEncoder->pVol->low_delay, 1);
		
		/* vbv_parameters = 0 */
		StreamPutBits(bs, 0, 1);
	}

	/*  video_object_layer_shape = rectangular  */
    StreamPutBits(bs, 0, 2);

	WRITE_MARKER();   
	/*   vop_time_increment_resolution  */
	StreamPutBits(bs, pEncoder->pVol->vop_time_increment_resolution, 16);

	WRITE_MARKER();
    /*   fixed_vop_rate  */
	StreamPutBits(bs, pEncoder->pVol->fixed_vop_rate, 1);
	if (pEncoder->pVol->fixed_vop_rate)
	{
		StreamPutBits(bs, pEncoder->pVol->fixed_vop_time_increment, MAX(log2bin(pEncoder->pVol->vop_time_increment_resolution-1),1));
	}
    
	/*  video_object_layer_width  video_object_layer_height  */
	WRITE_MARKER();
	StreamPutBits(bs, pEncoder->pVol->video_object_layer_width, 13);
	WRITE_MARKER();
	StreamPutBits(bs, pEncoder->pVol->video_object_layer_height, 13);
	WRITE_MARKER();
    
    /*  interlaced  */
	StreamPutBits(bs, pEncoder->pVol->interlaced, 1);

	/*  obmc_disable */
	StreamPutBits(bs, pEncoder->pVol->obmc_disable, 1);

	/*  sprite_enable  */
	if (pEncoder->pVol->video_object_layer_verid != 1)
	{
		if (pEncoder->pVol->sprite_enable == Sprite_Coding_Mode_GMC)
		{
			StreamPutBits(bs, 2, 2);		/* sprite_enable=='GMC' */
			StreamPutBits(bs, 3, 6);		/* no_of_sprite_warping_points */
			StreamPutBits(bs, 3, 2);		/* sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
			StreamPutBit(bs, 0);			/* sprite_brightness_change (not supported) */
		}
        else 
		{
			StreamPutBits(bs, 0, 2);
		}
	}
	else 
	{
		StreamPutBit(bs, 0);
	}

	/* not_8_bit = 0 */
	StreamPutBit(bs, 0);

	/* quant_type */
	StreamPutBit(bs, pEncoder->pVol->quant_type);
    if (pEncoder->pVol->quant_type)
	{   
		/*  load_intra_quant_mat , use quant-matrix */
//		iCustomMatrixFlag = IsCustomIntraMatrix(pEncoder->pVol->load_intra_quant_mat);
		
//		if (iCustomMatrixFlag)
//		{
            /* write quant-matrix  */
//			StreamPutBit(bs, 1);
//			LoadMpeg4IntraMatrix(bs, pEncoder->pVol->intra_quant_matrix);
//		}
//		else 
		{
			StreamPutBit(bs, 0);
		}
        
		/*  load_inter_quant_mat  */
//		iCustomMatrixFlag = IsCustomInterMatrix(pEncoder->pVol->load_inter_quant_mat);
//		if (iCustomMatrixFlag)
//		{  
			/* write quant-matrix  */
//			StreamPutBit(bs, 1);
//			LoadMpeg4InterMatrix(bs, pEncoder->pVol->inter_quant_matrix);
//		}
//		else 
		{
			StreamPutBit(bs, 0);
		}
	}

	if (pEncoder->pVol->video_object_layer_verid != 1)
	{
		StreamPutBit(bs, pEncoder->pVol->quarter_sample);
	}

	/*  complexity_estimation_disable = 1 */
	StreamPutBit(bs, 1);
	/*  resync_marker_disable  = 1 */
	StreamPutBit(bs, 1);
	/*  data_partitioned  = 0 */
	StreamPutBit(bs, 0);

	if (pEncoder->pVol->video_object_layer_verid != 1)
	{
		/*  newpred_enable = 0  */
		StreamPutBit(bs, 0);
		/*  reduced_resolution_vop_enable */
		StreamPutBit(bs, pEncoder->pVol->reduced_resolution_vop_enable);
	}

	/*  scalability = 0  */
	StreamPutBit(bs, pEncoder->pVol->scalability);
    
	StreamPad(bs);

	return GetStreamPosition(bs) - iBits;

}

void WriteMpeg4VopHeader(Stream* bs, Encoder* pEncoder)
{	
    short i;

#if 0
	StreamPad(bs);
#endif

	/*  vop_start_code  */
	StreamPutBits(bs, vop_start_code, 32);

	/*  vop_coding_type  */
	StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_coding_type, 2);

	/*  modulo_time_base  */
	i = 0;
    while(i < 0) 
	{
		StreamPutBit(bs, 1);
		i++;
	}
	StreamPutBit(bs, 0);

    WRITE_MARKER();

	StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_time_increment, MAX(log2bin(pEncoder->pVol->vop_time_increment_resolution-1), 1));
    
	WRITE_MARKER();
    
	if (!pEncoder->pCurFrameInfo->vop_coded)
	{
	   StreamPutBits(bs, 0, 1);
#if 0
	   StreamPad(bs);
#endif
	   return;
	}
	else 
	{
		StreamPutBits(bs, 1, 1);
	}
    
	/*  newpred_enable = 0  */

	/*  vop_rounding_type  */
	if (pEncoder->pCurFrameInfo->vop_coding_type == P_VOP)
    {
		StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_rounding_type,1);
	}

    /*  vop_reduced_resolution = 0  */
	if ((pEncoder->pVol->reduced_resolution_vop_enable == 1) && (pEncoder->pCurFrameInfo->vop_coding_type == I_VOP || pEncoder->pCurFrameInfo->vop_coding_type == P_VOP))
	{
		StreamPutBit(bs, 0);
	}

	/*  intra_dc_vlc_thr  = 0 */
	StreamPutBits(bs, 0, 3);	/* intra_dc_vlc_threshold */

	/*  interlace  */
	if (pEncoder->pVol->interlaced)
	{
		StreamPutBit(bs, pEncoder->pCurFrameInfo->top_field_first);
		StreamPutBit(bs, pEncoder->pCurFrameInfo->alternate_vertical_scan_flag);
	}
    
    /* S_VOP not support */

	/*  vop_quant ,  quant_precision = 5 */
	StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_quant, 5);
    
	/*  vop_fcode_forward  */
	if (pEncoder->pCurFrameInfo->vop_coding_type != I_VOP)
	{
	   StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_fcode_forward, 3);
	}

	/*  vop_fcode_backward  */
	if (pEncoder->pCurFrameInfo->vop_coding_type == B_VOP)
	{
	   StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_fcode_backward, 3);
	}

}


void WriteMpeg4Header(Stream* bs, Encoder* pEncoder)
{
    static const unsigned int vo_id = 0;
	static const unsigned int vol_id = 0;
	
	unsigned int iBits;

	int i;

#if 0
	StreamPad(bs);
#endif

	iBits = GetStreamPosition(bs);
//	int iCustomMatrixFlag;

    /* visual_object_sequence_start_code */
    StreamPutBits(bs, visual_object_sequence_start_code, 32);

	/* profile_and_level_indication */
    StreamPutBits(bs, pEncoder->pVol->profile_and_level_indication, 8);

	StreamPad(bs);
	/* visual_object_start_code */
	StreamPutBits(bs, visual_object_start_code, 32);
	/* is_visual_object_identifier */
	StreamPutBits(bs, 0, 1);
	/* visual_object_type */
	StreamPutBits(bs, Visual_Object_Type_Video, 4);
    /* video_signal_type */
	StreamPutBit(bs, 0);

	StreamPad(bs);
	/* video_object_start_code */
	StreamPutBits(bs, video_object_start_code|(vo_id&0x5), 32);

    StreamPad(bs);
    /*  video_object_layer_start_code  */
    StreamPutBits(bs, video_object_layer_start_code|(vol_id&0x4), 32);
   
    /*  random_accessible_vol  */
    StreamPutBits(bs, pEncoder->pVol->random_accessible_vol, 1);

	/*  video_object_type_indication  */
	StreamPutBits(bs, pEncoder->pVol->video_object_type_indication, 8);

	/*  is_object_layer_identifier  */
	StreamPutBits(bs, pEncoder->pVol->is_object_layer_identifier, 1);
	if (pEncoder->pVol->is_object_layer_identifier)
	{
		StreamPutBits(bs, pEncoder->pVol->video_object_layer_verid, 4);
		StreamPutBits(bs, pEncoder->pVol->video_object_layer_priority, 3);
	}

    /*  aspect_ratio_info */
	StreamPutBits(bs, pEncoder->pVol->aspect_ratio_info, 4);
	if (pEncoder->pVol->aspect_ratio_info == Pixel_Aspect_Ratio_Extended_PAR)
	{
		StreamPutBits(bs, pEncoder->pVol->par_width, 8);
		StreamPutBits(bs, pEncoder->pVol->par_height, 8);
	}

	/* vol_control_parameters */
	StreamPutBits(bs, pEncoder->pVol->vol_control_parameters, 1);
	if (pEncoder->pVol->vol_control_parameters)
	{
		StreamPutBits(bs, pEncoder->pVol->chroma_format, 2);
		StreamPutBits(bs, pEncoder->pVol->low_delay, 1);
		
		/* vbv_parameters = 0 */
		StreamPutBits(bs, 0, 1);
	}

	/*  video_object_layer_shape = rectangular  */
	StreamPutBits(bs, 0, 2);

	WRITE_MARKER();   
	/*   vop_time_increment_resolution  */
	StreamPutBits(bs, pEncoder->pVol->vop_time_increment_resolution, 16);

	WRITE_MARKER();
    /*   fixed_vop_rate  */
	StreamPutBits(bs, pEncoder->pVol->fixed_vop_rate, 1);
	if (pEncoder->pVol->fixed_vop_rate)
	{
		StreamPutBits(bs, pEncoder->pVol->fixed_vop_time_increment, MAX(log2bin(pEncoder->pVol->vop_time_increment_resolution-1),1));
	}
    
	/*  video_object_layer_width  video_object_layer_height  */
	WRITE_MARKER();
    StreamPutBits(bs, pEncoder->pVol->video_object_layer_width, 13);
	WRITE_MARKER();
	StreamPutBits(bs, pEncoder->pVol->video_object_layer_height, 13);
	WRITE_MARKER();
    
    /*  interlaced  */
	StreamPutBits(bs, pEncoder->pVol->interlaced, 1);

	/*  obmc_disable */
	StreamPutBits(bs, pEncoder->pVol->obmc_disable, 1);

	/*  sprite_enable  */
	if (pEncoder->pVol->video_object_layer_verid != 1)
	{
		if (pEncoder->pVol->sprite_enable == Sprite_Coding_Mode_GMC)
		{
			StreamPutBits(bs, 2, 2);		/* sprite_enable=='GMC' */
			StreamPutBits(bs, 3, 6);		/* no_of_sprite_warping_points */
			StreamPutBits(bs, 3, 2);		/* sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
			StreamPutBit(bs, 0);			/* sprite_brightness_change (not supported) */
		}
        else 
		{
			StreamPutBits(bs, 0, 2);
		}
	}
	else 
	{
		StreamPutBit(bs, 0);
	}

	/* not_8_bit = 0 */
	StreamPutBit(bs, 0);

	/* quant_type */
	StreamPutBit(bs, pEncoder->pVol->quant_type);
    if (pEncoder->pVol->quant_type)
	{   
		/*  load_intra_quant_mat ,user quant-matrix */
//		iCustomMatrixFlag = IsCustomIntraMatrix(pEncoder->pVol->load_intra_quant_mat);
		
//		if (iCustomMatrixFlag)
//		{
            /* write user quant-matrix  */
//			StreamPutBit(bs, 1);
//			LoadMpeg4IntraMatrix(bs, pEncoder->pVol->intra_quant_matrix);
//		}
//		else 
		{
			StreamPutBit(bs, 0);
		}
        
		/*  load_inter_quant_mat  */
//		iCustomMatrixFlag = IsCustomInterMatrix(pEncoder->pVol->load_inter_quant_mat);
//		if (iCustomMatrixFlag)
//		{  
			/* write user quant-matrix  */
//			StreamPutBit(bs, 1);
//			LoadMpeg4InterMatrix(bs, pEncoder->pVol->inter_quant_matrix);
//		}
//		else 
		{
			StreamPutBit(bs, 0);
		}
	}

	if (pEncoder->pVol->video_object_layer_verid != 1)
	{
		StreamPutBit(bs, pEncoder->pVol->quarter_sample);
	}

	/*  complexity_estimation_disable = 1 */
	StreamPutBit(bs, 1);
	/*  resync_marker_disable  = 1 */
	StreamPutBit(bs, 1);
	/*  data_partitioned  = 0 */
	StreamPutBit(bs, 0);

	if (pEncoder->pVol->video_object_layer_verid != 1)
	{
		/*  newpred_enable = 0  */
		StreamPutBit(bs, 0);
		/*  reduced_resolution_vop_enable */
		StreamPutBit(bs, pEncoder->pVol->reduced_resolution_vop_enable);
	}

	/*  scalability = 0  */
	StreamPutBit(bs, pEncoder->pVol->scalability);
    
	StreamPad(bs);

	/*  vop_start_code  */
	StreamPutBits(bs, vop_start_code, 32);

	/*  vop_coding_type  */
	StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_coding_type, 2);

	/*  modulo_time_base  */
	i = 0;
    while(i < 0) 
	{
		StreamPutBit(bs, 1);
		i++;
	}
	StreamPutBit(bs, 0);

    WRITE_MARKER();

	StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_time_increment, MAX(log2bin(pEncoder->pVol->vop_time_increment_resolution-1), 1));
    
	WRITE_MARKER();
    
	if (!pEncoder->pCurFrameInfo->vop_coded)
	{
	   StreamPutBits(bs, 0, 1);
	   PutBitsPad(bs);
	   return;
	}
	else 
	{
		StreamPutBits(bs, 1, 1);
	}
    
	/*  newpred_enable = 0  */

	/*  vop_rounding_type  */
	if (pEncoder->pCurFrameInfo->vop_coding_type == P_VOP || (pEncoder->pCurFrameInfo->vop_coding_type = S_VOP && pEncoder->pVol->sprite_enable == Sprite_Coding_Mode_GMC ))
    {
		StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_rounding_type,1);
	}

    /*  vop_reduced_resolution = 0  */
	if ((pEncoder->pVol->reduced_resolution_vop_enable == 1) && (pEncoder->pCurFrameInfo->vop_coding_type == I_VOP || pEncoder->pCurFrameInfo->vop_coding_type == P_VOP))
	{
		StreamPutBit(bs, 0);
	}

	/*  intra_dc_vlc_thr  = 0 */
	StreamPutBits(bs, 0, 3);

	/*  interlace  */
	if (pEncoder->pVol->interlaced)
	{
		StreamPutBit(bs, pEncoder->pCurFrameInfo->top_field_first);
		StreamPutBit(bs, pEncoder->pCurFrameInfo->alternate_vertical_scan_flag);
	}
    
    /*  S_VOP not support  */

	/*  vop_quant ,  quant_precision = 5 */
	StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_quant, 5);
    
	/*  vop_fcode_forward  */
	if (pEncoder->pCurFrameInfo->vop_coding_type != I_VOP)
	{
	   StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_fcode_forward, 3);
	}

	/*  vop_fcode_backward  */
	if (pEncoder->pCurFrameInfo->vop_coding_type == B_VOP)
	{
	   StreamPutBits(bs, pEncoder->pCurFrameInfo->vop_fcode_backward, 3);
	}
}

⌨️ 快捷键说明

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