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