Ardour  9.0-pre0-582-g084a23a80d
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 #include "ptformat/visibility.h"
31 
33 public:
36 
37  /* Return values: 0 success
38  -1 error decrypting pt session
39  -2 error detecting pt session
40  -3 incompatible pt version
41  -4 error parsing pt session
42  */
43  int load(std::string const& path, int64_t targetsr);
44 
45  /* Return values: 0 success
46  -1 error decrypting pt session
47  */
48  int unxor(std::string const& path);
49 
50  struct wav_t {
51  std::string filename;
52  uint16_t index;
53 
54  int64_t posabsolute;
55  int64_t length;
56 
57  bool operator <(const struct wav_t& other) const {
58  return (strcasecmp(this->filename.c_str(),
59  other.filename.c_str()) < 0);
60  }
61 
62  bool operator ==(const struct wav_t& other) const {
63  return (this->filename == other.filename ||
64  this->index == other.index);
65  }
66 
67  wav_t (uint16_t idx = 0) : index (idx), posabsolute (0), length (0) {}
68  };
69 
70  struct midi_ev_t {
71  uint64_t pos;
72  uint64_t length;
73  uint8_t note;
74  uint8_t velocity;
75  midi_ev_t () : pos (0), length (0), note (0), velocity (0) {}
76  };
77 
78  struct region_t {
79  std::string name;
80  uint16_t index;
81  int64_t startpos;
82  int64_t sampleoffset;
83  int64_t length;
85  std::vector<midi_ev_t> midi;
86 
87  bool operator ==(const region_t& other) const {
88  return (this->index == other.index);
89  }
90 
91  bool operator <(const region_t& other) const {
92  return (strcasecmp(this->name.c_str(),
93  other.name.c_str()) < 0);
94  }
95  region_t (uint16_t idx = 0) : index (idx), startpos (0), sampleoffset (0), length (0) {}
96  };
97 
98  struct track_t {
99  std::string name;
100  uint16_t index;
101  uint8_t playlist;
103 
104  bool operator <(const track_t& other) const {
105  return (this->index < other.index);
106  }
107 
108  bool operator ==(const track_t& other) const {
109  return (this->index == other.index);
110  }
111  track_t (uint16_t idx = 0) : index (idx), playlist (0) {}
112  };
113 
114  bool find_track(uint16_t index, track_t& tt) const {
115  std::vector<track_t>::const_iterator begin = _tracks.begin();
116  std::vector<track_t>::const_iterator finish = _tracks.end();
117  std::vector<track_t>::const_iterator found;
118 
119  track_t t (index);
120 
121  if ((found = std::find(begin, finish, t)) != finish) {
122  tt = *found;
123  return true;
124  }
125  return false;
126  }
127 
128  bool find_region(uint16_t index, region_t& rr) const {
129  std::vector<region_t>::const_iterator begin = _regions.begin();
130  std::vector<region_t>::const_iterator finish = _regions.end();
131  std::vector<region_t>::const_iterator found;
132 
133  region_t r;
134  r.index = index;
135 
136  if ((found = std::find(begin, finish, r)) != finish) {
137  rr = *found;
138  return true;
139  }
140  return false;
141  }
142 
143  bool find_miditrack(uint16_t index, track_t& tt) const {
144  std::vector<track_t>::const_iterator begin = _miditracks.begin();
145  std::vector<track_t>::const_iterator finish = _miditracks.end();
146  std::vector<track_t>::const_iterator found;
147 
148  track_t t (index);
149 
150  if ((found = std::find(begin, finish, t)) != finish) {
151  tt = *found;
152  return true;
153  }
154  return false;
155  }
156 
157  bool find_midiregion(uint16_t index, region_t& rr) const {
158  std::vector<region_t>::const_iterator begin = _midiregions.begin();
159  std::vector<region_t>::const_iterator finish = _midiregions.end();
160  std::vector<region_t>::const_iterator found;
161 
162  region_t r (index);
163 
164  if ((found = std::find(begin, finish, r)) != finish) {
165  rr = *found;
166  return true;
167  }
168  return false;
169  }
170 
171  bool find_wav(uint16_t index, wav_t& ww) const {
172  std::vector<wav_t>::const_iterator begin = _audiofiles.begin();
173  std::vector<wav_t>::const_iterator finish = _audiofiles.end();
174  std::vector<wav_t>::const_iterator found;
175 
176  wav_t w (index);
177 
178  if ((found = std::find(begin, finish, w)) != finish) {
179  ww = *found;
180  return true;
181  }
182  return false;
183  }
184 
185  static bool regionexistsin(std::vector<region_t> const& reg, uint16_t index) {
186  std::vector<region_t>::const_iterator begin = reg.begin();
187  std::vector<region_t>::const_iterator finish = reg.end();
188 
189  region_t r (index);
190 
191  if (std::find(begin, finish, r) != finish) {
192  return true;
193  }
194  return false;
195  }
196 
197  static bool wavexistsin (std::vector<wav_t> const& wv, uint16_t index) {
198  std::vector<wav_t>::const_iterator begin = wv.begin();
199  std::vector<wav_t>::const_iterator finish = wv.end();
200 
201  wav_t w (index);
202 
203  if (std::find(begin, finish, w) != finish) {
204  return true;
205  }
206  return false;
207  }
208 
209  uint8_t version () const { return _version; }
210  int64_t sessionrate () const { return _sessionrate ; }
211  const std::string& path () { return _path; }
212 
213  const std::vector<wav_t>& audiofiles () const { return _audiofiles ; }
214  const std::vector<region_t>& regions () const { return _regions ; }
215  const std::vector<region_t>& midiregions () const { return _midiregions ; }
216  const std::vector<track_t>& tracks () const { return _tracks ; }
217  const std::vector<track_t>& miditracks () const { return _miditracks ; }
218 
219  const unsigned char* unxored_data () const { return _ptfunxored; }
220  uint64_t unxored_size () const { return _len; }
221 
222 private:
223 
224  std::vector<wav_t> _audiofiles;
225  std::vector<region_t> _regions;
226  std::vector<region_t> _midiregions;
227  std::vector<track_t> _tracks;
228  std::vector<track_t> _miditracks;
229 
230  std::string _path;
231 
232  unsigned char* _ptfunxored;
233  uint64_t _len;
234  int64_t _sessionrate;
235  uint8_t _version;
236  uint8_t* _product;
237  int64_t _targetrate;
238  float _ratefactor;
240 
241  struct block_t {
242  uint8_t zmark; // 'Z'
243  uint16_t block_type; // type of block
244  uint32_t block_size; // size of block
245  uint16_t content_type; // type of content
246  uint32_t offset; // offset in file
247  std::vector<block_t> child; // vector of child blocks
248  };
249  std::vector<block_t> blocks;
250 
251  bool jumpback(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
252  bool jumpto(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
253  bool foundin(std::string const& haystack, std::string const& needle);
254  int64_t foundat(unsigned char *haystack, uint64_t n, const char *needle);
255 
256  std::string parsestring(uint32_t pos);
257  const std::string get_content_description(uint16_t ctype);
258  int parse(void);
259  void parseblocks(void);
260  bool parseheader(void);
261  bool parserest(void);
262  bool parseaudio(void);
263  bool parsemidi(void);
264  void dump(void);
265  bool parse_block_at(uint32_t pos, struct block_t *b, struct block_t *parent, int level);
266  void dump_block(struct block_t& b, int level);
268  void parse_region_info(uint32_t j, block_t& blk, region_t& r);
269  void parse_three_point(uint32_t j, uint64_t& start, uint64_t& offset, uint64_t& length);
270  uint8_t gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative);
271  void setrates(void);
272  void cleanup(void);
273  void free_block(struct block_t& b);
274  void free_all_blocks(void);
275 };
276 
277 #endif
void dump(void)
const std::vector< region_t > & midiregions() const
Definition: ptformat.h:215
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:230
std::vector< wav_t > _audiofiles
Definition: ptformat.h:224
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:185
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:238
bool parsemidi(void)
const std::vector< region_t > & regions() const
Definition: ptformat.h:214
static bool wavexistsin(std::vector< wav_t > const &wv, uint16_t index)
Definition: ptformat.h:197
bool find_wav(uint16_t index, wav_t &ww) const
Definition: ptformat.h:171
bool is_bigendian
Definition: ptformat.h:239
int64_t _sessionrate
Definition: ptformat.h:234
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:211
uint8_t _version
Definition: ptformat.h:235
bool find_region(uint16_t index, region_t &rr) const
Definition: ptformat.h:128
uint8_t version() const
Definition: ptformat.h:209
std::vector< region_t > _regions
Definition: ptformat.h:225
std::vector< track_t > _miditracks
Definition: ptformat.h:228
int64_t foundat(unsigned char *haystack, uint64_t n, const char *needle)
std::vector< block_t > blocks
Definition: ptformat.h:249
const std::vector< wav_t > & audiofiles() const
Definition: ptformat.h:213
uint8_t * _product
Definition: ptformat.h:236
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:143
bool foundin(std::string const &haystack, std::string const &needle)
bool find_track(uint16_t index, track_t &tt) const
Definition: ptformat.h:114
const unsigned char * unxored_data() const
Definition: ptformat.h:219
int64_t _targetrate
Definition: ptformat.h:237
const std::vector< track_t > & tracks() const
Definition: ptformat.h:216
unsigned char * _ptfunxored
Definition: ptformat.h:232
void cleanup(void)
void parseblocks(void)
int64_t sessionrate() const
Definition: ptformat.h:210
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:226
uint64_t unxored_size() const
Definition: ptformat.h:220
int unxor(std::string const &path)
bool find_midiregion(uint16_t index, region_t &rr) const
Definition: ptformat.h:157
uint64_t _len
Definition: ptformat.h:233
const std::vector< track_t > & miditracks() const
Definition: ptformat.h:217
std::vector< track_t > _tracks
Definition: ptformat.h:227
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:246
uint32_t block_size
Definition: ptformat.h:244
uint16_t block_type
Definition: ptformat.h:243
uint16_t content_type
Definition: ptformat.h:245
std::vector< block_t > child
Definition: ptformat.h:247
int64_t sampleoffset
Definition: ptformat.h:82
int64_t startpos
Definition: ptformat.h:81
std::string name
Definition: ptformat.h:79
uint16_t index
Definition: ptformat.h:80
region_t(uint16_t idx=0)
Definition: ptformat.h:95
std::vector< midi_ev_t > midi
Definition: ptformat.h:85
std::string name
Definition: ptformat.h:99
track_t(uint16_t idx=0)
Definition: ptformat.h:111
uint16_t index
Definition: ptformat.h:100
uint8_t playlist
Definition: ptformat.h:101
int64_t posabsolute
Definition: ptformat.h:54
int64_t length
Definition: ptformat.h:55
std::string filename
Definition: ptformat.h:51
uint16_t index
Definition: ptformat.h:52
wav_t(uint16_t idx=0)
Definition: ptformat.h:67
link region and track false waveform clip level