3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4 * Copyright (C) 2003-2016 Cinelerra CV contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 // work arounds (centos)
34 #define INT64_MAX 9223372036854775807LL
39 #include "bcsignals.h"
40 #include "byteorder.h"
42 #include "condition.h"
51 #include "fileffmpeg.h"
57 #undef HAVE_STDLIB_H // automake conflict
62 #include "filescene.h"
63 #include "filesndfile.h"
65 #include "filethread.h"
67 #include "filevorbis.h"
69 #include "formatwindow.h"
70 #include "formattools.h"
71 #include "framecache.h"
73 #include "mainprogress.inc"
76 #include "packagingengine.h"
77 #include "pluginserver.h"
78 #include "preferences.h"
79 #include "probeprefs.h"
84 //suppress noref warning
85 void *vorbis0_ov_callbacks[] = {
86 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
87 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
95 format_completion = new Condition(1, "File::format_completion");
96 write_lock = new Condition(1, "File::write_lock");
97 frame_cache = new FrameCache;
103 if( getting_options ) {
104 if( format_window ) format_window->set_done(0);
105 format_completion->lock("File::~File");
106 format_completion->unlock();
115 asset->Garbage::remove_user();
116 delete format_completion;
121 void File::reset_parameters()
134 normalized_sample = 0;
137 playback_subtitle = -1;
141 temp_samples_buffer = 0;
142 temp_frame_buffer = 0;
143 current_frame_buffer = 0;
144 audio_ring_buffers = 0;
145 video_ring_buffers = 0;
146 video_buffer_size = 0;
149 int File::raise_window()
151 if( getting_options && format_window ) {
152 format_window->raise_window();
153 format_window->flush();
158 void File::close_window()
160 if( getting_options ) {
161 format_window->lock_window("File::close_window");
162 format_window->set_done(1);
163 format_window->unlock_window();
168 int File::get_options(FormatTools *format,
169 int audio_options, int video_options)
171 BC_WindowBase *parent_window = format->window;
172 Asset *asset = format->asset;
173 EDL *edl = format->mwindow ? format->mwindow->edl : 0;
176 format_completion->lock("File::get_options");
177 switch( asset->format ) {
178 #ifdef HAVE_CIN_3RDPARTY
180 case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
181 audio_options, video_options, edl);
187 FileDV::get_parameters(parent_window, asset, format_window,
188 audio_options, video_options, edl);
196 FileSndFile::get_parameters(parent_window, asset, format_window,
197 audio_options, video_options, edl);
200 FileFFMPEG::get_parameters(parent_window, asset, format_window,
201 audio_options, video_options, edl);
206 FileMPEG::get_parameters(parent_window, asset, format_window,
207 audio_options, video_options, edl);
212 FileJPEG::get_parameters(parent_window, asset, format_window,
213 audio_options, video_options, edl);
218 FileDPX::get_parameters(parent_window, asset, format_window,
219 audio_options, video_options, edl);
225 FileEXR::get_parameters(parent_window, asset, format_window,
226 audio_options, video_options, edl);
230 FileFLAC::get_parameters(parent_window, asset, format_window,
231 audio_options, video_options, edl);
235 FilePNG::get_parameters(parent_window, asset, format_window,
236 audio_options, video_options, edl);
240 FilePPM::get_parameters(parent_window, asset, format_window,
241 audio_options, video_options, edl);
245 FileTGA::get_parameters(parent_window, asset, format_window,
246 audio_options, video_options, edl);
250 FileTIFF::get_parameters(parent_window, asset, format_window,
251 audio_options, video_options, edl);
255 FileOGG::get_parameters(parent_window, asset, format_window,
256 audio_options, video_options, edl);
263 if( !format_window ) {
264 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
265 parent_window->get_abs_cursor_x(1),
266 parent_window->get_abs_cursor_y(1));
267 format_window = errorbox;
270 errorbox->create_objects(_("This format doesn't support audio."));
273 errorbox->create_objects(_("This format doesn't support video."));
274 errorbox->run_window();
280 format_completion->unlock();
284 int File::can_scale_input(Asset *asset)
286 switch( asset->format ) {
297 if( asset->video_length < 0 )
304 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
306 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
307 cpus = 8; // 8 cpus ought to decode just about anything
308 // Set all instances so gets work.
314 int File::set_preload(int64_t size)
316 this->playback_preload = size;
320 void File::set_subtitle(int value)
322 this->playback_subtitle = value;
323 if( file ) file->set_subtitle(value);
326 void File::set_interpolate_raw(int value)
328 this->interpolate_raw = value;
331 void File::set_white_balance_raw(int value)
333 this->white_balance_raw = value;
336 void File::set_cache_frames(int value)
338 // caching only done locally
343 int File::purge_cache()
345 // caching only done locally
347 if( frame_cache->cache_items() > 0 ) {
348 frame_cache->remove_all();
354 int File::delete_oldest()
356 // return the number of bytes freed
357 return frame_cache->delete_oldest();
360 int File::get_cache_frame(VFrame *frame, int64_t position)
362 return frame_cache->get_cache_frame(frame, position,
363 current_layer, asset->frame_rate);
366 void File::put_cache_frame(VFrame *frame, int64_t position, int use_copy)
368 frame_cache->put_cache_frame(frame,
369 position, current_layer, asset->frame_rate, use_copy);
372 int File::get_use_cache()
378 // file driver in order of probe precidence
379 // can be reordered in preferences->interface
380 const char *File::default_probes[] = {
414 const int File::nb_probes =
415 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
420 FILE *fp = fopen(this->asset->path, "rb");
421 if( !fp ) return FILE_NOT_FOUND;
423 memset(data,0,sizeof(data));
424 int ret = fread(data, 1, 16, fp);
426 if( !ret ) return FILE_NOT_FOUND;
428 for( int i=0; i<preferences->file_probes.size(); ++i ) {
429 ProbePref *pref = preferences->file_probes[i];
430 if( !pref->armed ) continue;
431 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
432 if( !FileFFMPEG::check_sig(this->asset) ) continue;
433 file = new FileFFMPEG(this->asset, this);
436 #ifdef HAVE_COMMERCIAL
437 if( !strcmp(pref->name,"DB") ) { // MediaDB
438 if( !FileDB::check_sig(this->asset) ) continue;
439 file = new FileDB(this->asset, this);
443 if( !strcmp(pref->name,"Scene") ) { // scene file
444 if( !FileScene::check_sig(this->asset, data)) continue;
445 file = new FileScene(this->asset, this);
449 if( !strcmp(pref->name,"DV") ) { // libdv
450 if( !FileDV::check_sig(this->asset) ) continue;
451 file = new FileDV(this->asset, this);
455 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
456 if( !FileSndFile::check_sig(this->asset) ) continue;
457 file = new FileSndFile(this->asset, this);
460 if( !strcmp(pref->name,"PNG") ) { // PNG file
461 if( !FilePNG::check_sig(this->asset) ) continue;
462 file = new FilePNG(this->asset, this);
465 if( !strcmp(pref->name,"PPM") ) { // PPM file
466 if( !FilePPM::check_sig(this->asset) ) continue;
467 file = new FilePPM(this->asset, this);
470 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
471 if( !FileJPEG::check_sig(this->asset) ) continue;
472 file = new FileJPEG(this->asset, this);
476 if( !strcmp(pref->name,"GIF") ) { // GIF file
477 if( FileGIFList::check_sig(this->asset) )
478 file = new FileGIFList(this->asset, this);
479 else if( FileGIF::check_sig(this->asset) )
480 file = new FileGIF(this->asset, this);
486 if( !strcmp(pref->name,"DPX") ) { // DPX file
487 if( !FileDPX::check_sig(this->asset, data) ) continue;
488 file = new FileDPX(this->asset, this);
493 if( !strcmp(pref->name,"EXR") ) { // EXR file
494 if( !FileEXR::check_sig(this->asset, data)) continue;
495 file = new FileEXR(this->asset, this);
499 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
500 if( !FileFLAC::check_sig(this->asset, data)) continue;
501 file = new FileFLAC(this->asset, this);
504 if( !strcmp(pref->name,"CR2") ) { // CR2 file
505 if( !FileCR2::check_sig(this->asset)) continue;
506 file = new FileCR2(this->asset, this);
509 if( !strcmp(pref->name,"TGA") ) { // TGA file
510 if( !FileTGA::check_sig(this->asset) ) continue;
511 file = new FileTGA(this->asset, this);
514 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
515 if( !FileTIFF::check_sig(this->asset) ) continue;
516 file = new FileTIFF(this->asset, this);
520 if( !strcmp(pref->name,"OGG") ) { // OGG file
521 if( !FileOGG::check_sig(this->asset) ) continue;
522 file = new FileOGG(this->asset, this);
525 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
526 if( !FileVorbis::check_sig(this->asset) ) continue;
527 file = new FileVorbis(this->asset, this);
532 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
533 if( !FileMPEG::check_sig(this->asset) ) continue;
534 file = new FileMPEG(this->asset, this);
538 if( !strcmp(pref->name,"EDL") ) { // XML file
539 if( data[0] != '<' ) continue;
540 if( !strncmp(&data[1],"EDL>",4) ||
541 !strncmp(&data[1],"HTAL>",5) ||
542 !strncmp(&data[1],"?xml",4) )
546 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
549 int File::open_file(Preferences *preferences,
556 this->preferences = preferences;
557 this->asset->copy_from(asset, 1);
562 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
564 switch( this->asset->format ) {
565 // get the format now
566 // If you add another format to case 0, you also need to add another case for the
567 // file format #define.
570 if( ret != FILE_OK ) return ret;
572 // format already determined
573 #ifdef HAVE_CIN_3RDPARTY
576 file = new FileAC3(this->asset, this);
581 file = new FileScene(this->asset, this);
585 file = new FileFFMPEG(this->asset, this);
593 //printf("File::open_file 1\n");
594 file = new FileSndFile(this->asset, this);
599 file = new FilePNG(this->asset, this);
604 file = new FilePPM(this->asset, this);
609 file = new FileJPEG(this->asset, this);
613 file = new FileGIF(this->asset, this);
616 file = new FileGIFList(this->asset, this);
622 file = new FileDPX(this->asset, this);
628 file = new FileEXR(this->asset, this);
633 file = new FileFLAC(this->asset, this);
638 file = new FileCR2(this->asset, this);
643 file = new FileTGA(this->asset, this);
648 file = new FileTIFF(this->asset, this);
650 #ifdef HAVE_COMMERCIAL
652 file = new FileDB(this->asset, this);
660 file = new FileMPEG(this->asset, this);
665 file = new FileOGG(this->asset, this);
669 file = new FileVorbis(this->asset, this);
674 file = new FileDV(this->asset, this);
678 file = new FileREF(this->asset, this);
687 // Reopen file with correct parser and get header.
688 if( file->open_file(rd, wr) ) {
689 delete file; file = 0;
690 return FILE_NOT_FOUND;
694 // If file type is a list verify that all files match in dimensions.
695 // Should be done only after the file open function has been performed
696 // Reason: although this function checks if file exists or not but
697 // it has no way of relaying this information back and if this function
698 // is called before open_file the program may accidently interpret file
699 // not found as file size don't match
700 if( !file->verify_file_list() ) {
701 delete file; file = 0;
702 return FILE_SIZE_DONT_MATCH;
706 // Set extra writing parameters to mandatory settings.
708 if( this->asset->dither ) file->set_dither();
712 // one frame image file, not brender, no specific length
713 if( !this->asset->audio_data && this->asset->use_header &&
714 this->asset->video_data && !this->asset->single_frame &&
715 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
716 this->asset->single_frame = 1;
717 this->asset->video_length = -1;
721 // Synchronize header parameters
722 asset->copy_from(this->asset, 1);
725 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
731 void File::delete_temp_samples_buffer()
734 if( temp_samples_buffer ) {
735 for( int j = 0; j < audio_ring_buffers; j++ ) {
736 for( int i = 0; i < asset->channels; i++ ) {
737 delete temp_samples_buffer[j][i];
739 delete [] temp_samples_buffer[j];
742 delete [] temp_samples_buffer;
743 temp_samples_buffer = 0;
744 audio_ring_buffers = 0;
748 void File::delete_temp_frame_buffer()
751 if( temp_frame_buffer ) {
752 for( int k = 0; k < video_ring_buffers; k++ ) {
753 for( int i = 0; i < asset->layers; i++ ) {
754 for( int j = 0; j < video_buffer_size; j++ ) {
755 delete temp_frame_buffer[k][i][j];
757 delete [] temp_frame_buffer[k][i];
759 delete [] temp_frame_buffer[k];
762 delete [] temp_frame_buffer;
763 temp_frame_buffer = 0;
764 video_ring_buffers = 0;
765 video_buffer_size = 0;
769 int File::close_file(int ignore_thread)
771 if( !ignore_thread ) {
776 // The file's asset is a copy of the argument passed to open_file so the
777 // user must copy lengths from the file's asset.
779 asset->audio_length = current_sample;
780 asset->video_length = current_frame;
787 delete_temp_samples_buffer();
788 delete_temp_frame_buffer();
793 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
795 return !file ? -1 : file->get_index(index_file, progress_bar);
798 int File::start_audio_thread(int buffer_size, int ring_buffers)
800 this->audio_ring_buffers = ring_buffers;
803 if( !audio_thread ) {
804 audio_thread = new FileThread(this, 1, 0);
805 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
810 int File::start_video_thread(int buffer_size, int color_model,
811 int ring_buffers, int compressed)
813 this->video_ring_buffers = ring_buffers;
814 this->video_buffer_size = buffer_size;
816 if( !video_thread ) {
817 video_thread = new FileThread(this, 0, 1);
818 video_thread->start_writing(buffer_size, color_model,
819 ring_buffers, compressed);
824 int File::start_video_decode_thread()
826 // Currently, CR2 is the only one which won't work asynchronously, so
827 // we're not using a virtual function yet.
828 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
829 video_thread = new FileThread(this, 0, 1);
830 video_thread->start_reading();
836 int File::stop_audio_thread()
839 audio_thread->stop_writing();
846 int File::stop_video_thread()
849 video_thread->stop_reading();
850 video_thread->stop_writing();
857 FileThread* File::get_video_thread()
862 int File::set_channel(int channel)
864 if( file && channel < asset->channels ) {
865 current_channel = channel;
872 int File::get_channel()
874 return current_channel;
877 // if no>=0, sets new program
878 // returns current program
879 int File::set_program(int no)
881 int result = file ? file->set_program(no) : current_program;
882 current_program = no < 0 ? result : no;
886 int File::get_cell_time(int no, double &time)
888 return file ? file->get_cell_time(no, time) : -1;
891 int File::get_system_time(int64_t &tm)
893 return file ? file->get_system_time(tm) : -1;
896 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
898 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
901 int File::get_video_pid(int track)
903 return file ? file->get_video_pid(track) : -1;
908 int File::get_video_info(int track, int &pid, double &framerate,
909 int &width, int &height, char *title)
912 file->get_video_info(track, pid, framerate, width, height, title);
915 int File::select_video_stream(Asset *asset, int vstream)
918 file->select_video_stream(asset, vstream);
921 int File::select_audio_stream(Asset *asset, int astream)
924 file->select_audio_stream(asset, astream);
928 int File::get_thumbnail(int stream,
929 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
931 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
934 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
936 return file->set_skimming(track, skim, fn, vp);
939 int File::skim_video(int track, void *vp, skim_fn fn)
941 return file->skim_video(track, vp, fn);
945 int File::set_layer(int layer, int is_thread)
947 if( file && layer < asset->layers ) {
948 if( !is_thread && video_thread ) {
949 video_thread->set_layer(layer);
952 current_layer = layer;
960 int64_t File::get_audio_length()
962 int64_t result = asset->audio_length;
963 int64_t base_samplerate = -1;
965 if( base_samplerate > 0 )
966 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
974 int64_t File::get_video_length()
976 int64_t result = asset->video_length;
977 float base_framerate = -1;
979 if( base_framerate > 0 )
980 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
985 return -1; // infinity
989 int64_t File::get_video_position()
991 float base_framerate = -1;
992 if( base_framerate > 0 )
993 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
995 return current_frame;
998 int64_t File::get_audio_position()
1000 // int64_t base_samplerate = -1;
1001 // if( base_samplerate > 0 )
1003 // if( normalized_sample_rate == base_samplerate )
1004 // return normalized_sample;
1006 // return (int64_t)((double)current_sample /
1007 // asset->sample_rate *
1008 // base_samplerate +
1012 return current_sample;
1017 int File::set_audio_position(int64_t position)
1021 if( !file ) return 1;
1023 #define REPOSITION(x, y) \
1024 (labs((x) - (y)) > 1)
1026 float base_samplerate = asset->sample_rate;
1027 current_sample = normalized_sample = position;
1029 // printf("File::set_audio_position %d normalized_sample=%ld\n",
1031 // normalized_sample);
1032 result = file->set_audio_position(current_sample);
1035 printf("File::set_audio_position position=%jd"
1036 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
1037 position, base_samplerate, asset, asset->sample_rate);
1039 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1044 int File::set_video_position(int64_t position,
1048 if( !file ) return 0;
1050 // Convert to file's rate
1051 // if( base_framerate > 0 )
1052 // position = (int64_t)((double)position /
1054 // asset->frame_rate +
1058 if( video_thread && !is_thread ) {
1059 // Call thread. Thread calls this again to set the file state.
1060 video_thread->set_video_position(position);
1063 if( current_frame != position ) {
1065 current_frame = position;
1066 result = file->set_video_position(current_frame);
1073 // No resampling here.
1074 int File::write_samples(Samples **buffer, int64_t len)
1079 write_lock->lock("File::write_samples");
1081 // Convert to arrays for backwards compatability
1082 double *temp[asset->channels];
1083 for( int i = 0; i < asset->channels; i++ ) {
1084 temp[i] = buffer[i]->get_data();
1087 result = file->write_samples(temp, len);
1088 current_sample += len;
1089 normalized_sample += len;
1090 asset->audio_length += len;
1091 write_lock->unlock();
1100 // Can't put any cmodel abstraction here because the filebase couldn't be
1102 int File::write_frames(VFrame ***frames, int len)
1104 //printf("File::write_frames %d\n", __LINE__);
1106 // Store the counters in temps so the filebase can choose to overwrite them.
1108 int current_frame_temp = current_frame;
1109 int video_length_temp = asset->video_length;
1111 write_lock->lock("File::write_frames");
1114 result = file->write_frames(frames, len);
1117 current_frame = current_frame_temp + len;
1118 asset->video_length = video_length_temp + len;
1119 write_lock->unlock();
1124 // Only called by FileThread
1125 int File::write_compressed_frame(VFrame *buffer)
1128 write_lock->lock("File::write_compressed_frame");
1129 result = file->write_compressed_frame(buffer);
1131 asset->video_length++;
1132 write_lock->unlock();
1137 int File::write_audio_buffer(int64_t len)
1140 if( audio_thread ) {
1141 result = audio_thread->write_buffer(len);
1146 int File::write_video_buffer(int64_t len)
1149 if( video_thread ) {
1150 result = video_thread->write_buffer(len);
1156 Samples** File::get_audio_buffer()
1158 if( audio_thread ) return audio_thread->get_audio_buffer();
1162 VFrame*** File::get_video_buffer()
1164 if( video_thread ) {
1165 VFrame*** result = video_thread->get_video_buffer();
1174 int File::read_samples(Samples *samples, int64_t len)
1176 // Never try to read more samples than exist in the file
1177 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1178 len = asset->audio_length - current_sample;
1180 if( len <= 0 ) return 0;
1183 const int debug = 0;
1184 if( debug ) PRINT_TRACE
1186 if( debug ) PRINT_TRACE
1188 double *buffer = samples->get_data();
1190 int64_t base_samplerate = asset->sample_rate;
1193 // Resample recursively calls this with the asset sample rate
1194 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1196 if( debug ) PRINT_TRACE
1197 result = file->read_samples(buffer, len);
1199 if( debug ) PRINT_TRACE
1200 current_sample += len;
1202 normalized_sample += len;
1204 if( debug ) PRINT_TRACE
1209 int File::read_frame(VFrame *frame, int is_thread)
1211 const int debug = 0;
1212 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1213 // current_frame, use_cache, asset->single_frame);
1214 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1215 if( !file ) return 1;
1216 if( debug ) PRINT_TRACE
1218 int supported_colormodel = colormodel_supported(frame->get_color_model());
1220 // if reverse playback reading, use_cache is -1
1221 int cache_active = asset->single_frame ? 1 : use_cache;
1222 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1225 if( cache_active ) {
1226 if( get_cache_frame(frame, cache_position) ) {
1229 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1230 // reverse reading and cache miss, clear cache for new readahead
1236 VFrame *vframe = frame;
1237 if( frame->get_color_model() != BC_COMPRESSED &&
1238 (supported_colormodel != frame->get_color_model() ||
1239 (!file->can_scale_input() &&
1240 (frame->get_w() != asset->width ||
1241 frame->get_h() != asset->height))) ) {
1243 if( !temp_frame->params_match(asset->width, asset->height,
1244 supported_colormodel) ) {
1245 delete temp_frame; temp_frame = 0;
1249 temp_frame = new VFrame(asset->width, asset->height,
1250 supported_colormodel, 0);
1251 temp_frame->clear_frame();
1254 temp_frame->copy_stacks(frame);
1255 vframe = temp_frame;
1257 result = file->read_frame(vframe);
1259 if( frame != vframe )
1260 frame->transfer_from(vframe);
1261 if( cache_active > 0 )
1262 put_cache_frame(frame, cache_position, 1);
1264 else if( frame->get_status() > 0 )
1265 frame->set_status(-1);
1266 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1269 if( result && !current_frame )
1270 frame->clear_frame();
1272 //printf("File::read_frame %d\n", __LINE__);
1274 if( do_read ) current_frame++;
1275 if( debug ) PRINT_TRACE
1279 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1281 if( asset && file ) {
1282 return asset->width == output_w &&
1283 asset->height == output_h &&
1284 file->can_copy_from(asset, position);
1289 // Fill in queries about formats when adding formats here.
1292 int File::strtoformat(const char *format)
1294 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1295 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1296 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1297 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1298 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1299 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1300 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1301 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1302 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1303 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1304 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1305 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1306 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1307 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1308 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1309 if( !strcasecmp(format, _(DPX_NAME)) ) return FILE_DPX;
1310 if( !strcasecmp(format, _(DPX_LIST_NAME)) ) return FILE_DPX_LIST;
1311 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1312 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1313 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1314 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1315 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1316 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1317 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1318 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1319 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1320 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1321 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1322 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1323 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1324 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1325 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1326 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1327 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1328 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1334 const char* File::formattostr(int format)
1337 case FILE_SCENE: return _(SCENE_NAME);
1338 case FILE_AC3: return _(AC3_NAME);
1339 case FILE_WAV: return _(WAV_NAME);
1340 case FILE_PCM: return _(PCM_NAME);
1341 case FILE_AU: return _(AU_NAME);
1342 case FILE_AIFF: return _(AIFF_NAME);
1343 case FILE_SND: return _(SND_NAME);
1344 case FILE_PNG: return _(PNG_NAME);
1345 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1346 case FILE_PPM: return _(PPM_NAME);
1347 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1348 case FILE_JPEG: return _(JPEG_NAME);
1349 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1350 case FILE_CR2: return _(CR2_NAME);
1351 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1352 case FILE_FLAC: return _(FLAC_NAME);
1353 case FILE_GIF: return _(GIF_NAME);
1354 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1355 case FILE_DPX: return _(DPX_NAME);
1356 case FILE_DPX_LIST: return _(DPX_LIST_NAME);
1357 case FILE_EXR: return _(EXR_NAME);
1358 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1359 #ifdef HAVE_LIBZMPEG
1360 case FILE_MPEG: return _(MPEG_NAME);
1362 case FILE_AMPEG: return _(AMPEG_NAME);
1363 case FILE_VMPEG: return _(VMPEG_NAME);
1364 case FILE_TGA: return _(TGA_NAME);
1365 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1366 case FILE_TIFF: return _(TIFF_NAME);
1367 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1369 case FILE_OGG: return _(OGG_NAME);
1370 case FILE_VORBIS: return _(VORBIS_NAME);
1372 case FILE_RAWDV: return _(RAWDV_NAME);
1373 case FILE_FFMPEG: return _(FFMPEG_NAME);
1374 case FILE_DB: return _(DBASE_NAME);
1375 case FILE_REF: return _(REF_NAME);
1377 return _("Unknown");
1380 int File::strtobits(const char *bits)
1382 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1383 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1384 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1385 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1386 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1387 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1388 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1389 return BITSLINEAR16;
1392 const char* File::bitstostr(int bits)
1394 //printf("File::bitstostr\n");
1396 case BITSLINEAR8: return (NAME_8BIT);
1397 case BITSLINEAR16: return (NAME_16BIT);
1398 case BITSLINEAR24: return (NAME_24BIT);
1399 case BITSLINEAR32: return (NAME_32BIT);
1400 case BITSULAW: return (NAME_ULAW);
1401 case BITS_ADPCM: return (NAME_ADPCM);
1402 case BITSFLOAT: return (NAME_FLOAT);
1404 return _("Unknown");
1409 int File::str_to_byteorder(const char *string)
1411 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1415 const char* File::byteorder_to_str(int byte_order)
1417 if( byte_order ) return _("Lo Hi");
1421 int File::bytes_per_sample(int bits)
1424 case BITSLINEAR8: return 1;
1425 case BITSLINEAR16: return 2;
1426 case BITSLINEAR24: return 3;
1427 case BITSLINEAR32: return 4;
1428 case BITSULAW: return 1;
1434 int File::get_best_colormodel(int driver, int vstream)
1436 return file ? file->get_best_colormodel(driver, vstream) :
1437 get_best_colormodel(asset, driver);
1440 int File::get_best_colormodel(Asset *asset, int driver)
1442 switch( asset->format ) {
1444 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1446 #ifdef HAVE_LIBZMPEG
1447 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1450 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1453 case FILE_DPX_LIST: return FileDPX::get_best_colormodel(asset, driver);
1457 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1460 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1462 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1464 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1466 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1467 #ifdef HAVE_COMMERCIAL
1468 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1470 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1471 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1478 int File::colormodel_supported(int colormodel)
1481 return file->colormodel_supported(colormodel);
1487 int64_t File::file_memory_usage()
1489 return file ? file->base_memory_usage() : 0;
1492 int64_t File::get_memory_usage()
1496 result += file_memory_usage();
1497 if( temp_frame ) result += temp_frame->get_data_size();
1498 result += frame_cache->get_memory_usage();
1499 if( video_thread ) result += video_thread->get_memory_usage();
1501 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1506 int File::renders_video(int format)
1511 case FILE_JPEG_LIST:
1525 case FILE_TIFF_LIST:
1533 int File::renders_video(Asset *asset)
1535 return asset->format == FILE_FFMPEG ?
1536 FFMPEG::renders_video(asset->fformat) :
1537 renders_video(asset->format);
1540 int File::renders_audio(int format)
1559 int File::renders_audio(Asset *asset)
1561 return asset->format == FILE_FFMPEG ?
1562 FFMPEG::renders_audio(asset->fformat) :
1563 renders_audio(asset->format);
1566 int File::is_image_render(int format)
1581 const char* File::get_tag(int format)
1584 case FILE_AC3: return "ac3";
1585 case FILE_AIFF: return "aif";
1586 case FILE_AMPEG: return "mp3";
1587 case FILE_AU: return "au";
1588 case FILE_RAWDV: return "dv";
1589 case FILE_DB: return "db";
1590 case FILE_DPX: return "dpx";
1591 case FILE_EXR: return "exr";
1592 case FILE_DPX_LIST: return "dpxs";
1593 case FILE_EXR_LIST: return "exrs";
1594 case FILE_FLAC: return "flac";
1595 case FILE_JPEG: return "jpg";
1596 case FILE_JPEG_LIST: return "jpgs";
1597 case FILE_OGG: return "ogg";
1598 case FILE_GIF: return "gif";
1599 case FILE_GIF_LIST: return "gifs";
1600 case FILE_PCM: return "pcm";
1601 case FILE_PNG: return "png";
1602 case FILE_PNG_LIST: return "pngs";
1603 case FILE_PPM: return "ppm";
1604 case FILE_PPM_LIST: return "ppms";
1605 case FILE_TGA: return "tga";
1606 case FILE_TGA_LIST: return "tgas";
1607 case FILE_TIFF: return "tif";
1608 case FILE_TIFF_LIST: return "tifs";
1609 case FILE_VMPEG: return "m2v";
1610 case FILE_VORBIS: return "ogg";
1611 case FILE_WAV: return "wav";
1612 case FILE_FFMPEG: return "ffmpg";
1613 case FILE_REF: return "ref";
1618 const char* File::get_prefix(int format)
1621 case FILE_PCM: return "PCM";
1622 case FILE_WAV: return "WAV";
1623 case FILE_PNG: return "PNG";
1624 case FILE_PPM: return "PPM";
1625 case FILE_JPEG: return "JPEG";
1626 case FILE_TIFF: return "TIFF";
1627 case FILE_GIF: return "GIF";
1628 case FILE_JPEG_LIST: return "JPEG_LIST";
1629 case FILE_AU: return "AU";
1630 case FILE_AIFF: return "AIFF";
1631 case FILE_SND: return "SND";
1632 case FILE_TGA_LIST: return "TGA_LIST";
1633 case FILE_TGA: return "TGA";
1634 case FILE_MPEG: return "MPEG";
1635 case FILE_AMPEG: return "AMPEG";
1636 case FILE_VMPEG: return "VMPEG";
1637 case FILE_RAWDV: return "RAWDV";
1638 case FILE_TIFF_LIST: return "TIFF_LIST";
1639 case FILE_PNG_LIST: return "PNG_LIST";
1640 case FILE_PPM_LIST: return "PPM_LIST";
1641 case FILE_AC3: return "AC3";
1642 case FILE_DPX: return "DPX";
1643 case FILE_DPX_LIST: return "DPX_LIST";
1644 case FILE_EXR: return "EXR";
1645 case FILE_EXR_LIST: return "EXR_LIST";
1646 case FILE_CR2: return "CR2";
1647 case FILE_OGG: return "OGG";
1648 case FILE_VORBIS: return "VORBIS";
1649 case FILE_FLAC: return "FLAC";
1650 case FILE_FFMPEG: return "FFMPEG";
1651 case FILE_SCENE: return "SCENE";
1652 case FILE_CR2_LIST: return "CR2_LIST";
1653 case FILE_GIF_LIST: return "GIF_LIST";
1654 case FILE_DB: return "DB";
1655 case FILE_REF: return "REF";
1657 return _("UNKNOWN");
1661 int File::record_fd()
1663 return file ? file->record_fd() : -1;
1667 void File::get_exe_path(char *result, char *bnp)
1669 // Get executable path, basename
1670 #if !defined(__FreeBSD__)
1671 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1673 char exe_path[BCTEXTLEN];
1674 sprintf(exe_path,"/proc/%d/file",getpid());
1675 int len = readlink(exe_path, result, BCTEXTLEN-1);
1680 char *ptr = strrchr(result, '/');
1681 if( ptr ) *ptr++ = 0; else ptr = result;
1682 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1690 void File::getenv_path(char *result, const char *path)
1692 char *rp = result, *ep = rp + BCTEXTLEN-1;
1693 const char *cp = path;
1694 // any env var can be used here to expand a path as:
1695 // "path...$id...": and id is a word as alpha,alnum...
1696 // expands as "path...getenv(id)..."
1697 // CIN_PATH, CIN_LIB are set in main.C,
1698 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1699 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1700 const char *bp = cp; char *p = rp;
1701 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1703 const char *envp = getenv(rp); // read env value
1704 if( !envp ) continue;
1705 while( *envp && rp < ep ) *rp++ = *envp++;
1706 cp = bp; // subst id=value
1714 void File::setenv_path(const char *var, const char *path, int overwrite)
1716 char env_path[BCTEXTLEN];
1717 getenv_path(env_path, path);
1718 setenv(var, env_path, overwrite);
1721 * @brief Set various environment variables to pass constant values to
1722 * forks, shell scripts, and other parts of CinGG.
1724 void File::init_cin_env_vars()
1726 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1728 struct tm *dateTime;
1729 get_exe_path(env_path, env_pkg);
1730 setenv_path("CIN_PATH", env_path, 1);
1731 setenv_path("CIN_PKG", env_pkg, 1);
1732 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1733 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1734 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1735 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1736 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1737 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1738 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1739 // Create env_var CINGG_BUILD for use when running as AppImage
1740 stat(env_path, &st);
1741 dateTime = gmtime(&st.st_mtime);
1742 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1743 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1744 build_name[sizeof(build_name) - 1] = 0;
1745 setenv_path("CINGG_BUILD", build_name, 1);