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

📄 test_enc_suc.c

📁 MPEG4视频编解码内有divx(编码)
💻 C
字号:

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#include "encore.h"

#define MY_APP_ID 0x0815			// doesn't matter

#define XDIM 600
#define YDIM 400
#define BUFFERSIZE 1512000		// This should be more than enough for 1 frame

static float RGBYUV02990[256], RGBYUV05870[256], RGBYUV01140[256];
static float RGBYUV01684[256], RGBYUV03316[256];
static float RGBYUV04187[256], RGBYUV00813[256];
#   define  MAX(a,b)              (((a) > (b)) ? (a) : (b))
#   define  CLIP(a,i,s)           (((a) > (s)) ? (s) : MAX(a,i))
/************************************************************************
 *
 *  int RGB2YUV (int x_dim, int y_dim, void *bmp, YUV *yuv)
 *
 *	Purpose :	It takes a 24-bit RGB bitmap and convert it into
 *				YUV (4:2:0) format
 *
 *  Input :		x_dim	the x dimension of the bitmap
 *				y_dim	the y dimension of the bitmap
 *				bmp		pointer to the buffer of the bitmap
 *				yuv		pointer to the YUV structure
 *
 *  Output :	0		OK
 *				1		wrong dimension
 *				2		memory allocation error
 *
 *	Side Effect :
 *				None
************************************************************************/

void InitLookupTable()
{
	int i;

	for (i = 0; i < 256; i++) RGBYUV02990[i] = (float)0.2990 * i;
	for (i = 0; i < 256; i++) RGBYUV05870[i] = (float)0.5870 * i;
	for (i = 0; i < 256; i++) RGBYUV01140[i] = (float)0.1140 * i;
	for (i = 0; i < 256; i++) RGBYUV01684[i] = (float)0.1684 * i;
	for (i = 0; i < 256; i++) RGBYUV03316[i] = (float)0.3316 * i;
	for (i = 0; i < 256; i++) RGBYUV04187[i] = (float)0.4187 * i;
	for (i = 0; i < 256; i++) RGBYUV00813[i] = (float)0.0813 * i;
}

int convertRGB (void *bmp, void *yuv, long type, long x_dim, long y_dim)
{
	static int init_done = 0;

	long i, j, size, stride;
	unsigned char *r, *g, *b;
	unsigned char *y, *u, *v;
	unsigned char *pu1, *pu2, *pv1, *pv2, *psu, *psv;
	unsigned char *y_buffer, *u_buffer, *v_buffer;
	unsigned char *sub_u_buf, *sub_v_buf;

	if (init_done == 0)
	{
		InitLookupTable();
		init_done = 1;
	}

	// check to see if x_dim and y_dim are divisible by 2
	if ((x_dim % 2) || (y_dim % 2)) return 1;
	size = x_dim * y_dim;

	if (type == 17) stride = 3;
	else if (type == 18) stride = 4;

	// allocate memory
	y_buffer = (unsigned char *)yuv;
	sub_u_buf = y_buffer + size;
	sub_v_buf = y_buffer + size * 5 / 4;
	u_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
	v_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
	if (!(u_buffer && v_buffer))
	{
		if (u_buffer) free(u_buffer);
		if (v_buffer) free(v_buffer);
		return 2;
	}

	b = (unsigned char *)bmp;
	y = y_buffer;
	u = u_buffer;
	v = v_buffer;

	// convert RGB to YUV
	for (j = 0; j < y_dim; j ++)
	{
		y = y_buffer + (y_dim - j - 1) * x_dim;
		u = u_buffer + (y_dim - j - 1) * x_dim;
		v = v_buffer + (y_dim - j - 1) * x_dim;

		for (i = 0; i < x_dim; i ++) {
			g = b + 1;
			r = b + 2;
			*y = (unsigned char)CLIP ((  RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]),       0, 255.f);
			*u = (unsigned char)CLIP ((- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2          + 128), 0, 255.f);
			*v = (unsigned char)CLIP ((  (*r)/2          - RGBYUV04187[*g] - RGBYUV00813[*b] + 128), 0, 255.f);
			b += stride;
			y ++;
			u ++;
			v ++;
		}
	}

	// subsample UV
	for (j = 0; j < y_dim/2; j ++)
	{
		psu = sub_u_buf + j * x_dim / 2;
		psv = sub_v_buf + j * x_dim / 2;
		pu1 = u_buffer + 2 * j * x_dim;
		pu2 = u_buffer + (2 * j + 1) * x_dim;
		pv1 = v_buffer + 2 * j * x_dim;
		pv2 = v_buffer + (2 * j + 1) * x_dim;
		for (i = 0; i < x_dim/2; i ++)
		{
			*psu = (*pu1 + *(pu1+1) + *pu2 + *(pu2+1)) / 4;
			*psv = (*pv1 + *(pv1+1) + *pv2 + *(pv2+1)) / 4;
			psu ++;
			psv ++;
			pu1 += 2;
			pu2 += 2;
			pv1 += 2;
			pv2 += 2;
		}
	}

	free(u_buffer);
	free(v_buffer);

	return 0;
}

