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 Asset *asset = format->asset;
169 EDL *edl = format->mwindow ? format->mwindow->edl : 0;
172 format_completion->lock("File::get_options");
173 switch( asset->format ) {
174 #ifdef HAVE_CIN_3RDPARTY
175 case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
176 audio_options, video_options, edl);
181 FileDV::get_parameters(parent_window, asset, format_window,
182 audio_options, video_options, edl);
190 FileSndFile::get_parameters(parent_window, asset, format_window,
191 audio_options, video_options, edl);
194 FileFFMPEG::get_parameters(parent_window, asset, format_window,
195 audio_options, video_options, edl);
200 FileMPEG::get_parameters(parent_window, asset, format_window,
201 audio_options, video_options, edl);
206 FileJPEG::get_parameters(parent_window, asset, format_window,
207 audio_options, video_options, edl);
212 FileEXR::get_parameters(parent_window, asset, format_window,
213 audio_options, video_options, edl);
217 FileFLAC::get_parameters(parent_window, asset, format_window,
218 audio_options, video_options, edl);
222 FilePNG::get_parameters(parent_window, asset, format_window,
223 audio_options, video_options, edl);
227 FilePPM::get_parameters(parent_window, asset, format_window,
228 audio_options, video_options, edl);
232 FileTGA::get_parameters(parent_window, asset, format_window,
233 audio_options, video_options, edl);
237 FileTIFF::get_parameters(parent_window, asset, format_window,
238 audio_options, video_options, edl);
241 FileOGG::get_parameters(parent_window, asset, format_window,
242 audio_options, video_options, edl);
248 if( !format_window ) {
249 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
250 parent_window->get_abs_cursor_x(1),
251 parent_window->get_abs_cursor_y(1));
252 format_window = errorbox;
255 errorbox->create_objects(_("This format doesn't support audio."));
258 errorbox->create_objects(_("This format doesn't support video."));
259 errorbox->run_window();
265 format_completion->unlock();
269 int File::can_scale_input(Asset *asset)
271 switch( asset->format ) {
281 if( asset->video_length < 0 )
288 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
290 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
291 cpus = 8; // 8 cpus ought to decode just about anything
292 // Set all instances so gets work.
298 int File::set_preload(int64_t size)
300 this->playback_preload = size;
304 void File::set_subtitle(int value)
306 this->playback_subtitle = value;
307 if( file ) file->set_subtitle(value);
310 void File::set_interpolate_raw(int value)
312 this->interpolate_raw = value;
315 void File::set_white_balance_raw(int value)
317 this->white_balance_raw = value;
320 void File::set_cache_frames(int value)
322 // caching only done locally
327 int File::purge_cache()
329 // caching only done locally
331 if( frame_cache->cache_items() > 0 ) {
332 frame_cache->remove_all();
338 int File::delete_oldest()
340 // return the number of bytes freed
341 return frame_cache->delete_oldest();
344 // file driver in order of probe precidence
345 // can be reordered in preferences->interface
346 const char *File::default_probes[] = {
371 const int File::nb_probes =
372 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
377 FILE *fp = fopen(this->asset->path, "rb");
378 if( !fp ) return FILE_NOT_FOUND;
380 memset(data,0,sizeof(data));
381 int ret = fread(data, 1, 16, fp);
383 if( !ret ) return FILE_NOT_FOUND;
385 for( int i=0; i<preferences->file_probes.size(); ++i ) {
386 ProbePref *pref = preferences->file_probes[i];
387 if( !pref->armed ) continue;
388 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
389 if( !FileFFMPEG::check_sig(this->asset) ) continue;
390 file = new FileFFMPEG(this->asset, this);
393 #ifdef HAVE_COMMERCIAL
394 if( !strcmp(pref->name,"DB") ) { // MediaDB
395 if( !FileDB::check_sig(this->asset) ) continue;
396 file = new FileDB(this->asset, this);
400 if( !strcmp(pref->name,"Scene") ) { // scene file
401 if( !FileScene::check_sig(this->asset, data)) continue;
402 file = new FileScene(this->asset, this);
406 if( !strcmp(pref->name,"DV") ) { // libdv
407 if( !FileDV::check_sig(this->asset) ) continue;
408 file = new FileDV(this->asset, this);
412 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
413 if( !FileSndFile::check_sig(this->asset) ) continue;
414 file = new FileSndFile(this->asset, this);
417 if( !strcmp(pref->name,"PNG") ) { // PNG file
418 if( !FilePNG::check_sig(this->asset) ) continue;
419 file = new FilePNG(this->asset, this);
422 if( !strcmp(pref->name,"PPM") ) { // PPM file
423 if( !FilePPM::check_sig(this->asset) ) continue;
424 file = new FilePPM(this->asset, this);
427 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
428 if( !FileJPEG::check_sig(this->asset) ) continue;
429 file = new FileJPEG(this->asset, this);
432 if( !strcmp(pref->name,"GIF") ) { // GIF file
433 if( !FileGIF::check_sig(this->asset)) continue;
434 file = new FileGIF(this->asset, this);
438 if( !strcmp(pref->name,"EXR") ) { // EXR file
439 if( !FileEXR::check_sig(this->asset, data)) continue;
440 file = new FileEXR(this->asset, this);
444 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
445 if( !FileFLAC::check_sig(this->asset, data)) continue;
446 file = new FileFLAC(this->asset, this);
449 if( !strcmp(pref->name,"CR2") ) { // CR2 file
450 if( !FileCR2::check_sig(this->asset)) continue;
451 file = new FileCR2(this->asset, this);
454 if( !strcmp(pref->name,"TGA") ) { // TGA file
455 if( !FileTGA::check_sig(this->asset) ) continue;
456 file = new FileTGA(this->asset, this);
459 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
460 if( !FileTIFF::check_sig(this->asset) ) continue;
461 file = new FileTIFF(this->asset, this);
464 if( !strcmp(pref->name,"OGG") ) { // OGG file
465 if( !FileOGG::check_sig(this->asset) ) continue;
466 file = new FileOGG(this->asset, this);
469 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
470 if( !FileVorbis::check_sig(this->asset) ) continue;
471 file = new FileVorbis(this->asset, this);
475 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
476 if( !FileMPEG::check_sig(this->asset) ) continue;
477 file = new FileMPEG(this->asset, this);
481 if( !strcmp(pref->name,"EDL") ) { // XML file
482 if( data[0] != '<' ) continue;
483 if( !strncmp(&data[1],"EDL>",4) ||
484 !strncmp(&data[1],"HTAL>",5) ||
485 !strncmp(&data[1],"?xml",4) )
489 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
492 int File::open_file(Preferences *preferences,
499 this->preferences = preferences;
500 this->asset->copy_from(asset, 1);
505 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
507 switch( this->asset->format ) {
508 // get the format now
509 // If you add another format to case 0, you also need to add another case for the
510 // file format #define.
513 if( ret != FILE_OK ) return ret;
515 // format already determined
516 #ifdef HAVE_CIN_3RDPARTY
518 file = new FileAC3(this->asset, this);
522 file = new FileScene(this->asset, this);
526 file = new FileFFMPEG(this->asset, this);
534 //printf("File::open_file 1\n");
535 file = new FileSndFile(this->asset, this);
540 file = new FilePNG(this->asset, this);
545 file = new FilePPM(this->asset, this);
550 file = new FileJPEG(this->asset, this);
555 file = new FileGIF(this->asset, this);
560 file = new FileEXR(this->asset, this);
564 file = new FileFLAC(this->asset, this);
569 file = new FileCR2(this->asset, this);
574 file = new FileTGA(this->asset, this);
579 file = new FileTIFF(this->asset, this);
581 #ifdef HAVE_COMMERCIAL
583 file = new FileDB(this->asset, this);
591 file = new FileMPEG(this->asset, this);
595 file = new FileOGG(this->asset, this);
599 file = new FileVorbis(this->asset, this);
603 file = new FileDV(this->asset, this);
613 // Reopen file with correct parser and get header.
614 if( file->open_file(rd, wr) ) {
615 delete file; file = 0;
616 return FILE_NOT_FOUND;
621 // Set extra writing parameters to mandatory settings.
623 if( this->asset->dither ) file->set_dither();
627 // one frame image file, not brender, no specific length
628 if( !this->asset->audio_data && this->asset->use_header &&
629 this->asset->video_data && !this->asset->single_frame &&
630 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
631 this->asset->single_frame = 1;
632 this->asset->video_length = -1;
636 // Synchronize header parameters
637 asset->copy_from(this->asset, 1);
640 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
646 void File::delete_temp_samples_buffer()
649 if( temp_samples_buffer ) {
650 for( int j = 0; j < audio_ring_buffers; j++ ) {
651 for( int i = 0; i < asset->channels; i++ ) {
652 delete temp_samples_buffer[j][i];
654 delete [] temp_samples_buffer[j];
657 delete [] temp_samples_buffer;
658 temp_samples_buffer = 0;
659 audio_ring_buffers = 0;
663 void File::delete_temp_frame_buffer()
666 if( temp_frame_buffer ) {
667 for( int k = 0; k < video_ring_buffers; k++ ) {
668 for( int i = 0; i < asset->layers; i++ ) {
669 for( int j = 0; j < video_buffer_size; j++ ) {
670 delete temp_frame_buffer[k][i][j];
672 delete [] temp_frame_buffer[k][i];
674 delete [] temp_frame_buffer[k];
677 delete [] temp_frame_buffer;
678 temp_frame_buffer = 0;
679 video_ring_buffers = 0;
680 video_buffer_size = 0;
684 int File::close_file(int ignore_thread)
688 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
690 if( !ignore_thread ) {
696 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
698 // The file's asset is a copy of the argument passed to open_file so the
699 // user must copy lengths from the file's asset.
701 asset->audio_length = current_sample;
702 asset->video_length = current_frame;
708 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
710 delete_temp_samples_buffer();
711 delete_temp_frame_buffer();
712 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
714 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
717 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
723 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
725 return !file ? -1 : file->get_index(index_file, progress_bar);
730 int File::start_audio_thread(int buffer_size, int ring_buffers)
732 this->audio_ring_buffers = ring_buffers;
735 if( !audio_thread ) {
736 audio_thread = new FileThread(this, 1, 0);
737 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
742 int File::start_video_thread(int buffer_size,
747 this->video_ring_buffers = ring_buffers;
748 this->video_buffer_size = buffer_size;
750 if( !video_thread ) {
751 video_thread = new FileThread(this, 0, 1);
752 video_thread->start_writing(buffer_size,
760 int File::start_video_decode_thread()
762 // Currently, CR2 is the only one which won't work asynchronously, so
763 // we're not using a virtual function yet.
764 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
765 video_thread = new FileThread(this, 0, 1);
766 video_thread->start_reading();
772 int File::stop_audio_thread()
775 audio_thread->stop_writing();
782 int File::stop_video_thread()
785 video_thread->stop_reading();
786 video_thread->stop_writing();
793 FileThread* File::get_video_thread()
798 int File::set_channel(int channel)
800 if( file && channel < asset->channels ) {
801 current_channel = channel;
808 int File::get_channel()
810 return current_channel;
813 // if no>=0, sets new program
814 // returns current program
815 int File::set_program(int no)
817 int result = file ? file->set_program(no) : current_program;
818 current_program = no < 0 ? result : no;
822 int File::get_cell_time(int no, double &time)
824 return file ? file->get_cell_time(no, time) : -1;
827 int File::get_system_time(int64_t &tm)
829 return file ? file->get_system_time(tm) : -1;
832 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
834 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
837 int File::get_video_pid(int track)
839 return file ? file->get_video_pid(track) : -1;
844 int File::get_video_info(int track, int &pid, double &framerate,
845 int &width, int &height, char *title)
848 file->get_video_info(track, pid, framerate, width, height, title);
851 int File::select_video_stream(Asset *asset, int vstream)
854 file->select_video_stream(asset, vstream);
857 int File::select_audio_stream(Asset *asset, int astream)
860 file->select_audio_stream(asset, astream);
864 int File::get_thumbnail(int stream,
865 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
867 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
870 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
872 return file->set_skimming(track, skim, fn, vp);
875 int File::skim_video(int track, void *vp, skim_fn fn)
877 return file->skim_video(track, vp, fn);
881 int File::set_layer(int layer, int is_thread)
883 if( file && layer < asset->layers ) {
884 if( !is_thread && video_thread ) {
885 video_thread->set_layer(layer);
888 current_layer = layer;
896 int64_t File::get_audio_length()
898 int64_t result = asset->audio_length;
899 int64_t base_samplerate = -1;
901 if( base_samplerate > 0 )
902 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
910 int64_t File::get_video_length()
912 int64_t result = asset->video_length;
913 float base_framerate = -1;
915 if( base_framerate > 0 )
916 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
921 return -1; // infinity
925 int64_t File::get_video_position()
927 float base_framerate = -1;
928 if( base_framerate > 0 )
929 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
931 return current_frame;
934 int64_t File::get_audio_position()
936 // int64_t base_samplerate = -1;
937 // if( base_samplerate > 0 )
939 // if( normalized_sample_rate == base_samplerate )
940 // return normalized_sample;
942 // return (int64_t)((double)current_sample /
943 // asset->sample_rate *
948 return current_sample;
953 int File::set_audio_position(int64_t position)
957 if( !file ) return 1;
959 #define REPOSITION(x, y) \
960 (labs((x) - (y)) > 1)
962 float base_samplerate = asset->sample_rate;
963 current_sample = normalized_sample = position;
965 // printf("File::set_audio_position %d normalized_sample=%ld\n",
967 // normalized_sample);
968 result = file->set_audio_position(current_sample);
971 printf("File::set_audio_position position=%jd"
972 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
973 position, base_samplerate, asset, asset->sample_rate);
975 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
980 int File::set_video_position(int64_t position,
984 if( !file ) return 0;
986 // Convert to file's rate
987 // if( base_framerate > 0 )
988 // position = (int64_t)((double)position /
990 // asset->frame_rate +
994 if( video_thread && !is_thread ) {
995 // Call thread. Thread calls this again to set the file state.
996 video_thread->set_video_position(position);
999 if( current_frame != position ) {
1001 current_frame = position;
1002 result = file->set_video_position(current_frame);
1009 // No resampling here.
1010 int File::write_samples(Samples **buffer, int64_t len)
1015 write_lock->lock("File::write_samples");
1017 // Convert to arrays for backwards compatability
1018 double *temp[asset->channels];
1019 for( int i = 0; i < asset->channels; i++ ) {
1020 temp[i] = buffer[i]->get_data();
1023 result = file->write_samples(temp, len);
1024 current_sample += len;
1025 normalized_sample += len;
1026 asset->audio_length += len;
1027 write_lock->unlock();
1036 // Can't put any cmodel abstraction here because the filebase couldn't be
1038 int File::write_frames(VFrame ***frames, int len)
1040 //printf("File::write_frames %d\n", __LINE__);
1042 // Store the counters in temps so the filebase can choose to overwrite them.
1044 int current_frame_temp = current_frame;
1045 int video_length_temp = asset->video_length;
1047 write_lock->lock("File::write_frames");
1050 result = file->write_frames(frames, len);
1053 current_frame = current_frame_temp + len;
1054 asset->video_length = video_length_temp + len;
1055 write_lock->unlock();
1060 // Only called by FileThread
1061 int File::write_compressed_frame(VFrame *buffer)
1064 write_lock->lock("File::write_compressed_frame");
1065 result = file->write_compressed_frame(buffer);
1067 asset->video_length++;
1068 write_lock->unlock();
1073 int File::write_audio_buffer(int64_t len)
1076 if( audio_thread ) {
1077 result = audio_thread->write_buffer(len);
1082 int File::write_video_buffer(int64_t len)
1085 if( video_thread ) {
1086 result = video_thread->write_buffer(len);
1092 Samples** File::get_audio_buffer()
1094 if( audio_thread ) return audio_thread->get_audio_buffer();
1098 VFrame*** File::get_video_buffer()
1100 if( video_thread ) {
1101 VFrame*** result = video_thread->get_video_buffer();
1110 int File::read_samples(Samples *samples, int64_t len)
1112 // Never try to read more samples than exist in the file
1113 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1114 len = asset->audio_length - current_sample;
1116 if( len <= 0 ) return 0;
1119 const int debug = 0;
1120 if( debug ) PRINT_TRACE
1122 if( debug ) PRINT_TRACE
1124 double *buffer = samples->get_data();
1126 int64_t base_samplerate = asset->sample_rate;
1129 // Resample recursively calls this with the asset sample rate
1130 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1132 if( debug ) PRINT_TRACE
1133 result = file->read_samples(buffer, len);
1135 if( debug ) PRINT_TRACE
1136 current_sample += len;
1138 normalized_sample += len;
1140 if( debug ) PRINT_TRACE
1145 int File::read_frame(VFrame *frame, int is_thread)
1147 const int debug = 0;
1148 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1149 // current_frame, use_cache, asset->single_frame);
1150 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1151 if( !file ) return 1;
1152 if( debug ) PRINT_TRACE
1154 int supported_colormodel = colormodel_supported(frame->get_color_model());
1155 int advance_position = 1;
1156 int cache_active = use_cache || asset->single_frame ? 1 : 0;
1157 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1160 if( cache_active && frame_cache->get_frame(frame, cache_position,
1161 current_layer, asset->frame_rate) ) {
1162 // Can't advance position if cache used.
1163 //printf("File::read_frame %d\n", __LINE__);
1164 advance_position = 0;
1167 else if( frame->get_color_model() != BC_COMPRESSED &&
1168 (supported_colormodel != frame->get_color_model() ||
1169 (!file->can_scale_input() &&
1170 (frame->get_w() != asset->width ||
1171 frame->get_h() != asset->height))) ) {
1173 // printf("File::read_frame %d\n", __LINE__);
1174 // Can't advance position here because it needs to be added to cache
1176 if( !temp_frame->params_match(asset->width, asset->height, supported_colormodel) ) {
1183 temp_frame = new VFrame(asset->width, asset->height, supported_colormodel, 0);
1186 // printf("File::read_frame %d\n", __LINE__);
1187 temp_frame->copy_stacks(frame);
1188 result = file->read_frame(temp_frame);
1190 frame->transfer_from(temp_frame);
1191 else if( result && frame->get_status() > 0 )
1192 frame->set_status(-1);
1193 //printf("File::read_frame %d\n", __LINE__);
1196 // Can't advance position here because it needs to be added to cache
1197 //printf("File::read_frame %d\n", __LINE__);
1198 result = file->read_frame(frame);
1199 if( result && frame->get_status() > 0 )
1200 frame->set_status(-1);
1201 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1204 if( result && !current_frame )
1205 frame->clear_frame();
1207 if( cache_active && advance_position && frame->get_status() > 0 )
1208 frame_cache->put_frame(frame, cache_position,
1209 current_layer, asset->frame_rate, 1, 0);
1210 //printf("File::read_frame %d\n", __LINE__);
1212 if( advance_position ) current_frame++;
1213 if( debug ) PRINT_TRACE
1217 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1219 if( asset && file ) {
1220 return asset->width == output_w &&
1221 asset->height == output_h &&
1222 file->can_copy_from(asset, position);
1227 // Fill in queries about formats when adding formats here.
1230 int File::strtoformat(const char *format)
1232 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1233 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1234 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1235 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1236 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1237 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1238 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1239 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1240 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1241 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1242 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1243 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1244 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1245 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1246 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1247 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1248 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1249 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1250 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1251 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1252 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1253 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1254 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1255 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1256 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1257 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1258 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1259 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1260 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1261 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1267 const char* File::formattostr(int format)
1270 case FILE_SCENE: return _(SCENE_NAME);
1271 case FILE_AC3: return _(AC3_NAME);
1272 case FILE_WAV: return _(WAV_NAME);
1273 case FILE_PCM: return _(PCM_NAME);
1274 case FILE_AU: return _(AU_NAME);
1275 case FILE_AIFF: return _(AIFF_NAME);
1276 case FILE_SND: return _(SND_NAME);
1277 case FILE_PNG: return _(PNG_NAME);
1278 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1279 case FILE_PPM: return _(PPM_NAME);
1280 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1281 case FILE_JPEG: return _(JPEG_NAME);
1282 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1283 case FILE_CR2: return _(CR2_NAME);
1284 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1285 case FILE_FLAC: return _(FLAC_NAME);
1286 case FILE_EXR: return _(EXR_NAME);
1287 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1288 #ifdef HAVE_LIBZMPEG
1289 case FILE_MPEG: return _(MPEG_NAME);
1291 case FILE_AMPEG: return _(AMPEG_NAME);
1292 case FILE_VMPEG: return _(VMPEG_NAME);
1293 case FILE_TGA: return _(TGA_NAME);
1294 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1295 case FILE_TIFF: return _(TIFF_NAME);
1296 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1297 case FILE_OGG: return _(OGG_NAME);
1298 case FILE_VORBIS: return _(VORBIS_NAME);
1299 case FILE_RAWDV: return _(RAWDV_NAME);
1300 case FILE_FFMPEG: return _(FFMPEG_NAME);
1301 case FILE_DB: return _(DBASE_NAME);
1303 return _("Unknown");
1306 int File::strtobits(const char *bits)
1308 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1309 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1310 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1311 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1312 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1313 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1314 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1315 return BITSLINEAR16;
1318 const char* File::bitstostr(int bits)
1320 //printf("File::bitstostr\n");
1322 case BITSLINEAR8: return (NAME_8BIT);
1323 case BITSLINEAR16: return (NAME_16BIT);
1324 case BITSLINEAR24: return (NAME_24BIT);
1325 case BITSLINEAR32: return (NAME_32BIT);
1326 case BITSULAW: return (NAME_ULAW);
1327 case BITS_ADPCM: return (NAME_ADPCM);
1328 case BITSFLOAT: return (NAME_FLOAT);
1330 return _("Unknown");
1335 int File::str_to_byteorder(const char *string)
1337 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1341 const char* File::byteorder_to_str(int byte_order)
1343 if( byte_order ) return _("Lo Hi");
1347 int File::bytes_per_sample(int bits)
1350 case BITSLINEAR8: return 1;
1351 case BITSLINEAR16: return 2;
1352 case BITSLINEAR24: return 3;
1353 case BITSLINEAR32: return 4;
1354 case BITSULAW: return 1;
1360 int File::get_best_colormodel(int driver, int vstream)
1362 return file ? file->get_best_colormodel(driver, vstream) :
1363 get_best_colormodel(asset, driver);
1366 int File::get_best_colormodel(Asset *asset, int driver)
1368 switch( asset->format ) {
1370 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1372 #ifdef HAVE_LIBZMPEG
1373 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1376 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1379 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1382 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1384 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1386 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1388 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1389 #ifdef HAVE_COMMERCIAL
1390 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1392 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1399 int File::colormodel_supported(int colormodel)
1402 return file->colormodel_supported(colormodel);
1408 int64_t File::file_memory_usage()
1410 return file ? file->base_memory_usage() : 0;
1413 int64_t File::get_memory_usage()
1417 result += file_memory_usage();
1418 if( temp_frame ) result += temp_frame->get_data_size();
1419 result += frame_cache->get_memory_usage();
1420 if( video_thread ) result += video_thread->get_memory_usage();
1422 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1427 int File::renders_video(int format)
1432 case FILE_JPEG_LIST:
1444 case FILE_TIFF_LIST:
1452 int File::renders_video(Asset *asset)
1454 return asset->format == FILE_FFMPEG ?
1455 FFMPEG::renders_video(asset->fformat) :
1456 renders_video(asset->format);
1459 int File::renders_audio(int format)
1478 int File::renders_audio(Asset *asset)
1480 return asset->format == FILE_FFMPEG ?
1481 FFMPEG::renders_audio(asset->fformat) :
1482 renders_audio(asset->format);
1485 int File::is_image_render(int format)
1500 const char* File::get_tag(int format)
1503 case FILE_AC3: return "ac3";
1504 case FILE_AIFF: return "aif";
1505 case FILE_AMPEG: return "mp3";
1506 case FILE_AU: return "au";
1507 case FILE_RAWDV: return "dv";
1508 case FILE_DB: return "db";
1509 case FILE_EXR: return "exr";
1510 case FILE_EXR_LIST: return "exr";
1511 case FILE_FLAC: return "flac";
1512 case FILE_JPEG: return "jpg";
1513 case FILE_JPEG_LIST: return "jpg";
1514 case FILE_OGG: return "ogg";
1515 case FILE_PCM: return "pcm";
1516 case FILE_PNG: return "png";
1517 case FILE_PNG_LIST: return "png";
1518 case FILE_PPM: return "ppm";
1519 case FILE_PPM_LIST: return "ppm";
1520 case FILE_TGA: return "tga";
1521 case FILE_TGA_LIST: return "tga";
1522 case FILE_TIFF: return "tif";
1523 case FILE_TIFF_LIST: return "tif";
1524 case FILE_VMPEG: return "m2v";
1525 case FILE_VORBIS: return "ogg";
1526 case FILE_WAV: return "wav";
1527 case FILE_FFMPEG: return "ffmpg";
1532 const char* File::get_prefix(int format)
1535 case FILE_PCM: return "PCM";
1536 case FILE_WAV: return "WAV";
1537 case FILE_PNG: return "PNG";
1538 case FILE_PPM: return "PPM";
1539 case FILE_JPEG: return "JPEG";
1540 case FILE_TIFF: return "TIFF";
1541 case FILE_GIF: return "GIF";
1542 case FILE_JPEG_LIST: return "JPEG_LIST";
1543 case FILE_AU: return "AU";
1544 case FILE_AIFF: return "AIFF";
1545 case FILE_SND: return "SND";
1546 case FILE_TGA_LIST: return "TGA_LIST";
1547 case FILE_TGA: return "TGA";
1548 case FILE_MPEG: return "MPEG";
1549 case FILE_AMPEG: return "AMPEG";
1550 case FILE_VMPEG: return "VMPEG";
1551 case FILE_RAWDV: return "RAWDV";
1552 case FILE_TIFF_LIST: return "TIFF_LIST";
1553 case FILE_PNG_LIST: return "PNG_LIST";
1554 case FILE_PPM_LIST: return "PPM_LIST";
1555 case FILE_AC3: return "AC3";
1556 case FILE_EXR: return "EXR";
1557 case FILE_EXR_LIST: return "EXR_LIST";
1558 case FILE_CR2: return "CR2";
1559 case FILE_OGG: return "OGG";
1560 case FILE_VORBIS: return "VORBIS";
1561 case FILE_FLAC: return "FLAC";
1562 case FILE_FFMPEG: return "FFMPEG";
1563 case FILE_SCENE: return "SCENE";
1564 case FILE_CR2_LIST: return "CR2_LIST";
1565 case FILE_GIF_LIST: return "GIF_LIST";
1566 case FILE_DB: return "DB";
1568 return _("UNKNOWN");
1572 PackagingEngine *File::new_packaging_engine(Asset *asset)
1574 PackagingEngine *result;
1575 switch( asset->format ) {
1577 result = (PackagingEngine*)new PackagingEngineOGG();
1580 result = (PackagingEngine*) new PackagingEngineDefault();
1588 int File::record_fd()
1590 return file ? file->record_fd() : -1;
1594 void File::get_exe_path(char *result, char *bnp)
1596 // Get executable path, basename
1597 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1600 char *ptr = strrchr(result, '/');
1601 if( ptr ) *ptr++ = 0; else ptr = result;
1602 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1610 void File::getenv_path(char *result, const char *path)
1612 char *rp = result, *ep = rp + BCTEXTLEN-1;
1613 const char *cp = path;
1614 // any env var can be used here to expand a path as:
1615 // "path...$id...": and id is a word as alpha,alnum...
1616 // expands as "path...getenv(id)..."
1617 // CIN_PATH, CIN_LIB are set in main.C,
1618 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1619 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1620 const char *bp = cp; char *p = rp;
1621 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1623 const char *envp = getenv(rp); // read env value
1624 if( !envp ) continue;
1625 while( *envp && rp < ep ) *rp++ = *envp++;
1626 cp = bp; // subst id=value
1634 void File::setenv_path(const char *var, const char *path, int overwrite)
1636 char env_path[BCTEXTLEN];
1637 getenv_path(env_path, path);
1638 setenv(var, env_path, overwrite);
1641 void File::init_cin_path()
1643 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1644 // these values are advertised for forks/shell scripts
1645 get_exe_path(env_path, env_pkg);
1646 setenv_path("CIN_PATH", env_path, 1);
1647 setenv_path("CIN_PKG", env_pkg, 1);
1648 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1649 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1650 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1651 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1652 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1653 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1654 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);