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