File indexing completed on 2024-05-12 05:58:13

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.la.php                                         //
0012 // module for analyzing LA (LosslessAudio) audio files         //
0013 // dependencies: module.audio.riff.php                         //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
0018 
0019 class getid3_la extends getid3_handler
0020 {
0021 
0022   public function Analyze() {
0023     $info = &$this->getid3->info;
0024 
0025     $offset = 0;
0026     $this->fseek($info['avdataoffset']);
0027     $rawdata = $this->fread($this->getid3->fread_buffer_size());
0028 
0029     switch (substr($rawdata, $offset, 4)) {
0030       case 'LA02':
0031       case 'LA03':
0032       case 'LA04':
0033         $info['fileformat']          = 'la';
0034         $info['audio']['dataformat'] = 'la';
0035         $info['audio']['lossless']   = true;
0036 
0037         $info['la']['version_major'] = (int) substr($rawdata, $offset + 2, 1);
0038         $info['la']['version_minor'] = (int) substr($rawdata, $offset + 3, 1);
0039         $info['la']['version']       = (float) $info['la']['version_major'] + ($info['la']['version_minor'] / 10);
0040         $offset += 4;
0041 
0042         $info['la']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0043         $offset += 4;
0044         if ($info['la']['uncompressed_size'] == 0) {
0045           $info['error'][] = 'Corrupt LA file: uncompressed_size == zero';
0046           return false;
0047         }
0048 
0049         $WAVEchunk = substr($rawdata, $offset, 4);
0050         if ($WAVEchunk !== 'WAVE') {
0051           $info['error'][] = 'Expected "WAVE" ('.getid3_lib::PrintHexBytes('WAVE').') at offset '.$offset.', found "'.$WAVEchunk.'" ('.getid3_lib::PrintHexBytes($WAVEchunk).') instead.';
0052           return false;
0053         }
0054         $offset += 4;
0055 
0056         $info['la']['fmt_size'] = 24;
0057         if ($info['la']['version'] >= 0.3) {
0058 
0059           $info['la']['fmt_size']    = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0060           $info['la']['header_size'] = 49 + $info['la']['fmt_size'] - 24;
0061           $offset += 4;
0062 
0063         } else {
0064 
0065           // version 0.2 didn't support additional data blocks
0066           $info['la']['header_size'] = 41;
0067 
0068         }
0069 
0070         $fmt_chunk = substr($rawdata, $offset, 4);
0071         if ($fmt_chunk !== 'fmt ') {
0072           $info['error'][] = 'Expected "fmt " ('.getid3_lib::PrintHexBytes('fmt ').') at offset '.$offset.', found "'.$fmt_chunk.'" ('.getid3_lib::PrintHexBytes($fmt_chunk).') instead.';
0073           return false;
0074         }
0075         $offset += 4;
0076         $fmt_size = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0077         $offset += 4;
0078 
0079         $info['la']['raw']['format']  = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
0080         $offset += 2;
0081 
0082         $info['la']['channels']       = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
0083         $offset += 2;
0084         if ($info['la']['channels'] == 0) {
0085           $info['error'][] = 'Corrupt LA file: channels == zero';
0086             return false;
0087         }
0088 
0089         $info['la']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0090         $offset += 4;
0091         if ($info['la']['sample_rate'] == 0) {
0092           $info['error'][] = 'Corrupt LA file: sample_rate == zero';
0093             return false;
0094         }
0095 
0096         $info['la']['bytes_per_second']     = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0097         $offset += 4;
0098         $info['la']['bytes_per_sample']     = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
0099         $offset += 2;
0100         $info['la']['bits_per_sample']      = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
0101         $offset += 2;
0102 
0103         $info['la']['samples']              = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0104         $offset += 4;
0105 
0106         $info['la']['raw']['flags']         = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 1));
0107         $offset += 1;
0108         $info['la']['flags']['seekable']             = (bool) ($info['la']['raw']['flags'] & 0x01);
0109         if ($info['la']['version'] >= 0.4) {
0110           $info['la']['flags']['high_compression'] = (bool) ($info['la']['raw']['flags'] & 0x02);
0111         }
0112 
0113         $info['la']['original_crc']         = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0114         $offset += 4;
0115 
0116         // mikeƘbevin*de
0117         // Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16
0118         // in earlier versions. A seekpoint is added every blocksize * seekevery
0119         // samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should
0120         // give the number of bytes used for the seekpoints. Of course, if seeking
0121         // is disabled, there are no seekpoints stored.
0122         if ($info['la']['version'] >= 0.4) {
0123           $info['la']['blocksize'] = 61440;
0124           $info['la']['seekevery'] = 19;
0125         } else {
0126           $info['la']['blocksize'] = 73728;
0127           $info['la']['seekevery'] = 16;
0128         }
0129 
0130         $info['la']['seekpoint_count'] = 0;
0131         if ($info['la']['flags']['seekable']) {
0132           $info['la']['seekpoint_count'] = floor($info['la']['samples'] / ($info['la']['blocksize'] * $info['la']['seekevery']));
0133 
0134           for ($i = 0; $i < $info['la']['seekpoint_count']; $i++) {
0135             $info['la']['seekpoints'][] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0136             $offset += 4;
0137           }
0138         }
0139 
0140         if ($info['la']['version'] >= 0.3) {
0141 
0142           // Following the main header information, the program outputs all of the
0143           // seekpoints. Following these is what I called the 'footer start',
0144           // i.e. the position immediately after the La audio data is finished.
0145           $info['la']['footerstart'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
0146           $offset += 4;
0147 
0148           if ($info['la']['footerstart'] > $info['filesize']) {
0149             $info['warning'][] = 'FooterStart value points to offset '.$info['la']['footerstart'].' which is beyond end-of-file ('.$info['filesize'].')';
0150             $info['la']['footerstart'] = $info['filesize'];
0151           }
0152 
0153         } else {
0154 
0155           // La v0.2 didn't have FooterStart value
0156           $info['la']['footerstart'] = $info['avdataend'];
0157 
0158         }
0159 
0160         if ($info['la']['footerstart'] < $info['avdataend']) {
0161           if ($RIFFtempfilename = tempnam(GETID3_TEMP_DIR, 'id3')) {
0162             if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) {
0163               $RIFFdata = 'WAVE';
0164               if ($info['la']['version'] == 0.2) {
0165                 $RIFFdata .= substr($rawdata, 12, 24);
0166               } else {
0167                 $RIFFdata .= substr($rawdata, 16, 24);
0168               }
0169               if ($info['la']['footerstart'] < $info['avdataend']) {
0170                 $this->fseek($info['la']['footerstart']);
0171                 $RIFFdata .= $this->fread($info['avdataend'] - $info['la']['footerstart']);
0172               }
0173               $RIFFdata = 'RIFF'.getid3_lib::LittleEndian2String(strlen($RIFFdata), 4, false).$RIFFdata;
0174               fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata));
0175               fclose($RIFF_fp);
0176 
0177               $getid3_temp = new getID3();
0178               $getid3_temp->openfile($RIFFtempfilename);
0179               $getid3_riff = new getid3_riff($getid3_temp);
0180               $getid3_riff->Analyze();
0181 
0182               if (empty($getid3_temp->info['error'])) {
0183                 $info['riff'] = $getid3_temp->info['riff'];
0184               } else {
0185                 $info['warning'][] = 'Error parsing RIFF portion of La file: '.implode($getid3_temp->info['error']);
0186               }
0187               unset($getid3_temp, $getid3_riff);
0188             }
0189             unlink($RIFFtempfilename);
0190           }
0191         }
0192 
0193         // $info['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway
0194         $info['avdataend']    = $info['avdataoffset'] + $info['la']['footerstart'];
0195         $info['avdataoffset'] = $info['avdataoffset'] + $offset;
0196 
0197         $info['la']['compression_ratio']    = (float) (($info['avdataend'] - $info['avdataoffset']) / $info['la']['uncompressed_size']);
0198         $info['playtime_seconds']           = (float) ($info['la']['samples'] / $info['la']['sample_rate']) / $info['la']['channels'];
0199         if ($info['playtime_seconds'] == 0) {
0200           $info['error'][] = 'Corrupt LA file: playtime_seconds == zero';
0201           return false;
0202         }
0203 
0204         $info['audio']['bitrate']            = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['playtime_seconds'];
0205         //$info['audio']['codec']              = $info['la']['codec'];
0206         $info['audio']['bits_per_sample']    = $info['la']['bits_per_sample'];
0207         break;
0208 
0209       default:
0210         if (substr($rawdata, $offset, 2) == 'LA') {
0211           $info['error'][] = 'This version of getID3() ['.$this->getid3->version().'] does not support LA version '.substr($rawdata, $offset + 2, 1).'.'.substr($rawdata, $offset + 3, 1).' which this appears to be - check http://getid3.sourceforge.net for updates.';
0212         } else {
0213           $info['error'][] = 'Not a LA (Lossless-Audio) file';
0214         }
0215         return false;
0216         break;
0217     }
0218 
0219     $info['audio']['channels']    = $info['la']['channels'];
0220     $info['audio']['sample_rate'] = (int) $info['la']['sample_rate'];
0221     $info['audio']['encoder']     = 'LA v'.$info['la']['version'];
0222 
0223     return true;
0224   }
0225 
0226 }