+void
+Encoder::process_begin ()
+{
+ if (_film->audio_stream() && _film->audio_stream()->sample_rate() != _film->target_audio_sample_rate()) {
+#ifdef HAVE_SWRESAMPLE
+
+ stringstream s;
+ s << "Will resample audio from " << _film->audio_stream()->sample_rate() << " to " << _film->target_audio_sample_rate();
+ _film->log()->log (s.str ());
+
+ /* We will be using planar float data when we call the resampler */
+ _swr_context = swr_alloc_set_opts (
+ 0,
+ _film->audio_stream()->channel_layout(),
+ AV_SAMPLE_FMT_FLTP,
+ _film->target_audio_sample_rate(),
+ _film->audio_stream()->channel_layout(),
+ AV_SAMPLE_FMT_FLTP,
+ _film->audio_stream()->sample_rate(),
+ 0, 0
+ );
+
+ swr_init (_swr_context);
+#else
+ throw EncodeError ("Cannot resample audio as libswresample is not present");
+#endif
+ } else {
+#ifdef HAVE_SWRESAMPLE
+ _swr_context = 0;
+#endif
+ }
+
+ for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
+ _worker_threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, (ServerDescription *) 0)));
+ }
+
+ vector<ServerDescription*> servers = Config::instance()->servers ();
+
+ for (vector<ServerDescription*>::iterator i = servers.begin(); i != servers.end(); ++i) {
+ for (int j = 0; j < (*i)->threads (); ++j) {
+ _worker_threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, *i)));
+ }
+ }
+}
+
+
+void
+Encoder::process_end ()
+{
+#if HAVE_SWRESAMPLE
+ if (_film->audio_stream() && _swr_context) {
+
+ shared_ptr<AudioBuffers> out (new AudioBuffers (_film->audio_stream()->channels(), 256));
+
+ while (1) {
+ int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0);
+
+ if (frames < 0) {
+ throw EncodeError ("could not run sample-rate converter");
+ }
+
+ if (frames == 0) {
+ break;
+ }
+
+ out->set_frames (frames);
+ write_audio (out);
+ }
+
+ swr_free (&_swr_context);
+ }
+#endif
+
+ if (_film->audio_stream()) {
+ close_sound_files ();
+
+ /* Rename .wav.tmp files to .wav */
+ for (int i = 0; i < dcp_audio_channels (_film->audio_channels()); ++i) {
+ if (boost::filesystem::exists (_opt->multichannel_audio_out_path (i, false))) {
+ boost::filesystem::remove (_opt->multichannel_audio_out_path (i, false));
+ }
+ boost::filesystem::rename (_opt->multichannel_audio_out_path (i, true), _opt->multichannel_audio_out_path (i, false));
+ }
+ }
+
+ boost::mutex::scoped_lock lock (_worker_mutex);
+
+ _film->log()->log ("Clearing queue of " + lexical_cast<string> (_queue.size ()));
+
+ /* Keep waking workers until the queue is empty */
+ while (!_queue.empty ()) {
+ _film->log()->log ("Waking with " + lexical_cast<string> (_queue.size ()), Log::VERBOSE);
+ _worker_condition.notify_all ();
+ _worker_condition.wait (lock);
+ }
+
+ lock.unlock ();
+
+ terminate_worker_threads ();
+
+ _film->log()->log ("Mopping up " + lexical_cast<string> (_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_worker_threads
+
+ So just mop up anything left in the queue here.
+ */
+
+ for (list<shared_ptr<DCPVideoFrame> >::iterator i = _queue.begin(); i != _queue.end(); ++i) {
+ _film->log()->log (String::compose ("Encode left-over frame %1", (*i)->frame ()));
+ try {
+ shared_ptr<EncodedData> e = (*i)->encode_locally ();
+ e->write (_opt, (*i)->frame ());
+ frame_done ();
+ } catch (std::exception& e) {
+ _film->log()->log (String::compose ("Local encode failed (%1)", e.what ()));
+ }
+ }
+}