Somewhat hacky but seemingly functional frame back/forward (rest of #68).
authorCarl Hetherington <cth@carlh.net>
Sat, 20 Apr 2013 01:26:23 +0000 (02:26 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 20 Apr 2013 01:26:23 +0000 (02:26 +0100)
src/lib/decoder.h
src/lib/ffmpeg_decoder.cc
src/lib/ffmpeg_decoder.h
src/lib/format.cc
src/wx/film_viewer.cc
src/wx/film_viewer.h

index f2f5235168402c136e9c133ac8865c11396a99c7..2bc462c33c635013c4111447f43b2610204d562f 100644 (file)
@@ -59,6 +59,8 @@ public:
        virtual bool pass () = 0;
        virtual bool seek (double);
        virtual bool seek_to_last ();
+       virtual void seek_back () {}
+       virtual void seek_forward () {}
 
        boost::signals2::signal<void()> OutputChanged;
 
index 2d7092789b849de3110e07ceb7f980d28eb187bf..7c88c3c35717853f7fb790831bf4f01ff50b109f 100644 (file)
@@ -530,7 +530,7 @@ FFmpegDecoder::filter_and_emit_video ()
 bool
 FFmpegDecoder::seek (double p)
 {
-       return do_seek (p, false);
+       return do_seek (p, false, false);
 }
 
 bool
@@ -540,21 +540,57 @@ FFmpegDecoder::seek_to_last ()
           (used when we change decoder parameters and want to re-fetch the frame) we end up going forwards rather than
           staying in the same place.
        */
-       return do_seek (last_source_time(), true);
+       return do_seek (last_source_time(), true, false);
+}
+
+void
+FFmpegDecoder::seek_back ()
+{
+       do_seek (last_source_time() - 2.5 / frames_per_second (), true, true);
+}
+
+void
+FFmpegDecoder::seek_forward ()
+{
+       do_seek (last_source_time() - 0.5 / frames_per_second(), true, true);
 }
 
 bool
-FFmpegDecoder::do_seek (double p, bool backwards)
+FFmpegDecoder::do_seek (double p, bool backwards, bool accurate)
 {
        int64_t const vt = p / av_q2d (_format_context->streams[_video_stream]->time_base);
 
        int const r = av_seek_frame (_format_context, _video_stream, vt, backwards ? AVSEEK_FLAG_BACKWARD : 0);
-       
+
        avcodec_flush_buffers (_video_codec_context);
        if (_subtitle_codec_context) {
                avcodec_flush_buffers (_subtitle_codec_context);
        }
-       
+
+       if (accurate) {
+               while (1) {
+                       int r = av_read_frame (_format_context, &_packet);
+                       if (r < 0) {
+                               return true;
+                       }
+                       
+                       avcodec_get_frame_defaults (_frame);
+                       
+                       if (_packet.stream_index == _video_stream) {
+                               int finished = 0;
+                               int const r = avcodec_decode_video2 (_video_codec_context, _frame, &finished, &_packet);
+                               if (r >= 0 && finished) {
+                                       int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
+                                       if (bet > vt) {
+                                               break;
+                                       }
+                               }
+                       }
+                       
+                       av_free_packet (&_packet);
+               }
+       }
+               
        return r < 0;
 }
 
index 2a4d40b1d81be34879c85aaebcb8fffc80e47812..0c89b973dfbb9b47c237cd2e9ade391327758fd9 100644 (file)
@@ -102,11 +102,13 @@ public:
 
        bool seek (double);
        bool seek_to_last ();
+       void seek_forward ();
+       void seek_back ();
 
 private:
 
        bool pass ();
-       bool do_seek (double p, bool);
+       bool do_seek (double p, bool, bool);
        PixelFormat pixel_format () const;
        AVSampleFormat audio_sample_format () const;
        int bytes_per_audio_sample () const;
index 640eee1670369911c5608eb5f308d51306bb5e79..8c3d0d8ad7f8f5eea541258032415c1b6cab12b7 100644 (file)
@@ -50,7 +50,7 @@ FixedFormat::name () const
                s << _nickname << N_(" (");
        }
 
