add transition caching with pref, rev read frame caching, cache lock tweaks, fix...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / file.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <ctype.h>
28 #include <limits.h>
29 // work arounds (centos)
30 #include <lzma.h>
31 #ifndef INT64_MAX
32 #define INT64_MAX 9223372036854775807LL
33 #endif
34
35 #include "asset.h"
36 #include "bchash.h"
37 #include "bcsignals.h"
38 #include "byteorder.h"
39 #include "cache.inc"
40 #include "condition.h"
41 #include "errorbox.h"
42 #include "fileac3.h"
43 #include "filebase.h"
44 #include "filecr2.h"
45 #include "filedb.h"
46 #include "filedv.h"
47 #include "fileexr.h"
48 #include "fileffmpeg.h"
49 #include "fileflac.h"
50 #include "filegif.h"
51 #include "file.h"
52 #include "filejpeg.h"
53 #include "filempeg.h"
54 #undef HAVE_STDLIB_H // automake conflict
55 #include "fileogg.h"
56 #include "filepng.h"
57 #include "fileppm.h"
58 #include "fileref.h"
59 #include "filescene.h"
60 #include "filesndfile.h"
61 #include "filetga.h"
62 #include "filethread.h"
63 #include "filetiff.h"
64 #include "filevorbis.h"
65 #include "filexml.h"
66 #include "formatwindow.h"
67 #include "formattools.h"
68 #include "framecache.h"
69 #include "language.h"
70 #include "mainprogress.inc"
71 #include "mutex.h"
72 #include "mwindow.h"
73 #include "packagingengine.h"
74 #include "pluginserver.h"
75 #include "preferences.h"
76 #include "probeprefs.h"
77 #include "samples.h"
78 #include "vframe.h"
79
80 #ifdef HAVE_OGG
81 //suppress noref warning
82 void *vorbis0_ov_callbacks[] = {
83  &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
84  &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
85 };
86 #endif
87
88 File::File()
89 {
90         cpus = 1;
91         asset = new Asset;
92         format_completion = new Condition(1, "File::format_completion");
93         write_lock = new Condition(1, "File::write_lock");
94         frame_cache = new FrameCache;
95         reset_parameters();
96 }
97
98 File::~File()
99 {
100         if( getting_options ) {
101                 if( format_window ) format_window->set_done(0);
102                 format_completion->lock("File::~File");
103                 format_completion->unlock();
104         }
105
106         if( temp_frame ) {
107                 delete temp_frame;
108         }
109
110         close_file(0);
111
112         asset->Garbage::remove_user();
113         delete format_completion;
114         delete write_lock;
115         delete frame_cache;
116 }
117
118 void File::reset_parameters()
119 {
120         file = 0;
121         audio_thread = 0;
122         video_thread = 0;
123         getting_options = 0;
124         format_window = 0;
125         temp_frame = 0;
126         current_sample = 0;
127         current_frame = 0;
128         current_channel = 0;
129         current_program = 0;
130         current_layer = 0;
131         normalized_sample = 0;
132         use_cache = 0;
133         preferences = 0;
134         playback_subtitle = -1;
135         interpolate_raw = 1;
136
137
138         temp_samples_buffer = 0;
139         temp_frame_buffer = 0;
140         current_frame_buffer = 0;
141         audio_ring_buffers = 0;
142         video_ring_buffers = 0;
143         video_buffer_size = 0;
144 }
145
146 int File::raise_window()
147 {
148         if( getting_options && format_window ) {
149                 format_window->raise_window();
150                 format_window->flush();
151         }
152         return 0;
153 }
154
155 void File::close_window()
156 {
157         if( getting_options ) {
158                 format_window->lock_window("File::close_window");
159                 format_window->set_done(1);
160                 format_window->unlock_window();
161                 getting_options = 0;
162         }
163 }
164
165 int File::get_options(FormatTools *format,
166         int audio_options, int video_options)
167 {
168         BC_WindowBase *parent_window = format->window;
169         Asset *asset = format->asset;
170         EDL *edl = format->mwindow ? format->mwindow->edl : 0;
171         format_window = 0;
172         getting_options = 1;
173         format_completion->lock("File::get_options");
174         switch( asset->format ) {
175 #ifdef HAVE_CIN_3RDPARTY
176         case FILE_AC3: FileAC3::get_parameters(parent_window, asset, format_window,
177                         audio_options, video_options, edl);
178                 break;
179 #endif
180 #ifdef HAVE_DV
181         case FILE_RAWDV:
182                 FileDV::get_parameters(parent_window, asset, format_window,
183                         audio_options, video_options, edl);
184                 break;
185 #endif
186         case FILE_PCM:
187         case FILE_WAV:
188         case FILE_AU:
189         case FILE_AIFF:
190         case FILE_SND:
191                 FileSndFile::get_parameters(parent_window, asset, format_window,
192                         audio_options, video_options, edl);
193                 break;
194         case FILE_FFMPEG:
195                 FileFFMPEG::get_parameters(parent_window, asset, format_window,
196                         audio_options, video_options, edl);
197                 break;
198 #ifdef HAVE_LIBZMPEG
199         case FILE_AMPEG:
200         case FILE_VMPEG:
201                 FileMPEG::get_parameters(parent_window, asset, format_window,
202                         audio_options, video_options, edl);
203                 break;
204 #endif
205         case FILE_JPEG:
206         case FILE_JPEG_LIST:
207                 FileJPEG::get_parameters(parent_window, asset, format_window,
208                         audio_options, video_options, edl);
209                 break;
210 #ifdef HAVE_OPENEXR
211         case FILE_EXR:
212         case FILE_EXR_LIST:
213                 FileEXR::get_parameters(parent_window, asset, format_window,
214                         audio_options, video_options, edl);
215                 break;
216 #endif
217         case FILE_FLAC:
218                 FileFLAC::get_parameters(parent_window, asset, format_window,
219                         audio_options, video_options, edl);
220                 break;
221         case FILE_PNG:
222         case FILE_PNG_LIST:
223                 FilePNG::get_parameters(parent_window, asset, format_window,
224                         audio_options, video_options, edl);
225                 break;
226         case FILE_PPM:
227         case FILE_PPM_LIST:
228                 FilePPM::get_parameters(parent_window, asset, format_window,
229                         audio_options, video_options, edl);
230                 break;
231         case FILE_TGA:
232         case FILE_TGA_LIST:
233                 FileTGA::get_parameters(parent_window, asset, format_window,
234                         audio_options, video_options, edl);
235                 break;
236         case FILE_TIFF:
237         case FILE_TIFF_LIST:
238                 FileTIFF::get_parameters(parent_window, asset, format_window,
239                         audio_options, video_options, edl);
240                 break;
241 #ifdef HAVE_OGG
242         case FILE_OGG:
243                 FileOGG::get_parameters(parent_window, asset, format_window,
244                         audio_options, video_options, edl);
245                 break;
246 #endif
247         default:
248                 break;
249         }
250
251         if( !format_window ) {
252                 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
253                         parent_window->get_abs_cursor_x(1),
254                         parent_window->get_abs_cursor_y(1));
255                 format_window = errorbox;
256                 getting_options = 1;
257                 if( audio_options )
258                         errorbox->create_objects(_("This format doesn't support audio."));
259                 else
260                 if( video_options )
261                         errorbox->create_objects(_("This format doesn't support video."));
262                 errorbox->run_window();
263                 delete errorbox;
264         }
265
266         getting_options = 0;
267         format_window = 0;
268         format_completion->unlock();
269         return 0;
270 }
271
272 int File::can_scale_input(Asset *asset)
273 {
274         switch( asset->format ) {
275         case FILE_MPEG:
276         case FILE_FFMPEG:
277                 return 1;
278         case FILE_EXR:
279         case FILE_JPEG:
280         case FILE_PNG:
281         case FILE_PPM:
282         case FILE_TGA:
283         case FILE_TIFF:
284                 if( asset->video_length < 0 )
285                         return 1;
286         }
287         return 0;
288 }
289
290
291 int File::set_processors(int cpus)   // Set the number of cpus for certain codecs
292 {
293         if( cpus > 8 )          // mpegvideo max_threads = 16, more causes errs
294                 cpus = 8;       //  8 cpus ought to decode just about anything
295 // Set all instances so gets work.
296         this->cpus = cpus;
297
298         return 0;
299 }
300
301 int File::set_preload(int64_t size)
302 {
303         this->playback_preload = size;
304         return 0;
305 }
306
307 void File::set_subtitle(int value)
308 {
309         this->playback_subtitle = value;
310         if( file ) file->set_subtitle(value);
311 }
312
313 void File::set_interpolate_raw(int value)
314 {
315         this->interpolate_raw = value;
316 }
317
318 void File::set_white_balance_raw(int value)
319 {
320         this->white_balance_raw = value;
321 }
322
323 void File::set_cache_frames(int value)
324 {
325 // caching only done locally
326         if( !video_thread )
327                 use_cache = value;
328 }
329
330 int File::purge_cache()
331 {
332 // caching only done locally
333         int result = 0;
334         if( frame_cache->cache_items() > 0 ) {
335                 frame_cache->remove_all();
336                 result = 1;
337         }
338         return result;
339 }
340
341 int File::delete_oldest()
342 {
343 // return the number of bytes freed
344         return frame_cache->delete_oldest();
345 }
346
347 // create cache frame using input vframe as template
348 VFrame *File::new_cache_frame(VFrame *vframe, int64_t position, int first_frame)
349 {
350         return frame_cache->new_cache_frame(position,
351                 vframe->get_w(), vframe->get_h(), vframe->get_color_model(),
352                 current_layer, asset->frame_rate, first_frame);
353 }
354
355 void File::put_cache_frame()
356 {
357         return frame_cache->put_cache_frame();
358 }
359
360 int File::get_use_cache()
361 {
362         return use_cache;
363 }
364
365
366 // file driver in order of probe precidence
367 //  can be reordered in preferences->interface
368 const char *File::default_probes[] = { 
369         "FFMPEG_Early",
370         "Scene", 
371         "DB",
372 #ifdef HAVE_DV 
373         "DV",   
374 #endif 
375         "SndFile",
376         "PNG",
377         "PPM",
378         "JPEG",
379         "GIF",
380 #ifdef HAVE_OPENEXR
381         "EXR",
382 #endif
383         "FLAC",
384         "CR2",
385         "TGA",
386         "TIFF",
387 #ifdef HAVE_OGG
388         "OGG",
389         "Vorbis",
390 #endif
391         "MPEG",
392         "EDL",
393         "FFMPEG_Late", 
394 }; 
395 const int File::nb_probes =
396         sizeof(File::default_probes)/sizeof(File::default_probes[0]); 
397
398
399 int File::probe()
400 {
401         FILE *fp = fopen(this->asset->path, "rb");
402         if( !fp ) return FILE_NOT_FOUND;
403         char data[16];
404         memset(data,0,sizeof(data));
405         int ret = fread(data, 1, 16, fp);
406         fclose(fp);
407         if( !ret ) return FILE_NOT_FOUND;
408
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);
415                         return FILE_OK;
416                 }
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);
421                         return FILE_OK;
422                 }
423 #endif
424                 if( !strcmp(pref->name,"Scene") ) { // scene file
425                         if( !FileScene::check_sig(this->asset, data)) continue;
426                         file = new FileScene(this->asset, this);
427                         return FILE_OK;
428                 }
429 #ifdef HAVE_DV
430                 if( !strcmp(pref->name,"DV") ) { // libdv
431                         if( !FileDV::check_sig(this->asset) ) continue;
432                         file = new FileDV(this->asset, this);
433                         return FILE_OK;
434                 }
435 #endif
436                 if( !strcmp(pref->name,"SndFile") ) { // libsndfile
437                         if( !FileSndFile::check_sig(this->asset) ) continue;
438                         file = new FileSndFile(this->asset, this);
439                         return FILE_OK;
440                 }
441                 if( !strcmp(pref->name,"PNG") ) { // PNG file
442                         if( !FilePNG::check_sig(this->asset) ) continue;
443                         file = new FilePNG(this->asset, this);
444                         return FILE_OK;
445                 }
446                 if( !strcmp(pref->name,"PPM") ) { // PPM file
447                         if( !FilePPM::check_sig(this->asset) ) continue;
448                         file = new FilePPM(this->asset, this);
449                         return FILE_OK;
450                 }
451                 if( !strcmp(pref->name,"JPEG") ) { // JPEG file
452                         if( !FileJPEG::check_sig(this->asset) ) continue;
453                         file = new FileJPEG(this->asset, this);
454                         return FILE_OK;
455                 }
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);
461                         else continue;
462                         return FILE_OK;
463                 }
464 #ifdef HAVE_OPENEXR
465                 if( !strcmp(pref->name,"EXR") ) { // EXR file
466                         if( !FileEXR::check_sig(this->asset, data)) continue;
467                         file = new FileEXR(this->asset, this);
468                         return FILE_OK;
469                 }
470 #endif
471                 if( !strcmp(pref->name,"FLAC") ) { // FLAC file
472                         if( !FileFLAC::check_sig(this->asset, data)) continue;
473                         file = new FileFLAC(this->asset, this);
474                         return FILE_OK;
475                 }
476                 if( !strcmp(pref->name,"CR2") ) { // CR2 file
477                         if( !FileCR2::check_sig(this->asset)) continue;
478                         file = new FileCR2(this->asset, this);
479                         return FILE_OK;
480                 }
481                 if( !strcmp(pref->name,"TGA") ) { // TGA file
482                         if( !FileTGA::check_sig(this->asset) ) continue;
483                         file = new FileTGA(this->asset, this);
484                         return FILE_OK;
485                 }
486                 if( !strcmp(pref->name,"TIFF") ) { // TIFF file
487                         if( !FileTIFF::check_sig(this->asset) ) continue;
488                         file = new FileTIFF(this->asset, this);
489                         return FILE_OK;
490                 }
491 #ifdef HAVE_OGG
492                 if( !strcmp(pref->name,"OGG") ) { // OGG file
493                         if( !FileOGG::check_sig(this->asset) ) continue;
494                         file = new FileOGG(this->asset, this);
495                         return FILE_OK;
496                 }
497                 if( !strcmp(pref->name,"Vorbis") ) { // VorbisFile file
498                         if( !FileVorbis::check_sig(this->asset) ) continue;
499                         file = new FileVorbis(this->asset, this);
500                         return FILE_OK;
501                 }
502 #endif
503 #ifdef HAVE_LIBZMPEG
504                 if( !strcmp(pref->name,"MPEG") ) { // MPEG file
505                         if( !FileMPEG::check_sig(this->asset) ) continue;
506                         file = new FileMPEG(this->asset, this);
507                         return FILE_OK;
508                 }
509 #endif
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) )
515                                 return FILE_IS_XML;
516                 }
517         }
518         return FILE_UNRECOGNIZED_CODEC; // maybe PCM file ?
519 }
520
521 int File::open_file(Preferences *preferences,
522         Asset *asset,
523         int rd,
524         int wr)
525 {
526         const int debug = 0;
527
528         this->preferences = preferences;
529         this->asset->copy_from(asset, 1);
530         this->rd = rd;
531         this->wr = wr;
532         file = 0;
533
534         if( debug ) printf("File::open_file %p %d\n", this, __LINE__);
535
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.
540         case FILE_UNKNOWN: {
541                 int ret = probe();
542                 if( ret != FILE_OK ) return ret;
543                 break; }
544 // format already determined
545 #ifdef HAVE_CIN_3RDPARTY
546         case FILE_AC3:
547                 file = new FileAC3(this->asset, this);
548                 break;
549 #endif
550         case FILE_SCENE:
551                 file = new FileScene(this->asset, this);
552                 break;
553
554         case FILE_FFMPEG:
555                 file = new FileFFMPEG(this->asset, this);
556                 break;
557
558         case FILE_PCM:
559         case FILE_WAV:
560         case FILE_AU:
561         case FILE_AIFF:
562         case FILE_SND:
563 //printf("File::open_file 1\n");
564                 file = new FileSndFile(this->asset, this);
565                 break;
566
567         case FILE_PNG:
568         case FILE_PNG_LIST:
569                 file = new FilePNG(this->asset, this);
570                 break;
571
572         case FILE_PPM:
573         case FILE_PPM_LIST:
574                 file = new FilePPM(this->asset, this);
575                 break;
576
577         case FILE_JPEG:
578         case FILE_JPEG_LIST:
579                 file = new FileJPEG(this->asset, this);
580                 break;
581
582         case FILE_GIF:
583                 file = new FileGIF(this->asset, this);
584                 break;
585         case FILE_GIF_LIST:
586                 file = new FileGIFList(this->asset, this);
587                 break;
588
589 #ifdef HAVE_OPENEXR
590         case FILE_EXR:
591         case FILE_EXR_LIST:
592                 file = new FileEXR(this->asset, this);
593                 break;
594 #endif
595         case FILE_FLAC:
596                 file = new FileFLAC(this->asset, this);
597                 break;
598
599         case FILE_CR2:
600         case FILE_CR2_LIST:
601                 file = new FileCR2(this->asset, this);
602                 break;
603
604         case FILE_TGA_LIST:
605         case FILE_TGA:
606                 file = new FileTGA(this->asset, this);
607                 break;
608
609         case FILE_TIFF:
610         case FILE_TIFF_LIST:
611                 file = new FileTIFF(this->asset, this);
612                 break;
613 #ifdef HAVE_COMMERCIAL
614         case FILE_DB:
615                 file = new FileDB(this->asset, this);
616                 break;
617 #endif
618
619 #ifdef HAVE_LIBZMPEG
620         case FILE_MPEG:
621         case FILE_AMPEG:
622         case FILE_VMPEG:
623                 file = new FileMPEG(this->asset, this);
624                 break;
625 #endif
626 #ifdef HAVE_OGG
627         case FILE_OGG:
628                 file = new FileOGG(this->asset, this);
629                 break;
630
631         case FILE_VORBIS:
632                 file = new FileVorbis(this->asset, this);
633                 break;
634 #endif
635 #ifdef HAVE_DV
636         case FILE_RAWDV:
637                 file = new FileDV(this->asset, this);
638                 break;
639 #endif
640         case FILE_REF:
641                 file = new FileREF(this->asset, this);
642                 break;
643 // try plugins
644         default:
645                 return 1;
646                 break;
647         }
648
649
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;
654         }
655
656
657
658 // Set extra writing parameters to mandatory settings.
659         if( wr ) {
660                 if( this->asset->dither ) file->set_dither();
661         }
662
663         if( rd ) {
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;
670                 }
671         }
672
673 // Synchronize header parameters
674         asset->copy_from(this->asset, 1);
675 //asset->dump();
676
677         if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
678 // sleep(1);
679
680         return FILE_OK;
681 }
682
683 void File::delete_temp_samples_buffer()
684 {
685
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];
690                         }
691                         delete [] temp_samples_buffer[j];
692                 }
693
694                 delete [] temp_samples_buffer;
695                 temp_samples_buffer = 0;
696                 audio_ring_buffers = 0;
697         }
698 }
699
700 void File::delete_temp_frame_buffer()
701 {
702
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];
708                                 }
709                                 delete [] temp_frame_buffer[k][i];
710                         }
711                         delete [] temp_frame_buffer[k];
712                 }
713
714                 delete [] temp_frame_buffer;
715                 temp_frame_buffer = 0;
716                 video_ring_buffers = 0;
717                 video_buffer_size = 0;
718         }
719 }
720
721 int File::close_file(int ignore_thread)
722 {
723         if( !ignore_thread ) {
724                 stop_audio_thread();
725                 stop_video_thread();
726         }
727         if( file ) {
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.
730                 if( asset && wr ) {
731                         asset->audio_length = current_sample;
732                         asset->video_length = current_frame;
733                 }
734
735                 file->close_file();
736                 delete file;
737         }
738
739         delete_temp_samples_buffer();
740         delete_temp_frame_buffer();
741         reset_parameters();
742         return 0;
743 }
744
745 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
746 {
747         return !file ? -1 : file->get_index(index_file, progress_bar);
748 }
749
750 int File::start_audio_thread(int buffer_size, int ring_buffers)
751 {
752         this->audio_ring_buffers = ring_buffers;
753
754
755         if( !audio_thread ) {
756                 audio_thread = new FileThread(this, 1, 0);
757                 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
758         }
759         return 0;
760 }
761
762 int File::start_video_thread(int buffer_size, int color_model,
763                 int ring_buffers, int compressed)
764 {
765         this->video_ring_buffers = ring_buffers;
766         this->video_buffer_size = buffer_size;
767
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);
772         }
773         return 0;
774 }
775
776 int File::start_video_decode_thread()
777 {
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();
783                 use_cache = 0;
784         }
785         return 0;
786 }
787
788 int File::stop_audio_thread()
789 {
790         if( audio_thread ) {
791                 audio_thread->stop_writing();
792                 delete audio_thread;
793                 audio_thread = 0;
794         }
795         return 0;
796 }
797
798 int File::stop_video_thread()
799 {
800         if( video_thread ) {
801                 video_thread->stop_reading();
802                 video_thread->stop_writing();
803                 delete video_thread;
804                 video_thread = 0;
805         }
806         return 0;
807 }
808
809 FileThread* File::get_video_thread()
810 {
811         return video_thread;
812 }
813
814 int File::set_channel(int channel)
815 {
816         if( file && channel < asset->channels ) {
817                 current_channel = channel;
818                 return 0;
819         }
820         else
821                 return 1;
822 }
823
824 int File::get_channel()
825 {
826         return current_channel;
827 }
828
829 // if no>=0, sets new program
830 //  returns current program
831 int File::set_program(int no)
832 {
833         int result = file ? file->set_program(no) : current_program;
834         current_program = no < 0 ? result : no;
835         return result;
836 }
837
838 int File::get_cell_time(int no, double &time)
839 {
840         return file ? file->get_cell_time(no, time) : -1;
841 }
842
843 int File::get_system_time(int64_t &tm)
844 {
845         return file ? file->get_system_time(tm) : -1;
846 }
847
848 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
849 {
850         return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
851 }
852
853 int File::get_video_pid(int track)
854 {
855         return file ? file->get_video_pid(track) : -1;
856 }
857
858
859
860 int File::get_video_info(int track, int &pid, double &framerate,
861                 int &width, int &height, char *title)
862 {
863         return !file ? -1 :
864                  file->get_video_info(track, pid, framerate, width, height, title);
865 }
866
867 int File::select_video_stream(Asset *asset, int vstream)
868 {
869         return !file ? -1 :
870                  file->select_video_stream(asset, vstream);
871 }
872
873 int File::select_audio_stream(Asset *asset, int astream)
874 {
875         return !file ? -1 :
876                  file->select_audio_stream(asset, astream);
877 }
878
879
880 int File::get_thumbnail(int stream,
881         int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
882 {
883         return file->get_thumbnail(stream, position, thumbnail, ww, hh);
884 }
885
886 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
887 {
888         return file->set_skimming(track, skim, fn, vp);
889 }
890
891 int File::skim_video(int track, void *vp, skim_fn fn)
892 {
893         return file->skim_video(track, vp, fn);
894 }
895
896
897 int File::set_layer(int layer, int is_thread)
898 {
899         if( file && layer < asset->layers ) {
900                 if( !is_thread && video_thread ) {
901                         video_thread->set_layer(layer);
902                 }
903                 else {
904                         current_layer = layer;
905                 }
906                 return 0;
907         }
908         else
909                 return 1;
910 }
911
912 int64_t File::get_audio_length()
913 {
914         int64_t result = asset->audio_length;
915         int64_t base_samplerate = -1;
916         if( result > 0 ) {
917                 if( base_samplerate > 0 )
918                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
919                 else
920                         return result;
921         }
922         else
923                 return -1;
924 }
925
926 int64_t File::get_video_length()
927 {
928         int64_t result = asset->video_length;
929         float base_framerate = -1;
930         if( result > 0 ) {
931                 if( base_framerate > 0 )
932                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
933                 else
934                         return result;
935         }
936         else
937                 return -1;  // infinity
938 }
939
940
941 int64_t File::get_video_position()
942 {
943         float base_framerate = -1;
944         if( base_framerate > 0 )
945                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
946         else
947                 return current_frame;
948 }
949
950 int64_t File::get_audio_position()
951 {
952 //      int64_t base_samplerate = -1;
953 //      if( base_samplerate > 0 )
954 //      {
955 //              if( normalized_sample_rate == base_samplerate )
956 //                      return normalized_sample;
957 //              else
958 //                      return (int64_t)((double)current_sample /
959 //                              asset->sample_rate *
960 //                              base_samplerate +
961 //                              0.5);
962 //      }
963 //      else
964                 return current_sample;
965 }
966
967
968
969 int File::set_audio_position(int64_t position)
970 {
971         int result = 0;
972
973         if( !file ) return 1;
974
975 #define REPOSITION(x, y) \
976         (labs((x) - (y)) > 1)
977
978         float base_samplerate = asset->sample_rate;
979         current_sample = normalized_sample = position;
980
981 // printf("File::set_audio_position %d normalized_sample=%ld\n",
982 // __LINE__,
983 // normalized_sample);
984                 result = file->set_audio_position(current_sample);
985
986                 if( result )
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);
990
991 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
992
993         return result;
994 }
995
996 int File::set_video_position(int64_t position,
997         int is_thread)
998 {
999         int result = 0;
1000         if( !file ) return 0;
1001
1002 // Convert to file's rate
1003 //      if( base_framerate > 0 )
1004 //              position = (int64_t)((double)position /
1005 //                      base_framerate *
1006 //                      asset->frame_rate +
1007 //                      0.5);
1008
1009
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);
1013         }
1014         else
1015         if( current_frame != position ) {
1016                 if( file ) {
1017                         current_frame = position;
1018                         result = file->set_video_position(current_frame);
1019                 }
1020         }
1021
1022         return result;
1023 }
1024
1025 // No resampling here.
1026 int File::write_samples(Samples **buffer, int64_t len)
1027 {
1028         int result = 1;
1029
1030         if( file ) {
1031                 write_lock->lock("File::write_samples");
1032
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();
1037                 }
1038
1039                 result = file->write_samples(temp, len);
1040                 current_sample += len;
1041                 normalized_sample += len;
1042                 asset->audio_length += len;
1043                 write_lock->unlock();
1044         }
1045         return result;
1046 }
1047
1048
1049
1050
1051
1052 // Can't put any cmodel abstraction here because the filebase couldn't be
1053 // parallel.
1054 int File::write_frames(VFrame ***frames, int len)
1055 {
1056 //printf("File::write_frames %d\n", __LINE__);
1057 //PRINT_TRACE
1058 // Store the counters in temps so the filebase can choose to overwrite them.
1059         int result;
1060         int current_frame_temp = current_frame;
1061         int video_length_temp = asset->video_length;
1062
1063         write_lock->lock("File::write_frames");
1064
1065 //PRINT_TRACE
1066         result = file->write_frames(frames, len);
1067 //PRINT_TRACE
1068
1069         current_frame = current_frame_temp + len;
1070         asset->video_length = video_length_temp + len;
1071         write_lock->unlock();
1072 //PRINT_TRACE
1073         return result;
1074 }
1075
1076 // Only called by FileThread
1077 int File::write_compressed_frame(VFrame *buffer)
1078 {
1079         int result = 0;
1080         write_lock->lock("File::write_compressed_frame");
1081         result = file->write_compressed_frame(buffer);
1082         current_frame++;
1083         asset->video_length++;
1084         write_lock->unlock();
1085         return result;
1086 }
1087
1088
1089 int File::write_audio_buffer(int64_t len)
1090 {
1091         int result = 0;
1092         if( audio_thread ) {
1093                 result = audio_thread->write_buffer(len);
1094         }
1095         return result;
1096 }
1097
1098 int File::write_video_buffer(int64_t len)
1099 {
1100         int result = 0;
1101         if( video_thread ) {
1102                 result = video_thread->write_buffer(len);
1103         }
1104
1105         return result;
1106 }
1107
1108 Samples** File::get_audio_buffer()
1109 {
1110         if( audio_thread ) return audio_thread->get_audio_buffer();
1111         return 0;
1112 }
1113
1114 VFrame*** File::get_video_buffer()
1115 {
1116         if( video_thread ) {
1117                 VFrame*** result = video_thread->get_video_buffer();
1118
1119                 return result;
1120         }
1121
1122         return 0;
1123 }
1124
1125
1126 int File::read_samples(Samples *samples, int64_t len)
1127 {
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;
1131         }
1132         if( len <= 0 ) return 0;
1133
1134         int result = 0;
1135         const int debug = 0;
1136         if( debug ) PRINT_TRACE
1137
1138         if( debug ) PRINT_TRACE
1139
1140         double *buffer = samples->get_data();
1141
1142         int64_t base_samplerate = asset->sample_rate;
1143
1144         if( file ) {
1145 // Resample recursively calls this with the asset sample rate
1146                 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1147
1148                 if( debug ) PRINT_TRACE
1149                 result = file->read_samples(buffer, len);
1150
1151                 if( debug ) PRINT_TRACE
1152                 current_sample += len;
1153
1154                 normalized_sample += len;
1155         }
1156         if( debug ) PRINT_TRACE
1157
1158         return result;
1159 }
1160
1161 int File::read_frame(VFrame *frame, int is_thread)
1162 {
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
1169         int result = 0;
1170         int supported_colormodel = colormodel_supported(frame->get_color_model());
1171         int advance_position = 1;
1172         int cache_active = use_cache || asset->single_frame ? 1 : 0;
1173         int64_t cache_position = !asset->single_frame ? current_frame : -1;
1174
1175 // Test cache
1176         if( cache_active && frame_cache->get_frame(frame, cache_position,
1177                         current_layer, asset->frame_rate) ) {
1178 // Can't advance position if cache used.
1179 //printf("File::read_frame %d\n", __LINE__);
1180                 advance_position = 0;
1181         }
1182 // Need temp
1183         else if( frame->get_color_model() != BC_COMPRESSED &&
1184                 (supported_colormodel != frame->get_color_model() ||
1185                 (!file->can_scale_input() &&
1186                         (frame->get_w() != asset->width ||
1187                          frame->get_h() != asset->height))) ) {
1188
1189 //                      printf("File::read_frame %d\n", __LINE__);
1190 // Can't advance position here because it needs to be added to cache
1191                 if( temp_frame ) {
1192                         if( !temp_frame->params_match(asset->width, asset->height, supported_colormodel) ) {
1193                                 delete temp_frame;
1194                                 temp_frame = 0;
1195                         }
1196                 }
1197
1198                 if( !temp_frame ) {
1199                         temp_frame = new VFrame(asset->width, asset->height, supported_colormodel, 0);
1200                         temp_frame->clear_frame();
1201                 }
1202
1203 //                      printf("File::read_frame %d\n", __LINE__);
1204                 temp_frame->copy_stacks(frame);
1205                 result = file->read_frame(temp_frame);
1206                 if( !result )
1207                         frame->transfer_from(temp_frame);
1208                 else if( result && frame->get_status() > 0 )
1209                         frame->set_status(-1);
1210 //printf("File::read_frame %d\n", __LINE__);
1211         }
1212         else {
1213 // Can't advance position here because it needs to be added to cache
1214 //printf("File::read_frame %d\n", __LINE__);
1215                 result = file->read_frame(frame);
1216                 if( result && 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;
1219         }
1220
1221         if( result && !current_frame )
1222                 frame->clear_frame();
1223
1224         if( cache_active && advance_position && frame->get_status() > 0 )
1225                 frame_cache->put_frame(frame, cache_position,
1226                         current_layer, asset->frame_rate, 1, 0);
1227 //printf("File::read_frame %d\n", __LINE__);
1228
1229         if( advance_position ) current_frame++;
1230         if( debug ) PRINT_TRACE
1231         return 0;
1232 }
1233
1234 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1235 {
1236         if( asset && file ) {
1237                 return asset->width == output_w &&
1238                         asset->height == output_h &&
1239                         file->can_copy_from(asset, position);
1240         }
1241         return 0;
1242 }
1243
1244 // Fill in queries about formats when adding formats here.
1245
1246
1247 int File::strtoformat(const char *format)
1248 {
1249         if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1250         if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1251         if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1252         if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1253         if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1254         if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1255         if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1256         if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1257         if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1258         if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1259         if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1260         if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1261         if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1262         if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1263         if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1264         if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1265         if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1266         if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1267         if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1268         if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1269         if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1270         if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1271         if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1272         if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1273         if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1274         if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1275         if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1276         if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1277         if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1278         if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1279         if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1280         if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1281         if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1282
1283         return 0;
1284 }
1285
1286
1287 const char* File::formattostr(int format)
1288 {
1289         switch( format ) {
1290         case FILE_SCENE:        return _(SCENE_NAME);
1291         case FILE_AC3:          return _(AC3_NAME);
1292         case FILE_WAV:          return _(WAV_NAME);
1293         case FILE_PCM:          return _(PCM_NAME);
1294         case FILE_AU:           return _(AU_NAME);
1295         case FILE_AIFF:         return _(AIFF_NAME);
1296         case FILE_SND:          return _(SND_NAME);
1297         case FILE_PNG:          return _(PNG_NAME);
1298         case FILE_PNG_LIST:     return _(PNG_LIST_NAME);
1299         case FILE_PPM:          return _(PPM_NAME);
1300         case FILE_PPM_LIST:     return _(PPM_LIST_NAME);
1301         case FILE_JPEG:         return _(JPEG_NAME);
1302         case FILE_JPEG_LIST:    return _(JPEG_LIST_NAME);
1303         case FILE_CR2:          return _(CR2_NAME);
1304         case FILE_CR2_LIST:     return _(CR2_LIST_NAME);
1305         case FILE_FLAC:         return _(FLAC_NAME);
1306         case FILE_GIF:          return _(GIF_NAME);
1307         case FILE_GIF_LIST:     return _(GIF_LIST_NAME);
1308         case FILE_EXR:          return _(EXR_NAME);
1309         case FILE_EXR_LIST:     return _(EXR_LIST_NAME);
1310 #ifdef HAVE_LIBZMPEG
1311         case FILE_MPEG:         return _(MPEG_NAME);
1312 #endif
1313         case FILE_AMPEG:        return _(AMPEG_NAME);
1314         case FILE_VMPEG:        return _(VMPEG_NAME);
1315         case FILE_TGA:          return _(TGA_NAME);
1316         case FILE_TGA_LIST:     return _(TGA_LIST_NAME);
1317         case FILE_TIFF:         return _(TIFF_NAME);
1318         case FILE_TIFF_LIST:    return _(TIFF_LIST_NAME);
1319         case FILE_OGG:          return _(OGG_NAME);
1320         case FILE_VORBIS:       return _(VORBIS_NAME);
1321         case FILE_RAWDV:        return _(RAWDV_NAME);
1322         case FILE_FFMPEG:       return _(FFMPEG_NAME);
1323         case FILE_DB:           return _(DBASE_NAME);
1324         case FILE_REF:          return _(REF_NAME);
1325         }
1326         return _("Unknown");
1327 }
1328
1329 int File::strtobits(const char *bits)
1330 {
1331         if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1332         if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1333         if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1334         if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1335         if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1336         if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1337         if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1338         return BITSLINEAR16;
1339 }
1340
1341 const char* File::bitstostr(int bits)
1342 {
1343 //printf("File::bitstostr\n");
1344         switch( bits ) {
1345         case BITSLINEAR8:       return (NAME_8BIT);
1346         case BITSLINEAR16:      return (NAME_16BIT);
1347         case BITSLINEAR24:      return (NAME_24BIT);
1348         case BITSLINEAR32:      return (NAME_32BIT);
1349         case BITSULAW:          return (NAME_ULAW);
1350         case BITS_ADPCM:        return (NAME_ADPCM);
1351         case BITSFLOAT:         return (NAME_FLOAT);
1352         }
1353         return _("Unknown");
1354 }
1355
1356
1357
1358 int File::str_to_byteorder(const char *string)
1359 {
1360         if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1361         return 0;
1362 }
1363
1364 const char* File::byteorder_to_str(int byte_order)
1365 {
1366         if( byte_order ) return _("Lo Hi");
1367         return _("Hi Lo");
1368 }
1369
1370 int File::bytes_per_sample(int bits)
1371 {
1372         switch( bits ) {
1373         case BITSLINEAR8:       return 1;
1374         case BITSLINEAR16:      return 2;
1375         case BITSLINEAR24:      return 3;
1376         case BITSLINEAR32:      return 4;
1377         case BITSULAW:          return 1;
1378         }
1379         return 1;
1380 }
1381
1382
1383 int File::get_best_colormodel(int driver, int vstream)
1384 {
1385         return file ? file->get_best_colormodel(driver, vstream) :
1386                 get_best_colormodel(asset, driver);
1387 }
1388
1389 int File::get_best_colormodel(Asset *asset, int driver)
1390 {
1391         switch( asset->format ) {
1392 #ifdef HAVE_DV
1393         case FILE_RAWDV:        return FileDV::get_best_colormodel(asset, driver);
1394 #endif
1395 #ifdef HAVE_LIBZMPEG
1396         case FILE_MPEG:         return FileMPEG::get_best_colormodel(asset, driver);
1397 #endif
1398         case FILE_JPEG:
1399         case FILE_JPEG_LIST:    return FileJPEG::get_best_colormodel(asset, driver);
1400 #ifdef HAVE_OPENEXR
1401         case FILE_EXR:
1402         case FILE_EXR_LIST:     return FileEXR::get_best_colormodel(asset, driver);
1403 #endif
1404         case FILE_PNG:
1405         case FILE_PNG_LIST:     return FilePNG::get_best_colormodel(asset, driver);
1406         case FILE_PPM:
1407         case FILE_PPM_LIST:     return FilePPM::get_best_colormodel(asset, driver);
1408         case FILE_TGA:
1409         case FILE_TGA_LIST:     return FileTGA::get_best_colormodel(asset, driver);
1410         case FILE_CR2:
1411         case FILE_CR2_LIST:     return FileCR2::get_best_colormodel(asset, driver);
1412 #ifdef HAVE_COMMERCIAL
1413         case FILE_DB:           return FileDB::get_best_colormodel(asset, driver);
1414 #endif
1415         case FILE_FFMPEG:       return FileFFMPEG::get_best_colormodel(asset, driver);
1416         case FILE_REF:          return FileREF::get_best_colormodel(asset, driver);
1417         }
1418
1419         return BC_RGB888;
1420 }
1421
1422
1423 int File::colormodel_supported(int colormodel)
1424 {
1425         if( file )
1426                 return file->colormodel_supported(colormodel);
1427
1428         return BC_RGB888;
1429 }
1430
1431
1432 int64_t File::file_memory_usage()
1433 {
1434         return file ? file->base_memory_usage() : 0;
1435 }
1436
1437 int64_t File::get_memory_usage()
1438 {
1439         int64_t result = 0;
1440
1441         result += file_memory_usage();
1442         if( temp_frame ) result += temp_frame->get_data_size();
1443         result += frame_cache->get_memory_usage();
1444         if( video_thread ) result += video_thread->get_memory_usage();
1445
1446         if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1447         return result;
1448 }
1449
1450
1451 int File::renders_video(int format)
1452 {
1453         switch( format ) {
1454         case FILE_OGG:
1455         case FILE_JPEG:
1456         case FILE_JPEG_LIST:
1457         case FILE_CR2:
1458         case FILE_CR2_LIST:
1459         case FILE_EXR:
1460         case FILE_EXR_LIST:
1461         case FILE_GIF:
1462         case FILE_GIF_LIST:
1463         case FILE_PNG:
1464         case FILE_PNG_LIST:
1465         case FILE_PPM:
1466         case FILE_PPM_LIST:
1467         case FILE_TGA:
1468         case FILE_TGA_LIST:
1469         case FILE_TIFF:
1470         case FILE_TIFF_LIST:
1471         case FILE_VMPEG:
1472         case FILE_RAWDV:
1473         case FILE_FFMPEG:
1474                 return 1;
1475         }
1476         return 0;
1477 }
1478 int File::renders_video(Asset *asset)
1479 {
1480         return asset->format == FILE_FFMPEG ?
1481                 FFMPEG::renders_video(asset->fformat) :
1482                 renders_video(asset->format);
1483 }
1484
1485 int File::renders_audio(int format)
1486 {
1487         switch( format ) {
1488         case FILE_AC3:
1489         case FILE_FLAC:
1490         case FILE_PCM:
1491         case FILE_WAV:
1492         case FILE_OGG:
1493         case FILE_VORBIS:
1494         case FILE_AMPEG:
1495         case FILE_AU:
1496         case FILE_AIFF:
1497         case FILE_SND:
1498         case FILE_RAWDV:
1499         case FILE_FFMPEG:
1500                 return 1;
1501         }
1502         return 0;
1503 }
1504 int File::renders_audio(Asset *asset)
1505 {
1506         return asset->format == FILE_FFMPEG ?
1507                 FFMPEG::renders_audio(asset->fformat) :
1508                 renders_audio(asset->format);
1509 }
1510
1511 int File::is_image_render(int format)
1512 {
1513         switch( format ) {
1514         case FILE_EXR:
1515         case FILE_JPEG:
1516         case FILE_PNG:
1517         case FILE_PPM:
1518         case FILE_TGA:
1519         case FILE_TIFF:
1520                 return 1;
1521         }
1522
1523         return 0;
1524 }
1525
1526 const char* File::get_tag(int format)
1527 {
1528         switch( format ) {
1529         case FILE_AC3:          return "ac3";
1530         case FILE_AIFF:         return "aif";
1531         case FILE_AMPEG:        return "mp3";
1532         case FILE_AU:           return "au";
1533         case FILE_RAWDV:        return "dv";
1534         case FILE_DB:           return "db";
1535         case FILE_EXR:          return "exr";
1536         case FILE_EXR_LIST:     return "exrs";
1537         case FILE_FLAC:         return "flac";
1538         case FILE_JPEG:         return "jpg";
1539         case FILE_JPEG_LIST:    return "jpgs";
1540         case FILE_OGG:          return "ogg";
1541         case FILE_GIF:          return "gif";
1542         case FILE_GIF_LIST:     return "gifs";
1543         case FILE_PCM:          return "pcm";
1544         case FILE_PNG:          return "png";
1545         case FILE_PNG_LIST:     return "pngs";
1546         case FILE_PPM:          return "ppm";
1547         case FILE_PPM_LIST:     return "ppms";
1548         case FILE_TGA:          return "tga";
1549         case FILE_TGA_LIST:     return "tgas";
1550         case FILE_TIFF:         return "tif";
1551         case FILE_TIFF_LIST:    return "tifs";
1552         case FILE_VMPEG:        return "m2v";
1553         case FILE_VORBIS:       return "ogg";
1554         case FILE_WAV:          return "wav";
1555         case FILE_FFMPEG:       return "ffmpg";
1556         case FILE_REF:          return "ref";
1557         }
1558         return 0;
1559 }
1560
1561 const char* File::get_prefix(int format)
1562 {
1563         switch( format ) {
1564         case FILE_PCM:          return "PCM";
1565         case FILE_WAV:          return "WAV";
1566         case FILE_PNG:          return "PNG";
1567         case FILE_PPM:          return "PPM";
1568         case FILE_JPEG:         return "JPEG";
1569         case FILE_TIFF:         return "TIFF";
1570         case FILE_GIF:          return "GIF";
1571         case FILE_JPEG_LIST:    return "JPEG_LIST";
1572         case FILE_AU:           return "AU";
1573         case FILE_AIFF:         return "AIFF";
1574         case FILE_SND:          return "SND";
1575         case FILE_TGA_LIST:     return "TGA_LIST";
1576         case FILE_TGA:          return "TGA";
1577         case FILE_MPEG:         return "MPEG";
1578         case FILE_AMPEG:        return "AMPEG";
1579         case FILE_VMPEG:        return "VMPEG";
1580         case FILE_RAWDV:        return "RAWDV";
1581         case FILE_TIFF_LIST:    return "TIFF_LIST";
1582         case FILE_PNG_LIST:     return "PNG_LIST";
1583         case FILE_PPM_LIST:     return "PPM_LIST";
1584         case FILE_AC3:          return "AC3";
1585         case FILE_EXR:          return "EXR";
1586         case FILE_EXR_LIST:     return "EXR_LIST";
1587         case FILE_CR2:          return "CR2";
1588         case FILE_OGG:          return "OGG";
1589         case FILE_VORBIS:       return "VORBIS";
1590         case FILE_FLAC:         return "FLAC";
1591         case FILE_FFMPEG:       return "FFMPEG";
1592         case FILE_SCENE:        return "SCENE";
1593         case FILE_CR2_LIST:     return "CR2_LIST";
1594         case FILE_GIF_LIST:     return "GIF_LIST";
1595         case FILE_DB:           return "DB";
1596         case FILE_REF:          return "REF";
1597         }
1598         return _("UNKNOWN");
1599 }
1600
1601
1602 int File::record_fd()
1603 {
1604         return file ? file->record_fd() : -1;
1605 }
1606
1607
1608 void File::get_exe_path(char *result, char *bnp)
1609 {
1610 // Get executable path, basename
1611         int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1612         if( len >= 0 ) {
1613                 result[len] = 0;
1614                 char *ptr = strrchr(result, '/');
1615                 if( ptr ) *ptr++ = 0; else ptr = result;
1616                 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1617         }
1618         else {
1619                 *result = 0;
1620                 if( bnp ) *bnp = 0;
1621         }
1622 }
1623
1624 void File::getenv_path(char *result, const char *path)
1625 {
1626         char *rp = result, *ep = rp + BCTEXTLEN-1;
1627         const char *cp = path;
1628 // any env var can be used here to expand a path as:
1629 //   "path...$id...": and id is a word as alpha,alnum...
1630 //   expands as "path...getenv(id)..."
1631 // CIN_PATH, CIN_LIB are set in main.C,
1632         for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1633                 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1634                         const char *bp = cp;  char *p = rp;
1635                         while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1636                         *p = 0;
1637                         const char *envp = getenv(rp); // read env value
1638                         if( !envp ) continue;
1639                         while( *envp && rp < ep ) *rp++ = *envp++;
1640                         cp = bp;                 // subst id=value
1641                         continue;
1642                 }
1643                 *rp++ = ch;
1644         }
1645         *rp = 0;
1646 }
1647
1648 void File::setenv_path(const char *var, const char *path, int overwrite)
1649 {
1650         char env_path[BCTEXTLEN];
1651         getenv_path(env_path, path);
1652         setenv(var, env_path, overwrite);
1653 }
1654
1655 void File::init_cin_path()
1656 {
1657         char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1658 // these values are advertised for forks/shell scripts
1659         get_exe_path(env_path, env_pkg);
1660         setenv_path("CIN_PATH", env_path, 1);
1661         setenv_path("CIN_PKG", env_pkg, 1);
1662         setenv_path("CIN_DAT", CINDAT_DIR, 0);
1663         setenv_path("CIN_LIB", CINLIB_DIR, 0);
1664         setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1665         setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1666         setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1667         setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1668         setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1669 }
1670