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"
50 #include "fileffmpeg.h"
56 #undef HAVE_STDLIB_H // automake conflict
61 #include "filescene.h"
62 #include "filesndfile.h"
64 #include "filethread.h"
66 #include "filevorbis.h"
68 #include "formatwindow.h"
69 #include "formattools.h"
70 #include "framecache.h"
72 #include "mainprogress.inc"
75 #include "packagingengine.h"
76 #include "pluginserver.h"
77 #include "preferences.h"
78 #include "probeprefs.h"
83 //suppress noref warning
84 void *vorbis0_ov_callbacks[] = {
85 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
86 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
94 format_completion = new Condition(1, "File::format_completion");
95 write_lock = new Condition(1, "File::write_lock");
96 frame_cache = new FrameCache;
102 if( getting_options ) {
103 if( format_window ) format_window->set_done(0);
104 format_completion->lock("File::~File");
105 format_completion->unlock();
114 asset->Garbage::remove_user();
115 delete format_completion;
120 void File::reset_parameters()
133 normalized_sample = 0;
136 playback_subtitle = -1;
140 temp_samples_buffer = 0;
141 temp_frame_buffer = 0;
142 current_frame_buffer = 0;
143 audio_ring_buffers = 0;
144 video_ring_buffers = 0;
145 video_buffer_size = 0;
148 int File::raise_window()
150 if( getting_options && format_window ) {
151 format_window->raise_window();
152 format_window->flush();
157 void File::close_window()
159 if( getting_options ) {
160 format_window->lock_window("File::close_window");
161 format_window->set_done(1);
162 format_window->unlock_window();
167 int File::get_options(FormatTools *format,
168 int audio_options, int video_options)
170 BC_WindowBase *parent_window = format->window;
171 Asset *asset = format->asset;
172 EDL *edl = format->mwindow ? format->mwindow->edl : 0;
175 format_completion->lock("File::get_options");
176 switch( asset->format ) {
177 #ifdef HAVE_CIN_3RDPARTY
179 case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
180 audio_options, video_options, edl);
186 FileDV::get_parameters(parent_window, asset, format_window,
187 audio_options, video_options, edl);
195 FileSndFile::get_parameters(parent_window, asset, format_window,
196 audio_options, video_options, edl);
199 FileFFMPEG::get_parameters(parent_window, asset, format_window,
200 audio_options, video_options, edl);
205 FileMPEG::get_parameters(parent_window, asset, format_window,
206 audio_options, video_options, edl);
211 FileJPEG::get_parameters(parent_window, asset, format_window,
212 audio_options, video_options, edl);
217 FileDPX::get_parameters(parent_window, asset, format_window,
218 audio_options, video_options, edl);
224 FileEXR::get_parameters(parent_window, asset, format_window,
225 audio_options, video_options, edl);
229 FileFLAC::get_parameters(parent_window, asset, format_window,
230 audio_options, video_options, edl);
234 FilePNG::get_parameters(parent_window, asset, format_window,
235 audio_options, video_options, edl);
239 FilePPM::get_parameters(parent_window, asset, format_window,
240 audio_options, video_options, edl);
244 FileTGA::get_parameters(parent_window, asset, format_window,
245 audio_options, video_options, edl);
249 FileTIFF::get_parameters(parent_window, asset, format_window,
250 audio_options, video_options, edl);
254 FileOGG::get_parameters(parent_window, asset, format_window,
255 audio_options, video_options, edl);
262 if( !format_window ) {
263 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
264 parent_window->get_abs_cursor_x(1),
265 parent_window->get_abs_cursor_y(1));
266 format_window = errorbox;
269 errorbox->create_objects(_("This format doesn't support audio."));
272 errorbox->create_objects(_("This format doesn't support video."));
273 errorbox->run_window();
279 format_completion->unlock();
283 int File::can_scale_input(Asset *asset)
285 switch( asset->format ) {
296 if( asset->video_length < 0 )
303 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
305 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
306 cpus = 8; // 8 cpus ought to decode just about anything
307 // Set all instances so gets work.
313 int File::set_preload(int64_t size)
315 this->playback_preload = size;
319 void File::set_subtitle(int value)
321 this->playback_subtitle = value;
322 if( file ) file->set_subtitle(value);
325 void File::set_interpolate_raw(int value)
327 this->interpolate_raw = value;
330 void File::set_white_balance_raw(int value)
332 this->white_balance_raw = value;
335 void File::set_cache_frames(int value)
337 // caching only done locally
342 int File::purge_cache()
344 // caching only done locally
346 if( frame_cache->cache_items() > 0 ) {
347 frame_cache->remove_all();
353 int File::delete_oldest()
355 // return the number of bytes freed
356 return frame_cache->delete_oldest();
359 int File::get_cache_frame(VFrame *frame, int64_t position)
361 return frame_cache->get_cache_frame(frame, position,
362 current_layer, asset->frame_rate);
365 void File::put_cache_frame(VFrame *frame, int64_t position, int use_copy)
367 frame_cache->put_cache_frame(frame,
368 position, current_layer, asset->frame_rate, use_copy);
371 int File::get_use_cache()
377 // file driver in order of probe precidence
378 // can be reordered in preferences->interface
379 const char *File::default_probes[] = {
407 const int File::nb_probes =
408 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
413 FILE *fp = fopen(this->asset->path, "rb");
414 if( !fp ) return FILE_NOT_FOUND;
416 memset(data,0,sizeof(data));
417 int ret = fread(data, 1, 16, fp);
419 if( !ret ) return FILE_NOT_FOUND;
421 for( int i=0; i<preferences->file_probes.size(); ++i ) {
422 ProbePref *pref = preferences->file_probes[i];
423 if( !pref->armed ) continue;
424 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
425 if( !FileFFMPEG::check_sig(this->asset) ) continue;
426 file = new FileFFMPEG(this->asset, this);
429 #ifdef HAVE_COMMERCIAL
430 if( !strcmp(pref->name,"DB") ) { // MediaDB
431 if( !FileDB::check_sig(this->asset) ) continue;
432 file = new FileDB(this->asset, this);
436 if( !strcmp(pref->name,"Scene") ) { // scene file
437 if( !FileScene::check_sig(this->asset, data)) continue;
438 file = new FileScene(this->asset, this);
442 if( !strcmp(pref->name,"DV") ) { // libdv
443 if( !FileDV::check_sig(this->asset) ) continue;
444 file = new FileDV(this->asset, this);
448 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
449 if( !FileSndFile::check_sig(this->asset) ) continue;
450 file = new FileSndFile(this->asset, this);
453 if( !strcmp(pref->name,"PNG") ) { // PNG file
454 if( !FilePNG::check_sig(this->asset) ) continue;
455 file = new FilePNG(this->asset, this);
458 if( !strcmp(pref->name,"PPM") ) { // PPM file
459 if( !FilePPM::check_sig(this->asset) ) continue;
460 file = new FilePPM(this->asset, this);
463 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
464 if( !FileJPEG::check_sig(this->asset) ) continue;
465 file = new FileJPEG(this->asset, this);
468 if( !strcmp(pref->name,"GIF") ) { // GIF file
469 if( FileGIFList::check_sig(this->asset) )
470 file = new FileGIFList(this->asset, this);
471 else if( FileGIF::check_sig(this->asset) )
472 file = new FileGIF(this->asset, this);
477 if( !strcmp(pref->name,"DPX") ) { // DPX file
478 if( !FileDPX::check_sig(this->asset, data) ) continue;
479 file = new FileDPX(this->asset, this);
484 if( !strcmp(pref->name,"EXR") ) { // EXR file
485 if( !FileEXR::check_sig(this->asset, data)) continue;
486 file = new FileEXR(this->asset, this);
490 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
491 if( !FileFLAC::check_sig(this->asset, data)) continue;
492 file = new FileFLAC(this->asset, this);
495 if( !strcmp(pref->name,"CR2") ) { // CR2 file
496 if( !FileCR2::check_sig(this->asset)) continue;
497 file = new FileCR2(this->asset, this);
500 if( !strcmp(pref->name,"TGA") ) { // TGA file
501 if( !FileTGA::check_sig(this->asset) ) continue;
502 file = new FileTGA(this->asset, this);
505 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
506 if( !FileTIFF::check_sig(this->asset) ) continue;
507 file = new FileTIFF(this->asset, this);
511 if( !strcmp(pref->name,"OGG") ) { // OGG file
512 if( !FileOGG::check_sig(this->asset) ) continue;
513 file = new FileOGG(this->asset, this);
516 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
517 if( !FileVorbis::check_sig(this->asset) ) continue;
518 file = new FileVorbis(this->asset, this);
523 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
524 if( !FileMPEG::check_sig(this->asset) ) continue;
525 file = new FileMPEG(this->asset, this);
529 if( !strcmp(pref->name,"EDL") ) { // XML file
530 if( data[0] != '<' ) continue;
531 if( !strncmp(&data[1],"EDL>",4) ||
532 !strncmp(&data[1],"HTAL>",5) ||
533 !strncmp(&data[1],"?xml",4) )
537 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
540 int File::open_file(Preferences *preferences,
547 this->preferences = preferences;
548 this->asset->copy_from(asset, 1);
553 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
555 switch( this->asset->format ) {
556 // get the format now
557 // If you add another format to case 0, you also need to add another case for the
558 // file format #define.
561 if( ret != FILE_OK ) return ret;
563 // format already determined
564 #ifdef HAVE_CIN_3RDPARTY
567 file = new FileAC3(this->asset, this);
572 file = new FileScene(this->asset, this);
576 file = new FileFFMPEG(this->asset, this);
584 //printf("File::open_file 1\n");
585 file = new FileSndFile(this->asset, this);
590 file = new FilePNG(this->asset, this);
595 file = new FilePPM(this->asset, this);
600 file = new FileJPEG(this->asset, this);
604 file = new FileGIF(this->asset, this);
607 file = new FileGIFList(this->asset, this);
612 file = new FileDPX(this->asset, this);
618 file = new FileEXR(this->asset, this);
623 file = new FileFLAC(this->asset, this);
628 file = new FileCR2(this->asset, this);
633 file = new FileTGA(this->asset, this);
638 file = new FileTIFF(this->asset, this);
640 #ifdef HAVE_COMMERCIAL
642 file = new FileDB(this->asset, this);
650 file = new FileMPEG(this->asset, this);
655 file = new FileOGG(this->asset, this);
659 file = new FileVorbis(this->asset, this);
664 file = new FileDV(this->asset, this);
668 file = new FileREF(this->asset, this);
677 // Reopen file with correct parser and get header.
678 if( file->open_file(rd, wr) ) {
679 delete file; file = 0;
680 return FILE_NOT_FOUND;
684 // If file type is a list verify that all files match in dimensions.
685 // Should be done only after the file open function has been performed
686 // Reason: although this function checks if file exists or not but
687 // it has no way of relaying this information back and if this function
688 // is called before open_file the program may accidently interpret file
689 // not found as file size don't match
690 if( !file->verify_file_list() ) {
691 delete file; file = 0;
692 return FILE_SIZE_DONT_MATCH;
696 // Set extra writing parameters to mandatory settings.
698 if( this->asset->dither ) file->set_dither();
702 // one frame image file, not brender, no specific length
703 if( !this->asset->audio_data && this->asset->use_header &&
704 this->asset->video_data && !this->asset->single_frame &&
705 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
706 this->asset->single_frame = 1;
707 this->asset->video_length = -1;
711 // Synchronize header parameters
712 asset->copy_from(this->asset, 1);
715 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
721 void File::delete_temp_samples_buffer()
724 if( temp_samples_buffer ) {
725 for( int j = 0; j < audio_ring_buffers; j++ ) {
726 for( int i = 0; i < asset->channels; i++ ) {
727 delete temp_samples_buffer[j][i];
729 delete [] temp_samples_buffer[j];
732 delete [] temp_samples_buffer;
733 temp_samples_buffer = 0;
734 audio_ring_buffers = 0;
738 void File::delete_temp_frame_buffer()
741 if( temp_frame_buffer ) {
742 for( int k = 0; k < video_ring_buffers; k++ ) {
743 for( int i = 0; i < asset->layers; i++ ) {
744 for( int j = 0; j < video_buffer_size; j++ ) {
745 delete temp_frame_buffer[k][i][j];
747 delete [] temp_frame_buffer[k][i];
749 delete [] temp_frame_buffer[k];
752 delete [] temp_frame_buffer;
753 temp_frame_buffer = 0;
754 video_ring_buffers = 0;
755 video_buffer_size = 0;
759 int File::close_file(int ignore_thread)
761 if( !ignore_thread ) {
766 // The file's asset is a copy of the argument passed to open_file so the
767 // user must copy lengths from the file's asset.
769 asset->audio_length = current_sample;
770 asset->video_length = current_frame;
777 delete_temp_samples_buffer();
778 delete_temp_frame_buffer();
783 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
785 return !file ? -1 : file->get_index(index_file, progress_bar);
788 int File::start_audio_thread(int buffer_size, int ring_buffers)
790 this->audio_ring_buffers = ring_buffers;
793 if( !audio_thread ) {
794 audio_thread = new FileThread(this, 1, 0);
795 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
800 int File::start_video_thread(int buffer_size, int color_model,
801 int ring_buffers, int compressed)
803 this->video_ring_buffers = ring_buffers;
804 this->video_buffer_size = buffer_size;
806 if( !video_thread ) {
807 video_thread = new FileThread(this, 0, 1);
808 video_thread->start_writing(buffer_size, color_model,
809 ring_buffers, compressed);
814 int File::start_video_decode_thread()
816 // Currently, CR2 is the only one which won't work asynchronously, so
817 // we're not using a virtual function yet.
818 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
819 video_thread = new FileThread(this, 0, 1);
820 video_thread->start_reading();
826 int File::stop_audio_thread()
829 audio_thread->stop_writing();
836 int File::stop_video_thread()
839 video_thread->stop_reading();
840 video_thread->stop_writing();
847 FileThread* File::get_video_thread()
852 int File::set_channel(int channel)
854 if( file && channel < asset->channels ) {
855 current_channel = channel;
862 int File::get_channel()
864 return current_channel;
867 // if no>=0, sets new program
868 // returns current program
869 int File::set_program(int no)
871 int result = file ? file->set_program(no) : current_program;
872 current_program = no < 0 ? result : no;
876 int File::get_cell_time(int no, double &time)
878 return file ? file->get_cell_time(no, time) : -1;
881 int File::get_system_time(int64_t &tm)
883 return file ? file->get_system_time(tm) : -1;
886 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
888 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
891 int File::get_video_pid(int track)
893 return file ? file->get_video_pid(track) : -1;
898 int File::get_video_info(int track, int &pid, double &framerate,
899 int &width, int &height, char *title)
902 file->get_video_info(track, pid, framerate, width, height, title);
905 int File::select_video_stream(Asset *asset, int vstream)
908 file->select_video_stream(asset, vstream);
911 int File::select_audio_stream(Asset *asset, int astream)
914 file->select_audio_stream(asset, astream);
918 int File::get_thumbnail(int stream,
919 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
921 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
924 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
926 return file->set_skimming(track, skim, fn, vp);
929 int File::skim_video(int track, void *vp, skim_fn fn)
931 return file->skim_video(track, vp, fn);
935 int File::set_layer(int layer, int is_thread)
937 if( file && layer < asset->layers ) {
938 if( !is_thread && video_thread ) {
939 video_thread->set_layer(layer);
942 current_layer = layer;
950 int64_t File::get_audio_length()
952 int64_t result = asset->audio_length;
953 int64_t base_samplerate = -1;
955 if( base_samplerate > 0 )
956 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
964 int64_t File::get_video_length()
966 int64_t result = asset->video_length;
967 float base_framerate = -1;
969 if( base_framerate > 0 )
970 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
975 return -1; // infinity
979 int64_t File::get_video_position()
981 float base_framerate = -1;
982 if( base_framerate > 0 )
983 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
985 return current_frame;
988 int64_t File::get_audio_position()
990 // int64_t base_samplerate = -1;
991 // if( base_samplerate > 0 )
993 // if( normalized_sample_rate == base_samplerate )
994 // return normalized_sample;
996 // return (int64_t)((double)current_sample /
997 // asset->sample_rate *
1002 return current_sample;
1007 int File::set_audio_position(int64_t position)
1011 if( !file ) return 1;
1013 #define REPOSITION(x, y) \
1014 (labs((x) - (y)) > 1)
1016 float base_samplerate = asset->sample_rate;
1017 current_sample = normalized_sample = position;
1019 // printf("File::set_audio_position %d normalized_sample=%ld\n",
1021 // normalized_sample);
1022 result = file->set_audio_position(current_sample);
1025 printf("File::set_audio_position position=%jd"
1026 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
1027 position, base_samplerate, asset, asset->sample_rate);
1029 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1034 int File::set_video_position(int64_t position,
1038 if( !file ) return 0;
1040 // Convert to file's rate
1041 // if( base_framerate > 0 )
1042 // position = (int64_t)((double)position /
1044 // asset->frame_rate +
1048 if( video_thread && !is_thread ) {
1049 // Call thread. Thread calls this again to set the file state.
1050 video_thread->set_video_position(position);
1053 if( current_frame != position ) {
1055 current_frame = position;
1056 result = file->set_video_position(current_frame);
1063 // No resampling here.
1064 int File::write_samples(Samples **buffer, int64_t len)
1069 write_lock->lock("File::write_samples");
1071 // Convert to arrays for backwards compatability
1072 double *temp[asset->channels];
1073 for( int i = 0; i < asset->channels; i++ ) {
1074 temp[i] = buffer[i]->get_data();
1077 result = file->write_samples(temp, len);
1078 current_sample += len;
1079 normalized_sample += len;
1080 asset->audio_length += len;
1081 write_lock->unlock();
1090 // Can't put any cmodel abstraction here because the filebase couldn't be
1092 int File::write_frames(VFrame ***frames, int len)
1094 //printf("File::write_frames %d\n", __LINE__);
1096 // Store the counters in temps so the filebase can choose to overwrite them.
1098 int current_frame_temp = current_frame;
1099 int video_length_temp = asset->video_length;
1101 write_lock->lock("File::write_frames");
1104 result = file->write_frames(frames, len);
1107 current_frame = current_frame_temp + len;
1108 asset->video_length = video_length_temp + len;
1109 write_lock->unlock();
1114 // Only called by FileThread
1115 int File::write_compressed_frame(VFrame *buffer)
1118 write_lock->lock("File::write_compressed_frame");
1119 result = file->write_compressed_frame(buffer);
1121 asset->video_length++;
1122 write_lock->unlock();
1127 int File::write_audio_buffer(int64_t len)
1130 if( audio_thread ) {
1131 result = audio_thread->write_buffer(len);
1136 int File::write_video_buffer(int64_t len)
1139 if( video_thread ) {
1140 result = video_thread->write_buffer(len);
1146 Samples** File::get_audio_buffer()
1148 if( audio_thread ) return audio_thread->get_audio_buffer();
1152 VFrame*** File::get_video_buffer()
1154 if( video_thread ) {
1155 VFrame*** result = video_thread->get_video_buffer();
1164 int File::read_samples(Samples *samples, int64_t len)
1166 // Never try to read more samples than exist in the file
1167 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1168 len = asset->audio_length - current_sample;
1170 if( len <= 0 ) return 0;
1173 const int debug = 0;
1174 if( debug ) PRINT_TRACE
1176 if( debug ) PRINT_TRACE
1178 double *buffer = samples->get_data();
1180 int64_t base_samplerate = asset->sample_rate;
1183 // Resample recursively calls this with the asset sample rate
1184 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1186 if( debug ) PRINT_TRACE
1187 result = file->read_samples(buffer, len);
1189 if( debug ) PRINT_TRACE
1190 current_sample += len;
1192 normalized_sample += len;
1194 if( debug ) PRINT_TRACE
1199 int File::read_frame(VFrame *frame, int is_thread)
1201 const int debug = 0;
1202 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1203 // current_frame, use_cache, asset->single_frame);
1204 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1205 if( !file ) return 1;
1206 if( debug ) PRINT_TRACE
1208 int supported_colormodel = colormodel_supported(frame->get_color_model());
1210 // if reverse playback reading, use_cache is -1
1211 int cache_active = asset->single_frame ? 1 : use_cache;
1212 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1215 if( cache_active ) {
1216 if( get_cache_frame(frame, cache_position) ) {
1219 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1220 // reverse reading and cache miss, clear cache for new readahead
1226 VFrame *vframe = frame;
1227 if( frame->get_color_model() != BC_COMPRESSED &&
1228 (supported_colormodel != frame->get_color_model() ||
1229 (!file->can_scale_input() &&
1230 (frame->get_w() != asset->width ||
1231 frame->get_h() != asset->height))) ) {
1233 if( !temp_frame->params_match(asset->width, asset->height,
1234 supported_colormodel) ) {
1235 delete temp_frame; temp_frame = 0;
1239 temp_frame = new VFrame(asset->width, asset->height,
1240 supported_colormodel, 0);
1241 temp_frame->clear_frame();
1244 temp_frame->copy_stacks(frame);
1245 vframe = temp_frame;
1247 result = file->read_frame(vframe);
1249 if( frame != vframe )
1250 frame->transfer_from(vframe);
1251 if( cache_active > 0 )
1252 put_cache_frame(frame, cache_position, 1);
1254 else if( frame->get_status() > 0 )
1255 frame->set_status(-1);
1256 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1259 if( result && !current_frame )
1260 frame->clear_frame();
1262 //printf("File::read_frame %d\n", __LINE__);
1264 if( do_read ) current_frame++;
1265 if( debug ) PRINT_TRACE
1269 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1271 if( asset && file ) {
1272 return asset->width == output_w &&
1273 asset->height == output_h &&
1274 file->can_copy_from(asset, position);
1279 // Fill in queries about formats when adding formats here.
1282 int File::strtoformat(const char *format)
1284 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1285 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1286 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1287 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1288 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1289 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1290 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1291 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1292 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1293 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1294 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1295 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1296 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1297 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1298 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1299 if( !strcasecmp(format, _(DPX_NAME)) ) return FILE_DPX;
1300 if( !strcasecmp(format, _(DPX_LIST_NAME)) ) return FILE_DPX_LIST;
1301 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1302 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1303 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1304 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1305 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1306 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1307 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1308 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1309 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1310 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1311 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1312 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1313 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1314 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1315 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1316 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1317 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1318 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1324 const char* File::formattostr(int format)
1327 case FILE_SCENE: return _(SCENE_NAME);
1328 case FILE_AC3: return _(AC3_NAME);
1329 case FILE_WAV: return _(WAV_NAME);
1330 case FILE_PCM: return _(PCM_NAME);
1331 case FILE_AU: return _(AU_NAME);
1332 case FILE_AIFF: return _(AIFF_NAME);
1333 case FILE_SND: return _(SND_NAME);
1334 case FILE_PNG: return _(PNG_NAME);
1335 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1336 case FILE_PPM: return _(PPM_NAME);
1337 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1338 case FILE_JPEG: return _(JPEG_NAME);
1339 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1340 case FILE_CR2: return _(CR2_NAME);
1341 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1342 case FILE_FLAC: return _(FLAC_NAME);
1343 case FILE_GIF: return _(GIF_NAME);
1344 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1345 case FILE_DPX: return _(DPX_NAME);
1346 case FILE_DPX_LIST: return _(DPX_LIST_NAME);
1347 case FILE_EXR: return _(EXR_NAME);
1348 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1349 #ifdef HAVE_LIBZMPEG
1350 case FILE_MPEG: return _(MPEG_NAME);
1352 case FILE_AMPEG: return _(AMPEG_NAME);
1353 case FILE_VMPEG: return _(VMPEG_NAME);
1354 case FILE_TGA: return _(TGA_NAME);
1355 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1356 case FILE_TIFF: return _(TIFF_NAME);
1357 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1359 case FILE_OGG: return _(OGG_NAME);
1360 case FILE_VORBIS: return _(VORBIS_NAME);
1362 case FILE_RAWDV: return _(RAWDV_NAME);
1363 case FILE_FFMPEG: return _(FFMPEG_NAME);
1364 case FILE_DB: return _(DBASE_NAME);
1365 case FILE_REF: return _(REF_NAME);
1367 return _("Unknown");
1370 int File::strtobits(const char *bits)
1372 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1373 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1374 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1375 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1376 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1377 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1378 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1379 return BITSLINEAR16;
1382 const char* File::bitstostr(int bits)
1384 //printf("File::bitstostr\n");
1386 case BITSLINEAR8: return (NAME_8BIT);
1387 case BITSLINEAR16: return (NAME_16BIT);
1388 case BITSLINEAR24: return (NAME_24BIT);
1389 case BITSLINEAR32: return (NAME_32BIT);
1390 case BITSULAW: return (NAME_ULAW);
1391 case BITS_ADPCM: return (NAME_ADPCM);
1392 case BITSFLOAT: return (NAME_FLOAT);
1394 return _("Unknown");
1399 int File::str_to_byteorder(const char *string)
1401 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1405 const char* File::byteorder_to_str(int byte_order)
1407 if( byte_order ) return _("Lo Hi");
1411 int File::bytes_per_sample(int bits)
1414 case BITSLINEAR8: return 1;
1415 case BITSLINEAR16: return 2;
1416 case BITSLINEAR24: return 3;
1417 case BITSLINEAR32: return 4;
1418 case BITSULAW: return 1;
1424 int File::get_best_colormodel(int driver, int vstream)
1426 return file ? file->get_best_colormodel(driver, vstream) :
1427 get_best_colormodel(asset, driver);
1430 int File::get_best_colormodel(Asset *asset, int driver)
1432 switch( asset->format ) {
1434 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1436 #ifdef HAVE_LIBZMPEG
1437 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1440 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1443 case FILE_DPX_LIST: return FileDPX::get_best_colormodel(asset, driver);
1447 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1450 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1452 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1454 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1456 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1457 #ifdef HAVE_COMMERCIAL
1458 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1460 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1461 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1468 int File::colormodel_supported(int colormodel)
1471 return file->colormodel_supported(colormodel);
1477 int64_t File::file_memory_usage()
1479 return file ? file->base_memory_usage() : 0;
1482 int64_t File::get_memory_usage()
1486 result += file_memory_usage();
1487 if( temp_frame ) result += temp_frame->get_data_size();
1488 result += frame_cache->get_memory_usage();
1489 if( video_thread ) result += video_thread->get_memory_usage();
1491 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1496 int File::renders_video(int format)
1501 case FILE_JPEG_LIST:
1515 case FILE_TIFF_LIST:
1523 int File::renders_video(Asset *asset)
1525 return asset->format == FILE_FFMPEG ?
1526 FFMPEG::renders_video(asset->fformat) :
1527 renders_video(asset->format);
1530 int File::renders_audio(int format)
1549 int File::renders_audio(Asset *asset)
1551 return asset->format == FILE_FFMPEG ?
1552 FFMPEG::renders_audio(asset->fformat) :
1553 renders_audio(asset->format);
1556 int File::is_image_render(int format)
1571 const char* File::get_tag(int format)
1574 case FILE_AC3: return "ac3";
1575 case FILE_AIFF: return "aif";
1576 case FILE_AMPEG: return "mp3";
1577 case FILE_AU: return "au";
1578 case FILE_RAWDV: return "dv";
1579 case FILE_DB: return "db";
1580 case FILE_DPX: return "dpx";
1581 case FILE_EXR: return "exr";
1582 case FILE_DPX_LIST: return "dpxs";
1583 case FILE_EXR_LIST: return "exrs";
1584 case FILE_FLAC: return "flac";
1585 case FILE_JPEG: return "jpg";
1586 case FILE_JPEG_LIST: return "jpgs";
1587 case FILE_OGG: return "ogg";
1588 case FILE_GIF: return "gif";
1589 case FILE_GIF_LIST: return "gifs";
1590 case FILE_PCM: return "pcm";
1591 case FILE_PNG: return "png";
1592 case FILE_PNG_LIST: return "pngs";
1593 case FILE_PPM: return "ppm";
1594 case FILE_PPM_LIST: return "ppms";
1595 case FILE_TGA: return "tga";
1596 case FILE_TGA_LIST: return "tgas";
1597 case FILE_TIFF: return "tif";
1598 case FILE_TIFF_LIST: return "tifs";
1599 case FILE_VMPEG: return "m2v";
1600 case FILE_VORBIS: return "ogg";
1601 case FILE_WAV: return "wav";
1602 case FILE_FFMPEG: return "ffmpg";
1603 case FILE_REF: return "ref";
1608 const char* File::get_prefix(int format)
1611 case FILE_PCM: return "PCM";
1612 case FILE_WAV: return "WAV";
1613 case FILE_PNG: return "PNG";
1614 case FILE_PPM: return "PPM";
1615 case FILE_JPEG: return "JPEG";
1616 case FILE_TIFF: return "TIFF";
1617 case FILE_GIF: return "GIF";
1618 case FILE_JPEG_LIST: return "JPEG_LIST";
1619 case FILE_AU: return "AU";
1620 case FILE_AIFF: return "AIFF";
1621 case FILE_SND: return "SND";
1622 case FILE_TGA_LIST: return "TGA_LIST";
1623 case FILE_TGA: return "TGA";
1624 case FILE_MPEG: return "MPEG";
1625 case FILE_AMPEG: return "AMPEG";
1626 case FILE_VMPEG: return "VMPEG";
1627 case FILE_RAWDV: return "RAWDV";
1628 case FILE_TIFF_LIST: return "TIFF_LIST";
1629 case FILE_PNG_LIST: return "PNG_LIST";
1630 case FILE_PPM_LIST: return "PPM_LIST";
1631 case FILE_AC3: return "AC3";
1632 case FILE_DPX: return "DPX";
1633 case FILE_DPX_LIST: return "DPX_LIST";
1634 case FILE_EXR: return "EXR";
1635 case FILE_EXR_LIST: return "EXR_LIST";
1636 case FILE_CR2: return "CR2";
1637 case FILE_OGG: return "OGG";
1638 case FILE_VORBIS: return "VORBIS";
1639 case FILE_FLAC: return "FLAC";
1640 case FILE_FFMPEG: return "FFMPEG";
1641 case FILE_SCENE: return "SCENE";
1642 case FILE_CR2_LIST: return "CR2_LIST";
1643 case FILE_GIF_LIST: return "GIF_LIST";
1644 case FILE_DB: return "DB";
1645 case FILE_REF: return "REF";
1647 return _("UNKNOWN");
1651 int File::record_fd()
1653 return file ? file->record_fd() : -1;
1657 void File::get_exe_path(char *result, char *bnp)
1659 // Get executable path, basename
1660 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1663 char *ptr = strrchr(result, '/');
1664 if( ptr ) *ptr++ = 0; else ptr = result;
1665 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1673 void File::getenv_path(char *result, const char *path)
1675 char *rp = result, *ep = rp + BCTEXTLEN-1;
1676 const char *cp = path;
1677 // any env var can be used here to expand a path as:
1678 // "path...$id...": and id is a word as alpha,alnum...
1679 // expands as "path...getenv(id)..."
1680 // CIN_PATH, CIN_LIB are set in main.C,
1681 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1682 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1683 const char *bp = cp; char *p = rp;
1684 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1686 const char *envp = getenv(rp); // read env value
1687 if( !envp ) continue;
1688 while( *envp && rp < ep ) *rp++ = *envp++;
1689 cp = bp; // subst id=value
1697 void File::setenv_path(const char *var, const char *path, int overwrite)
1699 char env_path[BCTEXTLEN];
1700 getenv_path(env_path, path);
1701 setenv(var, env_path, overwrite);
1704 * @brief Set various environment variables to pass constant values to
1705 * forks, shell scripts, and other parts of CinGG.
1707 void File::init_cin_env_vars()
1709 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1711 struct tm *dateTime;
1712 get_exe_path(env_path, env_pkg);
1713 setenv_path("CIN_PATH", env_path, 1);
1714 setenv_path("CIN_PKG", env_pkg, 1);
1715 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1716 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1717 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1718 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1719 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1720 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1721 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1722 // Create env_var CINGG_BUILD for use when running as AppImage
1723 stat(env_path, &st);
1724 dateTime = gmtime(&st.st_mtime);
1725 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1726 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1727 build_name[sizeof(build_name) - 1] = 0;
1728 setenv_path("CINGG_BUILD", build_name, 1);