3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 // work arounds (centos)
32 #define INT64_MAX 9223372036854775807LL
37 #include "bcsignals.h"
38 #include "byteorder.h"
40 #include "condition.h"
48 #include "fileffmpeg.h"
54 #undef HAVE_STDLIB_H // automake conflict
58 #include "filescene.h"
59 #include "filesndfile.h"
61 #include "filethread.h"
63 #include "filevorbis.h"
65 #include "formatwindow.h"
66 #include "formattools.h"
67 #include "framecache.h"
69 #include "mainprogress.inc"
72 #include "packagingengine.h"
73 #include "pluginserver.h"
74 #include "preferences.h"
75 #include "probeprefs.h"
79 //static int temp_debug = 0;
80 //suppress noref warning
81 void *vorbis0_ov_callbacks[] = {
82 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
83 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
90 format_completion = new Condition(1, "File::format_completion");
91 write_lock = new Condition(1, "File::write_lock");
92 frame_cache = new FrameCache;
98 if( getting_options ) {
99 if( format_window ) format_window->set_done(0);
100 format_completion->lock("File::~File");
101 format_completion->unlock();
105 //printf("File::~File %d temp_debug=%d\n", __LINE__, --temp_debug);
111 asset->Garbage::remove_user();
112 delete format_completion;
117 void File::reset_parameters()
130 normalized_sample = 0;
133 playback_subtitle = -1;
137 temp_samples_buffer = 0;
138 temp_frame_buffer = 0;
139 current_frame_buffer = 0;
140 audio_ring_buffers = 0;
141 video_ring_buffers = 0;
142 video_buffer_size = 0;
145 int File::raise_window()
147 if( getting_options && format_window ) {
148 format_window->raise_window();
149 format_window->flush();
154 void File::close_window()
156 if( getting_options ) {
157 format_window->lock_window("File::close_window");
158 format_window->set_done(1);
159 format_window->unlock_window();
164 int File::get_options(FormatTools *format,
165 int audio_options, int video_options)
167 BC_WindowBase *parent_window = format->window;
168 //ArrayList<PluginServer*> *plugindb = format->plugindb;
169 Asset *asset = format->asset;
173 format_completion->lock("File::get_options");
174 switch( asset->format ) {
176 FileAC3::get_parameters(parent_window,
184 FileDV::get_parameters(parent_window, asset, format_window,
185 audio_options, video_options);
193 FileSndFile::get_parameters(parent_window, asset, format_window,
194 audio_options, video_options);
197 FileFFMPEG::get_parameters(parent_window, asset, format_window,
198 audio_options, video_options);
202 FileMPEG::get_parameters(parent_window, asset, format_window,
203 audio_options, video_options);
207 FileJPEG::get_parameters(parent_window, asset, format_window,
208 audio_options, video_options);
213 FileEXR::get_parameters(parent_window, asset, format_window,
214 audio_options, video_options);
218 FileFLAC::get_parameters(parent_window, asset, format_window,
219 audio_options, video_options);
223 FilePNG::get_parameters(parent_window, asset, format_window,
224 audio_options, video_options);
228 FilePPM::get_parameters(parent_window, asset, format_window,
229 audio_options, video_options);
233 FileTGA::get_parameters(parent_window, asset, format_window,
234 audio_options, video_options);
238 FileTIFF::get_parameters(parent_window, asset, format_window,
239 audio_options, video_options);
242 FileOGG::get_parameters(parent_window, asset, format_window,
243 audio_options, video_options);
249 if( !format_window ) {
250 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
251 parent_window->get_abs_cursor_x(1),
252 parent_window->get_abs_cursor_y(1));
253 format_window = errorbox;
256 errorbox->create_objects(_("This format doesn't support audio."));
259 errorbox->create_objects(_("This format doesn't support video."));
260 errorbox->run_window();
266 format_completion->unlock();
279 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
281 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
282 cpus = 8; // 8 cpus ought to decode just about anything
283 // Set all instances so gets work.
289 int File::set_preload(int64_t size)
291 this->playback_preload = size;
295 void File::set_subtitle(int value)
297 this->playback_subtitle = value;
298 if( file ) file->set_subtitle(value);
301 void File::set_interpolate_raw(int value)
303 this->interpolate_raw = value;
306 void File::set_white_balance_raw(int value)
308 this->white_balance_raw = value;
311 void File::set_cache_frames(int value)
313 // caching only done locally
318 int File::purge_cache()
320 // caching only done locally
322 if( frame_cache->cache_items() > 0 ) {
323 frame_cache->remove_all();
329 int File::delete_oldest()
331 // return the number of bytes freed
332 return frame_cache->delete_oldest();
335 // file driver in order of probe precidence
336 // can be reordered in preferences->interface
337 const char *File::default_probes[] = {
362 const int File::nb_probes =
363 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
368 FILE *fp = fopen(this->asset->path, "rb");
369 if( !fp ) return FILE_NOT_FOUND;
371 memset(data,0,sizeof(data));
372 int ret = fread(data, 1, 16, fp);
374 if( !ret ) return FILE_NOT_FOUND;
376 for( int i=0; i<preferences->file_probes.size(); ++i ) {
377 ProbePref *pref = preferences->file_probes[i];
378 if( !pref->armed ) continue;
379 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
380 if( !FileFFMPEG::check_sig(this->asset) ) continue;
381 file = new FileFFMPEG(this->asset, this);
384 if( !strcmp(pref->name,"DB") ) { // MediaDB
385 if( !FileDB::check_sig(this->asset) ) continue;
386 file = new FileDB(this->asset, this);
389 if( !strcmp(pref->name,"Scene") ) { // scene file
390 if( !FileScene::check_sig(this->asset, data)) continue;
391 file = new FileScene(this->asset, this);
395 if( !strcmp(pref->name,"DV") ) { // libdv
396 if( !FileDV::check_sig(this->asset) ) continue;
397 file = new FileDV(this->asset, this);
401 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
402 if( !FileSndFile::check_sig(this->asset) ) continue;
403 file = new FileSndFile(this->asset, this);
406 if( !strcmp(pref->name,"PNG") ) { // PNG file
407 if( !FilePNG::check_sig(this->asset) ) continue;
408 file = new FilePNG(this->asset, this);
411 if( !strcmp(pref->name,"PPM") ) { // PPM file
412 if( !FilePPM::check_sig(this->asset) ) continue;
413 file = new FilePPM(this->asset, this);
416 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
417 if( !FileJPEG::check_sig(this->asset) ) continue;
418 file = new FileJPEG(this->asset, this);
421 if( !strcmp(pref->name,"GIF") ) { // GIF file
422 if( !FileGIF::check_sig(this->asset)) continue;
423 file = new FileGIF(this->asset, this);
427 if( !strcmp(pref->name,"EXR") ) { // EXR file
428 if( !FileEXR::check_sig(this->asset, data)) continue;
429 file = new FileEXR(this->asset, this);
433 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
434 if( !FileFLAC::check_sig(this->asset, data)) continue;
435 file = new FileFLAC(this->asset, this);
438 if( !strcmp(pref->name,"CR2") ) { // CR2 file
439 if( !FileCR2::check_sig(this->asset)) continue;
440 file = new FileCR2(this->asset, this);
443 if( !strcmp(pref->name,"TGA") ) { // TGA file
444 if( !FileTGA::check_sig(this->asset) ) continue;
445 file = new FileTGA(this->asset, this);
448 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
449 if( !FileTIFF::check_sig(this->asset) ) continue;
450 file = new FileTIFF(this->asset, this);
453 if( !strcmp(pref->name,"OGG") ) { // OGG file
454 if( !FileOGG::check_sig(this->asset) ) continue;
455 file = new FileOGG(this->asset, this);
458 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
459 if( !FileVorbis::check_sig(this->asset) ) continue;
460 file = new FileVorbis(this->asset, this);
463 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
464 if( !FileMPEG::check_sig(this->asset) ) continue;
465 file = new FileMPEG(this->asset, this);
468 if( !strcmp(pref->name,"EDL") ) { // XML file
469 if( data[0] != '<' ) continue;
470 if( !strncmp(&data[1],"EDL>",4) ||
471 !strncmp(&data[1],"HTAL>",5) ||
472 !strncmp(&data[1],"?xml",4) )
476 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
479 int File::open_file(Preferences *preferences,
486 this->preferences = preferences;
487 this->asset->copy_from(asset, 1);
492 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
494 switch( this->asset->format ) {
495 // get the format now
496 // If you add another format to case 0, you also need to add another case for the
497 // file format #define.
500 if( ret != FILE_OK ) return ret;
502 // format already determined
504 file = new FileAC3(this->asset, this);
508 file = new FileScene(this->asset, this);
512 file = new FileFFMPEG(this->asset, this);
520 //printf("File::open_file 1\n");
521 file = new FileSndFile(this->asset, this);
526 file = new FilePNG(this->asset, this);
531 file = new FilePPM(this->asset, this);
536 file = new FileJPEG(this->asset, this);
541 file = new FileGIF(this->asset, this);
546 file = new FileEXR(this->asset, this);
550 file = new FileFLAC(this->asset, this);
555 file = new FileCR2(this->asset, this);
560 file = new FileTGA(this->asset, this);
565 file = new FileTIFF(this->asset, this);
569 file = new FileDB(this->asset, this);
575 file = new FileMPEG(this->asset, this);
579 file = new FileOGG(this->asset, this);
583 file = new FileVorbis(this->asset, this);
587 file = new FileDV(this->asset, this);
597 // Reopen file with correct parser and get header.
598 if( file->open_file(rd, wr) ) {
599 delete file; file = 0;
600 return FILE_NOT_FOUND;
605 // Set extra writing parameters to mandatory settings.
607 if( this->asset->dither ) file->set_dither();
611 // one frame image file, not brender, no specific length
612 if( !this->asset->audio_data && this->asset->use_header &&
613 this->asset->video_data && !this->asset->single_frame &&
614 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
615 this->asset->single_frame = 1;
616 this->asset->video_length = -1;
620 // Synchronize header parameters
621 asset->copy_from(this->asset, 1);
624 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
630 void File::delete_temp_samples_buffer()
633 if( temp_samples_buffer ) {
634 for( int j = 0; j < audio_ring_buffers; j++ ) {
635 for( int i = 0; i < asset->channels; i++ ) {
636 delete temp_samples_buffer[j][i];
638 delete [] temp_samples_buffer[j];
641 delete [] temp_samples_buffer;
642 temp_samples_buffer = 0;
643 audio_ring_buffers = 0;
647 void File::delete_temp_frame_buffer()
650 if( temp_frame_buffer ) {
651 for( int k = 0; k < video_ring_buffers; k++ ) {
652 for( int i = 0; i < asset->layers; i++ ) {
653 for( int j = 0; j < video_buffer_size; j++ ) {
654 delete temp_frame_buffer[k][i][j];
656 delete [] temp_frame_buffer[k][i];
658 delete [] temp_frame_buffer[k];
661 delete [] temp_frame_buffer;
662 temp_frame_buffer = 0;
663 video_ring_buffers = 0;
664 video_buffer_size = 0;
668 int File::close_file(int ignore_thread)
672 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
674 if( !ignore_thread ) {
680 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
682 // The file's asset is a copy of the argument passed to open_file so the
683 // user must copy lengths from the file's asset.
685 asset->audio_length = current_sample;
686 asset->video_length = current_frame;
692 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
694 delete_temp_samples_buffer();
695 delete_temp_frame_buffer();
696 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
698 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
701 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
707 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
709 return !file ? -1 : file->get_index(index_file, progress_bar);
714 int File::start_audio_thread(int buffer_size, int ring_buffers)
716 this->audio_ring_buffers = ring_buffers;
719 if( !audio_thread ) {
720 audio_thread = new FileThread(this, 1, 0);
721 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
726 int File::start_video_thread(int buffer_size,
731 this->video_ring_buffers = ring_buffers;
732 this->video_buffer_size = buffer_size;
734 if( !video_thread ) {
735 video_thread = new FileThread(this, 0, 1);
736 video_thread->start_writing(buffer_size,
744 int File::start_video_decode_thread()
746 // Currently, CR2 is the only one which won't work asynchronously, so
747 // we're not using a virtual function yet.
748 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
749 video_thread = new FileThread(this, 0, 1);
750 video_thread->start_reading();
756 int File::stop_audio_thread()
759 audio_thread->stop_writing();
766 int File::stop_video_thread()
769 video_thread->stop_reading();
770 video_thread->stop_writing();
777 FileThread* File::get_video_thread()
782 int File::set_channel(int channel)
784 if( file && channel < asset->channels ) {
785 current_channel = channel;
792 int File::get_channel()
794 return current_channel;
797 // if no>=0, sets new program
798 // returns current program
799 int File::set_program(int no)
801 int result = file ? file->set_program(no) : current_program;
802 current_program = no < 0 ? result : no;
806 int File::get_cell_time(int no, double &time)
808 return file ? file->get_cell_time(no, time) : -1;
811 int File::get_system_time(int64_t &tm)
813 return file ? file->get_system_time(tm) : -1;
816 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
818 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
821 int File::get_video_pid(int track)
823 return file ? file->get_video_pid(track) : -1;
828 int File::get_video_info(int track, int &pid, double &framerate,
829 int &width, int &height, char *title)
832 file->get_video_info(track, pid, framerate, width, height, title);
835 int File::select_video_stream(Asset *asset, int vstream)
838 file->select_video_stream(asset, vstream);
841 int File::select_audio_stream(Asset *asset, int astream)
844 file->select_audio_stream(asset, astream);
848 int File::get_thumbnail(int stream,
849 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
851 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
854 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
856 return file->set_skimming(track, skim, fn, vp);
859 int File::skim_video(int track, void *vp, skim_fn fn)
861 return file->skim_video(track, vp, fn);
865 int File::set_layer(int layer, int is_thread)
867 if( file && layer < asset->layers ) {
868 if( !is_thread && video_thread ) {
869 video_thread->set_layer(layer);
872 current_layer = layer;
880 int64_t File::get_audio_length()
882 int64_t result = asset->audio_length;
883 int64_t base_samplerate = -1;
885 if( base_samplerate > 0 )
886 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
894 int64_t File::get_video_length()
896 int64_t result = asset->video_length;
897 float base_framerate = -1;
899 if( base_framerate > 0 )
900 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
905 return -1; // infinity
909 int64_t File::get_video_position()
911 float base_framerate = -1;
912 if( base_framerate > 0 )
913 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
915 return current_frame;
918 int64_t File::get_audio_position()
920 // int64_t base_samplerate = -1;
921 // if( base_samplerate > 0 )
923 // if( normalized_sample_rate == base_samplerate )
924 // return normalized_sample;
926 // return (int64_t)((double)current_sample /
927 // asset->sample_rate *
932 return current_sample;
937 int File::set_audio_position(int64_t position)
941 if( !file ) return 1;
943 #define REPOSITION(x, y) \
944 (labs((x) - (y)) > 1)
946 float base_samplerate = asset->sample_rate;
947 current_sample = normalized_sample = position;
949 // printf("File::set_audio_position %d normalized_sample=%ld\n",
951 // normalized_sample);
952 result = file->set_audio_position(current_sample);
955 printf("File::set_audio_position position=%jd"
956 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
957 position, base_samplerate, asset, asset->sample_rate);
959 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
964 int File::set_video_position(int64_t position,
968 if( !file ) return 0;
970 // Convert to file's rate
971 // if( base_framerate > 0 )
972 // position = (int64_t)((double)position /
974 // asset->frame_rate +
978 if( video_thread && !is_thread ) {
979 // Call thread. Thread calls this again to set the file state.
980 video_thread->set_video_position(position);
983 if( current_frame != position ) {
985 current_frame = position;
986 result = file->set_video_position(current_frame);
993 // No resampling here.
994 int File::write_samples(Samples **buffer, int64_t len)
999 write_lock->lock("File::write_samples");
1001 // Convert to arrays for backwards compatability
1002 double *temp[asset->channels];
1003 for( int i = 0; i < asset->channels; i++ ) {
1004 temp[i] = buffer[i]->get_data();
1007 result = file->write_samples(temp, len);
1008 current_sample += len;
1009 normalized_sample += len;
1010 asset->audio_length += len;
1011 write_lock->unlock();
1020 // Can't put any cmodel abstraction here because the filebase couldn't be
1022 int File::write_frames(VFrame ***frames, int len)
1024 //printf("File::write_frames %d\n", __LINE__);
1026 // Store the counters in temps so the filebase can choose to overwrite them.
1028 int current_frame_temp = current_frame;
1029 int video_length_temp = asset->video_length;
1031 write_lock->lock("File::write_frames");
1034 result = file->write_frames(frames, len);
1037 current_frame = current_frame_temp + len;
1038 asset->video_length = video_length_temp + len;
1039 write_lock->unlock();
1044 // Only called by FileThread
1045 int File::write_compressed_frame(VFrame *buffer)
1048 write_lock->lock("File::write_compressed_frame");
1049 result = file->write_compressed_frame(buffer);
1051 asset->video_length++;
1052 write_lock->unlock();
1057 int File::write_audio_buffer(int64_t len)
1060 if( audio_thread ) {
1061 result = audio_thread->write_buffer(len);
1066 int File::write_video_buffer(int64_t len)
1069 if( video_thread ) {
1070 result = video_thread->write_buffer(len);
1076 Samples** File::get_audio_buffer()
1078 if( audio_thread ) return audio_thread->get_audio_buffer();
1082 VFrame*** File::get_video_buffer()
1084 if( video_thread ) {
1085 VFrame*** result = video_thread->get_video_buffer();
1094 int File::read_samples(Samples *samples, int64_t len)
1096 // Never try to read more samples than exist in the file
1097 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1098 len = asset->audio_length - current_sample;
1100 if( len <= 0 ) return 0;
1103 const int debug = 0;
1104 if( debug ) PRINT_TRACE
1106 if( debug ) PRINT_TRACE
1108 double *buffer = samples->get_data();
1110 int64_t base_samplerate = asset->sample_rate;
1113 // Resample recursively calls this with the asset sample rate
1114 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1116 if( debug ) PRINT_TRACE
1117 result = file->read_samples(buffer, len);
1119 if( debug ) PRINT_TRACE
1120 current_sample += len;
1122 normalized_sample += len;
1124 if( debug ) PRINT_TRACE
1129 int File::read_frame(VFrame *frame, int is_thread)
1131 const int debug = 0;
1132 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1133 // current_frame, use_cache, asset->single_frame);
1134 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1135 if( !file ) return 1;
1136 if( debug ) PRINT_TRACE
1138 int supported_colormodel = colormodel_supported(frame->get_color_model());
1139 int advance_position = 1;
1140 int cache_active = use_cache || asset->single_frame ? 1 : 0;
1141 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1144 if( cache_active && frame_cache->get_frame(frame, cache_position,
1145 current_layer, asset->frame_rate) ) {
1146 // Can't advance position if cache used.
1147 //printf("File::read_frame %d\n", __LINE__);
1148 advance_position = 0;
1151 else if( frame->get_color_model() != BC_COMPRESSED &&
1152 (supported_colormodel != frame->get_color_model() ||
1153 (!file->can_scale_input() &&
1154 (frame->get_w() != asset->width ||
1155 frame->get_h() != asset->height))) ) {
1157 // printf("File::read_frame %d\n", __LINE__);
1158 // Can't advance position here because it needs to be added to cache
1160 if( !temp_frame->params_match(asset->width, asset->height, supported_colormodel) ) {
1167 temp_frame = new VFrame(asset->width, asset->height, supported_colormodel, 0);
1170 // printf("File::read_frame %d\n", __LINE__);
1171 temp_frame->copy_stacks(frame);
1172 result = file->read_frame(temp_frame);
1174 frame->transfer_from(temp_frame);
1175 else if( result && frame->get_status() > 0 )
1176 frame->set_status(-1);
1177 //printf("File::read_frame %d\n", __LINE__);
1180 // Can't advance position here because it needs to be added to cache
1181 //printf("File::read_frame %d\n", __LINE__);
1182 result = file->read_frame(frame);
1183 if( result && frame->get_status() > 0 )
1184 frame->set_status(-1);
1185 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1188 if( result && !current_frame )
1189 frame->clear_frame();
1191 if( cache_active && advance_position && frame->get_status() > 0 )
1192 frame_cache->put_frame(frame, cache_position,
1193 current_layer, asset->frame_rate, 1, 0);
1194 //printf("File::read_frame %d\n", __LINE__);
1196 if( advance_position ) current_frame++;
1197 if( debug ) PRINT_TRACE
1201 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1203 if( asset && file ) {
1204 return asset->width == output_w &&
1205 asset->height == output_h &&
1206 file->can_copy_from(asset, position);
1211 // Fill in queries about formats when adding formats here.
1214 int File::strtoformat(const char *format)
1216 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1217 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1218 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1219 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1220 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1221 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1222 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1223 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1224 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1225 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1226 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1227 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1228 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1229 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1230 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1231 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1232 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1233 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1234 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1235 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1236 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1237 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1238 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1239 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1240 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1241 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1242 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1243 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1244 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1245 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1251 const char* File::formattostr(int format)
1254 case FILE_SCENE: return _(SCENE_NAME);
1255 case FILE_AC3: return _(AC3_NAME);
1256 case FILE_WAV: return _(WAV_NAME);
1257 case FILE_PCM: return _(PCM_NAME);
1258 case FILE_AU: return _(AU_NAME);
1259 case FILE_AIFF: return _(AIFF_NAME);
1260 case FILE_SND: return _(SND_NAME);
1261 case FILE_PNG: return _(PNG_NAME);
1262 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1263 case FILE_PPM: return _(PPM_NAME);
1264 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1265 case FILE_JPEG: return _(JPEG_NAME);
1266 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1267 case FILE_CR2: return _(CR2_NAME);
1268 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1269 case FILE_FLAC: return _(FLAC_NAME);
1270 case FILE_EXR: return _(EXR_NAME);
1271 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1272 case FILE_MPEG: return _(MPEG_NAME);
1273 case FILE_AMPEG: return _(AMPEG_NAME);
1274 case FILE_VMPEG: return _(VMPEG_NAME);
1275 case FILE_TGA: return _(TGA_NAME);
1276 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1277 case FILE_TIFF: return _(TIFF_NAME);
1278 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1279 case FILE_OGG: return _(OGG_NAME);
1280 case FILE_VORBIS: return _(VORBIS_NAME);
1281 case FILE_RAWDV: return _(RAWDV_NAME);
1282 case FILE_FFMPEG: return _(FFMPEG_NAME);
1283 case FILE_DB: return _(DBASE_NAME);
1285 return _("Unknown");
1288 int File::strtobits(const char *bits)
1290 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1291 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1292 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1293 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1294 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1295 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1296 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1297 return BITSLINEAR16;
1300 const char* File::bitstostr(int bits)
1302 //printf("File::bitstostr\n");
1304 case BITSLINEAR8: return (NAME_8BIT);
1305 case BITSLINEAR16: return (NAME_16BIT);
1306 case BITSLINEAR24: return (NAME_24BIT);
1307 case BITSLINEAR32: return (NAME_32BIT);
1308 case BITSULAW: return (NAME_ULAW);
1309 case BITS_ADPCM: return (NAME_ADPCM);
1310 case BITSFLOAT: return (NAME_FLOAT);
1312 return _("Unknown");
1317 int File::str_to_byteorder(const char *string)
1319 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1323 const char* File::byteorder_to_str(int byte_order)
1325 if( byte_order ) return _("Lo Hi");
1329 int File::bytes_per_sample(int bits)
1332 case BITSLINEAR8: return 1;
1333 case BITSLINEAR16: return 2;
1334 case BITSLINEAR24: return 3;
1335 case BITSLINEAR32: return 4;
1336 case BITSULAW: return 1;
1342 int File::get_best_colormodel(int driver, int vstream)
1344 return file ? file->get_best_colormodel(driver, vstream) :
1345 get_best_colormodel(asset, driver);
1348 int File::get_best_colormodel(Asset *asset, int driver)
1350 switch( asset->format ) {
1352 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1354 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1356 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1359 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1362 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1364 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1366 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1368 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1369 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1370 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1377 int File::colormodel_supported(int colormodel)
1380 return file->colormodel_supported(colormodel);
1386 int64_t File::file_memory_usage()
1388 return file ? file->base_memory_usage() : 0;
1391 int64_t File::get_memory_usage()
1395 result += file_memory_usage();
1396 if( temp_frame ) result += temp_frame->get_data_size();
1397 result += frame_cache->get_memory_usage();
1398 if( video_thread ) result += video_thread->get_memory_usage();
1400 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1405 int File::renders_video(int format)
1410 case FILE_JPEG_LIST:
1422 case FILE_TIFF_LIST:
1430 int File::renders_video(Asset *asset)
1432 return asset->format == FILE_FFMPEG ?
1433 FileFFMPEG::renders_video(asset->fformat) :
1434 renders_video(asset->format);
1437 int File::renders_audio(int format)
1456 int File::renders_audio(Asset *asset)
1458 return asset->format == FILE_FFMPEG ?
1459 FileFFMPEG::renders_audio(asset->fformat) :
1460 renders_audio(asset->format);
1463 int File::is_image_render(int format)
1478 const char* File::get_tag(int format)
1481 case FILE_AC3: return "ac3";
1482 case FILE_AIFF: return "aif";
1483 case FILE_AMPEG: return "mp3";
1484 case FILE_AU: return "au";
1485 case FILE_RAWDV: return "dv";
1486 case FILE_DB: return "db";
1487 case FILE_EXR: return "exr";
1488 case FILE_EXR_LIST: return "exr";
1489 case FILE_FLAC: return "flac";
1490 case FILE_JPEG: return "jpg";
1491 case FILE_JPEG_LIST: return "jpg";
1492 case FILE_OGG: return "ogg";
1493 case FILE_PCM: return "pcm";
1494 case FILE_PNG: return "png";
1495 case FILE_PNG_LIST: return "png";
1496 case FILE_PPM: return "ppm";
1497 case FILE_PPM_LIST: return "ppm";
1498 case FILE_TGA: return "tga";
1499 case FILE_TGA_LIST: return "tga";
1500 case FILE_TIFF: return "tif";
1501 case FILE_TIFF_LIST: return "tif";
1502 case FILE_VMPEG: return "m2v";
1503 case FILE_VORBIS: return "ogg";
1504 case FILE_WAV: return "wav";
1505 case FILE_FFMPEG: return "ffmpg";
1510 const char* File::get_prefix(int format)
1513 case FILE_PCM: return "PCM";
1514 case FILE_WAV: return "WAV";
1515 case FILE_PNG: return "PNG";
1516 case FILE_PPM: return "PPM";
1517 case FILE_JPEG: return "JPEG";
1518 case FILE_TIFF: return "TIFF";
1519 case FILE_GIF: return "GIF";
1520 case FILE_JPEG_LIST: return "JPEG_LIST";
1521 case FILE_AU: return "AU";
1522 case FILE_AIFF: return "AIFF";
1523 case FILE_SND: return "SND";
1524 case FILE_TGA_LIST: return "TGA_LIST";
1525 case FILE_TGA: return "TGA";
1526 case FILE_MPEG: return "MPEG";
1527 case FILE_AMPEG: return "AMPEG";
1528 case FILE_VMPEG: return "VMPEG";
1529 case FILE_RAWDV: return "RAWDV";
1530 case FILE_TIFF_LIST: return "TIFF_LIST";
1531 case FILE_PNG_LIST: return "PNG_LIST";
1532 case FILE_PPM_LIST: return "PPM_LIST";
1533 case FILE_AC3: return "AC3";
1534 case FILE_EXR: return "EXR";
1535 case FILE_EXR_LIST: return "EXR_LIST";
1536 case FILE_CR2: return "CR2";
1537 case FILE_OGG: return "OGG";
1538 case FILE_VORBIS: return "VORBIS";
1539 case FILE_FLAC: return "FLAC";
1540 case FILE_FFMPEG: return "FFMPEG";
1541 case FILE_SCENE: return "SCENE";
1542 case FILE_CR2_LIST: return "CR2_LIST";
1543 case FILE_GIF_LIST: return "GIF_LIST";
1544 case FILE_DB: return "DB";
1546 return _("UNKNOWN");
1550 PackagingEngine *File::new_packaging_engine(Asset *asset)
1552 PackagingEngine *result;
1553 switch( asset->format ) {
1555 result = (PackagingEngine*)new PackagingEngineOGG();
1558 result = (PackagingEngine*) new PackagingEngineDefault();
1566 int File::record_fd()
1568 return file ? file->record_fd() : -1;
1572 void File::get_exe_path(char *result, char *bnp)
1574 // Get executable path, basename
1575 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1578 char *ptr = strrchr(result, '/');
1579 if( ptr ) *ptr++ = 0; else ptr = result;
1580 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1588 void File::getenv_path(char *result, const char *path)
1590 char *rp = result, *ep = rp + BCTEXTLEN-1;
1591 const char *cp = path;
1592 // any env var can be used here to expand a path as:
1593 // "path...$id...": and id is a word as alpha,alnum...
1594 // expands as "path...getenv(id)..."
1595 // CIN_PATH, CIN_LIB are set in main.C,
1596 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1597 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1598 const char *bp = cp; char *p = rp;
1599 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1601 const char *envp = getenv(rp); // read env value
1602 if( !envp ) continue;
1603 while( *envp && rp < ep ) *rp++ = *envp++;
1604 cp = bp; // subst id=value
1612 void File::setenv_path(const char *var, const char *path, int overwrite)
1614 char env_path[BCTEXTLEN];
1615 getenv_path(env_path, path);
1616 setenv(var, env_path, overwrite);
1619 void File::init_cin_path()
1621 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1622 // these values are advertised for forks/shell scripts
1623 get_exe_path(env_path, env_pkg);
1624 setenv_path("CIN_PATH", env_path, 1);
1625 setenv_path("CIN_PKG", env_pkg, 1);
1626 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1627 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1628 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1629 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1630 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1631 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1632 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);