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-video.ts.php                                   //
12// module for analyzing MPEG Transport Stream (.ts) 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_ts 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		$TSheader = $this->fread(19);
31		$magic = "\x47";
32		if (substr($TSheader, 0, 1) != $magic) {
33			$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at '.$info['avdataoffset'].', found '.getid3_lib::PrintHexBytes(substr($TSheader, 0, 1)).' instead.');
34			return false;
35		}
36		$info['fileformat'] = 'ts';
37
38		// http://en.wikipedia.org/wiki/.ts
39
40		$offset = 0;
41		$info['ts']['packet']['sync'] = getid3_lib::BigEndian2Int(substr($TSheader, $offset, 1)); $offset += 1;
42		$pid_flags_raw                = getid3_lib::BigEndian2Int(substr($TSheader, $offset, 2)); $offset += 2;
43		$SAC_raw                      = getid3_lib::BigEndian2Int(substr($TSheader, $offset, 1)); $offset += 1;
44		$info['ts']['packet']['flags']['transport_error_indicator']    =      (bool) ($pid_flags_raw & 0x8000);      // Set by demodulator if can't correct errors in the stream, to tell the demultiplexer that the packet has an uncorrectable error
45		$info['ts']['packet']['flags']['payload_unit_start_indicator'] =      (bool) ($pid_flags_raw & 0x4000);      // 1 means start of PES data or PSI otherwise zero only.
46		$info['ts']['packet']['flags']['transport_high_priority']      =      (bool) ($pid_flags_raw & 0x2000);      // 1 means higher priority than other packets with the same PID.
47		$info['ts']['packet']['packet_id']                             =             ($pid_flags_raw & 0x1FFF) >> 0;
48
49		$info['ts']['packet']['raw']['scrambling_control']             =                   ($SAC_raw &   0xC0) >> 6;
50		$info['ts']['packet']['flags']['adaption_field_exists']        =      (bool)       ($SAC_raw &   0x20);
51		$info['ts']['packet']['flags']['payload_exists']               =      (bool)       ($SAC_raw &   0x10);
52		$info['ts']['packet']['continuity_counter']                    =                   ($SAC_raw &   0x0F) >> 0; // Incremented only when a payload is present
53		$info['ts']['packet']['scrambling_control']                    = $this->TSscramblingControlLookup($info['ts']['packet']['raw']['scrambling_control']);
54
55		if ($info['ts']['packet']['flags']['adaption_field_exists']) {
56			$AdaptionField_raw        = getid3_lib::BigEndian2Int(substr($TSheader, $offset, 2)); $offset += 2;
57			$info['ts']['packet']['adaption']['field_length']           =        ($AdaptionField_raw & 0xFF00) >> 8;  // Number of bytes in the adaptation field immediately following this byte
58			$info['ts']['packet']['adaption']['flags']['discontinuity'] = (bool) ($AdaptionField_raw & 0x0080);       // Set to 1 if current TS packet is in a discontinuity state with respect to either the continuity counter or the program clock reference
59			$info['ts']['packet']['adaption']['flags']['random_access'] = (bool) ($AdaptionField_raw & 0x0040);       // Set to 1 if the PES packet in this TS packet starts a video/audio sequence
60			$info['ts']['packet']['adaption']['flags']['high_priority'] = (bool) ($AdaptionField_raw & 0x0020);       // 1 = higher priority
61			$info['ts']['packet']['adaption']['flags']['pcr']           = (bool) ($AdaptionField_raw & 0x0010);       // 1 means adaptation field does contain a PCR field
62			$info['ts']['packet']['adaption']['flags']['opcr']          = (bool) ($AdaptionField_raw & 0x0008);       // 1 means adaptation field does contain an OPCR field
63			$info['ts']['packet']['adaption']['flags']['splice_point']  = (bool) ($AdaptionField_raw & 0x0004);       // 1 means presence of splice countdown field in adaptation field
64			$info['ts']['packet']['adaption']['flags']['private_data']  = (bool) ($AdaptionField_raw & 0x0002);       // 1 means presence of private data bytes in adaptation field
65			$info['ts']['packet']['adaption']['flags']['extension']     = (bool) ($AdaptionField_raw & 0x0001);       // 1 means presence of adaptation field extension
66			if ($info['ts']['packet']['adaption']['flags']['pcr']) {
67				$info['ts']['packet']['adaption']['raw']['pcr'] = getid3_lib::BigEndian2Int(substr($TSheader, $offset, 6)); $offset += 6;
68			}
69			if ($info['ts']['packet']['adaption']['flags']['opcr']) {
70				$info['ts']['packet']['adaption']['raw']['opcr'] = getid3_lib::BigEndian2Int(substr($TSheader, $offset, 6)); $offset += 6;
71			}
72		}
73
74		$this->error('MPEG Transport Stream (.ts) parsing not enabled in this version of getID3() ['.$this->getid3->version().']');
75		return false;
76
77	}
78
79	/**
80	 * @param int $raw
81	 *
82	 * @return string
83	 */
84	public function TSscramblingControlLookup($raw) {
85		$TSscramblingControlLookup = array(0x00=>'not scrambled', 0x01=>'reserved', 0x02=>'scrambled, even key', 0x03=>'scrambled, odd key');
86		return (isset($TSscramblingControlLookup[$raw]) ? $TSscramblingControlLookup[$raw] : 'invalid');
87	}
88}
89