2 * Copyright (c) Michael Hipp and other authors of the mpglib project.
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.
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.
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
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
35 static const struct parameter param = { 1 , 1 , 0 , 0 };
37 static const int tabsel_123[2][3][16] = {
38 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
39 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
40 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
42 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
43 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
44 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
47 static const long freqs[9] = { 44100, 48000, 32000,
49 11025 , 12000 , 8000 };
52 unsigned char *wordpointer;
53 unsigned char *pcm_sample;
57 #define HDRCMPMASK 0xfffffd00
59 static int head_check(unsigned long head)
61 if( (head & 0xffe00000) != 0xffe00000)
65 if( ((head>>12)&0xf) == 0xf)
67 if( ((head>>10)&0x3) == 0x3 )
74 * decode a header and write the information
75 * into the frame structure
77 int decode_header(struct frame *fr,unsigned long newhead)
79 if(head_check(newhead) == FALSE)
82 if( newhead & (1<<20) ) {
83 fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
91 fr->lay = 4-((newhead>>17)&3);
93 fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
96 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
97 fr->error_protection = ((newhead>>16)&0x1)^0x1;
99 if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */
100 fr->bitrate_index = ((newhead>>12)&0xf);
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;
111 fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
113 if(!fr->bitrate_index)
115 FIXME("Free format not supported.\n");
124 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
125 (fr->mode_ext<<2)+4 : 32;
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;
132 FIXME("Layer 1 not supported!\n");
138 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
139 (fr->mode_ext<<2)+4 : fr->II_sblimit;
141 fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
142 fr->framesize /= freqs[fr->sampling_frequency];
143 fr->framesize += fr->padding - 4;
146 FIXME("Layer 2 not supported!\n");
151 fr->do_layer = do_layer3;
153 ssize = (fr->stereo == 1) ? 9 : 17;
155 ssize = (fr->stereo == 1) ? 17 : 32;
159 if(fr->error_protection)
162 fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
163 fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
164 fr->framesize = fr->framesize + fr->padding - 4;
167 FIXME("Unknown layer type: %d\n", fr->lay);
174 void print_header(struct frame *fr)
176 static const char * const modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
177 static const char * const layers[4] = { "Unknown" , "I", "II", "III" };
179 FIXME("MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
180 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
181 layers[fr->lay],freqs[fr->sampling_frequency],
182 modes[fr->mode],fr->mode_ext,fr->framesize+4);
183 FIXME("Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
184 fr->stereo,fr->copyright?"Yes":"No",
185 fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
187 FIXME("Bitrate: %d Kbits/s, Extension value: %d\n",
188 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
191 void print_header_compact(struct frame *fr)
193 static const char * const modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
194 static const char * const layers[4] = { "Unknown" , "I", "II", "III" };
196 FIXME("MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
197 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
199 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
200 freqs[fr->sampling_frequency], modes[fr->mode]);
205 unsigned int getbits(int number_of_bits)
213 rval = wordpointer[0];
215 rval |= wordpointer[1];
217 rval |= wordpointer[2];
221 bitindex += number_of_bits;
223 rval >>= (24-number_of_bits);
225 wordpointer += (bitindex>>3);
231 unsigned int getbits_fast(int number_of_bits)
236 rval = wordpointer[0];
238 rval |= wordpointer[1];
241 bitindex += number_of_bits;
243 rval >>= (16-number_of_bits);
245 wordpointer += (bitindex>>3);
251 unsigned int get1bit(void)
254 rval = *wordpointer << bitindex;
257 wordpointer += (bitindex>>3);