1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
1 /******************************************************************************
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
2 * *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
3 * Copyright (C) 1992-1995 Tony Robinson *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
4 * *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
5 * See the file doc/LICENSE.shorten for conditions on distribution and usage *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
6 * *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
7 ******************************************************************************/
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
8
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
9 /*
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
10 * $Id: fixio.c,v 1.7 2003/08/26 05:34:04 jason Exp $
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
11 */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
12
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
13 #include <stdio.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
14 #include <stdlib.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
15 #include <string.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
16 #include <unistd.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
17 #include "shorten.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
18 #include "bitshift.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
19
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
20 void swab(const void *from, void *to, ssize_t n);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
21
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
22 #define CAPMAXSCHAR(x) ((x > 127) ? 127 : x)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
23 #define CAPMAXUCHAR(x) ((x > 255) ? 255 : x)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
24 #define CAPMAXSHORT(x) ((x > 32767) ? 32767 : x)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
25 #define CAPMAXUSHORT(x) ((x > 65535) ? 65535 : x)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
26
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
27 static int sizeof_sample[TYPE_EOF];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
28
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
29 void init_sizeof_sample() {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
30 sizeof_sample[TYPE_AU1] = sizeof(uchar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
31 sizeof_sample[TYPE_S8] = sizeof(schar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
32 sizeof_sample[TYPE_U8] = sizeof(uchar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
33 sizeof_sample[TYPE_S16HL] = sizeof(ushort);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
34 sizeof_sample[TYPE_U16HL] = sizeof(ushort);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
35 sizeof_sample[TYPE_S16LH] = sizeof(ushort);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
36 sizeof_sample[TYPE_U16LH] = sizeof(ushort);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
37 sizeof_sample[TYPE_ULAW] = sizeof(uchar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
38 sizeof_sample[TYPE_AU2] = sizeof(uchar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
39 sizeof_sample[TYPE_AU3] = sizeof(uchar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
40 sizeof_sample[TYPE_ALAW] = sizeof(uchar);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
41 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
43 /***************/
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44 /* fixed write */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45 /***************/
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
47 void fwrite_type_init(shn_file *this_shn) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
48 init_sizeof_sample();
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
49 this_shn->decode_state->writebuf = (schar*) NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
50 this_shn->decode_state->writefub = (schar*) NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
51 this_shn->decode_state->nwritebuf = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
53
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54 void fwrite_type_quit(shn_file *this_shn) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55 if(this_shn->decode_state->writebuf != NULL) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
56 free(this_shn->decode_state->writebuf);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57 this_shn->decode_state->writebuf = NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59 if(this_shn->decode_state->writefub != NULL) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60 free(this_shn->decode_state->writefub);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
61 this_shn->decode_state->writefub = NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65 /* convert from signed ints to a given type and write */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 void fwrite_type(slong **data,int ftype,int nchan,int nitem,shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68 int hiloint = 1, hilo = !(*((char*) &hiloint));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69 int i, nwrite = 0, datasize = sizeof_sample[ftype], chan;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 slong *data0 = data[0];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71 int bufAvailable = OUT_BUFFER_SIZE - this_shn->vars.bytes_in_buf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73 if(this_shn->decode_state->nwritebuf < nchan * nitem * datasize) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74 this_shn->decode_state->nwritebuf = nchan * nitem * datasize;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 if(this_shn->decode_state->writebuf != NULL) free(this_shn->decode_state->writebuf);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76 if(this_shn->decode_state->writefub != NULL) free(this_shn->decode_state->writefub);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 this_shn->decode_state->writebuf = (schar*) pmalloc((ulong) this_shn->decode_state->nwritebuf,this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78 if (!this_shn->decode_state->writebuf)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79 return;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80 this_shn->decode_state->writefub = (schar*) pmalloc((ulong) this_shn->decode_state->nwritebuf,this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 if (!this_shn->decode_state->writefub)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82 return;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
84
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85 switch(ftype) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
86 case TYPE_AU1: /* leave the conversion to fix_bitshift() */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 case TYPE_AU2: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88 uchar *writebufp = (uchar*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 *writebufp++ = data0[i];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
92 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
93 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95 *writebufp++ = data[chan][i];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
96 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
98 case TYPE_U8: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
99 uchar *writebufp = (uchar*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
100 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
101 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
102 *writebufp++ = CAPMAXUCHAR(data0[i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
105 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106 *writebufp++ = CAPMAXUCHAR(data[chan][i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109 case TYPE_S8: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
110 schar *writebufp = (schar*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
112 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
113 *writebufp++ = CAPMAXSCHAR(data0[i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
114 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
115 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117 *writebufp++ = CAPMAXSCHAR(data[chan][i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
118 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
119 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
120 case TYPE_S16HL:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
121 case TYPE_S16LH: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
122 short *writebufp = (short*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
123 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
124 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
125 *writebufp++ = CAPMAXSHORT(data0[i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
126 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
127 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
128 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
129 *writebufp++ = CAPMAXSHORT(data[chan][i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
130 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
131 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
132 case TYPE_U16HL:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
133 case TYPE_U16LH: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
134 ushort *writebufp = (ushort*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
135 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
136 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
137 *writebufp++ = CAPMAXUSHORT(data0[i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
138 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
139 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
140 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
141 *writebufp++ = CAPMAXUSHORT(data[chan][i]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
142 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
143 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
144 case TYPE_ULAW: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
145 uchar *writebufp = (uchar*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
146 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
147 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
148 *writebufp++ = Slinear2ulaw(CAPMAXSHORT((data0[i] << 3)));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
149 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
150 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
151 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
152 *writebufp++ = Slinear2ulaw(CAPMAXSHORT((data[chan][i] << 3)));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
153 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
154 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
155 case TYPE_AU3: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
156 uchar *writebufp = (uchar*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
157 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
158 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
159 if(data0[i] < 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
160 *writebufp++ = (127 - data0[i]) ^ 0xd5;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
161 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
162 *writebufp++ = (data0[i] + 128) ^ 0x55;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
163 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
164 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
165 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
166 if(data[chan][i] < 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
167 *writebufp++ = (127 - data[chan][i]) ^ 0xd5;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
168 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
169 *writebufp++ = (data[chan][i] + 128) ^ 0x55;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
170 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
171 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
172 case TYPE_ALAW: {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
173 uchar *writebufp = (uchar*) this_shn->decode_state->writebuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
174 if(nchan == 1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
175 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
176 *writebufp++ = Slinear2alaw(CAPMAXSHORT((data0[i] << 3)));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
177 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
178 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
179 for(chan = 0; chan < nchan; chan++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
180 *writebufp++ = Slinear2alaw(CAPMAXSHORT((data[chan][i] << 3)));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
181 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
182 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
183 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
184
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
185 switch(ftype) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
186 case TYPE_AU1:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
187 case TYPE_S8:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
188 case TYPE_U8:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
189 case TYPE_ULAW:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
190 case TYPE_AU2:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
191 case TYPE_AU3:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
192 case TYPE_ALAW:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
193 if (datasize*nchan*nitem <= bufAvailable) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
194 memcpy((void *)&this_shn->vars.buffer[this_shn->vars.bytes_in_buf],(const void *)this_shn->decode_state->writebuf,datasize*nchan*nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
195 this_shn->vars.bytes_in_buf += datasize*nchan*nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
196 nwrite = nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
197 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
198 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
199 shn_debug("Buffer overrun in fwrite_type() [case 1]: %d bytes to read, but only %d bytes are available",datasize*nchan*nitem,bufAvailable);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
200 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
201 case TYPE_S16HL:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
202 case TYPE_U16HL:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
203 if(hilo)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
204 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
205 if (datasize*nchan*nitem <= bufAvailable) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
206 memcpy((void *)&this_shn->vars.buffer[this_shn->vars.bytes_in_buf],(const void *)this_shn->decode_state->writebuf,datasize*nchan*nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
207 this_shn->vars.bytes_in_buf += datasize*nchan*nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
208 nwrite = nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
209 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
210 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
211 shn_debug("Buffer overrun in fwrite_type() [case 2]: %d bytes to read, but only %d bytes are available",datasize*nchan*nitem,bufAvailable);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
212 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
213 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
214 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
215 swab(this_shn->decode_state->writebuf, this_shn->decode_state->writefub, datasize * nchan * nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
216 if (datasize*nchan*nitem <= bufAvailable) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
217 memcpy((void *)&this_shn->vars.buffer[this_shn->vars.bytes_in_buf],(const void *)this_shn->decode_state->writefub,datasize*nchan*nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
218 this_shn->vars.bytes_in_buf += datasize*nchan*nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
219 nwrite = nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
220 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
221 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
222 shn_debug("Buffer overrun in fwrite_type() [case 3]: %d bytes to read, but only %d bytes are available",datasize*nchan*nitem,bufAvailable);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
223 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
224 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
225 case TYPE_S16LH:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
226 case TYPE_U16LH:
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
227 if(hilo)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
228 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
229 swab(this_shn->decode_state->writebuf, this_shn->decode_state->writefub, datasize * nchan * nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
230 if (datasize*nchan*nitem <= bufAvailable) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
231 memcpy((void *)&this_shn->vars.buffer[this_shn->vars.bytes_in_buf],(const void *)this_shn->decode_state->writefub,datasize*nchan*nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
232 this_shn->vars.bytes_in_buf += datasize*nchan*nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
233 nwrite = nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
234 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
235 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
236 shn_debug("Buffer overrun in fwrite_type() [case 4]: %d bytes to read, but only %d bytes are available",datasize*nchan*nitem,bufAvailable);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
237 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
238 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
239 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
240 if (datasize*nchan*nitem <= bufAvailable) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
241 memcpy((void *)&this_shn->vars.buffer[this_shn->vars.bytes_in_buf],(const void *)this_shn->decode_state->writebuf,datasize*nchan*nitem);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
242 this_shn->vars.bytes_in_buf += datasize*nchan*nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
243 nwrite = nitem;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
244 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
245 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
246 shn_debug("Buffer overrun in fwrite_type() [case 5]: %d bytes to read, but only %d bytes are available",datasize*nchan*nitem,bufAvailable);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
247 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
248 break;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
249 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
250
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
251 if(nwrite != nitem)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
252 shn_error_fatal(this_shn,"Failed to write decompressed stream -\npossible corrupt or truncated file");
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
253 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
254
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
255 /*************/
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
256 /* bitshifts */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
257 /*************/
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
258
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
259 void fix_bitshift(buffer, nitem, bitshift, ftype) slong *buffer; int nitem,
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
260 bitshift, ftype; {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
261 int i;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
262
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
263 if(ftype == TYPE_AU1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
264 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
265 buffer[i] = ulaw_outward[bitshift][buffer[i] + 128];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
266 else if(ftype == TYPE_AU2)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
267 for(i = 0; i < nitem; i++) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
268 if(buffer[i] >= 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
269 buffer[i] = ulaw_outward[bitshift][buffer[i] + 128];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
270 else if(buffer[i] == -1)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
271 buffer[i] = NEGATIVE_ULAW_ZERO;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
272 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
273 buffer[i] = ulaw_outward[bitshift][buffer[i] + 129];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
274 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
275 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
276 if(bitshift != 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
277 for(i = 0; i < nitem; i++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
278 buffer[i] <<= bitshift;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
279 }
|