module.audio.mp3.php 97 KB

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