Merge speed-up branch.
[dcpomatic.git] / src / lib / dcp_video_frame.cc
index 24bf2173cb149aaa1c2b870eea98734f5dd6ffd7..996aff33f7016d7c86f7e2a75ac537185543faab 100644 (file)
@@ -47,7 +47,6 @@
 #include "dcp_video_frame.h"
 #include "lut.h"
 #include "config.h"
-#include "film_state.h"
 #include "options.h"
 #include "exceptions.h"
 #include "server.h"
 #include "log.h"
 #include "subtitle.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using std::stringstream;
+using std::ofstream;
+using boost::shared_ptr;
 
 /** Construct a DCP video frame.
  *  @param input Input image.
@@ -73,9 +74,9 @@ using namespace boost;
  *  @param l Log to write to.
  */
 DCPVideoFrame::DCPVideoFrame (
-       shared_ptr<Image> yuv, shared_ptr<Subtitle> sub,
+       shared_ptr<const Image> yuv, shared_ptr<Subtitle> sub,
        Size out, int p, int subtitle_offset, float subtitle_scale,
-       Scaler const * s, int f, float fps, string pp, int clut, int bw, Log* l
+       Scaler const * s, SourceFrame f, float fps, string pp, int clut, int bw, Log* l
        )
        : _input (yuv)
        , _subtitle (sub)
@@ -85,8 +86,7 @@ DCPVideoFrame::DCPVideoFrame (
        , _subtitle_scale (subtitle_scale)
        , _scaler (s)
        , _frame (f)
-         /* we round here; not sure if this is right */
-       , _frames_per_second (rint (fps))
+       , _frames_per_second (dcp_frame_rate(fps).frames_per_second)
        , _post_process (pp)
        , _colour_lut_index (clut)
        , _j2k_bandwidth (bw)
@@ -142,7 +142,6 @@ DCPVideoFrame::~DCPVideoFrame ()
 
        if (_parameters) {
                free (_parameters->cp_comment);
-               free (_parameters->cp_matrice);
        }
        
        delete _parameters;
@@ -276,6 +275,9 @@ DCPVideoFrame::encode_locally ()
 
        /* get a J2K compressor handle */
        _cinfo = opj_create_compress (CODEC_J2K);
+       if (_cinfo == 0) {
+               throw EncodeError ("could not create JPEG2000 encoder");
+       }
 
        /* Set event manager to null (openjpeg 1.3 bug) */
        _cinfo->event_mgr = 0;
@@ -284,10 +286,13 @@ DCPVideoFrame::encode_locally ()
        opj_setup_encoder (_cinfo, _parameters, _image);
 
        _cio = opj_cio_open ((opj_common_ptr) _cinfo, 0, 0);
+       if (_cio == 0) {
+               throw EncodeError ("could not open JPEG2000 stream");
+       }
 
        int const r = opj_encode (_cinfo, _cio, _image, 0);
        if (r == 0) {
-               throw EncodeError ("jpeg2000 encoding failed");
+               throw EncodeError ("JPEG2000 encoding failed");
        }
 
        _log->log (String::compose ("Finished locally-encoded frame %1", _frame));
@@ -302,10 +307,10 @@ DCPVideoFrame::encode_locally ()
 shared_ptr<EncodedData>
 DCPVideoFrame::encode_remotely (ServerDescription const * serv)
 {
-       asio::io_service io_service;
-       asio::ip::tcp::resolver resolver (io_service);
-       asio::ip::tcp::resolver::query query (serv->host_name(), boost::lexical_cast<string> (Config::instance()->server_port ()));
-       asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
+       boost::asio::io_service io_service;
+       boost::asio::ip::tcp::resolver resolver (io_service);
+       boost::asio::ip::tcp::resolver::query query (serv->host_name(), boost::lexical_cast<string> (Config::instance()->server_port ()));
+       boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
 
        shared_ptr<Socket> socket (new Socket);
 
@@ -339,6 +344,13 @@ DCPVideoFrame::encode_remotely (ServerDescription const * serv)
                  << "subtitle_height " << _subtitle->image()->size().height << "\n";
        }
 
+       _log->log (String::compose (
+                          "Sending to remote; pixel format %1, components %2, lines (%3,%4,%5), line sizes (%6,%7,%8)",
+                          _input->pixel_format(), _input->components(),
+                          _input->lines(0), _input->lines(1), _input->lines(2),
+                          _input->line_size()[0], _input->line_size()[1], _input->line_size()[2]
+                          ));
+       
        socket->write ((uint8_t *) s.str().c_str(), s.str().length() + 1, 30);
 
        _input->write_to_socket (socket);
@@ -364,7 +376,7 @@ DCPVideoFrame::encode_remotely (ServerDescription const * serv)
  *  @param frame Frame index.
  */
 void
-EncodedData::write (shared_ptr<const Options> opt, int frame)
+EncodedData::write (shared_ptr<const EncodeOptions> opt, SourceFrame frame)
 {
        string const tmp_j2k = opt->frame_out_path (frame, true);
 
@@ -380,7 +392,7 @@ EncodedData::write (shared_ptr<const Options> opt, int frame)
        string const real_j2k = opt->frame_out_path (frame, false);
 
        /* Rename the file from foo.j2c.tmp to foo.j2c now that it is complete */
-       filesystem::rename (tmp_j2k, real_j2k);
+       boost::filesystem::rename (tmp_j2k, real_j2k);
 
        /* Write a file containing the hash */
        string const hash = real_j2k + ".md5";