Improve config dialog layout (#181).
[dcpomatic.git] / src / wx / film_viewer.cc
index a3827fba1df1e6acc5c67ca7dca661d97bda95ec..d42829880876959638afabcfdcf1d202a0c6d7fa 100644 (file)
 */
 
 /** @file  src/film_viewer.cc
- *  @brief A wx widget to view `thumbnails' of a Film.
+ *  @brief A wx widget to view a preview of a Film.
  */
 
 #include <iostream>
 #include <iomanip>
+#include <wx/tglbtn.h>
 #include "lib/film.h"
-#include "lib/format.h"
+#include "lib/ratio.h"
 #include "lib/util.h"
-#include "lib/thumbs_job.h"
 #include "lib/job_manager.h"
-#include "lib/film_state.h"
-#include "lib/options.h"
-#include "lib/subtitle.h"
+#include "lib/image.h"
+#include "lib/scaler.h"
+#include "lib/exceptions.h"
+#include "lib/examine_content_job.h"
+#include "lib/filter.h"
+#include "lib/player.h"
+#include "lib/video_content.h"
+#include "lib/ffmpeg_content.h"
+#include "lib/imagemagick_content.h"
 #include "film_viewer.h"
 #include "wx_util.h"
+#include "video_decoder.h"
+
+using std::string;
+using std::pair;
+using std::min;
+using std::max;
+using std::cout;
+using std::list;
+using std::make_pair;
+using boost::shared_ptr;
+using boost::dynamic_pointer_cast;
+using boost::weak_ptr;
+using libdcp::Size;
+
+FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
+       : wxPanel (p)
+       , _panel (new wxPanel (this))
+       , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
+       , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
+       , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
+       , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
+       , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
+       , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
+       , _got_frame (false)
+{
+#ifndef __WXOSX__
+       _panel->SetDoubleBuffered (true);
+#endif
+       
+       _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
+       
+       _v_sizer = new wxBoxSizer (wxVERTICAL);
+       SetSizer (_v_sizer);
 
-using namespace std;
-using namespace boost;
+       _v_sizer->Add (_panel, 1, wxEXPAND);
 
-class ThumbPanel : public wxPanel
-{
-public:
-       ThumbPanel (wxPanel* parent, Film* film)
-               : wxPanel (parent)
-               , _film (film)
-               , _frame_rebuild_needed (false)
-               , _composition_needed (false)
-       {}
-
-       /** Handle a paint event */
-       void paint_event (wxPaintEvent& ev)
-       {
-               if (!_film || _film->thumbs().size() == 0) {
-                       wxPaintDC dc (this);
-                       return;
-               }
+       wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
 
-               if (_frame_rebuild_needed) {
-                       _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
+       wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
+       time_sizer->Add (_frame_number, 0, wxEXPAND);
+       time_sizer->Add (_timecode, 0, wxEXPAND);
 
-                       _subtitle.reset ();
-                       pair<Position, string> s = _film->thumb_subtitle (_index);
-                       if (!s.second.empty ()) {
-                               _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
-                       }
+       h_sizer->Add (_back_button, 0, wxALL, 2);
+       h_sizer->Add (time_sizer, 0, wxEXPAND);
+       h_sizer->Add (_forward_button, 0, wxALL, 2);
+       h_sizer->Add (_play_button, 0, wxEXPAND);
+       h_sizer->Add (_slider, 1, wxEXPAND);
 
-                       _frame_rebuild_needed = false;
-                       compose ();
-               }
+       _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
 
-               if (_composition_needed) {
-                       compose ();
-               }
+       _frame_number->SetMinSize (wxSize (84, -1));
+       _back_button->SetMinSize (wxSize (32, -1));
+       _forward_button->SetMinSize (wxSize (32, -1));
 
-               wxPaintDC dc (this);
-               if (_bitmap) {
-                       dc.DrawBitmap (*_bitmap, 0, 0, false);
-               }
+       _panel->Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (FilmViewer::paint_panel), 0, this);
+       _panel->Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (FilmViewer::panel_sized), 0, this);
+       _slider->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
+       _slider->Connect (wxID_ANY, wxEVT_SCROLL_PAGEUP, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
+       _slider->Connect (wxID_ANY, wxEVT_SCROLL_PAGEDOWN, wxScrollEventHandler (FilmViewer::slider_moved), 0, this);
+       _play_button->Connect (wxID_ANY, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEventHandler (FilmViewer::play_clicked), 0, this);
+       _timer.Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (FilmViewer::timer), 0, this);
+       _back_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmViewer::back_clicked), 0, this);
+       _forward_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmViewer::forward_clicked), 0, this);
 
