Another try at sorting out the thorny question of timing.
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/film_viewer.cc
21  *  @brief A wx widget to view a preview of a Film.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include <wx/tglbtn.h>
27 #include "lib/film.h"
28 #include "lib/ratio.h"
29 #include "lib/util.h"
30 #include "lib/job_manager.h"
31 #include "lib/image.h"
32 #include "lib/scaler.h"
33 #include "lib/exceptions.h"
34 #include "lib/examine_content_job.h"
35 #include "lib/filter.h"
36 #include "lib/player.h"
37 #include "lib/video_content.h"
38 #include "lib/ffmpeg_content.h"
39 #include "lib/imagemagick_content.h"
40 #include "film_viewer.h"
41 #include "wx_util.h"
42 #include "video_decoder.h"
43
44 using std::string;
45 using std::pair;
46 using std::min;
47 using std::max;
48 using std::cout;
49 using std::list;
50 using boost::shared_ptr;
51 using boost::dynamic_pointer_cast;
52 using boost::weak_ptr;
53 using libdcp::Size;
54
55 FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
56         : wxPanel (p)
57         , _panel (new wxPanel (this))
58         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
59         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
60         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
61         , _frame (new wxStaticText (this, wxID_ANY, wxT("")))
62         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
63         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
64         , _got_frame (false)
65 {
66 #ifndef __WXOSX__
67         _panel->SetDoubleBuffered (true);
68 #endif
69         
70 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
71         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
72 #endif  
73         
74         _v_sizer = new wxBoxSizer (wxVERTICAL);
75         SetSizer (_v_sizer);
76
77         _v_sizer->Add (_panel, 1, wxEXPAND);
78
79         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
80
81         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
82         time_sizer->Add (_frame, 0, wxEXPAND);
83         time_sizer->Add (_timecode, 0, wxEXPAND);
84
85         h_sizer->Add (_back_button, 0, wxALL, 2);
86         h_sizer->Add (time_sizer, 0, wxEXPAND);
87         h_sizer->Add (_forward_button, 0, wxALL, 2);
88         h_sizer->Add (_play_button, 0, wxEXPAND);
89         h_sizer->Add (_slider, 1, wxEXPAND);
90
91         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
92
93         _frame->SetMinSize (wxSize (84, -1));
94         _back_button->SetMinSize (wxSize (32, -1));
95         _forward_button->SetMinSize (wxSize (32, -1));
96
97         _panel->Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (FilmViewer::paint_panel), 0, this);
98         _panel->Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (FilmViewer::panel_sized), 0, this);
99         _slider->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
100         _slider->Connect (wxID_ANY, wxEVT_SCROLL_PAGEUP, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
101         _slider->Connect (wxID_ANY, wxEVT_SCROLL_PAGEDOWN, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
102         _play_button->Connect (wxID_ANY, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEventHandler (FilmViewer::play_clicked), 0, this);
103         _timer.Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (FilmViewer::timer), 0, this);
104         _back_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmViewer::back_clicked), 0, this);
105         _forward_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmViewer::forward_clicked), 0, this);
106
107         set_film (f);
108
109         JobManager::instance()->ActiveJobsChanged.connect (
110                 bind (&FilmViewer::active_jobs_changed, this, _1)
111                 );
112 }
113
114 void
115 FilmViewer::film_changed (Film::Property p)
116 {
117         switch (p) {
118         case Film::CONTAINER:
119                 calculate_sizes ();
120                 update_from_raw ();
121                 break;
122         case Film::CONTENT:
123         {
124                 calculate_sizes ();
125                 wxScrollEvent ev;
126                 slider_moved (ev);
127                 break;
128         }
129         case Film::WITH_SUBTITLES:
130         case Film::SUBTITLE_OFFSET:
131         case Film::SUBTITLE_SCALE:
132                 update_from_decoder ();
133                 raw_to_display ();
134                 _panel->Refresh ();
135                 _panel->Update ();
136                 break;
137         case Film::SCALER:
138                 update_from_decoder ();
139                 break;
140         default:
141                 break;
142         }
143 }
144
145 void
146 FilmViewer::set_film (shared_ptr<Film> f)
147 {
148         if (_film == f) {
149                 return;
150         }
151
152         _film = f;
153
154         _raw_frame.reset ();
155         _display_frame.reset ();
156         _panel->Refresh ();
157         _panel->Update ();
158
159         if (!_film) {
160                 return;
161         }
162
163         _player = f->player ();
164         _player->disable_audio ();
165         _player->Video.connect (bind (&FilmViewer::process_video, this, _1, _2, _3));
166         
167         _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
168         _film->ContentChanged.connect (boost::bind (&FilmViewer::film_content_changed, this, _1, _2));
169
170         film_changed (Film::CONTENT);
171         film_changed (Film::CONTAINER);
172         film_changed (Film::WITH_SUBTITLES);
173         film_changed (Film::SUBTITLE_OFFSET);
174         film_changed (Film::SUBTITLE_SCALE);
175 }
176
177 void
178 FilmViewer::update_from_decoder ()
179 {
180         if (!_player) {
181                 return;
182         }
183
184         _player->seek (_player->video_position() - _film->video_frames_to_time (1));
185         get_frame ();
186         _panel->Refresh ();
187         _panel->Update ();
188 }
189
190 void
191 FilmViewer::timer (wxTimerEvent &)
192 {
193         if (!_player) {
194                 return;
195         }
196         
197         get_frame ();
198
199         if (_film->length()) {
200                 int const new_slider_position = 4096 * _player->video_position() / _film->length();
201                 if (new_slider_position != _slider->GetValue()) {
202                         _slider->SetValue (new_slider_position);
203                 }
204         }
205
206         _panel->Refresh ();
207         _panel->Update ();
208 }
209
210
211 void
212 FilmViewer::paint_panel (wxPaintEvent &)
213 {
214         wxPaintDC dc (_panel);
215
216         if (!_display_frame || !_film || !_out_size.width || !_out_size.height) {
217                 dc.Clear ();
218                 return;
219         }
220
221         wxImage frame (_out_size.width, _out_size.height, _display_frame->data()[0], true);
222         wxBitmap frame_bitmap (frame);
223         dc.DrawBitmap (frame_bitmap, 0, 0);
224
225         if (_out_size.width < _panel_size.width) {
226                 wxPen p (GetBackgroundColour ());
227                 wxBrush b (GetBackgroundColour ());
228                 dc.SetPen (p);
229                 dc.SetBrush (b);
230                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
231         }
232
233         if (_out_size.height < _panel_size.height) {
234                 wxPen p (GetBackgroundColour ());
235                 wxBrush b (GetBackgroundColour ());
236                 dc.SetPen (p);
237                 dc.SetBrush (b);
238                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
239         }               
240 }
241
242
243 void
244 FilmViewer::slider_moved (wxScrollEvent &)
245 {
246         cout << "slider " << _slider->GetValue() << " " << _film->length() << "\n";
247         
248         if (_film && _player) {
249                 _player->seek (_slider->GetValue() * _film->length() / 4096);
250         }
251         
252         get_frame ();
253         _panel->Refresh ();
254         _panel->Update ();
255 }
256
257 void
258 FilmViewer::panel_sized (wxSizeEvent& ev)
259 {
260         _panel_size.width = ev.GetSize().GetWidth();
261         _panel_size.height = ev.GetSize().GetHeight();
262         calculate_sizes ();
263         update_from_raw ();
264 }
265
266 void
267 FilmViewer::update_from_raw ()
268 {
269         if (!_raw_frame) {
270                 return;
271         }
272
273         raw_to_display ();
274         
275         _panel->Refresh ();
276         _panel->Update ();
277 }
278
279 void
280 FilmViewer::raw_to_display ()
281 {
282         if (!_raw_frame || _out_size.width < 64 || _out_size.height < 64 || !_film) {
283                 return;
284         }
285
286         /* Get a compacted image as we have to feed it to wxWidgets */
287         _display_frame.reset (new SimpleImage (_raw_frame, false));
288 }       
289
290 void
291 FilmViewer::calculate_sizes ()
292 {
293         if (!_film || !_player) {
294                 return;
295         }
296
297         Ratio const * container = _film->container ();
298         
299         float const panel_ratio = static_cast<float> (_panel_size.width) / _panel_size.height;
300         float const film_ratio = container ? container->ratio () : 1.78;
301                         
302         if (panel_ratio < film_ratio) {
303                 /* panel is less widscreen than the film; clamp width */
304                 _out_size.width = _panel_size.width;
305                 _out_size.height = _out_size.width / film_ratio;
306         } else {
307                 /* panel is more widescreen than the film; clamp height */
308                 _out_size.height = _panel_size.height;
309                 _out_size.width = _out_size.height * film_ratio;
310         }
311
312         /* Catch silly values */
313         _out_size.width = max (64, _out_size.width);
314         _out_size.height = max (64, _out_size.height);
315
316         _player->set_video_container_size (_out_size);
317         update_from_decoder ();
318 }
319
320 void
321 FilmViewer::play_clicked (wxCommandEvent &)
322 {
323         check_play_state ();
324 }
325
326 void
327 FilmViewer::check_play_state ()
328 {
329         if (!_film || _film->dcp_video_frame_rate() == 0) {
330                 return;
331         }
332         
333         if (_play_button->GetValue()) {
334                 _timer.Start (1000 / _film->dcp_video_frame_rate());
335         } else {
336                 _timer.Stop ();
337         }
338 }
339
340 void
341 FilmViewer::process_video (shared_ptr<const Image> image, bool, Time t)
342 {
343         _raw_frame = image;
344
345         raw_to_display ();
346
347         _got_frame = true;
348
349         double const fps = _film->dcp_video_frame_rate ();
350         /* Count frame number from 1 ... not sure if this is the best idea */
351         _frame->SetLabel (wxString::Format (wxT("%d"), int (rint (t * fps / TIME_HZ)) + 1));
352
353         double w = static_cast<double>(t) / TIME_HZ;
354         int const h = (w / 3600);
355         w -= h * 3600;
356         int const m = (w / 60);
357         w -= m * 60;
358         int const s = floor (w);
359         w -= s;
360         int const f = rint (w * fps);
361         _timecode->SetLabel (wxString::Format (wxT("%02d:%02d:%02d:%02d"), h, m, s, f));
362 }
363
364 /** Get a new _raw_frame from the decoder and then do
365  *  raw_to_display ().
366  */
367 void
368 FilmViewer::get_frame ()
369 {
370         /* Clear our raw frame in case we don't get a new one */
371         _raw_frame.reset ();
372
373         if (!_player) {
374                 _display_frame.reset ();
375                 return;
376         }
377
378         try {
379                 _got_frame = false;
380                 while (!_got_frame) {
381                         if (_player->pass ()) {
382                                 /* We didn't get a frame before the decoder gave up,
383                                    so clear our display frame.
384                                 */
385                                 _display_frame.reset ();
386                                 break;
387                         }
388                 }
389         } catch (DecodeError& e) {
390                 _play_button->SetValue (false);
391                 check_play_state ();
392                 error_dialog (this, wxString::Format (_("Could not decode video for view (%s)"), std_to_wx(e.what()).data()));
393         }
394 }
395
396 void
397 FilmViewer::active_jobs_changed (bool a)
398 {
399         if (a) {
400                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
401                 list<shared_ptr<Job> >::iterator i = jobs.begin ();             
402                 while (i != jobs.end() && boost::dynamic_pointer_cast<ExamineContentJob> (*i) == 0) {
403                         ++i;
404                 }
405                 
406                 if (i == jobs.end() || (*i)->finished()) {
407                         /* no examine content job running, so we're ok to use the viewer */
408                         a = false;
409                 }
410         }
411                         
412         _slider->Enable (!a);
413         _play_button->Enable (!a);
414 }
415
416 void
417 FilmViewer::film_content_changed (weak_ptr<Content>, int p)
418 {
419         if (p == ContentProperty::LENGTH) {
420                 /* Force an update to our frame */
421                 wxScrollEvent ev;
422                 slider_moved (ev);
423         } else if (p == VideoContentProperty::VIDEO_CROP || p == VideoContentProperty::VIDEO_RATIO) {
424                 update_from_decoder ();
425         }               
426 }
427
428 void
429 FilmViewer::back_clicked (wxCommandEvent &)
430 {
431         if (!_player) {
432                 return;
433         }
434         
435         _player->seek_back ();
436         get_frame ();
437         _panel->Refresh ();
438         _panel->Update ();
439 }
440
441 void
442 FilmViewer::forward_clicked (wxCommandEvent &)
443 {
444         if (!_player) {
445                 return;
446         }
447
448         get_frame ();
449         _panel->Refresh ();
450         _panel->Update ();
451 }