costg711.c
上传用户:zlh0619zlh
上传日期:2007-01-06
资源大小:10k
文件大小:7k
源码类别:

语音压缩

开发平台:

Unix_Linux

  1. /*
  2.  *-------------------------------------------------------------------------
  3.  *
  4.  * CCITT G.711 u-law, A-law and linear PCM conversions.
  5.  *       Version 94/12/30 - COST 232
  6.  *
  7.  * Usage :
  8.  *
  9.  * COSTG711 -au|ua|la|al|lu|ul|ll infile outfile
  10.  *
  11.  *     -au: A-law to u-law conversion
  12.  *     -ua: u-law to A-law conversion
  13.  *     -la: 16-bit linear PCM to A-law
  14.  *     -al: A-law to 16-bit linear PCM
  15.  *     -lu: 16-bit linear PCM to u-law
  16.  *     -ul: u-law to 16-bit linear PCM
  17.  *     -ll: 16-bit linear low/high byte swapping
  18.  *
  19.  *-------------------------------------------------------------------------
  20.  *
  21.  * The source code of Sun Microsystems, referenced below, has been modified by 
  22.  *
  23.  *    Boerge Lindberg
  24.  *    Center for PersonKommunikation
  25.  *    Aalborg University, Denmark
  26.  *    e-mail : bli@cpk.auc.dk
  27.  *
  28.  * on behalf of the now concluded COST 232 Project on 
  29.  *
  30.  *    "Speech Recognition over the Telephone Network".
  31.  *
  32.  * Modifications concern the addition of a main program for file processing. 
  33.  * Additonal changes:
  34.  *
  35.  * 94/02/10 : Low/high byte swapping added.
  36.  * 94/12/30 : Bugs fixed:
  37.  *
  38.  *            Functions linear2alaw, linear2ulaw have been updated to correctly
  39.  *            convert unquantized 16 bit values.
  40.  *            Tables for direct u- to A-law and A- to u-law conversions have been
  41.  *            corrected.
  42.  *
  43.  *-------------------------------------------------------------------------
  44.  */
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <stdarg.h>
  48. #include <stdlib.h>
  49. #include "g711.c"
  50. /* 16 bit swapping */
  51. short swap_linear (short pcm_val)
  52. { struct lohibyte { unsigned char lb, hb;}; 
  53.   union { struct lohibyte b;
  54.           short i;
  55.         } exchange;
  56.   unsigned char c;
  57.   
  58.   exchange.i      = pcm_val;
  59.   c               = exchange.b.hb;
  60.   exchange.b.hb   = exchange.b.lb; 
  61.   exchange.b.lb   = c;
  62.   return (exchange.i);
  63. }
  64. /*
  65.  **************************************************************************
  66.  **************************************************************************
  67. */
  68. void PrintUsage (FILE *ef)
  69. { fprintf(ef, "CCITT G.711 u-law, A-law and linear PCM conversions.n");
  70.   fprintf(ef, "          Version 94/12/30 - COST 232n");
  71.   fprintf(ef, "nUsage:nn");
  72.   fprintf(ef, "tcostg711 -au|ua|la|al|lu|ul|ll infile outfilenn");
  73.   fprintf(ef, "tt-autA-law to u-law conversionn");
  74.   fprintf(ef, "tt-uatu-law to A-law conversionn");
  75.   fprintf(ef, "tt-lat16-bit linear PCM to A-lawn");
  76.   fprintf(ef, "tt-altA-law to 16-bit linear PCMn"); 
  77.   fprintf(ef, "tt-lut16-bit linear PCM to u-lawn");
  78.   fprintf(ef, "tt-ultu-law to 16-bit linear PCMn"); 
  79.   fprintf(ef, "tt-llt16-bit linear low/high byte swappingn"); 
  80. }
  81. /*
  82.  **************************************************************************
  83.  **************************************************************************
  84. */
  85. #define BLOCK_SIZE 1024
  86. union
  87. { unsigned char in_samples_char[BLOCK_SIZE];
  88.   short in_samples_short[BLOCK_SIZE];
  89. } inbuf;
  90. union
  91. { unsigned char out_samples_char[BLOCK_SIZE];
  92.   short out_samples_short[BLOCK_SIZE];
  93. } outbuf;
  94. int main(
  95.         int                     argc,
  96.         char                    **argv)
  97. {
  98.         short                   in_size, out_size, count;
  99.         short                   (*char_short_routine)(unsigned char uval) = NULL;
  100.         short                   (*short_short_routine)(short pcm_val) = NULL;
  101.         unsigned char           (*char_char_routine)(unsigned char uval) = NULL;
  102.         unsigned char           (*short_char_routine)(short     pcm_val) = NULL;
  103.         FILE                    *infile, *outfile;
  104.         if (argc != 4) {
  105.           PrintUsage(stderr); 
  106.           return 1; /* Exit, error code 1 */
  107.         }
  108.         
  109.         out_size = sizeof (char);
  110.         in_size = sizeof (char);
  111.         switch (argv[1][1]) {
  112.         case 'u':
  113.                 switch (argv[1][2]) {
  114.                 case 'a':
  115.                         char_char_routine = ulaw2alaw;
  116.                         break;
  117.                 case 'l':
  118.                         out_size = sizeof (short);
  119.                         char_short_routine = ulaw2linear;
  120.                         break;
  121.                 }
  122.                 break;
  123.         case 'a':
  124.                 switch (argv[1][2]) {
  125.                 case 'u':
  126.                         char_char_routine = alaw2ulaw;
  127.                         break;
  128.                 case 'l':
  129.                         out_size = sizeof (short);
  130.                         char_short_routine = alaw2linear;
  131.                         break;
  132.                 }
  133.                 break;
  134.         case 'l':
  135.                 in_size = sizeof (short);
  136.                 switch (argv[1][2]) {
  137.                 case 'u':
  138.                         short_char_routine = linear2ulaw;
  139.                         break;
  140.                 case 'a':
  141.                         short_char_routine = linear2alaw;
  142.                         break;
  143.                 case 'l':
  144.                         out_size = sizeof (short);
  145.                         short_short_routine = swap_linear;
  146.                         break;
  147.                 }
  148.                 break;
  149.         default:
  150.                 PrintUsage(stderr); return 1; /* Exit, error code 1 */
  151.         }
  152.         if ((infile = fopen(argv[2],"rb")) == NULL)
  153.         { fprintf(stderr,"Cannot open input file %sn",argv[2]);
  154.           return 1; /* Exit, error code 1 */
  155.         }
  156.         
  157.         if ((outfile = fopen(argv[3],"wb")) == NULL)
  158.         { fprintf(stderr,"Cannot open output file %sn",argv[3]);
  159.           return 1; /* Exit, error code 1 */
  160.         }
  161.         
  162.         /* Read input file and process */
  163.         do
  164.         {  short i;
  165.            count = fread(&inbuf,in_size,BLOCK_SIZE,infile);
  166.            switch (out_size) {
  167.                 
  168.                 case 1: switch(in_size) {
  169.                         case 1: for (i=0; i < count; i++)
  170.                                   outbuf.out_samples_char[i] = (*char_char_routine) 
  171.                                     (inbuf.in_samples_char[i]);
  172.                                 break;
  173.                         case 2: for (i=0; i < count; i++)
  174.                                   outbuf.out_samples_char[i] = (*short_char_routine) 
  175.                                     (inbuf.in_samples_short[i]);
  176.                                 break;
  177.                         }
  178.                         break;
  179.                         
  180.                 case 2: switch(in_size) {
  181.                         case 1: for (i=0; i < count; i++)
  182.                                   outbuf.out_samples_short[i] = (*char_short_routine) 
  183.                                     (inbuf.in_samples_char[i]);
  184.                                 break;
  185.                         case 2: for (i=0; i < count; i++)
  186.                                   outbuf.out_samples_short[i] = (*short_short_routine) 
  187.                                     (inbuf.in_samples_short[i]);
  188.                                 break;
  189.                         }
  190.                         break;
  191.                         
  192.            } /* end switch */
  193.            if (fwrite(&outbuf, out_size, count, outfile) != count)
  194.              { fprintf(stderr,"Write Access Error, File=%s",argv[3]); return 1;}
  195.         } while (count == BLOCK_SIZE);
  196.         fclose(infile); 
  197.         fclose(outfile);
  198.         return 0; /* Exit, no errors */
  199. }