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

📄 ztiming.c

📁 可用于TM1300/PNX1300系列DSP(主要用于视频处理)压缩库即应用例子。
💻 C
字号:
/*
 *  +-------------------------------------------------------------------+
 *  | Copyright (c) 1995,2000 TriMedia Technologies Inc.                |
 *  |                                                                   |
 *  | This software  is furnished under a license  and may only be used |
 *  | and copied in accordance with the terms  and conditions of such a |
 *  | license  and with  the inclusion of this  copyright notice.  This |
 *  | software or any other copies of this software may not be provided |
 *  | or otherwise  made available  to any other person.  The ownership |
 *  | and title of this software is not transferred.                    |
 *  |                                                                   |
 *  | The information  in this software  is subject  to change  without |
 *  | any  prior notice  and should not be construed as a commitment by |
 *  | Philips Semiconductors.                                           |
 *  |                                                                   |
 *  | This  code  and  information  is  provided  "as is"  without  any |
 *  | warranty of any kind,  either expressed or implied, including but |
 *  | not limited  to the implied warranties  of merchantability and/or |
 *  | fitness for any particular purpose.                               |
 *  +-------------------------------------------------------------------+
 */

/*------------------------------ Includes ------------------------------------*/

#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include <tmTimers.h>
#include "unistd.h"
#include "errno.h"

#include <tmlib/tmtypes.h>
#include <tmlib/dprintf.h>

#include "zlib.h"

#ifndef O_BINARY
#define O_BINARY
#endif

/*---------------------------- Test parameters -------------------------------*/

typedef struct stat StatBuf;
int TimerInstance;
UInt32 clock_cycles;

UInt32 total_comp_read = 0;
UInt32 total_comp_cycles = 0;

UInt32 total_uncomp_read = 0;
UInt32 total_uncomp_cycles = 0;

UInt32 total_comp_size = 0;

/*------------------------ Recursive directory printing ----------------------*/

void print_file_info(String f)
{
    StatBuf buf;
    Int32 fid_norm;
    Int32 lv_read;
    Int32 lv_ret;
    Byte *comp_mem, *uncomp_mem, *decomp_mem;
    UInt32 lv_uncomp_size, lv_decomp_size, lv_comp_size;
    UInt32 lv_time;

    if (stat(f,&buf)==0 &&  S_ISDIR(buf.st_mode)== 0)
    {
       printf(" * size= %4d dir=%d chr=%d reg=%d ino=%04d | %s\n",
             buf.st_size, S_ISDIR(buf.st_mode), 
             S_ISCHR(buf.st_mode), S_ISREG(buf.st_mode), buf.st_ino, f);

       /* save size of uncompressed file */
       lv_uncomp_size = buf.st_size;

       /* allocate memory for uncompressed and decompressed data */
       uncomp_mem = (Byte *)malloc(lv_uncomp_size);
       decomp_mem = (Byte *)malloc(lv_uncomp_size);


       /* save size of max compressed file */
       lv_comp_size = lv_uncomp_size + ((lv_uncomp_size*0.015)+1) +12 ;
       comp_mem = (Byte *)malloc(lv_comp_size);

       if(comp_mem == NULL || uncomp_mem == NULL || decomp_mem == NULL)
       {
          printf("failed to allocate memory\n");
          return;
       }

       /* open uncompressed files */
       if((fid_norm = open(f, O_BINARY|O_RDONLY,0)) == -1){
          printf("failed to open %s\n",f+2);
          return;
       }

       /* read uncompressed data */
       if((lv_read = read(fid_norm,uncomp_mem,lv_uncomp_size)) != lv_uncomp_size){
           printf("failed to read data %d %d %s\n",lv_read,lv_uncomp_size,f);
           close (fid_norm);
           return;
       }

       /* reset timer */
       timSetTimerValue(TimerInstance, 0);

       /* compress data */
       if(lv_ret = compress(comp_mem, &lv_comp_size, uncomp_mem, lv_uncomp_size) != Z_OK){
          printf("failed compress data %d %d %s\n",lv_comp_size,lv_uncomp_size,f);
          close (fid_norm);
          return;
       }
   
       /* get timer */
       timGetTimerValue(TimerInstance, &lv_time );

       printf("Compression to %4.2f percent for %d bytes at rate of %8.3fMB/s\n",
             (lv_comp_size*100.0)/lv_uncomp_size,lv_uncomp_size,((float)lv_uncomp_size*clock_cycles)/(lv_time*1000.0));
       
       total_comp_cycles    += lv_time;
       total_comp_read      += lv_uncomp_size;
       total_comp_size      += lv_comp_size;

       /* uncompressed size equals decompressed size */
       lv_decomp_size = lv_uncomp_size;
       
       /* reset timer */
       timSetTimerValue(TimerInstance, 0);
       
       /* decompress compressed memory */
       if(lv_ret = uncompress(decomp_mem, &lv_decomp_size, comp_mem, lv_comp_size) != Z_OK){
          printf("failed uncompress data %d %d %d %s\n",lv_ret,lv_decomp_size,lv_comp_size,f);
          close (fid_norm);
          return;
       }
   
       /* get timer */
       timGetTimerValue(TimerInstance, &lv_time );

       printf("Uncompression of %d bytes at rate of %8.3fMB/s\n\n",
             lv_comp_size,((float)lv_uncomp_size*clock_cycles)/(lv_time*1000.0));


       total_uncomp_cycles    += lv_time;
       total_uncomp_read      += lv_uncomp_size;

       /* compare all of the the uncompressed and decompressed data */
       if((lv_ret = memcmp(decomp_mem,uncomp_mem,lv_uncomp_size))!= 0){
          printf("uncompressed data's have different data %d\n",lv_ret);
          close (fid_norm);
          return;
       }
       
       free(uncomp_mem);
       free(decomp_mem);
       free(comp_mem);
       
       close(fid_norm);
    }
}

