module.audio-video.real.php 32 KB

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