r863: Merge 2.1:
[cinelerra_cv.git] / cinelerra / file.h
blob084bdf97653fd7aa52ed627f5be37cf6d0262e11
1 #ifndef FILE_H
2 #define FILE_H
4 #include <stdlib.h>
6 #include "asset.inc"
7 #include "condition.inc"
8 #include "edit.inc"
9 #include "filebase.inc"
10 #include "file.inc"
11 #include "filethread.inc"
12 #include "filexml.inc"
13 #include "formatwindow.inc"
14 #include "formattools.h"
15 #include "framecache.inc"
16 #include "guicast.h"
17 #include "mutex.inc"
18 #include "pluginserver.inc"
19 #include "preferences.inc"
20 #include "resample.inc"
21 #include "vframe.inc"
22 #include "packagingengine.h"
24 // ======================================= include file types here
28 // generic file opened by user
29 class File
31 public:
32 File();
33 ~File();
35 // Get attributes for various file formats.
36 // The dither parameter is carried over from recording, where dither is done at the device.
37 int get_options(FormatTools *format,
38 int audio_options,
39 int video_options,
40 int lock_compressor);
42 int raise_window();
43 // Close parameter window
44 void close_window();
46 // ===================================== start here
47 int set_processors(int cpus); // Set the number of cpus for certain codecs.
48 // Set the number of bytes to preload during reads for Quicktime.
49 int set_preload(int64_t size);
50 // Set the subtitle for libmpeg3. -1 disables subtitles.
51 void set_subtitle(int value);
52 // Set whether to interpolate raw images
53 void set_interpolate_raw(int value);
54 // Set whether to white balance raw images. Always 0 if no interpolation.
55 void set_white_balance_raw(int value);
56 // When loading, the asset is deleted and a copy created in the EDL.
57 void set_asset(Asset *asset);
59 // Enable or disable frame caching. Must be tied to file to know when
60 // to delete the file object. Otherwise we'd delete just the cached frames
61 // while the list of open files grew.
62 void set_cache_frames(int value);
63 // Delete oldest frame from cache. Return 0 if successful. Return 1 if
64 // nothing to delete.
65 int purge_cache();
67 // Format may be preset if the asset format is not 0.
68 int open_file(Preferences *preferences,
69 Asset *asset,
70 int rd,
71 int wr,
72 int64_t base_samplerate,
73 float base_framerate);
75 // Get index from the file if one exists. Returns 0 on success.
76 int get_index(char *index_path);
78 // start a thread for writing to avoid blocking during record
79 int start_audio_thread(int64_t buffer_size, int ring_buffers);
80 int stop_audio_thread();
81 // The ring buffer must either be 1 or 2.
82 // The buffer_size for video needs to be > 1 on SMP systems to utilize
83 // multiple processors.
84 // For audio it's the number of samples per buffer.
85 // compressed - if 1 write_compressed_frame is called
86 // if 0 write_frames is called
87 int start_video_thread(int64_t buffer_size,
88 int color_model,
89 int ring_buffers,
90 int compressed);
91 int stop_video_thread();
93 int start_video_decode_thread();
95 // Return the thread.
96 // Used by functions that read only.
97 FileThread* get_video_thread();
99 // write any headers and close file
100 // ignore_thread is used by SigHandler to break out of the threads.
101 int close_file(int ignore_thread = 0);
103 // get length of file normalized to base samplerate
104 int64_t get_audio_length(int64_t base_samplerate = -1);
105 int64_t get_video_length(float base_framerate = -1);
107 // get current position
108 int64_t get_audio_position(int64_t base_samplerate = -1);
109 int64_t get_video_position(float base_framerate = -1);
113 // write samples for the current channel
114 // written to disk and file pointer updated after last channel is written
115 // return 1 if failed
116 // subsequent writes must be <= than first write's size because of buffers
117 int write_samples(double **buffer, int64_t len);
119 // Only called by filethread to write an array of an array of channels of frames.
120 int write_frames(VFrame ***frames, int len);
124 // For writing buffers in a background thread use these functions to get the buffer.
125 // Get a pointer to a buffer to write to.
126 double** get_audio_buffer();
127 VFrame*** get_video_buffer();
129 // Used by ResourcePixmap to directly access the cache.
130 FrameCache* get_frame_cache();
132 // Schedule a buffer for writing on the thread.
133 // thread calls write_samples
134 int write_audio_buffer(int64_t len);
135 int write_video_buffer(int64_t len);
140 // set channel for buffer accesses
141 int set_channel(int channel);
142 // set position in samples
143 int set_audio_position(int64_t position, float base_samplerate);
145 // Read samples for one channel into a shared memory segment.
146 // The offset is the offset in floats from the beginning of the buffer and the len
147 // is the length in floats from the offset.
148 // advances file pointer
149 // return 1 if failed
150 int read_samples(double *buffer, int64_t len, int64_t base_samplerate, float *buffer_float = 0);
153 // set layer for video read
154 // is_thread is used by FileThread::run to prevent recursive lockup.
155 int set_layer(int layer, int is_thread = 0);
156 // set position in frames
157 // is_thread is used by FileThread::run to prevent recursive lockup.
158 int set_video_position(int64_t position, float base_framerate = -1, int is_thread = 0);
160 // Read frame of video into the argument
161 // is_thread is used by FileThread::run to prevent recursive lockup.
162 int read_frame(VFrame *frame, int is_thread = 0);
165 // The following involve no extra copies.
166 // Direct copy routines for direct copy playback
167 int can_copy_from(Edit *edit, int64_t position, int output_w, int output_h); // This file can copy frames directly from the asset
168 int get_render_strategy(ArrayList<int>* render_strategies);
169 int64_t compressed_frame_size();
170 int read_compressed_frame(VFrame *buffer);
171 int write_compressed_frame(VFrame *buffer);
173 // These are separated into two routines so a file doesn't have to be
174 // allocated.
175 // Get best colormodel to translate for hardware accelerated playback.
176 // Called by VRender.
177 int get_best_colormodel(int driver);
178 // Get best colormodel for hardware accelerated recording.
179 // Called by VideoDevice.
180 static int get_best_colormodel(Asset *asset, int driver);
181 // Get nearest colormodel that can be decoded without a temporary frame.
182 // Used by read_frame.
183 int colormodel_supported(int colormodel);
185 // Used by CICache to calculate the total size of the cache.
186 // Based on temporary frames and a call to the file subclass.
187 // The return value is limited 1MB each in case of audio file.
188 // The minimum setting for cache_size should be bigger than 1MB.
189 int64_t get_memory_usage();
191 static int supports_video(ArrayList<PluginServer*> *plugindb, char *format); // returns 1 if the format supports video or audio
192 static int supports_audio(ArrayList<PluginServer*> *plugindb, char *format);
193 // Get the extension for the filename
194 static char* get_tag(int format);
195 static int supports_video(int format); // returns 1 if the format supports video or audio
196 static int supports_audio(int format);
197 static int strtoformat(char *format);
198 static char* formattostr(int format);
199 static int strtoformat(ArrayList<PluginServer*> *plugindb, char *format);
200 static char* formattostr(ArrayList<PluginServer*> *plugindb, int format);
201 static int strtobits(char *bits);
202 static char* bitstostr(int bits);
203 static int str_to_byteorder(char *string);
204 static char* byteorder_to_str(int byte_order);
205 int bytes_per_sample(int bits); // Convert the bit descriptor into a byte count.
207 Asset *asset; // Copy of asset since File outlives EDL
208 FileBase *file; // virtual class for file type
209 // Threads for writing data in the background.
210 FileThread *audio_thread, *video_thread;
212 // Temporary storage for color conversions
213 VFrame *temp_frame;
215 // Resampling engine
216 Resample *resample;
217 Resample_float *resample_float;
219 // Lock writes while recording video and audio.
220 // A binary lock won't do. We need a FIFO lock.
221 Condition *write_lock;
222 int cpus;
223 int64_t playback_preload;
224 int playback_subtitle;
225 int interpolate_raw;
226 int white_balance_raw;
228 // Position information is migrated here to allow samplerate conversion.
229 // Current position in file's samplerate.
230 // Can't normalize to base samplerate because this would
231 // require fractional positioning to know if the file's position changed.
232 int64_t current_sample;
233 int64_t current_frame;
234 int current_channel;
235 int current_layer;
237 // Position information normalized
238 int64_t normalized_sample;
239 int64_t normalized_sample_rate;
240 Preferences *preferences;
242 static PackagingEngine *new_packaging_engine(Asset *asset);
244 private:
245 void reset_parameters();
247 int getting_options;
248 BC_WindowBase *format_window;
249 Mutex *format_completion;
250 FrameCache *frame_cache;
251 // Copy read frames to the cache
252 int use_cache;
255 #endif