wined3d: Remove a redundant check in handleStreams().
[wine] / dlls / winemp3.acm / common.c
1 /*
2  * Copyright (c) Michael Hipp and other authors of the mpglib project.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <signal.h>
23
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
27 #endif
28 #include <fcntl.h>
29
30 #include "mpg123.h"
31
32 const struct parameter param = { 1 , 1 , 0 , 0 };
33
34 static const int tabsel_123[2][3][16] = {
35    { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
36      {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
37      {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
38
39    { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
40      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
41      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
42 };
43
44 const long freqs[9] = { 44100, 48000, 32000,
45                   22050, 24000, 16000 ,
46                   11025 , 12000 , 8000 };
47
48 int bitindex;
49 unsigned char *wordpointer;
50 unsigned char *pcm_sample;
51 int pcm_point = 0;
52
53
54 #define HDRCMPMASK 0xfffffd00
55
56 #if 0
57 int head_check(unsigned long head)
58 {
59     if( (head & 0xffe00000) != 0xffe00000)
60         return FALSE;
61     if(!((head>>17)&3))
62         return FALSE;
63     if( ((head>>12)&0xf) == 0xf)
64         return FALSE;
65     if( ((head>>10)&0x3) == 0x3 )
66         return FALSE;
67     return TRUE;
68 }
69 #endif
70
71
72 /*
73  * the code a header and write the information
74  * into the frame structure
75  */
76 int decode_header(struct frame *fr,unsigned long newhead)
77 {
78     if( newhead & (1<<20) ) {
79       fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
80       fr->mpeg25 = 0;
81     }
82     else {
83       fr->lsf = 1;
84       fr->mpeg25 = 1;
85     }
86
87     fr->lay = 4-((newhead>>17)&3);
88     if( ((newhead>>10)&0x3) == 0x3) {
89       fprintf(stderr,"Stream error\n");
90       return (0);
91     }
92     if(fr->mpeg25) {
93       fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
94     }
95     else
96       fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
97     fr->error_protection = ((newhead>>16)&0x1)^0x1;
98
99     if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */
100       fr->bitrate_index = ((newhead>>12)&0xf);
101
102     fr->bitrate_index = ((newhead>>12)&0xf);
103     fr->padding   = ((newhead>>9)&0x1);
104     fr->extension = ((newhead>>8)&0x1);
105     fr->mode      = ((newhead>>6)&0x3);
106     fr->mode_ext  = ((newhead>>4)&0x3);
107     fr->copyright = ((newhead>>3)&0x1);
108     fr->original  = ((newhead>>2)&0x1);
109     fr->emphasis  = newhead & 0x3;
110
111     fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
112
113     if(!fr->bitrate_index)
114     {
115       fprintf(stderr,"Free format not supported.\n");
116       return (0);
117     }
118
119     switch(fr->lay)
120     {
121       case 1:
122 #ifdef LAYER1
123 #if 0
124         fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
125                          (fr->mode_ext<<2)+4 : 32;
126 #endif
127         fr->framesize  = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
128         fr->framesize /= freqs[fr->sampling_frequency];
129         fr->framesize  = ((fr->framesize+fr->padding)<<2)-4;
130 #else
131         fprintf(stderr,"Not supported!\n");
132 #endif
133         break;
134       case 2:
135 #ifdef LAYER2
136 #if 0
137         fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
138                          (fr->mode_ext<<2)+4 : fr->II_sblimit;
139 #endif
140         fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
141         fr->framesize /= freqs[fr->sampling_frequency];
142         fr->framesize += fr->padding - 4;
143 #else
144         fprintf(stderr,"Not supported!\n");
145 #endif
146         break;
147       case 3:
148 #if 0
149         fr->do_layer = do_layer3;
150         if(fr->lsf)
151           ssize = (fr->stereo == 1) ? 9 : 17;
152         else
153           ssize = (fr->stereo == 1) ? 17 : 32;
154 #endif
155
156 #if 0
157         if(fr->error_protection)
158           ssize += 2;
159 #endif
160           fr->framesize  = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
161           fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
162           fr->framesize = fr->framesize + fr->padding - 4;
163         break;
164       default:
165         fprintf(stderr,"Sorry, unknown layer type.\n");
166         return (0);
167     }
168     return 1;
169 }
170
171 #if 0
172 void print_header(struct frame *fr)
173 {
174         static const char * const modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
175         static const char * const layers[4] = { "Unknown" , "I", "II", "III" };
176
177         fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
178                 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
179                 layers[fr->lay],freqs[fr->sampling_frequency],
180                 modes[fr->mode],fr->mode_ext,fr->framesize+4);
181         fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
182                 fr->stereo,fr->copyright?"Yes":"No",
183                 fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
184                 fr->emphasis);
185         fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
186                 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
187 }
188
189 void print_header_compact(struct frame *fr)
190 {
191         static const char * const modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
192         static const char * const layers[4] = { "Unknown" , "I", "II", "III" };
193
194         fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
195                 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
196                 layers[fr->lay],
197                 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
198                 freqs[fr->sampling_frequency], modes[fr->mode]);
199 }
200
201 #endif
202
203 unsigned int getbits(int number_of_bits)
204 {
205   unsigned long rval;
206
207   if(!number_of_bits)
208     return 0;
209
210   {
211     rval = wordpointer[0];
212     rval <<= 8;
213     rval |= wordpointer[1];
214     rval <<= 8;
215     rval |= wordpointer[2];
216     rval <<= bitindex;
217     rval &= 0xffffff;
218
219     bitindex += number_of_bits;
220
221     rval >>= (24-number_of_bits);
222
223     wordpointer += (bitindex>>3);
224     bitindex &= 7;
225   }
226   return rval;
227 }
228
229 unsigned int getbits_fast(int number_of_bits)
230 {
231   unsigned long rval;
232
233   {
234     rval = wordpointer[0];
235     rval <<= 8;
236     rval |= wordpointer[1];
237     rval <<= bitindex;
238     rval &= 0xffff;
239     bitindex += number_of_bits;
240
241     rval >>= (16-number_of_bits);
242
243     wordpointer += (bitindex>>3);
244     bitindex &= 7;
245   }
246   return rval;
247 }
248
249 unsigned int get1bit(void)
250 {
251   unsigned char rval;
252   rval = *wordpointer << bitindex;
253
254   bitindex++;
255   wordpointer += (bitindex>>3);
256   bitindex &= 7;
257
258   return rval>>7;
259 }