a4ac54e7aa42a9d62d11d61370159549837928df
[dcpomatic.git] / src / lib / j2k_still_encoder.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/j2k_still_encoder.cc
21  *  @brief An encoder which writes JPEG2000 files for a single still source image.
22  */
23
24 #include <sstream>
25 #include <stdexcept>
26 #include <iomanip>
27 #include <iostream>
28 #include <boost/filesystem.hpp>
29 #include <sndfile.h>
30 #include <openjpeg.h>
31 #include "j2k_still_encoder.h"
32 #include "config.h"
33 #include "options.h"
34 #include "exceptions.h"
35 #include "dcp_video_frame.h"
36 #include "filter.h"
37 #include "log.h"
38 #include "imagemagick_decoder.h"
39 #include "film.h"
40
41 using std::string;
42 using std::pair;
43 using boost::shared_ptr;
44
45 J2KStillEncoder::J2KStillEncoder (shared_ptr<const Film> f, shared_ptr<const Options> o)
46         : Encoder (f, o)
47 {
48         
49 }
50
51 void
52 J2KStillEncoder::do_process_video (shared_ptr<Image> yuv, shared_ptr<Subtitle> sub)
53 {
54         pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
55         DCPVideoFrame* f = new DCPVideoFrame (
56                 yuv, sub, _opt->out_size, _opt->padding, _film->subtitle_offset(), _film->subtitle_scale(), _film->scaler(), 0, _film->frames_per_second(), s.second,
57                 Config::instance()->colour_lut_index(), Config::instance()->j2k_bandwidth(),
58                 _film->log()
59                 );
60
61         if (!boost::filesystem::exists (_opt->frame_out_path (0, false))) {
62                 boost::shared_ptr<EncodedData> e = f->encode_locally ();
63                 e->write (_opt, 0);
64         }
65
66         string const real = _opt->frame_out_path (0, false);
67         for (int i = 1; i < (_film->still_duration() * ImageMagickDecoder::static_frames_per_second()); ++i) {
68                 if (!boost::filesystem::exists (_opt->frame_out_path (i, false))) {
69                         string const link = _opt->frame_out_path (i, false);
70 #ifdef DVDOMATIC_POSIX                  
71                         int const r = symlink (real.c_str(), link.c_str());
72                         if (r) {
73                                 throw EncodeError ("could not create symlink");
74                         }
75 #endif
76 #ifdef DVDOMATIC_WINDOWS
77                         boost::filesystem::copy_file (real, link);
78 #endif                  
79                 }
80                 frame_done ();
81         }
82 }