f62739e0990b23605f1ac0ff3d1c86867daacde9
[goodguy/history.git] / cinelerra-5.1 / cinelerra / file.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef FILE_H
23 #define FILE_H
24
25 #include <stdlib.h>
26
27 #include "asset.inc"
28 #include "condition.inc"
29 #include "edit.inc"
30 #include "filebase.inc"
31 #include "file.inc"
32
33 #include "filethread.inc"
34 #include "filexml.inc"
35 #include "formattools.inc"
36 #include "formatwindow.inc"
37 #include "formattools.h"
38 #include "framecache.inc"
39 #include "guicast.h"
40 #include "indexfile.inc"
41 #include "mainprogress.inc"
42 #include "mutex.inc"
43 #include "packagingengine.inc"
44 #include "pluginserver.inc"
45 #include "preferences.inc"
46 #include "samples.inc"
47 #include "vframe.inc"
48 #include "packagingengine.h"
49
50 // ======================================= include file types here
51
52
53
54 // generic file opened by user
55 class File
56 {
57 public:
58         File();
59         ~File();
60
61         int probe();
62 // Get attributes for various file formats.
63 // The dither parameter is carried over from recording, where dither is done at the device.
64         int get_options(FormatTools *format,
65                 int audio_options,
66                 int video_options);
67
68         int raise_window();
69 // Close parameter window
70         void close_window();
71
72 // ===================================== start here
73         int set_processors(int cpus);   // Set the number of cpus for certain codecs.
74 // Set the number of bytes to preload during reads for Quicktime.
75         int set_preload(int64_t size);
76 // Set the subtitle for libzmpeg3.  -1 disables subtitles.
77         void set_subtitle(int value);
78 // Set whether to interpolate raw images
79         void set_interpolate_raw(int value);
80 // Set whether to white balance raw images.  Always 0 if no interpolation.
81         void set_white_balance_raw(int value);
82 // When loading, the asset is deleted and a copy created in the EDL.
83 //      void set_asset(Asset *asset);
84
85 // Enable or disable frame caching.  Must be tied to file to know when
86 // to delete the file object.  Otherwise we'd delete just the cached frames
87 // while the list of open files grew.
88         void set_cache_frames(int value);
89 // Delete oldest frame from cache.
90 // Return number of bytes freed if successful.
91 // Return 0 if nothing to delete.
92         int purge_cache();
93 // Delete oldest frame from cache.  Return 0 if successful.  Return 1 if
94 // nothing to delete.
95         int delete_oldest();
96
97 // Format may be preset if the asset format is not 0.
98         int open_file(Preferences *preferences,
99                 Asset *asset,
100                 int rd,
101                 int wr);
102
103 // Get index from the file if one exists.  Returns 0 on success.
104         int get_index(IndexFile *index_file, MainProgressBar *progress_bar);
105
106 // start a thread for writing to avoid blocking during record
107         int start_audio_thread(int buffer_size, int ring_buffers);
108         int stop_audio_thread();
109 // The ring buffer must either be 1 or 2.
110 // The buffer_size for video needs to be > 1 on SMP systems to utilize
111 // multiple processors.
112 // For audio it's the number of samples per buffer.
113 // compressed - if 1 write_compressed_frame is called
114 //              if 0 write_frames is called
115         int start_video_thread(int buffer_size,
116                 int color_model,
117                 int ring_buffers,
118                 int compressed);
119         int stop_video_thread();
120
121         int start_video_decode_thread();
122
123 // Return the thread.
124 // Used by functions that read only.
125         FileThread* get_video_thread();
126
127 // write any headers and close file
128 // ignore_thread is used by SigHandler to break out of the threads.
129         int close_file(int ignore_thread = 0);
130         void delete_temp_samples_buffer();
131         void delete_temp_frame_buffer();
132
133 // get length of file normalized to base samplerate
134         int64_t get_audio_length();
135         int64_t get_video_length();
136
137 // get current position
138         int64_t get_audio_position();
139         int64_t get_video_position();
140
141
142
143 // write samples for the current channel
144 // written to disk and file pointer updated after last channel is written
145 // return 1 if failed
146 // subsequent writes must be <= than first write's size because of buffers
147         int write_samples(Samples **buffer, int64_t len);
148
149 // Only called by filethread to write an array of an array of channels of frames.
150         int write_frames(VFrame ***frames, int len);
151
152
153
154 // For writing buffers in a background thread use these functions to get the buffer.
155 // Get a pointer to a buffer to write to.
156         Samples** get_audio_buffer();
157         VFrame*** get_video_buffer();
158
159 // Used by ResourcePixmap to directly access the cache.
160         FrameCache* get_frame_cache();
161
162 // Schedule a buffer for writing on the thread.
163 // thread calls write_samples
164         int write_audio_buffer(int64_t len);
165         int write_video_buffer(int64_t len);
166
167
168
169
170 // set channel for buffer accesses
171         int set_channel(int channel);
172         int get_channel();
173
174 // set dvd program, no=-1 get current program
175         int set_program(int no);
176 // access cell times
177         int get_cell_time(int no, double &time);
178 // return stream pid
179         int get_video_pid(int track);
180 // return video info
181         int get_video_info(int track, int &pid, double &framerate,
182                 int &width, int &height, char *title);
183 // update asset with selected audio/video layer data
184         int select_video_stream(Asset *asset, int vstream);
185         int select_audio_stream(Asset *asset, int astream);
186 // get dvb system_time
187         int get_system_time(int64_t &tm);
188 // get dvb audio channels for video stream
189         int get_audio_for_video(int vstream, int astream, int64_t &channel_mask);
190 // get frame thumbnail data
191         int get_thumbnail(int stream,
192                 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh);
193         int set_skimming(int track, int skim, skim_fn fn, void *vp);
194         int skim_video(int track, void *vp, skim_fn fn);
195
196 // set position in samples
197         int set_audio_position(int64_t position);
198
199 // Read samples for one channel into a shared memory segment.
200 // The offset is the offset in floats from the beginning of the buffer and the len
201 // is the length in floats from the offset.
202 // advances file pointer
203 // return 1 if failed
204         int read_samples(Samples *buffer, int64_t len);
205
206 // set layer for video read
207 // is_thread is used by FileThread::run to prevent recursive lockup.
208         int set_layer(int layer, int is_thread = 0);
209 // set position in frames
210 // is_thread is set by FileThread::run to prevent recursive lockup.
211 //      int set_video_position(int64_t position, float base_framerate /* = -1 */, int is_thread /* = 0 */);
212         int set_video_position(int64_t position, int is_thread /* = 0 */);
213
214 // Read frame of video into the argument
215 // is_thread is used by FileThread::run to prevent recursive lockup.
216         int read_frame(VFrame *frame, int is_thread = 0);
217
218
219 // The following involve no extra copies.
220 // Direct copy routines for direct copy playback
221         int can_copy_from(Asset *asset, int64_t position, int output_w, int output_h); // This file can copy frames directly from the asset
222         int get_render_strategy(ArrayList<int>* render_strategies);
223         int64_t compressed_frame_size();
224         int read_compressed_frame(VFrame *buffer);
225         int write_compressed_frame(VFrame *buffer);
226
227 // These are separated into two routines so a file doesn't have to be
228 // allocated.
229 // Get best colormodel to translate for hardware accelerated playback.
230 // Called by VRender.
231         int get_best_colormodel(int driver);
232 // Get best colormodel for hardware accelerated recording.
233 // Called by VideoDevice.
234         static int get_best_colormodel(Asset *asset, int driver);
235 // Get nearest colormodel that can be decoded without a temporary frame.
236 // Used by read_frame.
237         int colormodel_supported(int colormodel);
238
239 // stubs for now
240         static const char *compressiontostr(const char *codec) { return codec; }
241         static const char *strtocompression(const char *string) { return string; }
242 // subclass memory usage
243         int64_t file_memory_usage();
244 // Used by CICache to calculate the total size of the cache.
245 // Based on temporary frames and a call to the file subclass.
246 // The return value is limited 1MB each in case of audio file.
247 // The minimum setting for cache_size should be bigger than 1MB.
248         int64_t get_memory_usage();
249
250         static int renders_video(int format);
251         static int renders_video(Asset *asset);
252         static int renders_audio(int format);
253         static int renders_audio(Asset *asset);
254 // Get the extension for the filename
255         static const char* get_tag(int format);
256         static const char* get_prefix(int format);
257         static int strtoformat(const char *format);
258         static const char* formattostr(int format);
259         static int is_image_render(int format);
260         static int strtobits(const char *bits);
261         static const char* bitstostr(int bits);
262         static int str_to_byteorder(const char *string);
263         static const char* byteorder_to_str(int byte_order);
264         int bytes_per_sample(int bits); // Convert the bit descriptor into a byte count.
265 // get record stream file descriptor
266         int record_fd();
267
268         Asset *asset;    // Copy of asset since File outlives EDL
269         FileBase *file; // virtual class for file type
270 // Threads for writing data in the background.
271         FileThread *audio_thread, *video_thread;
272
273 // Temporary storage for color conversions
274         VFrame *temp_frame;
275
276 // Temporary storage for get_audio_buffer.
277 // [ring buffers][channels][Samples]
278         Samples ***temp_samples_buffer;
279
280 // Temporary storage for get_video_buffer.
281 // [Ring buffers][layers][temp_frame_size][VFrame]
282         VFrame ****temp_frame_buffer;
283 // Return value of get_video_buffer
284         VFrame ***current_frame_buffer;
285 // server copies of variables for threaded recording
286         int audio_ring_buffers;
287         int video_ring_buffers;
288 // Number of frames in the temp_frame_buffer
289         int video_buffer_size;
290
291 // Lock writes while recording video and audio.
292 // A binary lock won't do.  We need a FIFO lock.
293         Condition *write_lock;
294         int cpus;
295         int64_t playback_preload;
296         int playback_subtitle;
297         int interpolate_raw;
298         int white_balance_raw;
299
300 // Position information is migrated here to allow samplerate conversion.
301 // Current position in file's samplerate.
302 // Can't normalize to base samplerate because this would
303 // require fractional positioning to know if the file's position changed.
304         int64_t current_sample;
305         int64_t current_frame;
306         int current_channel;
307         int current_layer;
308         int current_program;
309
310 // Position information normalized to project rates
311         int64_t normalized_sample;
312 //      int64_t normalized_sample_rate;
313         Preferences *preferences;
314         int wr, rd;
315         static PackagingEngine *new_packaging_engine(Asset *asset);
316
317         static void init_cin_path();
318         static void get_exe_path(char *result, char *bnp=0);
319         static void getenv_path(char *result, const char *path);
320         static void setenv_path(char *result, const char *var, const char *path);
321         static char cinexe_path[BCTEXTLEN], cinpkg_path[BCTEXTLEN];
322         static char cindat_path[BCTEXTLEN], cinlib_path[BCTEXTLEN];
323         static char cincfg_path[BCTEXTLEN], cinplg_path[BCTEXTLEN];
324         static char cinlad_path[BCTEXTLEN], cinlcl_path[BCTEXTLEN];
325         static char cinbwr_path[BCTEXTLEN];
326         static const char *get_cin() { return getenv("CIN_PKG"); }
327         static const char *get_cin_path() { return getenv("CIN_PATH"); }
328         static const char *get_cindat_path() { return getenv("CIN_DAT"); }
329         static const char *get_cinlib_path() { return getenv("CIN_LIB"); }
330         static const char *get_config_path() { return getenv("CIN_CONFIG"); }
331         static const char *get_plugin_path() { return getenv("CIN_PLUGIN"); }
332         static const char *get_ladspa_path() { return getenv("CIN_LADSPA"); }
333         static const char *get_locale_path() { return getenv("CIN_LOCALE"); }
334         static const char *get_browser_path() { return getenv("CIN_BROWSER"); }
335
336         static const char *default_probes[];
337         static const int nb_probes;
338
339 private:
340         void reset_parameters();
341
342         int getting_options;
343         BC_WindowBase *format_window;
344         Condition *format_completion;
345         FrameCache *frame_cache;
346 // Copy read frames to the cache
347         int use_cache;
348 };
349
350 #endif