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

📄 rgl_encode_v1.c

📁 经典的音频G711和G729编码和解码源代码,G711是纯C源代码,G279为静态链接库,里面有PDF格式的开发文档
💻 C
📖 第 1 页 / 共 2 页
字号:
// rgl_encode.c
// Copyright (c) 2003 Cisco Systems Inc.
// Author: M. A. Ramalho, Cisco Systems, Sarasota, FL 34238
// Last Revision: January 28, 2003
// Version 1.0.0
//
// Main program: "rgl_compress" will compress a G711 encoded mu-law or
// A-law input file to "Ramalho G711 Lossless" (RGL) format.
//
// The input file must have extension ".al" (e.g., foo.al) for A-law
// input or extension ".mu" (e.g., foo.mu) for mu-law input. The
// output files will have extension ".rla" for A-law compression and
// ".rlm" for mu-law compression, depending if the input file was
// A-law or mu-law encoded.
//
// The program is called as:
// rgl_encode [input_file_name] [input_frame_size]
//
// The input file must as specified above. The frame size is entered
// in bytes. The valid range for the frame size is from 1 byte (125
// nanoseconds) to 2000 (250 milliseconds).  For VoIP or VoFR
// applications, typical values are multiples of 10 msec (80 byte)
// frames. For ATM payload frame size are sized dependent on the
// number of payload bytes in a cell, which is dependent on the AAL
// type used.
//
// If the input file name isn't given, the program prompts for it
// (at the console) and the frame size.
//
// If the input file name is provided, but the frame size is not
// specified, the default frame size of 80 bytes is used (a message
// is sent to the console to remind the user of this fact).
//
// This program encodes in frame size increments of G711 encoded data.
// If the input file does not contain an interger multiple of frame
// sized samples, the program does not "RGL" encode the bytes after
// the last full frame size of input data. For this case, a warning
// is printed to the console and lists the number of input samples
// not encoded. If the input file does contain an integer multiple
// of frame sized input samples, the program silently encodes them.
//
// If the input file name and a valid frame size (e.g., between 1
// and 2000, inclusive) is provided, the program uses the frame
// size provided over the default frame size.
//
//
/************************VOVIDA LICENSE******************************/
//
//The Vovida Software License, Version 1.0 
//Copyright (c) 2002 Vovida Networks, Inc.  All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
//1. Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//2. Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in
//   the documentation and/or other materials provided with the
//   distribution.
//
//3. The names "VOCAL", "Vovida Open Communication Application Library",
//   and "Vovida Open Communication Application Library (VOCAL)" must
//   not be used to endorse or promote products derived from this
//   software without prior written permission. For written
//   permission, please contact vocal@vovida.org.
//
//4. Products derived from this software may not be called "VOCAL", nor
//   may "VOCAL" appear in their name, without prior written
//   permission.
//
//THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
//NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
//NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DAMAGES
//IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
//EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
//OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//DAMAGE.
//
/************************VOVIDA LICENSE******************************/
//
//

#include <stdlib.h>
#include <stdio.h>

//
//Some elementary definitions
//

#define false 0
#define true  1
#define error(msg) { fprintf (stderr, "%s\n", msg); exit (1); }
#define max_frame_size 2000   /* 1/4 second frame, a reasonable max */
#define default_frame_size 80

typedef unsigned char uint8;

// Prototype for RGL_Compress function used later (placed here instead
// of a header file for compile simplicity)
int RGL_Compress (int mu_or_a, uint8 *input_frame, int input_frame_size,
                  uint8 *output_frame, int output_frame_size);

