Fix improper WAVEFORMATEX size calculation for non PCM formats.
[wine] / dlls / msacm / winemp3 / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <signal.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26
27 #include "mpg123.h"
28
29 struct parameter param = { 1 , 1 , 0 , 0 };
30
31 int tabsel_123[2][3][16] = {
32    { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
33      {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
34      {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
35
36    { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
37      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
38      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
39 };
40
41 long freqs[9] = { 44100, 48000, 32000,
42                   22050, 24000, 16000 ,
43                   11025 , 12000 , 8000 };
44
45 int bitindex;
46 unsigned char *wordpointer;
47 unsigned char *pcm_sample;
48 int pcm_point = 0;
49
50
51 #define HDRCMPMASK 0xfffffd00
52
53 #if 0
54 int head_check(unsigned long head)
55 {
56     if( (head & 0xffe00000) != 0xffe00000)
57         return FALSE;
58     if(!((head>>17)&3))
59         return FALSE;
60     if( ((head>>12)&0xf) == 0xf)
61         return FALSE;
62     if( ((head>>10)&0x3) == 0x3 )
63         return FALSE;
64     return TRUE;
65 }
66 #endif
67
68
69 /*
70  * the code a header and write the information
71  * into the frame structure
72  */
73 int decode_header(struct frame *fr,unsigned long newhead)
74 {
75     if( newhead & (1<<20) ) {
76       fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
77       fr->mpeg25 = 0;
78     }
79     else {
80       fr->lsf = 1;
81       fr->mpeg25 = 1;
82     }
83
84     fr->lay = 4-((newhead>>17)&3);
85     if( ((newhead>>10)&0x3) == 0x3) {
86       fprintf(stderr,"Stream error\n");
87       return (0);
88     }
89     if(fr->mpeg25) {
90       fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
91     }
92     else
93       fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
94     fr->error_protection = ((newhead>>16)&0x1)^0x1;
95
96     if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */
97       fr->bitrate_index = ((newhead>>12)&0xf);
98
99     fr->bitrate_index = ((newhead>>12)&0xf);
100     fr->padding   = ((newhead>>9)&0x1);
101     fr->extension = ((newhead>>8)&0x1);
102     fr->mode      = ((newhead>>6)&0x3);
103     fr->mode_ext  = ((newhead>>4)&0x3);
104     fr->copyright = ((newhead>>3)&0x1);
105     fr->original  = ((newhead>>2)&0x1);
106     fr->emphasis  = newhead & 0x3;
107
108     fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
109
110     if(!fr->bitrate_index)
111     {
112       fprintf(stderr,"Free format not supported.\n");
113       return (0);
114     }
115
116     switch(fr->lay)
117     {
118       case 1:
119 #ifdef LAYER1
120 #if 0
121         fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
122                          (fr->mode_ext<<2)+4 : 32;
123 #endif
124         fr->framesize  = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
125         fr->framesize /= freqs[fr->sampling_frequency];
126         fr->framesize  = ((fr->framesize+fr->padding)<<2)-4;
127 #else
128         fprintf(stderr,"Not supported!\n");
129 #endif
130         break;
131       case 2:
132 #ifdef LAYER2
133 #if 0
134         fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
135                          (fr->mode_ext<<2)+4 : fr->II_sblimit;
136 #endif
137         fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
138         fr->framesize /= freqs[fr->sampling_frequency];
139         fr->framesize += fr->padding - 4;
140 #else
141         fprintf(stderr,"Not supported!\n");
142 #endif
143         break;
144       case 3:
145 #if 0
146         fr->do_layer = do_layer3;
147         if(fr->lsf)
148           ssize = (fr->stereo == 1) ? 9 : 17;
149         else
150           ssize = (fr->stereo == 1) ? 17 : 32;
151 #endif
152
153 #if 0
154         if(fr->error_protection)
155           ssize += 2;
156 #endif
157           fr->framesize  = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
158           fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
159           fr->framesize = fr->framesize + fr->padding - 4;
160         break;
161       default:
162         fprintf(stderr,"Sorry, unknown layer type.\n");
163         return (0);
164     }
165     return 1;
166 }
167
168 #if 0
169 void print_header(struct frame *fr)
170 {
171         static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
172         static char *layers[4] = { "Unknown" , "I", "II", "III" };
173
174         fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
175                 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
176                 layers[fr->lay],freqs[fr->sampling_frequency],
177                 modes[fr->mode],fr->mode_ext,fr->framesize+4);
178         fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
179                 fr->stereo,fr->copyright?"Yes":"No",
180                 fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
181                 fr->emphasis);
182         fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
183                 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
184 }
185
186 void print_header_compact(struct frame *fr)
187 {
188         static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
189         static char *layers[4] = { "Unknown" , "I", "II", "III" };
190
191         fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
192                 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
193                 layers[fr->lay],
194                 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
195                 freqs[fr->sampling_frequency], modes[fr->mode]);
196 }
197
198 #endif
199
200 unsigned int getbits(int number_of_bits)
201 {
202   unsigned long rval;
203
204   if(!number_of_bits)
205     return 0;
206
207   {
208     rval = wordpointer[0];
209     rval <<= 8;
210     rval |= wordpointer[1];
211     rval <<= 8;
212     rval |= wordpointer[2];
213     rval <<= bitindex;
214     rval &= 0xffffff;
215
216     bitindex += number_of_bits;
217
218     rval >>= (24-number_of_bits);
219
220     wordpointer += (bitindex>>3);
221     bitindex &= 7;
222   }
223   return rval;
224 }
225
226 unsigned int getbits_fast(int number_of_bits)
227 {
228   unsigned long rval;
229
230   {
231     rval = wordpointer[0];
232     rval <<= 8;
233     rval |= wordpointer[1];
234     rval <<= bitindex;
235     rval &= 0xffff;
236     bitindex += number_of_bits;
237
238     rval >>= (16-number_of_bits);
239
240     wordpointer += (bitindex>>3);
241     bitindex &= 7;
242   }
243   return rval;
244 }
245
246 unsigned int get1bit(void)
247 {
248   unsigned char rval;
249   rval = *wordpointer << bitindex;
250
251   bitindex++;
252   wordpointer += (bitindex>>3);
253   bitindex &= 7;
254
255   return rval>>7;
256 }