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);
259 FileVorbis::get_parameters(parent_window, asset, format_window,
260 audio_options, video_options, edl);
267 if( !format_window ) {
268 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
269 parent_window->get_abs_cursor_x(1),
270 parent_window->get_abs_cursor_y(1));
271 format_window = errorbox;
274 errorbox->create_objects(_("This format doesn't support audio."));
277 errorbox->create_objects(_("This format doesn't support video."));
278 errorbox->run_window();
284 format_completion->unlock();
288 int File::can_scale_input(Asset *asset)
290 switch( asset->format ) {
301 if( asset->video_length < 0 )
308 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
310 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
311 cpus = 8; // 8 cpus ought to decode just about anything
312 // Set all instances so gets work.
318 int File::set_preload(int64_t size)
320 this->playback_preload = size;
324 void File::set_subtitle(int value)
326 this->playback_subtitle = value;
327 if( file ) file->set_subtitle(value);
330 void File::set_interpolate_raw(int value)
332 this->interpolate_raw = value;
335 void File::set_white_balance_raw(int value)
337 this->white_balance_raw = value;
340 void File::set_cache_frames(int value)
342 // caching only done locally
347 int File::purge_cache()
349 // caching only done locally
351 if( frame_cache->cache_items() > 0 ) {
352 frame_cache->remove_all();
358 int File::delete_oldest()
360 // return the number of bytes freed
361 return frame_cache->delete_oldest();
364 int File::get_cache_frame(VFrame *frame, int64_t position)
366 return frame_cache->get_cache_frame(frame, position,
367 current_layer, asset->frame_rate);
370 void File::put_cache_frame(VFrame *frame, int64_t position, int use_copy)
372 frame_cache->put_cache_frame(frame,
373 position, current_layer, asset->frame_rate, use_copy);
376 int File::get_use_cache()
382 // file driver in order of probe precidence
383 // can be reordered in preferences->interface
384 const char *File::default_probes[] = {
418 const int File::nb_probes =
419 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
424 FILE *fp = fopen(this->asset->path, "rb");
425 if( !fp ) return FILE_NOT_FOUND;
427 memset(data,0,sizeof(data));
428 int ret = fread(data, 1, 16, fp);
430 if( !ret ) return FILE_NOT_FOUND;
432 for( int i=0; i<preferences->file_probes.size(); ++i ) {
433 ProbePref *pref = preferences->file_probes[i];
434 if( !pref->armed ) continue;
435 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
436 if( !FileFFMPEG::check_sig(this->asset) ) continue;
437 file = new FileFFMPEG(this->asset, this);
440 #ifdef HAVE_COMMERCIAL
441 if( !strcmp(pref->name,"DB") ) { // MediaDB
442 if( !FileDB::check_sig(this->asset) ) continue;
443 file = new FileDB(this->asset, this);
447 if( !strcmp(pref->name,"Scene") ) { // scene file
448 if( !FileScene::check_sig(this->asset, data)) continue;
449 file = new FileScene(this->asset, this);
453 if( !strcmp(pref->name,"DV") ) { // libdv
454 if( !FileDV::check_sig(this->asset) ) continue;
455 file = new FileDV(this->asset, this);
459 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
460 if( !FileSndFile::check_sig(this->asset) ) continue;
461 file = new FileSndFile(this->asset, this);
464 if( !strcmp(pref->name,"PNG") ) { // PNG file
465 if( !FilePNG::check_sig(this->asset) ) continue;
466 file = new FilePNG(this->asset, this);
469 if( !strcmp(pref->name,"PPM") ) { // PPM file
470 if( !FilePPM::check_sig(this->asset) ) continue;
471 file = new FilePPM(this->asset, this);
474 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
475 if( !FileJPEG::check_sig(this->asset) ) continue;
476 file = new FileJPEG(this->asset, this);
480 if( !strcmp(pref->name,"GIF") ) { // GIF file
481 if( FileGIFList::check_sig(this->asset) )
482 file = new FileGIFList(this->asset, this);
483 else if( FileGIF::check_sig(this->asset) )
484 file = new FileGIF(this->asset, this);
490 if( !strcmp(pref->name,"DPX") ) { // DPX file
491 if( !FileDPX::check_sig(this->asset, data) ) continue;
492 file = new FileDPX(this->asset, this);
497 if( !strcmp(pref->name,"EXR") ) { // EXR file
498 if( !FileEXR::check_sig(this->asset, data)) continue;
499 file = new FileEXR(this->asset, this);
503 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
504 if( !FileFLAC::check_sig(this->asset, data)) continue;
505 file = new FileFLAC(this->asset, this);
508 if( !strcmp(pref->name,"CR2") ) { // CR2 file
509 if( !FileCR2::check_sig(this->asset)) continue;
510 file = new FileCR2(this->asset, this);
513 if( !strcmp(pref->name,"TGA") ) { // TGA file
514 if( !FileTGA::check_sig(this->asset) ) continue;
515 file = new FileTGA(this->asset, this);
518 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
519 if( !FileTIFF::check_sig(this->asset) ) continue;
520 file = new FileTIFF(this->asset, this);
524 if( !strcmp(pref->name,"OGG") ) { // OGG file
525 if( !FileOGG::check_sig(this->asset) ) continue;
526 file = new FileOGG(this->asset, this);
529 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
530 if( !FileVorbis::check_sig(this->asset) ) continue;
531 file = new FileVorbis(this->asset, this);
536 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
537 if( !FileMPEG::check_sig(this->asset) ) continue;
538 file = new FileMPEG(this->asset, this);
542 if( !strcmp(pref->name,"EDL") ) { // XML file
543 if( data[0] != '<' ) continue;
544 if( !strncmp(&data[1],"EDL>",4) ||
545 !strncmp(&data[1],"HTAL>",5) ||
546 !strncmp(&data[1],"?xml",4) )
550 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
553 int File::open_file(Preferences *preferences,
560 this->preferences = preferences;
561 this->asset->copy_from(asset, 1);
566 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
568 switch( this->asset->format ) {
569 // get the format now
570 // If you add another format to case 0, you also need to add another case for the
571 // file format #define.
574 if( ret != FILE_OK ) return ret;
576 // format already determined
577 #ifdef HAVE_CIN_3RDPARTY
580 file = new FileAC3(this->asset, this);
585 file = new FileScene(this->asset, this);
589 file = new FileFFMPEG(this->asset, this);
597 //printf("File::open_file 1\n");
598 file = new FileSndFile(this->asset, this);
603 file = new FilePNG(this->asset, this);
608 file = new FilePPM(this->asset, this);
613 file = new FileJPEG(this->asset, this);
617 file = new FileGIF(this->asset, this);
620 file = new FileGIFList(this->asset, this);
626 file = new FileDPX(this->asset, this);
632 file = new FileEXR(this->asset, this);
637 file = new FileFLAC(this->asset, this);
642 file = new FileCR2(this->asset, this);
647 file = new FileTGA(this->asset, this);
652 file = new FileTIFF(this->asset, this);
654 #ifdef HAVE_COMMERCIAL
656 file = new FileDB(this->asset, this);
664 file = new FileMPEG(this->asset, this);
669 file = new FileOGG(this->asset, this);
673 file = new FileVorbis(this->asset, this);
678 file = new FileDV(this->asset, this);
682 file = new FileREF(this->asset, this);
691 // Reopen file with correct parser and get header.
692 if( file->open_file(rd, wr) ) {
693 delete file; file = 0;
694 return FILE_NOT_FOUND;
698 // If file type is a list verify that all files match in dimensions.
699 // Should be done only after the file open function has been performed
700 // Reason: although this function checks if file exists or not but
701 // it has no way of relaying this information back and if this function
702 // is called before open_file the program may accidently interpret file
703 // not found as file size don't match
704 if( !file->verify_file_list() ) {
705 delete file; file = 0;
706 return FILE_SIZE_DONT_MATCH;
710 // Set extra writing parameters to mandatory settings.
712 if( this->asset->dither ) file->set_dither();
716 // one frame image file, not brender, no specific length
717 if( !this->asset->audio_data && this->asset->use_header &&
718 this->asset->video_data && !this->asset->single_frame &&
719 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
720 this->asset->single_frame = 1;
721 this->asset->video_length = -1;
725 // Synchronize header parameters
726 asset->copy_from(this->asset, 1);
729 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
735 void File::delete_temp_samples_buffer()
738 if( temp_samples_buffer ) {
739 for( int j = 0; j < audio_ring_buffers; j++ ) {
740 for( int i = 0; i < asset->channels; i++ ) {
741 delete temp_samples_buffer[j][i];
743 delete [] temp_samples_buffer[j];
746 delete [] temp_samples_buffer;
747 temp_samples_buffer = 0;
748 audio_ring_buffers = 0;
752 void File::delete_temp_frame_buffer()
755 if( temp_frame_buffer ) {
756 for( int k = 0; k < video_ring_buffers; k++ ) {
757 for( int i = 0; i < asset->layers; i++ ) {
758 for( int j = 0; j < video_buffer_size; j++ ) {
759 delete temp_frame_buffer[k][i][j];
761 delete [] temp_frame_buffer[k][i];
763 delete [] temp_frame_buffer[k];
766 delete [] temp_frame_buffer;
767 temp_frame_buffer = 0;
768 video_ring_buffers = 0;
769 video_buffer_size = 0;
773 int File::close_file(int ignore_thread)
775 if( !ignore_thread ) {
780 // The file's asset is a copy of the argument passed to open_file so the
781 // user must copy lengths from the file's asset.
783 asset->audio_length = current_sample;
784 asset->video_length = current_frame;
791 delete_temp_samples_buffer();
792 delete_temp_frame_buffer();
797 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
799 return !file ? -1 : file->get_index(index_file, progress_bar);
802 int File::start_audio_thread(int buffer_size, int ring_buffers)
804 this->audio_ring_buffers = ring_buffers;
807 if( !audio_thread ) {
808 audio_thread = new FileThread(this, 1, 0);
809 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
814 int File::start_video_thread(int buffer_size, int color_model,
815 int ring_buffers, int compressed)
817 this->video_ring_buffers = ring_buffers;
818 this->video_buffer_size = buffer_size;
820 if( !video_thread ) {
821 video_thread = new FileThread(this, 0, 1);
822 video_thread->start_writing(buffer_size, color_model,
823 ring_buffers, compressed);
828 int File::start_video_decode_thread()
830 // Currently, CR2 is the only one which won't work asynchronously, so
831 // we're not using a virtual function yet.
832 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
833 video_thread = new FileThread(this, 0, 1);
834 video_thread->start_reading();
840 int File::stop_audio_thread()
843 audio_thread->stop_writing();
850 int File::stop_video_thread()
853 video_thread->stop_reading();
854 video_thread->stop_writing();
861 FileThread* File::get_video_thread()
866 int File::set_channel(int channel)
868 if( file && channel < asset->channels ) {
869 current_channel = channel;
876 int File::get_channel()
878 return current_channel;
881 // if no>=0, sets new program
882 // returns current program
883 int File::set_program(int no)
885 int result = file ? file->set_program(no) : current_program;
886 current_program = no < 0 ? result : no;
890 int File::get_cell_time(int no, double &time)
892 return file ? file->get_cell_time(no, time) : -1;
895 int File::get_system_time(int64_t &tm)
897 return file ? file->get_system_time(tm) : -1;
900 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
902 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
905 int File::get_video_pid(int track)
907 return file ? file->get_video_pid(track) : -1;
912 int File::get_video_info(int track, int &pid, double &framerate,
913 int &width, int &height, char *title)
916 file->get_video_info(track, pid, framerate, width, height, title);
919 int File::select_video_stream(Asset *asset, int vstream)
922 file->select_video_stream(asset, vstream);
925 int File::select_audio_stream(Asset *asset, int astream)
928 file->select_audio_stream(asset, astream);
932 int File::get_thumbnail(int stream,
933 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
935 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
938 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
940 return file->set_skimming(track, skim, fn, vp);
943 int File::skim_video(int track, void *vp, skim_fn fn)
945 return file->skim_video(track, vp, fn);
949 int File::set_layer(int layer, int is_thread)
951 if( file && layer < asset->layers ) {
952 if( !is_thread && video_thread ) {
953 video_thread->set_layer(layer);
956 current_layer = layer;
964 int64_t File::get_audio_length()
966 int64_t result = asset->audio_length;
967 int64_t base_samplerate = -1;
969 if( base_samplerate > 0 )
970 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
978 int64_t File::get_video_length()
980 int64_t result = asset->video_length;
981 float base_framerate = -1;
983 if( base_framerate > 0 )
984 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
989 return -1; // infinity
993 int64_t File::get_video_position()
995 float base_framerate = -1;
996 if( base_framerate > 0 )
997 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
999 return current_frame;
1002 int64_t File::get_audio_position()
1004 // int64_t base_samplerate = -1;
1005 // if( base_samplerate > 0 )
1007 // if( normalized_sample_rate == base_samplerate )
1008 // return normalized_sample;
1010 // return (int64_t)((double)current_sample /
1011 // asset->sample_rate *
1012 // base_samplerate +
1016 return current_sample;
1021 int File::set_audio_position(int64_t position)
1025 if( !file ) return 1;
1027 #define REPOSITION(x, y) \
1028 (labs((x) - (y)) > 1)
1030 float base_samplerate = asset->sample_rate;
1031 current_sample = normalized_sample = position;
1033 // printf("File::set_audio_position %d normalized_sample=%ld\n",
1035 // normalized_sample);
1036 result = file->set_audio_position(current_sample);
1039 printf("File::set_audio_position position=%jd"
1040 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
1041 position, base_samplerate, asset, asset->sample_rate);
1043 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1048 int File::set_video_position(int64_t position,
1052 if( !file ) return 0;
1054 // Convert to file's rate
1055 // if( base_framerate > 0 )
1056 // position = (int64_t)((double)position /
1058 // asset->frame_rate +
1062 if( video_thread && !is_thread ) {
1063 // Call thread. Thread calls this again to set the file state.
1064 video_thread->set_video_position(position);
1067 if( current_frame != position ) {
1069 current_frame = position;
1070 result = file->set_video_position(current_frame);
1077 // No resampling here.
1078 int File::write_samples(Samples **buffer, int64_t len)
1083 write_lock->lock("File::write_samples");
1085 // Convert to arrays for backwards compatability
1086 double *temp[asset->channels];
1087 for( int i = 0; i < asset->channels; i++ ) {
1088 temp[i] = buffer[i]->get_data();
1091 result = file->write_samples(temp, len);
1092 current_sample += len;
1093 normalized_sample += len;
1094 asset->audio_length += len;
1095 write_lock->unlock();
1104 // Can't put any cmodel abstraction here because the filebase couldn't be
1106 int File::write_frames(VFrame ***frames, int len)
1108 //printf("File::write_frames %d\n", __LINE__);
1110 // Store the counters in temps so the filebase can choose to overwrite them.
1112 int current_frame_temp = current_frame;
1113 int video_length_temp = asset->video_length;
1115 write_lock->lock("File::write_frames");
1118 result = file->write_frames(frames, len);
1121 current_frame = current_frame_temp + len;
1122 asset->video_length = video_length_temp + len;
1123 write_lock->unlock();
1128 // Only called by FileThread
1129 int File::write_compressed_frame(VFrame *buffer)
1132 write_lock->lock("File::write_compressed_frame");
1133 result = file->write_compressed_frame(buffer);
1135 asset->video_length++;
1136 write_lock->unlock();
1141 int File::write_audio_buffer(int64_t len)
1144 if( audio_thread ) {
1145 result = audio_thread->write_buffer(len);
1150 int File::write_video_buffer(int64_t len)
1153 if( video_thread ) {
1154 result = video_thread->write_buffer(len);
1160 Samples** File::get_audio_buffer()
1162 if( audio_thread ) return audio_thread->get_audio_buffer();
1166 VFrame*** File::get_video_buffer()
1168 if( video_thread ) {
1169 VFrame*** result = video_thread->get_video_buffer();
1178 int File::read_samples(Samples *samples, int64_t len)
1180 // Never try to read more samples than exist in the file
1181 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1182 len = asset->audio_length - current_sample;
1184 if( len <= 0 ) return 0;
1187 const int debug = 0;
1188 if( debug ) PRINT_TRACE
1190 if( debug ) PRINT_TRACE
1192 double *buffer = samples->get_data();
1194 int64_t base_samplerate = asset->sample_rate;
1197 // Resample recursively calls this with the asset sample rate
1198 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1200 if( debug ) PRINT_TRACE
1201 result = file->read_samples(buffer, len);
1203 if( debug ) PRINT_TRACE
1204 current_sample += len;
1206 normalized_sample += len;
1208 if( debug ) PRINT_TRACE
1213 int File::read_frame(VFrame *frame, int is_thread)
1215 const int debug = 0;
1216 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1217 // current_frame, use_cache, asset->single_frame);
1218 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1219 if( !file ) return 1;
1220 if( debug ) PRINT_TRACE
1222 int supported_colormodel = colormodel_supported(frame->get_color_model());
1224 // if reverse playback reading, use_cache is -1
1225 int cache_active = asset->single_frame ? 1 : use_cache;
1226 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1229 if( cache_active ) {
1230 if( get_cache_frame(frame, cache_position) ) {
1233 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1234 // reverse reading and cache miss, clear cache for new readahead
1240 VFrame *vframe = frame;
1241 if( frame->get_color_model() != BC_COMPRESSED &&
1242 (supported_colormodel != frame->get_color_model() ||
1243 (!file->can_scale_input() &&
1244 (frame->get_w() != asset->width ||
1245 frame->get_h() != asset->height))) ) {
1247 if( !temp_frame->params_match(asset->width, asset->height,
1248 supported_colormodel) ) {
1249 delete temp_frame; temp_frame = 0;
1253 temp_frame = new VFrame(asset->width, asset->height,
1254 supported_colormodel, 0);
1255 temp_frame->clear_frame();
1258 temp_frame->copy_stacks(frame);
1259 vframe = temp_frame;
1261 result = file->read_frame(vframe);
1263 if( frame != vframe )
1264 frame->transfer_from(vframe);
1265 if( cache_active > 0 )
1266 put_cache_frame(frame, cache_position, 1);
1268 else if( frame->get_status() > 0 )
1269 frame->set_status(-1);
1270 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1273 if( result && !current_frame )
1274 frame->clear_frame();
1276 //printf("File::read_frame %d\n", __LINE__);
1278 if( do_read ) current_frame++;
1279 if( debug ) PRINT_TRACE
1283 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1285 if( asset && file ) {
1286 return asset->width == output_w &&
1287 asset->height == output_h &&
1288 file->can_copy_from(asset, position);
1293 // Fill in queries about formats when adding formats here.
1296 int File::strtoformat(const char *format)
1298 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1299 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1300 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1301 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1302 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1303 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1304 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1305 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1306 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1307 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1308 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1309 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1310 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1311 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1312 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1313 if( !strcasecmp(format, _(DPX_NAME)) ) return FILE_DPX;
1314 if( !strcasecmp(format, _(DPX_LIST_NAME)) ) return FILE_DPX_LIST;
1315 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1316 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1317 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1318 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1319 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1320 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1321 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1322 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1323 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1324 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1325 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1326 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1327 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1328 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1329 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1330 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1331 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1332 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1338 const char* File::formattostr(int format)
1341 case FILE_SCENE: return _(SCENE_NAME);
1342 case FILE_AC3: return _(AC3_NAME);
1343 case FILE_WAV: return _(WAV_NAME);
1344 case FILE_PCM: return _(PCM_NAME);
1345 case FILE_AU: return _(AU_NAME);
1346 case FILE_AIFF: return _(AIFF_NAME);
1347 case FILE_SND: return _(SND_NAME);
1348 case FILE_PNG: return _(PNG_NAME);
1349 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1350 case FILE_PPM: return _(PPM_NAME);
1351 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1352 case FILE_JPEG: return _(JPEG_NAME);
1353 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1354 case FILE_CR2: return _(CR2_NAME);
1355 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1356 case FILE_FLAC: return _(FLAC_NAME);
1357 case FILE_GIF: return _(GIF_NAME);
1358 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1359 case FILE_DPX: return _(DPX_NAME);
1360 case FILE_DPX_LIST: return _(DPX_LIST_NAME);
1361 case FILE_EXR: return _(EXR_NAME);
1362 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1363 #ifdef HAVE_LIBZMPEG
1364 case FILE_MPEG: return _(MPEG_NAME);
1366 case FILE_AMPEG: return _(AMPEG_NAME);
1367 case FILE_VMPEG: return _(VMPEG_NAME);
1368 case FILE_TGA: return _(TGA_NAME);
1369 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1370 case FILE_TIFF: return _(TIFF_NAME);
1371 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1373 case FILE_OGG: return _(OGG_NAME);
1374 case FILE_VORBIS: return _(VORBIS_NAME);
1376 case FILE_RAWDV: return _(RAWDV_NAME);
1377 case FILE_FFMPEG: return _(FFMPEG_NAME);
1378 case FILE_DB: return _(DBASE_NAME);
1379 case FILE_REF: return _(REF_NAME);
1381 return _("Unknown");
1384 int File::strtobits(const char *bits)
1386 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1387 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1388 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1389 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1390 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1391 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1392 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1393 return BITSLINEAR16;
1396 const char* File::bitstostr(int bits)
1398 //printf("File::bitstostr\n");
1400 case BITSLINEAR8: return (NAME_8BIT);
1401 case BITSLINEAR16: return (NAME_16BIT);
1402 case BITSLINEAR24: return (NAME_24BIT);
1403 case BITSLINEAR32: return (NAME_32BIT);
1404 case BITSULAW: return (NAME_ULAW);
1405 case BITS_ADPCM: return (NAME_ADPCM);
1406 case BITSFLOAT: return (NAME_FLOAT);
1408 return _("Unknown");
1413 int File::str_to_byteorder(const char *string)
1415 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1419 const char* File::byteorder_to_str(int byte_order)
1421 if( byte_order ) return _("Lo Hi");
1425 int File::bytes_per_sample(int bits)
1428 case BITSLINEAR8: return 1;
1429 case BITSLINEAR16: return 2;
1430 case BITSLINEAR24: return 3;
1431 case BITSLINEAR32: return 4;
1432 case BITSULAW: return 1;
1438 int File::get_best_colormodel(int driver, int vstream)
1440 return file ? file->get_best_colormodel(driver, vstream) :
1441 get_best_colormodel(asset, driver);
1444 int File::get_best_colormodel(Asset *asset, int driver)
1446 switch( asset->format ) {
1448 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1450 #ifdef HAVE_LIBZMPEG
1451 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1454 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1457 case FILE_DPX_LIST: return FileDPX::get_best_colormodel(asset, driver);
1461 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1464 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1466 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1468 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1470 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1471 #ifdef HAVE_COMMERCIAL
1472 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1474 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1475 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1482 int File::colormodel_supported(int colormodel)
1485 return file->colormodel_supported(colormodel);
1491 int64_t File::file_memory_usage()
1493 return file ? file->base_memory_usage() : 0;
1496 int64_t File::get_memory_usage()
1500 result += file_memory_usage();
1501 if( temp_frame ) result += temp_frame->get_data_size();
1502 result += frame_cache->get_memory_usage();
1503 if( video_thread ) result += video_thread->get_memory_usage();
1505 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1510 int File::renders_video(int format)
1515 case FILE_JPEG_LIST:
1529 case FILE_TIFF_LIST:
1537 int File::renders_video(Asset *asset)
1539 return asset->format == FILE_FFMPEG ?
1540 FFMPEG::renders_video(asset->fformat) :
1541 renders_video(asset->format);
1544 int File::renders_audio(int format)
1563 int File::renders_audio(Asset *asset)
1565 return asset->format == FILE_FFMPEG ?
1566 FFMPEG::renders_audio(asset->fformat) :
1567 renders_audio(asset->format);
1570 int File::is_image_render(int format)
1585 const char* File::get_tag(int format)
1588 case FILE_AC3: return "ac3";
1589 case FILE_AIFF: return "aif";
1590 case FILE_AMPEG: return "mp3";
1591 case FILE_AU: return "au";
1592 case FILE_RAWDV: return "dv";
1593 case FILE_DB: return "db";
1594 case FILE_DPX: return "dpx";
1595 case FILE_EXR: return "exr";
1596 case FILE_DPX_LIST: return "dpxs";
1597 case FILE_EXR_LIST: return "exrs";
1598 case FILE_FLAC: return "flac";
1599 case FILE_JPEG: return "jpg";
1600 case FILE_JPEG_LIST: return "jpgs";
1601 case FILE_OGG: return "ogg";
1602 case FILE_GIF: return "gif";
1603 case FILE_GIF_LIST: return "gifs";
1604 case FILE_PCM: return "pcm";
1605 case FILE_PNG: return "png";
1606 case FILE_PNG_LIST: return "pngs";
1607 case FILE_PPM: return "ppm";
1608 case FILE_PPM_LIST: return "ppms";
1609 case FILE_TGA: return "tga";
1610 case FILE_TGA_LIST: return "tgas";
1611 case FILE_TIFF: return "tif";
1612 case FILE_TIFF_LIST: return "tifs";
1613 case FILE_VMPEG: return "m2v";
1614 case FILE_VORBIS: return "ogg";
1615 case FILE_WAV: return "wav";
1616 case FILE_FFMPEG: return "ffmpg";
1617 case FILE_REF: return "ref";
1622 const char* File::get_prefix(int format)
1625 case FILE_PCM: return "PCM";
1626 case FILE_WAV: return "WAV";
1627 case FILE_PNG: return "PNG";
1628 case FILE_PPM: return "PPM";
1629 case FILE_JPEG: return "JPEG";
1630 case FILE_TIFF: return "TIFF";
1631 case FILE_GIF: return "GIF";
1632 case FILE_JPEG_LIST: return "JPEG_LIST";
1633 case FILE_AU: return "AU";
1634 case FILE_AIFF: return "AIFF";
1635 case FILE_SND: return "SND";
1636 case FILE_TGA_LIST: return "TGA_LIST";
1637 case FILE_TGA: return "TGA";
1638 case FILE_MPEG: return "MPEG";
1639 case FILE_AMPEG: return "AMPEG";
1640 case FILE_VMPEG: return "VMPEG";
1641 case FILE_RAWDV: return "RAWDV";
1642 case FILE_TIFF_LIST: return "TIFF_LIST";
1643 case FILE_PNG_LIST: return "PNG_LIST";
1644 case FILE_PPM_LIST: return "PPM_LIST";
1645 case FILE_AC3: return "AC3";
1646 case FILE_DPX: return "DPX";
1647 case FILE_DPX_LIST: return "DPX_LIST";
1648 case FILE_EXR: return "EXR";
1649 case FILE_EXR_LIST: return "EXR_LIST";
1650 case FILE_CR2: return "CR2";
1651 case FILE_OGG: return "OGG";
1652 case FILE_VORBIS: return "VORBIS";
1653 case FILE_FLAC: return "FLAC";
1654 case FILE_FFMPEG: return "FFMPEG";
1655 case FILE_SCENE: return "SCENE";
1656 case FILE_CR2_LIST: return "CR2_LIST";
1657 case FILE_GIF_LIST: return "GIF_LIST";
1658 case FILE_DB: return "DB";
1659 case FILE_REF: return "REF";
1661 return _("UNKNOWN");
1665 int File::record_fd()
1667 return file ? file->record_fd() : -1;
1671 void File::get_exe_path(char *result, char *bnp)
1673 // Get executable path, basename
1674 #if !defined(__FreeBSD__)
1675 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1677 char exe_path[BCTEXTLEN];
1678 sprintf(exe_path,"/proc/%d/file",getpid());
1679 int len = readlink(exe_path, result, BCTEXTLEN-1);
1684 char *ptr = strrchr(result, '/');
1685 if( ptr ) *ptr++ = 0; else ptr = result;
1686 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1694 void File::getenv_path(char *result, const char *path)
1696 char *rp = result, *ep = rp + BCTEXTLEN-1;
1697 const char *cp = path;
1698 // any env var can be used here to expand a path as:
1699 // "path...$id...": and id is a word as alpha,alnum...
1700 // expands as "path...getenv(id)..."
1701 // CIN_PATH, CIN_LIB are set in main.C,
1702 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1703 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1704 const char *bp = cp; char *p = rp;
1705 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1707 const char *envp = getenv(rp); // read env value
1708 if( !envp ) continue;
1709 while( *envp && rp < ep ) *rp++ = *envp++;
1710 cp = bp; // subst id=value
1718 void File::setenv_path(const char *var, const char *path, int overwrite)
1720 char env_path[BCTEXTLEN];
1721 getenv_path(env_path, path);
1722 setenv(var, env_path, overwrite);
1725 * @brief Set various environment variables to pass constant values to
1726 * forks, shell scripts, and other parts of CinGG.
1728 void File::init_cin_env_vars()
1730 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1732 struct tm *dateTime;
1733 get_exe_path(env_path, env_pkg);
1734 setenv_path("CIN_PATH", env_path, 1);
1735 setenv_path("CIN_PKG", env_pkg, 1);
1736 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1737 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1738 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1739 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1740 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1741 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1742 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1743 // Create env_var CINGG_BUILD for use when running as AppImage
1744 stat(env_path, &st);
1745 dateTime = gmtime(&st.st_mtime);
1746 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1747 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1748 build_name[sizeof(build_name) - 1] = 0;
1749 setenv_path("CINGG_BUILD", build_name, 1);