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

📄 common.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 5 页
字号:
#ifdef TABLES_PATH
       strcpy(fulname, TABLES_PATH);   /* default relative path for tables */
#endif /* TABLES_PATH */          /* (includes terminal path seperator */

#ifdef UNIX                       /* envir. variables for UNIX only */
       {
        char *getenv(const char *);

        envdir = getenv(MPEGTABENV);   /* check for environment */
        if(envdir != NULL)
            strcpy(fulname, envdir);
        strcat(fulname, PATH_SEPARATOR);  /* add a "/" on the end */
      }
#endif /* UNIX */

    strcat(fulname, name);
    if( (f=fopen(fulname,"r"))==NULL ) {
        fprintf(stderr,"OpenTable: could not find %s\n", fulname);

#ifdef UNIX
          if(envdir != NULL)
            fprintf(stderr,"Check %s directory '%s'\n",MPEGTABENV, envdir);
          else
            fprintf(stderr,"Check local directory './%s' or setenv %s\n",
                    TABLES_PATH, MPEGTABENV);
#else /* not unix : no environment variables */

#ifdef TABLES_PATH
            fprintf(stderr,"Check local directory './%s'\n",TABLES_PATH);
#endif /* TABLES_PATH */

#endif /* UNIX */

    }
    return f;
}

/***********************************************************************
/*
/* Read one of the data files ("alloc_*") specifying the bit allocation/
/* quatization parameters for each subband in layer II encoding
/*
/**********************************************************************/

int read_bit_alloc(int table, al_table (*alloc))        /* read in table, return # subbands */
          
                
{
        long a, b, c, d, i, j;
        FILE *fp;
        char name[16], t[80];
        int sblim;

        strcpy(name, "alloc_0");

        switch (table) {
                case 0 : name[6] = '0';         break;
                case 1 : name[6] = '1';         break;
                case 2 : name[6] = '2';         break;
                case 3 : name[6] = '3';         break;
                case 4 : name[6] = '4';         break; /* for MultiLingual LSF , WtK 7/8/95 */
                default : name[6] = '0';
        }

        if (!(fp = OpenTableFile(name))) {
                printf("Please check bit allocation table %s\n", name);
                exit(0);
        }

#ifdef	PrintBitDebug
	printf("using bit allocation table %s\n", name);
#endif

        fgets(t, 80, fp);
        sscanf(t, "%d\n", &sblim);
        while (!feof(fp)) {
                fgets(t, 80, fp);
                sscanf(t, "%ld %ld %ld %ld %ld %ld\n", &i, &j, &a, &b, &c, &d);
                        (*alloc)[i][j].steps = a;
                        (*alloc)[i][j].bits  = b;
                        (*alloc)[i][j].group = c;
                        (*alloc)[i][j].quant = d;
        }
        fclose(fp);
        return sblim;
}

/***********************************************************************
/*
/* Using the decoded info the appropriate possible quantization per
/* subband table is loaded
/*
/**********************************************************************/

int pick_table (frame_params *fr_ps) /* choose table, load if necess, return # sb's */
{
    int table, tableml, lay, ws, bsp, br_per_ch, sfrq;
    int sblim = fr_ps->sblimit;     /* return current value if no load */

    lay = fr_ps->header->lay - 1;
    bsp = fr_ps->header->bitrate_index;

    /* Bug corrected by SWKim060695 */
    br_per_ch = bitrate[lay][bsp] / (fr_ps->stereo /* + fr_ps->stereomc */);
    ws = fr_ps->header->sampling_frequency;
    sfrq = s_freq[ws];

    /* decision rules refer to per-channel bitrates (kbits/sec/chan) */
    if ((sfrq == 48 && br_per_ch >= 56) ||
	(br_per_ch >= 56 && br_per_ch <= 80))
	table = 0;
    else if (sfrq != 48 && br_per_ch >= 96)
	table = 1;
    else if (sfrq != 32 && br_per_ch <= 48)
	table = 2;
    else
	table = 3;

    if (fr_ps->tab_num != table)
    {
	if (fr_ps->tab_num >= 0)
	    mem_free ((void **) &(fr_ps->alloc));
	fr_ps->alloc = (al_table *) mem_alloc (sizeof (al_table), "alloc");
	sblim = read_bit_alloc (fr_ps->tab_num = table, fr_ps->alloc);
    }

    if (verbosity >= 2)
	printf("sblim = %d,  table = %d,  br_per_ch = %d\n", sblim, table, br_per_ch);

    return (sblim);
}

