module.audio.ogg.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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.ogg.php //
  11. // module for analyzing Ogg Vorbis, OggFLAC and Speex files //
  12. // dependencies: module.audio.flac.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.flac.php', __FILE__, true);
  16. class getid3_ogg
  17. {
  18. function getid3_ogg(&$fd, &$ThisFileInfo) {
  19. $ThisFileInfo['fileformat'] = 'ogg';
  20. // Warn about illegal tags - only vorbiscomments are allowed
  21. if (isset($ThisFileInfo['id3v2'])) {
  22. $ThisFileInfo['warning'][] = 'Illegal ID3v2 tag present.';
  23. }
  24. if (isset($ThisFileInfo['id3v1'])) {
  25. $ThisFileInfo['warning'][] = 'Illegal ID3v1 tag present.';
  26. }
  27. if (isset($ThisFileInfo['ape'])) {
  28. $ThisFileInfo['warning'][] = 'Illegal APE tag present.';
  29. }
  30. // Page 1 - Stream Header
  31. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  32. $oggpageinfo = getid3_ogg::ParseOggPageHeader($fd);
  33. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  34. if (ftell($fd) >= GETID3_FREAD_BUFFER_SIZE) {
  35. $ThisFileInfo['error'][] = 'Could not find start of Ogg page in the first '.GETID3_FREAD_BUFFER_SIZE.' bytes (this might not be an Ogg-Vorbis file?)';
  36. unset($ThisFileInfo['fileformat']);
  37. unset($ThisFileInfo['ogg']);
  38. return false;
  39. }
  40. $filedata = fread($fd, $oggpageinfo['page_length']);
  41. $filedataoffset = 0;
  42. if (substr($filedata, 0, 4) == 'fLaC') {
  43. $ThisFileInfo['audio']['dataformat'] = 'flac';
  44. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  45. $ThisFileInfo['audio']['lossless'] = true;
  46. } elseif (substr($filedata, 1, 6) == 'vorbis') {
  47. $this->ParseVorbisPageHeader($filedata, $filedataoffset, $ThisFileInfo, $oggpageinfo);
  48. } elseif (substr($filedata, 0, 8) == 'Speex ') {
  49. // http://www.speex.org/manual/node10.html
  50. $ThisFileInfo['audio']['dataformat'] = 'speex';
  51. $ThisFileInfo['mime_type'] = 'audio/speex';
  52. $ThisFileInfo['audio']['bitrate_mode'] = 'abr';
  53. $ThisFileInfo['audio']['lossless'] = false;
  54. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_string'] = substr($filedata, $filedataoffset, 8); // hard-coded to 'Speex '
  55. $filedataoffset += 8;
  56. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version'] = substr($filedata, $filedataoffset, 20);
  57. $filedataoffset += 20;
  58. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version_id'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  59. $filedataoffset += 4;
  60. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['header_size'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  61. $filedataoffset += 4;
  62. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['rate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  63. $filedataoffset += 4;
  64. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  65. $filedataoffset += 4;
  66. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode_bitstream_version'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  67. $filedataoffset += 4;
  68. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['nb_channels'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  69. $filedataoffset += 4;
  70. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['bitrate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  71. $filedataoffset += 4;
  72. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['framesize'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  73. $filedataoffset += 4;
  74. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['vbr'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  75. $filedataoffset += 4;
  76. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['frames_per_packet'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  77. $filedataoffset += 4;
  78. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['extra_headers'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  79. $filedataoffset += 4;
  80. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['reserved1'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  81. $filedataoffset += 4;
  82. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['reserved2'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  83. $filedataoffset += 4;
  84. $ThisFileInfo['speex']['speex_version'] = trim($ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version']);
  85. $ThisFileInfo['speex']['sample_rate'] = $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['rate'];
  86. $ThisFileInfo['speex']['channels'] = $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['nb_channels'];
  87. $ThisFileInfo['speex']['vbr'] = (bool) $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['vbr'];
  88. $ThisFileInfo['speex']['band_type'] = getid3_ogg::SpeexBandModeLookup($ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode']);
  89. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['speex']['sample_rate'];
  90. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['speex']['channels'];
  91. if ($ThisFileInfo['speex']['vbr']) {
  92. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  93. }
  94. } else {
  95. $ThisFileInfo['error'][] = 'Expecting either "Speex " or "vorbis" identifier strings, found neither';
  96. unset($ThisFileInfo['ogg']);
  97. unset($ThisFileInfo['mime_type']);
  98. return false;
  99. }
  100. // Page 2 - Comment Header
  101. $oggpageinfo = getid3_ogg::ParseOggPageHeader($fd);
  102. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  103. switch ($ThisFileInfo['audio']['dataformat']) {
  104. case 'vorbis':
  105. $filedata = fread($fd, $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  106. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['packet_type'] = getid3_lib::LittleEndian2Int(substr($filedata, 0, 1));
  107. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, 1, 6); // hard-coded to 'vorbis'
  108. getid3_ogg::ParseVorbisCommentsFilepointer($fd, $ThisFileInfo);
  109. break;
  110. case 'flac':
  111. if (!getid3_flac::FLACparseMETAdata($fd, $ThisFileInfo)) {
  112. $ThisFileInfo['error'][] = 'Failed to parse FLAC headers';
  113. return false;
  114. }
  115. break;
  116. case 'speex':
  117. fseek($fd, $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length'], SEEK_CUR);
  118. getid3_ogg::ParseVorbisCommentsFilepointer($fd, $ThisFileInfo);
  119. break;
  120. }
  121. // Last Page - Number of Samples
  122. if ($ThisFileInfo['avdataend'] >= pow(2, 31)) {
  123. $ThisFileInfo['warning'][] = 'Unable to parse Ogg end chunk file (PHP does not support file operations beyond 2GB)';
  124. } else {
  125. fseek($fd, max($ThisFileInfo['avdataend'] - GETID3_FREAD_BUFFER_SIZE, 0), SEEK_SET);
  126. $LastChunkOfOgg = strrev(fread($fd, GETID3_FREAD_BUFFER_SIZE));
  127. if ($LastOggSpostion = strpos($LastChunkOfOgg, 'SggO')) {
  128. fseek($fd, $ThisFileInfo['avdataend'] - ($LastOggSpostion + strlen('SggO')), SEEK_SET);
  129. $ThisFileInfo['avdataend'] = ftell($fd);
  130. $ThisFileInfo['ogg']['pageheader']['eos'] = getid3_ogg::ParseOggPageHeader($fd);
  131. $ThisFileInfo['ogg']['samples'] = $ThisFileInfo['ogg']['pageheader']['eos']['pcm_abs_position'];
  132. if ($ThisFileInfo['ogg']['samples'] == 0) {
  133. $ThisFileInfo['error'][] = 'Corrupt Ogg file: eos.number of samples == zero';
  134. return false;
  135. }
  136. $ThisFileInfo['ogg']['bitrate_average'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / ($ThisFileInfo['ogg']['samples'] / $ThisFileInfo['audio']['sample_rate']);
  137. }
  138. }
  139. if (!empty($ThisFileInfo['ogg']['bitrate_average'])) {
  140. $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['ogg']['bitrate_average'];
  141. } elseif (!empty($ThisFileInfo['ogg']['bitrate_nominal'])) {
  142. $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['ogg']['bitrate_nominal'];
  143. } elseif (!empty($ThisFileInfo['ogg']['bitrate_min']) && !empty($ThisFileInfo['ogg']['bitrate_max'])) {
  144. $ThisFileInfo['audio']['bitrate'] = ($ThisFileInfo['ogg']['bitrate_min'] + $ThisFileInfo['ogg']['bitrate_max']) / 2;
  145. }
  146. if (isset($ThisFileInfo['audio']['bitrate']) && !isset($ThisFileInfo['playtime_seconds'])) {
  147. if ($ThisFileInfo['audio']['bitrate'] == 0) {
  148. $ThisFileInfo['error'][] = 'Corrupt Ogg file: bitrate_audio == zero';
  149. return false;
  150. }
  151. $ThisFileInfo['playtime_seconds'] = (float) ((($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['audio']['bitrate']);
  152. }
  153. if (isset($ThisFileInfo['ogg']['vendor'])) {
  154. $ThisFileInfo['audio']['encoder'] = preg_replace('/^Encoded with /', '', $ThisFileInfo['ogg']['vendor']);
  155. // Vorbis only
  156. if ($ThisFileInfo['audio']['dataformat'] == 'vorbis') {
  157. // Vorbis 1.0 starts with Xiph.Org
  158. if (preg_match('/^Xiph.Org/', $ThisFileInfo['audio']['encoder'])) {
  159. if ($ThisFileInfo['audio']['bitrate_mode'] == 'abr') {
  160. // Set -b 128 on abr files
  161. $ThisFileInfo['audio']['encoder_options'] = '-b '.round($ThisFileInfo['ogg']['bitrate_nominal'] / 1000);
  162. } elseif (($ThisFileInfo['audio']['bitrate_mode'] == 'vbr') && ($ThisFileInfo['audio']['channels'] == 2) && ($ThisFileInfo['audio']['sample_rate'] >= 44100) && ($ThisFileInfo['audio']['sample_rate'] <= 48000)) {
  163. // Set -q N on vbr files
  164. $ThisFileInfo['audio']['encoder_options'] = '-q '.$this->get_quality_from_nominal_bitrate($ThisFileInfo['ogg']['bitrate_nominal']);
  165. }
  166. }
  167. if (empty($ThisFileInfo['audio']['encoder_options']) && !empty($ThisFileInfo['ogg']['bitrate_nominal'])) {
  168. $ThisFileInfo['audio']['encoder_options'] = 'Nominal bitrate: '.intval(round($ThisFileInfo['ogg']['bitrate_nominal'] / 1000)).'kbps';
  169. }
  170. }
  171. }
  172. return true;
  173. }
  174. function ParseVorbisPageHeader(&$filedata, &$filedataoffset, &$ThisFileInfo, &$oggpageinfo) {
  175. $ThisFileInfo['audio']['dataformat'] = 'vorbis';
  176. $ThisFileInfo['audio']['lossless'] = false;
  177. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['packet_type'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  178. $filedataoffset += 1;
  179. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, $filedataoffset, 6); // hard-coded to 'vorbis'
  180. $filedataoffset += 6;
  181. $ThisFileInfo['ogg']['bitstreamversion'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  182. $filedataoffset += 4;
  183. $ThisFileInfo['ogg']['numberofchannels'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  184. $filedataoffset += 1;
  185. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['ogg']['numberofchannels'];
  186. $ThisFileInfo['ogg']['samplerate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  187. $filedataoffset += 4;
  188. if ($ThisFileInfo['ogg']['samplerate'] == 0) {
  189. $ThisFileInfo['error'][] = 'Corrupt Ogg file: sample rate == zero';
  190. return false;
  191. }
  192. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['ogg']['samplerate'];
  193. $ThisFileInfo['ogg']['samples'] = 0; // filled in later
  194. $ThisFileInfo['ogg']['bitrate_average'] = 0; // filled in later
  195. $ThisFileInfo['ogg']['bitrate_max'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  196. $filedataoffset += 4;
  197. $ThisFileInfo['ogg']['bitrate_nominal'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  198. $filedataoffset += 4;
  199. $ThisFileInfo['ogg']['bitrate_min'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  200. $filedataoffset += 4;
  201. $ThisFileInfo['ogg']['blocksize_small'] = pow(2, getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)) & 0x0F);
  202. $ThisFileInfo['ogg']['blocksize_large'] = pow(2, (getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)) & 0xF0) >> 4);
  203. $ThisFileInfo['ogg']['stop_bit'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); // must be 1, marks end of packet
  204. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr'; // overridden if actually abr
  205. if ($ThisFileInfo['ogg']['bitrate_max'] == 0xFFFFFFFF) {
  206. unset($ThisFileInfo['ogg']['bitrate_max']);
  207. $ThisFileInfo['audio']['bitrate_mode'] = 'abr';
  208. }
  209. if ($ThisFileInfo['ogg']['bitrate_nominal'] == 0xFFFFFFFF) {
  210. unset($ThisFileInfo['ogg']['bitrate_nominal']);
  211. }
  212. if ($ThisFileInfo['ogg']['bitrate_min'] == 0xFFFFFFFF) {
  213. unset($ThisFileInfo['ogg']['bitrate_min']);
  214. $ThisFileInfo['audio']['bitrate_mode'] = 'abr';
  215. }
  216. return true;
  217. }
  218. function ParseOggPageHeader(&$fd) {
  219. // http://xiph.org/ogg/vorbis/doc/framing.html
  220. $oggheader['page_start_offset'] = ftell($fd); // where we started from in the file
  221. $filedata = fread($fd, GETID3_FREAD_BUFFER_SIZE);
  222. $filedataoffset = 0;
  223. while ((substr($filedata, $filedataoffset++, 4) != 'OggS')) {
  224. if ((ftell($fd) - $oggheader['page_start_offset']) >= GETID3_FREAD_BUFFER_SIZE) {
  225. // should be found before here
  226. return false;
  227. }
  228. if ((($filedataoffset + 28) > strlen($filedata)) || (strlen($filedata) < 28)) {
  229. if (feof($fd) || (($filedata .= fread($fd, GETID3_FREAD_BUFFER_SIZE)) === false)) {
  230. // get some more data, unless eof, in which case fail
  231. return false;
  232. }
  233. }
  234. }
  235. $filedataoffset += strlen('OggS') - 1; // page, delimited by 'OggS'
  236. $oggheader['stream_structver'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  237. $filedataoffset += 1;
  238. $oggheader['flags_raw'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  239. $filedataoffset += 1;
  240. $oggheader['flags']['fresh'] = (bool) ($oggheader['flags_raw'] & 0x01); // fresh packet
  241. $oggheader['flags']['bos'] = (bool) ($oggheader['flags_raw'] & 0x02); // first page of logical bitstream (bos)
  242. $oggheader['flags']['eos'] = (bool) ($oggheader['flags_raw'] & 0x04); // last page of logical bitstream (eos)
  243. $oggheader['pcm_abs_position'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  244. $filedataoffset += 8;
  245. $oggheader['stream_serialno'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  246. $filedataoffset += 4;
  247. $oggheader['page_seqno'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  248. $filedataoffset += 4;
  249. $oggheader['page_checksum'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  250. $filedataoffset += 4;
  251. $oggheader['page_segments'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  252. $filedataoffset += 1;
  253. $oggheader['page_length'] = 0;
  254. for ($i = 0; $i < $oggheader['page_segments']; $i++) {
  255. $oggheader['segment_table'][$i] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  256. $filedataoffset += 1;
  257. $oggheader['page_length'] += $oggheader['segment_table'][$i];
  258. }
  259. $oggheader['header_end_offset'] = $oggheader['page_start_offset'] + $filedataoffset;
  260. $oggheader['page_end_offset'] = $oggheader['header_end_offset'] + $oggheader['page_length'];
  261. fseek($fd, $oggheader['header_end_offset'], SEEK_SET);
  262. return $oggheader;
  263. }
  264. function ParseVorbisCommentsFilepointer(&$fd, &$ThisFileInfo) {
  265. $OriginalOffset = ftell($fd);
  266. $CommentStartOffset = $OriginalOffset;
  267. $commentdataoffset = 0;
  268. $VorbisCommentPage = 1;
  269. switch ($ThisFileInfo['audio']['dataformat']) {
  270. case 'vorbis':
  271. $CommentStartOffset = $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_start_offset']; // Second Ogg page, after header block
  272. fseek($fd, $CommentStartOffset, SEEK_SET);
  273. $commentdataoffset = 27 + $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_segments'];
  274. $commentdata = fread($fd, getid3_ogg::OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1) + $commentdataoffset);
  275. $commentdataoffset += (strlen('vorbis') + 1);
  276. break;
  277. case 'flac':
  278. fseek($fd, $ThisFileInfo['flac']['VORBIS_COMMENT']['raw']['offset'] + 4, SEEK_SET);
  279. $commentdata = fread($fd, $ThisFileInfo['flac']['VORBIS_COMMENT']['raw']['block_length']);
  280. break;
  281. case 'speex':
  282. $CommentStartOffset = $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_start_offset']; // Second Ogg page, after header block
  283. fseek($fd, $CommentStartOffset, SEEK_SET);
  284. $commentdataoffset = 27 + $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_segments'];
  285. $commentdata = fread($fd, getid3_ogg::OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1) + $commentdataoffset);
  286. break;
  287. default:
  288. return false;
  289. break;
  290. }
  291. $VendorSize = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4));
  292. $commentdataoffset += 4;
  293. $ThisFileInfo['ogg']['vendor'] = substr($commentdata, $commentdataoffset, $VendorSize);
  294. $commentdataoffset += $VendorSize;
  295. $CommentsCount = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4));
  296. $commentdataoffset += 4;
  297. $ThisFileInfo['avdataoffset'] = $CommentStartOffset + $commentdataoffset;
  298. $basicfields = array('TITLE', 'ARTIST', 'ALBUM', 'TRACKNUMBER', 'GENRE', 'DATE', 'DESCRIPTION', 'COMMENT');
  299. for ($i = 0; $i < $CommentsCount; $i++) {
  300. $ThisFileInfo['ogg']['comments_raw'][$i]['dataoffset'] = $CommentStartOffset + $commentdataoffset;
  301. if (ftell($fd) < ($ThisFileInfo['ogg']['comments_raw'][$i]['dataoffset'] + 4)) {
  302. $VorbisCommentPage++;
  303. $oggpageinfo = getid3_ogg::ParseOggPageHeader($fd);
  304. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  305. // First, save what we haven't read yet
  306. $AsYetUnusedData = substr($commentdata, $commentdataoffset);
  307. // Then take that data off the end
  308. $commentdata = substr($commentdata, 0, $commentdataoffset);
  309. // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct
  310. $commentdata .= str_repeat("\x00", 27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  311. $commentdataoffset += (27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  312. // Finally, stick the unused data back on the end
  313. $commentdata .= $AsYetUnusedData;
  314. //$commentdata .= fread($fd, $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  315. $commentdata .= fread($fd, getid3_ogg::OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1));
  316. }
  317. $ThisFileInfo['ogg']['comments_raw'][$i]['size'] = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4));
  318. // replace avdataoffset with position just after the last vorbiscomment
  319. $ThisFileInfo['avdataoffset'] = $ThisFileInfo['ogg']['comments_raw'][$i]['dataoffset'] + $ThisFileInfo['ogg']['comments_raw'][$i]['size'] + 4;
  320. $commentdataoffset += 4;
  321. while ((strlen($commentdata) - $commentdataoffset) < $ThisFileInfo['ogg']['comments_raw'][$i]['size']) {
  322. if (($ThisFileInfo['ogg']['comments_raw'][$i]['size'] > $ThisFileInfo['avdataend']) || ($ThisFileInfo['ogg']['comments_raw'][$i]['size'] < 0)) {
  323. $ThisFileInfo['error'][] = 'Invalid Ogg comment size (comment #'.$i.', claims to be '.number_format($ThisFileInfo['ogg']['comments_raw'][$i]['size']).' bytes) - aborting reading comments';
  324. break 2;
  325. }
  326. $VorbisCommentPage++;
  327. $oggpageinfo = getid3_ogg::ParseOggPageHeader($fd);
  328. $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  329. // First, save what we haven't read yet
  330. $AsYetUnusedData = substr($commentdata, $commentdataoffset);
  331. // Then take that data off the end
  332. $commentdata = substr($commentdata, 0, $commentdataoffset);
  333. // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct
  334. $commentdata .= str_repeat("\x00", 27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  335. $commentdataoffset += (27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  336. // Finally, stick the unused data back on the end
  337. $commentdata .= $AsYetUnusedData;
  338. //$commentdata .= fread($fd, $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  339. $commentdata .= fread($fd, getid3_ogg::OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1));
  340. //$filebaseoffset += $oggpageinfo['header_end_offset'] - $oggpageinfo['page_start_offset'];
  341. }
  342. $commentstring = substr($commentdata, $commentdataoffset, $ThisFileInfo['ogg']['comments_raw'][$i]['size']);
  343. $commentdataoffset += $ThisFileInfo['ogg']['comments_raw'][$i]['size'];
  344. if (!$commentstring) {
  345. // no comment?
  346. $ThisFileInfo['warning'][] = 'Blank Ogg comment ['.$i.']';
  347. } elseif (strstr($commentstring, '=')) {
  348. $commentexploded = explode('=', $commentstring, 2);
  349. $ThisFileInfo['ogg']['comments_raw'][$i]['key'] = strtoupper($commentexploded[0]);
  350. $ThisFileInfo['ogg']['comments_raw'][$i]['value'] = @$commentexploded[1];
  351. $ThisFileInfo['ogg']['comments_raw'][$i]['data'] = base64_decode($ThisFileInfo['ogg']['comments_raw'][$i]['value']);
  352. $ThisFileInfo['ogg']['comments'][strtolower($ThisFileInfo['ogg']['comments_raw'][$i]['key'])][] = $ThisFileInfo['ogg']['comments_raw'][$i]['value'];
  353. $imageinfo = array();
  354. $imagechunkcheck = getid3_lib::GetDataImageSize($ThisFileInfo['ogg']['comments_raw'][$i]['data'], $imageinfo);
  355. $ThisFileInfo['ogg']['comments_raw'][$i]['image_mime'] = getid3_lib::image_type_to_mime_type($imagechunkcheck[2]);
  356. if (!$ThisFileInfo['ogg']['comments_raw'][$i]['image_mime'] || ($ThisFileInfo['ogg']['comments_raw'][$i]['image_mime'] == 'application/octet-stream')) {
  357. unset($ThisFileInfo['ogg']['comments_raw'][$i]['image_mime']);
  358. unset($ThisFileInfo['ogg']['comments_raw'][$i]['data']);
  359. }
  360. } else {
  361. $ThisFileInfo['warning'][] = '[known problem with CDex >= v1.40, < v1.50b7] Invalid Ogg comment name/value pair ['.$i.']: '.$commentstring;
  362. }
  363. }
  364. // Replay Gain Adjustment
  365. // http://privatewww.essex.ac.uk/~djmrob/replaygain/
  366. if (isset($ThisFileInfo['ogg']['comments']) && is_array($ThisFileInfo['ogg']['comments'])) {
  367. foreach ($ThisFileInfo['ogg']['comments'] as $index => $commentvalue) {
  368. switch ($index) {
  369. case 'rg_audiophile':
  370. case 'replaygain_album_gain':
  371. $ThisFileInfo['replay_gain']['album']['adjustment'] = (double) $commentvalue[0];
  372. unset($ThisFileInfo['ogg']['comments'][$index]);
  373. break;
  374. case 'rg_radio':
  375. case 'replaygain_track_gain':
  376. $ThisFileInfo['replay_gain']['track']['adjustment'] = (double) $commentvalue[0];
  377. unset($ThisFileInfo['ogg']['comments'][$index]);
  378. break;
  379. case 'replaygain_album_peak':
  380. $ThisFileInfo['replay_gain']['album']['peak'] = (double) $commentvalue[0];
  381. unset($ThisFileInfo['ogg']['comments'][$index]);
  382. break;
  383. case 'rg_peak':
  384. case 'replaygain_track_peak':
  385. $ThisFileInfo['replay_gain']['track']['peak'] = (double) $commentvalue[0];
  386. unset($ThisFileInfo['ogg']['comments'][$index]);
  387. break;
  388. default:
  389. // do nothing
  390. break;
  391. }
  392. }
  393. }
  394. fseek($fd, $OriginalOffset, SEEK_SET);
  395. return true;
  396. }
  397. function SpeexBandModeLookup($mode) {
  398. static $SpeexBandModeLookup = array();
  399. if (empty($SpeexBandModeLookup)) {
  400. $SpeexBandModeLookup[0] = 'narrow';
  401. $SpeexBandModeLookup[1] = 'wide';
  402. $SpeexBandModeLookup[2] = 'ultra-wide';
  403. }
  404. return (isset($SpeexBandModeLookup[$mode]) ? $SpeexBandModeLookup[$mode] : null);
  405. }
  406. function OggPageSegmentLength($OggInfoArray, $SegmentNumber=1) {
  407. for ($i = 0; $i < $SegmentNumber; $i++) {
  408. $segmentlength = 0;
  409. foreach ($OggInfoArray['segment_table'] as $key => $value) {
  410. $segmentlength += $value;
  411. if ($value < 255) {
  412. break;
  413. }
  414. }
  415. }
  416. return $segmentlength;
  417. }
  418. function get_quality_from_nominal_bitrate($nominal_bitrate) {
  419. // decrease precision
  420. $nominal_bitrate = $nominal_bitrate / 1000;
  421. if ($nominal_bitrate < 128) {
  422. // q-1 to q4
  423. $qval = ($nominal_bitrate - 64) / 16;
  424. } elseif ($nominal_bitrate < 256) {
  425. // q4 to q8
  426. $qval = $nominal_bitrate / 32;
  427. } elseif ($nominal_bitrate < 320) {
  428. // q8 to q9
  429. $qval = ($nominal_bitrate + 256) / 64;
  430. } else {
  431. // q9 to q10
  432. $qval = ($nominal_bitrate + 1300) / 180;
  433. }
  434. //return $qval; // 5.031324
  435. //return intval($qval); // 5
  436. return round($qval, 1); // 5 or 4.9
  437. }
  438. }
  439. ?>