1 /* 2 * Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved 3 * common.h 4 */ 5 /********************************************************************** 6 * MPEG/audio coding/decoding software, work in progress * 7 * NOT for public distribution until verified and approved by the * 8 * MPEG/audio committee. For further information, please contact * 9 * Davis Pan, 508-493-2241, e-mail: pan@gauss.enet.dec.com * 10 * * 11 * VERSION 4.0 * 12 * changes made since last update: * 13 * date programmers comment * 14 * 2/25/91 Doulas Wong, start of version 1.0 records * 15 * Davis Pan * 16 * 5/10/91 W. Joseph Carter Reorganized & renamed all ".h" files * 17 * into "common.h" and "encoder.h". * 18 * Ported to Macintosh and Unix. * 19 * Added additional type definitions for * 20 * AIFF, double/SANE and "bitstream.c". * 21 * Added function prototypes for more * 22 * rigorous type checking. * 23 * 27jun91 dpwe (Aware) Added "alloc_*" defs & prototypes * 24 * Defined new struct 'frame_params'. * 25 * Changed info.stereo to info.mode_ext * 26 * #define constants for mode types * 27 * Prototype arguments if PROTO_ARGS * 28 * 5/28/91 Earle Jennings added MS_DOS definition * 29 * MsDos function prototype declarations * 30 * 7/10/91 Earle Jennings added FLOAT definition as double * 31 *10/ 3/91 Don H. Lee implemented CRC-16 error protection * 32 * 2/11/92 W. Joseph Carter Ported new code to Macintosh. Most * 33 * important fixes involved changing * 34 * 16-bit ints to long or unsigned in * 35 * bit alloc routines for quant of 65535 * 36 * and passing proper function args. * 37 * Removed "Other Joint Stereo" option * 38 * and made bitrate be total channel * 39 * bitrate, irrespective of the mode. * 40 * Fixed many small bugs & reorganized. * 41 * Modified some function prototypes. * 42 * Changed BUFFER_SIZE back to 4096. * 43 * 7/27/92 Michael Li (re-)Ported to MS-DOS * 44 * 7/27/92 Masahiro Iwadare Ported to Convex * 45 * 8/07/92 mc@tv.tek.com * 46 * 8/10/92 Amit Gulati Ported to the AIX Platform (RS6000) * 47 * AIFF string constants redefined * 48 * 8/27/93 Seymour Shlien, Fixes in Unix and MSDOS ports, * 49 * Daniel Lauzon, and * 50 * Bill Truerniet * 51 * 2004/7/29 Steven Schultz Cleanup and modernize * 52 **********************************************************************/ 53 54 #include <stdio.h> 55 #include <string.h> 56 #include <math.h> 57 #include <unistd.h> 58 #include <stdlib.h> 59 #include "mjpeg_logging.h" 60 61 #define FLOAT float 62 #define FALSE 0 63 #define TRUE 1 64 65 #define MAX_U_32_NUM 0xFFFFFFFF 66 #define PI 3.14159265358979 67 #define PI4 PI/4 68 #define PI64 PI/64 69 #define LN_TO_LOG10 0.2302585093 70 71 #define MPEG_AUDIO_ID 1 72 73 #define MONO 1 74 #define STEREO 2 75 #define BITS_IN_A_BYTE 8 76 #define SBLIMIT 32 77 #define FFT_SIZE 1024 78 #define HAN_SIZE 512 79 #define SCALE_BLOCK 12 80 #define SCALE_RANGE 64 81 #define SCALE 32768 82 #define CRC16_POLYNOMIAL 0x8005 83 84 /* MPEG Header Definitions - Mode Values */ 85 86 #define MPG_MD_STEREO 0 87 #define MPG_MD_JOINT_STEREO 1 88 #define MPG_MD_DUAL_CHANNEL 2 89 #define MPG_MD_MONO 3 90 91 /* "bit_stream.h" Definitions */ 92 93 #define MINIMUM 4 /* Minimum size of the buffer in bytes */ 94 #define MAX_LENGTH 32 /* Maximum length of word written or 95 read from bit stream */ 96 #define READ_MODE 0 97 #define WRITE_MODE 1 98 #define ALIGNING 8 99 #define BINARY 0 100 #define ASCII 1 101 #define BS_FORMAT BINARY /* BINARY or ASCII = 2x bytes */ 102 #define BUFFER_SIZE 4096 103 104 #define MIN(A, B) ((A) < (B) ? (A) : (B)) 105 #define MAX(A, B) ((A) > (B) ? (A) : (B)) 106 107 /*********************************************************************** 108 * 109 * Global Type Definitions 110 * 111 ***********************************************************************/ 112 113 /* Structure for Reading Layer II Allocation Tables from File */ 114 115 typedef struct { 116 unsigned int steps; 117 unsigned int bits; 118 unsigned int group; 119 unsigned int quant; 120 } sb_alloc, *alloc_ptr; 121 122 typedef sb_alloc al_table[SBLIMIT][16]; 123 124 /* Header Information Structure */ 125 126 typedef struct { 127 int version; 128 int lay; 129 int error_protection; 130 int bitrate_index; 131 int sampling_frequency; 132 int padding; 133 int extension; 134 int mode; 135 int mode_ext; 136 int copyright; 137 int original; 138 int emphasis; 139 } layer, *the_layer; 140 141 /* Parent Structure Interpreting some Frame Parameters in Header */ 142 143 typedef struct { 144 layer *header; /* raw header information */ 145 int actual_mode; /* when writing IS, may forget if 0 chs */ 146 al_table *alloc; /* bit allocation table read in */ 147 int tab_num; /* number of table as loaded */ 148 int stereo; /* 1 for mono, 2 for stereo */ 149 int jsbound; /* first band of joint stereo coding */ 150 int sblimit; /* total number of sub bands */ 151 int in_freq; /* Freq. input in Hz */ 152 int down_freq; /* Freq. to downsample input to for encoding 153 in Hz */ 154 } frame_params; 155 156 /* "bit_stream.h" Type Definitions */ 157 158 typedef struct bit_stream_struc { 159 FILE *pt; /* pointer to bit stream device */ 160 unsigned char *buf; /* bit stream buffer */ 161 int buf_size; /* size of buffer (in number of bytes) */ 162 long totbit; /* bit counter of bit stream */ 163 int buf_byte_idx; /* pointer to top byte in buffer */ 164 int buf_bit_idx; /* pointer to top bit of top byte in buffer */ 165 int mode; /* bit stream open in read or write mode */ 166 int eob; /* end of buffer index */ 167 int eobs; /* end of bit stream flag */ 168 char format; 169 170 /* format of file in rd mode (BINARY/ASCII) */ 171 } Bit_stream_struc; 172 173 /*********************************************************************** 174 * 175 * Global Variable External Declarations 176 * 177 ***********************************************************************/ 178 179 extern const char *mode_names[4]; 180 extern const char *layer_names[3]; 181 extern double s_freq[4]; 182 extern int bitrate[3][15]; 183 extern double multiple[64]; 184 extern int verbose; 185 186 /*********************************************************************** 187 * 188 * Global Function Prototype Declarations 189 * 190 ***********************************************************************/ 191 192 /* The following functions are in the file "common.c" */ 193 194 extern FILE *OpenTableFile(char*); 195 extern int read_bit_alloc(int, al_table*); 196 extern int pick_table(frame_params*); 197 extern int js_bound(int, int); 198 extern void hdr_to_frps(frame_params*); 199 extern void WriteHdr(frame_params*, FILE*); 200 extern void WriteBitAlloc(unsigned int[2][SBLIMIT], frame_params*, 201 FILE*); 202 extern void WriteScale(unsigned int[2][SBLIMIT], 203 unsigned int[2][SBLIMIT], unsigned int[2][3][SBLIMIT], 204 frame_params*, FILE*); 205 extern void WriteSamples(int, unsigned int [SBLIMIT], 206 unsigned int[SBLIMIT], frame_params*, FILE*); 207 extern int NumericQ(char*); 208 extern int BitrateIndex(int, int); 209 extern int SmpFrqIndex(long); 210 extern void *mem_alloc(unsigned long, const char*); 211 extern void mem_free(void**); 212 extern void refill_buffer(Bit_stream_struc*); 213 extern void empty_buffer(Bit_stream_struc*, int); 214 extern void open_bit_stream_w(Bit_stream_struc*, char*, int); 215 extern void open_bit_stream_r(Bit_stream_struc*, char*, int); 216 extern void close_bit_stream_r(Bit_stream_struc*); 217 extern void close_bit_stream_w(Bit_stream_struc*); 218 extern void alloc_buffer(Bit_stream_struc*, int); 219 extern void desalloc_buffer(Bit_stream_struc*); 220 extern void back_track_buffer(Bit_stream_struc*, int); 221 extern unsigned int get1bit(Bit_stream_struc*); 222 extern void put1bit(Bit_stream_struc*, int); 223 extern unsigned long look_ahead(Bit_stream_struc*, int); 224 extern unsigned long getbits(Bit_stream_struc*, int); 225 extern void putbits(Bit_stream_struc*, unsigned int, int); 226 extern void byte_ali_putbits(Bit_stream_struc*, unsigned int, int); 227 extern unsigned long byte_ali_getbits(Bit_stream_struc*, int); 228 extern unsigned long sstell(Bit_stream_struc*); 229 extern int end_bs(Bit_stream_struc*); 230 extern int seek_sync(Bit_stream_struc*, long, int); 231 extern void I_CRC_calc(frame_params*, unsigned int[2][SBLIMIT], 232 unsigned int*); 233 extern void II_CRC_calc(frame_params*, unsigned int[2][SBLIMIT], 234 unsigned int[2][SBLIMIT], unsigned int*); 235 extern void update_CRC(unsigned int, unsigned int, unsigned int*); 236 extern void read_absthr(FLOAT*, int); 237