update libaom via Andrew + 0006 patch LIBZMPEG
[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
662 // Set extra writing parameters to mandatory settings.
663         if( wr ) {
664                 if( this->asset->dither ) file->set_dither();
665         }
666
667         if( rd ) {
668 // one frame image file, not brender, no specific length
669                 if( !this->asset->audio_data && this->asset->use_header &&
670                     this->asset->video_data && !this->asset->single_frame &&
671                     this->asset->video_length >= 0 && this->asset->video_length <= 1 ) {
672                         this->asset->single_frame = 1;
673                         this->asset->video_length = -1;
674                 }
675         }
676
677 // Synchronize header parameters
678         asset->copy_from(this->asset, 1);
679 //asset->dump();
680
681         if( debug ) printf("File::open_file %d file=%p\n", __LINE__, file);
682 // sleep(1);
683
684         return FILE_OK;
685 }
686
687 void File::delete_temp_samples_buffer()
688 {
689
690         if( temp_samples_buffer ) {
691                 for( int j = 0; j < audio_ring_buffers; j++ ) {
692                         for( int i = 0; i < asset->channels; i++ ) {
693                                 delete temp_samples_buffer[j][i];
694                         }
695                         delete [] temp_samples_buffer[j];
696                 }
697
698                 delete [] temp_samples_buffer;
699                 temp_samples_buffer = 0;
700                 audio_ring_buffers = 0;
701         }
702 }
703
704 void File::delete_temp_frame_buffer()
705 {
706
707         if( temp_frame_buffer ) {
708                 for( int k = 0; k < video_ring_buffers; k++ ) {
709                         for( int i = 0; i < asset->layers; i++ ) {
710                                 for( int j = 0; j < video_buffer_size; j++ ) {
711                                         delete temp_frame_buffer[k][i][j];
712                                 }
713                                 delete [] temp_frame_buffer[k][i];
714                         }
715                         delete [] temp_frame_buffer[k];
716                 }
717
718                 delete [] temp_frame_buffer;
719                 temp_frame_buffer = 0;
720                 video_ring_buffers = 0;
721                 video_buffer_size = 0;
722         }
723 }
724
725 int File::close_file(int ignore_thread)
726 {
727         if( !ignore_thread ) {
728                 stop_audio_thread();
729                 stop_video_thread();
730         }
731         if( file ) {
732 // The file's asset is a copy of the argument passed to open_file so the
733 // user must copy lengths from the file's asset.
734                 if( asset && wr ) {
735                         asset->audio_length = current_sample;
736                         asset->video_length = current_frame;
737                 }
738
739                 file->close_file();
740                 delete file;
741         }
742
743         delete_temp_samples_buffer();
744         delete_temp_frame_buffer();
745         reset_parameters();
746         return 0;
747 }
748
749 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
750 {
751         return !file ? -1 : file->get_index(index_file, progress_bar);
752 }
753
754 int File::start_audio_thread(int buffer_size, int ring_buffers)
755 {
756         this->audio_ring_buffers = ring_buffers;
757
758
759         if( !audio_thread ) {
760                 audio_thread = new FileThread(this, 1, 0);
761                 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
762         }
763         return 0;
764 }
765
766 int File::start_video_thread(int buffer_size, int color_model,
767                 int ring_buffers, int compressed)
768 {
769         this->video_ring_buffers = ring_buffers;
770         this->video_buffer_size = buffer_size;
771
772         if( !video_thread ) {
773                 video_thread = new FileThread(this, 0, 1);
774                 video_thread->start_writing(buffer_size, color_model,
775                                 ring_buffers, compressed);
776         }
777         return 0;
778 }
779
780 int File::start_video_decode_thread()
781 {
782 // Currently, CR2 is the only one which won't work asynchronously, so
783 // we're not using a virtual function yet.
784         if( !video_thread /* && asset->format != FILE_CR2 */ ) {
785                 video_thread = new FileThread(this, 0, 1);
786                 video_thread->start_reading();
787                 use_cache = 0;
788         }
789         return 0;
790 }
791
792 int File::stop_audio_thread()
793 {
794         if( audio_thread ) {
795                 audio_thread->stop_writing();
796                 delete audio_thread;
797                 audio_thread = 0;
798         }
799         return 0;
800 }
801
802 int File::stop_video_thread()
803 {
804         if( video_thread ) {
805                 video_thread->stop_reading();
806                 video_thread->stop_writing();
807                 delete video_thread;
808                 video_thread = 0;
809         }
810         return 0;
811 }
812
813 FileThread* File::get_video_thread()
814 {
815         return video_thread;
816 }
817
818 int File::set_channel(int channel)
819 {
820         if( file && channel < asset->channels ) {
821                 current_channel = channel;
822                 return 0;
823         }
824         else
825                 return 1;
826 }
827
828 int File::get_channel()
829 {
830         return current_channel;
831 }
832
833 // if no>=0, sets new program
834 //  returns current program
835 int File::set_program(int no)
836 {
837         int result = file ? file->set_program(no) : current_program;
838         current_program = no < 0 ? result : no;
839         return result;
840 }
841
842 int File::get_cell_time(int no, double &time)
843 {
844         return file ? file->get_cell_time(no, time) : -1;
845 }
846
847 int File::get_system_time(int64_t &tm)
848 {
849         return file ? file->get_system_time(tm) : -1;
850 }
851
852 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
853 {
854         return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
855 }
856
857 int File::get_video_pid(int track)
858 {
859         return file ? file->get_video_pid(track) : -1;
860 }
861
862
863
864 int File::get_video_info(int track, int &pid, double &framerate,
865                 int &width, int &height, char *title)
866 {
867         return !file ? -1 :
868                  file->get_video_info(track, pid, framerate, width, height, title);
869 }
870
871 int File::select_video_stream(Asset *asset, int vstream)
872 {
873         return !file ? -1 :
874                  file->select_video_stream(asset, vstream);
875 }
876
877 int File::select_audio_stream(Asset *asset, int astream)
878 {
879         return !file ? -1 :
880                  file->select_audio_stream(asset, astream);
881 }
882
883
884 int File::get_thumbnail(int stream,
885         int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
886 {
887         return file->get_thumbnail(stream, position, thumbnail, ww, hh);
888 }
889
890 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
891 {
892         return file->set_skimming(track, skim, fn, vp);
893 }
894
895 int File::skim_video(int track, void *vp, skim_fn fn)
896 {
897         return file->skim_video(track, vp, fn);
898 }
899
900
901 int File::set_layer(int layer, int is_thread)
902 {
903         if( file && layer < asset->layers ) {
904                 if( !is_thread && video_thread ) {
905                         video_thread->set_layer(layer);
906                 }
907                 else {
908                         current_layer = layer;
909                 }
910                 return 0;
911         }
912         else
913                 return 1;
914 }
915
916 int64_t File::get_audio_length()
917 {
918         int64_t result = asset->audio_length;
919         int64_t base_samplerate = -1;
920         if( result > 0 ) {
921                 if( base_samplerate > 0 )
922                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
923                 else
924                         return result;
925         }
926         else
927                 return -1;
928 }
929
930 int64_t File::get_video_length()
931 {
932         int64_t result = asset->video_length;
933         float base_framerate = -1;
934         if( result > 0 ) {
935                 if( base_framerate > 0 )
936                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
937                 else
938                         return result;
939         }
940         else
941                 return -1;  // infinity
942 }
943
944
945 int64_t File::get_video_position()
946 {
947         float base_framerate = -1;
948         if( base_framerate > 0 )
949                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
950         else
951                 return current_frame;
952 }
953
954 int64_t File::get_audio_position()
955 {
956 //      int64_t base_samplerate = -1;
957 //      if( base_samplerate > 0 )
958 //      {
959 //              if( normalized_sample_rate == base_samplerate )
960 //                      return normalized_sample;
961 //              else
962 //                      return (int64_t)((double)current_sample /
963 //                              asset->sample_rate *
964 //                              base_samplerate +
965 //                              0.5);
966 //      }
967 //      else
968                 return current_sample;
969 }
970
971
972
973 int File::set_audio_position(int64_t position)
974 {
975         int result = 0;
976
977         if( !file ) return 1;
978
979 #define REPOSITION(x, y) \
980         (labs((x) - (y)) > 1)
981
982         float base_samplerate = asset->sample_rate;
983         current_sample = normalized_sample = position;
984
985 // printf("File::set_audio_position %d normalized_sample=%ld\n",
986 // __LINE__,
987 // normalized_sample);
988                 result = file->set_audio_position(current_sample);
989
990                 if( result )
991                         printf("File::set_audio_position position=%jd"
992                                 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
993                                 position, base_samplerate, asset, asset->sample_rate);
994
995 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
996
997         return result;
998 }
999
1000 int File::set_video_position(int64_t position,
1001         int is_thread)
1002 {
1003         int result = 0;
1004         if( !file ) return 0;
1005
1006 // Convert to file's rate
1007 //      if( base_framerate > 0 )
1008 //              position = (int64_t)((double)position /
1009 //                      base_framerate *
1010 //                      asset->frame_rate +
1011 //                      0.5);
1012
1013
1014         if( video_thread && !is_thread ) {
1015 // Call thread.  Thread calls this again to set the file state.
1016                 video_thread->set_video_position(position);
1017         }
1018         else
1019         if( current_frame != position ) {
1020                 if( file ) {
1021                         current_frame = position;
1022                         result = file->set_video_position(current_frame);
1023                 }
1024         }
1025
1026         return result;
1027 }
1028
1029 // No resampling here.
1030 int File::write_samples(Samples **buffer, int64_t len)
1031 {
1032         int result = 1;
1033
1034         if( file ) {
1035                 write_lock->lock("File::write_samples");
1036
1037 // Convert to arrays for backwards compatability
1038                 double *temp[asset->channels];
1039                 for( int i = 0; i < asset->channels; i++ ) {
1040                         temp[i] = buffer[i]->get_data();
1041                 }
1042
1043                 result = file->write_samples(temp, len);
1044                 current_sample += len;
1045                 normalized_sample += len;
1046                 asset->audio_length += len;
1047                 write_lock->unlock();
1048         }
1049         return result;
1050 }
1051
1052
1053
1054
1055
1056 // Can't put any cmodel abstraction here because the filebase couldn't be
1057 // parallel.
1058 int File::write_frames(VFrame ***frames, int len)
1059 {
1060 //printf("File::write_frames %d\n", __LINE__);
1061 //PRINT_TRACE
1062 // Store the counters in temps so the filebase can choose to overwrite them.
1063         int result;
1064         int current_frame_temp = current_frame;
1065         int video_length_temp = asset->video_length;
1066
1067         write_lock->lock("File::write_frames");
1068
1069 //PRINT_TRACE
1070         result = file->write_frames(frames, len);
1071 //PRINT_TRACE
1072
1073         current_frame = current_frame_temp + len;
1074         asset->video_length = video_length_temp + len;
1075         write_lock->unlock();
1076 //PRINT_TRACE
1077         return result;
1078 }
1079
1080 // Only called by FileThread
1081 int File::write_compressed_frame(VFrame *buffer)
1082 {
1083         int result = 0;
1084         write_lock->lock("File::write_compressed_frame");
1085         result = file->write_compressed_frame(buffer);
1086         current_frame++;
1087         asset->video_length++;
1088         write_lock->unlock();
1089         return result;
1090 }
1091
1092
1093 int File::write_audio_buffer(int64_t len)
1094 {
1095         int result = 0;
1096         if( audio_thread ) {
1097                 result = audio_thread->write_buffer(len);
1098         }
1099         return result;
1100 }
1101
1102 int File::write_video_buffer(int64_t len)
1103 {
1104         int result = 0;
1105         if( video_thread ) {
1106                 result = video_thread->write_buffer(len);
1107         }
1108
1109         return result;
1110 }
1111
1112 Samples** File::get_audio_buffer()
1113 {
1114         if( audio_thread ) return audio_thread->get_audio_buffer();
1115         return 0;
1116 }
1117
1118 VFrame*** File::get_video_buffer()
1119 {
1120         if( video_thread ) {
1121                 VFrame*** result = video_thread->get_video_buffer();
1122
1123                 return result;
1124         }
1125
1126         return 0;
1127 }
1128
1129
1130 int File::read_samples(Samples *samples, int64_t len)
1131 {
1132 // Never try to read more samples than exist in the file
1133         if( asset->audio_length >= 0 && current_sample + len > asset->audio_length ) {
1134                 len = asset->audio_length - current_sample;
1135         }
1136         if( len <= 0 ) return 0;
1137
1138         int result = 0;
1139         const int debug = 0;
1140         if( debug ) PRINT_TRACE
1141
1142         if( debug ) PRINT_TRACE
1143
1144         double *buffer = samples->get_data();
1145
1146         int64_t base_samplerate = asset->sample_rate;
1147
1148         if( file ) {
1149 // Resample recursively calls this with the asset sample rate
1150                 if( base_samplerate == 0 ) base_samplerate = asset->sample_rate;
1151
1152                 if( debug ) PRINT_TRACE
1153                 result = file->read_samples(buffer, len);
1154
1155                 if( debug ) PRINT_TRACE
1156                 current_sample += len;
1157
1158                 normalized_sample += len;
1159         }
1160         if( debug ) PRINT_TRACE
1161
1162         return result;
1163 }
1164
1165 int File::read_frame(VFrame *frame, int is_thread)
1166 {
1167         const int debug = 0;
1168 //printf("File::read_frame pos=%jd cache=%d 1frame=%d\n",
1169 // current_frame, use_cache, asset->single_frame);
1170         if( video_thread && !is_thread ) return video_thread->read_frame(frame);
1171         if( !file ) return 1;
1172         if( debug ) PRINT_TRACE
1173         int result = 0;
1174         int supported_colormodel = colormodel_supported(frame->get_color_model());
1175         int do_read = 1;
1176 // if reverse playback reading, use_cache is -1
1177         int cache_active = asset->single_frame ? 1 : use_cache;
1178         int64_t cache_position = !asset->single_frame ? current_frame : -1;
1179
1180 // Test cache
1181         if( cache_active ) {
1182                 if( get_cache_frame(frame, cache_position) ) {
1183                         do_read = 0;
1184                 }
1185                 else if( cache_active < 0 && frame_cache->cache_items() > 0 ) {
1186 // reverse reading and cache miss, clear cache for new readahead
1187                         purge_cache();
1188                 }
1189         }
1190 // Need to read
1191         if( do_read ) {
1192                 VFrame *vframe = frame;
1193                 if( frame->get_color_model() != BC_COMPRESSED &&
1194                     (supported_colormodel != frame->get_color_model() ||
1195                         (!file->can_scale_input() &&
1196                                 (frame->get_w() != asset->width ||
1197                                  frame->get_h() != asset->height))) ) {
1198                         if( temp_frame ) {
1199                                 if( !temp_frame->params_match(asset->width, asset->height,
1200                                                 supported_colormodel) ) {
1201                                         delete temp_frame;  temp_frame = 0;
1202                                 }
1203                         }
1204                         if( !temp_frame ) {
1205                                 temp_frame = new VFrame(asset->width, asset->height,
1206                                                 supported_colormodel, 0);
1207                                 temp_frame->clear_frame();
1208                         }
1209
1210                         temp_frame->copy_stacks(frame);
1211                         vframe = temp_frame;
1212                 }
1213                 result = file->read_frame(vframe);
1214                 if( !result ) {
1215                         if( frame != vframe )
1216                                 frame->transfer_from(vframe);
1217                         if( cache_active > 0 )
1218                                 put_cache_frame(frame, cache_position, 1);
1219                 }
1220                 else if( frame->get_status() > 0 )
1221                         frame->set_status(-1);
1222 //for( int i = 0; i < 100 * 1000; i++ ) ((float*)frame->get_rows()[0])[i] = 1.0;
1223         }
1224
1225         if( result && !current_frame )
1226                 frame->clear_frame();
1227
1228 //printf("File::read_frame %d\n", __LINE__);
1229
1230         if( do_read ) current_frame++;
1231         if( debug ) PRINT_TRACE
1232         return 0;
1233 }
1234
1235 int File::can_copy_from(Asset *asset, int64_t position, int output_w, int output_h)
1236 {
1237         if( asset && file ) {
1238                 return asset->width == output_w &&
1239                         asset->height == output_h &&
1240                         file->can_copy_from(asset, position);
1241         }
1242         return 0;
1243 }
1244
1245 // Fill in queries about formats when adding formats here.
1246
1247
1248 int File::strtoformat(const char *format)
1249 {
1250         if( !strcasecmp(format, _(AC3_NAME)) ) return FILE_AC3;
1251         if( !strcasecmp(format, _(SCENE_NAME)) ) return FILE_SCENE;
1252         if( !strcasecmp(format, _(WAV_NAME)) ) return FILE_WAV;
1253         if( !strcasecmp(format, _(PCM_NAME)) ) return FILE_PCM;
1254         if( !strcasecmp(format, _(AU_NAME)) ) return FILE_AU;
1255         if( !strcasecmp(format, _(AIFF_NAME)) ) return FILE_AIFF;
1256         if( !strcasecmp(format, _(SND_NAME)) ) return FILE_SND;
1257         if( !strcasecmp(format, _(PNG_NAME)) ) return FILE_PNG;
1258         if( !strcasecmp(format, _(PNG_LIST_NAME)) ) return FILE_PNG_LIST;
1259         if( !strcasecmp(format, _(PPM_NAME)) ) return FILE_PPM;
1260         if( !strcasecmp(format, _(PPM_LIST_NAME)) ) return FILE_PPM_LIST;
1261         if( !strcasecmp(format, _(TIFF_NAME)) ) return FILE_TIFF;
1262         if( !strcasecmp(format, _(TIFF_LIST_NAME)) ) return FILE_TIFF_LIST;
1263         if( !strcasecmp(format, _(JPEG_NAME)) ) return FILE_JPEG;
1264         if( !strcasecmp(format, _(JPEG_LIST_NAME)) ) return FILE_JPEG_LIST;
1265         if( !strcasecmp(format, _(EXR_NAME)) ) return FILE_EXR;
1266         if( !strcasecmp(format, _(EXR_LIST_NAME)) ) return FILE_EXR_LIST;
1267         if( !strcasecmp(format, _(FLAC_NAME)) ) return FILE_FLAC;
1268         if( !strcasecmp(format, _(GIF_NAME)) ) return FILE_GIF;
1269         if( !strcasecmp(format, _(GIF_LIST_NAME)) ) return FILE_GIF_LIST;
1270         if( !strcasecmp(format, _(CR2_NAME)) ) return FILE_CR2;
1271         if( !strcasecmp(format, _(CR2_LIST_NAME)) ) return FILE_CR2_LIST;
1272         if( !strcasecmp(format, _(MPEG_NAME)) ) return FILE_MPEG;
1273         if( !strcasecmp(format, _(AMPEG_NAME)) ) return FILE_AMPEG;
1274         if( !strcasecmp(format, _(VMPEG_NAME)) ) return FILE_VMPEG;
1275         if( !strcasecmp(format, _(TGA_NAME)) ) return FILE_TGA;
1276         if( !strcasecmp(format, _(TGA_LIST_NAME)) ) return FILE_TGA_LIST;
1277         if( !strcasecmp(format, _(OGG_NAME)) ) return FILE_OGG;
1278         if( !strcasecmp(format, _(VORBIS_NAME)) ) return FILE_VORBIS;
1279         if( !strcasecmp(format, _(RAWDV_NAME)) ) return FILE_RAWDV;
1280         if( !strcasecmp(format, _(FFMPEG_NAME)) ) return FILE_FFMPEG;
1281         if( !strcasecmp(format, _(DBASE_NAME)) ) return FILE_DB;
1282         if( !strcasecmp(format, _(REF_NAME)) ) return FILE_REF;
1283
1284         return 0;
1285 }
1286
1287
1288 const char* File::formattostr(int format)
1289 {
1290         switch( format ) {
1291         case FILE_SCENE:        return _(SCENE_NAME);
1292         case FILE_AC3:          return _(AC3_NAME);
1293         case FILE_WAV:          return _(WAV_NAME);
1294         case FILE_PCM:          return _(PCM_NAME);
1295         case FILE_AU:           return _(AU_NAME);
1296         case FILE_AIFF:         return _(AIFF_NAME);
1297         case FILE_SND:          return _(SND_NAME);
1298         case FILE_PNG:          return _(PNG_NAME);
1299         case FILE_PNG_LIST:     return _(PNG_LIST_NAME);
1300         case FILE_PPM:          return _(PPM_NAME);
1301         case FILE_PPM_LIST:     return _(PPM_LIST_NAME);
1302         case FILE_JPEG:         return _(JPEG_NAME);
1303         case FILE_JPEG_LIST:    return _(JPEG_LIST_NAME);
1304         case FILE_CR2:          return _(CR2_NAME);
1305         case FILE_CR2_LIST:     return _(CR2_LIST_NAME);
1306         case FILE_FLAC:         return _(FLAC_NAME);
1307         case FILE_GIF:          return _(GIF_NAME);
1308         case FILE_GIF_LIST:     return _(GIF_LIST_NAME);
1309         case FILE_EXR:          return _(EXR_NAME);
1310         case FILE_EXR_LIST:     return _(EXR_LIST_NAME);
1311 #ifdef HAVE_LIBZMPEG
1312         case FILE_MPEG:         return _(MPEG_NAME);
1313 #endif
1314         case FILE_AMPEG:        return _(AMPEG_NAME);
1315         case FILE_VMPEG:        return _(VMPEG_NAME);
1316         case FILE_TGA:          return _(TGA_NAME);
1317         case FILE_TGA_LIST:     return _(TGA_LIST_NAME);
1318         case FILE_TIFF:         return _(TIFF_NAME);
1319         case FILE_TIFF_LIST:    return _(TIFF_LIST_NAME);
1320         case FILE_OGG:          return _(OGG_NAME);
1321         case FILE_VORBIS:       return _(VORBIS_NAME);
1322         case FILE_RAWDV:        return _(RAWDV_NAME);
1323         case FILE_FFMPEG:       return _(FFMPEG_NAME);
1324         case FILE_DB:           return _(DBASE_NAME);
1325         case FILE_REF:          return _(REF_NAME);
1326         }
1327         return _("Unknown");
1328 }
1329
1330 int File::strtobits(const char *bits)
1331 {
1332         if( !strcasecmp(bits, _(NAME_8BIT)) ) return BITSLINEAR8;
1333         if( !strcasecmp(bits, _(NAME_16BIT)) ) return BITSLINEAR16;
1334         if( !strcasecmp(bits, _(NAME_24BIT)) ) return BITSLINEAR24;
1335         if( !strcasecmp(bits, _(NAME_32BIT)) ) return BITSLINEAR32;
1336         if( !strcasecmp(bits, _(NAME_ULAW)) ) return BITSULAW;
1337         if( !strcasecmp(bits, _(NAME_ADPCM)) ) return BITS_ADPCM;
1338         if( !strcasecmp(bits, _(NAME_FLOAT)) ) return BITSFLOAT;
1339         return BITSLINEAR16;
1340 }
1341
1342 const char* File::bitstostr(int bits)
1343 {
1344 //printf("File::bitstostr\n");
1345         switch( bits ) {
1346         case BITSLINEAR8:       return (NAME_8BIT);
1347         case BITSLINEAR16:      return (NAME_16BIT);
1348         case BITSLINEAR24:      return (NAME_24BIT);
1349         case BITSLINEAR32:      return (NAME_32BIT);
1350         case BITSULAW:          return (NAME_ULAW);
1351         case BITS_ADPCM:        return (NAME_ADPCM);
1352         case BITSFLOAT:         return (NAME_FLOAT);
1353         }
1354         return _("Unknown");
1355 }
1356
1357
1358
1359 int File::str_to_byteorder(const char *string)
1360 {
1361         if( !strcasecmp(string, _("Lo Hi")) ) return 1;
1362         return 0;
1363 }
1364
1365 const char* File::byteorder_to_str(int byte_order)
1366 {
1367         if( byte_order ) return _("Lo Hi");
1368         return _("Hi Lo");
1369 }
1370
1371 int File::bytes_per_sample(int bits)
1372 {
1373         switch( bits ) {
1374         case BITSLINEAR8:       return 1;
1375         case BITSLINEAR16:      return 2;
1376         case BITSLINEAR24:      return 3;
1377         case BITSLINEAR32:      return 4;
1378         case BITSULAW:          return 1;
1379         }
1380         return 1;
1381 }
1382
1383
1384 int File::get_best_colormodel(int driver, int vstream)
1385 {
1386         return file ? file->get_best_colormodel(driver, vstream) :
1387                 get_best_colormodel(asset, driver);
1388 }
1389
1390 int File::get_best_colormodel(Asset *asset, int driver)
1391 {
1392         switch( asset->format ) {
1393 #ifdef HAVE_DV
1394         case FILE_RAWDV:        return FileDV::get_best_colormodel(asset, driver);
1395 #endif
1396 #ifdef HAVE_LIBZMPEG
1397         case FILE_MPEG:         return FileMPEG::get_best_colormodel(asset, driver);
1398 #endif
1399         case FILE_JPEG:
1400         case FILE_JPEG_LIST:    return FileJPEG::get_best_colormodel(asset, driver);
1401 #ifdef HAVE_OPENEXR
1402         case FILE_EXR:
1403         case FILE_EXR_LIST:     return FileEXR::get_best_colormodel(asset, driver);
1404 #endif
1405         case FILE_PNG:
1406         case FILE_PNG_LIST:     return FilePNG::get_best_colormodel(asset, driver);
1407         case FILE_PPM:
1408         case FILE_PPM_LIST:     return FilePPM::get_best_colormodel(asset, driver);
1409         case FILE_TGA:
1410         case FILE_TGA_LIST:     return FileTGA::get_best_colormodel(asset, driver);
1411         case FILE_CR2:
1412         case FILE_CR2_LIST:     return FileCR2::get_best_colormodel(asset, driver);
1413 #ifdef HAVE_COMMERCIAL
1414         case FILE_DB:           return FileDB::get_best_colormodel(asset, driver);
1415 #endif
1416         case FILE_FFMPEG:       return FileFFMPEG::get_best_colormodel(asset, driver);
1417         case FILE_REF:          return FileREF::get_best_colormodel(asset, driver);
1418         }
1419
1420         return BC_RGB888;
1421 }
1422
1423
1424 int File::colormodel_supported(int colormodel)
1425 {
1426         if( file )
1427                 return file->colormodel_supported(colormodel);
1428
1429         return BC_RGB888;
1430 }
1431
1432
1433 int64_t File::file_memory_usage()
1434 {
1435         return file ? file->base_memory_usage() : 0;
1436 }
1437
1438 int64_t File::get_memory_usage()
1439 {
1440         int64_t result = 0;
1441
1442         result += file_memory_usage();
1443         if( temp_frame ) result += temp_frame->get_data_size();
1444         result += frame_cache->get_memory_usage();
1445         if( video_thread ) result += video_thread->get_memory_usage();
1446
1447         if( result < MIN_CACHEITEM_SIZE ) result = MIN_CACHEITEM_SIZE;
1448         return result;
1449 }
1450
1451
1452 int File::renders_video(int format)
1453 {
1454         switch( format ) {
1455         case FILE_OGG:
1456         case FILE_JPEG:
1457         case FILE_JPEG_LIST:
1458         case FILE_CR2:
1459         case FILE_CR2_LIST:
1460         case FILE_EXR:
1461         case FILE_EXR_LIST:
1462         case FILE_GIF:
1463         case FILE_GIF_LIST:
1464         case FILE_PNG:
1465         case FILE_PNG_LIST:
1466         case FILE_PPM:
1467         case FILE_PPM_LIST:
1468         case FILE_TGA:
1469         case FILE_TGA_LIST:
1470         case FILE_TIFF:
1471         case FILE_TIFF_LIST:
1472         case FILE_VMPEG:
1473         case FILE_RAWDV:
1474         case FILE_FFMPEG:
1475                 return 1;
1476         }
1477         return 0;
1478 }
1479 int File::renders_video(Asset *asset)
1480 {
1481         return asset->format == FILE_FFMPEG ?
1482                 FFMPEG::renders_video(asset->fformat) :
1483                 renders_video(asset->format);
1484 }
1485
1486 int File::renders_audio(int format)
1487 {
1488         switch( format ) {
1489         case FILE_AC3:
1490         case FILE_FLAC:
1491         case FILE_PCM:
1492         case FILE_WAV:
1493         case FILE_OGG:
1494         case FILE_VORBIS:
1495         case FILE_AMPEG:
1496         case FILE_AU:
1497         case FILE_AIFF:
1498         case FILE_SND:
1499         case FILE_RAWDV:
1500         case FILE_FFMPEG:
1501                 return 1;
1502         }
1503         return 0;
1504 }
1505 int File::renders_audio(Asset *asset)
1506 {
1507         return asset->format == FILE_FFMPEG ?
1508                 FFMPEG::renders_audio(asset->fformat) :
1509                 renders_audio(asset->format);
1510 }
1511
1512 int File::is_image_render(int format)
1513 {
1514         switch( format ) {
1515         case FILE_EXR:
1516         case FILE_JPEG:
1517         case FILE_PNG:
1518         case FILE_PPM:
1519         case FILE_TGA:
1520         case FILE_TIFF:
1521                 return 1;
1522         }
1523
1524         return 0;
1525 }
1526
1527 const char* File::get_tag(int format)
1528 {
1529         switch( format ) {
1530         case FILE_AC3:          return "ac3";
1531         case FILE_AIFF:         return "aif";
1532         case FILE_AMPEG:        return "mp3";
1533         case FILE_AU:           return "au";
1534         case FILE_RAWDV:        return "dv";
1535         case FILE_DB:           return "db";
1536         case FILE_EXR:          return "exr";
1537         case FILE_EXR_LIST:     return "exrs";
1538         case FILE_FLAC:         return "flac";
1539         case FILE_JPEG:         return "jpg";
1540         case FILE_JPEG_LIST:    return "jpgs";
1541         case FILE_OGG:          return "ogg";
1542         case FILE_GIF:          return "gif";
1543         case FILE_GIF_LIST:     return "gifs";
1544         case FILE_PCM:          return "pcm";
1545         case FILE_PNG:          return "png";
1546         case FILE_PNG_LIST:     return "pngs";
1547         case FILE_PPM:          return "ppm";
1548         case FILE_PPM_LIST:     return "ppms";
1549         case FILE_TGA:          return "tga";
1550         case FILE_TGA_LIST:     return "tgas";
1551         case FILE_TIFF:         return "tif";
1552         case FILE_TIFF_LIST:    return "tifs";
1553         case FILE_VMPEG:        return "m2v";
1554         case FILE_VORBIS:       return "ogg";
1555         case FILE_WAV:          return "wav";
1556         case FILE_FFMPEG:       return "ffmpg";
1557         case FILE_REF:          return "ref";
1558         }
1559         return 0;
1560 }
1561
1562 const char* File::get_prefix(int format)
1563 {
1564         switch( format ) {
1565         case FILE_PCM:          return "PCM";
1566         case FILE_WAV:          return "WAV";
1567         case FILE_PNG:          return "PNG";
1568         case FILE_PPM:          return "PPM";
1569         case FILE_JPEG:         return "JPEG";
1570         case FILE_TIFF:         return "TIFF";
1571         case FILE_GIF:          return "GIF";
1572         case FILE_JPEG_LIST:    return "JPEG_LIST";
1573         case FILE_AU:           return "AU";
1574         case FILE_AIFF:         return "AIFF";
1575         case FILE_SND:          return "SND";
1576         case FILE_TGA_LIST:     return "TGA_LIST";
1577         case FILE_TGA:          return "TGA";
1578         case FILE_MPEG:         return "MPEG";
1579         case FILE_AMPEG:        return "AMPEG";
1580         case FILE_VMPEG:        return "VMPEG";
1581         case FILE_RAWDV:        return "RAWDV";
1582         case FILE_TIFF_LIST:    return "TIFF_LIST";
1583         case FILE_PNG_LIST:     return "PNG_LIST";
1584         case FILE_PPM_LIST:     return "PPM_LIST";
1585         case FILE_AC3:          return "AC3";
1586         case FILE_EXR:          return "EXR";
1587         case FILE_EXR_LIST:     return "EXR_LIST";
1588         case FILE_CR2:          return "CR2";
1589         case FILE_OGG:          return "OGG";
1590         case FILE_VORBIS:       return "VORBIS";
1591         case FILE_FLAC:         return "FLAC";
1592         case FILE_FFMPEG:       return "FFMPEG";
1593         case FILE_SCENE:        return "SCENE";
1594         case FILE_CR2_LIST:     return "CR2_LIST";
1595         case FILE_GIF_LIST:     return "GIF_LIST";
1596         case FILE_DB:           return "DB";
1597         case FILE_REF:          return "REF";
1598         }
1599         return _("UNKNOWN");
1600 }
1601
1602
1603 int File::record_fd()
1604 {
1605         return file ? file->record_fd() : -1;
1606 }
1607
1608
1609 void File::get_exe_path(char *result, char *bnp)
1610 {
1611 // Get executable path, basename
1612         int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1613         if( len >= 0 ) {
1614                 result[len] = 0;
1615                 char *ptr = strrchr(result, '/');
1616                 if( ptr ) *ptr++ = 0; else ptr = result;
1617                 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1618         }
1619         else {
1620                 *result = 0;
1621                 if( bnp ) *bnp = 0;
1622         }
1623 }
1624
1625 void File::getenv_path(char *result, const char *path)
1626 {
1627         char *rp = result, *ep = rp + BCTEXTLEN-1;
1628         const char *cp = path;
1629 // any env var can be used here to expand a path as:
1630 //   "path...$id...": and id is a word as alpha,alnum...
1631 //   expands as "path...getenv(id)..."
1632 // CIN_PATH, CIN_LIB are set in main.C,
1633         for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1634                 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1635                         const char *bp = cp;  char *p = rp;
1636                         while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1637                         *p = 0;
1638                         const char *envp = getenv(rp); // read env value
1639                         if( !envp ) continue;
1640                         while( *envp && rp < ep ) *rp++ = *envp++;
1641                         cp = bp;                 // subst id=value
1642                         continue;
1643                 }
1644                 *rp++ = ch;
1645         }
1646         *rp = 0;
1647 }
1648
1649 void File::setenv_path(const char *var, const char *path, int overwrite)
1650 {
1651         char env_path[BCTEXTLEN];
1652         getenv_path(env_path, path);
1653         setenv(var, env_path, overwrite);
1654 }
1655 /**
1656 * @brief Set various environment variables to pass constant values to
1657 *        forks, shell scripts, and other parts of CinGG.
1658 */
1659 void File::init_cin_env_vars()
1660 {
1661         char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN], build_name[32], dateTimeText[32];
1662         struct stat st;
1663         struct tm *dateTime;
1664         get_exe_path(env_path, env_pkg);
1665         setenv_path("CIN_PATH", env_path, 1);
1666         setenv_path("CIN_PKG", env_pkg, 1);
1667         setenv_path("CIN_DAT", CINDAT_DIR, 0);
1668         setenv_path("CIN_LIB", CINLIB_DIR, 0);
1669         setenv_path("CIN_CONFIG", CONFIG_DIR, 0);
1670         setenv_path("CIN_PLUGIN", PLUGIN_DIR, 0);
1671         setenv_path("CIN_LADSPA", LADSPA_DIR, 0);
1672         setenv_path("CIN_LOCALE", LOCALE_DIR, 0);
1673         setenv_path("CIN_BROWSER", CIN_BROWSER, 0);
1674 // Create env_var CINGG_BUILD for use when running as AppImage
1675         stat(env_path, &st);
1676         dateTime = gmtime(&st.st_mtime);
1677         strftime(dateTimeText, sizeof(dateTimeText), "%Y%m%d_%H%M%S", dateTime);
1678     snprintf(build_name, sizeof(build_name), "CINGG_%s", dateTimeText);
1679         build_name[sizeof(build_name) - 1] = 0;
1680         setenv_path("CINGG_BUILD", build_name, 1);
1681 }
1682