Put in silence where there was none.
[dcpomatic.git] / src / lib / encoder.cc
index 3cc643cd6cf3fca7855611cff6a35f074bef72cd..ebe72b6cd8839073e3b934cbcde3141e74a12f64 100644 (file)
@@ -47,6 +47,7 @@ using std::stringstream;
 using std::vector;
 using std::list;
 using std::cout;
+using std::min;
 using std::make_pair;
 using namespace boost;
 
@@ -149,6 +150,20 @@ Encoder::process_end ()
        }
 #endif
 
+       if (_film->audio_channels() == 0 && _film->minimum_audio_channels() > 0) {
+               /* Put audio in where there is none at all */
+               int64_t af = video_frames_to_audio_frames (_video_frames_out, 48000, _film->dcp_frame_rate ());
+               while (af) {
+                       int64_t const this_time = min (af, 24000L);
+                       shared_ptr<AudioBuffers> out (new AudioBuffers (_film->minimum_audio_channels(), this_time));
+                       out->make_silent ();
+                       out->set_frames (this_time);
+                       write_audio (out);
+
+                       af -= this_time;
+               }
+       }
+
        boost::mutex::scoped_lock lock (_mutex);
 
        _film->log()->log (String::compose (N_("Clearing queue of %1"), _queue.size ()));
@@ -231,11 +246,11 @@ Encoder::frame_done ()
 }
 
 void
-Encoder::process_video (shared_ptr<Image> image, bool same, boost::shared_ptr<Subtitle> sub)
+Encoder::process_video (shared_ptr<const Image> image, bool same, boost::shared_ptr<Subtitle> sub)
 {
-       DCPFrameRate dfr (_film->frames_per_second ());
+       FrameRateConversion frc (_film->source_frame_rate(), _film->dcp_frame_rate());
        
-       if (dfr.skip && (_video_frames_in % 2)) {
+       if (frc.skip && (_video_frames_in % 2)) {
                ++_video_frames_in;
                return;
        }
@@ -244,9 +259,9 @@ Encoder::process_video (shared_ptr<Image> image, bool same, boost::shared_ptr<Su
 
        /* Wait until the queue has gone down a bit */
        while (_queue.size() >= _threads.size() * 2 && !_terminate) {
-               TIMING (_("decoder sleeps with queue of %1"), _queue.size());
+               TIMING ("decoder sleeps with queue of %1", _queue.size());
                _condition.wait (lock);
-               TIMING (_("decoder wakes with queue of %1"), _queue.size());
+               TIMING ("decoder wakes with queue of %1", _queue.size());
        }
 
        if (_terminate) {
@@ -268,12 +283,12 @@ Encoder::process_video (shared_ptr<Image> image, bool same, boost::shared_ptr<Su
        } else {
                /* Queue this new frame for encoding */
                pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
-               TIMING (_("adding to queue of %1"), _queue.size ());
+               TIMING ("adding to queue of %1", _queue.size ());
                _queue.push_back (boost::shared_ptr<DCPVideoFrame> (
                                          new DCPVideoFrame (
                                                  image, sub, _film->format()->dcp_size(), _film->format()->dcp_padding (_film),
                                                  _film->subtitle_offset(), _film->subtitle_scale(),
-                                                 _film->scaler(), _video_frames_out, _film->frames_per_second(), s.second,
+                                                 _film->scaler(), _video_frames_out, _film->dcp_frame_rate(), s.second,
                                                  _film->colour_lut(), _film->j2k_bandwidth(),
                                                  _film->log()
                                                  )
@@ -286,7 +301,7 @@ Encoder::process_video (shared_ptr<Image> image, bool same, boost::shared_ptr<Su
        ++_video_frames_in;
        ++_video_frames_out;
 
-       if (dfr.repeat) {
+       if (frc.repeat) {
                _writer->repeat (_video_frames_out);
                ++_video_frames_out;
                frame_done ();
@@ -294,8 +309,12 @@ Encoder::process_video (shared_ptr<Image> image, bool same, boost::shared_ptr<Su
 }
 
 void
-Encoder::process_audio (shared_ptr<AudioBuffers> data)
+Encoder::process_audio (shared_ptr<const AudioBuffers> data)
 {
+       if (!data->frames ()) {
+               return;
+       }
+       
 #if HAVE_SWRESAMPLE
        /* Maybe sample-rate convert */
        if (_swr_context) {
@@ -333,9 +352,13 @@ Encoder::terminate_threads ()
        lock.unlock ();
 
        for (list<boost::thread *>::iterator i = _threads.begin(); i != _threads.end(); ++i) {
-               (*i)->join ();
+               if ((*i)->joinable ()) {
+                       (*i)->join ();
+               }
                delete *i;
        }
+
+       _threads.clear ();
 }
 
 void
@@ -349,7 +372,7 @@ Encoder::encoder_thread (ServerDescription* server)
        
        while (1) {
 
-               TIMING (N_("encoder thread %1 sleeps"), boost::this_thread::get_id());
+               TIMING ("encoder thread %1 sleeps", boost::this_thread::get_id());
                boost::mutex::scoped_lock lock (_mutex);
                while (_queue.empty () && !_terminate) {
                        _condition.wait (lock);
@@ -359,7 +382,7 @@ Encoder::encoder_thread (ServerDescription* server)
                        return;
                }
 
-               TIMING (N_("encoder thread %1 wakes with queue of %2"), boost::this_thread::get_id(), _queue.size());
+               TIMING ("encoder thread %1 wakes with queue of %2", boost::this_thread::get_id(), _queue.size());
                boost::shared_ptr<DCPVideoFrame> vf = _queue.front ();
                _film->log()->log (String::compose (N_("Encoder thread %1 pops frame %2 from queue"), boost::this_thread::get_id(), vf->frame()), Log::VERBOSE);
                _queue.pop_front ();
@@ -393,9 +416,9 @@ Encoder::encoder_thread (ServerDescription* server)
                                
                } else {
                        try {
-                               TIMING (N_("encoder thread %1 begins local encode of %2"), boost::this_thread::get_id(), vf->frame());
+                               TIMING ("encoder thread %1 begins local encode of %2", boost::this_thread::get_id(), vf->frame());
                                encoded = vf->encode_locally ();
-                               TIMING (N_("encoder thread %1 finishes local encode of %2"), boost::this_thread::get_id(), vf->frame());
+                               TIMING ("encoder thread %1 finishes local encode of %2", boost::this_thread::get_id(), vf->frame());
                        } catch (std::exception& e) {
                                _film->log()->log (String::compose (N_("Local encode failed (%1)"), e.what ()));
                        }
@@ -425,10 +448,10 @@ Encoder::encoder_thread (ServerDescription* server)
 void
 Encoder::write_audio (shared_ptr<const AudioBuffers> data)
 {
-       AudioMapping m (_film->audio_channels ());
+       AudioMapping m (_film);
        if (m.dcp_channels() != _film->audio_channels()) {
 
-               /* Remap (currently just for mono -> 5.1) */
+               /* Remap and pad with silence */
 
                shared_ptr<AudioBuffers> b (new AudioBuffers (m.dcp_channels(), data->frames ()));
                for (int i = 0; i < m.dcp_channels(); ++i) {