Use studio and facility from Interop/SMPTE metadata rather than ISDCF.
[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 #include "dcp_panel.h"
22 #include "wx_util.h"
23 #include "isdcf_metadata_dialog.h"
24 #include "audio_dialog.h"
25 #include "focus_manager.h"
26 #include "check_box.h"
27 #include "static_text.h"
28 #include "check_box.h"
29 #include "dcpomatic_button.h"
30 #include "markers_dialog.h"
31 #include "interop_metadata_dialog.h"
32 #include "smpte_metadata_dialog.h"
33 #include "lib/ratio.h"
34 #include "lib/config.h"
35 #include "lib/dcp_content_type.h"
36 #include "lib/util.h"
37 #include "lib/film.h"
38 #include "lib/ffmpeg_content.h"
39 #include "lib/audio_processor.h"
40 #include "lib/video_content.h"
41 #include "lib/text_content.h"
42 #include "lib/dcp_content.h"
43 #include "lib/audio_content.h"
44 #include <dcp/locale_convert.h>
45 #include <wx/wx.h>
46 #include <wx/notebook.h>
47 #include <wx/gbsizer.h>
48 #include <wx/spinctrl.h>
49 #include <boost/lexical_cast.hpp>
50 #include <iostream>
51
52 using std::cout;
53 using std::list;
54 using std::string;
55 using std::vector;
56 using std::pair;
57 using std::max;
58 using std::make_pair;
59 using boost::lexical_cast;
60 using std::shared_ptr;
61 using std::weak_ptr;
62 #if BOOST_VERSION >= 106100
63 using namespace boost::placeholders;
64 #endif
65 using dcp::locale_convert;
66
67 DCPPanel::DCPPanel (wxNotebook* n, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
68         : _audio_dialog (0)
69         , _markers_dialog (0)
70         , _interop_metadata_dialog (0)
71         , _smpte_metadata_dialog (0)
72         , _film (film)
73         , _viewer (viewer)
74         , _generally_sensitive (true)
75 {
76         _panel = new wxPanel (n);
77         _sizer = new wxBoxSizer (wxVERTICAL);
78         _panel->SetSizer (_sizer);
79
80         _grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
81         _sizer->Add (_grid, 0, wxEXPAND | wxALL, 8);
82
83         _name_label = create_label (_panel, _("Name"), true);
84         _name = new wxTextCtrl (_panel, wxID_ANY);
85         FocusManager::instance()->add(_name);
86
87         _use_isdcf_name = new CheckBox (_panel, _("Use ISDCF name"));
88         _edit_isdcf_button = new Button (_panel, _("Details..."));
89         _copy_isdcf_name_button = new Button (_panel, _("Copy as name"));
90
91         /* wxST_ELLIPSIZE_MIDDLE works around a bug in GTK2 and/or wxWidgets, see
92            http://trac.wxwidgets.org/ticket/12539
93         */
94         _dcp_name = new StaticText (
95                 _panel, wxT (""), wxDefaultPosition, wxDefaultSize,
96                 wxALIGN_CENTRE_HORIZONTAL | wxST_NO_AUTORESIZE | wxST_ELLIPSIZE_MIDDLE
97                 );
98
99         _dcp_content_type_label = create_label (_panel, _("Content Type"), true);
100         _dcp_content_type = new wxChoice (_panel, wxID_ANY);
101
102         _encrypted = new CheckBox (_panel, _("Encrypted"));
103
104         wxClientDC dc (_panel);
105         auto size = dc.GetTextExtent (wxT ("GGGGGGGG..."));
106         size.SetHeight (-1);
107
108         _reels_label = create_label (_panel, _("Reels"), true);
109         _reel_type = new wxChoice (_panel, wxID_ANY);
110
111         _reel_length_label = create_label (_panel, _("Reel length"), true);
112         _reel_length = new wxSpinCtrl (_panel, wxID_ANY);
113         _reel_length_gb_label = create_label (_panel, _("GB"), false);
114
115         _standard_label = create_label (_panel, _("Standard"), true);
116         _standard = new wxChoice (_panel, wxID_ANY);
117
118         _markers = new Button (_panel, _("Markers..."));
119         _metadata = new Button (_panel, _("Metadata..."));
120
121         _notebook = new wxNotebook (_panel, wxID_ANY);
122         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
123
124         _notebook->AddPage (make_video_panel (), _("Video"), false);
125         _notebook->AddPage (make_audio_panel (), _("Audio"), false);
126
127         _name->Bind                  (wxEVT_TEXT,     boost::bind (&DCPPanel::name_changed, this));
128         _use_isdcf_name->Bind        (wxEVT_CHECKBOX, boost::bind (&DCPPanel::use_isdcf_name_toggled, this));
129         _edit_isdcf_button->Bind     (wxEVT_BUTTON,   boost::bind (&DCPPanel::edit_isdcf_button_clicked, this));
130         _copy_isdcf_name_button->Bind(wxEVT_BUTTON,   boost::bind (&DCPPanel::copy_isdcf_name_button_clicked, this));
131         _dcp_content_type->Bind      (wxEVT_CHOICE,   boost::bind (&DCPPanel::dcp_content_type_changed, this));
132         _encrypted->Bind             (wxEVT_CHECKBOX, boost::bind (&DCPPanel::encrypted_toggled, this));
133         _reel_type->Bind             (wxEVT_CHOICE,   boost::bind (&DCPPanel::reel_type_changed, this));
134         _reel_length->Bind           (wxEVT_SPINCTRL, boost::bind (&DCPPanel::reel_length_changed, this));
135         _standard->Bind              (wxEVT_CHOICE,   boost::bind (&DCPPanel::standard_changed, this));
136         _markers->Bind               (wxEVT_BUTTON,   boost::bind (&DCPPanel::markers_clicked, this));
137         _metadata->Bind              (wxEVT_BUTTON,   boost::bind (&DCPPanel::metadata_clicked, this));
138
139         for (auto i: DCPContentType::all()) {
140                 _dcp_content_type->Append (std_to_wx (i->pretty_name ()));
141         }
142
143         _reel_type->Append (_("Single reel"));
144         _reel_type->Append (_("Split by video content"));
145         /// TRANSLATORS: translate the word "Custom" here; do not include the "Reel|" prefix
146         _reel_type->Append (S_("Reel|Custom"));
147
148         _reel_length->SetRange (1, 64);
149
150         _standard->Append (_("SMPTE"));
151         _standard->Append (_("Interop"));
152
153         Config::instance()->Changed.connect (boost::bind (&DCPPanel::config_changed, this, _1));
154
155         add_to_grid ();
156 }
157
158 void
159 DCPPanel::add_to_grid ()
160 {
161         int r = 0;
162
163         auto name_sizer = new wxBoxSizer (wxHORIZONTAL);
164         name_sizer->Add (_name_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
165         name_sizer->Add (_name, 1, wxALIGN_CENTER_VERTICAL);
166         _grid->Add (name_sizer, wxGBPosition(r, 0), wxGBSpan(1, 2), wxEXPAND);
167         ++r;
168
169         int flags = wxALIGN_CENTER_VERTICAL;
170 #ifdef __WXOSX__
171         flags |= wxALIGN_RIGHT;
172 #endif
173
174         _grid->Add (_use_isdcf_name, wxGBPosition (r, 0), wxDefaultSpan, flags);
175         {
176                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
177                 s->Add (_edit_isdcf_button, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
178                 s->Add (_copy_isdcf_name_button, 1, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
179                 _grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
180         }
181         ++r;
182
183         _grid->Add (_dcp_name, wxGBPosition(r, 0), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL | wxEXPAND);
184         ++r;
185
186         add_label_to_sizer (_grid, _dcp_content_type_label, true, wxGBPosition (r, 0));
187         _grid->Add (_dcp_content_type, wxGBPosition (r, 1));
188         ++r;
189
190         _grid->Add (_encrypted, wxGBPosition (r, 0), wxGBSpan (1, 2));
191         ++r;
192
193         add_label_to_sizer (_grid, _reels_label, true, wxGBPosition (r, 0));
194         _grid->Add (_reel_type, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
195         ++r;
196
197         add_label_to_sizer (_grid, _reel_length_label, true, wxGBPosition (r, 0));
198         {
199                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
200                 s->Add (_reel_length);
201                 add_label_to_sizer (s, _reel_length_gb_label, false, 0, wxLEFT | wxALIGN_CENTER_VERTICAL);
202                 _grid->Add (s, wxGBPosition (r, 1));
203         }
204         ++r;
205
206         add_label_to_sizer (_grid, _standard_label, true, wxGBPosition (r, 0));
207         _grid->Add (_standard, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
208         ++r;
209
210         auto extra = new wxBoxSizer (wxHORIZONTAL);
211         extra->Add (_markers, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
212         extra->Add (_metadata, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
213         _grid->Add (extra, wxGBPosition(r, 0), wxGBSpan(1, 2));
214         ++r;
215 }
216
217 void
218 DCPPanel::name_changed ()
219 {
220         if (!_film) {
221                 return;
222         }
223
224         _film->set_name (string (_name->GetValue().mb_str()));
225 }
226
227 void
228 DCPPanel::j2k_bandwidth_changed ()
229 {
230         if (!_film) {
231                 return;
232         }
233
234         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1000000);
235 }
236
237 void
238 DCPPanel::encrypted_toggled ()
239 {
240         if (!_film) {
241                 return;
242         }
243
244         _film->set_encrypted (_encrypted->GetValue ());
245 }
246
247 /** Called when the frame rate choice widget has been changed */
248 void
249 DCPPanel::frame_rate_choice_changed ()
250 {
251         if (!_film) {
252                 return;
253         }
254
255         _film->set_video_frame_rate (
256                 boost::lexical_cast<int>(
257                         wx_to_std(_frame_rate_choice->GetString(_frame_rate_choice->GetSelection()))
258                         ),
259                 true
260                 );
261 }
262
263 /** Called when the frame rate spin widget has been changed */
264 void
265 DCPPanel::frame_rate_spin_changed ()
266 {
267         if (!_film) {
268                 return;
269         }
270
271         _film->set_video_frame_rate (_frame_rate_spin->GetValue ());
272 }
273
274 void
275 DCPPanel::audio_channels_changed ()
276 {
277         if (!_film) {
278                 return;
279         }
280
281         _film->set_audio_channels (locale_convert<int> (string_client_data (_audio_channels->GetClientObject (_audio_channels->GetSelection ()))));
282 }
283
284 void
285 DCPPanel::resolution_changed ()
286 {
287         if (!_film) {
288                 return;
289         }
290
291         _film->set_resolution (_resolution->GetSelection() == 0 ? Resolution::TWO_K : Resolution::FOUR_K);
292 }
293
294 void
295 DCPPanel::standard_changed ()
296 {
297         if (!_film) {
298                 return;
299         }
300
301         _film->set_interop (_standard->GetSelection() == 1);
302 }
303
304 void
305 DCPPanel::markers_clicked ()
306 {
307         if (_markers_dialog) {
308                 _markers_dialog->Destroy ();
309                 _markers_dialog = 0;
310         }
311
312         _markers_dialog = new MarkersDialog (_panel, _film, _viewer);
313         _markers_dialog->Show();
314 }
315
316 void
317 DCPPanel::metadata_clicked ()
318 {
319         if (_film->interop()) {
320                 if (_interop_metadata_dialog) {
321                         _interop_metadata_dialog->Destroy ();
322                         _interop_metadata_dialog = 0;
323                 }
324
325                 _interop_metadata_dialog = new InteropMetadataDialog (_panel, _film);
326                 _interop_metadata_dialog->setup ();
327                 _interop_metadata_dialog->Show ();
328         } else {
329                 if (_smpte_metadata_dialog) {
330                         _smpte_metadata_dialog->Destroy ();
331                         _smpte_metadata_dialog = 0;
332                 }
333
334                 _smpte_metadata_dialog = new SMPTEMetadataDialog (_panel, _film);
335                 _smpte_metadata_dialog->setup ();
336                 _smpte_metadata_dialog->Show ();
337         }
338 }
339
340 void
341 DCPPanel::film_changed (Film::Property p)
342 {
343         switch (p) {
344         case Film::Property::NONE:
345                 break;
346         case Film::Property::CONTAINER:
347                 setup_container ();
348                 break;
349         case Film::Property::NAME:
350                 checked_set (_name, _film->name());
351                 setup_dcp_name ();
352                 break;
353         case Film::Property::DCP_CONTENT_TYPE:
354                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
355                 setup_dcp_name ();
356                 break;
357         case Film::Property::ENCRYPTED:
358                 checked_set (_encrypted, _film->encrypted ());
359                 break;
360         case Film::Property::RESOLUTION:
361                 checked_set (_resolution, _film->resolution() == Resolution::TWO_K ? 0 : 1);
362                 setup_container ();
363                 setup_dcp_name ();
364                 break;
365         case Film::Property::J2K_BANDWIDTH:
366                 checked_set (_j2k_bandwidth, _film->j2k_bandwidth() / 1000000);
367                 break;
368         case Film::Property::USE_ISDCF_NAME:
369         {
370                 checked_set (_use_isdcf_name, _film->use_isdcf_name ());
371                 if (_film->use_isdcf_name()) {
372                         /* We are going back to using an ISDCF name.  Remove anything after a _ in the current name,
373                            in case the user has clicked 'Copy as name' then re-ticked 'Use ISDCF name' (#1513).
374                         */
375                         string const name = _film->name ();
376                         string::size_type const u = name.find("_");
377                         if (u != string::npos) {
378                                 _film->set_name (name.substr(0, u));
379                         }
380                 }
381                 setup_dcp_name ();
382                 _edit_isdcf_button->Enable (_film->use_isdcf_name ());
383                 break;
384         }
385         case Film::Property::ISDCF_METADATA:
386                 setup_dcp_name ();
387                 break;
388         case Film::Property::VIDEO_FRAME_RATE:
389         {
390                 bool done = false;
391                 for (unsigned int i = 0; i < _frame_rate_choice->GetCount(); ++i) {
392                         if (wx_to_std (_frame_rate_choice->GetString(i)) == boost::lexical_cast<string> (_film->video_frame_rate())) {
393                                 checked_set (_frame_rate_choice, i);
394                                 done = true;
395                                 break;
396                         }
397                 }
398
399                 if (!done) {
400                         checked_set (_frame_rate_choice, -1);
401                 }
402
403                 checked_set (_frame_rate_spin, _film->video_frame_rate ());
404
405                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
406                 setup_dcp_name ();
407                 break;
408         }
409         case Film::Property::AUDIO_CHANNELS:
410                 if (_film->audio_channels () < minimum_allowed_audio_channels ()) {
411                         _film->set_audio_channels (minimum_allowed_audio_channels ());
412                 } else {
413                         checked_set (_audio_channels, locale_convert<string> (max (minimum_allowed_audio_channels(), _film->audio_channels ())));
414                         setup_dcp_name ();
415                 }
416                 break;
417         case Film::Property::THREE_D:
418                 checked_set (_three_d, _film->three_d ());
419                 setup_dcp_name ();
420                 break;
421         case Film::Property::REENCODE_J2K:
422                 checked_set (_reencode_j2k, _film->reencode_j2k());
423                 break;
424         case Film::Property::INTEROP:
425                 checked_set (_standard, _film->interop() ? 1 : 0);
426                 setup_dcp_name ();
427                 _markers->Enable (!_film->interop());
428                 break;
429         case Film::Property::AUDIO_PROCESSOR:
430                 if (_film->audio_processor ()) {
431                         checked_set (_audio_processor, _film->audio_processor()->id());
432                 } else {
433                         checked_set (_audio_processor, 0);
434                 }
435                 setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels ());
436                 film_changed (Film::Property::AUDIO_CHANNELS);
437                 break;
438         case Film::Property::REEL_TYPE:
439                 checked_set (_reel_type, static_cast<int>(_film->reel_type()));
440                 _reel_length->Enable (_film->reel_type() == ReelType::BY_LENGTH);
441                 break;
442         case Film::Property::REEL_LENGTH:
443                 checked_set (_reel_length, _film->reel_length() / 1000000000LL);
444                 break;
445         case Film::Property::CONTENT:
446                 setup_dcp_name ();
447                 setup_sensitivity ();
448                 break;
449         case Film::Property::CONTENT_VERSIONS:
450         case Film::Property::VERSION_NUMBER:
451         case Film::Property::RELEASE_TERRITORY:
452         case Film::Property::RATINGS:
453         case Film::Property::FACILITY:
454         case Film::Property::STUDIO:
455                 setup_dcp_name ();
456                 break;
457         default:
458                 break;
459         }
460 }
461
462 void
463 DCPPanel::film_content_changed (int property)
464 {
465         if (property == AudioContentProperty::STREAMS ||
466             property == AudioContentProperty::LANGUAGE ||
467             property == TextContentProperty::USE ||
468             property == TextContentProperty::BURN ||
469             property == TextContentProperty::LANGUAGE ||
470             property == TextContentProperty::LANGUAGE_IS_ADDITIONAL ||
471             property == VideoContentProperty::SCALE ||
472             property == VideoContentProperty::BURNT_SUBTITLE_LANGUAGE ||
473             property == DCPContentProperty::REFERENCE_VIDEO ||
474             property == DCPContentProperty::REFERENCE_AUDIO ||
475             property == DCPContentProperty::REFERENCE_TEXT) {
476                 setup_dcp_name ();
477                 setup_sensitivity ();
478         }
479 }
480
481
482 void
483 DCPPanel::setup_container ()
484 {
485         int n = 0;
486         vector<Ratio const *> ratios = Ratio::containers ();
487         vector<Ratio const *>::iterator i = ratios.begin ();
488         while (i != ratios.end() && *i != _film->container ()) {
489                 ++i;
490                 ++n;
491         }
492
493         if (i == ratios.end()) {
494                 checked_set (_container, -1);
495                 checked_set (_container_size, wxT (""));
496         } else {
497                 checked_set (_container, n);
498                 dcp::Size const size = fit_ratio_within (_film->container()->ratio(), _film->full_frame ());
499                 checked_set (_container_size, wxString::Format ("%dx%d", size.width, size.height));
500         }
501
502         setup_dcp_name ();
503 }
504
505 /** Called when the container widget has been changed */
506 void
507 DCPPanel::container_changed ()
508 {
509         if (!_film) {
510                 return;
511         }
512
513         int const n = _container->GetSelection ();
514         if (n >= 0) {
515                 vector<Ratio const *> ratios = Ratio::containers ();
516                 DCPOMATIC_ASSERT (n < int (ratios.size()));
517                 _film->set_container (ratios[n]);
518         }
519 }
520
521 /** Called when the DCP content type widget has been changed */
522 void
523 DCPPanel::dcp_content_type_changed ()
524 {
525         if (!_film) {
526                 return;
527         }
528
529         int const n = _dcp_content_type->GetSelection ();
530         if (n != wxNOT_FOUND) {
531                 _film->set_dcp_content_type (DCPContentType::from_index (n));
532         }
533 }
534
535 void
536 DCPPanel::set_film (shared_ptr<Film> film)
537 {
538         /* We are changing film, so destroy any dialogs for the old one */
539         if (_audio_dialog) {
540                 _audio_dialog->Destroy ();
541                 _audio_dialog = nullptr;
542         }
543         if (_markers_dialog) {
544                 _markers_dialog->Destroy ();
545                 _markers_dialog = nullptr;
546         }
547         if (_interop_metadata_dialog) {
548                 _interop_metadata_dialog->Destroy ();
549                 _interop_metadata_dialog = nullptr;
550         }
551         if (_smpte_metadata_dialog) {
552                 _smpte_metadata_dialog->Destroy ();
553                 _smpte_metadata_dialog = nullptr;
554         }
555
556         _film = film;
557
558         if (!_film) {
559                 /* Really should all the film_changed below but this might be enough */
560                 checked_set (_dcp_name, wxT(""));
561                 set_general_sensitivity (false);
562                 return;
563         }
564
565         film_changed (Film::Property::NAME);
566         film_changed (Film::Property::USE_ISDCF_NAME);
567         film_changed (Film::Property::CONTENT);
568         film_changed (Film::Property::DCP_CONTENT_TYPE);
569         film_changed (Film::Property::CONTAINER);
570         film_changed (Film::Property::RESOLUTION);
571         film_changed (Film::Property::ENCRYPTED);
572         film_changed (Film::Property::J2K_BANDWIDTH);
573         film_changed (Film::Property::ISDCF_METADATA);
574         film_changed (Film::Property::VIDEO_FRAME_RATE);
575         film_changed (Film::Property::AUDIO_CHANNELS);
576         film_changed (Film::Property::SEQUENCE);
577         film_changed (Film::Property::THREE_D);
578         film_changed (Film::Property::INTEROP);
579         film_changed (Film::Property::AUDIO_PROCESSOR);
580         film_changed (Film::Property::REEL_TYPE);
581         film_changed (Film::Property::REEL_LENGTH);
582         film_changed (Film::Property::REENCODE_J2K);
583
584         set_general_sensitivity(static_cast<bool>(_film));
585 }
586
587 void
588 DCPPanel::set_general_sensitivity (bool s)
589 {
590         _generally_sensitive = s;
591         setup_sensitivity ();
592 }
593
594 void
595 DCPPanel::setup_sensitivity ()
596 {
597         _name->Enable                   (_generally_sensitive);
598         _use_isdcf_name->Enable         (_generally_sensitive);
599         _edit_isdcf_button->Enable      (_generally_sensitive);
600         _dcp_content_type->Enable       (_generally_sensitive);
601         _copy_isdcf_name_button->Enable (_generally_sensitive);
602         _encrypted->Enable              (_generally_sensitive);
603         _reel_type->Enable              (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->references_dcp_audio());
604         _reel_length->Enable            (_generally_sensitive && _film && _film->reel_type() == ReelType::BY_LENGTH);
605         _markers->Enable                (_generally_sensitive && _film && !_film->interop());
606         _metadata->Enable               (_generally_sensitive);
607         _frame_rate_choice->Enable      (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->contains_atmos_content());
608         _frame_rate_spin->Enable        (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->contains_atmos_content());
609         _audio_channels->Enable         (_generally_sensitive && _film && !_film->references_dcp_audio() && !_film->contains_atmos_content());
610         _audio_processor->Enable        (_generally_sensitive && _film && !_film->references_dcp_audio());
611         _j2k_bandwidth->Enable          (_generally_sensitive && _film && !_film->references_dcp_video());
612         _container->Enable              (_generally_sensitive && _film && !_film->references_dcp_video());
613         _best_frame_rate->Enable (
614                 _generally_sensitive &&
615                 _film &&
616                 _film->best_video_frame_rate () != _film->video_frame_rate() &&
617                 !_film->references_dcp_video() &&
618                 !_film->contains_atmos_content()
619                 );
620         _resolution->Enable             (_generally_sensitive && _film && !_film->references_dcp_video());
621         _three_d->Enable                (_generally_sensitive && _film && !_film->references_dcp_video());
622
623         _standard->Enable (
624                 _generally_sensitive &&
625                 _film &&
626                 !_film->references_dcp_video() &&
627                 !_film->references_dcp_audio() &&
628                 !_film->contains_atmos_content()
629                 );
630
631         _reencode_j2k->Enable           (_generally_sensitive && _film);
632         _show_audio->Enable             (_generally_sensitive && _film);
633 }
634
635 void
636 DCPPanel::use_isdcf_name_toggled ()
637 {
638         if (!_film) {
639                 return;
640         }
641
642         _film->set_use_isdcf_name (_use_isdcf_name->GetValue ());
643 }
644
645 void
646 DCPPanel::edit_isdcf_button_clicked ()
647 {
648         if (!_film) {
649                 return;
650         }
651
652         auto d = new ISDCFMetadataDialog (_panel, _film->isdcf_metadata (), _film->three_d ());
653         d->ShowModal ();
654         _film->set_isdcf_metadata (d->isdcf_metadata ());
655         d->Destroy ();
656 }
657
658 void
659 DCPPanel::setup_dcp_name ()
660 {
661         _dcp_name->SetLabel (std_to_wx (_film->dcp_name (true)));
662         _dcp_name->SetToolTip (std_to_wx (_film->dcp_name (true)));
663 }
664
665 void
666 DCPPanel::best_frame_rate_clicked ()
667 {
668         if (!_film) {
669                 return;
670         }
671
672         _film->set_video_frame_rate (_film->best_video_frame_rate ());
673 }
674
675 void
676 DCPPanel::three_d_changed ()
677 {
678         if (!_film) {
679                 return;
680         }
681
682         _film->set_three_d (_three_d->GetValue ());
683 }
684
685 void
686 DCPPanel::reencode_j2k_changed ()
687 {
688         if (!_film) {
689                 return;
690         }
691
692         _film->set_reencode_j2k (_reencode_j2k->GetValue());
693 }
694
695 void
696 DCPPanel::config_changed (Config::Property p)
697 {
698         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
699         setup_frame_rate_widget ();
700
701         if (p == Config::SHOW_EXPERIMENTAL_AUDIO_PROCESSORS) {
702                 _audio_processor->Clear ();
703                 add_audio_processors ();
704                 if (_film) {
705                         film_changed (Film::Property::AUDIO_PROCESSOR);
706                 }
707         }
708 }
709
710 void
711 DCPPanel::setup_frame_rate_widget ()
712 {
713         if (Config::instance()->allow_any_dcp_frame_rate ()) {
714                 _frame_rate_choice->Hide ();
715                 _frame_rate_spin->Show ();
716         } else {
717                 _frame_rate_choice->Show ();
718                 _frame_rate_spin->Hide ();
719         }
720 }
721
722 wxPanel *
723 DCPPanel::make_video_panel ()
724 {
725         auto panel = new wxPanel (_notebook);
726         auto sizer = new wxBoxSizer (wxVERTICAL);
727         _video_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
728         sizer->Add (_video_grid, 0, wxALL, 8);
729         panel->SetSizer (sizer);
730
731         _container_label = create_label (panel, _("Container"), true);
732         _container = new wxChoice (panel, wxID_ANY);
733         _container_size = new StaticText (panel, wxT (""));
734
735         _resolution_label = create_label (panel, _("Resolution"), true);
736         _resolution = new wxChoice (panel, wxID_ANY);
737
738         _frame_rate_label = create_label (panel, _("Frame Rate"), true);
739         _frame_rate_choice = new wxChoice (panel, wxID_ANY);
740         _frame_rate_spin = new wxSpinCtrl (panel, wxID_ANY);
741         setup_frame_rate_widget ();
742         _best_frame_rate = new Button (panel, _("Use best"));
743
744         _three_d = new CheckBox (panel, _("3D"));
745
746         _j2k_bandwidth_label = create_label (panel, _("JPEG2000 bandwidth\nfor newly-encoded data"), true);
747         _j2k_bandwidth = new wxSpinCtrl (panel, wxID_ANY);
748         _mbits_label = create_label (panel, _("Mbit/s"), false);
749
750         _reencode_j2k = new CheckBox (panel, _("Re-encode JPEG2000 data from input"));
751
752         _container->Bind         (wxEVT_CHOICE,   boost::bind(&DCPPanel::container_changed, this));
753         _frame_rate_choice->Bind (wxEVT_CHOICE,   boost::bind(&DCPPanel::frame_rate_choice_changed, this));
754         _frame_rate_spin->Bind   (wxEVT_SPINCTRL, boost::bind(&DCPPanel::frame_rate_spin_changed, this));
755         _best_frame_rate->Bind   (wxEVT_BUTTON,   boost::bind(&DCPPanel::best_frame_rate_clicked, this));
756         _j2k_bandwidth->Bind     (wxEVT_SPINCTRL, boost::bind(&DCPPanel::j2k_bandwidth_changed, this));
757         /* Also listen to wxEVT_TEXT so that typing numbers directly in is always noticed */
758         _j2k_bandwidth->Bind     (wxEVT_TEXT,     boost::bind(&DCPPanel::j2k_bandwidth_changed, this));
759         _resolution->Bind        (wxEVT_CHOICE,   boost::bind(&DCPPanel::resolution_changed, this));
760         _three_d->Bind           (wxEVT_CHECKBOX, boost::bind(&DCPPanel::three_d_changed, this));
761         _reencode_j2k->Bind      (wxEVT_CHECKBOX, boost::bind(&DCPPanel::reencode_j2k_changed, this));
762
763         for (auto i: Ratio::containers()) {
764                 _container->Append (std_to_wx(i->container_nickname()));
765         }
766
767         for (auto i: Config::instance()->allowed_dcp_frame_rates()) {
768                 _frame_rate_choice->Append (std_to_wx (boost::lexical_cast<string> (i)));
769         }
770
771         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
772         _frame_rate_spin->SetRange (1, 480);
773
774         _resolution->Append (_("2K"));
775         _resolution->Append (_("4K"));
776
777         add_video_panel_to_grid ();
778
779         return panel;
780 }
781
782 void
783 DCPPanel::add_video_panel_to_grid ()
784 {
785         int r = 0;
786
787         add_label_to_sizer (_video_grid, _container_label, true, wxGBPosition (r, 0));
788         {
789                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
790                 s->Add (_container, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
791                 s->Add (_container_size, 1, wxLEFT | wxALIGN_CENTER_VERTICAL);
792                 _video_grid->Add (s, wxGBPosition(r, 1));
793                 ++r;
794         }
795
796         add_label_to_sizer (_video_grid, _resolution_label, true, wxGBPosition (r, 0));
797         _video_grid->Add (_resolution, wxGBPosition (r, 1));
798         ++r;
799
800         add_label_to_sizer (_video_grid, _frame_rate_label, true, wxGBPosition (r, 0));
801         {
802                 _frame_rate_sizer = new wxBoxSizer (wxHORIZONTAL);
803                 _frame_rate_sizer->Add (_frame_rate_choice, 1, wxALIGN_CENTER_VERTICAL);
804                 _frame_rate_sizer->Add (_frame_rate_spin, 1, wxALIGN_CENTER_VERTICAL);
805                 _frame_rate_sizer->Add (_best_frame_rate, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
806                 _video_grid->Add (_frame_rate_sizer, wxGBPosition (r, 1));
807                 ++r;
808         }
809
810         _video_grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
811         ++r;
812
813         add_label_to_sizer (_video_grid, _j2k_bandwidth_label, true, wxGBPosition (r, 0));
814         auto s = new wxBoxSizer (wxHORIZONTAL);
815         s->Add (_j2k_bandwidth, 0, wxALIGN_CENTER_VERTICAL);
816         add_label_to_sizer (s, _mbits_label, false, 0, wxLEFT | wxALIGN_CENTER_VERTICAL);
817         _video_grid->Add (s, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
818         ++r;
819         _video_grid->Add (_reencode_j2k, wxGBPosition(r, 0), wxGBSpan(1, 2));
820 }
821
822 int
823 DCPPanel::minimum_allowed_audio_channels () const
824 {
825         int min = 2;
826         if (_film && _film->audio_processor ()) {
827                 min = _film->audio_processor()->out_channels ();
828         }
829
830         if (min % 2 == 1) {
831                 ++min;
832         }
833
834         return min;
835 }
836
837 wxPanel *
838 DCPPanel::make_audio_panel ()
839 {
840         auto panel = new wxPanel (_notebook);
841         _audio_panel_sizer = new wxBoxSizer (wxVERTICAL);
842         _audio_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
843         _audio_panel_sizer->Add (_audio_grid, 0, wxALL, 8);
844         panel->SetSizer (_audio_panel_sizer);
845
846         _channels_label = create_label (panel, _("Channels"), true);
847         _audio_channels = new wxChoice (panel, wxID_ANY);
848         setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels ());
849
850         _processor_label = create_label (panel, _("Processor"), true);
851         _audio_processor = new wxChoice (panel, wxID_ANY);
852         add_audio_processors ();
853
854         _show_audio = new Button (panel, _("Show audio..."));
855
856         _audio_channels->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_channels_changed, this));
857         _audio_processor->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_processor_changed, this));
858         _show_audio->Bind (wxEVT_BUTTON, boost::bind (&DCPPanel::show_audio_clicked, this));
859
860         add_audio_panel_to_grid ();
861
862         return panel;
863 }
864
865 void
866 DCPPanel::add_audio_panel_to_grid ()
867 {
868         int r = 0;
869
870         add_label_to_sizer (_audio_grid, _channels_label, true, wxGBPosition (r, 0));
871         _audio_grid->Add (_audio_channels, wxGBPosition (r, 1));
872         ++r;
873
874         add_label_to_sizer (_audio_grid, _processor_label, true, wxGBPosition (r, 0));
875         _audio_grid->Add (_audio_processor, wxGBPosition (r, 1));
876         ++r;
877
878         _audio_grid->Add (_show_audio, wxGBPosition (r, 0), wxGBSpan (1, 2));
879         ++r;
880 }
881
882 void
883 DCPPanel::copy_isdcf_name_button_clicked ()
884 {
885         _film->set_name (_film->isdcf_name (true));
886         _film->set_use_isdcf_name (false);
887 }
888
889 void
890 DCPPanel::audio_processor_changed ()
891 {
892         if (!_film) {
893                 return;
894         }
895
896         auto const s = string_client_data (_audio_processor->GetClientObject (_audio_processor->GetSelection ()));
897         _film->set_audio_processor (AudioProcessor::from_id (s));
898 }
899
900 void
901 DCPPanel::show_audio_clicked ()
902 {
903         if (!_film) {
904                 return;
905         }
906
907         if (_audio_dialog) {
908                 _audio_dialog->Destroy ();
909                 _audio_dialog = 0;
910         }
911
912         auto d = new AudioDialog (_panel, _film, _viewer);
913         d->Show ();
914 }
915
916 void
917 DCPPanel::reel_type_changed ()
918 {
919         if (!_film) {
920                 return;
921         }
922
923         _film->set_reel_type (static_cast<ReelType> (_reel_type->GetSelection ()));
924 }
925
926 void
927 DCPPanel::reel_length_changed ()
928 {
929         if (!_film) {
930                 return;
931         }
932
933         _film->set_reel_length (_reel_length->GetValue() * 1000000000LL);
934 }
935
936 void
937 DCPPanel::add_audio_processors ()
938 {
939         _audio_processor->Append (_("None"), new wxStringClientData (N_("none")));
940         for (auto ap: AudioProcessor::visible()) {
941                 _audio_processor->Append (std_to_wx(ap->name()), new wxStringClientData(std_to_wx(ap->id())));
942         }
943         _audio_panel_sizer->Layout();
944 }