Annotation of /trunk/lib/getid3/module.audio.mpc.php
Parent Directory
|
Revision Log
Revision 2 - (view) (download)
| 1 : | andphe | 2 | <?php |
| 2 : | ///////////////////////////////////////////////////////////////// | ||
| 3 : | /// getID3() by James Heinrich <info@getid3.org> // | ||
| 4 : | // available at http://getid3.sourceforge.net // | ||
| 5 : | // or http://www.getid3.org // | ||
| 6 : | ///////////////////////////////////////////////////////////////// | ||
| 7 : | // See readme.txt for more details // | ||
| 8 : | ///////////////////////////////////////////////////////////////// | ||
| 9 : | // // | ||
| 10 : | // module.audio.mpc.php // | ||
| 11 : | // module for analyzing Musepack/MPEG+ Audio files // | ||
| 12 : | // dependencies: NONE // | ||
| 13 : | // /// | ||
| 14 : | ///////////////////////////////////////////////////////////////// | ||
| 15 : | // MOS Intruder Alerts | ||
| 16 : | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); | ||
| 17 : | |||
| 18 : | class getid3_mpc | ||
| 19 : | { | ||
| 20 : | |||
| 21 : | function getid3_mpc(&$fd, &$ThisFileInfo) { | ||
| 22 : | // http://www.uni-jena.de/~pfk/mpp/sv8/header.html | ||
| 23 : | |||
| 24 : | $ThisFileInfo['mpc']['header'] = array(); | ||
| 25 : | $thisfile_mpc_header = &$ThisFileInfo['mpc']['header']; | ||
| 26 : | |||
| 27 : | $ThisFileInfo['fileformat'] = 'mpc'; | ||
| 28 : | $ThisFileInfo['audio']['dataformat'] = 'mpc'; | ||
| 29 : | $ThisFileInfo['audio']['bitrate_mode'] = 'vbr'; | ||
| 30 : | $ThisFileInfo['audio']['channels'] = 2; // the format appears to be hardcoded for stereo only | ||
| 31 : | $ThisFileInfo['audio']['lossless'] = false; | ||
| 32 : | |||
| 33 : | fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET); | ||
| 34 : | |||
| 35 : | $thisfile_mpc_header['size'] = 28; | ||
| 36 : | $MPCheaderData = fread($fd, $thisfile_mpc_header['size']); | ||
| 37 : | $offset = 0; | ||
| 38 : | |||
| 39 : | if (substr($MPCheaderData, $offset, 3) == 'MP+') { | ||
| 40 : | |||
| 41 : | // great, this is SV7+ | ||
| 42 : | $thisfile_mpc_header['raw']['preamble'] = substr($MPCheaderData, $offset, 3); // should be 'MP+' | ||
| 43 : | $offset += 3; | ||
| 44 : | |||
| 45 : | } 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))) { | ||
| 46 : | |||
| 47 : | // this is SV4 - SV6, handle seperately | ||
| 48 : | $thisfile_mpc_header['size'] = 8; | ||
| 49 : | |||
| 50 : | // add size of file header to avdataoffset - calc bitrate correctly + MD5 data | ||
| 51 : | $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size']; | ||
| 52 : | |||
| 53 : | // Most of this code adapted from Jurgen Faul's MPEGplus source code - thanks Jurgen! :) | ||
| 54 : | $HeaderDWORD[0] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 0, 4)); | ||
| 55 : | $HeaderDWORD[1] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 4, 4)); | ||
| 56 : | |||
| 57 : | |||
| 58 : | // DDDD DDDD CCCC CCCC BBBB BBBB AAAA AAAA | ||
| 59 : | // aaaa aaaa abcd dddd dddd deee eeff ffff | ||
| 60 : | // | ||
| 61 : | // a = bitrate = anything | ||
| 62 : | // b = IS = anything | ||
| 63 : | // c = MS = anything | ||
| 64 : | // d = streamversion = 0000000004 or 0000000005 or 0000000006 | ||
| 65 : | // e = maxband = anything | ||
| 66 : | // f = blocksize = 000001 for SV5+, anything(?) for SV4 | ||
| 67 : | |||
| 68 : | $thisfile_mpc_header['target_bitrate'] = (($HeaderDWORD[0] & 0xFF800000) >> 23); | ||
| 69 : | $thisfile_mpc_header['intensity_stereo'] = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22); | ||
| 70 : | $thisfile_mpc_header['mid-side_stereo'] = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21); | ||
| 71 : | $thisfile_mpc_header['stream_major_version'] = ($HeaderDWORD[0] & 0x001FF800) >> 11; | ||
| 72 : | $thisfile_mpc_header['stream_minor_version'] = 0; // no sub-version numbers before SV7 | ||
| 73 : | $thisfile_mpc_header['max_band'] = ($HeaderDWORD[0] & 0x000007C0) >> 6; // related to lowpass frequency, not sure how it translates exactly | ||
| 74 : | $thisfile_mpc_header['block_size'] = ($HeaderDWORD[0] & 0x0000003F); | ||
| 75 : | |||
| 76 : | switch ($thisfile_mpc_header['stream_major_version']) { | ||
| 77 : | case 4: | ||
| 78 : | $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16); | ||
| 79 : | break; | ||
| 80 : | |||
| 81 : | case 5: | ||
| 82 : | case 6: | ||
| 83 : | $thisfile_mpc_header['frame_count'] = $HeaderDWORD[1]; | ||
| 84 : | break; | ||
| 85 : | |||
| 86 : | default: | ||
| 87 : | $ThisFileInfo['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_major_version'].' instead'; | ||
| 88 : | unset($ThisFileInfo['mpc']); | ||
| 89 : | return false; | ||
| 90 : | break; | ||
| 91 : | } | ||
| 92 : | |||
| 93 : | if (($thisfile_mpc_header['stream_major_version'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) { | ||
| 94 : | $ThisFileInfo['warning'][] = 'Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size']; | ||
| 95 : | } | ||
| 96 : | |||
| 97 : | $thisfile_mpc_header['sample_rate'] = 44100; // AB: used by all files up to SV7 | ||
| 98 : | $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate']; | ||
| 99 : | $thisfile_mpc_header['samples'] = $thisfile_mpc_header['frame_count'] * 1152 * $ThisFileInfo['audio']['channels']; | ||
| 100 : | |||
| 101 : | if ($thisfile_mpc_header['target_bitrate'] == 0) { | ||
| 102 : | $ThisFileInfo['audio']['bitrate_mode'] = 'vbr'; | ||
| 103 : | } else { | ||
| 104 : | $ThisFileInfo['audio']['bitrate_mode'] = 'cbr'; | ||
| 105 : | } | ||
| 106 : | |||
| 107 : | $ThisFileInfo['mpc']['bitrate'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152; | ||
| 108 : | $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpc']['bitrate']; | ||
| 109 : | $ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version']; | ||
| 110 : | |||
| 111 : | return true; | ||
| 112 : | |||
| 113 : | } else { | ||
| 114 : | |||
| 115 : | $ThisFileInfo['error'][] = 'Expecting "MP+" at offset '.$ThisFileInfo['avdataoffset'].', found "'.substr($MPCheaderData, $offset, 3).'"'; | ||
| 116 : | unset($ThisFileInfo['fileformat']); | ||
| 117 : | unset($ThisFileInfo['mpc']); | ||
| 118 : | return false; | ||
| 119 : | |||
| 120 : | } | ||
| 121 : | |||
| 122 : | // Continue with SV7+ handling | ||
| 123 : | $StreamVersionByte = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1)); | ||
| 124 : | $offset += 1; | ||
| 125 : | $thisfile_mpc_header['stream_major_version'] = ($StreamVersionByte & 0x0F); | ||
| 126 : | $thisfile_mpc_header['stream_minor_version'] = ($StreamVersionByte & 0xF0) >> 4; | ||
| 127 : | $thisfile_mpc_header['frame_count'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4)); | ||
| 128 : | $offset += 4; | ||
| 129 : | |||
| 130 : | switch ($thisfile_mpc_header['stream_major_version']) { | ||
| 131 : | case 7: | ||
| 132 : | //$ThisFileInfo['fileformat'] = 'SV7'; | ||
| 133 : | break; | ||
| 134 : | |||
| 135 : | default: | ||
| 136 : | $ThisFileInfo['error'][] = 'Only Musepack SV7 supported'; | ||
| 137 : | return false; | ||
| 138 : | } | ||
| 139 : | |||
| 140 : | $FlagsDWORD1 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4)); | ||
| 141 : | $offset += 4; | ||
| 142 : | $thisfile_mpc_header['intensity_stereo'] = (bool) (($FlagsDWORD1 & 0x80000000) >> 31); | ||
| 143 : | $thisfile_mpc_header['mid_side_stereo'] = (bool) (($FlagsDWORD1 & 0x40000000) >> 30); | ||
| 144 : | $thisfile_mpc_header['max_subband'] = ($FlagsDWORD1 & 0x3F000000) >> 24; | ||
| 145 : | $thisfile_mpc_header['raw']['profile'] = ($FlagsDWORD1 & 0x00F00000) >> 20; | ||
| 146 : | $thisfile_mpc_header['begin_loud'] = (bool) (($FlagsDWORD1 & 0x00080000) >> 19); | ||
| 147 : | $thisfile_mpc_header['end_loud'] = (bool) (($FlagsDWORD1 & 0x00040000) >> 18); | ||
| 148 : | $thisfile_mpc_header['raw']['sample_rate'] = ($FlagsDWORD1 & 0x00030000) >> 16; | ||
| 149 : | $thisfile_mpc_header['max_level'] = ($FlagsDWORD1 & 0x0000FFFF); | ||
| 150 : | |||
| 151 : | $thisfile_mpc_header['raw']['title_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2)); | ||
| 152 : | $offset += 2; | ||
| 153 : | $thisfile_mpc_header['raw']['title_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true); | ||
| 154 : | $offset += 2; | ||
| 155 : | |||
| 156 : | $thisfile_mpc_header['raw']['album_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2)); | ||
| 157 : | $offset += 2; | ||
| 158 : | $thisfile_mpc_header['raw']['album_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true); | ||
| 159 : | $offset += 2; | ||
| 160 : | |||
| 161 : | $FlagsDWORD2 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4)); | ||
| 162 : | $offset += 4; | ||
| 163 : | $thisfile_mpc_header['true_gapless'] = (bool) (($FlagsDWORD2 & 0x80000000) >> 31); | ||
| 164 : | $thisfile_mpc_header['last_frame_length'] = ($FlagsDWORD2 & 0x7FF00000) >> 20; | ||
| 165 : | |||
| 166 : | |||
| 167 : | $thisfile_mpc_header['raw']['not_sure_what'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 3)); | ||
| 168 : | $offset += 3; | ||
| 169 : | $thisfile_mpc_header['raw']['encoder_version'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1)); | ||
| 170 : | $offset += 1; | ||
| 171 : | |||
| 172 : | $thisfile_mpc_header['profile'] = $this->MPCprofileNameLookup($thisfile_mpc_header['raw']['profile']); | ||
| 173 : | $thisfile_mpc_header['sample_rate'] = $this->MPCfrequencyLookup($thisfile_mpc_header['raw']['sample_rate']); | ||
| 174 : | if ($thisfile_mpc_header['sample_rate'] == 0) { | ||
| 175 : | $ThisFileInfo['error'][] = 'Corrupt MPC file: frequency == zero'; | ||
| 176 : | return false; | ||
| 177 : | } | ||
| 178 : | $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate']; | ||
| 179 : | $thisfile_mpc_header['samples'] = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $ThisFileInfo['audio']['channels']; | ||
| 180 : | |||
| 181 : | $ThisFileInfo['playtime_seconds'] = ($thisfile_mpc_header['samples'] / $ThisFileInfo['audio']['channels']) / $ThisFileInfo['audio']['sample_rate']; | ||
| 182 : | if ($ThisFileInfo['playtime_seconds'] == 0) { | ||
| 183 : | $ThisFileInfo['error'][] = 'Corrupt MPC file: playtime_seconds == zero'; | ||
| 184 : | return false; | ||
| 185 : | } | ||
| 186 : | |||
| 187 : | // add size of file header to avdataoffset - calc bitrate correctly + MD5 data | ||
| 188 : | $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size']; | ||
| 189 : | |||
| 190 : | $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds']; | ||
| 191 : | |||
| 192 : | $thisfile_mpc_header['title_peak'] = $thisfile_mpc_header['raw']['title_peak']; | ||
| 193 : | $thisfile_mpc_header['title_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['title_peak']); | ||
| 194 : | if ($thisfile_mpc_header['raw']['title_gain'] < 0) { | ||
| 195 : | $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100; | ||
| 196 : | } else { | ||
| 197 : | $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100; | ||
| 198 : | } | ||
| 199 : | |||
| 200 : | $thisfile_mpc_header['album_peak'] = $thisfile_mpc_header['raw']['album_peak']; | ||
| 201 : | $thisfile_mpc_header['album_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['album_peak']); | ||
| 202 : | if ($thisfile_mpc_header['raw']['album_gain'] < 0) { | ||
| 203 : | $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100; | ||
| 204 : | } else { | ||
| 205 : | $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;; | ||
| 206 : | } | ||
| 207 : | $thisfile_mpc_header['encoder_version'] = $this->MPCencoderVersionLookup($thisfile_mpc_header['raw']['encoder_version']); | ||
| 208 : | |||
| 209 : | $ThisFileInfo['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db']; | ||
| 210 : | $ThisFileInfo['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db']; | ||
| 211 : | |||
| 212 : | if ($thisfile_mpc_header['title_peak'] > 0) { | ||
| 213 : | $ThisFileInfo['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak']; | ||
| 214 : | } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) { | ||
| 215 : | $ThisFileInfo['replay_gain']['track']['peak'] = getid3_lib::CastAsInt(round($thisfile_mpc_header['max_level'] * 1.18)); // why? I don't know - see mppdec.c | ||
| 216 : | } | ||
| 217 : | if ($thisfile_mpc_header['album_peak'] > 0) { | ||
| 218 : | $ThisFileInfo['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak']; | ||
| 219 : | } | ||
| 220 : | |||
| 221 : | //$ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version'].'.'.$thisfile_mpc_header['stream_minor_version'].', '.$thisfile_mpc_header['encoder_version']; | ||
| 222 : | $ThisFileInfo['audio']['encoder'] = $thisfile_mpc_header['encoder_version']; | ||
| 223 : | $ThisFileInfo['audio']['encoder_options'] = $thisfile_mpc_header['profile']; | ||
| 224 : | |||
| 225 : | return true; | ||
| 226 : | } | ||
| 227 : | |||
| 228 : | function MPCprofileNameLookup($profileid) { | ||
| 229 : | static $MPCprofileNameLookup = array( | ||
| 230 : | 0 => 'no profile', | ||
| 231 : | 1 => 'Experimental', | ||
| 232 : | 2 => 'unused', | ||
| 233 : | 3 => 'unused', | ||
| 234 : | 4 => 'unused', | ||
| 235 : | 5 => 'below Telephone (q = 0.0)', | ||
| 236 : | 6 => 'below Telephone (q = 1.0)', | ||
| 237 : | 7 => 'Telephone (q = 2.0)', | ||
| 238 : | 8 => 'Thumb (q = 3.0)', | ||
| 239 : | 9 => 'Radio (q = 4.0)', | ||
| 240 : | 10 => 'Standard (q = 5.0)', | ||
| 241 : | 11 => 'Extreme (q = 6.0)', | ||
| 242 : | 12 => 'Insane (q = 7.0)', | ||
| 243 : | 13 => 'BrainDead (q = 8.0)', | ||
| 244 : | 14 => 'above BrainDead (q = 9.0)', | ||
| 245 : | 15 => 'above BrainDead (q = 10.0)' | ||
| 246 : | ); | ||
| 247 : | return (isset($MPCprofileNameLookup[$profileid]) ? $MPCprofileNameLookup[$profileid] : 'invalid'); | ||
| 248 : | } | ||
| 249 : | |||
| 250 : | function MPCfrequencyLookup($frequencyid) { | ||
| 251 : | static $MPCfrequencyLookup = array( | ||
| 252 : | 0 => 44100, | ||
| 253 : | 1 => 48000, | ||
| 254 : | 2 => 37800, | ||
| 255 : | 3 => 32000 | ||
| 256 : | ); | ||
| 257 : | return (isset($MPCfrequencyLookup[$frequencyid]) ? $MPCfrequencyLookup[$frequencyid] : 'invalid'); | ||
| 258 : | } | ||
| 259 : | |||
| 260 : | function MPCpeakDBLookup($intvalue) { | ||
| 261 : | if ($intvalue > 0) { | ||
| 262 : | return ((log10($intvalue) / log10(2)) - 15) * 6; | ||
| 263 : | } | ||
| 264 : | return false; | ||
| 265 : | } | ||
| 266 : | |||
| 267 : | function MPCencoderVersionLookup($encoderversion) { | ||
| 268 : | //Encoder version * 100 (106 = 1.06) | ||
| 269 : | //EncoderVersion % 10 == 0 Release (1.0) | ||
| 270 : | //EncoderVersion % 2 == 0 Beta (1.06) | ||
| 271 : | //EncoderVersion % 2 == 1 Alpha (1.05a...z) | ||
| 272 : | |||
| 273 : | if ($encoderversion == 0) { | ||
| 274 : | // very old version, not known exactly which | ||
| 275 : | return 'Buschmann v1.7.0-v1.7.9 or Klemm v0.90-v1.05'; | ||
| 276 : | } | ||
| 277 : | |||
| 278 : | if (($encoderversion % 10) == 0) { | ||
| 279 : | |||
| 280 : | // release version | ||
| 281 : | return number_format($encoderversion / 100, 2); | ||
| 282 : | |||
| 283 : | } elseif (($encoderversion % 2) == 0) { | ||
| 284 : | |||
| 285 : | // beta version | ||
| 286 : | return number_format($encoderversion / 100, 2).' beta'; | ||
| 287 : | |||
| 288 : | } | ||
| 289 : | |||
| 290 : | // alpha version | ||
| 291 : | return number_format($encoderversion / 100, 2).' alpha'; | ||
| 292 : | } | ||
| 293 : | |||
| 294 : | } | ||
| 295 : | |||
| 296 : | |||
| 297 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

