3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 // work arounds (centos)
33 #define INT64_MAX 9223372036854775807LL
38 #include "bcsignals.h"
39 #include "byteorder.h"
41 #include "condition.h"
49 #include "fileffmpeg.h"
55 #undef HAVE_STDLIB_H // automake conflict
60 #include "filescene.h"
61 #include "filesndfile.h"
63 #include "filethread.h"
65 #include "filevorbis.h"
67 #include "formatwindow.h"
68 #include "formattools.h"
69 #include "framecache.h"
71 #include "mainprogress.inc"
74 #include "packagingengine.h"
75 #include "pluginserver.h"
76 #include "preferences.h"
77 #include "probeprefs.h"
82 //suppress noref warning
83 void *vorbis0_ov_callbacks[] = {
84 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
85 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
93 format_completion = new Condition(1, "File::format_completion");
94 write_lock = new Condition(1, "File::write_lock");
95 frame_cache = new FrameCache;
101 if( getting_options ) {
102 if( format_window ) format_window->set_done(0);
103 format_completion->lock("File::~File");
104 format_completion->unlock();
113 asset->Garbage::remove_user();
114 delete format_completion;
119 void File::reset_parameters()
132 normalized_sample = 0;
135 playback_subtitle = -1;
139 temp_samples_buffer = 0;
140 temp_frame_buffer = 0;
141 current_frame_buffer = 0;
142 audio_ring_buffers = 0;
143 video_ring_buffers = 0;
144 video_buffer_size = 0;
147 int File::raise_window()
149 if( getting_options && format_window ) {
150 format_window->raise_window();
151 format_window->flush();
156 void File::close_window()
158 if( getting_options ) {
159 format_window->lock_window("File::close_window");
160 format_window->set_done(1);
161 format_window->unlock_window();
166 int File::get_options(FormatTools *format,
167 int audio_options, int video_options)
169 BC_WindowBase *parent_window = format->window;
170 Asset *asset = format->asset;
171 EDL *edl = format->mwindow ? format->mwindow->edl : 0;
174 format_completion->lock("File::get_options");
175 switch( asset->format ) {
176 #ifdef HAVE_CIN_3RDPARTY
177 case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
178 audio_options, video_options, edl);
183 FileDV::get_parameters(parent_window, asset, format_window,
184 audio_options, video_options, edl);
192 FileSndFile::get_parameters(parent_window, asset, format_window,
193 audio_options, video_options, edl);
196 FileFFMPEG::get_parameters(parent_window, asset, format_window,
197 audio_options, video_options, edl);
202 FileMPEG::get_parameters(parent_window, asset, format_window,
203 audio_options, video_options, edl);
208 FileJPEG::get_parameters(parent_window, asset, format_window,
209 audio_options, video_options, edl);
214 FileEXR::get_parameters(parent_window, asset, format_window,
215 audio_options, video_options, edl);
219 FileFLAC::get_parameters(parent_window, asset, format_window,
220 audio_options, video_options, edl);
224 FilePNG::get_parameters(parent_window, asset, format_window,
225 audio_options, video_options, edl);
229 FilePPM::get_parameters(parent_window, asset, format_window,
230 audio_options, video_options, edl);
234 FileTGA::get_parameters(parent_window, asset, format_window,
235 audio_options, video_options, edl);
239 FileTIFF::get_parameters(parent_window, asset, format_window,
240 audio_options, video_options, edl);
244 FileOGG::get_parameters(parent_window, asset, format_window,
245 audio_options, video_options, edl);
252 if( !format_window ) {
253 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
254 parent_window->get_abs_cursor_x(1),
255 parent_window->get_abs_cursor_y(1));
256 format_window = errorbox;
259 errorbox->create_objects(_("This format doesn't support audio."));
262 errorbox->create_objects(_("This format doesn't support video."));
263 errorbox->run_window();
269 format_completion->unlock();
273 int File::can_scale_input(Asset *asset)
275 switch( asset->format ) {
285 if( asset->video_length < 0 )
292 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
294 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
295 cpus = 8; // 8 cpus ought to decode just about anything
296 // Set all instances so gets work.
302 int File::set_preload(int64_t size)
304 this->playback_preload = size;
308 void File::set_subtitle(int value)
310 this->playback_subtitle = value;
311 if( file ) file->set_subtitle(value);
314 void File::set_interpolate_raw(int value)
316 this->interpolate_raw = value;
319 void File::set_white_balance_raw(int value)
321 this->white_balance_raw = value;
324 void File::set_cache_frames(int value)
326 // caching only done locally
331 int File::purge_cache()
333 // caching only done locally
335 if( frame_cache->cache_items() > 0 ) {
336 frame_cache->remove_all();
342 int File::delete_oldest()
344 // return the number of bytes freed
345 return frame_cache->delete_oldest();
348 int File::get_cache_frame(VFrame *frame, int64_t position)
350 return frame_cache->get_cache_frame(frame, position,
351 current_layer, asset->frame_rate);
354 void File::put_cache_frame(VFrame *frame, int64_t position, int use_copy)
356 frame_cache->put_cache_frame(frame,
357 position, current_layer, asset->frame_rate, use_copy);
360 int File::get_use_cache()
366 // file driver in order of probe precidence
367 // can be reordered in preferences->interface
368 const char *File::default_probes[] = {
395 const int File::nb_probes =
396 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
401 FILE *fp = fopen(this->asset->path, "rb");
402 if( !fp ) return FILE_NOT_FOUND;
404 memset(data,0,sizeof(data));
405 int ret = fread(data, 1, 16, fp);
407 if( !ret ) return FILE_NOT_FOUND;
409 for( int i=0; i<preferences->file_probes.size(); ++i ) {
410 ProbePref *pref = preferences->file_probes[i];
411 if( !pref->armed ) continue;
412 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
413 if( !FileFFMPEG::check_sig(this->asset) ) continue;
414 file = new FileFFMPEG(this->asset, this);
417 #ifdef HAVE_COMMERCIAL
418 if( !strcmp(pref->name,"DB") ) { // MediaDB
419 if( !FileDB::check_sig(this->asset) ) continue;
420 file = new FileDB(this->asset, this);
424 if( !strcmp(pref->name,"Scene") ) { // scene file
425 if( !FileScene::check_sig(this->asset, data)) continue;
426 file = new FileScene(this->asset, this);
430 if( !strcmp(pref->name,"DV") ) { // libdv
431 if( !FileDV::check_sig(this->asset) ) continue;
432 file = new FileDV(this->asset, this);
436 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
437 if( !FileSndFile::check_sig(this->asset) ) continue;
438 file = new FileSndFile(this->asset, this);
441 if( !strcmp(pref->name,"PNG") ) { // PNG file
442 if( !FilePNG::check_sig(this->asset) ) continue;
443 file = new FilePNG(this->asset, this);
446 if( !strcmp(pref->name,"PPM") ) { // PPM file
447 if( !FilePPM::check_sig(this->asset) ) continue;
448 file = new FilePPM(this->asset, this);
451 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
452 if( !FileJPEG::check_sig(this->asset) ) continue;
453 file = new FileJPEG(this->asset, this);
456 if( !strcmp(pref->name,"GIF") ) { // GIF file
457 if( FileGIFList::check_sig(this->asset) )
458 file = new FileGIFList(this->asset, this);
459 else if( FileGIF::check_sig(this->asset) )
460 file = new FileGIF(this->asset, this);
465 if( !strcmp(pref->name,"EXR") ) { // EXR file
466 if( !FileEXR::check_sig(this->asset, data)) continue;
467 file = new FileEXR(this->asset, this);
471 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
472 if( !FileFLAC::check_sig(this->asset, data)) continue;
473 file = new FileFLAC(this->asset, this);
476 if( !strcmp(pref->name,"CR2") ) { // CR2 file
477 if( !FileCR2::check_sig(this->asset)) continue;
478 file = new FileCR2(this->asset, this);
481 if( !strcmp(pref->name,"TGA") ) { // TGA file
482 if( !FileTGA::check_sig(this->asset) ) continue;
483 file = new FileTGA(this->asset, this);
486 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
487 if( !FileTIFF::check_sig(this->asset) ) continue;
488 file = new FileTIFF(this->asset, this);
492 if( !strcmp(pref->name,"OGG") ) { // OGG file
493 if( !FileOGG::check_sig(this->asset) ) continue;
494 file = new FileOGG(this->asset, this);
497 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
498 if( !FileVorbis::check_sig(this->asset) ) continue;
499 file = new FileVorbis(this->asset, this);
504 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
505 if( !FileMPEG::check_sig(this->asset) ) continue;
506 file = new FileMPEG(this->asset, this);
510 if( !strcmp(pref->name,"EDL") ) { // XML file
511 if( data[0] != '<' ) continue;
512 if( !strncmp(&data[1],"EDL>",4) ||
513 !strncmp(&data[1],"HTAL>",5) ||
514 !strncmp(&data[1],"?xml",4) )
518 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
521 int File::open_file(Preferences *preferences,
528 this->preferences = preferences;
529 this->asset->copy_from(asset, 1);
534 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
536 switch( this->asset->format ) {
537 // get the format now
538 // If you add another format to case 0, you also need to add another case for the
539 // file format #define.
542 if( ret != FILE_OK ) return ret;
544 // format already determined
545 #ifdef HAVE_CIN_3RDPARTY
547 file = new FileAC3(this->asset, this);
551 file = new FileScene(this->asset, this);
555 file = new FileFFMPEG(this->asset, this);
563 //printf("File::open_file 1\n");
564 file = new FileSndFile(this->asset, this);
569 file = new FilePNG(this->asset, this);
574 file = new FilePPM(this->asset, this);
579 file = new FileJPEG(this->asset, this);
583 file = new FileGIF(this->asset, this);
586 file = new FileGIFList(this->asset, this);
592 file = new FileEXR(this->asset, this);
596 file = new FileFLAC(this->asset, this);
601 file = new FileCR2(this->asset, this);
606 file = new FileTGA(this->asset, this);
611 file = new FileTIFF(this->asset, this);
613 #ifdef HAVE_COMMERCIAL
615 file = new FileDB(this->asset, this);
623 file = new FileMPEG(this->asset, this);
628 file = new FileOGG(this->asset, this);
632 file = new FileVorbis(this->asset, this);
637 file = new FileDV(this->asset, this);
641 file = new FileREF(this->asset, this);
650 // Reopen file with correct parser and get header.
651 if( file->open_file(rd, wr) ) {
652 delete file; file = 0;
653 return FILE_NOT_FOUND;
658 // Set extra writing parameters to mandatory settings.
660 if( this->asset->dither ) file->set_dither();
664 // one frame image file, not brender, no specific length
665 if( !this->asset->audio_data && this->asset->use_header &&
666 this->asset->video_data && !this->asset->single_frame &&
667 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
668 this->asset->single_frame = 1;
669 this->asset->video_length = -1;
673 // Synchronize header parameters
674 asset->copy_from(this->asset, 1);
677 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
683 void File::delete_temp_samples_buffer()
686 if( temp_samples_buffer ) {
687 for( int j = 0; j < audio_ring_buffers; j++ ) {
688 for( int i = 0; i < asset->channels; i++ ) {
689 delete temp_samples_buffer[j][i];
691 delete [] temp_samples_buffer[j];
694 delete [] temp_samples_buffer;
695 temp_samples_buffer = 0;
696 audio_ring_buffers = 0;
700 void File::delete_temp_frame_buffer()
703 if( temp_frame_buffer ) {
704 for( int k = 0; k < video_ring_buffers; k++ ) {
705 for( int i = 0; i < asset->layers; i++ ) {
706 for( int j = 0; j < video_buffer_size; j++ ) {
707 delete temp_frame_buffer[k][i][j];
709 delete [] temp_frame_buffer[k][i];
711 delete [] temp_frame_buffer[k];
714 delete [] temp_frame_buffer;
715 temp_frame_buffer = 0;
716 video_ring_buffers = 0;
717 video_buffer_size = 0;
721 int File::close_file(int ignore_thread)
723 if( !ignore_thread ) {
728 // The file's asset is a copy of the argument passed to open_file so the
729 // user must copy lengths from the file's asset.
731 asset->audio_length = current_sample;
732 asset->video_length = current_frame;
739 delete_temp_samples_buffer();
740 delete_temp_frame_buffer();
745 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
747 return !file ? -1 : file->get_index(index_file, progress_bar);
750 int File::start_audio_thread(int buffer_size, int ring_buffers)
752 this->audio_ring_buffers = ring_buffers;
755 if( !audio_thread ) {
756 audio_thread = new FileThread(this, 1, 0);
757 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
762 int File::start_video_thread(int buffer_size, int color_model,
763 int ring_buffers, int compressed)
765 this->video_ring_buffers = ring_buffers;
766 this->video_buffer_size = buffer_size;
768 if( !video_thread ) {
769 video_thread = new FileThread(this, 0, 1);
770 video_thread->start_writing(buffer_size, color_model,
771 ring_buffers, compressed);
776 int File::start_video_decode_thread()
778 // Currently, CR2 is the only one which won't work asynchronously, so
779 // we're not using a virtual function yet.
780 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
781 video_thread = new FileThread(this, 0, 1);
782 video_thread->start_reading();
788 int File::stop_audio_thread()
791 audio_thread->stop_writing();
798 int File::stop_video_thread()
801 video_thread->stop_reading();
802 video_thread->stop_writing();
809 FileThread* File::get_video_thread()
814 int File::set_channel(int channel)
816 if( file && channel < asset->channels ) {
817 current_channel = channel;
824 int File::get_channel()
826 return current_channel;
829 // if no>=0, sets new program
830 // returns current program
831 int File::set_program(int no)
833 int result = file ? file->set_program(no) : current_program;
834 current_program = no < 0 ? result : no;
838 int File::get_cell_time(int no, double &time)
840 return file ? file->get_cell_time(no, time) : -1;
843 int File::get_system_time(int64_t &tm)
845 return file ? file->get_system_time(tm) : -1;
848 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
850 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
853 int File::get_video_pid(int track)
855 return file ? file->get_video_pid(track) : -1;
860 int File::get_video_info(int track, int &pid, double &framerate,
861 int &width, int &height, char *title)
864 file->get_video_info(track, pid, framerate, width, height, title);
867 int File::select_video_stream(Asset *asset, int vstream)
870 file->select_video_stream(asset, vstream);
873 int File::select_audio_stream(Asset *asset, int astream)
876 file->select_audio_stream(asset, astream);
880 int File::get_thumbnail(int stream,
881 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
883 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
886 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
888 return file->set_skimming(track, skim, fn, vp);
891 int File::skim_video(int track, void *vp, skim_fn fn)
893 return file->skim_video(track, vp, fn);
897 int File::set_layer(int layer, int is_thread)
899 if( file && layer < asset->layers ) {
900 if( !is_thread && video_thread ) {
901 video_thread->set_layer(layer);
904 current_layer = layer;
912 int64_t File::get_audio_length()
914 int64_t result = asset->audio_length;
915 int64_t base_samplerate = -1;
917 if( base_samplerate > 0 )
918 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
926 int64_t File::get_video_length()
928 int64_t result = asset->video_length;
929 float base_framerate = -1;
931 if( base_framerate > 0 )
932 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
937 return -1; // infinity
941 int64_t File::get_video_position()
943 float base_framerate = -1;
944 if( base_framerate > 0 )
945 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
947 return current_frame;
950 int64_t File::get_audio_position()
952 // int64_t base_samplerate = -1;
953 // if( base_samplerate > 0 )
955 // if( normalized_sample_rate == base_samplerate )
956 // return normalized_sample;
958 // return (int64_t)((double)current_sample /
959 // asset->sample_rate *
964 return current_sample;
969 int File::set_audio_position(int64_t position)
973 if( !file ) return 1;
975 #define REPOSITION(x, y) \
976 (labs((x) - (y)) > 1)
978 float base_samplerate = asset->sample_rate;
979 current_sample = normalized_sample = position;
981 // printf("File::set_audio_position %d normalized_sample=%ld\n",
983 // normalized_sample);
984 result = file->set_audio_position(current_sample);
987 printf("File::set_audio_position position=%jd"
988 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
989 position, base_samplerate, asset, asset->sample_rate);
991 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
996 int File::set_video_position(int64_t position,
1000 if( !file ) return 0;
1002 // Convert to file's rate
1003 // if( base_framerate > 0 )
1004 // position = (int64_t)((double)position /
1006 // asset->frame_rate +
1010 if( video_thread && !is_thread ) {
1011 // Call thread. Thread calls this again to set the file state.
1012 video_thread->set_video_position(position);
1015 if( current_frame != position ) {
1017 current_frame = position;
1018 result = file->set_video_position(current_frame);
1025 // No resampling here.
1026 int File::write_samples(Samples **buffer, int64_t len)
1031 write_lock->lock("File::write_samples");
1033 // Convert to arrays for backwards compatability
1034 double *temp[asset->channels];
1035 for( int i = 0; i < asset->channels; i++ ) {
1036 temp[i] = buffer[i]->get_data();
1039 result = file->write_samples(temp, len);
1040 current_sample += len;
1041 normalized_sample += len;
1042 asset->audio_length += len;
1043 write_lock->unlock();
1052 // Can't put any cmodel abstraction here because the filebase couldn't be
1054 int File::write_frames(VFrame ***frames, int len)
1056 //printf("File::write_frames %d\n", __LINE__);
1058 // Store the counters in temps so the filebase can choose to overwrite them.
1060 int current_frame_temp = current_frame;
1061 int video_length_temp = asset->video_length;
1063 write_lock->lock("File::write_frames");
1066 result = file->write_frames(frames, len);
1069 current_frame = current_frame_temp + len;
1070 asset->video_length = video_length_temp + len;
1071 write_lock->unlock();
1076 // Only called by FileThread
1077 int File::write_compressed_frame(VFrame *buffer)
1080 write_lock->lock("File::write_compressed_frame");
1081 result = file->write_compressed_frame(buffer);
1083 asset->video_length++;
1084 write_lock->unlock();
1089 int File::write_audio_buffer(int64_t len)
1092 if( audio_thread ) {
1093 result = audio_thread->write_buffer(len);
1098 int File::write_video_buffer(int64_t len)
1101 if( video_thread ) {
1102 result = video_thread->write_buffer(len);
1108 Samples** File::get_audio_buffer()
1110 if( audio_thread ) return audio_thread->get_audio_buffer();
1114 VFrame*** File::get_video_buffer()
1116 if( video_thread ) {
1117 VFrame*** result = video_thread->get_video_buffer();
1126 int File::read_samples(Samples *samples, int64_t len)
1128 // Never try to read more samples than exist in the file
1129 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1130 len = asset->audio_length - current_sample;
1132 if( len <= 0 ) return 0;
1135 const int debug = 0;
1136 if( debug ) PRINT_TRACE
1138 if( debug ) PRINT_TRACE
1140 double *buffer = samples->get_data();
1142 int64_t base_samplerate = asset->sample_rate;
1145 // Resample recursively calls this with the asset sample rate
1146 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1148 if( debug ) PRINT_TRACE
1149 result = file->read_samples(buffer, len);
1151 if( debug ) PRINT_TRACE
1152 current_sample += len;
1154 normalized_sample += len;
1156 if( debug ) PRINT_TRACE
1161 int File::read_frame(VFrame *frame, int is_thread)
1163 const int debug = 0;
1164 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1165 // current_frame, use_cache, asset->single_frame);
1166 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1167 if( !file ) return 1;
1168 if( debug ) PRINT_TRACE
1170 int supported_colormodel = colormodel_supported(frame->get_color_model());
1172 // if reverse playback reading, use_cache is -1
1173 int cache_active = asset->single_frame ? 1 : use_cache;
1174 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1177 if( cache_active ) {
1178 if( get_cache_frame(frame, cache_position) ) {
1181 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1182 // reverse reading and cache miss, clear cache for new readahead
1188 VFrame *vframe = frame;
1189 if( frame->get_color_model() != BC_COMPRESSED &&
1190 (supported_colormodel != frame->get_color_model() ||
1191 (!file->can_scale_input() &&
1192 (frame->get_w() != asset->width ||
1193 frame->get_h() != asset->height))) ) {
1195 if( !temp_frame->params_match(asset->width, asset->height,
1196 supported_colormodel) ) {
1197 delete temp_frame; temp_frame = 0;
1201 temp_frame = new VFrame(asset->width, asset->height,
1202 supported_colormodel, 0);
1203 temp_frame->clear_frame();
1206 temp_frame->copy_stacks(frame);
1207 vframe = temp_frame;
1209 result = file->read_frame(vframe);
1211 if( frame != vframe )
1212 frame->transfer_from(vframe);
1213 if( cache_active > 0 )
1214 put_cache_frame(frame, cache_position, 1);
1216 else if( frame->get_status() > 0 )
1217 frame->set_status(-1);
1218 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1221 if( result && !current_frame )
1222 frame->clear_frame();
1224 //printf("File::read_frame %d\n", __LINE__);
1226 if( do_read ) current_frame++;
1227 if( debug ) PRINT_TRACE
1231 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1233 if( asset && file ) {
1234 return asset->width == output_w &&
1235 asset->height == output_h &&
1236 file->can_copy_from(asset, position);
1241 // Fill in queries about formats when adding formats here.
1244 int File::strtoformat(const char *format)
1246 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1247 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1248 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1249 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1250 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1251 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1252 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1253 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1254 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1255 if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1256 if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1257 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1258 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1259 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1260 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1261 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1262 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1263 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1264 if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1265 if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1266 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1267 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1268 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1269 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1270 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1271 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1272 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1273 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1274 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1275 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1276 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1277 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1278 if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1284 const char* File::formattostr(int format)
1287 case FILE_SCENE: return _(SCENE_NAME);
1288 case FILE_AC3: return _(AC3_NAME);
1289 case FILE_WAV: return _(WAV_NAME);
1290 case FILE_PCM: return _(PCM_NAME);
1291 case FILE_AU: return _(AU_NAME);
1292 case FILE_AIFF: return _(AIFF_NAME);
1293 case FILE_SND: return _(SND_NAME);
1294 case FILE_PNG: return _(PNG_NAME);
1295 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1296 case FILE_PPM: return _(PPM_NAME);
1297 case FILE_PPM_LIST: return _(PPM_LIST_NAME);
1298 case FILE_JPEG: return _(JPEG_NAME);
1299 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1300 case FILE_CR2: return _(CR2_NAME);
1301 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1302 case FILE_FLAC: return _(FLAC_NAME);
1303 case FILE_GIF: return _(GIF_NAME);
1304 case FILE_GIF_LIST: return _(GIF_LIST_NAME);
1305 case FILE_EXR: return _(EXR_NAME);
1306 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1307 #ifdef HAVE_LIBZMPEG
1308 case FILE_MPEG: return _(MPEG_NAME);
1310 case FILE_AMPEG: return _(AMPEG_NAME);
1311 case FILE_VMPEG: return _(VMPEG_NAME);
1312 case FILE_TGA: return _(TGA_NAME);
1313 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1314 case FILE_TIFF: return _(TIFF_NAME);
1315 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1316 case FILE_OGG: return _(OGG_NAME);
1317 case FILE_VORBIS: return _(VORBIS_NAME);
1318 case FILE_RAWDV: return _(RAWDV_NAME);
1319 case FILE_FFMPEG: return _(FFMPEG_NAME);
1320 case FILE_DB: return _(DBASE_NAME);
1321 case FILE_REF: return _(REF_NAME);
1323 return _("Unknown");
1326 int File::strtobits(const char *bits)
1328 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1329 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1330 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1331 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1332 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1333 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1334 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1335 return BITSLINEAR16;
1338 const char* File::bitstostr(int bits)
1340 //printf("File::bitstostr\n");
1342 case BITSLINEAR8: return (NAME_8BIT);
1343 case BITSLINEAR16: return (NAME_16BIT);
1344 case BITSLINEAR24: return (NAME_24BIT);
1345 case BITSLINEAR32: return (NAME_32BIT);
1346 case BITSULAW: return (NAME_ULAW);
1347 case BITS_ADPCM: return (NAME_ADPCM);
1348 case BITSFLOAT: return (NAME_FLOAT);
1350 return _("Unknown");
1355 int File::str_to_byteorder(const char *string)
1357 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1361 const char* File::byteorder_to_str(int byte_order)
1363 if( byte_order ) return _("Lo Hi");
1367 int File::bytes_per_sample(int bits)
1370 case BITSLINEAR8: return 1;
1371 case BITSLINEAR16: return 2;
1372 case BITSLINEAR24: return 3;
1373 case BITSLINEAR32: return 4;
1374 case BITSULAW: return 1;
1380 int File::get_best_colormodel(int driver, int vstream)
1382 return file ? file->get_best_colormodel(driver, vstream) :
1383 get_best_colormodel(asset, driver);
1386 int File::get_best_colormodel(Asset *asset, int driver)
1388 switch( asset->format ) {
1390 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1392 #ifdef HAVE_LIBZMPEG
1393 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1396 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1399 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1402 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1404 case FILE_PPM_LIST: return FilePPM::get_best_colormodel(asset, driver);
1406 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1408 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1409 #ifdef HAVE_COMMERCIAL
1410 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1412 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1413 case FILE_REF: return FileREF::get_best_colormodel(asset, driver);
1420 int File::colormodel_supported(int colormodel)
1423 return file->colormodel_supported(colormodel);
1429 int64_t File::file_memory_usage()
1431 return file ? file->base_memory_usage() : 0;
1434 int64_t File::get_memory_usage()
1438 result += file_memory_usage();
1439 if( temp_frame ) result += temp_frame->get_data_size();
1440 result += frame_cache->get_memory_usage();
1441 if( video_thread ) result += video_thread->get_memory_usage();
1443 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1448 int File::renders_video(int format)
1453 case FILE_JPEG_LIST:
1467 case FILE_TIFF_LIST:
1475 int File::renders_video(Asset *asset)
1477 return asset->format == FILE_FFMPEG ?
1478 FFMPEG::renders_video(asset->fformat) :
1479 renders_video(asset->format);
1482 int File::renders_audio(int format)
1501 int File::renders_audio(Asset *asset)
1503 return asset->format == FILE_FFMPEG ?
1504 FFMPEG::renders_audio(asset->fformat) :
1505 renders_audio(asset->format);
1508 int File::is_image_render(int format)
1523 const char* File::get_tag(int format)
1526 case FILE_AC3: return "ac3";
1527 case FILE_AIFF: return "aif";
1528 case FILE_AMPEG: return "mp3";
1529 case FILE_AU: return "au";
1530 case FILE_RAWDV: return "dv";
1531 case FILE_DB: return "db";
1532 case FILE_EXR: return "exr";
1533 case FILE_EXR_LIST: return "exrs";
1534 case FILE_FLAC: return "flac";
1535 case FILE_JPEG: return "jpg";
1536 case FILE_JPEG_LIST: return "jpgs";
1537 case FILE_OGG: return "ogg";
1538 case FILE_GIF: return "gif";
1539 case FILE_GIF_LIST: return "gifs";
1540 case FILE_PCM: return "pcm";
1541 case FILE_PNG: return "png";
1542 case FILE_PNG_LIST: return "pngs";
1543 case FILE_PPM: return "ppm";
1544 case FILE_PPM_LIST: return "ppms";
1545 case FILE_TGA: return "tga";
1546 case FILE_TGA_LIST: return "tgas";
1547 case FILE_TIFF: return "tif";
1548 case FILE_TIFF_LIST: return "tifs";
1549 case FILE_VMPEG: return "m2v";
1550 case FILE_VORBIS: return "ogg";
1551 case FILE_WAV: return "wav";
1552 case FILE_FFMPEG: return "ffmpg";
1553 case FILE_REF: return "ref";
1558 const char* File::get_prefix(int format)
1561 case FILE_PCM: return "PCM";
1562 case FILE_WAV: return "WAV";
1563 case FILE_PNG: return "PNG";
1564 case FILE_PPM: return "PPM";
1565 case FILE_JPEG: return "JPEG";
1566 case FILE_TIFF: return "TIFF";
1567 case FILE_GIF: return "GIF";
1568 case FILE_JPEG_LIST: return "JPEG_LIST";
1569 case FILE_AU: return "AU";
1570 case FILE_AIFF: return "AIFF";
1571 case FILE_SND: return "SND";
1572 case FILE_TGA_LIST: return "TGA_LIST";
1573 case FILE_TGA: return "TGA";
1574 case FILE_MPEG: return "MPEG";
1575 case FILE_AMPEG: return "AMPEG";
1576 case FILE_VMPEG: return "VMPEG";
1577 case FILE_RAWDV: return "RAWDV";
1578 case FILE_TIFF_LIST: return "TIFF_LIST";
1579 case FILE_PNG_LIST: return "PNG_LIST";
1580 case FILE_PPM_LIST: return "PPM_LIST";
1581 case FILE_AC3: return "AC3";
1582 case FILE_EXR: return "EXR";
1583 case FILE_EXR_LIST: return "EXR_LIST";
1584 case FILE_CR2: return "CR2";
1585 case FILE_OGG: return "OGG";
1586 case FILE_VORBIS: return "VORBIS";
1587 case FILE_FLAC: return "FLAC";
1588 case FILE_FFMPEG: return "FFMPEG";
1589 case FILE_SCENE: return "SCENE";
1590 case FILE_CR2_LIST: return "CR2_LIST";
1591 case FILE_GIF_LIST: return "GIF_LIST";
1592 case FILE_DB: return "DB";
1593 case FILE_REF: return "REF";
1595 return _("UNKNOWN");
1599 int File::record_fd()
1601 return file ? file->record_fd() : -1;
1605 void File::get_exe_path(char *result, char *bnp)
1607 // Get executable path, basename
1608 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1611 char *ptr = strrchr(result, '/');
1612 if( ptr ) *ptr++ = 0; else ptr = result;
1613 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1621 void File::getenv_path(char *result, const char *path)
1623 char *rp = result, *ep = rp + BCTEXTLEN-1;
1624 const char *cp = path;
1625 // any env var can be used here to expand a path as:
1626 // "path...$id...": and id is a word as alpha,alnum...
1627 // expands as "path...getenv(id)..."
1628 // CIN_PATH, CIN_LIB are set in main.C,
1629 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1630 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1631 const char *bp = cp; char *p = rp;
1632 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1634 const char *envp = getenv(rp); // read env value
1635 if( !envp ) continue;
1636 while( *envp && rp < ep ) *rp++ = *envp++;
1637 cp = bp; // subst id=value
1645 void File::setenv_path(const char *var, const char *path, int overwrite)
1647 char env_path[BCTEXTLEN];
1648 getenv_path(env_path, path);
1649 setenv(var, env_path, overwrite);
1652 * @brief Set various environment variables to pass constant values to
1653 * forks, shell scripts, and other parts of CinGG.
1655 void File::init_cin_env_vars()
1657 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1659 struct tm *dateTime;
1660 get_exe_path(env_path, env_pkg);
1661 setenv_path("CIN_PATH", env_path, 1);
1662 setenv_path("CIN_PKG", env_pkg, 1);
1663 setenv_path("CIN_DAT", CINDAT_DIR, 0);
1664 setenv_path("CIN_LIB", CINLIB_DIR, 0);
1665 setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1666 setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1667 setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1668 setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1669 setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1670 // Create env_var CINGG_BUILD for use when running as AppImage
1671 stat(env_path, &st);
1672 dateTime = gmtime(&st.st_mtime);
1673 strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1674 snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1675 build_name[sizeof(build_name) - 1] = 0;
1676 setenv_path("CINGG_BUILD", build_name, 1);