Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MovieWriter.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21  */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/ImageIo.h"
27 #include "cinder/Stream.h"
28 #include "cinder/qtime/QuickTime.h"
29 
30 #include <string>
31 
32 // These forward declarations prevent us from having to bring all of QuickTime into the global namespace in MSW
34 #if defined( CINDER_MSW )
35  typedef struct ComponentInstanceRecord ComponentInstanceRecord;
36  typedef ComponentInstanceRecord * ComponentInstance;
37  typedef ComponentInstance DataHandler;
38  typedef struct TrackType** Track;
39  typedef struct MediaType** Media;
40  typedef struct OpaqueICMCompressionSession* ICMCompressionSessionRef;
41  typedef struct OpaqueICMCompressionSessionOptions* ICMCompressionSessionOptionsRef;
42  typedef const struct OpaqueICMEncodedFrame* ICMEncodedFrameRef;
43  typedef signed long OSStatus;
44  typedef unsigned long CodecType;
45  typedef unsigned long ICMCompressionPassModeFlags;
46 #else
47  #include <QuickTime/QuickTime.h>
48  #include <QuickTime/ImageCompression.h>
49 #endif // defined( CINDER_MSW )
50 
52 namespace cinder { namespace qtime {
53 
55 typedef std::shared_ptr<MovieWriter> MovieWriterRef;
56 
57 class MovieWriter {
58  struct Obj;
59 
60  public:
62  class Format {
63  public:
64  Format();
65  Format( uint32_t codec, float quality );
66  Format( const ICMCompressionSessionOptionsRef settings, uint32_t codec, float quality, float frameRate, bool enableMultiPass );
67  Format( const Format &format );
68  ~Format();
69 
70  const Format& operator=( const Format &format );
71 
73  uint32_t getCodec() const { return mCodec; }
75  Format& setCodec( uint32_t codec ) { mCodec = codec; return *this; }
77  float getQuality() const { return mQualityFloat; }
79  Format& setQuality( float quality );
81  float getDefaultDuration() const { return mDefaultTime; }
83  Format& setDefaultDuration( float defaultDuration ) { mDefaultTime = defaultDuration; return *this; }
85  long getTimeScale() const { return mTimeBase; }
87  Format& setTimeScale( long timeScale ) { mTimeBase = timeScale; return *this; }
89  float getGamma() const { return mGamma; }
91  Format& setGamma( float gamma ) { mGamma = gamma; return *this; }
93  bool isTemporal() const;
95  Format& enableTemporal( bool enable = true );
97  bool isReordering() const;
99  Format& enableReordering( bool enable = true );
101  int32_t getMaxKeyFrameRate() const;
103  Format& setMaxKeyFrameRate( int32_t rate );
105  bool isFrameTimeChanges() const;
107  Format& enableFrameTimeChanges( bool enable = true );
109  bool isMultiPass() const { return mEnableMultiPass; }
111  Format& enableMultiPass( bool enable = true ) { mEnableMultiPass = enable; return *this; }
112 
113  private:
114  void initDefaults();
115 
116  uint32_t mCodec;
117  long mTimeBase;
118  float mDefaultTime;
119  float mQualityFloat;
120  float mGamma;
121  bool mEnableMultiPass;
122 
123  ICMCompressionSessionOptionsRef mOptions;
124 
125  friend class MovieWriter;
126  friend struct Obj;
127  };
128 
129 
131  MovieWriter( const fs::path &path, int32_t width, int32_t height, const Format &format = Format::Format() );
132 
133  static MovieWriterRef create( const fs::path &path, int32_t width, int32_t height, const Format &format = Format::Format() )
134  { return std::shared_ptr<MovieWriter>( new MovieWriter( path, width, height, format ) ); }
135 
137  float getDefaultDuration() const { return mObj->mFormat.mDefaultTime; }
139  int32_t getWidth() const { return mObj->mWidth; }
141  int32_t getHeight() const { return mObj->mHeight; }
143  Vec2i getSize() const { return Vec2i( getWidth(), getHeight() ); }
145  float getAspectRatio() const { return getWidth() / (float)getHeight(); }
147  Area getBounds() const { return Area( 0, 0, getWidth(), getHeight() ); }
148 
150  const Format& getFormat() const { return mObj->mFormat; }
151 
154  static bool getUserCompressionSettings( Format *result, ImageSourceRef previewImage = ImageSourceRef() );
155 
158  void addFrame( const ImageSourceRef &imageSource, float duration = -1.0f ) { mObj->addFrame( imageSource, duration ); }
159 
161  uint32_t getNumFrames() const { return mObj->mNumFrames; }
162 
164  void finish() { mObj->finish(); }
165 
166  enum { CODEC_H264 = 'avc1', CODEC_JPEG = 'jpeg', CODEC_MP4 = 'mp4v', CODEC_PNG = 'png ', CODEC_RAW = 'raw ', CODEC_ANIMATION = 'rle ' };
167 
168  private:
170  struct Obj {
171  Obj( const fs::path &path, int32_t width, int32_t height, const Format &format );
172  ~Obj();
173 
174  void addFrame( const ImageSourceRef &imageSource, float duration );
175  void createCompressionSession();
176  void finish();
177 
178  static OSStatus encodedFrameOutputCallback( void *refCon, ::ICMCompressionSessionRef session, OSStatus err, ICMEncodedFrameRef encodedFrame, void *reserved );
179 
180  ::Movie mMovie;
181  ::DataHandler mDataHandler;
182  ::Track mTrack;
183  ::Media mMedia;
184  ::ICMCompressionSessionRef mCompressionSession;
185  ::ICMCompressionPassModeFlags mMultiPassModeFlags;
186  fs::path mPath;
187  uint32_t mNumFrames;
188  int64_t mCurrentTimeValue;
189 
190  int32_t mWidth, mHeight;
191  Format mFormat;
192  bool mRequestedMultiPass, mDoingMultiPass, mFinished;
193 
194  IoStreamRef mMultiPassFrameCache;
195 
196  std::vector<std::pair<int64_t,int64_t> > mFrameTimes;
197  };
199 
200  std::shared_ptr<Obj> mObj;
201 
202  public:
204  typedef std::shared_ptr<Obj> MovieWriter::*unspecified_bool_type;
206  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &MovieWriter::mObj; }
207  void reset() { mObj.reset(); }
209 };
210 
211 class MovieWriterExc : public Exception {
212 };
214 };
216 };
218 };
219 
220 } } // namespace cinder::qtime
static bool getUserCompressionSettings(Format *result, ImageSourceRef previewImage=ImageSourceRef())
Presents the user with the standard compression options dialog. Optional previewImage provides a stil...
Definition: MovieWriter.cpp:538
Definition: MovieWriter.h:166
Definition: MovieWriter.h:166
uint32_t getNumFrames() const
Returns the number of frames in the movie.
Definition: MovieWriter.h:161
float getGamma() const
Returns the gamma value by which image data is encoded.
Definition: MovieWriter.h:89
int32_t getWidth() const
Returns the width of the Movie in pixels.
Definition: MovieWriter.h:139
void enable(GLenum state)
Enables the OpenGL State state. Equivalent to calling to glEnable( state );.
Definition: dx.h:198
void finish()
Completes the encoding of the movie and closes the file. Calling finish() more than once has no effec...
Definition: MovieWriter.h:164
uint32_t getCodec() const
Returns the four character code for the QuickTime codec. Types can be found in QuickTime's ImageCompr...
Definition: MovieWriter.h:73
Definition: Area.h:37
int32_t getHeight() const
Returns the height of the Movie in pixels.
Definition: MovieWriter.h:141
Definition: MovieWriter.h:166
Format & setTimeScale(long timeScale)
Sets the integer base value for encoding time scale. Defaults to 600.
Definition: MovieWriter.h:87
GLenum GLsizei width
Definition: GLee.h:969
bool isTemporal() const
Returns if temporal compression (allowing P or B frames) is enabled. Defaults to true.
Definition: MovieWriter.cpp:129
void reset()
Emulates shared_ptr-like behavior.
Definition: MovieWriter.h:207
Definition: MovieWriter.h:211
std::shared_ptr< IoStream > IoStreamRef
Definition: Stream.h:150
Format()
Definition: MovieWriter.cpp:69
Definition: MovieWriter.h:213
Outputs QuickTime movies.
Definition: MovieWriter.h:57
float getDefaultDuration() const
Returns the standard duration of a frame measured in seconds.
Definition: MovieWriter.h:81
Format & setCodec(uint32_t codec)
Sets the four character code for the QuickTime codec. Defaults to PNG ('png '). Additional types can ...
Definition: MovieWriter.h:75
MovieWriter()
Definition: MovieWriter.h:130
bool isReordering() const
Returns if frame reordering is enabled. Defaults to true. In order to encode B frames, a compressor must reorder frames, which means that the order in which they will be emitted and stored (the decode order) is different from the order in which they were presented to the compressor (the display order).
Definition: MovieWriter.cpp:142
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
Format & enableFrameTimeChanges(bool enable=true)
Sets whether a codec is allowed to change frame times. Defaults to true. Some compressors are able to...
Definition: MovieWriter.cpp:160
std::shared_ptr< Obj > MovieWriter::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: MovieWriter.h:205
Format & enableTemporal(bool enable=true)
Enables temporal compression (allowing P or B frames). Defaults to true.
Definition: MovieWriter.cpp:134
Format & setQuality(float quality)
Sets the overall quality for encoding. Must be in a range of [0,1.0]. Defaults to 0...
Definition: MovieWriter.cpp:119
Defines the encoding parameters of a MovieWriter.
Definition: MovieWriter.h:62
long getTimeScale() const
Returns the integer base value for the encoding time scale. Defaults to 600.
Definition: MovieWriter.h:85
bool isFrameTimeChanges() const
Returns whether a codec is allowed to change frame times. Defaults to true. Some compressors are able...
Definition: MovieWriter.cpp:155
Format & setDefaultDuration(float defaultDuration)
Sets the default duration of a frame, measured in seconds. Defaults to 1/30 sec, meaning 30 FPS...
Definition: MovieWriter.h:83
Format & enableReordering(bool enable=true)
Enables frame reordering. Defaults to true. In order to encode B frames, a compressor must reorder fr...
Definition: MovieWriter.cpp:147
typedef int64_t(APIENTRYP GLEEPFNGLXSWAPBUFFERSMSCOMLPROC)(Display *dpy
Format & enableMultiPass(bool enable=true)
Enables multiPass encoding. Defaults to false. While multiPass encoding can result in significantly s...
Definition: MovieWriter.h:111
Definition: MovieWriter.h:215
~Format()
Definition: MovieWriter.cpp:98
int32_t getMaxKeyFrameRate() const
Gets the maximum number of frames between key frames. Default is 0, which indicates that the compress...
Definition: MovieWriter.cpp:168
Format & setMaxKeyFrameRate(int32_t rate)
Sets the maximum number of frames between key frames. Default is 0, which indicates that the compress...
Definition: MovieWriter.cpp:173
float getDefaultDuration() const
Returns the Movie's default frame duration measured in seconds. You can also think of this as the Mov...
Definition: MovieWriter.h:137
const Format & operator=(const Format &format)
Definition: MovieWriter.cpp:181
std::shared_ptr< MovieWriter > MovieWriterRef
Definition: MovieWriter.h:54
const Format & getFormat() const
Returns the Movie's Format.
Definition: MovieWriter.h:150
void addFrame(const ImageSourceRef &imageSource, float duration=-1.0f)
Appends a frame to the Movie. The optional duration parameter allows a frame to be inserted for a tim...
Definition: MovieWriter.h:158
GLenum GLsizei GLenum format
Definition: GLee.h:969
friend struct Obj
Definition: MovieWriter.h:126
Definition: Exception.h:32
std::shared_ptr< class ImageSource > ImageSourceRef
Definition: Channel.h:33
static MovieWriterRef create(const fs::path &path, int32_t width, int32_t height, const Format &format=Format::Format())
Definition: MovieWriter.h:133
float getAspectRatio() const
Returns the Movie's aspect ratio, which is its width / height.
Definition: MovieWriter.h:145
Definition: MovieWriter.h:166
float getQuality() const
Returns the overall quality for encoding in the range of [0,1.0]. Defaults to 0.99. 1.0 corresponds to lossless.
Definition: MovieWriter.h:77
Definition: MovieWriter.h:166
Area getBounds() const
Returns the bounding Area of the Movie in pixels: [0,0]-(width,height)
Definition: MovieWriter.h:147
GLclampf f
Definition: GLee.h:15307
Vec2i getSize() const
Returns the size of the Movie in pixels.
Definition: MovieWriter.h:143
Format & setGamma(float gamma)
Sets the gamma value by which image data is encoded. Defaults to 2.5 on MSW and 2.2 on Mac OS X.
Definition: MovieWriter.h:91
bool isMultiPass() const
Returns whether multiPass encoding is enabled. Defaults to false.
Definition: MovieWriter.h:109
Vec2< int > Vec2i
Definition: Vector.h:1313
Definition: MovieWriter.h:166