Merge speed-up branch.
authorCarl Hetherington <cth@carlh.net>
Sun, 16 Dec 2012 14:53:22 +0000 (14:53 +0000)
committerCarl Hetherington <cth@carlh.net>
Sun, 16 Dec 2012 14:53:22 +0000 (14:53 +0000)
1  2 
src/lib/examine_content_job.cc
src/lib/ffmpeg_decoder.cc
src/lib/ffmpeg_decoder.h
src/lib/film.cc
src/lib/film.h
src/lib/imagemagick_decoder.h
src/lib/transcoder.cc
src/lib/transcoder.h
src/lib/video_decoder.h
src/wx/film_editor.cc
src/wx/film_viewer.cc

index dc2fc305cdd5618524371bf299affbd101ba0ee6,93333605b8e6a6983fc63fd668f420bd426fae35..70a04b8255059262ec0de2fc8e0c929a277b6702
@@@ -59,30 -60,111 +59,46 @@@ ExamineContentJob::name () cons
  void
  ExamineContentJob::run ()
  {
-       /* Decode the content to get an accurate length */
 -      float progress_remaining = 1;
++      descend (1);
  
-       /* We don't want to use any existing length here, as progress
-          will be messed up.
+       /* Set the film's length to either
+          a) a length judged by running through the content or
+          b) the length from a decoder's header.
        */
-       _film->unset_length ();
-       _film->set_crop (Crop ());
 -
+       if (!_film->trust_content_header()) {
+               /* Decode the content to get an accurate length */
+               
+               /* We don't want to use any existing length here, as progress
+                  will be messed up.
+               */
+               _film->unset_length ();
++              _film->set_crop (Crop ());
+               
 -              shared_ptr<Options> o (new Options ("", "", ""));
 -              o->out_size = Size (512, 512);
 -              o->apply_crop = false;
++              shared_ptr<DecodeOptions> o (new DecodeOptions);
+               o->decode_audio = false;
+               
 -              descend (0.5);
 -              
 -              pair<shared_ptr<VideoDecoder>, shared_ptr<AudioDecoder> > decoders = decoder_factory (_film, o, this);
++              Decoders decoders = decoder_factory (_film, o, this);
+               
+               set_progress_unknown ();
 -              while (!decoders.first->pass()) {
++              while (!decoders.video->pass()) {
+                       /* keep going */
+               }
+               
 -              _film->set_length (decoders.first->video_frame());
++              _film->set_length (decoders.video->video_frame());
+               
+               _film->log()->log (String::compose ("Video length examined as %1 frames", _film->length().get()));
+               
 -              ascend ();
 -              
 -              progress_remaining -= 0.5;
 -              
+       } else {
 -              /* Get a quick decoder to get the content's length from its header.
 -                 It would have been nice to just use the thumbnail transcoder's decoder,
 -                 but that's a bit fiddly, and this isn't too expensive.
 -              */
++              /* Get a quick decoder to get the content's length from its header */
+               
 -              shared_ptr<Options> o (new Options ("", "", ""));
 -              o->out_size = Size (1024, 1024);
 -              pair<shared_ptr<VideoDecoder>, shared_ptr<AudioDecoder> > d = decoder_factory (_film, o, 0);
 -              _film->set_length (d.first->length());
++              shared_ptr<DecodeOptions> o (new DecodeOptions);
++              Decoders d = decoder_factory (_film, o, 0);
++              _film->set_length (d.video->length());
        
-       shared_ptr<DecodeOptions> o (new DecodeOptions);
-       o->decode_audio = false;
-       descend (1);
-       Decoders decoders = decoder_factory (_film, o, this);
-       set_progress_unknown ();
-       while (!decoders.video->pass()) {
-               /* keep going */
+               _film->log()->log (String::compose ("Video length obtained from header as %1 frames", _film->length().get()));
        }
  
-       _film->set_length (decoders.video->video_frame());
 -      /* Now make thumbnails for it */
--
-       _film->log()->log (String::compose ("Video length is %1 frames", _film->length()));
 -      descend (progress_remaining);
 -
 -      try {
 -              shared_ptr<Options> o (new Options (_film->dir ("thumbs"), ".png", ""));
 -              o->out_size = _film->size ();
 -              o->apply_crop = false;
 -              o->decode_audio = false;
 -              o->decode_video_skip = _film->length().get() / 128;
 -              o->decode_subtitles = true;
 -              shared_ptr<ImageMagickEncoder> e (new ImageMagickEncoder (_film, o));
 -              Transcoder w (_film, o, this, e);
 -              w.go ();
 -
 -              /* Now set the film's length from the transcoder's decoder, since we
 -                 went to all the trouble of going through the content.
 -              */
 -
 -              _film->set_length (w.video_decoder()->video_frame());
 -              
 -      } catch (std::exception& e) {
 -
 -              ascend ();
 -              set_progress (1);
 -              set_error (e.what ());
 -              set_state (FINISHED_ERROR);
 -              return;
 -              
 -      }
 -
 -      string const tdir = _film->dir ("thumbs");
 -      vector<SourceFrame> thumbs;
 -
 -      for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (tdir); i != boost::filesystem::directory_iterator(); ++i) {
 -
 -              /* Aah, the sweet smell of progress */
 -#if BOOST_FILESYSTEM_VERSION == 3             
 -              string const l = boost::filesystem::path(*i).leaf().generic_string();
 -#else
 -              string const l = i->leaf ();
 -#endif
 -              
 -              size_t const d = l.find (".png");
 -              size_t const t = l.find (".tmp");
 -              if (d != string::npos && t == string::npos) {
 -                      thumbs.push_back (atoi (l.substr (0, d).c_str()));
 -              }
 -      }
 -
 -      sort (thumbs.begin(), thumbs.end());
 -      _film->set_thumbs (thumbs);     
--
        ascend ();
        set_progress (1);
        set_state (FINISHED_OK);
index 24ee89b21c30fb0f81d516683ecbed9cd69cdd12,acaf149f43ade599dff78d95c109d36c86133097..136843190d9cbd1cb46290eb8e5dcd7c209ee671
@@@ -616,67 -631,9 +609,73 @@@ FFmpegAudioStream::to_string () cons
        return String::compose ("ffmpeg %1 %2 %3 %4", _id, _sample_rate, _channel_layout, _name);
  }
  
 +void
 +FFmpegDecoder::out_with_sync ()
 +{
 +      /* Where we are in the output, in seconds */
 +      double const out_pts_seconds = video_frame() / frames_per_second();
 +      
 +      /* Where we are in the source, in seconds */
 +      double const source_pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base)
 +              * av_frame_get_best_effort_timestamp(_frame);
 +      
 +      _film->log()->log (
 +              String::compose ("Source video frame ready; source at %1, output at %2", source_pts_seconds, out_pts_seconds),
 +              Log::VERBOSE
 +              );
 +      
 +      if (!_first_video) {
 +              _first_video = source_pts_seconds;
 +      }
 +      
 +      /* Difference between where we are and where we should be */
 +      double const delta = source_pts_seconds - _first_video.get() - out_pts_seconds;
 +      double const one_frame = 1 / frames_per_second();
 +      
 +      /* Insert frames if required to get out_pts_seconds up to pts_seconds */
 +      if (delta > one_frame) {
 +              int const extra = rint (delta / one_frame);
 +              for (int i = 0; i < extra; ++i) {
 +                      repeat_last_video ();
 +                      _film->log()->log (
 +                              String::compose (
 +                                      "Extra video frame inserted at %1s; source frame %2, source PTS %3 (at %4 fps)",
 +                                      out_pts_seconds, video_frame(), source_pts_seconds, frames_per_second()
 +                                                      )
 +                              );
 +              }
 +      }
 +      
 +      if (delta > -one_frame) {
 +              /* Process this frame */
 +              filter_and_emit_video (_frame);
 +      } else {
 +              /* Otherwise we are omitting a frame to keep things right */
 +              _film->log()->log (String::compose ("Frame removed at %1s", out_pts_seconds));
 +      }
 +}
 +
 +void
 +FFmpegDecoder::film_changed (Film::Property p)
 +{
 +      switch (p) {
 +      case Film::CROP:
 +      {
 +              boost::mutex::scoped_lock lm (_filter_graphs_mutex);
 +              _filter_graphs.clear ();
 +      }
 +      OutputChanged ();
 +      break;
 +
 +      default:
 +              break;
 +      }
 +}
 +
