1<?php
2
3/////////////////////////////////////////////////////////////////
4/// getID3() by James Heinrich <info@getid3.org>               //
5//  available at https://github.com/JamesHeinrich/getID3       //
6//            or https://www.getid3.org                        //
7//            or http://getid3.sourceforge.net                 //
8//  see readme.txt for more details                            //
9/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.audio.dss.php                                        //
12// module for analyzing Digital Speech Standard (DSS) files    //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20
21class getid3_dss extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		$this->fseek($info['avdataoffset']);
30		$DSSheader  = $this->fread(1540);
31
32		if (!preg_match('#^[\\x02-\\x08]ds[s2]#', $DSSheader)) {
33			$this->error('Expecting "[02-08] 64 73 [73|32]" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($DSSheader, 0, 4)).'"');
34			return false;
35		}
36
37		// some structure information taken from http://cpansearch.perl.org/src/RGIBSON/Audio-DSS-0.02/lib/Audio/DSS.pm
38		$info['encoding']              = 'ISO-8859-1'; // not certain, but assumed
39		$info['dss'] = array();
40
41		$info['fileformat']            = 'dss';
42		$info['mime_type']             = 'audio/x-'.substr($DSSheader, 1, 3); // "audio/x-dss" or "audio/x-ds2"
43		$info['audio']['dataformat']   =            substr($DSSheader, 1, 3); //         "dss" or         "ds2"
44		$info['audio']['bitrate_mode'] = 'cbr';
45
46		$info['dss']['version']            =                            ord(substr($DSSheader,    0,   1));
47		$info['dss']['hardware']           =                           trim(substr($DSSheader,   12,  16)); // identification string for hardware used to create the file, e.g. "DPM 9600", "DS2400"
48		$info['dss']['unknown1']           =   getid3_lib::LittleEndian2Int(substr($DSSheader,   28,   4));
49		// 32-37 = "FE FF FE FF F7 FF" in all the sample files I've seen
50		$info['dss']['date_create_unix']   = $this->DSSdateStringToUnixDate(substr($DSSheader,   38,  12));
51		$info['dss']['date_complete_unix'] = $this->DSSdateStringToUnixDate(substr($DSSheader,   50,  12));
52		$info['dss']['playtime_sec']       = ((int) substr($DSSheader, 62, 2) * 3600) + ((int) substr($DSSheader, 64, 2) * 60) + (int) substr($DSSheader, 66, 2); // approximate file playtime in HHMMSS
53		if ($info['dss']['version'] <= 3) {
54			$info['dss']['playtime_ms']        =   getid3_lib::LittleEndian2Int(substr($DSSheader,  512,   4)); // exact file playtime in milliseconds. Has also been observed at offset 530 in one sample file, with something else (unknown) at offset 512
55			$info['dss']['priority']           =                            ord(substr($DSSheader,  793,   1));
56			$info['dss']['comments']           =                           trim(substr($DSSheader,  798, 100));
57			$info['dss']['sample_rate_index']  =                            ord(substr($DSSheader, 1538,   1));  // this isn't certain, this may or may not be where the sample rate info is stored, but it seems consistent on my small selection of sample files
58			$info['audio']['sample_rate']      = $this->DSSsampleRateLookup($info['dss']['sample_rate_index']);
59		} else {
60			$this->getid3->warning('DSS above version 3 not fully supported in this version of getID3. Any additional documentation or format specifications would be welcome. This file is version '.$info['dss']['version']);
61		}
62
63		$info['audio']['bits_per_sample']  = 16; // maybe, maybe not -- most compressed audio formats don't have a fixed bits-per-sample value, but this is a reasonable approximation
64		$info['audio']['channels']         = 1;
65
66		if (!empty($info['dss']['playtime_ms']) && (floor($info['dss']['playtime_ms'] / 1000) == $info['dss']['playtime_sec'])) { // *should* just be playtime_ms / 1000 but at least one sample file has playtime_ms at offset 530 instead of offset 512, so safety check
67			$info['playtime_seconds'] = $info['dss']['playtime_ms'] / 1000;
68		} else {
69			$info['playtime_seconds'] = $info['dss']['playtime_sec'];
70			if (!empty($info['dss']['playtime_ms'])) {
71				$this->getid3->warning('playtime_ms ('.number_format($info['dss']['playtime_ms'] / 1000, 3).') does not match playtime_sec ('.number_format($info['dss']['playtime_sec']).') - using playtime_sec value');
72			}
73		}
74		$info['audio']['bitrate'] = ($info['filesize'] * 8) / $info['playtime_seconds'];
75
76		return true;
77	}
78
79	/**
80	 * @param string $datestring
81	 *
82	 * @return int|false
83	 */
84	public function DSSdateStringToUnixDate($datestring) {
85		$y = (int) substr($datestring,  0, 2);
86		$m = substr($datestring,  2, 2);
87		$d = substr($datestring,  4, 2);
88		$h = substr($datestring,  6, 2);
89		$i = substr($datestring,  8, 2);
90		$s = substr($datestring, 10, 2);
91		$y += (($y < 95) ? 2000 : 1900);
92		return mktime($h, $i, $s, $m, $d, $y);
93	}
94
95	/**
96	 * @param int $sample_rate_index
97	 *
98	 * @return int|false
99	 */
100	public function DSSsampleRateLookup($sample_rate_index) {
101		static $dssSampleRateLookup = array(
102			0x0A => 16000,
103			0x0C => 11025,
104			0x0D => 12000,
105			0x15 =>  8000,
106		);
107		if (!array_key_exists($sample_rate_index, $dssSampleRateLookup)) {
108			$this->getid3->warning('unknown sample_rate_index: 0x'.strtoupper(dechex($sample_rate_index)));
109			return false;
110		}
111		return $dssSampleRateLookup[$sample_rate_index];
112	}
113
114}
115