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.lpac.php                                       //
12// module for analyzing LPAC Audio files                       //
13// dependencies: module.audio-video.riff.php                   //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
21
22class getid3_lpac extends getid3_handler
23{
24	/**
25	 * @return bool
26	 */
27	public function Analyze() {
28		$info = &$this->getid3->info;
29
30		$this->fseek($info['avdataoffset']);
31		$LPACheader = $this->fread(14);
32		$StreamMarker = substr($LPACheader, 0, 4);
33		if ($StreamMarker != 'LPAC') {
34			$this->error('Expected "LPAC" at offset '.$info['avdataoffset'].', found "'.$StreamMarker.'"');
35			return false;
36		}
37		$info['avdataoffset'] += 14;
38
39		$info['fileformat']            = 'lpac';
40		$info['audio']['dataformat']   = 'lpac';
41		$info['audio']['lossless']     = true;
42		$info['audio']['bitrate_mode'] = 'vbr';
43
44		$info['lpac']['file_version'] = getid3_lib::BigEndian2Int(substr($LPACheader,  4, 1));
45		$flags['audio_type']                  = getid3_lib::BigEndian2Int(substr($LPACheader,  5, 1));
46		$info['lpac']['total_samples']= getid3_lib::BigEndian2Int(substr($LPACheader,  6, 4));
47		$flags['parameters']                  = getid3_lib::BigEndian2Int(substr($LPACheader, 10, 4));
48
49		$info['lpac']['flags']['is_wave'] = (bool) ($flags['audio_type'] & 0x40);
50		$info['lpac']['flags']['stereo']  = (bool) ($flags['audio_type'] & 0x04);
51		$info['lpac']['flags']['24_bit']  = (bool) ($flags['audio_type'] & 0x02);
52		$info['lpac']['flags']['16_bit']  = (bool) ($flags['audio_type'] & 0x01);
53
54		if ($info['lpac']['flags']['24_bit'] && $info['lpac']['flags']['16_bit']) {
55			$this->warning('24-bit and 16-bit flags cannot both be set');
56		}
57
58		$info['lpac']['flags']['fast_compress']             =  (bool) ($flags['parameters'] & 0x40000000);
59		$info['lpac']['flags']['random_access']             =  (bool) ($flags['parameters'] & 0x08000000);
60		$info['lpac']['block_length']                       = pow(2, (($flags['parameters'] & 0x07000000) >> 24)) * 256;
61		$info['lpac']['flags']['adaptive_prediction_order'] =  (bool) ($flags['parameters'] & 0x00800000);
62		$info['lpac']['flags']['adaptive_quantization']     =  (bool) ($flags['parameters'] & 0x00400000);
63		$info['lpac']['flags']['joint_stereo']              =  (bool) ($flags['parameters'] & 0x00040000);
64		$info['lpac']['quantization']                       =         ($flags['parameters'] & 0x00001F00) >> 8;
65		$info['lpac']['max_prediction_order']               =         ($flags['parameters'] & 0x0000003F);
66
67		if ($info['lpac']['flags']['fast_compress'] && ($info['lpac']['max_prediction_order'] != 3)) {
68			$this->warning('max_prediction_order expected to be "3" if fast_compress is true, actual value is "'.$info['lpac']['max_prediction_order'].'"');
69		}
70		switch ($info['lpac']['file_version']) {
71			case 6:
72				if ($info['lpac']['flags']['adaptive_quantization']) {
73					$this->warning('adaptive_quantization expected to be false in LPAC file stucture v6, actually true');
74				}
75				if ($info['lpac']['quantization'] != 20) {
76					$this->warning('Quantization expected to be 20 in LPAC file stucture v6, actually '.$info['lpac']['flags']['Q']);
77				}
78				break;
79
80			default:
81				//$this->warning('This version of getID3() ['.$this->getid3->version().'] only supports LPAC file format version 6, this file is version '.$info['lpac']['file_version'].' - please report to info@getid3.org');
82				break;
83		}
84
85		$getid3_temp = new getID3();
86		$getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp);
87		$getid3_temp->info = $info;
88		$getid3_riff = new getid3_riff($getid3_temp);
89		$getid3_riff->Analyze();
90		$info['avdataoffset']                = $getid3_temp->info['avdataoffset'];
91		$info['riff']                        = $getid3_temp->info['riff'];
92		$info['error']                       = $getid3_temp->info['error'];
93		$info['warning']                     = $getid3_temp->info['warning'];
94		$info['lpac']['comments']['comment'] = $getid3_temp->info['comments'];
95		$info['audio']['sample_rate']        = $getid3_temp->info['audio']['sample_rate'];
96		unset($getid3_temp, $getid3_riff);
97
98		$info['audio']['channels']    = ($info['lpac']['flags']['stereo'] ? 2 : 1);
99
100		if ($info['lpac']['flags']['24_bit']) {
101			$info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
102		} elseif ($info['lpac']['flags']['16_bit']) {
103			$info['audio']['bits_per_sample'] = 16;
104		} else {
105			$info['audio']['bits_per_sample'] = 8;
106		}
107
108		if ($info['lpac']['flags']['fast_compress']) {
109			 // fast
110			$info['audio']['encoder_options'] = '-1';
111		} else {
112			switch ($info['lpac']['max_prediction_order']) {
113				case 20: // simple
114					$info['audio']['encoder_options'] = '-2';
115					break;
116				case 30: // medium
117					$info['audio']['encoder_options'] = '-3';
118					break;
119				case 40: // high
120					$info['audio']['encoder_options'] = '-4';
121					break;
122				case 60: // extrahigh
123					$info['audio']['encoder_options'] = '-5';
124					break;
125			}
126		}
127
128		$info['playtime_seconds'] = $info['lpac']['total_samples'] / $info['audio']['sample_rate'];
129		$info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
130
131		return true;
132	}
133
134}
135