int main (int argc, char *argv[])
{
char name_buffer [80];
char *input_file_name;
char output_file_name [80];

FILE *input_file, *output_file;

float compression_factor;
int i, number_read, number_written;
int frame_counter = 1;
int sum_input_frame_size  = 0;
int sum_output_frame_size = 0;
int output_frame_size = default_frame_size;
int input_frame_size = default_frame_size;     /* init with default */
int mu_or_a;       /* mu_or_a = 1 or 3 for mu-law, 0 or 2 for A-law */
uint8 input_frame[max_frame_size];     /* make max size input array */
uint8 output_frame[(max_frame_size+1)];   /* max possible out_frame */
                                              /* is one byte larger */
// get base file name
switch (argc) {
case 1:
printf(" \n Ramalho G.711 Lossless Coding: mu-law or A-law version\n");
printf(" with first difference encoding. \n");
printf(" Copyright (c) 2002 Cisco Systems Inc. \n \n");
printf(" Enter input file name to compress (including extension): ");
fflush (stdout);
scanf ("%s", name_buffer);
input_file_name = name_buffer;
printf (" Enter the frame size in bytes: ");
fflush (stdout);
scanf ("%d", &input_frame_size);
break;
case 2:
input_file_name = argv [1];
input_frame_size = default_frame_size;
printf ("\n NOTE: Using default input_frame_size of 80 bytes \n\n ");
break;
case 3:
input_file_name = argv [1];
input_frame_size = atoi(argv [2]);
if ( (input_frame_size > max_frame_size) || (input_frame_size < 1) ) {
error(" \n Error: input frame size must be between 1 and 2000 bytes");
}
break;
default:
error (
"\n Error: rgl_encode presented with unexpected number of arguments");
break;
}

// make sure input file has a ".mu" or "al" extension
i = 0;
while ((input_file_name[i] != 0)&&(input_file_name[++i] != '.')) i=i;
if ((input_file_name[++i] == 'a')||(input_file_name[++i] == 'l')) { 
mu_or_a = 0;                    /* an A-law compression is required */
}
else if ((input_file_name[i--] == 'u')||(input_file_name[i] == 'm')) { 
mu_or_a = 1;                    /* a mu-law compression is required */
}
else {
error(
"\n Error: Input file name must have a \".mu\" or \".al\" extension");
}

// print space to console
(void) fprintf(stdout, "\n");

// create output file name
//(with extension "rlm" (mu-law) or "rla" (A-law))
i = 0;
while (input_file_name [i] && (input_file_name [i] != '.')) {
output_file_name [i] = input_file_name [i]; i++;
}
output_file_name [i++] = '.'; output_file_name [i++] = 'r';
output_file_name [i++] = 'l';
if (mu_or_a == 1) {
output_file_name [i++] = 'm'; output_file_name [i++] = 0;
}
else {
output_file_name [i++] = 'a'; output_file_name [i++] = 0;
}

// prepare files for reading and writing
input_file = fopen (input_file_name, "rb");
if (input_file == NULL) error ("can't open input file for reading");
output_file = fopen (output_file_name, "wb");
if (output_file == NULL) error ("can't open output file for writing");

// write to console
(void) fprintf(stdout, "Processing file (w/fd): %s \n",
                                                    output_file_name);

// read input file (note if last read chunk =! input_frame_size)
number_read = (int) fread (input_frame, sizeof(uint8),
                           input_frame_size, input_file);
while (number_read > 0){

// Compress input_frame array
// (compressed output_frame array has length "output_frame_size")
output_frame_size = RGL_Compress (mu_or_a, &input_frame[0],
             input_frame_size, &output_frame[0], (max_frame_size+1));

// If RGL_Compress returned a negative "output_frame_size", an
// error occurred in the RGL_Compress function
if (output_frame_size == -1 ){
error("\n \"RGL_Compress\": Parameter \"mu_or_a\" not as expected \n");
}
if (output_frame_size < -1 ){
error("\n Error In function \"RGL_Compress\": Program terminating\n");
}

// write compressed data to output file
number_written = (int) fwrite (output_frame, sizeof(uint8),
                               output_frame_size, output_file);

// write compression factor to console
compression_factor =
              ((float) output_frame_size)/ ((float) input_frame_size);
(void) fprintf(stdout, "Compression factor for frame %d was:    %f\n",
               frame_counter, compression_factor);

frame_counter++;
sum_output_frame_size = output_frame_size + sum_output_frame_size;
sum_input_frame_size  = input_frame_size  + sum_input_frame_size;

// read next input_frame_size of data to encode
number_read = (int) fread (input_frame, sizeof(uint8),
                           input_frame_size, input_file);

// if less than a input_frame_size number of samples is read, do not
// encode the remaining samples
if ( ( number_read !=0 ) && (number_read < input_frame_size) ) {
(void) fprintf(stdout,
        "Warning: Input file did not contain an integer number of\n");
(void) fprintf(stdout,
  "frame size samples - last %d bytes not encoded \n\n", number_read);
number_read = 0;        /* fake out the while statement to end loop */
}
}

// write compression factor for all frames to console
compression_factor =
    ( (float) sum_output_frame_size) / ((float) sum_input_frame_size);
(void) fprintf(stdout,
 "Compression factor for all frames was: %f \n\n",compression_factor);
compression_factor = 100 * (1 - compression_factor);
(void) fprintf(stdout,
  "Compression for all frames was %f percent \n", compression_factor);

// tidy up
fclose(input_file);
fclose(output_file);
return(0);                                   /* end of main program */
}

// "RGL Compression" function (with reserved overhead codes)
// Copyright (c) 2003 Cisco Systems Inc.
// Author: M. A. Ramalho, Cisco Systems, Sarasota, FL 34238
// Last Revision: January 28, 2003
// Version 1.0.0
//
// This function has been designed so that it will be easily portable
// to DSP code by means of standard C language cross-compliers.
//
// This function takes an "input_frame_size" sized array of G711
// encoded bytes and compresses them to an "output_frame_size" sized
// array of bytes of "rgl formatted" output (see "RGL Codec"

⌨️ 快捷键说明

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