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
30 // work arounds (centos)
33 #define INT64_MAX 9223372036854775807LL
38 #include "bcsignals.h"
39 #include "byteorder.h"
41 #include "condition.h"
49 #include "fileffmpeg.h"
55 #undef HAVE_STDLIB_H // automake conflict
60 #include "filescene.h"
61 #include "filesndfile.h"
63 #include "filethread.h"
65 #include "filevorbis.h"
67 #include "formatwindow.h"
68 #include "formattools.h"
69 #include "framecache.h"
71 #include "mainprogress.inc"
74 #include "packagingengine.h"
75 #include "pluginserver.h"
76 #include "preferences.h"
77 #include "probeprefs.h"
82 //suppress noref warning
83 void *vorbis0_ov_callbacks[] = {
84 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
85 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
93 format_completion = new Condition(1, "File::format_completion");
94 write_lock = new Condition(1, "File::write_lock");
95 frame_cache = new FrameCache;
101 if( getting_options ) {
102 if( format_window ) format_window->set_done(0);
103 format_completion->lock("File::~File");
104 format_completion->unlock();
113 asset->Garbage::remove_user();
114 delete format_completion;
119 void File::reset_parameters()
132 normalized_sample = 0;
135 playback_subtitle = -1;
139 temp_samples_buffer = 0;
140 temp_frame_buffer = 0;
141 current_frame_buffer = 0;
142 audio_ring_buffers = 0;
143 video_ring_buffers = 0;
144 video_buffer_size = 0;
147 int File::raise_window()
149 if( getting_options && format_window ) {
150 format_window->raise_window();
151 format_window->flush();
156 void File::close_window()
158 if( getting_options ) {
159 format_window->lock_window("File::close_window");
160 format_window->set_done(1);
161 format_window->unlock_window();
166 int File::get_options(FormatTools *format,
167 int audio_options, int video_options)
169 BC_WindowBase *parent_window = format->window;
170 Asset *asset = format->asset;
171 EDL *edl = format->mwindow ? format->mwindow->edl : 0;
174 format_completion->lock("File::get_options");
175 switch( asset->format ) {
176 #ifdef HAVE_CIN_3RDPARTY
178 case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
179 audio_options, video_options, edl);
185 FileDV::get_parameters(parent_window, asset, format_window,
186 audio_options, video_options, edl);
194 FileSndFile::get_parameters(parent_window, asset, format_window,
195 audio_options, video_options, edl);
198 FileFFMPEG::get_parameters(parent_window, asset, format_window,
199 audio_options, video_options, edl);
204 FileMPEG::get_parameters(parent_window, asset, format_window,
205 audio_options, video_options, edl);
210 FileJPEG::get_parameters(parent_window, asset, format_window,
211 audio_options, video_options, edl);
216 FileEXR::get_parameters(parent_window, asset, format_window,
217 audio_options, video_options, edl);
221 FileFLAC::get_parameters(parent_window, asset, format_window,
222 audio_options, video_options, edl);
226 FilePNG::get_parameters(parent_window, asset, format_window,
227 audio_options, video_options, edl);
231 FilePPM::get_parameters(parent_window, asset, format_window,
232 audio_options, video_options, edl);
236 FileTGA::get_parameters(parent_window, asset, format_window,
237 audio_options, video_options, edl);
241 FileTIFF::get_parameters(parent_window, asset, format_window,
242 audio_options, video_options, edl);
246 FileOGG::get_parameters(parent_window, asset, format_window,
247 audio_options, video_options, edl);
254 if( !format_window ) {
255 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
256 parent_window->get_abs_cursor_x(1),
257 parent_window->get_abs_cursor_y(1));
258 format_window = errorbox;
261 errorbox->create_objects(_("This format doesn't support audio."));
264 errorbox->create_objects(_("This format doesn't support video."));
265 errorbox->run_window();
271 format_completion->unlock();
275 int File::can_scale_input(Asset *asset)
277 switch( asset->format ) {
287 if( asset->video_length < 0 )
294 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
296 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
297 cpus = 8; // 8 cpus ought to decode just about anything
298 // Set all instances so gets work.
304 int File::set_preload(int64_t size)
306 this->playback_preload = size;
310 void File::set_subtitle(int value)
312 this->playback_subtitle = value;
313 if( file ) file->set_subtitle(value);
316 void File::set_interpolate_raw(int value)
318 this->interpolate_raw = value;
321 void File::set_white_balance_raw(int value)
323 this->white_balance_raw = value;
326 void File::set_cache_frames(int value)
328 // caching only done locally
333 int File::purge_cache()
335 // caching only done locally
337 if( frame_cache->cache_items() > 0 ) {
338 frame_cache->remove_all();
344 int File::delete_oldest()
346 // return the number of bytes freed
347 return frame_cache->delete_oldest();
350 int File::get_cache_frame(VFrame *frame, int64_t position)
352 return frame_cache->get_cache_frame(frame, position,
353 current_layer, asset->frame_rate);
356 void File::put_cache_frame(VFrame *frame, int64_t position, int use_copy)
358 frame_cache->put_cache_frame(frame,
359 position, current_layer, asset->frame_rate, use_copy);
362 int File::get_use_cache()
368 // file driver in order of probe precidence
369 // can be reordered in preferences->interface
370 const char *File::default_probes[] = {
397 const int File::nb_probes =
398 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
403 FILE *fp = fopen(this->asset->path, "rb");
404 if( !fp ) return FILE_NOT_FOUND;
406 memset(data,0,sizeof(data));
407 int ret = fread(data, 1, 16, fp);
409 if( !ret ) return FILE_NOT_FOUND;
411 for( int i=0; i<preferences->file_probes.size(); ++i ) {
412 ProbePref *pref = preferences->file_probes[i];
413 if( !pref->armed ) continue;
414 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
415 if( !FileFFMPEG::check_sig(this->asset) ) continue;
416 file = new FileFFMPEG(this->asset, this);
419 #ifdef HAVE_COMMERCIAL
420 if( !strcmp(pref->name,"DB") ) { // MediaDB
421 if( !FileDB::check_sig(this->asset) ) continue;
422 file = new FileDB(this->asset, this);
426 if( !strcmp(pref->name,"Scene") ) { // scene file
427 if( !FileScene::check_sig(this->asset, data)) continue;
428 file = new FileScene(this->asset, this);
432 if( !strcmp(pref->name,"DV") ) { // libdv
433 if( !FileDV::check_sig(this->asset) ) continue;
434 file = new FileDV(this->asset, this);
438 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
439 if( !FileSndFile::check_sig(this->asset) ) continue;
440 file = new FileSndFile(this->asset, this);
443 if( !strcmp(pref->name,"PNG") ) { // PNG file
444 if( !FilePNG::check_sig(this->asset) ) continue;
445 file = new FilePNG(this->asset, this);
448 if( !strcmp(pref->name,"PPM") ) { // PPM file
449 if( !FilePPM::check_sig(this->asset) ) continue;
450 file = new FilePPM(this->asset, this);
453 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
454 if( !FileJPEG::check_sig(this->asset) ) continue;
455 file = new FileJPEG(this->asset, this);
458 if( !strcmp(pref->name,"GIF") ) { // GIF file
459 if( FileGIFList::check_sig(this->asset) )
460 file = new FileGIFList(this->asset, this);
461 else if( FileGIF::check_sig(this->asset) )
462 file = new FileGIF(this->asset, this);
467 if( !strcmp(pref->name,"EXR") ) { // EXR file
468 if( !FileEXR::check_sig(this->asset, data)) continue;
469 file = new FileEXR(this->asset, this);
473 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
474 if( !FileFLAC::check_sig(this->asset, data)) continue;
475 file = new FileFLAC(this->asset, this);
478 if( !strcmp(pref->name,"CR2") ) { // CR2 file
479 if( !FileCR2::check_sig(this->asset)) continue;
480 file = new FileCR2(this->asset, this);
483 if( !strcmp(pref->name,"TGA") ) { // TGA file
484 if( !FileTGA::check_sig(this->asset) ) continue;
485 file = new FileTGA(this->asset, this);
488 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
489 if( !FileTIFF::check_sig(this->asset) ) continue;
490 file = new FileTIFF(this->asset, this);
494 if( !strcmp(pref->name,"OGG") ) { // OGG file
495 if( !FileOGG::check_sig(this->asset) ) continue;
496 file = new FileOGG(this->asset, this);
499 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
500 if( !FileVorbis::check_sig(this->asset) ) continue;
501 file = new FileVorbis(this->asset, this);
506 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
507 if( !FileMPEG::check_sig(this->asset) ) continue;
508 file = new FileMPEG(this->asset, this);
512 if( !strcmp(pref->name,"EDL") ) { // XML file
513 if( data[0] != '<' ) continue;
514 if( !strncmp(&data[1],"EDL>",4) ||
515 !strncmp(&data[1],"HTAL>",5) ||
516 !strncmp(&data[1],"?xml",4) )
520 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
523 int File::open_file(Preferences *preferences,
530 this->preferences = preferences;
531 this->asset->copy_from(asset, 1);
536 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
538 switch( this->asset->format ) {
539 // get the format now
540 // If you add another format to case 0, you also need to add another case for the
541 // file format #define.
544 if( ret != FILE_OK ) return ret;
546 // format already determined
547 #ifdef HAVE_CIN_3RDPARTY
550 file = new FileAC3(this->asset, this);
555 file = new FileScene(this->asset, this);
559 file = new FileFFMPEG(this->asset, this);
567 //printf("File::open_file 1\n");
568 file = new FileSndFile(this->asset, this);
573 file = new FilePNG(this->asset, this);
578 file = new FilePPM(this->asset, this);
583 file = new FileJPEG(this->asset, this);
587 file = new FileGIF(this->asset, this);
590 file = new FileGIFList(this->asset, this);
596 file = new FileEXR(this->asset, this);
600 file = new FileFLAC(this->asset, this);
605 file = new FileCR2(this->asset, this);
610 file = new FileTGA(this->asset, this);
615 file = new FileTIFF(this->asset, this);
617 #ifdef HAVE_COMMERCIAL
619 file = new FileDB(this->asset, this);
627 file = new FileMPEG(this->asset, this);
632 file = new FileOGG(this->asset, this);
636 file = new FileVorbis(this->asset, this);
641 file = new FileDV(this->asset, this);
645 file = new FileREF(this->asset, this);
654 // Reopen file with correct parser and get header.
655 if( file->open_file(rd, wr) ) {
656 delete file; file = 0;
657 return FILE_NOT_FOUND;
661 // If file type is a list verify that all files match in dimensions.
662 // Should be done only after the file open function has been performed
663 // Reason: although this function checks if file exists or not but
664 // it has no way of relaying this information back and if this function
665 // is called before open_file the program may accidently interpret file
666 // not found as file size don't match
667 if( !file->verify_file_list() ) {
668 delete file; file = 0;
669 return FILE_SIZE_DONT_MATCH;
673 // Set extra writing parameters to mandatory settings.
675 if( this->asset->dither ) file->set_dither();
679 // one frame image file, not brender, no specific length
680 if( !this->asset->audio_data && this->asset->use_header &&
681 this->asset->video_data && !this->asset->single_frame &&
682 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
683 this->asset->single_frame = 1;
684 this->asset->video_length = -1;
688 // Synchronize header parameters
689 asset->copy_from(this->asset, 1);
692 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
698 void File::delete_temp_samples_buffer()
701 if( temp_samples_buffer ) {
702 for( int j = 0; j < audio_ring_buffers; j++ ) {
703 for( int i = 0; i < asset->channels; i++ ) {
704 delete temp_samples_buffer[j][i];
706 delete [] temp_samples_buffer[j];
709 delete [] temp_samples_buffer;
710 temp_samples_buffer = 0;
711 audio_ring_buffers = 0;
715 void File::delete_temp_frame_buffer()
718 if( temp_frame_buffer ) {
719 for( int k = 0; k < video_ring_buffers; k++ ) {
720 for( int i = 0; i < asset->layers; i++ ) {
721 for( int j = 0; j < video_buffer_size; j++ ) {
722 delete temp_frame_buffer[k][i][j];
724 delete [] temp_frame_buffer[k][i];
726 delete [] temp_frame_buffer[k];
729 delete [] temp_frame_buffer;
730 temp_frame_buffer = 0;
731 video_ring_buffers = 0;
732 video_buffer_size = 0;
736 int File::close_file(int ignore_thread)
738 if( !ignore_thread ) {
743 // The file's asset is a copy of the argument passed to open_file so the
744 // user must copy lengths from the file's asset.
746 asset->audio_length = current_sample;
747 asset->video_length = current_frame;
754 delete_temp_samples_buffer();
755 delete_temp_frame_buffer();
760 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
762 return !file ? -1 : file->get_index(index_file, progress_bar);
765 int File::start_audio_thread(int buffer_size, int ring_buffers)
767 this->audio_ring_buffers = ring_buffers;
770 if( !audio_thread ) {
771 audio_thread = new FileThread(this, 1, 0);
772 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
777 int File::start_video_thread(int buffer_size, int color_model,
778 int ring_buffers, int compressed)
780 this->video_ring_buffers = ring_buffers;
781 this->video_buffer_size = buffer_size;
783 if( !video_thread ) {
784 video_thread = new FileThread(this, 0, 1);
785 video_thread->start_writing(buffer_size, color_model,
786 ring_buffers, compressed);
791 int File::start_video_decode_thread()
793 // Currently, CR2 is the only one which won't work asynchronously, so
794 // we're not using a virtual function yet.
795 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
796 video_thread = new FileThread(this, 0, 1);
797 video_thread->start_reading();
803 int File::stop_audio_thread()
806 audio_thread->stop_writing();
813 int File::stop_video_thread()
816 video_thread->stop_reading();
817 video_thread->stop_writing();
824 FileThread* File::get_video_thread()
829 int File::set_channel(int channel)
831 if( file && channel < asset->channels ) {
832 current_channel = channel;
839 int File::get_channel()
841 return current_channel;
844 // if no>=0, sets new program
845 // returns current program
846 int File::set_program(int no)
848 int result = file ? file->set_program(no) : current_program;
849 current_program = no < 0 ? result : no;
853 int File::get_cell_time(int no, double &time)
855 return file ? file->get_cell_time(no, time) : -1;
858 int File::get_system_time(int64_t &tm)
860 return file ? file->get_system_time(tm) : -1;
863 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
865 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
868 int File::get_video_pid(int track)
870 return file ? file->get_video_pid(track) : -1;
875 int File::get_video_info(int track, int &pid, double &framerate,
876 int &width, int &height, char *title)
879 file->get_video_info(track, pid, framerate, width, height, title);
882 int File::select_video_stream(Asset *asset, int vstream)
885 file->select_video_stream(asset, vstream);
888 int File::select_audio_stream(Asset *asset, int astream)
891 file->select_audio_stream(asset, astream);
895 int File::get_thumbnail(int stream,
896 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
898 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
901 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
903 return file->set_skimming(track, skim, fn, vp);
906 int File::skim_video(int track, void *vp, skim_fn fn)
908 return file->skim_video(track, vp, fn);
912 int File::set_layer(int layer, int is_thread)
914 if( file && layer < asset->layers ) {
915 if( !is_thread && video_thread ) {
916 video_thread->set_layer(layer);
919 current_layer = layer;
927 int64_t File::get_audio_length()
929 int64_t result = asset->audio_length;
930 int64_t base_samplerate = -1;
932 if( base_samplerate > 0 )
933 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
941 int64_t File::get_video_length()
943 int64_t result = asset->video_length;
944 float base_framerate = -1;
946 if( base_framerate > 0 )
947 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
952 return -1; // infinity
956 int64_t File::get_video_position()
958 float base_framerate = -1;
959 if( base_framerate > 0 )
960 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
962 return current_frame;
965 int64_t File::get_audio_position()
967 // int64_t base_samplerate = -1;
968 // if( base_samplerate > 0 )
970 // if( normalized_sample_rate == base_samplerate )
971 // return normalized_sample;
973 // return (int64_t)((double)current_sample /
974 // asset->sample_rate *
979 return current_sample;
984 int File::set_audio_position(int64_t position)
988 if( !file ) return 1;
990 #define REPOSITION(x, y) \
991 (labs((x) - (y)) > 1)
993 float base_samplerate = asset->sample_rate;
994 current_sample = normalized_sample = position;
996 // printf("File::set_audio_position %d normalized_sample=%ld\n",
998 // normalized_sample);
999 result = file->set_audio_position(current_sample);
1002 printf("File::set_audio_position position=%jd"
1003 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
1004 position, base_samplerate, asset, asset->sample_rate);
1006 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1011 int File::set_video_position(int64_t position,
1015 if( !file ) return 0;
1017 // Convert to file's rate
1018 // if( base_framerate > 0 )
1019 // position = (int64_t)((double)position /
1021 // asset->frame_rate +
1025 if( video_thread && !is_thread ) {
1026 // Call thread. Thread calls this again to set the file state.
1027 video_thread->set_video_position(position);
1030 if( current_frame != position ) {
1032 current_frame = position;
1033 result = file->set_video_position(current_frame);
1040 // No resampling here.
1041 int File::write_samples(Samples **buffer, int64_t len)
1046 write_lock->lock("File::write_samples");
1048 // Convert to arrays for backwards compatability
1049 double *temp[asset->channels];
1050 for( int i = 0; i < asset->channels; i++ ) {
1051 temp[i] = buffer[i]->get_data();
1054 result = file->write_samples(temp, len);
1055 current_sample += len;
1056 normalized_sample += len;
1057 asset->audio_length += len;
1058 write_lock->unlock();
1067 // Can't put any cmodel abstraction here because the filebase couldn't be
1069 int File::write_frames(VFrame ***frames, int len)
1071 //printf("File::write_frames %d\n", __LINE__);
1073 // Store the counters in temps so the filebase can choose to overwrite them.
1075 int current_frame_temp = current_frame;
1076 int video_length_temp = asset->video_length;
1078 write_lock->lock("File::write_frames");
1081 result = file->write_frames(frames, len);
1084 current_frame = current_frame_temp + len;
1085 asset->video_length = video_length_temp + len;
1086 write_lock->unlock();
1091 // Only called by FileThread
1092 int File::write_compressed_frame(VFrame *buffer)
1095 write_lock->lock("File::write_compressed_frame");
1096 result = file->write_compressed_frame(buffer);
1098 asset->video_length++;
1099 write_lock->unlock();
1104 int File::write_audio_buffer(int64_t len)
1107 if( audio_thread ) {
1108 result = audio_thread->write_buffer(len);
1113 int File::write_video_buffer(int64_t len)
1116 if( video_thread ) {
1117 result = video_thread->write_buffer(len);
1123 Samples** File::get_audio_buffer()
1125 if( audio_thread ) return audio_thread->get_audio_buffer();
1129 VFrame*** File::get_video_buffer()
1131 if( video_thread ) {
1132 VFrame*** result = video_thread->get_video_buffer();
1141 int File::read_samples(Samples *samples, int64_t len)
1143 // Never try to read more samples than exist in the file
1144 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1145 len = asset->audio_length - current_sample;
1147 if( len <= 0 ) return 0;
1150 const int debug = 0;
1151 if( debug ) PRINT_TRACE
1153 if( debug ) PRINT_TRACE
1155 double *buffer = samples->get_data();
1157 int64_t base_samplerate = asset->sample_rate;
1160 // Resample recursively calls this with the asset sample rate
1161 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1163 if( debug ) PRINT_TRACE
1164 result = file->read_samples(buffer, len);
1166 if( debug ) PRINT_TRACE
1167 current_sample += len;
1169 normalized_sample += len;
1171 if( debug ) PRINT_TRACE
1176 int File::read_frame(VFrame *frame, int is_thread)
1178 const int debug = 0;
1179 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1180 // current_frame, use_cache, asset->single_frame);
1181 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1182 if( !file ) return 1;
1183 if( debug ) PRINT_TRACE
1185 int supported_colormodel = colormodel_supported(frame->get_color_model());
1187 // if reverse playback reading, use_cache is -1
1188 int cache_active = asset->single_frame ? 1 : use_cache;
1189 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1192 if( cache_active ) {
1193 if( get_cache_frame(frame, cache_position) ) {
1196 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1197 // reverse reading and cache miss, clear cache for new readahead
1203 VFrame *vframe = frame;
1204 if( frame->get_color_model() != BC_COMPRESSED &&
1205 (supported_colormodel != frame->get_color_model() ||
1206 (!file->can_scale_input() &&
1207 (frame->get_w() != asset->width ||
1208 frame->get_h() != asset->height))) ) {
1210 if( !temp_frame->params_match(asset->width, asset->height,
1211 supported_colormodel) ) {
1212 delete temp_frame; temp_frame = 0;
1216 temp_frame = new VFrame(asset->width, asset->height,
1217 supported_colormodel, 0);
1218 temp_frame->clear_frame();
1221 temp_frame->copy_stacks(frame);
1222 vframe = temp_frame;
1224 result = file->read_frame(vframe);
1226 if( frame != vframe )
1227 frame->transfer_from(vframe);
1228 if( cache_active > 0 )
1229 put_cache_frame(frame, cache_position, 1);
1231 else if( frame->get_status() > 0 )
1232 frame->set_status(-1);
1233 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1236 if( result && !current_frame )
1237 frame->clear_frame();
1239 //printf("File::read_frame %d\n", __LINE__);
1241 if( do_read ) current_frame++;
1242 if( debug ) PRINT_TRACE
1246 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1248 if( asset && file ) {
1249 return asset->width == output_w &&
1250 asset->height == output_h &&
1251 file->can_copy_from(asset, position);
1256 // Fill in queries about formats when adding formats here.
1259 int File::strtoformat(const char *format)
1261 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1262 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1263 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1264 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1265 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1266 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1267 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1268 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1269 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1270 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1271 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1272 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1273 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1274 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1275 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1276 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1277 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1278 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1279 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1280 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1281 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1282 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1283 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1284 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1285 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1286 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1287 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1288 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1289 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1290 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1291 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1292 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1293 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1299 const char* File::formattostr(int format)
1302 case FILE_SCENE: return _(SCENE_NAME);
1303 case FILE_AC3: return _(AC3_NAME);
1304 case FILE_WAV: return _(WAV_NAME);
1305 case FILE_PCM: return _(PCM_NAME);
1306 case FILE_AU: return _(AU_NAME);
1307 case FILE_AIFF: return _(AIFF_NAME);
1308 case FILE_SND: return _(SND_NAME);
1309 case FILE_PNG: return _(PNG_NAME);
1310 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1311 case FILE_PPM: return _(PPM_NAME);
1312 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1313 case FILE_JPEG: return _(JPEG_NAME);
1314 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1315 case FILE_CR2: return _(CR2_NAME);
1316 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1317 case FILE_FLAC: return _(FLAC_NAME);
1318 case FILE_GIF: return _(GIF_NAME);
1319 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1320 case FILE_EXR: return _(EXR_NAME);
1321 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1322 #ifdef HAVE_LIBZMPEG
1323 case FILE_MPEG: return _(MPEG_NAME);
1325 case FILE_AMPEG: return _(AMPEG_NAME);
1326 case FILE_VMPEG: return _(VMPEG_NAME);
1327 case FILE_TGA: return _(TGA_NAME);
1328 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1329 case FILE_TIFF: return _(TIFF_NAME);
1330 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1331 case FILE_OGG: return _(OGG_NAME);
1332 case FILE_VORBIS: return _(VORBIS_NAME);
1333 case FILE_RAWDV: return _(RAWDV_NAME);
1334 case FILE_FFMPEG: return _(FFMPEG_NAME);
1335 case FILE_DB: return _(DBASE_NAME);
1336 case FILE_REF: return _(REF_NAME);
1338 return _("Unknown");
1341 int File::strtobits(const char *bits)
1343 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1344 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1345 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1346 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1347 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1348 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1349 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1350 return BITSLINEAR16;
1353 const char* File::bitstostr(int bits)
1355 //printf("File::bitstostr\n");
1357 case BITSLINEAR8: return (NAME_8BIT);
1358 case BITSLINEAR16: return (NAME_16BIT);
1359 case BITSLINEAR24: return (NAME_24BIT);
1360 case BITSLINEAR32: return (NAME_32BIT);
1361 case BITSULAW: return (NAME_ULAW);
1362 case BITS_ADPCM: return (NAME_ADPCM);
1363 case BITSFLOAT: return (NAME_FLOAT);
1365 return _("Unknown");
1370 int File::str_to_byteorder(const char *string)
1372 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1376 const char* File::byteorder_to_str(int byte_order)
1378 if( byte_order ) return _("Lo Hi");
1382 int File::bytes_per_sample(int bits)
1385 case BITSLINEAR8: return 1;
1386 case BITSLINEAR16: return 2;
1387 case BITSLINEAR24: return 3;
1388 case BITSLINEAR32: return 4;
1389 case BITSULAW: return 1;
1395 int File::get_best_colormodel(int driver, int vstream)
1397 return file ? file->get_best_colormodel(driver, vstream) :
1398 get_best_colormodel(asset, driver);
1401 int File::get_best_colormodel(Asset *asset, int driver)
1403 switch( asset->format ) {
1405 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1407 #ifdef HAVE_LIBZMPEG
1408 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1411 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1414 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1417 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1419 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1421 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1423 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1424 #ifdef HAVE_COMMERCIAL
1425 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1427 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1428 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1435 int File::colormodel_supported(int colormodel)
1438 return file->colormodel_supported(colormodel);
1444 int64_t File::file_memory_usage()
1446 return file ? file->base_memory_usage() : 0;
1449 int64_t File::get_memory_usage()
1453 result += file_memory_usage();
1454 if( temp_frame ) result += temp_frame->get_data_size();
1455 result += frame_cache->get_memory_usage();
1456 if( video_thread ) result += video_thread->get_memory_usage();
1458 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1463 int File::renders_video(int format)
1468 case FILE_JPEG_LIST:
1482 case FILE_TIFF_LIST:
1490 int File::renders_video(Asset *asset)
1492 return asset->format == FILE_FFMPEG ?
1493 FFMPEG::renders_video(asset->fformat) :
1494 renders_video(asset->format);
1497 int File::renders_audio(int format)
1516 int File::renders_audio(Asset *asset)
1518 return asset->format == FILE_FFMPEG ?
1519 FFMPEG::renders_audio(asset->fformat) :
1520 renders_audio(asset->format);
1523 int File::is_image_render(int format)
1538 const char* File::get_tag(int format)
1541 case FILE_AC3: return "ac3";
1542 case FILE_AIFF: return "aif";
1543 case FILE_AMPEG: return "mp3";
1544 case FILE_AU: return "au";
1545 case FILE_RAWDV: return "dv";
1546 case FILE_DB: return "db";
1547 case FILE_EXR: return "exr";
1548 case FILE_EXR_LIST: return "exrs";
1549 case FILE_FLAC: return "flac";
1550 case FILE_JPEG: return "jpg";
1551 case FILE_JPEG_LIST: return "jpgs";
1552 case FILE_OGG: return "ogg";
1553 case FILE_GIF: return "gif";
1554 case FILE_GIF_LIST: return "gifs";
1555 case FILE_PCM: return "pcm";
1556 case FILE_PNG: return "png";
1557 case FILE_PNG_LIST: return "pngs";
1558 case FILE_PPM: return "ppm";
1559 case FILE_PPM_LIST: return "ppms";
1560 case FILE_TGA: return "tga";
1561 case FILE_TGA_LIST: return "tgas";
1562 case FILE_TIFF: return "tif";
1563 case FILE_TIFF_LIST: return "tifs";
1564 case FILE_VMPEG: return "m2v";
1565 case FILE_VORBIS: return "ogg";
1566 case FILE_WAV: return "wav";
1567 case FILE_FFMPEG: return "ffmpg";
1568 case FILE_REF: return "ref";
1573 const char* File::get_prefix(int format)
1576 case FILE_PCM: return "PCM";
1577 case FILE_WAV: return "WAV";
1578 case FILE_PNG: return "PNG";
1579 case FILE_PPM: return "PPM";
1580 case FILE_JPEG: return "JPEG";
1581 case FILE_TIFF: return "TIFF";
1582 case FILE_GIF: return "GIF";
1583 case FILE_JPEG_LIST: return "JPEG_LIST";
1584 case FILE_AU: return "AU";
1585 case FILE_AIFF: return "AIFF";
1586 case FILE_SND: return "SND";
1587 case FILE_TGA_LIST: return "TGA_LIST";
1588 case FILE_TGA: return "TGA";
1589 case FILE_MPEG: return "MPEG";
1590 case FILE_AMPEG: return "AMPEG";
1591 case FILE_VMPEG: return "VMPEG";
1592 case FILE_RAWDV: return "RAWDV";
1593 case FILE_TIFF_LIST: return "TIFF_LIST";
1594 case FILE_PNG_LIST: return "PNG_LIST";
1595 case FILE_PPM_LIST: return "PPM_LIST";
1596 case FILE_AC3: return "AC3";
1597 case FILE_EXR: return "EXR";
1598 case FILE_EXR_LIST: return "EXR_LIST";
1599 case FILE_CR2: return "CR2";
1600 case FILE_OGG: return "OGG";
1601 case FILE_VORBIS: return "VORBIS";
1602 case FILE_FLAC: return "FLAC";
1603 case FILE_FFMPEG: return "FFMPEG";
1604 case FILE_SCENE: return "SCENE";
1605 case FILE_CR2_LIST: return "CR2_LIST";
1606 case FILE_GIF_LIST: return "GIF_LIST";
1607 case FILE_DB: return "DB";
1608 case FILE_REF: return "REF";
1610 return _("UNKNOWN");
1614 int File::record_fd()
1616 return file ? file->record_fd() : -1;
1620 void File::get_exe_path(char *result, char *bnp)
1622 // Get executable path, basename
1623 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1626 char *ptr = strrchr(result, '/');
1627 if( ptr ) *ptr++ = 0; else ptr = result;
1628 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1636 void File::getenv_path(char *result, const char *path)
1638 char *rp = result, *ep = rp + BCTEXTLEN-1;
1639 const char *cp = path;
1640 // any env var can be used here to expand a path as:
1641 // "path...$id...": and id is a word as alpha,alnum...
1642 // expands as "path...getenv(id)..."
1643 // CIN_PATH, CIN_LIB are set in main.C,
1644 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1645 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1646 const char *bp = cp; char *p = rp;
1647 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1649 const char *envp = getenv(rp); // read env value
1650 if( !envp ) continue;
1651 while( *envp && rp < ep ) *rp++ = *envp++;
1652 cp = bp; // subst id=value
1660 void File::setenv_path(const char *var, const char *path, int overwrite)
1662 char env_path[BCTEXTLEN];
1663 getenv_path(env_path, path);
1664 setenv(var, env_path, overwrite);
1667 * @brief Set various environment variables to pass constant values to
1668 * forks, shell scripts, and other parts of CinGG.
1670 void File::init_cin_env_vars()
1672 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1674 struct tm *dateTime;
1675 get_exe_path(env_path, env_pkg);
1676 setenv_path("CIN_PATH", env_path, 1);
1677 setenv_path("CIN_PKG", env_pkg, 1);
1678 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1679 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1680 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1681 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1682 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1683 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1684 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1685 // Create env_var CINGG_BUILD for use when running as AppImage
1686 stat(env_path, &st);
1687 dateTime = gmtime(&st.st_mtime);
1688 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1689 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1690 build_name[sizeof(build_name) - 1] = 0;
1691 setenv_path("CINGG_BUILD", build_name, 1);