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

📄 demo.c

📁 基于Linux的ffmepg decoder
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include "jpeg_enc.h"#include "../../faraday_mpeg4_common/dev_mem.h"// to declare the dma memory allocation functionvoid *TM_DmaMalloc(unsigned int size,unsigned char align_size,unsigned char reserved_size,void ** phy_ptr);void  TM_DmaFree(void * virt_ptr, void * phy_ptr);#define MAX(x, y) ((x) > (y) ? (x) : (y))#define MIN(x, y) ((x) < (y) ? (x) : (y))#define IMAGE_WIDTH	480	//192#define IMAGE_HEIGHT 640	//128#define IMAGE_COMP	3#define IMAGE_QUALITY	90#define IMAGE_RST	5#define SET_COMP(index,hsamp,vsamp)  \  (enc_param.rgComponentInfo[index].m_u8HSamplingFrequency = (hsamp), \   enc_param.rgComponentInfo[index].m_u8VSamplingFrequency = (vsamp))static void usage(char *name);int main(int argc, char **argv){  unsigned int image_size[3],yuv_size=0;   unsigned char *image_phy_addr[3];  unsigned char *image_virt_addr[3];  unsigned char *pbitstream_phy_addr,*pbitstream_virt_addr;  unsigned int bitstream_size;  void *enc_handle;  unsigned int max_h_samp,max_v_samp;  unsigned char * base;  FJPEG_ENC_PARAM enc_param;  FILE *in_file,*out_file;  char in_filename[256];  char out_filename[256];  int YUVsampling = 1; // set default to YUV422  int i;    enc_param.u32ImageQuality = IMAGE_QUALITY; // we set image quality to 90 (0~100)    enc_param.u32RestartInterval = IMAGE_RST; // we set restart interval to 5    enc_param.u32ImageWidth=IMAGE_WIDTH;  // set image width  enc_param.u32ImageHeight=IMAGE_HEIGHT; // set image height  // to describe the YUV format through the following encoding parameters    enc_param.u8NumComponents = IMAGE_COMP; // the input image has 3 components 'YUV'  	for (i=1; i< argc; i++) {		if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {			i ++;			strcpy (in_filename, argv[i]);		}		else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {			i ++;			strcpy (out_filename, argv[i]);		}		else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {			i++;			enc_param.u32ImageWidth = (unsigned int)atoi(argv[i]);		}		else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) {			i++;			enc_param.u32ImageHeight = (unsigned int)atoi(argv[i]);		}		else if (strcmp("-comp", argv[i]) == 0 && i < argc - 1 ) {			i++;			enc_param.u8NumComponents = (unsigned int)atoi(argv[i]);		}		else if (strcmp("-qlt", argv[i]) == 0 && i < argc - 1 ) {			i++;			enc_param.u32ImageQuality = (unsigned int)atoi(argv[i]);		}		else if (strcmp("-rst", argv[i]) == 0 && i < argc - 1 ) {			i++;			enc_param.u32RestartInterval = (unsigned int)atoi(argv[i]);		}		else if (strcmp("-fmt", argv[i]) == 0 && i < argc - 1 ) {			i ++;			if (strcmp("yuv420", argv[i]) == 0)				YUVsampling = 0;			else if (strcmp("yuv422", argv[i]) == 0)				YUVsampling = 1;			else if (strcmp("yuv211", argv[i]) == 0)				YUVsampling = 2;			else if (strcmp("yuv333", argv[i]) == 0)				YUVsampling = 3;			else if (strcmp("yuv222", argv[i]) == 0)				YUVsampling = 4;			else if (strcmp("yuv111", argv[i]) == 0)				YUVsampling = 5;			else {				printf("Unreconigized format '%s', stop\n", argv[i]);				usage(argv[0]);				return -1;			}		}		else {			printf("Unreconigized parameter '%s', stop\n", argv[i]);			usage(argv[0]);			return -1;		}	}/***************************************************************************** * Values checking ****************************************************************************/	if ((enc_param.u32ImageWidth > 65536) || (enc_param.u32ImageHeight > 65536)) {		usage(argv[0]);		return -1;	}	if (enc_param.u8NumComponents > 3) {		usage(argv[0]);		return -1;	}	if (enc_param.u32ImageQuality > 100) {		usage(argv[0]);		return -1;	}	// open input YUV file, the YUV data file is arranged in 2D format	if((in_file=fopen(in_filename,"rb"))==NULL) {      printf ("Can't open file %s\n", in_filename);      exit(1);	}	// open output bitstream file	if((out_file=fopen(out_filename,"wb"))==NULL) {      printf ("Can't open file %s\n", out_filename);      exit(1);	}#ifdef LINUX  base = (unsigned char *)ioremap(0x90700000, 0x100000);  if (base == 0) {			/// maping failure	printf("mpeg4 maping failure\n");	return -1;  }  printf("jpeg virt addr = 0x%x\n", (int)base);#else  base = 0x90700000;#endif  // set the hardware core base address  enc_param.pu32BaseAddr = base;    if(enc_param.u8NumComponents==1)    YUVsampling = 5; // if there is only one component, it is gray, so we force it YUV111    	switch (YUVsampling) {		case 0:		  SET_COMP(0, 2, 2);		  SET_COMP(1, 1,1);		  SET_COMP(2, 1,1);		  break;		case 1:		  SET_COMP(0, 4,1);		  SET_COMP(1, 2,1);		  SET_COMP(2, 2,1);		  break;		case 2:		  SET_COMP(0, 2,1);		  SET_COMP(1, 1,1);		  SET_COMP(2, 1,1);		  break;		case 3:		  SET_COMP(0, 3,1);		  SET_COMP(1, 3,1);		  SET_COMP(2, 3,1);		  break;		case 4:		  SET_COMP(0, 2,1);		  SET_COMP(1, 2,1);		  SET_COMP(2, 2,1);		  break;		case 5:		  SET_COMP(0, 1,1);		  SET_COMP(1, 1,1);		  SET_COMP(2, 1,1);		  break;		default:		  break;	}  // to set each component's sampling factor (horizontally and vertically)  // get the maximum horizontal sampling factor  max_h_samp=MAX(enc_param.rgComponentInfo[0].m_u8HSamplingFrequency,                 MAX(enc_param.rgComponentInfo[1].m_u8HSamplingFrequency,                     enc_param.rgComponentInfo[2].m_u8HSamplingFrequency));  // get the maximum horizontal sampling factor  max_v_samp=MAX(enc_param.rgComponentInfo[0].m_u8VSamplingFrequency,                 MAX(enc_param.rgComponentInfo[1].m_u8VSamplingFrequency,                     enc_param.rgComponentInfo[2].m_u8VSamplingFrequency));                         // calculate each component size according to its maximum sampling factor  // and individual sampling factor  for(i=0;i<enc_param.u8NumComponents;i++)    {      image_size[i]=(((enc_param.rgComponentInfo[i].m_u8HSamplingFrequency*enc_param.u32ImageWidth)                       /max_h_samp) *                    ((enc_param.rgComponentInfo[i].m_u8VSamplingFrequency*enc_param.u32ImageHeight)                       /max_v_samp));      yuv_size += image_size[i];            // allocate the input Y frame buffer with 8-byte aligned (64-bit alignment)        if((image_virt_addr[i]=(unsigned char *)TM_DmaMalloc(image_size[i],8,16,&image_phy_addr[i]))==NULL)        { printf("Memory allocation error!\n"); exit(1); }       // set each component's buffer address to the encoding parameter      enc_param.pu8YUVAddr[i]=image_phy_addr[i];      // to read the Y component of YUV data from file      if(fread(image_virt_addr[i],sizeof(unsigned char),image_size[i],in_file)!=image_size[i])        { printf("File read error!\n"); exit(1); }     }  // assign bitstream buffer about the 1.5 * YUV image buffer size  bitstream_size=(yuv_size*3)/2;  // allocate the output bitstream buffer with 4-byte aligned (32-bit alignment)    if((pbitstream_virt_addr=(unsigned char*)TM_DmaMalloc(bitstream_size,4,16,&pbitstream_phy_addr))==NULL)    exit(1);  enc_param.pu8BitstreamAddr=pbitstream_phy_addr;    // to create the jpeg encoder object  enc_handle=FJpegEncCreate(&enc_param);  if(!enc_handle) exit(1);    // to begin to encode the input image  FJpegEncEncode(enc_handle);      // to obtain encoded bitstream buffer length  bitstream_size=FJpegEncGetBitsLength(enc_handle);  // write the encoded bitstream to the file  fwrite(pbitstream_virt_addr,sizeof(unsigned char),bitstream_size,out_file);  printf("JPEG Encoding is done!, jpeg file size = 0x%x\n", bitstream_size);      // release the JPEG encoder object  FJpegEncDestroy(enc_handle);    // free the allocated JPEG bitstream aligned buffer  TM_DmaFree(pbitstream_virt_addr,pbitstream_phy_addr);  for(i=0;i<enc_param.u8NumComponents;i++)    { TM_DmaFree(image_virt_addr[i],image_phy_addr[i]); }      // close output YUV file  fclose(out_file);      // close input bitstream file  fclose(in_file);  return 0;}void * TM_DmaMalloc(unsigned int size,unsigned char align_size,unsigned char reserved_size,void ** phy_ptr){	unsigned char *virt_mem_ptr;	unsigned char *virt_ret;	if (!align_size) {		size += (reserved_size + 9);	   	if (dma_malloc(size, phy_ptr, (void **)&virt_mem_ptr, (void **)&virt_ret) == 0) {			/* Store (mem_ptr - "real allocated memory") in *(mem_ptr-9) */			*(virt_mem_ptr + 0) = 9;			/* Store (malloc size) in *(mem_ptr-8) ~ *(mem_ptr-5)*/			*(virt_mem_ptr + 1) = (size >> 24) & 0xFF;			*(virt_mem_ptr + 2) = (size >> 16) & 0xFF;			*(virt_mem_ptr + 3) = (size >> 8) & 0xFF;			*(virt_mem_ptr + 4) = (size >> 0) & 0xFF;			/* Store (virt_ret at consistent_alloc) in *(mem_ptr-4) ~ *(mem_ptr-1)*/			*(virt_mem_ptr + 5) = ((unsigned int)virt_ret >> 24) & 0xFF;			*(virt_mem_ptr + 6) = ((unsigned int)virt_ret >> 16) & 0xFF;			*(virt_mem_ptr + 7) = ((unsigned int)virt_ret >> 8) & 0xFF;			*(virt_mem_ptr + 8) = ((unsigned int)virt_ret >> 0) & 0xFF;			*(unsigned int *)phy_ptr += 9;			/* Return the mem_ptr pointer */			return (void *) (virt_mem_ptr + 9);		}	}	else {		unsigned char *tmp;		/*		 * Allocate the required size memory + alignment so we		 * can realign the data if necessary		 */		size += (reserved_size + align_size + 8);	   	if (dma_malloc(size, phy_ptr, (void **)&tmp, (void **)&virt_ret) == 0) {			/* Align the tmp pointer */			virt_mem_ptr =				(unsigned char *) ((unsigned int) (tmp + 8 + align_size - 1) &							 (~(unsigned int) (align_size - 1)));			/*			 * Special case where malloc have already satisfied the alignment			 * We must add alignment to mem_ptr because we must store			 * (mem_ptr - tmp) in *(mem_ptr-1)			 * If we do not add alignment to mem_ptr then *(mem_ptr-1) points			 * to a forbidden memory space			 */			while ((virt_mem_ptr - tmp) <= 8)				virt_mem_ptr += align_size;			*(unsigned int *)phy_ptr += (virt_mem_ptr - tmp);			/*			 * (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve			 * the real malloc block allocated and free it in xvid_free			 */			/* Store (mem_ptr - "real allocated memory") in *(mem_ptr-5) */			*(virt_mem_ptr - 9) = (unsigned char) (virt_mem_ptr - tmp);			/* Store (malloc size) in *(mem_ptr-8) ~ *(mem_ptr-5)*/			*(virt_mem_ptr - 8) = (size >> 24) & 0xFF;			*(virt_mem_ptr - 7) = (size >> 16) & 0xFF;			*(virt_mem_ptr - 6) = (size >> 8) & 0xFF;			*(virt_mem_ptr - 5) = (size >> 0) & 0xFF;			/* Store (virt_ret at consistent_alloc) in *(mem_ptr-4) ~ *(mem_ptr-1)*/			*(virt_mem_ptr - 4) = ((unsigned int)virt_ret >> 24) & 0xFF;			*(virt_mem_ptr - 3) = ((unsigned int)virt_ret >> 16) & 0xFF;			*(virt_mem_ptr - 2) = ((unsigned int)virt_ret >> 8) & 0xFF;			*(virt_mem_ptr - 1) = ((unsigned int)virt_ret >> 0) & 0xFF;			/* Return the aligned pointer */			return (void *) virt_mem_ptr;		}	}	return NULL;}void TM_DmaFree(void * virt_ptr, void * phy_ptr){	unsigned int tsize;	unsigned char *virt_ret;	unsigned char offset;	/* *(virt_mem_ptr - 1) give us the offset to the real malloc block */	if (virt_ptr) {		offset = *(unsigned char *) ((unsigned int) virt_ptr - 9);		tsize = (*(unsigned char *)((unsigned int) virt_ptr - 8) << 24) +		  	(*(unsigned char *)((unsigned int) virt_ptr - 7) << 16) +			(*(unsigned char *)((unsigned int) virt_ptr - 6) << 8) +			(*(unsigned char *)((unsigned int) virt_ptr - 5) << 0);		virt_ret = (unsigned char*)((*(unsigned char *)((unsigned int) virt_ptr - 4) << 24) +						(*(unsigned char *)((unsigned int) virt_ptr - 3) << 16) +						(*(unsigned char *)((unsigned int) virt_ptr - 2) << 8) +						(*(unsigned char *)((unsigned int) virt_ptr - 1) << 0));		dma_free(tsize,								// total size				(void *)((unsigned int) phy_ptr - offset),	// original phy point				(void *)((unsigned int) virt_ptr - offset),		// original virt point				virt_ret);					//  	}}static void usage(char *name){	fprintf(stderr, "Usage : %s <-i input_file>  <-o output_file> [OPTIONS]\n", name);	fprintf(stderr, "-i input_file            : input raw data file name\n");	fprintf(stderr, "-o output_file         : output jpeg file name\n");	fprintf(stderr, "Options :\n");	fprintf(stderr, "-fmt input_format  : input file format, posible: \"yuv420\", \"yuv422\", \"yuv211\", \"yuv333\", \"yuv222\", \"yuv111\"  (default = yuv422)\n");	fprintf(stderr, " -w integer            : picture width ([1.2048, default = %d])\n", IMAGE_WIDTH);	fprintf(stderr, " -h integer           : picture height ([1.2048], default = %d)\n", IMAGE_HEIGHT);	fprintf(stderr, " -qlt integer         : quality ([1.100], default = %d)\n", IMAGE_QUALITY);	fprintf(stderr, " -rst integer        : restart marker([1. inf], default = %d)\n", IMAGE_RST);	fprintf(stderr, " -comp integer    : component number in input file ([1. 3], default = %d)\n", IMAGE_COMP);}

⌨️ 快捷键说明

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