#include "format.h"
#include "filter.h"
#include "ab_transcoder.h"
-#include "encoder_factory.h"
#include "config.h"
+#include "encoder.h"
using std::string;
using boost::shared_ptr;
{
try {
/* _film_b is the one with reference filters */
- ABTranscoder w (_film_b, _film, _decode_opt, this, encoder_factory (_film, _encode_opt));
+ ABTranscoder w (_film_b, _film, _decode_opt, this, shared_ptr<Encoder> (new Encoder (_film, _encode_opt)));
w.go ();
set_progress (1);
set_state (FINISHED_OK);
_db.video->set_subtitle_stream (_film_a->subtitle_stream ());
_da.audio->set_audio_stream (_film_a->audio_stream ());
- _da.video->Video.connect (bind (&Combiner::process_video, _combiner, _1, _2));
- _db.video->Video.connect (bind (&Combiner::process_video_b, _combiner, _1, _2));
+ _da.video->Video.connect (bind (&Combiner::process_video, _combiner, _1, _2, _3));
+ _db.video->Video.connect (bind (&Combiner::process_video_b, _combiner, _1, _2, _3));
if (_matcher) {
_combiner->connect_video (_matcher);
* @param sub Subtitle (which will be ignored)
*/
void
-Combiner::process_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
+Combiner::process_video (shared_ptr<Image> image, bool, shared_ptr<Subtitle> sub)
{
_image = image;
}
* @param sub Subtitle (which will be put onto the whole frame)
*/
void
-Combiner::process_video_b (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
+Combiner::process_video_b (shared_ptr<Image> image, bool, shared_ptr<Subtitle> sub)
{
/* Copy the right half of this image into our _image */
/* XXX: this should probably be in the Image class */
}
}
- Video (_image, sub);
+ Video (_image, false, sub);
_image.reset ();
}
public:
Combiner (Log* log);
- void process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s);
- void process_video_b (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s);
+ void process_video (boost::shared_ptr<Image> i, bool, boost::shared_ptr<Subtitle> s);
+ void process_video_b (boost::shared_ptr<Image> i, bool, boost::shared_ptr<Subtitle> s);
private:
/** The image that we are currently working on */
*/
#include <boost/filesystem.hpp>
+#include <boost/lexical_cast.hpp>
#include "encoder.h"
#include "util.h"
#include "options.h"
#include "film.h"
#include "log.h"
#include "exceptions.h"
+#include "filter.h"
+#include "config.h"
+#include "dcp_video_frame.h"
+#include "server.h"
+#include "cross.h"
using std::pair;
+using std::string;
using std::stringstream;
using std::vector;
+using std::list;
+using std::cout;
using namespace boost;
int const Encoder::_history_size = 25;
, _swr_context (0)
#endif
, _audio_frames_written (0)
+ , _process_end (false)
{
if (_film->audio_stream()) {
/* Create sound output files with .tmp suffixes; we will rename
Encoder::~Encoder ()
{
close_sound_files ();
+ terminate_worker_threads ();
}
void
_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)));
+ }
+ }
}
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 ()));
+ }
+ }
}
/** @return an estimate of the current number of frames we are encoding per second,
}
void
-Encoder::process_video (shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
+Encoder::process_video (shared_ptr<Image> image, bool same, boost::shared_ptr<Subtitle> sub)
{
if (_opt->video_skip != 0 && (_video_frame % _opt->video_skip) != 0) {
++_video_frame;
}
}
- do_process_video (i, s);
+ boost::mutex::scoped_lock lock (_worker_mutex);
+
+ /* Wait until the queue has gone down a bit */
+ while (_queue.size() >= _worker_threads.size() * 2 && !_process_end) {
+ TIMING ("decoder sleeps with queue of %1", _queue.size());
+ _worker_condition.wait (lock);
+ TIMING ("decoder wakes with queue of %1", _queue.size());
+ }
+
+ if (_process_end) {
+ return;
+ }
+
+ /* Only do the processing if we don't already have a file for this frame */
+ if (boost::filesystem::exists (_opt->frame_out_path (_video_frame, false))) {
+ frame_skipped ();
+ return;
+ }
+
+ if (same) {
+ /* Use the last frame that we encoded */
+ assert (_last_real_frame);
+ link (_opt->frame_out_path (_last_real_frame.get(), false), _opt->frame_out_path (_video_frame, false));
+ link (_opt->hash_out_path (_last_real_frame.get(), false), _opt->hash_out_path (_video_frame, false));
+ } 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 ());
+ _queue.push_back (boost::shared_ptr<DCPVideoFrame> (
+ new DCPVideoFrame (
+ image, sub, _opt->out_size, _opt->padding, _film->subtitle_offset(), _film->subtitle_scale(),
+ _film->scaler(), _video_frame, _film->frames_per_second(), s.second,
+ Config::instance()->colour_lut_index (), Config::instance()->j2k_bandwidth (),
+ _film->log()
+ )
+ ));
+
+ _worker_condition.notify_all ();
+ _last_real_frame = _video_frame;
+ }
+
++_video_frame;
}
Encoder::process_audio (shared_ptr<AudioBuffers> data)
{
if (_opt->audio_range) {
-
shared_ptr<AudioBuffers> trimmed (new AudioBuffers (*data.get ()));
/* Range that we are encoding */
_sound_files.clear ();
}
+void
+Encoder::terminate_worker_threads ()
+{
+ boost::mutex::scoped_lock lock (_worker_mutex);
+ _process_end = true;
+ _worker_condition.notify_all ();
+ lock.unlock ();
+
+ for (list<boost::thread *>::iterator i = _worker_threads.begin(); i != _worker_threads.end(); ++i) {
+ (*i)->join ();
+ delete *i;
+ }
+}
+
+void
+Encoder::encoder_thread (ServerDescription* server)
+{
+ /* Number of seconds that we currently wait between attempts
+ to connect to the server; not relevant for localhost
+ encodings.
+ */
+ int remote_backoff = 0;
+
+ while (1) {
+
+ TIMING ("encoder thread %1 sleeps", boost::this_thread::get_id());
+ boost::mutex::scoped_lock lock (_worker_mutex);
+ while (_queue.empty () && !_process_end) {
+ _worker_condition.wait (lock);
+ }
+
+ if (_process_end) {
+ return;
+ }
+
+ 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 ("Encoder thread %1 pops frame %2 from queue", boost::this_thread::get_id(), vf->frame()), Log::VERBOSE);
+ _queue.pop_front ();
+
+ lock.unlock ();
+
+ shared_ptr<EncodedData> encoded;
+
+ if (server) {
+ try {
+ encoded = vf->encode_remotely (server);
+
+ if (remote_backoff > 0) {
+ _film->log()->log (String::compose ("%1 was lost, but now she is found; removing backoff", server->host_name ()));
+ }
+
+ /* This job succeeded, so remove any backoff */
+ remote_backoff = 0;
+
+ } catch (std::exception& e) {
+ if (remote_backoff < 60) {
+ /* back off more */
+ remote_backoff += 10;
+ }
+ _film->log()->log (
+ String::compose (
+ "Remote encode of %1 on %2 failed (%3); thread sleeping for %4s",
+ vf->frame(), server->host_name(), e.what(), remote_backoff)
+ );
+ }
+
+ } else {
+ try {
+ TIMING ("encoder thread %1 begins local encode of %2", boost::this_thread::get_id(), vf->frame());
+ encoded = vf->encode_locally ();
+ 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 ("Local encode failed (%1)", e.what ()));
+ }
+ }
+
+ if (encoded) {
+ encoded->write (_opt, vf->frame ());
+ frame_done ();
+ } else {
+ lock.lock ();
+ _film->log()->log (
+ String::compose ("Encoder thread %1 pushes frame %2 back onto queue after failure", boost::this_thread::get_id(), vf->frame())
+ );
+ _queue.push_front (vf);
+ lock.unlock ();
+ }
+
+ if (remote_backoff > 0) {
+ dvdomatic_sleep (remote_backoff);
+ }
+
+ lock.lock ();
+ _worker_condition.notify_all ();
+ }
+}
+
+void
+Encoder::link (string a, string b) const
+{
+#ifdef DVDOMATIC_POSIX
+ int const r = symlink (a.c_str(), b.c_str());
+ if (r) {
+ throw EncodeError (String::compose ("could not create symlink from %1 to %2", a, b));
+ }
+#endif
+
+#ifdef DVDOMATIC_WINDOWS
+ boost::filesystem::copy_file (a, b);
+#endif
+}
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
+#include <boost/thread/condition.hpp>
+#include <boost/thread.hpp>
#include <list>
#include <stdint.h>
extern "C" {
class Subtitle;
class AudioBuffers;
class Film;
+class ServerDescription;
+class DCPVideoFrame;
/** @class Encoder
- * @brief Parent class for classes which can encode video and audio frames.
+ * @brief Encoder to J2K and WAV for DCP.
*
* Video is supplied to process_video as YUV frames, and audio
* is supplied as uncompressed PCM in blocks of various sizes.
- *
- * The subclass is expected to encode the video and/or audio in
- * some way and write it to disk.
*/
class Encoder : public VideoSink, public AudioSink
/** Call with a frame of video.
* @param i Video frame image.
+ * @param same true if i is the same as the last time we were called.
* @param s A subtitle that should be on this frame, or 0.
*/
- void process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s);
+ void process_video (boost::shared_ptr<Image> i, bool same, boost::shared_ptr<Subtitle> s);
/** Call with some audio data */
void process_audio (boost::shared_ptr<AudioBuffers>);
SourceFrame video_frame () const;
protected:
-
- /** Called with a frame of video.
- * @param i Video frame image.
- * @param s A subtitle that should be on this frame, or 0.
- */
- virtual void do_process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s) = 0;
void frame_done ();
void frame_skipped ();
void close_sound_files ();
void write_audio (boost::shared_ptr<const AudioBuffers> audio);
+ void encoder_thread (ServerDescription *);
+ void terminate_worker_threads ();
+ void link (std::string, std::string) const;
+
#if HAVE_SWRESAMPLE
SwrContext* _swr_context;
#endif
std::vector<SNDFILE*> _sound_files;
int64_t _audio_frames_written;
+
+ boost::optional<int> _last_real_frame;
+ bool _process_end;
+ std::list<boost::shared_ptr<DCPVideoFrame> > _queue;
+ std::list<boost::thread *> _worker_threads;
+ mutable boost::mutex _worker_mutex;
+ boost::condition _worker_condition;
};
#endif
+++ /dev/null
-/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-/** @file src/encoder_factory.cc
- * @brief A method to create an appropriate encoder for some content.
- */
-
-#include <boost/filesystem.hpp>
-#include "j2k_video_encoder.h"
-#include "j2k_still_encoder.h"
-#include "film.h"
-
-using boost::shared_ptr;
-
-shared_ptr<Encoder>
-encoder_factory (shared_ptr<const Film> f, shared_ptr<const EncodeOptions> o)
-{
- if (!boost::filesystem::is_directory (f->content_path()) && f->content_type() == STILL) {
- return shared_ptr<Encoder> (new J2KStillEncoder (f, o));
- }
-
- return shared_ptr<Encoder> (new J2KVideoEncoder (f, o));
-}
+++ /dev/null
-/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-/** @file src/encoder_factory.h
- * @brief A method to create an appropriate encoder for some content.
- */
-
-class Encoder;
-class EncodeOptions;
-class Job;
-class Log;
-class Film;
-
-extern boost::shared_ptr<Encoder> encoder_factory (boost::shared_ptr<const Film>, boost::shared_ptr<const EncodeOptions>);
: StringError (s)
{}
};
-
-class PlayError : public StringError
-{
-public:
- PlayError (std::string s)
- : StringError (s)
- {}
-};
-
-class DVDError : public StringError
-{
-public:
- DVDError (std::string s)
- : StringError (s)
- {}
-};
list<shared_ptr<Image> > images = graph->process (frame);
- double const st = av_frame_get_best_effort_timestamp(_frame) * av_q2d (_format_context->streams[_video_stream]->time_base);
-
for (list<shared_ptr<Image> >::iterator i = images.begin(); i != images.end(); ++i) {
- emit_video (*i, st);
+ emit_video (*i, frame_time ());
}
}
String::compose (
"Extra video frame inserted at %1s; source frame %2, source PTS %3 (at %4 fps)",
out_pts_seconds, video_frame(), source_pts_seconds, frames_per_second()
- )
+ )
);
}
}
return (double(_format_context->duration) / AV_TIME_BASE) * frames_per_second();
}
+double
+FFmpegDecoder::frame_time () const
+{
+ return av_frame_get_best_effort_timestamp(_frame) * av_q2d (_format_context->streams[_video_stream]->time_base);
+}
+
void out_with_sync ();
void filter_and_emit_video (AVFrame *);
+ double frame_time () const;
void setup_general ();
void setup_video ();
}
_external_audio_stream = ExternalAudioStream::create ();
-
- read_metadata ();
+
+ if (must_exist) {
+ read_metadata ();
+ }
_log = new FileLog (file ("log"));
set_dci_date_today ();
oe->video_range = make_pair (dcp_trim_start(), dcp_trim_start() + dcp_length().get());
if (audio_stream()) {
oe->audio_range = make_pair (
- video_frames_to_audio_frames (oe->video_range.get().first, audio_stream()->sample_rate(), frames_per_second()),
- video_frames_to_audio_frames (oe->video_range.get().second, audio_stream()->sample_rate(), frames_per_second())
+
+ video_frames_to_audio_frames (
+ oe->video_range.get().first,
+ dcp_audio_sample_rate (audio_stream()->sample_rate()),
+ dcp_frame_rate (frames_per_second()).frames_per_second
+ ),
+
+ video_frames_to_audio_frames (
+ oe->video_range.get().second,
+ dcp_audio_sample_rate (audio_stream()->sample_rate()),
+ dcp_frame_rate (frames_per_second()).frames_per_second
+ )
);
}
boost::optional<int> audio_sample_rate;
boost::optional<int> audio_stream_index;
boost::optional<int> subtitle_stream_index;
-
+
ifstream f (file ("metadata").c_str());
+ if (!f.good()) {
+ throw OpenFileError (file("metadata"));
+ }
+
multimap<string, string> kv = read_key_value (f);
/* We need version before anything else */
return rint (t);
}
-boost::optional<SourceFrame>
+boost::optional<int>
Film::dcp_length () const
{
if (content_type() == STILL) {
}
if (!length()) {
- return boost::optional<SourceFrame> ();
+ return boost::optional<int> ();
}
return length().get() - dcp_trim_start() - dcp_trim_end();
_content_audio_stream = shared_ptr<AudioStream> ();
_subtitle_stream = shared_ptr<SubtitleStream> ();
+ /* Start off using content audio */
+ set_use_content_audio (true);
+
/* Create a temporary decoder so that we can get information
about the content.
*/
void read_metadata ();
Size cropped_size (Size) const;
- boost::optional<SourceFrame> dcp_length () const;
+ boost::optional<int> dcp_length () const;
std::string dci_name () const;
std::string dcp_name () const;
std::vector<Filter const *> _filters;
/** Scaler algorithm to use */
Scaler const * _scaler;
- /** Frames to trim off the start of the source */
- SourceFrame _dcp_trim_start;
- /** Frames to trim off the end of the source */
- SourceFrame _dcp_trim_end;
+ /** Frames to trim off the start of the DCP */
+ int _dcp_trim_start;
+ /** Frames to trim off the end of the DCP */
+ int _dcp_trim_end;
/** true to create an A/B comparison DCP, where the left half of the image
is the video without any filters or post-processing, and the right half
has the specified filters and post-processing.
ImageMagickDecoder::pass ()
{
if (_iter == _files.end()) {
- return true;
+ if (!_film->dcp_length() || video_frame() >= _film->dcp_length().get()) {
+ return true;
+ }
+
+ repeat_last_video ();
+ return false;
}
Magick::Image* magick_image = new Magick::Image (_film->content_path ());
+++ /dev/null
-/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-/** @file src/j2k_still_encoder.cc
- * @brief An encoder which writes JPEG2000 files for a single still source image.
- */
-
-#include <sstream>
-#include <stdexcept>
-#include <iomanip>
-#include <iostream>
-#include <boost/filesystem.hpp>
-#include <sndfile.h>
-#include <openjpeg.h>
-#include "j2k_still_encoder.h"
-#include "config.h"
-#include "options.h"
-#include "exceptions.h"
-#include "dcp_video_frame.h"
-#include "filter.h"
-#include "log.h"
-#include "imagemagick_decoder.h"
-#include "film.h"
-
-using std::string;
-using std::pair;
-using boost::shared_ptr;
-
-J2KStillEncoder::J2KStillEncoder (shared_ptr<const Film> f, shared_ptr<const EncodeOptions> o)
- : Encoder (f, o)
-{
-
-}
-
-void
-J2KStillEncoder::do_process_video (shared_ptr<Image> yuv, shared_ptr<Subtitle> sub)
-{
- pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
- DCPVideoFrame* f = new DCPVideoFrame (
- yuv, sub, _opt->out_size, _opt->padding, _film->subtitle_offset(), _film->subtitle_scale(), _film->scaler(), 0, _film->frames_per_second(), s.second,
- Config::instance()->colour_lut_index(), Config::instance()->j2k_bandwidth(),
- _film->log()
- );
-
- if (!boost::filesystem::exists (_opt->frame_out_path (0, false))) {
- boost::shared_ptr<EncodedData> e = f->encode_locally ();
- e->write (_opt, 0);
- }
-
- string const real = _opt->frame_out_path (0, false);
- string const real_hash = _opt->hash_out_path (0, false);
- for (int i = 1; i < (_film->still_duration() * _film->frames_per_second()); ++i) {
-
- if (!boost::filesystem::exists (_opt->frame_out_path (i, false))) {
- link (real, _opt->frame_out_path (i, false));
- }
-
- if (!boost::filesystem::exists (_opt->hash_out_path (i, false))) {
- link (real_hash, _opt->hash_out_path (i, false));
- }
-
- frame_done ();
- }
-}
-
-void
-J2KStillEncoder::link (string a, string b) const
-{
-#ifdef DVDOMATIC_POSIX
- int const r = symlink (a.c_str(), b.c_str());
- if (r) {
- throw EncodeError ("could not create symlink");
- }
-#endif
-
-#ifdef DVDOMATIC_WINDOWS
- boost::filesystem::copy_file (a, b);
-#endif
-}
+++ /dev/null
-/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-/** @file src/j2k_still_encoder.h
- * @brief An encoder which writes JPEG2000 files for a single still source image.
- */
-
-#include <list>
-#include <vector>
-#include "encoder.h"
-
-class Image;
-class Log;
-class EncodeOptions;
-
-/** @class J2KStillEncoder
- * @brief An encoder which writes repeated JPEG2000 files from a single decoded input.
- */
-class J2KStillEncoder : public Encoder
-{
-public:
- J2KStillEncoder (boost::shared_ptr<const Film>, boost::shared_ptr<const EncodeOptions>);
-
-private:
- void do_process_video (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>);
-
- void link (std::string, std::string) const;
-};
+++ /dev/null
-/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-/** @file src/j2k_video_encoder.cc
- * @brief An encoder which writes JPEG2000 files, where they are video (ie not still).
- */
-
-#include <sstream>
-#include <stdexcept>
-#include <iomanip>
-#include <iostream>
-#include <boost/thread.hpp>
-#include <boost/filesystem.hpp>
-#include <boost/lexical_cast.hpp>
-#include <sndfile.h>
-#include <openjpeg.h>
-#include "j2k_video_encoder.h"
-#include "config.h"
-#include "options.h"
-#include "exceptions.h"
-#include "dcp_video_frame.h"
-#include "server.h"
-#include "filter.h"
-#include "log.h"
-#include "cross.h"
-#include "film.h"
-
-using std::string;
-using std::stringstream;
-using std::list;
-using std::vector;
-using std::pair;
-using std::cout;
-using boost::shared_ptr;
-using boost::thread;
-using boost::lexical_cast;
-
-J2KVideoEncoder::J2KVideoEncoder (shared_ptr<const Film> f, shared_ptr<const EncodeOptions> o)
- : Encoder (f, o)
- , _process_end (false)
-{
-
-}
-
-J2KVideoEncoder::~J2KVideoEncoder ()
-{
- terminate_worker_threads ();
-}
-
-void
-J2KVideoEncoder::terminate_worker_threads ()
-{
- boost::mutex::scoped_lock lock (_worker_mutex);
- _process_end = true;
- _worker_condition.notify_all ();
- lock.unlock ();
-
- for (list<boost::thread *>::iterator i = _worker_threads.begin(); i != _worker_threads.end(); ++i) {
- (*i)->join ();
- delete *i;
- }
-}
-
-void
-J2KVideoEncoder::do_process_video (shared_ptr<Image> yuv, shared_ptr<Subtitle> sub)
-{
- boost::mutex::scoped_lock lock (_worker_mutex);
-
- /* Wait until the queue has gone down a bit */
- while (_queue.size() >= _worker_threads.size() * 2 && !_process_end) {
- TIMING ("decoder sleeps with queue of %1", _queue.size());
- _worker_condition.wait (lock);
- TIMING ("decoder wakes with queue of %1", _queue.size());
- }
-
- if (_process_end) {
- return;
- }
-
- /* Only do the processing if we don't already have a file for this frame */
- if (!boost::filesystem::exists (_opt->frame_out_path (_video_frame, false))) {
- pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
- TIMING ("adding to queue of %1", _queue.size ());
- _queue.push_back (boost::shared_ptr<DCPVideoFrame> (
- new DCPVideoFrame (
- yuv, sub, _opt->out_size, _opt->padding, _film->subtitle_offset(), _film->subtitle_scale(),
- _film->scaler(), _video_frame, _film->frames_per_second(), s.second,
- Config::instance()->colour_lut_index (), Config::instance()->j2k_bandwidth (),
- _film->log()
- )
- ));
-
- _worker_condition.notify_all ();
- } else {
- frame_skipped ();
- }
-}
-
-void
-J2KVideoEncoder::encoder_thread (ServerDescription* server)
-{
- /* Number of seconds that we currently wait between attempts
- to connect to the server; not relevant for localhost
- encodings.
- */
- int remote_backoff = 0;
-
- while (1) {
-
- TIMING ("encoder thread %1 sleeps", boost::this_thread::get_id());
- boost::mutex::scoped_lock lock (_worker_mutex);
- while (_queue.empty () && !_process_end) {
- _worker_condition.wait (lock);
- }
-
- if (_process_end) {
- return;
- }
-
- 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 ("Encoder thread %1 pops frame %2 from queue", boost::this_thread::get_id(), vf->frame()), Log::VERBOSE);
- _queue.pop_front ();
-
- lock.unlock ();
-
- shared_ptr<EncodedData> encoded;
-
- if (server) {
- try {
- encoded = vf->encode_remotely (server);
-
- if (remote_backoff > 0) {
- _film->log()->log (String::compose ("%1 was lost, but now she is found; removing backoff", server->host_name ()));
- }
-
- /* This job succeeded, so remove any backoff */
- remote_backoff = 0;
-
- } catch (std::exception& e) {
- if (remote_backoff < 60) {
- /* back off more */
- remote_backoff += 10;
- }
- _film->log()->log (
- String::compose (
- "Remote encode of %1 on %2 failed (%3); thread sleeping for %4s",
- vf->frame(), server->host_name(), e.what(), remote_backoff)
- );
- }
-
- } else {
- try {
- TIMING ("encoder thread %1 begins local encode of %2", boost::this_thread::get_id(), vf->frame());
- encoded = vf->encode_locally ();
- 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 ("Local encode failed (%1)", e.what ()));
- }
- }
-
- if (encoded) {
- encoded->write (_opt, vf->frame ());
- frame_done ();
- } else {
- lock.lock ();
- _film->log()->log (
- String::compose ("Encoder thread %1 pushes frame %2 back onto queue after failure", boost::this_thread::get_id(), vf->frame())
- );
- _queue.push_front (vf);
- lock.unlock ();
- }
-
- if (remote_backoff > 0) {
- dvdomatic_sleep (remote_backoff);
- }
-
- lock.lock ();
- _worker_condition.notify_all ();
- }
-}
-
-void
-J2KVideoEncoder::process_begin ()
-{
- Encoder::process_begin ();
-
- for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
- _worker_threads.push_back (new boost::thread (boost::bind (&J2KVideoEncoder::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 (&J2KVideoEncoder::encoder_thread, this, *i)));
- }
- }
-}
-
-void
-J2KVideoEncoder::process_end ()
-{
- Encoder::process_end ();
-
- 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 ()));
- }
- }
-}
-
+++ /dev/null
-/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
-
-/** @file src/j2k_video_encoder.h
- * @brief An encoder which writes JPEG2000 files, where they are video (ie not still).
- */
-
-#include <list>
-#include <vector>
-#include <boost/thread/condition.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/thread.hpp>
-#include "encoder.h"
-
-class ServerDescription;
-class DCPVideoFrame;
-class Image;
-class Log;
-class Subtitle;
-
-/** @class J2KVideoEncoder
- * @brief An encoder which writes JPEG2000 files, where they are video (ie not still).
- */
-class J2KVideoEncoder : public Encoder
-{
-public:
- J2KVideoEncoder (boost::shared_ptr<const Film>, boost::shared_ptr<const EncodeOptions>);
- ~J2KVideoEncoder ();
-
- void process_begin ();
- void process_end ();
-
-private:
-
- void do_process_video (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>);
-
- void encoder_thread (ServerDescription *);
- void terminate_worker_threads ();
-
- bool _process_end;
- std::list<boost::shared_ptr<DCPVideoFrame> > _queue;
- std::list<boost::thread *> _worker_threads;
- mutable boost::mutex _worker_mutex;
- boost::condition _worker_condition;
-};
}
void
-Matcher::process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
+Matcher::process_video (boost::shared_ptr<Image> i, bool same, boost::shared_ptr<Subtitle> s)
{
- Video (i, s);
+ Video (i, same, s);
_video_frames++;
_pixel_format = i->pixel_format ();
shared_ptr<Image> black (new SimpleImage (_pixel_format.get(), _size.get(), false));
black->make_black ();
for (int i = 0; i < black_video_frames; ++i) {
- Video (black, shared_ptr<Subtitle>());
+ Video (black, i != 0, shared_ptr<Subtitle>());
}
/* Now recompute our check value */
{
public:
Matcher (Log* log, int sample_rate, float frames_per_second);
- void process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s);
+ void process_video (boost::shared_ptr<Image> i, bool, boost::shared_ptr<Subtitle> s);
void process_audio (boost::shared_ptr<AudioBuffers>);
void process_end ();
Size out_size; ///< size of output images
int padding; ///< number of pixels of padding (in terms of the output size) each side of the image
- /** Range of video frames to decode */
- boost::optional<std::pair<SourceFrame, SourceFrame> > video_range;
- /** Range of audio frames to decode */
+ /** Range of video frames to encode (in DCP frames) */
+ boost::optional<std::pair<int, int> > video_range;
+ /** Range of audio frames to decode (in the DCP's sampling rate) */
boost::optional<std::pair<int64_t, int64_t> > audio_range;
/** Skip frames such that we don't decode any frame where (index % decode_video_skip) != 0; e.g.
#include "format.h"
#include "transcoder.h"
#include "log.h"
-#include "encoder_factory.h"
#include "encoder.h"
using std::string;
_film->log()->log ("Transcode job starting");
_film->log()->log (String::compose ("Audio delay is %1ms", _film->audio_delay()));
- _encoder = encoder_factory (_film, _encode_opt);
+ _encoder.reset (new Encoder (_film, _encode_opt));
Transcoder w (_film, _decode_opt, this, _encoder);
w.go ();
set_progress (1);
sub = _timed_subtitle->subtitle ();
}
- signal_video (image, sub);
+ signal_video (image, false, sub);
_last_source_time = t;
}
_last_image->make_black ();
}
- signal_video (_last_image, _last_subtitle);
+ signal_video (_last_image, true, _last_subtitle);
}
void
-VideoDecoder::signal_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
+VideoDecoder::signal_video (shared_ptr<Image> image, bool same, shared_ptr<Subtitle> sub)
{
TIMING ("Decoder emits %1", _video_frame);
- Video (image, sub);
+ Video (image, same, sub);
++_video_frame;
_last_image = image;
std::vector<boost::shared_ptr<SubtitleStream> > _subtitle_streams;
private:
- void signal_video (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>);
+ void signal_video (boost::shared_ptr<Image>, bool, boost::shared_ptr<Subtitle>);
int _video_frame;
double _last_source_time;
public:
/** Call with a frame of video.
* @param i Video frame image.
+ * @param same true if i is the same as last time we were called.
* @param s A subtitle that should be on this frame, or 0.
*/
- virtual void process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s) = 0;
+ virtual void process_video (boost::shared_ptr<Image> i, bool same, boost::shared_ptr<Subtitle> s) = 0;
};
#endif
void
VideoSource::connect_video (shared_ptr<VideoSink> s)
{
- Video.connect (bind (&VideoSink::process_video, s, _1, _2));
+ Video.connect (bind (&VideoSink::process_video, s, _1, _2, _3));
}
/** Emitted when a video frame is ready.
* First parameter is the video image.
- * Second parameter is either 0 or a subtitle that should be on this frame.
+ * Second parameter is true if the image is the same as the last one that was emitted.
+ * Third parameter is either 0 or a subtitle that should be on this frame.
*/
- boost::signals2::signal<void (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>)> Video;
+ boost::signals2::signal<void (boost::shared_ptr<Image>, bool, boost::shared_ptr<Subtitle>)> Video;
void connect_video (boost::shared_ptr<VideoSink>);
};
delay_line.cc
dolby_cp750.cc
encoder.cc
- encoder_factory.cc
examine_content_job.cc
external_audio_decoder.cc
filter_graph.cc
gain.cc
image.cc
imagemagick_decoder.cc
- j2k_still_encoder.cc
- j2k_video_encoder.cc
job.cc
job_manager.cc
log.cc
if (r == wxID_OK) {
maybe_save_then_delete_film ();
- film.reset (new Film (wx_to_std (c->GetPath ())));
- film->log()->set_level (log_level);
- set_film ();
+ try {
+ film.reset (new Film (wx_to_std (c->GetPath ())));
+ film->log()->set_level (log_level);
+ set_film ();
+ } catch (std::exception& e) {
+ error_dialog (this, String::compose ("Could not open film at %1 (%2)", wx_to_std (c->GetPath()), e.what()));
+ }
}
c->Destroy ();
static Log log_ ("servomatictest.log");
void
-process_video (shared_ptr<Image> image, int frame)
+process_video (shared_ptr<Image> image, bool, int frame)
{
shared_ptr<DCPVideoFrame> local (new DCPVideoFrame (image, Size (1024, 1024), 0, Scaler::from_id ("bicubic"), frame, 24, "", 0, 250000000, &log_));
shared_ptr<DCPVideoFrame> remote (new DCPVideoFrame (image, Size (1024, 1024), 0, Scaler::from_id ("bicubic"), frame, 24, "", 0, 250000000, &log_));
, _film (f)
, _generally_sensitive (true)
{
- wxSizer* s = new wxBoxSizer (wxVERTICAL);
+ wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
SetSizer (s);
_notebook = new wxNotebook (this, wxID_ANY);
s->Add (_notebook, 1);
film_changed (Film::USE_CONTENT_AUDIO);
film_changed (Film::AUDIO_GAIN);
film_changed (Film::AUDIO_DELAY);
+ film_changed (Film::STILL_DURATION);
film_changed (Film::WITH_SUBTITLES);
film_changed (Film::SUBTITLE_OFFSET);
film_changed (Film::SUBTITLE_SCALE);
}
_film_sizer->Layout ();
+ _film_sizer->SetSizeHints (_film_panel);
+ _video_sizer->Layout ();
+ _video_sizer->SetSizeHints (_video_panel);
+ _audio_sizer->Layout ();
+ _audio_sizer->SetSizeHints (_audio_panel);
+ _subtitle_sizer->Layout ();
+ _subtitle_sizer->SetSizeHints (_subtitle_panel);
+
+ _notebook->Fit ();
}
void
vector<shared_ptr<AudioStream> > a = _film->content_audio_streams ();
for (vector<shared_ptr<AudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
shared_ptr<FFmpegAudioStream> ffa = dynamic_pointer_cast<FFmpegAudioStream> (*i);
+ assert (ffa);
_audio_stream->Append (std_to_wx (ffa->name()), new wxStringClientData (std_to_wx (ffa->to_string ())));
}
, _panel (new wxPanel (this))
, _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
, _play_button (new wxToggleButton (this, wxID_ANY, wxT ("Play")))
+ , _got_frame (false)
, _out_width (0)
, _out_height (0)
, _panel_width (0)
o->decode_subtitles = true;
o->video_sync = false;
_decoders = decoder_factory (_film, o, 0);
- _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2));
+ _decoders.video->Video.connect (bind (&FilmViewer::process_video, this, _1, _2, _3));
_decoders.video->OutputChanged.connect (boost::bind (&FilmViewer::decoder_changed, this));
_decoders.video->set_subtitle_stream (_film->subtitle_stream());
calculate_sizes ();
}
void
-FilmViewer::process_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
+FilmViewer::process_video (shared_ptr<Image> image, bool, shared_ptr<Subtitle> sub)
{
_raw_frame = image;
_raw_sub = sub;
raw_to_display ();
+
+ _got_frame = true;
}
void
_raw_frame.reset ();
try {
- shared_ptr<Image> last = _display_frame;
- while (last == _display_frame) {
+ _got_frame = false;
+ while (!_got_frame) {
if (_decoders.video->pass ()) {
/* We didn't get a frame before the decoder gave up,
so clear our display frame.
void slider_moved (wxScrollEvent &);
void play_clicked (wxCommandEvent &);
void timer (wxTimerEvent &);
- void process_video (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>);
+ void process_video (boost::shared_ptr<Image>, bool, boost::shared_ptr<Subtitle>);
void calculate_sizes ();
void check_play_state ();
void update_from_raw ();
boost::shared_ptr<Image> _display_frame;
boost::shared_ptr<RGBPlusAlphaImage> _display_sub;
Position _display_sub_position;
+ bool _got_frame;
int _out_width;
int _out_height;
* @param s Sizer to add to.
* @param p Parent window for the wxStaticText.
* @param t Text for the wxStaticText.
- * @param prop Properties to pass when calling Add() on the wxSizer.
+ * @param prop Proportion to pass when calling Add() on the wxSizer.
*/
wxStaticText *
add_label_to_sizer (wxSizer* s, wxWindow* p, string t, int prop)