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