/* choose MC and ML tables, load if necess, return # sb's */
void mc_pick_table (frame_params *fr_ps)   
{
   int tablemc, tableml, ws, sfrq;

   /* BUG corrected SWKim060695 */
   ws = fr_ps->header->sampling_frequency;
   sfrq = s_freq[ws];

   /* decision rules refer to sampling frequency */
   if (sfrq == 48)
      tablemc = 0;
   else
      tablemc = 1;

   if (fr_ps->tab_num_mc != tablemc)
   {
      if (fr_ps->tab_num >= 0)
	 mem_free ((void **)& (fr_ps->alloc_mc));
      fr_ps->alloc_mc = (al_table *) mem_alloc (sizeof(al_table), "alloc_mc");
      fr_ps->sblimit_mc = read_bit_alloc (fr_ps->tab_num_mc = tablemc, fr_ps->alloc_mc);
   }


    if (fr_ps->header->multiling_ch > 0)
    {
	/* LSF MultiLingual 7/8/95 WtK */
	if (fr_ps->header->multiling_fs == 0)
	{
	    fr_ps->tab_num_ml = fr_ps->tab_num_mc;
	    fr_ps->alloc_ml   = fr_ps->alloc_mc;
	    fr_ps->sblimit_ml = fr_ps->sblimit_mc;
	}
	else
	{
	    tableml = 4;
	    if (fr_ps->tab_num_ml != tableml)
	    {
		if (fr_ps->tab_num_ml >= 0)
		    mem_free ((void **) &(fr_ps->alloc_ml));
		fr_ps->alloc_ml = (al_table *) mem_alloc (sizeof (al_table), "alloc_ml");
		fr_ps->sblimit_ml = read_bit_alloc (fr_ps->tab_num_ml = tableml, fr_ps->alloc_ml);
	    }
	} /* ml_fs==0 */
    } /* ml_ch>0 */

    if (verbosity >= 2)
    {
	printf("Multchannel uses : sblim = %d,  table = %d\n", fr_ps->sblimit_mc, fr_ps->tab_num_mc);
	if (fr_ps->header->multiling_ch > 0) 
	    printf ("Multilingual uses : sblim = %d, table = %d\n", fr_ps->sblimit_ml, fr_ps->tab_num_ml);
    }
}

int js_bound (int lay, int m_ext)
{
    static int jsb_table[3][4] =  { { 4, 8, 12, 16 }, { 4, 8, 12, 16},
				    { 0, 4, 8, 16} };  /* lay+m_e -> jsbound */

    if (lay < 1 || lay > 3 || m_ext < 0 || m_ext > 3)
    {
        fprintf (stderr, "js_bound bad layer/modext (%d/%d)\n", lay, m_ext);
        exit (1);
    }

    return (jsb_table[lay-1][m_ext]);
}

void hdr_to_frps (frame_params *fr_ps) /* interpret data in hdr str to fields in fr_ps */
{
    layer *hdr = fr_ps->header;     /* (or pass in as arg?) */

    fr_ps->actual_mode = hdr->mode;
    if (hdr->mode != MPG_MD_NONE)
	fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2;
    else
    	fr_ps->stereo = 0;

    if (verbosity >= 2)
        printf ("stereo = %d stereomc = %d stereoaug = %d\n",
		fr_ps->stereo, fr_ps->stereomc, fr_ps->stereoaug);

    if (hdr->lay == 2)
	fr_ps->sblimit = pick_table (fr_ps);
    else
	fr_ps->sblimit = SBLIMIT;

    if (hdr->mode == MPG_MD_JOINT_STEREO)
	fr_ps->jsbound = js_bound (hdr->lay, hdr->mode_ext);
    else
	fr_ps->jsbound = fr_ps->sblimit;

    if (hdr->multiling_ch > 0 && hdr->multiling_lay > 0)
    {
	fprintf (stderr, "MultiLingual not in Layer 2!\n exit.\n");
	exit (1);
    }

    if (fr_ps->stereomc > 0 || hdr->lfe || hdr->multiling_ch > 0)
		mc_pick_table (fr_ps);
}

