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
29 // work arounds (centos)
32 #define INT64_MAX 9223372036854775807LL
37 #include "bcsignals.h"
38 #include "byteorder.h"
40 #include "condition.h"
48 #include "fileffmpeg.h"
54 #undef HAVE_STDLIB_H // automake conflict
57 #include "filescene.h"
58 #include "filesndfile.h"
60 #include "filethread.h"
62 #include "filevorbis.h"
64 #include "formatwindow.h"
65 #include "formattools.h"
66 #include "framecache.h"
68 #include "mainprogress.inc"
71 #include "packagingengine.h"
72 #include "pluginserver.h"
73 #include "preferences.h"
74 #include "probeprefs.h"
78 //static int temp_debug = 0;
79 //suppress noref warning
80 void *vorbis0_ov_callbacks[] = {
81 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
82 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
89 format_completion = new Condition(1, "File::format_completion");
90 write_lock = new Condition(1, "File::write_lock");
91 frame_cache = new FrameCache;
97 if( getting_options ) {
98 if( format_window ) format_window->set_done(0);
99 format_completion->lock("File::~File");
100 format_completion->unlock();
104 //printf("File::~File %d temp_debug=%d\n", __LINE__, --temp_debug);
110 asset->Garbage::remove_user();
111 delete format_completion;
116 void File::reset_parameters()
129 normalized_sample = 0;
132 playback_subtitle = -1;
136 temp_samples_buffer = 0;
137 temp_frame_buffer = 0;
138 current_frame_buffer = 0;
139 audio_ring_buffers = 0;
140 video_ring_buffers = 0;
141 video_buffer_size = 0;
144 int File::raise_window()
146 if( getting_options && format_window ) {
147 format_window->raise_window();
148 format_window->flush();
153 void File::close_window()
155 if( getting_options ) {
156 format_window->lock_window("File::close_window");
157 format_window->set_done(1);
158 format_window->unlock_window();
163 int File::get_options(FormatTools *format,
164 int audio_options, int video_options)
166 BC_WindowBase *parent_window = format->window;
167 //ArrayList<PluginServer*> *plugindb = format->plugindb;
168 Asset *asset = format->asset;
171 format_completion->lock("File::get_options");
172 switch( asset->format ) {
174 FileAC3::get_parameters(parent_window,
182 FileDV::get_parameters(parent_window, asset, format_window,
183 audio_options, video_options);
191 FileSndFile::get_parameters(parent_window, asset, format_window,
192 audio_options, video_options);
195 FileFFMPEG::get_parameters(parent_window, asset, format_window,
196 audio_options, video_options);
200 FileMPEG::get_parameters(parent_window, asset, format_window,
201 audio_options, video_options);
205 FileJPEG::get_parameters(parent_window, asset, format_window,
206 audio_options, video_options);
211 FileEXR::get_parameters(parent_window, asset, format_window,
212 audio_options, video_options);
216 FileFLAC::get_parameters(parent_window, asset, format_window,
217 audio_options, video_options);
221 FilePNG::get_parameters(parent_window, asset, format_window,
222 audio_options, video_options);
226 FileTGA::get_parameters(parent_window, asset, format_window,
227 audio_options, video_options);
231 FileTIFF::get_parameters(parent_window, asset, format_window,
232 audio_options, video_options);
235 FileOGG::get_parameters(parent_window, asset, format_window,
236 audio_options, video_options);
242 if( !format_window ) {
243 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
244 parent_window->get_abs_cursor_x(1),
245 parent_window->get_abs_cursor_y(1));
246 format_window = errorbox;
249 errorbox->create_objects(_("This format doesn't support audio."));
252 errorbox->create_objects(_("This format doesn't support video."));
253 errorbox->run_window();
259 format_completion->unlock();
272 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
274 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
275 cpus = 8; // 8 cpus ought to decode just about anything
276 // Set all instances so gets work.
282 int File::set_preload(int64_t size)
284 this->playback_preload = size;
288 void File::set_subtitle(int value)
290 this->playback_subtitle = value;
291 if( file ) file->set_subtitle(value);
294 void File::set_interpolate_raw(int value)
296 this->interpolate_raw = value;
299 void File::set_white_balance_raw(int value)
301 this->white_balance_raw = value;
304 void File::set_cache_frames(int value)
306 // caching only done locally
311 int File::purge_cache()
313 // caching only done locally
315 if( frame_cache->cache_items() > 0 ) {
316 frame_cache->remove_all();
322 int File::delete_oldest()
324 // return the number of bytes freed
325 return frame_cache->delete_oldest();
328 // file driver in order of probe precidence
329 // can be reordered in preferences->interface
330 const char *File::default_probes[] = {
354 const int File::nb_probes =
355 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
360 FILE *fp = fopen(this->asset->path, "rb");
361 if( !fp ) return FILE_NOT_FOUND;
363 memset(data,0,sizeof(data));
364 int ret = fread(data, 1, 16, fp);
366 if( !ret ) return FILE_NOT_FOUND;
368 for( int i=0; i<preferences->file_probes.size(); ++i ) {
369 ProbePref *pref = preferences->file_probes[i];
370 if( !pref->armed ) continue;
371 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
372 if( !FileFFMPEG::check_sig(this->asset) ) continue;
373 file = new FileFFMPEG(this->asset, this);
376 if( !strcmp(pref->name,"DB") ) { // MediaDB
377 if( !FileDB::check_sig(this->asset) ) continue;
378 file = new FileDB(this->asset, this);
381 if( !strcmp(pref->name,"Scene") ) { // scene file
382 if( !FileScene::check_sig(this->asset, data)) continue;
383 file = new FileScene(this->asset, this);
387 if( !strcmp(pref->name,"DV") ) { // libdv
388 if( !FileDV::check_sig(this->asset) ) continue;
389 file = new FileDV(this->asset, this);
393 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
394 if( !FileSndFile::check_sig(this->asset) ) continue;
395 file = new FileSndFile(this->asset, this);
398 if( !strcmp(pref->name,"PNG") ) { // PNG file
399 if( !FilePNG::check_sig(this->asset) ) continue;
400 file = new FilePNG(this->asset, this);
403 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
404 if( !FileJPEG::check_sig(this->asset) ) continue;
405 file = new FileJPEG(this->asset, this);
408 if( !strcmp(pref->name,"GIF") ) { // GIF file
409 if( !FileGIF::check_sig(this->asset)) continue;
410 file = new FileGIF(this->asset, this);
414 if( !strcmp(pref->name,"EXR") ) { // EXR file
415 if( !FileEXR::check_sig(this->asset, data)) continue;
416 file = new FileEXR(this->asset, this);
420 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
421 if( !FileFLAC::check_sig(this->asset, data)) continue;
422 file = new FileFLAC(this->asset, this);
425 if( !strcmp(pref->name,"CR2") ) { // CR2 file
426 if( !FileCR2::check_sig(this->asset)) continue;
427 file = new FileCR2(this->asset, this);
430 if( !strcmp(pref->name,"TGA") ) { // TGA file
431 if( !FileTGA::check_sig(this->asset) ) continue;
432 file = new FileTGA(this->asset, this);
435 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
436 if( !FileTIFF::check_sig(this->asset) ) continue;
437 file = new FileTIFF(this->asset, this);
440 if( !strcmp(pref->name,"OGG") ) { // OGG file
441 if( !FileOGG::check_sig(this->asset) ) continue;
442 file = new FileOGG(this->asset, this);
445 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
446 if( !FileVorbis::check_sig(this->asset) ) continue;
447 file = new FileVorbis(this->asset, this);
450 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
451 if( !FileMPEG::check_sig(this->asset) ) continue;
452 file = new FileMPEG(this->asset, this);
455 if( !strcmp(pref->name,"EDL") ) { // XML file
456 if( data[0] != '<' ) continue;
457 if( !strncmp(&data[1],"EDL>",4) ||
458 !strncmp(&data[1],"HTAL>",5) ||
459 !strncmp(&data[1],"?xml",4) )
463 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
466 int File::open_file(Preferences *preferences,
473 this->preferences = preferences;
474 this->asset->copy_from(asset, 1);
479 if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
481 switch( this->asset->format ) {
482 // get the format now
483 // If you add another format to case 0, you also need to add another case for the
484 // file format #define.
487 if( ret != FILE_OK ) return ret;
489 // format already determined
491 file = new FileAC3(this->asset, this);
495 file = new FileScene(this->asset, this);
499 file = new FileFFMPEG(this->asset, this);
507 //printf("File::open_file 1\n");
508 file = new FileSndFile(this->asset, this);
513 file = new FilePNG(this->asset, this);
518 file = new FileJPEG(this->asset, this);
523 file = new FileGIF(this->asset, this);
528 file = new FileEXR(this->asset, this);
532 file = new FileFLAC(this->asset, this);
537 file = new FileCR2(this->asset, this);
542 file = new FileTGA(this->asset, this);
547 file = new FileTIFF(this->asset, this);
551 file = new FileDB(this->asset, this);
557 file = new FileMPEG(this->asset, this);
561 file = new FileOGG(this->asset, this);
565 file = new FileVorbis(this->asset, this);
569 file = new FileDV(this->asset, this);
579 // Reopen file with correct parser and get header.
580 if( file->open_file(rd, wr) ) {
581 delete file; file = 0;
582 return FILE_NOT_FOUND;
587 // Set extra writing parameters to mandatory settings.
589 if( this->asset->dither ) file->set_dither();
593 // one frame image file, not brender, no specific length
594 if( !this->asset->audio_data && this->asset->use_header &&
595 this->asset->video_data && !this->asset->single_frame &&
596 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
597 this->asset->single_frame = 1;
598 this->asset->video_length = -1;
602 // Synchronize header parameters
603 asset->copy_from(this->asset, 1);
606 if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
612 void File::delete_temp_samples_buffer()
615 if( temp_samples_buffer ) {
616 for( int j = 0; j < audio_ring_buffers; j++ ) {
617 for( int i = 0; i < asset->channels; i++ ) {
618 delete temp_samples_buffer[j][i];
620 delete [] temp_samples_buffer[j];
623 delete [] temp_samples_buffer;
624 temp_samples_buffer = 0;
625 audio_ring_buffers = 0;
629 void File::delete_temp_frame_buffer()
632 if( temp_frame_buffer ) {
633 for( int k = 0; k < video_ring_buffers; k++ ) {
634 for( int i = 0; i < asset->layers; i++ ) {
635 for( int j = 0; j < video_buffer_size; j++ ) {
636 delete temp_frame_buffer[k][i][j];
638 delete [] temp_frame_buffer[k][i];
640 delete [] temp_frame_buffer[k];
643 delete [] temp_frame_buffer;
644 temp_frame_buffer = 0;
645 video_ring_buffers = 0;
646 video_buffer_size = 0;
650 int File::close_file(int ignore_thread)
654 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
656 if( !ignore_thread ) {
662 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
664 // The file's asset is a copy of the argument passed to open_file so the
665 // user must copy lengths from the file's asset.
667 asset->audio_length = current_sample;
668 asset->video_length = current_frame;
674 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
676 delete_temp_samples_buffer();
677 delete_temp_frame_buffer();
678 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
680 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
683 if( debug ) printf("File::close_file file=%p %d\n", file, __LINE__);
689 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
691 return !file ? -1 : file->get_index(index_file, progress_bar);
696 int File::start_audio_thread(int buffer_size, int ring_buffers)
698 this->audio_ring_buffers = ring_buffers;
701 if( !audio_thread ) {
702 audio_thread = new FileThread(this, 1, 0);
703 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
708 int File::start_video_thread(int buffer_size,
713 this->video_ring_buffers = ring_buffers;
714 this->video_buffer_size = buffer_size;
716 if( !video_thread ) {
717 video_thread = new FileThread(this, 0, 1);
718 video_thread->start_writing(buffer_size,
726 int File::start_video_decode_thread()
728 // Currently, CR2 is the only one which won't work asynchronously, so
729 // we're not using a virtual function yet.
730 if( !video_thread /* && asset->format != FILE_CR2 */ ) {
731 video_thread = new FileThread(this, 0, 1);
732 video_thread->start_reading();
738 int File::stop_audio_thread()
741 audio_thread->stop_writing();
748 int File::stop_video_thread()
751 video_thread->stop_reading();
752 video_thread->stop_writing();
759 FileThread* File::get_video_thread()
764 int File::set_channel(int channel)
766 if( file && channel < asset->channels ) {
767 current_channel = channel;
774 int File::get_channel()
776 return current_channel;
779 // if no>=0, sets new program
780 // returns current program
781 int File::set_program(int no)
783 int result = file ? file->set_program(no) : current_program;
784 current_program = no < 0 ? result : no;
788 int File::get_cell_time(int no, double &time)
790 return file ? file->get_cell_time(no, time) : -1;
793 int File::get_system_time(int64_t &tm)
795 return file ? file->get_system_time(tm) : -1;
798 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
800 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
803 int File::get_video_pid(int track)
805 return file ? file->get_video_pid(track) : -1;
810 int File::get_video_info(int track, int &pid, double &framerate,
811 int &width, int &height, char *title)
814 file->get_video_info(track, pid, framerate, width, height, title);
817 int File::select_video_stream(Asset *asset, int vstream)
820 file->select_video_stream(asset, vstream);
823 int File::select_audio_stream(Asset *asset, int astream)
826 file->select_audio_stream(asset, astream);
830 int File::get_thumbnail(int stream,
831 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
833 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
836 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
838 return file->set_skimming(track, skim, fn, vp);
841 int File::skim_video(int track, void *vp, skim_fn fn)
843 return file->skim_video(track, vp, fn);
847 int File::set_layer(int layer, int is_thread)
849 if( file && layer < asset->layers ) {
850 if( !is_thread && video_thread ) {
851 video_thread->set_layer(layer);
854 current_layer = layer;
862 int64_t File::get_audio_length()
864 int64_t result = asset->audio_length;
865 int64_t base_samplerate = -1;
867 if( base_samplerate > 0 )
868 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
876 int64_t File::get_video_length()
878 int64_t result = asset->video_length;
879 float base_framerate = -1;
881 if( base_framerate > 0 )
882 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
887 return -1; // infinity
891 int64_t File::get_video_position()
893 float base_framerate = -1;
894 if( base_framerate > 0 )
895 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
897 return current_frame;
900 int64_t File::get_audio_position()
902 // int64_t base_samplerate = -1;
903 // if( base_samplerate > 0 )
905 // if( normalized_sample_rate == base_samplerate )
906 // return normalized_sample;
908 // return (int64_t)((double)current_sample /
909 // asset->sample_rate *
914 return current_sample;
919 int File::set_audio_position(int64_t position)
923 if( !file ) return 1;
925 #define REPOSITION(x, y) \
926 (labs((x) - (y)) > 1)
928 float base_samplerate = asset->sample_rate;
929 current_sample = normalized_sample = position;
931 // printf("File::set_audio_position %d normalized_sample=%ld\n",
933 // normalized_sample);
934 result = file->set_audio_position(current_sample);
937 printf("File::set_audio_position position=%jd"
938 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
939 position, base_samplerate, asset, asset->sample_rate);
941 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
946 int File::set_video_position(int64_t position,
950 if( !file ) return 0;
952 // Convert to file's rate
953 // if( base_framerate > 0 )
954 // position = (int64_t)((double)position /
956 // asset->frame_rate +
960 if( video_thread && !is_thread ) {
961 // Call thread. Thread calls this again to set the file state.
962 video_thread->set_video_position(position);
965 if( current_frame != position ) {
967 current_frame = position;
968 result = file->set_video_position(current_frame);
975 // No resampling here.
976 int File::write_samples(Samples **buffer, int64_t len)
981 write_lock->lock("File::write_samples");
983 // Convert to arrays for backwards compatability
984 double *temp[asset->channels];
985 for( int i = 0; i < asset->channels; i++ ) {
986 temp[i] = buffer[i]->get_data();
989 result = file->write_samples(temp, len);
990 current_sample += len;
991 normalized_sample += len;
992 asset->audio_length += len;
993 write_lock->unlock();
1002 // Can't put any cmodel abstraction here because the filebase couldn't be
1004 int File::write_frames(VFrame ***frames, int len)
1006 //printf("File::write_frames %d\n", __LINE__);
1008 // Store the counters in temps so the filebase can choose to overwrite them.
1010 int current_frame_temp = current_frame;
1011 int video_length_temp = asset->video_length;
1013 write_lock->lock("File::write_frames");
1016 result = file->write_frames(frames, len);
1019 current_frame = current_frame_temp + len;
1020 asset->video_length = video_length_temp + len;
1021 write_lock->unlock();
1026 // Only called by FileThread
1027 int File::write_compressed_frame(VFrame *buffer)
1030 write_lock->lock("File::write_compressed_frame");
1031 result = file->write_compressed_frame(buffer);
1033 asset->video_length++;
1034 write_lock->unlock();
1039 int File::write_audio_buffer(int64_t len)
1042 if( audio_thread ) {
1043 result = audio_thread->write_buffer(len);
1048 int File::write_video_buffer(int64_t len)
1051 if( video_thread ) {
1052 result = video_thread->write_buffer(len);
1058 Samples** File::get_audio_buffer()
1060 if( audio_thread ) return audio_thread->get_audio_buffer();
1064 VFrame*** File::get_video_buffer()
1066 if( video_thread ) {
1067 VFrame*** result = video_thread->get_video_buffer();
1076 int File::read_samples(Samples *samples, int64_t len)
1078 // Never try to read more samples than exist in the file
1079 if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1080 len = asset->audio_length - current_sample;
1082 if( len <= 0 ) return 0;
1085 const int debug = 0;
1086 if( debug ) PRINT_TRACE
1088 if( debug ) PRINT_TRACE
1090 double *buffer = samples->get_data();
1092 int64_t base_samplerate = asset->sample_rate;
1095 // Resample recursively calls this with the asset sample rate
1096 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1098 if( debug ) PRINT_TRACE
1099 result = file->read_samples(buffer, len);
1101 if( debug ) PRINT_TRACE
1102 current_sample += len;
1104 normalized_sample += len;
1106 if( debug ) PRINT_TRACE
1111 int File::read_frame(VFrame *frame, int is_thread)
1113 const int debug = 0;
1114 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1115 // current_frame, use_cache, asset->single_frame);
1116 if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1117 if( !file ) return 1;
1118 if( debug ) PRINT_TRACE
1120 int supported_colormodel = colormodel_supported(frame->get_color_model());
1121 int advance_position = 1;
1122 int cache_active = use_cache || asset->single_frame ? 1 : 0;
1123 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1126 if( cache_active && frame_cache->get_frame(frame, cache_position,
1127 current_layer, asset->frame_rate) ) {
1128 // Can't advance position if cache used.
1129 //printf("File::read_frame %d\n", __LINE__);
1130 advance_position = 0;
1133 else if( frame->get_color_model() != BC_COMPRESSED &&
1134 (supported_colormodel != frame->get_color_model() ||
1135 (!file->can_scale_input() &&
1136 (frame->get_w() != asset->width ||
1137 frame->get_h() != asset->height))) ) {
1139 // printf("File::read_frame %d\n", __LINE__);
1140 // Can't advance position here because it needs to be added to cache
1142 if( !temp_frame->params_match(asset->width, asset->height, supported_colormodel) ) {
1149 temp_frame = new VFrame(asset->width, asset->height, supported_colormodel, 0);
1152 // printf("File::read_frame %d\n", __LINE__);
1153 temp_frame->copy_stacks(frame);
1154 result = file->read_frame(temp_frame);
1156 frame->transfer_from(temp_frame);
1157 else if( result && frame->get_status() > 0 )
1158 frame->set_status(-1);
1159 //printf("File::read_frame %d\n", __LINE__);
1162 // Can't advance position here because it needs to be added to cache
1163 //printf("File::read_frame %d\n", __LINE__);
1164 result = file->read_frame(frame);
1165 if( result && frame->get_status() > 0 )
1166 frame->set_status(-1);
1167 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1170 if( result && !current_frame )
1171 frame->clear_frame();
1173 if( cache_active && advance_position && frame->get_status() > 0 )
1174 frame_cache->put_frame(frame, cache_position,
1175 current_layer, asset->frame_rate, 1, 0);
1176 //printf("File::read_frame %d\n", __LINE__);
1178 if( advance_position ) current_frame++;
1179 if( debug ) PRINT_TRACE
1183 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1185 if( asset && file ) {
1186 return asset->width == output_w &&
1187 asset->height == output_h &&
1188 file->can_copy_from(asset, position);
1193 // Fill in queries about formats when adding formats here.
1196 int File::strtoformat(const char *format)
1198 if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1199 if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1200 if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1201 if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1202 if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1203 if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1204 if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1205 if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1206 if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1207 if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1208 if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1209 if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1210 if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1211 if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1212 if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1213 if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1214 if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1215 if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1216 if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1217 if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1218 if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1219 if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1220 if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1221 if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1222 if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1223 if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1224 if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1225 if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1231 const char* File::formattostr(int format)
1234 case FILE_SCENE: return _(SCENE_NAME);
1235 case FILE_AC3: return _(AC3_NAME);
1236 case FILE_WAV: return _(WAV_NAME);
1237 case FILE_PCM: return _(PCM_NAME);
1238 case FILE_AU: return _(AU_NAME);
1239 case FILE_AIFF: return _(AIFF_NAME);
1240 case FILE_SND: return _(SND_NAME);
1241 case FILE_PNG: return _(PNG_NAME);
1242 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1243 case FILE_JPEG: return _(JPEG_NAME);
1244 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1245 case FILE_CR2: return _(CR2_NAME);
1246 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1247 case FILE_FLAC: return _(FLAC_NAME);
1248 case FILE_EXR: return _(EXR_NAME);
1249 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1250 case FILE_MPEG: return _(MPEG_NAME);
1251 case FILE_AMPEG: return _(AMPEG_NAME);
1252 case FILE_VMPEG: return _(VMPEG_NAME);
1253 case FILE_TGA: return _(TGA_NAME);
1254 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1255 case FILE_TIFF: return _(TIFF_NAME);
1256 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1257 case FILE_OGG: return _(OGG_NAME);
1258 case FILE_VORBIS: return _(VORBIS_NAME);
1259 case FILE_RAWDV: return _(RAWDV_NAME);
1260 case FILE_FFMPEG: return _(FFMPEG_NAME);
1261 case FILE_DB: return _(DBASE_NAME);
1263 return _("Unknown");
1266 int File::strtobits(const char *bits)
1268 if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1269 if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1270 if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1271 if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1272 if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1273 if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1274 if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1275 return BITSLINEAR16;
1278 const char* File::bitstostr(int bits)
1280 //printf("File::bitstostr\n");
1282 case BITSLINEAR8: return (NAME_8BIT);
1283 case BITSLINEAR16: return (NAME_16BIT);
1284 case BITSLINEAR24: return (NAME_24BIT);
1285 case BITSLINEAR32: return (NAME_32BIT);
1286 case BITSULAW: return (NAME_ULAW);
1287 case BITS_ADPCM: return (NAME_ADPCM);
1288 case BITSFLOAT: return (NAME_FLOAT);
1290 return _("Unknown");
1295 int File::str_to_byteorder(const char *string)
1297 if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1301 const char* File::byteorder_to_str(int byte_order)
1303 if( byte_order ) return _("Lo Hi");
1307 int File::bytes_per_sample(int bits)
1310 case BITSLINEAR8: return 1;
1311 case BITSLINEAR16: return 2;
1312 case BITSLINEAR24: return 3;
1313 case BITSLINEAR32: return 4;
1314 case BITSULAW: return 1;
1320 int File::get_best_colormodel(int driver, int vstream)
1322 return file ? file->get_best_colormodel(driver, vstream) :
1323 get_best_colormodel(asset, driver);
1326 int File::get_best_colormodel(Asset *asset, int driver)
1328 switch( asset->format ) {
1330 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1332 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1334 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1337 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1340 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1342 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1344 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1345 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1346 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1353 int File::colormodel_supported(int colormodel)
1356 return file->colormodel_supported(colormodel);
1362 int64_t File::file_memory_usage()
1364 return file ? file->base_memory_usage() : 0;
1367 int64_t File::get_memory_usage()
1371 result += file_memory_usage();
1372 if( temp_frame ) result += temp_frame->get_data_size();
1373 result += frame_cache->get_memory_usage();
1374 if( video_thread ) result += video_thread->get_memory_usage();
1376 if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1381 int File::renders_video(int format)
1386 case FILE_JPEG_LIST:
1396 case FILE_TIFF_LIST:
1404 int File::renders_video(Asset *asset)
1406 return asset->format == FILE_FFMPEG ?
1407 FileFFMPEG::renders_video(asset->fformat) :
1408 renders_video(asset->format);
1411 int File::renders_audio(int format)
1430 int File::renders_audio(Asset *asset)
1432 return asset->format == FILE_FFMPEG ?
1433 FileFFMPEG::renders_audio(asset->fformat) :
1434 renders_audio(asset->format);
1437 int File::is_image_render(int format)
1451 const char* File::get_tag(int format)
1454 case FILE_AC3: return "ac3";
1455 case FILE_AIFF: return "aif";
1456 case FILE_AMPEG: return "mp3";
1457 case FILE_AU: return "au";
1458 case FILE_RAWDV: return "dv";
1459 case FILE_DB: return "db";
1460 case FILE_EXR: return "exr";
1461 case FILE_EXR_LIST: return "exr";
1462 case FILE_FLAC: return "flac";
1463 case FILE_JPEG: return "jpg";
1464 case FILE_JPEG_LIST: return "jpg";
1465 case FILE_OGG: return "ogg";
1466 case FILE_PCM: return "pcm";
1467 case FILE_PNG: return "png";
1468 case FILE_PNG_LIST: return "png";
1469 case FILE_TGA: return "tga";
1470 case FILE_TGA_LIST: return "tga";
1471 case FILE_TIFF: return "tif";
1472 case FILE_TIFF_LIST: return "tif";
1473 case FILE_VMPEG: return "m2v";
1474 case FILE_VORBIS: return "ogg";
1475 case FILE_WAV: return "wav";
1476 case FILE_FFMPEG: return "ffmpg";
1481 const char* File::get_prefix(int format)
1484 case FILE_PCM: return "PCM";
1485 case FILE_WAV: return "WAV";
1486 case FILE_PNG: return "PNG";
1487 case FILE_JPEG: return "JPEG";
1488 case FILE_TIFF: return "TIFF";
1489 case FILE_GIF: return "GIF";
1490 case FILE_JPEG_LIST: return "JPEG_LIST";
1491 case FILE_AU: return "AU";
1492 case FILE_AIFF: return "AIFF";
1493 case FILE_SND: return "SND";
1494 case FILE_TGA_LIST: return "TGA_LIST";
1495 case FILE_TGA: return "TGA";
1496 case FILE_MPEG: return "MPEG";
1497 case FILE_AMPEG: return "AMPEG";
1498 case FILE_VMPEG: return "VMPEG";
1499 case FILE_RAWDV: return "RAWDV";
1500 case FILE_TIFF_LIST: return "TIFF_LIST";
1501 case FILE_PNG_LIST: return "PNG_LIST";
1502 case FILE_AC3: return "AC3";
1503 case FILE_EXR: return "EXR";
1504 case FILE_EXR_LIST: return "EXR_LIST";
1505 case FILE_CR2: return "CR2";
1506 case FILE_OGG: return "OGG";
1507 case FILE_VORBIS: return "VORBIS";
1508 case FILE_FLAC: return "FLAC";
1509 case FILE_FFMPEG: return "FFMPEG";
1510 case FILE_SCENE: return "SCENE";
1511 case FILE_CR2_LIST: return "CR2_LIST";
1512 case FILE_GIF_LIST: return "GIF_LIST";
1513 case FILE_DB: return "DB";
1515 return _("UNKNOWN");
1519 PackagingEngine *File::new_packaging_engine(Asset *asset)
1521 PackagingEngine *result;
1522 switch( asset->format ) {
1524 result = (PackagingEngine*)new PackagingEngineOGG();
1527 result = (PackagingEngine*) new PackagingEngineDefault();
1535 int File::record_fd()
1537 return file ? file->record_fd() : -1;
1541 void File::get_exe_path(char *result, char *bnp)
1543 // Get executable path, basename
1544 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1547 char *ptr = strrchr(result, '/');
1548 if( ptr ) *ptr++ = 0; else ptr = result;
1549 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1557 void File::getenv_path(char *result, const char *path)
1559 char *rp = result, *ep = rp + BCTEXTLEN-1;
1560 const char *cp = path;
1561 // any env var can be used here to expand a path as:
1562 // "path...$id...": and id is a word as alpha,alnum...
1563 // expands as "path...getenv(id)..."
1564 // CIN_PATH, CIN_LIB are set in main.C,
1565 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1566 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1567 const char *bp = cp; char *p = rp;
1568 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1570 const char *envp = getenv(rp); // read env value
1571 if( !envp ) continue;
1572 while( *envp && rp < ep ) *rp++ = *envp++;
1573 cp = bp; // subst id=value
1581 void File::setenv_path(char *result, const char *var, const char *path)
1583 char *env = getenv(var);
1585 char env_path[BCTEXTLEN];
1586 getenv_path(env_path, path);
1587 sprintf(result, "%s=%s", var, env_path);
1591 char File::cinexe_path[BCTEXTLEN];
1592 char File::cinpkg_path[BCTEXTLEN];
1593 char File::cindat_path[BCTEXTLEN];
1594 char File::cinlib_path[BCTEXTLEN];
1595 char File::cincfg_path[BCTEXTLEN];
1596 char File::cinplg_path[BCTEXTLEN];
1597 char File::cinlad_path[BCTEXTLEN];
1598 char File::cinlcl_path[BCTEXTLEN];
1599 char File::cinbwr_path[BCTEXTLEN];
1601 void File::init_cin_path()
1603 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1604 // these values are advertised for forks/shell scripts
1605 get_exe_path(env_path, env_pkg);
1606 snprintf(cinexe_path, sizeof(cinexe_path), "CIN_PATH=%s", env_path);
1607 putenv(cinexe_path);
1608 snprintf(cinpkg_path, sizeof(cinpkg_path), "CIN_PKG=%s", env_pkg);
1609 putenv(cinpkg_path);
1611 setenv_path(cindat_path, "CIN_DAT", CINDAT_DIR);
1612 setenv_path(cinlib_path, "CIN_LIB", CINLIB_DIR);
1613 setenv_path(cincfg_path, "CIN_CONFIG", CONFIG_DIR);
1614 setenv_path(cinplg_path, "CIN_PLUGIN", PLUGIN_DIR);
1615 setenv_path(cinlad_path, "CIN_LADSPA", LADSPA_DIR);
1616 setenv_path(cinlcl_path, "CIN_LOCALE", LOCALE_DIR);
1617 setenv_path(cinbwr_path, "CIN_BROWSER", CIN_BROWSER);