Allow specification of video bit rate separately for J2K and MPEG2.
[dcpomatic.git] / src / wx / dcp_panel.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "audio_dialog.h"
23 #include "check_box.h"
24 #include "check_box.h"
25 #include "dcp_panel.h"
26 #include "dcp_timeline_dialog.h"
27 #include "dcpomatic_button.h"
28 #include "dcpomatic_choice.h"
29 #include "dcpomatic_spin_ctrl.h"
30 #include "focus_manager.h"
31 #include "interop_metadata_dialog.h"
32 #include "language_tag_dialog.h"
33 #include "markers_dialog.h"
34 #include "smpte_metadata_dialog.h"
35 #include "static_text.h"
36 #include "wx_util.h"
37 #include "lib/audio_content.h"
38 #include "lib/audio_processor.h"
39 #include "lib/config.h"
40 #include "lib/dcp_content.h"
41 #include "lib/dcp_content_type.h"
42 #include "lib/ffmpeg_content.h"
43 #include "lib/film.h"
44 #include "lib/ratio.h"
45 #include "lib/text_content.h"
46 #include "lib/util.h"
47 #include "lib/video_content.h"
48 #include <dcp/locale_convert.h>
49 #include <dcp/warnings.h>
50 LIBDCP_DISABLE_WARNINGS
51 #include <wx/gbsizer.h>
52 #include <wx/notebook.h>
53 #include <wx/spinctrl.h>
54 #include <wx/wx.h>
55 LIBDCP_ENABLE_WARNINGS
56 #include <boost/lexical_cast.hpp>
57
58
59 using std::list;
60 using std::make_pair;
61 using std::max;
62 using std::pair;
63 using std::shared_ptr;
64 using std::string;
65 using std::vector;
66 using std::weak_ptr;
67 using boost::lexical_cast;
68 #if BOOST_VERSION >= 106100
69 using namespace boost::placeholders;
70 #endif
71 using dcp::locale_convert;
72
73
74 DCPPanel::DCPPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
75         : _film (film)
76         , _viewer (viewer)
77         , _generally_sensitive (true)
78 {
79         _panel = new wxPanel (n);
80         _sizer = new wxBoxSizer (wxVERTICAL);
81         _panel->SetSizer (_sizer);
82
83         _grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
84         _sizer->Add (_grid, 0, wxEXPAND | wxALL, 8);
85
86         _name_label = create_label (_panel, _("Name"), true);
87         _name = new wxTextCtrl (_panel, wxID_ANY);
88         FocusManager::instance()->add(_name);
89
90         _use_isdcf_name = new CheckBox (_panel, _("Use ISDCF name"));
91         _copy_isdcf_name_button = new Button (_panel, _("Copy as name"));
92
93         /* wxST_ELLIPSIZE_MIDDLE works around a bug in GTK2 and/or wxWidgets, see
94            http://trac.wxwidgets.org/ticket/12539
95         */
96         _dcp_name = new StaticText (
97                 _panel, wxT (""), wxDefaultPosition, wxDefaultSize,
98                 wxALIGN_CENTRE_HORIZONTAL | wxST_NO_AUTORESIZE | wxST_ELLIPSIZE_MIDDLE
99                 );
100
101         _dcp_content_type_label = create_label (_panel, _("Content Type"), true);
102         _dcp_content_type = new Choice(_panel);
103
104         _encrypted = new CheckBox (_panel, _("Encrypted"));
105
106         wxClientDC dc (_panel);
107         auto size = dc.GetTextExtent (wxT ("GGGGGGGG..."));
108         size.SetHeight (-1);
109
110         _standard_label = create_label (_panel, _("Standard"), true);
111         _standard = new Choice(_panel);
112
113         _markers = new Button (_panel, _("Markers..."));
114         _metadata = new Button (_panel, _("Metadata..."));
115         _reels = new Button(_panel, _("Reels..."));
116
117         _notebook = new wxNotebook (_panel, wxID_ANY);
118         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
119
120         _notebook->AddPage (make_video_panel (), _("Video"), false);
121         _notebook->AddPage (make_audio_panel (), _("Audio"), false);
122
123         _name->Bind                  (wxEVT_TEXT,     boost::bind(&DCPPanel::name_changed, this));
124         _use_isdcf_name->bind(&DCPPanel::use_isdcf_name_toggled, this);
125         _copy_isdcf_name_button->Bind(wxEVT_BUTTON,   boost::bind(&DCPPanel::copy_isdcf_name_button_clicked, this));
126         _dcp_content_type->Bind      (wxEVT_CHOICE,   boost::bind(&DCPPanel::dcp_content_type_changed, this));
127         _encrypted->bind(&DCPPanel::encrypted_toggled, this);
128         _standard->Bind              (wxEVT_CHOICE,   boost::bind(&DCPPanel::standard_changed, this));
129         _markers->Bind               (wxEVT_BUTTON,   boost::bind(&DCPPanel::markers_clicked, this));
130         _metadata->Bind              (wxEVT_BUTTON,   boost::bind(&DCPPanel::metadata_clicked, this));
131         _reels->Bind(wxEVT_BUTTON, boost::bind(&DCPPanel::reels_clicked, this));
132
133         for (auto i: DCPContentType::all()) {
134                 _dcp_content_type->add_entry(i->pretty_name());
135         }
136
137         add_standards();
138         _standard->SetToolTip(_("The standard that the DCP should use.  Interop is older, and SMPTE is the newer (current) standard.  If in doubt, choose 'SMPTE'"));
139
140         Config::instance()->Changed.connect (boost::bind(&DCPPanel::config_changed, this, _1));
141
142         add_to_grid ();
143 }
144
145
146 void
147 DCPPanel::add_standards()
148 {
149         _standard->add_entry(_("SMPTE"), N_("smpte"));
150         if (Config::instance()->allow_smpte_bv20() || (_film && _film->limit_to_smpte_bv20())) {
151                 _standard->add_entry(_("SMPTE (Bv2.0 only)"), N_("smpte-bv20"));
152         }
153         _standard->add_entry(_("Interop"), N_("interop"));
154         _standard->add_entry(_("MPEG2 Interop"), N_("mpeg2-interop"));
155         _sizer->Layout();
156 }
157
158
159 void
160 DCPPanel::set_standard()
161 {
162         DCPOMATIC_ASSERT(_film);
163         DCPOMATIC_ASSERT(!_film->limit_to_smpte_bv20() || _standard->GetCount() == 3);
164
165         if (_film->interop()) {
166                 if (_film->video_encoding() == VideoEncoding::JPEG2000) {
167                         checked_set(_standard, "interop");
168                 } else {
169                         checked_set(_standard, "mpeg2-interop");
170                 }
171         } else {
172                 checked_set(_standard, _film->limit_to_smpte_bv20() ? "smpte-bv20" : "smpte");
173         }
174 }
175
176
177 void
178 DCPPanel::standard_changed ()
179 {
180         if (!_film || !_standard->get()) {
181                 return;
182         }
183
184         auto const data = _standard->get_data();
185         if (!data) {
186                 return;
187         }
188
189         if (*data == N_("interop")) {
190                 _film->set_interop(true);
191                 _film->set_limit_to_smpte_bv20(false);
192                 _film->set_video_encoding(VideoEncoding::JPEG2000);
193         } else if (*data == N_("smpte")) {
194                 _film->set_interop(false);
195                 _film->set_limit_to_smpte_bv20(false);
196                 _film->set_video_encoding(VideoEncoding::JPEG2000);
197         } else if (*data == N_("smpte-bv20")) {
198                 _film->set_interop(false);
199                 _film->set_limit_to_smpte_bv20(true);
200                 _film->set_video_encoding(VideoEncoding::JPEG2000);
201         } else if (*data == N_("mpeg2-interop")) {
202                 _film->set_interop(true);
203                 _film->set_video_encoding(VideoEncoding::MPEG2);
204         }
205 }
206
207
208 void
209 DCPPanel::add_to_grid ()
210 {
211         int r = 0;
212
213         auto name_sizer = new wxBoxSizer (wxHORIZONTAL);
214         name_sizer->Add (_name_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
215         name_sizer->Add (_name, 1, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
216         _grid->Add (name_sizer, wxGBPosition(r, 0), wxGBSpan(1, 2), wxRIGHT | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
217         ++r;
218
219         int flags = wxALIGN_CENTER_VERTICAL;
220 #ifdef __WXOSX__
221         flags |= wxALIGN_RIGHT;
222 #endif
223
224         _grid->Add (_use_isdcf_name, wxGBPosition(r, 0), wxDefaultSpan, flags);
225         {
226                 auto s = new wxBoxSizer (wxHORIZONTAL);
227                 s->Add (_copy_isdcf_name_button, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
228                 _grid->Add (s, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxBOTTOM, DCPOMATIC_CHECKBOX_BOTTOM_PAD);
229         }
230         ++r;
231
232         _grid->Add (_dcp_name, wxGBPosition(r, 0), wxGBSpan(1, 2), wxALIGN_CENTER_VERTICAL | wxEXPAND);
233         ++r;
234
235         add_label_to_sizer (_grid, _dcp_content_type_label, true, wxGBPosition(r, 0));
236         _grid->Add (_dcp_content_type, wxGBPosition(r, 1));
237         ++r;
238
239         _grid->Add (_encrypted, wxGBPosition(r, 0), wxGBSpan(1, 2));
240         ++r;
241
242         add_label_to_sizer (_grid, _standard_label, true, wxGBPosition(r, 0));
243         _grid->Add (_standard, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
244         ++r;
245
246         auto extra = new wxBoxSizer (wxHORIZONTAL);
247         extra->Add (_markers, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
248         extra->Add (_metadata, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
249         extra->Add(_reels, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
250         _grid->Add (extra, wxGBPosition(r, 0), wxGBSpan(1, 2));
251         ++r;
252 }
253
254
255 void
256 DCPPanel::name_changed ()
257 {
258         if (!_film) {
259                 return;
260         }
261
262         _film->set_name (string(_name->GetValue().mb_str()));
263 }
264
265
266 void
267 DCPPanel::video_bit_rate_changed()
268 {
269         if (!_film) {
270                 return;
271         }
272
273         _film->set_video_bit_rate(_film->video_encoding(), _video_bit_rate->GetValue() * 1000000);
274 }
275
276
277 void
278 DCPPanel::encrypted_toggled ()
279 {
280         if (!_film) {
281                 return;
282         }
283
284         _film->set_encrypted (_encrypted->GetValue());
285 }
286
287
288 /** Called when the frame rate choice widget has been changed */
289 void
290 DCPPanel::frame_rate_choice_changed ()
291 {
292         if (!_film || !_frame_rate_choice->get()) {
293                 return;
294         }
295
296         _film->set_video_frame_rate (
297                 boost::lexical_cast<int>(
298                         wx_to_std(_frame_rate_choice->GetString(*_frame_rate_choice->get()))
299                         ),
300                 true
301                 );
302 }
303
304
305 /** Called when the frame rate spin widget has been changed */
306 void
307 DCPPanel::frame_rate_spin_changed ()
308 {
309         if (!_film) {
310                 return;
311         }
312
313         _film->set_video_frame_rate (_frame_rate_spin->GetValue());
314 }
315
316
317 void
318 DCPPanel::audio_channels_changed ()
319 {
320         if (!_film) {
321                 return;
322         }
323
324         _film->set_audio_channels(locale_convert<int>(string_client_data(_audio_channels->GetClientObject(*_audio_channels->get()))));
325 }
326
327
328 void
329 DCPPanel::resolution_changed ()
330 {
331         if (!_film || !_resolution->get()) {
332                 return;
333         }
334
335         _film->set_resolution(*_resolution->get() == 0 ? Resolution::TWO_K : Resolution::FOUR_K);
336 }
337
338
339 void
340 DCPPanel::markers_clicked ()
341 {
342         _markers_dialog.reset(_panel, _film, _viewer);
343         _markers_dialog->Show();
344 }
345
346
347 void
348 DCPPanel::metadata_clicked ()
349 {
350         if (_film->interop()) {
351                 _interop_metadata_dialog.reset(_panel, _film);
352                 _interop_metadata_dialog->setup ();
353                 _interop_metadata_dialog->Show ();
354         } else {
355                 _smpte_metadata_dialog.reset(_panel, _film);
356                 _smpte_metadata_dialog->setup ();
357                 _smpte_metadata_dialog->Show ();
358         }
359 }
360
361
362 void
363 DCPPanel::reels_clicked()
364 {
365         _dcp_timeline.reset(_panel, _film);
366         _dcp_timeline->Show();
367 }
368
369
370 void
371 DCPPanel::film_changed(FilmProperty p)
372 {
373         switch (p) {
374         case FilmProperty::NONE:
375                 break;
376         case FilmProperty::CONTAINER:
377                 setup_container ();
378                 break;
379         case FilmProperty::NAME:
380                 checked_set (_name, _film->name());
381                 setup_dcp_name ();
382                 break;
383         case FilmProperty::DCP_CONTENT_TYPE:
384         {
385                 auto index = DCPContentType::as_index(_film->dcp_content_type());
386                 DCPOMATIC_ASSERT (index);
387                 checked_set (_dcp_content_type, *index);
388                 setup_dcp_name ();
389                 break;
390         }
391         case FilmProperty::ENCRYPTED:
392                 checked_set (_encrypted, _film->encrypted ());
393                 break;
394         case FilmProperty::RESOLUTION:
395                 checked_set (_resolution, _film->resolution() == Resolution::TWO_K ? 0 : 1);
396                 setup_container ();
397                 setup_dcp_name ();
398                 break;
399         case FilmProperty::VIDEO_BIT_RATE:
400                 checked_set(_video_bit_rate, _film->video_bit_rate(_film->video_encoding()) / 1000000);
401                 break;
402         case FilmProperty::USE_ISDCF_NAME:
403         {
404                 checked_set (_use_isdcf_name, _film->use_isdcf_name());
405                 if (_film->use_isdcf_name()) {
406                         /* We are going back to using an ISDCF name.  Remove anything after a _ in the current name,
407                            in case the user has clicked 'Copy as name' then re-ticked 'Use ISDCF name' (#1513).
408                         */
409                         string const name = _film->name ();
410                         string::size_type const u = name.find("_");
411                         if (u != string::npos) {
412                                 _film->set_name (name.substr(0, u));
413                         }
414                 }
415                 setup_dcp_name ();
416                 break;
417         }
418         case FilmProperty::VIDEO_FRAME_RATE:
419         {
420                 bool done = false;
421                 for (unsigned int i = 0; i < _frame_rate_choice->GetCount(); ++i) {
422                         if (wx_to_std(_frame_rate_choice->GetString(i)) == boost::lexical_cast<string>(_film->video_frame_rate())) {
423                                 checked_set (_frame_rate_choice, i);
424                                 done = true;
425                                 break;
426                         }
427                 }
428
429                 if (!done) {
430                         checked_set (_frame_rate_choice, -1);
431                 }
432
433                 checked_set (_frame_rate_spin, _film->video_frame_rate ());
434
435                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
436                 setup_dcp_name ();
437                 break;
438         }
439         case FilmProperty::AUDIO_CHANNELS:
440                 if (_film->audio_channels() < minimum_allowed_audio_channels()) {
441                         _film->set_audio_channels (minimum_allowed_audio_channels());
442                 } else {
443                         checked_set (_audio_channels, locale_convert<string>(max(minimum_allowed_audio_channels(), _film->audio_channels())));
444                         setup_dcp_name ();
445                 }
446                 break;
447         case FilmProperty::THREE_D:
448                 checked_set (_three_d, _film->three_d());
449                 setup_dcp_name ();
450                 break;
451         case FilmProperty::REENCODE_J2K:
452                 checked_set (_reencode_j2k, _film->reencode_j2k());
453                 break;
454         case FilmProperty::INTEROP:
455                 set_standard();
456                 setup_dcp_name ();
457                 _markers->Enable (!_film->interop());
458                 break;
459         case FilmProperty::VIDEO_ENCODING:
460                 set_standard();
461                 setup_container();
462                 setup_sensitivity();
463                 film_changed(FilmProperty::VIDEO_BIT_RATE);
464                 break;
465         case FilmProperty::LIMIT_TO_SMPTE_BV20:
466                 set_standard();
467                 break;
468         case FilmProperty::AUDIO_PROCESSOR:
469                 if (_film->audio_processor()) {
470                         checked_set (_audio_processor, _film->audio_processor()->id());
471                 } else {
472                         checked_set (_audio_processor, 0);
473                 }
474                 setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels());
475                 film_changed (FilmProperty::AUDIO_CHANNELS);
476                 break;
477         case FilmProperty::CONTENT:
478                 setup_dcp_name ();
479                 setup_sensitivity ();
480                 /* Maybe we now have ATMOS content which changes our minimum_allowed_audio_channels */
481                 setup_audio_channels_choice(_audio_channels, minimum_allowed_audio_channels());
482                 film_changed(FilmProperty::AUDIO_CHANNELS);
483                 break;
484         case FilmProperty::AUDIO_LANGUAGE:
485         {
486                 auto al = _film->audio_language();
487                 checked_set (_enable_audio_language, static_cast<bool>(al));
488                 checked_set (_audio_language, al ? std_to_wx(al->to_string()) : wxT(""));
489                 setup_dcp_name ();
490                 setup_sensitivity ();
491                 _audio_panel_sizer->Layout();
492                 break;
493         }
494         case FilmProperty::AUDIO_FRAME_RATE:
495                 if (_audio_sample_rate) {
496                         checked_set (_audio_sample_rate, _film->audio_frame_rate() == 48000 ? 0 : 1);
497                 }
498                 break;
499         case FilmProperty::CONTENT_VERSIONS:
500         case FilmProperty::VERSION_NUMBER:
501         case FilmProperty::RELEASE_TERRITORY:
502         case FilmProperty::RATINGS:
503         case FilmProperty::FACILITY:
504         case FilmProperty::STUDIO:
505         case FilmProperty::TEMP_VERSION:
506         case FilmProperty::PRE_RELEASE:
507         case FilmProperty::RED_BAND:
508         case FilmProperty::TWO_D_VERSION_OF_THREE_D:
509         case FilmProperty::CHAIN:
510         case FilmProperty::LUMINANCE:
511         case FilmProperty::TERRITORY_TYPE:
512                 setup_dcp_name ();
513                 break;
514         default:
515                 break;
516         }
517 }
518
519
520 void
521 DCPPanel::film_content_changed (int property)
522 {
523         if (property == AudioContentProperty::STREAMS ||
524             property == TextContentProperty::USE ||
525             property == TextContentProperty::BURN ||
526             property == TextContentProperty::LANGUAGE ||
527             property == TextContentProperty::LANGUAGE_IS_ADDITIONAL ||
528             property == TextContentProperty::TYPE ||
529             property == TextContentProperty::DCP_TRACK ||
530             property == VideoContentProperty::CUSTOM_RATIO ||
531             property == VideoContentProperty::CUSTOM_SIZE ||
532             property == VideoContentProperty::BURNT_SUBTITLE_LANGUAGE ||
533             property == VideoContentProperty::CROP ||
534             property == DCPContentProperty::REFERENCE_VIDEO ||
535             property == DCPContentProperty::REFERENCE_AUDIO ||
536             property == DCPContentProperty::REFERENCE_TEXT) {
537                 setup_dcp_name ();
538                 setup_sensitivity ();
539         }
540 }
541
542
543 void
544 DCPPanel::setup_container ()
545 {
546         auto ratios = Ratio::containers();
547         if (std::find(ratios.begin(), ratios.end(), _film->container()) == ratios.end()) {
548                 ratios.push_back(_film->container());
549         }
550
551         wxArrayString new_ratios;
552         for (auto ratio: ratios) {
553                 new_ratios.Add(std_to_wx(ratio->container_nickname()));
554         }
555
556         _container->set_entries(new_ratios);
557
558         auto iter = std::find_if(ratios.begin(), ratios.end(), [this](Ratio const* ratio) { return ratio == _film->container(); });
559         DCPOMATIC_ASSERT(iter != ratios.end());
560
561         checked_set(_container, iter - ratios.begin());
562         auto const size = fit_ratio_within(_film->container()->ratio(), _film->full_frame ());
563         checked_set(_container_size, wxString::Format("%dx%d", size.width, size.height));
564
565         setup_dcp_name ();
566
567         _video_grid->Layout();
568 }
569
570
571 /** Called when the container widget has been changed */
572 void
573 DCPPanel::container_changed ()
574 {
575         if (!_film) {
576                 return;
577         }
578
579         if (auto const container = _container->get()) {
580                 auto ratios = Ratio::containers ();
581                 DCPOMATIC_ASSERT(*container < int(ratios.size()));
582                 _film->set_container(ratios[*container]);
583         }
584 }
585
586
587 /** Called when the DCP content type widget has been changed */
588 void
589 DCPPanel::dcp_content_type_changed ()
590 {
591         if (!_film) {
592                 return;
593         }
594
595         if (auto const type = _dcp_content_type->get()) {
596                 _film->set_dcp_content_type(DCPContentType::from_index(*type));
597         }
598 }
599
600
601 void
602 DCPPanel::set_film (shared_ptr<Film> film)
603 {
604         /* We are changing film, so destroy any dialogs for the old one */
605         _audio_dialog.reset();
606         _markers_dialog.reset();
607         _interop_metadata_dialog.reset();
608         _smpte_metadata_dialog.reset();
609
610         _film = film;
611
612         if (!_film) {
613                 /* Really should all the film_changed below but this might be enough */
614                 checked_set (_dcp_name, wxT(""));
615                 set_general_sensitivity (false);
616                 return;
617         }
618
619         _standard->Clear();
620         add_standards();
621
622         film_changed(FilmProperty::NAME);
623         film_changed(FilmProperty::USE_ISDCF_NAME);
624         film_changed(FilmProperty::CONTENT);
625         film_changed(FilmProperty::DCP_CONTENT_TYPE);
626         film_changed(FilmProperty::CONTAINER);
627         film_changed(FilmProperty::RESOLUTION);
628         film_changed(FilmProperty::ENCRYPTED);
629         film_changed(FilmProperty::VIDEO_BIT_RATE);
630         film_changed(FilmProperty::VIDEO_FRAME_RATE);
631         film_changed(FilmProperty::AUDIO_CHANNELS);
632         film_changed(FilmProperty::SEQUENCE);
633         film_changed(FilmProperty::THREE_D);
634         film_changed(FilmProperty::INTEROP);
635         film_changed(FilmProperty::AUDIO_PROCESSOR);
636         film_changed(FilmProperty::REEL_TYPE);
637         film_changed(FilmProperty::REEL_LENGTH);
638         film_changed(FilmProperty::REENCODE_J2K);
639         film_changed(FilmProperty::AUDIO_LANGUAGE);
640         film_changed(FilmProperty::AUDIO_FRAME_RATE);
641         film_changed(FilmProperty::LIMIT_TO_SMPTE_BV20);
642
643         set_general_sensitivity(static_cast<bool>(_film));
644 }
645
646
647 void
648 DCPPanel::set_general_sensitivity (bool s)
649 {
650         _generally_sensitive = s;
651         setup_sensitivity ();
652 }
653
654
655 void
656 DCPPanel::setup_sensitivity ()
657 {
658         auto const mpeg2 = _film && _film->video_encoding() == VideoEncoding::MPEG2;
659
660         _name->Enable                   (_generally_sensitive);
661         _use_isdcf_name->Enable         (_generally_sensitive);
662         _dcp_content_type->Enable       (_generally_sensitive);
663         _copy_isdcf_name_button->Enable (_generally_sensitive);
664         _enable_audio_language->Enable  (_generally_sensitive);
665         _audio_language->Enable         (_enable_audio_language->GetValue());
666         _edit_audio_language->Enable    (_enable_audio_language->GetValue());
667         _encrypted->Enable              (_generally_sensitive);
668         _markers->Enable                (_generally_sensitive && _film && !_film->interop());
669         _metadata->Enable               (_generally_sensitive);
670         _reels->Enable                  (_generally_sensitive && _film);
671         _frame_rate_choice->Enable      (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->contains_atmos_content());
672         _frame_rate_spin->Enable        (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->contains_atmos_content());
673         _audio_channels->Enable         (_generally_sensitive && _film && !_film->references_dcp_audio());
674         _audio_processor->Enable        (_generally_sensitive && _film && !_film->references_dcp_audio());
675         _video_bit_rate->Enable         (_generally_sensitive && _film && !_film->references_dcp_video());
676         _container->Enable              (_generally_sensitive && _film && !_film->references_dcp_video() && !mpeg2);
677         _best_frame_rate->Enable (
678                 _generally_sensitive &&
679                 _film &&
680                 _film->best_video_frame_rate () != _film->video_frame_rate() &&
681                 !_film->references_dcp_video() &&
682                 !_film->contains_atmos_content()
683                 );
684         _resolution->Enable             (_generally_sensitive && _film && !_film->references_dcp_video() && !mpeg2);
685         _three_d->Enable                (_generally_sensitive && _film && !_film->references_dcp_video() && !mpeg2);
686
687         _standard->Enable (
688                 _generally_sensitive &&
689                 _film &&
690                 !_film->references_dcp_video() &&
691                 !_film->references_dcp_audio() &&
692                 !_film->contains_atmos_content()
693                 );
694
695         _reencode_j2k->Enable           (_generally_sensitive && _film);
696         _show_audio->Enable             (_generally_sensitive && _film);
697 }
698
699
700 void
701 DCPPanel::use_isdcf_name_toggled ()
702 {
703         if (!_film) {
704                 return;
705         }
706
707         _film->set_use_isdcf_name (_use_isdcf_name->GetValue());
708 }
709
710 void
711 DCPPanel::setup_dcp_name ()
712 {
713         if (!_film) {
714                 return;
715         }
716
717         _dcp_name->SetLabel (std_to_wx(_film->dcp_name(true)));
718         _dcp_name->SetToolTip (std_to_wx(_film->dcp_name(true)));
719 }
720
721
722 void
723 DCPPanel::best_frame_rate_clicked ()
724 {
725         if (!_film) {
726                 return;
727         }
728
729         _film->set_video_frame_rate (_film->best_video_frame_rate());
730 }
731
732
733 void
734 DCPPanel::three_d_changed ()
735 {
736         if (!_film) {
737                 return;
738         }
739
740         _film->set_three_d (_three_d->GetValue());
741 }
742
743
744 void
745 DCPPanel::reencode_j2k_changed ()
746 {
747         if (!_film) {
748                 return;
749         }
750
751         _film->set_reencode_j2k (_reencode_j2k->GetValue());
752 }
753
754
755 void
756 DCPPanel::config_changed (Config::Property p)
757 {
758         VideoEncoding const encoding = _film ? _film->video_encoding() : VideoEncoding::JPEG2000;
759         _video_bit_rate->SetRange(1, Config::instance()->maximum_video_bit_rate(encoding) / 1000000);
760         setup_frame_rate_widget ();
761
762         if (p == Config::SHOW_EXPERIMENTAL_AUDIO_PROCESSORS) {
763                 _audio_processor->Clear ();
764                 add_audio_processors ();
765                 if (_film) {
766                         film_changed(FilmProperty::AUDIO_PROCESSOR);
767                 }
768         } else if (p == Config::ALLOW_SMPTE_BV20) {
769                 _standard->Clear();
770                 add_standards();
771                 if (_film) {
772                         film_changed(FilmProperty::INTEROP);
773                         film_changed(FilmProperty::LIMIT_TO_SMPTE_BV20);
774                 }
775         } else if (p == Config::ISDCF_NAME_PART_LENGTH) {
776                 setup_dcp_name();
777         } else if (p == Config::ALLOW_ANY_CONTAINER) {
778                 setup_container();
779         }
780 }
781
782
783 void
784 DCPPanel::setup_frame_rate_widget ()
785 {
786         if (Config::instance()->allow_any_dcp_frame_rate()) {
787                 _frame_rate_choice->Hide ();
788                 _frame_rate_spin->Show ();
789         } else {
790                 _frame_rate_choice->Show ();
791                 _frame_rate_spin->Hide ();
792         }
793         _frame_rate_sizer->Layout();
794 }
795
796
797 wxPanel *
798 DCPPanel::make_video_panel ()
799 {
800         auto panel = new wxPanel (_notebook);
801         auto sizer = new wxBoxSizer (wxVERTICAL);
802         _video_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
803         sizer->Add (_video_grid, 0, wxALL, 8);
804         panel->SetSizer (sizer);
805
806         _container_label = create_label (panel, _("Container"), true);
807         _container = new Choice(panel);
808         _container_size = new StaticText (panel, wxT (""));
809
810         _resolution_label = create_label (panel, _("Resolution"), true);
811         _resolution = new Choice(panel);
812
813         _frame_rate_label = create_label (panel, _("Frame Rate"), true);
814         _frame_rate_choice = new Choice(panel);
815         _frame_rate_spin = new SpinCtrl (panel, DCPOMATIC_SPIN_CTRL_WIDTH);
816         _best_frame_rate = new Button (panel, _("Use best"));
817
818         _three_d = new CheckBox (panel, _("3D"));
819
820         _video_bit_rate_label = create_label(panel, _("Video bit rate\nfor newly-encoded data"), true);
821         _video_bit_rate = new SpinCtrl(panel, DCPOMATIC_SPIN_CTRL_WIDTH);
822         _mbits_label = create_label (panel, _("Mbit/s"), false);
823
824         _reencode_j2k = new CheckBox (panel, _("Re-encode JPEG2000 data from input"));
825
826         _container->Bind         (wxEVT_CHOICE,   boost::bind(&DCPPanel::container_changed, this));
827         _frame_rate_choice->Bind (wxEVT_CHOICE,   boost::bind(&DCPPanel::frame_rate_choice_changed, this));
828         _frame_rate_spin->Bind   (wxEVT_SPINCTRL, boost::bind(&DCPPanel::frame_rate_spin_changed, this));
829         _best_frame_rate->Bind   (wxEVT_BUTTON,   boost::bind(&DCPPanel::best_frame_rate_clicked, this));
830         _video_bit_rate->Bind    (wxEVT_SPINCTRL, boost::bind(&DCPPanel::video_bit_rate_changed, this));
831         /* Also listen to wxEVT_TEXT so that typing numbers directly in is always noticed */
832         _video_bit_rate->Bind    (wxEVT_TEXT,     boost::bind(&DCPPanel::video_bit_rate_changed, this));
833         _resolution->Bind        (wxEVT_CHOICE,   boost::bind(&DCPPanel::resolution_changed, this));
834         _three_d->bind(&DCPPanel::three_d_changed, this);
835         _reencode_j2k->bind(&DCPPanel::reencode_j2k_changed, this);
836
837         for (auto i: Config::instance()->allowed_dcp_frame_rates()) {
838                 _frame_rate_choice->add_entry(boost::lexical_cast<string>(i));
839         }
840
841         VideoEncoding const encoding = _film ? _film->video_encoding() : VideoEncoding::JPEG2000;
842         _video_bit_rate->SetRange(1, Config::instance()->maximum_video_bit_rate(encoding) / 1000000);
843         _frame_rate_spin->SetRange (1, 480);
844
845         _resolution->add_entry(_("2K"));
846         _resolution->add_entry(_("4K"));
847
848         add_video_panel_to_grid ();
849         setup_frame_rate_widget();
850
851         return panel;
852 }
853
854
855 void
856 DCPPanel::add_video_panel_to_grid ()
857 {
858         int r = 0;
859
860         add_label_to_sizer (_video_grid, _container_label, true, wxGBPosition (r, 0));
861         {
862                 auto s = new wxBoxSizer (wxHORIZONTAL);
863                 s->Add (_container, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
864                 s->Add (_container_size, 1, wxLEFT | wxALIGN_CENTER_VERTICAL);
865                 _video_grid->Add (s, wxGBPosition(r, 1));
866                 ++r;
867         }
868
869         add_label_to_sizer (_video_grid, _resolution_label, true, wxGBPosition (r, 0));
870         _video_grid->Add (_resolution, wxGBPosition (r, 1));
871         ++r;
872
873         add_label_to_sizer (_video_grid, _frame_rate_label, true, wxGBPosition (r, 0));
874         {
875                 _frame_rate_sizer = new wxBoxSizer (wxHORIZONTAL);
876                 _frame_rate_sizer->Add (_frame_rate_choice, 1, wxALIGN_CENTER_VERTICAL);
877                 _frame_rate_sizer->Add (_frame_rate_spin, 1, wxALIGN_CENTER_VERTICAL);
878                 _frame_rate_sizer->Add (_best_frame_rate, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
879                 _video_grid->Add (_frame_rate_sizer, wxGBPosition (r, 1));
880                 ++r;
881         }
882
883         _video_grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
884         ++r;
885
886         add_label_to_sizer(_video_grid, _video_bit_rate_label, true, wxGBPosition (r, 0));
887         auto s = new wxBoxSizer (wxHORIZONTAL);
888         s->Add(_video_bit_rate, 0, wxALIGN_CENTER_VERTICAL);
889         add_label_to_sizer (s, _mbits_label, false, 0, wxLEFT | wxALIGN_CENTER_VERTICAL);
890         _video_grid->Add (s, wxGBPosition(r, 1), wxDefaultSpan);
891         ++r;
892         _video_grid->Add (_reencode_j2k, wxGBPosition(r, 0), wxGBSpan(1, 2));
893 }
894
895
896 int
897 DCPPanel::minimum_allowed_audio_channels () const
898 {
899         int min = 2;
900         if (_film) {
901                 if (_film->audio_processor()) {
902                         min = _film->audio_processor()->out_channels();
903                 }
904                 if (_film->contains_atmos_content()) {
905                         min = std::max(min, 14);
906                 }
907         }
908
909         if (min % 2 == 1) {
910                 ++min;
911         }
912
913         return min;
914 }
915
916
917 wxPanel *
918 DCPPanel::make_audio_panel ()
919 {
920         auto panel = new wxPanel (_notebook);
921         _audio_panel_sizer = new wxBoxSizer (wxVERTICAL);
922         _audio_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
923         _audio_panel_sizer->Add (_audio_grid, 0, wxALL, 8);
924         panel->SetSizer (_audio_panel_sizer);
925
926         _channels_label = create_label (panel, _("Channels"), true);
927         _audio_channels = new Choice(panel);
928         setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels ());
929
930         if (Config::instance()->allow_96khz_audio()) {
931                 _audio_sample_rate_label = create_label (panel, _("Sample rate"), true);
932                 _audio_sample_rate = new wxChoice (panel, wxID_ANY);
933         }
934
935         _processor_label = create_label (panel, _("Processor"), true);
936         _audio_processor = new Choice(panel);
937         add_audio_processors ();
938
939         _enable_audio_language = new CheckBox(panel, _("Language"));
940         _audio_language = new wxStaticText(panel, wxID_ANY, wxT(""));
941         _edit_audio_language = new Button(panel, _("Edit..."));
942
943         _show_audio = new Button (panel, _("Show graph of audio levels..."));
944
945         _audio_channels->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_channels_changed, this));
946         if (_audio_sample_rate) {
947                 _audio_sample_rate->Bind (wxEVT_CHOICE, boost::bind(&DCPPanel::audio_sample_rate_changed, this));
948         }
949         _audio_processor->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_processor_changed, this));
950
951         _enable_audio_language->bind(&DCPPanel::enable_audio_language_toggled, this);
952         _edit_audio_language->Bind(wxEVT_BUTTON, boost::bind(&DCPPanel::edit_audio_language_clicked, this));
953
954         _show_audio->Bind (wxEVT_BUTTON, boost::bind (&DCPPanel::show_audio_clicked, this));
955
956         if (_audio_sample_rate) {
957                 _audio_sample_rate->Append (_("48kHz"));
958                 _audio_sample_rate->Append (_("96kHz"));
959         }
960
961         add_audio_panel_to_grid ();
962
963         return panel;
964 }
965
966
967 void
968 DCPPanel::add_audio_panel_to_grid ()
969 {
970         int r = 0;
971
972         add_label_to_sizer (_audio_grid, _channels_label, true, wxGBPosition (r, 0));
973         _audio_grid->Add (_audio_channels, wxGBPosition (r, 1));
974         ++r;
975
976         if (_audio_sample_rate_label && _audio_sample_rate) {
977                 add_label_to_sizer (_audio_grid, _audio_sample_rate_label, true, wxGBPosition(r, 0));
978                 _audio_grid->Add (_audio_sample_rate, wxGBPosition(r, 1));
979                 ++r;
980         }
981
982         add_label_to_sizer (_audio_grid, _processor_label, true, wxGBPosition (r, 0));
983         _audio_grid->Add (_audio_processor, wxGBPosition (r, 1));
984         ++r;
985
986         {
987                 auto s = new wxBoxSizer (wxHORIZONTAL);
988                 s->Add(_enable_audio_language, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
989                 s->Add(_audio_language, 1, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_CHECKBOX_BOTTOM_PAD);
990                 s->Add(DCPOMATIC_SIZER_X_GAP, 0);
991                 s->Add(_edit_audio_language, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_CHECKBOX_BOTTOM_PAD);
992                 _audio_grid->Add(s, wxGBPosition(r, 0), wxGBSpan(1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL);
993         }
994         ++r;
995
996         _audio_grid->Add (_show_audio, wxGBPosition (r, 0), wxGBSpan (1, 2));
997         ++r;
998 }
999
1000
1001 void
1002 DCPPanel::copy_isdcf_name_button_clicked ()
1003 {
1004         _film->set_name (_film->isdcf_name (true));
1005         _film->set_use_isdcf_name (false);
1006 }
1007
1008
1009 void
1010 DCPPanel::audio_processor_changed ()
1011 {
1012         if (!_film || !_audio_processor->get()) {
1013                 return;
1014         }
1015
1016         auto const s = string_client_data(_audio_processor->GetClientObject(*_audio_processor->get()));
1017         _film->set_audio_processor (AudioProcessor::from_id (s));
1018 }
1019
1020
1021 void
1022 DCPPanel::show_audio_clicked ()
1023 {
1024         if (!_film) {
1025                 return;
1026         }
1027
1028         _audio_dialog.reset(_panel, _film, _viewer);
1029         _audio_dialog->Show();
1030 }
1031
1032
1033 void
1034 DCPPanel::add_audio_processors ()
1035 {
1036         _audio_processor->add_entry(_("None"), new wxStringClientData(N_("none")));
1037         for (auto ap: AudioProcessor::visible()) {
1038                 _audio_processor->add_entry(std_to_wx(ap->name()), new wxStringClientData(std_to_wx(ap->id())));
1039         }
1040         _audio_panel_sizer->Layout();
1041 }
1042
1043
1044 void
1045 DCPPanel::enable_audio_language_toggled ()
1046 {
1047         setup_sensitivity ();
1048         if (_enable_audio_language->GetValue()) {
1049                 auto al = wx_to_std (_audio_language->GetLabel());
1050                 _film->set_audio_language (al.empty() ? dcp::LanguageTag("en-US") : dcp::LanguageTag(al));
1051         } else {
1052                 _film->set_audio_language (boost::none);
1053         }
1054 }
1055
1056
1057 void
1058 DCPPanel::edit_audio_language_clicked ()
1059 {
1060        DCPOMATIC_ASSERT (_film->audio_language());
1061        auto d = make_wx<LanguageTagDialog>(_panel, *_film->audio_language());
1062        if (d->ShowModal() == wxID_OK) {
1063                _film->set_audio_language(d->get());
1064        }
1065 }
1066
1067
1068 void
1069 DCPPanel::audio_sample_rate_changed ()
1070 {
1071         if (_audio_sample_rate) {
1072                 _film->set_audio_frame_rate (_audio_sample_rate->GetSelection() == 0 ? 48000 : 96000);
1073         }
1074 }
1075