1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16 
17 // Use Safefree for libid3tag free calls on Windows
18 #ifdef _MSC_VER
19 #define free(ptr) Safefree(ptr)
20 #endif
21 
22 #define MP3_BLOCK_SIZE 4096
23 
24 #define XING_FRAMES  0x01
25 #define XING_BYTES   0x02
26 #define XING_TOC     0x04
27 #define XING_QUALITY 0x08
28 
29 #define CBR 1
30 #define ABR 2
31 #define VBR 3
32 
33 #define ILLEGAL_MPEG_ID  1
34 #define MPEG1_ID         3
35 #define MPEG2_ID         2
36 #define MPEG25_ID        0
37 #define ILLEGAL_LAYER_ID 0
38 #define LAYER1_ID        3
39 #define LAYER2_ID        2
40 #define LAYER3_ID        1
41 #define ILLEGAL_SR       3
42 #define MODE_MONO        3
43 
44 // Based on pcutmp3 FrameHeader
45 typedef struct mp3frame {
46   int header32;
47   int mpegID;
48   int layerID;
49   bool crc16_used;
50   int bitrate_index;
51   int samplingrate_index;
52   bool padding;
53   bool private_bit_set;
54   int mode;
55   int mode_extension;
56   bool copyrighted;
57   bool original;
58   int emphasis;
59 
60   bool valid;
61 
62   int samplerate;
63   int channels;
64   int bitrate_kbps;
65   int samples_per_frame;
66   int bytes_per_slot;
67   int frame_size;
68 } mp3frame;
69 
70 // based on pcutmp3 XingInfoLameTagFrame
71 typedef struct xingframe {
72   int frame_size;
73 
74   bool xing_tag;
75   bool info_tag;
76   int flags;
77   int xing_frames;
78   int xing_bytes;
79   bool has_toc;
80   uint8_t xing_toc[100];
81   int xing_quality;
82 
83   bool lame_tag;
84   char lame_encoder_version[9];
85   uint8_t lame_tag_revision;
86   uint8_t lame_vbr_method;
87   int lame_lowpass;
88   float lame_replay_gain[2];
89   uint16_t lame_abr_rate;
90   int lame_encoder_delay;
91   int lame_encoder_padding;
92   uint8_t lame_noise_shaping;
93   uint8_t lame_stereo_mode;
94   uint8_t lame_unwise;
95   uint8_t lame_source_freq;
96   int lame_mp3gain;
97   float lame_mp3gain_db;
98   uint8_t lame_surround;
99   uint16_t lame_preset;
100   int lame_music_length;
101 
102   bool vbri_tag;
103   uint16_t vbri_delay;
104   uint16_t vbri_quality;
105   uint32_t vbri_bytes;
106   uint32_t vbri_frames;
107 
108   int lame_tag_ofs;
109 } xingframe;
110 
111 typedef struct mp3info {
112   PerlIO *infile;
113   char *file;
114   Buffer *buf;
115   HV *info;
116 
117   off_t file_size;
118   uint32_t id3_size;
119   off_t audio_offset;
120   off_t audio_size;
121   uint16_t bitrate;
122   uint32_t song_length_ms;
123 
124   uint8_t vbr;
125   int music_frame_count;
126   int samples_per_frame;
127 
128   mp3frame *first_frame;
129   xingframe *xing_frame;
130 } mp3info;
131 
132 // LAME lookup tables
133 const char *stereo_modes[] = {
134   "Mono",
135   "Stereo",
136   "Dual",
137   "Joint",
138   "Force",
139   "Auto",
140   "Intensity",
141   "Undefined"
142 };
143 
144 const char *source_freqs[] = {
145   "<= 32 kHz",
146   "44.1 kHz",
147   "48 kHz",
148   "> 48 kHz"
149 };
150 
151 const char *surround[] = {
152   "None",
153   "DPL encoding",
154   "DPL2 encoding",
155   "Ambisonic encoding",
156   "Reserved"
157 };
158 
159 const char *vbr_methods[] = {
160   "Unknown",
161   "Constant Bitrate",
162   "Average Bitrate",
163   "Variable Bitrate method1 (old/rh)",
164   "Variable Bitrate method2 (mtrh)",
165   "Variable Bitrate method3 (mt)",
166   "Variable Bitrate method4",
167   NULL,
168   "Constant Bitrate (2 pass)",
169   "Average Bitrate (2 pass)",
170   NULL,
171   NULL,
172   NULL,
173   NULL,
174   NULL,
175   "Reserved"
176 };
177 
178 const char *presets_v[] = {
179   "V9",
180   "V8",
181   "V7",
182   "V6",
183   "V5",
184   "V4",
185   "V3",
186   "V2",
187   "V1",
188   "V0"
189 };
190 
191 const char *presets_old[] = {
192   "r3mix",
193   "standard",
194   "extreme",
195   "insane",
196   "standard/fast",
197   "extreme/fast",
198   "medium",
199   "medium/fast"
200 };
201 
202 static int bitrate_map[4][4][16] = {
203   { { 0 }, //MPEG2.5
204     { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 },
205     { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 },
206     { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 }
207   },
208   { { 0 } },
209   { { 0 }, // MPEG2
210     { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 },
211     { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 },
212     { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 }
213   },
214   { { 0 }, // MPEG1
215     { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 },
216     { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0 },
217     { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 }
218   }
219 };
220 
221 // sample_rate[samplingrate_index]
222 static int sample_rate_tbl[ ] = {
223   44100, 48000, 32000, 0,
224 };
225 
226 int get_mp3tags(PerlIO *infile, char *file, HV *info, HV *tags);
227 int get_mp3fileinfo(PerlIO *infile, char *file, HV *info);
228 int mp3_find_frame(PerlIO *infile, char *file, int offset);
229 
230 mp3info * _mp3_parse(PerlIO *infile, char *file, HV *info);
231 int _decode_mp3_frame(unsigned char *bptr, struct mp3frame *frame);
232 int _is_ape_header(char *bptr);
233 int _has_ape(PerlIO *infile, off_t file_size, HV *info);
234 void _mp3_skip(mp3info *mp3, uint32_t size);
235