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