Merge branch 'staging'
[dcpomatic.git] / src / lib / encoder.cc
index 7b338407eae7765c3bd7aac7bb5fd519b45675a7..0ac32d3bfa9cd2eb099360080d9ac6e4657d715d 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, static_cast<int64_t> (24000));
+                       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,7 +246,7 @@ 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)
 {
        FrameRateConversion frc (_film->source_frame_rate(), _film->dcp_frame_rate());
        
@@ -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
@@ -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) {