00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include "common.h"
00004 #include "mem.h"
00005 #include "bitstream.h"
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 int minimum = MINIMUM;
00044 int refill_buffer (Bit_stream_struc * bs)
00045 {
00046 register int i = bs->buf_size - 2 - bs->buf_byte_idx;
00047 register unsigned long n = 1;
00048 register int index = 0;
00049 char val[2];
00050
00051 while ((i >= 0) && (!bs->eob)) {
00052
00053 if (bs->format == BINARY)
00054 n = fread (&bs->buf[i--], sizeof (unsigned char), 1, bs->pt);
00055
00056 else {
00057 while ((index < 2) && n) {
00058 n = fread (&val[index], sizeof (char), 1, bs->pt);
00059 switch (val[index]) {
00060 case 0x30:
00061 case 0x31:
00062 case 0x32:
00063 case 0x33:
00064 case 0x34:
00065 case 0x35:
00066 case 0x36:
00067 case 0x37:
00068 case 0x38:
00069 case 0x39:
00070 case 0x41:
00071 case 0x42:
00072 case 0x43:
00073 case 0x44:
00074 case 0x45:
00075 case 0x46:
00076 index++;
00077 break;
00078 default:
00079 break;
00080 }
00081 }
00082
00083 if (val[0] <= 0x39)
00084 bs->buf[i] = (val[0] - 0x30) << 4;
00085 else
00086 bs->buf[i] = (val[0] - 0x37) << 4;
00087 if (val[1] <= 0x39)
00088 bs->buf[i--] |= (val[1] - 0x30);
00089 else
00090 bs->buf[i--] |= (val[1] - 0x37);
00091 index = 0;
00092 }
00093
00094 if (!n) {
00095 bs->eob = i + 1;
00096 }
00097
00098 }
00099 return 0;
00100 }
00101
00102
00103 void empty_buffer (Bit_stream_struc * bs, int minimum)
00104 {
00105 register int i;
00106
00107 for (i = bs->buf_size - 1; i >= minimum; i--)
00108 fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt);
00109
00110 fflush (bs->pt);
00111
00112 for (i = minimum - 1; i >= 0; i--)
00113 bs->buf[bs->buf_size - minimum + i] = bs->buf[i];
00114
00115 bs->buf_byte_idx = bs->buf_size - 1 - minimum;
00116 bs->buf_bit_idx = 8;
00117 }
00118
00119
00120 void open_bit_stream_w (Bit_stream_struc * bs, char *bs_filenam, int size)
00121 {
00122 if (bs_filenam[0] == '-')
00123 bs->pt = stdout;
00124 else if ((bs->pt = fopen (bs_filenam, "wb")) == NULL) {
00125 fprintf (stderr, "Could not create \"%s\".\n", bs_filenam);
00126 exit (1);
00127 }
00128 alloc_buffer (bs, size);
00129 bs->buf_byte_idx = size - 1;
00130 bs->buf_bit_idx = 8;
00131 bs->totbit = 0;
00132 bs->mode = WRITE_MODE;
00133 bs->eob = FALSE;
00134 bs->eobs = FALSE;
00135 }
00136
00137
00138 void close_bit_stream_w (Bit_stream_struc * bs)
00139 {
00140 putbits (bs, 0, 7);
00141 empty_buffer (bs, bs->buf_byte_idx + 1);
00142 fclose (bs->pt);
00143 desalloc_buffer (bs);
00144 }
00145
00146
00147 void alloc_buffer (Bit_stream_struc * bs, int size)
00148 {
00149 bs->buf =
00150 (unsigned char *) mem_alloc (size * sizeof (unsigned char), "buffer");
00151 bs->buf_size = size;
00152 }
00153
00154
00155 void desalloc_buffer (Bit_stream_struc * bs)
00156 {
00157 free (bs->buf);
00158 }
00159
00160 int putmask[9] = { 0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff };
00161 int clearmask[9] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x0 };
00162
00163 void back_track_buffer (Bit_stream_struc * bs, int N)
00164
00165 {
00166 int tmp = N - (N / 8) * 8;
00167 register int i;
00168
00169 bs->totbit -= N;
00170 for (i = bs->buf_byte_idx; i < bs->buf_byte_idx + N / 8 - 1; i++)
00171 bs->buf[i] = 0;
00172 bs->buf_byte_idx += N / 8;
00173 if ((tmp + bs->buf_bit_idx) <= 8) {
00174 bs->buf_bit_idx += tmp;
00175 } else {
00176 bs->buf_byte_idx++;
00177 bs->buf_bit_idx += (tmp - 8);
00178 }
00179 bs->buf[bs->buf_byte_idx] &= clearmask[bs->buf_bit_idx];
00180 }
00181
00182 int mask[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
00183
00184
00185 void put1bit (Bit_stream_struc * bs, int bit)
00186 {
00187 bs->totbit++;
00188
00189 bs->buf[bs->buf_byte_idx] |= (bit & 0x1) << (bs->buf_bit_idx - 1);
00190 bs->buf_bit_idx--;
00191 if (!bs->buf_bit_idx) {
00192 bs->buf_bit_idx = 8;
00193 bs->buf_byte_idx--;
00194 if (bs->buf_byte_idx < 0)
00195 empty_buffer (bs, minimum);
00196 bs->buf[bs->buf_byte_idx] = 0;
00197 }
00198 }
00199
00200
00201 INLINE void putbits (Bit_stream_struc * bs, unsigned int val, int N)
00202 {
00203 register int j = N;
00204 register int k, tmp;
00205
00206
00207
00208
00209 bs->totbit += N;
00210 while (j > 0) {
00211 k = MIN (j, bs->buf_bit_idx);
00212 tmp = val >> (j - k);
00213 bs->buf[bs->buf_byte_idx] |= (tmp & putmask[k]) << (bs->buf_bit_idx - k);
00214 bs->buf_bit_idx -= k;
00215 if (!bs->buf_bit_idx) {
00216 bs->buf_bit_idx = 8;
00217 bs->buf_byte_idx--;
00218 if (bs->buf_byte_idx < 0)
00219 empty_buffer (bs, minimum);
00220 bs->buf[bs->buf_byte_idx] = 0;
00221 }
00222 j -= k;
00223 }
00224 }
00225
00226
00227 void byte_ali_putbits (Bit_stream_struc * bs, unsigned int val, int N)
00228 {
00229 unsigned long aligning;
00230
00231 if (N > MAX_LENGTH)
00232 fprintf (stderr, "Cannot read or write more than %d bits at a time.\n",
00233 MAX_LENGTH);
00234 aligning = sstell (bs) % 8;
00235 if (aligning)
00236 putbits (bs, (unsigned int) 0, (int) (8 - aligning));
00237
00238 putbits (bs, val, N);
00239 }
00240
00241
00242 unsigned long sstell (Bit_stream_struc * bs)
00243 {
00244 return (bs->totbit);
00245 }
00246
00247
00248
00249
00250 int end_bs (Bit_stream_struc * bs)
00251 {
00252 return (bs->eobs);
00253 }
00254
00255
00256
00257
00258
00259
00260
00261 #define BUFSIZE 4096
00262 static unsigned long offset, totbit = 0;
00263 static unsigned int buf[BUFSIZE];
00264
00265
00266 unsigned long hsstell ()
00267 {
00268 return (totbit);
00269 }
00270
00271
00272
00273
00274
00275 void hputbuf (unsigned int val, int N)
00276 {
00277 if (N != 8) {
00278 fprintf (stderr, "Not Supported yet!!\n");
00279 exit (-3);
00280 }
00281 buf[offset % BUFSIZE] = val;
00282 offset++;
00283 }