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
57 #include "filescene.h"
58 #include "filesndfile.h"
60 #include "filethread.h"
63 #include "formatwindow.h"
64 #include "formattools.h"
65 #include "framecache.h"
67 #include "mainprogress.inc"
70 #include "packagingengine.h"
71 #include "pluginserver.h"
72 #include "preferences.h"
73 #include "probeprefs.h"
81 format_completion = new Condition(1, "File::format_completion");
82 write_lock = new Condition(1, "File::write_lock");
83 frame_cache = new FrameCache;
89 if( getting_options ) {
90 if( format_window ) format_window->set_done(0);
91 format_completion->lock("File::~File");
92 format_completion->unlock();
96 //printf("File::~File %d temp_debug=%d\n", __LINE__, --temp_debug);
102 asset->Garbage::remove_user();
103 delete format_completion;
108 void File::reset_parameters()
121 normalized_sample = 0;
124 playback_subtitle = -1;
128 temp_samples_buffer = 0;
129 temp_frame_buffer = 0;
130 current_frame_buffer = 0;
131 audio_ring_buffers = 0;
132 video_ring_buffers = 0;
133 video_buffer_size = 0;
136 int File::raise_window()
138 if( getting_options && format_window ) {
139 format_window->raise_window();
140 format_window->flush();
145 void File::close_window()
147 if( getting_options ) {
148 format_window->lock_window("File::close_window");
149 format_window->set_done(1);
150 format_window->unlock_window();
155 int File::get_options(FormatTools *format,
156 int audio_options, int video_options)
158 BC_WindowBase *parent_window = format->window;
159 Asset *asset = format->asset;
160 EDL *edl = format->mwindow ? format->mwindow->edl : 0;
163 format_completion->lock("File::get_options");
164 switch( asset->format ) {
165 #ifdef HAVE_CIN_3RDPARTY
166 case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
167 audio_options, video_options, edl);
172 FileDV::get_parameters(parent_window, asset, format_window,
173 audio_options, video_options, edl);
181 FileSndFile::get_parameters(parent_window, asset, format_window,
182 audio_options, video_options, edl);
185 FileFFMPEG::get_parameters(parent_window, asset, format_window,
186 audio_options, video_options, edl);
191 FileMPEG::get_parameters(parent_window, asset, format_window,
192 audio_options, video_options, edl);
197 FileJPEG::get_parameters(parent_window, asset, format_window,
198 audio_options, video_options, edl);
203 FileEXR::get_parameters(parent_window, asset, format_window,
204 audio_options, video_options, edl);
208 FileFLAC::get_parameters(parent_window, asset, format_window,
209 audio_options, video_options, edl);
213 FilePNG::get_parameters(parent_window, asset, format_window,
214 audio_options, video_options, edl);
218 FilePPM::get_parameters(parent_window, asset, format_window,
219 audio_options, video_options, edl);
223 FileTGA::get_parameters(parent_window, asset, format_window,
224 audio_options, video_options, edl);
228 FileTIFF::get_parameters(parent_window, asset, format_window,
229 audio_options, video_options, edl);
235 if( !format_window ) {
236 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
237 parent_window->get_abs_cursor_x(1),
238 parent_window->get_abs_cursor_y(1));
239 format_window = errorbox;
242 errorbox->create_objects(_("This format doesn't support audio."));
245 errorbox->create_objects(_("This format doesn't support video."));
246 errorbox->run_window();
252 format_completion->unlock();
256 int File::can_scale_input(Asset *asset)
258 switch( asset->format ) {
268 if( asset->video_length < 0 )
275 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
277 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
278 cpus = 8; // 8 cpus ought to decode just about anything
279 // Set all instances so gets work.
285 int File::set_preload(int64_t size)
287 this->playback_preload = size;
291 void File::set_subtitle(int value)
293 this->playback_subtitle = value;
294 if( file ) file->set_subtitle(value);
297 void File::set_interpolate_raw(int value)
299 this->interpolate_raw = value;
302 void File::set_white_balance_raw(int value)
304 this->white_balance_raw = value;
307 void File::set_cache_frames(int value)
309 // caching only done locally
314 int File::purge_cache()
316 // caching only done locally
318 if( frame_cache->cache_items() > 0 ) {
319 frame_cache->remove_all();
325 int File::delete_oldest()
327 // return the number of bytes freed
328 return frame_cache->delete_oldest();
331 // file driver in order of probe precidence
332 // can be reordered in preferences->interface
333 const char *File::default_probes[] = {
356 const int File::nb_probes =
357 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
362 FILE *fp = fopen(this->asset->path, "rb");
363 if( !fp ) return FILE_NOT_FOUND;
365 memset(data,0,sizeof(data));
366 int ret = fread(data, 1, 16, fp);
368 if( !ret ) return FILE_NOT_FOUND;
370 for( int i=0; i<preferences->file_probes.size(); ++i ) {
371 ProbePref *pref = preferences->file_probes[i];
372 if( !pref->armed ) continue;
373 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
374 if( !FileFFMPEG::check_sig(this->asset) ) continue;
375 file = new FileFFMPEG(this->asset, this);
378 #ifdef HAVE_COMMERCIAL
379 if( !strcmp(pref->name,"DB") ) { // MediaDB
380 if( !FileDB::check_sig(this->asset) ) continue;
381 file = new FileDB(this->asset, this);
385 if( !strcmp(pref->name,"Scene") ) { // scene file
386 if( !FileScene::check_sig(this->asset, data)) continue;
387 file = new FileScene(this->asset, this);
391 if( !strcmp(pref->name,"DV") ) { // libdv
392 if( !FileDV::check_sig(this->asset) ) continue;
393 file = new FileDV(this->asset, this);
397 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
398 if( !FileSndFile::check_sig(this->asset) ) continue;
399 file = new FileSndFile(this->asset, this);
402 if( !strcmp(pref->name,"PNG") ) { // PNG file
403 if( !FilePNG::check_sig(this->asset) ) continue;
404 file = new FilePNG(this->asset, this);
407 if( !strcmp(pref->name,"PPM") ) { // PPM file
408 if( !FilePPM::check_sig(this->asset) ) continue;
409 file = new FilePPM(this->asset, this);
412 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
413 if( !FileJPEG::check_sig(this->asset) ) continue;
414 file = new FileJPEG(this->asset, this);
417 if( !strcmp(pref->name,"GIF") ) { // GIF file
418 if( FileGIFList::check_sig(this->asset) )
419 file = new FileGIFList(this->asset, this);
420 else if( FileGIF::check_sig(this->asset) )
421 file = new FileGIF(this->asset, this);
426 if( !strcmp(pref->name,"EXR") ) { // EXR file
427 if( !FileEXR::check_sig(this->asset, data)) continue;
428 file = new FileEXR(this->asset, this);
432 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
433 if( !FileFLAC::check_sig(this->asset, data)) continue;
434 file = new FileFLAC(this->asset, this);
437 if( !strcmp(pref->name,"CR2") ) { // CR2 file
438 if( !FileCR2::check_sig(this->asset)) continue;
439 file = new FileCR2(this->asset, this);
442 if( !strcmp(pref->name,"TGA") ) { // TGA file
443 if( !FileTGA::check_sig(this->asset) ) continue;
444 file = new FileTGA(this->asset, this);
447 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
448 if( !FileTIFF::check_sig(this->asset) ) continue;
449 file = new FileTIFF(this->asset, this);
453 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
454 if( !FileMPEG::check_sig(this->asset) ) continue;
455 file = new FileMPEG(this->asset, this);
459 if( !strcmp(pref->name,"EDL") ) { // XML file
460 if( data[0] != '<' ) continue;
461 if( !strncmp(&data[1],"EDL>",4) ||
462 !strncmp(&data[1],"HTAL>",5) ||
463 !strncmp(&data[1],"?xml",4) )
467 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
470 int File::open_file(Preferences *preferences,
477 this->preferences = preferences;
478 this->asset->copy_from(asset, 1);
483 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
485 switch( this->asset->format ) {
486 // get the format now
487 // If you add another format to case 0, you also need to add another case for the
488 // file format #define.
491 if( ret != FILE_OK ) return ret;
493 // format already determined
494 #ifdef HAVE_CIN_3RDPARTY
496 file = new FileAC3(this->asset, this);
500 file = new FileScene(this->asset, this);
504 file = new FileFFMPEG(this->asset, this);
512 //printf("File::open_file 1\n");
513 file = new FileSndFile(this->asset, this);
518 file = new FilePNG(this->asset, this);
523 file = new FilePPM(this->asset, this);
528 file = new FileJPEG(this->asset, this);
532 file = new FileGIF(this->asset, this);
535 file = new FileGIFList(this->asset, this);
541 file = new FileEXR(this->asset, this);
545 file = new FileFLAC(this->asset, this);
550 file = new FileCR2(this->asset, this);
555 file = new FileTGA(this->asset, this);
560 file = new FileTIFF(this->asset, this);
562 #ifdef HAVE_COMMERCIAL
564 file = new FileDB(this->asset, this);
572 file = new FileMPEG(this->asset, this);
577 file = new FileDV(this->asset, this);
587 // Reopen file with correct parser and get header.
588 if( file->open_file(rd, wr) ) {
589 delete file; file = 0;
590 return FILE_NOT_FOUND;
595 // Set extra writing parameters to mandatory settings.
597 if( this->asset->dither ) file->set_dither();
601 // one frame image file, not brender, no specific length
602 if( !this->asset->audio_data && this->asset->use_header &&
603 this->asset->video_data && !this->asset->single_frame &&
604 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
605 this->asset->single_frame = 1;
606 this->asset->video_length = -1;
610 // Synchronize header parameters
611 asset->copy_from(this->asset, 1);
614 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
620 void File::delete_temp_samples_buffer()
623 if( temp_samples_buffer ) {
624 for( int j = 0; j < audio_ring_buffers; j++ ) {
625 for( int i = 0; i < asset->channels; i++ ) {
626 delete temp_samples_buffer[j][i];
628 delete [] temp_samples_buffer[j];
631 delete [] temp_samples_buffer;
632 temp_samples_buffer = 0;
633 audio_ring_buffers = 0;
637 void File::delete_temp_frame_buffer()
640 if( temp_frame_buffer ) {
641 for( int k = 0; k < video_ring_buffers; k++ ) {
642 for( int i = 0; i < asset->layers; i++ ) {
643 for( int j = 0; j < video_buffer_size; j++ ) {
644 delete temp_frame_buffer[k][i][j];
646 delete [] temp_frame_buffer[k][i];
648 delete [] temp_frame_buffer[k];
651 delete [] temp_frame_buffer;
652 temp_frame_buffer = 0;
653 video_ring_buffers = 0;
654 video_buffer_size = 0;
658 int File::close_file(int ignore_thread)
660 if( !ignore_thread ) {
665 // The file's asset is a copy of the argument passed to open_file so the
666 // user must copy lengths from the file's asset.
668 asset->audio_length = current_sample;
669 asset->video_length = current_frame;
676 delete_temp_samples_buffer();
677 delete_temp_frame_buffer();
682 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
684 return !file ? -1 : file->get_index(index_file, progress_bar);
687 int File::start_audio_thread(int buffer_size, int ring_buffers)
689 this->audio_ring_buffers = ring_buffers;
692 if( !audio_thread ) {
693 audio_thread = new FileThread(this, 1, 0);
694 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
699 int File::start_video_thread(int buffer_size, int color_model,
700 int ring_buffers, int compressed)
702 this->video_ring_buffers = ring_buffers;
703 this->video_buffer_size = buffer_size;
705 if( !video_thread ) {
706 video_thread = new FileThread(this, 0, 1);
707 video_thread->start_writing(buffer_size, color_model,
708 ring_buffers, compressed);
713 int File::start_video_decode_thread()
715 // Currently, CR2 is the only one which won't work asynchronously, so
716 // we're not using a virtual function yet.
717 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
718 video_thread = new FileThread(this, 0, 1);
719 video_thread->start_reading();
725 int File::stop_audio_thread()
728 audio_thread->stop_writing();
735 int File::stop_video_thread()
738 video_thread->stop_reading();
739 video_thread->stop_writing();
746 FileThread* File::get_video_thread()
751 int File::set_channel(int channel)
753 if( file && channel < asset->channels ) {
754 current_channel = channel;
761 int File::get_channel()
763 return current_channel;
766 // if no>=0, sets new program
767 // returns current program
768 int File::set_program(int no)
770 int result = file ? file->set_program(no) : current_program;
771 current_program = no < 0 ? result : no;
775 int File::get_cell_time(int no, double &time)
777 return file ? file->get_cell_time(no, time) : -1;
780 int File::get_system_time(int64_t &tm)
782 return file ? file->get_system_time(tm) : -1;
785 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
787 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
790 int File::get_video_pid(int track)
792 return file ? file->get_video_pid(track) : -1;
797 int File::get_video_info(int track, int &pid, double &framerate,
798 int &width, int &height, char *title)
801 file->get_video_info(track, pid, framerate, width, height, title);
804 int File::select_video_stream(Asset *asset, int vstream)
807 file->select_video_stream(asset, vstream);
810 int File::select_audio_stream(Asset *asset, int astream)
813 file->select_audio_stream(asset, astream);
817 int File::get_thumbnail(int stream,
818 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
820 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
823 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
825 return file->set_skimming(track, skim, fn, vp);
828 int File::skim_video(int track, void *vp, skim_fn fn)
830 return file->skim_video(track, vp, fn);
834 int File::set_layer(int layer, int is_thread)
836 if( file && layer < asset->layers ) {
837 if( !is_thread && video_thread ) {
838 video_thread->set_layer(layer);
841 current_layer = layer;
849 int64_t File::get_audio_length()
851 int64_t result = asset->audio_length;
852 int64_t base_samplerate = -1;
854 if( base_samplerate > 0 )
855 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
863 int64_t File::get_video_length()
865 int64_t result = asset->video_length;
866 float base_framerate = -1;
868 if( base_framerate > 0 )
869 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
874 return -1; // infinity
878 int64_t File::get_video_position()
880 float base_framerate = -1;
881 if( base_framerate > 0 )
882 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
884 return current_frame;
887 int64_t File::get_audio_position()
889 // int64_t base_samplerate = -1;
890 // if( base_samplerate > 0 )
892 // if( normalized_sample_rate == base_samplerate )
893 // return normalized_sample;
895 // return (int64_t)((double)current_sample /
896 // asset->sample_rate *
901 return current_sample;
906 int File::set_audio_position(int64_t position)
910 if( !file ) return 1;
912 #define REPOSITION(x, y) \
913 (labs((x) - (y)) > 1)
915 float base_samplerate = asset->sample_rate;
916 current_sample = normalized_sample = position;
918 // printf("File::set_audio_position %d normalized_sample=%ld\n",
920 // normalized_sample);
921 result = file->set_audio_position(current_sample);
924 printf("File::set_audio_position position=%jd"
925 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
926 position, base_samplerate, asset, asset->sample_rate);
928 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
933 int File::set_video_position(int64_t position,
937 if( !file ) return 0;
939 // Convert to file's rate
940 // if( base_framerate > 0 )
941 // position = (int64_t)((double)position /
943 // asset->frame_rate +
947 if( video_thread && !is_thread ) {
948 // Call thread. Thread calls this again to set the file state.
949 video_thread->set_video_position(position);
952 if( current_frame != position ) {
954 current_frame = position;
955 result = file->set_video_position(current_frame);
962 // No resampling here.
963 int File::write_samples(Samples **buffer, int64_t len)
968 write_lock->lock("File::write_samples");
970 // Convert to arrays for backwards compatability
971 double *temp[asset->channels];
972 for( int i = 0; i < asset->channels; i++ ) {
973 temp[i] = buffer[i]->get_data();
976 result = file->write_samples(temp, len);
977 current_sample += len;
978 normalized_sample += len;
979 asset->audio_length += len;
980 write_lock->unlock();
989 // Can't put any cmodel abstraction here because the filebase couldn't be
991 int File::write_frames(VFrame ***frames, int len)
993 //printf("File::write_frames %d\n", __LINE__);
995 // Store the counters in temps so the filebase can choose to overwrite them.
997 int current_frame_temp = current_frame;
998 int video_length_temp = asset->video_length;
1000 write_lock->lock("File::write_frames");
1003 result = file->write_frames(frames, len);
1006 current_frame = current_frame_temp + len;
1007 asset->video_length = video_length_temp + len;
1008 write_lock->unlock();
1013 // Only called by FileThread
1014 int File::write_compressed_frame(VFrame *buffer)
1017 write_lock->lock("File::write_compressed_frame");
1018 result = file->write_compressed_frame(buffer);
1020 asset->video_length++;
1021 write_lock->unlock();
1026 int File::write_audio_buffer(int64_t len)
1029 if( audio_thread ) {
1030 result = audio_thread->write_buffer(len);
1035 int File::write_video_buffer(int64_t len)
1038 if( video_thread ) {
1039 result = video_thread->write_buffer(len);
1045 Samples** File::get_audio_buffer()
1047 if( audio_thread ) return audio_thread->get_audio_buffer();
1051 VFrame*** File::get_video_buffer()
1053 if( video_thread ) {
1054 VFrame*** result = video_thread->get_video_buffer();
1063 int File::read_samples(Samples *samples, int64_t len)
1065 // Never try to read more samples than exist in the file
1066 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1067 len = asset->audio_length - current_sample;
1069 if( len <= 0 ) return 0;
1072 const int debug = 0;
1073 if( debug ) PRINT_TRACE
1075 if( debug ) PRINT_TRACE
1077 double *buffer = samples->get_data();
1079 int64_t base_samplerate = asset->sample_rate;
1082 // Resample recursively calls this with the asset sample rate
1083 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1085 if( debug ) PRINT_TRACE
1086 result = file->read_samples(buffer, len);
1088 if( debug ) PRINT_TRACE
1089 current_sample += len;
1091 normalized_sample += len;
1093 if( debug ) PRINT_TRACE
1098 int File::read_frame(VFrame *frame, int is_thread)
1100 const int debug = 0;
1101 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1102 // current_frame, use_cache, asset->single_frame);
1103 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1104 if( !file ) return 1;
1105 if( debug ) PRINT_TRACE
1107 int supported_colormodel = colormodel_supported(frame->get_color_model());
1108 int advance_position = 1;
1109 int cache_active = use_cache || asset->single_frame ? 1 : 0;
1110 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1113 if( cache_active && frame_cache->get_frame(frame, cache_position,
1114 current_layer, asset->frame_rate) ) {
1115 // Can't advance position if cache used.
1116 //printf("File::read_frame %d\n", __LINE__);
1117 advance_position = 0;
1120 else if( frame->get_color_model() != BC_COMPRESSED &&
1121 (supported_colormodel != frame->get_color_model() ||
1122 (!file->can_scale_input() &&
1123 (frame->get_w() != asset->width ||
1124 frame->get_h() != asset->height))) ) {
1126 // printf("File::read_frame %d\n", __LINE__);
1127 // Can't advance position here because it needs to be added to cache
1129 if( !temp_frame->params_match(asset->width, asset->height, supported_colormodel) ) {
1136 temp_frame = new VFrame(asset->width, asset->height, supported_colormodel, 0);
1137 temp_frame->clear_frame();
1140 // printf("File::read_frame %d\n", __LINE__);
1141 temp_frame->copy_stacks(frame);
1142 result = file->read_frame(temp_frame);
1144 frame->transfer_from(temp_frame);
1145 else if( result && frame->get_status() > 0 )
1146 frame->set_status(-1);
1147 //printf("File::read_frame %d\n", __LINE__);
1150 // Can't advance position here because it needs to be added to cache
1151 //printf("File::read_frame %d\n", __LINE__);
1152 result = file->read_frame(frame);
1153 if( result && frame->get_status() > 0 )
1154 frame->set_status(-1);
1155 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1158 if( result && !current_frame )
1159 frame->clear_frame();
1161 if( cache_active && advance_position && frame->get_status() > 0 )
1162 frame_cache->put_frame(frame, cache_position,
1163 current_layer, asset->frame_rate, 1, 0);
1164 //printf("File::read_frame %d\n", __LINE__);
1166 if( advance_position ) current_frame++;
1167 if( debug ) PRINT_TRACE
1171 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1173 if( asset && file ) {
1174 return asset->width == output_w &&
1175 asset->height == output_h &&
1176 file->can_copy_from(asset, position);
1181 // Fill in queries about formats when adding formats here.
1184 int File::strtoformat(const char *format)
1186 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1187 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1188 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1189 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1190 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1191 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1192 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1193 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1194 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1195 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1196 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1197 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1198 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1199 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1200 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1201 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1202 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1203 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1204 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1205 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1206 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1207 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1208 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1209 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1210 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1211 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1212 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1213 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1214 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1215 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1221 const char* File::formattostr(int format)
1224 case FILE_SCENE: return _(SCENE_NAME);
1225 case FILE_AC3: return _(AC3_NAME);
1226 case FILE_WAV: return _(WAV_NAME);
1227 case FILE_PCM: return _(PCM_NAME);
1228 case FILE_AU: return _(AU_NAME);
1229 case FILE_AIFF: return _(AIFF_NAME);
1230 case FILE_SND: return _(SND_NAME);
1231 case FILE_PNG: return _(PNG_NAME);
1232 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1233 case FILE_PPM: return _(PPM_NAME);
1234 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1235 case FILE_JPEG: return _(JPEG_NAME);
1236 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1237 case FILE_CR2: return _(CR2_NAME);
1238 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1239 case FILE_FLAC: return _(FLAC_NAME);
1240 case FILE_GIF: return _(GIF_NAME);
1241 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1242 case FILE_EXR: return _(EXR_NAME);
1243 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1244 #ifdef HAVE_LIBZMPEG
1245 case FILE_MPEG: return _(MPEG_NAME);
1247 case FILE_AMPEG: return _(AMPEG_NAME);
1248 case FILE_VMPEG: return _(VMPEG_NAME);
1249 case FILE_TGA: return _(TGA_NAME);
1250 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1251 case FILE_TIFF: return _(TIFF_NAME);
1252 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1253 case FILE_RAWDV: return _(RAWDV_NAME);
1254 case FILE_FFMPEG: return _(FFMPEG_NAME);
1255 case FILE_DB: return _(DBASE_NAME);
1257 return _("Unknown");
1260 int File::strtobits(const char *bits)
1262 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1263 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1264 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1265 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1266 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1267 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1268 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1269 return BITSLINEAR16;
1272 const char* File::bitstostr(int bits)
1274 //printf("File::bitstostr\n");
1276 case BITSLINEAR8: return (NAME_8BIT);
1277 case BITSLINEAR16: return (NAME_16BIT);
1278 case BITSLINEAR24: return (NAME_24BIT);
1279 case BITSLINEAR32: return (NAME_32BIT);
1280 case BITSULAW: return (NAME_ULAW);
1281 case BITS_ADPCM: return (NAME_ADPCM);
1282 case BITSFLOAT: return (NAME_FLOAT);
1284 return _("Unknown");
1289 int File::str_to_byteorder(const char *string)
1291 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1295 const char* File::byteorder_to_str(int byte_order)
1297 if( byte_order ) return _("Lo Hi");
1301 int File::bytes_per_sample(int bits)
1304 case BITSLINEAR8: return 1;
1305 case BITSLINEAR16: return 2;
1306 case BITSLINEAR24: return 3;
1307 case BITSLINEAR32: return 4;
1308 case BITSULAW: return 1;
1314 int File::get_best_colormodel(int driver, int vstream)
1316 return file ? file->get_best_colormodel(driver, vstream) :
1317 get_best_colormodel(asset, driver);
1320 int File::get_best_colormodel(Asset *asset, int driver)
1322 switch( asset->format ) {
1324 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1326 #ifdef HAVE_LIBZMPEG
1327 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1330 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1333 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1336 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1338 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1340 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1342 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1343 #ifdef HAVE_COMMERCIAL
1344 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1346 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1353 int File::colormodel_supported(int colormodel)
1356 return file->colormodel_supported(colormodel);
1362 int64_t File::file_memory_usage()
1364 return file ? file->base_memory_usage() : 0;
1367 int64_t File::get_memory_usage()
1371 result += file_memory_usage();
1372 if( temp_frame ) result += temp_frame->get_data_size();
1373 result += frame_cache->get_memory_usage();
1374 if( video_thread ) result += video_thread->get_memory_usage();
1376 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1381 int File::renders_video(int format)
1385 case FILE_JPEG_LIST:
1399 case FILE_TIFF_LIST:
1407 int File::renders_video(Asset *asset)
1409 return asset->format == FILE_FFMPEG ?
1410 FFMPEG::renders_video(asset->fformat) :
1411 renders_video(asset->format);
1414 int File::renders_audio(int format)
1431 int File::renders_audio(Asset *asset)
1433 return asset->format == FILE_FFMPEG ?
1434 FFMPEG::renders_audio(asset->fformat) :
1435 renders_audio(asset->format);
1438 int File::is_image_render(int format)
1453 const char* File::get_tag(int format)
1456 case FILE_AC3: return "ac3";
1457 case FILE_AIFF: return "aif";
1458 case FILE_AMPEG: return "mp3";
1459 case FILE_AU: return "au";
1460 case FILE_RAWDV: return "dv";
1461 case FILE_DB: return "db";
1462 case FILE_EXR: return "exr";
1463 case FILE_EXR_LIST: return "exrs";
1464 case FILE_FLAC: return "flac";
1465 case FILE_JPEG: return "jpg";
1466 case FILE_JPEG_LIST: return "jpgs";
1467 case FILE_GIF: return "gif";
1468 case FILE_GIF_LIST: return "gifs";
1469 case FILE_PCM: return "pcm";
1470 case FILE_PNG: return "png";
1471 case FILE_PNG_LIST: return "pngs";
1472 case FILE_PPM: return "ppm";
1473 case FILE_PPM_LIST: return "ppms";
1474 case FILE_TGA: return "tga";
1475 case FILE_TGA_LIST: return "tgas";
1476 case FILE_TIFF: return "tif";
1477 case FILE_TIFF_LIST: return "tifs";
1478 case FILE_VMPEG: return "m2v";
1479 case FILE_WAV: return "wav";
1480 case FILE_FFMPEG: return "ffmpg";
1485 const char* File::get_prefix(int format)
1488 case FILE_PCM: return "PCM";
1489 case FILE_WAV: return "WAV";
1490 case FILE_PNG: return "PNG";
1491 case FILE_PPM: return "PPM";
1492 case FILE_JPEG: return "JPEG";
1493 case FILE_TIFF: return "TIFF";
1494 case FILE_GIF: return "GIF";
1495 case FILE_JPEG_LIST: return "JPEG_LIST";
1496 case FILE_AU: return "AU";
1497 case FILE_AIFF: return "AIFF";
1498 case FILE_SND: return "SND";
1499 case FILE_TGA_LIST: return "TGA_LIST";
1500 case FILE_TGA: return "TGA";
1501 case FILE_MPEG: return "MPEG";
1502 case FILE_AMPEG: return "AMPEG";
1503 case FILE_VMPEG: return "VMPEG";
1504 case FILE_RAWDV: return "RAWDV";
1505 case FILE_TIFF_LIST: return "TIFF_LIST";
1506 case FILE_PNG_LIST: return "PNG_LIST";
1507 case FILE_PPM_LIST: return "PPM_LIST";
1508 case FILE_AC3: return "AC3";
1509 case FILE_EXR: return "EXR";
1510 case FILE_EXR_LIST: return "EXR_LIST";
1511 case FILE_CR2: return "CR2";
1512 case FILE_FLAC: return "FLAC";
1513 case FILE_FFMPEG: return "FFMPEG";
1514 case FILE_SCENE: return "SCENE";
1515 case FILE_CR2_LIST: return "CR2_LIST";
1516 case FILE_GIF_LIST: return "GIF_LIST";
1517 case FILE_DB: return "DB";
1519 return _("UNKNOWN");
1523 PackagingEngine *File::new_packaging_engine(Asset *asset)
1525 PackagingEngine *result = (PackagingEngine*) new PackagingEngineDefault();
1530 int File::record_fd()
1532 return file ? file->record_fd() : -1;
1536 void File::get_exe_path(char *result, char *bnp)
1538 // Get executable path, basename
1539 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1542 char *ptr = strrchr(result, '/');
1543 if( ptr ) *ptr++ = 0; else ptr = result;
1544 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1552 void File::getenv_path(char *result, const char *path)
1554 char *rp = result, *ep = rp + BCTEXTLEN-1;
1555 const char *cp = path;
1556 // any env var can be used here to expand a path as:
1557 // "path...$id...": and id is a word as alpha,alnum...
1558 // expands as "path...getenv(id)..."
1559 // CIN_PATH, CIN_LIB are set in main.C,
1560 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1561 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1562 const char *bp = cp; char *p = rp;
1563 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1565 const char *envp = getenv(rp); // read env value
1566 if( !envp ) continue;
1567 while( *envp && rp < ep ) *rp++ = *envp++;
1568 cp = bp; // subst id=value
1576 void File::setenv_path(const char *var, const char *path, int overwrite)
1578 char env_path[BCTEXTLEN];
1579 getenv_path(env_path, path);
1580 setenv(var, env_path, overwrite);
1583 void File::init_cin_path()
1585 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1586 // these values are advertised for forks/shell scripts
1587 get_exe_path(env_path, env_pkg);
1588 setenv_path("CIN_PATH", env_path, 1);
1589 setenv_path("CIN_PKG", env_pkg, 1);
1590 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1591 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1592 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1593 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1594 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1595 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1596 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);