From 4f9cb03792e85cbb5b4d554ab8ec0a3275fa7524 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 7 Mar 2017 19:27:33 +0000 Subject: [PATCH] Initial butler work. --- src/lib/audio_ring_buffers.cc | 104 +++++++++++++++++++++++++++++++ src/lib/audio_ring_buffers.h | 54 +++++++++++++++++ src/lib/butler.cc | 111 ++++++++++++++++++++++++++++++++++ src/lib/butler.h | 60 ++++++++++++++++++ src/lib/player_video.h | 5 ++ src/lib/video_ring_buffers.cc | 79 ++++++++++++++++++++++++ src/lib/video_ring_buffers.h | 45 ++++++++++++++ src/lib/wscript | 2 + src/wx/film_viewer.cc | 55 +++++++++-------- src/wx/film_viewer.h | 7 ++- 10 files changed, 495 insertions(+), 27 deletions(-) create mode 100644 src/lib/audio_ring_buffers.cc create mode 100644 src/lib/audio_ring_buffers.h create mode 100644 src/lib/butler.cc create mode 100644 src/lib/butler.h create mode 100644 src/lib/video_ring_buffers.cc create mode 100644 src/lib/video_ring_buffers.h diff --git a/src/lib/audio_ring_buffers.cc b/src/lib/audio_ring_buffers.cc new file mode 100644 index 000000000..81072474e --- /dev/null +++ b/src/lib/audio_ring_buffers.cc @@ -0,0 +1,104 @@ +/* + Copyright (C) 2016-2017 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "audio_ring_buffers.h" +#include "dcpomatic_assert.h" +#include "exceptions.h" +#include + +using std::min; +using std::cout; +using boost::shared_ptr; + +AudioRingBuffers::AudioRingBuffers () + : _used_in_head (0) +{ + +} + +void +AudioRingBuffers::put (shared_ptr data, DCPTime time) +{ + boost::mutex::scoped_lock lm (_mutex); + + if (!_buffers.empty ()) { + DCPOMATIC_ASSERT (_buffers.front()->channels() == data->channels()); + } + + _buffers.push_back (data); + _latest = time; +} + +void +AudioRingBuffers::get (float* out, int channels, int frames) +{ + boost::mutex::scoped_lock lm (_mutex); + + while (frames > 0) { + if (_buffers.empty ()) { + for (int i = 0; i < frames; ++i) { + for (int j = 0; j < channels; ++j) { + *out++ = 0; + } + } + cout << "audio underrun; missing " << frames << "!\n"; + return; + } + + int const to_do = min (frames, _buffers.front()->frames() - _used_in_head); + float** p = _buffers.front()->data(); + int const c = min (_buffers.front()->channels(), channels); + for (int i = 0; i < to_do; ++i) { + for (int j = 0; j < c; ++j) { + *out++ = p[j][i + _used_in_head]; + } + for (int j = c; j < channels; ++j) { + *out++ = 0; + } + } + _used_in_head += to_do; + frames -= to_do; + + if (_used_in_head == _buffers.front()->frames()) { + _buffers.pop_front (); + _used_in_head = 0; + } + } +} + +void +AudioRingBuffers::clear () +{ + boost::mutex::scoped_lock lm (_mutex); + _buffers.clear (); + _used_in_head = 0; + _latest = DCPTime (); +} + +Frame +AudioRingBuffers::size () +{ + boost::mutex::scoped_lock lm (_mutex); + Frame s = 0; + BOOST_FOREACH (shared_ptr i, _buffers) { + s += i->frames (); + } + return s - _used_in_head; +} diff --git a/src/lib/audio_ring_buffers.h b/src/lib/audio_ring_buffers.h new file mode 100644 index 000000000..cec7c712a --- /dev/null +++ b/src/lib/audio_ring_buffers.h @@ -0,0 +1,54 @@ +/* + Copyright (C) 2016-2017 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#ifndef DCPOMATIC_AUDIO_RING_BUFFERS_H +#define DCPOMATIC_AUDIO_RING_BUFFERS_H + +#include "audio_buffers.h" +#include "types.h" +#include "dcpomatic_time.h" +#include +#include +#include + +class AudioRingBuffers : public boost::noncopyable +{ +public: + AudioRingBuffers (); + + void put (boost::shared_ptr data, DCPTime time); + void get (float* out, int channels, int frames); + + void clear (); + Frame size (); + + DCPTime latest () const { + boost::mutex::scoped_lock lm (_mutex); + return _latest; + } + +private: + mutable boost::mutex _mutex; + std::list > _buffers; + DCPTime _latest; + int _used_in_head; +}; + +#endif diff --git a/src/lib/butler.cc b/src/lib/butler.cc new file mode 100644 index 000000000..e40ae0b24 --- /dev/null +++ b/src/lib/butler.cc @@ -0,0 +1,111 @@ +/* + Copyright (C) 2016-2017 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "butler.h" +#include "player.h" +#include +#include + +using std::cout; +using std::pair; +using std::make_pair; +using boost::weak_ptr; +using boost::shared_ptr; +using boost::bind; +using boost::optional; + +/** Video readahead in frames */ +#define VIDEO_READAHEAD 10 + +Butler::Butler (weak_ptr film, shared_ptr player) + : _film (film) + , _player (player) + , _pending_seek_accurate (false) + , _finished (false) +{ + _player->Video.connect (bind (&VideoRingBuffers::put, &_video, _1, _2)); + _thread = new boost::thread (bind (&Butler::thread, this)); +} + +Butler::~Butler () +{ + _thread->interrupt (); + try { + _thread->join (); + } catch (boost::thread_interrupted& e) { + /* No problem */ + } + delete _thread; +} + +void +Butler::thread () +{ + while (true) { + boost::mutex::scoped_lock lm (_mutex); + + while (_video.size() > VIDEO_READAHEAD && !_pending_seek_position) { + _summon.wait (lm); + } + + if (_pending_seek_position) { + _player->seek (*_pending_seek_position, _pending_seek_accurate); + _pending_seek_position = optional (); + } + + while (_video.size() < VIDEO_READAHEAD) { + _arrived.notify_all (); + if (_player->pass ()) { + _finished = true; + break; + } + } + } +} + +pair, DCPTime> +Butler::get_video () +{ + boost::mutex::scoped_lock lm (_mutex); + while (_video.size() == 0 && !_finished) { + _arrived.wait (lm); + } + + if (_finished) { + return make_pair (shared_ptr(), DCPTime()); + } + + return _video.get (); +} + +void +Butler::seek (DCPTime position, bool accurate) +{ + _video.clear (); + + { + boost::mutex::scoped_lock lm (_mutex); + _finished = false; + _pending_seek_position = position; + _pending_seek_accurate = accurate; + } + + _summon.notify_all (); +} diff --git a/src/lib/butler.h b/src/lib/butler.h new file mode 100644 index 000000000..d801d6cf9 --- /dev/null +++ b/src/lib/butler.h @@ -0,0 +1,60 @@ +/* + Copyright (C) 2016-2017 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "video_ring_buffers.h" +#include +#include +#include +#include +#include + +class Film; +class Player; +class PlayerVideo; + +class Butler : public boost::noncopyable +{ +public: + Butler (boost::weak_ptr film, boost::shared_ptr player); + ~Butler (); + + void seek (DCPTime position, bool accurate); + std::pair, DCPTime> get_video (); + +private: + void thread (); + void video (boost::shared_ptr video, DCPTime time); + + boost::weak_ptr _film; + boost::shared_ptr _player; + boost::thread* _thread; + + VideoRingBuffers _video; + + boost::mutex _mutex; + boost::condition _summon; + boost::condition _arrived; + boost::optional _pending_seek_position; + bool _pending_seek_accurate; + + bool _finished; + + boost::signals2::scoped_connection _player_video_connection; +}; diff --git a/src/lib/player_video.h b/src/lib/player_video.h index 60c9224b0..5017d0e3c 100644 --- a/src/lib/player_video.h +++ b/src/lib/player_video.h @@ -18,6 +18,9 @@ */ +#ifndef DCPOMATIC_PLAYER_VIDEO_H +#define DCPOMATIC_PLAYER_VIDEO_H + #include "types.h" #include "position.h" #include "dcpomatic_time.h" @@ -98,3 +101,5 @@ private: boost::optional _colour_conversion; boost::optional _subtitle; }; + +#endif diff --git a/src/lib/video_ring_buffers.cc b/src/lib/video_ring_buffers.cc new file mode 100644 index 000000000..254285456 --- /dev/null +++ b/src/lib/video_ring_buffers.cc @@ -0,0 +1,79 @@ +/* + Copyright (C) 2016-2017 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "video_ring_buffers.h" +#include "player_video.h" +#include +#include +#include + +using std::list; +using std::make_pair; +using std::cout; +using std::pair; +using boost::shared_ptr; +using boost::optional; + +void +VideoRingBuffers::put (shared_ptr frame, DCPTime time) +{ + cout << "put " << to_string(time) << "\n"; + boost::mutex::scoped_lock lm (_mutex); + _data.push_back (make_pair (frame, time)); +} + +pair, DCPTime> +VideoRingBuffers::get () +{ + boost::mutex::scoped_lock lm (_mutex); + if (_data.empty ()) { + cout << "get: no data.\n"; + return make_pair(shared_ptr(), DCPTime()); + } + pair, DCPTime> const r = _data.front (); + cout << "get: here we go! " << to_string(r.second) << "\n"; + _data.pop_front (); + return r; +} + +Frame +VideoRingBuffers::size () const +{ + boost::mutex::scoped_lock lm (_mutex); + return _data.size (); +} + +void +VideoRingBuffers::clear () +{ + boost::mutex::scoped_lock lm (_mutex); + _data.clear (); +} + +optional +VideoRingBuffers::latest () const +{ + boost::mutex::scoped_lock lm (_mutex); + if (_data.empty ()) { + return optional (); + } + + return _data.back().second; +} diff --git a/src/lib/video_ring_buffers.h b/src/lib/video_ring_buffers.h new file mode 100644 index 000000000..f728247fe --- /dev/null +++ b/src/lib/video_ring_buffers.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2016-2017 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "dcpomatic_time.h" +#include "player_video.h" +#include "types.h" +#include +#include +#include +#include + +class PlayerVideo; + +class VideoRingBuffers : public boost::noncopyable +{ +public: + void put (boost::shared_ptr frame, DCPTime time); + std::pair, DCPTime> get (); + + void clear (); + Frame size () const; + + boost::optional latest () const; + +private: + mutable boost::mutex _mutex; + std::list, DCPTime> > _data; +}; diff --git a/src/lib/wscript b/src/lib/wscript index 2c34577d9..23e846812 100644 --- a/src/lib/wscript +++ b/src/lib/wscript @@ -35,6 +35,7 @@ sources = """ audio_point.cc audio_processor.cc audio_stream.cc + butler.cc case_insensitive_sorter.cc cinema.cc cinema_kdms.cc @@ -144,6 +145,7 @@ sources = """ video_mxf_content.cc video_mxf_decoder.cc video_mxf_examiner.cc + video_ring_buffers.cc writer.cc """ diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc index a5371fcf0..8fff21345 100644 --- a/src/wx/film_viewer.cc +++ b/src/wx/film_viewer.cc @@ -39,6 +39,7 @@ #include "lib/video_content.h" #include "lib/video_decoder.h" #include "lib/timer.h" +#include "lib/butler.h" #include "lib/log.h" #include "lib/config.h" extern "C" { @@ -179,9 +180,9 @@ FilmViewer::set_film (shared_ptr film) _player->set_play_referenced (); _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1)); - _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1)); - _player->Video.connect (boost::bind (&FilmViewer::video, this, _1, _2)); + + recreate_butler (); calculate_sizes (); refresh (); @@ -189,6 +190,18 @@ FilmViewer::set_film (shared_ptr film) setup_sensitivity (); } +void +FilmViewer::recreate_butler () +{ + _butler.reset (); + + if (!_film) { + return; + } + + _butler.reset (new Butler (_film, _player)); +} + void FilmViewer::refresh_panel () { @@ -197,16 +210,15 @@ FilmViewer::refresh_panel () } void -FilmViewer::video (shared_ptr pv, DCPTime time) +FilmViewer::get () { - if (!_player) { - return; - } + pair, DCPTime> video; + do { + video = _butler->get_video (); + } while (_film->three_d() && ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT))); - if (_film->three_d ()) { - if ((_left_eye->GetValue() && pv->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && pv->eyes() == EYES_LEFT)) { - return; - } + if (!video.first) { + return; } /* In an ideal world, what we would do here is: @@ -227,28 +239,21 @@ FilmViewer::video (shared_ptr pv, DCPTime time) * image and convert it (from whatever the user has said it is) to RGB. */ - _frame = pv->image ( + _frame = video.first->image ( bind (&Log::dcp_log, _film->log().get(), _1, _2), bind (&PlayerVideo::always_rgb, _1), false, true ); - ImageChanged (pv); + ImageChanged (video.first); - _position = time; - _inter_position = pv->inter_position (); - _inter_size = pv->inter_size (); + _position = video.second; + _inter_position = video.first->inter_position (); + _inter_size = video.first->inter_size (); refresh_panel (); } -void -FilmViewer::get () -{ - Image const * current = _frame.get (); - while (!_player->pass() && _frame.get() == current) {} -} - void FilmViewer::timer () { @@ -334,7 +339,7 @@ FilmViewer::panel_sized (wxSizeEvent& ev) void FilmViewer::calculate_sizes () { - if (!_film || !_player) { + if (!_film) { return; } @@ -570,11 +575,11 @@ FilmViewer::jump_to_selected_clicked () void FilmViewer::seek (DCPTime t, bool accurate) { - if (!_player) { + if (!_butler) { return; } - _player->seek (t, accurate); + _butler->seek (t, accurate); _last_seek_accurate = accurate; get (); } diff --git a/src/wx/film_viewer.h b/src/wx/film_viewer.h index 9fc21fc9c..d50cc562d 100644 --- a/src/wx/film_viewer.h +++ b/src/wx/film_viewer.h @@ -22,8 +22,8 @@ * @brief A wx widget to view `thumbnails' of a Film. */ -#include #include "lib/film.h" +#include class wxToggleButton; class FFmpegPlayer; @@ -31,6 +31,7 @@ class Image; class RGBPlusAlphaImage; class PlayerVideo; class Player; +class Butler; /** @class FilmViewer * @brief A wx widget to view a preview of a Film. @@ -68,7 +69,6 @@ private: void player_changed (bool); void update_position_label (); void update_position_slider (); - void video (boost::shared_ptr, DCPTime time); void get (); void seek (DCPTime t, bool accurate); void refresh_panel (); @@ -79,6 +79,7 @@ private: void frame_number_clicked (); void go_to (DCPTime t); void jump_to_selected_clicked (); + void recreate_butler (); boost::shared_ptr _film; boost::shared_ptr _player; @@ -113,4 +114,6 @@ private: * can get the same one that we got last time. */ bool _last_seek_accurate; + + boost::shared_ptr _butler; }; -- 2.30.2