Merge master.
[dcpomatic.git] / src / lib / encoder.cc
index 5dc9e47c772cb7ec0e7c13a91b332c54bce63307..8caa0190c38726121d182b1f81e27ebf99b85295 100644 (file)
 #include "film.h"
 #include "log.h"
 #include "config.h"
-#include "dcp_video_frame.h"
+#include "dcp_video.h"
 #include "server.h"
 #include "cross.h"
 #include "writer.h"
 #include "server_finder.h"
 #include "player.h"
-#include "player_video_frame.h"
+#include "player_video.h"
 
 #include "i18n.h"
 
@@ -45,7 +45,6 @@
 
 using std::pair;
 using std::string;
-using std::stringstream;
 using std::vector;
 using std::list;
 using std::cout;
@@ -59,11 +58,12 @@ using boost::scoped_array;
 int const Encoder::_history_size = 25;
 
 /** @param f Film that we are encoding */
-Encoder::Encoder (shared_ptr<const Film> f, weak_ptr<Job> j)
+Encoder::Encoder (shared_ptr<const Film> f, weak_ptr<Job> j, shared_ptr<Writer> writer)
        : _film (f)
        , _job (j)
        , _video_frames_out (0)
        , _terminate (false)
+       , _writer (writer)
 {
 
 }
@@ -87,18 +87,17 @@ Encoder::add_worker_threads (ServerDescription d)
 }
 
 void
-Encoder::process_begin ()
+Encoder::begin ()
 {
        for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
                _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<ServerDescription> ())));
        }
 
-       _writer.reset (new Writer (_film, _job));
        ServerFinder::instance()->connect (boost::bind (&Encoder::server_found, this, _1));
 }
 
 void
-Encoder::process_end ()
+Encoder::end ()
 {
        boost::mutex::scoped_lock lock (_mutex);
 
@@ -106,8 +105,8 @@ Encoder::process_end ()
 
        /* Keep waking workers until the queue is empty */
        while (!_queue.empty ()) {
-               _condition.notify_all ();
-               _condition.wait (lock);
+               _empty_condition.notify_all ();
+               _full_condition.wait (lock);
        }
 
        lock.unlock ();
@@ -125,7 +124,7 @@ Encoder::process_end ()
             So just mop up anything left in the queue here.
        */
 
-       for (list<shared_ptr<DCPVideoFrame> >::iterator i = _queue.begin(); i != _queue.end(); ++i) {
+       for (list<shared_ptr<DCPVideo> >::iterator i = _queue.begin(); i != _queue.end(); ++i) {
                LOG_GENERAL (N_("Encode left-over frame %1"), (*i)->index ());
                try {
                        _writer->write ((*i)->encode_locally(), (*i)->index (), (*i)->eyes ());
@@ -134,9 +133,6 @@ Encoder::process_end ()
                        LOG_ERROR (N_("Local encode failed (%1)"), e.what ());
                }
        }
-               
-       _writer->finish ();
-       _writer.reset ();
 }      
 
 /** @return an estimate of the current number of frames we are encoding per second,
@@ -181,7 +177,7 @@ Encoder::frame_done ()
 }
 
 void
-Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf)
+Encoder::enqueue (shared_ptr<PlayerVideo> pvf)
 {
        _waker.nudge ();
        
@@ -192,7 +188,7 @@ Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf)
        /* Wait until the queue has gone down a bit */
        while (_queue.size() >= _threads.size() * 2 && !_terminate) {
                LOG_TIMING ("decoder sleeps with queue of %1", _queue.size());
-               _condition.wait (lock);
+               _full_condition.wait (lock);
                LOG_TIMING ("decoder wakes with queue of %1", _queue.size());
        }
 
@@ -210,21 +206,27 @@ Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf)
        if (_writer->can_fake_write (_video_frames_out)) {
                _writer->fake_write (_video_frames_out, pvf->eyes ());
                frame_done ();
+       } else if (pvf->has_j2k ()) {
+               _writer->write (pvf->j2k(), _video_frames_out, pvf->eyes ());
        } else {
                /* Queue this new frame for encoding */
                LOG_TIMING ("adding to queue of %1", _queue.size ());
-               _queue.push_back (shared_ptr<DCPVideoFrame> (
-                                         new DCPVideoFrame (
+               _queue.push_back (shared_ptr<DCPVideo> (
+                                         new DCPVideo (
                                                  pvf,
                                                  _video_frames_out,
                                                  _film->video_frame_rate(),
                                                  _film->j2k_bandwidth(),
                                                  _film->resolution(),
+                                                 _film->burn_subtitles(),
                                                  _film->log()
                                                  )
                                          ));
-               
-               _condition.notify_all ();
+
+               /* The queue might not be empty any more, so notify anything which is
+                  waiting on that.
+               */
+               _empty_condition.notify_all ();
        }
 
        if (pvf->eyes() != EYES_LEFT) {
@@ -232,19 +234,14 @@ Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf)
        }
 }
 
-void
-Encoder::process_audio (shared_ptr<const AudioBuffers> data)
-{
-       _writer->write (data);
-}
-
 void
 Encoder::terminate_threads ()
 {
        {
                boost::mutex::scoped_lock lock (_mutex);
                _terminate = true;
-               _condition.notify_all ();
+               _full_condition.notify_all ();
+               _empty_condition.notify_all ();
        }
 
        for (list<boost::thread *>::iterator i = _threads.begin(); i != _threads.end(); ++i) {
@@ -267,12 +264,12 @@ try
        */
        int remote_backoff = 0;
        
-       while (1) {
+       while (true) {
 
                LOG_TIMING ("[%1] encoder thread sleeps", boost::this_thread::get_id());
                boost::mutex::scoped_lock lock (_mutex);
                while (_queue.empty () && !_terminate) {
-                       _condition.wait (lock);
+                       _empty_condition.wait (lock);
                }
 
                if (_terminate) {
@@ -280,7 +277,7 @@ try
                }
 
                LOG_TIMING ("[%1] encoder thread wakes with queue of %2", boost::this_thread::get_id(), _queue.size());
-               shared_ptr<DCPVideoFrame> vf = _queue.front ();
+               shared_ptr<DCPVideo> vf = _queue.front ();
                LOG_TIMING ("[%1] encoder thread pops frame %2 (%3) from queue", boost::this_thread::get_id(), vf->index(), vf->eyes ());
                _queue.pop_front ();
                
@@ -334,8 +331,9 @@ try
                        dcpomatic_sleep (remote_backoff);
                }
 
+               /* The queue might not be full any more, so notify anything that is waiting on that */
                lock.lock ();
-               _condition.notify_all ();
+               _full_condition.notify_all ();
        }
 }
 catch (...)