📄 encode.c
字号:
/**********************************************************************
* ISO MPEG Audio Subgroup Software Simulation Group (1996)
* ISO 13818-3 MPEG-2 Audio Multichannel Encoder
*
* $Id: encode.c 1.9 1996/02/12 07:13:35 rowlands Exp $
*
* $Log: encode.c $
* Revision 1.9 1996/02/12 07:13:35 rowlands
* Release following Munich meeting
*
* Revision 1.6.2.1 1995/11/06 04:19:12 rowlands
* Received from Uwe Felderhoff (IRT)
*
* Revision 1.8 1995/08/14 07:53:28 tenkate
* ML-LSF added Warner ten Kate 7/8/95 (Philips)
* variables "alloc" and "sblimit" changed into "alloc_ml" and
* "sblimit_ml" where appropriate.
* sample loops adapted to 12 or 6 depending on full or half fs.
* II_a_bit_allocation separate summing for ML.
*
* Revision 1.7 1995/07/31 07:48:55 tenkate
* addition of phantom coding, in void matricing(), 25/07/95 WtK
*
* Revision 1.4.3.1 1995/06/16 08:25:11 rowlands
* Input from Sang Wook Kim (Samsung AIT)
*
* Revision 1.4.2.1 1995/06/16 03:46:42 rowlands
* Input from Susanne Ritscher (IRT)
*
* Revision 1.4.1.6 1995/06/16 02:41:51 rowlands
* Added dematrix procedure 2, corrected dematrixing values.
*
* Added support for dematrix procedure 2, without predistortion.
* To implement predistortion will require a delay in the encoder
* to be able to calculate the predistorted filtered surround signal
* subband samples.
*
* Modified procedure matricing() to reuse the compatible stereo
* channel signals computed by matricing_fft(). This was needed to
* avoid calculating the low-pass-filtered surround signal in the
* subband domain. The main function of procedure matricing() now
* is to weight the multichannels. These channels are unweighted
* prior to this for the benefit of the psycho model.
*
* Corrected weighting values for dematrixing procedures.
* Deleted matrixing on multilingual channels.
* In the quantization procedures, made quantizing to zero bits
* a special case to skip some operations on undefined values.
*
**********************************************************************/
/**********************************************************************
* 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. *
* 92-08-11 Soren H. Nielsen Fixed bug: allocation of space in the *
* bitstream for the CRC-word. Fixed *
* reading of window from file. *
* 92-11-06 Soren H. Nielsen Fixed scalefactor calculation. *
**********************************************************************
* *
* *
* MPEG/audio Phase 2 coding/decoding multichannel *
* *
* 7/27/93 Susanne Ritscher, IRT Munich *
* 8/10/93 changed matricing to 7 channels *
* added void matricing_fft *
* 8/12/93 added int required_bits, *
* int max_alloc *
* implemented the new mc_header (third working draft)*
* 8/13/93 added channel-switching in required_bits and *
* II_subband_quantisation, II_encode_scale, *
* II_encode_bit_alloc, II_encode_sample, *
* encode_info *
* all channels normalized *
* 9/20/93 channel-switching is only performed at a *
* certain limit of TC_ALLOC dB, which is included *
* in encoder.h *
* 1/04/94 get out some rubbish *
* *
* 01/05/94 implemented the Committee Draft header *
* *
* 01/12/94 changed matricing procedure according to *
* Committee Draft *
* *
* Version 1.0 *
* *
* 07/12/94 Susanne Ritscher, IRT Munich *
* Tel: +49 89 32399 458 *
* Fax: +49 89 32399 415 *
* *
* Version 1.1 *
* *
* 02/23/95 Susanne Ritscher, IRT Munich *
* corrected some bugs *
* extension bitstream is working *
* *
* Version 2.0 *
* *
* 01/28/97 Frans de Bont, Philips Sound & Vision, Eindhoven *
* - dynamic crosstalk working for all configurations*
* - prediction working for all configurations *
* - extension bitstream fixed *
* - fully compliant to DIS 13818-3.2 *
* * *
**********************************************************************/
/**********************************************************************
* *
* 06/06/95 Yeon Bae Thomas Kim, Samsung AIT *
* ancillary data is working *
* *
* 06/06/95 Sang Wook Kim, Samsung AIT *
* corrected some bugs *
* *
**********************************************************************/
#define VERY_FAST_FILTER 1 /* JMZ 08/03/1995 FILTER */
#include "common.h"
#include "encoder.h"
/*=======================================================================\
| |
| 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(FILE *musicin, long int *sample_buffer, long unsigned int num_samples, long unsigned int frame_size, int
*byte_per_sample, int *aiff)
{
unsigned long samples_read;
static unsigned long samples_to_read;
static char init = TRUE;
short pcm_sample_buffer[9216]; /*for correct reading of pcm-data*/
int i;
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((*aiff==1) &&(*byte_per_sample !=2)){
if ((samples_read =
fread(sample_buffer, *byte_per_sample, (int)samples_read, musicin)) == 0)
if (verbosity >= 2) printf("Hit end of audio data\n");
}
else{
if ((samples_read =
fread(pcm_sample_buffer, sizeof(short), (int)samples_read, musicin)) == 0)
if (verbosity >= 2) printf("Hit end of audio data\n");
for(i = 0; i < 9216; ++i) sample_buffer[i] = pcm_sample_buffer[i];
}
samples_to_read -= samples_read;
if (samples_read < frame_size && samples_read > 0) {
if (verbosity >= 2) printf("Insufficient PCM input for one frame - fillout with zeros\n");
for (; samples_read < frame_size; sample_buffer[samples_read++] = 0);
samples_to_read = 0;
}
return(samples_read);
}
/************************************************************************/
/*
/* get_audio()
/*
/* PURPOSE: reads a frame of audio data from a file to the buffer,
/* aligns the data for future processing, and separates the
/* left and right channels
/*
/* SEMANTICS:
/* Calls read_samples() to read a frame of audio data from filepointer
/* #musicin# to #insampl[]#. The data is shifted to make sure the data
/* is centered for the 1024pt window to be used by the psychoacoustic model,
/* and to compensate for the 256 sample delay from the filter bank. For
/* stereo, the channels are also demultiplexed into #buffer[0][]# and
/* #buffer[1][]#
/*
/* 21/03/1995 JMZ Multimode adaptations
/************************************************************************/
unsigned long
get_audio (
FILE *musicin,
double (*buffer)[1152],
long unsigned int num_samples,
int stereo,
IFF_AIFF *aiff_ptr,
int stereomc,
int stereoaug,
frame_params *fr_ps,
int *aiff,
int *byte_per_sample,
double (*buffer_matr)[1152]
)
{
int k, j, i;
long insamp[9216];
unsigned long samples_read;
int lay;
int lfe;
layer *info = fr_ps->header;
lay = info->lay;
lfe = info->lfe;
if (lay == 1)
{
if (stereo == 2)
{ /* layer 1, stereo */
samples_read = read_samples (musicin, insamp, num_samples,
(unsigned long) 768, byte_per_sample, aiff);
for (j = 0; j < 448; j++)
{
if (j<64)
{
buffer[0][j] = buffer[0][j+384];
buffer[1][j] = buffer[1][j+384];
buffer[2][j] = 0;
buffer[3][j] = 0;
buffer[4][j] = 0;
}
else
{
buffer[0][j] = insamp[2*j-128];
buffer[1][j] = insamp[2*j-127];
buffer[2][j] = 0;
buffer[3][j] = 0;
buffer[4][j] = 0;
}
}
}
else
{ /* layer 1, mono */
samples_read = read_samples (musicin, insamp, num_samples,
(unsigned long) 384, byte_per_sample, aiff);
for (j = 0; j < 448; j++)
{
if (j < 64)
{
buffer[0][j] = buffer[0][j+384];
buffer[1][j] = 0;
buffer[2][j] = 0;
buffer[3][j] = 0;
buffer[4][j] = 0;
}
else
{
buffer[0][j] = insamp[j-64];
buffer[1][j] = 0;
buffer[2][j] = 0;
buffer[3][j] = 0;
buffer[4][j] = 0;
}
}
}
}
else
{
if (*aiff == 1)
{
k = aiff_ptr->numChannels;
samples_read = read_samples (musicin, insamp, num_samples,
(unsigned long) (k * 1152), byte_per_sample, aiff);
for (i = 0; i < k; i++)
for (j = 0; j < 1152; j++)
buffer[i][j] = insamp[k*j+i];
}
else
{ /* layerII, stereo */
if (stereo == 2)
{
samples_read = read_samples (musicin, insamp, num_samples,
(unsigned long) ((2+lfe)*1152), byte_per_sample, aiff);
for (j = 0; j < 1152; j++)
{ /* fixed bug 28.6.93 S.R. */
buffer[0][j] = insamp[(2+lfe)*j];
buffer[1][j] = insamp[(2+lfe)*j+1];
buffer[2][j] = 0;
buffer[3+lfe][j] = 0;
buffer[4+lfe][j] = 0;
if (lfe)
buffer[3][j] = insamp[(2+lfe)*j+2]; /* ########### */
}
}
else
{ /* layer 2 (or 3), mono */
samples_read = read_samples (musicin, insamp, num_samples,
(unsigned long) 1152, byte_per_sample, aiff);
for (j = 0; j < 1152; j++)
{
buffer[0][j] = insamp[j];
buffer[1][j] = 0;
buffer[2][j] = 0;
buffer[3][j] = 0;
buffer[4][j] = 0;
}
}
}
}
/*
* If LFE is not enabled, "buffer" contains:
* buffer[0] L
* buffer[1] R
* buffer[2] C
* buffer[3] Ls
* buffer[4] Rs
#ifdef Augmentation_7ch
* optional in 7.1 channel augmentation mode
* buffer[5] Lc
* buffer[6] Rc
#endif
*
* If LFE is enabled, "buffer" contains:
* buffer[0] L
* buffer[1] R
* buffer[2] C
* buffer[3] LFE
* buffer[4] Ls
* buffer[5] Rs
#ifdef Augmentation_7ch
* optional in 7.1 channel augmentation mode
* buffer[6] Lc
* buffer[7] Rc
#endif
*/
#ifdef Augmentation_7ch
if (stereoaug == 2)
matricing_aug_fft (buffer, buffer_matr, fr_ps);
else
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -