Prepare export-format codec-quality setting
[ardour.git] / libs / ardour / export_format_specification.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/export_format_specification.h"
22
23 #include "ardour/export_format_compatibility.h"
24 #include "ardour/export_formats.h"
25 #include "ardour/session.h"
26 #include "ardour/types_convert.h"
27
28 #include "pbd/error.h"
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
31 #include "pbd/enum_convert.h"
32 #include "pbd/string_convert.h"
33 #include "pbd/types_convert.h"
34
35 #include "pbd/i18n.h"
36
37 namespace PBD {
38         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::FormatId)
39         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SampleRate)
40         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SampleFormat)
41         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::DitherType)
42         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SRCQuality)
43         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::Type)
44 }
45
46 namespace ARDOUR
47 {
48
49 using namespace PBD;
50 using std::string;
51 using std::list;
52
53 ExportFormatSpecification::Time &
54 ExportFormatSpecification::Time::operator= (AnyTime const & other)
55 {
56         static_cast<AnyTime &>(*this) = other;
57         return *this;
58 }
59
60 samplecnt_t
61 ExportFormatSpecification::Time::get_samples_at (samplepos_t position, samplecnt_t target_rate) const
62 {
63         samplecnt_t duration = session.any_duration_to_samples (position, *this);
64         return ((double) target_rate / session.sample_rate()) * duration + 0.5;
65 }
66
67 XMLNode &
68 ExportFormatSpecification::Time::get_state ()
69 {
70
71         XMLNode * node = new XMLNode ("Duration");
72
73         node->set_property ("format", type);
74
75         switch (type) {
76           case Timecode:
77                 node->set_property ("hours", timecode.hours);
78                 node->set_property ("minutes", timecode.minutes);
79                 node->set_property ("seconds", timecode.seconds);
80                 node->set_property ("frames", timecode.frames);
81                 break;
82           case BBT:
83                 node->set_property ("bars", bbt.bars);
84                 node->set_property ("beats", bbt.beats);
85                 node->set_property ("ticks", bbt.ticks);
86                 break;
87           case Samples:
88                 node->set_property ("samples", samples);
89                 break;
90           case Seconds:
91                 node->set_property ("seconds", seconds);
92                 break;
93         }
94
95         return *node;
96 }
97
98 int
99 ExportFormatSpecification::Time::set_state (const XMLNode & node)
100 {
101         if (!node.get_property ("format", type)) {
102                 return -1;
103         }
104
105         switch (type) {
106         case Timecode:
107                 node.get_property ("hours", timecode.hours);
108                 node.get_property ("minutes", timecode.minutes);
109                 node.get_property ("seconds", timecode.seconds);
110                 node.get_property ("frames", timecode.frames);
111                 break;
112
113         case BBT:
114                 node.get_property ("bars", bbt.bars);
115                 node.get_property ("beats", bbt.beats);
116                 node.get_property ("ticks", bbt.ticks);
117                 break;
118
119         case Samples:
120                 node.get_property ("samples", samples);
121                 break;
122
123         case Seconds:
124                 node.get_property ("seconds", seconds);
125                 break;
126
127         }
128
129         return 0;
130 }
131
132 ExportFormatSpecification::ExportFormatSpecification (Session & s)
133         : session (s)
134
135         , has_sample_format (false)
136         , supports_tagging (false)
137         , _has_broadcast_info (false)
138         , _channel_limit (0)
139         , _dither_type (D_None)
140         , _src_quality (SRC_SincBest)
141         , _tag (true)
142
143         , _trim_beginning (false)
144         , _silence_beginning (s)
145         , _trim_end (false)
146         , _silence_end (s)
147
148         , _normalize (false)
149         , _normalize_loudness (false)
150         , _normalize_dbfs (GAIN_COEFF_UNITY)
151         , _normalize_lufs (-23)
152         , _normalize_dbtp (-1)
153         , _with_toc (false)
154         , _with_cue (false)
155         , _with_mp4chaps (false)
156         , _soundcloud_upload (false)
157         , _command ("")
158         , _analyse (true)
159         , _codec_quality (-3)
160 {
161         format_ids.insert (F_None);
162         endiannesses.insert (E_FileDefault);
163         sample_formats.insert (SF_None);
164         sample_rates.insert (SR_None);
165         qualities.insert (Q_None);
166 }
167
168 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
169         : session (s)
170
171         , has_sample_format (false)
172         , supports_tagging (false)
173         , _has_broadcast_info (false)
174         , _channel_limit (0)
175         , _dither_type (D_None)
176         , _src_quality (SRC_SincBest)
177         , _tag (true)
178
179         , _trim_beginning (false)
180         , _silence_beginning (s)
181         , _trim_end (false)
182         , _silence_end (s)
183
184         , _normalize (false)
185         , _normalize_loudness (false)
186         , _normalize_dbfs (GAIN_COEFF_UNITY)
187         , _normalize_lufs (-23)
188         , _normalize_dbtp (-1)
189         , _with_toc (false)
190         , _with_cue (false)
191         , _with_mp4chaps (false)
192         , _soundcloud_upload (false)
193         , _command ("")
194         , _analyse (true)
195         , _codec_quality (-3)
196 {
197         _silence_beginning.type = Time::Timecode;
198         _silence_end.type = Time::Timecode;
199
200         set_state (state);
201 }
202
203 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other, bool modify_name)
204         : ExportFormatBase(other)
205         , session (other.session)
206         , _silence_beginning (other.session)
207         , _silence_end (other.session)
208         , _soundcloud_upload (false)
209         , _analyse (other._analyse)
210         , _codec_quality (other._codec_quality)
211 {
212         if (modify_name) {
213                 set_name (other.name() + " (copy)");
214         } else {
215                 set_name (other.name());
216         }
217
218         _format_name = other._format_name;
219         has_sample_format = other.has_sample_format;
220
221         supports_tagging = other.supports_tagging;
222         _has_broadcast_info = other._has_broadcast_info;
223         _channel_limit = other._channel_limit;
224
225         set_type (other.type());
226         set_format_id (other.format_id());
227         set_endianness (other.endianness());
228         set_sample_format (other.sample_format());
229         set_sample_rate (other.sample_rate());
230         set_quality (other.quality());
231
232         set_dither_type (other.dither_type());
233         set_src_quality (other.src_quality());
234         set_trim_beginning (other.trim_beginning());
235         set_trim_end (other.trim_end());
236         set_normalize (other.normalize());
237         set_normalize_loudness (other.normalize_loudness());
238         set_normalize_dbfs (other.normalize_dbfs());
239         set_normalize_lufs (other.normalize_lufs());
240         set_normalize_dbtp (other.normalize_dbtp());
241
242         set_tag (other.tag());
243
244         set_silence_beginning (other.silence_beginning_time());
245         set_silence_end (other.silence_end_time());
246
247         set_extension(other.extension());
248 }
249
250 ExportFormatSpecification::~ExportFormatSpecification ()
251 {
252 }
253
254 XMLNode &
255 ExportFormatSpecification::get_state ()
256 {
257         XMLNode * node;
258         XMLNode * root = new XMLNode ("ExportFormatSpecification");
259
260         root->set_property ("name", _name);
261         root->set_property ("id", _id.to_s());
262         root->set_property ("with-cue", _with_cue);
263         root->set_property ("with-toc", _with_toc);
264         root->set_property ("with-mp4chaps", _with_mp4chaps);
265         root->set_property ("command", _command);
266         root->set_property ("analyse", _analyse);
267         root->set_property ("soundcloud-upload", _soundcloud_upload);
268
269         node = root->add_child ("Encoding");
270         node->set_property ("id", format_id());
271         node->set_property ("type", type());
272         node->set_property ("extension", extension());
273         node->set_property ("name", _format_name);
274         node->set_property ("has-sample-format", has_sample_format);
275         node->set_property ("channel-limit", _channel_limit);
276
277         node = root->add_child ("SampleRate");
278         node->set_property ("rate", sample_rate());
279
280         node = root->add_child ("SRCQuality");
281         node->set_property ("quality", src_quality());
282
283         node = root->add_child ("CodecQuality");
284         node->set_property ("quality", codec_quality());
285
286         XMLNode * enc_opts = root->add_child ("EncodingOptions");
287
288         add_option (enc_opts, "sample-format", to_string(sample_format()));
289         add_option (enc_opts, "dithering", to_string (dither_type()));
290         add_option (enc_opts, "tag-metadata", to_string (_tag));
291         add_option (enc_opts, "tag-support", to_string (supports_tagging));
292         add_option (enc_opts, "broadcast-info", to_string (_has_broadcast_info));
293
294         XMLNode * processing = root->add_child ("Processing");
295
296         node = processing->add_child ("Normalize");
297         node->set_property ("enabled", normalize());
298         node->set_property ("loudness", normalize_loudness());
299         node->set_property ("dbfs", normalize_dbfs());
300         node->set_property ("lufs", normalize_lufs());
301         node->set_property ("dbtp", normalize_dbtp());
302
303         XMLNode * silence = processing->add_child ("Silence");
304         XMLNode * start = silence->add_child ("Start");
305         XMLNode * end = silence->add_child ("End");
306
307         node = start->add_child ("Trim");
308         node->set_property ("enabled", trim_beginning());
309
310         node = start->add_child ("Add");
311         node->set_property ("enabled", _silence_beginning.not_zero());
312         node->add_child_nocopy (_silence_beginning.get_state());
313
314         node = end->add_child ("Trim");
315         node->set_property ("enabled", trim_end());
316
317         node = end->add_child ("Add");
318         node->set_property ("enabled", _silence_end.not_zero());
319         node->add_child_nocopy (_silence_end.get_state());
320
321         return *root;
322 }
323
324 int
325 ExportFormatSpecification::set_state (const XMLNode & root)
326 {
327         XMLNode const * child;
328         string str;
329
330         root.get_property ("name", _name);
331
332         if (root.get_property ("id", str)) {
333                 _id = str;
334         }
335
336         if (!root.get_property ("with-cue", _with_cue)) {
337                 _with_cue = false;
338         }
339
340         if (!root.get_property ("with-toc", _with_toc)) {
341                 _with_toc = false;
342         }
343
344         if (!root.get_property ("with-mp4chaps", _with_mp4chaps)) {
345                 _with_mp4chaps = false;
346         }
347
348         if (!root.get_property ("command", _command)) {
349                 _command = "";
350         }
351
352         if (!root.get_property ("analyse", _analyse)) {
353                 _analyse = false;
354         }
355
356         if (!root.get_property ("soundcloud-upload", _soundcloud_upload)) {
357                 _soundcloud_upload = false;
358         }
359
360         /* Encoding and SRC */
361
362         if ((child = root.child ("Encoding"))) {
363                 FormatId fid;
364                 if (child->get_property ("id", fid)) {
365                         set_format_id (fid);
366                 }
367
368                 ExportFormatBase::Type type;
369                 if (child->get_property ("type", type)) {
370                         set_type (type);
371                 }
372
373                 if (child->get_property ("extension", str)) {
374                         set_extension (str);
375                 }
376
377                 child->get_property ("name", _format_name);
378                 child->get_property ("has-sample-format", has_sample_format);
379                 child->get_property ("channel-limit", _channel_limit);
380         }
381
382         if ((child = root.child ("SampleRate"))) {
383                 SampleRate rate;
384                 if (child->get_property ("rate", rate)) {
385                         set_sample_rate (rate);
386                 }
387         }
388
389         if ((child = root.child ("SRCQuality"))) {
390                 child->get_property ("quality", _src_quality);
391         }
392
393         if ((child = root.child ("CodecQuality"))) {
394                 child->get_property ("quality", _codec_quality);
395         }
396
397         /* Encoding options */
398
399         if ((child = root.child ("EncodingOptions"))) {
400                 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
401                 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
402                 set_tag (string_to<bool>(get_option (child, "tag-metadata")));
403                 supports_tagging = string_to<bool>(get_option (child, "tag-support"));
404                 _has_broadcast_info = string_to<bool>(get_option (child, "broadcast-info"));
405         }
406
407         /* Processing */
408
409         XMLNode const * proc = root.child ("Processing");
410         if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
411
412         if ((child = proc->child ("Normalize"))) {
413                 child->get_property ("enabled", _normalize);
414                 // old formats before ~ 4.7-930ish
415                 child->get_property ("target", _normalize_dbfs);
416                 child->get_property ("loudness", _normalize_loudness);
417                 child->get_property ("dbfs", _normalize_dbfs);
418                 child->get_property ("lufs", _normalize_lufs);
419                 child->get_property ("dbtp", _normalize_dbtp);
420         }
421
422         XMLNode const * silence = proc->child ("Silence");
423         if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
424
425         XMLNode const * start = silence->child ("Start");
426         XMLNode const * end = silence->child ("End");
427         if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
428
429         /* Silence start */
430
431         if ((child = start->child ("Trim"))) {
432                 child->get_property ("enabled", _trim_beginning);
433         }
434
435         bool enabled;
436         if ((child = start->child ("Add"))) {
437                 if (child->get_property ("enabled", enabled) && enabled) {
438                         if ((child = child->child ("Duration"))) {
439                                 _silence_beginning.set_state (*child);
440                         }
441                 } else {
442                         _silence_beginning.type = Time::Timecode;
443                 }
444         }
445
446         /* Silence end */
447
448         if ((child = end->child ("Trim"))) {
449                 child->get_property ("enabled", _trim_end);
450         }
451
452         if ((child = end->child ("Add"))) {
453                 if (child->get_property ("enabled", enabled) && enabled) {
454                         if ((child = child->child ("Duration"))) {
455                                 _silence_end.set_state (*child);
456                         }
457                 } else {
458                                 _silence_end.type = Time::Timecode;
459                 }
460         }
461
462         return 0;
463 }
464
465 bool
466 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
467 {
468         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
469
470         if (intersection->formats_empty() && format_id() != 0) {
471                 return false;
472         }
473
474         if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
475                 return false;
476         }
477
478         if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
479                 return false;
480         }
481
482         if (intersection->sample_formats_empty() && sample_format() != SF_None) {
483                 return false;
484         }
485
486         if (intersection->qualities_empty() && quality() != Q_None) {
487                 return false;
488         }
489
490         return true;
491 }
492
493 bool
494 ExportFormatSpecification::is_complete () const
495 {
496         if (type() == T_None) {
497                 return false;
498         }
499
500         if (!format_id()) {
501                 return false;
502         }
503
504         if (!sample_rate()) {
505                 return false;
506         }
507
508         if (has_sample_format) {
509                 if (sample_format() == SF_None) {
510                         return false;
511                 }
512         }
513
514         return true;
515 }
516
517 void
518 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
519 {
520         if (format) {
521                 set_format_id (format->get_format_id ());
522                 set_type (format->get_type());
523                 set_extension (format->extension());
524
525                 if (format->get_explicit_sample_format()) {
526                         set_sample_format (format->get_explicit_sample_format());
527                 }
528
529                 if (format->has_sample_format()) {
530                         has_sample_format = true;
531                 }
532
533                 if (format->has_broadcast_info()) {
534                         _has_broadcast_info = true;
535                 }
536
537                 supports_tagging = format->supports_tagging ();
538                 _channel_limit = format->get_channel_limit();
539
540                 _format_name = format->name();
541         } else {
542                 set_format_id (F_None);
543                 set_type (T_None);
544                 set_extension ("");
545                 _has_broadcast_info = false;
546                 has_sample_format = false;
547                 supports_tagging = false;
548                 _channel_limit = 0;
549                 _format_name = "";
550         }
551 }
552
553 string
554 ExportFormatSpecification::description (bool include_name)
555 {
556         list<string> components;
557
558         if (_normalize) {
559                 if (_normalize_loudness) {
560                         components.push_back (_("normalize loudness"));
561                 } else {
562                         components.push_back (_("normalize peak"));
563                 }
564         }
565
566         if (_trim_beginning && _trim_end) {
567                 components.push_back ( _("trim"));
568         } else if (_trim_beginning) {
569                 components.push_back (_("trim start"));
570         } else if (_trim_end) {
571                 components.push_back (_("trim end"));
572         }
573
574         if (_format_name != "") {
575                 components.push_back (_format_name);
576         }
577
578         if (has_sample_format) {
579                 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
580         }
581
582         switch (sample_rate()) {
583         case SR_8:
584                 components.push_back ("8 kHz");
585                 break;
586         case SR_22_05:
587                 components.push_back ("22,5 kHz");
588                 break;
589         case SR_44_1:
590                 components.push_back ("44,1 kHz");
591                 break;
592         case SR_48:
593                 components.push_back ("48 kHz");
594                 break;
595         case SR_88_2:
596                 components.push_back ("88,2 kHz");
597                 break;
598         case SR_96:
599                 components.push_back ("96 kHz");
600                 break;
601         case SR_176_4:
602                 components.push_back ("176.4 kHz");
603                 break;
604         case SR_192:
605                 components.push_back ("192 kHz");
606                 break;
607         case SR_Session:
608                 components.push_back (_("Session rate"));
609                 break;
610         case SR_None:
611                 break;
612         }
613
614         if (_with_toc) {
615                 components.push_back ("TOC");
616         }
617
618         if (_with_cue) {
619                 components.push_back ("CUE");
620         }
621
622         if (_with_mp4chaps) {
623                 components.push_back ("MP4ch");
624         }
625
626         if (!_command.empty()) {
627                 components.push_back ("+");
628         }
629
630         string desc;
631         if (include_name) {
632                 desc = _name + ": ";
633         }
634
635         for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
636                 if (it != components.begin()) { desc += ", "; }
637                 desc += *it;
638         }
639         return desc;
640 }
641
642 void
643 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
644 {
645         node = node->add_child ("Option");
646         node->set_property ("name", name);
647         node->set_property ("value", value);
648 }
649
650 std::string
651 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
652 {
653         XMLNodeList list (node->children ("Option"));
654
655         for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
656                 std::string str;
657                 if ((*it)->get_property ("name", str) && name == str) {
658                         if ((*it)->get_property ("value", str)) {
659                                 return str;
660                         }
661                 }
662         }
663
664         std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
665
666         return "";
667 }
668
669 }; // namespace ARDOUR