int main()
{
	FILE* f_out;
	FILE* f_in;
	//char *in_buffer[3];
	//char bmp_buffer[]=(char*)malloc(3*XDIM*YDIM);
	//char divx_buffer[]=(char*)malloc(BUFFERSIZE);  
	//char yuv_buffer[XDIM*YDIM + XDIM/2*YDIM/2 + XDIM/2*YDIM/2];
  
	char *in_buffer[3];
	char *bmp_buffer;
	char *divx_buffer;  
	char *yuv_buffer;
  
	int status;
   int frame_size;
   
   ENC_PARAM enc_param;
   ENC_FRAME enc_frame;
   ENC_RESULT enc_result;

   frame_size = XDIM*YDIM + XDIM/2*YDIM/2 + XDIM/2*YDIM/2;		// full Y, subsamples U,V

/* ...skip check for malloc failure... don't do that in application!!! */

   if((!(yuv_buffer=(char*) malloc(frame_size)))||
   !(bmp_buffer=(char*) malloc(3*XDIM*YDIM))||
   !(divx_buffer=(char*) malloc(BUFFERSIZE))
   )return 11;		

/*********************************************************************/
/*                            DIVX PART  Start                       */
/*********************************************************************/

/* Init the encoder */

    enc_param.x_dim = XDIM; 
    enc_param.y_dim = YDIM;
    enc_param.framerate = 24.0;
    enc_param.bitrate = 900000;

    enc_param.rc_period = 2000; 
    enc_param.rc_reaction_period = 10;
    enc_param.rc_reaction_ratio = 20;
    enc_param.search_range = 128;				

    enc_param.max_quantizer = 15;
    enc_param.min_quantizer = 2;    		// these setting are not
    						// important for a single file	
	if((f_in=fopen("win2k.raw","rb"))==0)     //open a file of bmp raw data 
	{
	//	printf("Can't open file bmp.raw to read!");
		exit(0);
	}
	if((f_out=fopen("win2k.div","wb"))==0)
	{
		printf("Can't open file to write!");
		exit(0);
	}

    status = encore(MY_APP_ID, ENC_OPT_INIT, &enc_param, NULL);
	 printf("Encore INIT return %d\n",status);

/*********************************************************************/
/*                            Main Part                              */
/*********************************************************************/

/* read raw RGB from stdin */
	
	fread(bmp_buffer,XDIM,3*YDIM,f_in);
	fclose(f_in);
	
/* convert raw RGB to YUV 4:2:0, that encore()-input format */

	in_buffer[0]=yuv_buffer;
	in_buffer[1]=yuv_buffer + XDIM*YDIM;
	in_buffer[2]=yuv_buffer + XDIM*YDIM + XDIM/2*YDIM/2;
  
  	//RGB2YUV(XDIM,YDIM,bmp_buffer,in_buffer[0],in_buffer[1],in_buffer[2], 0) ; 
	convertRGB (bmp_buffer, yuv_buffer, 17, XDIM, YDIM);

/* Encode one frame */

   enc_frame.image       = (void *) yuv_buffer;
   enc_frame.bitstream   = (void *) divx_buffer;
   enc_frame.length      = 0;							// will be filled by routine

   status = encore(MY_APP_ID, ENC_OPT_WRITE, &enc_frame, &enc_result);				

   printf("Encore ENCODE return %d, divx-stream length=%ld bytes\n",status,enc_frame.length);
   printf("Encore RESULT IsKeyFrame=%d\n",enc_result.isKeyFrame);

/* Write encoded data to stdout */

	fwrite(divx_buffer,1,enc_frame.length,f_out);
	fclose(f_out);

/*********************************************************************/
/*                            DIVX PART  Stop                        */
/*********************************************************************/

/* Stop the encoder */

   status = encore(MY_APP_ID, ENC_OPT_RELEASE, &enc_frame, NULL);							// END Encoding
	printf("Encore RELEASE return %d\n",status);
	
	free(divx_buffer);
	free(yuv_buffer);
	free(bmp_buffer);
   
	return 0;
}

⌨️ 快捷键说明

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