1 /**
2  * @file
3  * @brief Source file for ImageWriter class
4  * @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
12  * (http://www.openshotstudios.com). This file is part of
13  * OpenShot Library (http://www.openshot.org), an open-source project
14  * dedicated to delivering high quality video editing and animation solutions
15  * to the world.
16  *
17  * This file is originally based on the Libavformat API example, and then modified
18  * by the libopenshot project.
19  *
20  * OpenShot Library (libopenshot) is free software: you can redistribute it
21  * and/or modify it under the terms of the GNU Lesser General Public License
22  * as published by the Free Software Foundation, either version 3 of the
23  * License, or (at your option) any later version.
24  *
25  * OpenShot Library (libopenshot) is distributed in the hope that it will be
26  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
32  */
33 
34 //Require ImageMagick support
35 #ifdef USE_IMAGEMAGICK
36 
37 #include "ImageWriter.h"
38 #include "Exceptions.h"
39 
40 using namespace openshot;
41 
ImageWriter(std::string path)42 ImageWriter::ImageWriter(std::string path) :
43 		path(path), cache_size(8), write_video_count(0), image_quality(75), number_of_loops(1),
44 		combine_frames(true), is_open(false)
45 {
46 	// Disable audio & video (so they can be independently enabled)
47 	info.has_audio = false;
48 	info.has_video = true;
49 }
50 
51 // Set video export options
SetVideoOptions(std::string format,Fraction fps,int width,int height,int quality,int loops,bool combine)52 void ImageWriter::SetVideoOptions(std::string format, Fraction fps, int width, int height,
53 		int quality, int loops, bool combine)
54 {
55 	// Set frames per second (if provided)
56 	info.fps.num = fps.num;
57 	info.fps.den = fps.den;
58 
59 	// Set image magic properties
60 	image_quality = quality;
61 	number_of_loops = loops;
62 	combine_frames = combine;
63 	info.vcodec = format;
64 
65 	// Set the timebase (inverse of fps)
66 	info.video_timebase.num = info.fps.den;
67 	info.video_timebase.den = info.fps.num;
68 
69 	if (width >= 1)
70 		info.width = width;
71 	if (height >= 1)
72 		info.height = height;
73 
74 	info.video_bit_rate = quality;
75 
76 	// Calculate the DAR (display aspect ratio)
77 	Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
78 
79 	// Reduce size fraction
80 	size.Reduce();
81 
82 	// Set the ratio based on the reduced fraction
83 	info.display_ratio.num = size.num;
84 	info.display_ratio.den = size.den;
85 
86 	ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::SetVideoOptions (" + format + ")", "width", width, "height", height, "size.num", size.num, "size.den", size.den, "fps.num", fps.num, "fps.den", fps.den);
87 }
88 
89 // Open the writer
Open()90 void ImageWriter::Open()
91 {
92 	is_open = true;
93 }
94 
95 // Add a frame to the queue waiting to be encoded.
WriteFrame(std::shared_ptr<Frame> frame)96 void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
97 {
98 	// Check for open reader (or throw exception)
99 	if (!is_open)
100 		throw WriterClosed("The ImageWriter is closed.  Call Open() before calling this method.", path);
101 
102 
103 	// Copy and resize image
104 	std::shared_ptr<Magick::Image> frame_image = frame->GetMagickImage();
105 	frame_image->magick( info.vcodec );
106 	frame_image->backgroundColor(Magick::Color("none"));
107 	MAGICK_IMAGE_ALPHA(frame_image, true);
108 	frame_image->quality(image_quality);
109 	frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
110 	frame_image->animationIterations(number_of_loops);
111 
112 	// Calculate correct DAR (display aspect ratio)
113 	int new_width = info.width;
114 	int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
115 
116 	// Resize image
117 	Magick::Geometry new_size(new_width, new_height);
118 	new_size.aspect(true);
119 	frame_image->resize(new_size);
120 
121 
122 	// Put resized frame in vector (waiting to be written)
123 	frames.push_back(*frame_image.get());
124 
125 	// Keep track of the last frame added
126 	last_frame = frame;
127 }
128 
129 // Write a block of frames from a reader
WriteFrame(ReaderBase * reader,int64_t start,int64_t length)130 void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
131 {
132 	ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::WriteFrame (from Reader)", "start", start, "length", length);
133 
134 	// Loop through each frame (and encoded it)
135 	for (int64_t number = start; number <= length; number++)
136 	{
137 		// Get the frame
138 		std::shared_ptr<Frame> f = reader->GetFrame(number);
139 
140 		// Encode frame
141 		WriteFrame(f);
142 	}
143 }
144 
145 // Close the writer and encode/output final image to the disk.
Close()146 void ImageWriter::Close()
147 {
148 	// Write frame's image to file
149 	Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
150 
151 	// Clear frames vector
152 	frames.clear();
153 
154 	// Reset frame counters
155 	write_video_count = 0;
156 
157 	// Close writer
158 	is_open = false;
159 
160 	ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close");
161 }
162 
163 #endif //USE_IMAGEMAGICK
164