module.audio-video.real.php 32 KB

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