More OS X packaging tweaks.
[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/format.h"
29 #include "lib/util.h"
30 #include "lib/job_manager.h"
31 #include "lib/options.h"
32 #include "lib/subtitle.h"
33 #include "lib/image.h"
34 #include "lib/scaler.h"
35 #include "lib/exceptions.h"
36 #include "lib/examine_content_job.h"
37 #include "lib/filter.h"
38 #include "film_viewer.h"
39 #include "wx_util.h"
40 #include "video_decoder.h"
41
42 using std::string;
43 using std::pair;
44 using std::max;
45 using std::cout;
46 using std::list;
47 using boost::shared_ptr;
48 using libdcp::Size;
49
50 FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
51         : wxPanel (p)
52         , _panel (new wxPanel (this))
53         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
54         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
55         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
56         , _frame (new wxStaticText (this, wxID_ANY, wxT("")))
57         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
58         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
59         , _display_frame_x (0)
60         , _got_frame (false)
61 {
62 #ifndef __WXOSX__
63         _panel->SetDoubleBuffered (true);
64 #endif
65         
66 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
67         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
68 #endif  
69         
70         _v_sizer = new wxBoxSizer (wxVERTICAL);
71         SetSizer (_v_sizer);
72
73         _v_sizer->Add (_panel, 1, wxEXPAND);
74
75         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
76
77         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
78         time_sizer->Add (_frame, 0, wxEXPAND);
79         time_sizer->Add (_timecode, 0, wxEXPAND);
80
81         h_sizer->Add (_back_button, 0, wxALL, 2);
82         h_sizer->Add (time_sizer, 0, wxEXPAND);
83         h_sizer->Add (_forward_button, 0, wxALL, 2);
84         h_sizer->Add (_play_button, 0, wxEXPAND);
85         h_sizer->Add (_slider, 1, wxEXPAND);
86
87         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
88
89         _frame->SetMinSize (wxSize (84, -1));
90         _back_button->SetMinSize (wxSize (32, -1));
91         _forward_button->SetMinSize (wxSize (32, -1));
92
93         _panel->Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (FilmViewer::paint_panel), 0, this);
94         _panel->Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (FilmViewer::panel_sized), 0, this);
95         _slider->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
96         _slider->Connect (wxID_ANY, wxEVT_SCROLL_PAGEUP, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
97         _slider->Connect (wxID_ANY, wxEVT_SCROLL_PAGEDOWN, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
98         _play_button->Connect (wxID_ANY, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEventHandler (FilmViewer::play_clicked), 0, this);
99         _timer.Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (FilmViewer::timer), 0, this);
100         _back_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmViewer::back_clicked), 0, this);
101         _forward_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmViewer::forward_clicked), 0, this);
102
103         set_film (f);
104
105         JobManager::instance()->ActiveJobsChanged.connect (
106                 bind (&FilmViewer::active_jobs_changed, this, _1)
107                 );
108 }
109
110 void
111 FilmViewer::film_changed (Film::Property p)
112 {
113         switch (p) {
114         case Film::FORMAT:
115                 calculate_sizes ();
116                 update_from_raw ();
117                 break;
118         case Film::CONTENT:
119         {
120                 DecodeOptions o;
121                 o.decode_audio = false;
122                 o.decode_subtitles = true;
123                 o.video_sync = false;
124
125                 try {
126                         _decoders = decoder_factory (_film, o);
127                 } catch (StringError& e) {
128                         error_dialog (this, wxString::Format (_("Could not open content file (%s)"), std_to_wx(e.what()).data()));
129                         return;
130                 }
131                 
132                 if (_decoders.video == 0) {
133                         break;
134                 }
135                 _decoders.video->set_subtitle_stream (_film->subtitle_stream());
136                 _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2, _3, _4));
137                 _decoders.video->OutputChanged.connect (boost::bind (&FilmViewer::decoder_changed, this));
138                 calculate_sizes ();
139                 get_frame ();
140                 _panel->Refresh ();
141                 _slider->Show (_film->content_type() == VIDEO);
142                 _play_button->Show (_film->content_type() == VIDEO);
143                 _v_sizer->Layout ();
144                 break;
145         }
146         case Film::WITH_SUBTITLES:
147         case Film::SUBTITLE_OFFSET:
148         case Film::SUBTITLE_SCALE:
149         case Film::SCALER:
150         case Film::FILTERS:
151                 update_from_raw ();
152                 break;
153         case Film::SUBTITLE_STREAM:
154                 if (_decoders.video) {
155                         _decoders.video->set_subtitle_stream (_film->subtitle_stream ());
156                 }
157                 break;
158         default:
159                 break;
160         }
161 }
162
163 void
164 FilmViewer::set_film (shared_ptr<Film> f)
165 {
166         if (_film == f) {
167                 return;
168         }
169         
170         _film = f;
171
172         _raw_frame.reset ();
173         _display_frame.reset ();
174         _panel->Refresh ();
175         _panel->Update ();
176
177         if (!_film) {
178                 return;
179         }
180
181         _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
182
183         film_changed (Film::CONTENT);
184         film_changed (Film::FORMAT);
185         film_changed (Film::WITH_SUBTITLES);
186         film_changed (Film::SUBTITLE_OFFSET);
187         film_changed (Film::SUBTITLE_SCALE);
188         film_changed (Film::SUBTITLE_STREAM);
189 }
190
191 void
192 FilmViewer::decoder_changed ()
193 {
194         if (_decoders.video == 0 || _decoders.video->seek_to_last ()) {
195                 return;
196         }
197
198         get_frame ();
199         _panel->Refresh ();
200         _panel->Update ();
201 }
202
203 void
204 FilmViewer::timer (wxTimerEvent &)
205 {
206         if (!_film || !_decoders.video) {
207                 return;
208         }
209         
210         get_frame ();
211
212         if (_film->length()) {
213                 int const new_slider_position = 4096 * _decoders.video->last_source_time() / (_film->length().get() / _film->source_frame_rate());
214                 if (new_slider_position != _slider->GetValue()) {
215                         _slider->SetValue (new_slider_position);
216                 }
217         }
218
219         _panel->Refresh ();
220         _panel->Update ();
221 }
222
223
224 void
225 FilmViewer::paint_panel (wxPaintEvent &)
226 {
227         wxPaintDC dc (_panel);
228
229         if (!_display_frame || !_film || !_out_size.width || !_out_size.height) {
230                 dc.Clear ();
231                 return;
232         }
233
234         if (_display_frame_x) {
235                 dc.SetPen(*wxBLACK_PEN);
236                 dc.SetBrush(*wxBLACK_BRUSH);
237                 dc.DrawRectangle (0, 0, _display_frame_x, _film_size.height);
238                 dc.DrawRectangle (_display_frame_x + _film_size.width, 0, _display_frame_x, _film_size.height);
239         }
240
241         wxImage frame (_film_size.width, _film_size.height, _display_frame->data()[0], true);
242         wxBitmap frame_bitmap (frame);
243         dc.DrawBitmap (frame_bitmap, _display_frame_x, 0);
244
245         if (_film->with_subtitles() && _display_sub) {
246                 wxImage sub (_display_sub->size().width, _display_sub->size().height, _display_sub->data()[0], _display_sub->alpha(), true);
247                 wxBitmap sub_bitmap (sub);
248                 dc.DrawBitmap (sub_bitmap, _display_sub_position.x, _display_sub_position.y);
249         }
250
251         if (_out_size.width < _panel_size.width) {
252                 wxPen p (GetBackgroundColour ());
253                 wxBrush b (GetBackgroundColour ());
254                 dc.SetPen (p);
255                 dc.SetBrush (b);
256                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
257         }
258
259         if (_out_size.height < _panel_size.height) {
260                 wxPen p (GetBackgroundColour ());
261                 wxBrush b (GetBackgroundColour ());
262                 dc.SetPen (p);
263                 dc.SetBrush (b);
264                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
265         }               
266 }
267
268
269 void
270 FilmViewer::slider_moved (wxScrollEvent &)
271 {
272         if (!_film || !_film->length() || !_decoders.video) {
273                 return;
274         }
275         
276         if (_decoders.video->seek (_slider->GetValue() * _film->length().get() / (4096 * _film->source_frame_rate()))) {
277                 return;
278         }
279         
280         get_frame ();
281         _panel->Refresh ();
282         _panel->Update ();
283 }
284
285 void
286 FilmViewer::panel_sized (wxSizeEvent& ev)
287 {
288         _panel_size.width = ev.GetSize().GetWidth();
289         _panel_size.height = ev.GetSize().GetHeight();
290         calculate_sizes ();
291         update_from_raw ();
292 }
293
294 void
295 FilmViewer::update_from_raw ()
296 {
297         if (!_raw_frame) {
298                 return;
299         }
300
301         raw_to_display ();
302         
303         _panel->Refresh ();
304         _panel->Update ();
305 }
306
307 void
308 FilmViewer::raw_to_display ()
309 {
310         if (!_raw_frame || _out_size.width < 64 || _out_size.height < 64 || !_film) {
311                 return;
312         }
313
314         boost::shared_ptr<const Image> input = _raw_frame;
315
316         pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
317         if (!s.second.empty ()) {
318                 input = input->post_process (s.second, true);
319         }
320         
321         /* Get a compacted image as we have to feed it to wxWidgets */
322         _display_frame = input->scale_and_convert_to_rgb (_film_size, 0, _film->scaler(), false);
323
324         if (_raw_sub) {
325
326                 /* Our output is already cropped by the decoder, so we need to account for that
327                    when working out the scale that we are applying.
328                 */
329
330                 Size const cropped_size = _film->cropped_size (_film->size ());
331
332                 dvdomatic::Rect tx = subtitle_transformed_area (
333                         float (_film_size.width) / cropped_size.width,
334                         float (_film_size.height) / cropped_size.height,
335                         _raw_sub->area(), _film->subtitle_offset(), _film->subtitle_scale()
336                         );
337                 
338                 _display_sub.reset (new RGBPlusAlphaImage (_raw_sub->image()->scale (tx.size(), _film->scaler(), false)));
339                 _display_sub_position = tx.position();
340                 _display_sub_position.x += _display_frame_x;
341         } else {
342                 _display_sub.reset ();
343         }
344 }       
345
346 void
347 FilmViewer::calculate_sizes ()
348 {
349         if (!_film) {
350                 return;
351         }
352
353         Format const * format = _film->format ();
354         
355         float const panel_ratio = static_cast<float> (_panel_size.width) / _panel_size.height;
356         float const film_ratio = format ? format->container_ratio () : 1.78;
357                         
358         if (panel_ratio < film_ratio) {
359                 /* panel is less widscreen than the film; clamp width */
360                 _out_size.width = _panel_size.width;
361                 _out_size.height = _out_size.width / film_ratio;
362         } else {
363                 /* panel is more widescreen than the film; clamp height */
364                 _out_size.height = _panel_size.height;
365                 _out_size.width = _out_size.height * film_ratio;
366         }
367
368         /* Work out how much padding there is in terms of our display; this will be the x position
369            of our _display_frame.
370         */
371         _display_frame_x = 0;
372         if (format) {
373                 _display_frame_x = static_cast<float> (format->dcp_padding (_film)) * _out_size.width / format->dcp_size().width;
374         }
375
376         _film_size = _out_size;
377         _film_size.width -= _display_frame_x * 2;
378
379         /* Catch silly values */
380         if (_out_size.width < 64) {
381                 _out_size.width = 64;
382         }
383 }
384
385 void
386 FilmViewer::play_clicked (wxCommandEvent &)
387 {
388         check_play_state ();
389 }
390
391 void
392 FilmViewer::check_play_state ()
393 {
394         if (!_film) {
395                 return;
396         }
397         
398         if (_play_button->GetValue()) {
399                 _timer.Start (1000 / _film->source_frame_rate());
400         } else {
401                 _timer.Stop ();
402         }
403 }
404
405 void
406 FilmViewer::process_video (shared_ptr<const Image> image, bool, shared_ptr<Subtitle> sub, double t)
407 {
408         _raw_frame = image;
409         _raw_sub = sub;
410
411         raw_to_display ();
412
413         _got_frame = true;
414
415         double const fps = _decoders.video->frames_per_second ();
416         /* Count frame number from 1 ... not sure if this is the best idea */
417         _frame->SetLabel (wxString::Format (wxT("%d"), int (rint (t * fps)) + 1));
418
419         double w = t;
420         int const h = (w / 3600);
421         w -= h * 3600;
422         int const m = (w / 60);
423         w -= m * 60;
424         int const s = floor (w);
425         w -= s;
426         int const f = rint (w * fps);
427         _timecode->SetLabel (wxString::Format (wxT("%02d:%02d:%02d:%02d"), h, m, s, f));
428 }
429
430 void
431 FilmViewer::get_frame ()
432 {
433         /* Clear our raw frame in case we don't get a new one */
434         _raw_frame.reset ();
435
436         if (_decoders.video == 0) {
437                 _display_frame.reset ();
438                 return;
439         }
440
441         try {
442                 _got_frame = false;
443                 while (!_got_frame) {
444                         if (_decoders.video->pass ()) {
445                                 /* We didn't get a frame before the decoder gave up,
446                                    so clear our display frame.
447                                 */
448                                 _display_frame.reset ();
449                                 break;
450                         }
451                 }
452         } catch (DecodeError& e) {
453                 _play_button->SetValue (false);
454                 check_play_state ();
455                 error_dialog (this, wxString::Format (_("Could not decode video for view (%s)"), std_to_wx(e.what()).data()));
456         }
457 }
458
459 void
460 FilmViewer::active_jobs_changed (bool a)
461 {
462         if (a) {
463                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
464                 list<shared_ptr<Job> >::iterator i = jobs.begin ();             
465                 while (i != jobs.end() && boost::dynamic_pointer_cast<ExamineContentJob> (*i) == 0) {
466                         ++i;
467                 }
468                 
469                 if (i == jobs.end() || (*i)->finished()) {
470                         /* no examine content job running, so we're ok to use the viewer */
471                         a = false;
472                 }
473         }
474                         
475         _slider->Enable (!a);
476         _play_button->Enable (!a);
477 }
478
479 void
480 FilmViewer::back_clicked (wxCommandEvent &)
481 {
482         if (!_decoders.video) {
483                 return;
484         }
485         
486         _decoders.video->seek_back ();
487         get_frame ();
488         _panel->Refresh ();
489         _panel->Update ();
490 }
491
492 void
493 FilmViewer::forward_clicked (wxCommandEvent &)
494 {
495         if (!_decoders.video) {
496                 return;
497         }
498
499         _decoders.video->seek_forward ();
500         get_frame ();
501         _panel->Refresh ();
502         _panel->Update ();
503 }