ENCODE.C
资源名称:mpeg.zip [点击查看]
上传用户:njqiyou
上传日期:2007-01-08
资源大小:574k
文件大小:145k
源码类别:
mpeg/mp3
开发平台:
C/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 datan");
- }
- else{
- if ((samples_read =
- fread(pcm_sample_buffer, sizeof(short), (int)samples_read, musicin)) == 0)
- if (verbosity >= 2) printf("Hit end of audio datan");
- 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 zerosn");
- 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
- matricing_fft (buffer, buffer_matr, fr_ps);
- /*
- * After matrixing, "buffer_matr" contains:
- * buffer_matr[0] Lo
- * buffer_matr[1] Ro
- * buffer_matr[2] C
- * buffer_matr[3] Ls
- * buffer_matr[4] Rs
- * buffer_matr[5] L
- * buffer_matr[6] R
- #ifdef Augmentation_7ch
- * optional in 7.1 channel augmentation mode
- * buffer_matr[7] L7
- * buffer_matr[8] R7
- * buffer_matr[9] C7
- * buffer_matr[10] Lc
- * buffer_matr[11] Rc
- #endif
- */
- return (samples_read);
- }
- /*************************************************************************
- /*
- /* matricing()
- /*
- /* The five-channel signal must be matricied to guarantee
- /* the compatibility to the stereo-decoder.
- /* There must be something of the surround information in the
- /* two front channels. In a five-channel decoder there will
- /* be a dematricing.
- /* There must be 7 channels for channel switching 8/10/93, SR
- /*
- /* Channel 5 and 6 are the not matriced signals L and R
- /*
- /* If phantom coding is used the high-frequency part of the
- /* center signal is matrixed to left and right.
- /* 27/07/95 WtK
- /* */
- /***************************************************************************/
- void normalizing (double (*sb_sample)[3][12][32], frame_params *fr_ps)
- {
- double matrNorm; /* factor for normalizing JMZ */
- double matrC; /* weighting factor for C, Phantom C */
- double matrLsRs; /* weighting factor for Ls, Rs */
- int stereo = fr_ps->stereo;
- int i, j, k, l;
- layer *info = fr_ps->header;
- switch (info->matrix)
- {
- /* Changed the factors according to International Standard */
- case 0:
- case 2: matrNorm = 1 / (1 + sqrt(2.0));
- matrC = 1 / sqrt(2.0);
- matrLsRs = 1 / sqrt(2.0);
- break;
- case 1: matrNorm = 1 / (1.5 + 0.5*sqrt(2.0));
- matrC = 1 / sqrt(2.0);
- matrLsRs = 0.5;
- break;
- case 3: matrNorm = 1;
- matrC = 1;
- matrLsRs = 1;
- break;
- }
- for (i = 0; i < stereo; i++)
- for (j = 0; j < 3; j++)
- for (l = 0; l < 12; l++)
- for (k = 0; k < SBLIMIT; k ++)
- sb_sample[i][j][l][k] *= matrNorm;
- for (j = 0; j < 3; ++j)
- for (l = 0; l < 12; l ++)
- for (k = 0; k < SBLIMIT; k ++)
- if (fr_ps->config == 320)
- {
- sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrC;
- sb_sample[3][j][l][k] = sb_sample[3][j][l][k] * matrNorm * matrLsRs;
- sb_sample[4][j][l][k] = sb_sample[4][j][l][k] * matrNorm * matrLsRs;
- }
- else if (fr_ps->config == 310)
- {
- sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrC;
- sb_sample[3][j][l][k] = sb_sample[3][j][l][k] * matrNorm * matrLsRs;
- }
- else if (fr_ps->config == 220)
- {
- sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrLsRs;
- sb_sample[3][j][l][k] = sb_sample[3][j][l][k] * matrNorm * matrLsRs;
- }
- else if (fr_ps->config == 300 || fr_ps->config == 302)
- sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrC;
- else if (fr_ps->config == 210)
- sb_sample[2][j][l][k] = sb_sample[2][j][l][k] * matrNorm * matrLsRs;
- }
- void matricing (double (*sb_sample)[3][12][32], frame_params *fr_ps)
- {
- double matrPC; /* weighting factor for Phantom C */
- double mono_surround;
- register double val;
- int i, j, k, l;
- layer *info = fr_ps->header;
- switch (info->matrix)
- {
- /* Changed the factors according to International Standard */
- case 0:
- case 1:
- case 2: matrPC = 1;
- break;
- case 3: matrPC = 1 / sqrt(2.0);
- break;
- }
- for (j = 0; j < 3; ++j)
- {
- for (l = 0; l < 12; l ++)
- {
- for (k = 0; k < SBLIMIT; k ++)
- {
- sb_sample[5][j][l][k] = sb_sample[0][j][l][k];
- sb_sample[6][j][l][k] = sb_sample[1][j][l][k];
- if (fr_ps->config == 320)
- {
- /* 960819 FdB changed matricing */
- if (info->matrix == 0 || info->matrix == 1)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k] + sb_sample[3][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[2][j][l][k] + sb_sample[4][j][l][k];
- }
- else if (info->matrix == 2)
- {
- mono_surround = (sb_sample[3][j][l][k] + sb_sample[4][j][l][k]) / 2.0;
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k] - mono_surround;
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[2][j][l][k] + mono_surround;
- }
- else
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
- }
- }
- else if (fr_ps->config == 310)
- {
- /* 960819 FdB changed matricing */
- if (info->matrix == 0 || info->matrix == 1)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k] + sb_sample[3][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[2][j][l][k] + sb_sample[3][j][l][k];
- }
- else if (info->matrix == 2)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k] - sb_sample[3][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[2][j][l][k] + sb_sample[3][j][l][k];
- }
- else
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
- }
- }
- else if (fr_ps->config == 220)
- {
- /* 960819 FdB changed matricing */
- if (info->matrix == 0 || info->matrix == 1)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[3][j][l][k];
- }
- else if (info->matrix == 3)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
- }
- }
- else if (fr_ps->config == 300 || fr_ps->config == 302)
- {
- /* 960819 FdB changed matricing */
- if (info->matrix == 0 || info->matrix == 1)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[2][j][l][k];
- }
- else if (info->matrix == 3)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
- }
- }
- else if (fr_ps->config == 210)
- {
- /* 960819 FdB changed matricing */
- if (info->matrix == 0 || info->matrix == 1)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k] + sb_sample[2][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k] + sb_sample[2][j][l][k];
- }
- else if (info->matrix == 3)
- {
- sb_sample[0][j][l][k] = sb_sample[5][j][l][k];
- sb_sample[1][j][l][k] = sb_sample[6][j][l][k];
- }
- }
- }
- if (info->center == 3) /* 27/07/95 WtK */
- {
- if (info->matrix==3)
- for (k = 12; k < SBLIMIT; k ++)
- {
- val = matrPC * sb_sample[2][j][l][k];
- sb_sample[0][j][l][k] += val;
- sb_sample[1][j][l][k] += val;
- sb_sample[2][j][l][k] = 0;
- }
- else
- for (k = 12; k < SBLIMIT; k ++)
- {
- val = matrPC * sb_sample[2][j][l][k];
- sb_sample[5][j][l][k] += val;
- sb_sample[6][j][l][k] += val;
- sb_sample[2][j][l][k] = 0;
- }
- }
- }
- }
- }
- #ifdef Augmentation_7ch
- void normalizing_aug (double (*sb_sample)[3][12][32], frame_params *fr_ps)
- {
- double norm; /* factor for normalizing JMZ */
- double c[7];
- int i, j, k, l;
- layer *info = fr_ps->header;
- for (i = 0; i < 7; i++)
- c[i] = 1.0;
- switch (info->matrix)
- {
- /* factors according to International Standard */
- case 0:
- case 2: c[2] = c[3] = c[4] = 1.0 / sqrt (2.0); /* weigh factor for C, Ls and Rs */
- break;
- case 1: c[2] = 1.0 / sqrt (2.0); /* weigh factor for C */
- c[3] = c[4] = 0.5; /* weigh factor for Ls, Rs */
- break;
- }
- if (info->aug_mtx_proc == 0)
- /* factors according to 7-ch augmentation */
- /* unweigh factor for LC, RC */
- c[5] = c[6] = 0.75;
- /* normalization factor */
- switch (info->matrix * 10 + info->aug_mtx_proc)
- {
- case 00:
- case 20: norm = 1.0 / (1.75 + 1.25 * sqrt (2.0));
- break;
- case 10: norm = 1.0 / (2.25 + 0.75 * sqrt (2.0));
- break;
- case 30: norm = 1.0 / 1.75;
- break;
- case 01:
- case 21: norm = 1.0 / (2.0 + sqrt (2.0));
- break;
- case 11: norm = 1.0 / (2.5 + 0.5 * sqrt (2.0));
- break;
- case 31: norm = 0.5;
- break;
- case 03:
- case 23: norm = 1.0 / (1.0 + sqrt (2.0));
- break;
- case 13: norm = 1.0 / (1.5 + 0.5 * sqrt (2.0));
- break;
- case 33: norm = 1.0;
- break;
- }
- for (i = 0; i < 7; i++)
- c[i] *= norm;
- /* normalizing */
- for (i = 0; i < 7; i++)
- for (j = 0; j < 3; j++)
- for (k = 0; k < 12; k++)
- for (l = 0; l < SBLIMIT; l++)
- sb_sample[i][j][k][l] *= c[i];
- }
- void matricing_aug (double (*sb_sample)[3][12][32], frame_params *fr_ps)
- {
- int i, j, k, l;
- layer *info = fr_ps->header;
- for (j = 0; j < 3; ++j)
- for (l = 0; l < 12; l ++)
- for (k = 0; k < SBLIMIT; k ++)
- {
- /* copy L7, R7, C7, Lc and Rc to sb_sample[7 .. 11] */
- sb_sample[11][j][l][k] = sb_sample[6][j][l][k];
- sb_sample[10][j][l][k] = sb_sample[5][j][l][k];
- sb_sample[ 9][j][l][k] = sb_sample[2][j][l][k];
- sb_sample[ 8][j][l][k] = sb_sample[1][j][l][k];
- sb_sample[ 7][j][l][k] = sb_sample[0][j][l][k];
- /* 960819 FdB changed matricing */
- if (info->aug_mtx_proc == 0 || info->aug_mtx_proc == 1)
- {
- sb_sample[0][j][l][k] += sb_sample[5][j][l][k];
- sb_sample[1][j][l][k] += sb_sample[6][j][l][k];
- }
- if (info->aug_mtx_proc == 0)
- sb_sample[2][j][l][k] += (sb_sample[5][j][l][k] + sb_sample[6][j][l][k]) / 3;
- }
- }
- #endif
- /*************************************************************************
- /*
- /* matricing_fft()
- /*
- /* To get the best results in psychoacoustics there must be both,
- /* the matriced and the not matriced signal. This matricing
- /* may be in full bandwith.
- *
- * If LFE is not enabled, "buffer" contains:
- * buffer[0] L
- * buffer[1] R
- * buffer[2] C
- * buffer[3] Ls
- * buffer[4] Rs
- *
- * If LFE is enabled, "buffer" contains:
- * buffer[0] L
- * buffer[1] R
- * buffer[2] C
- * buffer[3] LFE
- * buffer[4] Ls
- * buffer[5] Rs
- *
- * This function matrixes the original audio samples to pass to the
- * psychoacoustic model. The model considers the matrixed and non-
- * matrixed versions of the signal, so both are retained here.
- *
- * On exit, buffer_matr[0] to buffer_matr[6] contain the channels
- * Lo, Ro, C, Ls, Rs, L, R, respectively.
- *
- **************************************************************************/
- void matricing_fft (double (*buffer)[1152],
- double (*buffer_matr)[1152],
- frame_params *fr_ps)
- {
- double matrNorm; /* factor for normalizing JMZ */
- double matrC; /* weighting factor for C */
- double matrLsRs; /* weighting factor for Ls, Rs */
- double mono_surround;
- int i, j, k, l;
- int lfe, lfe_pos;
- double dummy;
- layer *info = fr_ps->header;
- lfe = info->lfe;
- lfe_pos = fr_ps->lfe_pos;
- switch(info->matrix)
- {
- case 0:
- case 2: matrNorm = 1 / (1 + sqrt(2.0));
- matrC = 1 / sqrt(2.0);
- matrLsRs = 1 / sqrt(2.0);
- break;
- case 1: matrNorm = 1 / (1.5 + 0.5*sqrt(2.0));
- matrC = 1 / sqrt(2.0);
- matrLsRs = 0.5;
- break;
- case 3: matrNorm = 1;
- matrC = 1;
- matrLsRs = 1;
- break;
- }
- for (i = 0; i < 1152; i++)
- {
- if (lfe && lfe_pos < 3)
- buffer_matr[2][i] = buffer[3][i];
- else
- buffer_matr[2][i] = buffer[2][i];
- buffer_matr[3][i] = buffer[3+lfe][i];
- buffer_matr[4][i] = buffer[4+lfe][i];
- buffer_matr[5][i] = buffer[0][i];
- buffer_matr[6][i] = buffer[1][i];
- switch (info->matrix)
- {
- case 0:
- case 1: buffer_matr[0][i] = matrNorm*(buffer[0][i] + matrC*buffer_matr[2][i] + matrLsRs*buffer_matr[3][i]);
- buffer_matr[1][i] = matrNorm*(buffer[1][i] + matrC*buffer_matr[2][i] + matrLsRs*buffer_matr[4][i]);
- break;
- case 2: mono_surround = (buffer_matr[3][i] + buffer_matr[4][i]) / 2.0;
- buffer_matr[0][i] = matrNorm*(buffer[0][i] + matrC*buffer_matr[2][i] - matrLsRs * mono_surround);
- buffer_matr[1][i] = matrNorm*(buffer[1][i] + matrC*buffer_matr[2][i] + matrLsRs * mono_surround);
- break;
- case 3: buffer_matr[0][i] = buffer[0][i];
- buffer_matr[1][i] = buffer[1][i];
- break;
- }
- }
- }
- #ifdef Augmentation_7ch
- /*************************************************************************
- /*
- /* matricing_aug_fft()
- /*
- /* To get the best results in psychoacoustics there must be both,
- /* the matriced and the not matriced signal. This matricing
- /* may be in full bandwith.
- *
- * If LFE is not enabled, "buffer" contains:
- * buffer[0] L
- * buffer[1] R
- * buffer[2] C
- * buffer[3] Ls
- * buffer[4] Rs
- * buffer[5] Lc
- * buffer[6] Rc
- *
- * If LFE is enabled, "buffer" contains:
- * buffer[0] L
- * buffer[1] R
- * buffer[2] C
- * buffer[3] LFE
- * buffer[4] Ls
- * buffer[5] Rs
- * buffer[6] Lc
- * buffer[7] Rc
- *
- * This function matrixes the original audio samples to pass to the
- * psychoacoustic model. The model considers the matrixed and non-
- * matrixed versions of the signal, so both are retained here.
- *
- * On exit, buffer[0] to buffer[5] contain the channels
- * L5, R5, C5, (LFE, ) Ls, Rs, respectively.
- * On exit, buffer_matr[7] to buffer_matr[11] contain the channels
- * L7, R7, C7, Lc and Rc respectively.
- *
- **************************************************************************/
- void matricing_aug_fft (double (*buffer)[1152],
- double (*buffer_matr)[1152],
- frame_params *fr_ps)
- {
- double matrNorm; /* factor for normalizing JMZ */
- double matrC; /* weighting factor for C */
- double matrLR; /* weighting factor for L, R */
- double matrLsRs; /* weighting factor for Ls, Rs */
- int i, j, k, l;
- int lfe;
- double dummy;
- layer *info = fr_ps->header;
- lfe = info->lfe;
- switch (info->aug_mtx_proc)
- {
- case 0:
- matrNorm = 1 / 1.75;
- matrC = 0.25;
- matrLR = 0.75;
- break;
- case 1:
- matrNorm = 0.5;
- matrC = 0;
- matrLR = 1;
- break;
- case 3:
- matrNorm = 1;
- matrC = 0;
- matrLR = 0;
- break;
- }
- for (i = 0; i < 1152; i++)
- {
- buffer_matr[3][i] = buffer[3+lfe][i]; /* Ls */
- buffer_matr[4][i] = buffer[4+lfe][i]; /* Rs */
- buffer_matr[7][i] = buffer[0][i]; /* L7 */
- buffer_matr[8][i] = buffer[1][i]; /* R7 */
- buffer_matr[9][i] = buffer[2][i]; /* C7 */
- buffer_matr[10][i] = buffer[5+lfe][i]; /* Lc */
- buffer_matr[11][i] = buffer[6+lfe][i]; /* Rc */
- /* calculate L5, R5, C5 */
- buffer_matr[0][i] = matrNorm * (buffer_matr[7][i] + matrLR * buffer_matr[10][i]);
- buffer_matr[1][i] = matrNorm * (buffer_matr[8][i] + matrLR * buffer_matr[11][i]);
- buffer_matr[2][i] = matrNorm * (buffer_matr[9][i] + matrC * (buffer_matr[10][i] + buffer_matr[11][i]));
- }
- switch (info->matrix)
- {
- case 0:
- case 2:
- matrNorm = 1 / (1 + sqrt(2.0));
- matrC = 1 / sqrt(2.0);
- matrLsRs = 1 / sqrt(2.0);
- break;
- case 1:
- matrNorm = 1 / (1.5 + 0.5*sqrt(2.0));
- matrC = 1 / sqrt(2.0);
- matrLsRs = 0.5;
- break;
- case 3:
- matrNorm = 1;
- matrC = 1;
- matrLsRs = 1;
- break;
- }
- for (i = 0; i < 1152; i++)
- {
- buffer_matr[5][i] = buffer_matr[0][i];
- buffer_matr[6][i] = buffer_matr[1][i];
- switch (info->matrix)
- {
- case 0:
- case 1: buffer_matr[0][i] = matrNorm * (buffer_matr[0][i] +
- matrC * buffer_matr[2][i] +
- matrLsRs * buffer_matr[3][i]);
- buffer_matr[1][i] = matrNorm * (buffer_matr[1][i] +
- matrC * buffer_matr[2][i] +
- matrLsRs * buffer_matr[4][i]);
- break;
- case 2: buffer_matr[0][i] = matrNorm * (buffer_matr[0][i] +
- matrC * buffer_matr[2][i] -
- matrLsRs * 0.5 *
- (buffer_matr[3][i] + buffer_matr[4][i]));
- buffer_matr[1][i] = matrNorm * (buffer_matr[1][i] +
- matrC * buffer_matr[2][i] +
- matrLsRs * 0.5 *
- (buffer_matr[3][i] + buffer_matr[4][i]));
- break;
- }
- }
- }
- #endif
- /************************************************************************/
- /*
- /* read_ana_window()
- /*
- /* PURPOSE: Reads encoder window file "enwindow" into array #ana_win#
- /*
- /************************************************************************/
- void read_ana_window(double *ana_win)
- /*far*/
- {
- int i,j[4];
- FILE *fp;
- double f[4];
- char t[150];
- if (!(fp = OpenTableFile("enwindow") ) ) {
- printf("Please check analysis window table 'enwindow'n");
- exit(1);
- }
- for (i=0;i<512;i+=4) {
- fgets(t, 80, fp); /* changed from 150, 92-08-11 shn */
- sscanf(t,"C[%d] = %lf C[%d] = %lf C[%d] = %lf C[%d] = %lfn",
- j, f,j+1,f+1,j+2,f+2,j+3,f+3);
- if (i==j[0]) {
- ana_win[i] = f[0];
- ana_win[i+1] = f[1];
- ana_win[i+2] = f[2];
- ana_win[i+3] = f[3];
- }
- else {
- printf("Check index in analysis window tablen");
- exit(1);
- }
- fgets(t,80,fp); /* changed from 150, 92-08-11 shn */
- }
- fclose(fp);
- }
- /************************************************************************/
- /*
- /* window_subband()
- /*
- /* PURPOSE: Overlapping window on PCM samples
- /*
- /* SEMANTICS:
- /* 32 16-bit pcm samples are scaled to fractional 2's complement and
- /* concatenated to the end of the window buffer #x#. The updated window
- /* buffer #x# is then windowed by the analysis window #c# to produce the
- /* windowed sample #z#
- /*
- /************************************************************************/
- void window_subband (double **buffer, double *z, int k)
- {
- typedef double XX[14][HAN_SIZE]; /* 08/03/1995 JMZ Multilingual */
- static XX *x;
- int i, j;
- static off[14]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0};/* 08/03/1995 JMZ Multilingual */
- static char init = 0;
- static double *c;
- if (!init)
- {
- c = (double *) mem_alloc (sizeof(double) * HAN_SIZE, "window");
- read_ana_window (c);
- x = (XX *) mem_alloc (sizeof(XX), "x");
- for (i = 0; i < 14; i++)
- for (j = 0; j < HAN_SIZE; j++)
- (*x)[i][j] = 0;
- init = 1;
- }
- for (i = 0; i < 32; i++)
- (*x)[k][31-i+off[k]] = (double) *(*buffer)++ / SCALE;
- for (i = 0; i < HAN_SIZE; i++)
- z[i] = (*x)[k][(i+off[k])&HAN_SIZE-1] * c[i];
- off[k] += 480; /*offset is modulo (HAN_SIZE-1)*/
- off[k] &= HAN_SIZE-1;
- }
- /************************************************************************/
- /*
- /* create_ana_filter()
- /*
- /* PURPOSE: Calculates the analysis filter bank coefficients
- /*
- /* SEMANTICS:
- /* Calculates the analysis filterbank coefficients and rounds to the
- /* 9th decimal place accuracy of the filterbank tables in the ISO
- /* document. The coefficients are stored in #filter#
- /*
- /************************************************************************/
- void create_ana_filter(double (*filter)[64])
- /*far*/
- {
- register int i,k;
- for (i = 0; i < 32; i++)
- for (k = 0; k < 64; k++) {
- if ((filter[i][k] = 1e9*cos((double)((2*i+1)*(16-k)*PI64))) >= 0)
- modf(filter[i][k]+0.5, &filter[i][k]);
- else
- modf(filter[i][k]-0.5, &filter[i][k]);
- filter[i][k] *= 1e-9;
- }
- }
- /************************************************************************/
- /*
- /* filter_subband()
- /*
- /* PURPOSE: Calculates the analysis filter bank coefficients
- /*
- /* SEMANTICS:
- /* The windowed samples #z# is filtered by the digital filter matrix #m#
- /* to produce the subband samples #s#. This done by first selectively
- /* picking out values from the windowed samples, and then multiplying
- /* them by the filter matrix, producing 32 subband samples.
- /*
- /************************************************************************/
- void filter_subband_old(double *z, double *s)
- /*far*/
- {
- double y[64];
- int i,j, k;
- static char init = 0;
- typedef double MM[SBLIMIT][64];
- static MM /*far*/ *m;
- double sum1, sum2;
- #ifdef MS_DOS
- long SIZE_OF_MM;
- SIZE_OF_MM = SBLIMIT*64;
- SIZE_OF_MM *= 8;
- if (!init) {
- m = (MM /*far*/ *) mem_alloc(SIZE_OF_MM, "filter");
- create_ana_filter(*m);
- init = 1;
- }
- #else
- if (!init) {
- m = (MM /*far*/ *) mem_alloc(sizeof(MM), "filter");
- create_ana_filter(*m);
- init = 1;
- }
- #endif
- /* Window */
- for (i=0; i<64; i++)
- {
- for (k=0, sum1 = 0.0; k<8; k++)
- sum1 += z[i+64*k];
- y[i] = sum1;
- }
- /* Filter */
- for (i=0;i<SBLIMIT;i++)
- {
- for (k=0, sum1=0.0 ;k<64;k++)
- sum1 += (*m)[i][k] * y[k];
- s[i] = sum1;
- }
- /* for (i=0;i<64;i++) for (j=0, y[i] = 0;j<8;j++) y[i] += z[i+64*j];*/
- /* for (i=0;i<SBLIMIT;i++)*/
- /* for (j=0, s[i]= 0;j<64;j++) s[i] += (*m)[i][j] * y[j];*/
- }
- /************************************************************************/
- /* JMZ 08/03/1995 FILTER */
- void filter_subband(double *z, double *s)
- /*far*/
- {
- double y[64];
- int i,j, k;
- static char init = 0;
- typedef double MM[SBLIMIT][64];
- static MM /*far*/ *m;
- double sum1, sum2;
- if (!init) {
- m = (MM /*far*/ *) mem_alloc(sizeof(MM), "filter");
- create_ana_filter(*m);
- init = 1;
- }
- /* Window */
- for (i=0; i<64; i++)
- {
- for (k=0, sum1 = 0.0; k<8; k++)
- sum1 += z[i+64*k];
- y[i] = sum1;
- }
- /* Filter */
- #if VERY_FAST_FILTER
- for (i=0; i<SBLIMIT/2; i++)
- {
- for (k=0, sum1=0.0, sum2=0.0; k<16;)
- {
- sum1 += (*m)[i][k] * (y[k]+y[32-k]);
- sum2 += (*m)[i][k+1] * (y[k+1]+y[31-k]);
- sum2 += (*m)[i][k+33] * (y[k+33]-y[63-k]);
- sum1 += (*m)[i][k+34] * (y[k+34]-y[62-k]);
- k+=2;
- }
- sum1 += (*m)[i][16]*y[16] - (*m)[i][48]*y[48];
- s[i] = sum1 + sum2;
- s[31-i] = sum1 - sum2;
- }
- #else
- for (i=0;i<SBLIMIT;i++)
- {
- for (k=0, sum1=0.0 ;k<64;k++)
- sum1 += (*m)[i][k] * y[k];
- s[i] = sum1;
- }
- #endif /*VERY_FAST_FILTER*/
- }
- /* JMZ 08/03/1995 FILTER */
- /************************************************************************/
- /************************************************************************/
- /*
- /* encode_info()
- /* encode_infomc1() SR
- /* encode_infomc2() SR
- /*
- /* PURPOSE: Puts the syncword and header information on the output
- /* bitstream.
- /*
- /************************************************************************/
- void encode_info (frame_params *fr_ps, Bit_stream_struc *bs)
- {
- layer *info = fr_ps->header;
- putbits (bs, 0xfff, 12); /* syncword 12 bits */
- put1bit (bs, info->version); /* ID 1 bit */
- putbits (bs, 4-info->lay, 2); /* layer 2 bits */
- put1bit (bs, !info->error_protection); /* bit set => no err prot */
- putbits (bs, info->bitrate_index, 4);
- putbits (bs, info->sampling_frequency, 2);
- put1bit (bs, info->padding);
- put1bit (bs, info->extension); /* private_bit */
- putbits (bs, info->mode, 2);
- putbits (bs, info->mode_ext, 2);
- put1bit (bs, info->copyright);
- put1bit (bs, info->original);
- putbits (bs, info->emphasis, 2);
- }
- void encode_info_mc1 (frame_params *fr_ps, Bit_stream_struc *bs)
- {
- layer *info = fr_ps->header;
- put1bit (bs, info->ext_bit_stream_present);
- if(info->ext_bit_stream_present == 1)
- putbits (bs, info->n_ad_bytes, 8);
- putbits (bs, info->center, 2);
- putbits (bs, info->surround, 2);
- put1bit (bs, info->lfe);
- put1bit (bs, info->audio_mix);
- putbits (bs, info->matrix, 2);
- putbits (bs, info->multiling_ch, 3);
- put1bit (bs, info->multiling_fs);
- put1bit (bs, info->multiling_lay);
- put1bit (bs, info->copy_ident_bit);
- put1bit (bs, info->copy_ident_start);
- }
- void encode_info_mc2 (frame_params *fr_ps, Bit_stream_struc *bs)
- {
- layer *info = fr_ps->header;
- int i, j;
- put1bit (bs, info->tc_sbgr_select);
- put1bit (bs, info->dyn_cross_on);
- put1bit (bs, info->mc_prediction_on);
- /* 960627 FdB tca bits dependent on configuration */
- if (fr_ps->config == 320 || fr_ps->config == 310)
- {
- /* 3 bits for tca's */
- if (info->tc_sbgr_select == 1)
- putbits (bs, info->tc_allocation, 3);
- else
- for (i = 0; i < 12; i++)
- putbits (bs, info->tc_alloc[i], 3);
- }
- else if (fr_ps->config == 300 || fr_ps->config == 302 ||
- fr_ps->config == 220 || fr_ps->config == 210)
- {
- /* 2 bits for tca's */
- if (info->tc_sbgr_select == 1)
- putbits (bs, info->tc_allocation, 2);
- else
- for (i = 0; i < 12; i++)
- putbits (bs, info->tc_alloc[i], 2);
- }
- if (info->dyn_cross_on == 1)
- {
- put1bit (bs, info->dyn_cross_LR);
- for (i = 0; i < 12; i++)
- {
- /* 960627 FdB DynX bits dependent on configuration */
- if (fr_ps->config == 320)
- /* 3/2 */
- putbits (bs, info->dyn_cross[i], 4);
- else if (fr_ps->config == 310 || fr_ps->config == 220)
- /* 3/1 and 2/2 */
- putbits (bs, info->dyn_cross[i], 3);
- else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
- /* 3/0 (+2/0) and 2/1 */
- putbits (bs, info->dyn_cross[i], 1);
- if (info->surround == 3)
- put1bit (bs, info->dyn_second_stereo[i]);
- }
- }
- if (info->mc_prediction_on == 1)
- {
- for(i = 0; i < 8; i++)
- {
- put1bit (bs, info->mc_pred[i]);
- if (info->mc_pred[i] == 1)
- {
- for (j = 0; j < n_pred_coef[info->dyn_cross[i]]; j++)
- putbits (bs, info->predsi[i][j], 2);
- }
- }
- }
- }
- #ifdef Augmentation_7ch
- void encode_info_aug (frame_params *fr_ps, Bit_stream_struc *bs)
- {
- int sbgr;
- layer *info = fr_ps->header;
- putbits (bs, info->aug_mtx_proc, 2);
- put1bit (bs, info->aug_dyn_cross_on);
- put1bit (bs, info->aug_future_ext);
- if (info->aug_mtx_proc == 0)
- for (sbgr = 0; sbgr < 12; sbgr++)
- putbits (bs, info->aug_tc_alloc[sbgr], 3);
- else if (info->aug_mtx_proc == 1)
- for (sbgr = 0; sbgr < 12; sbgr++)
- putbits (bs, info->aug_tc_alloc[sbgr], 2);
- if (info->aug_dyn_cross_on == 1)
- for (sbgr = 0; sbgr < 12; sbgr++)
- putbits (bs, info->aug_dyn_cross[sbgr], 5);
- }
- #endif
- void encode_info_ext1 (frame_params *fr_ps, Bit_stream_struc *bs_ext)
- {
- layer *info = fr_ps->header;
- info->ext_sync = 0x7ff;
- putbits (bs_ext, info->ext_sync, 12);
- }
- void encode_info_ext2 (frame_params *fr_ps, Bit_stream_struc *bs_ext, unsigned int crc)
- {
- layer *info = fr_ps->header;
- putbits (bs_ext, crc, 16);
- putbits (bs_ext, info->ext_length, 11);
- put1bit (bs_ext, info->ext_bit);
- }
- /************************************************************************/
- /*
- /* mod()
- /*
- /* PURPOSE: Returns the absolute value of its argument
- /*
- /************************************************************************/
- double mod(double a)
- {
- return (a > 0) ? a : -a;
- }
- /************************************************************************/
- /*
- /* I_combine_LR (Layer I)
- /* II_combine_LR (Layer II)
- /*
- /* PURPOSE:Combines left and right channels into a mono channel
- /*
- /* SEMANTICS: The average of left and right subband samples is put into
- /* #joint_sample#
- /*
- /* Layer I and II differ in frame length and # subbands used
- /*
- /************************************************************************/
- void I_combine_LR(double (*sb_sample)[3][12][32], double (*joint_sample)[3][12][32])
- /*far*/
- /*far*/
- { /* make a filtered mono for joint stereo */
- int sb, smp;
- for(sb = 0; sb<SBLIMIT; ++sb)
- for(smp = 0; smp<SCALE_BLOCK; ++smp)
- joint_sample[0][0][smp][sb] = .5 *
- (sb_sample[0][0][smp][sb] + sb_sample[1][0][smp][sb]);
- }
- void II_combine_LR(double (*sb_sample)[3][12][32], double (*joint_sample)[3][12][32], int sblimit)
- /*far*/
- /*far*/
- { /* make a filtered mono for joint stereo */
- int sb, smp, sufr;
- for(sb = 0; sb<sblimit; ++sb)
- for(smp = 0; smp<SCALE_BLOCK; ++smp)
- for(sufr = 0; sufr<3; ++sufr)
- joint_sample[0][sufr][smp][sb] = .5 * (sb_sample[0][sufr][smp][sb]
- + sb_sample[1][sufr][smp][sb]);
- }
- /************************************************************************
- /*
- /* I_scale_factor_calc (Layer I)
- /* II_scale_factor_calc (Layer II)
- /*
- /* PURPOSE:For each subband, calculate the scale factor for each set
- /* of the 12 (6 in case of lsf ML) subband samples
- /*
- /* SEMANTICS: Pick the scalefactor #multiple[]# just larger than the
- /* absolute value of the peak subband sample of 12 samples,
- /* and store the corresponding scalefactor index in #scalar#.
- /*
- /* Layer II has three sets of 12 (6 in case of lsf ML) subband samples
- /* for a given subband.
- /*
- /************************************************************************/
- void I_scale_factor_calc(double (*sb_sample)[3][12][32], unsigned int (*scalar)[3][32], int stereo)
- /*far*/
- {
- int i,j, k;
- double s[SBLIMIT];
- for (k=0;k<stereo;k++) {
- for (i=0;i<SBLIMIT;i++)
- for (j=1, s[i] = mod(sb_sample[k][0][0][i]);j<SCALE_BLOCK;j++)
- if (mod(sb_sample[k][0][j][i]) > s[i])
- s[i] = mod(sb_sample[k][0][j][i]);
- for (i=0;i<SBLIMIT;i++)
- for (j=SCALE_RANGE-1,scalar[k][0][i]=0;j>=0;j--)
- if (s[i] < multiple[j]) { /* <= changed to <, 1992-11-06 shn */
- scalar[k][0][i] = j;
- break;
- }
- }
- }
- /******************************** Layer II ******************************/
- void II_scale_factor_calc (frame_params *fr_ps,
- double (*sb_sample)[3][12][32],
- unsigned int (*scalar)[3][32],
- int sblimit, int l, int m)
- /* sblimit has the value of sblimit_ml in case II_scale_factor_calc */
- /* is called in a ML channel , 7/8/95 WtK */
- {
- int i,j, k,t;
- double s[SBLIMIT];
- int leng;
- leng = SCALE_BLOCK; /* == 12 */
- if (l >= 7 && fr_ps->header->multiling_fs == 1)
- leng /= 2;
- for (k = l; k < m; k++)
- for (t = 0; t < 3; t++)
- {
- for (i = 0; i < sblimit; i++)
- for (j = 1, s[i] = mod (sb_sample[k][t][0][i]); j < leng; j++)
- if (mod (sb_sample[k][t][j][i]) > s[i])
- s[i] = mod (sb_sample[k][t][j][i]);
- for (i = 0; i < sblimit; i++)
- for (j = SCALE_RANGE - 1, scalar[k][t][i] = 0; j >= 0; j--)
- if (s[i] < multiple[j])
- {
- /* <= changed to <, 1992-11-06 shn */
- scalar[k][t][i] = j;
- break;
- }
- for (i = sblimit; i < SBLIMIT; i++)
- scalar[k][t][i] = SCALE_RANGE - 1;
- }
- }
- /***************************************************************************
- /* void II_scale_factor_calc1(sb_sample, scalar, stereo, sblimit)
- /*
- /* in case of any joint stereo the scalefactor must be computed
- /* a second time for the combind samples
- /*
- /***************************************************************************/
- void II_scale_factor_calc1(double (*sb_sample)[3][12][32], unsigned int (*scalar)[3][32], int sblimit, int dim)
- /*far*/
- {
- int i,j, k,t;
- double s[SBLIMIT];
- for (t=0;t<3;t++) {
- for (i=0;i<sblimit;i++)
- for (j=1, s[i] = mod(sb_sample[dim][t][0][i]);j<SCALE_BLOCK;j++)
- if (mod(sb_sample[dim][t][j][i]) > s[i])
- s[i] = mod(sb_sample[dim][t][j][i]);
- for (i=0;i<sblimit;i++)
- for (j=SCALE_RANGE-1,scalar[dim][t][i]=0;j>=0;j--)
- if (s[i] < multiple[j]) { /* <= changed to <, 1992-11-06 shn */
- scalar[dim][t][i] = j;
- break;
- }
- for (i=sblimit;i<SBLIMIT;i++) scalar[dim][t][i] = SCALE_RANGE-1;
- }
- }
- /************************************************************************
- /*
- /* pick_scale (Layer II)
- /*
- /* PURPOSE:For each subband, puts the smallest scalefactor of the 3
- /* associated with a frame into #max_sc#. This is used
- /* used by Psychoacoustic Model I.
- /* (I would recommend changin max_sc to min_sc)
- /*
- /************************************************************************/
- void pick_scale (unsigned int (*scalar)[3][32],
- frame_params *fr_ps,
- double (*max_sc)[32],
- int cha_sw,
- int aug_cha_sw,
- int aiff)
- {
- int i,j,k,l,m;
- int max;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int stereoaug = fr_ps->stereoaug;
- int sblimit = fr_ps->sblimit;
- int sblimit_mc = fr_ps->sblimit_mc;
- int sblimit_ml = fr_ps->sblimit_ml;
- int n_ml_ch = fr_ps->header->multiling_ch; /* 08/03/1995 JMZ Multilingual */
- if (aiff != 1)
- {
- l = 0; m = stereo;
- }
- else
- {
- l = 0;
- if (stereoaug == 2) m = 12;
- else m = 7;
- }
- for (k = 0; k < stereo; k++)
- {
- for (i = 0; i < sblimit; max_sc[k][i] = multiple[max], i++)
- for (j=1, max = scalar[k][0][i];j<3;j++)
- if (max > scalar[k][j][i])
- max = scalar[k][j][i];
- for (i = sblimit; i < SBLIMIT;i++)
- max_sc[k][i] = 1E-20;
- }
- for (k = stereo; k < m; k++)
- {
- for (i = 0; i < sblimit_mc; max_sc[k][i] = multiple[max], i++)
- for (j=1, max = scalar[k][0][i];j<3;j++)
- if (max > scalar[k][j][i])
- max = scalar[k][j][i];
- for (i = sblimit_mc; i < SBLIMIT;i++)
- max_sc[k][i] = 1E-20;
- }
- if (aiff == 1)
- {
- /* OLD 961114 FdB
- if (fr_ps->header->matrix == 3 || cha_sw == 0)
- {
- fr_ps->header->tc_sbgr_select = 1;
- for (i = 0; i < 12; i++)
- fr_ps->header->tc_alloc[i] = 0;
- }
- else
- tc_alloc (fr_ps, max_sc);
- */
- for (i = 0; i < 12; i++)
- if (cha_sw == -1 && fr_ps->header->matrix != 3)
- switch (fr_ps->config)
- {
- case 320:
- if (fr_ps->header->center == 3 && i >= 10) /* tc_alloc = 0,3,4,5 */
- {
- fr_ps->header->tc_alloc[i] = rand () % 4;
- if (fr_ps->header->tc_alloc[i] > 0)
- fr_ps->header->tc_alloc[i] += 2;
- }
- else
- fr_ps->header->tc_alloc[i] = rand () % 8;
- break;
- case 310:
- if (fr_ps->header->center == 3 && i >= 10) /* tc_alloc = 0,3,4 */
- {
- fr_ps->header->tc_alloc[i] = rand () % 3;
- if (fr_ps->header->tc_alloc[i] > 0)
- fr_ps->header->tc_alloc[i] += 2;
- }
- else if (fr_ps->header->matrix == 2)
- fr_ps->header->tc_alloc[i] = rand () % 6;
- else
- fr_ps->header->tc_alloc[i] = rand () % 5;
- break;
- case 300:
- case 302:
- if (fr_ps->header->center == 3 && i >= 10) /* tc_alloc = 0 */
- fr_ps->header->tc_alloc[i] = 0;
- else
- fr_ps->header->tc_alloc[i] = rand () % 3;
- break;
- case 220:
- fr_ps->header->tc_alloc[i] = rand () % 4;
- break;
- case 210:
- fr_ps->header->tc_alloc[i] = rand () % 3;
- break;
- default:
- break;
- }
- else if (cha_sw == -2 && fr_ps->header->matrix != 3)
- tc_alloc (fr_ps, max_sc);
- else if (fr_ps->header->matrix == 3)
- fr_ps->header->tc_alloc[i] = 0;
- else
- fr_ps->header->tc_alloc[i] = cha_sw;
- fr_ps->header->tc_sbgr_select = 1;
- fr_ps->header->tc_allocation = fr_ps->header->tc_alloc[0];
- for (i = 1; i < 12; i++)
- if (fr_ps->header->tc_alloc[i] != fr_ps->header->tc_alloc[0])
- fr_ps->header->tc_sbgr_select = 0;
- #ifdef Augmentation_7ch
- for (i = 0; i < 12; i++)
- if (aug_cha_sw == -1)
- switch (fr_ps->header->aug_mtx_proc)
- {
- case 0: fr_ps->header->aug_tc_alloc[i] = rand () % 8;
- break;
- case 1: fr_ps->header->aug_tc_alloc[i] = rand () % 4;
- break;
- case 3: fr_ps->header->aug_tc_alloc[i] = 0;
- break;
- }
- else
- fr_ps->header->aug_tc_alloc[i] = aug_cha_sw;
- #endif
- }
- /********************************************************/
- /* JMZ 08/03/1995 Multilingual , WtK 07/08/95 */
- if (n_ml_ch > 0)
- {
- for (k = 7; k < 7 + n_ml_ch; k++)
- {
- for (i = 0; i < sblimit_ml; max_sc[k][i] = multiple[max], i++)
- for (j = 1, max = scalar[k][0][i]; j < 3; j++)
- if (max > scalar[k][j][i])
- max = scalar[k][j][i];
- for (i= sblimit_ml; i < SBLIMIT;i++)
- max_sc[k][i] = 1E-20;
- }
- }
- /* JMZ 08/03/1995 Multilingual */
- /********************************************************/
- }
- /***************************************************************************
- /*
- /* tc_alloc (Layer II, multichannel)
- /*
- /* PURPOSE: For each subbandgroup the three transmissionchannels are
- /* determined by taking the channel with the lowest level
- /* according to the tabel tc_allocation in the draft
- /* 8/10/93, SR
- /*
- /* changed to a certain limit of TC_ALLOC which must be stepped
- /* beyond, before there is channel-switching
- /* 9/20/93 SR
- /* JMZ 08/03/1995 Ajout pour traiter les differentes configurations
- /* envisagees dans la norme
- /**************************************************************************/
- void tc_alloc (frame_params *fr_ps, double (*max_sc)[32])
- {
- layer *info = fr_ps->header;
- int center = info->center;
- int surround = info->surround;
- int matrix = info->matrix;
- int i, l, k;
- int min;
- double min1;
- double min2[7][12];
- /* 01/03/1995 JMZ Configuration 3/2 */
- if (surround == 2 && center != 0)
- {
- /* if (matrix == 3) ->tc_alloc = 0
- else if (center == 3) ->tc_alloc = 0,3,4,5
- else ->tc_alloc = 0,1,2,3,4,5,6,7 */
- if (matrix == 3)
- {
- for (i = 0; i < 12; i++)
- fr_ps->header->tc_alloc[i] = 0;
- }
- else if (center == 3) /* && matrix != 3 */
- {
- /* 3/2 Phantom Center coding */
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[4][i])
- fr_ps->header->tc_alloc[i] = 5;
- else
- fr_ps->header->tc_alloc[i] = 3;
- }
- else if (((20 * log10(max_sc[4][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- for (i = 8; i < 12; i++)
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[3][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[4][i])
- fr_ps->header->tc_alloc[i] = 5;
- else
- fr_ps->header->tc_alloc[i] = 3;
- }
- else if (((20 * log10(min2[4][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- }
- else
- {
- /* 3/2 no Phantom Center coding */
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[5][i])
- min = 6;
- else if (max_sc[6][i] == max_sc[5][i])
- {
- if (max_sc[3][i] <= max_sc[5][i])
- min = 5;
- else
- min = 6;
- }
- else
- min = 5;
- }
- else if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- {
- min = 6;
- /* 01/03/1995 JMZ Simplification */
- }
- else
- {
- min = 2;
- }
- switch (min)
- {
- case 5: if (max_sc[4][i] <= max_sc[6][i]) /* left front,Rs*/
- fr_ps->header->tc_alloc[i] = 1;
- else
- fr_ps->header->tc_alloc[i] = 7; /*R*/
- break;
- case 6: if (max_sc[3][i] <= max_sc[5][i]) /* right front,Ls*/
- fr_ps->header->tc_alloc[i] = 2;
- else
- fr_ps->header->tc_alloc[i] = 6; /* L */
- break;
- case 2: if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[4][i] <= max_sc[6][i])
- fr_ps->header->tc_alloc[i] = 3;
- else
- fr_ps->header->tc_alloc[i] = 5;
- }
- else
- {
- if (((20 * log10(max_sc[4][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- break;
- }
- }
- for (i = 8; i < 12; i++) /*taking the average scalefactor of each sb-group*/
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[5][i])
- min = 6;
- else if (min2[6][i] == min2[5][i])
- {
- if (min2[3][i] <= min2[5][i])
- min = 5;
- else
- min = 6;
- }
- else
- min = 5;
- }
- else if (((20 * log10(min2[2][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- {
- min = 6;
- /* 01/03/1995 JMZ Simplification */
- }
- else
- {
- min = 2;
- }
- switch (min)
- {
- case 5: if (min2[4][i] <= min2[6][i]) /* left front,Rs*/
- fr_ps->header->tc_alloc[i] = 1;
- else
- fr_ps->header->tc_alloc[i] = 7; /*R*/
- break;
- case 6: if (min2[3][i] <= min2[5][i]) /* right front,Ls*/
- fr_ps->header->tc_alloc[i] = 2;
- else
- fr_ps->header->tc_alloc[i] = 6; /* L */
- break;
- case 2: if (((20 * log10(min2[3][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[4][i] <= min2[6][i])
- fr_ps->header->tc_alloc[i] = 3;
- else
- fr_ps->header->tc_alloc[i] = 5;
- }
- else
- {
- if (((20 * log10(min2[4][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- break;
- }
- }
- }
- }
- /* 01/03/1995 JMZ Configuration 3/1 */
- if (surround == 1 && center != 0)
- {
- /* if (matrix == 3) ->tc_alloc = 0
- else if (center == 3) ->tc_alloc = 0,3,4
- else if (matrix == 2) ->tc_alloc = 0,1,2,3,4,5
- else ->tc_alloc = 0,1,2,3,4 */
- if (matrix == 3)
- {
- for (i = 0; i < 12; i++)
- fr_ps->header->tc_alloc[i] = 0;
- }
- else if (center == 3) /* && matrix != 3 */
- {
- /* 3/1 Phantom Center coding */
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[5][i])
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 3;
- }
- else if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- for (i = 8; i < 12; i++)
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[3][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[5][i])
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 3;
- }
- else
- if (((20 * log10(min2[3][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- }
- else
- /*
- if(matrix==2)
- {
- }
- else
- */
- {
- /* 3/1 no Phantom Center coding */
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[5][i])
- min = 6;
- else
- min = 5;
- }
- else if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- min = 6;
- else
- min = 2;
- switch (min)
- {
- case 5: fr_ps->header->tc_alloc[i] = 1;
- break;
- case 6: fr_ps->header->tc_alloc[i] = 2;
- break;
- case 2: if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] <= max_sc[5][i])
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 3;
- }
- else
- {
- if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- break;
- }
- }
- for (i = 8; i < 12; i++)
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[5][i])
- min = 6;
- else
- min = 5;
- }
- else if (((20 * log10(min2[2][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- min = 6;
- else
- min = 2;
- switch (min)
- {
- case 5: fr_ps->header->tc_alloc[i] = 1;
- break;
- case 6: fr_ps->header->tc_alloc[i] = 2;
- break;
- case 2: if (((20 * log10(min2[3][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] <= min2[5][i])
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 3;
- }
- else
- {
- if (((20 * log10(min2[3][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 4;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- break;
- }
- }
- }
- }
- /* 01/03/1995 JMZ Configuration 3/0 (+2/0) */
- if (center != 0 && (surround == 3 || surround == 0))
- {
- if (matrix == 3 || center == 3)
- {
- for (i = 0; i < 12; i++)
- fr_ps->header->tc_alloc[i] = 0;
- }
- /* 02/02/97 FdB no matrix == 2 allowed for 3/0
- else if (matrix == 2)
- {
- for(i = 0; i < 8; i++)
- {
- if(((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if(max_sc[6][i] < max_sc[5][i])
- fr_ps->header->tc_alloc[i] = 2;
- else fr_ps->header->tc_alloc[i] = 1;
- }
- else if(((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 2;
- else fr_ps->header->tc_alloc[i] = 0;
- }
- for(i = 8; i < 12; i++)
- for(k = 2; k < 7; k++)
- min2[k][i] = 0.0;
- for(i = 8; i < 12; i++)
- {
- for(k = 2; k < 7; k++)
- {
- for(l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- {
- min2[k][i] += max_sc[k][l];
- }
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if(((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if(min2[6][i] < min2[5][i])
- fr_ps->header->tc_alloc[i] = 2;
- else fr_ps->header->tc_alloc[i] = 1;
- }
- else if(((20 * log10(min2[2][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 2;
- else fr_ps->header->tc_alloc[i] = 0;
- }
- }
- */
- else
- {
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[5][i])
- min = 6;
- else
- min = 5;
- }
- else if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- min = 6;
- else
- min = 2;
- switch (min)
- {
- case 5: fr_ps->header->tc_alloc[i] = 1;
- break;
- case 6: fr_ps->header->tc_alloc[i] = 2;
- break;
- case 2: fr_ps->header->tc_alloc[i] = 0;
- break;
- }
- }
- for (i = 8; i < 12; i++)
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[5][i])
- min = 6;
- else
- min = 5;
- }
- else if (((20 * log10(min2[2][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- min = 6;
- else
- min = 2;
- switch (min)
- {
- case 5: fr_ps->header->tc_alloc[i] = 1;
- break;
- case 6: fr_ps->header->tc_alloc[i] = 2;
- break;
- case 2: fr_ps->header->tc_alloc[i] = 0;
- break;
- }
- }
- }
- }
- /* 01/03/1995 JMZ Configuration 2/2 */
- if (center == 0 && surround == 2)
- {
- if (matrix == 3)
- {
- for (i = 0; i < 12; i++)
- fr_ps->header->tc_alloc[i] = 0;
- }
- else
- {
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[3][i])
- fr_ps->header->tc_alloc[i] = 3;
- else
- fr_ps->header->tc_alloc[i] = 2;
- }
- else if (((20 * log10(max_sc[3][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 1;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- for (i = 8; i < 12; i++)
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[3][i])
- fr_ps->header->tc_alloc[i] = 3;
- else
- fr_ps->header->tc_alloc[i] = 2;
- }
- else if (((20 * log10(min2[3][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- fr_ps->header->tc_alloc[i] = 1;
- else
- fr_ps->header->tc_alloc[i] = 0;
- }
- }
- }
- /* 01/03/1995 JMZ et Configuration 2/1*/
- if (center == 0 && surround == 1)
- {
- if (matrix == 3)
- {
- for (i = 0; i < 12; i++)
- fr_ps->header->tc_alloc[i] = 0;
- }
- else
- {
- for (i = 0; i < 8; i++)
- {
- if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[5][i]))) > TC_ALLOC)
- {
- if (max_sc[6][i] < max_sc[5][i])
- min = 6;
- else
- min = 5;
- }
- else if (((20 * log10(max_sc[2][i])) - (20 * log10(max_sc[6][i]))) > TC_ALLOC)
- min = 6;
- else
- min = 2;
- switch (min)
- {
- case 5: fr_ps->header->tc_alloc[i] = 1;
- break;
- case 6: fr_ps->header->tc_alloc[i] = 2;
- break;
- case 2: fr_ps->header->tc_alloc[i] = 0;
- break;
- }
- }
- for (i = 8; i < 12; i++)
- {
- for (k = 2; k < 7; k++)
- {
- min2[k][i] = 0.0;
- for (l = (sb_groups[i-1] + 1); l <= sb_groups[i]; l++)
- min2[k][i] += max_sc[k][l];
- min2[k][i] = min2[k][i] / (sb_groups[i] - sb_groups[i-1]);
- }
- if (((20 * log10(min2[2][i])) - (20 * log10(min2[5][i]))) > TC_ALLOC)
- {
- if (min2[6][i] < min2[5][i])
- min = 6;
- else
- min = 5;
- }
- else if (((20 * log10(min2[2][i])) - (20 * log10(min2[6][i]))) > TC_ALLOC)
- min = 6;
- else
- min = 2;
- switch (min)
- {
- case 5: fr_ps->header->tc_alloc[i] = 1;
- break;
- case 6: fr_ps->header->tc_alloc[i] = 2;
- break;
- case 2: fr_ps->header->tc_alloc[i] = 0;
- break;
- }
- }
- }
- }
- if (fr_ps->header->tc_alloc[0] == fr_ps->header->tc_alloc[1] &&
- fr_ps->header->tc_alloc[1] == fr_ps->header->tc_alloc[2] &&
- fr_ps->header->tc_alloc[2] == fr_ps->header->tc_alloc[3] &&
- fr_ps->header->tc_alloc[3] == fr_ps->header->tc_alloc[4] &&
- fr_ps->header->tc_alloc[4] == fr_ps->header->tc_alloc[5] &&
- fr_ps->header->tc_alloc[5] == fr_ps->header->tc_alloc[6] &&
- fr_ps->header->tc_alloc[6] == fr_ps->header->tc_alloc[7] &&
- fr_ps->header->tc_alloc[7] == fr_ps->header->tc_alloc[8] &&
- fr_ps->header->tc_alloc[8] == fr_ps->header->tc_alloc[9] &&
- fr_ps->header->tc_alloc[9] == fr_ps->header->tc_alloc[10] &&
- fr_ps->header->tc_alloc[10] == fr_ps->header->tc_alloc[11])
- {
- fr_ps->header->tc_sbgr_select = 1;
- fr_ps->header->tc_allocation = fr_ps->header->tc_alloc[0];
- }
- else
- fr_ps->header->tc_sbgr_select = 0; /* added 8/20/93,SR*/
- }
- /************************************************************************
- /*
- /* put_scale (Layer I)
- /*
- /* PURPOSE:Sets #max_sc# to the scalefactor index in #scalar.
- /* This is used by Psychoacoustic Model I
- /*
- /************************************************************************/
- void put_scale(unsigned int (*scalar)[3][32], frame_params *fr_ps, double (*max_sc)[32])
- /*far*/
- {
- int i,j,k, max;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit;
- /* for (k = 0; k < stereo+stereomc+2; k++) 960814 FdB bug for some configurations */
- for (k = 0; k < 7; k++)
- for (i = 0; i < SBLIMIT; i++)
- max_sc[k][i] = multiple[scalar[k][0][i]];
- }
- /************************************************************************
- /*
- /* II_transmission_pattern (Layer II only)
- /*
- /* PURPOSE:For a given subband, determines whether to send 1, 2, or
- /* all 3 of the scalefactors, and fills in the scalefactor
- /* select information accordingly
- /*
- /* SEMANTICS: The subbands and channels are classified based on how much
- /* the scalefactors changes over its three values (corresponding
- /* to the 3 sets of 12 samples per subband). The classification
- /* will send 1 or 2 scalefactors instead of three if the scalefactors
- /* do not change much. The scalefactor select information,
- /* #scfsi#, is filled in accordingly.
- /*
- /************************************************************************/
- void II_transmission_pattern (unsigned int (*scalar)[3][32], unsigned int (*scfsi)[32], frame_params *fr_ps)
- {
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit;
- int sblimit_mc = fr_ps->sblimit_mc;
- int sblimit_ml = fr_ps->sblimit_ml;
- int n_ml_ch = fr_ps->header->multiling_ch;
- int dscf[2];
- int class[2],i,j,k;