1 /**********************************************************
2  *
3  * libmp3splt -- library based on mp3splt,
4  *               for mp3/ogg splitting without decoding
5  *
6  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
7  * Copyright (c) 2005-2014 Alexandru Munteanu - m@ioalex.net
8  *
9  * http://mp3splt.sourceforge.net
10  *
11  *********************************************************/
12 
13 /**********************************************************
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28  * USA.
29  *
30  *********************************************************/
31 
32 #ifndef MP3SPLT_MP3_H
33 
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <math.h>
39 #include <ctype.h>
40 
41 #ifdef __WIN32__
42 #include <io.h>
43 #include <fcntl.h>
44 #endif
45 
46 #ifndef NO_ID3TAG
47 #include <id3tag.h>
48 #endif
49 
50 #include <mad.h>
51 
52 /**********************************/
53 /* Mp3 structures                 */
54 
55 #define SPLT_MAD_BSIZE 4032
56 
57 #define SPLT_MP3_MAX_BYTE_RESERVOIR_HEADERS 30
58 
59 #ifndef NO_ID3TAG
60 typedef struct {
61   id3_byte_t *tag_bytes;
62   id3_length_t tag_length;
63 
64   id3_byte_t *tag_bytes_v1;
65   id3_length_t tag_length_v1;
66 
67   unsigned int version;
68   unsigned int bytes_tags_version;
69 } tag_bytes_and_size;
70 #endif
71 
72 struct splt_header {
73   off_t ptr;    // offset of header
74   int bitrate;
75   int padding;
76   int framesize;
77 
78   int has_crc;
79   int sideinfo_size;
80 
81   //info from the side info for layer 3
82   int main_data_begin;
83   int frame_data_space;
84 };
85 
86 struct splt_reservoir {
87   unsigned char reservoir[511];
88   int reservoir_end;
89   unsigned char *reservoir_frame;
90   unsigned int reservoir_frame_size;
91 };
92 
93 // Struct that will contains infos on mp3 and an header struct of first valid header
94 struct splt_mp3 {
95   int mpgid;    // mpgid among SPLT_MP3_MPEG1_ID or SPLT_MP3_MPEG2_ID or SPLT_MP3_MPEG25_ID
96   int layer;    // layer 1, 2 or 3
97   //0 = single channel
98   //1 = dual channel
99   //2 = joint stereo
100   //3 = stereo
101   //4 = other
102   int channels;
103   int freq;
104   int bitrate;
105   float fps;
106   int samples_per_frame;
107   //used for the xing header
108   int xing;
109   char *xingbuffer;
110   off_t xing_offset;
111   int xing_content_size;
112   int xing_has_frames;
113   int xing_has_bytes;
114   int xing_has_toc;
115   int xing_has_quality;
116   int lame_delay;
117   int lame_padding;
118   //length of the mp3 file
119   off_t len;
120   //where we begin reading
121   off_t firsth;
122   struct splt_header firsthead;
123 };
124 
125 typedef struct {
126   FILE *file_input;
127   struct splt_header h;
128   //if we are in framemode or not
129   short framemode;
130   //total frames
131   unsigned long frames;
132   int syncdetect;
133   off_t end;
134   unsigned long fend;
135   off_t end_non_zero;
136   off_t end2;
137   off_t bytes;
138   int first;
139   unsigned long headw;
140 
141   unsigned first_frame_header_for_reservoir;
142   int is_guessed_vbr;
143 
144   //see the mp3 structure
145   struct splt_mp3 mp3file;
146 
147   //for byte reservoir
148   struct splt_header br_headers[SPLT_MP3_MAX_BYTE_RESERVOIR_HEADERS];
149   int next_br_header_index;
150   int number_of_br_headers_stored;
151   struct splt_reservoir reservoir;
152   long begin_sample;
153   long end_sample;
154   long first_frame_inclusive;
155   long last_frame_inclusive;
156 
157   long overlapped_number_of_frames;
158   unsigned char *overlapped_frames;
159   size_t overlapped_frames_bytes;
160 
161   int new_xing_lame_frame_size;
162   unsigned char *new_xing_lame_frame;
163 
164   //used internally, libmad structures
165   struct mad_stream stream;
166   struct mad_frame frame;
167   struct mad_synth synth;
168   //internally used by the silence detection functions
169   mad_fixed_t temp_level;
170   //the offset
171   float off;
172   //used internally when reading the file
173   unsigned char inputBuffer[SPLT_MAD_BSIZE];
174   //mad timer
175   mad_timer_t timer;
176   //used internally, pointer to the beginning of a frame
177   unsigned char *data_ptr;
178   //used internally, length of a frame
179   long data_len;
180   //length of a buffer when reading a frame
181   int buf_len;
182 } splt_mp3_state;
183 
184 /****************************/
185 /* mp3 constants */
186 
187 /*
188    Frame per second:
189    Each MPEG1 frame decodes to 1152 PCM
190    samples, 576 with MPEG2.
191    32000/1152 = 27.77778 = 16000/576
192    44100/1152 = 38.28125 = 22050/576
193    48000/1152 = 41.66667 = 24000/576
194    */
195 
196 #define SPLT_MP3_TAG "TAG"
197 
198 #define SPLT_MP3_LAYER1_SAMPLES_PER_FRAME 384
199 #define SPLT_MP3_LAYER3_MPEG1_AND_LAYER2_SAMPLES_PER_FRAME 1152
200 #define SPLT_MP3_LAYER3_MPEG2_SAMPLES_PER_FRAME 576
201 
202 #define SPLT_MP3_NO_END_SAMPLE -10000
203 #define SPLT_MP3_MIN_OVERLAP_SAMPLES_START SPLT_MP3_LAYER3_MPEG2_SAMPLES_PER_FRAME
204 #define SPLT_MP3_MIN_OVERLAP_SAMPLES_END SPLT_MP3_LAYER3_MPEG1_AND_LAYER2_SAMPLES_PER_FRAME
205 
206 #define SPLT_MP3_BYTE 8
207 
208 #define SPLT_MP3_MPEG1_ID 3
209 #define SPLT_MP3_MPEG2_ID 2
210 #define SPLT_MP3_MPEG25_ID 0
211 
212 #define SPLT_MP3_XING_MAGIC 0x58696E67
213 #define SPLT_MP3_INFO_MAGIC 0x496E666F
214 
215 #define SPLT_MP3_XING_FRAMES  0x00000001L
216 #define SPLT_MP3_XING_BYTES   0x00000002L
217 #define SPLT_MP3_XING_TOC     0x00000004L
218 #define SPLT_MP3_XING_QUALITY 0x00000008L
219 
220 #define SPLT_MP3_XING_FLAGS_SIZE 4
221 
222 #define SPLT_MP3_LAME_DELAY_OFFSET 21
223 #define SPLT_MP3_LAME_MAX_DELAY 4095
224 #define SPLT_MP3_LAME_MAX_PADDING 4095
225 
226 #define SPLT_MP3_ID3_ARTIST 1
227 #define SPLT_MP3_ID3_ALBUM 2
228 #define SPLT_MP3_ID3_TITLE 3
229 #define SPLT_MP3_ID3_YEAR 4
230 #define SPLT_MP3_ID3_GENRE 5
231 #define SPLT_MP3_ID3_TRACK 6
232 #define SPLT_MP3_ID3_COMMENT 7
233 
234 #define SPLT_MP3_CRCLEN 4
235 #define SPLT_MP3_ABWINDEXOFFSET 0x539
236 #define SPLT_MP3_ABWLEN 0x1f5
237 #define SPLT_MP3_INDEXVERSION 1
238 #define SPLT_MP3_READBSIZE 1024
239 
240 #define SPLT_MP3EXT ".mp3"
241 
242 //! The layer- and-bitrate-table
243 static const int splt_mp3_tabsel_123[2][3][16] = {
244   { {128,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
245     {128,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
246     {128,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
247 
248   { {128,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
249     {128,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
250     {128,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
251 };
252 
253 #define MP3SPLT_MP3_H
254 
255 #endif
256 
257