Cleanup: white space.
[dcpomatic.git] / src / lib / j2k_encoder.cc
index e2fca09b11112e37b80a1e0cdfb4fa3bf4f4db20..552135469131a0a38fe273b6ce571bde29591d4c 100644 (file)
@@ -61,9 +61,9 @@ static grk_plugin::GrokInitializer grokInitializer;
 J2KEncoder::J2KEncoder(shared_ptr<const Film> film, Writer& writer)
        : _film (film)
        , _history (200)
-       , _writer (writer) ,
-       dcpomaticContext_(film,writer,_history, Config::instance()->gpu_binary_location ()),
-       context_(Config::instance()->enable_gpu () ? new grk_plugin::GrokContext(dcpomaticContext_) : nullptr)
+       , _writer (writer)
+       , _dcpomatic_context(film, writer, _history, Config::instance()->gpu_binary_location())
+       , _context(Config::instance()->enable_gpu() ? new grk_plugin::GrokContext(_dcpomatic_context) : nullptr)
 {
 }
 
@@ -73,11 +73,11 @@ J2KEncoder::~J2KEncoder ()
        _server_found_connection.disconnect();
 
        {
-       boost::mutex::scoped_lock lm (_threads_mutex);
-       terminate_threads ();
+               boost::mutex::scoped_lock lm(_threads_mutex);
+               terminate_threads();
        }
 
-       delete context_;
+       delete _context;
 }
 
 void
@@ -89,34 +89,52 @@ J2KEncoder::begin ()
        servers_list_changed ();
 }
 
-void J2KEncoder::pause(void){
-       if (Config::instance()->enable_gpu ())
-               end(false);
+
+void
+J2KEncoder::pause()
+{
+       if (!Config::instance()->enable_gpu()) {
+               return;
+       }
+
+       {
+               boost::mutex::scoped_lock lm (_threads_mutex);
+               terminate_threads ();
+       }
+
+       /* Something might have been thrown during terminate_threads */
+       rethrow ();
+
+       delete _context;
+       _context = nullptr;
 }
 
-void J2KEncoder::resume(void){
-       if (Config::instance()->enable_gpu ()) {
-               context_ = new grk_plugin::GrokContext(dcpomaticContext_);
-               servers_list_changed ();
+
+void J2KEncoder::resume()
+{
+       if (!Config::instance()->enable_gpu()) {
+               return;
        }
+
+       _context = new grk_plugin::GrokContext(_dcpomatic_context);
+       servers_list_changed();
 }
 
+
 void
-J2KEncoder::end (bool isFinal)
+J2KEncoder::end()
 {
-       if (isFinal) {
-               boost::mutex::scoped_lock lock (_queue_mutex);
+       boost::mutex::scoped_lock lock (_queue_mutex);
 
-               LOG_GENERAL (N_("Clearing queue of %1"), _queue.size ());
+       LOG_GENERAL (N_("Clearing queue of %1"), _queue.size ());
 
-               /* Keep waking workers until the queue is empty */
-                       while (!_queue.empty ()) {
-                               rethrow ();
-                               _empty_condition.notify_all ();
-                               _full_condition.wait (lock);
-                       }
-               lock.unlock ();
+       /* Keep waking workers until the queue is empty */
+       while (!_queue.empty ()) {
+               rethrow ();
+               _empty_condition.notify_all ();
+               _full_condition.wait (lock);
        }
+       lock.unlock ();
 
        LOG_GENERAL_NC (N_("Terminating encoder threads"));
 
@@ -131,38 +149,37 @@ J2KEncoder::end (bool isFinal)
        LOG_GENERAL (N_("Mopping up %1"), _queue.size());
 
        /* The following sequence of events can occur in the above code:
-                1. a remote worker takes the last image off the queue
-                2. the loop above terminates
-                3. the remote worker fails to encode the image and puts it back on the queue
-                4. the remote worker is then terminated by terminate_threads
+            1. a remote worker takes the last image off the queue
+            2. the loop above terminates
+            3. the remote worker fails to encode the image and puts it back on the queue
+            4. the remote worker is then terminated by terminate_threads
 
-                So just mop up anything left in the queue here.
+            So just mop up anything left in the queue here.
        */
-       if (isFinal) {
-               for (auto & i: _queue) {
-                       if (Config::instance()->enable_gpu ()) {
-                               if (!context_->scheduleCompress(i)){
-                                       LOG_GENERAL (N_("[%1] J2KEncoder thread pushes frame %2 back onto queue after failure"), thread_id(), i.index());
-                                       // handle error
-                               }
+       for (auto & i: _queue) {
+               if (Config::instance()->enable_gpu ()) {
+                       if (!_context->scheduleCompress(i)){
+                               LOG_GENERAL (N_("[%1] J2KEncoder thread pushes frame %2 back onto queue after failure"), thread_id(), i.index());
+                               // handle error
                        }
-                       else {
-                               LOG_GENERAL(N_("Encode left-over frame %1"), i.index());
-                               try {
-                                       _writer.write(
-                                                       make_shared<dcp::ArrayData>(i.encode_locally()),
-                                               i.index(),
-                                               i.eyes()
-                                               );
-                                       frame_done ();
-                               } catch (std::exception& e) {
-                                       LOG_ERROR (N_("Local encode failed (%1)"), e.what ());
-                               }
+               }
+               else {
+                       LOG_GENERAL(N_("Encode left-over frame %1"), i.index());
+                       try {
+                               _writer.write(
+                                               make_shared<dcp::ArrayData>(i.encode_locally()),
+                                       i.index(),
+                                       i.eyes()
+                                       );
+                               frame_done ();
+                       } catch (std::exception& e) {
+                               LOG_ERROR (N_("Local encode failed (%1)"), e.what ());
                        }
                }
        }
-       delete context_;
-       context_ = nullptr;
+
+       delete _context;
+       _context = nullptr;
 }
 
 
@@ -367,12 +384,11 @@ try
                                }
 
                        } else {
-                               if (context_) {
-                                       if (!context_->launch(vf, config->selected_gpu()) || !context_->scheduleCompress(vf)) {
+                               if (_context) {
+                                       if (!_context->launch(vf, config->selected_gpu()) || !_context->scheduleCompress(vf)) {
                                                LOG_GENERAL (N_("[%1] J2KEncoder thread pushes frame %2 back onto queue after failure"), thread_id(), vf.index());
                                                _queue.push_front (vf);
                                        }
-
                                } else {
                                        try {
                                                LOG_TIMING ("start-local-encode thread=%1 frame=%2", thread_id(), vf.index());