r105: This commit was manufactured by cvs2svn to create tag
[cinelerra_cv/mob.git] / hvirtual / cinelerra / file.h
blobbdf56f9ead2faa6ac4ede6bdecd70f3c43154f98
1 #ifndef FILE_H
2 #define FILE_H
4 #include <stdlib.h>
6 #include "assets.inc"
7 #include "edit.inc"
8 #include "file.inc"
9 #include "filebase.inc"
10 #include "filethread.inc"
11 #include "filexml.inc"
12 #include "formatwindow.inc"
13 #include "guicast.h"
14 #include "mutex.h"
15 #include "pluginserver.inc"
16 #include "resample.inc"
17 #include "sema.h"
18 #include "vframe.inc"
20 #include <map>
21 #include "timer.h"
22 // ======================================= include file types here
25 class FrameCacheElement {
26 public:
27 int frame_layer;
28 int frame_number;
29 long long time_diff;
30 VFrame *frame;
33 typedef std::multimap<int, FrameCacheElement*> FrameCacheTree;
34 typedef std::multimap<long long, FrameCacheElement*> FrameCacheTree_ByTime;
37 class FrameCache {
38 public:
39 FrameCache(int cache_size = 100);
40 ~FrameCache();
41 VFrame *get_frame(long frame_number, int frame_layer, int frame_width, int frame_height, int color_model); // implicit lock
42 void add_frame(long frame_number, int frame_layer, VFrame *frame, int do_not_copy_frame = 0);
43 void unlock_cache();
44 void lock_cache();
45 void reset();
46 void disable_cache();
47 void enable_cache();
48 void dump();
50 private:
51 int compare_with_frame(FrameCacheElement *element, int frame_number, int frame_layer, int frame_width, int frame_height, int frame_color_model);
52 FrameCacheTree::iterator find_element_byframe(long frame_number, int frame_layer, int frame_width, int frame_height, int frame_color_model);
53 FrameCacheTree_ByTime::iterator find_element_bytime(long long frame_time_diff, long frame_number, int frame_layer, int frame_width, int frame_height, int frame_color_model);
54 Mutex change_lock;
55 int cache_enabled;
56 int cache_size;
57 FrameCacheTree cache_tree;
58 FrameCacheTree_ByTime cache_tree_bytime;
59 Timer timer;
63 // generic file opened by user
64 class File
66 public:
67 File();
68 ~File();
70 // Get attributes for various file formats.
71 // The dither parameter is carried over from recording, where dither is done at the device.
72 int get_options(BC_WindowBase *parent_window,
73 ArrayList<PluginServer*> *plugindb,
74 Asset *asset,
75 int audio_options,
76 int video_options,
77 int lock_compressor);
78 int raise_window();
80 // ===================================== start here
81 int set_processors(int cpus); // Set the number of cpus for certain codecs.
82 int set_preload(int64_t size); // Set the number of bytes to preload during reads.
83 // When loading, the asset is deleted and a copy created in the EDL.
84 void set_asset(Asset *asset);
86 // Format may be preset if the asset format is not 0.
87 int open_file(ArrayList<PluginServer*> *plugindb,
88 Asset *asset,
89 int rd,
90 int wr,
91 int64_t base_samplerate,
92 float base_framerate);
94 // start a thread for writing to avoid blocking during record
95 int start_audio_thread(int64_t buffer_size, int ring_buffers);
96 int stop_audio_thread();
97 int start_video_thread(int64_t buffer_size,
98 int color_model,
99 int ring_buffers,
100 int compressed);
101 int stop_video_thread();
102 int lock_read();
103 int unlock_read();
105 // write any headers and close file
106 // ignore_thread is used by SigHandler to break out of the threads.
107 int close_file(int ignore_thread = 0);
109 // set channel for buffer accesses
110 int set_channel(int channel);
112 // set layer for video read
113 int set_layer(int layer);
115 // get length of file normalized to base samplerate
116 int64_t get_audio_length(int64_t base_samplerate = -1);
117 int64_t get_video_length(float base_framerate = -1);
119 // get current position
120 int64_t get_audio_position(int64_t base_samplerate = -1);
121 int64_t get_video_position(float base_framerate = -1);
124 // set position in samples
125 int set_audio_position(int64_t position, float base_samplerate);
126 // set position in frames
127 int set_video_position(int64_t position, float base_framerate);
129 // write samples for the current channel
130 // written to disk and file pointer updated after last channel is written
131 // return 1 if failed
132 // subsequent writes must be <= than first write's size because of buffers
133 int write_samples(double **buffer, int64_t len);
135 // Only called by filethread
136 int write_frames(VFrame ***frames, int len);
138 // For writing buffers in a background thread use these functions to get the buffer.
139 // Get a pointer to a buffer to write to.
140 double** get_audio_buffer();
141 VFrame*** get_video_buffer();
143 // Schedule a buffer for writing on the thread.
144 // thread calls write_samples
145 int write_audio_buffer(int64_t len);
146 int write_video_buffer(int64_t len);
148 // Read samples for one channel into a shared memory segment.
149 // The offset is the offset in floats from the beginning of the buffer and the len
150 // is the length in floats from the offset.
151 // advances file pointer
152 // return 1 if failed
153 int read_samples(double *buffer, int64_t len, int64_t base_samplerate);
155 // Return a pointer to the frame in a video file for drawing or 0.
156 // The following routine copies a frame once to a temporary buffer and either
157 // returns a pointer to the temporary buffer or copies the temporary buffer again.
158 VFrame* read_frame(int color_model);
160 int read_frame(VFrame *frame);
162 // The following involve no extra copies.
163 // Direct copy routines for direct copy playback
164 int can_copy_from(Edit *edit, int64_t position, int output_w, int output_h); // This file can copy frames directly from the asset
165 int get_render_strategy(ArrayList<int>* render_strategies);
166 int64_t compressed_frame_size();
167 int read_compressed_frame(VFrame *buffer);
168 int write_compressed_frame(VFrame *buffer);
170 // These are separated into two routines so a file doesn't have to be
171 // allocated.
172 // Get best colormodel to translate for hardware acceleration
173 int get_best_colormodel(int driver);
174 static int get_best_colormodel(Asset *asset, int driver);
175 // Get nearest colormodel in format after the file is opened and the
176 // direction determined to know whether to use a temp.
177 int colormodel_supported(int colormodel);
181 static int supports_video(ArrayList<PluginServer*> *plugindb, char *format); // returns 1 if the format supports video or audio
182 static int supports_audio(ArrayList<PluginServer*> *plugindb, char *format);
183 static int supports_video(int format); // returns 1 if the format supports video or audio
184 static int supports_audio(int format);
185 static int strtoformat(ArrayList<PluginServer*> *plugindb, char *format);
186 static char* formattostr(ArrayList<PluginServer*> *plugindb, int format);
187 static int strtobits(char *bits);
188 static char* bitstostr(int bits);
189 static int str_to_byteorder(char *string);
190 static char* byteorder_to_str(int byte_order);
191 int bytes_per_sample(int bits); // Convert the bit descriptor into a byte count.
193 Asset *asset; // Copy of asset since File outlives EDL
194 FileBase *file; // virtual class for file type
195 // Threads for writing data in the background.
196 FileThread *audio_thread, *video_thread;
197 // Temporary storage for color conversions
198 VFrame *temp_frame;
199 // Frame to return pointer to
200 VFrame *return_frame;
201 // Resampling engine
202 Resample *resample;
204 // Lock writes while recording video and audio.
205 // A binary lock won't do. We need a FIFO lock.
206 Sema write_lock;
207 int cpus;
208 int64_t playback_preload;
210 // Position information is migrated here to allow samplerate conversion.
211 // Current position in file's samplerate.
212 // Can't normalize to base samplerate because this would
213 // require fractional positioning to know if the file's position changed.
214 int64_t current_sample;
215 int64_t current_frame;
216 int current_channel;
217 int current_layer;
219 // Position information normalized
220 int64_t normalized_sample;
221 int64_t normalized_sample_rate;
223 FrameCache *frames_cache;
225 private:
226 void reset_parameters();
228 int getting_options;
229 BC_WindowBase *format_window;
230 Mutex format_completion;
233 #endif