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

📄 demo.c

📁 基于Linux的ffmepg decoder
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include "jpeg_dec.h"#ifdef LINUX#include "../../faraday_mpeg4_common/dev_mem.h"#define mFa526CleanInvalidateDCacheAll()#else// Clean & Invalidate all D-cache#define mFa526CleanInvalidateDCacheAll()	\__asm {							            \        MCR p15, 0, 0, c7, c14, 0			\}#endifvoid *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);// the exit entry pointvoid exit_main(void);int main(int argc, char **argv){  unsigned char *pbitstream_phy_addr,*pbitstream_virt_addr;  unsigned char *yuv_phy_addr[3],*yuv_virt_addr[3];  unsigned int bitstream_size;  void *dec_handle;  int ci; // component index  FJPEG_DEC_PARAM dec_param;  FJPEG_DEC_RESULT dec_result;  FILE *in_file,*out_file;  unsigned char * base;  if (argc != 3) {     printf ("usage: jpeg_decoder input_jpg_file output_yuv_file\n");     exit(1);  }  // open input YUV file, the YUV data file is arranged in 2D format  if((in_file=fopen(argv[1],"rb"))==NULL) {    printf ("Can't open file %s\n", argv[1]);    exit(1);  }  // open output bitstream file  if((out_file=fopen(argv[2],"wb"))==NULL) {    printf ("Can't open file %s\n", argv[2]);    exit(1);  }  // let's calculate the bitstream file size  if(fseek(in_file,0,SEEK_END))  exit(1);  if((bitstream_size=ftell(in_file))==-1)  exit(1);    rewind(in_file); // rewind the input bitstream file    // allocate the input bitstream buffer with 4-byte aligned (32-bit alignment)  // and obtain the physical address and virtual address  if((pbitstream_virt_addr=(unsigned char*)TM_DmaMalloc(bitstream_size,4,16,&pbitstream_phy_addr))==NULL)    exit(1);  // to read the jpeg bitstream file to the allocated memory space  if(fread(pbitstream_virt_addr,sizeof(unsigned char),bitstream_size,in_file)!=bitstream_size)    exit(1);  // let's flush the D-cache  mFa526CleanInvalidateDCacheAll();    printf("The bitstream size is %d\n",bitstream_size);#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 decoding parameters//  dec_param.pu32BaseAddr=(unsigned char *)0x90e00000;  dec_param.pu32BaseAddr= base;  dec_param.pu8BitstreamAddr=pbitstream_phy_addr;  dec_param.pfnDmaMalloc = TM_DmaMalloc;	// ben add  dec_param.pfnDmaFree = TM_DmaFree;		// ben add    // to create the jpeg decoder object  dec_handle=FJpegDecCreate(&dec_param);  if(!dec_handle) exit(1);    // to read the header of jpeg bitstream and get the yuv image information from     // variable 'dec_result' which is data type FJPEG_DEC_RESULT structure.  // Only after reading JPEG header can we know the component number and size information,  // so we should read the JPEG header in the first place before starting decoding.  FJpegDecReadHeader(dec_handle,&dec_result);     // set the number of components according after reading the header informations    dec_param.u8NumComponents=dec_result.u8NumComponents;    for(ci=0; ci<dec_result.u8NumComponents; ci++)    {      // depending on the number of components of this JPEG bitstream, we can allocate      // output buffer space of each component according to the returned informations in      // 'dec_result' variable.      // Note that the allocated YUV buffer space must be 8-byte aligned with physical address.      if((yuv_virt_addr[ci]=(unsigned char *)TM_DmaMalloc(dec_result.rgComponentInfo[ci].m_u32ComponentTotalSize,8,16,&yuv_phy_addr[ci]))==NULL)        exit(1);      dec_param.pu8YUVAddr[ci]=yuv_phy_addr[ci];    }  // let's flush the D-cache  mFa526CleanInvalidateDCacheAll();    // to begin to decode the jpeg bitstream and get the yuv image information from     // variable dec_result with data type FJPEG_DEC_RESULT structure  FJpegDecDecode(dec_handle,&dec_param);    // analyze the dec_result data structure in order to get the information of YUV output image  for(ci=0; ci<dec_result.u8NumComponents; ci++)    {      // write each component of YUV to the file      fwrite(yuv_virt_addr[ci],sizeof(unsigned char),dec_result.rgComponentInfo[ci].m_u32ComponentTotalSize,out_file);    }      // release the JPEG decoder object  FJpegDecDestroy(dec_handle);    // free the allocated YUV output aligned buffer  for(ci=0; ci<dec_result.u8NumComponents; ci++)    TM_DmaFree(yuv_virt_addr[ci],yuv_phy_addr[ci]);  // free the allocated JPEG bitstream aligned buffer  TM_DmaFree(pbitstream_virt_addr,pbitstream_phy_addr);    // close output YUV file  fclose(out_file);      // close input bitstream file  fclose(in_file);  // to the end of encoding  printf("Image width = %d, height = %d\n", dec_result.u32ImageWidth, dec_result.u32ImageHeight);  printf("JPEG decoding is done. See the output file.\n");//  exit_main();}#ifndef LINUXint dma_malloc(int size, void ** phy, void ** virt, void ** virt_ret){	*phy = *virt = (void *) malloc(size);	if (*virt != NULL)		return 0;	//ok	else		return -1;//fail}int dma_free(unsigned int size, void * phy, void * virt, void *virt_ret){	free ((unsigned char *)virt);	return 0;	//ok}#endifvoid * 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);					//  	}}void exit_main(void){  while(1) ; }

⌨️ 快捷键说明

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