Add new "copy markers from this DCP" option (#2628).
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013-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 "auto_crop_dialog.h"
23 #include "content_advanced_dialog.h"
24 #include "content_menu.h"
25 #include "content_properties_dialog.h"
26 #include "film_viewer.h"
27 #include "repeat_dialog.h"
28 #include "timeline_video_content_view.h"
29 #include "timeline_audio_content_view.h"
30 #include "wx_util.h"
31 #include "lib/audio_content.h"
32 #include "lib/config.h"
33 #include "lib/content_factory.h"
34 #include "lib/constants.h"
35 #include "lib/copy_dcp_details_to_film.h"
36 #include "lib/dcp_content.h"
37 #include "lib/dcp_examiner.h"
38 #include "lib/examine_content_job.h"
39 #include "lib/exceptions.h"
40 #include "lib/ffmpeg_content.h"
41 #include "lib/film.h"
42 #include "lib/find_missing.h"
43 #include "lib/guess_crop.h"
44 #include "lib/image_content.h"
45 #include "lib/job_manager.h"
46 #include "lib/playlist.h"
47 #include "lib/video_content.h"
48 #include <dcp/cpl.h>
49 #include <dcp/decrypted_kdm.h>
50 #include <dcp/exceptions.h>
51 #include <dcp/search.h>
52 #include <dcp/warnings.h>
53 LIBDCP_DISABLE_WARNINGS
54 #include <wx/dirdlg.h>
55 #include <wx/wx.h>
56 LIBDCP_ENABLE_WARNINGS
57
58
59 using std::dynamic_pointer_cast;
60 using std::exception;
61 using std::list;
62 using std::make_shared;
63 using std::shared_ptr;
64 using std::vector;
65 using std::weak_ptr;
66 using boost::optional;
67 #if BOOST_VERSION >= 106100
68 using namespace boost::placeholders;
69 #endif
70 using namespace dcpomatic;
71
72
73 enum {
74         /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
75         ID_repeat = 256,
76         ID_join,
77         ID_find_missing,
78         ID_properties,
79         ID_advanced,
80         ID_re_examine,
81         ID_auto_crop,
82         ID_kdm,
83         ID_ov,
84         ID_choose_cpl,
85         ID_set_dcp_settings,
86         ID_set_dcp_markers,
87         ID_remove
88 };
89
90
91 ContentMenu::ContentMenu(wxWindow* p, FilmViewer& viewer)
92         : _menu (new wxMenu)
93         , _parent (p)
94         , _pop_up_open (false)
95         , _viewer (viewer)
96 {
97         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
98         _join = _menu->Append (ID_join, _("Join"));
99         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
100         _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
101         _auto_crop = _menu->Append (ID_auto_crop, _("Auto-crop..."));
102         _properties = _menu->Append (ID_properties, _("Properties..."));
103         _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
104         _menu->AppendSeparator ();
105         _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
106         _ov = _menu->Append (ID_ov, _("Add OV..."));
107         _cpl_menu = new wxMenu ();
108         _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
109         _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
110         _set_dcp_markers = _menu->Append(ID_set_dcp_markers, _("Set project markers from this DCP"));
111         _menu->AppendSeparator ();
112         _remove = _menu->Append (ID_remove, _("Remove"));
113
114         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
115         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
116         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
117         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
118         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
119         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
120         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::auto_crop, this), ID_auto_crop);
121         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
122         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
123         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
124         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_markers, this), ID_set_dcp_markers);
125         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
126         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
127 }
128
129 void
130 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
131 {
132         _film = film;
133         _content = c;
134         _views = v;
135
136         int const N = _cpl_menu->GetMenuItemCount();
137         for (int i = 1; i <= N; ++i) {
138                 _cpl_menu->Delete (i);
139         }
140
141         _repeat->Enable (!_content.empty ());
142
143         int n = 0;
144         for (auto i: _content) {
145                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
146                         ++n;
147                 }
148         }
149
150         _join->Enable (n > 1);
151
152         _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
153         _properties->Enable (_content.size() == 1);
154         _advanced->Enable (_content.size() == 1);
155         _re_examine->Enable (!_content.empty ());
156         _auto_crop->Enable (_content.size() == 1);
157
158         if (_content.size() == 1) {
159                 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
160                 if (dcp) {
161                         _kdm->Enable (dcp->encrypted ());
162                         _ov->Enable (dcp->needs_assets ());
163                         _set_dcp_settings->Enable (static_cast<bool>(dcp));
164                         _set_dcp_markers->Enable(static_cast<bool>(dcp));
165                         try {
166                                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
167                                 _choose_cpl->Enable (cpls.size() > 1);
168                                 /* We can't have 0 as a menu item ID on OS X */
169                                 int id = 1;
170                                 for (auto i: cpls) {
171                                         auto item = _cpl_menu->AppendRadioItem (
172                                                 id++,
173                                                 wxString::Format (
174                                                         "%s (%s)",
175                                                         std_to_wx(i->annotation_text().get_value_or("")).data(),
176                                                         std_to_wx(i->id()).data()
177                                                         )
178                                                 );
179                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
180                                 }
181                         } catch (dcp::ReadError &) {
182                                 /* The DCP is probably missing */
183                         } catch (dcp::KDMDecryptionError &) {
184                                 /* We have an incorrect KDM */
185                         } catch (KDMError &) {
186                                 /* We have an incorrect KDM */
187                         }
188                 } else {
189                         _kdm->Enable (false);
190                         _ov->Enable (false);
191                         _choose_cpl->Enable (false);
192                         _set_dcp_settings->Enable (false);
193                         _set_dcp_markers->Enable(false);
194                 }
195         } else {
196                 _kdm->Enable (false);
197                 _set_dcp_settings->Enable (false);
198                 _set_dcp_markers->Enable(false);
199         }
200
201         _remove->Enable (!_content.empty ());
202
203         _pop_up_open = true;
204         _parent->PopupMenu (_menu, p);
205         _pop_up_open = false;
206 }
207
208
209 void
210 ContentMenu::set_dcp_settings ()
211 {
212         auto film = _film.lock ();
213         if (!film) {
214                 return;
215         }
216
217         DCPOMATIC_ASSERT (_content.size() == 1);
218         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
219         DCPOMATIC_ASSERT (dcp);
220         copy_dcp_settings_to_film(dcp, film);
221 }
222
223
224 void
225 ContentMenu::set_dcp_markers()
226 {
227         auto film = _film.lock();
228         if (!film) {
229                 return;
230         }
231
232         DCPOMATIC_ASSERT(_content.size() == 1);
233         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
234         DCPOMATIC_ASSERT(dcp);
235         copy_dcp_markers_to_film(dcp, film);
236 }
237
238
239 void
240 ContentMenu::repeat ()
241 {
242         if (_content.empty ()) {
243                 return;
244         }
245
246         auto d = make_wx<RepeatDialog>(_parent);
247         if (d->ShowModal() != wxID_OK) {
248                 return;
249         }
250
251         auto film = _film.lock ();
252         if (!film) {
253                 return;
254         }
255
256         film->repeat_content (_content, d->number ());
257
258         _content.clear ();
259         _views.clear ();
260 }
261
262
263 void
264 ContentMenu::join ()
265 {
266         vector<shared_ptr<Content>> fc;
267         for (auto i: _content) {
268                 auto f = dynamic_pointer_cast<FFmpegContent> (i);
269                 if (f) {
270                         fc.push_back (f);
271                 }
272         }
273
274         DCPOMATIC_ASSERT (fc.size() > 1);
275
276         auto film = _film.lock ();
277         if (!film) {
278                 return;
279         }
280
281         try {
282                 auto joined = make_shared<FFmpegContent>(fc);
283                 film->remove_content (_content);
284                 film->examine_and_add_content (joined);
285         } catch (JoinError& e) {
286                 error_dialog (_parent, std_to_wx (e.what ()));
287         }
288 }
289
290
291 void
292 ContentMenu::remove ()
293 {
294         if (_content.empty ()) {
295                 return;
296         }
297
298         auto film = _film.lock ();
299         if (!film) {
300                 return;
301         }
302
303         /* We are removing from the timeline if _views is not empty */
304         bool handled = false;
305         if (!_views.empty ()) {
306                 /* Special case: we only remove FFmpegContent if its video view is selected;
307                    if not, and its audio view is selected, we unmap the audio.
308                 */
309                 for (auto i: _content) {
310                         auto fc = dynamic_pointer_cast<FFmpegContent> (i);
311                         if (!fc) {
312                                 continue;
313                         }
314
315                         shared_ptr<TimelineVideoContentView> video;
316                         shared_ptr<TimelineAudioContentView> audio;
317
318                         for (auto j: _views) {
319                                 auto v = dynamic_pointer_cast<TimelineVideoContentView>(j);
320                                 auto a = dynamic_pointer_cast<TimelineAudioContentView>(j);
321                                 if (v && v->content() == fc) {
322                                         video = v;
323                                 } else if (a && a->content() == fc) {
324                                         audio = a;
325                                 }
326                         }
327
328                         if (!video && audio) {
329                                 auto m = fc->audio->mapping ();
330                                 m.unmap_all ();
331                                 fc->audio->set_mapping (m);
332                                 handled = true;
333                         }
334                 }
335         }
336
337         if (!handled) {
338                 film->remove_content (_content);
339         }
340
341         _content.clear ();
342         _views.clear ();
343 }
344
345
346 void
347 ContentMenu::find_missing ()
348 {
349         if (_content.size() != 1) {
350                 return;
351         }
352
353         auto film = _film.lock ();
354         if (!film) {
355                 return;
356         }
357
358         /* XXX: a bit nasty */
359         auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
360         auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
361
362         int r = wxID_CANCEL;
363         boost::filesystem::path path;
364
365         if ((ic && !ic->still ()) || dc) {
366                 auto d = make_wx<wxDirDialog>(nullptr, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
367                 r = d->ShowModal ();
368                 path = wx_to_std (d->GetPath());
369         } else {
370                 auto d = make_wx<wxFileDialog>(nullptr, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
371                 r = d->ShowModal ();
372                 path = wx_to_std (d->GetPath());
373         }
374
375         if (r == wxID_CANCEL) {
376                 return;
377         }
378
379         dcpomatic::find_missing (film->content(), path);
380 }
381
382 void
383 ContentMenu::re_examine ()
384 {
385         auto film = _film.lock ();
386         if (!film) {
387                 return;
388         }
389
390         for (auto i: _content) {
391                 JobManager::instance()->add(make_shared<ExamineContentJob>(film, i));
392         }
393 }
394
395
396 void
397 ContentMenu::kdm ()
398 {
399         DCPOMATIC_ASSERT (!_content.empty ());
400         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
401         DCPOMATIC_ASSERT (dcp);
402
403         auto d = make_wx<wxFileDialog>(_parent, _("Select KDM"));
404
405         if (d->ShowModal() != wxID_OK) {
406                 return;
407         }
408
409         optional<dcp::EncryptedKDM> kdm;
410         try {
411                 kdm = dcp::EncryptedKDM (dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE));
412         } catch (exception& e) {
413                 error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
414                 return;
415         }
416
417         /* Try to decrypt it to get an early preview of any errors */
418         try {
419                 decrypt_kdm_with_helpful_error (*kdm);
420         } catch (KDMError& e) {
421                 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
422                 return;
423         } catch (exception& e) {
424                 error_dialog (_parent, e.what());
425                 return;
426         }
427
428         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
429         bool const kdm_matches_any_cpl = std::any_of(cpls.begin(), cpls.end(), [kdm](shared_ptr<const dcp::CPL> cpl) { return cpl->id() == kdm->cpl_id(); });
430         bool const kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
431
432         if (!kdm_matches_any_cpl) {
433                 error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
434                 return;
435         }
436
437         if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
438                 message_dialog (_parent, _("This KDM was made for one of the CPLs in this DCP, but not the currently selected one.  To play the currently-selected CPL you will need a different KDM."));
439         }
440
441         dcp->add_kdm (*kdm);
442
443         auto film = _film.lock ();
444         DCPOMATIC_ASSERT (film);
445         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
446 }
447
448 void
449 ContentMenu::ov ()
450 {
451         DCPOMATIC_ASSERT (!_content.empty ());
452         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
453         DCPOMATIC_ASSERT (dcp);
454
455         auto d = make_wx<wxDirDialog>(_parent, _("Select OV"));
456
457         if (d->ShowModal() == wxID_OK) {
458                 dcp->add_ov (wx_to_std (d->GetPath()));
459                 shared_ptr<Film> film = _film.lock ();
460                 DCPOMATIC_ASSERT (film);
461                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
462         }
463 }
464
465 void
466 ContentMenu::properties ()
467 {
468         auto film = _film.lock ();
469         DCPOMATIC_ASSERT (film);
470         auto d = make_wx<ContentPropertiesDialog>(_parent, film, _content.front());
471         d->ShowModal ();
472 }
473
474
475 void
476 ContentMenu::advanced ()
477 {
478         DCPOMATIC_ASSERT(!_content.empty());
479
480         auto content = _content.front();
481         auto dialog = make_wx<ContentAdvancedDialog>(_parent, content);
482
483         if (dialog->ShowModal() == wxID_CANCEL) {
484                 return;
485         }
486
487         if (content->video) {
488                 content->video->set_use(!dialog->ignore_video());
489                 content->video->set_burnt_subtitle_language(dialog->burnt_subtitle_language());
490         }
491
492         auto ffmpeg = dynamic_pointer_cast<FFmpegContent>(content);
493         if (ffmpeg) {
494                 ffmpeg->set_filters(dialog->filters());
495         }
496
497         if (dialog->video_frame_rate()) {
498                 auto film = _film.lock();
499                 DCPOMATIC_ASSERT(film);
500                 content->set_video_frame_rate(film, *dialog->video_frame_rate());
501         } else {
502                 content->unset_video_frame_rate();
503         }
504 }
505
506
507 void
508 ContentMenu::cpl_selected (wxCommandEvent& ev)
509 {
510         if (!_pop_up_open) {
511                 return;
512         }
513
514         DCPOMATIC_ASSERT (!_content.empty ());
515         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
516         DCPOMATIC_ASSERT (dcp);
517
518         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
519         DCPOMATIC_ASSERT (ev.GetId() > 0);
520         DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
521
522         auto i = cpls.begin ();
523         for (int j = 0; j < ev.GetId() - 1; ++j) {
524                 ++i;
525         }
526
527         dcp->set_cpl ((*i)->id ());
528         auto film = _film.lock ();
529         DCPOMATIC_ASSERT (film);
530         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
531 }
532
533
534 void
535 ContentMenu::auto_crop ()
536 {
537         DCPOMATIC_ASSERT (_content.size() == 1);
538
539         auto film = _film.lock ();
540         DCPOMATIC_ASSERT (film);
541
542         auto update_viewer = [this](Crop crop) {
543                 auto film = _film.lock();
544                 DCPOMATIC_ASSERT (film);
545                 auto const content = _content.front();
546                 auto const current_crop = content->video->actual_crop();
547                 _viewer.set_crop_guess(
548                         dcpomatic::Rect<float>(
549                                 static_cast<float>(std::max(0, crop.left - current_crop.left)) / content->video->size().width,
550                                 static_cast<float>(std::max(0, crop.top - current_crop.top)) / content->video->size().height,
551                                 1.0f - (static_cast<float>(std::max(0, crop.left - current_crop.left + crop.right - current_crop.right)) / content->video->size().width),
552                                 1.0f - (static_cast<float>(std::max(0, crop.top - current_crop.top + crop.bottom - current_crop.bottom)) / content->video->size().height)
553                                 ));
554         };
555
556         auto guess_crop_for_content = [this, film]() {
557                 auto position = _viewer.position_in_content(_content.front()).get_value_or(
558                         ContentTime::from_frames(_content.front()->video->length(), _content.front()->video_frame_rate().get_value_or(24))
559                         );
560                 return guess_crop(film, _content.front(), Config::instance()->auto_crop_threshold(), position);
561         };
562
563         /* Make an initial guess in the view and open the dialog */
564
565         auto const crop = guess_crop_for_content ();
566         update_viewer (crop);
567
568         _auto_crop_dialog.reset(_parent, crop);
569         _auto_crop_dialog->Show ();
570
571         /* Update the dialog and view when the crop threshold changes */
572         _auto_crop_config_connection = Config::instance()->Changed.connect([this, guess_crop_for_content, update_viewer](Config::Property property) {
573                 auto film = _film.lock();
574                 DCPOMATIC_ASSERT (film);
575                 if (property == Config::AUTO_CROP_THRESHOLD) {
576                         auto const crop = guess_crop_for_content();
577                         _auto_crop_dialog->set(crop);
578                         update_viewer(crop);
579                 }
580         });
581
582         /* Also update the dialog and view when we're looking at a different frame */
583         _auto_crop_viewer_connection = _viewer.ImageChanged.connect([this, guess_crop_for_content, update_viewer](shared_ptr<PlayerVideo>) {
584                 auto const crop = guess_crop_for_content();
585                 _auto_crop_dialog->set(crop);
586                 update_viewer(crop);
587         });
588
589         /* Handle the user closing the dialog (with OK or cancel) */
590         _auto_crop_dialog->Bind(wxEVT_BUTTON, [this](wxCommandEvent& ev) {
591                 _auto_crop_config_connection.disconnect ();
592                 _auto_crop_viewer_connection.disconnect ();
593                 if (ev.GetId() == wxID_OK) {
594                         _content.front()->video->set_crop(_auto_crop_dialog->get());
595                 }
596                 _auto_crop_dialog->Show (false);
597                 _viewer.unset_crop_guess ();
598         });
599
600         /* Update the view when something in the dialog is changed */
601         _auto_crop_dialog->Changed.connect([update_viewer](Crop crop) {
602                 update_viewer (crop);
603         });
604 }