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[] = {
413 const int File::nb_probes =
414 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
419 FILE *fp = fopen(this->asset->path, "rb");
420 if( !fp ) return FILE_NOT_FOUND;
422 memset(data,0,sizeof(data));
423 int ret = fread(data, 1, 16, fp);
425 if( !ret ) return FILE_NOT_FOUND;
427 for( int i=0; i<preferences->file_probes.size(); ++i ) {
428 ProbePref *pref = preferences->file_probes[i];
429 if( !pref->armed ) continue;
430 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
431 if( !FileFFMPEG::check_sig(this->asset) ) continue;
432 file = new FileFFMPEG(this->asset, this);
435 #ifdef HAVE_COMMERCIAL
436 if( !strcmp(pref->name,"DB") ) { // MediaDB
437 if( !FileDB::check_sig(this->asset) ) continue;
438 file = new FileDB(this->asset, this);
442 if( !strcmp(pref->name,"Scene") ) { // scene file
443 if( !FileScene::check_sig(this->asset, data)) continue;
444 file = new FileScene(this->asset, this);
448 if( !strcmp(pref->name,"DV") ) { // libdv
449 if( !FileDV::check_sig(this->asset) ) continue;
450 file = new FileDV(this->asset, this);
454 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
455 if( !FileSndFile::check_sig(this->asset) ) continue;
456 file = new FileSndFile(this->asset, this);
459 if( !strcmp(pref->name,"PNG") ) { // PNG file
460 if( !FilePNG::check_sig(this->asset) ) continue;
461 file = new FilePNG(this->asset, this);
464 if( !strcmp(pref->name,"PPM") ) { // PPM file
465 if( !FilePPM::check_sig(this->asset) ) continue;
466 file = new FilePPM(this->asset, this);
469 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
470 if( !FileJPEG::check_sig(this->asset) ) continue;
471 file = new FileJPEG(this->asset, this);
475 if( !strcmp(pref->name,"GIF") ) { // GIF file
476 if( FileGIFList::check_sig(this->asset) )
477 file = new FileGIFList(this->asset, this);
478 else if( FileGIF::check_sig(this->asset) )
479 file = new FileGIF(this->asset, this);
485 if( !strcmp(pref->name,"DPX") ) { // DPX file
486 if( !FileDPX::check_sig(this->asset, data) ) continue;
487 file = new FileDPX(this->asset, this);
492 if( !strcmp(pref->name,"EXR") ) { // EXR file
493 if( !FileEXR::check_sig(this->asset, data)) continue;
494 file = new FileEXR(this->asset, this);
498 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
499 if( !FileFLAC::check_sig(this->asset, data)) continue;
500 file = new FileFLAC(this->asset, this);
503 if( !strcmp(pref->name,"CR2") ) { // CR2 file
504 if( !FileCR2::check_sig(this->asset)) continue;
505 file = new FileCR2(this->asset, this);
508 if( !strcmp(pref->name,"TGA") ) { // TGA file
509 if( !FileTGA::check_sig(this->asset) ) continue;
510 file = new FileTGA(this->asset, this);
513 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
514 if( !FileTIFF::check_sig(this->asset) ) continue;
515 file = new FileTIFF(this->asset, this);
519 if( !strcmp(pref->name,"OGG") ) { // OGG file
520 if( !FileOGG::check_sig(this->asset) ) continue;
521 file = new FileOGG(this->asset, this);
524 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
525 if( !FileVorbis::check_sig(this->asset) ) continue;
526 file = new FileVorbis(this->asset, this);
531 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
532 if( !FileMPEG::check_sig(this->asset) ) continue;
533 file = new FileMPEG(this->asset, this);
537 if( !strcmp(pref->name,"EDL") ) { // XML file
538 if( data[0] != '<' ) continue;
539 if( !strncmp(&data[1],"EDL>",4) ||
540 !strncmp(&data[1],"HTAL>",5) ||
541 !strncmp(&data[1],"?xml",4) )
545 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
548 int File::open_file(Preferences *preferences,
555 this->preferences = preferences;
556 this->asset->copy_from(asset, 1);
561 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
563 switch( this->asset->format ) {
564 // get the format now
565 // If you add another format to case 0, you also need to add another case for the
566 // file format #define.
569 if( ret != FILE_OK ) return ret;
571 // format already determined
572 #ifdef HAVE_CIN_3RDPARTY
575 file = new FileAC3(this->asset, this);
580 file = new FileScene(this->asset, this);
584 file = new FileFFMPEG(this->asset, this);
592 //printf("File::open_file 1\n");
593 file = new FileSndFile(this->asset, this);
598 file = new FilePNG(this->asset, this);
603 file = new FilePPM(this->asset, this);
608 file = new FileJPEG(this->asset, this);
612 file = new FileGIF(this->asset, this);
615 file = new FileGIFList(this->asset, this);
621 file = new FileDPX(this->asset, this);
627 file = new FileEXR(this->asset, this);
632 file = new FileFLAC(this->asset, this);
637 file = new FileCR2(this->asset, this);
642 file = new FileTGA(this->asset, this);
647 file = new FileTIFF(this->asset, this);
649 #ifdef HAVE_COMMERCIAL
651 file = new FileDB(this->asset, this);
659 file = new FileMPEG(this->asset, this);
664 file = new FileOGG(this->asset, this);
668 file = new FileVorbis(this->asset, this);
673 file = new FileDV(this->asset, this);
677 file = new FileREF(this->asset, this);
686 // Reopen file with correct parser and get header.
687 if( file->open_file(rd, wr) ) {
688 delete file; file = 0;
689 return FILE_NOT_FOUND;
693 // If file type is a list verify that all files match in dimensions.
694 // Should be done only after the file open function has been performed
695 // Reason: although this function checks if file exists or not but
696 // it has no way of relaying this information back and if this function
697 // is called before open_file the program may accidently interpret file
698 // not found as file size don't match
699 if( !file->verify_file_list() ) {
700 delete file; file = 0;
701 return FILE_SIZE_DONT_MATCH;
705 // Set extra writing parameters to mandatory settings.
707 if( this->asset->dither ) file->set_dither();
711 // one frame image file, not brender, no specific length
712 if( !this->asset->audio_data && this->asset->use_header &&
713 this->asset->video_data && !this->asset->single_frame &&
714 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
715 this->asset->single_frame = 1;
716 this->asset->video_length = -1;
720 // Synchronize header parameters
721 asset->copy_from(this->asset, 1);
724 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
730 void File::delete_temp_samples_buffer()
733 if( temp_samples_buffer ) {
734 for( int j = 0; j < audio_ring_buffers; j++ ) {
735 for( int i = 0; i < asset->channels; i++ ) {
736 delete temp_samples_buffer[j][i];
738 delete [] temp_samples_buffer[j];
741 delete [] temp_samples_buffer;
742 temp_samples_buffer = 0;
743 audio_ring_buffers = 0;
747 void File::delete_temp_frame_buffer()
750 if( temp_frame_buffer ) {
751 for( int k = 0; k < video_ring_buffers; k++ ) {
752 for( int i = 0; i < asset->layers; i++ ) {
753 for( int j = 0; j < video_buffer_size; j++ ) {
754 delete temp_frame_buffer[k][i][j];
756 delete [] temp_frame_buffer[k][i];
758 delete [] temp_frame_buffer[k];
761 delete [] temp_frame_buffer;
762 temp_frame_buffer = 0;
763 video_ring_buffers = 0;
764 video_buffer_size = 0;
768 int File::close_file(int ignore_thread)
770 if( !ignore_thread ) {
775 // The file's asset is a copy of the argument passed to open_file so the
776 // user must copy lengths from the file's asset.
778 asset->audio_length = current_sample;
779 asset->video_length = current_frame;
786 delete_temp_samples_buffer();
787 delete_temp_frame_buffer();
792 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
794 return !file ? -1 : file->get_index(index_file, progress_bar);
797 int File::start_audio_thread(int buffer_size, int ring_buffers)
799 this->audio_ring_buffers = ring_buffers;
802 if( !audio_thread ) {
803 audio_thread = new FileThread(this, 1, 0);
804 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
809 int File::start_video_thread(int buffer_size, int color_model,
810 int ring_buffers, int compressed)
812 this->video_ring_buffers = ring_buffers;
813 this->video_buffer_size = buffer_size;
815 if( !video_thread ) {
816 video_thread = new FileThread(this, 0, 1);
817 video_thread->start_writing(buffer_size, color_model,
818 ring_buffers, compressed);
823 int File::start_video_decode_thread()
825 // Currently, CR2 is the only one which won't work asynchronously, so
826 // we're not using a virtual function yet.
827 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
828 video_thread = new FileThread(this, 0, 1);
829 video_thread->start_reading();
835 int File::stop_audio_thread()
838 audio_thread->stop_writing();
845 int File::stop_video_thread()
848 video_thread->stop_reading();
849 video_thread->stop_writing();
856 FileThread* File::get_video_thread()
861 int File::set_channel(int channel)
863 if( file && channel < asset->channels ) {
864 current_channel = channel;
871 int File::get_channel()
873 return current_channel;
876 // if no>=0, sets new program
877 // returns current program
878 int File::set_program(int no)
880 int result = file ? file->set_program(no) : current_program;
881 current_program = no < 0 ? result : no;
885 int File::get_cell_time(int no, double &time)
887 return file ? file->get_cell_time(no, time) : -1;
890 int File::get_system_time(int64_t &tm)
892 return file ? file->get_system_time(tm) : -1;
895 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
897 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
900 int File::get_video_pid(int track)
902 return file ? file->get_video_pid(track) : -1;
907 int File::get_video_info(int track, int &pid, double &framerate,
908 int &width, int &height, char *title)
911 file->get_video_info(track, pid, framerate, width, height, title);
914 int File::select_video_stream(Asset *asset, int vstream)
917 file->select_video_stream(asset, vstream);
920 int File::select_audio_stream(Asset *asset, int astream)
923 file->select_audio_stream(asset, astream);
927 int File::get_thumbnail(int stream,
928 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
930 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
933 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
935 return file->set_skimming(track, skim, fn, vp);
938 int File::skim_video(int track, void *vp, skim_fn fn)
940 return file->skim_video(track, vp, fn);
944 int File::set_layer(int layer, int is_thread)
946 if( file && layer < asset->layers ) {
947 if( !is_thread && video_thread ) {
948 video_thread->set_layer(layer);
951 current_layer = layer;
959 int64_t File::get_audio_length()
961 int64_t result = asset->audio_length;
962 int64_t base_samplerate = -1;
964 if( base_samplerate > 0 )
965 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
973 int64_t File::get_video_length()
975 int64_t result = asset->video_length;
976 float base_framerate = -1;
978 if( base_framerate > 0 )
979 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
984 return -1; // infinity
988 int64_t File::get_video_position()
990 float base_framerate = -1;
991 if( base_framerate > 0 )
992 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
994 return current_frame;
997 int64_t File::get_audio_position()
999 // int64_t base_samplerate = -1;
1000 // if( base_samplerate > 0 )
1002 // if( normalized_sample_rate == base_samplerate )
1003 // return normalized_sample;
1005 // return (int64_t)((double)current_sample /
1006 // asset->sample_rate *
1007 // base_samplerate +
1011 return current_sample;
1016 int File::set_audio_position(int64_t position)
1020 if( !file ) return 1;
1022 #define REPOSITION(x, y) \
1023 (labs((x) - (y)) > 1)
1025 float base_samplerate = asset->sample_rate;
1026 current_sample = normalized_sample = position;
1028 // printf("File::set_audio_position %d normalized_sample=%ld\n",
1030 // normalized_sample);
1031 result = file->set_audio_position(current_sample);
1034 printf("File::set_audio_position position=%jd"
1035 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
1036 position, base_samplerate, asset, asset->sample_rate);
1038 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1043 int File::set_video_position(int64_t position,
1047 if( !file ) return 0;
1049 // Convert to file's rate
1050 // if( base_framerate > 0 )
1051 // position = (int64_t)((double)position /
1053 // asset->frame_rate +
1057 if( video_thread && !is_thread ) {
1058 // Call thread. Thread calls this again to set the file state.
1059 video_thread->set_video_position(position);
1062 if( current_frame != position ) {
1064 current_frame = position;
1065 result = file->set_video_position(current_frame);
1072 // No resampling here.
1073 int File::write_samples(Samples **buffer, int64_t len)
1078 write_lock->lock("File::write_samples");
1080 // Convert to arrays for backwards compatability
1081 double *temp[asset->channels];
1082 for( int i = 0; i < asset->channels; i++ ) {
1083 temp[i] = buffer[i]->get_data();
1086 result = file->write_samples(temp, len);
1087 current_sample += len;
1088 normalized_sample += len;
1089 asset->audio_length += len;
1090 write_lock->unlock();
1099 // Can't put any cmodel abstraction here because the filebase couldn't be
1101 int File::write_frames(VFrame ***frames, int len)
1103 //printf("File::write_frames %d\n", __LINE__);
1105 // Store the counters in temps so the filebase can choose to overwrite them.
1107 int current_frame_temp = current_frame;
1108 int video_length_temp = asset->video_length;
1110 write_lock->lock("File::write_frames");
1113 result = file->write_frames(frames, len);
1116 current_frame = current_frame_temp + len;
1117 asset->video_length = video_length_temp + len;
1118 write_lock->unlock();
1123 // Only called by FileThread
1124 int File::write_compressed_frame(VFrame *buffer)
1127 write_lock->lock("File::write_compressed_frame");
1128 result = file->write_compressed_frame(buffer);
1130 asset->video_length++;
1131 write_lock->unlock();
1136 int File::write_audio_buffer(int64_t len)
1139 if( audio_thread ) {
1140 result = audio_thread->write_buffer(len);
1145 int File::write_video_buffer(int64_t len)
1148 if( video_thread ) {
1149 result = video_thread->write_buffer(len);
1155 Samples** File::get_audio_buffer()
1157 if( audio_thread ) return audio_thread->get_audio_buffer();
1161 VFrame*** File::get_video_buffer()
1163 if( video_thread ) {
1164 VFrame*** result = video_thread->get_video_buffer();
1173 int File::read_samples(Samples *samples, int64_t len)
1175 // Never try to read more samples than exist in the file
1176 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1177 len = asset->audio_length - current_sample;
1179 if( len <= 0 ) return 0;
1182 const int debug = 0;
1183 if( debug ) PRINT_TRACE
1185 if( debug ) PRINT_TRACE
1187 double *buffer = samples->get_data();
1189 int64_t base_samplerate = asset->sample_rate;
1192 // Resample recursively calls this with the asset sample rate
1193 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1195 if( debug ) PRINT_TRACE
1196 result = file->read_samples(buffer, len);
1198 if( debug ) PRINT_TRACE
1199 current_sample += len;
1201 normalized_sample += len;
1203 if( debug ) PRINT_TRACE
1208 int File::read_frame(VFrame *frame, int is_thread)
1210 const int debug = 0;
1211 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1212 // current_frame, use_cache, asset->single_frame);
1213 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1214 if( !file ) return 1;
1215 if( debug ) PRINT_TRACE
1217 int supported_colormodel = colormodel_supported(frame->get_color_model());
1219 // if reverse playback reading, use_cache is -1
1220 int cache_active = asset->single_frame ? 1 : use_cache;
1221 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1224 if( cache_active ) {
1225 if( get_cache_frame(frame, cache_position) ) {
1228 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1229 // reverse reading and cache miss, clear cache for new readahead
1235 VFrame *vframe = frame;
1236 if( frame->get_color_model() != BC_COMPRESSED &&
1237 (supported_colormodel != frame->get_color_model() ||
1238 (!file->can_scale_input() &&
1239 (frame->get_w() != asset->width ||
1240 frame->get_h() != asset->height))) ) {
1242 if( !temp_frame->params_match(asset->width, asset->height,
1243 supported_colormodel) ) {
1244 delete temp_frame; temp_frame = 0;
1248 temp_frame = new VFrame(asset->width, asset->height,
1249 supported_colormodel, 0);
1250 temp_frame->clear_frame();
1253 temp_frame->copy_stacks(frame);
1254 vframe = temp_frame;
1256 result = file->read_frame(vframe);
1258 if( frame != vframe )
1259 frame->transfer_from(vframe);
1260 if( cache_active > 0 )
1261 put_cache_frame(frame, cache_position, 1);
1263 else if( frame->get_status() > 0 )
1264 frame->set_status(-1);
1265 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1268 if( result && !current_frame )
1269 frame->clear_frame();
1271 //printf("File::read_frame %d\n", __LINE__);
1273 if( do_read ) current_frame++;
1274 if( debug ) PRINT_TRACE
1278 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1280 if( asset && file ) {
1281 return asset->width == output_w &&
1282 asset->height == output_h &&
1283 file->can_copy_from(asset, position);
1288 // Fill in queries about formats when adding formats here.
1291 int File::strtoformat(const char *format)
1293 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1294 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1295 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1296 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1297 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1298 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1299 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1300 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1301 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1302 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1303 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1304 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1305 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1306 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1307 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1308 if( !strcasecmp(format, _(DPX_NAME)) ) return FILE_DPX;
1309 if( !strcasecmp(format, _(DPX_LIST_NAME)) ) return FILE_DPX_LIST;
1310 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1311 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1312 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1313 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1314 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1315 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1316 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1317 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1318 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1319 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1320 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1321 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1322 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1323 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1324 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1325 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1326 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1327 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1333 const char* File::formattostr(int format)
1336 case FILE_SCENE: return _(SCENE_NAME);
1337 case FILE_AC3: return _(AC3_NAME);
1338 case FILE_WAV: return _(WAV_NAME);
1339 case FILE_PCM: return _(PCM_NAME);
1340 case FILE_AU: return _(AU_NAME);
1341 case FILE_AIFF: return _(AIFF_NAME);
1342 case FILE_SND: return _(SND_NAME);
1343 case FILE_PNG: return _(PNG_NAME);
1344 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1345 case FILE_PPM: return _(PPM_NAME);
1346 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1347 case FILE_JPEG: return _(JPEG_NAME);
1348 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1349 case FILE_CR2: return _(CR2_NAME);
1350 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1351 case FILE_FLAC: return _(FLAC_NAME);
1352 case FILE_GIF: return _(GIF_NAME);
1353 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1354 case FILE_DPX: return _(DPX_NAME);
1355 case FILE_DPX_LIST: return _(DPX_LIST_NAME);
1356 case FILE_EXR: return _(EXR_NAME);
1357 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1358 #ifdef HAVE_LIBZMPEG
1359 case FILE_MPEG: return _(MPEG_NAME);
1361 case FILE_AMPEG: return _(AMPEG_NAME);
1362 case FILE_VMPEG: return _(VMPEG_NAME);
1363 case FILE_TGA: return _(TGA_NAME);
1364 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1365 case FILE_TIFF: return _(TIFF_NAME);
1366 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1368 case FILE_OGG: return _(OGG_NAME);
1369 case FILE_VORBIS: return _(VORBIS_NAME);
1371 case FILE_RAWDV: return _(RAWDV_NAME);
1372 case FILE_FFMPEG: return _(FFMPEG_NAME);
1373 case FILE_DB: return _(DBASE_NAME);
1374 case FILE_REF: return _(REF_NAME);
1376 return _("Unknown");
1379 int File::strtobits(const char *bits)
1381 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1382 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1383 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1384 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1385 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1386 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1387 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1388 return BITSLINEAR16;
1391 const char* File::bitstostr(int bits)
1393 //printf("File::bitstostr\n");
1395 case BITSLINEAR8: return (NAME_8BIT);
1396 case BITSLINEAR16: return (NAME_16BIT);
1397 case BITSLINEAR24: return (NAME_24BIT);
1398 case BITSLINEAR32: return (NAME_32BIT);
1399 case BITSULAW: return (NAME_ULAW);
1400 case BITS_ADPCM: return (NAME_ADPCM);
1401 case BITSFLOAT: return (NAME_FLOAT);
1403 return _("Unknown");
1408 int File::str_to_byteorder(const char *string)
1410 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1414 const char* File::byteorder_to_str(int byte_order)
1416 if( byte_order ) return _("Lo Hi");
1420 int File::bytes_per_sample(int bits)
1423 case BITSLINEAR8: return 1;
1424 case BITSLINEAR16: return 2;
1425 case BITSLINEAR24: return 3;
1426 case BITSLINEAR32: return 4;
1427 case BITSULAW: return 1;
1433 int File::get_best_colormodel(int driver, int vstream)
1435 return file ? file->get_best_colormodel(driver, vstream) :
1436 get_best_colormodel(asset, driver);
1439 int File::get_best_colormodel(Asset *asset, int driver)
1441 switch( asset->format ) {
1443 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1445 #ifdef HAVE_LIBZMPEG
1446 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1449 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1452 case FILE_DPX_LIST: return FileDPX::get_best_colormodel(asset, driver);
1456 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1459 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1461 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1463 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1465 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1466 #ifdef HAVE_COMMERCIAL
1467 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1469 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1470 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1477 int File::colormodel_supported(int colormodel)
1480 return file->colormodel_supported(colormodel);
1486 int64_t File::file_memory_usage()
1488 return file ? file->base_memory_usage() : 0;
1491 int64_t File::get_memory_usage()
1495 result += file_memory_usage();
1496 if( temp_frame ) result += temp_frame->get_data_size();
1497 result += frame_cache->get_memory_usage();
1498 if( video_thread ) result += video_thread->get_memory_usage();
1500 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1505 int File::renders_video(int format)
1510 case FILE_JPEG_LIST:
1524 case FILE_TIFF_LIST:
1532 int File::renders_video(Asset *asset)
1534 return asset->format == FILE_FFMPEG ?
1535 FFMPEG::renders_video(asset->fformat) :
1536 renders_video(asset->format);
1539 int File::renders_audio(int format)
1558 int File::renders_audio(Asset *asset)
1560 return asset->format == FILE_FFMPEG ?
1561 FFMPEG::renders_audio(asset->fformat) :
1562 renders_audio(asset->format);
1565 int File::is_image_render(int format)
1580 const char* File::get_tag(int format)
1583 case FILE_AC3: return "ac3";
1584 case FILE_AIFF: return "aif";
1585 case FILE_AMPEG: return "mp3";
1586 case FILE_AU: return "au";
1587 case FILE_RAWDV: return "dv";
1588 case FILE_DB: return "db";
1589 case FILE_DPX: return "dpx";
1590 case FILE_EXR: return "exr";
1591 case FILE_DPX_LIST: return "dpxs";
1592 case FILE_EXR_LIST: return "exrs";
1593 case FILE_FLAC: return "flac";
1594 case FILE_JPEG: return "jpg";
1595 case FILE_JPEG_LIST: return "jpgs";
1596 case FILE_OGG: return "ogg";
1597 case FILE_GIF: return "gif";
1598 case FILE_GIF_LIST: return "gifs";
1599 case FILE_PCM: return "pcm";
1600 case FILE_PNG: return "png";
1601 case FILE_PNG_LIST: return "pngs";
1602 case FILE_PPM: return "ppm";
1603 case FILE_PPM_LIST: return "ppms";
1604 case FILE_TGA: return "tga";
1605 case FILE_TGA_LIST: return "tgas";
1606 case FILE_TIFF: return "tif";
1607 case FILE_TIFF_LIST: return "tifs";
1608 case FILE_VMPEG: return "m2v";
1609 case FILE_VORBIS: return "ogg";
1610 case FILE_WAV: return "wav";
1611 case FILE_FFMPEG: return "ffmpg";
1612 case FILE_REF: return "ref";
1617 const char* File::get_prefix(int format)
1620 case FILE_PCM: return "PCM";
1621 case FILE_WAV: return "WAV";
1622 case FILE_PNG: return "PNG";
1623 case FILE_PPM: return "PPM";
1624 case FILE_JPEG: return "JPEG";
1625 case FILE_TIFF: return "TIFF";
1626 case FILE_GIF: return "GIF";
1627 case FILE_JPEG_LIST: return "JPEG_LIST";
1628 case FILE_AU: return "AU";
1629 case FILE_AIFF: return "AIFF";
1630 case FILE_SND: return "SND";
1631 case FILE_TGA_LIST: return "TGA_LIST";
1632 case FILE_TGA: return "TGA";
1633 case FILE_MPEG: return "MPEG";
1634 case FILE_AMPEG: return "AMPEG";
1635 case FILE_VMPEG: return "VMPEG";
1636 case FILE_RAWDV: return "RAWDV";
1637 case FILE_TIFF_LIST: return "TIFF_LIST";
1638 case FILE_PNG_LIST: return "PNG_LIST";
1639 case FILE_PPM_LIST: return "PPM_LIST";
1640 case FILE_AC3: return "AC3";
1641 case FILE_DPX: return "DPX";
1642 case FILE_DPX_LIST: return "DPX_LIST";
1643 case FILE_EXR: return "EXR";
1644 case FILE_EXR_LIST: return "EXR_LIST";
1645 case FILE_CR2: return "CR2";
1646 case FILE_OGG: return "OGG";
1647 case FILE_VORBIS: return "VORBIS";
1648 case FILE_FLAC: return "FLAC";
1649 case FILE_FFMPEG: return "FFMPEG";
1650 case FILE_SCENE: return "SCENE";
1651 case FILE_CR2_LIST: return "CR2_LIST";
1652 case FILE_GIF_LIST: return "GIF_LIST";
1653 case FILE_DB: return "DB";
1654 case FILE_REF: return "REF";
1656 return _("UNKNOWN");
1660 int File::record_fd()
1662 return file ? file->record_fd() : -1;
1666 void File::get_exe_path(char *result, char *bnp)
1668 // Get executable path, basename
1669 #if !defined(__FreeBSD__)
1670 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1672 char exe_path[BCTEXTLEN];
1673 sprintf(exe_path,"/proc/%d/file",getpid());
1674 int len = readlink(exe_path, result, BCTEXTLEN-1);
1679 char *ptr = strrchr(result, '/');
1680 if( ptr ) *ptr++ = 0; else ptr = result;
1681 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1689 void File::getenv_path(char *result, const char *path)
1691 char *rp = result, *ep = rp + BCTEXTLEN-1;
1692 const char *cp = path;
1693 // any env var can be used here to expand a path as:
1694 // "path...$id...": and id is a word as alpha,alnum...
1695 // expands as "path...getenv(id)..."
1696 // CIN_PATH, CIN_LIB are set in main.C,
1697 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1698 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1699 const char *bp = cp; char *p = rp;
1700 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1702 const char *envp = getenv(rp); // read env value
1703 if( !envp ) continue;
1704 while( *envp && rp < ep ) *rp++ = *envp++;
1705 cp = bp; // subst id=value
1713 void File::setenv_path(const char *var, const char *path, int overwrite)
1715 char env_path[BCTEXTLEN];
1716 getenv_path(env_path, path);
1717 setenv(var, env_path, overwrite);
1720 * @brief Set various environment variables to pass constant values to
1721 * forks, shell scripts, and other parts of CinGG.
1723 void File::init_cin_env_vars()
1725 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1727 struct tm *dateTime;
1728 get_exe_path(env_path, env_pkg);
1729 setenv_path("CIN_PATH", env_path, 1);
1730 setenv_path("CIN_PKG", env_pkg, 1);
1731 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1732 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1733 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1734 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1735 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1736 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1737 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1738 // Create env_var CINGG_BUILD for use when running as AppImage
1739 stat(env_path, &st);
1740 dateTime = gmtime(&st.st_mtime);
1741 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1742 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1743 build_name[sizeof(build_name) - 1] = 0;
1744 setenv_path("CINGG_BUILD", build_name, 1);