File indexing completed on 2024-05-12 17:26:00

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.vqf.php                                        //
0012 // module for analyzing VQF audio files                        //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_vqf extends getid3_handler
0019 {
0020   public function Analyze() {
0021     $info = &$this->getid3->info;
0022 
0023     // based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
0024     // http://jfaul.de/atl  or  http://j-faul.virtualave.net/atl/atl.html
0025 
0026     $info['fileformat']            = 'vqf';
0027     $info['audio']['dataformat']   = 'vqf';
0028     $info['audio']['bitrate_mode'] = 'cbr';
0029     $info['audio']['lossless']     = false;
0030 
0031     // shortcut
0032     $info['vqf']['raw'] = array();
0033     $thisfile_vqf               = &$info['vqf'];
0034     $thisfile_vqf_raw           = &$thisfile_vqf['raw'];
0035 
0036     $this->fseek($info['avdataoffset']);
0037     $VQFheaderData = $this->fread(16);
0038 
0039     $offset = 0;
0040     $thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
0041     $magic = 'TWIN';
0042     if ($thisfile_vqf_raw['header_tag'] != $magic) {
0043       $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"';
0044       unset($info['vqf']);
0045       unset($info['fileformat']);
0046       return false;
0047     }
0048     $offset += 4;
0049     $thisfile_vqf_raw['version'] =                           substr($VQFheaderData, $offset, 8);
0050     $offset += 8;
0051     $thisfile_vqf_raw['size']    = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
0052     $offset += 4;
0053 
0054     while ($this->ftell() < $info['avdataend']) {
0055 
0056       $ChunkBaseOffset = $this->ftell();
0057       $chunkoffset = 0;
0058       $ChunkData = $this->fread(8);
0059       $ChunkName = substr($ChunkData, $chunkoffset, 4);
0060       if ($ChunkName == 'DATA') {
0061         $info['avdataoffset'] = $ChunkBaseOffset;
0062         break;
0063       }
0064       $chunkoffset += 4;
0065       $ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
0066       $chunkoffset += 4;
0067       if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
0068         $info['error'][] = 'Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
0069         break;
0070       }
0071       if ($ChunkSize > 0) {
0072         $ChunkData .= $this->fread($ChunkSize);
0073       }
0074 
0075       switch ($ChunkName) {
0076         case 'COMM':
0077           // shortcut
0078           $thisfile_vqf['COMM'] = array();
0079           $thisfile_vqf_COMM    = &$thisfile_vqf['COMM'];
0080 
0081           $thisfile_vqf_COMM['channel_mode']   = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
0082           $chunkoffset += 4;
0083           $thisfile_vqf_COMM['bitrate']        = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
0084           $chunkoffset += 4;
0085           $thisfile_vqf_COMM['sample_rate']    = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
0086           $chunkoffset += 4;
0087           $thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
0088           $chunkoffset += 4;
0089 
0090           $info['audio']['channels']        = $thisfile_vqf_COMM['channel_mode'] + 1;
0091           $info['audio']['sample_rate']     = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
0092           $info['audio']['bitrate']         = $thisfile_vqf_COMM['bitrate'] * 1000;
0093           $info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
0094 
0095           if ($info['audio']['bitrate'] == 0) {
0096             $info['error'][] = 'Corrupt VQF file: bitrate_audio == zero';
0097             return false;
0098           }
0099           break;
0100 
0101         case 'NAME':
0102         case 'AUTH':
0103         case '(c) ':
0104         case 'FILE':
0105         case 'COMT':
0106         case 'ALBM':
0107           $thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
0108           break;
0109 
0110         case 'DSIZ':
0111           $thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
0112           break;
0113 
0114         default:
0115           $info['warning'][] = 'Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
0116           break;
0117       }
0118     }
0119 
0120     $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
0121 
0122     if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
0123       switch ($thisfile_vqf['DSIZ']) {
0124         case 0:
0125         case 1:
0126           $info['warning'][] = 'Invalid DSIZ value "'.$thisfile_vqf['DSIZ'].'". This is known to happen with VQF files encoded by Ahead Nero, and seems to be its way of saying this is TwinVQF v'.($thisfile_vqf['DSIZ'] + 1).'.0';
0127           $info['audio']['encoder'] = 'Ahead Nero';
0128           break;
0129 
0130         default:
0131           $info['warning'][] = 'Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'));
0132           break;
0133       }
0134     }
0135 
0136     return true;
0137   }
0138 
0139   public function VQFchannelFrequencyLookup($frequencyid) {
0140     static $VQFchannelFrequencyLookup = array(
0141       11 => 11025,
0142       22 => 22050,
0143       44 => 44100
0144     );
0145     return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
0146   }
0147 
0148   public function VQFcommentNiceNameLookup($shortname) {
0149     static $VQFcommentNiceNameLookup = array(
0150       'NAME' => 'title',
0151       'AUTH' => 'artist',
0152       '(c) ' => 'copyright',
0153       'FILE' => 'filename',
0154       'COMT' => 'comment',
0155       'ALBM' => 'album'
0156     );
0157     return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
0158   }
0159 
0160 }