module.audio-video.real.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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-video.real.php //
  11. // module for analyzing Real Audio/Video files //
  12. // dependencies: module.audio-video.riff.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  19. class getid3_real extends getid3_handler
  20. {
  21. /**
  22. * @return bool
  23. */
  24. public function Analyze() {
  25. $info = &$this->getid3->info;
  26. $info['fileformat'] = 'real';
  27. $info['bitrate'] = 0;
  28. $info['playtime_seconds'] = 0;
  29. $this->fseek($info['avdataoffset']);
  30. $ChunkCounter = 0;
  31. while ($this->ftell() < $info['avdataend']) {
  32. $ChunkData = $this->fread(8);
  33. $ChunkName = substr($ChunkData, 0, 4);
  34. $ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, 4, 4));
  35. if ($ChunkName == '.ra'."\xFD") {
  36. $ChunkData .= $this->fread($ChunkSize - 8);
  37. if ($this->ParseOldRAheader(substr($ChunkData, 0, 128), $info['real']['old_ra_header'])) {
  38. $info['audio']['dataformat'] = 'real';
  39. $info['audio']['lossless'] = false;
  40. $info['audio']['sample_rate'] = $info['real']['old_ra_header']['sample_rate'];
  41. $info['audio']['bits_per_sample'] = $info['real']['old_ra_header']['bits_per_sample'];
  42. $info['audio']['channels'] = $info['real']['old_ra_header']['channels'];
  43. $info['playtime_seconds'] = 60 * ($info['real']['old_ra_header']['audio_bytes'] / $info['real']['old_ra_header']['bytes_per_minute']);
  44. $info['audio']['bitrate'] = 8 * ($info['real']['old_ra_header']['audio_bytes'] / $info['playtime_seconds']);
  45. $info['audio']['codec'] = $this->RealAudioCodecFourCClookup($info['real']['old_ra_header']['fourcc'], $info['audio']['bitrate']);
  46. foreach ($info['real']['old_ra_header']['comments'] as $key => $valuearray) {
  47. if (strlen(trim($valuearray[0])) > 0) {
  48. $info['real']['comments'][$key][] = trim($valuearray[0]);
  49. }
  50. }
  51. return true;
  52. }
  53. $this->error('There was a problem parsing this RealAudio file. Please submit it for analysis to info@getid3.org');
  54. unset($info['bitrate']);
  55. unset($info['playtime_seconds']);
  56. return false;
  57. }
  58. // shortcut
  59. $info['real']['chunks'][$ChunkCounter] = array();
  60. $thisfile_real_chunks_currentchunk = &$info['real']['chunks'][$ChunkCounter];
  61. $thisfile_real_chunks_currentchunk['name'] = $ChunkName;
  62. $thisfile_real_chunks_currentchunk['offset'] = $this->ftell() - 8;
  63. $thisfile_real_chunks_currentchunk['length'] = $ChunkSize;
  64. if (($thisfile_real_chunks_currentchunk['offset'] + $thisfile_real_chunks_currentchunk['length']) > $info['avdataend']) {
  65. $this->warning('Chunk "'.$thisfile_real_chunks_currentchunk['name'].'" at offset '.$thisfile_real_chunks_currentchunk['offset'].' claims to be '.$thisfile_real_chunks_currentchunk['length'].' bytes long, which is beyond end of file');
  66. return false;
  67. }
  68. if ($ChunkSize > ($this->getid3->fread_buffer_size() + 8)) {
  69. $ChunkData .= $this->fread($this->getid3->fread_buffer_size() - 8);
  70. $this->fseek($thisfile_real_chunks_currentchunk['offset'] + $ChunkSize);
  71. } elseif(($ChunkSize - 8) > 0) {
  72. $ChunkData .= $this->fread($ChunkSize - 8);
  73. }
  74. $offset = 8;
  75. switch ($ChunkName) {
  76. case '.RMF': // RealMedia File Header
  77. $thisfile_real_chunks_currentchunk['object_version'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  78. $offset += 2;
  79. switch ($thisfile_real_chunks_currentchunk['object_version']) {
  80. case 0:
  81. $thisfile_real_chunks_currentchunk['file_version'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  82. $offset += 4;
  83. $thisfile_real_chunks_currentchunk['headers_count'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  84. $offset += 4;
  85. break;
  86. default:
  87. //$this->warning('Expected .RMF-object_version to be "0", actual value is "'.$thisfile_real_chunks_currentchunk['object_version'].'" (should not be a problem)');
  88. break;
  89. }
  90. break;
  91. case 'PROP': // Properties Header
  92. $thisfile_real_chunks_currentchunk['object_version'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  93. $offset += 2;
  94. if ($thisfile_real_chunks_currentchunk['object_version'] == 0) {
  95. $thisfile_real_chunks_currentchunk['max_bit_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  96. $offset += 4;
  97. $thisfile_real_chunks_currentchunk['avg_bit_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  98. $offset += 4;
  99. $thisfile_real_chunks_currentchunk['max_packet_size'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  100. $offset += 4;
  101. $thisfile_real_chunks_currentchunk['avg_packet_size'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  102. $offset += 4;
  103. $thisfile_real_chunks_currentchunk['num_packets'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  104. $offset += 4;
  105. $thisfile_real_chunks_currentchunk['duration'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  106. $offset += 4;
  107. $thisfile_real_chunks_currentchunk['preroll'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  108. $offset += 4;
  109. $thisfile_real_chunks_currentchunk['index_offset'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  110. $offset += 4;
  111. $thisfile_real_chunks_currentchunk['data_offset'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  112. $offset += 4;
  113. $thisfile_real_chunks_currentchunk['num_streams'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  114. $offset += 2;
  115. $thisfile_real_chunks_currentchunk['flags_raw'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  116. $offset += 2;
  117. $info['playtime_seconds'] = $thisfile_real_chunks_currentchunk['duration'] / 1000;
  118. if ($thisfile_real_chunks_currentchunk['duration'] > 0) {
  119. $info['bitrate'] += $thisfile_real_chunks_currentchunk['avg_bit_rate'];
  120. }
  121. $thisfile_real_chunks_currentchunk['flags']['save_enabled'] = (bool) ($thisfile_real_chunks_currentchunk['flags_raw'] & 0x0001);
  122. $thisfile_real_chunks_currentchunk['flags']['perfect_play'] = (bool) ($thisfile_real_chunks_currentchunk['flags_raw'] & 0x0002);
  123. $thisfile_real_chunks_currentchunk['flags']['live_broadcast'] = (bool) ($thisfile_real_chunks_currentchunk['flags_raw'] & 0x0004);
  124. }
  125. break;
  126. case 'MDPR': // Media Properties Header
  127. $thisfile_real_chunks_currentchunk['object_version'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  128. $offset += 2;
  129. if ($thisfile_real_chunks_currentchunk['object_version'] == 0) {
  130. $thisfile_real_chunks_currentchunk['stream_number'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  131. $offset += 2;
  132. $thisfile_real_chunks_currentchunk['max_bit_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  133. $offset += 4;
  134. $thisfile_real_chunks_currentchunk['avg_bit_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  135. $offset += 4;
  136. $thisfile_real_chunks_currentchunk['max_packet_size'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  137. $offset += 4;
  138. $thisfile_real_chunks_currentchunk['avg_packet_size'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  139. $offset += 4;
  140. $thisfile_real_chunks_currentchunk['start_time'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  141. $offset += 4;
  142. $thisfile_real_chunks_currentchunk['preroll'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  143. $offset += 4;
  144. $thisfile_real_chunks_currentchunk['duration'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  145. $offset += 4;
  146. $thisfile_real_chunks_currentchunk['stream_name_size'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 1));
  147. $offset += 1;
  148. $thisfile_real_chunks_currentchunk['stream_name'] = substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['stream_name_size']);
  149. $offset += $thisfile_real_chunks_currentchunk['stream_name_size'];
  150. $thisfile_real_chunks_currentchunk['mime_type_size'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 1));
  151. $offset += 1;
  152. $thisfile_real_chunks_currentchunk['mime_type'] = substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['mime_type_size']);
  153. $offset += $thisfile_real_chunks_currentchunk['mime_type_size'];
  154. $thisfile_real_chunks_currentchunk['type_specific_len'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  155. $offset += 4;
  156. $thisfile_real_chunks_currentchunk['type_specific_data'] = substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['type_specific_len']);
  157. $offset += $thisfile_real_chunks_currentchunk['type_specific_len'];
  158. // shortcut
  159. $thisfile_real_chunks_currentchunk_typespecificdata = &$thisfile_real_chunks_currentchunk['type_specific_data'];
  160. switch ($thisfile_real_chunks_currentchunk['mime_type']) {
  161. case 'video/x-pn-realvideo':
  162. case 'video/x-pn-multirate-realvideo':
  163. // http://www.freelists.org/archives/matroska-devel/07-2003/msg00010.html
  164. // shortcut
  165. $thisfile_real_chunks_currentchunk['video_info'] = array();
  166. $thisfile_real_chunks_currentchunk_videoinfo = &$thisfile_real_chunks_currentchunk['video_info'];
  167. $thisfile_real_chunks_currentchunk_videoinfo['dwSize'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 0, 4));
  168. $thisfile_real_chunks_currentchunk_videoinfo['fourcc1'] = substr($thisfile_real_chunks_currentchunk_typespecificdata, 4, 4);
  169. $thisfile_real_chunks_currentchunk_videoinfo['fourcc2'] = substr($thisfile_real_chunks_currentchunk_typespecificdata, 8, 4);
  170. $thisfile_real_chunks_currentchunk_videoinfo['width'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 12, 2));
  171. $thisfile_real_chunks_currentchunk_videoinfo['height'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 14, 2));
  172. $thisfile_real_chunks_currentchunk_videoinfo['bits_per_sample'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 16, 2));
  173. //$thisfile_real_chunks_currentchunk_videoinfo['unknown1'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 18, 2));
  174. //$thisfile_real_chunks_currentchunk_videoinfo['unknown2'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 20, 2));
  175. $thisfile_real_chunks_currentchunk_videoinfo['frames_per_second'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 22, 2));
  176. //$thisfile_real_chunks_currentchunk_videoinfo['unknown3'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 24, 2));
  177. //$thisfile_real_chunks_currentchunk_videoinfo['unknown4'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 26, 2));
  178. //$thisfile_real_chunks_currentchunk_videoinfo['unknown5'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 28, 2));
  179. //$thisfile_real_chunks_currentchunk_videoinfo['unknown6'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 30, 2));
  180. //$thisfile_real_chunks_currentchunk_videoinfo['unknown7'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 32, 2));
  181. //$thisfile_real_chunks_currentchunk_videoinfo['unknown8'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 34, 2));
  182. //$thisfile_real_chunks_currentchunk_videoinfo['unknown9'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 36, 2));
  183. $thisfile_real_chunks_currentchunk_videoinfo['codec'] = getid3_riff::fourccLookup($thisfile_real_chunks_currentchunk_videoinfo['fourcc2']);
  184. $info['video']['resolution_x'] = $thisfile_real_chunks_currentchunk_videoinfo['width'];
  185. $info['video']['resolution_y'] = $thisfile_real_chunks_currentchunk_videoinfo['height'];
  186. $info['video']['frame_rate'] = (float) $thisfile_real_chunks_currentchunk_videoinfo['frames_per_second'];
  187. $info['video']['codec'] = $thisfile_real_chunks_currentchunk_videoinfo['codec'];
  188. $info['video']['bits_per_sample'] = $thisfile_real_chunks_currentchunk_videoinfo['bits_per_sample'];
  189. break;
  190. case 'audio/x-pn-realaudio':
  191. case 'audio/x-pn-multirate-realaudio':
  192. $this->ParseOldRAheader($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk['parsed_audio_data']);
  193. $info['audio']['sample_rate'] = $thisfile_real_chunks_currentchunk['parsed_audio_data']['sample_rate'];
  194. $info['audio']['bits_per_sample'] = $thisfile_real_chunks_currentchunk['parsed_audio_data']['bits_per_sample'];
  195. $info['audio']['channels'] = $thisfile_real_chunks_currentchunk['parsed_audio_data']['channels'];
  196. if (!empty($info['audio']['dataformat'])) {
  197. foreach ($info['audio'] as $key => $value) {
  198. if ($key != 'streams') {
  199. $info['audio']['streams'][$thisfile_real_chunks_currentchunk['stream_number']][$key] = $value;
  200. }
  201. }
  202. }
  203. break;
  204. case 'logical-fileinfo':
  205. // shortcut
  206. $thisfile_real_chunks_currentchunk['logical_fileinfo'] = array();
  207. $thisfile_real_chunks_currentchunk_logicalfileinfo = &$thisfile_real_chunks_currentchunk['logical_fileinfo'];
  208. $thisfile_real_chunks_currentchunk_logicalfileinfo_offset = 0;
  209. $thisfile_real_chunks_currentchunk_logicalfileinfo['logical_fileinfo_length'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 4));
  210. $thisfile_real_chunks_currentchunk_logicalfileinfo_offset += 4;
  211. //$thisfile_real_chunks_currentchunk_logicalfileinfo['unknown1'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 4));
  212. $thisfile_real_chunks_currentchunk_logicalfileinfo_offset += 4;
  213. $thisfile_real_chunks_currentchunk_logicalfileinfo['num_tags'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 4));
  214. $thisfile_real_chunks_currentchunk_logicalfileinfo_offset += 4;
  215. //$thisfile_real_chunks_currentchunk_logicalfileinfo['unknown2'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 4));
  216. $thisfile_real_chunks_currentchunk_logicalfileinfo_offset += 4;
  217. //$thisfile_real_chunks_currentchunk_logicalfileinfo['d'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 1));
  218. //$thisfile_real_chunks_currentchunk_logicalfileinfo['one_type'] = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 4));
  219. //$thisfile_real_chunks_currentchunk_logicalfileinfo_thislength = getid3_lib::BigEndian2Int(substr($thisfile_real_chunks_currentchunk_typespecificdata, 4 + $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, 2));
  220. //$thisfile_real_chunks_currentchunk_logicalfileinfo['one'] = substr($thisfile_real_chunks_currentchunk_typespecificdata, 6 + $thisfile_real_chunks_currentchunk_logicalfileinfo_offset, $thisfile_real_chunks_currentchunk_logicalfileinfo_thislength);
  221. //$thisfile_real_chunks_currentchunk_logicalfileinfo_offset += (6 + $thisfile_real_chunks_currentchunk_logicalfileinfo_thislength);
  222. break;
  223. }
  224. if (empty($info['playtime_seconds'])) {
  225. $info['playtime_seconds'] = max($info['playtime_seconds'], ($thisfile_real_chunks_currentchunk['duration'] + $thisfile_real_chunks_currentchunk['start_time']) / 1000);
  226. }
  227. if ($thisfile_real_chunks_currentchunk['duration'] > 0) {
  228. switch ($thisfile_real_chunks_currentchunk['mime_type']) {
  229. case 'audio/x-pn-realaudio':
  230. case 'audio/x-pn-multirate-realaudio':
  231. $info['audio']['bitrate'] = (isset($info['audio']['bitrate']) ? $info['audio']['bitrate'] : 0) + $thisfile_real_chunks_currentchunk['avg_bit_rate'];
  232. $info['audio']['codec'] = $this->RealAudioCodecFourCClookup($thisfile_real_chunks_currentchunk['parsed_audio_data']['fourcc'], $info['audio']['bitrate']);
  233. $info['audio']['dataformat'] = 'real';
  234. $info['audio']['lossless'] = false;
  235. break;
  236. case 'video/x-pn-realvideo':
  237. case 'video/x-pn-multirate-realvideo':
  238. $info['video']['bitrate'] = (isset($info['video']['bitrate']) ? $info['video']['bitrate'] : 0) + $thisfile_real_chunks_currentchunk['avg_bit_rate'];
  239. $info['video']['bitrate_mode'] = 'cbr';
  240. $info['video']['dataformat'] = 'real';
  241. $info['video']['lossless'] = false;
  242. $info['video']['pixel_aspect_ratio'] = (float) 1;
  243. break;
  244. case 'audio/x-ralf-mpeg4-generic':
  245. $info['audio']['bitrate'] = (isset($info['audio']['bitrate']) ? $info['audio']['bitrate'] : 0) + $thisfile_real_chunks_currentchunk['avg_bit_rate'];
  246. $info['audio']['codec'] = 'RealAudio Lossless';
  247. $info['audio']['dataformat'] = 'real';
  248. $info['audio']['lossless'] = true;
  249. break;
  250. }
  251. $info['bitrate'] = (isset($info['video']['bitrate']) ? $info['video']['bitrate'] : 0) + (isset($info['audio']['bitrate']) ? $info['audio']['bitrate'] : 0);
  252. }
  253. }
  254. break;
  255. case 'CONT': // Content Description Header (text comments)
  256. $thisfile_real_chunks_currentchunk['object_version'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  257. $offset += 2;
  258. if ($thisfile_real_chunks_currentchunk['object_version'] == 0) {
  259. $thisfile_real_chunks_currentchunk['title_len'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  260. $offset += 2;
  261. $thisfile_real_chunks_currentchunk['title'] = (string) substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['title_len']);
  262. $offset += $thisfile_real_chunks_currentchunk['title_len'];
  263. $thisfile_real_chunks_currentchunk['artist_len'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  264. $offset += 2;
  265. $thisfile_real_chunks_currentchunk['artist'] = (string) substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['artist_len']);
  266. $offset += $thisfile_real_chunks_currentchunk['artist_len'];
  267. $thisfile_real_chunks_currentchunk['copyright_len'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  268. $offset += 2;
  269. $thisfile_real_chunks_currentchunk['copyright'] = (string) substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['copyright_len']);
  270. $offset += $thisfile_real_chunks_currentchunk['copyright_len'];
  271. $thisfile_real_chunks_currentchunk['comment_len'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  272. $offset += 2;
  273. $thisfile_real_chunks_currentchunk['comment'] = (string) substr($ChunkData, $offset, $thisfile_real_chunks_currentchunk['comment_len']);
  274. $offset += $thisfile_real_chunks_currentchunk['comment_len'];
  275. $commentkeystocopy = array('title'=>'title', 'artist'=>'artist', 'copyright'=>'copyright', 'comment'=>'comment');
  276. foreach ($commentkeystocopy as $key => $val) {
  277. if ($thisfile_real_chunks_currentchunk[$key]) {
  278. $info['real']['comments'][$val][] = trim($thisfile_real_chunks_currentchunk[$key]);
  279. }
  280. }
  281. }
  282. break;
  283. case 'DATA': // Data Chunk Header
  284. // do nothing
  285. break;
  286. case 'INDX': // Index Section Header
  287. $thisfile_real_chunks_currentchunk['object_version'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  288. $offset += 2;
  289. if ($thisfile_real_chunks_currentchunk['object_version'] == 0) {
  290. $thisfile_real_chunks_currentchunk['num_indices'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  291. $offset += 4;
  292. $thisfile_real_chunks_currentchunk['stream_number'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 2));
  293. $offset += 2;
  294. $thisfile_real_chunks_currentchunk['next_index_header'] = getid3_lib::BigEndian2Int(substr($ChunkData, $offset, 4));
  295. $offset += 4;
  296. if ($thisfile_real_chunks_currentchunk['next_index_header'] == 0) {
  297. // last index chunk found, ignore rest of file
  298. break 2;
  299. } else {
  300. // non-last index chunk, seek to next index chunk (skipping actual index data)
  301. $this->fseek($thisfile_real_chunks_currentchunk['next_index_header']);
  302. }
  303. }
  304. break;
  305. default:
  306. $this->warning('Unhandled RealMedia chunk "'.$ChunkName.'" at offset '.$thisfile_real_chunks_currentchunk['offset']);
  307. break;
  308. }
  309. $ChunkCounter++;
  310. }
  311. if (!empty($info['audio']['streams'])) {
  312. $info['audio']['bitrate'] = 0;
  313. foreach ($info['audio']['streams'] as $key => $valuearray) {
  314. $info['audio']['bitrate'] += $valuearray['bitrate'];
  315. }
  316. }
  317. return true;
  318. }
  319. /**
  320. * @param string $OldRAheaderData
  321. * @param array $ParsedArray
  322. *
  323. * @return bool
  324. */
  325. public function ParseOldRAheader($OldRAheaderData, &$ParsedArray) {
  326. // http://www.freelists.org/archives/matroska-devel/07-2003/msg00010.html
  327. $ParsedArray = array();
  328. $ParsedArray['magic'] = substr($OldRAheaderData, 0, 4);
  329. if ($ParsedArray['magic'] != '.ra'."\xFD") {
  330. return false;
  331. }
  332. $ParsedArray['version1'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 4, 2));
  333. if ($ParsedArray['version1'] < 3) {
  334. return false;
  335. } elseif ($ParsedArray['version1'] == 3) {
  336. $ParsedArray['fourcc1'] = '.ra3';
  337. $ParsedArray['bits_per_sample'] = 16; // hard-coded for old versions?
  338. $ParsedArray['sample_rate'] = 8000; // hard-coded for old versions?
  339. $ParsedArray['header_size'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 6, 2));
  340. $ParsedArray['channels'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 8, 2)); // always 1 (?)
  341. //$ParsedArray['unknown1'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 10, 2));
  342. //$ParsedArray['unknown2'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 12, 2));
  343. //$ParsedArray['unknown3'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 14, 2));
  344. $ParsedArray['bytes_per_minute'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 16, 2));
  345. $ParsedArray['audio_bytes'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 18, 4));
  346. $ParsedArray['comments_raw'] = substr($OldRAheaderData, 22, $ParsedArray['header_size'] - 22 + 1); // not including null terminator
  347. $commentoffset = 0;
  348. $commentlength = getid3_lib::BigEndian2Int(substr($ParsedArray['comments_raw'], $commentoffset++, 1));
  349. $ParsedArray['comments']['title'][] = substr($ParsedArray['comments_raw'], $commentoffset, $commentlength);
  350. $commentoffset += $commentlength;
  351. $commentlength = getid3_lib::BigEndian2Int(substr($ParsedArray['comments_raw'], $commentoffset++, 1));
  352. $ParsedArray['comments']['artist'][] = substr($ParsedArray['comments_raw'], $commentoffset, $commentlength);
  353. $commentoffset += $commentlength;
  354. $commentlength = getid3_lib::BigEndian2Int(substr($ParsedArray['comments_raw'], $commentoffset++, 1));
  355. $ParsedArray['comments']['copyright'][] = substr($ParsedArray['comments_raw'], $commentoffset, $commentlength);
  356. $commentoffset += $commentlength;
  357. $commentoffset++; // final null terminator (?)
  358. $commentoffset++; // fourcc length (?) should be 4
  359. $ParsedArray['fourcc'] = substr($OldRAheaderData, 23 + $commentoffset, 4);
  360. } elseif ($ParsedArray['version1'] <= 5) {
  361. //$ParsedArray['unknown1'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 6, 2));
  362. $ParsedArray['fourcc1'] = substr($OldRAheaderData, 8, 4);
  363. $ParsedArray['file_size'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 12, 4));
  364. $ParsedArray['version2'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 16, 2));
  365. $ParsedArray['header_size'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 18, 4));
  366. $ParsedArray['codec_flavor_id'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 22, 2));
  367. $ParsedArray['coded_frame_size'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 24, 4));
  368. $ParsedArray['audio_bytes'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 28, 4));
  369. $ParsedArray['bytes_per_minute'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 32, 4));
  370. //$ParsedArray['unknown5'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 36, 4));
  371. $ParsedArray['sub_packet_h'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 40, 2));
  372. $ParsedArray['frame_size'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 42, 2));
  373. $ParsedArray['sub_packet_size'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 44, 2));
  374. //$ParsedArray['unknown6'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 46, 2));
  375. switch ($ParsedArray['version1']) {
  376. case 4:
  377. $ParsedArray['sample_rate'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 48, 2));
  378. //$ParsedArray['unknown8'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 50, 2));
  379. $ParsedArray['bits_per_sample'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 52, 2));
  380. $ParsedArray['channels'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 54, 2));
  381. $ParsedArray['length_fourcc2'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 56, 1));
  382. $ParsedArray['fourcc2'] = substr($OldRAheaderData, 57, 4);
  383. $ParsedArray['length_fourcc3'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 61, 1));
  384. $ParsedArray['fourcc3'] = substr($OldRAheaderData, 62, 4);
  385. //$ParsedArray['unknown9'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 66, 1));
  386. //$ParsedArray['unknown10'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 67, 2));
  387. $ParsedArray['comments_raw'] = substr($OldRAheaderData, 69, $ParsedArray['header_size'] - 69 + 16);
  388. $commentoffset = 0;
  389. $commentlength = getid3_lib::BigEndian2Int(substr($ParsedArray['comments_raw'], $commentoffset++, 1));
  390. $ParsedArray['comments']['title'][] = substr($ParsedArray['comments_raw'], $commentoffset, $commentlength);
  391. $commentoffset += $commentlength;
  392. $commentlength = getid3_lib::BigEndian2Int(substr($ParsedArray['comments_raw'], $commentoffset++, 1));
  393. $ParsedArray['comments']['artist'][] = substr($ParsedArray['comments_raw'], $commentoffset, $commentlength);
  394. $commentoffset += $commentlength;
  395. $commentlength = getid3_lib::BigEndian2Int(substr($ParsedArray['comments_raw'], $commentoffset++, 1));
  396. $ParsedArray['comments']['copyright'][] = substr($ParsedArray['comments_raw'], $commentoffset, $commentlength);
  397. $commentoffset += $commentlength;
  398. break;
  399. case 5:
  400. $ParsedArray['sample_rate'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 48, 4));
  401. $ParsedArray['sample_rate2'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 52, 4));
  402. $ParsedArray['bits_per_sample'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 56, 4));
  403. $ParsedArray['channels'] = getid3_lib::BigEndian2Int(substr($OldRAheaderData, 60, 2));
  404. $ParsedArray['genr'] = substr($OldRAheaderData, 62, 4);
  405. $ParsedArray['fourcc3'] = substr($OldRAheaderData, 66, 4);
  406. $ParsedArray['comments'] = array();
  407. break;
  408. }
  409. $ParsedArray['fourcc'] = $ParsedArray['fourcc3'];
  410. }
  411. /** @var string[]|false[] $value */
  412. foreach ($ParsedArray['comments'] as $key => $value) {
  413. if ($value[0] === false) {
  414. $ParsedArray['comments'][$key][0] = '';
  415. }
  416. }
  417. return true;
  418. }
  419. /**
  420. * @param string $fourcc
  421. * @param int $bitrate
  422. *
  423. * @return string
  424. */
  425. public function RealAudioCodecFourCClookup($fourcc, $bitrate) {
  426. static $RealAudioCodecFourCClookup = array();
  427. if (empty($RealAudioCodecFourCClookup)) {
  428. // http://www.its.msstate.edu/net/real/reports/config/tags.stats
  429. // http://www.freelists.org/archives/matroska-devel/06-2003/fullthread18.html
  430. $RealAudioCodecFourCClookup['14_4'][8000] = 'RealAudio v2 (14.4kbps)';
  431. $RealAudioCodecFourCClookup['14.4'][8000] = 'RealAudio v2 (14.4kbps)';
  432. $RealAudioCodecFourCClookup['lpcJ'][8000] = 'RealAudio v2 (14.4kbps)';
  433. $RealAudioCodecFourCClookup['28_8'][15200] = 'RealAudio v2 (28.8kbps)';
  434. $RealAudioCodecFourCClookup['28.8'][15200] = 'RealAudio v2 (28.8kbps)';
  435. $RealAudioCodecFourCClookup['sipr'][4933] = 'RealAudio v4 (5kbps Voice)';
  436. $RealAudioCodecFourCClookup['sipr'][6444] = 'RealAudio v4 (6.5kbps Voice)';
  437. $RealAudioCodecFourCClookup['sipr'][8444] = 'RealAudio v4 (8.5kbps Voice)';
  438. $RealAudioCodecFourCClookup['sipr'][16000] = 'RealAudio v4 (16kbps Wideband)';
  439. $RealAudioCodecFourCClookup['dnet'][8000] = 'RealAudio v3 (8kbps Music)';
  440. $RealAudioCodecFourCClookup['dnet'][16000] = 'RealAudio v3 (16kbps Music Low Response)';
  441. $RealAudioCodecFourCClookup['dnet'][15963] = 'RealAudio v3 (16kbps Music Mid/High Response)';
  442. $RealAudioCodecFourCClookup['dnet'][20000] = 'RealAudio v3 (20kbps Music Stereo)';
  443. $RealAudioCodecFourCClookup['dnet'][32000] = 'RealAudio v3 (32kbps Music Mono)';
  444. $RealAudioCodecFourCClookup['dnet'][31951] = 'RealAudio v3 (32kbps Music Stereo)';
  445. $RealAudioCodecFourCClookup['dnet'][39965] = 'RealAudio v3 (40kbps Music Mono)';
  446. $RealAudioCodecFourCClookup['dnet'][40000] = 'RealAudio v3 (40kbps Music Stereo)';
  447. $RealAudioCodecFourCClookup['dnet'][79947] = 'RealAudio v3 (80kbps Music Mono)';
  448. $RealAudioCodecFourCClookup['dnet'][80000] = 'RealAudio v3 (80kbps Music Stereo)';
  449. $RealAudioCodecFourCClookup['dnet'][0] = 'RealAudio v3';
  450. $RealAudioCodecFourCClookup['sipr'][0] = 'RealAudio v4';
  451. $RealAudioCodecFourCClookup['cook'][0] = 'RealAudio G2';
  452. $RealAudioCodecFourCClookup['atrc'][0] = 'RealAudio 8';
  453. }
  454. $roundbitrate = intval(round($bitrate));
  455. if (isset($RealAudioCodecFourCClookup[$fourcc][$roundbitrate])) {
  456. return $RealAudioCodecFourCClookup[$fourcc][$roundbitrate];
  457. } elseif (isset($RealAudioCodecFourCClookup[$fourcc][0])) {
  458. return $RealAudioCodecFourCClookup[$fourcc][0];
  459. }
  460. return $fourcc;
  461. }
  462. }