module.audio.dsdiff.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.dsdiff.php //
  11. // module for analyzing Direct Stream Digital Interchange //
  12. // File Format (DSDIFF) files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  17. exit;
  18. }
  19. class getid3_dsdiff extends getid3_handler
  20. {
  21. /**
  22. * @return bool
  23. */
  24. public function Analyze() {
  25. $info = &$this->getid3->info;
  26. $this->fseek($info['avdataoffset']);
  27. $DSDIFFheader = $this->fread(4);
  28. // https://dsd-guide.com/sites/default/files/white-papers/DSDIFF_1.5_Spec.pdf
  29. if (substr($DSDIFFheader, 0, 4) != 'FRM8') {
  30. $this->error('Expecting "FRM8" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($DSDIFFheader, 0, 4)).'"');
  31. return false;
  32. }
  33. unset($DSDIFFheader);
  34. $this->fseek($info['avdataoffset']);
  35. $info['encoding'] = 'ISO-8859-1'; // not certain, but assumed
  36. $info['fileformat'] = 'dsdiff';
  37. $info['mime_type'] = 'audio/dsd';
  38. $info['audio']['dataformat'] = 'dsdiff';
  39. $info['audio']['bitrate_mode'] = 'cbr';
  40. $info['audio']['bits_per_sample'] = 1;
  41. $info['dsdiff'] = array();
  42. while (!$this->feof() && ($ChunkHeader = $this->fread(12))) {
  43. if (strlen($ChunkHeader) < 12) {
  44. $this->error('Expecting chunk header at offset '.(isset($thisChunk['offset']) ? $thisChunk['offset'] : 'N/A').', found insufficient data in file, aborting parsing');
  45. break;
  46. }
  47. $thisChunk = array();
  48. $thisChunk['offset'] = $this->ftell() - 12;
  49. $thisChunk['name'] = substr($ChunkHeader, 0, 4);
  50. if (!preg_match('#^[\\x21-\\x7E]+ *$#', $thisChunk['name'])) {
  51. // "a concatenation of four printable ASCII characters in the range ' ' (space, 0x20) through '~'(0x7E). Space (0x20) cannot precede printing characters; trailing spaces are allowed."
  52. $this->error('Invalid chunk name "'.$thisChunk['name'].'" ('.getid3_lib::PrintHexBytes($thisChunk['name']).') at offset '.$thisChunk['offset'].', aborting parsing');
  53. }
  54. $thisChunk['size'] = getid3_lib::BigEndian2Int(substr($ChunkHeader, 4, 8));
  55. $datasize = $thisChunk['size'] + ($thisChunk['size'] % 2); // "If the data is an odd number of bytes in length, a pad byte must be added at the end. The pad byte is not included in ckDataSize."
  56. switch ($thisChunk['name']) {
  57. case 'FRM8':
  58. $thisChunk['form_type'] = $this->fread(4);
  59. if ($thisChunk['form_type'] != 'DSD ') {
  60. $this->error('Expecting "DSD " at offset '.($this->ftell() - 4).', found "'.getid3_lib::PrintHexBytes($thisChunk['form_type']).'", aborting parsing');
  61. break 2;
  62. }
  63. // do nothing further, prevent skipping subchunks
  64. break;
  65. case 'PROP': // PROPerty chunk
  66. $thisChunk['prop_type'] = $this->fread(4);
  67. if ($thisChunk['prop_type'] != 'SND ') {
  68. $this->error('Expecting "SND " at offset '.($this->ftell() - 4).', found "'.getid3_lib::PrintHexBytes($thisChunk['prop_type']).'", aborting parsing');
  69. break 2;
  70. }
  71. // do nothing further, prevent skipping subchunks
  72. break;
  73. case 'DIIN': // eDIted master INformation chunk
  74. // do nothing, just prevent skipping subchunks
  75. break;
  76. case 'FVER': // Format VERsion chunk
  77. if ($thisChunk['size'] == 4) {
  78. $FVER = $this->fread(4);
  79. $info['dsdiff']['format_version'] = ord($FVER[0]).'.'.ord($FVER[1]).'.'.ord($FVER[2]).'.'.ord($FVER[3]);
  80. unset($FVER);
  81. } else {
  82. $this->warning('Expecting "FVER" chunk to be 4 bytes, found '.$thisChunk['size'].' bytes, skipping chunk');
  83. $this->fseek($datasize, SEEK_CUR);
  84. }
  85. break;
  86. case 'FS ': // sample rate chunk
  87. if ($thisChunk['size'] == 4) {
  88. $info['dsdiff']['sample_rate'] = getid3_lib::BigEndian2Int($this->fread(4));
  89. $info['audio']['sample_rate'] = $info['dsdiff']['sample_rate'];
  90. } else {
  91. $this->warning('Expecting "FVER" chunk to be 4 bytes, found '.$thisChunk['size'].' bytes, skipping chunk');
  92. $this->fseek($datasize, SEEK_CUR);
  93. }
  94. break;
  95. case 'CHNL': // CHaNneLs chunk
  96. $thisChunk['num_channels'] = getid3_lib::BigEndian2Int($this->fread(2));
  97. if ($thisChunk['num_channels'] == 0) {
  98. $this->warning('channel count should be greater than zero, skipping chunk');
  99. $this->fseek($datasize - 2, SEEK_CUR);
  100. }
  101. for ($i = 0; $i < $thisChunk['num_channels']; $i++) {
  102. $thisChunk['channels'][$i] = $this->fread(4);
  103. }
  104. $info['audio']['channels'] = $thisChunk['num_channels'];
  105. break;
  106. case 'CMPR': // CoMPRession type chunk
  107. $thisChunk['compression_type'] = $this->fread(4);
  108. $info['audio']['dataformat'] = trim($thisChunk['compression_type']);
  109. $humanReadableByteLength = getid3_lib::BigEndian2Int($this->fread(1));
  110. $thisChunk['compression_name'] = $this->fread($humanReadableByteLength);
  111. if (($humanReadableByteLength % 2) == 0) {
  112. // need to seek to multiple of 2 bytes, human-readable string length is only one byte long so if the string is an even number of bytes we need to seek past a padding byte after the string
  113. $this->fseek(1, SEEK_CUR);
  114. }
  115. unset($humanReadableByteLength);
  116. break;
  117. case 'ABSS': // ABSolute Start time chunk
  118. $ABSS = $this->fread(8);
  119. $info['dsdiff']['absolute_start_time']['hours'] = getid3_lib::BigEndian2Int(substr($ABSS, 0, 2));
  120. $info['dsdiff']['absolute_start_time']['minutes'] = getid3_lib::BigEndian2Int(substr($ABSS, 2, 1));
  121. $info['dsdiff']['absolute_start_time']['seconds'] = getid3_lib::BigEndian2Int(substr($ABSS, 3, 1));
  122. $info['dsdiff']['absolute_start_time']['samples'] = getid3_lib::BigEndian2Int(substr($ABSS, 4, 4));
  123. unset($ABSS);
  124. break;
  125. case 'LSCO': // LoudSpeaker COnfiguration chunk
  126. // 0 = 2-channel stereo set-up
  127. // 3 = 5-channel set-up according to ITU-R BS.775-1 [ITU]
  128. // 4 = 6-channel set-up, 5-channel set-up according to ITU-R BS.775-1 [ITU], plus additional Low Frequency Enhancement (LFE) loudspeaker. Also known as "5.1 configuration"
  129. // 65535 = Undefined channel set-up
  130. $thisChunk['loundspeaker_config_id'] = getid3_lib::BigEndian2Int($this->fread(2));
  131. break;
  132. case 'COMT': // COMmenTs chunk
  133. $thisChunk['num_comments'] = getid3_lib::BigEndian2Int($this->fread(2));
  134. for ($i = 0; $i < $thisChunk['num_comments']; $i++) {
  135. $thisComment = array();
  136. $COMT = $this->fread(14);
  137. $thisComment['creation_year'] = getid3_lib::BigEndian2Int(substr($COMT, 0, 2));
  138. $thisComment['creation_month'] = getid3_lib::BigEndian2Int(substr($COMT, 2, 1));
  139. $thisComment['creation_day'] = getid3_lib::BigEndian2Int(substr($COMT, 3, 1));
  140. $thisComment['creation_hour'] = getid3_lib::BigEndian2Int(substr($COMT, 4, 1));
  141. $thisComment['creation_minute'] = getid3_lib::BigEndian2Int(substr($COMT, 5, 1));
  142. $thisComment['comment_type_id'] = getid3_lib::BigEndian2Int(substr($COMT, 6, 2));
  143. $thisComment['comment_ref_id'] = getid3_lib::BigEndian2Int(substr($COMT, 8, 2));
  144. $thisComment['string_length'] = getid3_lib::BigEndian2Int(substr($COMT, 10, 4));
  145. $thisComment['comment_text'] = $this->fread($thisComment['string_length']);
  146. if ($thisComment['string_length'] % 2) {
  147. // commentText[] is the description of the Comment. This text must be padded with a byte at the end, if needed, to make it an even number of bytes long. This pad byte, if present, is not included in count.
  148. $this->fseek(1, SEEK_CUR);
  149. }
  150. $thisComment['comment_type'] = $this->DSDIFFcmtType($thisComment['comment_type_id']);
  151. $thisComment['comment_reference'] = $this->DSDIFFcmtRef($thisComment['comment_type_id'], $thisComment['comment_ref_id']);
  152. $thisComment['creation_unix'] = mktime($thisComment['creation_hour'], $thisComment['creation_minute'], 0, $thisComment['creation_month'], $thisComment['creation_day'], $thisComment['creation_year']);
  153. $thisChunk['comments'][$i] = $thisComment;
  154. $commentkey = ($thisComment['comment_reference'] ?: 'comment');
  155. $info['dsdiff']['comments'][$commentkey][] = $thisComment['comment_text'];
  156. unset($thisComment);
  157. }
  158. break;
  159. case 'MARK': // MARKer chunk
  160. $MARK = $this->fread(22);
  161. $thisChunk['marker_hours'] = getid3_lib::BigEndian2Int(substr($MARK, 0, 2));
  162. $thisChunk['marker_minutes'] = getid3_lib::BigEndian2Int(substr($MARK, 2, 1));
  163. $thisChunk['marker_seconds'] = getid3_lib::BigEndian2Int(substr($MARK, 3, 1));
  164. $thisChunk['marker_samples'] = getid3_lib::BigEndian2Int(substr($MARK, 4, 4));
  165. $thisChunk['marker_offset'] = getid3_lib::BigEndian2Int(substr($MARK, 8, 4));
  166. $thisChunk['marker_type_id'] = getid3_lib::BigEndian2Int(substr($MARK, 12, 2));
  167. $thisChunk['marker_channel'] = getid3_lib::BigEndian2Int(substr($MARK, 14, 2));
  168. $thisChunk['marker_flagraw'] = getid3_lib::BigEndian2Int(substr($MARK, 16, 2));
  169. $thisChunk['string_length'] = getid3_lib::BigEndian2Int(substr($MARK, 18, 4));
  170. $thisChunk['description'] = ($thisChunk['string_length'] ? $this->fread($thisChunk['string_length']) : '');
  171. if ($thisChunk['string_length'] % 2) {
  172. // markerText[] is the description of the marker. This text must be padded with a byte at the end, if needed, to make it an even number of bytes long. This pad byte, if present, is not included in count.
  173. $this->fseek(1, SEEK_CUR);
  174. }
  175. $thisChunk['marker_type'] = $this->DSDIFFmarkType($thisChunk['marker_type_id']);
  176. unset($MARK);
  177. break;
  178. case 'DIAR': // artist chunk
  179. case 'DITI': // title chunk
  180. $thisChunk['string_length'] = getid3_lib::BigEndian2Int($this->fread(4));
  181. $thisChunk['description'] = ($thisChunk['string_length'] ? $this->fread($thisChunk['string_length']) : '');
  182. if ($thisChunk['string_length'] % 2) {
  183. // This text must be padded with a byte at the end, if needed, to make it an even number of bytes long. This pad byte, if present, is not included in count.
  184. $this->fseek(1, SEEK_CUR);
  185. }
  186. if ($commentkey = (($thisChunk['name'] == 'DIAR') ? 'artist' : (($thisChunk['name'] == 'DITI') ? 'title' : ''))) {
  187. @$info['dsdiff']['comments'][$commentkey][] = $thisChunk['description'];
  188. }
  189. break;
  190. case 'EMID': // Edited Master ID chunk
  191. if ($thisChunk['size']) {
  192. $thisChunk['identifier'] = $this->fread($thisChunk['size']);
  193. }
  194. break;
  195. case 'ID3 ':
  196. $endOfID3v2 = $this->ftell() + $datasize; // we will need to reset the filepointer after parsing ID3v2
  197. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true);
  198. $getid3_temp = new getID3();
  199. $getid3_temp->openfile($this->getid3->filename, null, $this->getid3->fp);
  200. $getid3_id3v2 = new getid3_id3v2($getid3_temp);
  201. $getid3_id3v2->StartingOffset = $this->ftell();
  202. if ($thisChunk['valid'] = $getid3_id3v2->Analyze()) {
  203. $info['id3v2'] = $getid3_temp->info['id3v2'];
  204. }
  205. unset($getid3_temp, $getid3_id3v2);
  206. $this->fseek($endOfID3v2);
  207. break;
  208. case 'DSD ': // DSD sound data chunk
  209. case 'DST ': // DST sound data chunk
  210. // actual audio data, we're not interested, skip
  211. $this->fseek($datasize, SEEK_CUR);
  212. break;
  213. default:
  214. $this->warning('Unhandled chunk "'.$thisChunk['name'].'"');
  215. $this->fseek($datasize, SEEK_CUR);
  216. break;
  217. }
  218. @$info['dsdiff']['chunks'][] = $thisChunk;
  219. //break;
  220. }
  221. if (empty($info['audio']['bitrate']) && !empty($info['audio']['channels']) && !empty($info['audio']['sample_rate']) && !empty($info['audio']['bits_per_sample'])) {
  222. $info['audio']['bitrate'] = $info['audio']['bits_per_sample'] * $info['audio']['sample_rate'] * $info['audio']['channels'];
  223. }
  224. return true;
  225. }
  226. /**
  227. * @param int $cmtType
  228. *
  229. * @return string
  230. */
  231. public static function DSDIFFcmtType($cmtType) {
  232. static $DSDIFFcmtType = array(
  233. 0 => 'General (album) Comment',
  234. 1 => 'Channel Comment',
  235. 2 => 'Sound Source',
  236. 3 => 'File History',
  237. );
  238. return (isset($DSDIFFcmtType[$cmtType]) ? $DSDIFFcmtType[$cmtType] : 'reserved');
  239. }
  240. /**
  241. * @param int $cmtType
  242. * @param int $cmtRef
  243. *
  244. * @return string
  245. */
  246. public static function DSDIFFcmtRef($cmtType, $cmtRef) {
  247. static $DSDIFFcmtRef = array(
  248. 2 => array( // Sound Source
  249. 0 => 'DSD recording',
  250. 1 => 'Analogue recording',
  251. 2 => 'PCM recording',
  252. ),
  253. 3 => array( // File History
  254. 0 => 'comment', // General Remark
  255. 1 => 'encodeby', // Name of the operator
  256. 2 => 'encoder', // Name or type of the creating machine
  257. 3 => 'timezone', // Time zone information
  258. 4 => 'revision', // Revision of the file
  259. ),
  260. );
  261. switch ($cmtType) {
  262. case 0:
  263. // If the comment type is General Comment the comment reference must be 0
  264. return '';
  265. case 1:
  266. // If the comment type is Channel Comment, the comment reference defines the channel number to which the comment belongs
  267. return ($cmtRef ? 'channel '.$cmtRef : 'all channels');
  268. case 2:
  269. case 3:
  270. return (isset($DSDIFFcmtRef[$cmtType][$cmtRef]) ? $DSDIFFcmtRef[$cmtType][$cmtRef] : 'reserved');
  271. }
  272. return 'unsupported $cmtType='.$cmtType;
  273. }
  274. /**
  275. * @param int $markType
  276. *
  277. * @return string
  278. */
  279. public static function DSDIFFmarkType($markType) {
  280. static $DSDIFFmarkType = array(
  281. 0 => 'TrackStart', // Entry point for a Track start
  282. 1 => 'TrackStop', // Entry point for ending a Track
  283. 2 => 'ProgramStart', // Start point of 2-channel or multi-channel area
  284. 3 => 'Obsolete', //
  285. 4 => 'Index', // Entry point of an Index
  286. );
  287. return (isset($DSDIFFmarkType[$markType]) ? $DSDIFFmarkType[$markType] : 'reserved');
  288. }
  289. }