4 * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
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.
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.
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
28 #include "condition.inc"
30 #include "filebase.inc"
33 #include "filethread.inc"
34 #include "filexml.inc"
35 #include "formattools.inc"
36 #include "formatwindow.inc"
37 #include "formattools.h"
38 #include "framecache.inc"
40 #include "indexfile.inc"
41 #include "mainprogress.inc"
43 #include "packagingengine.inc"
44 #include "pluginserver.inc"
45 #include "preferences.inc"
46 #include "samples.inc"
49 // ======================================= include file types here
53 // generic file opened by user
61 // Get attributes for various file formats.
62 // The dither parameter is carried over from recording, where dither is done at the device.
63 int get_options(FormatTools *format,
68 // Close parameter window
71 // ===================================== start here
72 int set_processors(int cpus); // Set the number of cpus for certain codecs.
73 // Set the number of bytes to preload during reads for Quicktime.
74 int set_preload(int64_t size);
75 // Set the subtitle for libzmpeg3. -1 disables subtitles.
76 void set_subtitle(int value);
77 // Set whether to interpolate raw images
78 void set_interpolate_raw(int value);
79 // Set whether to white balance raw images. Always 0 if no interpolation.
80 void set_white_balance_raw(int value);
81 // When loading, the asset is deleted and a copy created in the EDL.
82 // void set_asset(Asset *asset);
84 // Enable or disable frame caching. Must be tied to file to know when
85 // to delete the file object. Otherwise we'd delete just the cached frames
86 // while the list of open files grew.
87 void set_cache_frames(int value);
88 // Delete oldest frame from cache.
89 // Return number of bytes freed if successful.
90 // Return 0 if nothing to delete.
92 // Delete oldest frame from cache. Return 0 if successful. Return 1 if
96 // Format may be preset if the asset format is not 0.
97 int open_file(Preferences *preferences,
102 // Get index from the file if one exists. Returns 0 on success.
103 int get_index(IndexFile *index_file, MainProgressBar *progress_bar);
105 // start a thread for writing to avoid blocking during record
106 int start_audio_thread(int buffer_size, int ring_buffers);
107 int stop_audio_thread();
108 // The ring buffer must either be 1 or 2.
109 // The buffer_size for video needs to be > 1 on SMP systems to utilize
110 // multiple processors.
111 // For audio it's the number of samples per buffer.
112 // compressed - if 1 write_compressed_frame is called
113 // if 0 write_frames is called
114 int start_video_thread(int buffer_size,
118 int stop_video_thread();
120 int start_video_decode_thread();
122 // Return the thread.
123 // Used by functions that read only.
124 FileThread* get_video_thread();
126 // write any headers and close file
127 // ignore_thread is used by SigHandler to break out of the threads.
128 int close_file(int ignore_thread = 0);
129 void delete_temp_samples_buffer();
130 void delete_temp_frame_buffer();
132 // get length of file normalized to base samplerate
133 int64_t get_audio_length();
134 int64_t get_video_length();
136 // get current position
137 int64_t get_audio_position();
138 int64_t get_video_position();
142 // write samples for the current channel
143 // written to disk and file pointer updated after last channel is written
144 // return 1 if failed
145 // subsequent writes must be <= than first write's size because of buffers
146 int write_samples(Samples **buffer, int64_t len);
148 // Only called by filethread to write an array of an array of channels of frames.
149 int write_frames(VFrame ***frames, int len);
153 // For writing buffers in a background thread use these functions to get the buffer.
154 // Get a pointer to a buffer to write to.
155 Samples** get_audio_buffer();
156 VFrame*** get_video_buffer();
158 // Used by ResourcePixmap to directly access the cache.
159 FrameCache* get_frame_cache();
161 // Schedule a buffer for writing on the thread.
162 // thread calls write_samples
163 int write_audio_buffer(int64_t len);
164 int write_video_buffer(int64_t len);
169 // set channel for buffer accesses
170 int set_channel(int channel);
173 // set dvd program, no=-1 get current program
174 int set_program(int no);
176 int get_cell_time(int no, double &time);
178 int get_video_pid(int track);
180 int get_video_info(int track, int &pid, double &framerate,
181 int &width, int &height, char *title);
182 // update asset with selected audio/video layer data
183 int select_video_stream(Asset *asset, int vstream);
184 int select_audio_stream(Asset *asset, int astream);
185 // get dvb system_time
186 int get_system_time(int64_t &tm);
187 // get dvb audio channels for video stream
188 int get_audio_for_video(int vstream, int astream, int64_t &channel_mask);
189 // get frame thumbnail data
190 int get_thumbnail(int stream,
191 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh);
192 int set_skimming(int track, int skim, skim_fn fn, void *vp);
193 int skim_video(int track, void *vp, skim_fn fn);
195 // set position in samples
196 int set_audio_position(int64_t position);
198 // Read samples for one channel into a shared memory segment.
199 // The offset is the offset in floats from the beginning of the buffer and the len
200 // is the length in floats from the offset.
201 // advances file pointer
202 // return 1 if failed
203 int read_samples(Samples *buffer, int64_t len);
205 // set layer for video read
206 // is_thread is used by FileThread::run to prevent recursive lockup.
207 int set_layer(int layer, int is_thread = 0);
208 // set position in frames
209 // is_thread is set by FileThread::run to prevent recursive lockup.
210 // int set_video_position(int64_t position, float base_framerate /* = -1 */, int is_thread /* = 0 */);
211 int set_video_position(int64_t position, int is_thread /* = 0 */);
213 // Read frame of video into the argument
214 // is_thread is used by FileThread::run to prevent recursive lockup.
215 int read_frame(VFrame *frame, int is_thread = 0);
218 // The following involve no extra copies.
219 // Direct copy routines for direct copy playback
220 int can_copy_from(Asset *asset, int64_t position, int output_w, int output_h); // This file can copy frames directly from the asset
221 int get_render_strategy(ArrayList<int>* render_strategies);
222 int64_t compressed_frame_size();
223 int read_compressed_frame(VFrame *buffer);
224 int write_compressed_frame(VFrame *buffer);
226 // These are separated into two routines so a file doesn't have to be
228 // Get best colormodel to translate for hardware accelerated playback.
229 // Called by VRender.
230 int get_best_colormodel(int driver, int vstream=-1);
231 // Get best colormodel for hardware accelerated recording.
232 // Called by VideoDevice.
233 static int get_best_colormodel(Asset *asset, int driver);
234 // asset scaling on read_frame
235 static int can_scale_input(Asset *asset);
236 // Get nearest colormodel that can be decoded without a temporary frame.
237 // Used by read_frame.
238 int colormodel_supported(int colormodel);
240 int get_cache_frame(VFrame *vframe, int64_t position);
241 void put_cache_frame(VFrame *frame, int64_t position, int use_copy);
245 static const char *compressiontostr(const char *codec) { return codec; }
246 static const char *strtocompression(const char *string) { return string; }
247 // subclass memory usage
248 int64_t file_memory_usage();
249 // Used by CICache to calculate the total size of the cache.
250 // Based on temporary frames and a call to the file subclass.
251 // The return value is limited 1MB each in case of audio file.
252 // The minimum setting for cache_size should be bigger than 1MB.
253 int64_t get_memory_usage();
255 static int renders_video(int format);
256 static int renders_video(Asset *asset);
257 static int renders_audio(int format);
258 static int renders_audio(Asset *asset);
259 // Get the extension for the filename
260 static const char* get_tag(int format);
261 static const char* get_prefix(int format);
262 static int strtoformat(const char *format);
263 static const char* formattostr(int format);
264 static int is_image_render(int format);
265 static int strtobits(const char *bits);
266 static const char* bitstostr(int bits);
267 static int str_to_byteorder(const char *string);
268 static const char* byteorder_to_str(int byte_order);
269 int bytes_per_sample(int bits); // Convert the bit descriptor into a byte count.
270 // get record stream file descriptor
272 // brender update video map
273 virtual int write_frame_done(int64_t position) { return 0; }
275 Asset *asset; // Copy of asset since File outlives EDL
276 FileBase *file; // virtual class for file type
277 // Threads for writing data in the background.
278 FileThread *audio_thread, *video_thread;
280 // Temporary storage for color conversions
283 // Temporary storage for get_audio_buffer.
284 // [ring buffers][channels][Samples]
285 Samples ***temp_samples_buffer;
287 // Temporary storage for get_video_buffer.
288 // [Ring buffers][layers][temp_frame_size][VFrame]
289 VFrame ****temp_frame_buffer;
290 // Return value of get_video_buffer
291 VFrame ***current_frame_buffer;
292 // server copies of variables for threaded recording
293 int audio_ring_buffers;
294 int video_ring_buffers;
295 // Number of frames in the temp_frame_buffer
296 int video_buffer_size;
298 // Lock writes while recording video and audio.
299 // A binary lock won't do. We need a FIFO lock.
300 Condition *write_lock;
302 int64_t playback_preload;
303 int playback_subtitle;
305 int white_balance_raw;
307 // Position information is migrated here to allow samplerate conversion.
308 // Current position in file's samplerate.
309 // Can't normalize to base samplerate because this would
310 // require fractional positioning to know if the file's position changed.
311 int64_t current_sample;
312 int64_t current_frame;
317 // Position information normalized to project rates
318 int64_t normalized_sample;
319 // int64_t normalized_sample_rate;
320 Preferences *preferences;
323 static void init_cin_env_vars();
324 static void get_exe_path(char *result, char *bnp=0);
325 static void getenv_path(char *result, const char *path);
326 static void setenv_path(const char *var, const char *path, int overwrite);
327 static const char *get_cin() { return getenv("CIN_PKG"); }
328 static const char *get_cin_path() { return getenv("CIN_PATH"); }
329 static const char *get_cindat_path() { return getenv("CIN_DAT"); }
330 static const char *get_cinlib_path() { return getenv("CIN_LIB"); }
331 static const char *get_config_path() { return getenv("CIN_CONFIG"); }
332 static const char *get_plugin_path() { return getenv("CIN_PLUGIN"); }
333 static const char *get_ladspa_path() { return getenv("CIN_LADSPA"); }
334 static const char *get_locale_path() { return getenv("CIN_LOCALE"); }
335 static const char *get_render_path() { return getenv("CIN_RENDER"); }
336 static const char *get_browser_path() { return getenv("CIN_BROWSER"); }
338 static const char *default_probes[];
339 static const int nb_probes;
342 void reset_parameters();
345 BC_WindowBase *format_window;
346 Condition *format_completion;
347 FrameCache *frame_cache;
348 // Copy read frames to the cache