module.audio.mp3.php 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  1. <?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.mp3.php //
  11. // module for analyzing MP3 files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. // number of frames to scan to determine if MPEG-audio sequence is valid
  16. // Lower this number to 5-20 for faster scanning
  17. // Increase this number to 50+ for most accurate detection of valid VBR/CBR
  18. // mpeg-audio streams
  19. define('GETID3_MP3_VALID_CHECK_FRAMES', 35);
  20. class getid3_mp3
  21. {
  22. var $allow_bruteforce = false; // forces getID3() to scan the file byte-by-byte and log all the valid audio frame headers - extremely slow, unrecommended, but may provide data from otherwise-unusuable files
  23. function getid3_mp3(&$fd, &$ThisFileInfo) {
  24. if (!$this->getOnlyMPEGaudioInfo($fd, $ThisFileInfo, $ThisFileInfo['avdataoffset'])) {
  25. if ($this->allow_bruteforce) {
  26. $ThisFileInfo['error'][] = 'Rescanning file in BruteForce mode';
  27. $this->getOnlyMPEGaudioInfoBruteForce($fd, $ThisFileInfo);
  28. }
  29. }
  30. if (isset($ThisFileInfo['mpeg']['audio']['bitrate_mode'])) {
  31. $ThisFileInfo['audio']['bitrate_mode'] = strtolower($ThisFileInfo['mpeg']['audio']['bitrate_mode']);
  32. }
  33. if (((isset($ThisFileInfo['id3v2']['headerlength']) && ($ThisFileInfo['avdataoffset'] > $ThisFileInfo['id3v2']['headerlength'])) || (!isset($ThisFileInfo['id3v2']) && ($ThisFileInfo['avdataoffset'] > 0)))) {
  34. $synchoffsetwarning = 'Unknown data before synch ';
  35. if (isset($ThisFileInfo['id3v2']['headerlength'])) {
  36. $synchoffsetwarning .= '(ID3v2 header ends at '.$ThisFileInfo['id3v2']['headerlength'].', then '.($ThisFileInfo['avdataoffset'] - $ThisFileInfo['id3v2']['headerlength']).' bytes garbage, ';
  37. } else {
  38. $synchoffsetwarning .= '(should be at beginning of file, ';
  39. }
  40. $synchoffsetwarning .= 'synch detected at '.$ThisFileInfo['avdataoffset'].')';
  41. if (@$ThisFileInfo['audio']['bitrate_mode'] == 'cbr') {
  42. if (!empty($ThisFileInfo['id3v2']['headerlength']) && (($ThisFileInfo['avdataoffset'] - $ThisFileInfo['id3v2']['headerlength']) == $ThisFileInfo['mpeg']['audio']['framelength'])) {
  43. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.';
  44. $ThisFileInfo['audio']['codec'] = 'LAME';
  45. $CurrentDataLAMEversionString = 'LAME3.';
  46. } elseif (empty($ThisFileInfo['id3v2']['headerlength']) && ($ThisFileInfo['avdataoffset'] == $ThisFileInfo['mpeg']['audio']['framelength'])) {
  47. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.';
  48. $ThisFileInfo['audio']['codec'] = 'LAME';
  49. $CurrentDataLAMEversionString = 'LAME3.';
  50. }
  51. }
  52. $ThisFileInfo['warning'][] = $synchoffsetwarning;
  53. }
  54. if (isset($ThisFileInfo['mpeg']['audio']['LAME'])) {
  55. $ThisFileInfo['audio']['codec'] = 'LAME';
  56. if (!empty($ThisFileInfo['mpeg']['audio']['LAME']['long_version'])) {
  57. $ThisFileInfo['audio']['encoder'] = rtrim($ThisFileInfo['mpeg']['audio']['LAME']['long_version'], "\x00");
  58. } elseif (!empty($ThisFileInfo['mpeg']['audio']['LAME']['short_version'])) {
  59. $ThisFileInfo['audio']['encoder'] = rtrim($ThisFileInfo['mpeg']['audio']['LAME']['short_version'], "\x00");
  60. }
  61. }
  62. $CurrentDataLAMEversionString = (!empty($CurrentDataLAMEversionString) ? $CurrentDataLAMEversionString : @$ThisFileInfo['audio']['encoder']);
  63. if (!empty($CurrentDataLAMEversionString) && (substr($CurrentDataLAMEversionString, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($CurrentDataLAMEversionString, -1))) {
  64. // a version number of LAME that does not end with a number like "LAME3.92"
  65. // or with a closing parenthesis like "LAME3.88 (alpha)"
  66. // or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92)
  67. // not sure what the actual last frame length will be, but will be less than or equal to 1441
  68. $PossiblyLongerLAMEversion_FrameLength = 1441;
  69. // Not sure what version of LAME this is - look in padding of last frame for longer version string
  70. $PossibleLAMEversionStringOffset = $ThisFileInfo['avdataend'] - $PossiblyLongerLAMEversion_FrameLength;
  71. fseek($fd, $PossibleLAMEversionStringOffset);
  72. $PossiblyLongerLAMEversion_Data = fread($fd, $PossiblyLongerLAMEversion_FrameLength);
  73. switch (substr($CurrentDataLAMEversionString, -1)) {
  74. case 'a':
  75. case 'b':
  76. // "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example
  77. // need to trim off "a" to match longer string
  78. $CurrentDataLAMEversionString = substr($CurrentDataLAMEversionString, 0, -1);
  79. break;
  80. }
  81. if (($PossiblyLongerLAMEversion_String = strstr($PossiblyLongerLAMEversion_Data, $CurrentDataLAMEversionString)) !== false) {
  82. if (substr($PossiblyLongerLAMEversion_String, 0, strlen($CurrentDataLAMEversionString)) == $CurrentDataLAMEversionString) {
  83. $PossiblyLongerLAMEversion_NewString = substr($PossiblyLongerLAMEversion_String, 0, strspn($PossiblyLongerLAMEversion_String, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3" "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)"
  84. if (strlen($PossiblyLongerLAMEversion_NewString) > strlen(@$ThisFileInfo['audio']['encoder'])) {
  85. $ThisFileInfo['audio']['encoder'] = $PossiblyLongerLAMEversion_NewString;
  86. }
  87. }
  88. }
  89. }
  90. if (!empty($ThisFileInfo['audio']['encoder'])) {
  91. $ThisFileInfo['audio']['encoder'] = rtrim($ThisFileInfo['audio']['encoder'], "\x00 ");
  92. }
  93. switch (@$ThisFileInfo['mpeg']['audio']['layer']) {
  94. case 1:
  95. case 2:
  96. $ThisFileInfo['audio']['dataformat'] = 'mp'.$ThisFileInfo['mpeg']['audio']['layer'];
  97. break;
  98. }
  99. if (@$ThisFileInfo['fileformat'] == 'mp3') {
  100. switch ($ThisFileInfo['audio']['dataformat']) {
  101. case 'mp1':
  102. case 'mp2':
  103. case 'mp3':
  104. $ThisFileInfo['fileformat'] = $ThisFileInfo['audio']['dataformat'];
  105. break;
  106. default:
  107. $ThisFileInfo['warning'][] = 'Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$ThisFileInfo['audio']['dataformat'].'"';
  108. break;
  109. }
  110. }
  111. if (empty($ThisFileInfo['fileformat'])) {
  112. unset($ThisFileInfo['fileformat']);
  113. unset($ThisFileInfo['audio']['bitrate_mode']);
  114. unset($ThisFileInfo['avdataoffset']);
  115. unset($ThisFileInfo['avdataend']);
  116. return false;
  117. }
  118. $ThisFileInfo['mime_type'] = 'audio/mpeg';
  119. $ThisFileInfo['audio']['lossless'] = false;
  120. // Calculate playtime
  121. if (!isset($ThisFileInfo['playtime_seconds']) && isset($ThisFileInfo['audio']['bitrate']) && ($ThisFileInfo['audio']['bitrate'] > 0)) {
  122. $ThisFileInfo['playtime_seconds'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 / $ThisFileInfo['audio']['bitrate'];
  123. }
  124. $ThisFileInfo['audio']['encoder_options'] = $this->GuessEncoderOptions($ThisFileInfo);
  125. return true;
  126. }
  127. function GuessEncoderOptions(&$ThisFileInfo) {
  128. // shortcuts
  129. if (!empty($ThisFileInfo['mpeg']['audio'])) {
  130. $thisfile_mpeg_audio = &$ThisFileInfo['mpeg']['audio'];
  131. if (!empty($thisfile_mpeg_audio['LAME'])) {
  132. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  133. }
  134. }
  135. $encoder_options = '';
  136. static $NamedPresetBitrates = array(16, 24, 40, 56, 112, 128, 160, 192, 256);
  137. if ((@$thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) {
  138. $encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality'];
  139. } elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $NamedPresetBitrates))) {
  140. $encoder_options = $thisfile_mpeg_audio_lame['preset_used'];
  141. } elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) {
  142. static $KnownEncoderValues = array();
  143. if (empty($KnownEncoderValues)) {
  144. //$KnownEncoderValues[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name';
  145. $KnownEncoderValues[0xFF][58][1][1][3][2][20500] = '--alt-preset insane'; // 3.90, 3.90.1, 3.92
  146. $KnownEncoderValues[0xFF][58][1][1][3][2][20600] = '--alt-preset insane'; // 3.90.2, 3.90.3, 3.91
  147. $KnownEncoderValues[0xFF][57][1][1][3][4][20500] = '--alt-preset insane'; // 3.94, 3.95
  148. $KnownEncoderValues['**'][78][3][2][3][2][19500] = '--alt-preset extreme'; // 3.90, 3.90.1, 3.92
  149. $KnownEncoderValues['**'][78][3][2][3][2][19600] = '--alt-preset extreme'; // 3.90.2, 3.91
  150. $KnownEncoderValues['**'][78][3][1][3][2][19600] = '--alt-preset extreme'; // 3.90.3
  151. $KnownEncoderValues['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme'; // 3.90, 3.90.1, 3.92
  152. $KnownEncoderValues['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme'; // 3.90.2, 3.90.3, 3.91
  153. $KnownEncoderValues['**'][78][3][2][3][4][19000] = '--alt-preset standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  154. $KnownEncoderValues['**'][78][3][1][3][4][19000] = '--alt-preset standard'; // 3.90.3
  155. $KnownEncoderValues['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  156. $KnownEncoderValues['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3
  157. $KnownEncoderValues['**'][88][4][1][3][3][19500] = '--r3mix'; // 3.90, 3.90.1, 3.92
  158. $KnownEncoderValues['**'][88][4][1][3][3][19600] = '--r3mix'; // 3.90.2, 3.90.3, 3.91
  159. $KnownEncoderValues['**'][67][4][1][3][4][18000] = '--r3mix'; // 3.94, 3.95
  160. $KnownEncoderValues['**'][68][3][2][3][4][18000] = '--alt-preset medium'; // 3.90.3
  161. $KnownEncoderValues['**'][68][4][2][3][4][18000] = '--alt-preset fast medium'; // 3.90.3
  162. $KnownEncoderValues[0xFF][99][1][1][1][2][0] = '--preset studio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  163. $KnownEncoderValues[0xFF][58][2][1][3][2][20600] = '--preset studio'; // 3.90.3, 3.93.1
  164. $KnownEncoderValues[0xFF][58][2][1][3][2][20500] = '--preset studio'; // 3.93
  165. $KnownEncoderValues[0xFF][57][2][1][3][4][20500] = '--preset studio'; // 3.94, 3.95
  166. $KnownEncoderValues[0xC0][88][1][1][1][2][0] = '--preset cd'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  167. $KnownEncoderValues[0xC0][58][2][2][3][2][19600] = '--preset cd'; // 3.90.3, 3.93.1
  168. $KnownEncoderValues[0xC0][58][2][2][3][2][19500] = '--preset cd'; // 3.93
  169. $KnownEncoderValues[0xC0][57][2][1][3][4][19500] = '--preset cd'; // 3.94, 3.95
  170. $KnownEncoderValues[0xA0][78][1][1][3][2][18000] = '--preset hifi'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  171. $KnownEncoderValues[0xA0][58][2][2][3][2][18000] = '--preset hifi'; // 3.90.3, 3.93, 3.93.1
  172. $KnownEncoderValues[0xA0][57][2][1][3][4][18000] = '--preset hifi'; // 3.94, 3.95
  173. $KnownEncoderValues[0x80][67][1][1][3][2][18000] = '--preset tape'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  174. $KnownEncoderValues[0x80][67][1][1][3][2][15000] = '--preset radio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  175. $KnownEncoderValues[0x70][67][1][1][3][2][15000] = '--preset fm'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  176. $KnownEncoderValues[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm'; // 3.90.3, 3.93, 3.93.1
  177. $KnownEncoderValues[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm'; // 3.94, 3.95
  178. $KnownEncoderValues[0x38][58][2][2][0][2][10000] = '--preset voice'; // 3.90.3, 3.93, 3.93.1
  179. $KnownEncoderValues[0x38][57][2][1][0][4][15000] = '--preset voice'; // 3.94, 3.95
  180. $KnownEncoderValues[0x38][57][2][1][0][4][16000] = '--preset voice'; // 3.94a14
  181. $KnownEncoderValues[0x28][65][1][1][0][2][7500] = '--preset mw-us'; // 3.90, 3.90.1, 3.92
  182. $KnownEncoderValues[0x28][65][1][1][0][2][7600] = '--preset mw-us'; // 3.90.2, 3.91
  183. $KnownEncoderValues[0x28][58][2][2][0][2][7000] = '--preset mw-us'; // 3.90.3, 3.93, 3.93.1
  184. $KnownEncoderValues[0x28][57][2][1][0][4][10500] = '--preset mw-us'; // 3.94, 3.95
  185. $KnownEncoderValues[0x28][57][2][1][0][4][11200] = '--preset mw-us'; // 3.94a14
  186. $KnownEncoderValues[0x28][57][2][1][0][4][8800] = '--preset mw-us'; // 3.94a15
  187. $KnownEncoderValues[0x18][58][2][2][0][2][4000] = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1
  188. $KnownEncoderValues[0x18][58][2][2][0][2][3900] = '--preset phon+/lw/mw-eu/sw'; // 3.93
  189. $KnownEncoderValues[0x18][57][2][1][0][4][5900] = '--preset phon+/lw/mw-eu/sw'; // 3.94, 3.95
  190. $KnownEncoderValues[0x18][57][2][1][0][4][6200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a14
  191. $KnownEncoderValues[0x18][57][2][1][0][4][3200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a15
  192. $KnownEncoderValues[0x10][58][2][2][0][2][3800] = '--preset phone'; // 3.90.3, 3.93.1
  193. $KnownEncoderValues[0x10][58][2][2][0][2][3700] = '--preset phone'; // 3.93
  194. $KnownEncoderValues[0x10][57][2][1][0][4][5600] = '--preset phone'; // 3.94, 3.95
  195. }
  196. if (isset($KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
  197. $encoder_options = $KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
  198. } elseif (isset($KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
  199. $encoder_options = $KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
  200. } elseif ($ThisFileInfo['audio']['bitrate_mode'] == 'vbr') {
  201. // http://gabriel.mp3-tech.org/mp3infotag.html
  202. // int Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h
  203. $LAME_V_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10);
  204. $LAME_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($LAME_V_value * 10);
  205. $encoder_options = '-V'.$LAME_V_value.' -q'.$LAME_q_value;
  206. } elseif ($ThisFileInfo['audio']['bitrate_mode'] == 'cbr') {
  207. $encoder_options = strtoupper($ThisFileInfo['audio']['bitrate_mode']).ceil($ThisFileInfo['audio']['bitrate'] / 1000);
  208. } else {
  209. $encoder_options = strtoupper($ThisFileInfo['audio']['bitrate_mode']);
  210. }
  211. } elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) {
  212. $encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr'];
  213. } elseif (!empty($ThisFileInfo['audio']['bitrate'])) {
  214. if ($ThisFileInfo['audio']['bitrate_mode'] == 'cbr') {
  215. $encoder_options = strtoupper($ThisFileInfo['audio']['bitrate_mode']).ceil($ThisFileInfo['audio']['bitrate'] / 1000);
  216. } else {
  217. $encoder_options = strtoupper($ThisFileInfo['audio']['bitrate_mode']);
  218. }
  219. }
  220. if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) {
  221. $encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min'];
  222. }
  223. if (@$thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] || @$thisfile_mpeg_audio_lame['encoding_flags']['nogap_next']) {
  224. $encoder_options .= ' --nogap';
  225. }
  226. if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) {
  227. $ExplodedOptions = explode(' ', $encoder_options, 4);
  228. if ($ExplodedOptions[0] == '--r3mix') {
  229. $ExplodedOptions[1] = 'r3mix';
  230. }
  231. switch ($ExplodedOptions[0]) {
  232. case '--preset':
  233. case '--alt-preset':
  234. case '--r3mix':
  235. if ($ExplodedOptions[1] == 'fast') {
  236. $ExplodedOptions[1] .= ' '.$ExplodedOptions[2];
  237. }
  238. switch ($ExplodedOptions[1]) {
  239. case 'portable':
  240. case 'medium':
  241. case 'standard':
  242. case 'extreme':
  243. case 'insane':
  244. case 'fast portable':
  245. case 'fast medium':
  246. case 'fast standard':
  247. case 'fast extreme':
  248. case 'fast insane':
  249. case 'r3mix':
  250. static $ExpectedLowpass = array(
  251. 'insane|20500' => 20500,
  252. 'insane|20600' => 20600, // 3.90.2, 3.90.3, 3.91
  253. 'medium|18000' => 18000,
  254. 'fast medium|18000' => 18000,
  255. 'extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  256. 'extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  257. 'fast extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  258. 'fast extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  259. 'standard|19000' => 19000,
  260. 'fast standard|19000' => 19000,
  261. 'r3mix|19500' => 19500, // 3.90, 3.90.1, 3.92
  262. 'r3mix|19600' => 19600, // 3.90.2, 3.90.3, 3.91
  263. 'r3mix|18000' => 18000, // 3.94, 3.95
  264. );
  265. if (!isset($ExpectedLowpass[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio_lame['lowpass_frequency']]) && ($thisfile_mpeg_audio_lame['lowpass_frequency'] < 22050) && (round($thisfile_mpeg_audio_lame['lowpass_frequency'] / 1000) < round($thisfile_mpeg_audio['sample_rate'] / 2000))) {
  266. $encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency'];
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. break;
  273. }
  274. }
  275. if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) {
  276. if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) {
  277. $encoder_options .= ' --resample 44100';
  278. } elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) {
  279. $encoder_options .= ' --resample 48000';
  280. } elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) {
  281. switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) {
  282. case 0: // <= 32000
  283. // may or may not be same as source frequency - ignore
  284. break;
  285. case 1: // 44100
  286. case 2: // 48000
  287. case 3: // 48000+
  288. $ExplodedOptions = explode(' ', $encoder_options, 4);
  289. switch ($ExplodedOptions[0]) {
  290. case '--preset':
  291. case '--alt-preset':
  292. switch ($ExplodedOptions[1]) {
  293. case 'fast':
  294. case 'portable':
  295. case 'medium':
  296. case 'standard':
  297. case 'extreme':
  298. case 'insane':
  299. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  300. break;
  301. default:
  302. static $ExpectedResampledRate = array(
  303. 'phon+/lw/mw-eu/sw|16000' => 16000,
  304. 'mw-us|24000' => 24000, // 3.95
  305. 'mw-us|32000' => 32000, // 3.93
  306. 'mw-us|16000' => 16000, // 3.92
  307. 'phone|16000' => 16000,
  308. 'phone|11025' => 11025, // 3.94a15
  309. 'radio|32000' => 32000, // 3.94a15
  310. 'fm/radio|32000' => 32000, // 3.92
  311. 'fm|32000' => 32000, // 3.90
  312. 'voice|32000' => 32000);
  313. if (!isset($ExpectedResampledRate[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio['sample_rate']])) {
  314. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  315. }
  316. break;
  317. }
  318. break;
  319. case '--r3mix':
  320. default:
  321. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  322. break;
  323. }
  324. break;
  325. }
  326. }
  327. }
  328. if (empty($encoder_options) && !empty($ThisFileInfo['audio']['bitrate']) && !empty($ThisFileInfo['audio']['bitrate_mode'])) {
  329. //$encoder_options = strtoupper($ThisFileInfo['audio']['bitrate_mode']).ceil($ThisFileInfo['audio']['bitrate'] / 1000);
  330. $encoder_options = strtoupper($ThisFileInfo['audio']['bitrate_mode']);
  331. }
  332. return $encoder_options;
  333. }
  334. function decodeMPEGaudioHeader($fd, $offset, &$ThisFileInfo, $recursivesearch=true, $ScanAsCBR=false, $FastMPEGheaderScan=false) {
  335. static $MPEGaudioVersionLookup;
  336. static $MPEGaudioLayerLookup;
  337. static $MPEGaudioBitrateLookup;
  338. static $MPEGaudioFrequencyLookup;
  339. static $MPEGaudioChannelModeLookup;
  340. static $MPEGaudioModeExtensionLookup;
  341. static $MPEGaudioEmphasisLookup;
  342. if (empty($MPEGaudioVersionLookup)) {
  343. $MPEGaudioVersionLookup = getid3_mp3::MPEGaudioVersionArray();
  344. $MPEGaudioLayerLookup = getid3_mp3::MPEGaudioLayerArray();
  345. $MPEGaudioBitrateLookup = getid3_mp3::MPEGaudioBitrateArray();
  346. $MPEGaudioFrequencyLookup = getid3_mp3::MPEGaudioFrequencyArray();
  347. $MPEGaudioChannelModeLookup = getid3_mp3::MPEGaudioChannelModeArray();
  348. $MPEGaudioModeExtensionLookup = getid3_mp3::MPEGaudioModeExtensionArray();
  349. $MPEGaudioEmphasisLookup = getid3_mp3::MPEGaudioEmphasisArray();
  350. }
  351. if ($offset >= $ThisFileInfo['avdataend']) {
  352. $ThisFileInfo['error'][] = 'end of file encounter looking for MPEG synch';
  353. return false;
  354. }
  355. fseek($fd, $offset, SEEK_SET);
  356. //$headerstring = fread($fd, 1441); // worst-case max length = 32kHz @ 320kbps layer 3 = 1441 bytes/frame
  357. $headerstring = fread($fd, 226); // LAME header at offset 36 + 190 bytes of Xing/LAME data
  358. // MP3 audio frame structure:
  359. // $aa $aa $aa $aa [$bb $bb] $cc...
  360. // where $aa..$aa is the four-byte mpeg-audio header (below)
  361. // $bb $bb is the optional 2-byte CRC
  362. // and $cc... is the audio data
  363. $head4 = substr($headerstring, 0, 4);
  364. static $MPEGaudioHeaderDecodeCache = array();
  365. if (isset($MPEGaudioHeaderDecodeCache[$head4])) {
  366. $MPEGheaderRawArray = $MPEGaudioHeaderDecodeCache[$head4];
  367. } else {
  368. $MPEGheaderRawArray = getid3_mp3::MPEGaudioHeaderDecode($head4);
  369. $MPEGaudioHeaderDecodeCache[$head4] = $MPEGheaderRawArray;
  370. }
  371. static $MPEGaudioHeaderValidCache = array();
  372. // Not in cache
  373. if (!isset($MPEGaudioHeaderValidCache[$head4])) {
  374. //$MPEGaudioHeaderValidCache[$head4] = getid3_mp3::MPEGaudioHeaderValid($MPEGheaderRawArray, false, true); // allow badly-formatted freeformat (from LAME 3.90 - 3.93.1)
  375. $MPEGaudioHeaderValidCache[$head4] = getid3_mp3::MPEGaudioHeaderValid($MPEGheaderRawArray, false, false);
  376. }
  377. // shortcut
  378. if (!isset($ThisFileInfo['mpeg']['audio'])) {
  379. $ThisFileInfo['mpeg']['audio'] = array();
  380. }
  381. $thisfile_mpeg_audio = &$ThisFileInfo['mpeg']['audio'];
  382. if ($MPEGaudioHeaderValidCache[$head4]) {
  383. $thisfile_mpeg_audio['raw'] = $MPEGheaderRawArray;
  384. } else {
  385. $ThisFileInfo['error'][] = 'Invalid MPEG audio header at offset '.$offset;
  386. return false;
  387. }
  388. if (!$FastMPEGheaderScan) {
  389. $thisfile_mpeg_audio['version'] = $MPEGaudioVersionLookup[$thisfile_mpeg_audio['raw']['version']];
  390. $thisfile_mpeg_audio['layer'] = $MPEGaudioLayerLookup[$thisfile_mpeg_audio['raw']['layer']];
  391. $thisfile_mpeg_audio['channelmode'] = $MPEGaudioChannelModeLookup[$thisfile_mpeg_audio['raw']['channelmode']];
  392. $thisfile_mpeg_audio['channels'] = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2);
  393. $thisfile_mpeg_audio['sample_rate'] = $MPEGaudioFrequencyLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']];
  394. $thisfile_mpeg_audio['protection'] = !$thisfile_mpeg_audio['raw']['protection'];
  395. $thisfile_mpeg_audio['private'] = (bool) $thisfile_mpeg_audio['raw']['private'];
  396. $thisfile_mpeg_audio['modeextension'] = $MPEGaudioModeExtensionLookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']];
  397. $thisfile_mpeg_audio['copyright'] = (bool) $thisfile_mpeg_audio['raw']['copyright'];
  398. $thisfile_mpeg_audio['original'] = (bool) $thisfile_mpeg_audio['raw']['original'];
  399. $thisfile_mpeg_audio['emphasis'] = $MPEGaudioEmphasisLookup[$thisfile_mpeg_audio['raw']['emphasis']];
  400. $ThisFileInfo['audio']['channels'] = $thisfile_mpeg_audio['channels'];
  401. $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate'];
  402. if ($thisfile_mpeg_audio['protection']) {
  403. $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($headerstring, 4, 2));
  404. }
  405. }
  406. if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) {
  407. // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0
  408. $ThisFileInfo['warning'][] = 'Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1';
  409. $thisfile_mpeg_audio['raw']['bitrate'] = 0;
  410. }
  411. $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding'];
  412. $thisfile_mpeg_audio['bitrate'] = $MPEGaudioBitrateLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']];
  413. if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $ThisFileInfo['avdataoffset'])) {
  414. // only skip multiple frame check if free-format bitstream found at beginning of file
  415. // otherwise is quite possibly simply corrupted data
  416. $recursivesearch = false;
  417. }
  418. // For Layer 2 there are some combinations of bitrate and mode which are not allowed.
  419. if (!$FastMPEGheaderScan && ($thisfile_mpeg_audio['layer'] == '2')) {
  420. $ThisFileInfo['audio']['dataformat'] = 'mp2';
  421. switch ($thisfile_mpeg_audio['channelmode']) {
  422. case 'mono':
  423. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) {
  424. // these are ok
  425. } else {
  426. $ThisFileInfo['error'][] = $thisfile_mpeg_audio['bitrate'].'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  427. return false;
  428. }
  429. break;
  430. case 'stereo':
  431. case 'joint stereo':
  432. case 'dual channel':
  433. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) {
  434. // these are ok
  435. } else {
  436. $ThisFileInfo['error'][] = intval(round($thisfile_mpeg_audio['bitrate'] / 1000)).'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  437. return false;
  438. }
  439. break;
  440. }
  441. }
  442. if ($ThisFileInfo['audio']['sample_rate'] > 0) {
  443. $thisfile_mpeg_audio['framelength'] = getid3_mp3::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $ThisFileInfo['audio']['sample_rate']);
  444. }
  445. $nextframetestoffset = $offset + 1;
  446. if ($thisfile_mpeg_audio['bitrate'] != 'free') {
  447. $ThisFileInfo['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  448. if (isset($thisfile_mpeg_audio['framelength'])) {
  449. $nextframetestoffset = $offset + $thisfile_mpeg_audio['framelength'];
  450. } else {
  451. $ThisFileInfo['error'][] = 'Frame at offset('.$offset.') is has an invalid frame length.';
  452. return false;
  453. }
  454. }
  455. $ExpectedNumberOfAudioBytes = 0;
  456. ////////////////////////////////////////////////////////////////////////////////////
  457. // Variable-bitrate headers
  458. if (substr($headerstring, 4 + 32, 4) == 'VBRI') {
  459. // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36)
  460. // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html
  461. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  462. $thisfile_mpeg_audio['VBR_method'] = 'Fraunhofer';
  463. $ThisFileInfo['audio']['codec'] = 'Fraunhofer';
  464. $SideInfoData = substr($headerstring, 4 + 2, 32);
  465. $FraunhoferVBROffset = 36;
  466. $thisfile_mpeg_audio['VBR_encoder_version'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 4, 2)); // VbriVersion
  467. $thisfile_mpeg_audio['VBR_encoder_delay'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 6, 2)); // VbriDelay
  468. $thisfile_mpeg_audio['VBR_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 8, 2)); // VbriQuality
  469. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 10, 4)); // VbriStreamBytes
  470. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 14, 4)); // VbriStreamFrames
  471. $thisfile_mpeg_audio['VBR_seek_offsets'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 18, 2)); // VbriTableSize
  472. $thisfile_mpeg_audio['VBR_seek_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 20, 2)); // VbriTableScale
  473. $thisfile_mpeg_audio['VBR_entry_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 22, 2)); // VbriEntryBytes
  474. $thisfile_mpeg_audio['VBR_entry_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 24, 2)); // VbriEntryFrames
  475. $ExpectedNumberOfAudioBytes = $thisfile_mpeg_audio['VBR_bytes'];
  476. $previousbyteoffset = $offset;
  477. for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) {
  478. $Fraunhofer_OffsetN = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset, $thisfile_mpeg_audio['VBR_entry_bytes']));
  479. $FraunhoferVBROffset += $thisfile_mpeg_audio['VBR_entry_bytes'];
  480. $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']);
  481. $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']) + $previousbyteoffset;
  482. $previousbyteoffset += $Fraunhofer_OffsetN;
  483. }
  484. } else {
  485. // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36)
  486. // depending on MPEG layer and number of channels
  487. $VBRidOffset = getid3_mp3::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']);
  488. $SideInfoData = substr($headerstring, 4 + 2, $VBRidOffset - 4);
  489. if ((substr($headerstring, $VBRidOffset, strlen('Xing')) == 'Xing') || (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Info')) {
  490. // 'Xing' is traditional Xing VBR frame
  491. // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.)
  492. // 'Info' *can* legally be used to specify a VBR file as well, however.
  493. // http://www.multiweb.cz/twoinches/MP3inside.htm
  494. //00..03 = "Xing" or "Info"
  495. //04..07 = Flags:
  496. // 0x01 Frames Flag set if value for number of frames in file is stored
  497. // 0x02 Bytes Flag set if value for filesize in bytes is stored
  498. // 0x04 TOC Flag set if values for TOC are stored
  499. // 0x08 VBR Scale Flag set if values for VBR scale is stored
  500. //08..11 Frames: Number of frames in file (including the first Xing/Info one)
  501. //12..15 Bytes: File length in Bytes
  502. //16..115 TOC (Table of Contents):
  503. // Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file.
  504. // Each Byte has a value according this formula:
  505. // (TOC[i] / 256) * fileLenInBytes
  506. // So if song lasts eg. 240 sec. and you want to jump to 60. sec. (and file is 5 000 000 Bytes length) you can use:
  507. // TOC[(60/240)*100] = TOC[25]
  508. // and corresponding Byte in file is then approximately at:
  509. // (TOC[25]/256) * 5000000
  510. //116..119 VBR Scale
  511. // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME
  512. // if (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Xing') {
  513. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  514. $thisfile_mpeg_audio['VBR_method'] = 'Xing';
  515. // } else {
  516. // $ScanAsCBR = true;
  517. // $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  518. // }
  519. $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 4, 4));
  520. $thisfile_mpeg_audio['xing_flags']['frames'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001);
  521. $thisfile_mpeg_audio['xing_flags']['bytes'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002);
  522. $thisfile_mpeg_audio['xing_flags']['toc'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004);
  523. $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008);
  524. if ($thisfile_mpeg_audio['xing_flags']['frames']) {
  525. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 8, 4));
  526. //$thisfile_mpeg_audio['VBR_frames']--; // don't count header Xing/Info frame
  527. }
  528. if ($thisfile_mpeg_audio['xing_flags']['bytes']) {
  529. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 12, 4));
  530. }
  531. //if (($thisfile_mpeg_audio['bitrate'] == 'free') && !empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  532. if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  533. $framelengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'];
  534. if ($thisfile_mpeg_audio['layer'] == '1') {
  535. // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
  536. //$ThisFileInfo['audio']['bitrate'] = ((($framelengthfloat / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
  537. $ThisFileInfo['audio']['bitrate'] = ($framelengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $ThisFileInfo['audio']['channels']) / 12;
  538. } else {
  539. // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
  540. //$ThisFileInfo['audio']['bitrate'] = (($framelengthfloat - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
  541. $ThisFileInfo['audio']['bitrate'] = $framelengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $ThisFileInfo['audio']['channels']) / 144;
  542. }
  543. $thisfile_mpeg_audio['framelength'] = floor($framelengthfloat);
  544. }
  545. if ($thisfile_mpeg_audio['xing_flags']['toc']) {
  546. $LAMEtocData = substr($headerstring, $VBRidOffset + 16, 100);
  547. for ($i = 0; $i < 100; $i++) {
  548. $thisfile_mpeg_audio['toc'][$i] = ord($LAMEtocData{$i});
  549. }
  550. }
  551. if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) {
  552. $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 116, 4));
  553. }
  554. // http://gabriel.mp3-tech.org/mp3infotag.html
  555. if (substr($headerstring, $VBRidOffset + 120, 4) == 'LAME') {
  556. // shortcut
  557. $thisfile_mpeg_audio['LAME'] = array();
  558. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  559. $thisfile_mpeg_audio_lame['long_version'] = substr($headerstring, $VBRidOffset + 120, 20);
  560. $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9);
  561. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') {
  562. // extra 11 chars are not part of version string when LAMEtag present
  563. unset($thisfile_mpeg_audio_lame['long_version']);
  564. // It the LAME tag was only introduced in LAME v3.90
  565. // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933
  566. // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html
  567. // are assuming a 'Xing' identifier offset of 0x24, which is the case for
  568. // MPEG-1 non-mono, but not for other combinations
  569. $LAMEtagOffsetContant = $VBRidOffset - 0x24;
  570. // shortcuts
  571. $thisfile_mpeg_audio_lame['RGAD'] = array('track'=>array(), 'album'=>array());
  572. $thisfile_mpeg_audio_lame_RGAD = &$thisfile_mpeg_audio_lame['RGAD'];
  573. $thisfile_mpeg_audio_lame_RGAD_track = &$thisfile_mpeg_audio_lame_RGAD['track'];
  574. $thisfile_mpeg_audio_lame_RGAD_album = &$thisfile_mpeg_audio_lame_RGAD['album'];
  575. $thisfile_mpeg_audio_lame['raw'] = array();
  576. $thisfile_mpeg_audio_lame_raw = &$thisfile_mpeg_audio_lame['raw'];
  577. // byte $9B VBR Quality
  578. // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications.
  579. // Actually overwrites original Xing bytes
  580. unset($thisfile_mpeg_audio['VBR_scale']);
  581. $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0x9B, 1));
  582. // bytes $9C-$A4 Encoder short VersionString
  583. $thisfile_mpeg_audio_lame['short_version'] = substr($headerstring, $LAMEtagOffsetContant + 0x9C, 9);
  584. // byte $A5 Info Tag revision + VBR method
  585. $LAMEtagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA5, 1));
  586. $thisfile_mpeg_audio_lame['tag_revision'] = ($LAMEtagRevisionVBRmethod & 0xF0) >> 4;
  587. $thisfile_mpeg_audio_lame_raw['vbr_method'] = $LAMEtagRevisionVBRmethod & 0x0F;
  588. $thisfile_mpeg_audio_lame['vbr_method'] = getid3_mp3::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']);
  589. $thisfile_mpeg_audio['bitrate_mode'] = substr($thisfile_mpeg_audio_lame['vbr_method'], 0, 3); // usually either 'cbr' or 'vbr', but truncates 'vbr-old / vbr-rh' to 'vbr'
  590. // byte $A6 Lowpass filter value
  591. $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA6, 1)) * 100;
  592. // bytes $A7-$AE Replay Gain
  593. // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html
  594. // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude"
  595. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') {
  596. // LAME 3.94a16 and later - 9.23 fixed point
  597. // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375
  598. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4))) / 8388608);
  599. } else {
  600. // LAME 3.94a15 and earlier - 32-bit floating point
  601. // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15
  602. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4));
  603. }
  604. if ($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] == 0) {
  605. unset($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  606. } else {
  607. $thisfile_mpeg_audio_lame_RGAD['peak_db'] = getid3_lib::RGADamplitude2dB($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  608. }
  609. $thisfile_mpeg_audio_lame_raw['RGAD_track'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAB, 2));
  610. $thisfile_mpeg_audio_lame_raw['RGAD_album'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAD, 2));
  611. if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) {
  612. $thisfile_mpeg_audio_lame_RGAD_track['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13;
  613. $thisfile_mpeg_audio_lame_RGAD_track['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10;
  614. $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9;
  615. $thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF;
  616. $thisfile_mpeg_audio_lame_RGAD_track['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['name']);
  617. $thisfile_mpeg_audio_lame_RGAD_track['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']);
  618. $thisfile_mpeg_audio_lame_RGAD_track['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit']);
  619. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  620. $ThisFileInfo['replay_gain']['track']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  621. }
  622. $ThisFileInfo['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_RGAD_track['originator'];
  623. $ThisFileInfo['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_track['gain_db'];
  624. } else {
  625. unset($thisfile_mpeg_audio_lame_RGAD['track']);
  626. }
  627. if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) {
  628. $thisfile_mpeg_audio_lame_RGAD_album['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13;
  629. $thisfile_mpeg_audio_lame_RGAD_album['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10;
  630. $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9;
  631. $thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF;
  632. $thisfile_mpeg_audio_lame_RGAD_album['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['name']);
  633. $thisfile_mpeg_audio_lame_RGAD_album['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']);
  634. $thisfile_mpeg_audio_lame_RGAD_album['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit']);
  635. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  636. $ThisFileInfo['replay_gain']['album']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  637. }
  638. $ThisFileInfo['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_RGAD_album['originator'];
  639. $ThisFileInfo['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_album['gain_db'];
  640. } else {
  641. unset($thisfile_mpeg_audio_lame_RGAD['album']);
  642. }
  643. if (empty($thisfile_mpeg_audio_lame_RGAD)) {
  644. unset($thisfile_mpeg_audio_lame['RGAD']);
  645. }
  646. // byte $AF Encoding flags + ATH Type
  647. $EncodingFlagsATHtype = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAF, 1));
  648. $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune'] = (bool) ($EncodingFlagsATHtype & 0x10);
  649. $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($EncodingFlagsATHtype & 0x20);
  650. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'] = (bool) ($EncodingFlagsATHtype & 0x40);
  651. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] = (bool) ($EncodingFlagsATHtype & 0x80);
  652. $thisfile_mpeg_audio_lame['ath_type'] = $EncodingFlagsATHtype & 0x0F;
  653. // byte $B0 if ABR {specified bitrate} else {minimal bitrate}
  654. $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB0, 1));
  655. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR)
  656. $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  657. } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR)
  658. // ignore
  659. } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate
  660. $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  661. }
  662. // bytes $B1-$B3 Encoder delays
  663. $EncoderDelays = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB1, 3));
  664. $thisfile_mpeg_audio_lame['encoder_delay'] = ($EncoderDelays & 0xFFF000) >> 12;
  665. $thisfile_mpeg_audio_lame['end_padding'] = $EncoderDelays & 0x000FFF;
  666. // byte $B4 Misc
  667. $MiscByte = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB4, 1));
  668. $thisfile_mpeg_audio_lame_raw['noise_shaping'] = ($MiscByte & 0x03);
  669. $thisfile_mpeg_audio_lame_raw['stereo_mode'] = ($MiscByte & 0x1C) >> 2;
  670. $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($MiscByte & 0x20) >> 5;
  671. $thisfile_mpeg_audio_lame_raw['source_sample_freq'] = ($MiscByte & 0xC0) >> 6;
  672. $thisfile_mpeg_audio_lame['noise_shaping'] = $thisfile_mpeg_audio_lame_raw['noise_shaping'];
  673. $thisfile_mpeg_audio_lame['stereo_mode'] = getid3_mp3::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']);
  674. $thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality'];
  675. $thisfile_mpeg_audio_lame['source_sample_freq'] = getid3_mp3::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']);
  676. // byte $B5 MP3 Gain
  677. $thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB5, 1), false, true);
  678. $thisfile_mpeg_audio_lame['mp3_gain_db'] = (getid3_lib::RGADamplitude2dB(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain'];
  679. $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6));
  680. // bytes $B6-$B7 Preset and surround info
  681. $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB6, 2));
  682. // Reserved = ($PresetSurroundBytes & 0xC000);
  683. $thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800);
  684. $thisfile_mpeg_audio_lame['surround_info'] = getid3_mp3::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']);
  685. $thisfile_mpeg_audio_lame['preset_used_id'] = ($PresetSurroundBytes & 0x07FF);
  686. $thisfile_mpeg_audio_lame['preset_used'] = getid3_mp3::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame);
  687. if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) {
  688. $ThisFileInfo['warning'][] = 'Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org';
  689. }
  690. if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) {
  691. // this may change if 3.90.4 ever comes out
  692. $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3';
  693. }
  694. // bytes $B8-$BB MusicLength
  695. $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB8, 4));
  696. $ExpectedNumberOfAudioBytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']);
  697. // bytes $BC-$BD MusicCRC
  698. $thisfile_mpeg_audio_lame['music_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBC, 2));
  699. // bytes $BE-$BF CRC-16 of Info Tag
  700. $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBE, 2));
  701. // LAME CBR
  702. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) {
  703. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  704. $thisfile_mpeg_audio['bitrate'] = getid3_mp3::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']);
  705. $ThisFileInfo['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  706. //if (empty($thisfile_mpeg_audio['bitrate']) || (!empty($thisfile_mpeg_audio_lame['bitrate_min']) && ($thisfile_mpeg_audio_lame['bitrate_min'] != 255))) {
  707. // $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio_lame['bitrate_min'];
  708. //}
  709. }
  710. }
  711. }
  712. } else {
  713. // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header)
  714. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  715. if ($recursivesearch) {
  716. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  717. if (getid3_mp3::RecursiveFrameScanning($fd, $ThisFileInfo, $offset, $nextframetestoffset, true)) {
  718. $recursivesearch = false;
  719. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  720. }
  721. if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') {
  722. $ThisFileInfo['warning'][] = 'VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.';
  723. }
  724. }
  725. }
  726. }
  727. if (($ExpectedNumberOfAudioBytes > 0) && ($ExpectedNumberOfAudioBytes != ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']))) {
  728. if ($ExpectedNumberOfAudioBytes > ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'])) {
  729. if (@$ThisFileInfo['fileformat'] == 'riff') {
  730. // ignore, audio data is broken into chunks so will always be data "missing"
  731. } elseif (($ExpectedNumberOfAudioBytes - ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'])) == 1) {
  732. $ThisFileInfo['warning'][] = 'Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)';
  733. } else {
  734. $ThisFileInfo['warning'][] = 'Probable truncated file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, only found '.($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']).' (short by '.($ExpectedNumberOfAudioBytes - ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'])).' bytes)';
  735. }
  736. } else {
  737. if ((($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) - $ExpectedNumberOfAudioBytes) == 1) {
  738. // $prenullbytefileoffset = ftell($fd);
  739. // fseek($fd, $ThisFileInfo['avdataend'], SEEK_SET);
  740. // $PossibleNullByte = fread($fd, 1);
  741. // fseek($fd, $prenullbytefileoffset, SEEK_SET);
  742. // if ($PossibleNullByte === "\x00") {
  743. $ThisFileInfo['avdataend']--;
  744. // $ThisFileInfo['warning'][] = 'Extra null byte at end of MP3 data assumed to be RIFF padding and therefore ignored';
  745. // } else {
  746. // $ThisFileInfo['warning'][] = 'Too much data in file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, found '.($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']).' ('.(($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) - $ExpectedNumberOfAudioBytes).' bytes too many)';
  747. // }
  748. } else {
  749. $ThisFileInfo['warning'][] = 'Too much data in file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, found '.($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']).' ('.(($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) - $ExpectedNumberOfAudioBytes).' bytes too many)';
  750. }
  751. }
  752. }
  753. if (($thisfile_mpeg_audio['bitrate'] == 'free') && empty($ThisFileInfo['audio']['bitrate'])) {
  754. if (($offset == $ThisFileInfo['avdataoffset']) && empty($thisfile_mpeg_audio['VBR_frames'])) {
  755. $framebytelength = getid3_mp3::FreeFormatFrameLength($fd, $offset, $ThisFileInfo, true);
  756. if ($framebytelength > 0) {
  757. $thisfile_mpeg_audio['framelength'] = $framebytelength;
  758. if ($thisfile_mpeg_audio['layer'] == '1') {
  759. // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
  760. $ThisFileInfo['audio']['bitrate'] = ((($framebytelength / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
  761. } else {
  762. // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
  763. $ThisFileInfo['audio']['bitrate'] = (($framebytelength - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
  764. }
  765. } else {
  766. $ThisFileInfo['error'][] = 'Error calculating frame length of free-format MP3 without Xing/LAME header';
  767. }
  768. }
  769. }
  770. if (@$thisfile_mpeg_audio['VBR_frames']) {
  771. switch ($thisfile_mpeg_audio['bitrate_mode']) {
  772. case 'vbr':
  773. case 'abr':
  774. if (($thisfile_mpeg_audio['version'] == '1') && ($thisfile_mpeg_audio['layer'] == 1)) {
  775. $thisfile_mpeg_audio['VBR_bitrate'] = ((@$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($ThisFileInfo['audio']['sample_rate'] / 384);
  776. } elseif ((($thisfile_mpeg_audio['version'] == '2') || ($thisfile_mpeg_audio['version'] == '2.5')) && ($thisfile_mpeg_audio['layer'] == 3)) {
  777. $thisfile_mpeg_audio['VBR_bitrate'] = ((@$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($ThisFileInfo['audio']['sample_rate'] / 576);
  778. } else {
  779. $thisfile_mpeg_audio['VBR_bitrate'] = ((@$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($ThisFileInfo['audio']['sample_rate'] / 1152);
  780. }
  781. if ($thisfile_mpeg_audio['VBR_bitrate'] > 0) {
  782. $ThisFileInfo['audio']['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate'];
  783. $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; // to avoid confusion
  784. }
  785. break;
  786. }
  787. }
  788. // End variable-bitrate headers
  789. ////////////////////////////////////////////////////////////////////////////////////
  790. if ($recursivesearch) {
  791. if (!getid3_mp3::RecursiveFrameScanning($fd, $ThisFileInfo, $offset, $nextframetestoffset, $ScanAsCBR)) {
  792. return false;
  793. }
  794. }
  795. //if (false) {
  796. // // experimental side info parsing section - not returning anything useful yet
  797. //
  798. // $SideInfoBitstream = getid3_lib::BigEndian2Bin($SideInfoData);
  799. // $SideInfoOffset = 0;
  800. //
  801. // if ($thisfile_mpeg_audio['version'] == '1') {
  802. // if ($thisfile_mpeg_audio['channelmode'] == 'mono') {
  803. // // MPEG-1 (mono)
  804. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  805. // $SideInfoOffset += 9;
  806. // $SideInfoOffset += 5;
  807. // } else {
  808. // // MPEG-1 (stereo, joint-stereo, dual-channel)
  809. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  810. // $SideInfoOffset += 9;
  811. // $SideInfoOffset += 3;
  812. // }
  813. // } else { // 2 or 2.5
  814. // if ($thisfile_mpeg_audio['channelmode'] == 'mono') {
  815. // // MPEG-2, MPEG-2.5 (mono)
  816. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 8);
  817. // $SideInfoOffset += 8;
  818. // $SideInfoOffset += 1;
  819. // } else {
  820. // // MPEG-2, MPEG-2.5 (stereo, joint-stereo, dual-channel)
  821. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 8);
  822. // $SideInfoOffset += 8;
  823. // $SideInfoOffset += 2;
  824. // }
  825. // }
  826. //
  827. // if ($thisfile_mpeg_audio['version'] == '1') {
  828. // for ($channel = 0; $channel < $ThisFileInfo['audio']['channels']; $channel++) {
  829. // for ($scfsi_band = 0; $scfsi_band < 4; $scfsi_band++) {
  830. // $thisfile_mpeg_audio['scfsi'][$channel][$scfsi_band] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  831. // $SideInfoOffset += 2;
  832. // }
  833. // }
  834. // }
  835. // for ($granule = 0; $granule < (($thisfile_mpeg_audio['version'] == '1') ? 2 : 1); $granule++) {
  836. // for ($channel = 0; $channel < $ThisFileInfo['audio']['channels']; $channel++) {
  837. // $thisfile_mpeg_audio['part2_3_length'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 12);
  838. // $SideInfoOffset += 12;
  839. // $thisfile_mpeg_audio['big_values'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  840. // $SideInfoOffset += 9;
  841. // $thisfile_mpeg_audio['global_gain'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 8);
  842. // $SideInfoOffset += 8;
  843. // if ($thisfile_mpeg_audio['version'] == '1') {
  844. // $thisfile_mpeg_audio['scalefac_compress'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 4);
  845. // $SideInfoOffset += 4;
  846. // } else {
  847. // $thisfile_mpeg_audio['scalefac_compress'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  848. // $SideInfoOffset += 9;
  849. // }
  850. // $thisfile_mpeg_audio['window_switching_flag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  851. // $SideInfoOffset += 1;
  852. //
  853. // if ($thisfile_mpeg_audio['window_switching_flag'][$granule][$channel] == '1') {
  854. //
  855. // $thisfile_mpeg_audio['block_type'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 2);
  856. // $SideInfoOffset += 2;
  857. // $thisfile_mpeg_audio['mixed_block_flag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  858. // $SideInfoOffset += 1;
  859. //
  860. // for ($region = 0; $region < 2; $region++) {
  861. // $thisfile_mpeg_audio['table_select'][$granule][$channel][$region] = substr($SideInfoBitstream, $SideInfoOffset, 5);
  862. // $SideInfoOffset += 5;
  863. // }
  864. // $thisfile_mpeg_audio['table_select'][$granule][$channel][2] = 0;
  865. //
  866. // for ($window = 0; $window < 3; $window++) {
  867. // $thisfile_mpeg_audio['subblock_gain'][$granule][$channel][$window] = substr($SideInfoBitstream, $SideInfoOffset, 3);
  868. // $SideInfoOffset += 3;
  869. // }
  870. //
  871. // } else {
  872. //
  873. // for ($region = 0; $region < 3; $region++) {
  874. // $thisfile_mpeg_audio['table_select'][$granule][$channel][$region] = substr($SideInfoBitstream, $SideInfoOffset, 5);
  875. // $SideInfoOffset += 5;
  876. // }
  877. //
  878. // $thisfile_mpeg_audio['region0_count'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 4);
  879. // $SideInfoOffset += 4;
  880. // $thisfile_mpeg_audio['region1_count'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 3);
  881. // $SideInfoOffset += 3;
  882. // $thisfile_mpeg_audio['block_type'][$granule][$channel] = 0;
  883. // }
  884. //
  885. // if ($thisfile_mpeg_audio['version'] == '1') {
  886. // $thisfile_mpeg_audio['preflag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  887. // $SideInfoOffset += 1;
  888. // }
  889. // $thisfile_mpeg_audio['scalefac_scale'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  890. // $SideInfoOffset += 1;
  891. // $thisfile_mpeg_audio['count1table_select'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  892. // $SideInfoOffset += 1;
  893. // }
  894. // }
  895. //}
  896. return true;
  897. }
  898. function RecursiveFrameScanning(&$fd, &$ThisFileInfo, &$offset, &$nextframetestoffset, $ScanAsCBR) {
  899. for ($i = 0; $i < GETID3_MP3_VALID_CHECK_FRAMES; $i++) {
  900. // check next GETID3_MP3_VALID_CHECK_FRAMES frames for validity, to make sure we haven't run across a false synch
  901. if (($nextframetestoffset + 4) >= $ThisFileInfo['avdataend']) {
  902. // end of file
  903. return true;
  904. }
  905. $nextframetestarray = array('error'=>'', 'warning'=>'', 'avdataend'=>$ThisFileInfo['avdataend'], 'avdataoffset'=>$ThisFileInfo['avdataoffset']);
  906. if (getid3_mp3::decodeMPEGaudioHeader($fd, $nextframetestoffset, $nextframetestarray, false)) {
  907. if ($ScanAsCBR) {
  908. // force CBR mode, used for trying to pick out invalid audio streams with
  909. // valid(?) VBR headers, or VBR streams with no VBR header
  910. if (!isset($nextframetestarray['mpeg']['audio']['bitrate']) || !isset($ThisFileInfo['mpeg']['audio']['bitrate']) || ($nextframetestarray['mpeg']['audio']['bitrate'] != $ThisFileInfo['mpeg']['audio']['bitrate'])) {
  911. return false;
  912. }
  913. }
  914. // next frame is OK, get ready to check the one after that
  915. if (isset($nextframetestarray['mpeg']['audio']['framelength']) && ($nextframetestarray['mpeg']['audio']['framelength'] > 0)) {
  916. $nextframetestoffset += $nextframetestarray['mpeg']['audio']['framelength'];
  917. } else {
  918. $ThisFileInfo['error'][] = 'Frame at offset ('.$offset.') is has an invalid frame length.';
  919. return false;
  920. }
  921. } else {
  922. // next frame is not valid, note the error and fail, so scanning can contiue for a valid frame sequence
  923. $ThisFileInfo['error'][] = 'Frame at offset ('.$offset.') is valid, but the next one at ('.$nextframetestoffset.') is not.';
  924. return false;
  925. }
  926. }
  927. return true;
  928. }
  929. function FreeFormatFrameLength($fd, $offset, &$ThisFileInfo, $deepscan=false) {
  930. fseek($fd, $offset, SEEK_SET);
  931. $MPEGaudioData = fread($fd, 32768);
  932. $SyncPattern1 = substr($MPEGaudioData, 0, 4);
  933. // may be different pattern due to padding
  934. $SyncPattern2 = $SyncPattern1{0}.$SyncPattern1{1}.chr(ord($SyncPattern1{2}) | 0x02).$SyncPattern1{3};
  935. if ($SyncPattern2 === $SyncPattern1) {
  936. $SyncPattern2 = $SyncPattern1{0}.$SyncPattern1{1}.chr(ord($SyncPattern1{2}) & 0xFD).$SyncPattern1{3};
  937. }
  938. $framelength = false;
  939. $framelength1 = strpos($MPEGaudioData, $SyncPattern1, 4);
  940. $framelength2 = strpos($MPEGaudioData, $SyncPattern2, 4);
  941. if ($framelength1 > 4) {
  942. $framelength = $framelength1;
  943. }
  944. if (($framelength2 > 4) && ($framelength2 < $framelength1)) {
  945. $framelength = $framelength2;
  946. }
  947. if (!$framelength) {
  948. // LAME 3.88 has a different value for modeextension on the first frame vs the rest
  949. $framelength1 = strpos($MPEGaudioData, substr($SyncPattern1, 0, 3), 4);
  950. $framelength2 = strpos($MPEGaudioData, substr($SyncPattern2, 0, 3), 4);
  951. if ($framelength1 > 4) {
  952. $framelength = $framelength1;
  953. }
  954. if (($framelength2 > 4) && ($framelength2 < $framelength1)) {
  955. $framelength = $framelength2;
  956. }
  957. if (!$framelength) {
  958. $ThisFileInfo['error'][] = 'Cannot find next free-format synch pattern ('.getid3_lib::PrintHexBytes($SyncPattern1).' or '.getid3_lib::PrintHexBytes($SyncPattern2).') after offset '.$offset;
  959. return false;
  960. } else {
  961. $ThisFileInfo['warning'][] = 'ModeExtension varies between first frame and other frames (known free-format issue in LAME 3.88)';
  962. $ThisFileInfo['audio']['codec'] = 'LAME';
  963. $ThisFileInfo['audio']['encoder'] = 'LAME3.88';
  964. $SyncPattern1 = substr($SyncPattern1, 0, 3);
  965. $SyncPattern2 = substr($SyncPattern2, 0, 3);
  966. }
  967. }
  968. if ($deepscan) {
  969. $ActualFrameLengthValues = array();
  970. $nextoffset = $offset + $framelength;
  971. while ($nextoffset < ($ThisFileInfo['avdataend'] - 6)) {
  972. fseek($fd, $nextoffset - 1, SEEK_SET);
  973. $NextSyncPattern = fread($fd, 6);
  974. if ((substr($NextSyncPattern, 1, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 1, strlen($SyncPattern2)) == $SyncPattern2)) {
  975. // good - found where expected
  976. $ActualFrameLengthValues[] = $framelength;
  977. } elseif ((substr($NextSyncPattern, 0, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 0, strlen($SyncPattern2)) == $SyncPattern2)) {
  978. // ok - found one byte earlier than expected (last frame wasn't padded, first frame was)
  979. $ActualFrameLengthValues[] = ($framelength - 1);
  980. $nextoffset--;
  981. } elseif ((substr($NextSyncPattern, 2, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 2, strlen($SyncPattern2)) == $SyncPattern2)) {
  982. // ok - found one byte later than expected (last frame was padded, first frame wasn't)
  983. $ActualFrameLengthValues[] = ($framelength + 1);
  984. $nextoffset++;
  985. } else {
  986. $ThisFileInfo['error'][] = 'Did not find expected free-format sync pattern at offset '.$nextoffset;
  987. return false;
  988. }
  989. $nextoffset += $framelength;
  990. }
  991. if (count($ActualFrameLengthValues) > 0) {
  992. $framelength = intval(round(array_sum($ActualFrameLengthValues) / count($ActualFrameLengthValues)));
  993. }
  994. }
  995. return $framelength;
  996. }
  997. function getOnlyMPEGaudioInfoBruteForce($fd, &$ThisFileInfo) {
  998. $MPEGaudioHeaderDecodeCache = array();
  999. $MPEGaudioHeaderValidCache = array();
  1000. $MPEGaudioHeaderLengthCache = array();
  1001. $MPEGaudioVersionLookup = getid3_mp3::MPEGaudioVersionArray();
  1002. $MPEGaudioLayerLookup = getid3_mp3::MPEGaudioLayerArray();
  1003. $MPEGaudioBitrateLookup = getid3_mp3::MPEGaudioBitrateArray();
  1004. $MPEGaudioFrequencyLookup = getid3_mp3::MPEGaudioFrequencyArray();
  1005. $MPEGaudioChannelModeLookup = getid3_mp3::MPEGaudioChannelModeArray();
  1006. $MPEGaudioModeExtensionLookup = getid3_mp3::MPEGaudioModeExtensionArray();
  1007. $MPEGaudioEmphasisLookup = getid3_mp3::MPEGaudioEmphasisArray();
  1008. $LongMPEGversionLookup = array();
  1009. $LongMPEGlayerLookup = array();
  1010. $LongMPEGbitrateLookup = array();
  1011. $LongMPEGpaddingLookup = array();
  1012. $LongMPEGfrequencyLookup = array();
  1013. $Distribution['bitrate'] = array();
  1014. $Distribution['frequency'] = array();
  1015. $Distribution['layer'] = array();
  1016. $Distribution['version'] = array();
  1017. $Distribution['padding'] = array();
  1018. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  1019. $max_frames_scan = 5000;
  1020. $frames_scanned = 0;
  1021. $previousvalidframe = $ThisFileInfo['avdataoffset'];
  1022. while (ftell($fd) < $ThisFileInfo['avdataend']) {
  1023. set_time_limit(30);
  1024. $head4 = fread($fd, 4);
  1025. if (strlen($head4) < 4) {
  1026. break;
  1027. }
  1028. if ($head4{0} != "\xFF") {
  1029. for ($i = 1; $i < 4; $i++) {
  1030. if ($head4{$i} == "\xFF") {
  1031. fseek($fd, $i - 4, SEEK_CUR);
  1032. continue 2;
  1033. }
  1034. }
  1035. continue;
  1036. }
  1037. if (!isset($MPEGaudioHeaderDecodeCache[$head4])) {
  1038. $MPEGaudioHeaderDecodeCache[$head4] = getid3_mp3::MPEGaudioHeaderDecode($head4);
  1039. }
  1040. if (!isset($MPEGaudioHeaderValidCache[$head4])) {
  1041. $MPEGaudioHeaderValidCache[$head4] = getid3_mp3::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$head4], false, false);
  1042. }
  1043. if ($MPEGaudioHeaderValidCache[$head4]) {
  1044. if (!isset($MPEGaudioHeaderLengthCache[$head4])) {
  1045. $LongMPEGversionLookup[$head4] = $MPEGaudioVersionLookup[$MPEGaudioHeaderDecodeCache[$head4]['version']];
  1046. $LongMPEGlayerLookup[$head4] = $MPEGaudioLayerLookup[$MPEGaudioHeaderDecodeCache[$head4]['layer']];
  1047. $LongMPEGbitrateLookup[$head4] = $MPEGaudioBitrateLookup[$LongMPEGversionLookup[$head4]][$LongMPEGlayerLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['bitrate']];
  1048. $LongMPEGpaddingLookup[$head4] = (bool) $MPEGaudioHeaderDecodeCache[$head4]['padding'];
  1049. $LongMPEGfrequencyLookup[$head4] = $MPEGaudioFrequencyLookup[$LongMPEGversionLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['sample_rate']];
  1050. $MPEGaudioHeaderLengthCache[$head4] = getid3_mp3::MPEGaudioFrameLength(
  1051. $LongMPEGbitrateLookup[$head4],
  1052. $LongMPEGversionLookup[$head4],
  1053. $LongMPEGlayerLookup[$head4],
  1054. $LongMPEGpaddingLookup[$head4],
  1055. $LongMPEGfrequencyLookup[$head4]);
  1056. }
  1057. if ($MPEGaudioHeaderLengthCache[$head4] > 4) {
  1058. $WhereWeWere = ftell($fd);
  1059. fseek($fd, $MPEGaudioHeaderLengthCache[$head4] - 4, SEEK_CUR);
  1060. $next4 = fread($fd, 4);
  1061. if ($next4{0} == "\xFF") {
  1062. if (!isset($MPEGaudioHeaderDecodeCache[$next4])) {
  1063. $MPEGaudioHeaderDecodeCache[$next4] = getid3_mp3::MPEGaudioHeaderDecode($next4);
  1064. }
  1065. if (!isset($MPEGaudioHeaderValidCache[$next4])) {
  1066. $MPEGaudioHeaderValidCache[$next4] = getid3_mp3::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$next4], false, false);
  1067. }
  1068. if ($MPEGaudioHeaderValidCache[$next4]) {
  1069. fseek($fd, -4, SEEK_CUR);
  1070. @$Distribution['bitrate'][$LongMPEGbitrateLookup[$head4]]++;
  1071. @$Distribution['layer'][$LongMPEGlayerLookup[$head4]]++;
  1072. @$Distribution['version'][$LongMPEGversionLookup[$head4]]++;
  1073. @$Distribution['padding'][intval($LongMPEGpaddingLookup[$head4])]++;
  1074. @$Distribution['frequency'][$LongMPEGfrequencyLookup[$head4]]++;
  1075. if ($max_frames_scan && (++$frames_scanned >= $max_frames_scan)) {
  1076. $pct_data_scanned = (ftell($fd) - $ThisFileInfo['avdataoffset']) / ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']);
  1077. $ThisFileInfo['warning'][] = 'too many MPEG audio frames to scan, only scanned first '.$max_frames_scan.' frames ('.number_format($pct_data_scanned * 100, 1).'% of file) and extrapolated distribution, playtime and bitrate may be incorrect.';
  1078. foreach ($Distribution as $key1 => $value1) {
  1079. foreach ($value1 as $key2 => $value2) {
  1080. $Distribution[$key1][$key2] = round($value2 / $pct_data_scanned);
  1081. }
  1082. }
  1083. break;
  1084. }
  1085. continue;
  1086. }
  1087. }
  1088. unset($next4);
  1089. fseek($fd, $WhereWeWere - 3, SEEK_SET);
  1090. }
  1091. }
  1092. }
  1093. foreach ($Distribution as $key => $value) {
  1094. ksort($Distribution[$key], SORT_NUMERIC);
  1095. }
  1096. ksort($Distribution['version'], SORT_STRING);
  1097. $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = $Distribution['bitrate'];
  1098. $ThisFileInfo['mpeg']['audio']['frequency_distribution'] = $Distribution['frequency'];
  1099. $ThisFileInfo['mpeg']['audio']['layer_distribution'] = $Distribution['layer'];
  1100. $ThisFileInfo['mpeg']['audio']['version_distribution'] = $Distribution['version'];
  1101. $ThisFileInfo['mpeg']['audio']['padding_distribution'] = $Distribution['padding'];
  1102. if (count($Distribution['version']) > 1) {
  1103. $ThisFileInfo['error'][] = 'Corrupt file - more than one MPEG version detected';
  1104. }
  1105. if (count($Distribution['layer']) > 1) {
  1106. $ThisFileInfo['error'][] = 'Corrupt file - more than one MPEG layer detected';
  1107. }
  1108. if (count($Distribution['frequency']) > 1) {
  1109. $ThisFileInfo['error'][] = 'Corrupt file - more than one MPEG sample rate detected';
  1110. }
  1111. $bittotal = 0;
  1112. foreach ($Distribution['bitrate'] as $bitratevalue => $bitratecount) {
  1113. if ($bitratevalue != 'free') {
  1114. $bittotal += ($bitratevalue * $bitratecount);
  1115. }
  1116. }
  1117. $ThisFileInfo['mpeg']['audio']['frame_count'] = array_sum($Distribution['bitrate']);
  1118. if ($ThisFileInfo['mpeg']['audio']['frame_count'] == 0) {
  1119. $ThisFileInfo['error'][] = 'no MPEG audio frames found';
  1120. return false;
  1121. }
  1122. $ThisFileInfo['mpeg']['audio']['bitrate'] = ($bittotal / $ThisFileInfo['mpeg']['audio']['frame_count']);
  1123. $ThisFileInfo['mpeg']['audio']['bitrate_mode'] = ((count($Distribution['bitrate']) > 0) ? 'vbr' : 'cbr');
  1124. $ThisFileInfo['mpeg']['audio']['sample_rate'] = getid3_lib::array_max($Distribution['frequency'], true);
  1125. $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpeg']['audio']['bitrate'];
  1126. $ThisFileInfo['audio']['bitrate_mode'] = $ThisFileInfo['mpeg']['audio']['bitrate_mode'];
  1127. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['mpeg']['audio']['sample_rate'];
  1128. $ThisFileInfo['audio']['dataformat'] = 'mp'.getid3_lib::array_max($Distribution['layer'], true);
  1129. $ThisFileInfo['fileformat'] = $ThisFileInfo['audio']['dataformat'];
  1130. return true;
  1131. }
  1132. function getOnlyMPEGaudioInfo($fd, &$ThisFileInfo, $avdataoffset, $BitrateHistogram=false) {
  1133. // looks for synch, decodes MPEG audio header
  1134. static $MPEGaudioVersionLookup;
  1135. static $MPEGaudioLayerLookup;
  1136. static $MPEGaudioBitrateLookup;
  1137. if (empty($MPEGaudioVersionLookup)) {
  1138. $MPEGaudioVersionLookup = getid3_mp3::MPEGaudioVersionArray();
  1139. $MPEGaudioLayerLookup = getid3_mp3::MPEGaudioLayerArray();
  1140. $MPEGaudioBitrateLookup = getid3_mp3::MPEGaudioBitrateArray();
  1141. }
  1142. fseek($fd, $avdataoffset, SEEK_SET);
  1143. $sync_seek_buffer_size = min(128 * 1024, $ThisFileInfo['avdataend'] - $avdataoffset);
  1144. if ($sync_seek_buffer_size <= 0) {
  1145. $ThisFileInfo['error'][] = 'Invalid $sync_seek_buffer_size at offset '.$avdataoffset;
  1146. return false;
  1147. }
  1148. $header = fread($fd, $sync_seek_buffer_size);
  1149. $sync_seek_buffer_size = strlen($header);
  1150. $SynchSeekOffset = 0;
  1151. while ($SynchSeekOffset < $sync_seek_buffer_size) {
  1152. if ((($avdataoffset + $SynchSeekOffset) < $ThisFileInfo['avdataend']) && !feof($fd)) {
  1153. if ($SynchSeekOffset > $sync_seek_buffer_size) {
  1154. // if a synch's not found within the first 128k bytes, then give up
  1155. $ThisFileInfo['error'][] = 'Could not find valid MPEG audio synch within the first '.round($sync_seek_buffer_size / 1024).'kB';
  1156. if (isset($ThisFileInfo['audio']['bitrate'])) {
  1157. unset($ThisFileInfo['audio']['bitrate']);
  1158. }
  1159. if (isset($ThisFileInfo['mpeg']['audio'])) {
  1160. unset($ThisFileInfo['mpeg']['audio']);
  1161. }
  1162. if (empty($ThisFileInfo['mpeg'])) {
  1163. unset($ThisFileInfo['mpeg']);
  1164. }
  1165. return false;
  1166. } elseif (feof($fd)) {
  1167. $ThisFileInfo['error'][] = 'Could not find valid MPEG audio synch before end of file';
  1168. if (isset($ThisFileInfo['audio']['bitrate'])) {
  1169. unset($ThisFileInfo['audio']['bitrate']);
  1170. }
  1171. if (isset($ThisFileInfo['mpeg']['audio'])) {
  1172. unset($ThisFileInfo['mpeg']['audio']);
  1173. }
  1174. if (isset($ThisFileInfo['mpeg']) && (!is_array($ThisFileInfo['mpeg']) || (count($ThisFileInfo['mpeg']) == 0))) {
  1175. unset($ThisFileInfo['mpeg']);
  1176. }
  1177. return false;
  1178. }
  1179. }
  1180. if (($SynchSeekOffset + 1) >= strlen($header)) {
  1181. $ThisFileInfo['error'][] = 'Could not find valid MPEG synch before end of file';
  1182. return false;
  1183. }
  1184. if (($header{$SynchSeekOffset} == "\xFF") && ($header{($SynchSeekOffset + 1)} > "\xE0")) { // synch detected
  1185. if (!isset($FirstFrameThisfileInfo) && !isset($ThisFileInfo['mpeg']['audio'])) {
  1186. $FirstFrameThisfileInfo = $ThisFileInfo;
  1187. $FirstFrameAVDataOffset = $avdataoffset + $SynchSeekOffset;
  1188. if (!getid3_mp3::decodeMPEGaudioHeader($fd, $avdataoffset + $SynchSeekOffset, $FirstFrameThisfileInfo, false)) {
  1189. // if this is the first valid MPEG-audio frame, save it in case it's a VBR header frame and there's
  1190. // garbage between this frame and a valid sequence of MPEG-audio frames, to be restored below
  1191. unset($FirstFrameThisfileInfo);
  1192. }
  1193. }
  1194. $dummy = $ThisFileInfo; // only overwrite real data if valid header found
  1195. if (getid3_mp3::decodeMPEGaudioHeader($fd, $avdataoffset + $SynchSeekOffset, $dummy, true)) {
  1196. $ThisFileInfo = $dummy;
  1197. $ThisFileInfo['avdataoffset'] = $avdataoffset + $SynchSeekOffset;
  1198. switch (@$ThisFileInfo['fileformat']) {
  1199. case '':
  1200. case 'id3':
  1201. case 'ape':
  1202. case 'mp3':
  1203. $ThisFileInfo['fileformat'] = 'mp3';
  1204. $ThisFileInfo['audio']['dataformat'] = 'mp3';
  1205. break;
  1206. }
  1207. if (isset($FirstFrameThisfileInfo['mpeg']['audio']['bitrate_mode']) && ($FirstFrameThisfileInfo['mpeg']['audio']['bitrate_mode'] == 'vbr')) {
  1208. if (!(abs($ThisFileInfo['audio']['bitrate'] - $FirstFrameThisfileInfo['audio']['bitrate']) <= 1)) {
  1209. // If there is garbage data between a valid VBR header frame and a sequence
  1210. // of valid MPEG-audio frames the VBR data is no longer discarded.
  1211. $ThisFileInfo = $FirstFrameThisfileInfo;
  1212. $ThisFileInfo['avdataoffset'] = $FirstFrameAVDataOffset;
  1213. $ThisFileInfo['fileformat'] = 'mp3';
  1214. $ThisFileInfo['audio']['dataformat'] = 'mp3';
  1215. $dummy = $ThisFileInfo;
  1216. unset($dummy['mpeg']['audio']);
  1217. $GarbageOffsetStart = $FirstFrameAVDataOffset + $FirstFrameThisfileInfo['mpeg']['audio']['framelength'];
  1218. $GarbageOffsetEnd = $avdataoffset + $SynchSeekOffset;
  1219. if (getid3_mp3::decodeMPEGaudioHeader($fd, $GarbageOffsetEnd, $dummy, true, true)) {
  1220. $ThisFileInfo = $dummy;
  1221. $ThisFileInfo['avdataoffset'] = $GarbageOffsetEnd;
  1222. $ThisFileInfo['warning'][] = 'apparently-valid VBR header not used because could not find '.GETID3_MP3_VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.'), but did find valid CBR stream starting at '.$GarbageOffsetEnd;
  1223. } else {
  1224. $ThisFileInfo['warning'][] = 'using data from VBR header even though could not find '.GETID3_MP3_VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.')';
  1225. }
  1226. }
  1227. }
  1228. if (isset($ThisFileInfo['mpeg']['audio']['bitrate_mode']) && ($ThisFileInfo['mpeg']['audio']['bitrate_mode'] == 'vbr') && !isset($ThisFileInfo['mpeg']['audio']['VBR_method'])) {
  1229. // VBR file with no VBR header
  1230. $BitrateHistogram = true;
  1231. }
  1232. if ($BitrateHistogram) {
  1233. $ThisFileInfo['mpeg']['audio']['stereo_distribution'] = array('stereo'=>0, 'joint stereo'=>0, 'dual channel'=>0, 'mono'=>0);
  1234. $ThisFileInfo['mpeg']['audio']['version_distribution'] = array('1'=>0, '2'=>0, '2.5'=>0);
  1235. if ($ThisFileInfo['mpeg']['audio']['version'] == '1') {
  1236. if ($ThisFileInfo['mpeg']['audio']['layer'] == 3) {
  1237. $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 40000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 320000=>0);
  1238. } elseif ($ThisFileInfo['mpeg']['audio']['layer'] == 2) {
  1239. $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 320000=>0, 384000=>0);
  1240. } elseif ($ThisFileInfo['mpeg']['audio']['layer'] == 1) {
  1241. $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 64000=>0, 96000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 288000=>0, 320000=>0, 352000=>0, 384000=>0, 416000=>0, 448000=>0);
  1242. }
  1243. } elseif ($ThisFileInfo['mpeg']['audio']['layer'] == 1) {
  1244. $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 144000=>0, 160000=>0, 176000=>0, 192000=>0, 224000=>0, 256000=>0);
  1245. } else {
  1246. $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 8000=>0, 16000=>0, 24000=>0, 32000=>0, 40000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 144000=>0, 160000=>0);
  1247. }
  1248. $dummy = array('error'=>$ThisFileInfo['error'], 'warning'=>$ThisFileInfo['warning'], 'avdataend'=>$ThisFileInfo['avdataend'], 'avdataoffset'=>$ThisFileInfo['avdataoffset']);
  1249. $synchstartoffset = $ThisFileInfo['avdataoffset'];
  1250. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  1251. // you can play with these numbers:
  1252. $max_frames_scan = 50000;
  1253. $max_scan_segments = 10;
  1254. // don't play with these numbers:
  1255. $FastMode = false;
  1256. $SynchErrorsFound = 0;
  1257. $frames_scanned = 0;
  1258. $this_scan_segment = 0;
  1259. $frames_scan_per_segment = ceil($max_frames_scan / $max_scan_segments);
  1260. $pct_data_scanned = 0;
  1261. for ($current_segment = 0; $current_segment < $max_scan_segments; $current_segment++) {
  1262. //echo 'was at '.ftell($fd).'<br>';
  1263. $frames_scanned_this_segment = 0;
  1264. if (ftell($fd) >= $ThisFileInfo['avdataend']) {
  1265. //echo 'breaking because current position ('.ftell($fd).') is >= $ThisFileInfo[avdataend] ('.$ThisFileInfo['avdataend'].')<br>';
  1266. break;
  1267. }
  1268. $scan_start_offset[$current_segment] = max(ftell($fd), $ThisFileInfo['avdataoffset'] + round($current_segment * (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) / $max_scan_segments)));
  1269. //echo 'start at '.$scan_start_offset[$current_segment].'<br>';
  1270. if ($current_segment > 0) {
  1271. fseek($fd, $scan_start_offset[$current_segment], SEEK_SET);
  1272. $buffer_4k = fread($fd, 4096);
  1273. for ($j = 0; $j < (strlen($buffer_4k) - 4); $j++) {
  1274. if (($buffer_4k{$j} == "\xFF") && ($buffer_4k{($j + 1)} > "\xE0")) { // synch detected
  1275. if (getid3_mp3::decodeMPEGaudioHeader($fd, $scan_start_offset[$current_segment] + $j, $dummy, false, false, $FastMode)) {
  1276. $calculated_next_offset = $scan_start_offset[$current_segment] + $j + $dummy['mpeg']['audio']['framelength'];
  1277. if (getid3_mp3::decodeMPEGaudioHeader($fd, $calculated_next_offset, $dummy, false, false, $FastMode)) {
  1278. $scan_start_offset[$current_segment] += $j;
  1279. break;
  1280. } else {
  1281. //echo 'header['.__LINE__.'] at '.($calculated_next_offset).' invalid<br>';
  1282. }
  1283. } else {
  1284. //echo 'header['.__LINE__.'] at '.($scan_start_offset[$current_segment] + $j).' invalid<br>';
  1285. }
  1286. }
  1287. }
  1288. }
  1289. //echo 'actually start at '.$scan_start_offset[$current_segment].'<br>';
  1290. $synchstartoffset = $scan_start_offset[$current_segment];
  1291. while (getid3_mp3::decodeMPEGaudioHeader($fd, $synchstartoffset, $dummy, false, false, $FastMode)) {
  1292. $FastMode = true;
  1293. $thisframebitrate = $MPEGaudioBitrateLookup[$MPEGaudioVersionLookup[$dummy['mpeg']['audio']['raw']['version']]][$MPEGaudioLayerLookup[$dummy['mpeg']['audio']['raw']['layer']]][$dummy['mpeg']['audio']['raw']['bitrate']];
  1294. if (empty($dummy['mpeg']['audio']['framelength'])) {
  1295. $SynchErrorsFound++;
  1296. $synchstartoffset++;
  1297. //echo ' [Ø] ';
  1298. } else {
  1299. //echo ' . ';
  1300. @$ThisFileInfo['mpeg']['audio']['bitrate_distribution'][$thisframebitrate]++;
  1301. @$ThisFileInfo['mpeg']['audio']['stereo_distribution'][$dummy['mpeg']['audio']['channelmode']]++;
  1302. @$ThisFileInfo['mpeg']['audio']['version_distribution'][$dummy['mpeg']['audio']['version']]++;
  1303. $synchstartoffset += $dummy['mpeg']['audio']['framelength'];
  1304. }
  1305. $frames_scanned++;
  1306. if ($frames_scan_per_segment && (++$frames_scanned_this_segment >= $frames_scan_per_segment)) {
  1307. $this_pct_scanned = (ftell($fd) - $scan_start_offset[$current_segment]) / ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']);
  1308. if (($current_segment == 0) && (($this_pct_scanned * $max_scan_segments) >= 1)) {
  1309. // file likely contains < $max_frames_scan, just scan as one segment
  1310. $max_scan_segments = 1;
  1311. $frames_scan_per_segment = $max_frames_scan;
  1312. } else {
  1313. $pct_data_scanned += $this_pct_scanned;
  1314. //var_dump($pct_data_scanned);
  1315. //exit;
  1316. break;
  1317. }
  1318. }
  1319. }
  1320. //echo '<hr>';
  1321. }
  1322. if ($pct_data_scanned > 0) {
  1323. $ThisFileInfo['warning'][] = 'too many MPEG audio frames to scan, only scanned '.$frames_scanned.' frames in '.$max_scan_segments.' segments ('.number_format($pct_data_scanned * 100, 1).'% of file) and extrapolated distribution, playtime and bitrate may be incorrect.';
  1324. foreach ($ThisFileInfo['mpeg']['audio'] as $key1 => $value1) {
  1325. if (!eregi('_distribution$', $key1)) {
  1326. continue;
  1327. }
  1328. foreach ($value1 as $key2 => $value2) {
  1329. $ThisFileInfo['mpeg']['audio'][$key1][$key2] = round($value2 / $pct_data_scanned);
  1330. }
  1331. }
  1332. }
  1333. if ($SynchErrorsFound > 0) {
  1334. $ThisFileInfo['warning'][] = 'Found '.$SynchErrorsFound.' synch errors in histogram analysis';
  1335. //return false;
  1336. }
  1337. $bittotal = 0;
  1338. $framecounter = 0;
  1339. foreach ($ThisFileInfo['mpeg']['audio']['bitrate_distribution'] as $bitratevalue => $bitratecount) {
  1340. $framecounter += $bitratecount;
  1341. if ($bitratevalue != 'free') {
  1342. $bittotal += ($bitratevalue * $bitratecount);
  1343. }
  1344. }
  1345. if ($framecounter == 0) {
  1346. $ThisFileInfo['error'][] = 'Corrupt MP3 file: framecounter == zero';
  1347. return false;
  1348. }
  1349. $ThisFileInfo['mpeg']['audio']['frame_count'] = getid3_lib::CastAsInt($framecounter);
  1350. $ThisFileInfo['mpeg']['audio']['bitrate'] = ($bittotal / $framecounter);
  1351. $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpeg']['audio']['bitrate'];
  1352. // Definitively set VBR vs CBR, even if the Xing/LAME/VBRI header says differently
  1353. $distinct_bitrates = 0;
  1354. foreach ($ThisFileInfo['mpeg']['audio']['bitrate_distribution'] as $bitrate_value => $bitrate_count) {
  1355. if ($bitrate_count > 0) {
  1356. $distinct_bitrates++;
  1357. }
  1358. }
  1359. if ($distinct_bitrates > 1) {
  1360. $ThisFileInfo['mpeg']['audio']['bitrate_mode'] = 'vbr';
  1361. } else {
  1362. $ThisFileInfo['mpeg']['audio']['bitrate_mode'] = 'cbr';
  1363. }
  1364. $ThisFileInfo['audio']['bitrate_mode'] = $ThisFileInfo['mpeg']['audio']['bitrate_mode'];
  1365. }
  1366. break; // exit while()
  1367. }
  1368. }
  1369. $SynchSeekOffset++;
  1370. if (($avdataoffset + $SynchSeekOffset) >= $ThisFileInfo['avdataend']) {
  1371. // end of file/data
  1372. if (empty($ThisFileInfo['mpeg']['audio'])) {
  1373. $ThisFileInfo['error'][] = 'could not find valid MPEG synch before end of file';
  1374. if (isset($ThisFileInfo['audio']['bitrate'])) {
  1375. unset($ThisFileInfo['audio']['bitrate']);
  1376. }
  1377. if (isset($ThisFileInfo['mpeg']['audio'])) {
  1378. unset($ThisFileInfo['mpeg']['audio']);
  1379. }
  1380. if (isset($ThisFileInfo['mpeg']) && (!is_array($ThisFileInfo['mpeg']) || empty($ThisFileInfo['mpeg']))) {
  1381. unset($ThisFileInfo['mpeg']);
  1382. }
  1383. return false;
  1384. }
  1385. break;
  1386. }
  1387. }
  1388. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['mpeg']['audio']['channels'];
  1389. $ThisFileInfo['audio']['channelmode'] = $ThisFileInfo['mpeg']['audio']['channelmode'];
  1390. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['mpeg']['audio']['sample_rate'];
  1391. return true;
  1392. }
  1393. function MPEGaudioVersionArray() {
  1394. static $MPEGaudioVersion = array('2.5', false, '2', '1');
  1395. return $MPEGaudioVersion;
  1396. }
  1397. function MPEGaudioLayerArray() {
  1398. static $MPEGaudioLayer = array(false, 3, 2, 1);
  1399. return $MPEGaudioLayer;
  1400. }
  1401. function MPEGaudioBitrateArray() {
  1402. static $MPEGaudioBitrate;
  1403. if (empty($MPEGaudioBitrate)) {
  1404. $MPEGaudioBitrate = array (
  1405. '1' => array (1 => array('free', 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000),
  1406. 2 => array('free', 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000),
  1407. 3 => array('free', 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000)
  1408. ),
  1409. '2' => array (1 => array('free', 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000),
  1410. 2 => array('free', 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000),
  1411. )
  1412. );
  1413. $MPEGaudioBitrate['2'][3] = $MPEGaudioBitrate['2'][2];
  1414. $MPEGaudioBitrate['2.5'] = $MPEGaudioBitrate['2'];
  1415. }
  1416. return $MPEGaudioBitrate;
  1417. }
  1418. function MPEGaudioFrequencyArray() {
  1419. static $MPEGaudioFrequency;
  1420. if (empty($MPEGaudioFrequency)) {
  1421. $MPEGaudioFrequency = array (
  1422. '1' => array(44100, 48000, 32000),
  1423. '2' => array(22050, 24000, 16000),
  1424. '2.5' => array(11025, 12000, 8000)
  1425. );
  1426. }
  1427. return $MPEGaudioFrequency;
  1428. }
  1429. function MPEGaudioChannelModeArray() {
  1430. static $MPEGaudioChannelMode = array('stereo', 'joint stereo', 'dual channel', 'mono');
  1431. return $MPEGaudioChannelMode;
  1432. }
  1433. function MPEGaudioModeExtensionArray() {
  1434. static $MPEGaudioModeExtension;
  1435. if (empty($MPEGaudioModeExtension)) {
  1436. $MPEGaudioModeExtension = array (
  1437. 1 => array('4-31', '8-31', '12-31', '16-31'),
  1438. 2 => array('4-31', '8-31', '12-31', '16-31'),
  1439. 3 => array('', 'IS', 'MS', 'IS+MS')
  1440. );
  1441. }
  1442. return $MPEGaudioModeExtension;
  1443. }
  1444. function MPEGaudioEmphasisArray() {
  1445. static $MPEGaudioEmphasis = array('none', '50/15ms', false, 'CCIT J.17');
  1446. return $MPEGaudioEmphasis;
  1447. }
  1448. function MPEGaudioHeaderBytesValid($head4, $allowBitrate15=false) {
  1449. return getid3_mp3::MPEGaudioHeaderValid(getid3_mp3::MPEGaudioHeaderDecode($head4), false, $allowBitrate15);
  1450. }
  1451. function MPEGaudioHeaderValid($rawarray, $echoerrors=false, $allowBitrate15=false) {
  1452. if (($rawarray['synch'] & 0x0FFE) != 0x0FFE) {
  1453. return false;
  1454. }
  1455. static $MPEGaudioVersionLookup;
  1456. static $MPEGaudioLayerLookup;
  1457. static $MPEGaudioBitrateLookup;
  1458. static $MPEGaudioFrequencyLookup;
  1459. static $MPEGaudioChannelModeLookup;
  1460. static $MPEGaudioModeExtensionLookup;
  1461. static $MPEGaudioEmphasisLookup;
  1462. if (empty($MPEGaudioVersionLookup)) {
  1463. $MPEGaudioVersionLookup = getid3_mp3::MPEGaudioVersionArray();
  1464. $MPEGaudioLayerLookup = getid3_mp3::MPEGaudioLayerArray();
  1465. $MPEGaudioBitrateLookup = getid3_mp3::MPEGaudioBitrateArray();
  1466. $MPEGaudioFrequencyLookup = getid3_mp3::MPEGaudioFrequencyArray();
  1467. $MPEGaudioChannelModeLookup = getid3_mp3::MPEGaudioChannelModeArray();
  1468. $MPEGaudioModeExtensionLookup = getid3_mp3::MPEGaudioModeExtensionArray();
  1469. $MPEGaudioEmphasisLookup = getid3_mp3::MPEGaudioEmphasisArray();
  1470. }
  1471. if (isset($MPEGaudioVersionLookup[$rawarray['version']])) {
  1472. $decodedVersion = $MPEGaudioVersionLookup[$rawarray['version']];
  1473. } else {
  1474. echo ($echoerrors ? "\n".'invalid Version ('.$rawarray['version'].')' : '');
  1475. return false;
  1476. }
  1477. if (isset($MPEGaudioLayerLookup[$rawarray['layer']])) {
  1478. $decodedLayer = $MPEGaudioLayerLookup[$rawarray['layer']];
  1479. } else {
  1480. echo ($echoerrors ? "\n".'invalid Layer ('.$rawarray['layer'].')' : '');
  1481. return false;
  1482. }
  1483. if (!isset($MPEGaudioBitrateLookup[$decodedVersion][$decodedLayer][$rawarray['bitrate']])) {
  1484. echo ($echoerrors ? "\n".'invalid Bitrate ('.$rawarray['bitrate'].')' : '');
  1485. if ($rawarray['bitrate'] == 15) {
  1486. // known issue in LAME 3.90 - 3.93.1 where free-format has bitrate ID of 15 instead of 0
  1487. // let it go through here otherwise file will not be identified
  1488. if (!$allowBitrate15) {
  1489. return false;
  1490. }
  1491. } else {
  1492. return false;
  1493. }
  1494. }
  1495. if (!isset($MPEGaudioFrequencyLookup[$decodedVersion][$rawarray['sample_rate']])) {
  1496. echo ($echoerrors ? "\n".'invalid Frequency ('.$rawarray['sample_rate'].')' : '');
  1497. return false;
  1498. }
  1499. if (!isset($MPEGaudioChannelModeLookup[$rawarray['channelmode']])) {
  1500. echo ($echoerrors ? "\n".'invalid ChannelMode ('.$rawarray['channelmode'].')' : '');
  1501. return false;
  1502. }
  1503. if (!isset($MPEGaudioModeExtensionLookup[$decodedLayer][$rawarray['modeextension']])) {
  1504. echo ($echoerrors ? "\n".'invalid Mode Extension ('.$rawarray['modeextension'].')' : '');
  1505. return false;
  1506. }
  1507. if (!isset($MPEGaudioEmphasisLookup[$rawarray['emphasis']])) {
  1508. echo ($echoerrors ? "\n".'invalid Emphasis ('.$rawarray['emphasis'].')' : '');
  1509. return false;
  1510. }
  1511. // These are just either set or not set, you can't mess that up :)
  1512. // $rawarray['protection'];
  1513. // $rawarray['padding'];
  1514. // $rawarray['private'];
  1515. // $rawarray['copyright'];
  1516. // $rawarray['original'];
  1517. return true;
  1518. }
  1519. function MPEGaudioHeaderDecode($Header4Bytes) {
  1520. // AAAA AAAA AAAB BCCD EEEE FFGH IIJJ KLMM
  1521. // A - Frame sync (all bits set)
  1522. // B - MPEG Audio version ID
  1523. // C - Layer description
  1524. // D - Protection bit
  1525. // E - Bitrate index
  1526. // F - Sampling rate frequency index
  1527. // G - Padding bit
  1528. // H - Private bit
  1529. // I - Channel Mode
  1530. // J - Mode extension (Only if Joint stereo)
  1531. // K - Copyright
  1532. // L - Original
  1533. // M - Emphasis
  1534. if (strlen($Header4Bytes) != 4) {
  1535. return false;
  1536. }
  1537. $MPEGrawHeader['synch'] = (getid3_lib::BigEndian2Int(substr($Header4Bytes, 0, 2)) & 0xFFE0) >> 4;
  1538. $MPEGrawHeader['version'] = (ord($Header4Bytes{1}) & 0x18) >> 3; // BB
  1539. $MPEGrawHeader['layer'] = (ord($Header4Bytes{1}) & 0x06) >> 1; // CC
  1540. $MPEGrawHeader['protection'] = (ord($Header4Bytes{1}) & 0x01); // D
  1541. $MPEGrawHeader['bitrate'] = (ord($Header4Bytes{2}) & 0xF0) >> 4; // EEEE
  1542. $MPEGrawHeader['sample_rate'] = (ord($Header4Bytes{2}) & 0x0C) >> 2; // FF
  1543. $MPEGrawHeader['padding'] = (ord($Header4Bytes{2}) & 0x02) >> 1; // G
  1544. $MPEGrawHeader['private'] = (ord($Header4Bytes{2}) & 0x01); // H
  1545. $MPEGrawHeader['channelmode'] = (ord($Header4Bytes{3}) & 0xC0) >> 6; // II
  1546. $MPEGrawHeader['modeextension'] = (ord($Header4Bytes{3}) & 0x30) >> 4; // JJ
  1547. $MPEGrawHeader['copyright'] = (ord($Header4Bytes{3}) & 0x08) >> 3; // K
  1548. $MPEGrawHeader['original'] = (ord($Header4Bytes{3}) & 0x04) >> 2; // L
  1549. $MPEGrawHeader['emphasis'] = (ord($Header4Bytes{3}) & 0x03); // MM
  1550. return $MPEGrawHeader;
  1551. }
  1552. function MPEGaudioFrameLength(&$bitrate, &$version, &$layer, $padding, &$samplerate) {
  1553. static $AudioFrameLengthCache = array();
  1554. if (!isset($AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate])) {
  1555. $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate] = false;
  1556. if ($bitrate != 'free') {
  1557. if ($version == '1') {
  1558. if ($layer == '1') {
  1559. // For Layer I slot is 32 bits long
  1560. $FrameLengthCoefficient = 48;
  1561. $SlotLength = 4;
  1562. } else { // Layer 2 / 3
  1563. // for Layer 2 and Layer 3 slot is 8 bits long.
  1564. $FrameLengthCoefficient = 144;
  1565. $SlotLength = 1;
  1566. }
  1567. } else { // MPEG-2 / MPEG-2.5
  1568. if ($layer == '1') {
  1569. // For Layer I slot is 32 bits long
  1570. $FrameLengthCoefficient = 24;
  1571. $SlotLength = 4;
  1572. } elseif ($layer == '2') {
  1573. // for Layer 2 and Layer 3 slot is 8 bits long.
  1574. $FrameLengthCoefficient = 144;
  1575. $SlotLength = 1;
  1576. } else { // layer 3
  1577. // for Layer 2 and Layer 3 slot is 8 bits long.
  1578. $FrameLengthCoefficient = 72;
  1579. $SlotLength = 1;
  1580. }
  1581. }
  1582. // FrameLengthInBytes = ((Coefficient * BitRate) / SampleRate) + Padding
  1583. if ($samplerate > 0) {
  1584. $NewFramelength = ($FrameLengthCoefficient * $bitrate) / $samplerate;
  1585. $NewFramelength = floor($NewFramelength / $SlotLength) * $SlotLength; // round to next-lower multiple of SlotLength (1 byte for Layer 2/3, 4 bytes for Layer I)
  1586. if ($padding) {
  1587. $NewFramelength += $SlotLength;
  1588. }
  1589. $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate] = (int) $NewFramelength;
  1590. }
  1591. }
  1592. }
  1593. return $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate];
  1594. }
  1595. function ClosestStandardMP3Bitrate($bitrate) {
  1596. static $StandardBitrates = array(320000, 256000, 224000, 192000, 160000, 128000, 112000, 96000, 80000, 64000, 56000, 48000, 40000, 32000, 24000, 16000, 8000);
  1597. static $BitrateTable = array(0=>'-');
  1598. $roundbitrate = intval(round($bitrate, -3));
  1599. if (!isset($BitrateTable[$roundbitrate])) {
  1600. if ($roundbitrate > 320000) {
  1601. $BitrateTable[$roundbitrate] = round($bitrate, -4);
  1602. } else {
  1603. $LastBitrate = 320000;
  1604. foreach ($StandardBitrates as $StandardBitrate) {
  1605. $BitrateTable[$roundbitrate] = $StandardBitrate;
  1606. if ($roundbitrate >= $StandardBitrate - (($LastBitrate - $StandardBitrate) / 2)) {
  1607. break;
  1608. }
  1609. $LastBitrate = $StandardBitrate;
  1610. }
  1611. }
  1612. }
  1613. return $BitrateTable[$roundbitrate];
  1614. }
  1615. function XingVBRidOffset($version, $channelmode) {
  1616. static $XingVBRidOffsetCache = array();
  1617. if (empty($XingVBRidOffset)) {
  1618. $XingVBRidOffset = array (
  1619. '1' => array ('mono' => 0x15, // 4 + 17 = 21
  1620. 'stereo' => 0x24, // 4 + 32 = 36
  1621. 'joint stereo' => 0x24,
  1622. 'dual channel' => 0x24
  1623. ),
  1624. '2' => array ('mono' => 0x0D, // 4 + 9 = 13
  1625. 'stereo' => 0x15, // 4 + 17 = 21
  1626. 'joint stereo' => 0x15,
  1627. 'dual channel' => 0x15
  1628. ),
  1629. '2.5' => array ('mono' => 0x15,
  1630. 'stereo' => 0x15,
  1631. 'joint stereo' => 0x15,
  1632. 'dual channel' => 0x15
  1633. )
  1634. );
  1635. }
  1636. return $XingVBRidOffset[$version][$channelmode];
  1637. }
  1638. function LAMEvbrMethodLookup($VBRmethodID) {
  1639. static $LAMEvbrMethodLookup = array(
  1640. 0x00 => 'unknown',
  1641. 0x01 => 'cbr',
  1642. 0x02 => 'abr',
  1643. 0x03 => 'vbr-old / vbr-rh',
  1644. 0x04 => 'vbr-new / vbr-mtrh',
  1645. 0x05 => 'vbr-mt',
  1646. 0x06 => 'vbr (full vbr method 4)',
  1647. 0x08 => 'cbr (constant bitrate 2 pass)',
  1648. 0x09 => 'abr (2 pass)',
  1649. 0x0F => 'reserved'
  1650. );
  1651. return (isset($LAMEvbrMethodLookup[$VBRmethodID]) ? $LAMEvbrMethodLookup[$VBRmethodID] : '');
  1652. }
  1653. function LAMEmiscStereoModeLookup($StereoModeID) {
  1654. static $LAMEmiscStereoModeLookup = array(
  1655. 0 => 'mono',
  1656. 1 => 'stereo',
  1657. 2 => 'dual mono',
  1658. 3 => 'joint stereo',
  1659. 4 => 'forced stereo',
  1660. 5 => 'auto',
  1661. 6 => 'intensity stereo',
  1662. 7 => 'other'
  1663. );
  1664. return (isset($LAMEmiscStereoModeLookup[$StereoModeID]) ? $LAMEmiscStereoModeLookup[$StereoModeID] : '');
  1665. }
  1666. function LAMEmiscSourceSampleFrequencyLookup($SourceSampleFrequencyID) {
  1667. static $LAMEmiscSourceSampleFrequencyLookup = array(
  1668. 0 => '<= 32 kHz',
  1669. 1 => '44.1 kHz',
  1670. 2 => '48 kHz',
  1671. 3 => '> 48kHz'
  1672. );
  1673. return (isset($LAMEmiscSourceSampleFrequencyLookup[$SourceSampleFrequencyID]) ? $LAMEmiscSourceSampleFrequencyLookup[$SourceSampleFrequencyID] : '');
  1674. }
  1675. function LAMEsurroundInfoLookup($SurroundInfoID) {
  1676. static $LAMEsurroundInfoLookup = array(
  1677. 0 => 'no surround info',
  1678. 1 => 'DPL encoding',
  1679. 2 => 'DPL2 encoding',
  1680. 3 => 'Ambisonic encoding'
  1681. );
  1682. return (isset($LAMEsurroundInfoLookup[$SurroundInfoID]) ? $LAMEsurroundInfoLookup[$SurroundInfoID] : 'reserved');
  1683. }
  1684. function LAMEpresetUsedLookup($LAMEtag) {
  1685. if ($LAMEtag['preset_used_id'] == 0) {
  1686. // no preset used (LAME >=3.93)
  1687. // no preset recorded (LAME <3.93)
  1688. return '';
  1689. }
  1690. $LAMEpresetUsedLookup = array();
  1691. ///// THIS PART CANNOT BE STATIC .
  1692. for ($i = 8; $i <= 320; $i++) {
  1693. switch ($LAMEtag['vbr_method']) {
  1694. case 'cbr':
  1695. $LAMEpresetUsedLookup[$i] = '--alt-preset '.$LAMEtag['vbr_method'].' '.$i;
  1696. break;
  1697. case 'abr':
  1698. default: // other VBR modes shouldn't be here(?)
  1699. $LAMEpresetUsedLookup[$i] = '--alt-preset '.$i;
  1700. break;
  1701. }
  1702. }
  1703. // named old-style presets (studio, phone, voice, etc) are handled in GuessEncoderOptions()
  1704. // named alt-presets
  1705. $LAMEpresetUsedLookup[1000] = '--r3mix';
  1706. $LAMEpresetUsedLookup[1001] = '--alt-preset standard';
  1707. $LAMEpresetUsedLookup[1002] = '--alt-preset extreme';
  1708. $LAMEpresetUsedLookup[1003] = '--alt-preset insane';
  1709. $LAMEpresetUsedLookup[1004] = '--alt-preset fast standard';
  1710. $LAMEpresetUsedLookup[1005] = '--alt-preset fast extreme';
  1711. $LAMEpresetUsedLookup[1006] = '--alt-preset medium';
  1712. $LAMEpresetUsedLookup[1007] = '--alt-preset fast medium';
  1713. // LAME 3.94 additions/changes
  1714. $LAMEpresetUsedLookup[1010] = '--preset portable'; // 3.94a15 Oct 21 2003
  1715. $LAMEpresetUsedLookup[1015] = '--preset radio'; // 3.94a15 Oct 21 2003
  1716. $LAMEpresetUsedLookup[320] = '--preset insane'; // 3.94a15 Nov 12 2003
  1717. $LAMEpresetUsedLookup[410] = '-V9';
  1718. $LAMEpresetUsedLookup[420] = '-V8';
  1719. $LAMEpresetUsedLookup[440] = '-V6';
  1720. $LAMEpresetUsedLookup[430] = '--preset radio'; // 3.94a15 Nov 12 2003
  1721. $LAMEpresetUsedLookup[450] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'portable'; // 3.94a15 Nov 12 2003
  1722. $LAMEpresetUsedLookup[460] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'medium'; // 3.94a15 Nov 12 2003
  1723. $LAMEpresetUsedLookup[470] = '--r3mix'; // 3.94b1 Dec 18 2003
  1724. $LAMEpresetUsedLookup[480] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'standard'; // 3.94a15 Nov 12 2003
  1725. $LAMEpresetUsedLookup[490] = '-V1';
  1726. $LAMEpresetUsedLookup[500] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'extreme'; // 3.94a15 Nov 12 2003
  1727. return (isset($LAMEpresetUsedLookup[$LAMEtag['preset_used_id']]) ? $LAMEpresetUsedLookup[$LAMEtag['preset_used_id']] : 'new/unknown preset: '.$LAMEtag['preset_used_id'].' - report to info@getid3.org');
  1728. }
  1729. }
  1730. ?>