20 {
21
22
23 $ThisFileInfo['mpc']['header'] = array();
24 $thisfile_mpc_header = &$ThisFileInfo['mpc']['header'];
25
26 $ThisFileInfo['fileformat'] = 'mpc';
27 $ThisFileInfo['audio']['dataformat'] = 'mpc';
28 $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
29 $ThisFileInfo['audio']['channels'] = 2;
30 $ThisFileInfo['audio']['lossless'] = false;
31
32 fseek($fd, $ThisFileInfo[
'avdataoffset'], SEEK_SET);
33
34 $thisfile_mpc_header['size'] = 28;
35 $MPCheaderData =
fread($fd, $thisfile_mpc_header[
'size']);
36 $offset = 0;
37
38 if (substr($MPCheaderData, $offset, 3) == 'MP+') {
39
40
41 $thisfile_mpc_header['raw']['preamble'] = substr($MPCheaderData, $offset, 3);
42 $offset += 3;
43
44 } elseif (preg_match('/^[\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0]/s', substr($MPCheaderData, 0, 4))) {
45
46
47 $thisfile_mpc_header['size'] = 8;
48
49
50 $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size'];
51
52
55
56
57
58
59
60
61
62
63
64
65
66
67 $thisfile_mpc_header['target_bitrate'] = (($HeaderDWORD[0] & 0xFF800000) >> 23);
68 $thisfile_mpc_header['intensity_stereo'] = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22);
69 $thisfile_mpc_header['mid-side_stereo'] = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21);
70 $thisfile_mpc_header['stream_major_version'] = ($HeaderDWORD[0] & 0x001FF800) >> 11;
71 $thisfile_mpc_header['stream_minor_version'] = 0;
72 $thisfile_mpc_header['max_band'] = ($HeaderDWORD[0] & 0x000007C0) >> 6;
73 $thisfile_mpc_header['block_size'] = ($HeaderDWORD[0] & 0x0000003F);
74
75 switch ($thisfile_mpc_header['stream_major_version']) {
76 case 4:
77 $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16);
78 break;
79
80 case 5:
81 case 6:
82 $thisfile_mpc_header['frame_count'] = $HeaderDWORD[1];
83 break;
84
85 default:
86 $ThisFileInfo['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_major_version'].' instead';
87 unset($ThisFileInfo['mpc']);
88 return false;
89 break;
90 }
91
92 if (($thisfile_mpc_header['stream_major_version'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) {
93 $ThisFileInfo['warning'][] = 'Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size'];
94 }
95
96 $thisfile_mpc_header['sample_rate'] = 44100;
97 $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
98 $thisfile_mpc_header['samples'] = $thisfile_mpc_header['frame_count'] * 1152 * $ThisFileInfo['audio']['channels'];
99
100 if ($thisfile_mpc_header['target_bitrate'] == 0) {
101 $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
102 } else {
103 $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
104 }
105
106 $ThisFileInfo['mpc']['bitrate'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152;
107 $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpc']['bitrate'];
108 $ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version'];
109
110 return true;
111
112 } else {
113
114 $ThisFileInfo['error'][] = 'Expecting "MP+" at offset '.$ThisFileInfo['avdataoffset'].', found "'.substr($MPCheaderData, $offset, 3).'"';
115 unset($ThisFileInfo['fileformat']);
116 unset($ThisFileInfo['mpc']);
117 return false;
118
119 }
120
121
123 $offset += 1;
124 $thisfile_mpc_header['stream_major_version'] = ($StreamVersionByte & 0x0F);
125 $thisfile_mpc_header['stream_minor_version'] = ($StreamVersionByte & 0xF0) >> 4;
127 $offset += 4;
128
129 switch ($thisfile_mpc_header['stream_major_version']) {
130 case 7:
131
132 break;
133
134 default:
135 $ThisFileInfo['error'][] = 'Only Musepack SV7 supported';
136 return false;
137 }
138
140 $offset += 4;
141 $thisfile_mpc_header['intensity_stereo'] = (bool) (($FlagsDWORD1 & 0x80000000) >> 31);
142 $thisfile_mpc_header['mid_side_stereo'] = (bool) (($FlagsDWORD1 & 0x40000000) >> 30);
143 $thisfile_mpc_header['max_subband'] = ($FlagsDWORD1 & 0x3F000000) >> 24;
144 $thisfile_mpc_header['raw']['profile'] = ($FlagsDWORD1 & 0x00F00000) >> 20;
145 $thisfile_mpc_header['begin_loud'] = (bool) (($FlagsDWORD1 & 0x00080000) >> 19);
146 $thisfile_mpc_header['end_loud'] = (bool) (($FlagsDWORD1 & 0x00040000) >> 18);
147 $thisfile_mpc_header['raw']['sample_rate'] = ($FlagsDWORD1 & 0x00030000) >> 16;
148 $thisfile_mpc_header['max_level'] = ($FlagsDWORD1 & 0x0000FFFF);
149
151 $offset += 2;
153 $offset += 2;
154
156 $offset += 2;
158 $offset += 2;
159
161 $offset += 4;
162 $thisfile_mpc_header['true_gapless'] = (bool) (($FlagsDWORD2 & 0x80000000) >> 31);
163 $thisfile_mpc_header['last_frame_length'] = ($FlagsDWORD2 & 0x7FF00000) >> 20;
164
165
167 $offset += 3;
169 $offset += 1;
170
171 $thisfile_mpc_header[
'profile'] = $this->
MPCprofileNameLookup($thisfile_mpc_header[
'raw'][
'profile']);
172 $thisfile_mpc_header[
'sample_rate'] = $this->
MPCfrequencyLookup($thisfile_mpc_header[
'raw'][
'sample_rate']);
173 if ($thisfile_mpc_header['sample_rate'] == 0) {
174 $ThisFileInfo['error'][] = 'Corrupt MPC file: frequency == zero';
175 return false;
176 }
177 $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
178 $thisfile_mpc_header['samples'] = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $ThisFileInfo['audio']['channels'];
179
180 $ThisFileInfo['playtime_seconds'] = ($thisfile_mpc_header['samples'] / $ThisFileInfo['audio']['channels']) / $ThisFileInfo['audio']['sample_rate'];
181 if ($ThisFileInfo['playtime_seconds'] == 0) {
182 $ThisFileInfo['error'][] = 'Corrupt MPC file: playtime_seconds == zero';
183 return false;
184 }
185
186
187 $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size'];
188
189 $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
190
191 $thisfile_mpc_header['title_peak'] = $thisfile_mpc_header['raw']['title_peak'];
192 $thisfile_mpc_header[
'title_peak_db'] = $this->
MPCpeakDBLookup($thisfile_mpc_header[
'title_peak']);
193 if ($thisfile_mpc_header['raw']['title_gain'] < 0) {
194 $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100;
195 } else {
196 $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100;
197 }
198
199 $thisfile_mpc_header['album_peak'] = $thisfile_mpc_header['raw']['album_peak'];
200 $thisfile_mpc_header[
'album_peak_db'] = $this->
MPCpeakDBLookup($thisfile_mpc_header[
'album_peak']);
201 if ($thisfile_mpc_header['raw']['album_gain'] < 0) {
202 $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100;
203 } else {
204 $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;;
205 }
206 $thisfile_mpc_header[
'encoder_version'] = $this->
MPCencoderVersionLookup($thisfile_mpc_header[
'raw'][
'encoder_version']);
207
208 $ThisFileInfo['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db'];
209 $ThisFileInfo['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db'];
210
211 if ($thisfile_mpc_header['title_peak'] > 0) {
212 $ThisFileInfo['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak'];
213 } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) {
214 $ThisFileInfo[
'replay_gain'][
'track'][
'peak'] =
getid3_lib::CastAsInt(round($thisfile_mpc_header[
'max_level'] * 1.18));
215 }
216 if ($thisfile_mpc_header['album_peak'] > 0) {
217 $ThisFileInfo['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak'];
218 }
219
220
221 $ThisFileInfo['audio']['encoder'] = $thisfile_mpc_header['encoder_version'];
222 $ThisFileInfo['audio']['encoder_options'] = $thisfile_mpc_header['profile'];
223
224 return true;
225 }
LittleEndian2Int($byteword, $signed=false)
MPCpeakDBLookup($intvalue)
MPCencoderVersionLookup($encoderversion)
MPCprofileNameLookup($profileid)
MPCfrequencyLookup($frequencyid)