home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- PCM2MP.cpp
-
- 03/02/08 Xiaohong
- *************************************************************************/
- //#include <dos.h>
- //#include <ctype.h>
- #include "tompeg.h"
- #include "alloc_0.h"
- #include "alloc_1.h"
- #include "alloc_2.h"
- #include "alloc_3.h"
- #include "absthr_0.h"
- #include "absthr_1.h"
- #include "absthr_2.h"
- #include "fft.h"
- #include "BitStreamStruct.h"
- #include <math.h>
- #include <windows.h>
- #include <vfw.h>
- #include "macros.h"
-
- /***********************************************************************
- *
- * Global Definitions
- *
- ***********************************************************************/
-
- /* General Definitions */
-
- #define MAX_U_32_NUM 0xFFFFFFFF
- #define PI 3.14159265358979
- #define PI4 (PI/4)
- #define PI64 (PI/64)
- #define LN_TO_LOG10 0.2302585093
-
- #define MPEG_AUDIO_ID 1
-
- #define SBLIMIT 32
- #define HAN_SIZE 512
- #define SCALE_BLOCK 12
- #define SCALE_RANGE 64
- #define SCALE 32768
- #define CRC16_POLYNOMIAL 0x8005
-
- /* MPEG Header Definitions - Mode Values */
-
- #define MPG_MD_STEREO 0
- #define MPG_MD_JOINT_STEREO 1
- #define MPG_MD_DUAL_CHANNEL 2
- #define MPG_MD_MONO 3
-
- /***********************************************************************
- *
- * Global Type Definitions
- *
- ***********************************************************************/
-
- /* Structure for Reading Layer II Allocation Tables from File */
-
- typedef struct {
- unsigned int steps;
- unsigned int bits;
- unsigned int group;
- unsigned int quant;
- } sb_alloc, *alloc_ptr;
-
- typedef sb_alloc al_table[SBLIMIT][16];
-
- /* Header Information Structure */
-
- typedef struct
- {
- int version;
- int lay;
- bool error_protection;
- int bitrate_index;
- int sampling_frequency;
- int padding;
- int extension;
- int mode;
- int mode_ext;
- int copyright;
- int original;
- int emphasis;
- } layer, *the_layer;
-
- /* Parent Structure Interpreting some Frame Parameters in Header */
-
- class frame_params
- {
- public:
- frame_params(layer*);
- ~frame_params();
-
- void free_table(void);
- bool alloc_table(void);
-
- layer *header; /* raw header information */
- int actual_mode; /* when writing IS, may forget if 0 chs */
- al_table *alloc; /* bit allocation table read in */
- int tab_num; /* number of table as loaded */
- int stereo; /* 1 for mono, 2 for stereo */
- int jsbound; /* first band of joint stereo coding */
- int sblimit; /* total number of sub bands */
- };
- frame_params::frame_params(layer* pl)
- {
- header = pl;
- alloc = NULL;
- tab_num = -1;
- }
- frame_params::~frame_params()
- {
- free_table();
- }
- void frame_params::free_table(void)
- {
- if(alloc!=NULL)
- {
- _free(alloc);
- }
- }
- bool frame_params::alloc_table(void)
- {
- free_table();
-
- if((alloc = (al_table *)_malloc(sizeof(al_table)))==NULL)
- {
- return false;
- }
- ZeroMemory(alloc,sizeof(al_table));
- return true;
- }
-
- #define DFLT_SFQ 44.1 /* default input sampling rate is 44.1 kHz */
- #define DFLT_EMP 'n' /* default de-emphasis is none */
-
- /* This is the smallest MNR a subband can have before it is counted
- as 'noisy' by the logic which chooses the number of JS subbands */
-
- #define NOISY_MIN_MNR 0.0
-
- /* Psychoacoustic Model 2 Definitions */
-
- #define HBLKSIZE 513
- #define CBANDS 63
- #define LXMIN 32.0
-
- /* additonal def's for avi handling */
-
- #define PCM_BUFFER 2304
-
-
- /* Psychoacoustic Model 2 Type Definitions */
-
-
- /***********************************************************************
- *
- * Encoder Function Prototype Declarations
- *
- ***********************************************************************/
- /* The following functions are in the file "encode.c" */
-
- static unsigned long read_samples(short[PCM_BUFFER],const long,const long,bool*);
- static inline unsigned char get_audio(short[2][1152], unsigned long,int, int);
- static void window_subband(short**, double[], int);
- static inline void create_ana_filter(double[SBLIMIT][64]);
- static void filter_subband(double[HAN_SIZE], double[SBLIMIT]);
- static void encode_info(frame_params*, BitStreamStruct*);
- static inline void I_combine_LR(double[2][3][SCALE_BLOCK][SBLIMIT],
- double[3][SCALE_BLOCK][SBLIMIT]);
- static inline void II_combine_LR(double[2][3][SCALE_BLOCK][SBLIMIT],
- double[3][SCALE_BLOCK][SBLIMIT], int);
- static void I_scale_factor_calc(double[][3][SCALE_BLOCK][SBLIMIT],
- unsigned int[][3][SBLIMIT], int);
- static void II_scale_factor_calc(double[][3][SCALE_BLOCK][SBLIMIT],
- unsigned int[][3][SBLIMIT], int, int);
- static inline void pick_scale(unsigned int[2][3][SBLIMIT], frame_params*,
- double[2][SBLIMIT]);
- static inline void put_scale(unsigned int[][3][SBLIMIT],const frame_params*,
- double[2][SBLIMIT]);
- static inline void II_transmission_pattern(unsigned int[2][3][SBLIMIT],
- unsigned int[2][SBLIMIT], frame_params*);
- static inline void II_encode_scale(unsigned int[2][SBLIMIT],
- unsigned int[2][SBLIMIT],
- unsigned int[2][3][SBLIMIT], frame_params*,
- BitStreamStruct*);
- static inline void I_encode_scale(unsigned int[2][3][SBLIMIT],
- const unsigned int[2][SBLIMIT],
- const frame_params*,
- BitStreamStruct*);
- static int II_bits_for_nonoise(const double[2][SBLIMIT],const unsigned int[2][SBLIMIT],
- const frame_params*);
- static inline bool II_main_bit_allocation(const double[2][SBLIMIT],
- const unsigned int[2][SBLIMIT], unsigned int[2][SBLIMIT],
- int*,frame_params*);
- static inline int II_a_bit_allocation(const double[][SBLIMIT],const unsigned int[][SBLIMIT],
- unsigned int[][SBLIMIT], int*,const frame_params*);
- static int I_bits_for_nonoise(const double[2][SBLIMIT],const frame_params*);
- static inline bool I_main_bit_allocation(const double[2][SBLIMIT],
- unsigned int[2][SBLIMIT], int*,frame_params*);
- static inline int I_a_bit_allocation(const double[2][SBLIMIT], unsigned int[2][SBLIMIT],
- int*,const frame_params*);
- static inline void I_subband_quantization(unsigned int[2][3][SBLIMIT],
- double[2][3][SCALE_BLOCK][SBLIMIT], unsigned int[3][SBLIMIT],
- double[3][SCALE_BLOCK][SBLIMIT], unsigned int[2][SBLIMIT],
- unsigned int[2][3][SCALE_BLOCK][SBLIMIT], frame_params*);
- static inline void II_subband_quantization(unsigned int[2][3][SBLIMIT],
- double[2][3][SCALE_BLOCK][SBLIMIT], unsigned int[3][SBLIMIT],
- double[3][SCALE_BLOCK][SBLIMIT], unsigned int[2][SBLIMIT],
- unsigned int[2][3][SCALE_BLOCK][SBLIMIT], frame_params*);
- static inline void II_encode_bit_alloc(const unsigned int[][SBLIMIT],const frame_params*,
- BitStreamStruct*);
- static inline void I_encode_bit_alloc(const unsigned int[][SBLIMIT],const frame_params*,
- BitStreamStruct*);
- static inline void I_sample_encoding(unsigned int[2][3][SCALE_BLOCK][SBLIMIT],
- unsigned int[2][SBLIMIT], frame_params*,
- BitStreamStruct*);
- static inline void II_sample_encoding(unsigned int[2][3][SCALE_BLOCK][SBLIMIT],
- unsigned int[2][SBLIMIT], frame_params*,
- BitStreamStruct*);
- static inline void encode_CRC(unsigned int, BitStreamStruct*);
- /* The following functions are in the file "common.c" */
-
- static inline int read_bit_alloc(int, al_table*);
- static inline int pick_table(frame_params*,bool*);
- static int js_bound(const int,const int);
- static inline bool hdr_to_frps(frame_params*);
- static inline int BitrateIndex(const int,const unsigned int);
- static inline int SmpFrqIndex(long);
- static inline void I_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
- unsigned int*);
- static inline void II_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
- unsigned int[2][SBLIMIT], unsigned int*);
- static void update_CRC(unsigned int, unsigned int, unsigned int*);
-
- /* The following functions are in the file "psy.c" */
-
- static TOMPGRET psycho_anal(
- const short* buffer,short int savebuf[1056],
- const int chn,const int lay,
- double snr32[],const double sfreq);
-
- static void filter(short *work_buffer, unsigned int length, unsigned int stereo, unsigned int mult);
-
-
- /* Global variable definitions for "avi2mp2.c" */
-
- static const double g_fSamplingFrequency[4] = {44.1, 48.0, 32.0, 0.0};
-
- static const unsigned int g_uBitRateArray[2][15] =
- {
- {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
- {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384}
- };
-
- static const double g_fMultiple[64] =
- {
- 2.00000000000000, 1.58740105196820, 1.25992104989487,
- 1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
- 0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
- 0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
- 0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
- 0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
- 0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
- 0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
- 0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
- 0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
- 0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
- 0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
- 0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
- 0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
- 0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
- 0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
- 1E-20
- };
-
-
- //char programName[] = "avi2mpg1";
- //static int channels;
-
- static unsigned long g_nLastSample = 0;
- static long g_nBytesProcessed = 0;
-
- static WAVEFORMATEX g_stWavFormat;
- static PAVISTREAM g_pAudioStream = NULL;
- static AVISTREAMINFO g_stAudioStreamInfo;
-
- /* Implementations */
-
- /*---------------------------------------------------------
- âpâëâüü[â^é╠É▌ÆΦ
- ---------------------------------------------------------*/
- static TOMPGRET set_parms(frame_params *fr_ps,
- BitStreamStruct *bs,
- unsigned long *num_samples,
- const char *tempfile,
- const int audio_layer_param,
- const bool joint_stereo_param,
- const unsigned int bitrate)
- {
- layer *info = fr_ps->header;
- int err = 0, i = 0;
-
- /* preset defaults */
- info->lay = audio_layer_param;
-
- if(g_stWavFormat.nChannels == 1)
- {
- info->mode = MPG_MD_MONO;
- info->mode_ext = 0;
- }
- else
- {
- if(joint_stereo_param)
- {
- info->mode = MPG_MD_JOINT_STEREO;
- }
- else
- {
- info->mode = MPG_MD_STEREO;
- info->mode_ext = 0;
- }
- }
-
- if((info->sampling_frequency = SmpFrqIndex((long)(1000*DFLT_SFQ))) < 0)
- {
- return TR_SAMPLING_FREQUENCY_ERR;
- }
- if((info->bitrate_index = BitrateIndex(info->lay, bitrate)) < 0)
- {
- return TR_AUDIO_BITRATE_ERR;
- }
-
- switch(DFLT_EMP)
- {
- case 'n': info->emphasis = 0; break;
- case '5': info->emphasis = 1; break;
- case 'c': info->emphasis = 3; break;
- default:
- return TR_ERR;
- }
-
- info->copyright = 0;
- info->original = 0;
- info->error_protection = false;
-
- //length = sizeof(pWavFormat);
-
- //channels = g_stWavFormat.nChannels; // store for later
-
- if(g_stWavFormat.wFormatTag != WAVE_FORMAT_PCM)
- {
- return TR_NOT_PCM;
- }
-
- if ((g_stWavFormat.nSamplesPerSec != 11025)
- && (g_stWavFormat.nSamplesPerSec != 22050)
- && (g_stWavFormat.nSamplesPerSec != 44100))
- {
- return TR_SAMPLE_PER_SEC_ERR;
- }
- if ((g_stWavFormat.wBitsPerSample != 16)
- && (g_stWavFormat.wBitsPerSample != 8))
- {
- return TR_BITS_PER_SAMPLE_ERR;
- }
-
- switch(bs->OpenBitStreamW(tempfile,BUFFER_SIZE))
- {
- case BSSR_OK:
- break;
- case BSSR_OPEN_FILE_ERR:
- return TR_OPEN_TMP_AUDIO_FILE_ERR;
- case BSSR_ALLOC_BUFFER_ERR:
- return TR_ALLOC_BIT_STREAM_BUFFER_ERR;
- default:
- return TR_ERR;
- }
-
- *num_samples=0;
- if(g_stWavFormat.nChannels == 2)
- *num_samples += g_stAudioStreamInfo.dwLength * 2;
- else
- *num_samples += g_stAudioStreamInfo.dwLength;
-
- if(g_stWavFormat.nSamplesPerSec == 22050)
- *num_samples = *num_samples * 2;
-
- if(g_stWavFormat.nSamplesPerSec == 11025)
- *num_samples = *num_samples * 4;
-
- return TR_OK;
-
- }
-
-
- /************************************************************************
- /*
- /* avi2mp2
- /*
- /* PURPOSE: MPEG I Encoder supporting layers 1 and 2, and
- /* psychoacoustic model 2 (AT&T)
- /*
- /* SEMANTICS: One overlapping frame of audio of up to 2 channels are
- /* processed at a time in the following order:
- /* (associated routines are in parentheses)
- /*
- /* 1. Filter sliding window of data to get 32 subband
- /* samples per channel.
- /* (window_subband,filter_subband)
- /*
- /* 2. If joint stereo mode, combine left and right channels
- /* for subbands above #jsbound#.
- /* (*_combine_LR)
- /*
- /* 3. Calculate scalefactors for the frame, and if layer 2,
- /* also calculate scalefactor select information.
- /* (*_scale_factor_calc)
- /*
- /* 4. Calculate psychoacoustic masking levels using selected
- /* psychoacoustic model.
- /* (*_Psycho_One, psycho_anal)
- /*
- /* 5. Perform iterative bit allocation for subbands with low
- /* mask_to_noise ratios using masking levels from step 4.
- /* (*_main_bit_allocation)
- /*
- /* 6. If error protection flag is active, add redundancy for
- /* error protection.
- /* (*_CRC_calc)
- /*
- /* 7. Pack bit allocation, scalefactors, and scalefactor select
- /* information (layer 2) onto bitstream.
- /* (*_encode_bit_alloc,*_encode_scale,II_transmission_pattern)
- /*
- /* 8. Quantize subbands and pack them into bitstream
- /* (*_subband_quantization, *_sample_encoding)
- /*
- /************************************************************************/
-
- EXPORT TOMPGRET CALLBACK TOMPEG_PCM2MP(const char *tempfile,
- const TOMPEG_AUDIO_FORMAT* pTompegAudioFormat,
- bool(* CallBack)(TOMPEG_PROCESS_PARAM,int))
- {
- double sb_sample[2][3][SCALE_BLOCK][SBLIMIT];
- double j_sample[3][SCALE_BLOCK][SBLIMIT];
- double win_que[2][HAN_SIZE];
- unsigned int subband[2][3][SCALE_BLOCK][SBLIMIT];
-
- BitStreamStruct bs;
- layer info;
- frame_params fr_ps(&info);
- short *win_buf[2] = {NULL,NULL};
- short buffer[2][1152];
- unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
- unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
- double ltmin[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
- double snr32[32];
- short sam[2][1056];
- int whole_SpF, extra_slot = 0;
- double avg_slots_per_frame, frac_SpF, slot_lag;
- int stereo;
- bool error_protection;
- unsigned int crc;
- int i, j, k, adb;
- unsigned long bitsPerSlot, samplesPerFrame, frameNum = 0;
- unsigned long frameBits, sentBits = 0;
- unsigned long num_samples;
- long length;
-
- TOMPGRET ret;
- g_nLastSample = 0;
- g_nBytesProcessed = 0;
-
- if(pTompegAudioFormat->uAudioLayerParam!=0&&pTompegAudioFormat->uAudioLayerParam!=1)
- return TR_ERR_AUDIO_PARAM;
-
- g_pAudioStream = pTompegAudioFormat->pAudioStream;
- if (AVIStreamInfo(g_pAudioStream, &g_stAudioStreamInfo, sizeof(g_stAudioStreamInfo)))
- {
- return TR_AVISTREAMINFO_ERR;
- }
-
- AVIStreamReadFormat(g_pAudioStream, 0, 0, (long*)&length);
-
- if(AVIStreamReadFormat(g_pAudioStream, 0, &g_stWavFormat, (long*)&length))
- {
- return TR_AVISTREAMREADFORMAT_ERR;
- }
-
- /* Most large variables are declared dynamically to ensure
- compatibility with smaller machines */
-
- ZeroMemory(sb_sample,sizeof(sb_sample));
- ZeroMemory(j_sample,sizeof(j_sample));
- ZeroMemory(win_que,sizeof(win_que));
- ZeroMemory(subband,sizeof(subband));
-
- /* clear buffers */
- ZeroMemory(buffer,sizeof(buffer));
- ZeroMemory(bit_alloc,sizeof(bit_alloc));
- ZeroMemory(scfsi,sizeof(scfsi));
- ZeroMemory(scalar,sizeof(scalar));
- ZeroMemory(j_scale,sizeof(j_scale));
- ZeroMemory(ltmin,sizeof(ltmin));
- ZeroMemory(lgmin,sizeof(lgmin));
- ZeroMemory(max_sc,sizeof(max_sc));
- ZeroMemory(snr32,sizeof(snr32));
- ZeroMemory(sam,sizeof(sam));
-
- info.version = MPEG_AUDIO_ID;
-
- if((ret = set_parms(&fr_ps,
- &bs,
- &num_samples,
- tempfile,
- pTompegAudioFormat->uAudioLayerParam,
- pTompegAudioFormat->bJointStereoParam,
- pTompegAudioFormat->uAudioBitrateParam))!=TR_OK)
- {
- bs.CloseBitStreamW();
- return ret;
- }
-
- if(!hdr_to_frps(&fr_ps))
- {
- bs.CloseBitStreamW();
- return TR_HDR_TO_FRPS_ERR;
- }
- stereo = fr_ps.stereo;
- error_protection = info.error_protection;
-
- if (info.lay == 0)
- {
- bitsPerSlot = 32;
- samplesPerFrame = 384;
- }
- else
- {
- bitsPerSlot = 8;
- samplesPerFrame = 1152;
- }
- /* Figure average number of 'slots' per frame. */
- /* Bitrate means TOTAL for both channels, not per side. */
- avg_slots_per_frame = (((double)samplesPerFrame) /
- g_fSamplingFrequency[info.sampling_frequency]) *
- (((double)g_uBitRateArray[info.lay][info.bitrate_index]) /
- ((double)bitsPerSlot));
- whole_SpF = (int)(avg_slots_per_frame);
- //printf("slots/frame = %d\n",whole_SpF);
- frac_SpF = avg_slots_per_frame - (double)whole_SpF;
- slot_lag = -frac_SpF;
-
- if (frac_SpF != 0)
- {}
- else
- info.padding = 0;
-
- while(1)
- {
- const unsigned char get_audio_return = get_audio(buffer, num_samples, stereo, info.lay);
-
- if(CallBack!=NULL)
- {
- if(!CallBack(TOMPEG_PROCESS_ENCODE_MP2,100*(num_samples-g_nBytesProcessed)/num_samples))
- {
- bs.CloseBitStreamW();
- return TR_CANCEL;
- }
- }
- if(get_audio_return==0)
- {
- // æ▒ìs
- }
- else if(get_audio_return==1)
- {
- bs.CloseBitStreamW();
- return TR_ERR;
- }
- else
- {
- // while âïü[âvé╠ÅIù╣
- break;
- }
-
- //_proc( "\raudio frame %u", frameNum++); fflush(stderr);
- //_proc( "\r%5.1f%% complete", ((float)(num_samples - bytes_processed)/(float)num_samples)*100.0);
-
- win_buf[0] = &buffer[0][0];
- win_buf[1] = &buffer[1][0];
-
- if (frac_SpF != 0)
- {
- if (slot_lag > (frac_SpF-1.0) )
- {
- slot_lag -= frac_SpF;
- extra_slot = 0;
- info.padding = 0;
- /* printf("No padding for this frame\n"); */
- }
- else
- {
- extra_slot = 1;
- info.padding = 1;
- slot_lag += (1.0-frac_SpF);
- /* printf("Padding for this frame\n"); */
- }
- }
- adb = (whole_SpF+extra_slot) * bitsPerSlot;
-
- switch (info.lay)
- {
-
- /***************************** Layer I **********************************/
-
- case 0 :
- for (j=0;j<SCALE_BLOCK;j++)
- for (k=0;k<stereo;k++)
- {
- window_subband(&win_buf[k], &(win_que)[k][0], k);
- filter_subband(&(win_que)[k][0], &(sb_sample)[k][0][j][0]);
- }
-
- I_scale_factor_calc(sb_sample, scalar, stereo);
- if(fr_ps.actual_mode == MPG_MD_JOINT_STEREO)
- {
- I_combine_LR(sb_sample, j_sample);
- I_scale_factor_calc(&j_sample, &j_scale, 1);
- }
-
- put_scale(scalar, &fr_ps, max_sc);
-
- for (k=0;k<stereo;k++)
- {
- if((ret = psycho_anal(&buffer[k][0],&sam[k][0], k, info.lay, snr32,
- (double)g_fSamplingFrequency[info.sampling_frequency]*1000))!=TR_OK)
- {
- bs.CloseBitStreamW();
- return ret;
- }
- for (i=0;i<SBLIMIT;i++)
- ltmin[k][i] = (double) snr32[i];
- }
-
- if(!I_main_bit_allocation(ltmin, bit_alloc, &adb, &fr_ps))
- {
- bs.CloseBitStreamW();
- return TR_I_MAIN_BIT_ALLOCATION_ERR;
- }
-
- if (error_protection)
- I_CRC_calc(&fr_ps, bit_alloc, &crc);
-
- encode_info(&fr_ps, &bs);
-
- if (error_protection)
- encode_CRC(crc, &bs);
-
- I_encode_bit_alloc(bit_alloc, &fr_ps, &bs);
- I_encode_scale(scalar,bit_alloc,&fr_ps, &bs);
- I_subband_quantization(scalar, sb_sample, j_scale, j_sample,
- bit_alloc, subband, &fr_ps);
- I_sample_encoding(subband, bit_alloc, &fr_ps, &bs);
- for (i=0;i<adb;i++)
- bs.PutBit(0);
- //put1bit(&bs, 0);
- break;
-
- /***************************** Layer 2 **********************************/
-
- case 1 :
- for (i=0;i<3;i++)
- for (j=0;j<SCALE_BLOCK;j++)
- for (k=0;k<stereo;k++)
- {
- window_subband(&win_buf[k], &(win_que)[k][0], k);
- filter_subband(&(win_que)[k][0], &(sb_sample)[k][i][j][0]);
- }
-
- II_scale_factor_calc(sb_sample, scalar, stereo, fr_ps.sblimit);
- pick_scale(scalar, &fr_ps, max_sc);
- if(fr_ps.actual_mode == MPG_MD_JOINT_STEREO)
- {
- II_combine_LR(sb_sample, j_sample, fr_ps.sblimit);
- II_scale_factor_calc(&j_sample, &j_scale, 1, fr_ps.sblimit);
- } /* this way we calculate more mono than we need */
- /* but it is cheap */
-
- for (k=0;k<stereo;k++)
- {
- if((ret = psycho_anal(&buffer[k][0],&sam[k][0], k,
- info.lay, snr32,
- g_fSamplingFrequency[info.sampling_frequency]*1000))!=TR_OK)
- {
- return ret;
- }
- for (i=0;i<SBLIMIT;i++)
- ltmin[k][i] = snr32[i];
- }
-
- II_transmission_pattern(scalar, scfsi, &fr_ps);
- if(!II_main_bit_allocation(ltmin, scfsi, bit_alloc, &adb, &fr_ps))
- {
- return TR_II_MAIN_BIT_ALLOCATION_ERR;
- }
-
- if (error_protection)
- {
- II_CRC_calc(&fr_ps, bit_alloc, scfsi, &crc);
- }
-
- encode_info(&fr_ps, &bs);
-
- if (error_protection)
- {
- encode_CRC(crc, &bs);
- }
-
- II_encode_bit_alloc(bit_alloc, &fr_ps, &bs);
- II_encode_scale(bit_alloc, scfsi, scalar, &fr_ps, &bs);
- II_subband_quantization(scalar, sb_sample, j_scale,
- j_sample, bit_alloc, subband, &fr_ps);
- II_sample_encoding(subband, bit_alloc, &fr_ps, &bs);
- for (i=0;i<adb;i++)
- {
- bs.PutBit(0);
- }
- break;
-
- }
- frameBits = bs.sstell() - sentBits;
-
- sentBits += frameBits;
-
- }
-
- bs.CloseBitStreamW();
-
- return TR_OK;
- }
-
-
- void low_pass(short* buffer,unsigned int length,unsigned int stereo,unsigned int mult)
- {
- static short work_buffer[PCM_BUFFER + 8];
- unsigned int i;
- static bool init = false;
- static short pass1[8], pass2[8], pass3[8], pass4[8];
-
- if(!init)
- {
- for(i=0; i<8; i++)
- {
- work_buffer[i] = 0; // first time thru we need 0's in first 8 samples
- pass1[i] = pass2[i] = pass3[i] = pass4[i] = 0;
- }
- init = true;
- }
-
- for(i=0; i<length; i++)
- work_buffer[i+8] = buffer[i]; // copy samples to work buffer
-
- // retrieve first 8 samples from last time
- for(i=0; i<8; i++)
- work_buffer[i] = pass1[i];
-
- //save last 8 samples for next time
- for(i=0; i<8; i++)
- pass1[i] = buffer[length - 8 + i];
-
- filter( work_buffer, length, stereo, mult); //apply 1st order filter
-
- // a first order filter really doesn't roll off quickly enough, but if we
- // apply it four times, it does.
-
- for(i=0; i<8; i++)
- work_buffer[i] = pass2[i];
- for(i=0; i<8; i++)
- pass2[i] = buffer[length - 8 + i];
- filter( work_buffer, length, stereo, mult); //apply 1st order filter
-
- for(i=0; i<8; i++)
- work_buffer[i] = pass3[i];
- for(i=0; i<8; i++)
- pass3[i] = buffer[length - 8 + i];
- filter( work_buffer, length, stereo, mult); //apply 1st order filter
-
- for(i=0; i<8; i++)
- work_buffer[i] = pass4[i];
- for(i=0; i<8; i++)
- pass4[i] = buffer[length - 8 + i];
- filter( work_buffer, length, stereo, mult); //apply 1st order filter
-
- for(i=0; i<length; i++)
- buffer[i] = work_buffer [i+8];
- }
-
- void filter(short* work_buffer,unsigned int length,unsigned int stereo,unsigned int mult)
- {
- short temp_buffer[PCM_BUFFER];
- unsigned int i;
-
- if(stereo)
- {
- if(mult == 2)
- {
- //stereo at 22050
- for(i=0; i<length; i++)
- temp_buffer[i] = (short)(0.612626*work_buffer[i+8] +
- 0.375311*work_buffer[i+6]);
- }
- else
- {
- //stereo at 11025
- for(i=0; i<length; i++)
- temp_buffer[i] = (short)(0.516851*work_buffer[i+8] +
- 0.267135*work_buffer[i+6] +
- 0.138069*work_buffer[i+4] +
- 0.071361*work_buffer[i+2]);
- }
- }
- else // mono
- {
- if(mult == 2)
- {
- //mono at 22050
- for (i=0; i<length; i++)
- temp_buffer[i] = (short)(0.612626*work_buffer[i+8] +
- 0.375311*work_buffer[i+7]);
- }
- else
- {
- //mono at 11025
- for (i=0; i<length; i++)
- temp_buffer[i] = (short)(0.516851*work_buffer[i+8] +
- 0.267135*work_buffer[i+7] +
- 0.138069*work_buffer[i+6] +
- 0.071361*work_buffer[i+5]);
- }
- }
- for(i=0; i<length; i++)
- work_buffer[i+8] = temp_buffer[i];
- }
-
- /************************************************************************/
- /*
- /* read_samples()
- /*
- /* PURPOSE: reads the PCM samples from avi file to the buffer
- /*
- /* Restrictions: only works with uncompressed 8 & 16 bit audio at
- /* at 11.025, 22.05, and 44.1Khz sample rate
- /*
- /* SEMANTICS:
- /* Reads #samples_read# number of shorts from #pAudioStream# avi file
- /* into #sample_buffer[]#. If 8 bit, data is expanded to 16 bit.
- /* If sample rate is 11.025 or 22.05 Khz, then duplicate (or quadruple)
- /* samples and apply low pass filter. Returns the number of samples read.
- /*
- /************************************************************************/
- static unsigned long read_samples(short sample_buffer[PCM_BUFFER],
- const long num_samples,
- const long frame_size,
- bool* error)
- {
- long samples_read, written,nsamples;
- static unsigned long samples_to_read = num_samples;
- long avi_samples;
- int result;
- int i;
- short *dest, *source;
- unsigned char *csource;
-
- if(g_nBytesProcessed == num_samples)
- {
- if(error!=NULL)
- *error = true;
- return 0;
- }
- if(error!=NULL)
- *error = false;
-
- if (samples_to_read >= frame_size)
- samples_read = frame_size;
- else
- samples_read = samples_to_read;
-
- if (g_stWavFormat.nChannels == 2)
- avi_samples = samples_read/2; // stereo - request half as many avi samples!
- else
- avi_samples = samples_read;
-
- if(g_stWavFormat.nSamplesPerSec == 22050)
- avi_samples = avi_samples/2; // will have to double samples
-
- if(g_stWavFormat.nSamplesPerSec == 11025)
- avi_samples = avi_samples/4; // will have to quadruple samples
-
- result=-1; // See below
- if (avi_samples==0)
- nsamples=0;
- else
- result = AVIStreamRead(g_pAudioStream,
- g_nLastSample, avi_samples,
- sample_buffer,
- (PCM_BUFFER)*sizeof(short),
- &written, &nsamples);
-
- g_nLastSample += nsamples;
-
- if(avi_samples != nsamples)
- {
- csource = (unsigned char*)sample_buffer;
- for(i = nsamples; i < avi_samples; i++)
- {
- //bad_audio_count++;
- if(g_stWavFormat.wBitsPerSample == 8)
- {
- *csource++ = 128;
- i++;
- }
- else
- {
- *csource++ = 0;
- *csource++ = 0;
- }
- }
- }
-
- // Check, if there are no more samples to read and no new audio-files to open
- // if samples_to_read==0 and result!=0 we have to skip the audio manipulation
- // section to prevent running out of the buffer area. Apr. 10, 2000
- // Volker Bartheld (entwicklung@reetcom.de)
-
- if(samples_to_read&&!result)
- {
- samples_to_read -= samples_read;
-
- if(g_stWavFormat.wBitsPerSample == 8)
- {
- dest = sample_buffer + written - 1;
- csource = ((unsigned char*)(sample_buffer)) + written - 1;
- // audio is 8 bit, we have to expand it to 16 bit
- for(i = 0; i < written; i++)
- *dest-- = (*csource-- - 128) << 8;
- written = written * 2;
- }
-
- if((g_stWavFormat.nSamplesPerSec == 22050)
- &&(g_stWavFormat.nChannels == 1))
- {
- // we have to double up the samples
- dest = sample_buffer + PCM_BUFFER - 1;
- source = sample_buffer + PCM_BUFFER/2 - 1;
- for(i = 0; i < PCM_BUFFER/2; i++)
- {
- *dest-- = *source;
- *dest-- = *source--;
- }
- low_pass(sample_buffer, samples_read, 0, 2);
- }
-
- if((g_stWavFormat.nSamplesPerSec == 22050)
- &&(g_stWavFormat.nChannels == 2))
- {
- // we have to double up the samples
- dest = sample_buffer + PCM_BUFFER - 1;
- source = sample_buffer + PCM_BUFFER/2 - 1;
- for(i = 0; i < PCM_BUFFER/4; i++)
- {
- *dest = *source;
- *(dest - 2) = *source--;
- dest--;
- *dest = *source;
- *(dest - 2) = *source--;
- dest = dest - 3;
- }
- low_pass(sample_buffer, samples_read, 1, 2);
- }
-
- if((g_stWavFormat.nSamplesPerSec == 11025)
- &&(g_stWavFormat.nChannels == 1))
- {
- // we have to quadruple samples
- dest = sample_buffer + PCM_BUFFER - 1;
- source = sample_buffer + PCM_BUFFER/4 - 1;
- for(i = 0; i < PCM_BUFFER/4; i++)
- {
- *dest-- = *source;
- *dest-- = *source;
- *dest-- = *source;
- *dest-- = *source--;
- }
- low_pass(sample_buffer, samples_read, 0, 4);
- }
-
- if((g_stWavFormat.nSamplesPerSec == 11025)
- &&(g_stWavFormat.nChannels == 2))
- {
- // we have to quadruple samples
- dest = sample_buffer + PCM_BUFFER - 1;
- source = sample_buffer + PCM_BUFFER/4 - 1;
- for(i = 0; i < PCM_BUFFER/8; i++)
- {
- *dest = *source;
- *(dest - 2) = *source;
- *(dest - 4) = *source;
- *(dest - 6) = *source--;
- dest--;
- *dest = *source;
- *(dest - 2) = *source;
- *(dest - 4) = *source;
- *(dest - 6) = *source--;
- dest = dest - 7;
- }
- low_pass(sample_buffer, samples_read, 1, 4);
- }
-
- if (samples_read < frame_size && samples_read > 0)
- {
- //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;
- }
- g_nBytesProcessed = samples_to_read;
-
- return samples_read;
- }
- else
- return 0; // No more samples to read from AVI-File
- }
-
- /************************************************************************/
- /*
- /* 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][]#
- /*
- /************************************************************************/
-
- static inline unsigned char get_audio(short buffer[2][1152],unsigned long num_samples,
- int stereo,int lay)
- {
- int j;
- short insamp[2304];
- unsigned long samples_read;
- bool error = false;
-
- if (lay == 0)
- {
- if(stereo == 2)
- { /* layer 1, stereo */
- samples_read = read_samples(insamp, num_samples,
- (unsigned long) 768,
- &error);
- for(j=0;j<448;j++)
- {
- if(j<64)
- {
- buffer[0][j] = buffer[0][j+384];
- buffer[1][j] = buffer[1][j+384];
- }
- else
- {
- buffer[0][j] = insamp[2*j-128];
- buffer[1][j] = insamp[2*j-127];
- }
- }
- }
- else
- { /* layer 1, mono */
- samples_read = read_samples(insamp, num_samples,
- (unsigned long) 384,
- &error);
- for(j=0;j<448;j++)
- {
- if(j<64)
- {
- buffer[0][j] = buffer[0][j+384];
- buffer[1][j] = 0;
- }
- else
- {
- buffer[0][j] = insamp[j-64];
- buffer[1][j] = 0;
- }
- }
- }
- }
- else
- {
- if(stereo == 2)
- { /* layer 2 (or 3), stereo */
- samples_read = read_samples(insamp, num_samples,
- (unsigned long) 2304,
- &error);
- for(j=0;j<1152;j++)
- {
- buffer[0][j] = insamp[2*j];
- buffer[1][j] = insamp[2*j+1];
- }
- }
- else
- { /* layer 2 (or 3), mono */
- samples_read = read_samples(insamp, num_samples,
- (unsigned long) 1152,
- &error);
- for(j=0;j<1152;j++)
- {
- buffer[0][j] = insamp[j];
- buffer[1][j] = 0;
- }
- }
- }
- if(samples_read>0)
- {
- return 0;
- }
- else if(error)
- {
- return 1;
- }
- return 2;
- }
-
- /************************************************************************/
- /*
- /* 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#
- /*
- /************************************************************************/
-
- static void window_subband(short** buffer,double z[],int k)
- {
- static double x[2][HAN_SIZE];
- int i, j;
- static off[2] = {0,0};
- static bool init = false;
- static const double c[HAN_SIZE] =
- {
- 0.000000000, -0.000000477, -0.000000477, -0.000000477,
- -0.000000477, -0.000000477, -0.000000477, -0.000000954,
- -0.000000954, -0.000000954, -0.000000954, -0.000001431,
- -0.000001431, -0.000001907, -0.000001907, -0.000002384,
- -0.000002384, -0.000002861, -0.000003338, -0.000003338,
- -0.000003815, -0.000004292, -0.000004768, -0.000005245,
- -0.000006199, -0.000006676, -0.000007629, -0.000008106,
- -0.000009060, -0.000010014, -0.000011444, -0.000012398,
- -0.000013828, -0.000014782, -0.000016689, -0.000018120,
- -0.000019550, -0.000021458, -0.000023365, -0.000025272,
- -0.000027657, -0.000030041, -0.000032425, -0.000034809,
- -0.000037670, -0.000040531, -0.000043392, -0.000046253,
- -0.000049591, -0.000052929, -0.000055790, -0.000059605,
- -0.000062943, -0.000066280, -0.000070095, -0.000073433,
- -0.000076771, -0.000080585, -0.000083923, -0.000087261,
- -0.000090599, -0.000093460, -0.000096321, -0.000099182,
- 0.000101566, 0.000103951, 0.000105858, 0.000107288,
- 0.000108242, 0.000108719, 0.000108719, 0.000108242,
- 0.000106812, 0.000105381, 0.000102520, 0.000099182,
- 0.000095367, 0.000090122, 0.000084400, 0.000077724,
- 0.000069618, 0.000060558, 0.000050545, 0.000039577,
- 0.000027180, 0.000013828, -0.000000954, -0.000017166,
- -0.000034332, -0.000052929, -0.000072956, -0.000093937,
- -0.000116348, -0.000140190, -0.000165462, -0.000191212,
- -0.000218868, -0.000247478, -0.000277042, -0.000307560,
- -0.000339031, -0.000371456, -0.000404358, -0.000438213,
- -0.000472546, -0.000507355, -0.000542164, -0.000576973,
- -0.000611782, -0.000646591, -0.000680923, -0.000714302,
- -0.000747204, -0.000779152, -0.000809669, -0.000838757,
- -0.000866413, -0.000891685, -0.000915051, -0.000935555,
- -0.000954151, -0.000968933, -0.000980854, -0.000989437,
- -0.000994205, -0.000995159, -0.000991821, -0.000983715,
- 0.000971317, 0.000953674, 0.000930786, 0.000902653,
- 0.000868797, 0.000829220, 0.000783920, 0.000731945,
- 0.000674248, 0.000610352, 0.000539303, 0.000462532,
- 0.000378609, 0.000288486, 0.000191689, 0.000088215,
- -0.000021458, -0.000137329, -0.000259876, -0.000388145,
- -0.000522137, -0.000661850, -0.000806808, -0.000956535,
- -0.001111031, -0.001269817, -0.001432419, -0.001597881,
- -0.001766682, -0.001937389, -0.002110004, -0.002283096,
- -0.002457142, -0.002630711, -0.002803326, -0.002974033,
- -0.003141880, -0.003306866, -0.003467083, -0.003622532,
- -0.003771782, -0.003914356, -0.004048824, -0.004174709,
- -0.004290581, -0.004395962, -0.004489899, -0.004570484,
- -0.004638195, -0.004691124, -0.004728317, -0.004748821,
- -0.004752159, -0.004737377, -0.004703045, -0.004649162,
- -0.004573822, -0.004477024, -0.004357815, -0.004215240,
- -0.004049301, -0.003858566, -0.003643036, -0.003401756,
- 0.003134727, 0.002841473, 0.002521515, 0.002174854,
- 0.001800537, 0.001399517, 0.000971317, 0.000515938,
- 0.000033379, -0.000475883, -0.001011848, -0.001573563,
- -0.002161503, -0.002774239, -0.003411293, -0.004072189,
- -0.004756451, -0.005462170, -0.006189346, -0.006937027,
- -0.007703304, -0.008487225, -0.009287834, -0.010103703,
- -0.010933399, -0.011775017, -0.012627602, -0.013489246,
- -0.014358521, -0.015233517, -0.016112804, -0.016994476,
- -0.017876148, -0.018756866, -0.019634247, -0.020506859,
- -0.021372318, -0.022228718, -0.023074150, -0.023907185,
- -0.024725437, -0.025527000, -0.026310921, -0.027073860,
- -0.027815342, -0.028532982, -0.029224873, -0.029890060,
- -0.030526638, -0.031132698, -0.031706810, -0.032248020,
- -0.032754898, -0.033225536, -0.033659935, -0.034055710,
- -0.034412861, -0.034730434, -0.035007000, -0.035242081,
- -0.035435200, -0.035586357, -0.035694122, -0.035758972,
- 0.035780907, 0.035758972, 0.035694122, 0.035586357,
- 0.035435200, 0.035242081, 0.035007000, 0.034730434,
- 0.034412861, 0.034055710, 0.033659935, 0.033225536,
- 0.032754898, 0.032248020, 0.031706810, 0.031132698,
- 0.030526638, 0.029890060, 0.029224873, 0.028532982,
- 0.027815342, 0.027073860, 0.026310921, 0.025527000,
- 0.024725437, 0.023907185, 0.023074150, 0.022228718,
- 0.021372318, 0.020506859, 0.019634247, 0.018756866,
- 0.017876148, 0.016994476, 0.016112804, 0.015233517,
- 0.014358521, 0.013489246, 0.012627602, 0.011775017,
- 0.010933399, 0.010103703, 0.009287834, 0.008487225,
- 0.007703304, 0.006937027, 0.006189346, 0.005462170,
- 0.004756451, 0.004072189, 0.003411293, 0.002774239,
- 0.002161503, 0.001573563, 0.001011848, 0.000475883,
- -0.000033379, -0.000515938, -0.000971317, -0.001399517,
- -0.001800537, -0.002174854, -0.002521515, -0.002841473,
- 0.003134727, 0.003401756, 0.003643036, 0.003858566,
- 0.004049301, 0.004215240, 0.004357815, 0.004477024,
- 0.004573822, 0.004649162, 0.004703045, 0.004737377,
- 0.004752159, 0.004748821, 0.004728317, 0.004691124,
- 0.004638195, 0.004570484, 0.004489899, 0.004395962,
- 0.004290581, 0.004174709, 0.004048824, 0.003914356,
- 0.003771782, 0.003622532, 0.003467083, 0.003306866,
- 0.003141880, 0.002974033, 0.002803326, 0.002630711,
- 0.002457142, 0.002283096, 0.002110004, 0.001937389,
- 0.001766682, 0.001597881, 0.001432419, 0.001269817,
- 0.001111031, 0.000956535, 0.000806808, 0.000661850,
- 0.000522137, 0.000388145, 0.000259876, 0.000137329,
- 0.000021458, -0.000088215, -0.000191689, -0.000288486,
- -0.000378609, -0.000462532, -0.000539303, -0.000610352,
- -0.000674248, -0.000731945, -0.000783920, -0.000829220,
- -0.000868797, -0.000902653, -0.000930786, -0.000953674,
- 0.000971317, 0.000983715, 0.000991821, 0.000995159,
- 0.000994205, 0.000989437, 0.000980854, 0.000968933,
- 0.000954151, 0.000935555, 0.000915051, 0.000891685,
- 0.000866413, 0.000838757, 0.000809669, 0.000779152,
- 0.000747204, 0.000714302, 0.000680923, 0.000646591,
- 0.000611782, 0.000576973, 0.000542164, 0.000507355,
- 0.000472546, 0.000438213, 0.000404358, 0.000371456,
- 0.000339031, 0.000307560, 0.000277042, 0.000247478,
- 0.000218868, 0.000191212, 0.000165462, 0.000140190,
- 0.000116348, 0.000093937, 0.000072956, 0.000052929,
- 0.000034332, 0.000017166, 0.000000954, -0.000013828,
- -0.000027180, -0.000039577, -0.000050545, -0.000060558,
- -0.000069618, -0.000077724, -0.000084400, -0.000090122,
- -0.000095367, -0.000099182, -0.000102520, -0.000105381,
- -0.000106812, -0.000108242, -0.000108719, -0.000108719,
- -0.000108242, -0.000107288, -0.000105858, -0.000103951,
- 0.000101566, 0.000099182, 0.000096321, 0.000093460,
- 0.000090599, 0.000087261, 0.000083923, 0.000080585,
- 0.000076771, 0.000073433, 0.000070095, 0.000066280,
- 0.000062943, 0.000059605, 0.000055790, 0.000052929,
- 0.000049591, 0.000046253, 0.000043392, 0.000040531,
- 0.000037670, 0.000034809, 0.000032425, 0.000030041,
- 0.000027657, 0.000025272, 0.000023365, 0.000021458,
- 0.000019550, 0.000018120, 0.000016689, 0.000014782,
- 0.000013828, 0.000012398, 0.000011444, 0.000010014,
- 0.000009060, 0.000008106, 0.000007629, 0.000006676,
- 0.000006199, 0.000005245, 0.000004768, 0.000004292,
- 0.000003815, 0.000003338, 0.000003338, 0.000002861,
- 0.000002384, 0.000002384, 0.000001907, 0.000001907,
- 0.000001431, 0.000001431, 0.000000954, 0.000000954,
- 0.000000954, 0.000000954, 0.000000477, 0.000000477,
- 0.000000477, 0.000000477, 0.000000477, 0.000000477
- };
- if (!init)
- {
- for (i=0;i<2;i++)
- for (j=0;j<HAN_SIZE;j++)
- x[i][j] = 0;
- init = true;
- }
-
- /* replace 32 oldest samples with 32 new samples */
- for (i=0;i<32;i++)
- x[k][31-i+off[k]] = (double) *(*buffer)++/SCALE;
- /* shift samples into proper window positions */
- 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#
- /*
- /************************************************************************/
-
- static inline void create_ana_filter(double filter[SBLIMIT][64])
- {
- 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.
- /*
- /************************************************************************/
-
- static void filter_subband(double z[HAN_SIZE],double s[SBLIMIT])
- {
- double y[64];
- int i,j;
- static bool init = false;
- static double m[SBLIMIT][64];
- long SIZE_OF_MM;
- SIZE_OF_MM = SBLIMIT*64;
- SIZE_OF_MM *= 8;
- if(!init)
- {
- create_ana_filter(m);
- init = true;
- }
- 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];
- }
-
- /************************************************************************/
- /*
- /* encode_info()
- /*
- /* PURPOSE: Puts the syncword and header information on the output
- /* bitstream.
- /*
- /************************************************************************/
-
- static void encode_info(frame_params* fr_ps,BitStreamStruct* bs)
- {
- layer *info = fr_ps->header;
-
- bs->PutBitStream(0xfff,12);
- //putabits(bs,0xfff,12); /* syncword 12 bits */
- bs->PutBit(info->version);
- //put1bit(bs,info->version); /* ID 1 bit */
- bs->PutBitStream(3-info->lay,2);
- //putabits(bs,4-info->lay,2); /* layer 2 bits */
- bs->PutBit(!info->error_protection);
- //put1bit(bs,!info->error_protection); /* bit set => no err prot */
- bs->PutBitStream(info->bitrate_index,4);
- // putabits(bs,info->bitrate_index,4);
- bs->PutBitStream(info->sampling_frequency,2);
- //putabits(bs,info->sampling_frequency,2);
- bs->PutBit(info->padding);
- //put1bit(bs,info->padding);
- bs->PutBit(info->extension);
- //put1bit(bs,info->extension); /* private_bit */
- bs->PutBitStream(info->mode,2);
- //putabits(bs,info->mode,2);
- bs->PutBitStream(info->mode_ext,2);
- //putabits(bs,info->mode_ext,2);
- bs->PutBit(info->copyright);
- //put1bit(bs,info->copyright);
- bs->PutBit(info->original);
- //put1bit(bs,info->original);
- bs->PutBitStream(info->emphasis,2);
- //putabits(bs,info->emphasis,2);
- }
- /************************************************************************/
- /*
- /* 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
- /*
- /************************************************************************/
-
- static inline void I_combine_LR(double sb_sample[2][3][SCALE_BLOCK][SBLIMIT],
- double joint_sample[3][SCALE_BLOCK][SBLIMIT])
- { /* 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][smp][sb] = .5 *
- (sb_sample[0][0][smp][sb] + sb_sample[1][0][smp][sb]);
- }
-
- static inline void II_combine_LR(double sb_sample[2][3][SCALE_BLOCK][SBLIMIT],
- double joint_sample[3][SCALE_BLOCK][SBLIMIT],
- int sblimit)
- {
- /* 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[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 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-subband samples for a given
- /* subband.
- /*
- /************************************************************************/
-
- static void I_scale_factor_calc(double sb_sample[][3][SCALE_BLOCK][SBLIMIT],unsigned int scalar[][3][SBLIMIT],int stereo)
- {
- int i,j, k;
- double s[SBLIMIT];
-
- for (k=0;k<stereo;k++)
- {
- for (i=0;i<SBLIMIT;i++)
- for (j=1, s[i] = fabs(sb_sample[k][0][0][i]);j<SCALE_BLOCK;j++)
- if (fabs(sb_sample[k][0][j][i]) > s[i])
- s[i] = fabs(sb_sample[k][0][j][i]);
-
- for (i=0;i<SBLIMIT;i++)
- for (j=SCALE_RANGE-2,scalar[k][0][i]=0;j>=0;j--) /* $A 6/16/92 */
- if (s[i] <= g_fMultiple[j])
- {
- scalar[k][0][i] = j;
- break;
- }
- }
- }
-
- /******************************** Layer II ******************************/
-
- static void II_scale_factor_calc(double sb_sample[][3][SCALE_BLOCK][SBLIMIT],
- unsigned int scalar[][3][SBLIMIT],
- int stereo,int sblimit)
- {
- int i,j, k,t;
- double s[SBLIMIT];
-
- for (k=0;k<stereo;k++) for (t=0;t<3;t++)
- {
- for (i=0;i<sblimit;i++)
- for (j=1, s[i] = fabs(sb_sample[k][t][0][i]);j<SCALE_BLOCK;j++)
- if (fabs(sb_sample[k][t][j][i]) > s[i])
- s[i] = fabs(sb_sample[k][t][j][i]);
-
- for (i=0;i<sblimit;i++)
- for (j=SCALE_RANGE-2,scalar[k][t][i]=0;j>=0;j--) /* $A 6/16/92 */
- if (s[i] <= g_fMultiple[j])
- {
- scalar[k][t][i] = j;
- break;
- }
- for (i=sblimit;i<SBLIMIT;i++)
- scalar[k][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)
- /*
- /************************************************************************/
-
- static inline void pick_scale(unsigned int scalar[2][3][SBLIMIT],
- frame_params* fr_ps,
- double max_sc[2][SBLIMIT])
- {
- int i,j,k;
- unsigned int max;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
-
- for (k=0;k<stereo;k++)
- for (i=0;i<sblimit;max_sc[k][i] = g_fMultiple[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[0][i] = max_sc[1][i] = 1E-20;
- }
-
- /************************************************************************
- /*
- /* put_scale (Layer I)
- /*
- /* PURPOSE:Sets #max_sc# to the scalefactor index in #scalar.
- /* This is used by Psychoacoustic Model I
- /*
- /************************************************************************/
-
- static inline void put_scale(unsigned int scalar[][3][SBLIMIT],
- const frame_params* fr_ps,
- double max_sc[2][SBLIMIT])
- {
- int i,k;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
-
- for (k=0;k<stereo;k++)
- for (i=0;i<SBLIMIT;i++)
- max_sc[k][i] = g_fMultiple[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.
- /*
- /************************************************************************/
-
- static inline void II_transmission_pattern(unsigned int scalar[2][3][SBLIMIT],
- unsigned int scfsi[2][SBLIMIT],
- frame_params* fr_ps)
- {
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- int dscf[2];
- int sclass[2],i,j,k;
- static int pattern[5][5] = {0x123, 0x122, 0x122, 0x133, 0x123,
- 0x113, 0x111, 0x111, 0x444, 0x113,
- 0x111, 0x111, 0x111, 0x333, 0x113,
- 0x222, 0x222, 0x222, 0x333, 0x123,
- 0x123, 0x122, 0x122, 0x133, 0x123};
-
- for (k=0;k<stereo;k++)
- for (i=0;i<sblimit;i++)
- {
- dscf[0] = (scalar[k][0][i]-scalar[k][1][i]);
- dscf[1] = (scalar[k][1][i]-scalar[k][2][i]);
- for (j=0;j<2;j++)
- {
- if (dscf[j]<=-3)
- sclass[j] = 0;
- else if (dscf[j] > -3 && dscf[j] <0)
- sclass[j] = 1;
- else if (dscf[j] == 0)
- sclass[j] = 2;
- else if (dscf[j] > 0 && dscf[j] < 3)
- sclass[j] = 3;
- else sclass[j] = 4;
- }
- switch (pattern[sclass[0]][sclass[1]])
- {
- case 0x123 :
- scfsi[k][i] = 0;
- break;
- case 0x122 :
- scfsi[k][i] = 3;
- scalar[k][2][i] = scalar[k][1][i];
- break;
- case 0x133 :
- scfsi[k][i] = 3;
- scalar[k][1][i] = scalar[k][2][i];
- break;
- case 0x113 :
- scfsi[k][i] = 1;
- scalar[k][1][i] = scalar[k][0][i];
- break;
- case 0x111 :
- scfsi[k][i] = 2;
- scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
- break;
- case 0x222 :
- scfsi[k][i] = 2;
- scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i];
- break;
- case 0x333 :
- scfsi[k][i] = 2;
- scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i];
- break;
- case 0x444 :
- scfsi[k][i] = 2;
- if (scalar[k][0][i] > scalar[k][2][i])
- scalar[k][0][i] = scalar[k][2][i];
- scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
- }
- }
- }
-
- /************************************************************************
- /*
- /* I_encode_scale (Layer I)
- /* II_encode_scale (Layer II)
- /*
- /* PURPOSE:The encoded scalar factor information is arranged and
- /* queued into the output fifo to be transmitted.
- /*
- /* For Layer II, the three scale factors associated with
- /* a given subband and channel are transmitted in accordance
- /* with the scfsi, which is transmitted first.
- /*
- /************************************************************************/
-
- static inline void I_encode_scale(
- unsigned int scalar[2][3][SBLIMIT],
- const unsigned int bit_alloc[2][SBLIMIT],
- const frame_params* fr_ps,
- BitStreamStruct* bs)
- {
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- int i,j;
-
- for (i=0;i<SBLIMIT;i++)
- for (j=0;j<stereo;j++)
- if (bit_alloc[j][i])
- bs->PutBitStream(scalar[j][0][i],6);
- //putabits(bs,scalar[j][0][i],6);
- }
-
- /***************************** Layer II ********************************/
-
- static inline void II_encode_scale(unsigned int bit_alloc[2][SBLIMIT],
- unsigned int scfsi[2][SBLIMIT],
- unsigned int scalar[2][3][SBLIMIT],
- frame_params* fr_ps,BitStreamStruct* bs)
- {
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- int i,j,k;
-
- for (i=0;i<sblimit;i++)
- for (k=0;k<stereo;k++)
- if (bit_alloc[k][i])
- bs->PutBitStream(scfsi[k][i],2);
- //putabits(bs,scfsi[k][i],2);
-
- for (i=0;i<sblimit;i++)
- for (k=0;k<stereo;k++)
- if (bit_alloc[k][i]) /* above jsbound, bit_alloc[0][i] == ba[1][i] */
- switch (scfsi[k][i])
- {
- case 0:
- for (j=0;j<3;j++)
- bs->PutBitStream(scalar[k][j][i],6);
- //putabits(bs,scalar[k][j][i],6);
- break;
- case 1:
- case 3:
- bs->PutBitStream(scalar[k][0][i],6);
- //putabits(bs,scalar[k][0][i],6);
- bs->PutBitStream(scalar[k][2][i],6);
- //putabits(bs,scalar[k][2][i],6);
- break;
- case 2:
- bs->PutBitStream(scalar[k][0][i],6);
- //putabits(bs,scalar[k][0][i],6);
- }
- }
-
- /*=======================================================================\
- | |
- | The following routines are done after the masking threshold |
- | has been calculated by the fft analysis routines in the Psychoacoustic |
- | model. Using the MNR calculated, the actual number of bits allocated |
- | to each subband is found iteratively. |
- | |
- \=======================================================================*/
-
- /************************************************************************
- /*
- /* I_bits_for_nonoise (Layer I)
- /* II_bits_for_nonoise (Layer II)
- /*
- /* PURPOSE:Returns the number of bits required to produce a
- /* mask-to-noise ratio better or equal to the noise/no_noise threshold.
- /*
- /* SEMANTICS:
- /* bbal = # bits needed for encoding bit allocation
- /* bsel = # bits needed for encoding scalefactor select information
- /* banc = # bits needed for ancillary data (header info included)
- /*
- /* For each subband and channel, will add bits until one of the
- /* following occurs:
- /* - Hit maximum number of bits we can allocate for that subband
- /* - MNR is better than or equal to the minimum masking level
- /* (NOISY_MIN_MNR)
- /* Then the bits required for scalefactors, scfsi, bit allocation,
- /* and the subband samples are tallied (#req_bits#) and returned.
- /*
- /* (NOISY_MIN_MNR) is the smallest MNR a subband can have before it is
- /* counted as 'noisy' by the logic which chooses the number of JS
- /* subbands.
- /*
- /* Joint stereo is supported.
- /*
- /************************************************************************/
-
- static double g_fSnr[18] = {0.00, 7.00, 11.00, 16.00, 20.84,
- 25.28, 31.59, 37.75, 43.84,
- 49.89, 55.93, 61.96, 67.98, 74.01,
- 80.03, 86.05, 92.01, 98.01};
-
- static int I_bits_for_nonoise(const double perm_smr[2][SBLIMIT],
- const frame_params* fr_ps)
- {
- int i,j,k;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- int req_bits = 0;
-
- /* initial b_anc (header) allocation bits */
- req_bits = 32 + 4 * ( (jsbound * stereo) + (SBLIMIT-jsbound) );
-
- for(i=0; i<SBLIMIT; ++i)
- for(j=0; j<((i<jsbound)?stereo:1); ++j)
- {
- for(k=0;k<14; ++k)
- if( (-perm_smr[j][i] + g_fSnr[k]) >= NOISY_MIN_MNR)
- break; /* we found enough bits */
- if(stereo == 2 && i >= jsbound) /* check other JS channel */
- for(;k<14; ++k)
- if( (-perm_smr[1-j][i] + g_fSnr[k]) >= NOISY_MIN_MNR)
- break;
- if(k>0)
- req_bits += (k+1)*SCALE_BLOCK + 6*((i>=jsbound)?stereo:1);
- }
- return req_bits;
- }
-
- /***************************** Layer II ********************************/
-
- static int II_bits_for_nonoise(const double perm_smr[2][SBLIMIT],
- const unsigned int scfsi[2][SBLIMIT],
- const frame_params* fr_ps)
- {
- int sb,ch,ba;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
- int req_bits = 0, bbal = 0, berr = 0, banc = 32;
- int maxAlloc, sel_bits, sc_bits, smp_bits;
- static const int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */
-
- /* added 92-08-11 shn */
- if (fr_ps->header->error_protection) berr=16; else berr=0;
-
- for (sb=0; sb<jsbound; ++sb)
- bbal += stereo * (*alloc)[sb][0].bits;
- for (sb=jsbound; sb<sblimit; ++sb)
- bbal += (*alloc)[sb][0].bits;
- req_bits = banc + bbal + berr;
-
- for(sb=0; sb<sblimit; ++sb)
- for(ch=0; ch<((sb<jsbound)?stereo:1); ++ch) {
- maxAlloc = (1<<(*alloc)[sb][0].bits)-1;
- sel_bits = sc_bits = smp_bits = 0;
- for(ba=0;ba<maxAlloc-1; ++ba)
- if( (-perm_smr[ch][sb] + g_fSnr[(*alloc)[sb][ba].quant+((ba>0)?1:0)])
- >= NOISY_MIN_MNR)
- break; /* we found enough bits */
- if(stereo == 2 && sb >= jsbound) /* check other JS channel */
- for(;ba<maxAlloc-1; ++ba)
- if( (-perm_smr[1-ch][sb]+ g_fSnr[(*alloc)[sb][ba].quant+((ba>0)?1:0)])
- >= NOISY_MIN_MNR)
- break;
- if(ba>0) {
- smp_bits = SCALE_BLOCK * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
- /* scale factor bits required for subband */
- sel_bits = 2;
- sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
- if(stereo == 2 && sb >= jsbound) {
- /* each new js sb has L+R scfsis */
- sel_bits += 2;
- sc_bits += 6 * sfsPerScfsi[scfsi[1-ch][sb]];
- }
- req_bits += smp_bits+sel_bits+sc_bits;
- }
- }
- return req_bits;
- }
-
- /************************************************************************
- /*
- /* I_main_bit_allocation (Layer I)
- /* II_main_bit_allocation (Layer II)
- /*
- /* PURPOSE:For joint stereo mode, determines which of the 4 joint
- /* stereo modes is needed. Then calls *_a_bit_allocation(), which
- /* allocates bits for each of the subbands until there are no more bits
- /* left, or the MNR is at the noise/no_noise threshold.
- /*
- /* SEMANTICS:
- /*
- /* For joint stereo mode, joint stereo is changed to stereo if
- /* there are enough bits to encode stereo at or better than the
- /* no-noise threshold (NOISY_MIN_MNR). Otherwise, the system
- /* iteratively allocates less bits by using joint stereo until one
- /* of the following occurs:
- /* - there are no more noisy subbands (MNR >= NOISY_MIN_MNR)
- /* - mode_ext has been reduced to 0, which means that all but the
- /* lowest 4 subbands have been converted from stereo to joint
- /* stereo, and no more subbands may be converted
- /*
- /* This function calls *_bits_for_nonoise() and *_a_bit_allocation().
- /*
- /************************************************************************/
-
- static inline bool I_main_bit_allocation(const double perm_smr[2][SBLIMIT],
- unsigned int bit_alloc[2][SBLIMIT],
- int* adb,frame_params* fr_ps)
- {
- int noisy_sbs;
- int mode, mode_ext, lay, i;
- int rq_db, av_db = *adb;
- static bool init = false;
- bool error = false;
-
- if(!init)
- {
- /* rearrange snr for layer I */
- g_fSnr[2] = g_fSnr[3];
- for (i=3;i<16;i++)
- g_fSnr[i] = g_fSnr[i+2];
- init = true;
- }
-
- if((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO)
- {
- fr_ps->header->mode = MPG_MD_STEREO;
- fr_ps->header->mode_ext = 0;
- fr_ps->jsbound = fr_ps->sblimit;
- if(rq_db = I_bits_for_nonoise(perm_smr, fr_ps) > *adb)
- {
- fr_ps->header->mode = MPG_MD_JOINT_STEREO;
- mode_ext = 4; /* 3 is least severe reduction */
- lay = fr_ps->header->lay;
- do
- {
- --mode_ext;
- if((fr_ps->jsbound = js_bound(lay, mode_ext))<0)
- {
- return false;
- }
- rq_db = I_bits_for_nonoise(perm_smr, fr_ps);
- } while( (rq_db > *adb) && (mode_ext > 0));
- fr_ps->header->mode_ext = mode_ext;
- } /* well we either eliminated noisy sbs or mode_ext == 0 */
- }
- noisy_sbs = I_a_bit_allocation(perm_smr, bit_alloc, adb, fr_ps);
- return true;
- }
-
- /***************************** Layer II ********************************/
-
- static inline bool II_main_bit_allocation(
- const double perm_smr[2][SBLIMIT],
- const unsigned int scfsi[2][SBLIMIT],
- unsigned int bit_alloc[2][SBLIMIT],
- int *adb,
- frame_params *fr_ps)
- {
- int noisy_sbs;
- int mode, mode_ext, lay;
- int rq_db, av_db = *adb;
-
- if((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO)
- {
- fr_ps->header->mode = MPG_MD_STEREO;
- fr_ps->header->mode_ext = 0;
- fr_ps->jsbound = fr_ps->sblimit;
- if((rq_db=II_bits_for_nonoise(perm_smr, scfsi, fr_ps)) > *adb)
- {
- fr_ps->header->mode = MPG_MD_JOINT_STEREO;
- mode_ext = 4; /* 3 is least severe reduction */
- lay = fr_ps->header->lay;
- do
- {
- --mode_ext;
- fr_ps->jsbound = js_bound(lay, mode_ext);
- rq_db = II_bits_for_nonoise(perm_smr, scfsi, fr_ps);
- } while( (rq_db > *adb) && (mode_ext > 0));
- fr_ps->header->mode_ext = mode_ext;
- } /* well we either eliminated noisy sbs or mode_ext == 0 */
- }
- noisy_sbs = II_a_bit_allocation(perm_smr, scfsi, bit_alloc, adb, fr_ps);
-
- return true;
- }
-
- /************************************************************************
- /*
- /* I_a_bit_allocation (Layer I)
- /* II_a_bit_allocation (Layer II)
- /*
- /* PURPOSE:Adds bits to the subbands with the lowest mask-to-noise
- /* ratios, until the maximum number of bits for the subband has
- /* been allocated.
- /*
- /* SEMANTICS:
- /* 1. Find the subband and channel with the smallest MNR (#min_sb#,
- /* and #min_ch#)
- /* 2. Calculate the increase in bits needed if we increase the bit
- /* allocation to the next higher level
- /* 3. If there are enough bits available for increasing the resolution
- /* in #min_sb#, #min_ch#, and the subband has not yet reached its
- /* maximum allocation, update the bit allocation, MNR, and bits
- /* available accordingly
- /* 4. Repeat until there are no more bits left, or no more available
- /* subbands. (A subband is still available until the maximum
- /* number of bits for the subband has been allocated, or there
- /* aren't enough bits to go to the next higher resolution in the
- /* subband.)
- /*
- /************************************************************************/
-
- static inline int I_a_bit_allocation(
- const double perm_smr[2][SBLIMIT],
- unsigned int bit_alloc[2][SBLIMIT],
- int* adb,
- const frame_params* fr_ps)
- {
- int i, k, smpl_bits, scale_bits, min_sb, min_ch, oth_ch;
- int bspl, bscf, ad, noisy_sbs, done = 0, bbal ;
- double mnr[2][SBLIMIT];
- double smallm;
- char used[2][SBLIMIT];
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- static int banc=32, berr=(fr_ps->header->error_protection?16:0);
-
- bbal = 4 * ( (jsbound * stereo) + (SBLIMIT-jsbound) );
- *adb -= bbal + berr + banc;
- ad= *adb;
-
- for (i=0;i<SBLIMIT;i++)
- for (k=0;k<stereo;k++)
- {
- mnr[k][i]=g_fSnr[0]-perm_smr[k][i];
- bit_alloc[k][i] = 0;
- used[k][i] = 0;
- }
- bspl = bscf = 0;
-
- do
- {
- /* locate the subband with minimum SMR */
- smallm = mnr[0][0]+1; min_sb = -1; min_ch = -1;
- for (i=0;i<SBLIMIT;i++) for (k=0;k<stereo;k++)
- /* go on only if there are bits left */
- if (used[k][i] != 2 && smallm > mnr[k][i])
- {
- smallm = mnr[k][i];
- min_sb = i; min_ch = k;
- }
- if(min_sb > -1) { /* there was something to find */
- /* first step of bit allocation is biggest */
- if (used[min_ch][min_sb])
- {
- smpl_bits = SCALE_BLOCK;
- scale_bits = 0;
- }
- else
- {
- smpl_bits = 24;
- scale_bits = 6;
- }
- if(min_sb >= jsbound)
- scale_bits *= stereo;
-
- /* check to see enough bits were available for */
- /* increasing resolution in the minimum band */
-
- if (ad >= bspl + bscf + scale_bits + smpl_bits)
- {
- bspl += smpl_bits; /* bit for subband sample */
- bscf += scale_bits; /* bit for scale factor */
- bit_alloc[min_ch][min_sb]++;
- used[min_ch][min_sb] = 1; /* subband has bits */
- mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb]
- + g_fSnr[bit_alloc[min_ch][min_sb]];
- /* Check if subband has been fully allocated max bits */
- if (bit_alloc[min_ch][min_sb] == 14 )
- used[min_ch][min_sb] = 2;
- }
- else /* no room to improve this band */
- used[min_ch][min_sb] = 2; /* for allocation anymore */
- if(stereo == 2 && min_sb >= jsbound)
- {
- oth_ch = 1-min_ch; /* joint-st : fix other ch */
- bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];
- used[oth_ch][min_sb] = used[min_ch][min_sb];
- mnr[oth_ch][min_sb] = -perm_smr[oth_ch][min_sb]
- + g_fSnr[bit_alloc[oth_ch][min_sb]];
- }
- }
- } while(min_sb>-1); /* i.e. still some sub-bands to find */
-
- /* Calculate the number of bits left, add on to pointed var */
- ad -= bspl+bscf;
- *adb = ad;
-
- /* see how many channels are noisy */
- noisy_sbs = 0; smallm = mnr[0][0];
- for(k=0; k<stereo; ++k)
- {
- for(i = 0; i< SBLIMIT; ++i)
- {
- if(mnr[k][i] < NOISY_MIN_MNR)
- ++noisy_sbs;
- if(smallm > mnr[k][i])
- smallm = mnr[k][i];
- }
- }
- return noisy_sbs;
- }
-
- /***************************** Layer II ********************************/
- static inline int II_a_bit_allocation(
- const double perm_smr[][SBLIMIT],
- const unsigned int scfsi[][SBLIMIT],
- unsigned int bit_alloc[][SBLIMIT],
- int* adb,
- const frame_params* fr_ps)
- {
- int i, min_ch, min_sb, oth_ch, k, increment, scale, seli, ba;
- int bspl, bscf, bsel, ad, noisy_sbs, bbal=0;
- double mnr[2][SBLIMIT], smallm;
- char used[2][SBLIMIT];
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
- static int banc=32, berr=(fr_ps->header->error_protection?16:0);
- static const int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */
-
-
- for (i=0; i<jsbound; ++i)
- bbal += stereo * (*alloc)[i][0].bits;
- for (i=jsbound; i<sblimit; ++i)
- bbal += (*alloc)[i][0].bits;
- *adb -= bbal + berr + banc;
- ad = *adb;
-
- for (i=0;i<sblimit;i++)
- for (k=0;k<stereo;k++)
- {
- mnr[k][i]=g_fSnr[0]-perm_smr[k][i];
- bit_alloc[k][i] = 0;
- used[k][i] = 0;
- }
- bspl = bscf = bsel = 0;
-
- do {
- /* locate the subband with minimum SMR */
- smallm = 999999.0; min_sb = -1; min_ch = -1;
- for (i=0;i<sblimit;i++) for(k=0;k<stereo;++k)
- if (used[k][i] != 2 && smallm > mnr[k][i]) {
- smallm = mnr[k][i];
- min_sb = i; min_ch = k;
- }
- if(min_sb > -1) { /* there was something to find */
- /* find increase in bit allocation in subband [min] */
- increment = SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]+1].group *
- (*alloc)[min_sb][bit_alloc[min_ch][min_sb]+1].bits);
- if (used[min_ch][min_sb])
- increment -= SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]].group*
- (*alloc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
-
- /* scale factor bits required for subband [min] */
- oth_ch = 1 - min_ch; /* above js bound, need both chans */
- if (used[min_ch][min_sb]) scale = seli = 0;
- else { /* this channel had no bits or scfs before */
- seli = 2;
- scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];
- if(stereo == 2 && min_sb >= jsbound) {
- /* each new js sb has L+R scfsis */
- seli += 2;
- scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
- }
- }
- /* check to see enough bits were available for */
- /* increasing resolution in the minimum band */
- if (ad >= bspl + bscf + bsel + seli + scale + increment) {
- ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */
- bspl += increment; /* bits for subband sample */
- bscf += scale; /* bits for scale factor */
- bsel += seli; /* bits for scfsi code */
- used[min_ch][min_sb] = 1; /* subband has bits */
- mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +
- g_fSnr[(*alloc)[min_sb][ba].quant+1];
- /* Check if subband has been fully allocated max bits */
- if (ba >= (1<<(*alloc)[min_sb][0].bits)-1) used[min_ch][min_sb] = 2;
- }
- else used[min_ch][min_sb] = 2; /* can't increase this alloc */
- if(min_sb >= jsbound && stereo == 2) {
- /* above jsbound, alloc applies L+R */
- ba = bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];
- used[oth_ch][min_sb] = used[min_ch][min_sb];
- mnr[oth_ch][min_sb] = -perm_smr[oth_ch][min_sb] +
- g_fSnr[(*alloc)[min_sb][ba].quant+1];
- }
- }
- } while(min_sb > -1); /* until could find no channel */
- /* Calculate the number of bits left */
- ad -= bspl+bscf+bsel; *adb = ad;
- for (i=sblimit;i<SBLIMIT;i++) for (k=0;k<stereo;k++) bit_alloc[k][i]=0;
-
- noisy_sbs = 0; smallm = mnr[0][0]; /* calc worst noise in case */
- for(k=0;k<stereo;++k) {
- for (i=0;i<sblimit;i++) {
- if (smallm > mnr[k][i]) smallm = mnr[k][i];
- if(mnr[k][i] < NOISY_MIN_MNR) ++noisy_sbs; /* noise is not masked */
-
- }
- }
-
- return noisy_sbs;
- }
-
- /************************************************************************
- /*
- /* I_subband_quantization (Layer I)
- /* II_subband_quantization (Layer II)
- /*
- /* PURPOSE:Quantizes subband samples to appropriate number of bits
- /*
- /* SEMANTICS: Subband samples are divided by their scalefactors, which
- /* makes the quantization more efficient. The scaled samples are
- /* quantized by the function a*x+b, where a and b are functions of
- /* the number of quantization levels. The result is then truncated
- /* to the appropriate number of bits and the MSB is inverted.
- /*
- /* Note that for fractional 2's complement, inverting the MSB for a
- /* negative number x is equivalent to adding 1 to it.
- /*
- /************************************************************************/
-
- static double g_fA[17] = {
- 0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,
- 0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,
- 0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,
- 0.999969482, 0.999984741 };
-
- static double g_fB[17] = {
- -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,
- -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,
- -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,
- -0.000030518, -0.000015259 };
-
- static inline void I_subband_quantization(
- unsigned int scalar[2][3][SBLIMIT],
- double sb_samples[2][3][SCALE_BLOCK][SBLIMIT],
- unsigned int j_scale[3][SBLIMIT],
- double j_samps[3][SCALE_BLOCK][SBLIMIT], /* L+R for j-stereo if necess */
- unsigned int bit_alloc[2][SBLIMIT],
- unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
- frame_params* fr_ps)
- {
- int i, j, k, n, sig;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- double d;
- static bool init = false;
-
- if (!init)
- {
- init = true;
- /* rearrange quantization coef to correspond to layer I table */
- g_fA[1] = g_fA[2];
- g_fB[1] = g_fB[2];
- for (i=2;i<15;i++)
- {
- g_fA[i] = g_fA[i+2];
- g_fB[i] = g_fB[i+2];
- }
- }
- for (j=0;j<SCALE_BLOCK;j++)
- for (i=0;i<SBLIMIT;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- if (bit_alloc[k][i])
- {
- /* for joint stereo mode, have to construct a single subband stream
- for the js channels. At present, we calculate a set of mono
- subband samples and pass them through the scaling system to
- generate an alternate normalised sample stream.
-
- Could normalise both streams (divide by their scfs), then average
- them. In bad conditions, this could give rise to spurious
- cancellations. Instead, we could just select the sb stream from
- the larger channel (higher scf), in which case _that_ channel
- would be 'properly' reconstructed, and the mate would just be a
- scaled version. Spec recommends averaging the two (unnormalised)
- subband channels, then normalising this new signal without
- actually sending this scale factor... This means looking ahead.
- */
- if(stereo == 2 && i>=jsbound)
- /* use the joint data passed in */
- d = j_samps[0][j][i] / g_fMultiple[j_scale[0][i]];
- else
- d = sb_samples[k][0][j][i] / g_fMultiple[scalar[k][0][i]];
- /* scale and quantize floating point sample */
- n = bit_alloc[k][i];
- d = d * g_fA[n-1] + g_fB[n-1];
- /* extract MSB N-1 bits from the floating point sample */
- if (d >= 0)
- sig = 1;
- else
- {
- sig = 0;
- d += 1.0;
- }
- sbband[k][0][j][i] = (unsigned int) (d * (double) (1L<<n));
- /* tag the inverted sign bit to sbband at position N */
- if (sig)
- sbband[k][0][j][i] |= 1<<n;
- }
- }
-
- /***************************** Layer II ********************************/
-
- static inline void II_subband_quantization(
- unsigned int scalar[2][3][SBLIMIT],
- double sb_samples[2][3][SCALE_BLOCK][SBLIMIT],
- unsigned int j_scale[3][SBLIMIT],
- double j_samps[3][SCALE_BLOCK][SBLIMIT],
- unsigned int bit_alloc[2][SBLIMIT],
- unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
- frame_params* fr_ps)
- {
- int i, j, k, s, n, qnt, sig;
- int stereo = fr_ps->stereo;
- int sblimit = fr_ps->sblimit;
- int jsbound = fr_ps->jsbound;
- double d;
- al_table *alloc = fr_ps->alloc;
-
- for (s=0;s<3;s++)
- for (j=0;j<SCALE_BLOCK;j++)
- for (i=0;i<sblimit;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- if (bit_alloc[k][i])
- {
- /* scale and quantize floating point sample */
- if(stereo == 2 && i>=jsbound) /* use j-stereo samples */
- d = j_samps[s][j][i] / g_fMultiple[j_scale[s][i]];
- else
- d = sb_samples[k][s][j][i] / g_fMultiple[scalar[k][s][i]];
- qnt = (*alloc)[i][bit_alloc[k][i]].quant;
- d = d * g_fA[qnt] + g_fB[qnt];
- /* extract MSB N-1 bits from the floating point sample */
- if (d >= 0)
- sig = 1;
- else
- {
- sig = 0;
- d += 1.0;
- }
- n = 0;
- while ( ( (unsigned long)(1L<<(long)n) <
- ((unsigned long) ((*alloc)[i][bit_alloc[k][i]].steps)
- & 0xffff
- )
- ) && ( n <16)
- ) n++;
- n--;
- sbband[k][s][j][i] = (unsigned int) (d * (double) (1L<<n));
- /* tag the inverted sign bit to sbband at position N */
- /* The bit inversion is a must for grouping with 3,5,9 steps
- so it is done for all subbands */
- if (sig)
- sbband[k][s][j][i] |= 1<<n;
- }
- for (s=0;s<3;s++)
- for (j=sblimit;j<SBLIMIT;j++)
- for (i=0;i<SCALE_BLOCK;i++)
- for (k=0;k<stereo;k++)
- sbband[k][s][i][j] = 0;
- }
-
- /************************************************************************
- /*
- /* I_encode_bit_alloc (Layer I)
- /* II_encode_bit_alloc (Layer II)
- /*
- /* PURPOSE:Writes bit allocation information onto bitstream
- /*
- /* Layer I uses 4 bits/subband for bit allocation information,
- /* and Layer II uses 4,3,2, or 0 bits depending on the
- /* quantization table used.
- /*
- /************************************************************************/
-
- static inline void I_encode_bit_alloc(
- const unsigned int bit_alloc[][SBLIMIT],
- const frame_params* fr_ps,
- BitStreamStruct* bs)
- {
- int i,k;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
-
- for (i=0;i<SBLIMIT;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- bs->PutBitStream(bit_alloc[k][i],4);
- //putabits(bs,bit_alloc[k][i],4);
- }
-
- /***************************** Layer II ********************************/
-
- static inline void II_encode_bit_alloc(
- const unsigned int bit_alloc[][SBLIMIT],
- const frame_params* fr_ps,
- BitStreamStruct* bs)
- {
- int i,k;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
-
- for (i=0;i<sblimit;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- bs->PutBitStream(bit_alloc[k][i],(*alloc)[i][0].bits);
- //putabits(bs,bit_alloc[k][i],(*alloc)[i][0].bits);
- }
-
- /************************************************************************
- /*
- /* I_sample_encoding (Layer I)
- /* II_sample_encoding (Layer II)
- /*
- /* PURPOSE:Put one frame of subband samples on to the bitstream
- /*
- /* SEMANTICS: The number of bits allocated per sample is read from
- /* the bit allocation information #bit_alloc#. Layer 2
- /* supports writing grouped samples for quantization steps
- /* that are not a power of 2.
- /*
- /************************************************************************/
-
- static inline void I_sample_encoding(
- unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
- unsigned int bit_alloc[2][SBLIMIT],
- frame_params* fr_ps,
- BitStreamStruct* bs)
- {
- int i,j,k;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
-
- for(j=0;j<SCALE_BLOCK;j++)
- {
- for(i=0;i<SBLIMIT;i++)
- for(k=0;k<((i<jsbound)?stereo:1);k++)
- if(bit_alloc[k][i])
- bs->PutBitStream(sbband[k][0][j][i],bit_alloc[k][i]+1);
- //putabits(bs,sbband[k][0][j][i],bit_alloc[k][i]+1);
- }
- }
-
- /***************************** Layer II ********************************/
-
- static inline void II_sample_encoding(
- unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT],
- unsigned int bit_alloc[2][SBLIMIT],
- frame_params* fr_ps,
- BitStreamStruct* bs)
- {
- unsigned int temp;
- unsigned int j,s,x,y;
- int i, k;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
-
- for (s=0;s<3;s++)
- for (j=0;j<SCALE_BLOCK;j+=3)
- for (i=0;i<sblimit;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- if (bit_alloc[k][i])
- {
- if ((*alloc)[i][bit_alloc[k][i]].group == 3)
- {
- for (x=0;x<3;x++)
- bs->PutBitStream(sbband[k][s][j+x][i],(*alloc)[i][bit_alloc[k][i]].bits);
- //putabits(bs,sbband[k][s][j+x][i],(*alloc)[i][bit_alloc[k][i]].bits);
- }
- else
- {
- y =(*alloc)[i][bit_alloc[k][i]].steps;
- temp = sbband[k][s][j][i] +
- sbband[k][s][j+1][i] * y +
- sbband[k][s][j+2][i] * y * y;
- bs->PutBitStream(temp,(*alloc)[i][bit_alloc[k][i]].bits);
- //putabits(bs,temp,(*alloc)[i][bit_alloc[k][i]].bits);
- }
- }
- }
-
- /************************************************************************
- /*
- /* encode_CRC
- /*
- /************************************************************************/
-
- static inline void encode_CRC(unsigned int crc,BitStreamStruct* bs)
- {
- bs->PutBitStream(crc, 16);
- //putabits(bs, crc, 16);
- }
-
- static inline int read_bit_alloc(int table,al_table* alloc) /* read in table, return # subbands */
- {
- unsigned int i, j, k;
-
- int sblim;
-
- switch (table)
- {
- case 1 :
- //printf("using bit allocation table 1\n");
- sblim = sblim_1;
- k = 0;
- while (k < alloc_1_size)
- {
- i = alloc_1[k][0];
- j = alloc_1[k][1];
- (*alloc)[i][j].steps = alloc_1[k][2];
- (*alloc)[i][j].bits = alloc_1[k][3];
- (*alloc)[i][j].group = alloc_1[k][4];
- (*alloc)[i][j].quant = alloc_1[k][5];
- k++;
- }
- break;
- case 2 :
- //printf("using bit allocation table 2\n");
- sblim = sblim_2;
- k = 0;
- while (k < alloc_2_size)
- {
- i = alloc_2[k][0];
- j = alloc_2[k][1];
- (*alloc)[i][j].steps = alloc_2[k][2];
- (*alloc)[i][j].bits = alloc_2[k][3];
- (*alloc)[i][j].group = alloc_2[k][4];
- (*alloc)[i][j].quant = alloc_2[k][5];
- k++;
- }
- break;
- case 3:
- sblim = sblim_3;
- k = 0;
- while (k < alloc_3_size)
- {
- i = alloc_3[k][0];
- j = alloc_3[k][1];
- (*alloc)[i][j].steps = alloc_3[k][2];
- (*alloc)[i][j].bits = alloc_3[k][3];
- (*alloc)[i][j].group = alloc_3[k][4];
- (*alloc)[i][j].quant = alloc_3[k][5];
- k++;
- }
- break;
- case 0 :
- default :
- //printf("using bit allocation table 0\n");
- sblim = sblim_0;
- k = 0;
- while (k < alloc_0_size)
- {
- i = alloc_0[k][0];
- j = alloc_0[k][1];
- (*alloc)[i][j].steps = alloc_0[k][2];
- (*alloc)[i][j].bits = alloc_0[k][3];
- (*alloc)[i][j].group = alloc_0[k][4];
- (*alloc)[i][j].quant = alloc_0[k][5];
- k++;
- }
- }
-
- return sblim;
- }
-
- /***********************************************************************
- /*
- /* Using the decoded info the appropriate possible quantization per
- /* subband table is loaded
- /*
- /**********************************************************************/
-
- static inline int pick_table(frame_params* fr_ps,bool* error) /* choose table, load if necess, return # sb's */
- {
- int table, lay, bsp, br_per_ch, sfrq;
- int sblimit = fr_ps->sblimit;
-
- lay = fr_ps->header->lay;
- bsp = fr_ps->header->bitrate_index;
- br_per_ch = g_uBitRateArray[lay][bsp] / fr_ps->stereo;
- sfrq = (int)g_fSamplingFrequency[fr_ps->header->sampling_frequency];
- /* decision rules refer to per-channel bitrates (kbits/sec/chan) */
- if((sfrq == 48 && br_per_ch >= 56) ||
- (br_per_ch >= 56 && br_per_ch <= 80))
- {
- table = 0;
- }
- else if(sfrq != 48 && br_per_ch >= 96)
- {
- table = 1;
- }
- else if(sfrq != 32 && br_per_ch <= 48)
- {
- table = 2;
- }
- else
- {
- table = 3;
- }
-
- if (fr_ps->tab_num != table)
- {
- if(!fr_ps->alloc_table())
- {
- if(error!=NULL)
- {
- *error = true;
- }
- return 0;
- }
- sblimit = read_bit_alloc(fr_ps->tab_num = table, fr_ps->alloc);
- }
- if(error!=NULL)
- {
- *error = false;
- }
- return sblimit;
- }
-
- static int js_bound(const int lay,const int m_ext)
- {
- static int jsb_table[2][4] = { { 4, 8, 12, 16 }, { 4, 8, 12, 16} }; /* lay+m_e -> jsbound */
-
- if(lay<0 || lay >1 || m_ext<0 || m_ext>3) {
- return -1;
- }
-
- return jsb_table[lay][m_ext];
- }
-
- static inline bool hdr_to_frps(frame_params* fr_ps) /* interpret data in hdr str to fields in fr_ps */
- {
- const layer *hdr = fr_ps->header; /* (or pass in as arg?) */
- bool error = false;
-
- fr_ps->actual_mode = hdr->mode;
- fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2;
- if (hdr->lay == 1)
- {
- fr_ps->sblimit = pick_table(fr_ps,&error);
- if(error)
- {
- return false;
- }
- }
- else
- fr_ps->sblimit = SBLIMIT;
-
- if(hdr->mode == MPG_MD_JOINT_STEREO)
- {
- if((fr_ps->jsbound = js_bound(hdr->lay, hdr->mode_ext))<0)
- {
- return false;
- }
- }
- else
- {
- fr_ps->jsbound = fr_ps->sblimit;
- }
-
- return true;
- }
-
-
- static inline int BitrateIndex(const int layr,const unsigned int bRate) /* convert bitrate in kbps to index */
- {
- int index;
-
- for(index = 1;index < 15; index++ )
- {
- if(g_uBitRateArray[layr][index] == bRate)
- return index;
- }
- return -1;
- }
-
- static inline int SmpFrqIndex(long sRate) /* convert samp frq in Hz to index */
- {
- if(sRate == 44100L)
- return(0);
- else if(sRate == 48000L)
- return(1);
- else if(sRate == 32000L)
- return(2);
- else {
- //_proc( "SmpFrqIndex: %ld is not a legal sample rate\n", sRate);
- return(-1); /* Error! */
- }
- }
- /*****************************************************************************
- *
- * CRC error protection package
- *
- *****************************************************************************/
-
- static inline void I_CRC_calc(frame_params* fr_ps,
- unsigned int bit_alloc[2][SBLIMIT],
- unsigned int* crc)
- {
- int i, k;
- layer *info = fr_ps->header;
- const int stereo = fr_ps->stereo;
- const int jsbound = fr_ps->jsbound;
-
- *crc = 0xffff; /* changed from '0' 92-08-11 shn */
- update_CRC(info->bitrate_index, 4, crc);
- update_CRC(info->sampling_frequency, 2, crc);
- update_CRC(info->padding, 1, crc);
- update_CRC(info->extension, 1, crc);
- update_CRC(info->mode, 2, crc);
- update_CRC(info->mode_ext, 2, crc);
- update_CRC(info->copyright, 1, crc);
- update_CRC(info->original, 1, crc);
- update_CRC(info->emphasis, 2, crc);
-
- for (i=0;i<SBLIMIT;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- update_CRC(bit_alloc[k][i], 4, crc);
- }
-
- static inline void II_CRC_calc(frame_params* fr_ps,
- unsigned int bit_alloc[2][SBLIMIT],
- unsigned int scfsi[2][SBLIMIT],
- unsigned int* crc)
- {
- int i, k;
- layer *info = fr_ps->header;
- const int stereo = fr_ps->stereo;
- const int sblimit = fr_ps->sblimit;
- const int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
-
- *crc = 0xffff; /* changed from '0' 92-08-11 shn */
- update_CRC(info->bitrate_index, 4, crc);
- update_CRC(info->sampling_frequency, 2, crc);
- update_CRC(info->padding, 1, crc);
- update_CRC(info->extension, 1, crc);
- update_CRC(info->mode, 2, crc);
- update_CRC(info->mode_ext, 2, crc);
- update_CRC(info->copyright, 1, crc);
- update_CRC(info->original, 1, crc);
- update_CRC(info->emphasis, 2, crc);
-
- for (i=0;i<sblimit;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- update_CRC(bit_alloc[k][i], (*alloc)[i][0].bits, crc);
-
- for (i=0;i<sblimit;i++)
- for (k=0;k<stereo;k++)
- if (bit_alloc[k][i])
- update_CRC(scfsi[k][i], 2, crc);
- }
-
- static void update_CRC(unsigned int data,unsigned int length,unsigned int* crc)
- {
- unsigned int masking, carry;
-
- masking = 1 << length;
-
- while((masking >>= 1)){
- carry = *crc & 0x8000;
- *crc <<= 1;
- if (!carry ^ !(data & masking))
- *crc ^= CRC16_POLYNOMIAL;
- }
- *crc &= 0xffff;
- }
- static inline bool read_absthr(double* absthr,const int sfreq_idx)
- {
- switch(sfreq_idx)
- {
- case 0:
- memcpy(absthr,absthr_0,sizeof(absthr_0));
- break;
- case 1:
- memcpy(absthr,absthr_1,sizeof(absthr_1));
- break;
- case 2:
- memcpy(absthr,absthr_2,sizeof(absthr_2));
- break;
- default:
- return false;
- }
-
- return true;
- }
-
- static TOMPGRET psycho_anal(
- const short* buffer,short int savebuf[1056],
- const int chn,const int lay,
- double snr32[],const double sfreq)
- {
- unsigned int i, j, k;
- int sfreq_idx;
- int buffer_address = 0;
- double r_prime, phi_prime;
- double freq_mult, bval_lo, minthres, sum_energy;
- double tb, temp1, temp2, temp3;
-
-
- /* The static variables "r", "phi_sav", "recent", "old" and "oldest" have */
- /* to be remembered for the unpredictability measure. For "r" and */
- /* "phi_sav", the first index from the left is the channel select and */
- /* the second index is the "age" of the data. */
-
- static int recent = 0, old = 1, oldest = 0;
- static int flush, sync_flush, syncsize;
- static bool init = false;
-
- /* The following static variables are constants. */
-
- static double nmt = 5.5;
-
- static const double crit_band[27] = {0, 100, 200, 300, 400, 510, 630, 770,
- 920, 1080, 1270,1480,1720,2000,2320, 2700,
- 3150, 3700, 4400,5300,6400,7700,9500,12000,
- 15500,25000,30000};
-
- static const double bmax[27] = {20.0, 20.0, 20.0, 20.0, 20.0, 17.0, 15.0,
- 10.0, 7.0, 4.4, 4.5, 4.5, 4.5, 4.5,
- 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5,
- 4.5, 4.5, 4.5, 3.5, 3.5, 3.5};
-
- /* The following pointer variables point to large areas of memory */
- /* dynamically allocated by the mem_alloc() function. Dynamic memory */
- /* allocation is used in order to avoid stack frame or data area */
- /* overflow errors that otherwise would have occurred at compile time */
- /* on the Macintosh computer. */
-
- static int numlines[CBANDS];
- static int partition[HBLKSIZE];
- static double cbval[CBANDS];
- static double rnorm[CBANDS];
- static double window[BLKSIZE];
- static double absthr[HBLKSIZE];
- static double tmn[CBANDS];
- static double s[CBANDS][CBANDS];
- static double lthr[2][HBLKSIZE];
- static double r[2][2][HBLKSIZE];
- static double phi_sav[2][2][HBLKSIZE];
-
- /* These dynamic memory allocations simulate "automatic" variables */
- /* placed on the stack. For each mem_alloc() call here, there must be */
- /* a corresponding mem_free() call at the end of this function. */
-
- double grouped_c[CBANDS];
- double grouped_e[CBANDS];
- double nb[CBANDS];
- double cb[CBANDS];
- double ecb[CBANDS];
- double bc[CBANDS];
- double wsamp_r[BLKSIZE];
- double wsamp_i[BLKSIZE];
- double phi[BLKSIZE];
- double energy[BLKSIZE];
- double c[HBLKSIZE];
- double fthr[HBLKSIZE];
- double snrtmp[2][32];
-
- ZeroMemory(grouped_c,sizeof(grouped_c));
- ZeroMemory(grouped_e,sizeof(grouped_e));
- ZeroMemory(nb,sizeof(nb));
- ZeroMemory(cb,sizeof(cb));
- ZeroMemory(ecb,sizeof(ecb));
- ZeroMemory(bc,sizeof(bc));
- ZeroMemory(wsamp_r,sizeof(wsamp_r));
- ZeroMemory(wsamp_i,sizeof(wsamp_i));
- ZeroMemory(phi,sizeof(phi));
- ZeroMemory(energy,sizeof(energy));
- ZeroMemory(c,sizeof(c));
- ZeroMemory(fthr,sizeof(fthr));
- ZeroMemory(snrtmp,sizeof(snrtmp));
-
- if(!init)
- {
- init = true;
-
- /* These dynamic memory allocations simulate "static" variables placed */
- /* in the data space. Each mem_alloc() call here occurs only once at */
- /* initialization time. The mem_free() function must not be called. */
-
- ZeroMemory(numlines,sizeof(numlines));
- ZeroMemory(partition,sizeof(partition));
- ZeroMemory(cbval,sizeof(cbval));
- ZeroMemory(rnorm,sizeof(rnorm));
- ZeroMemory(window,sizeof(window));
- ZeroMemory(tmn,sizeof(tmn));
- ZeroMemory(absthr,sizeof(absthr));
- ZeroMemory(s,sizeof(s));
- ZeroMemory(lthr,sizeof(lthr));
- ZeroMemory(r,sizeof(r));
- ZeroMemory(phi_sav,sizeof(phi_sav));
-
-
- switch((unsigned int)(sfreq + 0.5)){
- case 32000:
- sfreq_idx = 0;
- break;
- case 44100:
- sfreq_idx = 1;
- break;
- case 48000:
- sfreq_idx = 2;
- break;
- default:
- return TR_SAMPLING_FREQUENCY_ERR;
- }
- read_absthr(absthr, sfreq_idx);
-
- if(lay==0)
- {
- flush = 384;
- syncsize = 1024;
- sync_flush = 576;
- }
- else
- {
- flush = (int)(384.0*3.0/2.0);
- syncsize = 1056;
- sync_flush = syncsize - flush;
- }
- /* calculate HANN window coefficients */
- /* for(i=0;i<BLKSIZE;i++)window[i]=0.5*(1-cos(2.0*PI*i/(BLKSIZE-1.0))); */
- for(i=0;i<BLKSIZE;i++)
- window[i]=0.5*(1.0-cos(2.0*PI*(((double)i)-0.5)/BLKSIZE));
- /* reset states used in unpredictability measure */
- for(i=0;i<HBLKSIZE;i++)
- {
- r[0][0][i]=r[1][0][i]=r[0][1][i]=r[1][1][i]=0.0;
- phi_sav[0][0][i]=phi_sav[1][0][i]=0.0;
- phi_sav[0][1][i]=phi_sav[1][1][i]=0.0;
- lthr[0][i] = 60802371420160.0;
- lthr[1][i] = 60802371420160.0;
- }
- /*****************************************************************************
- * Initialization: Compute the following constants for use later *
- * partition[HBLKSIZE] = the partition number associated with each *
- * frequency line *
- * cbval[CBANDS] = the center (average) bark value of each *
- * partition *
- * numlines[CBANDS] = the number of frequency lines in each partition *
- * tmn[CBANDS] = tone masking noise *
- *****************************************************************************/
- /* compute fft frequency multiplicand */
- freq_mult = sfreq/BLKSIZE;
-
- /* calculate fft frequency, then bval of each line (use fthr[] as tmp storage)*/
- for(i=0;i<HBLKSIZE;i++)
- {
- temp1 = ((double)i)*freq_mult;
- j = 1;
- while(temp1>crit_band[j])
- j++;
- fthr[i]=j-1+(temp1-crit_band[j-1])/(crit_band[j]-crit_band[j-1]);
- }
- partition[0] = 0;
- /* temp2 is the counter of the number of frequency lines in each partition */
- temp2 = 1;
- cbval[0]=fthr[0];
- bval_lo=fthr[0];
- for(i=1;i<HBLKSIZE;i++)
- {
- if((fthr[i]-bval_lo)>0.33)
- {
- partition[i]=partition[i-1]+1;
- cbval[partition[i-1]] = cbval[partition[i-1]]/temp2;
- cbval[partition[i]] = fthr[i];
- bval_lo = fthr[i];
- numlines[partition[i-1]] = (int)temp2;
- temp2 = 1;
- }
- else
- {
- partition[i]=partition[i-1];
- cbval[partition[i]] += fthr[i];
- temp2++;
- }
- }
- numlines[partition[i-1]] = (int)temp2;
- cbval[partition[i-1]] = cbval[partition[i-1]]/temp2;
-
- /************************************************************************
- * Now compute the spreading function, s[j][i], the value of the spread-*
- * ing function, centered at band j, for band i, store for later use *
- ************************************************************************/
- for(j=0;j<CBANDS;j++)
- {
- for(i=0;i<CBANDS;i++)
- {
- temp1 = (cbval[i] - cbval[j])*1.05;
- if(temp1>=0.5 && temp1<=2.5)
- {
- temp2 = temp1 - 0.5;
- temp2 = 8.0 * (temp2*temp2 - 2.0 * temp2);
- }
- else
- temp2 = 0;
-
- temp1 += 0.474;
- temp3 = 15.811389+7.5*temp1-17.5*sqrt((double) (1.0+temp1*temp1));
-
- if(temp3 <= -100)
- s[i][j] = 0;
- else
- {
- temp3 = (temp2 + temp3)*LN_TO_LOG10;
- s[i][j] = exp(temp3);
- }
- }
- }
-
- /* Calculate Tone Masking Noise values */
- for(j=0;j<CBANDS;j++)
- {
- temp1 = 15.5 + cbval[j];
- tmn[j] = (temp1>24.5) ? temp1 : 24.5;
- /* Calculate normalization factors for the net spreading functions */
- rnorm[j] = 0;
- for(i=0;i<CBANDS;i++)
- {
- rnorm[j] += s[j][i];
- }
- }
- }
-
- /************************* End of Initialization *****************************/
- switch(lay)
- {
- case 0:
- case 1:
- for(i=0; i<=(unsigned)lay; i++)
- {
- /*****************************************************************************
- * Net offset is 480 samples (1056-576) for layer 2; this is because one must*
- * stagger input data by 256 samples to synchronize psychoacoustic model with*
- * filter bank outputs, then stagger so that center of 1024 FFT window lines *
- * up with center of 576 "recent" audio samples. *
- * *
- * For layer 1, the input data still needs to be staggered by 256 samples, *
- * then it must be staggered again so that the 384 "recent" samples are centered*
- * in the 1024 FFT window. The net offset is then 576 and you need 448 "recent"*
- * samples for each iteration to keep the 384 samples of interest centered *
- *****************************************************************************/
- for(j=0; j<(unsigned)syncsize; j++)
- {
- if(j<((unsigned)sync_flush))
- savebuf[j] = savebuf[j+flush];
- else
- {
- savebuf[j] = buffer[buffer_address];
- buffer_address++;
- }
-
- if(j<BLKSIZE)
- {
- /**window data with HANN window***********************************************/
- wsamp_r[j] = window[j]*((double) savebuf[j]);
- wsamp_i[j] = 0;
- }
- }
- /**Compute FFT****************************************************************/
- fft(wsamp_r,wsamp_i,energy,phi,1024);
- /*****************************************************************************
- * calculate the unpredictability measure, given energy[f] and phi[f] *
- *****************************************************************************/
- /*only update data "age" pointers after you are done with both channels */
- /*for layer 1 computations, for the layer 2 double computations, the pointers*/
- /*are reset automatically on the second pass */
- if(lay==1 || (lay==0 && chn==0) )
- {
- if(recent==0)
- {
- recent = 1;
- oldest = 1;
- }
- else
- {
- recent = 0;
- oldest = 0;
- }
- if(old==0)
- old = 1;
- else
- old = 0;
- }
- for(j=0; j<HBLKSIZE; j++)
- {
- r_prime = 2.0 * r[chn][old][j] - r[chn][oldest][j];
- phi_prime = 2.0 * phi_sav[chn][old][j] - phi_sav[chn][oldest][j];
- r[chn][recent][j] = sqrt(energy[j]);
- phi_sav[chn][recent][j] = phi[j];
- temp1=r[chn][recent][j] * cos(phi[j]) - r_prime * cos(phi_prime);
- temp2=r[chn][recent][j] * sin(phi[j]) - r_prime * sin(phi_prime);
- temp3=r[chn][recent][j] + fabs(r_prime);
- if(temp3 != 0)
- c[j]=sqrt(temp1*temp1+temp2*temp2)/temp3;
- else
- c[j] = 0;
- }
- /*****************************************************************************
- * Calculate the grouped, energy-weighted, unpredictability measure, *
- * grouped_c[], and the grouped energy. grouped_e[] *
- *****************************************************************************/
- for(j=1;j<CBANDS;j++)
- {
- grouped_e[j] = 0;
- grouped_c[j] = 0;
- }
- grouped_e[0] = energy[0];
- grouped_c[0] = energy[0]*c[0];
- for(j=1;j<HBLKSIZE;j++)
- {
- grouped_e[partition[j]] += energy[j];
- grouped_c[partition[j]] += energy[j]*c[j];
- }
- /*****************************************************************************
- * convolve the grouped energy-weighted unpredictability measure *
- * and the grouped energy with the spreading function, s[j][k] *
- *****************************************************************************/
- for(j=0;j<CBANDS;j++)
- {
- ecb[j] = 0;
- cb[j] = 0;
- for(k=0;k<CBANDS;k++)
- {
- if(s[j][k] != 0.0)
- {
- ecb[j] += s[j][k]*grouped_e[k];
- cb[j] += s[j][k]*grouped_c[k];
- }
- }
- if(ecb[j] !=0)
- cb[j] = cb[j]/ecb[j];
- else
- cb[j] = 0;
- }
- /*****************************************************************************
- * Calculate the required SNR for each of the frequency partitions *
- * this whole section can be accomplished by a table lookup *
- *****************************************************************************/
- for(j=0;j<CBANDS;j++){
- if(cb[j]<.05)
- cb[j]=0.05;
- else if(cb[j]>.5)
- cb[j]=0.5;
- tb = -0.434294482*log(cb[j])-0.301029996;
- bc[j] = tmn[j]*tb + nmt*(1.0-tb);
- k = (unsigned int)(cbval[j] + 0.5);
- bc[j] = (bc[j] > bmax[k]) ? bc[j] : bmax[k];
- bc[j] = exp(-bc[j]*LN_TO_LOG10);
- }
- /*****************************************************************************
- * Calculate the permissible noise energy level in each of the frequency *
- * partitions. Include absolute threshold and pre-echo controls *
- * this whole section can be accomplished by a table lookup *
- *****************************************************************************/
- for(j=0;j<CBANDS;j++)
- if(rnorm[j] && numlines[j])
- nb[j] = ecb[j]*bc[j]/(rnorm[j]*numlines[j]);
- else
- nb[j] = 0;
- for(j=0;j<HBLKSIZE;j++)
- {
- /*temp1 is the preliminary threshold */
- temp1=nb[partition[j]];
- temp1=(temp1>absthr[j])?temp1:absthr[j];
- /*do not use pre-echo control for layer 2 because it may do bad things to the*/
- /* MUSICAM bit allocation algorithm */
- if(lay==0)
- {
- fthr[j] = (temp1 < lthr[chn][j]) ? temp1 : lthr[chn][j];
- temp2 = temp1 * 0.00316;
- fthr[j] = (temp2 > fthr[j]) ? temp2 : fthr[j];
- }
- else
- fthr[j] = temp1;
- lthr[chn][j] = LXMIN*temp1;
- }
- /*****************************************************************************
- * Translate the 512 threshold values to the 32 filter bands of the coder *
- *****************************************************************************/
- for(j=0;j<193;j += 16)
- {
- minthres = 60802371420160.0;
- sum_energy = 0.0;
- for(k=0;k<17;k++)
- {
- if(minthres>fthr[j+k])
- minthres = fthr[j+k];
- sum_energy += energy[j+k];
- }
- snrtmp[i][j/16] = sum_energy/(minthres * 17.0);
- snrtmp[i][j/16] = 4.342944819 * log(snrtmp[i][j/16]);
- }
- for(j=208;j<(HBLKSIZE-1);j += 16)
- {
- minthres = 0.0;
- sum_energy = 0.0;
- for(k=0;k<17;k++)
- {
- minthres += fthr[j+k];
- sum_energy += energy[j+k];
- }
- snrtmp[i][j/16] = sum_energy/minthres;
- snrtmp[i][j/16] = 4.342944819 * log(snrtmp[i][j/16]);
- }
- /*****************************************************************************
- * End of Psychoacuostic calculation loop *
- *****************************************************************************/
- }
- for(i=0; i<32; i++)
- {
- if(lay==1)
- snr32[i]=(snrtmp[0][i]>snrtmp[1][i])?snrtmp[0][i]:snrtmp[1][i];
- else
- snr32[i]=snrtmp[0][i];
- }
- break;
- case 3:
- return TR_NO_SUPPORTED_AUDIO_LAYER;
- default:
- return TR_NO_SUPPORTED_AUDIO_LAYER;
- }
- return TR_OK;
- }
-
-