Ardour  9.2-266-g5d535d4cb7
ptformat.h
Go to the documentation of this file.
1 /*
2  * libptformat - a library to read ProTools sessions
3  *
4  * Copyright (C) 2015-2019 Damien Zammit
5  * Copyright (C) 2015-2019 Robin Gareus
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 #ifndef PTFFORMAT_H
23 #define PTFFORMAT_H
24 
25 #include <string>
26 #include <cstring>
27 #include <algorithm>
28 #include <vector>
29 #include <stdint.h>
30 
31 #include "ptformat/visibility.h"
32 
34 public:
37 
38  /* Return values: 0 success
39  -1 error decrypting pt session
40  -2 error detecting pt session
41  -3 incompatible pt version
42  -4 error parsing pt session
43  */
44  int load(std::string const& path, int64_t targetsr);
45 
46  /* Return values: 0 success
47  -1 error decrypting pt session
48  */
49  int unxor(std::string const& path);
50 
51  struct wav_t {
52  std::string filename;
53  uint16_t index;
54 
55  int64_t posabsolute;
56  int64_t length;
57 
58  bool operator <(const struct wav_t& other) const {
59  std::string a (this->filename);
60  std::string b (other.filename);
61  std::transform (a.begin(), a.end(), a.begin(), ::tolower);
62  std::transform (b.begin(), b.end(), b.begin(), ::tolower);
63  return a < b;
64  }
65 
66  bool operator ==(const struct wav_t& other) const {
67  return (this->filename == other.filename ||
68  this->index == other.index);
69  }
70 
71  wav_t (uint16_t idx = 0) : index (idx), posabsolute (0), length (0) {}
72  };
73 
74  struct midi_ev_t {
75  uint64_t pos;
76  uint64_t length;
77  uint8_t note;
78  uint8_t velocity;
79  midi_ev_t () : pos (0), length (0), note (0), velocity (0) {}
80  };
81 
82  struct region_t {
83  std::string name;
84  uint16_t index;
85  int64_t startpos;
86  int64_t sampleoffset;
87  int64_t length;
89  std::vector<midi_ev_t> midi;
90 
91  bool operator ==(const region_t& other) const {
92  return (this->index == other.index);
93  }
94 
95  bool operator <(const region_t& other) const {
96  std::string a (this->name);
97  std::string b (other.name);
98  std::transform (a.begin(), a.end(), a.begin(), ::tolower);
99  std::transform (b.begin(), b.end(), b.begin(), ::tolower);
100  return a < b;
101  }
102  region_t (uint16_t idx = 0) : index (idx), startpos (0), sampleoffset (0), length (0) {}
103  };
104 
105  struct track_t {
106  std::string name;
107  uint16_t index;
108  uint8_t playlist;
110 
111  bool operator <(const track_t& other) const {
112  return (this->index < other.index);
113  }
114 
115  bool operator ==(const track_t& other) const {
116  return (this->index == other.index);
117  }
118  track_t (uint16_t idx = 0) : index (idx), playlist (0) {}
119  };
120 
121  bool find_track(uint16_t index, track_t& tt) const {
122  std::vector<track_t>::const_iterator begin = _tracks.begin();
123  std::vector<track_t>::const_iterator finish = _tracks.end();
124  std::vector<track_t>::const_iterator found;
125 
126  track_t t (index);
127 
128  if ((found = std::find(begin, finish, t)) != finish) {
129  tt = *found;
130  return true;
131  }
132  return false;
133  }
134 
135  bool find_region(uint16_t index, region_t& rr) const {
136  std::vector<region_t>::const_iterator begin = _regions.begin();
137  std::vector<region_t>::const_iterator finish = _regions.end();
138  std::vector<region_t>::const_iterator found;
139 
140  region_t r;
141  r.index = index;
142 
143  if ((found = std::find(begin, finish, r)) != finish) {
144  rr = *found;
145  return true;
146  }
147  return false;
148  }
149 
150  bool find_miditrack(uint16_t index, track_t& tt) const {
151  std::vector<track_t>::const_iterator begin = _miditracks.begin();
152  std::vector<track_t>::const_iterator finish = _miditracks.end();
153  std::vector<track_t>::const_iterator found;
154 
155  track_t t (index);
156 
157  if ((found = std::find(begin, finish, t)) != finish) {
158  tt = *found;
159  return true;
160  }
161  return false;
162  }
163 
164  bool find_midiregion(uint16_t index, region_t& rr) const {
165  std::vector<region_t>::const_iterator begin = _midiregions.begin();
166  std::vector<region_t>::const_iterator finish = _midiregions.end();
167  std::vector<region_t>::const_iterator found;
168 
169  region_t r (index);
170 
171  if ((found = std::find(begin, finish, r)) != finish) {
172  rr = *found;
173  return true;
174  }
175  return false;
176  }
177 
178  bool find_wav(uint16_t index, wav_t& ww) const {
179  std::vector<wav_t>::const_iterator begin = _audiofiles.begin();
180  std::vector<wav_t>::const_iterator finish = _audiofiles.end();
181  std::vector<wav_t>::const_iterator found;
182 
183  wav_t w (index);
184 
185  if ((found = std::find(begin, finish, w)) != finish) {
186  ww = *found;
187  return true;
188  }
189  return false;
190  }
191 
192  static bool regionexistsin(std::vector<region_t> const& reg, uint16_t index) {
193  std::vector<region_t>::const_iterator begin = reg.begin();
194  std::vector<region_t>::const_iterator finish = reg.end();
195 
196  region_t r (index);
197 
198  if (std::find(begin, finish, r) != finish) {
199  return true;
200  }
201  return false;
202  }
203 
204  static bool wavexistsin (std::vector<wav_t> const& wv, uint16_t index) {
205  std::vector<wav_t>::const_iterator begin = wv.begin();
206  std::vector<wav_t>::const_iterator finish = wv.end();
207 
208  wav_t w (index);
209 
210  if (std::find(begin, finish, w) != finish) {
211  return true;
212  }
213  return false;
214  }
215 
216  uint8_t version () const { return _version; }
217  int64_t sessionrate () const { return _sessionrate ; }
218  const std::string& path () { return _path; }
219 
220  const std::vector<wav_t>& audiofiles () const { return _audiofiles ; }
221  const std::vector<region_t>& regions () const { return _regions ; }
222  const std::vector<region_t>& midiregions () const { return _midiregions ; }
223  const std::vector<track_t>& tracks () const { return _tracks ; }
224  const std::vector<track_t>& miditracks () const { return _miditracks ; }
225 
226  const unsigned char* unxored_data () const { return _ptfunxored; }
227  uint64_t unxored_size () const { return _len; }
228 
229 private:
230 
231  std::vector<wav_t> _audiofiles;
232  std::vector<region_t> _regions;
233  std::vector<region_t> _midiregions;
234  std::vector<track_t> _tracks;
235  std::vector<track_t> _miditracks;
236 
237  std::string _path;
238 
239  unsigned char* _ptfunxored;
240  uint64_t _len;
241  int64_t _sessionrate;
242  uint8_t _version;
243  uint8_t* _product;
244  int64_t _targetrate;
245  float _ratefactor;
247 
248  struct block_t {
249  uint8_t zmark; // 'Z'
250  uint16_t block_type; // type of block
251  uint32_t block_size; // size of block
252  uint16_t content_type; // type of content
253  uint32_t offset; // offset in file
254  std::vector<block_t> child; // vector of child blocks
255  };
256  std::vector<block_t> blocks;
257 
258  bool jumpback(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
259  bool jumpto(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
260  bool foundin(std::string const& haystack, std::string const& needle);
261  int64_t foundat(unsigned char *haystack, uint64_t n, const char *needle);
262 
263  std::string parsestring(uint32_t pos);
264  const std::string get_content_description(uint16_t ctype);
265  int parse(void);
266  void parseblocks(void);
267  bool parseheader(void);
268  bool parserest(void);
269  bool parseaudio(void);
270  bool parsemidi(void);
271  void dump(void);
272  bool parse_block_at(uint32_t pos, struct block_t *b, struct block_t *parent, int level);
273  void dump_block(struct block_t& b, int level);
275  void parse_region_info(uint32_t j, block_t& blk, region_t& r);
276  void parse_three_point(uint32_t j, uint64_t& start, uint64_t& offset, uint64_t& length);
277  uint8_t gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative);
278  void setrates(void);
279  void cleanup(void);
280  void free_block(struct block_t& b);
281  void free_all_blocks(void);
282 };
283 
284 #endif
void dump(void)
const std::vector< region_t > & midiregions() const
Definition: ptformat.h:222
void parse_three_point(uint32_t j, uint64_t &start, uint64_t &offset, uint64_t &length)
void free_block(struct block_t &b)
bool parse_block_at(uint32_t pos, struct block_t *b, struct block_t *parent, int level)
std::string _path
Definition: ptformat.h:237
std::vector< wav_t > _audiofiles
Definition: ptformat.h:231
uint8_t gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative)
const std::string get_content_description(uint16_t ctype)
int parse(void)
static bool regionexistsin(std::vector< region_t > const &reg, uint16_t index)
Definition: ptformat.h:192
bool jumpto(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen)
float _ratefactor
Definition: ptformat.h:245
bool parsemidi(void)
const std::vector< region_t > & regions() const
Definition: ptformat.h:221
static bool wavexistsin(std::vector< wav_t > const &wv, uint16_t index)
Definition: ptformat.h:204
bool find_wav(uint16_t index, wav_t &ww) const
Definition: ptformat.h:178
bool is_bigendian
Definition: ptformat.h:246
int64_t _sessionrate
Definition: ptformat.h:241
std::string parsestring(uint32_t pos)
void dump_block(struct block_t &b, int level)
bool parse_version()
bool parserest(void)
const std::string & path()
Definition: ptformat.h:218
uint8_t _version
Definition: ptformat.h:242
bool find_region(uint16_t index, region_t &rr) const
Definition: ptformat.h:135
uint8_t version() const
Definition: ptformat.h:216
std::vector< region_t > _regions
Definition: ptformat.h:232
std::vector< track_t > _miditracks
Definition: ptformat.h:235
int64_t foundat(unsigned char *haystack, uint64_t n, const char *needle)
std::vector< block_t > blocks
Definition: ptformat.h:256
const std::vector< wav_t > & audiofiles() const
Definition: ptformat.h:220
uint8_t * _product
Definition: ptformat.h:243
void parse_region_info(uint32_t j, block_t &blk, region_t &r)
void setrates(void)
bool find_miditrack(uint16_t index, track_t &tt) const
Definition: ptformat.h:150
bool foundin(std::string const &haystack, std::string const &needle)
bool find_track(uint16_t index, track_t &tt) const
Definition: ptformat.h:121
const unsigned char * unxored_data() const
Definition: ptformat.h:226
int64_t _targetrate
Definition: ptformat.h:244
const std::vector< track_t > & tracks() const
Definition: ptformat.h:223
unsigned char * _ptfunxored
Definition: ptformat.h:239
void cleanup(void)
void parseblocks(void)
int64_t sessionrate() const
Definition: ptformat.h:217
bool parseheader(void)
int load(std::string const &path, int64_t targetsr)
void free_all_blocks(void)
bool parseaudio(void)
bool jumpback(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen)
std::vector< region_t > _midiregions
Definition: ptformat.h:233
uint64_t unxored_size() const
Definition: ptformat.h:227
int unxor(std::string const &path)
bool find_midiregion(uint16_t index, region_t &rr) const
Definition: ptformat.h:164
uint64_t _len
Definition: ptformat.h:240
const std::vector< track_t > & miditracks() const
Definition: ptformat.h:224
std::vector< track_t > _tracks
Definition: ptformat.h:234
PBD::PropertyDescriptor< timecnt_t > length
PBD::PropertyDescriptor< timepos_t > start
bool operator==(const ProcessorSelection &a, const ProcessorSelection &b)
#define LIBPTFORMAT_API
uint32_t offset
Definition: ptformat.h:253
uint32_t block_size
Definition: ptformat.h:251
uint16_t block_type
Definition: ptformat.h:250
uint16_t content_type
Definition: ptformat.h:252
std::vector< block_t > child
Definition: ptformat.h:254
int64_t sampleoffset
Definition: ptformat.h:86
int64_t startpos
Definition: ptformat.h:85
std::string name
Definition: ptformat.h:83
uint16_t index
Definition: ptformat.h:84
region_t(uint16_t idx=0)
Definition: ptformat.h:102
std::vector< midi_ev_t > midi
Definition: ptformat.h:89
std::string name
Definition: ptformat.h:106
track_t(uint16_t idx=0)
Definition: ptformat.h:118
uint16_t index
Definition: ptformat.h:107
uint8_t playlist
Definition: ptformat.h:108
int64_t posabsolute
Definition: ptformat.h:55
int64_t length
Definition: ptformat.h:56
std::string filename
Definition: ptformat.h:52
uint16_t index
Definition: ptformat.h:53
wav_t(uint16_t idx=0)
Definition: ptformat.h:71
link region and track false waveform clip level