1 /*
2  *  mpadec - MPEG audio decoder
3  *  Copyright (C) 2002-2004 Dmitriy Startsev (dstartsev@rambler.ru)
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 /* $Id: mpadec_internal.h,v 1.1.1.1 2004/07/27 02:57:55 metal_man Exp $ */
21 
22 #ifndef __MPADEC_INTERNAL_H
23 #define __MPADEC_INTERNAL_H
24 
25 #include "csoundCore.h"
26 #include "mpadec.h"
27 
28 #define MPADEC_STATE_UNDEFINED 0
29 #define MPADEC_STATE_START     1
30 #define MPADEC_STATE_DECODE    2
31 
32 #define MPG_MD_STEREO       0
33 #define MPG_MD_JOINT_STEREO 1
34 #define MPG_MD_DUAL_CHANNEL 2
35 #define MPG_MD_MONO         3
36 
37 #define SBLIMIT     32
38 #define SSLIMIT     18
39 #define SCALE_BLOCK 12
40 
41 typedef struct {
42   MYFLT decwin[512 + 32];
43   MYFLT muls[27][64];
44   MYFLT gainpow2[256 + 122];
45   MYFLT ispow[8207];
46   MYFLT win[2][4][36];
47   MYFLT *istabs[3][2][2];
48   MYFLT tan1_1[16];
49   MYFLT tan2_1[16];
50   MYFLT tan1_2[16];
51   MYFLT tan2_2[16];
52   MYFLT pow1_1[2][16];
53   MYFLT pow2_1[2][16];
54   MYFLT pow1_2[2][16];
55   MYFLT pow2_2[2][16];
56   int32_t long_limit[9][23];
57   int32_t short_limit[9][14];
58   int32_t n_slen2[512];
59   int32_t i_slen2[256];
60   int32_t mapbuf0[9][152];
61   int32_t mapbuf1[9][156];
62   int32_t mapbuf2[9][44];
63   int32_t *map[9][3];
64   int32_t *mapend[9][3];
65   uint8_t *mp2tables[10];
66   uint8_t grp3tab[32*3];
67   uint8_t grp5tab[128*3];
68   uint8_t grp9tab[1024*3];
69 } mpadec_tables_t;
70 
71 typedef struct {
72   uint16_t bits;
73   int16_t d;
74 } alloc_table_t;
75 
76 typedef struct {
77   uint32_t linbits;
78   int16_t *table;
79 } newhuff_t;
80 
81 typedef struct {
82   int16_t long_idx[23];
83   int16_t long_diff[22];
84   int16_t short_idx[14];
85   int16_t short_diff[13];
86 } bandinfo_t;
87 
88 typedef struct {
89   uint8_t layer;
90   uint8_t mode;
91   uint8_t channels;
92   uint8_t decoded_channels;
93   uint8_t LSF;
94   uint8_t MPEG25;
95   uint8_t CRC;
96   uint8_t extension;
97   uint8_t mode_ext;
98   uint8_t copyright;
99   uint8_t original;
100   uint8_t emphasis;
101   uint8_t jsbound;
102   uint8_t sblimit;
103   uint8_t downsample;
104   uint8_t downsample_sblimit;
105   uint16_t crc;
106   uint16_t rsvd;
107   uint32_t padding;
108   uint32_t bitrate_index;
109   uint32_t frequency_index;
110   uint32_t bitrate;
111   uint32_t frequency;
112   uint32_t frame_size;
113   uint32_t frame_samples;
114   uint32_t decoded_frequency;
115   uint32_t decoded_samples;
116   uint32_t decoded_size;
117   alloc_table_t *alloc_table;
118 } frameinfo_t;
119 
120 typedef struct {
121   int32_t scfsi;
122   uint32_t part2_3_length;
123   uint32_t big_values;
124   uint32_t scalefac_compress;
125   uint8_t block_type;
126   uint8_t mixed_block_flag;
127   uint8_t preflag;
128   uint8_t scalefac_scale;
129   uint32_t table_select[3];
130   uint32_t subblock_gain[3];
131   uint32_t maxband[3];
132   uint32_t maxbandl;
133   uint32_t maxb;
134   uint32_t region1start;
135   uint32_t region2start;
136   uint32_t count1table_select;
137   MYFLT *full_gain[3];
138   MYFLT *pow2gain;
139 } grinfo_t;
140 
141 typedef struct {
142   uint32_t main_data_begin;
143   uint32_t private_bits;
144   struct {
145     grinfo_t gr[2];
146   } ch[2];
147 } sideinfo_t;
148 
149 struct mpadec_t {
150   uint32_t size;
151   uint32_t state;
152   uint8_t *next_byte;
153   uint32_t bytes_left;
154   uint32_t bit_buffer;
155   uint8_t bits_left;
156   uint8_t error;
157   uint8_t free_format;
158   uint8_t pad1;
159   uint32_t sample_size;
160   uint32_t prev_frame_size;
161   uint32_t header;
162   uint32_t hsize;
163   uint32_t ssize;
164   uint32_t dsize;
165   uint16_t crc;
166   uint16_t pad2;
167   uint32_t skip_samples;
168   uint32_t padding_samples;
169   uint32_t padding_start;
170   uint32_t decoded_frames;
171   uint32_t decoded_samples;
172   mp3tag_info_t tag_info;
173   uint32_t synth_size;
174   MYFLT replay_gain;
175   void (*synth_func)(void *mpadec, MYFLT block[SBLIMIT],
176                      int channel, uint8_t *buffer);
177   uint32_t reservoir_size;
178   uint8_t reservoir[2048];
179   frameinfo_t frame;
180   sideinfo_t sideinfo;
181   mpadec_config_t config;
182   mpadec_tables_t tables;
183   uint32_t synth_bufoffs;
184   uint8_t hybrid_block[4];
185   MYFLT hybrid_in[2][SBLIMIT+1][SSLIMIT];
186   MYFLT hybrid_out[2][SSLIMIT+1][SBLIMIT];
187   MYFLT hybrid_buffers[2][2][SBLIMIT*SSLIMIT+SBLIMIT+SSLIMIT];
188   MYFLT synth_buffers[2][2][0x110];
189 };
190 
191 struct mpabuffer_t {
192   uint32_t size;
193   uint32_t offset;
194   uint32_t used;
195   uint8_t *buffer;
196   struct mpabuffer_t *next;
197 };
198 
199 struct mpadec2_t {
200   uint32_t size;
201   mpadec_t mpadec;
202   struct mpabuffer_t *buffers;
203   uint32_t in_buffer_offset;
204   uint32_t in_buffer_used;
205   uint32_t out_buffer_offset;
206   uint32_t out_buffer_used;
207   uint8_t in_buffer[0x10000];
208   uint8_t out_buffer[8*1152];
209 };
210 
211 #define GETBITS(n) ((mpa->bits_left >= \
212    (uint8_t)(n)) ? ((mpa->bit_buffer >> (mpa->bits_left -= (uint8_t)(n))) \
213                     & bitmask[n]) : getbits(mpa, n))
214 
215 #endif
216