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;
662 // Set extra writing parameters to mandatory settings.
664 if( this->asset->dither ) file->set_dither();
668 // one frame image file, not brender, no specific length
669 if( !this->asset->audio_data && this->asset->use_header &&
670 this->asset->video_data && !this->asset->single_frame &&
671 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
672 this->asset->single_frame = 1;
673 this->asset->video_length = -1;
677 // Synchronize header parameters
678 asset->copy_from(this->asset, 1);
681 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
687 void File::delete_temp_samples_buffer()
690 if( temp_samples_buffer ) {
691 for( int j = 0; j < audio_ring_buffers; j++ ) {
692 for( int i = 0; i < asset->channels; i++ ) {
693 delete temp_samples_buffer[j][i];
695 delete [] temp_samples_buffer[j];
698 delete [] temp_samples_buffer;
699 temp_samples_buffer = 0;
700 audio_ring_buffers = 0;
704 void File::delete_temp_frame_buffer()
707 if( temp_frame_buffer ) {
708 for( int k = 0; k < video_ring_buffers; k++ ) {
709 for( int i = 0; i < asset->layers; i++ ) {
710 for( int j = 0; j < video_buffer_size; j++ ) {
711 delete temp_frame_buffer[k][i][j];
713 delete [] temp_frame_buffer[k][i];
715 delete [] temp_frame_buffer[k];
718 delete [] temp_frame_buffer;
719 temp_frame_buffer = 0;
720 video_ring_buffers = 0;
721 video_buffer_size = 0;
725 int File::close_file(int ignore_thread)
727 if( !ignore_thread ) {
732 // The file's asset is a copy of the argument passed to open_file so the
733 // user must copy lengths from the file's asset.
735 asset->audio_length = current_sample;
736 asset->video_length = current_frame;
743 delete_temp_samples_buffer();
744 delete_temp_frame_buffer();
749 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
751 return !file ? -1 : file->get_index(index_file, progress_bar);
754 int File::start_audio_thread(int buffer_size, int ring_buffers)
756 this->audio_ring_buffers = ring_buffers;
759 if( !audio_thread ) {
760 audio_thread = new FileThread(this, 1, 0);
761 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
766 int File::start_video_thread(int buffer_size, int color_model,
767 int ring_buffers, int compressed)
769 this->video_ring_buffers = ring_buffers;
770 this->video_buffer_size = buffer_size;
772 if( !video_thread ) {
773 video_thread = new FileThread(this, 0, 1);
774 video_thread->start_writing(buffer_size, color_model,
775 ring_buffers, compressed);
780 int File::start_video_decode_thread()
782 // Currently, CR2 is the only one which won't work asynchronously, so
783 // we're not using a virtual function yet.
784 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
785 video_thread = new FileThread(this, 0, 1);
786 video_thread->start_reading();
792 int File::stop_audio_thread()
795 audio_thread->stop_writing();
802 int File::stop_video_thread()
805 video_thread->stop_reading();
806 video_thread->stop_writing();
813 FileThread* File::get_video_thread()
818 int File::set_channel(int channel)
820 if( file && channel < asset->channels ) {
821 current_channel = channel;
828 int File::get_channel()
830 return current_channel;
833 // if no>=0, sets new program
834 // returns current program
835 int File::set_program(int no)
837 int result = file ? file->set_program(no) : current_program;
838 current_program = no < 0 ? result : no;
842 int File::get_cell_time(int no, double &time)
844 return file ? file->get_cell_time(no, time) : -1;
847 int File::get_system_time(int64_t &tm)
849 return file ? file->get_system_time(tm) : -1;
852 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
854 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
857 int File::get_video_pid(int track)
859 return file ? file->get_video_pid(track) : -1;
864 int File::get_video_info(int track, int &pid, double &framerate,
865 int &width, int &height, char *title)
868 file->get_video_info(track, pid, framerate, width, height, title);
871 int File::select_video_stream(Asset *asset, int vstream)
874 file->select_video_stream(asset, vstream);
877 int File::select_audio_stream(Asset *asset, int astream)
880 file->select_audio_stream(asset, astream);
884 int File::get_thumbnail(int stream,
885 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
887 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
890 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
892 return file->set_skimming(track, skim, fn, vp);
895 int File::skim_video(int track, void *vp, skim_fn fn)
897 return file->skim_video(track, vp, fn);
901 int File::set_layer(int layer, int is_thread)
903 if( file && layer < asset->layers ) {
904 if( !is_thread && video_thread ) {
905 video_thread->set_layer(layer);
908 current_layer = layer;
916 int64_t File::get_audio_length()
918 int64_t result = asset->audio_length;
919 int64_t base_samplerate = -1;
921 if( base_samplerate > 0 )
922 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
930 int64_t File::get_video_length()
932 int64_t result = asset->video_length;
933 float base_framerate = -1;
935 if( base_framerate > 0 )
936 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
941 return -1; // infinity
945 int64_t File::get_video_position()
947 float base_framerate = -1;
948 if( base_framerate > 0 )
949 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
951 return current_frame;
954 int64_t File::get_audio_position()
956 // int64_t base_samplerate = -1;
957 // if( base_samplerate > 0 )
959 // if( normalized_sample_rate == base_samplerate )
960 // return normalized_sample;
962 // return (int64_t)((double)current_sample /
963 // asset->sample_rate *
968 return current_sample;
973 int File::set_audio_position(int64_t position)
977 if( !file ) return 1;
979 #define REPOSITION(x, y) \
980 (labs((x) - (y)) > 1)
982 float base_samplerate = asset->sample_rate;
983 current_sample = normalized_sample = position;
985 // printf("File::set_audio_position %d normalized_sample=%ld\n",
987 // normalized_sample);
988 result = file->set_audio_position(current_sample);
991 printf("File::set_audio_position position=%jd"
992 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
993 position, base_samplerate, asset, asset->sample_rate);
995 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1000 int File::set_video_position(int64_t position,
1004 if( !file ) return 0;
1006 // Convert to file's rate
1007 // if( base_framerate > 0 )
1008 // position = (int64_t)((double)position /
1010 // asset->frame_rate +
1014 if( video_thread && !is_thread ) {
1015 // Call thread. Thread calls this again to set the file state.
1016 video_thread->set_video_position(position);
1019 if( current_frame != position ) {
1021 current_frame = position;
1022 result = file->set_video_position(current_frame);
1029 // No resampling here.
1030 int File::write_samples(Samples **buffer, int64_t len)
1035 write_lock->lock("File::write_samples");
1037 // Convert to arrays for backwards compatability
1038 double *temp[asset->channels];
1039 for( int i = 0; i < asset->channels; i++ ) {
1040 temp[i] = buffer[i]->get_data();
1043 result = file->write_samples(temp, len);
1044 current_sample += len;
1045 normalized_sample += len;
1046 asset->audio_length += len;
1047 write_lock->unlock();
1056 // Can't put any cmodel abstraction here because the filebase couldn't be
1058 int File::write_frames(VFrame ***frames, int len)
1060 //printf("File::write_frames %d\n", __LINE__);
1062 // Store the counters in temps so the filebase can choose to overwrite them.
1064 int current_frame_temp = current_frame;
1065 int video_length_temp = asset->video_length;
1067 write_lock->lock("File::write_frames");
1070 result = file->write_frames(frames, len);
1073 current_frame = current_frame_temp + len;
1074 asset->video_length = video_length_temp + len;
1075 write_lock->unlock();
1080 // Only called by FileThread
1081 int File::write_compressed_frame(VFrame *buffer)
1084 write_lock->lock("File::write_compressed_frame");
1085 result = file->write_compressed_frame(buffer);
1087 asset->video_length++;
1088 write_lock->unlock();
1093 int File::write_audio_buffer(int64_t len)
1096 if( audio_thread ) {
1097 result = audio_thread->write_buffer(len);
1102 int File::write_video_buffer(int64_t len)
1105 if( video_thread ) {
1106 result = video_thread->write_buffer(len);
1112 Samples** File::get_audio_buffer()
1114 if( audio_thread ) return audio_thread->get_audio_buffer();
1118 VFrame*** File::get_video_buffer()
1120 if( video_thread ) {
1121 VFrame*** result = video_thread->get_video_buffer();
1130 int File::read_samples(Samples *samples, int64_t len)
1132 // Never try to read more samples than exist in the file
1133 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1134 len = asset->audio_length - current_sample;
1136 if( len <= 0 ) return 0;
1139 const int debug = 0;
1140 if( debug ) PRINT_TRACE
1142 if( debug ) PRINT_TRACE
1144 double *buffer = samples->get_data();
1146 int64_t base_samplerate = asset->sample_rate;
1149 // Resample recursively calls this with the asset sample rate
1150 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1152 if( debug ) PRINT_TRACE
1153 result = file->read_samples(buffer, len);
1155 if( debug ) PRINT_TRACE
1156 current_sample += len;
1158 normalized_sample += len;
1160 if( debug ) PRINT_TRACE
1165 int File::read_frame(VFrame *frame, int is_thread)
1167 const int debug = 0;
1168 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1169 // current_frame, use_cache, asset->single_frame);
1170 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1171 if( !file ) return 1;
1172 if( debug ) PRINT_TRACE
1174 int supported_colormodel = colormodel_supported(frame->get_color_model());
1176 // if reverse playback reading, use_cache is -1
1177 int cache_active = asset->single_frame ? 1 : use_cache;
1178 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1181 if( cache_active ) {
1182 if( get_cache_frame(frame, cache_position) ) {
1185 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1186 // reverse reading and cache miss, clear cache for new readahead
1192 VFrame *vframe = frame;
1193 if( frame->get_color_model() != BC_COMPRESSED &&
1194 (supported_colormodel != frame->get_color_model() ||
1195 (!file->can_scale_input() &&
1196 (frame->get_w() != asset->width ||
1197 frame->get_h() != asset->height))) ) {
1199 if( !temp_frame->params_match(asset->width, asset->height,
1200 supported_colormodel) ) {
1201 delete temp_frame; temp_frame = 0;
1205 temp_frame = new VFrame(asset->width, asset->height,
1206 supported_colormodel, 0);
1207 temp_frame->clear_frame();
1210 temp_frame->copy_stacks(frame);
1211 vframe = temp_frame;
1213 result = file->read_frame(vframe);
1215 if( frame != vframe )
1216 frame->transfer_from(vframe);
1217 if( cache_active > 0 )
1218 put_cache_frame(frame, cache_position, 1);
1220 else if( frame->get_status() > 0 )
1221 frame->set_status(-1);
1222 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1225 if( result && !current_frame )
1226 frame->clear_frame();
1228 //printf("File::read_frame %d\n", __LINE__);
1230 if( do_read ) current_frame++;
1231 if( debug ) PRINT_TRACE
1235 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1237 if( asset && file ) {
1238 return asset->width == output_w &&
1239 asset->height == output_h &&
1240 file->can_copy_from(asset, position);
1245 // Fill in queries about formats when adding formats here.
1248 int File::strtoformat(const char *format)
1250 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1251 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1252 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1253 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1254 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1255 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1256 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1257 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1258 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1259 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1260 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1261 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1262 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1263 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1264 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1265 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1266 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1267 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1268 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1269 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1270 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1271 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1272 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1273 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1274 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1275 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1276 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1277 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1278 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1279 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1280 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1281 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1282 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1288 const char* File::formattostr(int format)
1291 case FILE_SCENE: return _(SCENE_NAME);
1292 case FILE_AC3: return _(AC3_NAME);
1293 case FILE_WAV: return _(WAV_NAME);
1294 case FILE_PCM: return _(PCM_NAME);
1295 case FILE_AU: return _(AU_NAME);
1296 case FILE_AIFF: return _(AIFF_NAME);
1297 case FILE_SND: return _(SND_NAME);
1298 case FILE_PNG: return _(PNG_NAME);
1299 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1300 case FILE_PPM: return _(PPM_NAME);
1301 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1302 case FILE_JPEG: return _(JPEG_NAME);
1303 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1304 case FILE_CR2: return _(CR2_NAME);
1305 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1306 case FILE_FLAC: return _(FLAC_NAME);
1307 case FILE_GIF: return _(GIF_NAME);
1308 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1309 case FILE_EXR: return _(EXR_NAME);
1310 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1311 #ifdef HAVE_LIBZMPEG
1312 case FILE_MPEG: return _(MPEG_NAME);
1314 case FILE_AMPEG: return _(AMPEG_NAME);
1315 case FILE_VMPEG: return _(VMPEG_NAME);
1316 case FILE_TGA: return _(TGA_NAME);
1317 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1318 case FILE_TIFF: return _(TIFF_NAME);
1319 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1320 case FILE_OGG: return _(OGG_NAME);
1321 case FILE_VORBIS: return _(VORBIS_NAME);
1322 case FILE_RAWDV: return _(RAWDV_NAME);
1323 case FILE_FFMPEG: return _(FFMPEG_NAME);
1324 case FILE_DB: return _(DBASE_NAME);
1325 case FILE_REF: return _(REF_NAME);
1327 return _("Unknown");
1330 int File::strtobits(const char *bits)
1332 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1333 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1334 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1335 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1336 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1337 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1338 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1339 return BITSLINEAR16;
1342 const char* File::bitstostr(int bits)
1344 //printf("File::bitstostr\n");
1346 case BITSLINEAR8: return (NAME_8BIT);
1347 case BITSLINEAR16: return (NAME_16BIT);
1348 case BITSLINEAR24: return (NAME_24BIT);
1349 case BITSLINEAR32: return (NAME_32BIT);
1350 case BITSULAW: return (NAME_ULAW);
1351 case BITS_ADPCM: return (NAME_ADPCM);
1352 case BITSFLOAT: return (NAME_FLOAT);
1354 return _("Unknown");
1359 int File::str_to_byteorder(const char *string)
1361 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1365 const char* File::byteorder_to_str(int byte_order)
1367 if( byte_order ) return _("Lo Hi");
1371 int File::bytes_per_sample(int bits)
1374 case BITSLINEAR8: return 1;
1375 case BITSLINEAR16: return 2;
1376 case BITSLINEAR24: return 3;
1377 case BITSLINEAR32: return 4;
1378 case BITSULAW: return 1;
1384 int File::get_best_colormodel(int driver, int vstream)
1386 return file ? file->get_best_colormodel(driver, vstream) :
1387 get_best_colormodel(asset, driver);
1390 int File::get_best_colormodel(Asset *asset, int driver)
1392 switch( asset->format ) {
1394 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1396 #ifdef HAVE_LIBZMPEG
1397 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1400 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1403 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1406 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1408 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1410 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1412 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1413 #ifdef HAVE_COMMERCIAL
1414 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1416 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1417 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1424 int File::colormodel_supported(int colormodel)
1427 return file->colormodel_supported(colormodel);
1433 int64_t File::file_memory_usage()
1435 return file ? file->base_memory_usage() : 0;
1438 int64_t File::get_memory_usage()
1442 result += file_memory_usage();
1443 if( temp_frame ) result += temp_frame->get_data_size();
1444 result += frame_cache->get_memory_usage();
1445 if( video_thread ) result += video_thread->get_memory_usage();
1447 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1452 int File::renders_video(int format)
1457 case FILE_JPEG_LIST:
1471 case FILE_TIFF_LIST:
1479 int File::renders_video(Asset *asset)
1481 return asset->format == FILE_FFMPEG ?
1482 FFMPEG::renders_video(asset->fformat) :
1483 renders_video(asset->format);
1486 int File::renders_audio(int format)
1505 int File::renders_audio(Asset *asset)
1507 return asset->format == FILE_FFMPEG ?
1508 FFMPEG::renders_audio(asset->fformat) :
1509 renders_audio(asset->format);
1512 int File::is_image_render(int format)
1527 const char* File::get_tag(int format)
1530 case FILE_AC3: return "ac3";
1531 case FILE_AIFF: return "aif";
1532 case FILE_AMPEG: return "mp3";
1533 case FILE_AU: return "au";
1534 case FILE_RAWDV: return "dv";
1535 case FILE_DB: return "db";
1536 case FILE_EXR: return "exr";
1537 case FILE_EXR_LIST: return "exrs";
1538 case FILE_FLAC: return "flac";
1539 case FILE_JPEG: return "jpg";
1540 case FILE_JPEG_LIST: return "jpgs";
1541 case FILE_OGG: return "ogg";
1542 case FILE_GIF: return "gif";
1543 case FILE_GIF_LIST: return "gifs";
1544 case FILE_PCM: return "pcm";
1545 case FILE_PNG: return "png";
1546 case FILE_PNG_LIST: return "pngs";
1547 case FILE_PPM: return "ppm";
1548 case FILE_PPM_LIST: return "ppms";
1549 case FILE_TGA: return "tga";
1550 case FILE_TGA_LIST: return "tgas";
1551 case FILE_TIFF: return "tif";
1552 case FILE_TIFF_LIST: return "tifs";
1553 case FILE_VMPEG: return "m2v";
1554 case FILE_VORBIS: return "ogg";
1555 case FILE_WAV: return "wav";
1556 case FILE_FFMPEG: return "ffmpg";
1557 case FILE_REF: return "ref";
1562 const char* File::get_prefix(int format)
1565 case FILE_PCM: return "PCM";
1566 case FILE_WAV: return "WAV";
1567 case FILE_PNG: return "PNG";
1568 case FILE_PPM: return "PPM";
1569 case FILE_JPEG: return "JPEG";
1570 case FILE_TIFF: return "TIFF";
1571 case FILE_GIF: return "GIF";
1572 case FILE_JPEG_LIST: return "JPEG_LIST";
1573 case FILE_AU: return "AU";
1574 case FILE_AIFF: return "AIFF";
1575 case FILE_SND: return "SND";
1576 case FILE_TGA_LIST: return "TGA_LIST";
1577 case FILE_TGA: return "TGA";
1578 case FILE_MPEG: return "MPEG";
1579 case FILE_AMPEG: return "AMPEG";
1580 case FILE_VMPEG: return "VMPEG";
1581 case FILE_RAWDV: return "RAWDV";
1582 case FILE_TIFF_LIST: return "TIFF_LIST";
1583 case FILE_PNG_LIST: return "PNG_LIST";
1584 case FILE_PPM_LIST: return "PPM_LIST";
1585 case FILE_AC3: return "AC3";
1586 case FILE_EXR: return "EXR";
1587 case FILE_EXR_LIST: return "EXR_LIST";
1588 case FILE_CR2: return "CR2";
1589 case FILE_OGG: return "OGG";
1590 case FILE_VORBIS: return "VORBIS";
1591 case FILE_FLAC: return "FLAC";
1592 case FILE_FFMPEG: return "FFMPEG";
1593 case FILE_SCENE: return "SCENE";
1594 case FILE_CR2_LIST: return "CR2_LIST";
1595 case FILE_GIF_LIST: return "GIF_LIST";
1596 case FILE_DB: return "DB";
1597 case FILE_REF: return "REF";
1599 return _("UNKNOWN");
1603 int File::record_fd()
1605 return file ? file->record_fd() : -1;
1609 void File::get_exe_path(char *result, char *bnp)
1611 // Get executable path, basename
1612 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1615 char *ptr = strrchr(result, '/');
1616 if( ptr ) *ptr++ = 0; else ptr = result;
1617 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1625 void File::getenv_path(char *result, const char *path)
1627 char *rp = result, *ep = rp + BCTEXTLEN-1;
1628 const char *cp = path;
1629 // any env var can be used here to expand a path as:
1630 // "path...$id...": and id is a word as alpha,alnum...
1631 // expands as "path...getenv(id)..."
1632 // CIN_PATH, CIN_LIB are set in main.C,
1633 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1634 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1635 const char *bp = cp; char *p = rp;
1636 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1638 const char *envp = getenv(rp); // read env value
1639 if( !envp ) continue;
1640 while( *envp && rp < ep ) *rp++ = *envp++;
1641 cp = bp; // subst id=value
1649 void File::setenv_path(const char *var, const char *path, int overwrite)
1651 char env_path[BCTEXTLEN];
1652 getenv_path(env_path, path);
1653 setenv(var, env_path, overwrite);
1656 * @brief Set various environment variables to pass constant values to
1657 * forks, shell scripts, and other parts of CinGG.
1659 void File::init_cin_env_vars()
1661 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1663 struct tm *dateTime;
1664 get_exe_path(env_path, env_pkg);
1665 setenv_path("CIN_PATH", env_path, 1);
1666 setenv_path("CIN_PKG", env_pkg, 1);
1667 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1668 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1669 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1670 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1671 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1672 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1673 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1674 // Create env_var CINGG_BUILD for use when running as AppImage
1675 stat(env_path, &st);
1676 dateTime = gmtime(&st.st_mtime);
1677 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1678 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1679 build_name[sizeof(build_name) - 1] = 0;
1680 setenv_path("CINGG_BUILD", build_name, 1);