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 //suppress noref warning
79 void *vorbis0_ov_callbacks[] = {
80 &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
81 &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
88 format_completion = new Condition(1, "File::format_completion");
89 write_lock = new Condition(1, "File::write_lock");
90 frame_cache = new FrameCache;
98 if(format_window) format_window->set_done(0);
99 format_completion->lock("File::~File");
100 format_completion->unlock();
103 if(temp_frame) delete temp_frame;
108 asset->Garbage::remove_user();
109 delete format_completion;
114 void File::reset_parameters()
127 normalized_sample = 0;
130 playback_subtitle = -1;
134 temp_samples_buffer = 0;
135 temp_frame_buffer = 0;
136 current_frame_buffer = 0;
137 audio_ring_buffers = 0;
138 video_ring_buffers = 0;
139 video_buffer_size = 0;
142 int File::raise_window()
144 if(getting_options && format_window)
146 format_window->raise_window();
147 format_window->flush();
152 void File::close_window()
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)
175 FileAC3::get_parameters(parent_window,
183 FileDV::get_parameters(parent_window,
195 FileSndFile::get_parameters(parent_window,
202 FileFFMPEG::get_parameters(parent_window,
210 FileMPEG::get_parameters(parent_window,
218 FileJPEG::get_parameters(parent_window,
227 FileEXR::get_parameters(parent_window,
235 FileFLAC::get_parameters(parent_window,
243 FilePNG::get_parameters(parent_window,
251 FileTGA::get_parameters(parent_window,
259 FileTIFF::get_parameters(parent_window,
266 FileOGG::get_parameters(parent_window,
278 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
279 parent_window->get_abs_cursor_x(1),
280 parent_window->get_abs_cursor_y(1));
281 format_window = errorbox;
284 errorbox->create_objects(_("This format doesn't support audio."));
287 errorbox->create_objects(_("This format doesn't support video."));
288 errorbox->run_window();
294 format_completion->unlock();
307 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
309 if( cpus > 8 ) // mpegvideo max_threads = 16, more causes errs
310 cpus = 8; // 8 cpus ought to decode just about anything
311 // Set all instances so gets work.
317 int File::set_preload(int64_t size)
319 this->playback_preload = size;
323 void File::set_subtitle(int value)
325 this->playback_subtitle = value;
326 if( file ) file->set_subtitle(value);
329 void File::set_interpolate_raw(int value)
331 this->interpolate_raw = value;
334 void File::set_white_balance_raw(int value)
336 this->white_balance_raw = value;
340 void File::set_cache_frames(int value)
342 // caching only done locally
347 int File::purge_cache()
349 // caching only done locally
351 if( frame_cache->cache_items() > 0 )
353 frame_cache->remove_all();
359 int File::delete_oldest()
361 // caching only done locally
362 return frame_cache->delete_oldest();
365 // file driver in order of probe precidence
366 // can be reordered in preferences->interface
367 const char *File::default_probes[] = {
391 const int File::nb_probes =
392 sizeof(File::default_probes)/sizeof(File::default_probes[0]);
397 FILE *fp = fopen(this->asset->path, "rb");
398 if( !fp ) return FILE_NOT_FOUND;
400 memset(data,0,sizeof(data));
401 int ret = fread(data, 1, 16, fp);
403 if( !ret ) return FILE_NOT_FOUND;
405 for( int i=0; i<preferences->file_probes.size(); ++i ) {
406 ProbePref *pref = preferences->file_probes[i];
407 if( !pref->armed ) continue;
408 if( !strncmp(pref->name,"FFMPEG",6) ) { // FFMPEG Early/Late
409 if( !FileFFMPEG::check_sig(this->asset) ) continue;
410 file = new FileFFMPEG(this->asset, this);
413 if( !strcmp(pref->name,"DB") ) { // MediaDB
414 if( !FileDB::check_sig(this->asset) ) continue;
415 file = new FileDB(this->asset, this);
418 if( !strcmp(pref->name,"Scene") ) { // scene file
419 if( !FileScene::check_sig(this->asset, data)) continue;
420 file = new FileScene(this->asset, this);
424 if( !strcmp(pref->name,"DV") ) { // libdv
425 if( !FileDV::check_sig(this->asset) ) continue;
426 file = new FileDV(this->asset, this);
430 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
431 if( !FileSndFile::check_sig(this->asset) ) continue;
432 file = new FileSndFile(this->asset, this);
435 if( !strcmp(pref->name,"PNG") ) { // PNG file
436 if( !FilePNG::check_sig(this->asset) ) continue;
437 file = new FilePNG(this->asset, this);
440 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
441 if( !FileJPEG::check_sig(this->asset) ) continue;
442 file = new FileJPEG(this->asset, this);
445 if( !strcmp(pref->name,"GIF") ) { // GIF file
446 if( !FileGIF::check_sig(this->asset)) continue;
447 file = new FileGIF(this->asset, this);
451 if( !strcmp(pref->name,"EXR") ) { // EXR file
452 if( !FileEXR::check_sig(this->asset, data)) continue;
453 file = new FileEXR(this->asset, this);
457 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
458 if( !FileFLAC::check_sig(this->asset, data)) continue;
459 file = new FileFLAC(this->asset, this);
462 if( !strcmp(pref->name,"CR2") ) { // CR2 file
463 if( !FileCR2::check_sig(this->asset)) continue;
464 file = new FileCR2(this->asset, this);
467 if( !strcmp(pref->name,"TGA") ) { // TGA file
468 if( !FileTGA::check_sig(this->asset) ) continue;
469 file = new FileTGA(this->asset, this);
472 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
473 if( !FileTIFF::check_sig(this->asset) ) continue;
474 file = new FileTIFF(this->asset, this);
477 if( !strcmp(pref->name,"OGG") ) { // OGG file
478 if( !FileOGG::check_sig(this->asset) ) continue;
479 file = new FileOGG(this->asset, this);
482 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
483 if( !FileVorbis::check_sig(this->asset) ) continue;
484 file = new FileVorbis(this->asset, this);
487 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
488 if( !FileMPEG::check_sig(this->asset) ) continue;
489 file = new FileMPEG(this->asset, this);
492 if( !strcmp(pref->name,"EDL") ) { // XML file
493 if( data[0] != '<' ) continue;
494 if( !strncmp(&data[1],"EDL>",4) ||
495 !strncmp(&data[1],"HTAL>",5) ||
496 !strncmp(&data[1],"?xml",4) )
500 return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
503 int File::open_file(Preferences *preferences,
510 this->preferences = preferences;
511 this->asset->copy_from(asset, 1);
516 if(debug) printf("File::open_file %d\n", __LINE__);
518 if(debug) printf("File::open_file %p %d\n", this, __LINE__);
520 switch(this->asset->format) {
521 // get the format now
522 // If you add another format to case 0, you also need to add another case for the
523 // file format #define.
526 if( ret != FILE_OK ) return ret;
528 // format already determined
530 file = new FileAC3(this->asset, this);
534 file = new FileScene(this->asset, this);
538 file = new FileFFMPEG(this->asset, this);
546 //printf("File::open_file 1\n");
547 file = new FileSndFile(this->asset, this);
552 file = new FilePNG(this->asset, this);
557 file = new FileJPEG(this->asset, this);
562 file = new FileGIF(this->asset, this);
567 file = new FileEXR(this->asset, this);
571 file = new FileFLAC(this->asset, this);
576 file = new FileCR2(this->asset, this);
581 file = new FileTGA(this->asset, this);
586 file = new FileTIFF(this->asset, this);
590 file = new FileDB(this->asset, this);
596 file = new FileMPEG(this->asset, this);
600 file = new FileOGG(this->asset, this);
604 file = new FileVorbis(this->asset, this);
608 file = new FileDV(this->asset, this);
618 // Reopen file with correct parser and get header.
619 if(file->open_file(rd, wr)) {
620 delete file; file = 0;
621 return FILE_NOT_FOUND;
626 // Set extra writing parameters to mandatory settings.
628 if(this->asset->dither) file->set_dither();
632 // one frame image file, not brender, no specific length
633 if( !this->asset->audio_data && this->asset->use_header &&
634 this->asset->video_data && !this->asset->single_frame &&
635 this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
636 this->asset->single_frame = 1;
637 this->asset->video_length = -1;
641 // Synchronize header parameters
642 asset->copy_from(this->asset, 1);
645 if(debug) printf("File::open_file %d file=%p\n", __LINE__, file);
651 void File::delete_temp_samples_buffer()
654 if(temp_samples_buffer) {
655 for(int j = 0; j < audio_ring_buffers; j++) {
656 for(int i = 0; i < asset->channels; i++) {
657 delete temp_samples_buffer[j][i];
659 delete [] temp_samples_buffer[j];
662 delete [] temp_samples_buffer;
663 temp_samples_buffer = 0;
664 audio_ring_buffers = 0;
668 void File::delete_temp_frame_buffer()
671 if(temp_frame_buffer) {
672 for(int k = 0; k < video_ring_buffers; k++) {
673 for(int i = 0; i < asset->layers; i++) {
674 for(int j = 0; j < video_buffer_size; j++) {
675 delete temp_frame_buffer[k][i][j];
677 delete [] temp_frame_buffer[k][i];
679 delete [] temp_frame_buffer[k];
682 delete [] temp_frame_buffer;
683 temp_frame_buffer = 0;
684 video_ring_buffers = 0;
685 video_buffer_size = 0;
689 int File::close_file(int ignore_thread)
693 if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
701 if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
703 // The file's asset is a copy of the argument passed to open_file so the
704 // user must copy lengths from the file's asset.
706 asset->audio_length = current_sample;
707 asset->video_length = current_frame;
713 if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
715 delete_temp_samples_buffer();
716 delete_temp_frame_buffer();
717 if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
719 if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
722 if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
728 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
730 return !file ? -1 : file->get_index(index_file, progress_bar);
735 int File::start_audio_thread(int buffer_size, int ring_buffers)
737 this->audio_ring_buffers = ring_buffers;
742 audio_thread = new FileThread(this, 1, 0);
743 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
748 int File::start_video_thread(int buffer_size,
753 this->video_ring_buffers = ring_buffers;
754 this->video_buffer_size = buffer_size;
758 video_thread = new FileThread(this, 0, 1);
759 video_thread->start_writing(buffer_size,
767 int File::start_video_decode_thread()
769 // Currently, CR2 is the only one which won't work asynchronously, so
770 // we're not using a virtual function yet.
771 if(!video_thread /* && asset->format != FILE_CR2 */)
773 video_thread = new FileThread(this, 0, 1);
774 video_thread->start_reading();
780 int File::stop_audio_thread()
784 audio_thread->stop_writing();
791 int File::stop_video_thread()
795 video_thread->stop_reading();
796 video_thread->stop_writing();
803 FileThread* File::get_video_thread()
808 int File::set_channel(int channel)
810 if(file && channel < asset->channels)
812 current_channel = channel;
819 int File::get_channel()
821 return current_channel;
824 // if no>=0, sets new program
825 // returns current program
826 int File::set_program(int no)
828 int result = file ? file->set_program(no) : current_program;
829 current_program = no < 0 ? result : no;
833 int File::get_cell_time(int no, double &time)
835 return file ? file->get_cell_time(no, time) : -1;
838 int File::get_system_time(int64_t &tm)
840 return file ? file->get_system_time(tm) : -1;
843 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
845 return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
848 int File::get_video_pid(int track)
850 return file ? file->get_video_pid(track) : -1;
855 int File::get_video_info(int track, int &pid, double &framerate,
856 int &width, int &height, char *title)
859 file->get_video_info(track, pid, framerate, width, height, title);
862 int File::select_video_stream(Asset *asset, int vstream)
865 file->select_video_stream(asset, vstream);
868 int File::select_audio_stream(Asset *asset, int astream)
871 file->select_audio_stream(asset, astream);
875 int File::get_thumbnail(int stream,
876 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
878 return file->get_thumbnail(stream, position, thumbnail, ww, hh);
881 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
883 return file->set_skimming(track, skim, fn, vp);
886 int File::skim_video(int track, void *vp, skim_fn fn)
888 return file->skim_video(track, vp, fn);
892 int File::set_layer(int layer, int is_thread)
894 if(file && layer < asset->layers)
896 if(!is_thread && video_thread)
898 video_thread->set_layer(layer);
902 current_layer = layer;
910 int64_t File::get_audio_length()
912 int64_t result = asset->audio_length;
913 int64_t base_samplerate = -1;
916 if(base_samplerate > 0)
917 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
925 int64_t File::get_video_length()
927 int64_t result = asset->video_length;
928 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)
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,
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)
1012 // Call thread. Thread calls this again to set the file state.
1013 video_thread->set_video_position(position);
1016 if(current_frame != position)
1020 current_frame = position;
1021 result = file->set_video_position(current_frame);
1028 // No resampling here.
1029 int File::write_samples(Samples **buffer, int64_t len)
1035 write_lock->lock("File::write_samples");
1037 // Convert to arrays for backwards compatability
1038 double *temp[asset->channels];
1039 for(int i = 0; i < asset->channels; i++)
1041 temp[i] = buffer[i]->get_data();
1044 result = file->write_samples(temp, len);
1045 current_sample += len;
1046 normalized_sample += len;
1047 asset->audio_length += len;
1048 write_lock->unlock();
1057 // Can't put any cmodel abstraction here because the filebase couldn't be
1059 int File::write_frames(VFrame ***frames, int len)
1061 //printf("File::write_frames %d\n", __LINE__);
1063 // Store the counters in temps so the filebase can choose to overwrite them.
1065 int current_frame_temp = current_frame;
1066 int video_length_temp = asset->video_length;
1068 write_lock->lock("File::write_frames");
1071 result = file->write_frames(frames, len);
1074 current_frame = current_frame_temp + len;
1075 asset->video_length = video_length_temp + len;
1076 write_lock->unlock();
1081 // Only called by FileThread
1082 int File::write_compressed_frame(VFrame *buffer)
1085 write_lock->lock("File::write_compressed_frame");
1086 result = file->write_compressed_frame(buffer);
1088 asset->video_length++;
1089 write_lock->unlock();
1094 int File::write_audio_buffer(int64_t len)
1099 result = audio_thread->write_buffer(len);
1104 int File::write_video_buffer(int64_t len)
1109 result = video_thread->write_buffer(len);
1115 Samples** File::get_audio_buffer()
1117 if(audio_thread) return audio_thread->get_audio_buffer();
1121 VFrame*** File::get_video_buffer()
1125 VFrame*** result = video_thread->get_video_buffer();
1134 int File::read_samples(Samples *samples, int64_t len)
1136 // Never try to read more samples than exist in the file
1137 if (asset->audio_length >= 0 && current_sample + len > asset->audio_length) {
1138 len = asset->audio_length - current_sample;
1140 if(len <= 0) return 0;
1143 const int debug = 0;
1144 if(debug) PRINT_TRACE
1146 if(debug) PRINT_TRACE
1148 double *buffer = samples->get_data();
1150 int64_t base_samplerate = asset->sample_rate;
1154 // Resample recursively calls this with the asset sample rate
1155 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
1157 if(debug) PRINT_TRACE
1158 result = file->read_samples(buffer, len);
1160 if(debug) PRINT_TRACE
1161 current_sample += len;
1163 normalized_sample += len;
1165 if(debug) PRINT_TRACE
1170 int File::read_frame(VFrame *frame, int is_thread)
1172 const int debug = 0;
1174 if(debug) PRINT_TRACE
1176 //printf("File::read_frame %d\n", __LINE__);
1178 if(video_thread && !is_thread) return video_thread->read_frame(frame);
1180 //printf("File::read_frame %d\n", __LINE__);
1181 if(debug) PRINT_TRACE
1182 if( !file ) return 1;
1183 if(debug) PRINT_TRACE
1185 int supported_colormodel = colormodel_supported(frame->get_color_model());
1186 int advance_position = 1;
1187 int cache_active = use_cache || asset->single_frame ? 1 : 0;
1188 int64_t cache_position = !asset->single_frame ? current_frame : -1;
1190 if( cache_active && frame_cache->get_frame(frame, cache_position,
1191 current_layer, asset->frame_rate) )
1193 // Can't advance position if cache used.
1194 //printf("File::read_frame %d\n", __LINE__);
1195 advance_position = 0;
1198 else if(frame->get_color_model() != BC_COMPRESSED &&
1199 (supported_colormodel != frame->get_color_model() ||
1200 frame->get_w() != asset->width ||
1201 frame->get_h() != asset->height))
1204 // printf("File::read_frame %d\n", __LINE__);
1205 // Can't advance position here because it needs to be added to cache
1208 if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel))
1215 // printf("File::read_frame %d\n", __LINE__);
1218 temp_frame = new VFrame(0,
1222 supported_colormodel,
1226 // printf("File::read_frame %d\n", __LINE__);
1227 temp_frame->copy_stacks(frame);
1228 result = file->read_frame(temp_frame);
1230 frame->transfer_from(temp_frame);
1231 else if( result && frame->get_status() > 0 )
1232 frame->set_status(-1);
1233 //printf("File::read_frame %d\n", __LINE__);
1237 // Can't advance position here because it needs to be added to cache
1238 //printf("File::read_frame %d\n", __LINE__);
1239 result = file->read_frame(frame);
1240 if( result && frame->get_status() > 0 )
1241 frame->set_status(-1);
1242 //for(int i = 0; i < 100 * 1000; i++) ((float*)frame->get_rows()[0])[i] = 1.0;
1245 if( result && !current_frame )
1246 frame->clear_frame();
1248 if( cache_active && advance_position && frame->get_status() > 0 )
1249 frame_cache->put_frame(frame, cache_position,
1250 current_layer, asset->frame_rate, 1, 0);
1251 //printf("File::read_frame %d\n", __LINE__);
1253 if(advance_position) current_frame++;
1254 if(debug) PRINT_TRACE
1258 int File::can_copy_from(Asset *asset,
1263 if( asset && file ) {
1264 return asset->width == output_w &&
1265 asset->height == output_h &&
1266 file->can_copy_from(asset, position);
1271 // Fill in queries about formats when adding formats here.
1274 int File::strtoformat(const char *format)
1276 return strtoformat(0, format);
1279 int File::strtoformat(ArrayList<PluginServer*> *plugindb, const char *format)
1281 if(!strcasecmp(format, _(AC3_NAME))) return FILE_AC3;
1282 if(!strcasecmp(format, _(SCENE_NAME))) return FILE_SCENE;
1283 if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
1284 if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
1285 if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
1286 if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
1287 if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
1288 if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
1289 if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
1290 if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
1291 if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
1292 if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
1293 if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
1294 if(!strcasecmp(format, _(EXR_NAME))) return FILE_EXR;
1295 if(!strcasecmp(format, _(EXR_LIST_NAME))) return FILE_EXR_LIST;
1296 if(!strcasecmp(format, _(FLAC_NAME))) return FILE_FLAC;
1297 if(!strcasecmp(format, _(CR2_NAME))) return FILE_CR2;
1298 if(!strcasecmp(format, _(CR2_LIST_NAME))) return FILE_CR2_LIST;
1299 if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
1300 if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
1301 if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
1302 if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
1303 if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
1304 if(!strcasecmp(format, _(OGG_NAME))) return FILE_OGG;
1305 if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
1306 if(!strcasecmp(format, _(RAWDV_NAME))) return FILE_RAWDV;
1307 if(!strcasecmp(format, _(FFMPEG_NAME))) return FILE_FFMPEG;
1308 if(!strcasecmp(format, _(DBASE_NAME))) return FILE_DB;
1314 const char* File::formattostr(int format)
1316 return formattostr(0, format);
1319 const char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
1322 case FILE_SCENE: return _(SCENE_NAME);
1323 case FILE_AC3: return _(AC3_NAME);
1324 case FILE_WAV: return _(WAV_NAME);
1325 case FILE_PCM: return _(PCM_NAME);
1326 case FILE_AU: return _(AU_NAME);
1327 case FILE_AIFF: return _(AIFF_NAME);
1328 case FILE_SND: return _(SND_NAME);
1329 case FILE_PNG: return _(PNG_NAME);
1330 case FILE_PNG_LIST: return _(PNG_LIST_NAME);
1331 case FILE_JPEG: return _(JPEG_NAME);
1332 case FILE_JPEG_LIST: return _(JPEG_LIST_NAME);
1333 case FILE_CR2: return _(CR2_NAME);
1334 case FILE_CR2_LIST: return _(CR2_LIST_NAME);
1335 case FILE_FLAC: return _(FLAC_NAME);
1336 case FILE_EXR: return _(EXR_NAME);
1337 case FILE_EXR_LIST: return _(EXR_LIST_NAME);
1338 case FILE_MPEG: return _(MPEG_NAME);
1339 case FILE_AMPEG: return _(AMPEG_NAME);
1340 case FILE_VMPEG: return _(VMPEG_NAME);
1341 case FILE_TGA: return _(TGA_NAME);
1342 case FILE_TGA_LIST: return _(TGA_LIST_NAME);
1343 case FILE_TIFF: return _(TIFF_NAME);
1344 case FILE_TIFF_LIST: return _(TIFF_LIST_NAME);
1345 case FILE_OGG: return _(OGG_NAME);
1346 case FILE_VORBIS: return _(VORBIS_NAME);
1347 case FILE_RAWDV: return _(RAWDV_NAME);
1348 case FILE_FFMPEG: return _(FFMPEG_NAME);
1349 case FILE_DB: return _(DBASE_NAME);
1351 return _("Unknown");
1354 int File::strtobits(const char *bits)
1356 if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
1357 if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
1358 if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
1359 if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
1360 if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
1361 if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
1362 if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
1363 return BITSLINEAR16;
1366 const char* File::bitstostr(int bits)
1368 //printf("File::bitstostr\n");
1370 case BITSLINEAR8: return (NAME_8BIT);
1371 case BITSLINEAR16: return (NAME_16BIT);
1372 case BITSLINEAR24: return (NAME_24BIT);
1373 case BITSLINEAR32: return (NAME_32BIT);
1374 case BITSULAW: return (NAME_ULAW);
1375 case BITS_ADPCM: return (NAME_ADPCM);
1376 case BITSFLOAT: return (NAME_FLOAT);
1378 return _("Unknown");
1383 int File::str_to_byteorder(const char *string)
1385 if(!strcasecmp(string, _("Lo Hi"))) return 1;
1389 const char* File::byteorder_to_str(int byte_order)
1391 if(byte_order) return _("Lo Hi");
1395 int File::bytes_per_sample(int bits)
1399 case BITSLINEAR8: return 1;
1400 case BITSLINEAR16: return 2;
1401 case BITSLINEAR24: return 3;
1402 case BITSLINEAR32: return 4;
1403 case BITSULAW: return 1;
1412 int File::get_best_colormodel(int driver)
1414 return get_best_colormodel(asset, driver);
1417 int File::get_best_colormodel(Asset *asset, int driver)
1419 switch(asset->format)
1422 case FILE_RAWDV: return FileDV::get_best_colormodel(asset, driver);
1424 case FILE_MPEG: return FileMPEG::get_best_colormodel(asset, driver);
1426 case FILE_JPEG_LIST: return FileJPEG::get_best_colormodel(asset, driver);
1429 case FILE_EXR_LIST: return FileEXR::get_best_colormodel(asset, driver);
1432 case FILE_PNG_LIST: return FilePNG::get_best_colormodel(asset, driver);
1434 case FILE_TGA_LIST: return FileTGA::get_best_colormodel(asset, driver);
1436 case FILE_CR2_LIST: return FileCR2::get_best_colormodel(asset, driver);
1437 case FILE_DB: return FileDB::get_best_colormodel(asset, driver);
1438 case FILE_FFMPEG: return FileFFMPEG::get_best_colormodel(asset, driver);
1445 int File::colormodel_supported(int colormodel)
1448 return file->colormodel_supported(colormodel);
1454 int64_t File::file_memory_usage()
1456 return file ? file->base_memory_usage() : 0;
1459 int64_t File::get_memory_usage()
1463 result += file_memory_usage();
1464 if(temp_frame) result += temp_frame->get_data_size();
1465 result += frame_cache->get_memory_usage();
1466 if(video_thread) result += video_thread->get_memory_usage();
1468 if(result < MIN_CACHEITEM_SIZE) result = MIN_CACHEITEM_SIZE;
1473 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
1475 int format_i = strtoformat(plugindb, format);
1477 return supports_video(format_i);
1481 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
1483 int format_i = strtoformat(plugindb, format);
1485 return supports_audio(format_i);
1490 int File::supports_video(int format)
1492 //printf("File::supports_video %d\n", format);
1497 case FILE_JPEG_LIST:
1507 case FILE_TIFF_LIST:
1516 int File::supports_audio(int format)
1537 const char* File::get_tag(int format)
1541 case FILE_AC3: return "ac3";
1542 case FILE_AIFF: return "aif";
1543 case FILE_AMPEG: return "mp3";
1544 case FILE_AU: return "au";
1545 case FILE_RAWDV: return "dv";
1546 case FILE_DB: return "db";
1547 case FILE_EXR: return "exr";
1548 case FILE_EXR_LIST: return "exr";
1549 case FILE_FLAC: return "flac";
1550 case FILE_JPEG: return "jpg";
1551 case FILE_JPEG_LIST: return "jpg";
1552 case FILE_OGG: return "ogg";
1553 case FILE_PCM: return "pcm";
1554 case FILE_PNG: return "png";
1555 case FILE_PNG_LIST: return "png";
1556 case FILE_TGA: return "tga";
1557 case FILE_TGA_LIST: return "tga";
1558 case FILE_TIFF: return "tif";
1559 case FILE_TIFF_LIST: return "tif";
1560 case FILE_VMPEG: return "m2v";
1561 case FILE_VORBIS: return "ogg";
1562 case FILE_WAV: return "wav";
1563 case FILE_FFMPEG: return "ffmpg";
1568 const char* File::get_prefix(int format)
1571 case FILE_PCM: return "PCM";
1572 case FILE_WAV: return "WAV";
1573 case FILE_PNG: return "PNG";
1574 case FILE_JPEG: return "JPEG";
1575 case FILE_TIFF: return "TIFF";
1576 case FILE_GIF: return "GIF";
1577 case FILE_JPEG_LIST: return "JPEG_LIST";
1578 case FILE_AU: return "AU";
1579 case FILE_AIFF: return "AIFF";
1580 case FILE_SND: return "SND";
1581 case FILE_TGA_LIST: return "TGA_LIST";
1582 case FILE_TGA: return "TGA";
1583 case FILE_MPEG: return "MPEG";
1584 case FILE_AMPEG: return "AMPEG";
1585 case FILE_VMPEG: return "VMPEG";
1586 case FILE_RAWDV: return "RAWDV";
1587 case FILE_TIFF_LIST: return "TIFF_LIST";
1588 case FILE_PNG_LIST: return "PNG_LIST";
1589 case FILE_AC3: return "AC3";
1590 case FILE_EXR: return "EXR";
1591 case FILE_EXR_LIST: return "EXR_LIST";
1592 case FILE_CR2: return "CR2";
1593 case FILE_OGG: return "OGG";
1594 case FILE_VORBIS: return "VORBIS";
1595 case FILE_FLAC: return "FLAC";
1596 case FILE_FFMPEG: return "FFMPEG";
1597 case FILE_SCENE: return "SCENE";
1598 case FILE_CR2_LIST: return "CR2_LIST";
1599 case FILE_GIF_LIST: return "GIF_LIST";
1600 case FILE_DB: return "DB";
1602 return _("UNKNOWN");
1606 PackagingEngine *File::new_packaging_engine(Asset *asset)
1608 PackagingEngine *result;
1609 switch (asset->format)
1612 result = (PackagingEngine*)new PackagingEngineOGG();
1615 result = (PackagingEngine*) new PackagingEngineDefault();
1623 int File::record_fd()
1625 return file ? file->record_fd() : -1;
1629 void File::get_exe_path(char *result, char *bnp)
1631 // Get executable path, basename
1632 int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1635 char *ptr = strrchr(result, '/');
1636 if( ptr ) *ptr++ = 0; else ptr = result;
1637 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1645 void File::getenv_path(char *result, const char *path)
1647 char *rp = result, *ep = rp + BCTEXTLEN-1;
1648 const char *cp = path;
1649 // any env var can be used here to expand a path as:
1650 // "path...$id...": and id is a word as alpha,alnum...
1651 // expands as "path...getenv(id)..."
1652 // CIN_PATH, CIN_LIB are set in main.C,
1653 for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1654 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1655 const char *bp = cp; char *p = rp;
1656 while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1658 const char *envp = getenv(rp); // read env value
1659 if( !envp ) continue;
1660 while( *envp && rp < ep ) *rp++ = *envp++;
1661 cp = bp; // subst id=value
1669 void File::setenv_path(char *result, const char *var, const char *path)
1671 char *env = getenv(var);
1673 char env_path[BCTEXTLEN];
1674 getenv_path(env_path, path);
1675 sprintf(result, "%s=%s", var, env_path);
1679 char File::cinexe_path[BCTEXTLEN];
1680 char File::cinpkg_path[BCTEXTLEN];
1681 char File::cindat_path[BCTEXTLEN];
1682 char File::cinlib_path[BCTEXTLEN];
1683 char File::cincfg_path[BCTEXTLEN];
1684 char File::cinplg_path[BCTEXTLEN];
1685 char File::cinlad_path[BCTEXTLEN];
1686 char File::cinlcl_path[BCTEXTLEN];
1687 char File::cinbwr_path[BCTEXTLEN];
1689 void File::init_cin_path()
1691 char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1692 // these values are advertised for forks/shell scripts
1693 get_exe_path(env_path, env_pkg);
1694 snprintf(cinexe_path, sizeof(cinexe_path), "CIN_PATH=%s", env_path);
1695 putenv(cinexe_path);
1696 snprintf(cinpkg_path, sizeof(cinpkg_path), "CIN_PKG=%s", env_pkg);
1697 putenv(cinpkg_path);
1699 setenv_path(cindat_path, "CIN_DAT", CINDAT_DIR);
1700 setenv_path(cinlib_path, "CIN_LIB", CINLIB_DIR);
1701 setenv_path(cincfg_path, "CIN_CONFIG", CONFIG_DIR);
1702 setenv_path(cinplg_path, "CIN_PLUGIN", PLUGIN_DIR);
1703 setenv_path(cinlad_path, "CIN_LADSPA", LADSPA_DIR);
1704 setenv_path(cinlcl_path, "CIN_LOCALE", LOCALE_DIR);
1705 setenv_path(cinbwr_path, "CIN_BROWSER", CIN_BROWSER);