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