aboutsummaryrefslogtreecommitdiff
path: root/includes/getid3/module.audio.lpac.php
blob: 769a8aa2db3df9aa3f843b09c8c7407c9235bb14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/*
 * MIT License
 *
 * Copyright (c) 2022- Minteck
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org>               //
//  available at https://github.com/JamesHeinrich/getID3       //
//            or https://www.getid3.org                        //
//            or http://getid3.sourceforge.net                 //
//  see readme.txt for more details                            //
/////////////////////////////////////////////////////////////////
//                                                             //
// module.audio.lpac.php                                       //
// module for analyzing LPAC Audio files                       //
// dependencies: module.audio-video.riff.php                   //
//                                                            ///
/////////////////////////////////////////////////////////////////

if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
	exit;
}
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);

class getid3_lpac extends getid3_handler
{
	/**
	 * @return bool
	 */
	public function Analyze() {
		$info = &$this->getid3->info;

		$this->fseek($info['avdataoffset']);
		$LPACheader = $this->fread(14);
		$StreamMarker = substr($LPACheader, 0, 4);
		if ($StreamMarker != 'LPAC') {
			$this->error('Expected "LPAC" at offset '.$info['avdataoffset'].', found "'.$StreamMarker.'"');
			return false;
		}
		$info['avdataoffset'] += 14;

		$info['fileformat']            = 'lpac';
		$info['audio']['dataformat']   = 'lpac';
		$info['audio']['lossless']     = true;
		$info['audio']['bitrate_mode'] = 'vbr';

		$info['lpac']['file_version'] = getid3_lib::BigEndian2Int(substr($LPACheader,  4, 1));
		$flags['audio_type']                  = getid3_lib::BigEndian2Int(substr($LPACheader,  5, 1));
		$info['lpac']['total_samples']= getid3_lib::BigEndian2Int(substr($LPACheader,  6, 4));
		$flags['parameters']                  = getid3_lib::BigEndian2Int(substr($LPACheader, 10, 4));

		$info['lpac']['flags']['is_wave'] = (bool) ($flags['audio_type'] & 0x40);
		$info['lpac']['flags']['stereo']  = (bool) ($flags['audio_type'] & 0x04);
		$info['lpac']['flags']['24_bit']  = (bool) ($flags['audio_type'] & 0x02);
		$info['lpac']['flags']['16_bit']  = (bool) ($flags['audio_type'] & 0x01);

		if ($info['lpac']['flags']['24_bit'] && $info['lpac']['flags']['16_bit']) {
			$this->warning('24-bit and 16-bit flags cannot both be set');
		}

		$info['lpac']['flags']['fast_compress']             =  (bool) ($flags['parameters'] & 0x40000000);
		$info['lpac']['flags']['random_access']             =  (bool) ($flags['parameters'] & 0x08000000);
		$info['lpac']['block_length']                       = pow(2, (($flags['parameters'] & 0x07000000) >> 24)) * 256;
		$info['lpac']['flags']['adaptive_prediction_order'] =  (bool) ($flags['parameters'] & 0x00800000);
		$info['lpac']['flags']['adaptive_quantization']     =  (bool) ($flags['parameters'] & 0x00400000);
		$info['lpac']['flags']['joint_stereo']              =  (bool) ($flags['parameters'] & 0x00040000);
		$info['lpac']['quantization']                       =         ($flags['parameters'] & 0x00001F00) >> 8;
		$info['lpac']['max_prediction_order']               =         ($flags['parameters'] & 0x0000003F);

		if ($info['lpac']['flags']['fast_compress'] && ($info['lpac']['max_prediction_order'] != 3)) {
			$this->warning('max_prediction_order expected to be "3" if fast_compress is true, actual value is "'.$info['lpac']['max_prediction_order'].'"');
		}
		switch ($info['lpac']['file_version']) {
			case 6:
				if ($info['lpac']['flags']['adaptive_quantization']) {
					$this->warning('adaptive_quantization expected to be false in LPAC file stucture v6, actually true');
				}
				if ($info['lpac']['quantization'] != 20) {
					$this->warning('Quantization expected to be 20 in LPAC file stucture v6, actually '.$info['lpac']['flags']['Q']);
				}
				break;

			default:
				//$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');
				break;
		}

		$getid3_temp = new getID3();
		$getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp);
		$getid3_temp->info = $info;
		$getid3_riff = new getid3_riff($getid3_temp);
		$getid3_riff->Analyze();
		$info['avdataoffset']                = $getid3_temp->info['avdataoffset'];
		$info['riff']                        = $getid3_temp->info['riff'];
		$info['error']                       = $getid3_temp->info['error'];
		$info['warning']                     = $getid3_temp->info['warning'];
		$info['lpac']['comments']['comment'] = $getid3_temp->info['comments'];
		$info['audio']['sample_rate']        = $getid3_temp->info['audio']['sample_rate'];
		unset($getid3_temp, $getid3_riff);

		$info['audio']['channels']    = ($info['lpac']['flags']['stereo'] ? 2 : 1);

		if ($info['lpac']['flags']['24_bit']) {
			$info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
		} elseif ($info['lpac']['flags']['16_bit']) {
			$info['audio']['bits_per_sample'] = 16;
		} else {
			$info['audio']['bits_per_sample'] = 8;
		}

		if ($info['lpac']['flags']['fast_compress']) {
			 // fast
			$info['audio']['encoder_options'] = '-1';
		} else {
			switch ($info['lpac']['max_prediction_order']) {
				case 20: // simple
					$info['audio']['encoder_options'] = '-2';
					break;
				case 30: // medium
					$info['audio']['encoder_options'] = '-3';
					break;
				case 40: // high
					$info['audio']['encoder_options'] = '-4';
					break;
				case 60: // extrahigh
					$info['audio']['encoder_options'] = '-5';
					break;
			}
		}

		$info['playtime_seconds'] = $info['lpac']['total_samples'] / $info['audio']['sample_rate'];
		$info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];

		return true;
	}

}