home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Da Capo
/
da_capo_vol1.bin
/
programs
/
amiga
/
misc
/
mpegaudio
/
encode.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-21
|
53KB
|
1,417 lines
/**********************************************************************
Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
encode.c
**********************************************************************/
/**********************************************************************
* MPEG/audio coding/decoding software, work in progress *
* NOT for public distribution until verified and approved by the *
* MPEG/audio committee. For further information, please contact *
* Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com *
* *
* VERSION 3.9t *
* changes made since last update: *
* date programmers comment *
* 3/01/91 Douglas Wong, start of version 1.1 records *
* Davis Pan *
* 3/06/91 Douglas Wong rename: setup.h to endef.h *
* efilter to enfilter *
* ewindow to enwindow *
* integrated "quantizer", "scalefactor",*
* and "transmission" files *
* update routine "window_subband" *
* 3/31/91 Bill Aspromonte replaced read_filter by *
* create_an_filter *
* 5/10/91 W. Joseph Carter Ported to Macintosh and Unix. *
* Incorporated Jean-Georges Fritsch's *
* "bitstream.c" package. *
* Incorporated Bill Aspromonte's *
* filterbank coefficient matrix *
* calculation routines and added *
* roundoff to coincide with specs. *
* Modified to strictly adhere to *
* encoded bitstream specs, including *
* "Berlin changes". *
* Modified PCM sound file handling to *
* process all incoming samples and fill *
* out last encoded frame with zeros *
* (silence) if needed. *
* Located and fixed numerous software *
* bugs and table data errors. *
* 19jun91 dpwe (Aware) moved "alloc_*" reader to common.c *
* Globals sblimit, alloc replaced by new*
* struct 'frame_params' passed as arg. *
* Added JOINT STEREO coding, layers I,II*
* Affects: *_bit_allocation, *
* subband_quantization, encode_bit_alloc*
* sample_encoding *
* 6/10/91 Earle Jennings modified II_subband_quantization to *
* resolve type cast problem for MS_DOS *
* 6/11/91 Earle Jennings modified to avoid overflow on MS_DOS *
* in routine filter_subband *
* 7/10/91 Earle Jennings port to MsDos from MacIntosh version *
* 8/ 8/91 Jens Spille Change for MS-C6.00 *
*10/ 1/91 S.I. Sudharsanan, Ported to IBM AIX platform. *
* Don H. Lee, *
* Peter W. Farrett *
*10/ 3/91 Don H. Lee implemented CRC-16 error protection *
* newly introduced function encode_CRC *
*11/ 8/91 Kathy Wang Documentation of code *
* All variablenames are referred to *
* with surrounding pound (#) signs *
* 2/11/92 W. Joseph Carter Ported new code to Macintosh. Most *
* important fixes involved changing *
* 16-bit ints to long or unsigned in *
* bit alloc routines for quant of 65535 *
* and passing proper function args. *
* Removed "Other Joint Stereo" option *
* and made bitrate be total channel *
* bitrate, irrespective of the mode. *
* Fixed many small bugs & reorganized. *
* 6/16/92 Shaun Astarabadi Changed I_scale_factor_calc() and *
* II_scale_factor_calc() to use scale *
* factor 0 thru 62 only and not to *
* encode index 63 into the bit stream. *
* 7/27/92 Mike Li (re-)Port to MS-DOS *
* 9/22/92 jddevine@aware.com Fixed _scale_factor_calc() defs *
* 3/31/93 Giogio Dimino changed II_a_bit_allocation() from: *
* if( ad > ...) to if(ad >= ...) *
* 8/05/93 TEST changed I_a_bit_allocation() from: *
* if( ad > ...) to if(ad >= ...) *
**********************************************************************/
#include "common.h"
#include "encoder.h"
#ifdef MS_DOS
extern unsigned _stklen = 16384;
#endif
/*=======================================================================\
| |
| This segment contains all the core routines of the encoder, |
| except for the psychoacoustic models. |
| |
| The user can select either one of the two psychoacoustic |
| models. Model I is a simple tonal and noise masking threshold |
| generator, and Model II is a more sophisticated cochlear masking |
| threshold generator. Model I is recommended for lower complexity |
| applications whereas Model II gives better subjective quality at low |
| bit rates. |
| |
| Layers I and II of mono, stereo, and joint stereo modes are supported.|
| Routines associated with a given layer are prefixed by "I_" for layer |
| 1 and "II_" for layer 2. |
\=======================================================================*/
/************************************************************************/
/*
/* read_samples()
/*
/* PURPOSE: reads the PCM samples from a file to the buffer
/*
/* SEMANTICS:
/* Reads #samples_read# number of shorts from #musicin# filepointer
/* into #sample_buffer[]#. Returns the number of samples read.
/*
/************************************************************************/
unsigned long read_samples(musicin, sample_buffer, num_samples, frame_size)
FILE *musicin;
short sample_buffer[2304];
unsigned long num_samples, frame_size;
{
unsigned long samples_read;
static unsigned long samples_to_read;
static char init = TRUE;
if (init) {
samples_to_read = num_samples;
init = FALSE;
}
if (samples_to_read >= frame_size)
samples_read = frame_size;
else
samples_read = samples_to_read;
if ((samples_read =
fread(sample_buffer, sizeof(short), (int)samples_read, musicin)) == 0)
printf("Hit end of audio data\n");
samples_to_read -= samples_read;
if (samples_read < frame_size && samples_read > 0) {
printf("Insufficient PCM input for one frame - fillout with zeros\n");
for (; samples_