-               if (_film->with_subtitles() && _subtitle) {
-                       dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
-               }
-       }
+       set_film (f);
 
-       /** Handle a size event */
-       void size_event (wxSizeEvent &)
-       {
-               if (!_image) {
-                       return;
-               }
+       JobManager::instance()->ActiveJobsChanged.connect (
+               bind (&FilmViewer::active_jobs_changed, this, _1)
+               );
+}
 
-               recompose ();
+void
+FilmViewer::set_film (shared_ptr<Film> f)
+{
+       if (_film == f) {
+               return;
        }
 
-       /** @param n Thumbnail index */
-       void set (int n)
-       {
-               _index = n;
-               _frame_rebuild_needed = true;
-               Refresh ();
-       }
+       _film = f;
 
-       void set_film (Film* f)
-       {
-               _film = f;
-               if (!_film) {
-                       clear ();
-                       _frame_rebuild_needed = true;
-                       Refresh ();
-               } else {
-                       _frame_rebuild_needed = true;
-                       Refresh ();
-               }
+       _frame.reset ();
+       _queue.clear ();
+
+       if (!_film) {
+               return;
        }
 
-       /** Clear our thumbnail image */
-       void clear ()
-       {
-               _bitmap.reset ();
-               _image.reset ();
-               _subtitle.reset ();
+       _player = f->make_player ();
+       _player->disable_audio ();
+       _player->Video.connect (boost::bind (&FilmViewer::process_video, this, _1, _3));
+       _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
+
+       calculate_sizes ();
+       fetch_current_frame_again ();
+}
+
+void
+FilmViewer::fetch_current_frame_again ()
+{
+       if (!_player) {
+               return;
        }
 
-       void recompose ()
-       {
-               _composition_needed = true;
-               Refresh ();
+       /* Player::video_position is the time after the last frame that we received.
+          We want to see it again, so seek back one frame.
+       */
+
+       Time p = _player->video_position() - _film->video_frames_to_time (1);
+       if (p < 0) {
+               p = 0;
        }
 
-       DECLARE_EVENT_TABLE ();
+       _player->seek (p, true);
+       fetch_next_frame ();
+}
 
-private:
+void
+FilmViewer::timer (wxTimerEvent &)
+{
+       if (!_player) {
+               return;
+       }
+       
+       fetch_next_frame ();
 
-       void compose ()
-       {
-               _composition_needed = false;
-               
-               if (!_film || !_image) {
-                       return;
+       if (_film->length()) {
+               int const new_slider_position = 4096 * _player->video_position() / _film->length();
+               if (new_slider_position != _slider->GetValue()) {
+                       _slider->SetValue (new_slider_position);
                }
+       }
+}
 
-               /* Size of the view */
-               int vw, vh;
-               GetSize (&vw, &vh);
-
-               Crop const fc = _film->crop ();
-
-               /* Cropped rectangle */
-               Rect cropped_area (
-                       fc.left,
-                       fc.top,
-                       _image->GetWidth() - (fc.left + fc.right),
-                       _image->GetHeight() - (fc.top + fc.bottom)
-                       );
-
-               /* Target ratio */
-               float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
-
-               _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
-
-               float x_scale = 1;
-               float y_scale = 1;
-
-               if ((float (vw) / vh) > target) {
-                       /* view is longer (horizontally) than the ratio; fit height */
-                       _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
-                       x_scale = vh * target / cropped_area.width;
-                       y_scale = float (vh) / cropped_area.height;
-               } else {
-                       /* view is shorter (horizontally) than the ratio; fit width */
-                       _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
-                       x_scale = float (vw) / cropped_area.width;
-                       y_scale = (vw / target) / cropped_area.height;
-               }
 
-               _bitmap.reset (new wxBitmap (_transformed_image));
+void
+FilmViewer::paint_panel (wxPaintEvent &)
+{
+       wxPaintDC dc (_panel);
+
+       if (!_frame || !_film || !_out_size.width || !_out_size.height) {
+               dc.Clear ();
+               return;
+       }
 
-               if (_subtitle) {
+       shared_ptr<Image> packed_frame (new Image (_frame, false));
 
-                       _subtitle->transformed_area = subtitle_transformed_area (
-                               x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
-                               );
+       wxImage frame (_out_size.width, _out_size.height, packed_frame->data()[0], true);
+       wxBitmap frame_bitmap (frame);
+       dc.DrawBitmap (frame_bitmap, 0, 0);
 
-                       _subtitle->transformed_image = _subtitle->base_image;
-                       _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
-                       _subtitle->transformed_area.x -= _film->crop().left;
-                       _subtitle->transformed_area.y -= _film->crop().top;
-                       _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
-               }
+       if (_out_size.width < _panel_size.width) {
+               wxPen p (GetBackgroundColour ());
+               wxBrush b (GetBackgroundColour ());
+               dc.SetPen (p);
+               dc.SetBrush (b);
+               dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
        }
 
-       Film* _film;
-       shared_ptr<wxImage> _image;
-       wxImage _transformed_image;
-       /** currently-displayed thumbnail index */
-       int _index;
-       shared_ptr<wxBitmap> _bitmap;
-       bool _frame_rebuild_needed;
-       bool _composition_needed;
-
-       struct SubtitleView
-       {
-               SubtitleView (Position p, wxString const & i)
-                       : base_image (i)
-               {
-                       base_area.x = p.x;
-                       base_area.y = p.y;
-                       base_area.width = base_image.GetWidth ();
-                       base_area.height = base_image.GetHeight ();
-               }
+       if (_out_size.height < _panel_size.height) {
+               wxPen p (GetBackgroundColour ());
+               wxBrush b (GetBackgroundColour ());
+               dc.SetPen (p);
+               dc.SetBrush (b);
+               dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
+       }               
+}
 
-               Rect base_area;
-               Rect transformed_area;
-               wxImage base_image;
-               wxImage transformed_image;
-               shared_ptr<wxBitmap> bitmap;
-       };
 
-       shared_ptr<SubtitleView> _subtitle;
-};
+void
+FilmViewer::slider_moved (wxScrollEvent &)
+{
+       if (_film && _player) {
+               _player->seek (_slider->GetValue() * _film->length() / 4096, false);
+               fetch_next_frame ();
+       }
+}
 
-BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
-EVT_PAINT (ThumbPanel::paint_event)
-EVT_SIZE (ThumbPanel::size_event)
-END_EVENT_TABLE ()
+void
+FilmViewer::panel_sized (wxSizeEvent& ev)
+{
+       _panel_size.width = ev.GetSize().GetWidth();
+       _panel_size.height = ev.GetSize().GetHeight();
+       calculate_sizes ();
+       fetch_current_frame_again ();
+}
 
-FilmViewer::FilmViewer (Film* f, wxWindow* p)
-       : wxPanel (p)
-       , _film (0)
+void
+FilmViewer::calculate_sizes ()
 {
-       _sizer = new wxBoxSizer (wxVERTICAL);
-       SetSizer (_sizer);
+       if (!_film || !_player) {
+               return;
+       }
+
+       Ratio const * container = _film->container ();
        
-       _thumb_panel = new ThumbPanel (this, f);
-       _sizer->Add (_thumb_panel, 1, wxEXPAND);
+       float const panel_ratio = static_cast<float> (_panel_size.width) / _panel_size.height;
+       float const film_ratio = container ? container->ratio () : 1.78;
+                       
+       if (panel_ratio < film_ratio) {
+               /* panel is less widscreen than the film; clamp width */
+               _out_size.width = _panel_size.width;
+               _out_size.height = _out_size.width / film_ratio;
+       } else {
+               /* panel is more widescreen than the film; clamp height */
+               _out_size.height = _panel_size.height;
+               _out_size.width = _out_size.height * film_ratio;
+       }
 
-       int const m = max ((size_t) 1, f ? f->thumbs().size() - 1 : 0);
-       _slider = new wxSlider (this, wxID_ANY, 0, 0, m);
-       _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
-       set_thumbnail (0);
+       /* Catch silly values */
+       _out_size.width = max (64, _out_size.width);
+       _out_size.height = max (64, _out_size.height);
 
-       _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
+       _player->set_video_container_size (_out_size);
+}
 
-       set_film (_film);
+void
+FilmViewer::play_clicked (wxCommandEvent &)
+{
+       check_play_state ();
 }
 
 void
-FilmViewer::set_thumbnail (int n)
+FilmViewer::check_play_state ()
 {
-       if (_film == 0 || int (_film->thumbs().size()) <= n) {
+       if (!_film || _film->dcp_video_frame_rate() == 0) {
                return;
        }
-
-       _thumb_panel->set (n);
+       
+       if (_play_button->GetValue()) {
+               _timer.Start (1000 / _film->dcp_video_frame_rate());
+       } else {
+               _timer.Stop ();
+       }
 }
 
 void
-FilmViewer::slider_changed (wxCommandEvent &)
+FilmViewer::process_video (shared_ptr<const Image> image, Time t)
 {
-       set_thumbnail (_slider->GetValue ());
+       if (_got_frame) {
+               /* This is an additional frame emitted by a single pass.  Store it. */
+               _queue.push_front (make_pair (image, t));
+               return;
+       }
+       
+       _frame = image;
+       _got_frame = true;
+
+       double const fps = _film->dcp_video_frame_rate ();
+       /* Count frame number from 1 ... not sure if this is the best idea */
+       _frame_number->SetLabel (wxString::Format (wxT("%d"), int (rint (t * fps / TIME_HZ)) + 1));
+       
+       double w = static_cast<double>(t) / TIME_HZ;
+       int const h = (w / 3600);
+       w -= h * 3600;
+       int const m = (w / 60);
+       w -= m * 60;
+       int const s = floor (w);
+       w -= s;
+       int const f = rint (w * fps);
+       _timecode->SetLabel (wxString::Format (wxT("%02d:%02d:%02d:%02d"), h, m, s, f));
 }
 
+/** Ask the player to emit its next frame, then update our display */
 void
-FilmViewer::film_changed (FilmState::Property p)
+FilmViewer::fetch_next_frame ()
 {
-       switch (p) {
-       case FilmState::THUMBS:
-               if (_film && _film->thumbs().size() > 1) {
-                       _slider->SetRange (0, _film->thumbs().size() - 1);
-               } else {
-                       _thumb_panel->clear ();
-                       _slider->SetRange (0, 1);
+       /* Clear our frame in case we don't get a new one */
+       _frame.reset ();
+
+       if (!_player) {
+               return;
+       }
+
+       _got_frame = false;
+       
+       if (!_queue.empty ()) {
+               process_video (_queue.back().first, _queue.back().second);
+               _queue.pop_back ();
+       } else {
+               try {
+                       while (!_got_frame && !_player->pass ()) {}
+               } catch (DecodeError& e) {
+                       _play_button->SetValue (false);
+                       check_play_state ();
+                       error_dialog (this, wxString::Format (_("Could not decode video for view (%s)"), std_to_wx(e.what()).data()));
                }
-               
-               _slider->SetValue (0);
-               set_thumbnail (0);
-               break;
-       case FilmState::CONTENT:
-               setup_visibility ();
-               _film->examine_content ();
-               update_thumbs ();
-               break;
-       case FilmState::CROP:
-       case FilmState::FORMAT:
-       case FilmState::WITH_SUBTITLES:
-       case FilmState::SUBTITLE_OFFSET:
-       case FilmState::SUBTITLE_SCALE:
-               _thumb_panel->recompose ();
-               break;
-       default:
-               break;
        }
+
+       _panel->Refresh ();
+       _panel->Update ();
 }
 
 void
-FilmViewer::set_film (Film* f)
+FilmViewer::active_jobs_changed (bool a)
 {
-       if (_film == f) {
-               return;
+       if (a) {
+               list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
+               list<shared_ptr<Job> >::iterator i = jobs.begin ();             
+               while (i != jobs.end() && boost::dynamic_pointer_cast<ExamineContentJob> (*i) == 0) {
+                       ++i;
+               }
+               
+               if (i == jobs.end() || (*i)->finished()) {
+                       /* no examine content job running, so we're ok to use the viewer */
+                       a = false;
+               }
        }
-       
-       _film = f;
-       _thumb_panel->set_film (_film);
+                       
+       _slider->Enable (!a);
+       _play_button->Enable (!a);
+}
 
-       if (!_film) {
+void
+FilmViewer::back_clicked (wxCommandEvent &)
+{
+       if (!_player) {
                return;
        }
 
-       _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
-       film_changed (Film::CROP);
-       film_changed (Film::THUMBS);
-       setup_visibility ();
+       /* Player::video_position is the time after the last frame that we received.
+          We want to see the one before it, so we need to go back 2.
+       */
+       
+       _player->seek (_player->video_position() - _film->video_frames_to_time(2), true);
+       fetch_next_frame ();
 }
 
 void
-FilmViewer::update_thumbs ()
+FilmViewer::forward_clicked (wxCommandEvent &)
 {
-       if (!_film) {
+       if (!_player) {
                return;
        }
 
-       _film->update_thumbs_pre_gui ();
-
-       shared_ptr<const FilmState> s = _film->state_copy ();
-       shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
-       o->out_size = _film->size ();
-       o->apply_crop = false;
-       o->decode_audio = false;
-       o->decode_video_frequency = 128;
-       o->decode_subtitles = true;
-       
-       shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
-       j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
-       JobManager::instance()->add (j);
+       fetch_next_frame ();
 }
 
 void
-FilmViewer::setup_visibility ()
+FilmViewer::player_changed (bool frequent)
 {
-       if (!_film) {
+       if (frequent) {
                return;
        }
-
-       ContentType const c = _film->content_type ();
-       _slider->Show (c == VIDEO);
+       
+       calculate_sizes ();
+       fetch_current_frame_again ();
 }