int BitrateIndex(int layr, int bRate)   /* convert bitrate in kbps to index */
                        /* 1 or 2 */
                        /* legal rates from 32 to 448 */
{
int     index = 0;
int     found = 0;

    while(!found && index<15)   {
        if(bitrate[layr-1][index] == bRate)
            found = 1;
        else
            ++index;
    }
    if(found)
        return(index);
    else {
        fprintf(stderr, "BitrateIndex: %d (layer %d) is not a legal bitrate\n",
                bRate, layr);
        return(-1);     /* Error! */
    }
}

int SmpFrqIndex(long int sRate)  /* convert samp frq in Hz to index */
/* for MultiLingual LSF                                                       */
/* Note this function differs from the one called upon MPEG2 Audio-LSF coding */
/* The value '16' is used to detect half sample rate of ML wrt. MC            */
/* 7/8/95 WtK                                                                 */
{
    switch (sRate) {
      case    44100    :   return( 0);     break;
      case    48000    :   return( 1);     break;
      case    32000    :   return( 2);     break;
      case    22050    :   return(16);     break;
      case    24000    :   return(17);     break;
      case    16000    :   return(18);     break;
      default          :   fprintf(stderr, "SmpFrqIndex: %ld is not a legal sample rate\n", sRate);
                           return(-1);     /* Error! */
    }
}

/*******************************************************************************
*
*  Allocate number of bytes of memory equal to "block".
*
*******************************************************************************/

void *mem_alloc (long unsigned int block, char *item)
{
#ifndef MSDOS
    void *ptr;
#else
    void _far *ptr;
#endif

#ifdef MACINTOSH
    ptr = NewPtr (block);
#endif

#ifdef MSDOS
    ptr = (void _far *) _fmalloc ((unsigned int) block); /* far memory, 92-07-08 sr */
#endif

#if ! defined (MACINTOSH) && ! defined (MSDOS)
    ptr = (void *) malloc (block);
#endif

    if (ptr != NULL)
    {
#ifdef MSDOS
	_fmemset (ptr, 0, (unsigned int) block); /* far memory, 92-07-08 sr */
#else
        memset (ptr, 0, block);
#endif
    }
    else
    {
        printf ("Unable to allocate %s\n", item);
        exit (1);
    }

    return (ptr);
}

/****************************************************************************
*
*  Free memory pointed to by "*ptr_addr".
*
*****************************************************************************/

void    mem_free (void **ptr_addr)
{
    if (*ptr_addr != NULL)
    {
#ifdef  MACINTOSH
	DisposPtr (*ptr_addr);
#else
	free (*ptr_addr);
#endif
	*ptr_addr = NULL;
    }
}

/****************************************************************************
*
*  Routines to convert between the Apple SANE extended floating point format
*  and the IEEE double precision floating point format.  These routines are
*  called from within the Audio Interchange File Format (AIFF) routines.
*
*****************************************************************************/

/*
*** Apple's 80-bit SANE extended has the following format:

 1       15      1            63
+-+-------------+-+-----------------------------+
|s|       e     |i|            f                |
+-+-------------+-+-----------------------------+
  msb        lsb   msb                       lsb

The value v of the number is determined by these fields as follows:
If 0 <= e < 32767,              then v = (-1)^s * 2^(e-16383) * (i.f).
If e == 32767 and f == 0,       then v = (-1)^s * (infinity), regardless of i.
If e == 32767 and f != 0,       then v is a NaN, regardless of i.

*** IEEE Draft Standard 754 Double Precision has the following format:

MSB
+-+---------+-----------------------------+
|1| 11 Bits |           52 Bits           |
+-+---------+-----------------------------+
 ^     ^                ^
 |     |                |
 Sign  Exponent         Mantissa
*/

/*****************************************************************************
*
*  double_to_extended()
*
*  Purpose:     Convert from IEEE double precision format to SANE extended
*               format.
*
*  Passed:      Pointer to the double precision number and a pointer to what
*               will hold the Apple SANE extended format value.
*
*  Outputs:     The SANE extended format pointer will be filled with the
*               converted value.
*
*  Returned:    Nothing.
*
*****************************************************************************/


void    double_to_extended(double *pd, char *ps)
{

#ifdef  MACINTOSH

        x96tox80(pd, (extended *) ps);

#else

/* fixed bus alignment error, HP 27-may-93 */

register unsigned long  top2bits;

register unsigned short *ps2;
register IEEE_DBL       *p_dbl;

⌨️ 快捷键说明

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