Merge writer-thread with original which was time-cleanup.
authorCarl Hetherington <cth@carlh.net>
Thu, 17 Jan 2013 21:42:43 +0000 (21:42 +0000)
committerCarl Hetherington <cth@carlh.net>
Thu, 17 Jan 2013 21:42:43 +0000 (21:42 +0000)
1  2 
src/lib/dcp_video_frame.cc
src/lib/dcp_video_frame.h
src/lib/encoder.cc
src/lib/encoder.h

index 427d447ef8d32c02f07569695b29b37da86b9a0d,f84fa8a3f1be39053b4bb77c75a7f2e1938ca789..1c408270e46ae6b4e34c027740e760897ede819a
@@@ -59,8 -59,8 +59,9 @@@
  using std::string;
  using std::stringstream;
  using std::ofstream;
+ using std::cout;
  using boost::shared_ptr;
 +using libdcp::Size;
  
  /** Construct a DCP video frame.
   *  @param input Input image.
@@@ -372,14 -372,26 +373,26 @@@ DCPVideoFrame::encode_remotely (ServerD
        return e;
  }
  
+ EncodedData::EncodedData (int s)
+       : _data (new uint8_t[s])
+       , _size (s)
+ {
+ }
+ EncodedData::~EncodedData ()
+ {
+       delete[] _data;
+ }
  /** Write this data to a J2K file.
   *  @param opt Options.
 - *  @param frame Frame index.
 + *  @param frame DCP Frame index.
   */
  void
 -EncodedData::write (shared_ptr<const EncodeOptions> opt, SourceFrame frame)
 +EncodedData::write (shared_ptr<const Film> film, int frame)
  {
 -      string const tmp_j2k = opt->frame_out_path (frame, true);
 +      string const tmp_j2k = film->frame_out_path (frame, true);
  
        FILE* f = fopen (tmp_j2k.c_str (), "wb");
        
index 4271ebb285b7962f07e3b21a93e6bfab50b81493,f5ffa467e81b8ef45de2b4941ff95940ae80f9df..e311724d8d8b9aa137d367ceaeef02136efbee0c
@@@ -39,18 -39,14 +39,14 @@@ class Subtitle
  class EncodedData
  {
  public:
-       /** @param d Data (will not be freed by this class, but may be by subclasses)
-        *  @param s Size of data, in bytes.
+       /** @param s Size of data, in bytes.
         */
-       EncodedData (uint8_t* d, int s)
-               : _data (d)
-               , _size (s)
-       {}
+       EncodedData (int s);
  
-       virtual ~EncodedData () {}
+       virtual ~EncodedData ();
  
        void send (boost::shared_ptr<Socket> socket);
 -      void write (boost::shared_ptr<const EncodeOptions>, SourceFrame);
 +      void write (boost::shared_ptr<const Film>, int);
  
        /** @return data */
        uint8_t* data () const {
index 6df7c85a1bde49750ef78f0238cd7cfa6af13ecd,731a39042ea0a8be2993aee073a75334192eb5a7..0f48310ef873ce9ec6a447509f7c9f1a20bea242
@@@ -61,7 -59,10 +61,9 @@@ Encoder::Encoder (shared_ptr<const Film
  #ifdef HAVE_SWRESAMPLE          
        , _swr_context (0)
  #endif          
-       , _process_end (false)
 -      , _audio_frames_written (0)
+       , _terminate_encoder (false)
+       , _writer_thread (0)
+       , _terminate_writer (false)
  {
        if (_film->audio_stream()) {
                /* Create sound output files with .tmp suffixes; we will rename
@@@ -210,10 -215,15 +216,15 @@@ Encoder::process_end (
                }
        }
  
+       /* Mop up any unwritten things in the writer's queue */
+       for (list<pair<shared_ptr<EncodedData>, int> >::iterator i = _write_queue.begin(); i != _write_queue.end(); ++i) {
+               i->first->write (_opt, i->second);
+       }
        /* Now do links (or copies on windows) to duplicate frames */
        for (list<pair<int, int> >::iterator i = _links_required.begin(); i != _links_required.end(); ++i) {
 -              link (_opt->frame_out_path (i->first, false), _opt->frame_out_path (i->second, false));
 -              link (_opt->hash_out_path (i->first, false), _opt->hash_out_path (i->second, false));
 +              link (_film->frame_out_path (i->first, false), _film->frame_out_path (i->second, false));
 +              link (_film->hash_out_path (i->first, false), _film->hash_out_path (i->second, false));
        }
  }     
  
@@@ -315,13 -331,12 +326,13 @@@ Encoder::process_video (shared_ptr<Imag
        } 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> (
+               TIMING ("adding to queue of %1", _encode_queue.size ());
+               _encode_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 (),
 +                                                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->colour_lut(), _film->j2k_bandwidth(),
                                                  _film->log()
                                                  )
                                          ));
index 20255cca90d483375cc2998f377ca748d42a2e9e,fff26496345aefac365ac17bfcd39e2efcc54cf0..96e7a1d25e08742597da886b84e0346c89de1caf
@@@ -111,14 -108,21 +112,17 @@@ private
        bool _just_skipped;
  
        /** Number of video frames received so far */
 -      SourceFrame _video_frame;
 +      SourceFrame _video_frames_in;
        /** Number of audio frames received so far */
 -      int64_t _audio_frame;
 -
 -private:
 -      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;
 +      int64_t _audio_frames_in;
 +      /** Number of video frames written for the DCP so far */
 +      int _video_frames_out;
 +      /** Number of audio frames written for the DCP so far */
 +      int64_t _audio_frames_out;
  
+       void writer_thread ();
+       void terminate_writer_thread ();
  #if HAVE_SWRESAMPLE   
        SwrContext* _swr_context;
  #endif
        std::list<std::pair<int, int> > _links_required;
  
        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;
+       bool _terminate_encoder;
+       std::list<boost::shared_ptr<DCPVideoFrame> > _encode_queue;
        std::list<boost::thread *> _worker_threads;
        mutable boost::mutex _worker_mutex;
        boost::condition _worker_condition;