PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Vendor/getid3/module.audio-video.riff.php

https://bitbucket.org/nova-atlantis/simple-server-media-player
PHP | 2586 lines | 1541 code | 270 blank | 775 comment | 292 complexity | ee82540f026662197f7003474fd92de2 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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.riff.php //
  12. // module for analyzing RIFF files //
  13. // multiple formats supported by this module: //
  14. // Wave, AVI, AIFF/AIFC, (MP3,AC3)/RIFF, Wavpack v3, 8SVX //
  15. // dependencies: module.audio.mp3.php //
  16. // module.audio.ac3.php //
  17. // module.audio.dts.php //
  18. // ///
  19. /////////////////////////////////////////////////////////////////
  20. /**
  21. * @todo Parse AC-3/DTS audio inside WAVE correctly
  22. * @todo Rewrite RIFF parser totally
  23. */
  24. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.mp3.php', __FILE__, true);
  25. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ac3.php', __FILE__, true);
  26. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.dts.php', __FILE__, true);
  27. class getid3_riff extends getid3_handler {
  28. protected $container = 'riff'; // default
  29. public function Analyze() {
  30. $info = &$this->getid3->info;
  31. // initialize these values to an empty array, otherwise they default to NULL
  32. // and you can't append array values to a NULL value
  33. $info['riff'] = array('raw'=>array());
  34. // Shortcuts
  35. $thisfile_riff = &$info['riff'];
  36. $thisfile_riff_raw = &$thisfile_riff['raw'];
  37. $thisfile_audio = &$info['audio'];
  38. $thisfile_video = &$info['video'];
  39. $thisfile_audio_dataformat = &$thisfile_audio['dataformat'];
  40. $thisfile_riff_audio = &$thisfile_riff['audio'];
  41. $thisfile_riff_video = &$thisfile_riff['video'];
  42. $Original['avdataoffset'] = $info['avdataoffset'];
  43. $Original['avdataend'] = $info['avdataend'];
  44. $this->fseek($info['avdataoffset']);
  45. $RIFFheader = $this->fread(12);
  46. $offset = $this->ftell();
  47. $RIFFtype = substr($RIFFheader, 0, 4);
  48. $RIFFsize = substr($RIFFheader, 4, 4);
  49. $RIFFsubtype = substr($RIFFheader, 8, 4);
  50. switch ($RIFFtype) {
  51. case 'FORM': // AIFF, AIFC
  52. //$info['fileformat'] = 'aiff';
  53. $this->container = 'aiff';
  54. $thisfile_riff['header_size'] = $this->EitherEndian2Int($RIFFsize);
  55. $thisfile_riff[$RIFFsubtype] = $this->ParseRIFF($offset, ($offset + $thisfile_riff['header_size'] - 4));
  56. break;
  57. case 'RIFF': // AVI, WAV, etc
  58. case 'SDSS': // SDSS is identical to RIFF, just renamed. Used by SmartSound QuickTracks (www.smartsound.com)
  59. case 'RMP3': // RMP3 is identical to RIFF, just renamed. Used by [unknown program] when creating RIFF-MP3s
  60. //$info['fileformat'] = 'riff';
  61. $this->container = 'riff';
  62. $thisfile_riff['header_size'] = $this->EitherEndian2Int($RIFFsize);
  63. if ($RIFFsubtype == 'RMP3') {
  64. // RMP3 is identical to WAVE, just renamed. Used by [unknown program] when creating RIFF-MP3s
  65. $RIFFsubtype = 'WAVE';
  66. }
  67. if ($RIFFsubtype != 'AMV ') {
  68. // AMV files are RIFF-AVI files with parts of the spec deliberately broken, such as chunk size fields hardcoded to zero (because players known in hardware that these fields are always a certain size
  69. // Handled separately in ParseRIFFAMV()
  70. $thisfile_riff[$RIFFsubtype] = $this->ParseRIFF($offset, ($offset + $thisfile_riff['header_size'] - 4));
  71. }
  72. if (($info['avdataend'] - $info['filesize']) == 1) {
  73. // LiteWave appears to incorrectly *not* pad actual output file
  74. // to nearest WORD boundary so may appear to be short by one
  75. // byte, in which case - skip warning
  76. $info['avdataend'] = $info['filesize'];
  77. }
  78. $nextRIFFoffset = $Original['avdataoffset'] + 8 + $thisfile_riff['header_size']; // 8 = "RIFF" + 32-bit offset
  79. while ($nextRIFFoffset < min($info['filesize'], $info['avdataend'])) {
  80. try {
  81. $this->fseek($nextRIFFoffset);
  82. } catch (getid3_exception $e) {
  83. if ($e->getCode() == 10) {
  84. //$this->warning('RIFF parser: '.$e->getMessage());
  85. $this->error('AVI extends beyond '.round(PHP_INT_MAX / 1073741824).'GB and PHP filesystem functions cannot read that far, playtime may be wrong');
  86. $this->warning('[avdataend] value may be incorrect, multiple AVIX chunks may be present');
  87. break;
  88. } else {
  89. throw $e;
  90. }
  91. }
  92. $nextRIFFheader = $this->fread(12);
  93. if ($nextRIFFoffset == ($info['avdataend'] - 1)) {
  94. if (substr($nextRIFFheader, 0, 1) == "\x00") {
  95. // RIFF padded to WORD boundary, we're actually already at the end
  96. break;
  97. }
  98. }
  99. $nextRIFFheaderID = substr($nextRIFFheader, 0, 4);
  100. $nextRIFFsize = $this->EitherEndian2Int(substr($nextRIFFheader, 4, 4));
  101. $nextRIFFtype = substr($nextRIFFheader, 8, 4);
  102. $chunkdata = array();
  103. $chunkdata['offset'] = $nextRIFFoffset + 8;
  104. $chunkdata['size'] = $nextRIFFsize;
  105. $nextRIFFoffset = $chunkdata['offset'] + $chunkdata['size'];
  106. switch ($nextRIFFheaderID) {
  107. case 'RIFF':
  108. $chunkdata['chunks'] = $this->ParseRIFF($chunkdata['offset'] + 4, $nextRIFFoffset);
  109. if (!isset($thisfile_riff[$nextRIFFtype])) {
  110. $thisfile_riff[$nextRIFFtype] = array();
  111. }
  112. $thisfile_riff[$nextRIFFtype][] = $chunkdata;
  113. break;
  114. case 'AMV ':
  115. unset($info['riff']);
  116. $info['amv'] = $this->ParseRIFFAMV($chunkdata['offset'] + 4, $nextRIFFoffset);
  117. break;
  118. case 'JUNK':
  119. // ignore
  120. $thisfile_riff[$nextRIFFheaderID][] = $chunkdata;
  121. break;
  122. case 'IDVX':
  123. $info['divxtag']['comments'] = self::ParseDIVXTAG($this->fread($chunkdata['size']));
  124. break;
  125. default:
  126. if ($info['filesize'] == ($chunkdata['offset'] - 8 + 128)) {
  127. $DIVXTAG = $nextRIFFheader.$this->fread(128 - 12);
  128. if (substr($DIVXTAG, -7) == 'DIVXTAG') {
  129. // DIVXTAG is supposed to be inside an IDVX chunk in a LIST chunk, but some bad encoders just slap it on the end of a file
  130. $this->warning('Found wrongly-structured DIVXTAG at offset '.($this->ftell() - 128).', parsing anyway');
  131. $info['divxtag']['comments'] = self::ParseDIVXTAG($DIVXTAG);
  132. break 2;
  133. }
  134. }
  135. $this->warning('Expecting "RIFF|JUNK|IDVX" at '.$nextRIFFoffset.', found "'.$nextRIFFheaderID.'" ('.getid3_lib::PrintHexBytes($nextRIFFheaderID).') - skipping rest of file');
  136. break 2;
  137. }
  138. }
  139. if ($RIFFsubtype == 'WAVE') {
  140. $thisfile_riff_WAVE = &$thisfile_riff['WAVE'];
  141. }
  142. break;
  143. default:
  144. $this->error('Cannot parse RIFF (this is maybe not a RIFF / WAV / AVI file?) - expecting "FORM|RIFF|SDSS|RMP3" found "'.$RIFFsubtype.'" instead');
  145. //unset($info['fileformat']);
  146. return false;
  147. }
  148. $streamindex = 0;
  149. switch ($RIFFsubtype) {
  150. // http://en.wikipedia.org/wiki/Wav
  151. case 'WAVE':
  152. $info['fileformat'] = 'wav';
  153. if (empty($thisfile_audio['bitrate_mode'])) {
  154. $thisfile_audio['bitrate_mode'] = 'cbr';
  155. }
  156. if (empty($thisfile_audio_dataformat)) {
  157. $thisfile_audio_dataformat = 'wav';
  158. }
  159. if (isset($thisfile_riff_WAVE['data'][0]['offset'])) {
  160. $info['avdataoffset'] = $thisfile_riff_WAVE['data'][0]['offset'] + 8;
  161. $info['avdataend'] = $info['avdataoffset'] + $thisfile_riff_WAVE['data'][0]['size'];
  162. }
  163. if (isset($thisfile_riff_WAVE['fmt '][0]['data'])) {
  164. $thisfile_riff_audio[$streamindex] = self::parseWAVEFORMATex($thisfile_riff_WAVE['fmt '][0]['data']);
  165. $thisfile_audio['wformattag'] = $thisfile_riff_audio[$streamindex]['raw']['wFormatTag'];
  166. if (!isset($thisfile_riff_audio[$streamindex]['bitrate']) || ($thisfile_riff_audio[$streamindex]['bitrate'] == 0)) {
  167. $info['error'][] = 'Corrupt RIFF file: bitrate_audio == zero';
  168. return false;
  169. }
  170. $thisfile_riff_raw['fmt '] = $thisfile_riff_audio[$streamindex]['raw'];
  171. unset($thisfile_riff_audio[$streamindex]['raw']);
  172. $thisfile_audio['streams'][$streamindex] = $thisfile_riff_audio[$streamindex];
  173. $thisfile_audio = getid3_lib::array_merge_noclobber($thisfile_audio, $thisfile_riff_audio[$streamindex]);
  174. if (substr($thisfile_audio['codec'], 0, strlen('unknown: 0x')) == 'unknown: 0x') {
  175. $info['warning'][] = 'Audio codec = '.$thisfile_audio['codec'];
  176. }
  177. $thisfile_audio['bitrate'] = $thisfile_riff_audio[$streamindex]['bitrate'];
  178. if (empty($info['playtime_seconds'])) { // may already be set (e.g. DTS-WAV)
  179. $info['playtime_seconds'] = (float) ((($info['avdataend'] - $info['avdataoffset']) * 8) / $thisfile_audio['bitrate']);
  180. }
  181. $thisfile_audio['lossless'] = false;
  182. if (isset($thisfile_riff_WAVE['data'][0]['offset']) && isset($thisfile_riff_raw['fmt ']['wFormatTag'])) {
  183. switch ($thisfile_riff_raw['fmt ']['wFormatTag']) {
  184. case 0x0001: // PCM
  185. $thisfile_audio['lossless'] = true;
  186. break;
  187. case 0x2000: // AC-3
  188. $thisfile_audio_dataformat = 'ac3';
  189. break;
  190. default:
  191. // do nothing
  192. break;
  193. }
  194. }
  195. $thisfile_audio['streams'][$streamindex]['wformattag'] = $thisfile_audio['wformattag'];
  196. $thisfile_audio['streams'][$streamindex]['bitrate_mode'] = $thisfile_audio['bitrate_mode'];
  197. $thisfile_audio['streams'][$streamindex]['lossless'] = $thisfile_audio['lossless'];
  198. $thisfile_audio['streams'][$streamindex]['dataformat'] = $thisfile_audio_dataformat;
  199. }
  200. if (isset($thisfile_riff_WAVE['rgad'][0]['data'])) {
  201. // shortcuts
  202. $rgadData = &$thisfile_riff_WAVE['rgad'][0]['data'];
  203. $thisfile_riff_raw['rgad'] = array('track'=>array(), 'album'=>array());
  204. $thisfile_riff_raw_rgad = &$thisfile_riff_raw['rgad'];
  205. $thisfile_riff_raw_rgad_track = &$thisfile_riff_raw_rgad['track'];
  206. $thisfile_riff_raw_rgad_album = &$thisfile_riff_raw_rgad['album'];
  207. $thisfile_riff_raw_rgad['fPeakAmplitude'] = getid3_lib::LittleEndian2Float(substr($rgadData, 0, 4));
  208. $thisfile_riff_raw_rgad['nRadioRgAdjust'] = $this->EitherEndian2Int(substr($rgadData, 4, 2));
  209. $thisfile_riff_raw_rgad['nAudiophileRgAdjust'] = $this->EitherEndian2Int(substr($rgadData, 6, 2));
  210. $nRadioRgAdjustBitstring = str_pad(getid3_lib::Dec2Bin($thisfile_riff_raw_rgad['nRadioRgAdjust']), 16, '0', STR_PAD_LEFT);
  211. $nAudiophileRgAdjustBitstring = str_pad(getid3_lib::Dec2Bin($thisfile_riff_raw_rgad['nAudiophileRgAdjust']), 16, '0', STR_PAD_LEFT);
  212. $thisfile_riff_raw_rgad_track['name'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 0, 3));
  213. $thisfile_riff_raw_rgad_track['originator'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 3, 3));
  214. $thisfile_riff_raw_rgad_track['signbit'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 6, 1));
  215. $thisfile_riff_raw_rgad_track['adjustment'] = getid3_lib::Bin2Dec(substr($nRadioRgAdjustBitstring, 7, 9));
  216. $thisfile_riff_raw_rgad_album['name'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 0, 3));
  217. $thisfile_riff_raw_rgad_album['originator'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 3, 3));
  218. $thisfile_riff_raw_rgad_album['signbit'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 6, 1));
  219. $thisfile_riff_raw_rgad_album['adjustment'] = getid3_lib::Bin2Dec(substr($nAudiophileRgAdjustBitstring, 7, 9));
  220. $thisfile_riff['rgad']['peakamplitude'] = $thisfile_riff_raw_rgad['fPeakAmplitude'];
  221. if (($thisfile_riff_raw_rgad_track['name'] != 0) && ($thisfile_riff_raw_rgad_track['originator'] != 0)) {
  222. $thisfile_riff['rgad']['track']['name'] = getid3_lib::RGADnameLookup($thisfile_riff_raw_rgad_track['name']);
  223. $thisfile_riff['rgad']['track']['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_riff_raw_rgad_track['originator']);
  224. $thisfile_riff['rgad']['track']['adjustment'] = getid3_lib::RGADadjustmentLookup($thisfile_riff_raw_rgad_track['adjustment'], $thisfile_riff_raw_rgad_track['signbit']);
  225. }
  226. if (($thisfile_riff_raw_rgad_album['name'] != 0) && ($thisfile_riff_raw_rgad_album['originator'] != 0)) {
  227. $thisfile_riff['rgad']['album']['name'] = getid3_lib::RGADnameLookup($thisfile_riff_raw_rgad_album['name']);
  228. $thisfile_riff['rgad']['album']['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_riff_raw_rgad_album['originator']);
  229. $thisfile_riff['rgad']['album']['adjustment'] = getid3_lib::RGADadjustmentLookup($thisfile_riff_raw_rgad_album['adjustment'], $thisfile_riff_raw_rgad_album['signbit']);
  230. }
  231. }
  232. if (isset($thisfile_riff_WAVE['fact'][0]['data'])) {
  233. $thisfile_riff_raw['fact']['NumberOfSamples'] = $this->EitherEndian2Int(substr($thisfile_riff_WAVE['fact'][0]['data'], 0, 4));
  234. // This should be a good way of calculating exact playtime,
  235. // but some sample files have had incorrect number of samples,
  236. // so cannot use this method
  237. // if (!empty($thisfile_riff_raw['fmt ']['nSamplesPerSec'])) {
  238. // $info['playtime_seconds'] = (float) $thisfile_riff_raw['fact']['NumberOfSamples'] / $thisfile_riff_raw['fmt ']['nSamplesPerSec'];
  239. // }
  240. }
  241. if (!empty($thisfile_riff_raw['fmt ']['nAvgBytesPerSec'])) {
  242. $thisfile_audio['bitrate'] = getid3_lib::CastAsInt($thisfile_riff_raw['fmt ']['nAvgBytesPerSec'] * 8);
  243. }
  244. if (isset($thisfile_riff_WAVE['bext'][0]['data'])) {
  245. // shortcut
  246. $thisfile_riff_WAVE_bext_0 = &$thisfile_riff_WAVE['bext'][0];
  247. $thisfile_riff_WAVE_bext_0['title'] = trim(substr($thisfile_riff_WAVE_bext_0['data'], 0, 256));
  248. $thisfile_riff_WAVE_bext_0['author'] = trim(substr($thisfile_riff_WAVE_bext_0['data'], 256, 32));
  249. $thisfile_riff_WAVE_bext_0['reference'] = trim(substr($thisfile_riff_WAVE_bext_0['data'], 288, 32));
  250. $thisfile_riff_WAVE_bext_0['origin_date'] = substr($thisfile_riff_WAVE_bext_0['data'], 320, 10);
  251. $thisfile_riff_WAVE_bext_0['origin_time'] = substr($thisfile_riff_WAVE_bext_0['data'], 330, 8);
  252. $thisfile_riff_WAVE_bext_0['time_reference'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_bext_0['data'], 338, 8));
  253. $thisfile_riff_WAVE_bext_0['bwf_version'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_bext_0['data'], 346, 1));
  254. $thisfile_riff_WAVE_bext_0['reserved'] = substr($thisfile_riff_WAVE_bext_0['data'], 347, 254);
  255. $thisfile_riff_WAVE_bext_0['coding_history'] = explode("\r\n", trim(substr($thisfile_riff_WAVE_bext_0['data'], 601)));
  256. if (preg_match('#^([0-9]{4}).([0-9]{2}).([0-9]{2})$#', $thisfile_riff_WAVE_bext_0['origin_date'], $matches_bext_date)) {
  257. if (preg_match('#^([0-9]{2}).([0-9]{2}).([0-9]{2})$#', $thisfile_riff_WAVE_bext_0['origin_time'], $matches_bext_time)) {
  258. list($dummy, $bext_timestamp['year'], $bext_timestamp['month'], $bext_timestamp['day']) = $matches_bext_date;
  259. list($dummy, $bext_timestamp['hour'], $bext_timestamp['minute'], $bext_timestamp['second']) = $matches_bext_time;
  260. $thisfile_riff_WAVE_bext_0['origin_date_unix'] = gmmktime($bext_timestamp['hour'], $bext_timestamp['minute'], $bext_timestamp['second'], $bext_timestamp['month'], $bext_timestamp['day'], $bext_timestamp['year']);
  261. } else {
  262. $info['warning'][] = 'RIFF.WAVE.BEXT.origin_time is invalid';
  263. }
  264. } else {
  265. $info['warning'][] = 'RIFF.WAVE.BEXT.origin_date is invalid';
  266. }
  267. $thisfile_riff['comments']['author'][] = $thisfile_riff_WAVE_bext_0['author'];
  268. $thisfile_riff['comments']['title'][] = $thisfile_riff_WAVE_bext_0['title'];
  269. }
  270. if (isset($thisfile_riff_WAVE['MEXT'][0]['data'])) {
  271. // shortcut
  272. $thisfile_riff_WAVE_MEXT_0 = &$thisfile_riff_WAVE['MEXT'][0];
  273. $thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 0, 2));
  274. $thisfile_riff_WAVE_MEXT_0['flags']['homogenous'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0001);
  275. if ($thisfile_riff_WAVE_MEXT_0['flags']['homogenous']) {
  276. $thisfile_riff_WAVE_MEXT_0['flags']['padding'] = ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0002) ? false : true;
  277. $thisfile_riff_WAVE_MEXT_0['flags']['22_or_44'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0004);
  278. $thisfile_riff_WAVE_MEXT_0['flags']['free_format'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['sound_information'] & 0x0008);
  279. $thisfile_riff_WAVE_MEXT_0['nominal_frame_size'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 2, 2));
  280. }
  281. $thisfile_riff_WAVE_MEXT_0['anciliary_data_length'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 6, 2));
  282. $thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_MEXT_0['data'], 8, 2));
  283. $thisfile_riff_WAVE_MEXT_0['flags']['anciliary_data_left'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] & 0x0001);
  284. $thisfile_riff_WAVE_MEXT_0['flags']['anciliary_data_free'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] & 0x0002);
  285. $thisfile_riff_WAVE_MEXT_0['flags']['anciliary_data_right'] = (bool) ($thisfile_riff_WAVE_MEXT_0['raw']['anciliary_data_def'] & 0x0004);
  286. }
  287. if (isset($thisfile_riff_WAVE['cart'][0]['data'])) {
  288. // shortcut
  289. $thisfile_riff_WAVE_cart_0 = &$thisfile_riff_WAVE['cart'][0];
  290. $thisfile_riff_WAVE_cart_0['version'] = substr($thisfile_riff_WAVE_cart_0['data'], 0, 4);
  291. $thisfile_riff_WAVE_cart_0['title'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 4, 64));
  292. $thisfile_riff_WAVE_cart_0['artist'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 68, 64));
  293. $thisfile_riff_WAVE_cart_0['cut_id'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 132, 64));
  294. $thisfile_riff_WAVE_cart_0['client_id'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 196, 64));
  295. $thisfile_riff_WAVE_cart_0['category'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 260, 64));
  296. $thisfile_riff_WAVE_cart_0['classification'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 324, 64));
  297. $thisfile_riff_WAVE_cart_0['out_cue'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 388, 64));
  298. $thisfile_riff_WAVE_cart_0['start_date'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 452, 10));
  299. $thisfile_riff_WAVE_cart_0['start_time'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 462, 8));
  300. $thisfile_riff_WAVE_cart_0['end_date'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 470, 10));
  301. $thisfile_riff_WAVE_cart_0['end_time'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 480, 8));
  302. $thisfile_riff_WAVE_cart_0['producer_app_id'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 488, 64));
  303. $thisfile_riff_WAVE_cart_0['producer_app_version'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 552, 64));
  304. $thisfile_riff_WAVE_cart_0['user_defined_text'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 616, 64));
  305. $thisfile_riff_WAVE_cart_0['zero_db_reference'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_cart_0['data'], 680, 4), true);
  306. for ($i = 0; $i < 8; $i++) {
  307. $thisfile_riff_WAVE_cart_0['post_time'][$i]['usage_fourcc'] = substr($thisfile_riff_WAVE_cart_0['data'], 684 + ($i * 8), 4);
  308. $thisfile_riff_WAVE_cart_0['post_time'][$i]['timer_value'] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE_cart_0['data'], 684 + ($i * 8) + 4, 4));
  309. }
  310. $thisfile_riff_WAVE_cart_0['url'] = trim(substr($thisfile_riff_WAVE_cart_0['data'], 748, 1024));
  311. $thisfile_riff_WAVE_cart_0['tag_text'] = explode("\r\n", trim(substr($thisfile_riff_WAVE_cart_0['data'], 1772)));
  312. $thisfile_riff['comments']['artist'][] = $thisfile_riff_WAVE_cart_0['artist'];
  313. $thisfile_riff['comments']['title'][] = $thisfile_riff_WAVE_cart_0['title'];
  314. }
  315. if (isset($thisfile_riff_WAVE['SNDM'][0]['data'])) {
  316. // SoundMiner metadata
  317. // shortcuts
  318. $thisfile_riff_WAVE_SNDM_0 = &$thisfile_riff_WAVE['SNDM'][0];
  319. $thisfile_riff_WAVE_SNDM_0_data = &$thisfile_riff_WAVE_SNDM_0['data'];
  320. $SNDM_startoffset = 0;
  321. $SNDM_endoffset = $thisfile_riff_WAVE_SNDM_0['size'];
  322. while ($SNDM_startoffset < $SNDM_endoffset) {
  323. $SNDM_thisTagOffset = 0;
  324. $SNDM_thisTagSize = getid3_lib::BigEndian2Int(substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 4));
  325. $SNDM_thisTagOffset += 4;
  326. $SNDM_thisTagKey = substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 4);
  327. $SNDM_thisTagOffset += 4;
  328. $SNDM_thisTagDataSize = getid3_lib::BigEndian2Int(substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 2));
  329. $SNDM_thisTagOffset += 2;
  330. $SNDM_thisTagDataFlags = getid3_lib::BigEndian2Int(substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, 2));
  331. $SNDM_thisTagOffset += 2;
  332. $SNDM_thisTagDataText = substr($thisfile_riff_WAVE_SNDM_0_data, $SNDM_startoffset + $SNDM_thisTagOffset, $SNDM_thisTagDataSize);
  333. $SNDM_thisTagOffset += $SNDM_thisTagDataSize;
  334. if ($SNDM_thisTagSize != (4 + 4 + 2 + 2 + $SNDM_thisTagDataSize)) {
  335. $info['warning'][] = 'RIFF.WAVE.SNDM.data contains tag not expected length (expected: '.$SNDM_thisTagSize.', found: '.(4 + 4 + 2 + 2 + $SNDM_thisTagDataSize).') at offset '.$SNDM_startoffset.' (file offset '.($thisfile_riff_WAVE_SNDM_0['offset'] + $SNDM_startoffset).')';
  336. break;
  337. } elseif ($SNDM_thisTagSize <= 0) {
  338. $info['warning'][] = 'RIFF.WAVE.SNDM.data contains zero-size tag at offset '.$SNDM_startoffset.' (file offset '.($thisfile_riff_WAVE_SNDM_0['offset'] + $SNDM_startoffset).')';
  339. break;
  340. }
  341. $SNDM_startoffset += $SNDM_thisTagSize;
  342. $thisfile_riff_WAVE_SNDM_0['parsed_raw'][$SNDM_thisTagKey] = $SNDM_thisTagDataText;
  343. if ($parsedkey = self::waveSNDMtagLookup($SNDM_thisTagKey)) {
  344. $thisfile_riff_WAVE_SNDM_0['parsed'][$parsedkey] = $SNDM_thisTagDataText;
  345. } else {
  346. $info['warning'][] = 'RIFF.WAVE.SNDM contains unknown tag "'.$SNDM_thisTagKey.'" at offset '.$SNDM_startoffset.' (file offset '.($thisfile_riff_WAVE_SNDM_0['offset'] + $SNDM_startoffset).')';
  347. }
  348. }
  349. $tagmapping = array(
  350. 'tracktitle'=>'title',
  351. 'category' =>'genre',
  352. 'cdtitle' =>'album',
  353. 'tracktitle'=>'title',
  354. );
  355. foreach ($tagmapping as $fromkey => $tokey) {
  356. if (isset($thisfile_riff_WAVE_SNDM_0['parsed'][$fromkey])) {
  357. $thisfile_riff['comments'][$tokey][] = $thisfile_riff_WAVE_SNDM_0['parsed'][$fromkey];
  358. }
  359. }
  360. }
  361. if (isset($thisfile_riff_WAVE['iXML'][0]['data'])) {
  362. // requires functions simplexml_load_string and get_object_vars
  363. if ($parsedXML = getid3_lib::XML2array($thisfile_riff_WAVE['iXML'][0]['data'])) {
  364. $thisfile_riff_WAVE['iXML'][0]['parsed'] = $parsedXML;
  365. if (isset($parsedXML['SPEED']['MASTER_SPEED'])) {
  366. @list($numerator, $denominator) = explode('/', $parsedXML['SPEED']['MASTER_SPEED']);
  367. $thisfile_riff_WAVE['iXML'][0]['master_speed'] = $numerator / ($denominator ? $denominator : 1000);
  368. }
  369. if (isset($parsedXML['SPEED']['TIMECODE_RATE'])) {
  370. @list($numerator, $denominator) = explode('/', $parsedXML['SPEED']['TIMECODE_RATE']);
  371. $thisfile_riff_WAVE['iXML'][0]['timecode_rate'] = $numerator / ($denominator ? $denominator : 1000);
  372. }
  373. if (isset($parsedXML['SPEED']['TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_LO']) && !empty($parsedXML['SPEED']['TIMESTAMP_SAMPLE_RATE']) && !empty($thisfile_riff_WAVE['iXML'][0]['timecode_rate'])) {
  374. $samples_since_midnight = floatval(ltrim($parsedXML['SPEED']['TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_HI'].$parsedXML['SPEED']['TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_LO'], '0'));
  375. $thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] = $samples_since_midnight / $parsedXML['SPEED']['TIMESTAMP_SAMPLE_RATE'];
  376. $h = floor( $thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] / 3600);
  377. $m = floor(($thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] - ($h * 3600)) / 60);
  378. $s = floor( $thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] - ($h * 3600) - ($m * 60));
  379. $f = ($thisfile_riff_WAVE['iXML'][0]['timecode_seconds'] - ($h * 3600) - ($m * 60) - $s) * $thisfile_riff_WAVE['iXML'][0]['timecode_rate'];
  380. $thisfile_riff_WAVE['iXML'][0]['timecode_string'] = sprintf('%02d:%02d:%02d:%05.2f', $h, $m, $s, $f);
  381. $thisfile_riff_WAVE['iXML'][0]['timecode_string_round'] = sprintf('%02d:%02d:%02d:%02d', $h, $m, $s, round($f));
  382. }
  383. unset($parsedXML);
  384. }
  385. }
  386. if (!isset($thisfile_audio['bitrate']) && isset($thisfile_riff_audio[$streamindex]['bitrate'])) {
  387. $thisfile_audio['bitrate'] = $thisfile_riff_audio[$streamindex]['bitrate'];
  388. $info['playtime_seconds'] = (float) ((($info['avdataend'] - $info['avdataoffset']) * 8) / $thisfile_audio['bitrate']);
  389. }
  390. if (!empty($info['wavpack'])) {
  391. $thisfile_audio_dataformat = 'wavpack';
  392. $thisfile_audio['bitrate_mode'] = 'vbr';
  393. $thisfile_audio['encoder'] = 'WavPack v'.$info['wavpack']['version'];
  394. // Reset to the way it was - RIFF parsing will have messed this up
  395. $info['avdataend'] = $Original['avdataend'];
  396. $thisfile_audio['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  397. $this->fseek($info['avdataoffset'] - 44);
  398. $RIFFdata = $this->fread(44);
  399. $OrignalRIFFheaderSize = getid3_lib::LittleEndian2Int(substr($RIFFdata, 4, 4)) + 8;
  400. $OrignalRIFFdataSize = getid3_lib::LittleEndian2Int(substr($RIFFdata, 40, 4)) + 44;
  401. if ($OrignalRIFFheaderSize > $OrignalRIFFdataSize) {
  402. $info['avdataend'] -= ($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
  403. $this->fseek($info['avdataend']);
  404. $RIFFdata .= $this->fread($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
  405. }
  406. // move the data chunk after all other chunks (if any)
  407. // so that the RIFF parser doesn't see EOF when trying
  408. // to skip over the data chunk
  409. $RIFFdata = substr($RIFFdata, 0, 36).substr($RIFFdata, 44).substr($RIFFdata, 36, 8);
  410. $getid3_riff = new getid3_riff($this->getid3);
  411. $getid3_riff->ParseRIFFdata($RIFFdata);
  412. unset($getid3_riff);
  413. }
  414. if (isset($thisfile_riff_raw['fmt ']['wFormatTag'])) {
  415. switch ($thisfile_riff_raw['fmt ']['wFormatTag']) {
  416. case 0x0001: // PCM
  417. if (!empty($info['ac3'])) {
  418. // Dolby Digital WAV files masquerade as PCM-WAV, but they're not
  419. $thisfile_audio['wformattag'] = 0x2000;
  420. $thisfile_audio['codec'] = self::wFormatTagLookup($thisfile_audio['wformattag']);
  421. $thisfile_audio['lossless'] = false;
  422. $thisfile_audio['bitrate'] = $info['ac3']['bitrate'];
  423. $thisfile_audio['sample_rate'] = $info['ac3']['sample_rate'];
  424. }
  425. if (!empty($info['dts'])) {
  426. // Dolby DTS files masquerade as PCM-WAV, but they're not
  427. $thisfile_audio['wformattag'] = 0x2001;
  428. $thisfile_audio['codec'] = self::wFormatTagLookup($thisfile_audio['wformattag']);
  429. $thisfile_audio['lossless'] = false;
  430. $thisfile_audio['bitrate'] = $info['dts']['bitrate'];
  431. $thisfile_audio['sample_rate'] = $info['dts']['sample_rate'];
  432. }
  433. break;
  434. case 0x08AE: // ClearJump LiteWave
  435. $thisfile_audio['bitrate_mode'] = 'vbr';
  436. $thisfile_audio_dataformat = 'litewave';
  437. //typedef struct tagSLwFormat {
  438. // WORD m_wCompFormat; // low byte defines compression method, high byte is compression flags
  439. // DWORD m_dwScale; // scale factor for lossy compression
  440. // DWORD m_dwBlockSize; // number of samples in encoded blocks
  441. // WORD m_wQuality; // alias for the scale factor
  442. // WORD m_wMarkDistance; // distance between marks in bytes
  443. // WORD m_wReserved;
  444. //
  445. // //following paramters are ignored if CF_FILESRC is not set
  446. // DWORD m_dwOrgSize; // original file size in bytes
  447. // WORD m_bFactExists; // indicates if 'fact' chunk exists in the original file
  448. // DWORD m_dwRiffChunkSize; // riff chunk size in the original file
  449. //
  450. // PCMWAVEFORMAT m_OrgWf; // original wave format
  451. // }SLwFormat, *PSLwFormat;
  452. // shortcut
  453. $thisfile_riff['litewave']['raw'] = array();
  454. $riff_litewave = &$thisfile_riff['litewave'];
  455. $riff_litewave_raw = &$riff_litewave['raw'];
  456. $flags = array(
  457. 'compression_method' => 1,
  458. 'compression_flags' => 1,
  459. 'm_dwScale' => 4,
  460. 'm_dwBlockSize' => 4,
  461. 'm_wQuality' => 2,
  462. 'm_wMarkDistance' => 2,
  463. 'm_wReserved' => 2,
  464. 'm_dwOrgSize' => 4,
  465. 'm_bFactExists' => 2,
  466. 'm_dwRiffChunkSize' => 4,
  467. );
  468. $litewave_offset = 18;
  469. foreach ($flags as $flag => $length) {
  470. $riff_litewave_raw[$flag] = getid3_lib::LittleEndian2Int(substr($thisfile_riff_WAVE['fmt '][0]['data'], $litewave_offset, $length));
  471. $litewave_offset += $length;
  472. }
  473. //$riff_litewave['quality_factor'] = intval(round((2000 - $riff_litewave_raw['m_dwScale']) / 20));
  474. $riff_litewave['quality_factor'] = $riff_litewave_raw['m_wQuality'];
  475. $riff_litewave['flags']['raw_source'] = ($riff_litewave_raw['compression_flags'] & 0x01) ? false : true;
  476. $riff_litewave['flags']['vbr_blocksize'] = ($riff_litewave_raw['compression_flags'] & 0x02) ? false : true;
  477. $riff_litewave['flags']['seekpoints'] = (bool) ($riff_litewave_raw['compression_flags'] & 0x04);
  478. $thisfile_audio['lossless'] = (($riff_litewave_raw['m_wQuality'] == 100) ? true : false);
  479. $thisfile_audio['encoder_options'] = '-q'.$riff_litewave['quality_factor'];
  480. break;
  481. default:
  482. break;
  483. }
  484. }
  485. if ($info['avdataend'] > $info['filesize']) {
  486. switch (!empty($thisfile_audio_dataformat) ? $thisfile_audio_dataformat : '') {
  487. case 'wavpack': // WavPack
  488. case 'lpac': // LPAC
  489. case 'ofr': // OptimFROG
  490. case 'ofs': // OptimFROG DualStream
  491. // lossless compressed audio formats that keep original RIFF headers - skip warning
  492. break;
  493. case 'litewave':
  494. if (($info['avdataend'] - $info['filesize']) == 1) {
  495. // LiteWave appears to incorrectly *not* pad actual output file
  496. // to nearest WORD boundary so may appear to be short by one
  497. // byte, in which case - skip warning
  498. } else {
  499. // Short by more than one byte, throw warning
  500. $info['warning'][] = 'Probably truncated file - expecting '.$thisfile_riff[$RIFFsubtype]['data'][0]['size'].' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' (short by '.($thisfile_riff[$RIFFsubtype]['data'][0]['size'] - ($info['filesize'] - $info['avdataoffset'])).' bytes)';
  501. $info['avdataend'] = $info['filesize'];
  502. }
  503. break;
  504. default:
  505. if ((($info['avdataend'] - $info['filesize']) == 1) && (($thisfile_riff[$RIFFsubtype]['data'][0]['size'] % 2) == 0) && ((($info['filesize'] - $info['avdataoffset']) % 2) == 1)) {
  506. // output file appears to be incorrectly *not* padded to nearest WORD boundary
  507. // Output less severe warning
  508. $info['warning'][] = 'File should probably be padded to nearest WORD boundary, but it is not (expecting '.$thisfile_riff[$RIFFsubtype]['data'][0]['size'].' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' therefore short by '.($thisfile_riff[$RIFFsubtype]['data'][0]['size'] - ($info['filesize'] - $info['avdataoffset'])).' bytes)';
  509. $info['avdataend'] = $info['filesize'];
  510. } else {
  511. // Short by more than one byte, throw warning
  512. $info['warning'][] = 'Probably truncated file - expecting '.$thisfile_riff[$RIFFsubtype]['data'][0]['size'].' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' (short by '.($thisfile_riff[$RIFFsubtype]['data'][0]['size'] - ($info['filesize'] - $info['avdataoffset'])).' bytes)';
  513. $info['avdataend'] = $info['filesize'];
  514. }
  515. break;
  516. }
  517. }
  518. if (!empty($info['mpeg']['audio']['LAME']['audio_bytes'])) {
  519. if ((($info['avdataend'] - $info['avdataoffset']) - $info['mpeg']['audio']['LAME']['audio_bytes']) == 1) {
  520. $info['avdataend']--;
  521. $info['warning'][] = 'Extra null byte at end of MP3 data assumed to be RIFF padding and therefore ignored';
  522. }
  523. }
  524. if (isset($thisfile_audio_dataformat) && ($thisfile_audio_dataformat == 'ac3')) {
  525. unset($thisfile_audio['bits_per_sample']);
  526. if (!empty($info['ac3']['bitrate']) && ($info['ac3']['bitrate'] != $thisfile_audio['bitrate'])) {
  527. $thisfile_audio['bitrate'] = $info['ac3']['bitrate'];
  528. }
  529. }
  530. break;
  531. // http://en.wikipedia.org/wiki/Audio_Video_Interleave
  532. case 'AVI ':
  533. $info['fileformat'] = 'avi';
  534. $info['mime_type'] = 'video/avi';
  535. $thisfile_video['bitrate_mode'] = 'vbr'; // maybe not, but probably
  536. $thisfile_video['dataformat'] = 'avi';
  537. if (isset($thisfile_riff[$RIFFsubtype]['movi']['offset'])) {
  538. $info['avdataoffset'] = $thisfile_riff[$RIFFsubtype]['movi']['offset'] + 8;
  539. if (isset($thisfile_riff['AVIX'])) {
  540. $info['avdataend'] = $thisfile_riff['AVIX'][(count($thisfile_riff['AVIX']) - 1)]['chunks']['movi']['offset'] + $thisfile_riff['AVIX'][(count($thisfile_riff['AVIX']) - 1)]['chunks']['movi']['size'];
  541. } else {
  542. $info['avdataend'] = $thisfile_riff['AVI ']['movi']['offset'] + $thisfile_riff['AVI ']['movi']['size'];
  543. }
  544. if ($info['avdataend'] > $info['filesize']) {
  545. $info['warning'][] = 'Probably truncated file - expecting '.($info['avdataend'] - $info['avdataoffset']).' bytes of data, only found '.($info['filesize'] - $info['avdataoffset']).' (short by '.($info['avdataend'] - $info['filesize']).' bytes)';
  546. $info['avdataend'] = $info['filesize'];
  547. }
  548. }
  549. if (isset($thisfile_riff['AVI ']['hdrl']['strl']['indx'])) {
  550. //$bIndexType = array(
  551. // 0x00 => 'AVI_INDEX_OF_INDEXES',
  552. // 0x01 => 'AVI_INDEX_OF_CHUNKS',
  553. // 0x80 => 'AVI_INDEX_IS_DATA',
  554. //);
  555. //$bIndexSubtype = array(
  556. // 0x01 => array(
  557. // 0x01 => 'AVI_INDEX_2FIELD',
  558. // ),
  559. //);
  560. foreach ($thisfile_riff['AVI ']['hdrl']['strl']['indx'] as $streamnumber => $steamdataarray) {
  561. $ahsisd = &$thisfile_riff['AVI ']['hdrl']['strl']['indx'][$streamnumber]['data'];
  562. $thisfile_riff_raw['indx'][$streamnumber]['wLongsPerEntry'] = $this->EitherEndian2Int(substr($ahsisd, 0, 2));
  563. $thisfile_riff_raw['indx'][$streamnumber]['bIndexSubType'] = $this->EitherEndian2Int(substr($ahsisd, 2, 1));
  564. $thisfile_riff_raw['indx'][$streamnumber]['bIndexType'] = $this->EitherEndian2Int(substr($ahsisd, 3, 1));
  565. $thisfile_riff_raw['indx'][$streamnumber]['nEntriesInUse'] = $this->EitherEndian2Int(substr($ahsisd, 4, 4));
  566. $thisfile_riff_raw['indx'][$streamnumber]['dwChunkId'] = substr($ahsisd, 8, 4);
  567. $thisfile_riff_raw['indx'][$streamnumber]['dwReserved'] = $this->EitherEndian2Int(substr($ahsisd, 12, 4));
  568. //$thisfile_riff_raw['indx'][$streamnumber]['bIndexType_name'] = $bIndexType[$thisfile_riff_raw['indx'][$streamnumber]['bIndexType']];
  569. //$thisfile_riff_raw['indx'][$streamnumber]['bIndexSubType_name'] = $bIndexSubtype[$thisfile_riff_raw['indx'][$streamnumber]['bIndexType']][$thisfile_riff_raw['indx'][$streamnumber]['bIndexSubType']];
  570. unset($ahsisd);
  571. }
  572. }
  573. if (isset($thisfile_riff['AVI ']['hdrl']['avih'][$streamindex]['data'])) {
  574. $avihData = $thisfile_riff['AVI ']['hdrl']['avih'][$streamindex]['data'];
  575. // shortcut
  576. $thisfile_riff_raw['avih'] = array();
  577. $thisfile_riff_raw_avih = &$thisfile_riff_raw['avih'];
  578. $thisfile_riff_raw_avih['dwMicroSecPerFrame'] = $this->EitherEndian2Int(substr($avihData, 0, 4)); // frame display rate (or 0L)
  579. if ($thisfile_riff_raw_avih['dwMicroSecPerFrame'] == 0) {
  580. $info['error'][] = 'Corrupt RIFF file: avih.dwMicroSecPerFrame == zero';
  581. return false;
  582. }
  583. $flags = array(
  584. 'dwMaxBytesPerSec', // max. transfer rate
  585. 'dwPaddingGranularity', // pad to multiples of this size; normally 2K.
  586. 'dwFlags', // the ever-present flags
  587. 'dwTotalFrames', // # frames in file
  588. 'dwInitialFrames', //
  589. 'dwStreams', //
  590. 'dwSuggestedBufferSize', //
  591. 'dwWidth', //
  592. 'dwHeight', //
  593. 'dwScale', //
  594. 'dwRate', //
  595. 'dwStart', //
  596. 'dwLength', //
  597. );
  598. $avih_offset = 4;
  599. foreach ($flags as $flag) {
  600. $thisfile_riff_raw_avih[$flag] = $this->EitherEndian2Int(substr($avihData, $avih_offset, 4));
  601. $avih_offset += 4;
  602. }
  603. $flags = array(
  604. 'hasindex' => 0x00000010,
  605. 'mustuseindex' => 0x00000020,
  606. 'interleaved' => 0x00000100,
  607. 'trustcktype' => 0x00000800,
  608. 'capturedfile' => 0x00010000,
  609. 'copyrighted' => 0x00020010,
  610. );
  611. foreach ($flags as $flag => $value) {
  612. $thisfile_riff_raw_avih['flags'][$flag] = (bool) ($thisfile_riff_raw_avih['dwFlags'] & $value);
  613. }
  614. // shortcut
  615. $thisfile_riff_video[$streamindex] = array();
  616. $thisfile_riff_video_current = &$thisfile_riff_video[$streamindex];
  617. if ($thisfile_riff_raw_avih['dwWidth'] > 0) {
  618. $thisfile_riff_video_current['frame_width'] = $thisfile_riff_raw_avih['dwWidth'];
  619. $thisfile_video['resolution_x'] = $thisfile_riff_video_current['frame_width'];
  620. }
  621. if ($thisfile_riff_raw_avih['dwHeight'] > 0) {
  622. $thisfile_riff_video_current['frame_height'] = $thisfile_riff_raw_avih['dwHeight'];
  623. $thisfile_video['resolution_y'] = $thisfile_riff_video_current['frame_height'];
  624. }
  625. if ($thisfile_riff_raw_avih['dwTotalFrames'] > 0) {
  626. $thisfile_riff_video_current['total_frames'] = $thisfile_riff_raw_avih['dwTotalFrames'];
  627. $thisfile_video['total_frames'] = $thisfile_riff_video_current['total_frames'];
  628. }
  629. $thisfile_riff_video_current['frame_rate'] = round(1000000 / $thisfile_riff_raw_avih['dwMicroSecPerFrame'], 3);
  630. $thisfile_video['frame_rate'] = $thisfile_riff_video_current['frame_rate'];
  631. }
  632. if (isset($thisfile_riff['AVI ']['hdrl']['strl']['strh'][0]['data'])) {
  633. if (is_array($thisfile_riff['AVI ']['hdrl']['strl']['strh'])) {
  634. for ($i = 0; $i < count($thisfile_riff['AVI ']['hdrl']['strl']['strh']); $i++) {
  635. if (isset($thisfile_riff['AVI ']['hdrl']['strl']['strh'][$i]['data'])) {
  636. $strhData = $thisfile_riff['AVI ']['hdrl']['strl']['strh'][$i]['data'];
  637. $strhfccType = substr($strhData, 0, 4);
  638. if (isset($thisfile_riff['AVI ']['hdrl']['strl']['strf'][$i]['data'])) {
  639. $strfData = $thisfile_riff['AVI ']['hdrl']['strl']['strf'][$i]['data'];
  640. // shortcut
  641. $thisfile_riff_raw_strf_strhfccType_streamindex = &$thisfile_riff_raw['strf'][$strhfccType][$streamindex];
  642. switch ($strhfccType) {
  643. case 'auds':
  644. $thisfile_audio['bitrate_mode'] = 'cbr';
  645. $thisfile_audio_dataformat = 'wav';
  646. if (isset($thisfile_riff_audio) && is_array($thisfile_riff_audio)) {
  647. $streamindex = count($thisfile_riff_audio);
  648. }
  649. $thisfile_riff_audio[$streamindex] = self::parseWAVEFORMATex($strfData);
  650. $thisfile_audio['wformattag'] = $thisfile_riff_audio[$streamindex]['raw']['wFormatTag'];
  651. // shortcut
  652. $thisfile_audio['streams'][$streamindex] = $thisfile_riff_audio[$streamindex];
  653. $thisfile_audio_streams_currentstream = &$thisfile_audio['streams'][$streamindex];
  654. if ($thisfile_audio_streams_currentstream['bits_per_sample'] == 0) {
  655. unset($thisfile_audio_streams_currentstream['bits_per_sample']);
  656. }
  657. $thisfile_audio_streams_currentstream['wformattag'] = $thisfile_audio_streams_currentstream['raw']['wFormatTag'];
  658. unset($thisfile_audio_streams_currentstream['raw']);
  659. // shortcut
  660. $thisfile_riff_raw['strf'][$strhfccType][$streamindex] = $thisfile_riff_audio[$streamindex]['raw'];
  661. unset($thisfile_riff_audio[$streamindex]['raw']);
  662. $thisfile_audio = getid3_lib::array_merge_noclobber($thisfile_audio, $thisfile_riff_audio[$streamindex]);
  663. $thisfile_audio['lossless'] = false;
  664. switch ($thisfile_riff_raw_strf_strhfccType_streamindex['wFormatTag']) {
  665. case 0x0001: // PCM
  666. $thisfile_audio_dataformat = 'wav';
  667. $thisfile_audio['lossless'] = true;
  668. break;
  669. case 0x0050: // MPEG Layer 2 or Layer 1
  670. $thisfile_audio_dataformat = 'mp2'; // Assume Layer-2
  671. break;
  672. case 0x0055: // MPEG Layer 3
  673. $thisfile_audio_dataformat = 'mp3';
  674. break;
  675. case 0x00FF: // AAC
  676. $thisfile_audio_dataformat = 'aac';
  677. break;
  678. case 0x0161: // Windows Media v7 / v8 / v9
  679. case 0x0162: // Windows Media Professional v9
  680. case 0x0163: // Windows Media Lossess v9
  681. $thisfile_audio_dataformat = 'wma';
  682. break;
  683. case 0x2000: // AC-3
  684. $thisfile_audio_dataformat = 'ac3';
  685. break;
  686. case 0x2001: // DTS
  687. $thisfile_audio_dataformat = 'dts';
  688. break;
  689. default:
  690. $thisfile_audio_dataformat = 'wav';
  691. break;
  692. }
  693. $thisfile_audio_streams_currentstream['dataformat'] = $thisfile_audio_dataformat;
  694. $thisfile_audio_streams_currentstream['lossless'] = $thisfile_audio['lossless'];
  695. $thisfile_audio_streams_currentstream['bitrate_mode'] = $thisfile_audio['bitrate_mode'];
  696. break;
  697. case 'iavs':
  698. case 'vids':
  699. // shortcut
  700. $thisfile_riff_raw['strh'][$i] = array();
  701. $thisfile_riff_raw_strh_current = &$thisfile_riff_raw['strh'][$i];
  702. $thisfile_riff_raw_strh_current['fccType'] = substr($strhData, 0, 4); // same as $strhfccType;
  703. $thisfile_riff_raw_strh_current['fccHandler'] = substr($strhData, 4, 4);
  704. $thisfile_riff_raw_strh_current['dwFlags'] = $this->EitherEndian2Int(substr($strhData, 8, 4)); // Contains AVITF_* flags
  705. $thisfile_riff_raw_strh_current['wPriority'] = $this->EitherEndian2Int(substr($strhData, 12, 2));
  706. $thisfile_riff_raw_strh_current['wLanguage'] = $this->EitherEndian2Int(substr($strhData, 14, 2));
  707. $thisfile_riff_raw_strh_current['dwInitialFrames'] = $this->EitherEndian2Int(substr($strhData, 16, 4));
  708. $thisfile_riff_raw_strh_current['dwScale'] = $this->EitherEndian2Int(substr($strhData, 20, 4));
  709. $thisfile_riff_raw_strh_current['dwRate'] = $this->EitherEndian2Int(substr($strhData, 24, 4));
  710. $thisfile_riff_raw_strh_current['dwStart'] = $this->EitherEndian2Int(substr($strhData, 28, 4));
  711. $thisfile_riff_raw_strh_current['dwLength'] = $this->EitherEndian2Int(substr($strhData, 32, 4));
  712. $thisfile_riff_raw_strh_current['dwSuggestedBufferSize'] = $this->EitherEndian2Int(substr($strhData, 36, 4));
  713. $thisfile_riff_raw_strh_current['dwQuality'] = $this->EitherEndian2Int(substr($strhData, 40, 4));
  714. $thisfile_riff_raw_strh_current['dwSampleSize'] = $this->EitherEndian2Int(substr($strhData, 44, 4));
  715. $thisfile_riff_raw_strh_current['rcFrame'] = $this->EitherEndian2Int(substr($strhData, 48, 4));
  716. $thisfile_riff_video_current['codec'] = self::fourccLookup($thisfile_riff_raw_strh_current['fccHandler']);
  717. $thisfile_video['fourcc'] = $thisfile_riff_raw_strh_current['fccHandler'];
  718. if (!$thisfile_riff_video_current['codec'] && isset($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']) && self::fourccLookup($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc'])) {
  719. $thisfile_riff_video_current['codec'] = self::fourccLookup($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']);
  720. $thisfile_video['fourcc'] = $thisfile_riff_raw_strf_strhfccType_streamindex['fourcc'];
  721. }
  722. $thisfile_video['codec'] = $thisfile_riff_video_current['codec'];
  723. $thisfile_video['pixel_aspect_ratio'] = (float) 1;
  724. switch ($thisfile_riff_raw_strh_current['fccHandler']) {
  725. case 'HFYU': // Huffman Lossless Codec
  726. case 'IRAW': // Intel YUV Uncompressed
  727. case 'YUY2': // Uncompressed YUV 4:2:2
  728. $thisfile_video['lossless'] = true;
  729. break;
  730. default:
  731. $thisfile_video['lossless'] = false;
  732. break;
  733. }
  734. switch ($strhfccType) {
  735. case 'vids':
  736. $thisfile_riff_raw_strf_strhfccType_streamindex = self::ParseBITMAPINFOHEADER(substr($strfData, 0, 40), ($this->container == 'riff'));
  737. $thisfile_video['bits_per_sample'] = $thisfile_riff_raw_strf_strhfccType_streamindex['biBitCount'];
  738. if ($thisfile_riff_video_current['codec'] == 'DV') {
  739. $thisfile_riff_video_current['dv_type'] = 2;
  740. }
  741. break;
  742. case 'iavs':
  743. $thisfile_riff_video_current['dv_type'] = 1;
  744. break;
  745. }
  746. break;
  747. default:
  748. $info['warning'][] = 'Unhandled fccType for stream ('.$i.'): "'.$strhfccType.'"';
  749. break;
  750. }
  751. }
  752. }
  753. if (isset($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc'])) {
  754. $thisfile_video['fourcc'] = $thisfile_riff_raw_strf_strhfccType_streamindex['fourcc'];
  755. if (self::fourccLookup($thisfile_video['fourcc'])) {
  756. $thisfile_riff_video_current['codec'] = self::fourccLookup($thisfile_video['fourcc']);
  757. $thisfile_video['codec'] = $thisfile_riff_video_current['codec'];
  758. }
  759. switch ($thisfile_riff_raw_strf_strhfccType_streamindex['fourcc']) {
  760. case 'HFYU': // Huffman Lossless Codec
  761. case 'IRAW': // Intel YUV Uncompressed
  762. case 'YUY2': // Uncompressed YUV 4:2:2
  763. $thisfile_video['lossless'] = true;
  764. //$thisfile_video['bits_per_sample'] = 24;
  765. break;
  766. default:
  767. $thisfile_video['lossless'] = false;
  768. //$thisfile_video['bits_per_sample'] = 24;
  769. break;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. break;
  776. case 'AMV ':
  777. $info['fileformat'] = 'amv';
  778. $info['mime_type'] = 'video/amv';
  779. $thisfile_video['bitrate_mode'] = 'vbr'; // it's MJPEG, presumably contant-quality encoding, thereby VBR
  780. $thisfile_video['dataformat'] = 'mjpeg';
  781. $thisfile_video['codec'] = 'mjpeg';
  782. $thisfile_video['lossless'] = false;
  783. $thisfile_video['bits_per_sample'] = 24;
  784. $thisfile_audio['dataformat'] = 'adpcm';
  785. $thisfile_audio['lossless'] = false;
  786. break;
  787. // http://en.wikipedia.org/wiki/CD-DA
  788. case 'CDDA':
  789. $info['fileformat'] = 'cda';
  790. unset($info['mime_type']);
  791. $thisfile_audio_dataformat = 'cda';
  792. $info['avdataoffset'] = 44;
  793. if (isset($thisfile_riff['CDDA']['fmt '][0]['data'])) {
  794. // shortcut
  795. $thisfile_riff_CDDA_fmt_0 = &$thisfile_riff['CDDA']['fmt '][0];
  796. $thisfile_riff_CDDA_fmt_0['unknown1'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 0, 2));
  797. $thisfile_riff_CDDA_fmt_0['track_num'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 2, 2));
  798. $thisfile_riff_CDDA_fmt_0['disc_id'] = $this->EitherEndian2Int(substr($thisfile_riff_CDDA_fmt_0['data'], 4, 4));

Large files files are truncated, but you can click here to view the full file