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

📄 init.cpp

📁 H.263的编码程序,加了CPU指令优化,VC版.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*!
********************************************************
*\file
*    init.cpp
*\brief
*    implementation for functions which is used to initialize encoder status.
*\date
*    12/6/2002
*
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "init.h"
#include "mot_est.h"
#include "Exceptions.h"

extern void __cdecl rgbtoyuv(unsigned char *pSrc, int rows, int cols, unsigned char *pY,
							 unsigned char *pU, unsigned char *pV);
/*!
*******************************************************************************
*
*   Name:          alloc_mem
*   Description:   allocate memory for buffers to be used by encoder
*   Input:         
*   Output:        
*   Last modified: 2002/12/17
*
*******************************************************************************/
void alloc_mem(H263VencStatus *encoder, MCParam *mc, putstrm *strm)
{
	int width = encoder->pels;
	int height = encoder->lines;
	int x = width/16;
	int y = height/16;

	int i, j;
	unsigned char *tmp;
	int edge  = encoder->mv_outside_frame ? 32*(width+height)+1024 : 0;
	int skip1 = encoder->mv_outside_frame ? 16*(width+32)+16 : 0;
	int skip2 = encoder->mv_outside_frame ? 8*(width/2+16)+8 : 0;
	int offset1 = width * height;
	int offset2 = offset1*5/4;
	int offset3 = offset1*3/2;

/* Allocate memory for image*/

	/* Buffer to store source frames if video format other than YUV420 is used */
	if (encoder->VideoFormat != YUV420)
	{
		int len = (encoder->VideoFormat == RGB24) ? (width * height * 3) : offset1*3/2;
		int x1  = offset1;
		int x2  = (encoder->VideoFormat == RGB24) ? (width * height * 2) : offset2;

		if (NULL == (encoder->frameToEncode.pLum = (unsigned char *) malloc(sizeof(unsigned char)*len)))
		{
			printf("no memory!");
			exit(-1);
		}
		encoder->frameToEncode.pCb = encoder->frameToEncode.pLum + x1;
        encoder->frameToEncode.pCr = encoder->frameToEncode.pLum + x2; 
	}

	/* Buffer to all store reconstructed frames for reference */
	for (i = 0; i < encoder->buf_cycle; i++)
	{
		if (NULL == (tmp = (unsigned char *)malloc(sizeof(unsigned char)*(offset1+edge)*3/2)))
		{
			printf("no memory\n");
			exit(-1);
		}
		(encoder->frame_buf[i]).pLum = tmp + skip1;
		(encoder->frame_buf[i]).pCb  = tmp + offset1 + edge + skip2;
		(encoder->frame_buf[i]).pCr  = tmp + offset2 + edge*5/4 + skip2;
	}
    /* Buffer for B pictures */
    for (i = 0; i <= encoder->B_frame; i++)
	{
		if (NULL == (tmp = (unsigned char *)malloc(sizeof(unsigned char)*offset1*3/2)))
		{
			printf("no memory\n");
			exit(-1);
		}
		(encoder->BPicture[i]).pLum = tmp;
		(encoder->BPicture[i]).pCb  = tmp + offset1;
		(encoder->BPicture[i]).pCr  = tmp + offset2;
	}
    /* Buffer for interpolated lum picture */
	if (NULL == (tmp = (unsigned char *)malloc(sizeof(unsigned char)*(offset1+edge)*4)))
	{
		printf("no memory\n");
		exit(-1);
	}
	encoder->prev_ipol = encoder->mv_outside_frame ? tmp+(width*2+64)*32+32 : tmp;
	if (encoder->B_frame)
	{
		if (NULL == (tmp = (unsigned char *)malloc(sizeof(unsigned char)*(offset1+edge)*4)))
		{
			printf("no memory\n");
			exit(-1);
		}
		encoder->next_ipol = encoder->mv_outside_frame ? tmp+(width*2+64)*32+32 : tmp;
	}
/* initialize MC */
	for (i = 0; i < y+1; i++)
	{
		for (j = 0; j < x+2; j++)
		{

			if (NULL == (mc->mv_frame[0][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
				NULL == (mc->mv_frame[1][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
				NULL == (mc->mv_frame[2][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
				NULL == (mc->mv_frame[3][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
				NULL == (mc->mv_frame[4][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
				NULL == (mc->mv_frame[5][i][j] = (MotionVector *)malloc(sizeof(MotionVector))))
			{
				printf("no memory!\n");
				exit(-1);
			}
		}
	}
	for (i = 1; i < x+1; i++)
	{
		mc->mv_frame[0][0][i]->x = NO_VEC;
		mc->mv_frame[0][0][i]->y = NO_VEC;
		mc->mv_frame[0][0][i]->x_half = 0;
		mc->mv_frame[0][0][i]->y_half = 0;
		mc->mv_frame[0][0][i]->Mode = MODE_INTRA;
	}
	for (i = 0; i < y+1; i++)
	{
		zeroVec(mc->mv_frame[0][i][0]);
		mc->mv_frame[0][i][0]->Mode = MODE_INTRA;
        zeroVec(mc->mv_frame[0][i][x+1]);
		mc->mv_frame[0][i][x+1]->Mode = MODE_INTRA;
	}

	if(encoder->B_frame)
	{
		for (i = 0; i < y; i++)
		{
			for (j = 0; j < x; j++)
			{
				
				if (NULL == (mc->mv_lastframe[0][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
					NULL == (mc->mv_lastframe[1][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
					NULL == (mc->mv_lastframe[2][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
					NULL == (mc->mv_lastframe[3][i][j] = (MotionVector *)malloc(sizeof(MotionVector)))||
					NULL == (mc->mv_lastframe[4][i][j] = (MotionVector *)malloc(sizeof(MotionVector))))
					
				{
					printf("no memory!\n");
					exit(-1);
				}
			}
		}
		//! initialize backward mvs
		for (i = 1; i < x+1; i++)
		{
			mc->mv_frame[5][0][i]->x = NO_VEC;
			mc->mv_frame[5][0][i]->y = NO_VEC;
			mc->mv_frame[5][0][i]->x_half = 0;
			mc->mv_frame[5][0][i]->y_half = 0;
		}
		for (i = 0; i < y+1; i++)
		{
			zeroVec(mc->mv_frame[5][i][0]);
			zeroVec(mc->mv_frame[5][i][x+1]);
		}
	}
/* allocate memory for stream buffer */
	if(NULL == (strm->poutint = (int*)malloc(strm->buff_size)))
	{
		printf("no memory!\n");
		exit(-1);
	}
}

/*!
*******************************************************************************
*
*   Name:          ini_para
*   Description:   initialize parameters of encoder at the beginning of encoding
*   Input:         
*   Output:        
*   Last modified: 2002/12/5
*
*******************************************************************************/
int init_para (H263VencStatus *encoder, int P_rate, int QP, int QI, int QB, int gob, int framerate, int bitrate)
{

	if(P_rate >= 0)                          //< p_rate
	{
		encoder->p_rate = P_rate;
	}
	else
	{
		printf("Error Prate");
		return 0;
	}
	if(QP < 32 && QP > 0)                    //< QP
	{
		encoder->QP     = QP;
	}
	else
	{
		printf("Error QUANT value for P frame");
		return 0;
	}
	if(QI < 32 && QI > 0)                    //< QI 
	{
		encoder->QI     = QI;
	}
	else
	{
		printf("Error QUANT value for I frame");
		return 0;
	}
	if(QB < 32 && QB > 0)                    //< QB
	{
		encoder->QB     = QB;
	}
	else
	{
		printf("Error QUANT value for B frame");
		return 0;
	}
	if (!gob)                               //< gobsync
		encoder->gobsync = 0;

	encoder->gfid = 0;                      //< gob frame ID

	if (framerate >= 0 && framerate <=30)   //< framerate
	{
		encoder->framerate = framerate;
	}
	else
	{
		printf("Error frame rate");
		return 0;
	}

	if (bitrate > 0)
	{
		encoder->target_bitrate = bitrate*1024;
	}
	else if (bitrate == 0)
	{		
		encoder->target_bitrate = 0;
	}
	else
	{
		printf("Error Target bitrate");
		return 0;
	}


	return 1;
}

/*!
*******************************************************************************
*
*   Name:          
*   Description:   
*   Input:         
*   Output:        
*   Last modified: 2002/12/21
*
*******************************************************************************/
int  init_bitstrm(int type, unsigned int time, putstrm* putstrmctrl)
{
	putstrmctrl->bitoutint = 0;
	putstrmctrl->pcntint = 32;
	putstrmctrl->ptrint = 0;
	putstrmctrl->pic_type = type;
	putstrmctrl->timestamp = time;

	return 1;
}
/*!
*******************************************************************************
*
*   Name:          
*   Description:   
*   Input:         
*   Output:        
*   Last modified: 2002/12/21
*
*******************************************************************************/
int  init_control(H263VencStatus *encoder)
{
	encoder->PTYPE = INTRA;
	encoder->total_Q = encoder->QI;
	encoder->TR     = 0;
	encoder->pic_nbr= 0;
	encoder->pcount = 0;
	encoder->B_count = 0;
	encoder->totalBits = 0;
	encoder->zero_index = 0;
	encoder->ref_index = 0;
	encoder->ref_pic = 0;
	encoder->frame_skip = 0;
    
	if (encoder->target_bitrate)
	{
		for (int i = 0; i < encoder->framerate; i++)
		{
			encoder->framebits[i] = -1;
		}
	}

⌨️ 快捷键说明

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