-       s << setprecision(3) << (_ratio / 100.0) << N_(":1");
+       s << setprecision(3) << _ratio << N_(":1");
 
        if (!_nickname.empty ()) {
                s << N_(")");
index 00f89528553ced676392fc8833dbd1b44349043b..5770c5b70e5066e30b0bd0b7631a2163cd3f8b17 100644 (file)
@@ -51,6 +51,8 @@ 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 (new wxStaticText (this, wxID_ANY, wxT("")))
        , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
        , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
@@ -72,14 +74,18 @@ FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
        wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
        time_sizer->Add (_frame, 0, wxEXPAND);
        time_sizer->Add (_timecode, 0, wxEXPAND);
-       
+
+       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);
 
        _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
 
        _frame->SetMinSize (wxSize (84, -1));
+       _back_button->SetMinSize (wxSize (32, -1));
+       _forward_button->SetMinSize (wxSize (32, -1));
 
        _panel->Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (FilmViewer::paint_panel), 0, this);
        _panel->Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (FilmViewer::panel_sized), 0, this);
@@ -88,6 +94,8 @@ FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
        _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);
 
        set_film (f);
 
@@ -121,7 +129,7 @@ FilmViewer::film_changed (Film::Property p)
                if (_decoders.video == 0) {
                        break;
                }
-               _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2, _3));
+               _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2, _3, _4));
                _decoders.video->OutputChanged.connect (boost::bind (&FilmViewer::decoder_changed, this));
                _decoders.video->set_subtitle_stream (_film->subtitle_stream());
                calculate_sizes ();
@@ -392,7 +400,7 @@ FilmViewer::check_play_state ()
 }
 
 void
-FilmViewer::process_video (shared_ptr<Image> image, bool, shared_ptr<Subtitle> sub)
+FilmViewer::process_video (shared_ptr<Image> image, bool, shared_ptr<Subtitle> sub, double t)
 {
        _raw_frame = image;
        _raw_sub = sub;
@@ -401,7 +409,6 @@ FilmViewer::process_video (shared_ptr<Image> image, bool, shared_ptr<Subtitle> s
 
        _got_frame = true;
 
-       double const t = _decoders.video->last_source_time ();
        double const fps = _decoders.video->frames_per_second ();
        _frame->SetLabel (wxString::Format ("%d", int (rint (t * fps))));
 
@@ -465,3 +472,28 @@ FilmViewer::active_jobs_changed (bool a)
        _play_button->Enable (!a);
 }
 
+void
+FilmViewer::back_clicked (wxCommandEvent &)
+{
+       if (!_decoders.video) {
+               return;
+       }
+       
+       _decoders.video->seek_back ();
+       get_frame ();
+       _panel->Refresh ();
+       _panel->Update ();
+}
+
+void
+FilmViewer::forward_clicked (wxCommandEvent &)
+{
+       if (!_decoders.video) {
+               return;
+       }
+
+       _decoders.video->seek_forward ();
+       get_frame ();
+       _panel->Refresh ();
+       _panel->Update ();
+}
index 859bf7edeb8939f2b8f042bcf9558996b679c700..a78c772a49858570f9dd0170e6cf8dd5a752460c 100644 (file)
@@ -48,7 +48,7 @@ private:
        void slider_moved (wxScrollEvent &);
        void play_clicked (wxCommandEvent &);
        void timer (wxTimerEvent &);
-       void process_video (boost::shared_ptr<Image>, bool, boost::shared_ptr<Subtitle>);
+       void process_video (boost::shared_ptr<Image>, bool, boost::shared_ptr<Subtitle>, double);
        void calculate_sizes ();
        void check_play_state ();
        void update_from_raw ();
@@ -56,12 +56,16 @@ private:
        void raw_to_display ();
        void get_frame ();
        void active_jobs_changed (bool);
+       void back_clicked (wxCommandEvent &);
+       void forward_clicked (wxCommandEvent &);
 
        boost::shared_ptr<Film> _film;
 
        wxSizer* _v_sizer;
        wxPanel* _panel;
        wxSlider* _slider;
+       wxButton* _back_button;
+       wxButton* _forward_button;
        wxStaticText* _frame;
        wxStaticText* _timecode;
        wxToggleButton* _play_button;