+ /** @return Length (in video frames) according to our content's header */
+ SourceFrame
+ FFmpegDecoder::length () const
+ {
+       return (double(_format_context->duration) / AV_TIME_BASE) * frames_per_second();
+ }
++
Simple merge
diff --cc src/lib/film.cc
index e7f47c462c550c4bb8bc9fedb97cad1871bdd1ed,e2a4cbeda60efda1089f2b8e840c6a64940f5702..4cfe7de0abce543e9b7c71e9743c422bf8542338
@@@ -882,6 -992,22 +887,22 @@@ Film::set_content (string c
  
        }
  }
 -      if (!_trust_content_header) && !content().empty()) {
+ void
+ Film::set_trust_content_header (bool t)
+ {
+       {
+               boost::mutex::scoped_lock lm (_state_mutex);
+               _trust_content_header = t;
+       }
+       
+       signal_changed (TRUST_CONTENT_HEADER);
++      if (!_trust_content_header && !content().empty()) {
+               /* We just said that we don't trust the content's header */
+               examine_content ();
+       }
+ }
               
  void
  Film::set_dcp_content_type (DCPContentType const * t)
diff --cc src/lib/film.h
Simple merge
Simple merge
index f44a3ed7be30de313b459e2e242a13c66c7ed566,a7e79b05f74d6338cd2c5debfe6d77381b905839..87a1fb3f28c8435587e7601b11de13cde3b08a30
@@@ -63,18 -63,20 +63,20 @@@ Transcoder::Transcoder (shared_ptr<Film
        }
  
        /* Set up the decoder to use the film's set streams */
 -      _decoders.first->set_subtitle_stream (f->subtitle_stream ());
 -      if (_decoders.second) {
 -              _decoders.second->set_audio_stream (f->audio_stream ());
 +      _decoders.video->set_subtitle_stream (f->subtitle_stream ());
-       _decoders.audio->set_audio_stream (f->audio_stream ());
++      if (_decoders.audio) {
++              _decoders.audio->set_audio_stream (f->audio_stream ());
+       }
  
        if (_matcher) {
 -              _decoders.first->connect_video (_matcher);
 +              _decoders.video->connect_video (_matcher);
                _matcher->connect_video (_encoder);
        } else {
 -              _decoders.first->connect_video (_encoder);
 +              _decoders.video->connect_video (_encoder);
        }
        
-       if (_matcher && _delay_line) {
 -      if (_matcher && _delay_line && _decoders.second) {
 -              _decoders.second->connect_audio (_delay_line);
++      if (_matcher && _delay_line && _decoders.audio) {
 +              _decoders.audio->connect_audio (_delay_line);
                _delay_line->connect_audio (_matcher);
                _matcher->connect_audio (_gain);
                _gain->connect_audio (_encoder);
@@@ -93,12 -95,12 +95,12 @@@ Transcoder::go (
                
                while (1) {
                        if (!done[0]) {
 -                              done[0] = _decoders.first->pass ();
 -                              _decoders.first->set_progress ();
 +                              done[0] = _decoders.video->pass ();
 +                              _decoders.video->set_progress ();
                        }
  
-                       if (!done[1] && dynamic_pointer_cast<Decoder> (_decoders.audio) != dynamic_pointer_cast<Decoder> (_decoders.video)) {
 -                      if (!done[1] && _decoders.second && dynamic_pointer_cast<Decoder> (_decoders.second) != dynamic_pointer_cast<Decoder> (_decoders.first)) {
 -                              done[1] = _decoders.second->pass ();
++                      if (!done[1] && _decoders.audio && dynamic_pointer_cast<Decoder> (_decoders.audio) != dynamic_pointer_cast<Decoder> (_decoders.video)) {
 +                              done[1] = _decoders.audio->pass ();
                        } else {
                                done[1] = true;
                        }
index f27984aaa53f5632ecd3547695f345bdbaa2eddb,4a9667b3c5165b95aa9f8aeea2786b206f184b56..b50113742369c817aa3e0b57d28158f31d111b41
@@@ -57,6 -49,10 +57,10 @@@ public
  
        void go ();
  
 -              return _decoders.first;
+       boost::shared_ptr<VideoDecoder> video_decoder () const {
++              return _decoders.video;
+       }
  protected:
        /** A Job that is running this Transcoder, or 0 */
        Job* _job;
Simple merge
Simple merge
index 1cf45fd4e47a568b22ae3af5c94aceb6a069535f,a821323586dd2cce1c5e29b275aac541667bca7f..8312fa5e5527bcded5dea1373fb245b47e41ffb0
  using std::string;
  using std::pair;
  using std::max;
 +using std::cout;
  using boost::shared_ptr;
  
 -class ThumbPanel : public wxPanel
 +FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
 +      : wxPanel (p)
 +      , _panel (new wxPanel (this))
 +      , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
 +      , _play_button (new wxToggleButton (this, wxID_ANY, wxT ("Play")))
 +      , _out_width (0)
 +      , _out_height (0)
 +      , _panel_width (0)
 +      , _panel_height (0)
  {
 -public:
 -      ThumbPanel (wxPanel* parent, shared_ptr<Film> film)
 -              : wxPanel (parent)
 -              , _film (film)
 -              , _index (0)
 -              , _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;
 -              }
 -
 -              if (_frame_rebuild_needed) {
 -                      _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
 -
 -                      _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)));
 -                      }
 -
 -                      _frame_rebuild_needed = false;
 -                      compose ();
 -              }
 +      wxBoxSizer* v_sizer = new wxBoxSizer (wxVERTICAL);
 +      SetSizer (v_sizer);
  
 -              if (_composition_needed) {
 -                      compose ();
 -              }
 +      v_sizer->Add (_panel, 1, wxEXPAND);
  
 -              wxPaintDC dc (this);
 -              if (_bitmap) {
 -                      dc.DrawBitmap (*_bitmap, 0, 0, false);
 -              }
 +      wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
 +      h_sizer->Add (_play_button, 0, wxEXPAND);
 +      h_sizer->Add (_slider, 1, wxEXPAND);
  
 -              if (_film->with_subtitles() && _subtitle) {
 -                      dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
 -              }
 -      }
 +      v_sizer->Add (h_sizer, 0, wxEXPAND);
  
 -      /** Handle a size event */
 -      void size_event (wxSizeEvent &)
 -      {
 -              if (!_image) {
 -                      return;
 -              }
 +      _panel->Bind (wxEVT_PAINT, &FilmViewer::paint_panel, this);
 +      _panel->Bind (wxEVT_SIZE, &FilmViewer::panel_sized, this);
 +      _slider->Bind (wxEVT_SCROLL_THUMBTRACK, &FilmViewer::slider_moved, this);
 +      _slider->Bind (wxEVT_SCROLL_PAGEUP, &FilmViewer::slider_moved, this);
 +      _slider->Bind (wxEVT_SCROLL_PAGEDOWN, &FilmViewer::slider_moved, this);
 +      _play_button->Bind (wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, &FilmViewer::play_clicked, this);
 +      _timer.Bind (wxEVT_TIMER, &FilmViewer::timer, this);
  
 -              recompose ();
 -      }
 +      set_film (_film);
 +}
  
 -      /** @param n Thumbnail index */
 -      void set (int n)
 +void
 +FilmViewer::film_changed (Film::Property p)
 +{
 +      switch (p) {
 +      case Film::FORMAT:
 +              calculate_sizes ();
 +              update_from_raw ();
 +              break;
 +      case Film::CONTENT:
        {
 -              _index = n;
 -              _frame_rebuild_needed = true;
 -              Refresh ();
 +              shared_ptr<DecodeOptions> o (new DecodeOptions);
 +              o->decode_audio = false;
 +              o->video_sync = false;
 +              _decoders = decoder_factory (_film, o, 0);
 +              _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2));
 +              _decoders.video->OutputChanged.connect (boost::bind (&FilmViewer::decoder_changed, this));
 +              break;
        }
 -
 -      void set_film (shared_ptr<Film> f)
 -      {
 -              _film = f;
 -              if (!_film) {
 -                      clear ();
 -                      _frame_rebuild_needed = true;
 -                      Refresh ();
 -              } else {
 -                      _frame_rebuild_needed = true;
 -                      Refresh ();
 -              }
 +      default:
 +              break;
        }
 +}
  
 -      /** Clear our thumbnail image */
 -      void clear ()
 -      {
 -              _bitmap.reset ();
 -              _image.reset ();
 -              _subtitle.reset ();
 +void
 +FilmViewer::set_film (shared_ptr<Film> f)
 +{
 +      if (_film == f) {
 +              return;
        }
 +      
 +      _film = f;
  
 -      void recompose ()
 -      {
 -              _composition_needed = true;
 -              Refresh ();
 +      if (!_film) {
 +              return;
        }
  
 -      DECLARE_EVENT_TABLE ();
 -
 -private:
 +      _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
  
 -      void compose ()
 -      {
 -              _composition_needed = false;
 -              
 -              if (!_film || !_image) {
 -                      return;
 -              }
 -
 -              /* 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;
 -              }
 +      film_changed (Film::CONTENT);
 +      film_changed (Film::CROP);
 +      film_changed (Film::FORMAT);
 +}
  
 -              _bitmap.reset (new wxBitmap (_transformed_image));
 +void
 +FilmViewer::decoder_changed ()
 +{
 +      seek_and_update (_decoders.video->last_source_frame ());
 +}
  
 -              if (_subtitle) {
 +void
 +FilmViewer::timer (wxTimerEvent& ev)
 +{
 +      _panel->Refresh ();
 +      _panel->Update ();
  
 -                      _subtitle->transformed_area = subtitle_transformed_area (
 -                              x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
 -                              );
 +      shared_ptr<Image> last = _display;
 +      while (last == _display) {
 +              _decoders.video->pass ();
 +      }
  
 -                      _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 -= rint (_film->crop().left * x_scale);
 -                      _subtitle->transformed_area.y -= rint (_film->crop().top * y_scale);
 -                      _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
 +      if (_film->length()) {
 +              int const new_slider_position = 4096 * _decoders.video->last_source_frame() / _film->length().get();
 +              if (new_slider_position != _slider->GetValue()) {
 +                      _slider->SetValue (new_slider_position);
                }
        }
 +}
  
 -      shared_ptr<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 ();
 -              }
 +void
 +FilmViewer::paint_panel (wxPaintEvent& ev)
 +{
 +      wxPaintDC dc (_panel);
 +      if (!_display) {
 +              return;
 +      }
  
 -              Rect base_area;
 -              Rect transformed_area;
 -              wxImage base_image;
 -              wxImage transformed_image;
 -              shared_ptr<wxBitmap> bitmap;
 -      };
 +      wxImage i (_out_width, _out_height, _display->data()[0], true);
 +      wxBitmap b (i);
 +      dc.DrawBitmap (b, 0, 0);
 +}
  
 -      shared_ptr<SubtitleView> _subtitle;
 -};
  
 -BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
 -EVT_PAINT (ThumbPanel::paint_event)
 -EVT_SIZE (ThumbPanel::size_event)
 -END_EVENT_TABLE ()
 +void
 +FilmViewer::slider_moved (wxCommandEvent& ev)
 +{
 +      if (_film->length()) {
 +              seek_and_update (_slider->GetValue() * _film->length().get() / 4096);
 +      }
 +}
  
 -FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
 -      : wxPanel (p)
 +void
 +FilmViewer::seek_and_update (SourceFrame f)
  {
-       _decoders.video->seek (f);
 -      _sizer = new wxBoxSizer (wxVERTICAL);
 -      SetSizer (_sizer);
++      if (_decoders.video->seek (f)) {
++              return;
++      }
        
 -      _thumb_panel = new ThumbPanel (this, f);
 -      _sizer->Add (_thumb_panel, 1, wxEXPAND);
 -
 -      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);
 -
 -      _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
 +      shared_ptr<Image> last = _display;
 +      while (last == _display) {
 +              _decoders.video->pass ();
 +      }
 +      _panel->Refresh ();
 +      _panel->Update ();
 +}
  
 -      set_film (_film);
 +void
 +FilmViewer::panel_sized (wxSizeEvent& ev)
 +{
 +      _panel_width = ev.GetSize().GetWidth();
 +      _panel_height = ev.GetSize().GetHeight();
 +      calculate_sizes ();
 +      update_from_raw ();
  }
  
  void