📄 common.c
字号:
/********************************************************************** * ISO MPEG Audio Subgroup Software Simulation Group (1996) * ISO 13818-3 MPEG-2 Audio Multichannel Encoder * * $Id: common.c 1.8 1996/02/12 07:13:35 rowlands Exp $ * * $Log: common.c $ * Revision 1.8 1996/02/12 07:13:35 rowlands * Release following Munich meeting * * Revision 1.5.2.1 1995/11/06 04:19:12 rowlands * Received from Uwe Felderhoff (IRT) * * Revision 1.6.1 1995/08/14 07:49:33 tenkate * ML-LSF added Warner ten Kate 7/8/95 (Philips) * allocation table alloc_4 read to allocml * * Revision 1.6 1995/07/31 07:44:27 tenkate * void hdr_to_frps updated for phantom coding, 25/07/95 WtK * * Revision 1.3.2.1 1995/06/16 03:46:42 rowlands * Input from Susanne Ritscher (IRT) * * Revision 1.3.3.1 1995/06/16 08:25:11 rowlands * Input from Sang Wook Kim (Samsung AIT) * **********************************************************************//********************************************************************** * date programmers comment * * 2/25/91 Doulas Wong, start of version 1.0 records * * Davis Pan * * 5/10/91 W. Joseph Carter Created this file for all common * * functions and global variables. * * Ported to Macintosh and Unix. * * Added Jean-Georges Fritsch's * * "bitstream.c" package. * * Added routines to handle AIFF PCM * * sound files. * * Added "mem_alloc()" and "mem_free()" * * routines for memory allocation * * portability. * * Added routines to convert between * * Apple SANE extended floating point * * format and IEEE double precision * * floating point format. For AIFF. * * 02jul91 dpwe (Aware Inc) Moved allocation table input here; * * Tables read from subdir TABLES_PATH. * * Added some debug printout fns (Write*)* * 7/10/91 Earle Jennings replacement of the one float by FLOAT * * port to MsDos from MacIntosh version * * 8/ 5/91 Jean-Georges Fritsch fixed bug in open_bit_stream_r() * *10/ 1/91 S.I. Sudharsanan, Ported to IBM AIX platform. * * Don H. Lee, * * Peter W. Farrett * *10/3/91 Don H. Lee implemented CRC-16 error protection * * newly introduced functions are * * I_CRC_calc, II_CRC_calc and * * update_CRC. Additions and revisions * * are marked with dhl for clarity * *10/18/91 Jean-Georges Fritsch fixed bug in update_CRC(), * * II_CRC_calc() and I_CRC_calc() * * 2/11/92 W. Joseph Carter Ported new code to Macintosh. Most * * important fixes involved changing * * 16-bit ints to long or unsigned in * * bit alloc routines for quant of 65535 * * and passing proper function args. * * Removed "Other Joint Stereo" option * * and made bitrate be total channel * * bitrate, irrespective of the mode. * * Fixed many small bugs & reorganized. * *3/20/92 Jean-Georges Fritsch fixed bug in start-of-frame search * * 8jul92 Susanne Ritscher MS-DOS, MSC 6.0 port fixes. * * 19aug92 Soren H. Nielsen Fixed bug in I_CRC_calc and in * * II_CRC_calc. Added function: new_ext * * for better MS-DOS compatability * *26nov92 Susanne Ritscher AIFF for MS-DOS * ********************************************************************** * * * * * MPEG/audio Phase 2 coding/decoding multichannel * * * * 7/27/93 Susanne Ritscher, IRT Munich * * * * 8/27/93 Susanne Ritscher, IRT Munich * * Channel-Switching is working * * 9/1/93 Susanne Ritscher, IRT Munich * * all channels normalized * * 9/20/93 channel-switching is only performed at a * * certain limit of TC_ALLOC dB, which is included * * in encoder.h * * * * Version 1.0 * * * * 07/12/94 Susanne Ritscher, IRT Munich * * * * Version 1.1 * * * * 02/23/95 Susanne Ritscher, IRT Munich * * corrected some bugs * * extension bitstream is working * * * * Version 2.0 * * * * 01/28/97 Frans de Bont, Philips Sound & Vision, Eindhoven * * - dynamic crosstalk working for all configurations* * - prediction working for all configurations * * - extension bitstream fixed * * - fully compliant to DIS 13818-3.2 * * * * **********************************************************************//*********************************************************************** * * * 06/06/95 Sang Wook Kim, Samsung AIT * * corrected some bugs * * * **********************************************************************//************************************************************************* Global Include Files************************************************************************/#include "common.h"/*#undef PrintCRCDebug#undef PrintBitDebug*/#ifdef MACINTOSH#include <SANE.h>#include <pascal.h>#endif#include <ctype.h>/************************************************************************* Global Variable Definitions************************************************************************/char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" };char *layer_names[3] = { "I", "II", "III" };double s_freq[4] = {44.1, 48, 32, 0};int bitrate[3][16] = { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0}, {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0}, {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0} };double /*far*/ multiple[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};int sbgrp[32] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11};int sb_groups[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 15, 26 /*31*/};int transmission_channel7[8][2] = { /* for 5/2 config */ {10, 11}, { 7, 11}, {10, 8}, { 7, 8}, {10, 9}, { 7, 9}, { 9, 11}, { 9, 8}, }; int transmission_channel5[8][5] = { /* for 3/2 config */ {0, 1, 2, 3, 4}, {0, 1, 5, 3, 4}, {0, 1, 6, 3, 4}, {0, 1, 2, 5, 4}, {0, 1, 2, 3, 6}, {0, 1, 2, 5, 6}, {0, 1, 6, 5, 4}, {0, 1, 5, 3, 6}, };int transmission_channel4a[6][4] = { /* for 3/1 config */ {0, 1, 2, 3}, {0, 1, 5, 3}, {0, 1, 6, 3}, {0, 1, 2, 5}, {0, 1, 2, 6}, {0, 1, 5, 6}, };int transmission_channel4b[4][4] = { /* for 2/2 config */ {0, 1, 2, 3}, {0, 1, 5, 3}, {0, 1, 2, 6}, {0, 1, 5, 6}, };int transmission_channel3[3][5] = { /* for 3/0 (+ 2/0) and 2/1 config's */ {0, 1, 2, 3, 4}, {0, 1, 5, 3, 4}, {0, 1, 6, 3, 4}, };int no_channel[8][2] = { {5, 6}, {2, 6}, {5, 2}, {3, 6}, {5, 4}, {3, 4}, {3, 2}, {2, 4}, }; int n_pred_coef[16]; int n_pred_coef4[16] = { 6, 4, 4, 4, 2, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0 }; int n_pred_coef3[16] = { 4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int n_pred_coef1[16] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int dyn_bbal5[19][12] = { /* for 5/2 config */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, };int dyn_bbal4[15][12] = { /* for 3/2 config */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {12, 12, 12, 12, 12, 12, 12, 12, 24, 21, 36, 87}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, };int dyn_bbal4PhC[15][12] = { /* for 3/2 config in case of Phantom center coding */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 0, 0}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29}, {12, 12, 12, 12, 12, 12, 12, 12, 24, 21, 24, 58}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, };int dyn_bbal3[5][12] = { /* for 3/1 and 2/2 config */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, };int dyn_bbal3PhC[5][12] = { /* for 3/1 config in case of Phantom center coding */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 0, 0}, {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, };int dyn_bbal1[2][12] = { /* for 3/0 (+2/0) and 2/1 config */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29}, };int dyn_bbal1PhC[2][12] = { /* for 3/0 (+2/0) config in case of Phantom center coding */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 0, 0}, };int dyn_ch5[19][2] = { /* for 5/2 config */ {1, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, }; int dyn_ch4[15][3] = { /* for 3/2 config */ {1, 1, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 0}, {1, 1, 0}, {1, 0, 1}, {1, 1, 0}, {1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 0, 0}, }; int dyn_ch3[5][2] = { /* for 3/1 and 2/2 config */ {1, 1}, {1, 0}, {0, 1}, {0, 0}, {1, 0}, }; int dyn_ch1[2][3] = { /* for 3/0 (+2/0) and 2/1 config */ {1, 1, 1}, {0, 1, 1}, }; int T2[12], T3[12], T4[12], T5[12], T6[12]; int T2outof[12], T3outof[12], T4outof[12]; /* L0 or R0 */ int T2from[12], T3from[12], T4from[12];/************************************************************************* Global Function Definitions************************************************************************/void program_information(void){ printf("ISO MPEG Audio Subgroup Software Simulation Group (1996)\n"); printf("ISO 13818-3 MPEG-2 Audio Multichannel Encoder\n"); printf("%s\n", VERSION);}/* The system uses a variety of data files. By opening them via this function, we can accommodate various locations. */FILE *OpenTableFile(char *name){char fulname[80];char *envdir;FILE *f; fulname[0] = '\0';#ifdef TABLES_PATH strcpy(fulname, TABLES_PATH); /* default relative path for tables */#endif /* TABLES_PATH */ /* (includes terminal path seperator */#ifdef UNIX /* envir. variables for UNIX only */ { char *getenv(const char *); envdir = getenv(MPEGTABENV); /* check for environment */ if(envdir != NULL) strcpy(fulname, envdir); strcat(fulname, PATH_SEPARATOR); /* add a "/" on the end */ }#endif /* UNIX */ strcat(fulname, name); if( (f=fopen(fulname,"r"))==NULL ) { fprintf(stderr,"OpenTable: could not find %s\n", fulname);#ifdef UNIX if(envdir != NULL) fprintf(stderr,"Check %s directory '%s'\n",MPEGTABENV, envdir); else fprintf(stderr,"Check local directory './%s' or setenv %s\n", TABLES_PATH, MPEGTABENV);#else /* not unix : no environment variables */#ifdef TABLES_PATH fprintf(stderr,"Check local directory './%s'\n",TABLES_PATH);#endif /* TABLES_PATH */#endif /* UNIX */ } return f;}/***********************************************************************/*/* Read one of the data files ("alloc_*") specifying the bit allocation//* quatization parameters for each subband in layer II encoding/*/**********************************************************************/int read_bit_alloc(int table, al_table (*alloc)) /* read in table, return # subbands */ { long a, b, c, d, i, j; FILE *fp; char name[16], t[80]; int sblim; strcpy(name, "alloc_0"); switch (table) { case 0 : name[6] = '0'; break; case 1 : name[6] = '1'; break; case 2 : name[6] = '2'; break; case 3 : name[6] = '3'; break; case 4 : name[6] = '4'; break; /* for MultiLingual LSF , WtK 7/8/95 */ default : name[6] = '0'; } if (!(fp = OpenTableFile(name))) { printf("Please check bit allocation table %s\n", name); exit(0); }#ifdef PrintBitDebug printf("using bit allocation table %s\n", name);#endif fgets(t, 80, fp); sscanf(t, "%d\n", &sblim); while (!feof(fp)) { fgets(t, 80, fp); sscanf(t, "%ld %ld %ld %ld %ld %ld\n", &i, &j, &a, &b, &c, &d); (*alloc)[i][j].steps = a; (*alloc)[i][j].bits = b; (*alloc)[i][j].group = c; (*alloc)[i][j].quant = d; } fclose(fp); return sblim;}/***********************************************************************/*/* Using the decoded info the appropriate possible quantization per/* subband table is loaded/*/**********************************************************************/int pick_table (frame_params *fr_ps) /* choose table, load if necess, return # sb's */{ int table, tableml, lay, ws, bsp, br_per_ch, sfrq; int sblim = fr_ps->sblimit; /* return current value if no load */ lay = fr_ps->header->lay - 1; bsp = fr_ps->header->bitrate_index;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -