summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-04-20 02:26:23 +0100
committerCarl Hetherington <cth@carlh.net>2013-04-20 02:26:23 +0100
commit98499a61e17e68c438e56fd8854081a4c98b15ad (patch)
treedaa2d57f826486ee05fef3fc5fc7ba5af56f789b /src
parentf2989966b2871ac5fc5f78c2a3ce7867f344b7cd (diff)
Somewhat hacky but seemingly functional frame back/forward (rest of #68).
Diffstat (limited to 'src')
-rw-r--r--src/lib/decoder.h2
-rw-r--r--src/lib/ffmpeg_decoder.cc46
-rw-r--r--src/lib/ffmpeg_decoder.h4
-rw-r--r--src/lib/format.cc2
-rw-r--r--src/wx/film_viewer.cc40
-rw-r--r--src/wx/film_viewer.h6
6 files changed, 88 insertions, 12 deletions
diff --git a/src/lib/decoder.h b/src/lib/decoder.h
index f2f523516..2bc462c33 100644
--- a/src/lib/decoder.h
+++ b/src/lib/decoder.h
@@ -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;
diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc
index 2d7092789..7c88c3c35 100644
--- a/src/lib/ffmpeg_decoder.cc
+++ b/src/lib/ffmpeg_decoder.cc
@@ -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;
}
diff --git a/src/lib/ffmpeg_decoder.h b/src/lib/ffmpeg_decoder.h
index 2a4d40b1d..0c89b973d 100644
--- a/src/lib/ffmpeg_decoder.h
+++ b/src/lib/ffmpeg_decoder.h
@@ -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;
diff --git a/src/lib/format.cc b/src/lib/format.cc
index 640eee167..8c3d0d8ad 100644
--- a/src/lib/format.cc
+++ b/src/lib/format.cc
@@ -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_(")");
diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc
index 00f895285..5770c5b70 100644
--- a/src/wx/film_viewer.cc
+++ b/src/wx/film_viewer.cc
@@ -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 ();
+}
diff --git a/src/wx/film_viewer.h b/src/wx/film_viewer.h
index 859bf7ede..a78c772a4 100644
--- a/src/wx/film_viewer.h
+++ b/src/wx/film_viewer.h
@@ -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;