File indexing completed on 2024-05-12 17:25:59

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // module.audio.mpc.php                                        //
0012 // module for analyzing Musepack/MPEG+ Audio files             //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_mpc extends getid3_handler
0019 {
0020 
0021   public function Analyze() {
0022     $info = &$this->getid3->info;
0023 
0024     $info['mpc']['header'] = array();
0025     $thisfile_mpc_header   = &$info['mpc']['header'];
0026 
0027     $info['fileformat']               = 'mpc';
0028     $info['audio']['dataformat']      = 'mpc';
0029     $info['audio']['bitrate_mode']    = 'vbr';
0030     $info['audio']['channels']        = 2;  // up to SV7 the format appears to have been hardcoded for stereo only
0031     $info['audio']['lossless']        = false;
0032 
0033     $this->fseek($info['avdataoffset']);
0034     $MPCheaderData = $this->fread(4);
0035     $info['mpc']['header']['preamble'] = substr($MPCheaderData, 0, 4); // should be 'MPCK' (SV8) or 'MP+' (SV7), otherwise possible stream data (SV4-SV6)
0036     if (preg_match('#^MPCK#', $info['mpc']['header']['preamble'])) {
0037 
0038       // this is SV8
0039       return $this->ParseMPCsv8();
0040 
0041     } elseif (preg_match('#^MP\+#', $info['mpc']['header']['preamble'])) {
0042 
0043       // this is SV7
0044       return $this->ParseMPCsv7();
0045 
0046     } 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', $MPCheaderData)) {
0047 
0048       // this is SV4 - SV6, handle seperately
0049       return $this->ParseMPCsv6();
0050 
0051     } else {
0052 
0053       $info['error'][] = 'Expecting "MP+" or "MPCK" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($MPCheaderData, 0, 4)).'"';
0054       unset($info['fileformat']);
0055       unset($info['mpc']);
0056       return false;
0057 
0058     }
0059     return false;
0060   }
0061 
0062 
0063   public function ParseMPCsv8() {
0064     // this is SV8
0065     // http://trac.musepack.net/trac/wiki/SV8Specification
0066 
0067     $info = &$this->getid3->info;
0068     $thisfile_mpc_header = &$info['mpc']['header'];
0069 
0070     $keyNameSize            = 2;
0071     $maxHandledPacketLength = 9; // specs say: "n*8; 0 < n < 10"
0072 
0073     $offset = $this->ftell();
0074     while ($offset < $info['avdataend']) {
0075       $thisPacket = array();
0076       $thisPacket['offset'] = $offset;
0077       $packet_offset = 0;
0078 
0079       // Size is a variable-size field, could be 1-4 bytes (possibly more?)
0080       // read enough data in and figure out the exact size later
0081       $MPCheaderData = $this->fread($keyNameSize + $maxHandledPacketLength);
0082       $packet_offset += $keyNameSize;
0083       $thisPacket['key']      = substr($MPCheaderData, 0, $keyNameSize);
0084       $thisPacket['key_name'] = $this->MPCsv8PacketName($thisPacket['key']);
0085       if ($thisPacket['key'] == $thisPacket['key_name']) {
0086         $info['error'][] = 'Found unexpected key value "'.$thisPacket['key'].'" at offset '.$thisPacket['offset'];
0087         return false;
0088       }
0089       $packetLength = 0;
0090       $thisPacket['packet_size'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $keyNameSize), $packetLength); // includes keyname and packet_size field
0091       if ($thisPacket['packet_size'] === false) {
0092         $info['error'][] = 'Did not find expected packet length within '.$maxHandledPacketLength.' bytes at offset '.($thisPacket['offset'] + $keyNameSize);
0093         return false;
0094       }
0095       $packet_offset += $packetLength;
0096       $offset += $thisPacket['packet_size'];
0097 
0098       switch ($thisPacket['key']) {
0099         case 'SH': // Stream Header
0100           $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
0101           if ($moreBytesToRead > 0) {
0102             $MPCheaderData .= $this->fread($moreBytesToRead);
0103           }
0104           $thisPacket['crc']               =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 4));
0105           $packet_offset += 4;
0106           $thisPacket['stream_version']    =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
0107           $packet_offset += 1;
0108 
0109           $packetLength = 0;
0110           $thisPacket['sample_count']      = $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
0111           $packet_offset += $packetLength;
0112 
0113           $packetLength = 0;
0114           $thisPacket['beginning_silence'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
0115           $packet_offset += $packetLength;
0116 
0117           $otherUsefulData                 =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
0118           $packet_offset += 2;
0119           $thisPacket['sample_frequency_raw'] =        (($otherUsefulData & 0xE000) >> 13);
0120           $thisPacket['max_bands_used']       =        (($otherUsefulData & 0x1F00) >>  8);
0121           $thisPacket['channels']             =        (($otherUsefulData & 0x00F0) >>  4) + 1;
0122           $thisPacket['ms_used']              = (bool) (($otherUsefulData & 0x0008) >>  3);
0123           $thisPacket['audio_block_frames']   =        (($otherUsefulData & 0x0007) >>  0);
0124           $thisPacket['sample_frequency']     = $this->MPCfrequencyLookup($thisPacket['sample_frequency_raw']);
0125 
0126           $thisfile_mpc_header['mid_side_stereo']      = $thisPacket['ms_used'];
0127           $thisfile_mpc_header['sample_rate']          = $thisPacket['sample_frequency'];
0128           $thisfile_mpc_header['samples']              = $thisPacket['sample_count'];
0129           $thisfile_mpc_header['stream_version_major'] = $thisPacket['stream_version'];
0130 
0131           $info['audio']['channels']    = $thisPacket['channels'];
0132           $info['audio']['sample_rate'] = $thisPacket['sample_frequency'];
0133           $info['playtime_seconds'] = $thisPacket['sample_count'] / $thisPacket['sample_frequency'];
0134           $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
0135           break;
0136 
0137         case 'RG': // Replay Gain
0138           $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
0139           if ($moreBytesToRead > 0) {
0140             $MPCheaderData .= $this->fread($moreBytesToRead);
0141           }
0142           $thisPacket['replaygain_version']     =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
0143           $packet_offset += 1;
0144           $thisPacket['replaygain_title_gain']  =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
0145           $packet_offset += 2;
0146           $thisPacket['replaygain_title_peak']  =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
0147           $packet_offset += 2;
0148           $thisPacket['replaygain_album_gain']  =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
0149           $packet_offset += 2;
0150           $thisPacket['replaygain_album_peak']  =       getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
0151           $packet_offset += 2;
0152 
0153           if ($thisPacket['replaygain_title_gain']) { $info['replay_gain']['title']['gain'] = $thisPacket['replaygain_title_gain']; }
0154           if ($thisPacket['replaygain_title_peak']) { $info['replay_gain']['title']['peak'] = $thisPacket['replaygain_title_peak']; }
0155           if ($thisPacket['replaygain_album_gain']) { $info['replay_gain']['album']['gain'] = $thisPacket['replaygain_album_gain']; }
0156           if ($thisPacket['replaygain_album_peak']) { $info['replay_gain']['album']['peak'] = $thisPacket['replaygain_album_peak']; }
0157           break;
0158 
0159         case 'EI': // Encoder Info
0160           $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
0161           if ($moreBytesToRead > 0) {
0162             $MPCheaderData .= $this->fread($moreBytesToRead);
0163           }
0164           $profile_pns                 = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
0165           $packet_offset += 1;
0166           $quality_int =                   (($profile_pns & 0xF0) >> 4);
0167           $quality_dec =                   (($profile_pns & 0x0E) >> 3);
0168           $thisPacket['quality'] = (float) $quality_int + ($quality_dec / 8);
0169           $thisPacket['pns_tool'] = (bool) (($profile_pns & 0x01) >> 0);
0170           $thisPacket['version_major'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
0171           $packet_offset += 1;
0172           $thisPacket['version_minor'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
0173           $packet_offset += 1;
0174           $thisPacket['version_build'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
0175           $packet_offset += 1;
0176           $thisPacket['version'] = $thisPacket['version_major'].'.'.$thisPacket['version_minor'].'.'.$thisPacket['version_build'];
0177 
0178           $info['audio']['encoder'] = 'MPC v'.$thisPacket['version'].' ('.(($thisPacket['version_minor'] % 2) ? 'unstable' : 'stable').')';
0179           $thisfile_mpc_header['encoder_version'] = $info['audio']['encoder'];
0180           //$thisfile_mpc_header['quality']         = (float) ($thisPacket['quality'] / 1.5875); // values can range from 0.000 to 15.875, mapped to qualities of 0.0 to 10.0
0181           $thisfile_mpc_header['quality']         = (float) ($thisPacket['quality'] - 5); // values can range from 0.000 to 15.875, of which 0..4 are "reserved/experimental", and 5..15 are mapped to qualities of 0.0 to 10.0
0182           break;
0183 
0184         case 'SO': // Seek Table Offset
0185           $packetLength = 0;
0186           $thisPacket['seek_table_offset'] = $thisPacket['offset'] + $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
0187           $packet_offset += $packetLength;
0188           break;
0189 
0190         case 'ST': // Seek Table
0191         case 'SE': // Stream End
0192         case 'AP': // Audio Data
0193           // nothing useful here, just skip this packet
0194           $thisPacket = array();
0195           break;
0196 
0197         default:
0198           $info['error'][] = 'Found unhandled key type "'.$thisPacket['key'].'" at offset '.$thisPacket['offset'];
0199           return false;
0200           break;
0201       }
0202       if (!empty($thisPacket)) {
0203         $info['mpc']['packets'][] = $thisPacket;
0204       }
0205       $this->fseek($offset);
0206     }
0207     $thisfile_mpc_header['size'] = $offset;
0208     return true;
0209   }
0210 
0211   public function ParseMPCsv7() {
0212     // this is SV7
0213     // http://www.uni-jena.de/~pfk/mpp/sv8/header.html
0214 
0215     $info = &$this->getid3->info;
0216     $thisfile_mpc_header = &$info['mpc']['header'];
0217     $offset = 0;
0218 
0219     $thisfile_mpc_header['size'] = 28;
0220     $MPCheaderData  = $info['mpc']['header']['preamble'];
0221     $MPCheaderData .= $this->fread($thisfile_mpc_header['size'] - strlen($info['mpc']['header']['preamble']));
0222     $offset = strlen('MP+');
0223 
0224     $StreamVersionByte                           = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
0225     $offset += 1;
0226     $thisfile_mpc_header['stream_version_major'] = ($StreamVersionByte & 0x0F) >> 0;
0227     $thisfile_mpc_header['stream_version_minor'] = ($StreamVersionByte & 0xF0) >> 4; // should always be 0, subversions no longer exist in SV8
0228     $thisfile_mpc_header['frame_count']          = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
0229     $offset += 4;
0230 
0231     if ($thisfile_mpc_header['stream_version_major'] != 7) {
0232       $info['error'][] = 'Only Musepack SV7 supported (this file claims to be v'.$thisfile_mpc_header['stream_version_major'].')';
0233       return false;
0234     }
0235 
0236     $FlagsDWORD1                                   = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
0237     $offset += 4;
0238     $thisfile_mpc_header['intensity_stereo']       = (bool) (($FlagsDWORD1 & 0x80000000) >> 31);
0239     $thisfile_mpc_header['mid_side_stereo']        = (bool) (($FlagsDWORD1 & 0x40000000) >> 30);
0240     $thisfile_mpc_header['max_subband']            =         ($FlagsDWORD1 & 0x3F000000) >> 24;
0241     $thisfile_mpc_header['raw']['profile']         =         ($FlagsDWORD1 & 0x00F00000) >> 20;
0242     $thisfile_mpc_header['begin_loud']             = (bool) (($FlagsDWORD1 & 0x00080000) >> 19);
0243     $thisfile_mpc_header['end_loud']               = (bool) (($FlagsDWORD1 & 0x00040000) >> 18);
0244     $thisfile_mpc_header['raw']['sample_rate']     =         ($FlagsDWORD1 & 0x00030000) >> 16;
0245     $thisfile_mpc_header['max_level']              =         ($FlagsDWORD1 & 0x0000FFFF);
0246 
0247     $thisfile_mpc_header['raw']['title_peak']      = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
0248     $offset += 2;
0249     $thisfile_mpc_header['raw']['title_gain']      = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
0250     $offset += 2;
0251 
0252     $thisfile_mpc_header['raw']['album_peak']      = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
0253     $offset += 2;
0254     $thisfile_mpc_header['raw']['album_gain']      = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
0255     $offset += 2;
0256 
0257     $FlagsDWORD2                                   = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
0258     $offset += 4;
0259     $thisfile_mpc_header['true_gapless']           = (bool) (($FlagsDWORD2 & 0x80000000) >> 31);
0260     $thisfile_mpc_header['last_frame_length']      =         ($FlagsDWORD2 & 0x7FF00000) >> 20;
0261 
0262 
0263     $thisfile_mpc_header['raw']['not_sure_what']   = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 3));
0264     $offset += 3;
0265     $thisfile_mpc_header['raw']['encoder_version'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
0266     $offset += 1;
0267 
0268     $thisfile_mpc_header['profile']     = $this->MPCprofileNameLookup($thisfile_mpc_header['raw']['profile']);
0269     $thisfile_mpc_header['sample_rate'] = $this->MPCfrequencyLookup($thisfile_mpc_header['raw']['sample_rate']);
0270     if ($thisfile_mpc_header['sample_rate'] == 0) {
0271       $info['error'][] = 'Corrupt MPC file: frequency == zero';
0272       return false;
0273     }
0274     $info['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
0275     $thisfile_mpc_header['samples']       = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $info['audio']['channels'];
0276 
0277     $info['playtime_seconds']     = ($thisfile_mpc_header['samples'] / $info['audio']['channels']) / $info['audio']['sample_rate'];
0278     if ($info['playtime_seconds'] == 0) {
0279       $info['error'][] = 'Corrupt MPC file: playtime_seconds == zero';
0280       return false;
0281     }
0282 
0283     // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
0284     $info['avdataoffset'] += $thisfile_mpc_header['size'];
0285 
0286     $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
0287 
0288     $thisfile_mpc_header['title_peak']        = $thisfile_mpc_header['raw']['title_peak'];
0289     $thisfile_mpc_header['title_peak_db']     = $this->MPCpeakDBLookup($thisfile_mpc_header['title_peak']);
0290     if ($thisfile_mpc_header['raw']['title_gain'] < 0) {
0291       $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100;
0292     } else {
0293       $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100;
0294     }
0295 
0296     $thisfile_mpc_header['album_peak']        = $thisfile_mpc_header['raw']['album_peak'];
0297     $thisfile_mpc_header['album_peak_db']     = $this->MPCpeakDBLookup($thisfile_mpc_header['album_peak']);
0298     if ($thisfile_mpc_header['raw']['album_gain'] < 0) {
0299       $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100;
0300     } else {
0301       $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;;
0302     }
0303     $thisfile_mpc_header['encoder_version']   = $this->MPCencoderVersionLookup($thisfile_mpc_header['raw']['encoder_version']);
0304 
0305     $info['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db'];
0306     $info['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db'];
0307 
0308     if ($thisfile_mpc_header['title_peak'] > 0) {
0309       $info['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak'];
0310     } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) {
0311       $info['replay_gain']['track']['peak'] = getid3_lib::CastAsInt(round($thisfile_mpc_header['max_level'] * 1.18)); // why? I don't know - see mppdec.c
0312     }
0313     if ($thisfile_mpc_header['album_peak'] > 0) {
0314       $info['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak'];
0315     }
0316 
0317     //$info['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_version_major'].'.'.$thisfile_mpc_header['stream_version_minor'].', '.$thisfile_mpc_header['encoder_version'];
0318     $info['audio']['encoder'] = $thisfile_mpc_header['encoder_version'];
0319     $info['audio']['encoder_options'] = $thisfile_mpc_header['profile'];
0320     $thisfile_mpc_header['quality'] = (float) ($thisfile_mpc_header['raw']['profile'] - 5); // values can range from 0 to 15, of which 0..4 are "reserved/experimental", and 5..15 are mapped to qualities of 0.0 to 10.0
0321 
0322     return true;
0323   }
0324 
0325   public function ParseMPCsv6() {
0326     // this is SV4 - SV6
0327 
0328     $info = &$this->getid3->info;
0329     $thisfile_mpc_header = &$info['mpc']['header'];
0330     $offset = 0;
0331 
0332     $thisfile_mpc_header['size'] = 8;
0333     $this->fseek($info['avdataoffset']);
0334     $MPCheaderData = $this->fread($thisfile_mpc_header['size']);
0335 
0336     // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
0337     $info['avdataoffset'] += $thisfile_mpc_header['size'];
0338 
0339     // Most of this code adapted from Jurgen Faul's MPEGplus source code - thanks Jurgen! :)
0340     $HeaderDWORD[0] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 0, 4));
0341     $HeaderDWORD[1] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 4, 4));
0342 
0343 
0344     // DDDD DDDD  CCCC CCCC  BBBB BBBB  AAAA AAAA
0345     // aaaa aaaa  abcd dddd  dddd deee  eeff ffff
0346     //
0347     // a = bitrate       = anything
0348     // b = IS            = anything
0349     // c = MS            = anything
0350     // d = streamversion = 0000000004 or 0000000005 or 0000000006
0351     // e = maxband       = anything
0352     // f = blocksize     = 000001 for SV5+, anything(?) for SV4
0353 
0354     $thisfile_mpc_header['target_bitrate']       =        (($HeaderDWORD[0] & 0xFF800000) >> 23);
0355     $thisfile_mpc_header['intensity_stereo']     = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22);
0356     $thisfile_mpc_header['mid_side_stereo']      = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21);
0357     $thisfile_mpc_header['stream_version_major'] =         ($HeaderDWORD[0] & 0x001FF800) >> 11;
0358     $thisfile_mpc_header['stream_version_minor'] = 0; // no sub-version numbers before SV7
0359     $thisfile_mpc_header['max_band']             =         ($HeaderDWORD[0] & 0x000007C0) >>  6;  // related to lowpass frequency, not sure how it translates exactly
0360     $thisfile_mpc_header['block_size']           =         ($HeaderDWORD[0] & 0x0000003F);
0361 
0362     switch ($thisfile_mpc_header['stream_version_major']) {
0363       case 4:
0364         $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16);
0365         break;
0366 
0367       case 5:
0368       case 6:
0369         $thisfile_mpc_header['frame_count'] =  $HeaderDWORD[1];
0370         break;
0371 
0372       default:
0373         $info['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_version_major'].' instead';
0374         unset($info['mpc']);
0375         return false;
0376         break;
0377     }
0378 
0379     if (($thisfile_mpc_header['stream_version_major'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) {
0380       $info['warning'][] = 'Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size'];
0381     }
0382 
0383     $thisfile_mpc_header['sample_rate']   = 44100; // AB: used by all files up to SV7
0384     $info['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
0385     $thisfile_mpc_header['samples']       = $thisfile_mpc_header['frame_count'] * 1152 * $info['audio']['channels'];
0386 
0387     if ($thisfile_mpc_header['target_bitrate'] == 0) {
0388       $info['audio']['bitrate_mode'] = 'vbr';
0389     } else {
0390       $info['audio']['bitrate_mode'] = 'cbr';
0391     }
0392 
0393     $info['mpc']['bitrate']   = ($info['avdataend'] - $info['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152;
0394     $info['audio']['bitrate'] = $info['mpc']['bitrate'];
0395     $info['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_version_major'];
0396 
0397     return true;
0398   }
0399 
0400 
0401   public function MPCprofileNameLookup($profileid) {
0402     static $MPCprofileNameLookup = array(
0403       0  => 'no profile',
0404       1  => 'Experimental',
0405       2  => 'unused',
0406       3  => 'unused',
0407       4  => 'unused',
0408       5  => 'below Telephone (q = 0.0)',
0409       6  => 'below Telephone (q = 1.0)',
0410       7  => 'Telephone (q = 2.0)',
0411       8  => 'Thumb (q = 3.0)',
0412       9  => 'Radio (q = 4.0)',
0413       10 => 'Standard (q = 5.0)',
0414       11 => 'Extreme (q = 6.0)',
0415       12 => 'Insane (q = 7.0)',
0416       13 => 'BrainDead (q = 8.0)',
0417       14 => 'above BrainDead (q = 9.0)',
0418       15 => 'above BrainDead (q = 10.0)'
0419     );
0420     return (isset($MPCprofileNameLookup[$profileid]) ? $MPCprofileNameLookup[$profileid] : 'invalid');
0421   }
0422 
0423   public function MPCfrequencyLookup($frequencyid) {
0424     static $MPCfrequencyLookup = array(
0425       0 => 44100,
0426       1 => 48000,
0427       2 => 37800,
0428       3 => 32000
0429     );
0430     return (isset($MPCfrequencyLookup[$frequencyid]) ? $MPCfrequencyLookup[$frequencyid] : 'invalid');
0431   }
0432 
0433   public function MPCpeakDBLookup($intvalue) {
0434     if ($intvalue > 0) {
0435       return ((log10($intvalue) / log10(2)) - 15) * 6;
0436     }
0437     return false;
0438   }
0439 
0440   public function MPCencoderVersionLookup($encoderversion) {
0441     //Encoder version * 100  (106 = 1.06)
0442     //EncoderVersion % 10 == 0        Release (1.0)
0443     //EncoderVersion %  2 == 0        Beta (1.06)
0444     //EncoderVersion %  2 == 1        Alpha (1.05a...z)
0445 
0446     if ($encoderversion == 0) {
0447       // very old version, not known exactly which
0448       return 'Buschmann v1.7.0-v1.7.9 or Klemm v0.90-v1.05';
0449     }
0450 
0451     if (($encoderversion % 10) == 0) {
0452 
0453       // release version
0454       return number_format($encoderversion / 100, 2);
0455 
0456     } elseif (($encoderversion % 2) == 0) {
0457 
0458       // beta version
0459       return number_format($encoderversion / 100, 2).' beta';
0460 
0461     }
0462 
0463     // alpha version
0464     return number_format($encoderversion / 100, 2).' alpha';
0465   }
0466 
0467   public function SV8variableLengthInteger($data, &$packetLength, $maxHandledPacketLength=9) {
0468     $packet_size = 0;
0469     for ($packetLength = 1; $packetLength <= $maxHandledPacketLength; $packetLength++) {
0470       // variable-length size field:
0471       //  bits, big-endian
0472       //  0xxx xxxx                                           - value 0 to  2^7-1
0473       //  1xxx xxxx  0xxx xxxx                                - value 0 to 2^14-1
0474       //  1xxx xxxx  1xxx xxxx  0xxx xxxx                     - value 0 to 2^21-1
0475       //  1xxx xxxx  1xxx xxxx  1xxx xxxx  0xxx xxxx          - value 0 to 2^28-1
0476       //  ...
0477       $thisbyte = ord(substr($data, ($packetLength - 1), 1));
0478       // look through bytes until find a byte with MSB==0
0479       $packet_size = ($packet_size << 7);
0480       $packet_size = ($packet_size | ($thisbyte & 0x7F));
0481       if (($thisbyte & 0x80) === 0) {
0482         break;
0483       }
0484       if ($packetLength >= $maxHandledPacketLength) {
0485         return false;
0486       }
0487     }
0488     return $packet_size;
0489   }
0490 
0491   public function MPCsv8PacketName($packetKey) {
0492     static $MPCsv8PacketName = array();
0493     if (empty($MPCsv8PacketName)) {
0494       $MPCsv8PacketName = array(
0495         'AP' => 'Audio Packet',
0496         'CT' => 'Chapter Tag',
0497         'EI' => 'Encoder Info',
0498         'RG' => 'Replay Gain',
0499         'SE' => 'Stream End',
0500         'SH' => 'Stream Header',
0501         'SO' => 'Seek Table Offset',
0502         'ST' => 'Seek Table',
0503       );
0504     }
0505     return (isset($MPCsv8PacketName[$packetKey]) ? $MPCsv8PacketName[$packetKey] : $packetKey);
0506   }
0507 }