1<?php
2/////////////////////////////////////////////////////////////////
3/// getID3() by James Heinrich <info@getid3.org>               //
4//  available at https://github.com/JamesHeinrich/getID3       //
5//            or https://www.getid3.org                        //
6//            or http://getid3.sourceforge.net                 //
7//  see readme.txt for more details                            //
8/////////////////////////////////////////////////////////////////
9//                                                             //
10// module.audio.ivf.php                                        //
11// module for analyzing IVF audio-video files                  //
12// dependencies: NONE                                          //
13//                                                            ///
14/////////////////////////////////////////////////////////////////
15
16if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
17	exit;
18}
19
20class getid3_ivf extends getid3_handler
21{
22	/**
23	 * @return bool
24	 */
25	public function Analyze() {
26		$info = &$this->getid3->info;
27
28		$info['fileformat']          = 'ivf';
29		$info['video']['dataformat'] = 'ivf';
30
31		$this->fseek($info['avdataoffset']);
32		$IVFheader = $this->fread(32);
33
34		if (substr($IVFheader, 0, 4) == 'DKIF') {
35
36			// https://wiki.multimedia.cx/index.php/IVF
37			$info['ivf']['header']['signature']            =                              substr($IVFheader,  0, 4);
38			$info['ivf']['header']['version']              = getid3_lib::LittleEndian2Int(substr($IVFheader,  4, 2)); // should be 0
39			$info['ivf']['header']['headersize']           = getid3_lib::LittleEndian2Int(substr($IVFheader,  6, 2));
40			$info['ivf']['header']['fourcc']               =                              substr($IVFheader,  8, 4);
41			$info['ivf']['header']['resolution_x']         = getid3_lib::LittleEndian2Int(substr($IVFheader, 12, 2));
42			$info['ivf']['header']['resolution_y']         = getid3_lib::LittleEndian2Int(substr($IVFheader, 14, 2));
43			$info['ivf']['header']['timebase_numerator']   = getid3_lib::LittleEndian2Int(substr($IVFheader, 16, 4));
44			$info['ivf']['header']['timebase_denominator'] = getid3_lib::LittleEndian2Int(substr($IVFheader, 20, 4));
45			$info['ivf']['header']['frame_count']          = getid3_lib::LittleEndian2Int(substr($IVFheader, 24, 4));
46			//$info['ivf']['header']['reserved']             =                              substr($IVFheader, 28, 4);
47
48			$info['ivf']['header']['frame_rate'] = (float) $info['ivf']['header']['timebase_numerator'] / $info['ivf']['header']['timebase_denominator'];
49
50			if ($info['ivf']['header']['version'] > 0) {
51				$this->warning('Expecting IVF header version 0, found version '.$info['ivf']['header']['version'].', results may not be accurate');
52			}
53
54			$info['video']['resolution_x']    =         $info['ivf']['header']['resolution_x'];
55			$info['video']['resolution_y']    =         $info['ivf']['header']['resolution_y'];
56			$info['video']['codec']           =         $info['ivf']['header']['fourcc'];
57
58			$info['ivf']['frame_count'] = 0;
59			while (!$this->feof()) {
60				if ($frameheader = $this->fread(12)) {
61					$framesize = getid3_lib::LittleEndian2Int(substr($frameheader, 0, 4)); // size of frame in bytes (not including the 12-byte header)
62					$timestamp = getid3_lib::LittleEndian2Int(substr($frameheader, 4, 8)); // 64-bit presentation timestamp
63					$this->fseek($framesize, SEEK_CUR);
64					$info['ivf']['frame_count']++;
65				}
66			}
67			if ($info['ivf']['frame_count']) {
68				$info['playtime_seconds']    = $timestamp / 100000;
69				$info['video']['frame_rate'] = (float) $info['ivf']['frame_count'] / $info['playtime_seconds'];
70			}
71
72		} else {
73			$this->error('Expecting "DKIF" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($IVFheader, 0, 4)).'"');
74			return false;
75		}
76
77		return true;
78	}
79
80}
81