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