void treewalk(String f)
{
   DIR *dirp;
   struct dirent *direntp;

   dirp = opendir(f);

   if (dirp) {
       while ( (direntp = readdir( dirp )) != NULL ) {
	   Char buffer[1000];
	   sprintf(buffer,"%s/%s",f,direntp->d_name);
           print_file_info(buffer);
           if (strcmp(direntp->d_name,".") != 0 
                 && strcmp(direntp->d_name,"..") != 0) {
	      treewalk(buffer);
           }
       }
       (void)closedir( dirp );
   }
}

void comp_dir() { treewalk("/flash"); }

/*------------------------------ Main program --------------------------------*/

Int main()
{  
   timInstanceSetup_t setup;
   UInt32 one_ms_in_nano_seconds = 1000000;

   printf("-------------  APPLICATION STARTED  ------------\n");

   timToCycles(one_ms_in_nano_seconds,&clock_cycles);

   setup.source   = timCLOCK;
   setup.prescale = 0;
   setup.modulus = 0xFFFFFFFF;
   setup.handler = 0;
   setup.running = True;
   
   if(timOpen (&TimerInstance) != 0)
      printf("error with timOpen");;
   if(timInstanceSetup(TimerInstance, &setup) != 0)
      printf("error with timInstanceSetup");

   /* restart timer */
   timStart(TimerInstance);
   
   comp_dir();

   timStop(TimerInstance );
   timClose(TimerInstance);

   if( total_comp_read != 0 && total_comp_cycles != 0)
      printf("Average Compression Speed %8.3fMB/s\n", ((float)total_comp_read*clock_cycles)/((float)total_comp_cycles*1000.0));
   if( total_uncomp_read != 0 && total_uncomp_cycles )
      printf("Average Decompression Speed %8.3fMB/s\n", ((float)total_uncomp_read*clock_cycles)/((float)total_uncomp_cycles*1000.0));
   if( total_uncomp_read != 0 && total_comp_size )
      printf("Average Compression  %8.3f\n", (total_comp_size*100.0)/total_uncomp_read);

   printf("-------------SUCCESSFULL TERMINATION ------------\n");
   
   exit (0);
}

⌨️ 快捷键说明

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