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